lundump.c 4.5 KB

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