proxmark3/client/deps/liblua/ldump.c

155 lines
3.7 KiB
C
Raw Normal View History

2013-05-22 02:23:42 +08:00
/*
** $Id: ldump.c,v 2.17 2012/01/23 23:02:10 roberto Exp $
** save precompiled Lua chunks
** See Copyright Notice in lua.h
*/
#include <stddef.h>
#define ldump_c
#define LUA_CORE
#include "lua.h"
#include "lobject.h"
#include "lstate.h"
#include "lundump.h"
typedef struct {
2019-03-10 07:00:59 +08:00
lua_State *L;
lua_Writer writer;
void *data;
int strip;
int status;
2013-05-22 02:23:42 +08:00
} DumpState;
2019-03-10 01:16:50 +08:00
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
2013-05-22 02:23:42 +08:00
static void DumpBlock(const void *b, size_t size, DumpState *D) {
2019-03-10 07:00:59 +08:00
if (D->status == 0) {
lua_unlock(D->L);
D->status = (*D->writer)(D->L, b, size, D->data);
lua_lock(D->L);
}
2013-05-22 02:23:42 +08:00
}
static void DumpChar(int y, DumpState *D) {
2019-03-10 07:00:59 +08:00
char x = (char)y;
DumpVar(x, D);
2013-05-22 02:23:42 +08:00
}
static void DumpInt(int x, DumpState *D) {
2019-03-10 07:00:59 +08:00
DumpVar(x, D);
2013-05-22 02:23:42 +08:00
}
static void DumpNumber(lua_Number x, DumpState *D) {
2019-03-10 07:00:59 +08:00
DumpVar(x, D);
2013-05-22 02:23:42 +08:00
}
static void DumpVector(const void *b, int n, size_t size, DumpState *D) {
2019-03-10 07:00:59 +08:00
DumpInt(n, D);
DumpMem(b, n, size, D);
2013-05-22 02:23:42 +08:00
}
static void DumpString(const TString *s, DumpState *D) {
2019-03-10 07:00:59 +08:00
if (s == NULL) {
size_t size = 0;
DumpVar(size, D);
} else {
size_t size = s->tsv.len + 1; /* include trailing '\0' */
DumpVar(size, D);
DumpBlock(getstr(s), size * sizeof(char), D);
}
2013-05-22 02:23:42 +08:00
}
2019-03-10 01:16:50 +08:00
#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
2013-05-22 02:23:42 +08:00
2019-03-10 07:00:59 +08:00
static void DumpFunction(const Proto *f, DumpState *D);
2013-05-22 02:23:42 +08:00
static void DumpConstants(const Proto *f, DumpState *D) {
2019-03-10 07:00:59 +08:00
int i, n = f->sizek;
DumpInt(n, D);
for (i = 0; i < n; i++) {
const TValue *o = &f->k[i];
DumpChar(ttypenv(o), D);
switch (ttypenv(o)) {
case LUA_TNIL:
break;
case LUA_TBOOLEAN:
DumpChar(bvalue(o), D);
break;
case LUA_TNUMBER:
DumpNumber(nvalue(o), D);
break;
case LUA_TSTRING:
DumpString(rawtsvalue(o), D);
break;
default:
lua_assert(0);
}
}
n = f->sizep;
DumpInt(n, D);
for (i = 0; i < n; i++) DumpFunction(f->p[i], D);
2013-05-22 02:23:42 +08:00
}
static void DumpUpvalues(const Proto *f, DumpState *D) {
2019-03-10 07:00:59 +08:00
int i, n = f->sizeupvalues;
DumpInt(n, D);
for (i = 0; i < n; i++) {
DumpChar(f->upvalues[i].instack, D);
DumpChar(f->upvalues[i].idx, D);
}
2013-05-22 02:23:42 +08:00
}
static void DumpDebug(const Proto *f, DumpState *D) {
2019-03-10 07:00:59 +08:00
int i, n;
DumpString((D->strip) ? NULL : f->source, D);
n = (D->strip) ? 0 : f->sizelineinfo;
DumpVector(f->lineinfo, n, sizeof(int), D);
n = (D->strip) ? 0 : f->sizelocvars;
DumpInt(n, D);
for (i = 0; i < n; i++) {
DumpString(f->locvars[i].varname, D);
DumpInt(f->locvars[i].startpc, D);
DumpInt(f->locvars[i].endpc, D);
}
n = (D->strip) ? 0 : f->sizeupvalues;
DumpInt(n, D);
for (i = 0; i < n; i++) DumpString(f->upvalues[i].name, D);
2013-05-22 02:23:42 +08:00
}
static void DumpFunction(const Proto *f, DumpState *D) {
2019-03-10 07:00:59 +08:00
DumpInt(f->linedefined, D);
DumpInt(f->lastlinedefined, D);
DumpChar(f->numparams, D);
DumpChar(f->is_vararg, D);
DumpChar(f->maxstacksize, D);
DumpCode(f, D);
DumpConstants(f, D);
DumpUpvalues(f, D);
DumpDebug(f, D);
2013-05-22 02:23:42 +08:00
}
static void DumpHeader(DumpState *D) {
2019-03-10 07:00:59 +08:00
lu_byte h[LUAC_HEADERSIZE];
luaU_header(h);
DumpBlock(h, LUAC_HEADERSIZE, D);
2013-05-22 02:23:42 +08:00
}
/*
** dump Lua function as precompiled chunk
*/
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip) {
2019-03-10 07:00:59 +08:00
DumpState D;
D.L = L;
D.writer = w;
D.data = data;
D.strip = strip;
D.status = 0;
DumpHeader(&D);
DumpFunction(f, &D);
return D.status;
2013-05-22 02:23:42 +08:00
}