lundump.c 5.2 KB

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