lundump.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. ** $Id: lundump.c,v 1.28 2000/04/24 19:32:58 lhf Exp $
  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. int isglobal=LoadByte(L,Z);
  141. if (isglobal) luaS_assertglobal(L,s);
  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. }