undump.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. ** undump.c
  3. ** load bytecodes from files
  4. */
  5. char* rcs_undump="$Id: undump.c,v 1.25 1997/07/29 19:44:02 roberto Exp roberto $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "auxlib.h"
  9. #include "opcode.h"
  10. #include "luamem.h"
  11. #include "table.h"
  12. #include "undump.h"
  13. #include "zio.h"
  14. static int swapword=0;
  15. static int swapfloat=0;
  16. static TFunc* Main=NULL; /* functions in a chunk */
  17. static TFunc* lastF=NULL;
  18. static void FixCode(Byte* code, Byte* end) /* swap words */
  19. {
  20. Byte* p;
  21. for (p=code; p!=end;)
  22. {
  23. int op=*p;
  24. switch (op)
  25. {
  26. case PUSHNIL:
  27. case PUSH0:
  28. case PUSH1:
  29. case PUSH2:
  30. case PUSHLOCAL0:
  31. case PUSHLOCAL1:
  32. case PUSHLOCAL2:
  33. case PUSHLOCAL3:
  34. case PUSHLOCAL4:
  35. case PUSHLOCAL5:
  36. case PUSHLOCAL6:
  37. case PUSHLOCAL7:
  38. case PUSHLOCAL8:
  39. case PUSHLOCAL9:
  40. case PUSHINDEXED:
  41. case STORELOCAL0:
  42. case STORELOCAL1:
  43. case STORELOCAL2:
  44. case STORELOCAL3:
  45. case STORELOCAL4:
  46. case STORELOCAL5:
  47. case STORELOCAL6:
  48. case STORELOCAL7:
  49. case STORELOCAL8:
  50. case STORELOCAL9:
  51. case STOREINDEXED0:
  52. case ADJUST0:
  53. case EQOP:
  54. case LTOP:
  55. case LEOP:
  56. case GTOP:
  57. case GEOP:
  58. case ADDOP:
  59. case SUBOP:
  60. case MULTOP:
  61. case DIVOP:
  62. case POWOP:
  63. case CONCOP:
  64. case MINUSOP:
  65. case NOTOP:
  66. case RETCODE0:
  67. p++;
  68. break;
  69. case PUSHBYTE:
  70. case PUSHLOCAL:
  71. case STORELOCAL:
  72. case STOREINDEXED:
  73. case STORELIST0:
  74. case ADJUST:
  75. case RETCODE:
  76. case VARARGS:
  77. case STOREMAP:
  78. p+=2;
  79. break;
  80. case STORELIST:
  81. case CALLFUNC:
  82. p+=3;
  83. break;
  84. case PUSHWORD:
  85. case PUSHSELF:
  86. case CREATEARRAY:
  87. case ONTJMP:
  88. case ONFJMP:
  89. case JMP:
  90. case UPJMP:
  91. case IFFJMP:
  92. case IFFUPJMP:
  93. case SETLINE:
  94. case PUSHGLOBAL:
  95. case STOREGLOBAL:
  96. {
  97. Byte t;
  98. t=p[1]; p[1]=p[2]; p[2]=t;
  99. p+=3;
  100. break;
  101. }
  102. case STORERECORD:
  103. {
  104. int n=*++p;
  105. p++;
  106. while (n--)
  107. {
  108. Byte t;
  109. t=p[0]; p[0]=p[1]; p[1]=t;
  110. p+=2;
  111. }
  112. break;
  113. }
  114. default:
  115. luaL_verror("corrupt binary file: bad opcode %d at %d\n",
  116. op,(int)(p-code));
  117. break;
  118. }
  119. }
  120. }
  121. static void Unthread(Byte* code, int i, int v)
  122. {
  123. while (i!=0)
  124. {
  125. Word w;
  126. Byte* p=code+i;
  127. memcpy(&w,p,sizeof(w));
  128. i=w; w=v;
  129. memcpy(p,&w,sizeof(w));
  130. }
  131. }
  132. static int LoadWord(ZIO* Z)
  133. {
  134. Word w;
  135. zread(Z,&w,sizeof(w));
  136. if (swapword)
  137. {
  138. Byte* p=(Byte*)&w;
  139. Byte t;
  140. t=p[0]; p[0]=p[1]; p[1]=t;
  141. }
  142. return w;
  143. }
  144. static int LoadSize(ZIO* Z)
  145. {
  146. Word hi=LoadWord(Z);
  147. Word lo=LoadWord(Z);
  148. int s=(hi<<16)|lo;
  149. if ((Word)s != s) lua_error("code too long");
  150. return s;
  151. }
  152. static void* LoadBlock(int size, ZIO* Z)
  153. {
  154. void* b=luaI_malloc(size);
  155. zread(Z,b,size);
  156. return b;
  157. }
  158. static char* LoadString(ZIO* Z)
  159. {
  160. int size=LoadWord(Z);
  161. char *b=luaI_buffer(size);
  162. zread(Z,b,size);
  163. return b;
  164. }
  165. static char* LoadNewString(ZIO* Z)
  166. {
  167. return LoadBlock(LoadWord(Z),Z);
  168. }
  169. static void LoadFunction(ZIO* Z)
  170. {
  171. int size;
  172. TFunc* tf=new(TFunc);
  173. tf->next=NULL;
  174. tf->locvars=NULL;
  175. size=LoadSize(Z);
  176. tf->lineDefined=LoadWord(Z);
  177. if (IsMain(tf)) /* new main */
  178. {
  179. tf->fileName=LoadNewString(Z);
  180. Main=lastF=tf;
  181. }
  182. else /* fix PUSHFUNCTION */
  183. {
  184. tf->marked=LoadWord(Z);
  185. tf->fileName=Main->fileName;
  186. memcpy(Main->code+tf->marked,&tf,sizeof(tf));
  187. lastF=lastF->next=tf;
  188. }
  189. tf->code=LoadBlock(size,Z);
  190. if (swapword || swapfloat) FixCode(tf->code,tf->code+size);
  191. while (1) /* unthread */
  192. {
  193. int c=zgetc(Z);
  194. if (c==ID_VAR) /* global var */
  195. {
  196. int i=LoadWord(Z);
  197. char* s=LoadString(Z);
  198. int v=luaI_findsymbolbyname(s);
  199. Unthread(tf->code,i,v);
  200. }
  201. else if (c==ID_STR) /* constant string */
  202. {
  203. int i=LoadWord(Z);
  204. char* s=LoadString(Z);
  205. int v; /*=luaI_findconstantbyname(s); ??????? */
  206. Unthread(tf->code,i,v);
  207. }
  208. else
  209. {
  210. zungetc(Z);
  211. break;
  212. }
  213. }
  214. }
  215. static void LoadSignature(ZIO* Z)
  216. {
  217. char* s=SIGNATURE;
  218. while (*s!=0 && zgetc(Z)==*s)
  219. ++s;
  220. if (*s!=0) lua_error("cannot load binary file: bad signature");
  221. }
  222. static void LoadHeader(ZIO* Z)
  223. {
  224. Word w,tw=TEST_WORD;
  225. float f,tf=TEST_FLOAT;
  226. int version;
  227. LoadSignature(Z);
  228. version=zgetc(Z);
  229. if (version>0x23) /* after 2.5 */
  230. {
  231. int oldsizeofW=zgetc(Z);
  232. int oldsizeofF=zgetc(Z);
  233. int oldsizeofP=zgetc(Z);
  234. if (oldsizeofW!=2)
  235. luaL_verror(
  236. "cannot load binary file created on machine with sizeof(Word)=%d; "
  237. "expected 2",oldsizeofW);
  238. if (oldsizeofF!=4)
  239. luaL_verror(
  240. "cannot load binary file created on machine with sizeof(float)=%d; "
  241. "expected 4\nnot an IEEE machine?",oldsizeofF);
  242. if (oldsizeofP!=sizeof(TFunc*)) /* TODO: pack? */
  243. luaL_verror(
  244. "cannot load binary file created on machine with sizeof(TFunc*)=%d; "
  245. "expected %d",oldsizeofP,(int)sizeof(TFunc*));
  246. }
  247. zread(Z,&w,sizeof(w)); /* test word */
  248. if (w!=tw)
  249. {
  250. swapword=1;
  251. }
  252. zread(Z,&f,sizeof(f)); /* test float */
  253. if (f!=tf)
  254. {
  255. Byte* p=(Byte*)&f;
  256. Byte t;
  257. swapfloat=1;
  258. t=p[0]; p[0]=p[3]; p[3]=t;
  259. t=p[1]; p[1]=p[2]; p[2]=t;
  260. if (f!=tf) /* TODO: try another perm? */
  261. lua_error("cannot load binary file: unknown float representation");
  262. }
  263. }
  264. static void LoadChunk(ZIO* Z)
  265. {
  266. LoadHeader(Z);
  267. while (1)
  268. {
  269. int c=zgetc(Z);
  270. if (c==ID_FUN) LoadFunction(Z); else { zungetc(Z); break; }
  271. }
  272. }
  273. /*
  274. ** load one chunk from a file.
  275. ** return list of functions found, headed by main, or NULL at EOF.
  276. */
  277. TFunc* luaI_undump1(ZIO* Z)
  278. {
  279. int c=zgetc(Z);
  280. if (c==ID_CHUNK)
  281. {
  282. LoadChunk(Z);
  283. return Main;
  284. }
  285. else if (c!=EOZ)
  286. lua_error("not a lua binary file");
  287. return NULL;
  288. }
  289. /*
  290. ** load and run all chunks in a file
  291. */
  292. int luaI_undump(ZIO* Z)
  293. {
  294. TFunc* m;
  295. while ((m=luaI_undump1(Z)))
  296. {
  297. int status=luaI_dorun(m);
  298. /* luaI_freefunc(m); ???*/
  299. if (status!=0) return status;
  300. }
  301. return 0;
  302. }