fixed endian support
[ekto.git] / src / lib / ekto_undump.c
blob0c3a88e5864f2e831b6c204f901dec940f6fabe1
1 /*
2 ** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $
3 ** load precompiled Ekto chunks
4 ** See Copyright Notice in ekto.h
5 */
7 #include <string.h>
9 #define lundump_c
10 #define EKTO_CORE
12 #include "Ekto.h"
14 #include "ekto_debug.h"
15 #include "ekto_do.h"
16 #include "ekto_func.h"
17 #include "ekto_mem.h"
18 #include "ekto_object.h"
19 #include "ekto_string.h"
20 #include "ekto_undump.h"
21 #include "ekto_zio.h"
23 typedef struct {
24 ekto_State* L;
25 ZIO* Z;
26 Mbuffer* b;
27 const char* name;
28 } LoadState;
30 #ifdef EKTOC_TRUST_BINARIES
31 #define IF(c,s)
32 #define error(S,s)
33 #else
34 #define IF(c,s) if (c) error(S,s)
36 static void error(LoadState* S, const char* why)
38 ektoO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
39 ektoD_throw(S->L,EKTO_ERRSYNTAX);
41 #endif
43 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
44 #define LoadByte(S) (lu_byte)LoadChar(S)
45 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
46 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
48 static void LoadBlock(LoadState* S, void* b, uint32_t size)
50 uint32_t r=ektoZ_read(S->Z,b,size);
51 IF (r!=0, "unexpected end");
54 static int LoadChar(LoadState* S)
56 char x;
57 LoadVar(S,x);
58 return x;
61 static int LoadInt(LoadState* S)
63 int x;
64 LoadVar(S,x);
65 uint32_t *y = &x;
66 #ifdef WORDS_BIGENDIAN
67 uint32_t z = bswap_32 (*y);
68 #else
69 uint32_t z = *y;
70 #endif
71 IF (z<0, "bad integer");
72 return *(&z);
75 static ekto_Number LoadNumber(LoadState* S)
77 ekto_Number x;
78 LoadVar(S,x);
79 uint64_t *y = &x;
80 #ifdef WORDS_BIGENDIAN
81 uint64_t z = bswap_64 (*y);
82 #else
83 uint64_t z = *y;
84 #endif
85 return *(&z);
88 static TString* LoadString(LoadState* S)
90 uint32_t size;
91 LoadVar(S,size);
92 if (size==0)
93 return NULL;
94 else
96 char* s=ektoZ_openspace(S->L,S->b,size);
97 LoadBlock(S,s,size);
98 return ektoS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
102 static void LoadCode(LoadState* S, Proto* f)
104 int n=LoadInt(S);
105 f->code=ektoM_newvector(S->L,n,Instruction);
106 f->sizecode=n;
107 LoadVector(S,f->code,n,sizeof(Instruction));
110 static Proto* LoadFunction(LoadState* S, TString* p);
112 static void LoadConstants(LoadState* S, Proto* f)
114 int i,n;
115 n=LoadInt(S);
116 f->k=ektoM_newvector(S->L,n,TValue);
117 f->sizek=n;
118 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
119 for (i=0; i<n; i++)
121 TValue* o=&f->k[i];
122 int t=LoadChar(S);
123 switch (t)
125 case EKTO_TNIL:
126 setnilvalue(o);
127 break;
128 case EKTO_TBOOLEAN:
129 setbvalue(o,LoadChar(S)!=0);
130 break;
131 case EKTO_TNUMBER:
132 setnvalue(o,LoadNumber(S));
133 break;
134 case EKTO_TSTRING:
135 setsvalue2n(S->L,o,LoadString(S));
136 break;
137 default:
138 error(S,"bad constant");
139 break;
142 n=LoadInt(S);
143 f->p=ektoM_newvector(S->L,n,Proto*);
144 f->sizep=n;
145 for (i=0; i<n; i++) f->p[i]=NULL;
146 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
149 static void LoadDebug(LoadState* S, Proto* f)
151 int i,n;
152 n=LoadInt(S);
153 f->lineinfo=ektoM_newvector(S->L,n,int);
154 f->sizelineinfo=n;
155 LoadVector(S,f->lineinfo,n,sizeof(int));
156 n=LoadInt(S);
157 f->locvars=ektoM_newvector(S->L,n,LocVar);
158 f->sizelocvars=n;
159 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
160 for (i=0; i<n; i++)
162 f->locvars[i].varname=LoadString(S);
163 f->locvars[i].startpc=LoadInt(S);
164 f->locvars[i].endpc=LoadInt(S);
166 n=LoadInt(S);
167 f->upvalues=ektoM_newvector(S->L,n,TString*);
168 f->sizeupvalues=n;
169 for (i=0; i<n; i++) f->upvalues[i]=NULL;
170 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
173 static Proto* LoadFunction(LoadState* S, TString* p)
175 Proto* f;
176 if (++S->L->nCcalls > EKTOI_MAXCCALLS) error(S,"code too deep");
177 f=ektoF_newproto(S->L);
178 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
179 f->source=LoadString(S); if (f->source==NULL) f->source=p;
180 f->linedefined=LoadInt(S);
181 f->lastlinedefined=LoadInt(S);
182 f->nups=LoadByte(S);
183 f->numparams=LoadByte(S);
184 f->is_vararg=LoadByte(S);
185 f->maxstacksize=LoadByte(S);
186 LoadCode(S,f);
187 LoadConstants(S,f);
188 LoadDebug(S,f);
189 IF (!ektoG_checkcode(f), "bad code");
190 S->L->top--;
191 S->L->nCcalls--;
192 return f;
195 static void LoadHeader(LoadState* S)
197 char h[EKTOC_HEADERSIZE];
198 char s[EKTOC_HEADERSIZE];
199 ektoU_header(h);
200 LoadBlock(S,s,EKTOC_HEADERSIZE);
201 IF (memcmp(h,s,EKTOC_HEADERSIZE)!=0, "bad header");
205 ** load precompiled chunk
207 Proto* ektoU_undump (ekto_State* L, ZIO* Z, Mbuffer* buff, const char* name)
209 LoadState S;
210 if (*name=='@' || *name=='=')
211 S.name=name+1;
212 else if (*name==EKTO_SIGNATURE[0])
213 S.name="binary string";
214 else
215 S.name=name;
216 S.L=L;
217 S.Z=Z;
218 S.b=buff;
219 LoadHeader(&S);
220 return LoadFunction(&S,ektoS_newliteral(L,"=?"));
224 * make header
226 void ektoU_header (char* h)
228 int x=0;
229 memcpy(h,EKTO_SIGNATURE,sizeof(EKTO_SIGNATURE)-1);
230 h+=sizeof(EKTO_SIGNATURE)-1;
231 *h++=(char)EKTOC_VERSION;
232 *h++=(char)EKTOC_FORMAT;
233 *h++=(char)*(char*)&x; /* endianness */
234 *h++=(char)sizeof(int);
235 *h++=(char)sizeof(uint32_t);
236 *h++=(char)sizeof(Instruction);
237 *h++=(char)sizeof(ekto_Number);
238 *h++=(char)(((ekto_Number)0.5)==0); /* is ekto_Number integral? */