lundump.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. ** $Id: lundump.c,v 1.63 2003/08/25 19:51:54 roberto Exp roberto $
  3. ** load pre-compiled Lua chunks
  4. ** See Copyright Notice in lua.h
  5. */
  6. #define lundump_c
  7. #include "lua.h"
  8. #include "ldebug.h"
  9. #include "ldo.h"
  10. #include "lfunc.h"
  11. #include "lmem.h"
  12. #include "lopcodes.h"
  13. #include "lstring.h"
  14. #include "lundump.h"
  15. #include "lzio.h"
  16. #define LoadByte (lu_byte) ezgetc
  17. typedef struct {
  18. lua_State* L;
  19. ZIO* Z;
  20. Mbuffer* b;
  21. int swap;
  22. const char* name;
  23. } LoadState;
  24. static void unexpectedEOZ (LoadState* S)
  25. {
  26. luaG_runerror(S->L,"unexpected end of file in %s",S->name);
  27. }
  28. static int ezgetc (LoadState* S)
  29. {
  30. int c=zgetc(S->Z);
  31. if (c==EOZ) unexpectedEOZ(S);
  32. return c;
  33. }
  34. static void ezread (LoadState* S, void* b, int n)
  35. {
  36. int r=luaZ_read(S->Z,b,n);
  37. if (r!=0) unexpectedEOZ(S);
  38. }
  39. static void LoadBlock (LoadState* S, void* b, size_t size)
  40. {
  41. if (S->swap)
  42. {
  43. char* p=(char*) b+size-1;
  44. int n=size;
  45. while (n--) *p--=(char)ezgetc(S);
  46. }
  47. else
  48. ezread(S,b,size);
  49. }
  50. static void LoadVector (LoadState* S, void* b, int m, size_t size)
  51. {
  52. if (S->swap)
  53. {
  54. char* q=(char*) b;
  55. while (m--)
  56. {
  57. char* p=q+size-1;
  58. int n=size;
  59. while (n--) *p--=(char)ezgetc(S);
  60. q+=size;
  61. }
  62. }
  63. else
  64. ezread(S,b,m*size);
  65. }
  66. static int LoadInt (LoadState* S)
  67. {
  68. int x;
  69. LoadBlock(S,&x,sizeof(x));
  70. if (x<0) luaG_runerror(S->L,"bad integer in %s",S->name);
  71. return x;
  72. }
  73. static size_t LoadSize (LoadState* S)
  74. {
  75. size_t x;
  76. LoadBlock(S,&x,sizeof(x));
  77. return x;
  78. }
  79. static lua_Number LoadNumber (LoadState* S)
  80. {
  81. lua_Number x;
  82. LoadBlock(S,&x,sizeof(x));
  83. return x;
  84. }
  85. static TString* LoadString (LoadState* S)
  86. {
  87. size_t size=LoadSize(S);
  88. if (size==0)
  89. return NULL;
  90. else
  91. {
  92. char* s=luaZ_openspace(S->L,S->b,size);
  93. ezread(S,s,size);
  94. return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
  95. }
  96. }
  97. static void LoadCode (LoadState* S, Proto* f)
  98. {
  99. int size=LoadInt(S);
  100. f->code=luaM_newvector(S->L,size,Instruction);
  101. f->sizecode=size;
  102. LoadVector(S,f->code,size,sizeof(*f->code));
  103. }
  104. static void LoadLocals (LoadState* S, Proto* f)
  105. {
  106. int i,n;
  107. n=LoadInt(S);
  108. f->locvars=luaM_newvector(S->L,n,LocVar);
  109. f->sizelocvars=n;
  110. for (i=0; i<n; i++) f->locvars[i].varname=NULL;
  111. for (i=0; i<n; i++)
  112. {
  113. f->locvars[i].varname=LoadString(S);
  114. f->locvars[i].startpc=LoadInt(S);
  115. f->locvars[i].endpc=LoadInt(S);
  116. }
  117. }
  118. static void LoadLines (LoadState* S, Proto* f)
  119. {
  120. int size=LoadInt(S);
  121. f->lineinfo=luaM_newvector(S->L,size,int);
  122. f->sizelineinfo=size;
  123. LoadVector(S,f->lineinfo,size,sizeof(*f->lineinfo));
  124. }
  125. static void LoadUpvalues (LoadState* S, Proto* f)
  126. {
  127. int i,n;
  128. n=LoadInt(S);
  129. if (n!=0 && n!=f->nups)
  130. luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d",
  131. S->name,n,f->nups);
  132. f->upvalues=luaM_newvector(S->L,n,TString*);
  133. f->sizeupvalues=n;
  134. for (i=0; i<n; i++) f->upvalues[i]=NULL;
  135. for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
  136. }
  137. static Proto* LoadFunction (LoadState* S, TString* p);
  138. static void LoadConstants (LoadState* S, Proto* f)
  139. {
  140. int i,n;
  141. n=LoadInt(S);
  142. f->k=luaM_newvector(S->L,n,TObject);
  143. f->sizek=n;
  144. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  145. for (i=0; i<n; i++)
  146. {
  147. TObject* o=&f->k[i];
  148. int t=LoadByte(S);
  149. switch (t)
  150. {
  151. case LUA_TNUMBER:
  152. setnvalue(o,LoadNumber(S));
  153. break;
  154. case LUA_TSTRING:
  155. setsvalue2n(o,LoadString(S));
  156. break;
  157. case LUA_TNIL:
  158. setnilvalue(o);
  159. break;
  160. default:
  161. luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);
  162. break;
  163. }
  164. }
  165. n=LoadInt(S);
  166. f->p=luaM_newvector(S->L,n,Proto*);
  167. f->sizep=n;
  168. for (i=0; i<n; i++) f->p[i]=NULL;
  169. for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
  170. }
  171. static Proto* LoadFunction (LoadState* S, TString* p)
  172. {
  173. Proto* f=luaF_newproto(S->L);
  174. setptvalue2s(S->L->top, f);
  175. incr_top(S->L);
  176. f->source=LoadString(S); if (f->source==NULL) f->source=p;
  177. f->lineDefined=LoadInt(S);
  178. f->nups=LoadByte(S);
  179. f->numparams=LoadByte(S);
  180. f->is_vararg=LoadByte(S);
  181. f->maxstacksize=LoadByte(S);
  182. LoadLines(S,f);
  183. LoadLocals(S,f);
  184. LoadUpvalues(S,f);
  185. LoadConstants(S,f);
  186. LoadCode(S,f);
  187. #ifndef TRUST_BINARIES
  188. if (!luaG_checkcode(f)) luaG_runerror(S->L,"bad code in %s",S->name);
  189. #endif
  190. S->L->top--;
  191. return f;
  192. }
  193. static void LoadSignature (LoadState* S)
  194. {
  195. const char* s=LUA_SIGNATURE;
  196. while (*s!=0 && ezgetc(S)==*s)
  197. ++s;
  198. if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
  199. }
  200. static void TestSize (LoadState* S, int s, const char* what)
  201. {
  202. int r=LoadByte(S);
  203. if (r!=s)
  204. luaG_runerror(S->L,"virtual machine mismatch in %s: "
  205. "size of %s is %d but read %d",S->name,what,s,r);
  206. }
  207. #define TESTSIZE(s,w) TestSize(S,s,w)
  208. #define V(v) v/16,v%16
  209. static void LoadHeader (LoadState* S)
  210. {
  211. int version;
  212. lua_Number x,tx=TEST_NUMBER;
  213. LoadSignature(S);
  214. version=LoadByte(S);
  215. if (version>VERSION)
  216. luaG_runerror(S->L,"%s too new: "
  217. "read version %d.%d; expected at most %d.%d",
  218. S->name,V(version),V(VERSION));
  219. if (version<VERSION0) /* check last major change */
  220. luaG_runerror(S->L,"%s too old: "
  221. "read version %d.%d; expected at least %d.%d",
  222. S->name,V(version),V(VERSION0));
  223. S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */
  224. TESTSIZE(sizeof(int),"int");
  225. TESTSIZE(sizeof(size_t), "size_t");
  226. TESTSIZE(sizeof(Instruction), "Instruction");
  227. TESTSIZE(sizeof(lua_Number), "number");
  228. x=LoadNumber(S);
  229. if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
  230. luaG_runerror(S->L,"unknown number format in %s",S->name);
  231. }
  232. static Proto* LoadChunk (LoadState* S)
  233. {
  234. LoadHeader(S);
  235. return LoadFunction(S,NULL);
  236. }
  237. /*
  238. ** load precompiled chunk
  239. */
  240. Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char *s)
  241. {
  242. LoadState S;
  243. if (*s=='@' || *s=='=')
  244. S.name=s+1;
  245. else if (*s==LUA_SIGNATURE[0])
  246. S.name="binary string";
  247. else
  248. S.name=s;
  249. S.L=L;
  250. S.Z=Z;
  251. S.b=buff;
  252. return LoadChunk(&S);
  253. }
  254. /*
  255. ** find byte order
  256. */
  257. int luaU_endianness (void)
  258. {
  259. int x=1;
  260. return *(char*)&x;
  261. }