lundump.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. ** $Id: lundump.c,v 1.33 2000/10/31 16:57:23 lhf Exp $
  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 Number LoadNumber (lua_State* L, ZIO* Z, int swap)
  74. {
  75. 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. LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
  96. if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.99s'",ZNAME(Z));
  97. luaF_protook(L,tf,size);
  98. }
  99. static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap)
  100. {
  101. int i,n;
  102. tf->nlocvars=n=LoadInt(L,Z,swap);
  103. tf->locvars=luaM_newvector(L,n,LocVar);
  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. tf->nlineinfo=n=LoadInt(L,Z,swap);
  115. tf->lineinfo=luaM_newvector(L,n,int);
  116. LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
  117. }
  118. static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
  119. static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
  120. {
  121. int i,n;
  122. tf->nkstr=n=LoadInt(L,Z,swap);
  123. tf->kstr=luaM_newvector(L,n,TString*);
  124. for (i=0; i<n; i++)
  125. tf->kstr[i]=LoadString(L,Z,swap);
  126. tf->nknum=n=LoadInt(L,Z,swap);
  127. tf->knum=luaM_newvector(L,n,Number);
  128. LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
  129. tf->nkproto=n=LoadInt(L,Z,swap);
  130. tf->kproto=luaM_newvector(L,n,Proto*);
  131. for (i=0; i<n; i++)
  132. tf->kproto[i]=LoadFunction(L,Z,swap);
  133. }
  134. static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
  135. {
  136. Proto* tf=luaF_newproto(L);
  137. tf->source=LoadString(L,Z,swap);
  138. tf->lineDefined=LoadInt(L,Z,swap);
  139. tf->numparams=LoadInt(L,Z,swap);
  140. tf->is_vararg=LoadByte(L,Z);
  141. tf->maxstacksize=LoadInt(L,Z,swap);
  142. LoadLocals(L,tf,Z,swap);
  143. LoadLines(L,tf,Z,swap);
  144. LoadConstants(L,tf,Z,swap);
  145. LoadCode(L,tf,Z,swap);
  146. return tf;
  147. }
  148. static void LoadSignature (lua_State* L, ZIO* Z)
  149. {
  150. const char* s=SIGNATURE;
  151. while (*s!=0 && ezgetc(L,Z)==*s)
  152. ++s;
  153. if (*s!=0) luaO_verror(L,"bad signature in `%.99s'",ZNAME(Z));
  154. }
  155. static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
  156. {
  157. int r=ezgetc(L,Z);
  158. if (r!=s)
  159. luaO_verror(L,"virtual machine mismatch in `%.99s':\n"
  160. " %.20s is %d but read %d",ZNAME(Z),what,s,r);
  161. }
  162. #define TESTSIZE(s) TestSize(L,s,#s,Z)
  163. #define V(v) v/16,v%16
  164. static int LoadHeader (lua_State* L, ZIO* Z)
  165. {
  166. int version,swap;
  167. Number f=0,tf=TEST_NUMBER;
  168. LoadSignature(L,Z);
  169. version=ezgetc(L,Z);
  170. if (version>VERSION)
  171. luaO_verror(L,"`%.99s' too new:\n"
  172. " read version %d.%d; expected at most %d.%d",
  173. ZNAME(Z),V(version),V(VERSION));
  174. if (version<VERSION0) /* check last major change */
  175. luaO_verror(L,"`%.99s' too old:\n"
  176. " read version %d.%d; expected at least %d.%d",
  177. ZNAME(Z),V(version),V(VERSION));
  178. swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
  179. TESTSIZE(sizeof(int));
  180. TESTSIZE(sizeof(size_t));
  181. TESTSIZE(sizeof(Instruction));
  182. TESTSIZE(SIZE_INSTRUCTION);
  183. TESTSIZE(SIZE_OP);
  184. TESTSIZE(SIZE_B);
  185. TESTSIZE(sizeof(Number));
  186. f=LoadNumber(L,Z,swap);
  187. if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
  188. luaO_verror(L,"unknown number format in `%.99s':\n"
  189. " read " NUMBER_FMT "; expected " NUMBER_FMT, ZNAME(Z),f,tf);
  190. return swap;
  191. }
  192. static Proto* LoadChunk (lua_State* L, ZIO* Z)
  193. {
  194. return LoadFunction(L,Z,LoadHeader(L,Z));
  195. }
  196. /*
  197. ** load one chunk from a file or buffer
  198. ** return main if ok and NULL at EOF
  199. */
  200. Proto* luaU_undump (lua_State* L, ZIO* Z)
  201. {
  202. Proto* tf=NULL;
  203. int c=zgetc(Z);
  204. if (c==ID_CHUNK)
  205. tf=LoadChunk(L,Z);
  206. c=zgetc(Z);
  207. if (c!=EOZ)
  208. luaO_verror(L,"`%.99s' apparently contains more than one chunk",ZNAME(Z));
  209. return tf;
  210. }
  211. /*
  212. ** find byte order
  213. */
  214. int luaU_endianess (void)
  215. {
  216. int x=1;
  217. return *(char*)&x;
  218. }