aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>1996-03-05 22:41:18 -0300
committerLuiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>1996-03-05 22:41:18 -0300
commit852b91946547a2fdaf971705ddd5ee854fccbf9c (patch)
treecaf1efdebe830fbeb60a8d4a138e1596a9e50ebe
parentef94999647561f4c043dc8b529618a8a1f20d51d (diff)
downloadlua-852b91946547a2fdaf971705ddd5ee854fccbf9c.tar.gz
lua-852b91946547a2fdaf971705ddd5ee854fccbf9c.tar.bz2
lua-852b91946547a2fdaf971705ddd5ee854fccbf9c.zip
now swaps bytes (but need to make it more robust)
-rw-r--r--undump.c29
1 files changed, 24 insertions, 5 deletions
diff --git a/undump.c b/undump.c
index d83875a7..3599d563 100644
--- a/undump.c
+++ b/undump.c
@@ -3,12 +3,15 @@
3** load bytecodes from files 3** load bytecodes from files
4*/ 4*/
5 5
6char *rcs_undump="$Id: undump.c,v 1.6 1996/02/28 23:10:46 lhf Exp lhf $"; 6char *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
12static int swapword=0;
13static int swapfloat=0;
14
12static void warn(char *s) /* TODO: remove */ 15static 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
143static void LoadChunk(FILE *D) 161static void LoadChunk(FILE *D)
@@ -173,11 +191,12 @@ void luaI_undump(FILE *D)
173 191
174int main(int argc, char* argv[]) 192int 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);