lundump.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. ** $Id: lundump.c,v 1.43 2001/07/24 21:57:19 roberto Exp $
  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. luaO_verror(L,"unexpected end of file in `%.99s'",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. f->sizelineinfo=n;
  120. LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap);
  121. }
  122. static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap);
  123. static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
  124. {
  125. int i,n;
  126. n=LoadInt(L,Z,swap);
  127. f->k=luaM_newvector(L,n,TObject);
  128. f->sizek=n;
  129. for (i=0; i<n; i++)
  130. {
  131. TObject* o=&f->k[i];
  132. ttype(o)=LoadByte(L,Z);
  133. switch (ttype(o))
  134. {
  135. case LUA_TNUMBER:
  136. nvalue(o)=LoadNumber(L,Z,swap);
  137. break;
  138. case LUA_TSTRING:
  139. tsvalue(o)=LoadString(L,Z,swap);
  140. break;
  141. default:
  142. luaO_verror(L,"bad constant type (%d) in `%.99s'",ttype(o),ZNAME(Z));
  143. break;
  144. }
  145. }
  146. n=LoadInt(L,Z,swap);
  147. f->p=luaM_newvector(L,n,Proto*);
  148. f->sizep=n;
  149. for (i=0; i<n; i++) f->p[i]=LoadFunction(L,f->source,Z,swap);
  150. }
  151. static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap)
  152. {
  153. Proto* f=luaF_newproto(L);
  154. f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p;
  155. f->lineDefined=LoadInt(L,Z,swap);
  156. f->nupvalues=LoadShort(L,Z,swap);
  157. f->numparams=LoadShort(L,Z,swap);
  158. f->is_vararg=LoadShort(L,Z,swap);
  159. f->maxstacksize=LoadShort(L,Z,swap);
  160. LoadLocals(L,f,Z,swap);
  161. LoadLines(L,f,Z,swap);
  162. LoadConstants(L,f,Z,swap);
  163. LoadCode(L,f,Z,swap);
  164. #ifndef TRUST_BINARIES
  165. if (!luaG_checkcode(f)) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
  166. #endif
  167. return f;
  168. }
  169. static void LoadSignature (lua_State* L, ZIO* Z)
  170. {
  171. const char* s=LUA_SIGNATURE;
  172. while (*s!=0 && ezgetc(L,Z)==*s)
  173. ++s;
  174. if (*s!=0) luaO_verror(L,"bad signature in `%.99s'",ZNAME(Z));
  175. }
  176. static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
  177. {
  178. int r=LoadByte(L,Z);
  179. if (r!=s)
  180. luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
  181. " size of %.20s is %d but read %d",ZNAME(Z),what,s,r);
  182. }
  183. #define TESTSIZE(s,w) TestSize(L,s,w,Z)
  184. #define V(v) v/16,v%16
  185. static int LoadHeader (lua_State* L, ZIO* Z)
  186. {
  187. int version,swap;
  188. lua_Number x=0,tx=TEST_NUMBER;
  189. LoadSignature(L,Z);
  190. version=LoadByte(L,Z);
  191. if (version>VERSION)
  192. luaO_verror(L,"`%.99s' too new:\n"
  193. " read version %d.%d; expected at most %d.%d",
  194. ZNAME(Z),V(version),V(VERSION));
  195. if (version<VERSION0) /* check last major change */
  196. luaO_verror(L,"`%.99s' too old:\n"
  197. " read version %d.%d; expected at least %d.%d",
  198. ZNAME(Z),V(version),V(VERSION));
  199. swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */
  200. TESTSIZE(sizeof(int),"int");
  201. TESTSIZE(sizeof(size_t), "size_t");
  202. TESTSIZE(sizeof(Instruction), "size_t");
  203. TESTSIZE(SIZE_OP, "OP");
  204. TESTSIZE(SIZE_A, "A");
  205. TESTSIZE(SIZE_B, "B");
  206. TESTSIZE(SIZE_C, "C");
  207. TESTSIZE(sizeof(lua_Number), "number");
  208. x=LoadNumber(L,Z,swap);
  209. if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
  210. luaO_verror(L,"unknown number format in `%.99s':\n"
  211. " read " LUA_NUMBER_FMT "; expected " LUA_NUMBER_FMT,
  212. ZNAME(Z),x,tx);
  213. return swap;
  214. }
  215. static Proto* LoadChunk (lua_State* L, ZIO* Z)
  216. {
  217. return LoadFunction(L,NULL,Z,LoadHeader(L,Z));
  218. }
  219. /*
  220. ** load one chunk from a file or buffer
  221. */
  222. Proto* luaU_undump (lua_State* L, ZIO* Z)
  223. {
  224. Proto* f=LoadChunk(L,Z);
  225. if (zgetc(Z)!=EOZ)
  226. luaO_verror(L,"`%.99s' apparently contains more than one chunk",ZNAME(Z));
  227. return f;
  228. }
  229. /*
  230. ** find byte order
  231. */
  232. int luaU_endianness (void)
  233. {
  234. int x=1;
  235. return *(char*)&x;
  236. }