lundump.c 5.7 KB

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