lundump.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. ** $Id: lundump.c,v 1.47 2003/01/10 11:08:45 lhf Exp $
  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 void LoadUpvalues (LoadState* S, Proto* f)
  124. {
  125. int i,n,noname;
  126. n=LoadInt(S);
  127. noname=(n==0);
  128. if (!noname && n!=f->nupvalues)
  129. luaG_runerror(S->L,"bad nupvalues in %s: read %d; expected %d",
  130. S->name,n,f->nupvalues);
  131. n=f->nupvalues;
  132. f->upvalues=luaM_newvector(S->L,n,TString*);
  133. if (noname)
  134. {
  135. TString* name=luaS_newliteral(S->L,"(no name)");
  136. for (i=0; i<n; i++) f->upvalues[i]=name;
  137. }
  138. else
  139. for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
  140. }
  141. static Proto* LoadFunction (LoadState* S, TString* p);
  142. static void LoadConstants (LoadState* S, Proto* f)
  143. {
  144. int i,n;
  145. n=LoadInt(S);
  146. f->k=luaM_newvector(S->L,n,TObject);
  147. f->sizek=n;
  148. for (i=0; i<n; i++)
  149. {
  150. TObject* o=&f->k[i];
  151. int t=LoadByte(S);
  152. switch (t)
  153. {
  154. case LUA_TNUMBER:
  155. setnvalue(o,LoadNumber(S));
  156. break;
  157. case LUA_TSTRING:
  158. setsvalue2n(o,LoadString(S));
  159. break;
  160. case LUA_TNIL:
  161. setnilvalue(o);
  162. break;
  163. default:
  164. luaG_runerror(S->L,"bad constant type (%d) in %s",t,S->name);
  165. break;
  166. }
  167. }
  168. n=LoadInt(S);
  169. f->p=luaM_newvector(S->L,n,Proto*);
  170. f->sizep=n;
  171. for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
  172. }
  173. static Proto* LoadFunction (LoadState* S, TString* p)
  174. {
  175. Proto* f=luaF_newproto(S->L);
  176. f->source=LoadString(S); if (f->source==NULL) f->source=p;
  177. f->lineDefined=LoadInt(S);
  178. f->nupvalues=LoadInt(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. return f;
  191. }
  192. static void LoadSignature (LoadState* S)
  193. {
  194. const char* s=LUA_SIGNATURE;
  195. while (*s!=0 && ezgetc(S)==*s)
  196. ++s;
  197. if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
  198. }
  199. static void TestSize (LoadState* S, int s, const char* what)
  200. {
  201. int r=LoadByte(S);
  202. if (r!=s)
  203. luaG_runerror(S->L,"virtual machine mismatch in %s: "
  204. "size of %s is %d but read %d",S->name,what,s,r);
  205. }
  206. #define TESTSIZE(s,w) TestSize(S,s,w)
  207. #define V(v) v/16,v%16
  208. static void LoadHeader (LoadState* S)
  209. {
  210. int version;
  211. lua_Number x=0,tx=TEST_NUMBER;
  212. LoadSignature(S);
  213. version=LoadByte(S);
  214. if (version>VERSION)
  215. luaG_runerror(S->L,"%s too new: "
  216. "read version %d.%d; expected at most %d.%d",
  217. S->name,V(version),V(VERSION));
  218. if (version<VERSION0) /* check last major change */
  219. luaG_runerror(S->L,"%s too old: "
  220. "read version %d.%d; expected at least %d.%d",
  221. S->name,V(version),V(VERSION0));
  222. S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */
  223. TESTSIZE(sizeof(int),"int");
  224. TESTSIZE(sizeof(size_t), "size_t");
  225. TESTSIZE(sizeof(Instruction), "Instruction");
  226. TESTSIZE(SIZE_OP, "OP");
  227. TESTSIZE(SIZE_A, "A");
  228. TESTSIZE(SIZE_B, "B");
  229. TESTSIZE(SIZE_C, "C");
  230. TESTSIZE(sizeof(lua_Number), "number");
  231. x=LoadNumber(S);
  232. if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
  233. luaG_runerror(S->L,"unknown number format in %s",S->name);
  234. }
  235. static Proto* LoadChunk (LoadState* S)
  236. {
  237. LoadHeader(S);
  238. return LoadFunction(S,NULL);
  239. }
  240. /*
  241. ** load precompiled chunk
  242. */
  243. Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff)
  244. {
  245. LoadState S;
  246. const char* s=zname(Z);
  247. if (*s=='@' || *s=='=')
  248. S.name=s+1;
  249. else if (*s==LUA_SIGNATURE[0])
  250. S.name="binary string";
  251. else
  252. S.name=s;
  253. S.L=L;
  254. S.Z=Z;
  255. S.b=buff;
  256. return LoadChunk(&S);
  257. }
  258. /*
  259. ** find byte order
  260. */
  261. int luaU_endianness (void)
  262. {
  263. int x=1;
  264. return *(char*)&x;
  265. }