surenyi
6 years ago
4 changed files with 290 additions and 2 deletions
@ -0,0 +1,272 @@ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include "libfdt.h" |
|||
#include "lua.h" |
|||
#include "lualib.h" |
|||
#include "lauxlib.h" |
|||
#include "auxiliar.h" |
|||
#include "script.h" |
|||
|
|||
struct fdt_priv { |
|||
void *blob; |
|||
char buf[0]; |
|||
}; |
|||
|
|||
#define FDT_CLASS_NAME "fdt{class}" |
|||
#define FDT_GROUP_NAME "fdt{any}" |
|||
|
|||
static int meth_strerror(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int err = luaL_checkinteger(L, 2); |
|||
|
|||
lua_pushstring(L, fdt_strerror(err)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_del_node(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
|
|||
lua_pushinteger(L, fdt_del_node(fdt->blob, off)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_add_subnode(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
lua_pushinteger(L, fdt_add_subnode(fdt->blob, off, s)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_del_prop(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
lua_pushinteger(L, fdt_delprop(fdt->blob, off, s)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_append_string(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
const char *v; |
|||
size_t len; |
|||
int r; |
|||
|
|||
v = luaL_checklstring(L, 4, &len); |
|||
|
|||
r = fdt_appendprop(fdt->blob, off, s, v, len + 1); |
|||
lua_pushinteger(L, r); |
|||
|
|||
return 1; |
|||
} |
|||
|
|||
static int meth_append_prop(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
const char *v; |
|||
size_t len; |
|||
int r; |
|||
|
|||
v = luaL_checklstring(L, 4, &len); |
|||
|
|||
r = fdt_appendprop(fdt->blob, off, s, v, len); |
|||
lua_pushinteger(L, r); |
|||
|
|||
return 1; |
|||
} |
|||
|
|||
static int meth_append_u32(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
uint32_t v = luaL_checkinteger(L, 4); |
|||
|
|||
lua_pushinteger(L, fdt_appendprop_u32(fdt->blob, off, s, v)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_append_u64(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
uint64_t v = luaL_checkinteger(L, 4); |
|||
|
|||
lua_pushinteger(L, fdt_appendprop_u64(fdt->blob, off, s, v)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_set_prop_empty(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
lua_pushinteger(L, fdt_setprop(fdt->blob, off, s, NULL, 0)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_set_prop_string(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
size_t len; |
|||
const char *v = luaL_checklstring(L, 4, &len); |
|||
|
|||
lua_pushinteger(L, fdt_setprop(fdt->blob, off, s, v, len + 1)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_set_prop(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
size_t len; |
|||
const char *v = luaL_checklstring(L, 4, &len); |
|||
|
|||
lua_pushinteger(L, fdt_setprop(fdt->blob, off, s, v, len)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_set_prop_cell(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
uint32_t v = luaL_checkinteger(L, 4); |
|||
lua_pushinteger(L, fdt_setprop_cell(fdt->blob, off, s, v)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_set_prop_u64(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
uint64_t v = luaL_checkinteger(L, 4); |
|||
lua_pushinteger(L, fdt_setprop_u64(fdt->blob, off, s, v)); |
|||
return 1; |
|||
} |
|||
|
|||
static int meth_set_name(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt = auxiliar_checkgroup(L, FDT_GROUP_NAME, 1); |
|||
int off = luaL_checkinteger(L, 2); |
|||
const char *s = luaL_checkstring(L, 3); |
|||
lua_pushinteger(L, fdt_set_name(L, off, s)); |
|||
return 1; |
|||
} |
|||
|
|||
static luaL_Reg meths[] = { |
|||
{"setName", meth_set_name}, |
|||
{"setProperty", meth_set_prop}, |
|||
{"setPropertyCell", meth_set_prop_cell}, |
|||
{"setPropertyU32", meth_set_prop_cell}, |
|||
{"setPropertyU64", meth_set_prop_u64}, |
|||
{"setPropertyString", meth_set_prop_string}, |
|||
{"setPropertyEmpty", meth_set_prop_empty}, |
|||
{"appendProperty", meth_append_prop}, |
|||
{"appendCell", meth_append_u32}, |
|||
{"appendU32", meth_append_u32}, |
|||
{"appendU64", meth_append_u64}, |
|||
{"appendString",meth_append_string}, |
|||
{"delProperty", meth_del_prop}, |
|||
{"addSubnode", meth_add_subnode}, |
|||
{"delNode", meth_del_node}, |
|||
{"strerror", meth_strerror}, |
|||
{"__tostring", auxiliar_tostring}, |
|||
{NULL, NULL}, |
|||
}; |
|||
|
|||
static int __fdt_open_address(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt; |
|||
uint32_t addr = luaL_checkinteger(L, 1); |
|||
int size = luaL_checkinteger(L, 2); |
|||
|
|||
fdt = lua_newuserdata(L, sizeof *fdt); |
|||
if (fdt == NULL) { |
|||
return 0; |
|||
} |
|||
auxiliar_setclass(L, FDT_CLASS_NAME, -1); |
|||
fdt->blob = (void *)(addr); |
|||
return 1; |
|||
} |
|||
|
|||
static int __fdt_open(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt; |
|||
const char *s; |
|||
size_t len; |
|||
|
|||
s = luaL_checklstring(L, 1, &len); |
|||
if (!s) { |
|||
return 0; |
|||
} |
|||
fdt = lua_newuserdata(L, sizeof *fdt + len); |
|||
if (fdt == NULL) { |
|||
return 0; |
|||
} |
|||
auxiliar_setclass(L, FDT_CLASS_NAME, -1); |
|||
memcpy(fdt->buf, s, len); |
|||
fdt->blob = fdt->buf; |
|||
return 1; |
|||
} |
|||
|
|||
static int __fdt_open_file(lua_State *L) |
|||
{ |
|||
struct fdt_priv *fdt; |
|||
FILE *fp; |
|||
size_t len; |
|||
const char *name = luaL_checkstring(L, 1); |
|||
|
|||
if (!name) { |
|||
return 0; |
|||
} |
|||
fp = fopen(name, "rb"); |
|||
if (!fp) { |
|||
return 0; |
|||
} |
|||
fseek(fp, 0L, SEEK_END); |
|||
len = ftell(fp); |
|||
fseek(fp, 0L, SEEK_SET); |
|||
fdt = lua_newuserdata(L, sizeof *fdt + len); |
|||
if (fdt == NULL) { |
|||
fclose(fp); |
|||
return 0; |
|||
} |
|||
auxiliar_setclass(L, FDT_CLASS_NAME, -1); |
|||
fread(fdt->buf, 1, len, fp); |
|||
fclose(fp); |
|||
fdt->blob = fdt->buf; |
|||
return 1; |
|||
} |
|||
|
|||
int luaopen_libfdt(lua_State *L) |
|||
{ |
|||
static luaL_Reg funcs[] = { |
|||
{"open", __fdt_open}, |
|||
{"openWithFile", __fdt_open_file}, |
|||
{"openWithAddress", __fdt_open_address}, |
|||
{NULL, NULL} |
|||
}; |
|||
|
|||
auxiliar_newclass(L, FDT_CLASS_NAME, meths); |
|||
auxiliar_add2group(L, FDT_CLASS_NAME, FDT_GROUP_NAME); |
|||
|
|||
script_add_global(L, funcs, "fdt"); |
|||
return 0; |
|||
} |
|||
|
Loading…
Reference in new issue