lundump.c 5.3 KB

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