lundump.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. ** $Id: lundump.c,v 1.20 2000/04/25 16:44:31 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_REENTRANT
  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. int 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. int i,n=LoadInt(L,Z,"too many locals");
  119. if (n==0) return;
  120. tf->locvars=luaM_newvector(L,n+1,LocVar);
  121. for (i=0; i<n; i++)
  122. {
  123. tf->locvars[i].line=LoadInt(L,Z,"too many lines");
  124. tf->locvars[i].varname=LoadString(L,Z);
  125. }
  126. tf->locvars[i].line=-1; /* flag end of vector */
  127. tf->locvars[i].varname=NULL;
  128. }
  129. static Proto* LoadFunction (lua_State* L, ZIO* Z, int native);
  130. static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
  131. {
  132. int i,n;
  133. tf->nkstr=n=LoadInt(L,Z,"too many strings");
  134. if (n>0)
  135. {
  136. tf->kstr=luaM_newvector(L,n,TString*);
  137. for (i=0; i<n; i++)
  138. {
  139. TString* s=LoadString(L,Z);
  140. LoadByte(L,Z);
  141. tf->kstr[i]=s;
  142. }
  143. }
  144. tf->nknum=n=LoadInt(L,Z,"too many numbers");
  145. if (n>0)
  146. {
  147. tf->knum=luaM_newvector(L,n,Number);
  148. if (native)
  149. LoadBlock(L,tf->knum,n*sizeof(*tf->knum),Z);
  150. else
  151. for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
  152. }
  153. tf->nkproto=n=LoadInt(L,Z,"too many functions");
  154. if (n>0)
  155. {
  156. tf->kproto=luaM_newvector(L,n,Proto*);
  157. for (i=0; i<n; i++) tf->kproto[i]=LoadFunction(L,Z,native);
  158. }
  159. }
  160. static Proto* LoadFunction (lua_State* L, ZIO* Z, int native)
  161. {
  162. Proto* tf=luaF_newproto(L);
  163. tf->source=LoadString(L,Z);
  164. if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
  165. tf->lineDefined=LoadInt(L,Z,"lineDefined too large");
  166. tf->numparams=LoadInt(L,Z,"too many parameters");
  167. tf->is_vararg=LoadByte(L,Z);
  168. tf->maxstacksize=LoadInt(L,Z,"too much stack needed");
  169. LoadCode(L,tf,Z);
  170. LoadLocals(L,tf,Z);
  171. LoadConstants(L,tf,Z,native);
  172. return tf;
  173. }
  174. static void LoadSignature (lua_State* L, ZIO* Z)
  175. {
  176. const char* s=SIGNATURE;
  177. while (*s!=0 && ezgetc(L,Z)==*s)
  178. ++s;
  179. if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
  180. }
  181. #define V(v) v/16,v%16
  182. static int LoadHeader (lua_State* L, ZIO* Z)
  183. {
  184. int version,sizeofR,native;
  185. LoadSignature(L,Z);
  186. version=ezgetc(L,Z);
  187. if (version>VERSION)
  188. luaL_verror(L,"`%.255s' too new:\n"
  189. " read version %d.%d; expected at most %d.%d",
  190. ZNAME(Z),V(version),V(VERSION));
  191. if (version<VERSION0) /* check last major change */
  192. luaL_verror(L,"`%.255s' too old:\n"
  193. " read version %d.%d; expected at least %d.%d",
  194. ZNAME(Z),V(version),V(VERSION));
  195. {
  196. int I=ezgetc(L,Z);
  197. int i=ezgetc(L,Z);
  198. int op=ezgetc(L,Z);
  199. int b=ezgetc(L,Z);
  200. if (I!=sizeof(Instruction) || i!=SIZE_INSTRUCTION || op!=SIZE_OP || b!=SIZE_B)
  201. luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
  202. " read %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments;\n"
  203. " expected %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments",
  204. ZNAME(Z),i,I,op,b,SIZE_INSTRUCTION,(int)sizeof(Instruction),SIZE_OP,SIZE_B);
  205. }
  206. sizeofR=ezgetc(L,Z);
  207. native=(sizeofR!=0);
  208. if (native) /* test number representation */
  209. {
  210. if (sizeofR!=sizeof(Number))
  211. luaL_verror(L,"native number size mismatch in `%.255s':\n"
  212. " read %d; expected %d",
  213. ZNAME(Z),sizeofR,(int)sizeof(Number));
  214. else
  215. {
  216. Number f=0,tf=TEST_NUMBER;
  217. LoadBlock(L,&f,sizeof(f),Z);
  218. if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
  219. luaL_verror(L,"unknown number format in `%.255s':\n"
  220. " read " NUMBER_FMT "; expected " NUMBER_FMT,
  221. ZNAME(Z),f,tf);
  222. }
  223. }
  224. return native;
  225. }
  226. static Proto* LoadChunk (lua_State* L, ZIO* Z)
  227. {
  228. return LoadFunction(L,Z,LoadHeader(L,Z));
  229. }
  230. /*
  231. ** load one chunk from a file or buffer
  232. ** return main if ok and NULL at EOF
  233. */
  234. Proto* luaU_undump1 (lua_State* L, ZIO* Z)
  235. {
  236. int c=zgetc(Z);
  237. if (c==ID_CHUNK)
  238. return LoadChunk(L,Z);
  239. else if (c!=EOZ)
  240. luaL_verror(L,"`%.255s' is not a Lua binary file",ZNAME(Z));
  241. return NULL;
  242. }
  243. /*
  244. * convert number from text
  245. */
  246. double luaU_str2d (lua_State* L, const char* b, const char* where)
  247. {
  248. double x;
  249. if (!luaO_str2d(b,&x))
  250. luaL_verror(L,"cannot convert number `%.255s' in `%.255s'",b,where);
  251. return x;
  252. }