lundump.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ** $Id: lundump.c,v 2.20 2012/01/23 23:02:10 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. static l_noret error(LoadState* S, const char* why)
  25. {
  26. luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
  27. luaD_throw(S->L,LUA_ERRSYNTAX);
  28. }
  29. #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
  30. #define LoadByte(S) (lu_byte)LoadChar(S)
  31. #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
  32. #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
  33. #if !defined(luai_verifycode)
  34. #define luai_verifycode(L,b,f) (f)
  35. #endif
  36. static void LoadBlock(LoadState* S, void* b, size_t size)
  37. {
  38. if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
  39. }
  40. static int LoadChar(LoadState* S)
  41. {
  42. char x;
  43. LoadVar(S,x);
  44. return x;
  45. }
  46. static int LoadInt(LoadState* S)
  47. {
  48. int x;
  49. LoadVar(S,x);
  50. if (x<0) error(S,"corrupted");
  51. return x;
  52. }
  53. static lua_Number LoadNumber(LoadState* S)
  54. {
  55. lua_Number x;
  56. LoadVar(S,x);
  57. return x;
  58. }
  59. static TString* LoadString(LoadState* S)
  60. {
  61. size_t size;
  62. LoadVar(S,size);
  63. if (size==0)
  64. return NULL;
  65. else
  66. {
  67. char* s=luaZ_openspace(S->L,S->b,size);
  68. LoadBlock(S,s,size*sizeof(char));
  69. return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
  70. }
  71. }
  72. static void LoadCode(LoadState* S, Proto* f)
  73. {
  74. int n=LoadInt(S);
  75. f->code=luaM_newvector(S->L,n,Instruction);
  76. f->sizecode=n;
  77. LoadVector(S,f->code,n,sizeof(Instruction));
  78. }
  79. static Proto* LoadFunction(LoadState* S);
  80. static void LoadConstants(LoadState* S, Proto* f)
  81. {
  82. int i,n;
  83. n=LoadInt(S);
  84. f->k=luaM_newvector(S->L,n,TValue);
  85. f->sizek=n;
  86. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  87. for (i=0; i<n; i++)
  88. {
  89. TValue* o=&f->k[i];
  90. int t=LoadChar(S);
  91. switch (t)
  92. {
  93. case LUA_TNIL:
  94. setnilvalue(o);
  95. break;
  96. case LUA_TBOOLEAN:
  97. setbvalue(o,LoadChar(S));
  98. break;
  99. case LUA_TNUMBER:
  100. setnvalue(o,LoadNumber(S));
  101. break;
  102. case LUA_TSTRING:
  103. setsvalue2n(S->L,o,LoadString(S));
  104. break;
  105. default: lua_assert(0);
  106. }
  107. }
  108. n=LoadInt(S);
  109. f->p=luaM_newvector(S->L,n,Proto*);
  110. f->sizep=n;
  111. for (i=0; i<n; i++) f->p[i]=NULL;
  112. for (i=0; i<n; i++) f->p[i]=LoadFunction(S);
  113. }
  114. static void LoadUpvalues(LoadState* S, Proto* f)
  115. {
  116. int i,n;
  117. n=LoadInt(S);
  118. f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
  119. f->sizeupvalues=n;
  120. for (i=0; i<n; i++) f->upvalues[i].name=NULL;
  121. for (i=0; i<n; i++)
  122. {
  123. f->upvalues[i].instack=LoadByte(S);
  124. f->upvalues[i].idx=LoadByte(S);
  125. }
  126. }
  127. static void LoadDebug(LoadState* S, Proto* f)
  128. {
  129. int i,n;
  130. f->source=LoadString(S);
  131. n=LoadInt(S);
  132. f->lineinfo=luaM_newvector(S->L,n,int);
  133. f->sizelineinfo=n;
  134. LoadVector(S,f->lineinfo,n,sizeof(int));
  135. n=LoadInt(S);
  136. f->locvars=luaM_newvector(S->L,n,LocVar);
  137. f->sizelocvars=n;
  138. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  139. for (i=0; i<n; i++)
  140. {
  141. f->locvars[i].varname=LoadString(S);
  142. f->locvars[i].startpc=LoadInt(S);
  143. f->locvars[i].endpc=LoadInt(S);
  144. }
  145. n=LoadInt(S);
  146. for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
  147. }
  148. static Proto* LoadFunction(LoadState* S)
  149. {
  150. Proto* f=luaF_newproto(S->L);
  151. setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
  152. f->linedefined=LoadInt(S);
  153. f->lastlinedefined=LoadInt(S);
  154. f->numparams=LoadByte(S);
  155. f->is_vararg=LoadByte(S);
  156. f->maxstacksize=LoadByte(S);
  157. LoadCode(S,f);
  158. LoadConstants(S,f);
  159. LoadUpvalues(S,f);
  160. LoadDebug(S,f);
  161. S->L->top--;
  162. return f;
  163. }
  164. /* the code below must be consistent with the code in luaU_header */
  165. #define N0 LUAC_HEADERSIZE
  166. #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
  167. #define N2 N1+2
  168. #define N3 N2+6
  169. static void LoadHeader(LoadState* S)
  170. {
  171. lu_byte h[LUAC_HEADERSIZE];
  172. lu_byte s[LUAC_HEADERSIZE];
  173. luaU_header(h);
  174. memcpy(s,h,sizeof(char)); /* first char already read */
  175. LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
  176. if (memcmp(h,s,N0)==0) return;
  177. if (memcmp(h,s,N1)!=0) error(S,"not a");
  178. if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
  179. if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
  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 luai_verifycode(L,buff,LoadFunction(&S));
  198. }
  199. #define MYINT(s) (s[0]-'0')
  200. #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
  201. #define FORMAT 0 /* this is the official format */
  202. /*
  203. * make header for precompiled chunks
  204. * if you change the code below be sure to update LoadHeader and FORMAT above
  205. * and LUAC_HEADERSIZE in lundump.h
  206. */
  207. void luaU_header (lu_byte* h)
  208. {
  209. int x=1;
  210. memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
  211. h+=sizeof(LUA_SIGNATURE)-sizeof(char);
  212. *h++=cast_byte(VERSION);
  213. *h++=cast_byte(FORMAT);
  214. *h++=cast_byte(*(char*)&x); /* endianness */
  215. *h++=cast_byte(sizeof(int));
  216. *h++=cast_byte(sizeof(size_t));
  217. *h++=cast_byte(sizeof(Instruction));
  218. *h++=cast_byte(sizeof(lua_Number));
  219. *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
  220. memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
  221. }