diff options
| -rw-r--r-- | undump.c | 134 |
1 files changed, 134 insertions, 0 deletions
diff --git a/undump.c b/undump.c new file mode 100644 index 00000000..c3abd366 --- /dev/null +++ b/undump.c | |||
| @@ -0,0 +1,134 @@ | |||
| 1 | /* | ||
| 2 | ** undump.c | ||
| 3 | ** load bytecodes from files | ||
| 4 | */ | ||
| 5 | |||
| 6 | char *rcs_undump="$Id$"; | ||
| 7 | |||
| 8 | #include <stdio.h> | ||
| 9 | #include <string.h> | ||
| 10 | #include "luac.h" | ||
| 11 | |||
| 12 | static void warn(char *s) | ||
| 13 | { | ||
| 14 | fprintf(stderr,"luac: %s\n",s); | ||
| 15 | } | ||
| 16 | |||
| 17 | static void panic(char *s) | ||
| 18 | { | ||
| 19 | warn(s); | ||
| 20 | exit(1); | ||
| 21 | } | ||
| 22 | |||
| 23 | static void Unthread(Byte *p, int i, int v) | ||
| 24 | { | ||
| 25 | while (i!=0) | ||
| 26 | { | ||
| 27 | CodeWord c; | ||
| 28 | Byte *q=p+i; | ||
| 29 | get_word(c,q); | ||
| 30 | q=p+i; | ||
| 31 | i=c.w; | ||
| 32 | c.w=v; | ||
| 33 | q[0]=c.m.c1; | ||
| 34 | q[1]=c.m.c2; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | |||
| 38 | static char* LoadBlock(int size, FILE *D) | ||
| 39 | { | ||
| 40 | char *b=luaI_malloc(size); | ||
| 41 | fread(b,size,1,D); | ||
| 42 | return b; | ||
| 43 | } | ||
| 44 | |||
| 45 | static int LoadWord(FILE *D) | ||
| 46 | { | ||
| 47 | Word w; | ||
| 48 | fread(&w,sizeof(w),1,D); | ||
| 49 | return w; | ||
| 50 | } | ||
| 51 | |||
| 52 | static char* LoadString(FILE *D) | ||
| 53 | { | ||
| 54 | return LoadBlock(LoadWord(D),D); | ||
| 55 | } | ||
| 56 | |||
| 57 | static char* LoadCode(int size, FILE *D) | ||
| 58 | { | ||
| 59 | return LoadBlock(size,D); | ||
| 60 | } | ||
| 61 | |||
| 62 | static TFunc* LoadFunction(FILE *D) | ||
| 63 | { | ||
| 64 | TFunc *tf=new(TFunc); | ||
| 65 | tf->size=LoadWord(D); | ||
| 66 | tf->marked=LoadWord(D); | ||
| 67 | tf->lineDefined=LoadWord(D); | ||
| 68 | tf->fileName=LoadString(D); | ||
| 69 | tf->code=LoadCode(tf->size,D); | ||
| 70 | while (1) | ||
| 71 | { | ||
| 72 | int c=getc(D); | ||
| 73 | if (c=='V') | ||
| 74 | { | ||
| 75 | int i=LoadWord(D); | ||
| 76 | char *s=LoadString(D); | ||
| 77 | int v=luaI_findsymbolbyname(s); | ||
| 78 | Unthread(tf->code,i,v); | ||
| 79 | } | ||
| 80 | else if (c=='S') | ||
| 81 | { | ||
| 82 | int i=LoadWord(D); | ||
| 83 | char *s=LoadString(D); | ||
| 84 | int v=luaI_findconstantbyname(s); | ||
| 85 | Unthread(tf->code,i,v); | ||
| 86 | } | ||
| 87 | else | ||
| 88 | { | ||
| 89 | PrintFunction(tf); | ||
| 90 | ungetc(c,D); | ||
| 91 | return tf; | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | static void LoadHeader(FILE *D) | ||
| 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 | getc(D); /* skip version */ | ||
| 103 | fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */ | ||
| 104 | if (w!=tw) warn("different byte order"); | ||
| 105 | fread(&f,sizeof(f),1,D); /* a float for testing byte ordering */ | ||
| 106 | if (f!=tf) warn("different float representation"); | ||
| 107 | } | ||
| 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); return; } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | void Undump(FILE *D) | ||
| 120 | { | ||
| 121 | while (1) | ||
| 122 | { | ||
| 123 | int c=getc(D); | ||
| 124 | if (c==ESC) LoadChunk(D); else | ||
| 125 | if (c==EOF) return; else | ||
| 126 | panic("not a lua binary file"); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | int main(int argc, char* argv[]) | ||
| 131 | { | ||
| 132 | Undump(stdin); | ||
| 133 | return 0; | ||
| 134 | } | ||
