summaryrefslogtreecommitdiff
path: root/lundump.c
blob: 225eda2856493991f73ca80ccbf181f6c10cb6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
** $Id: lundump.c,v 1.12 1999/07/08 12:43:23 roberto Exp roberto $
** load bytecodes from files
** See Copyright Notice in lua.h
*/

#include <stdio.h>
#include <string.h>
#include "lauxlib.h"
#include "lfunc.h"
#include "lmem.h"
#include "lopcodes.h"
#include "lstring.h"
#include "lundump.h"

#define	LoadBlock(b,size,Z)	ezread(Z,b,size)

static void unexpectedEOZ (ZIO* Z)
{
 luaL_verror("unexpected end of file in %s",zname(Z));
}

static int ezgetc (ZIO* Z)
{
 int c=zgetc(Z);
 if (c==EOZ) unexpectedEOZ(Z);
 return c;
}

static void ezread (ZIO* Z, void* b, int n)
{
 int r=zread(Z,b,n);
 if (r!=0) unexpectedEOZ(Z);
}

static unsigned int LoadWord (ZIO* Z)
{
 unsigned int hi=ezgetc(Z);
 unsigned int lo=ezgetc(Z);
 return (hi<<8)|lo;
}

static unsigned long LoadLong (ZIO* Z)
{
 unsigned long hi=LoadWord(Z);
 unsigned long lo=LoadWord(Z);
 return (hi<<16)|lo;
}

/*
* convert number from text
*/
double luaU_str2d (const char* b, const char* where)
{
 int negative=(b[0]=='-');
 double x=luaO_str2d(b+negative);
 if (x<0) luaL_verror("cannot convert number '%s' in %s",b,where);
 return negative ? -x : x;
}

static real LoadNumber (ZIO* Z, int native)
{
 real x;
 if (native)
 {
  LoadBlock(&x,sizeof(x),Z);
  return x;
 }
 else
 {
  char b[256];
  int size=ezgetc(Z);
  LoadBlock(b,size,Z);
  b[size]=0;
  return luaU_str2d(b,zname(Z));
 }
}

static int LoadInt (ZIO* Z, const char* message)
{
 unsigned long l=LoadLong(Z);
 unsigned int i=l;
 if (i!=l) luaL_verror(message,l,zname(Z));
 return i;
}

#define PAD	5			/* two word operands plus opcode */

static Byte* LoadCode (ZIO* Z)
{
 int size=LoadInt(Z,"code too long (%ld bytes) in %s");
 Byte* b=luaM_malloc(size+PAD);
 LoadBlock(b,size,Z);
 if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z));
 memset(b+size,ENDCODE,PAD);		/* pad code for safety */
 return b;
}

static TaggedString* LoadTString (ZIO* Z)
{
 long size=LoadLong(Z);
 if (size==0)
  return NULL;
 else
 {
  char* s=luaL_openspace(size);
  LoadBlock(s,size,Z);
  return luaS_newlstr(s,size-1);
 }
}

static void LoadLocals (TProtoFunc* tf, ZIO* Z)
{
 int i,n=LoadInt(Z,"too many locals (%ld) in %s");
 if (n==0) return;
 tf->locvars=luaM_newvector(n+1,LocVar);
 for (i=0; i<n; i++)
 {
  tf->locvars[i].line=LoadInt(Z,"too many lines (%ld) in %s");
  tf->locvars[i].varname=LoadTString(Z);
 }
 tf->locvars[i].line=-1;		/* flag end of vector */
 tf->locvars[i].varname=NULL;
}

static TProtoFunc* LoadFunction (ZIO* Z, int native);

static void LoadConstants (TProtoFunc* tf, ZIO* Z, int native)
{
 int i,n=LoadInt(Z,"too many constants (%ld) in %s");
 tf->nconsts=n;
 if (n==0) return;
 tf->consts=luaM_newvector(n,TObject);
 for (i=0; i<n; i++)
 {
  TObject* o=tf->consts+i;
  ttype(o)=-ezgetc(Z);			/* ttype(o) is negative - ORDER LUA_T */
  switch (ttype(o))
  {
   case LUA_T_NUMBER:
	nvalue(o)=LoadNumber(Z,native);
	break;
   case LUA_T_STRING:
	tsvalue(o)=LoadTString(Z);
	break;
   case LUA_T_PROTO:
	tfvalue(o)=LoadFunction(Z,native);
	break;
   case LUA_T_NIL:
	break;
   default:				/* cannot happen */
	luaU_badconstant("load",i,o,tf);
	break;
  }
 }
}

static TProtoFunc* LoadFunction (ZIO* Z, int native)
{
 TProtoFunc* tf=luaF_newproto();
 tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s");
 tf->source=LoadTString(Z);
 if (tf->source==NULL) tf->source=luaS_new(zname(Z));
 tf->code=LoadCode(Z);
 LoadLocals(tf,Z);
 LoadConstants(tf,Z,native);
 return tf;
}

static void LoadSignature (ZIO* Z)
{
 const char* s=SIGNATURE;
 while (*s!=0 && ezgetc(Z)==*s)
  ++s;
 if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
}

static int LoadHeader (ZIO* Z)
{
 int version,sizeofR;
 int native;
 LoadSignature(Z);
 version=ezgetc(Z);
 if (version>VERSION)
  luaL_verror(
	"%s too new: version=0x%02x; expected at most 0x%02x",
	zname(Z),version,VERSION);
 if (version<VERSION0)			/* check last major change */
  luaL_verror(
	"%s too old: version=0x%02x; expected at least 0x%02x",
	zname(Z),version,VERSION0);
 sizeofR=ezgetc(Z);
 native=(sizeofR!=0);
 if (native)				/* test number representation */
 {
  if (sizeofR!=sizeof(real))
   luaL_verror("unknown number size in %s: read %d; expected %d",
	 zname(Z),sizeofR,sizeof(real));
  else
  {
   real tf=TEST_NUMBER;
   real f=LoadNumber(Z,native);
   if ((long)f!=(long)tf)
    luaL_verror("unknown number format in %s: "
	  "read " NUMBER_FMT "; expected " NUMBER_FMT,
	  zname(Z),f,tf);
  }
 }
 return native;
}

static TProtoFunc* LoadChunk (ZIO* Z)
{
 return LoadFunction(Z,LoadHeader(Z));
}

/*
** load one chunk from a file or buffer
** return main if ok and NULL at EOF
*/
TProtoFunc* luaU_undump1 (ZIO* Z)
{
 int c=zgetc(Z);
 if (c==ID_CHUNK)
  return LoadChunk(Z);
 else if (c!=EOZ)
  luaL_verror("%s is not a Lua binary file",zname(Z));
 return NULL;
}

/*
* handle constants that cannot happen
*/
void luaU_badconstant (const char* s, int i, const TObject* o, TProtoFunc* tf)
{
 int t=ttype(o);
 const char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
 luaL_verror("cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
}