diff options
author | Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> | 1996-03-05 22:41:18 -0300 |
---|---|---|
committer | Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br> | 1996-03-05 22:41:18 -0300 |
commit | 852b91946547a2fdaf971705ddd5ee854fccbf9c (patch) | |
tree | caf1efdebe830fbeb60a8d4a138e1596a9e50ebe | |
parent | ef94999647561f4c043dc8b529618a8a1f20d51d (diff) | |
download | lua-852b91946547a2fdaf971705ddd5ee854fccbf9c.tar.gz lua-852b91946547a2fdaf971705ddd5ee854fccbf9c.tar.bz2 lua-852b91946547a2fdaf971705ddd5ee854fccbf9c.zip |
now swaps bytes (but need to make it more robust)
-rw-r--r-- | undump.c | 29 |
1 files changed, 24 insertions, 5 deletions
@@ -3,12 +3,15 @@ | |||
3 | ** load bytecodes from files | 3 | ** load bytecodes from files |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_undump="$Id: undump.c,v 1.6 1996/02/28 23:10:46 lhf Exp lhf $"; | 6 | char *rcs_undump="$Id: undump.c,v 1.7 1996/03/01 03:43:50 lhf Exp lhf $"; |
7 | 7 | ||
8 | #include <stdio.h> | 8 | #include <stdio.h> |
9 | #include <string.h> | 9 | #include <string.h> |
10 | #include "luac.h" | 10 | #include "luac.h" |
11 | 11 | ||
12 | static int swapword=0; | ||
13 | static int swapfloat=0; | ||
14 | |||
12 | static void warn(char *s) /* TODO: remove */ | 15 | static void warn(char *s) /* TODO: remove */ |
13 | { | 16 | { |
14 | fprintf(stderr,"undump: %s\n",s); | 17 | fprintf(stderr,"undump: %s\n",s); |
@@ -38,6 +41,12 @@ static int LoadWord(FILE *D) | |||
38 | { | 41 | { |
39 | Word w; | 42 | Word w; |
40 | fread(&w,sizeof(w),1,D); | 43 | fread(&w,sizeof(w),1,D); |
44 | if (swapword) | ||
45 | { | ||
46 | Byte *p=&w; | ||
47 | Byte t; | ||
48 | t=p[0]; p[0]=p[1]; p[1]=t; | ||
49 | } | ||
41 | return w; | 50 | return w; |
42 | } | 51 | } |
43 | 52 | ||
@@ -82,6 +91,7 @@ static void LoadFunction(FILE *D) | |||
82 | tf->marked=0; /* TODO: is this ok? */ | 91 | tf->marked=0; /* TODO: is this ok? */ |
83 | #endif | 92 | #endif |
84 | tf->code=LoadBlock(tf->size,D); | 93 | tf->code=LoadBlock(tf->size,D); |
94 | if (swapword || swapfloat) FixCode(tf->code,tf->code+tf->size); | ||
85 | while (1) /* unthread */ | 95 | while (1) /* unthread */ |
86 | { | 96 | { |
87 | int c=getc(D); | 97 | int c=getc(D); |
@@ -135,9 +145,17 @@ static void LoadHeader(FILE *D) /* TODO: error handling */ | |||
135 | LoadSignature(D); | 145 | LoadSignature(D); |
136 | getc(D); /* skip version */ | 146 | getc(D); /* skip version */ |
137 | fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */ | 147 | fread(&w,sizeof(w),1,D); /* a word for testing byte ordering */ |
138 | if (w!=tw) warn("different byte order"); | 148 | if (w!=tw) |
149 | { | ||
150 | swapword=1; | ||
151 | warn("different byte order"); | ||
152 | } | ||
139 | fread(&f,sizeof(f),1,D); /* a float for testing byte ordering */ | 153 | fread(&f,sizeof(f),1,D); /* a float for testing byte ordering */ |
140 | if (f!=tf) warn("different float representation"); | 154 | if (f!=tf) |
155 | { | ||
156 | swapfloat=1; | ||
157 | if (f!=tf) warn("different float representation"); | ||
158 | } | ||
141 | } | 159 | } |
142 | 160 | ||
143 | static void LoadChunk(FILE *D) | 161 | static void LoadChunk(FILE *D) |
@@ -173,11 +191,12 @@ void luaI_undump(FILE *D) | |||
173 | 191 | ||
174 | int main(int argc, char* argv[]) | 192 | int main(int argc, char* argv[]) |
175 | { | 193 | { |
176 | FILE *f=freopen("luac.out","rb",stdin); | 194 | char* fn=(argc>1)? argv[1] : "luac.out"; |
195 | FILE *f=freopen(fn,"rb",stdin); | ||
177 | if (f==NULL) | 196 | if (f==NULL) |
178 | { | 197 | { |
179 | fprintf(stderr,"undump: cannot open "); | 198 | fprintf(stderr,"undump: cannot open "); |
180 | perror("luac.out"); | 199 | perror(fn); |
181 | exit(1); | 200 | exit(1); |
182 | } | 201 | } |
183 | luaI_undump(stdin); | 202 | luaI_undump(stdin); |