lundump.c 5.3 KB

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