lundump.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #define lundump_c
  7. #define LUA_CORE
  8. #define LUAC_CROSS_FILE
  9. #include "lua.h"
  10. #include C_HEADER_STRING
  11. #include "ldebug.h"
  12. #include "ldo.h"
  13. #include "lfunc.h"
  14. #include "lmem.h"
  15. #include "lobject.h"
  16. #include "lstring.h"
  17. #include "lundump.h"
  18. #include "lzio.h"
  19. typedef struct {
  20. lua_State* L;
  21. ZIO* Z;
  22. Mbuffer* b;
  23. const char* name;
  24. int swap;
  25. int numsize;
  26. int toflt;
  27. size_t total;
  28. } LoadState;
  29. #ifdef LUAC_TRUST_BINARIES
  30. #define IF(c,s)
  31. #define error(S,s)
  32. #else
  33. #define IF(c,s) if (c) error(S,s)
  34. static void error(LoadState* S, const char* why)
  35. {
  36. luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
  37. luaD_throw(S->L,LUA_ERRSYNTAX);
  38. }
  39. #endif
  40. #define LoadByte(S) (lu_byte)LoadChar(S)
  41. #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
  42. #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
  43. static void LoadBlock(LoadState* S, void* b, size_t size)
  44. {
  45. size_t r=luaZ_read(S->Z,b,size);
  46. IF (r!=0, "unexpected end");
  47. S->total+=size;
  48. }
  49. static void LoadMem (LoadState* S, void* b, int n, size_t size)
  50. {
  51. LoadBlock(S,b,n*size);
  52. if (S->swap && b)
  53. {
  54. char* p=(char*) b;
  55. char c;
  56. switch (size)
  57. {
  58. case 1:
  59. break;
  60. case 2:
  61. while (n--)
  62. {
  63. c=p[0]; p[0]=p[1]; p[1]=c;
  64. p+=2;
  65. }
  66. break;
  67. case 4:
  68. while (n--)
  69. {
  70. c=p[0]; p[0]=p[3]; p[3]=c;
  71. c=p[1]; p[1]=p[2]; p[2]=c;
  72. p+=4;
  73. }
  74. break;
  75. case 8:
  76. while (n--)
  77. {
  78. c=p[0]; p[0]=p[7]; p[7]=c;
  79. c=p[1]; p[1]=p[6]; p[6]=c;
  80. c=p[2]; p[2]=p[5]; p[5]=c;
  81. c=p[3]; p[3]=p[4]; p[4]=c;
  82. p+=8;
  83. }
  84. break;
  85. default:
  86. IF(1, "bad size");
  87. break;
  88. }
  89. }
  90. }
  91. static int LoadChar(LoadState* S)
  92. {
  93. char x;
  94. LoadVar(S,x);
  95. return x;
  96. }
  97. static void Align4(LoadState* S)
  98. {
  99. while(S->total&3)
  100. LoadChar(S);
  101. }
  102. static int LoadInt(LoadState* S)
  103. {
  104. int x;
  105. LoadVar(S,x);
  106. IF (x<0, "bad integer");
  107. return x;
  108. }
  109. static lua_Number LoadNumber(LoadState* S)
  110. {
  111. lua_Number x;
  112. if(S->toflt)
  113. {
  114. switch(S->numsize)
  115. {
  116. case 1: {
  117. int8_t y;
  118. LoadVar(S,y);
  119. x = (lua_Number)y;
  120. } break;
  121. case 2: {
  122. int16_t y;
  123. LoadVar(S,y);
  124. x = (lua_Number)y;
  125. } break;
  126. case 4: {
  127. int32_t y;
  128. LoadVar(S,y);
  129. x = (lua_Number)y;
  130. } break;
  131. case 8: {
  132. int64_t y;
  133. LoadVar(S,y);
  134. x = (lua_Number)y;
  135. } break;
  136. default: lua_assert(0);
  137. }
  138. }
  139. else
  140. {
  141. LoadVar(S,x); /* should probably handle more cases for float here... */
  142. }
  143. return x;
  144. }
  145. static TString* LoadString(LoadState* S)
  146. {
  147. int32_t size;
  148. LoadVar(S,size);
  149. if (size==0)
  150. return NULL;
  151. else
  152. {
  153. char* s;
  154. if (!luaZ_direct_mode(S->Z)) {
  155. s = luaZ_openspace(S->L,S->b,size);
  156. LoadBlock(S,s,size);
  157. return luaS_newlstr(S->L,s,size-1); /* remove trailing zero */
  158. } else {
  159. s = (char*)luaZ_get_crt_address(S->Z);
  160. LoadBlock(S,NULL,size);
  161. return luaS_newrolstr(S->L,s,size-1);
  162. }
  163. }
  164. }
  165. static void LoadCode(LoadState* S, Proto* f)
  166. {
  167. int n=LoadInt(S);
  168. Align4(S);
  169. if (!luaZ_direct_mode(S->Z)) {
  170. f->code=luaM_newvector(S->L,n,Instruction);
  171. LoadVector(S,f->code,n,sizeof(Instruction));
  172. } else {
  173. f->code=(Instruction*)luaZ_get_crt_address(S->Z);
  174. LoadVector(S,NULL,n,sizeof(Instruction));
  175. }
  176. f->sizecode=n;
  177. }
  178. static Proto* LoadFunction(LoadState* S, TString* p);
  179. static void LoadConstants(LoadState* S, Proto* f)
  180. {
  181. int i,n;
  182. n=LoadInt(S);
  183. f->k=luaM_newvector(S->L,n,TValue);
  184. f->sizek=n;
  185. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  186. for (i=0; i<n; i++)
  187. {
  188. TValue* o=&f->k[i];
  189. int t=LoadChar(S);
  190. switch (t)
  191. {
  192. case LUA_TNIL:
  193. setnilvalue(o);
  194. break;
  195. case LUA_TBOOLEAN:
  196. setbvalue(o,LoadChar(S)!=0);
  197. break;
  198. case LUA_TNUMBER:
  199. setnvalue(o,LoadNumber(S));
  200. break;
  201. case LUA_TSTRING:
  202. setsvalue2n(S->L,o,LoadString(S));
  203. break;
  204. default:
  205. error(S,"bad constant");
  206. break;
  207. }
  208. }
  209. n=LoadInt(S);
  210. f->p=luaM_newvector(S->L,n,Proto*);
  211. f->sizep=n;
  212. for (i=0; i<n; i++) f->p[i]=NULL;
  213. for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
  214. }
  215. static void LoadDebug(LoadState* S, Proto* f)
  216. {
  217. int i,n;
  218. n=LoadInt(S);
  219. Align4(S);
  220. if (!luaZ_direct_mode(S->Z)) {
  221. f->lineinfo=luaM_newvector(S->L,n,int);
  222. LoadVector(S,f->lineinfo,n,sizeof(int));
  223. } else {
  224. f->lineinfo=(int*)luaZ_get_crt_address(S->Z);
  225. LoadVector(S,NULL,n,sizeof(int));
  226. }
  227. f->sizelineinfo=n;
  228. n=LoadInt(S);
  229. f->locvars=luaM_newvector(S->L,n,LocVar);
  230. f->sizelocvars=n;
  231. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  232. for (i=0; i<n; i++)
  233. {
  234. f->locvars[i].varname=LoadString(S);
  235. f->locvars[i].startpc=LoadInt(S);
  236. f->locvars[i].endpc=LoadInt(S);
  237. }
  238. n=LoadInt(S);
  239. f->upvalues=luaM_newvector(S->L,n,TString*);
  240. f->sizeupvalues=n;
  241. for (i=0; i<n; i++) f->upvalues[i]=NULL;
  242. for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
  243. }
  244. static Proto* LoadFunction(LoadState* S, TString* p)
  245. {
  246. Proto* f;
  247. if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
  248. f=luaF_newproto(S->L);
  249. if (luaZ_direct_mode(S->Z)) proto_readonly(f);
  250. setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
  251. f->source=LoadString(S); if (f->source==NULL) f->source=p;
  252. f->linedefined=LoadInt(S);
  253. f->lastlinedefined=LoadInt(S);
  254. f->nups=LoadByte(S);
  255. f->numparams=LoadByte(S);
  256. f->is_vararg=LoadByte(S);
  257. f->maxstacksize=LoadByte(S);
  258. LoadCode(S,f);
  259. LoadConstants(S,f);
  260. LoadDebug(S,f);
  261. IF (!luaG_checkcode(f), "bad code");
  262. S->L->top--;
  263. S->L->nCcalls--;
  264. return f;
  265. }
  266. static void LoadHeader(LoadState* S)
  267. {
  268. char h[LUAC_HEADERSIZE];
  269. char s[LUAC_HEADERSIZE];
  270. int intck = (((lua_Number)0.5)==0); /* 0=float, 1=int */
  271. luaU_header(h);
  272. LoadBlock(S,s,LUAC_HEADERSIZE);
  273. S->swap=(s[6]!=h[6]); s[6]=h[6]; /* Check if byte-swapping is needed */
  274. S->numsize=h[10]=s[10]; /* length of lua_Number */
  275. S->toflt=(s[11]>intck); /* check if conversion from int lua_Number to flt is needed */
  276. if(S->toflt) s[11]=h[11];
  277. IF (c_memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
  278. }
  279. /*
  280. ** load precompiled chunk
  281. */
  282. Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
  283. {
  284. LoadState S;
  285. if (*name=='@' || *name=='=')
  286. S.name=name+1;
  287. else if (*name==LUA_SIGNATURE[0])
  288. S.name="binary string";
  289. else
  290. S.name=name;
  291. S.L=L;
  292. S.Z=Z;
  293. S.b=buff;
  294. LoadHeader(&S);
  295. S.total=0;
  296. return LoadFunction(&S,luaS_newliteral(L,"=?"));
  297. }
  298. /*
  299. * make header
  300. */
  301. void luaU_header (char* h)
  302. {
  303. int x=1;
  304. c_memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
  305. h+=sizeof(LUA_SIGNATURE)-1;
  306. *h++=(char)LUAC_VERSION;
  307. *h++=(char)LUAC_FORMAT;
  308. *h++=(char)*(char*)&x; /* endianness */
  309. *h++=(char)sizeof(int);
  310. *h++=(char)sizeof(int32_t);
  311. *h++=(char)sizeof(Instruction);
  312. *h++=(char)sizeof(lua_Number);
  313. *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
  314. }