lundump.c 6.5 KB

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