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