annotate src/lua/lundump.c @ 33:44974c3e093b

found source of problem for video recording
author Robert McIntyre <rlm@mit.edu>
date Mon, 05 Mar 2012 01:25:11 -0600
parents 27763b933818
children
rev   line source
rlm@1 1 /*
rlm@1 2 ** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $
rlm@1 3 ** load precompiled Lua chunks
rlm@1 4 ** See Copyright Notice in lua.h
rlm@1 5 */
rlm@1 6
rlm@1 7 #include <string.h>
rlm@1 8
rlm@1 9 #define lundump_c
rlm@1 10 #define LUA_CORE
rlm@1 11
rlm@1 12 #include "lua.h"
rlm@1 13
rlm@1 14 #include "ldebug.h"
rlm@1 15 #include "ldo.h"
rlm@1 16 #include "lfunc.h"
rlm@1 17 #include "lmem.h"
rlm@1 18 #include "lobject.h"
rlm@1 19 #include "lstring.h"
rlm@1 20 #include "lundump.h"
rlm@1 21 #include "lzio.h"
rlm@1 22
rlm@1 23 typedef struct {
rlm@1 24 lua_State* L;
rlm@1 25 ZIO* Z;
rlm@1 26 Mbuffer* b;
rlm@1 27 const char* name;
rlm@1 28 } LoadState;
rlm@1 29
rlm@1 30 #ifdef LUAC_TRUST_BINARIES
rlm@1 31 #define IF(c,s)
rlm@1 32 #define error(S,s)
rlm@1 33 #else
rlm@1 34 #define IF(c,s) if (c) error(S,s)
rlm@1 35
rlm@1 36 static void error(LoadState* S, const char* why)
rlm@1 37 {
rlm@1 38 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why);
rlm@1 39 luaD_throw(S->L,LUA_ERRSYNTAX);
rlm@1 40 }
rlm@1 41 #endif
rlm@1 42
rlm@1 43 #define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
rlm@1 44 #define LoadByte(S) (lu_byte)LoadChar(S)
rlm@1 45 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
rlm@1 46 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
rlm@1 47
rlm@1 48 static void LoadBlock(LoadState* S, void* b, size_t size)
rlm@1 49 {
rlm@1 50 size_t r=luaZ_read(S->Z,b,size);
rlm@1 51 IF (r!=0, "unexpected end");
rlm@1 52 }
rlm@1 53
rlm@1 54 static int LoadChar(LoadState* S)
rlm@1 55 {
rlm@1 56 char x;
rlm@1 57 LoadVar(S,x);
rlm@1 58 return x;
rlm@1 59 }
rlm@1 60
rlm@1 61 static int LoadInt(LoadState* S)
rlm@1 62 {
rlm@1 63 int x;
rlm@1 64 LoadVar(S,x);
rlm@1 65 IF (x<0, "bad integer");
rlm@1 66 return x;
rlm@1 67 }
rlm@1 68
rlm@1 69 static lua_Number LoadNumber(LoadState* S)
rlm@1 70 {
rlm@1 71 lua_Number x;
rlm@1 72 LoadVar(S,x);
rlm@1 73 return x;
rlm@1 74 }
rlm@1 75
rlm@1 76 static TString* LoadString(LoadState* S)
rlm@1 77 {
rlm@1 78 size_t size;
rlm@1 79 LoadVar(S,size);
rlm@1 80 if (size==0)
rlm@1 81 return NULL;
rlm@1 82 else
rlm@1 83 {
rlm@1 84 char* s=luaZ_openspace(S->L,S->b,size);
rlm@1 85 LoadBlock(S,s,size);
rlm@1 86 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
rlm@1 87 }
rlm@1 88 }
rlm@1 89
rlm@1 90 static void LoadCode(LoadState* S, Proto* f)
rlm@1 91 {
rlm@1 92 int n=LoadInt(S);
rlm@1 93 f->code=luaM_newvector(S->L,n,Instruction);
rlm@1 94 f->sizecode=n;
rlm@1 95 LoadVector(S,f->code,n,sizeof(Instruction));
rlm@1 96 }
rlm@1 97
rlm@1 98 static Proto* LoadFunction(LoadState* S, TString* p);
rlm@1 99
rlm@1 100 static void LoadConstants(LoadState* S, Proto* f)
rlm@1 101 {
rlm@1 102 int i,n;
rlm@1 103 n=LoadInt(S);
rlm@1 104 f->k=luaM_newvector(S->L,n,TValue);
rlm@1 105 f->sizek=n;
rlm@1 106 for (i=0; i<n; i++) setnilvalue(&f->k[i]);
rlm@1 107 for (i=0; i<n; i++)
rlm@1 108 {
rlm@1 109 TValue* o=&f->k[i];
rlm@1 110 int t=LoadChar(S);
rlm@1 111 switch (t)
rlm@1 112 {
rlm@1 113 case LUA_TNIL:
rlm@1 114 setnilvalue(o);
rlm@1 115 break;
rlm@1 116 case LUA_TBOOLEAN:
rlm@1 117 setbvalue(o,LoadChar(S)!=0);
rlm@1 118 break;
rlm@1 119 case LUA_TNUMBER:
rlm@1 120 setnvalue(o,LoadNumber(S));
rlm@1 121 break;
rlm@1 122 case LUA_TSTRING:
rlm@1 123 setsvalue2n(S->L,o,LoadString(S));
rlm@1 124 break;
rlm@1 125 default:
rlm@1 126 error(S,"bad constant");
rlm@1 127 break;
rlm@1 128 }
rlm@1 129 }
rlm@1 130 n=LoadInt(S);
rlm@1 131 f->p=luaM_newvector(S->L,n,Proto*);
rlm@1 132 f->sizep=n;
rlm@1 133 for (i=0; i<n; i++) f->p[i]=NULL;
rlm@1 134 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source);
rlm@1 135 }
rlm@1 136
rlm@1 137 static void LoadDebug(LoadState* S, Proto* f)
rlm@1 138 {
rlm@1 139 int i,n;
rlm@1 140 n=LoadInt(S);
rlm@1 141 f->lineinfo=luaM_newvector(S->L,n,int);
rlm@1 142 f->sizelineinfo=n;
rlm@1 143 LoadVector(S,f->lineinfo,n,sizeof(int));
rlm@1 144 n=LoadInt(S);
rlm@1 145 f->locvars=luaM_newvector(S->L,n,LocVar);
rlm@1 146 f->sizelocvars=n;
rlm@1 147 for (i=0; i<n; i++) f->locvars[i].varname=NULL;
rlm@1 148 for (i=0; i<n; i++)
rlm@1 149 {
rlm@1 150 f->locvars[i].varname=LoadString(S);
rlm@1 151 f->locvars[i].startpc=LoadInt(S);
rlm@1 152 f->locvars[i].endpc=LoadInt(S);
rlm@1 153 }
rlm@1 154 n=LoadInt(S);
rlm@1 155 f->upvalues=luaM_newvector(S->L,n,TString*);
rlm@1 156 f->sizeupvalues=n;
rlm@1 157 for (i=0; i<n; i++) f->upvalues[i]=NULL;
rlm@1 158 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
rlm@1 159 }
rlm@1 160
rlm@1 161 static Proto* LoadFunction(LoadState* S, TString* p)
rlm@1 162 {
rlm@1 163 Proto* f;
rlm@1 164 if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
rlm@1 165 f=luaF_newproto(S->L);
rlm@1 166 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
rlm@1 167 f->source=LoadString(S); if (f->source==NULL) f->source=p;
rlm@1 168 f->linedefined=LoadInt(S);
rlm@1 169 f->lastlinedefined=LoadInt(S);
rlm@1 170 f->nups=LoadByte(S);
rlm@1 171 f->numparams=LoadByte(S);
rlm@1 172 f->is_vararg=LoadByte(S);
rlm@1 173 f->maxstacksize=LoadByte(S);
rlm@1 174 LoadCode(S,f);
rlm@1 175 LoadConstants(S,f);
rlm@1 176 LoadDebug(S,f);
rlm@1 177 IF (!luaG_checkcode(f), "bad code");
rlm@1 178 S->L->top--;
rlm@1 179 S->L->nCcalls--;
rlm@1 180 return f;
rlm@1 181 }
rlm@1 182
rlm@1 183 static void LoadHeader(LoadState* S)
rlm@1 184 {
rlm@1 185 char h[LUAC_HEADERSIZE];
rlm@1 186 char s[LUAC_HEADERSIZE];
rlm@1 187 luaU_header(h);
rlm@1 188 LoadBlock(S,s,LUAC_HEADERSIZE);
rlm@1 189 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header");
rlm@1 190 }
rlm@1 191
rlm@1 192 /*
rlm@1 193 ** load precompiled chunk
rlm@1 194 */
rlm@1 195 Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
rlm@1 196 {
rlm@1 197 LoadState S;
rlm@1 198 if (*name=='@' || *name=='=')
rlm@1 199 S.name=name+1;
rlm@1 200 else if (*name==LUA_SIGNATURE[0])
rlm@1 201 S.name="binary string";
rlm@1 202 else
rlm@1 203 S.name=name;
rlm@1 204 S.L=L;
rlm@1 205 S.Z=Z;
rlm@1 206 S.b=buff;
rlm@1 207 LoadHeader(&S);
rlm@1 208 return LoadFunction(&S,luaS_newliteral(L,"=?"));
rlm@1 209 }
rlm@1 210
rlm@1 211 /*
rlm@1 212 * make header
rlm@1 213 */
rlm@1 214 void luaU_header (char* h)
rlm@1 215 {
rlm@1 216 int x=1;
rlm@1 217 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
rlm@1 218 h+=sizeof(LUA_SIGNATURE)-1;
rlm@1 219 *h++=(char)LUAC_VERSION;
rlm@1 220 *h++=(char)LUAC_FORMAT;
rlm@1 221 *h++=(char)*(char*)&x; /* endianness */
rlm@1 222 *h++=(char)sizeof(int);
rlm@1 223 *h++=(char)sizeof(size_t);
rlm@1 224 *h++=(char)sizeof(Instruction);
rlm@1 225 *h++=(char)sizeof(lua_Number);
rlm@1 226 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
rlm@1 227 }