undump.c 5.1 KB

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