lundump.c 4.5 KB

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