lundump.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. ** $Id: lundump.c,v 2.11 2009/09/28 16:32:50 roberto Exp roberto $
  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)!=0);
  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 LoadUpvalues(LoadState* S, Proto* f)
  120. {
  121. int i,n;
  122. n=LoadInt(S);
  123. f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
  124. f->sizeupvalues=n;
  125. for (i=0; i<n; i++) f->upvalues[i].name=NULL;
  126. for (i=0; i<n; i++)
  127. {
  128. f->upvalues[i].instack=LoadChar(S);
  129. f->upvalues[i].idx=LoadChar(S);
  130. }
  131. }
  132. static void LoadDebug(LoadState* S, Proto* f)
  133. {
  134. int i,n;
  135. n=LoadInt(S);
  136. f->lineinfo=luaM_newvector(S->L,n,int);
  137. f->sizelineinfo=n;
  138. LoadVector(S,f->lineinfo,n,sizeof(int));
  139. n=LoadInt(S);
  140. f->locvars=luaM_newvector(S->L,n,LocVar);
  141. f->sizelocvars=n;
  142. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  143. for (i=0; i<n; i++)
  144. {
  145. f->locvars[i].varname=LoadString(S);
  146. f->locvars[i].startpc=LoadInt(S);
  147. f->locvars[i].endpc=LoadInt(S);
  148. }
  149. n=LoadInt(S);
  150. for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
  151. }
  152. static Proto* LoadFunction(LoadState* S, TString* p)
  153. {
  154. Proto* f;
  155. if (++G(S->L)->nCcalls > LUAI_MAXCCALLS) error(S, "function nest too deep");
  156. f=luaF_newproto(S->L);
  157. setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
  158. f->source=LoadString(S); if (f->source==NULL) f->source=p;
  159. f->linedefined=LoadInt(S);
  160. f->lastlinedefined=LoadInt(S);
  161. f->numparams=LoadByte(S);
  162. f->is_vararg=LoadByte(S);
  163. f->maxstacksize=LoadByte(S);
  164. f->envreg=LoadByte(S);
  165. LoadCode(S,f);
  166. LoadConstants(S,f);
  167. LoadUpvalues(S,f);
  168. LoadDebug(S,f);
  169. S->L->top--;
  170. G(S->L)->nCcalls--;
  171. return f;
  172. }
  173. static void LoadHeader(LoadState* S)
  174. {
  175. char h[LUAC_HEADERSIZE];
  176. char s[LUAC_HEADERSIZE];
  177. luaU_header(h);
  178. LoadBlock(S,s,LUAC_HEADERSIZE);
  179. IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
  180. }
  181. /*
  182. ** load precompiled chunk
  183. */
  184. Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
  185. {
  186. LoadState S;
  187. if (*name=='@' || *name=='=')
  188. S.name=name+1;
  189. else if (*name==LUA_SIGNATURE[0])
  190. S.name="binary string";
  191. else
  192. S.name=name;
  193. S.L=L;
  194. S.Z=Z;
  195. S.b=buff;
  196. LoadHeader(&S);
  197. return LoadFunction(&S,luaS_newliteral(L,"=?"));
  198. }
  199. /*
  200. * make header
  201. */
  202. void luaU_header (char* h)
  203. {
  204. int x=1;
  205. memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
  206. h+=sizeof(LUA_SIGNATURE)-1;
  207. *h++=(char)LUAC_VERSION;
  208. *h++=(char)LUAC_FORMAT;
  209. *h++=(char)*(char*)&x; /* endianness */
  210. *h++=(char)sizeof(int);
  211. *h++=(char)sizeof(size_t);
  212. *h++=(char)sizeof(Instruction);
  213. *h++=(char)sizeof(lua_Number);
  214. *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
  215. }