lundump.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. ** $Id: lundump.c,v 1.64 2003/08/27 21:01:44 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. lua_State *L=S->L;
  142. n=LoadInt(S);
  143. f->k=luaM_newvector(L,n,TValue);
  144. f->sizek=n;
  145. for (i=0; i<n; i++) setnilvalue(&f->k[i]);
  146. for (i=0; i<n; i++)
  147. {
  148. TValue* o=&f->k[i];
  149. int t=LoadByte(S);
  150. switch (t)
  151. {
  152. case LUA_TNUMBER:
  153. setnvalue(o,LoadNumber(S));
  154. break;
  155. case LUA_TSTRING:
  156. setsvalue2n(L, o,LoadString(S));
  157. break;
  158. case LUA_TNIL:
  159. setnilvalue(o);
  160. break;
  161. default:
  162. luaG_runerror(L,"bad constant type (%d) in %s",t,S->name);
  163. break;
  164. }
  165. }
  166. n=LoadInt(S);
  167. f->p=luaM_newvector(L,n,Proto*);
  168. f->sizep=n;
  169. for (i=0; i<n; i++) f->p[i]=NULL;
  170. for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
  171. }
  172. static Proto* LoadFunction (LoadState* S, TString* p)
  173. {
  174. lua_State *L=S->L;
  175. Proto* f=luaF_newproto(L);
  176. setptvalue2s(L, L->top, f);
  177. incr_top(L);
  178. f->source=LoadString(S); if (f->source==NULL) f->source=p;
  179. f->lineDefined=LoadInt(S);
  180. f->nups=LoadByte(S);
  181. f->numparams=LoadByte(S);
  182. f->is_vararg=LoadByte(S);
  183. f->maxstacksize=LoadByte(S);
  184. LoadLines(S,f);
  185. LoadLocals(S,f);
  186. LoadUpvalues(S,f);
  187. LoadConstants(S,f);
  188. LoadCode(S,f);
  189. #ifndef TRUST_BINARIES
  190. if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in %s",S->name);
  191. #endif
  192. L->top--;
  193. return f;
  194. }
  195. static void LoadSignature (LoadState* S)
  196. {
  197. const char* s=LUA_SIGNATURE;
  198. while (*s!=0 && ezgetc(S)==*s)
  199. ++s;
  200. if (*s!=0) luaG_runerror(S->L,"bad signature in %s",S->name);
  201. }
  202. static void TestSize (LoadState* S, int s, const char* what)
  203. {
  204. int r=LoadByte(S);
  205. if (r!=s)
  206. luaG_runerror(S->L,"virtual machine mismatch in %s: "
  207. "size of %s is %d but read %d",S->name,what,s,r);
  208. }
  209. #define TESTSIZE(s,w) TestSize(S,s,w)
  210. #define V(v) v/16,v%16
  211. static void LoadHeader (LoadState* S)
  212. {
  213. int version;
  214. lua_Number x,tx=TEST_NUMBER;
  215. LoadSignature(S);
  216. version=LoadByte(S);
  217. if (version>VERSION)
  218. luaG_runerror(S->L,"%s too new: "
  219. "read version %d.%d; expected at most %d.%d",
  220. S->name,V(version),V(VERSION));
  221. if (version<VERSION0) /* check last major change */
  222. luaG_runerror(S->L,"%s too old: "
  223. "read version %d.%d; expected at least %d.%d",
  224. S->name,V(version),V(VERSION0));
  225. S->swap=(luaU_endianness()!=LoadByte(S)); /* need to swap bytes? */
  226. TESTSIZE(sizeof(int),"int");
  227. TESTSIZE(sizeof(size_t), "size_t");
  228. TESTSIZE(sizeof(Instruction), "Instruction");
  229. TESTSIZE(sizeof(lua_Number), "number");
  230. x=LoadNumber(S);
  231. if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
  232. luaG_runerror(S->L,"unknown number format in %s",S->name);
  233. }
  234. static Proto* LoadChunk (LoadState* S)
  235. {
  236. LoadHeader(S);
  237. return LoadFunction(S,NULL);
  238. }
  239. /*
  240. ** load precompiled chunk
  241. */
  242. Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char *s)
  243. {
  244. LoadState S;
  245. if (*s=='@' || *s=='=')
  246. S.name=s+1;
  247. else if (*s==LUA_SIGNATURE[0])
  248. S.name="binary string";
  249. else
  250. S.name=s;
  251. S.L=L;
  252. S.Z=Z;
  253. S.b=buff;
  254. return LoadChunk(&S);
  255. }
  256. /*
  257. ** find byte order
  258. */
  259. int luaU_endianness (void)
  260. {
  261. int x=1;
  262. return *(char*)&x;
  263. }