lundump.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. ** $Id: lundump.c,v 1.25 2000/08/24 14:19:39 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 "lua.h"
  9. #include "lauxlib.h"
  10. #include "lfunc.h"
  11. #include "lmem.h"
  12. #include "lopcodes.h"
  13. #include "lstring.h"
  14. #include "lundump.h"
  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 unsigned int LoadWord (lua_State* L, ZIO* Z)
  38. {
  39. unsigned int hi=ezgetc(L,Z);
  40. unsigned int lo=ezgetc(L,Z);
  41. return (hi<<8)|lo;
  42. }
  43. static unsigned long LoadLong (lua_State* L, ZIO* Z)
  44. {
  45. unsigned long hi=LoadWord(L,Z);
  46. unsigned long lo=LoadWord(L,Z);
  47. return (hi<<16)|lo;
  48. }
  49. static Number LoadNumber (lua_State* L, ZIO* Z)
  50. {
  51. char b[256];
  52. int size=ezgetc(L,Z);
  53. LoadBlock(L,b,size,Z);
  54. b[size]=0;
  55. return luaU_str2d(L,b,ZNAME(Z));
  56. }
  57. static int LoadInt (lua_State* L, ZIO* Z, const char* message)
  58. {
  59. unsigned long l=LoadLong(L,Z);
  60. unsigned int i=l;
  61. if (i!=l) luaL_verror(L,"%s in `%.255s';\n"
  62. " read %lu; expected %u",message,ZNAME(Z),l,-1);
  63. return i;
  64. }
  65. static TString* LoadString (lua_State* L, ZIO* Z)
  66. {
  67. long size=LoadLong(L,Z);
  68. if (size==0)
  69. return NULL;
  70. else
  71. {
  72. char* s=luaL_openspace(L,size);
  73. LoadBlock(L,s,size,Z);
  74. return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
  75. }
  76. }
  77. static void SwapCode (lua_State* L, Instruction* code, int size, ZIO* Z)
  78. {
  79. unsigned char* p;
  80. unsigned char c;
  81. if (sizeof(Instruction)==4)
  82. while (size--)
  83. {
  84. p=(unsigned char *) code++;
  85. c=p[0]; p[0]=p[3]; p[3]=c;
  86. c=p[1]; p[1]=p[2]; p[2]=c;
  87. }
  88. else if (sizeof(Instruction)==2)
  89. while (size--)
  90. {
  91. p=(unsigned char *) code++;
  92. c=p[0]; p[0]=p[1]; p[1]=c;
  93. }
  94. else
  95. luaL_verror(L,"cannot swap code with %d-byte instructions in `%.255s'",
  96. (int)sizeof(Instruction),ZNAME(Z));
  97. }
  98. static void LoadCode (lua_State* L, Proto* tf, ZIO* Z)
  99. {
  100. int size=LoadInt(L,Z,"code too long");
  101. Instruction t=0,tt=TEST_CODE;
  102. tf->code=luaM_newvector(L,size,Instruction);
  103. LoadBlock(L,tf->code,size*sizeof(*tf->code),Z);
  104. if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in `%.255s'",ZNAME(Z));
  105. LoadBlock(L,&t,sizeof(t),Z);
  106. if (t!=tt)
  107. {
  108. Instruction ot=t;
  109. SwapCode(L,&t,1,Z);
  110. if (t!=tt) luaL_verror(L,"cannot swap code in `%.255s';\n"
  111. " read 0x%08X; swapped to 0x%08X; expected 0x%08X",
  112. ZNAME(Z),(unsigned long)ot,(unsigned long)t,(unsigned long)tt);
  113. SwapCode(L,tf->code,size,Z);
  114. }
  115. }
  116. static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z)
  117. {
  118. }
  119. static Proto* LoadFunction (lua_State* L, ZIO* Z, int native);
  120. static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
  121. {
  122. int i,n;
  123. tf->nkstr=n=LoadInt(L,Z,"too many strings");
  124. if (n>0)
  125. {
  126. tf->kstr=luaM_newvector(L,n,TString*);
  127. for (i=0; i<n; i++)
  128. {
  129. TString* s=LoadString(L,Z);
  130. LoadByte(L,Z);
  131. tf->kstr[i]=s;
  132. }
  133. }
  134. tf->nknum=n=LoadInt(L,Z,"too many numbers");
  135. if (n>0)
  136. {
  137. tf->knum=luaM_newvector(L,n,Number);
  138. if (native)
  139. LoadBlock(L,tf->knum,n*sizeof(*tf->knum),Z);
  140. else
  141. for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
  142. }
  143. tf->nkproto=n=LoadInt(L,Z,"too many functions");
  144. if (n>0)
  145. {
  146. tf->kproto=luaM_newvector(L,n,Proto*);
  147. for (i=0; i<n; i++) tf->kproto[i]=LoadFunction(L,Z,native);
  148. }
  149. }
  150. static Proto* LoadFunction (lua_State* L, ZIO* Z, int native)
  151. {
  152. Proto* tf=luaF_newproto(L);
  153. tf->source=LoadString(L,Z);
  154. if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
  155. tf->lineDefined=LoadInt(L,Z,"lineDefined too large");
  156. tf->numparams=LoadInt(L,Z,"too many parameters");
  157. tf->is_vararg=LoadByte(L,Z);
  158. tf->maxstacksize=LoadInt(L,Z,"too much stack needed");
  159. LoadCode(L,tf,Z);
  160. LoadLocals(L,tf,Z);
  161. LoadConstants(L,tf,Z,native);
  162. return tf;
  163. }
  164. static void LoadSignature (lua_State* L, ZIO* Z)
  165. {
  166. const char* s=SIGNATURE;
  167. while (*s!=0 && ezgetc(L,Z)==*s)
  168. ++s;
  169. if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
  170. }
  171. #define V(v) v/16,v%16
  172. static int LoadHeader (lua_State* L, ZIO* Z)
  173. {
  174. int version,sizeofR,native;
  175. LoadSignature(L,Z);
  176. version=ezgetc(L,Z);
  177. if (version>VERSION)
  178. luaL_verror(L,"`%.255s' too new:\n"
  179. " read version %d.%d; expected at most %d.%d",
  180. ZNAME(Z),V(version),V(VERSION));
  181. if (version<VERSION0) /* check last major change */
  182. luaL_verror(L,"`%.255s' too old:\n"
  183. " read version %d.%d; expected at least %d.%d",
  184. ZNAME(Z),V(version),V(VERSION));
  185. {
  186. int I=ezgetc(L,Z);
  187. int i=ezgetc(L,Z);
  188. int op=ezgetc(L,Z);
  189. int b=ezgetc(L,Z);
  190. if (I!=sizeof(Instruction) || i!=SIZE_INSTRUCTION || op!=SIZE_OP || b!=SIZE_B)
  191. luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
  192. " read %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments;\n"
  193. " expected %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments",
  194. ZNAME(Z),i,I,op,b,SIZE_INSTRUCTION,(int)sizeof(Instruction),SIZE_OP,SIZE_B);
  195. }
  196. sizeofR=ezgetc(L,Z);
  197. native=(sizeofR!=0);
  198. if (native) /* test number representation */
  199. {
  200. if (sizeofR!=sizeof(Number))
  201. luaL_verror(L,"native number size mismatch in `%.255s':\n"
  202. " read %d; expected %d",
  203. ZNAME(Z),sizeofR,(int)sizeof(Number));
  204. else
  205. {
  206. Number f=0,tf=TEST_NUMBER;
  207. LoadBlock(L,&f,sizeof(f),Z);
  208. if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
  209. luaL_verror(L,"unknown number format in `%.255s':\n"
  210. " read " NUMBER_FMT "; expected " NUMBER_FMT,
  211. ZNAME(Z),f,tf);
  212. }
  213. }
  214. return native;
  215. }
  216. static Proto* LoadChunk (lua_State* L, ZIO* Z)
  217. {
  218. return LoadFunction(L,Z,LoadHeader(L,Z));
  219. }
  220. /*
  221. ** load one chunk from a file or buffer
  222. ** return main if ok and NULL at EOF
  223. */
  224. Proto* luaU_undump1 (lua_State* L, ZIO* Z)
  225. {
  226. int c=zgetc(Z);
  227. if (c==ID_CHUNK)
  228. return LoadChunk(L,Z);
  229. else if (c!=EOZ)
  230. luaL_verror(L,"`%.255s' is not a Lua binary file",ZNAME(Z));
  231. return NULL;
  232. }
  233. /*
  234. * convert number from text
  235. */
  236. double luaU_str2d (lua_State* L, const char* b, const char* where)
  237. {
  238. double x;
  239. if (!luaO_str2d(b,&x))
  240. luaL_verror(L,"cannot convert number `%.255s' in `%.255s'",b,where);
  241. return x;
  242. }