lundump.c 5.7 KB

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