lundump.c 5.6 KB

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