undump.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ** undump.c
  3. ** load bytecodes from files
  4. */
  5. char *rcs_undump="$Id: undump.c,v 1.5 1996/02/26 19:44:17 lhf Exp lhf $";
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "luac.h"
  9. static void warn(char *s) /* TODO: remove */
  10. {
  11. fprintf(stderr,"luac: %s\n",s);
  12. }
  13. static void panic(char *s) /* TODO: remove */
  14. {
  15. warn(s);
  16. exit(1);
  17. }
  18. static void Unthread(Byte *code, int i, int v)
  19. {
  20. while (i!=0)
  21. {
  22. CodeWord c;
  23. Byte *p=code+i;
  24. get_word(c,p);
  25. i=c.w;
  26. c.w=v;
  27. p[-2]=c.m.c1;
  28. p[-1]=c.m.c2;
  29. }
  30. }
  31. static int LoadWord(FILE *D)
  32. {
  33. Word w;
  34. fread(&w,sizeof(w),1,D);
  35. return w;
  36. }
  37. static char* LoadBlock(int size, FILE *D)
  38. {
  39. char *b=luaI_malloc(size);
  40. fread(b,size,1,D);
  41. return b;
  42. }
  43. static char* LoadString(FILE *D)
  44. {
  45. return LoadBlock(LoadWord(D),D);
  46. }
  47. static TFunc *Main=NULL;
  48. static TFunc *lastF=NULL;
  49. static void LoadFunction(FILE *D)
  50. {
  51. TFunc *tf=new(TFunc);
  52. tf->next=NULL;
  53. tf->size=LoadWord(D); /* TODO: Long? */
  54. tf->lineDefined=LoadWord(D);
  55. if (IsMain(tf)) /* new main */
  56. {
  57. tf->fileName=LoadString(D);
  58. Main=lastF=tf;
  59. }
  60. else /* fix PUSHFUNCTION */
  61. {
  62. CodeCode c;
  63. Byte *p;
  64. tf->marked=LoadWord(D);
  65. tf->fileName=Main->fileName;
  66. p=Main->code+tf->marked; /* TODO: tf->marked=? */
  67. c.tf=tf;
  68. *p++=c.m.c1; *p++=c.m.c2; *p++=c.m.c3; *p++=c.m.c4;
  69. lastF->next=tf; lastF=tf;
  70. }
  71. tf->code=LoadBlock(tf->size,D);
  72. while (1) /* unthread */
  73. {
  74. int c=getc(D);
  75. if (c=='V')
  76. {
  77. int i=LoadWord(D);
  78. char *s=LoadString(D);
  79. int v=luaI_findsymbolbyname(s); /* TODO: free s? */
  80. Unthread(tf->code,i,v);
  81. }
  82. else if (c=='S')
  83. {
  84. int i=LoadWord(D);
  85. char *s=LoadString(D);
  86. int v=luaI_findconstantbyname(s); /* TODO: free s? */
  87. Unthread(tf->code,i,v);
  88. }
  89. else
  90. {
  91. ungetc(c,D);
  92. return;
  93. }
  94. }
  95. }
  96. static void LoadHeader(FILE *D) /* TODO: error handling */
  97. {
  98. char *s=LoadString(D);
  99. Word w,tw=TEST_WORD;
  100. float f,tf=TEST_FLOAT;
  101. if (strcmp(s,SIGNATURE)!=0) panic("bad signature");
  102. luaI_free(s);
  103. getc(D); /* skip version */
  104. fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */
  105. if (w!=tw) warn("different byte order");
  106. fread(&f,sizeof(f),1,D); /* a float for testing byte ordering */
  107. if (f!=tf) warn("different float representation");
  108. }
  109. static void LoadChunk(FILE *D)
  110. {
  111. LoadHeader(D);
  112. while (1)
  113. {
  114. int c=getc(D);
  115. if (c=='F') LoadFunction(D); else { ungetc(c,D); break; }
  116. }
  117. #if 1
  118. { /* TODO: run Main? */
  119. TFunc *tf;
  120. for (tf=Main; tf!=NULL; tf=tf->next)
  121. PrintFunction(tf);
  122. }
  123. #endif
  124. }
  125. void luaI_undump(FILE *D)
  126. {
  127. while (1)
  128. {
  129. int c=getc(D);
  130. if (c==ESC) LoadChunk(D); else
  131. if (c==EOF) return; else
  132. panic("not a lua binary file");
  133. }
  134. }
  135. int main(int argc, char* argv[])
  136. {
  137. FILE *f=freopen("luac.out","rb",stdin);
  138. if (f==NULL)
  139. {
  140. fprintf(stderr,"undump: cannot open ");
  141. perror("luac.out");
  142. exit(1);
  143. }
  144. luaI_undump(stdin);
  145. return 0;
  146. }