lundump.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ** $Id: lundump.c,v 1.29 2000/06/28 14:12:55 lhf Exp lhf $
  3. ** load bytecodes from files
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "lauxlib.h"
  9. #include "lfunc.h"
  10. #include "lmem.h"
  11. #include "lopcodes.h"
  12. #include "lstring.h"
  13. #include "lundump.h"
  14. #define LoadVector(L,b,n,s,Z) LoadBlock(L,b,(n)*(s),Z)
  15. #define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
  16. #define LoadByte ezgetc
  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. luaL_verror(L,"unexpected end of file in `%.255s'",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 LoadReverse (lua_State* L, void* b, size_t size, ZIO* Z)
  38. {
  39. unsigned char *p=(unsigned char *) b+size;
  40. int n=size;
  41. while (n--) *p--=ezgetc(L,Z);
  42. }
  43. static int LoadInt (lua_State* L, ZIO* Z, int swap)
  44. {
  45. int x;
  46. if (swap)
  47. LoadReverse(L,&x,sizeof(x),Z);
  48. else
  49. LoadBlock(L,&x,sizeof(x),Z);
  50. return x;
  51. }
  52. static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
  53. {
  54. size_t x;
  55. if (swap)
  56. LoadReverse(L,&x,sizeof(x),Z);
  57. else
  58. LoadBlock(L,&x,sizeof(x),Z);
  59. return x;
  60. }
  61. static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
  62. {
  63. Number x;
  64. if (swap)
  65. LoadReverse(L,&x,sizeof(x),Z);
  66. else
  67. LoadBlock(L,&x,sizeof(x),Z);
  68. return x;
  69. }
  70. static TString* LoadString (lua_State* L, ZIO* Z, int swap)
  71. {
  72. size_t size=LoadSize(L,Z,swap);
  73. if (size==0)
  74. return NULL;
  75. else
  76. {
  77. char* s=luaL_openspace(L,size);
  78. LoadBlock(L,s,size,Z);
  79. return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
  80. }
  81. }
  82. static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
  83. {
  84. int size=LoadInt(L,Z,swap);
  85. tf->code=luaM_newvector(L,size,Instruction);
  86. LoadVector(L,tf->code,size,sizeof(*tf->code),Z);
  87. #if 0
  88. if (swap) SwapBytes(tf->code,sizeof(*tf->code),size);
  89. #endif
  90. if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in `%.255s'",ZNAME(Z));
  91. }
  92. static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
  93. {
  94. int i,n;
  95. tf->nlocvars=n=LoadInt(L,Z,swap);
  96. tf->locvars=luaM_newvector(L,n,LocVar);
  97. for (i=0; i<n; i++)
  98. {
  99. tf->locvars[i].varname=LoadString(L,Z,swap);
  100. tf->locvars[i].startpc=LoadInt(L,Z,swap);
  101. tf->locvars[i].endpc=LoadInt(L,Z,swap);
  102. }
  103. }
  104. static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
  105. {
  106. int n=LoadInt(L,Z,swap);
  107. if (n==0) return;
  108. tf->lineinfo=luaM_newvector(L,n,int);
  109. LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z);
  110. #if 0
  111. if (swap) SwapBytes(tf->lineinfo,sizeof(*tf->lineinfo),n);
  112. #endif
  113. }
  114. static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
  115. static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
  116. {
  117. int i,n;
  118. tf->nkstr=n=LoadInt(L,Z,swap);
  119. tf->kstr=luaM_newvector(L,n,TString*);
  120. for (i=0; i<n; i++)
  121. tf->kstr[i]=LoadString(L,Z,swap);
  122. tf->nknum=n=LoadInt(L,Z,swap);
  123. tf->knum=luaM_newvector(L,n,Number);
  124. LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z);
  125. if (swap)
  126. for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z,swap); /* TODO */
  127. tf->nkproto=n=LoadInt(L,Z,swap);
  128. tf->kproto=luaM_newvector(L,n,Proto*);
  129. for (i=0; i<n; i++)
  130. tf->kproto[i]=LoadFunction(L,Z,swap);
  131. }
  132. static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
  133. {
  134. Proto* tf=luaF_newproto(L);
  135. tf->source=LoadString(L,Z,swap);
  136. tf->lineDefined=LoadInt(L,Z,swap);
  137. tf->numparams=LoadInt(L,Z,swap);
  138. tf->is_vararg=LoadByte(L,Z);
  139. tf->maxstacksize=LoadInt(L,Z,swap);
  140. LoadCode(L,tf,Z,swap);
  141. LoadLocals(L,tf,Z,swap);
  142. LoadLines(L,tf,Z,swap);
  143. LoadConstants(L,tf,Z,swap);
  144. return tf;
  145. }
  146. static void LoadSignature (lua_State* L, ZIO* Z)
  147. {
  148. const char* s=SIGNATURE;
  149. while (*s!=0 && ezgetc(L,Z)==*s)
  150. ++s;
  151. if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
  152. }
  153. static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
  154. {
  155. int r=ezgetc(L,Z);
  156. if (r!=s)
  157. luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
  158. " %s is %d but read %d",ZNAME(Z),what,r,s);
  159. }
  160. #define TESTSIZE(s) TestSize(L,s,#s,Z)
  161. #define V(v) v/16,v%16
  162. static int LoadHeader (lua_State* L, ZIO* Z)
  163. {
  164. int version,swap;
  165. Number f=0,tf=TEST_NUMBER;
  166. LoadSignature(L,Z);
  167. version=ezgetc(L,Z);
  168. if (version>VERSION)
  169. luaL_verror(L,"`%.255s' too new:\n"
  170. " read version %d.%d; expected at most %d.%d",
  171. ZNAME(Z),V(version),V(VERSION));
  172. if (version<VERSION0) /* check last major change */
  173. luaL_verror(L,"`%.255s' too old:\n"
  174. " read version %d.%d; expected at least %d.%d",
  175. ZNAME(Z),V(version),V(VERSION));
  176. swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
  177. TESTSIZE(sizeof(int));
  178. TESTSIZE(sizeof(size_t));
  179. TESTSIZE(sizeof(Instruction));
  180. TESTSIZE(SIZE_INSTRUCTION);
  181. TESTSIZE(SIZE_OP);
  182. TESTSIZE(SIZE_B);
  183. TESTSIZE(sizeof(Number));
  184. f=LoadNumber(L,Z,swap);
  185. if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
  186. luaL_verror(L,"unknown number format in `%.255s':\n"
  187. " read " NUMBER_FMT "; expected " NUMBER_FMT,
  188. ZNAME(Z),f,tf);
  189. return swap;
  190. }
  191. static Proto* LoadChunk (lua_State* L, ZIO* Z)
  192. {
  193. return LoadFunction(L,Z,LoadHeader(L,Z));
  194. }
  195. /*
  196. ** load one chunk from a file or buffer
  197. ** return main if ok and NULL at EOF
  198. */
  199. Proto* luaU_undump (lua_State* L, ZIO* Z)
  200. {
  201. int c=zgetc(Z);
  202. if (c==ID_CHUNK)
  203. return LoadChunk(L,Z);
  204. else if (c!=EOZ)
  205. luaL_verror(L,"`%.255s' is not a precompiled Lua chunk",ZNAME(Z));
  206. return NULL;
  207. }
  208. Proto* luaU_undump1 (lua_State* L, ZIO* Z)
  209. {
  210. return luaU_undump(L,Z);
  211. }
  212. /*
  213. ** find byte order
  214. */
  215. int luaU_endianess (void)
  216. {
  217. int x=1;
  218. return *(char*)&x;
  219. }