lundump.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. ** $Id: lundump.c,v 2.23 2013/04/26 18:48:35 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) /* empty */
  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 lua_Integer LoadInteger(LoadState* S)
  60. {
  61. lua_Integer 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. TString* ts;
  74. char* s=luaZ_openspace(S->L,S->b,size);
  75. LoadBlock(S,s,size*sizeof(char));
  76. ts = luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
  77. nolocal(obj2gco(ts)); /* all strings here anchored in non-thread objects */
  78. return ts;
  79. }
  80. }
  81. static void LoadCode(LoadState* S, Proto* f)
  82. {
  83. int n=LoadInt(S);
  84. f->code=luaM_newvector(S->L,n,Instruction);
  85. f->sizecode=n;
  86. LoadVector(S,f->code,n,sizeof(Instruction));
  87. }
  88. static void LoadFunction(LoadState* S, Proto* f);
  89. static void LoadConstants(LoadState* S, Proto* f)
  90. {
  91. int i,n;
  92. n=LoadInt(S);
  93. f->k=luaM_newvector(S->L,n,TValue);
  94. f->sizek=n;
  95. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  96. for (i=0; i<n; i++)
  97. {
  98. TValue* o=&f->k[i];
  99. int t=LoadChar(S);
  100. switch (t)
  101. {
  102. case LUA_TNIL:
  103. setnilvalue(o);
  104. break;
  105. case LUA_TBOOLEAN:
  106. setbvalue(o,LoadChar(S));
  107. break;
  108. case LUA_TNUMFLT:
  109. setnvalue(o,LoadNumber(S));
  110. break;
  111. case LUA_TNUMINT:
  112. setivalue(o,LoadInteger(S));
  113. break;
  114. case LUA_TSHRSTR: case LUA_TLNGSTR:
  115. setsvalue2n(S->L,o,LoadString(S));
  116. break;
  117. default: lua_assert(0);
  118. }
  119. }
  120. n=LoadInt(S);
  121. f->p=luaM_newvector(S->L,n,Proto*);
  122. f->sizep=n;
  123. for (i=0; i<n; i++) f->p[i]=NULL;
  124. for (i=0; i<n; i++)
  125. {
  126. f->p[i]=luaF_newproto(S->L);
  127. LoadFunction(S,f->p[i]);
  128. }
  129. }
  130. static void LoadUpvalues(LoadState* S, Proto* f)
  131. {
  132. int i,n;
  133. n=LoadInt(S);
  134. f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
  135. f->sizeupvalues=n;
  136. for (i=0; i<n; i++) f->upvalues[i].name=NULL;
  137. for (i=0; i<n; i++)
  138. {
  139. f->upvalues[i].instack=LoadByte(S);
  140. f->upvalues[i].idx=LoadByte(S);
  141. }
  142. }
  143. static void LoadDebug(LoadState* S, Proto* f)
  144. {
  145. int i,n;
  146. f->source=LoadString(S);
  147. n=LoadInt(S);
  148. f->lineinfo=luaM_newvector(S->L,n,int);
  149. f->sizelineinfo=n;
  150. LoadVector(S,f->lineinfo,n,sizeof(int));
  151. n=LoadInt(S);
  152. f->locvars=luaM_newvector(S->L,n,LocVar);
  153. f->sizelocvars=n;
  154. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  155. for (i=0; i<n; i++)
  156. {
  157. f->locvars[i].varname=LoadString(S);
  158. f->locvars[i].startpc=LoadInt(S);
  159. f->locvars[i].endpc=LoadInt(S);
  160. }
  161. n=LoadInt(S);
  162. for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
  163. }
  164. static void LoadFunction(LoadState* S, Proto* f)
  165. {
  166. f->linedefined=LoadInt(S);
  167. f->lastlinedefined=LoadInt(S);
  168. f->numparams=LoadByte(S);
  169. f->is_vararg=LoadByte(S);
  170. f->maxstacksize=LoadByte(S);
  171. LoadCode(S,f);
  172. LoadConstants(S,f);
  173. LoadUpvalues(S,f);
  174. LoadDebug(S,f);
  175. }
  176. /* the code below must be consistent with the code in luaU_header */
  177. #define N0 LUAC_HEADERSIZE
  178. #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
  179. #define N2 N1+2
  180. #define N3 N2+6
  181. static void LoadHeader(LoadState* S)
  182. {
  183. lu_byte h[LUAC_HEADERSIZE];
  184. lu_byte s[LUAC_HEADERSIZE];
  185. luaU_header(h);
  186. memcpy(s,h,sizeof(char)); /* first char already read */
  187. LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
  188. if (memcmp(h,s,N0)==0) return;
  189. if (memcmp(h,s,N1)!=0) error(S,"not a");
  190. if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
  191. if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
  192. }
  193. /*
  194. ** load precompiled chunk
  195. */
  196. Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
  197. {
  198. LoadState S;
  199. Closure* cl;
  200. if (*name=='@' || *name=='=')
  201. S.name=name+1;
  202. else if (*name==LUA_SIGNATURE[0])
  203. S.name="binary string";
  204. else
  205. S.name=name;
  206. S.L=L;
  207. S.Z=Z;
  208. S.b=buff;
  209. LoadHeader(&S);
  210. cl=luaF_newLclosure(L,1);
  211. setclLvalue(L,L->top,cl); incr_top(L);
  212. cl->l.p=luaF_newproto(L);
  213. LoadFunction(&S,cl->l.p);
  214. if (cl->l.p->sizeupvalues != 1)
  215. {
  216. Proto* p=cl->l.p;
  217. cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
  218. cl->l.p=p;
  219. setclLvalue(L,L->top-1,cl);
  220. }
  221. luai_verifycode(L,buff,cl->l.p);
  222. return cl;
  223. }
  224. #define MYINT(s) (s[0]-'0')
  225. #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
  226. #define FORMAT 0 /* this is the official format */
  227. /*
  228. * make header for precompiled chunks
  229. * if you change the code below be sure to update LoadHeader and FORMAT above
  230. * and LUAC_HEADERSIZE in lundump.h
  231. */
  232. void luaU_header (lu_byte* h)
  233. {
  234. int x=1;
  235. memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
  236. h+=sizeof(LUA_SIGNATURE)-sizeof(char);
  237. *h++=cast_byte(VERSION);
  238. *h++=cast_byte(FORMAT);
  239. *h++=cast_byte(*(char*)&x); /* endianness */
  240. *h++=cast_byte(sizeof(int));
  241. *h++=cast_byte(sizeof(size_t));
  242. *h++=cast_byte(sizeof(Instruction));
  243. *h++=cast_byte(sizeof(lua_Number));
  244. *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
  245. memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
  246. }