lundump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. ** $Id: lundump.c,v 1.60 2006/02/16 15:53:49 lhf Exp $
  3. ** load precompiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <string.h>
  7. #define lundump_c
  8. #define LUA_CORE
  9. #include "lua.h"
  10. #include "ldebug.h"
  11. #include "ldo.h"
  12. #include "lfunc.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstring.h"
  16. #include "lundump.h"
  17. #include "lzio.h"
  18. typedef struct {
  19. lua_State* L;
  20. ZIO* Z;
  21. Mbuffer* b;
  22. const char* name;
  23. } LoadState;
  24. #ifdef LUAC_TRUST_BINARIES
  25. #define IF(c,s)
  26. #else
  27. #define IF(c,s) if (c) error(S,s)
  28. static void error(LoadState* S, const char* why)
  29. {
  30. luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
  31. luaD_throw(S->L,LUA_ERRSYNTAX);
  32. }
  33. #endif
  34. #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
  35. #define LoadByte(S) (lu_byte)LoadChar(S)
  36. #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
  37. #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
  38. static void LoadBlock(LoadState* S, void* b, size_t size)
  39. {
  40. size_t r=luaZ_read(S->Z,b,size);
  41. IF (r!=0, "unexpected end");
  42. }
  43. static int LoadChar(LoadState* S)
  44. {
  45. char x;
  46. LoadVar(S,x);
  47. return x;
  48. }
  49. static int LoadInt(LoadState* S)
  50. {
  51. int x;
  52. LoadVar(S,x);
  53. IF (x<0, "bad integer");
  54. return x;
  55. }
  56. static lua_Number LoadNumber(LoadState* S)
  57. {
  58. lua_Number x;
  59. LoadVar(S,x);
  60. return x;
  61. }
  62. static TString* LoadString(LoadState* S)
  63. {
  64. size_t size;
  65. LoadVar(S,size);
  66. if (size==0)
  67. return NULL;
  68. else
  69. {
  70. char* s=luaZ_openspace(S->L,S->b,size);
  71. LoadBlock(S,s,size);
  72. return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
  73. }
  74. }
  75. static void LoadCode(LoadState* S, Proto* f)
  76. {
  77. int n=LoadInt(S);
  78. f->code=luaM_newvector(S->L,n,Instruction);
  79. f->sizecode=n;
  80. LoadVector(S,f->code,n,sizeof(Instruction));
  81. }
  82. static Proto* LoadFunction(LoadState* S, TString* p);
  83. static void LoadConstants(LoadState* S, Proto* f)
  84. {
  85. int i,n;
  86. n=LoadInt(S);
  87. f->k=luaM_newvector(S->L,n,TValue);
  88. f->sizek=n;
  89. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  90. for (i=0; i<n; i++)
  91. {
  92. TValue* o=&f->k[i];
  93. int t=LoadChar(S);
  94. switch (t)
  95. {
  96. case LUA_TNIL:
  97. setnilvalue(o);
  98. break;
  99. case LUA_TBOOLEAN:
  100. setbvalue(o,LoadChar(S));
  101. break;
  102. case LUA_TNUMBER:
  103. setnvalue(o,LoadNumber(S));
  104. break;
  105. case LUA_TSTRING:
  106. setsvalue2n(S->L,o,LoadString(S));
  107. break;
  108. default:
  109. IF (1, "bad constant");
  110. break;
  111. }
  112. }
  113. n=LoadInt(S);
  114. f->p=luaM_newvector(S->L,n,Proto*);
  115. f->sizep=n;
  116. for (i=0; i<n; i++) f->p[i]=NULL;
  117. for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
  118. }
  119. static void LoadDebug(LoadState* S, Proto* f)
  120. {
  121. int i,n;
  122. n=LoadInt(S);
  123. f->lineinfo=luaM_newvector(S->L,n,int);
  124. f->sizelineinfo=n;
  125. LoadVector(S,f->lineinfo,n,sizeof(int));
  126. n=LoadInt(S);
  127. f->locvars=luaM_newvector(S->L,n,LocVar);
  128. f->sizelocvars=n;
  129. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  130. for (i=0; i<n; i++)
  131. {
  132. f->locvars[i].varname=LoadString(S);
  133. f->locvars[i].startpc=LoadInt(S);
  134. f->locvars[i].endpc=LoadInt(S);
  135. }
  136. n=LoadInt(S);
  137. f->upvalues=luaM_newvector(S->L,n,TString*);
  138. f->sizeupvalues=n;
  139. for (i=0; i<n; i++) f->upvalues[i]=NULL;
  140. for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
  141. }
  142. static Proto* LoadFunction(LoadState* S, TString* p)
  143. {
  144. Proto* f=luaF_newproto(S->L);
  145. setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
  146. f->source=LoadString(S); if (f->source==NULL) f->source=p;
  147. f->linedefined=LoadInt(S);
  148. f->lastlinedefined=LoadInt(S);
  149. f->nups=LoadByte(S);
  150. f->numparams=LoadByte(S);
  151. f->is_vararg=LoadByte(S);
  152. f->maxstacksize=LoadByte(S);
  153. LoadCode(S,f);
  154. LoadConstants(S,f);
  155. LoadDebug(S,f);
  156. IF (!luaG_checkcode(f), "bad code");
  157. S->L->top--;
  158. return f;
  159. }
  160. static void LoadHeader(LoadState* S)
  161. {
  162. char h[LUAC_HEADERSIZE];
  163. char s[LUAC_HEADERSIZE];
  164. luaU_header(h);
  165. LoadBlock(S,s,LUAC_HEADERSIZE);
  166. IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
  167. }
  168. /*
  169. ** load precompiled chunk
  170. */
  171. Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
  172. {
  173. LoadState S;
  174. if (*name=='@' || *name=='=')
  175. S.name=name+1;
  176. else if (*name==LUA_SIGNATURE[0])
  177. S.name="binary string";
  178. else
  179. S.name=name;
  180. S.L=L;
  181. S.Z=Z;
  182. S.b=buff;
  183. LoadHeader(&S);
  184. return LoadFunction(&S,luaS_newliteral(L,"=?"));
  185. }
  186. /*
  187. * make header
  188. */
  189. void luaU_header (char* h)
  190. {
  191. int x=1;
  192. memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
  193. h+=sizeof(LUA_SIGNATURE)-1;
  194. *h++=(char)LUAC_VERSION;
  195. *h++=(char)LUAC_FORMAT;
  196. *h++=(char)*(char*)&x; /* endianness */
  197. *h++=(char)sizeof(int);
  198. *h++=(char)sizeof(size_t);
  199. *h++=(char)sizeof(Instruction);
  200. *h++=(char)sizeof(lua_Number);
  201. *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
  202. }