lundump.c 5.8 KB

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