The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lundump.cpp
Go to the documentation of this file.
1 /*
2 ** load precompiled Lua chunks
3 ** See Copyright Notice in lua.h
4 */
5 
6 #include <string.h>
7 
8 #define lundump_c
9 #define LUA_CORE
10 
11 #include "lua.h"
12 
13 #include "ldebug.h"
14 #include "ldo.h"
15 #include "lfunc.h"
16 #include "lmem.h"
17 #include "lobject.h"
18 #include "lstring.h"
19 #include "lundump.h"
20 #include "lzio.h"
21 
22 typedef struct {
24  ZIO* Z;
26  const char* name;
27 } LoadState;
28 
29 static l_noret error(LoadState* S, const char* why)
30 {
31  luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
33 }
34 
35 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
36 #define LoadByte(S) (lu_byte)LoadChar(S)
37 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
38 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
39 
40 #if !defined(luai_verifycode)
41 #define luai_verifycode(L,b,f) /* empty */
42 #endif
43 
44 static void LoadBlock(LoadState* S, void* b, size_t size)
45 {
46  if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
47 }
48 
49 static int LoadChar(LoadState* S)
50 {
51  char x;
52  LoadVar(S,x);
53  return x;
54 }
55 
56 static int LoadInt(LoadState* S)
57 {
58  int x;
59  LoadVar(S,x);
60  if (x<0) error(S,"corrupted");
61  return x;
62 }
63 
65 {
66  lua_Number x;
67  LoadVar(S,x);
68  return x;
69 }
70 
72 {
73  size_t size;
74  LoadVar(S,size);
75  if (size==0)
76  return NULL;
77  else
78  {
79  char* s=luaZ_openspace(S->L,S->b,size);
80  LoadBlock(S,s,size*sizeof(char));
81  return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
82  }
83 }
84 
85 static void LoadCode(LoadState* S, Proto* f)
86 {
87  int n=LoadInt(S);
89  f->sizecode=n;
90  LoadVector(S,f->code,n,sizeof(Instruction));
91 }
92 
93 static void LoadFunction(LoadState* S, Proto* f);
94 
95 static void LoadConstants(LoadState* S, Proto* f)
96 {
97  int i,n;
98  n=LoadInt(S);
99  f->k=luaM_newvector(S->L,n,TValue);
100  f->sizek=n;
101  for (i=0; i<n; i++) setnilvalue(&f->k[i]);
102  for (i=0; i<n; i++)
103  {
104  TValue* o=&f->k[i];
105  int t=LoadChar(S);
106  switch (t)
107  {
108  case LUA_TNIL:
109  setnilvalue(o);
110  break;
111  case LUA_TBOOLEAN:
112  setbvalue(o,LoadChar(S));
113  break;
114  case LUA_TNUMBER:
115  setnvalue(o,LoadNumber(S));
116  break;
117  case LUA_TSTRING:
118  setsvalue2n(S->L,o,LoadString(S));
119  break;
120  default: lua_assert(0);
121  }
122  }
123  n=LoadInt(S);
124  f->p=luaM_newvector(S->L,n,Proto*);
125  f->sizep=n;
126  for (i=0; i<n; i++) f->p[i]=NULL;
127  for (i=0; i<n; i++)
128  {
129  f->p[i]=luaF_newproto(S->L);
130  LoadFunction(S,f->p[i]);
131  }
132 }
133 
134 static void LoadUpvalues(LoadState* S, Proto* f)
135 {
136  int i,n;
137  n=LoadInt(S);
139  f->sizeupvalues=n;
140  for (i=0; i<n; i++) f->upvalues[i].name=NULL;
141  for (i=0; i<n; i++)
142  {
143  f->upvalues[i].instack=LoadByte(S);
144  f->upvalues[i].idx=LoadByte(S);
145  }
146 }
147 
148 static void LoadDebug(LoadState* S, Proto* f)
149 {
150  int i,n;
151  f->source=LoadString(S);
152  n=LoadInt(S);
153  f->lineinfo=luaM_newvector(S->L,n,int);
154  f->sizelineinfo=n;
155  LoadVector(S,f->lineinfo,n,sizeof(int));
156  n=LoadInt(S);
157  f->locvars=luaM_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++)
161  {
162  f->locvars[i].varname=LoadString(S);
163  f->locvars[i].startpc=LoadInt(S);
164  f->locvars[i].endpc=LoadInt(S);
165  }
166  n=LoadInt(S);
167  for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
168 }
169 
170 static void LoadFunction(LoadState* S, Proto* f)
171 {
172  f->linedefined=LoadInt(S);
173  f->lastlinedefined=LoadInt(S);
174  f->numparams=LoadByte(S);
175  f->is_vararg=LoadByte(S);
176  f->maxstacksize=LoadByte(S);
177  LoadCode(S,f);
178  LoadConstants(S,f);
179  LoadUpvalues(S,f);
180  LoadDebug(S,f);
181 }
182 
183 /* the code below must be consistent with the code in luaU_header */
184 #define N0 LUAC_HEADERSIZE
185 #define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
186 #define N2 N1+2
187 #define N3 N2+6
188 
189 static void LoadHeader(LoadState* S)
190 {
193  luaU_header(h);
194  memcpy(s,h,sizeof(char)); /* first char already read */
195  LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
196  if (memcmp(h,s,N0)==0) return;
197  if (memcmp(h,s,N1)!=0) error(S,"not a");
198  if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
199  if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
200 }
201 
202 /*
203 ** load precompiled chunk
204 */
205 Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
206 {
207  LoadState S;
208  Closure* cl;
209  if (*name=='@' || *name=='=')
210  S.name=name+1;
211  else if (*name==LUA_SIGNATURE[0])
212  S.name="binary string";
213  else
214  S.name=name;
215  S.L=L;
216  S.Z=Z;
217  S.b=buff;
218  LoadHeader(&S);
219  cl=luaF_newLclosure(L,1);
220  setclLvalue(L,L->top,cl); incr_top(L);
221  cl->l.p=luaF_newproto(L);
222  LoadFunction(&S,cl->l.p);
223  if (cl->l.p->sizeupvalues != 1)
224  {
225  Proto* p=cl->l.p;
226  cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
227  cl->l.p=p;
228  setclLvalue(L,L->top-1,cl);
229  }
230  luai_verifycode(L,buff,cl->l.p);
231  return cl;
232 }
233 
234 #define MYINT(s) (s[0]-'0')
235 #define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
236 #define FORMAT 0 /* this is the official format */
237 
238 /*
239 * make header for precompiled chunks
240 * if you change the code below be sure to update LoadHeader and FORMAT above
241 * and LUAC_HEADERSIZE in lundump.h
242 */
244 {
245  int x=1;
246  memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
247  h+=sizeof(LUA_SIGNATURE)-sizeof(char);
248  *h++=cast_byte(VERSION);
249  *h++=cast_byte(FORMAT);
250  *h++=cast_byte(*(char*)&x); /* endianness */
251  *h++=cast_byte(sizeof(int));
252  *h++=cast_byte(sizeof(size_t));
253  *h++=cast_byte(sizeof(Instruction));
254  *h++=cast_byte(sizeof(lua_Number));
255  *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
256  memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
257 }
TString * source
Definition: lobject.h:475
#define luaM_newvector(L, n, t)
Definition: lmem.h:34
static void LoadBlock(LoadState *S, void *b, size_t size)
Definition: lundump.cpp:44
#define setbvalue(obj, x)
Definition: lobject.h:197
#define luai_verifycode(L, b, f)
Definition: lundump.cpp:41
Definition: lobject.h:466
Closure * luaF_newLclosure(lua_State *L, int n)
Definition: lfunc.cpp:29
Proto * luaF_newproto(lua_State *L)
Definition: lfunc.cpp:109
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:29
int sizep
Definition: lobject.h:480
#define N1
Definition: lundump.cpp:185
static int LoadInt(LoadState *S)
Definition: lundump.cpp:56
#define setnvalue(obj, x)
Definition: lobject.h:186
#define cast_byte(i)
Definition: llimits.h:94
#define l_noret
Definition: llimits.h:108
#define setnilvalue(obj)
Definition: lobject.h:189
Closure * luaU_undump(lua_State *L, ZIO *Z, Mbuffer *buff, const char *name)
Definition: lundump.cpp:205
static void LoadCode(LoadState *S, Proto *f)
Definition: lundump.cpp:85
LocVar * locvars
Definition: lobject.h:472
static void LoadDebug(LoadState *S, Proto *f)
Definition: lundump.cpp:148
#define LUA_SIGNATURE
Definition: lua.h:30
GLdouble GLdouble t
Definition: glew.h:1366
static void LoadUpvalues(LoadState *S, Proto *f)
Definition: lundump.cpp:134
static void LoadFunction(LoadState *S, Proto *f)
Definition: lundump.cpp:170
int sizelocvars
Definition: lobject.h:481
Upvaldesc * upvalues
Definition: lobject.h:473
StkId top
Definition: lstate.h:156
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
#define LUA_TSTRING
Definition: lua.h:81
unsigned char lu_byte
Definition: llimits.h:26
#define N3
Definition: lundump.cpp:187
#define LUAC_TAIL
Definition: lundump.h:22
#define LUA_TNIL
Definition: lua.h:77
static TString * LoadString(LoadState *S)
Definition: lundump.cpp:71
LClosure l
Definition: lobject.h:531
#define LUA_TNUMBER
Definition: lua.h:80
static void LoadHeader(LoadState *S)
Definition: lundump.cpp:189
int * lineinfo
Definition: lobject.h:471
int lastlinedefined
Definition: lobject.h:483
TString * varname
Definition: lobject.h:457
GLfloat GLfloat p
Definition: glew.h:12766
Instruction * code
Definition: lobject.h:469
struct Proto ** p
Definition: lobject.h:470
lu_byte maxstacksize
Definition: lobject.h:487
int sizelineinfo
Definition: lobject.h:479
l_noret luaD_throw(lua_State *L, int errcode)
Definition: ldo.cpp:125
lua_State * L
Definition: lundump.cpp:23
Definition: lzio.h:53
#define lua_assert(c)
Definition: llimits.h:65
static lua_Number LoadNumber(LoadState *S)
Definition: lundump.cpp:64
static void LoadConstants(LoadState *S, Proto *f)
Definition: lundump.cpp:95
#define LoadVector(S, b, n, size)
Definition: lundump.cpp:38
#define incr_top(L)
Definition: ldo.h:19
#define setsvalue2n
Definition: lobject.h:262
lu_byte idx
Definition: lobject.h:448
lu_byte instack
Definition: lobject.h:447
#define setclLvalue(L, obj, x)
Definition: lobject.h:220
int sizek
Definition: lobject.h:477
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
size_t i
Definition: function.cpp:1057
#define LUA_ERRSYNTAX
Definition: lua.h:47
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
const char * name
Definition: lundump.cpp:26
lu_byte is_vararg
Definition: lobject.h:486
int linedefined
Definition: lobject.h:482
const char * luaO_pushfstring(lua_State *L, const char *fmt,...)
Definition: lobject.cpp:232
int sizecode
Definition: lobject.h:478
GLuint const GLchar * name
Definition: glew.h:1782
#define LoadByte(S)
Definition: lundump.cpp:36
GLsizeiptr size
Definition: glew.h:1649
size_t luaZ_read(ZIO *z, void *b, size_t n)
Definition: lzio.cpp:45
GLclampd n
Definition: glew.h:5903
void luaU_header(lu_byte *h)
Definition: lundump.cpp:243
TString * name
Definition: lobject.h:446
struct Proto * p
Definition: lobject.h:524
char * luaZ_openspace(lua_State *L, Mbuffer *buff, size_t n)
Definition: lzio.cpp:67
ZIO * Z
Definition: lundump.cpp:24
int startpc
Definition: lobject.h:458
int endpc
Definition: lobject.h:459
Definition: lzio.h:22
#define FORMAT
Definition: lundump.cpp:236
int sizeupvalues
Definition: lobject.h:476
#define LUAC_HEADERSIZE
Definition: lundump.h:25
#define N0
Definition: lundump.cpp:184
TValue * k
Definition: lobject.h:468
TString * luaS_newlstr(lua_State *L, const char *str, size_t l)
Definition: lstring.cpp:155
GLdouble s
Definition: glew.h:1358
Mbuffer * b
Definition: lundump.cpp:25
#define N2
Definition: lundump.cpp:186
#define VERSION
Definition: lundump.cpp:235
LUA_NUMBER lua_Number
Definition: lua.h:102
lu_int32 Instruction
Definition: llimits.h:132
#define LUA_TBOOLEAN
Definition: lua.h:78
lu_byte numparams
Definition: lobject.h:485
#define S(x)
Definition: luac.cpp:374
#define LoadVar(S, x)
Definition: lundump.cpp:37
static int LoadChar(LoadState *S)
Definition: lundump.cpp:49
GLclampf f
Definition: glew.h:3024