lundump.c 5.5 KB

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