lundump.c 5.3 KB

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