lundump.c 5.5 KB

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