undump.c 5.6 KB

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