Update sdb and use of SdbGperf in asm.d ##asm

This commit is contained in:
pancake 2021-04-25 02:31:46 +02:00 committed by GitHub
parent 17ddbfd7d4
commit 7546f34e57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 313 additions and 99 deletions

View File

@ -49,7 +49,7 @@ jobs:
- name: Build on a directory with spaces (meson only) - name: Build on a directory with spaces (meson only)
run: | run: |
export PATH=${HOME}/.local/bin:${HOME}/Library/Python/3.9/bin:${PATH} export PATH=${HOME}/.local/bin:${HOME}/Library/Python/3.9/bin:${PATH}
sudo apt-get --assume-yes install python3-wheel python3-setuptools cabextract sudo apt-get --assume-yes install python3-wheel gperf python3-setuptools cabextract gperf
sudo pip3 install meson==0.55.3 ninja==1.10.0 sudo pip3 install meson==0.55.3 ninja==1.10.0
git config --global pull.rebase false git config --global pull.rebase false
git clone --depth 1 . "spa ces" git clone --depth 1 . "spa ces"
@ -161,7 +161,7 @@ jobs:
path: r2-static.tar.xz path: r2-static.tar.xz
- name: Static static r2 with meson - name: Static static r2 with meson
run: | run: |
sudo apt-get --assume-yes install python3-wheel python3-setuptools cabextract sudo apt-get --assume-yes install python3-wheel python3-setuptools cabextract gperf
sudo pip3 install meson==0.55.3 ninja==1.10.0 sudo pip3 install meson==0.55.3 ninja==1.10.0
CFLAGS="-static" LDFLAGS="-static" meson --prefix=${HOME}/.local --buildtype release --default-library static build CFLAGS="-static" LDFLAGS="-static" meson --prefix=${HOME}/.local --buildtype release --default-library static build
ninja -C build && ninja -C build install ninja -C build && ninja -C build install
@ -184,7 +184,7 @@ jobs:
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Packaging for 32bit Debian - name: Packaging for 32bit Debian
run: | run: |
sudo apt install gcc-multilib sudo apt install gcc-multilib gperf
export CFLAGS=-m32 export CFLAGS=-m32
export LDFLAGS=-m32 export LDFLAGS=-m32
export ARCH=i386 export ARCH=i386
@ -252,7 +252,7 @@ jobs:
pip install r2pipe pip install r2pipe
sys/sanitize.sh sys/sanitize.sh
- name: Installing tools for the testsuite - name: Installing tools for the testsuite
run: sudo apt-get --assume-yes install python3-wheel python3-setuptools cabextract run: sudo apt-get --assume-yes install python3-wheel python3-setuptools cabextract gperf
- name: Running tests - name: Running tests
run: | run: |
export ASAN_OPTIONS=detect_leaks=0 export ASAN_OPTIONS=detect_leaks=0
@ -349,7 +349,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- name: Install dependencies - name: Install dependencies
run: sudo apt-get --assume-yes install pax wget cabextract unzip python3-wheel python3-setuptools python3-pip && pip3 install --user meson ninja run: sudo apt-get --assume-yes install pax wget gperf cabextract unzip python3-wheel python3-setuptools python3-pip && pip3 install --user meson ninja
- name: Compile with acr - name: Compile with acr
run: | run: |
sys/android-ndk-install.sh sys/android-ndk-install.sh

View File

@ -33,7 +33,7 @@ jobs:
- name: Install dependencies - name: Install dependencies
run: | run: |
sudo apt-get --assume-yes install wheel setuptools || true sudo apt-get --assume-yes install gperf wheel setuptools || true
sudo python -m pip install --upgrade pip sudo python -m pip install --upgrade pip
pip install r2pipe pip install r2pipe
- name: Configure, build and install - name: Configure, build and install

View File

@ -85,6 +85,8 @@ HAVE_LIBUV=@HAVE_LIBUV@
LIBUV_CFLAGS=@LIBUV_CFLAGS@ LIBUV_CFLAGS=@LIBUV_CFLAGS@
LIBUV_LDFLAGS=@LIBUV_LDFLAGS@ LIBUV_LDFLAGS=@LIBUV_LDFLAGS@
HAVE_GPERF=@HAVE_GPERF@
HAVE_PTRACE=@HAVE_PTRACE@ HAVE_PTRACE=@HAVE_PTRACE@
USE_PTRACE_WRAP=@USE_PTRACE_WRAP@ USE_PTRACE_WRAP=@USE_PTRACE_WRAP@

28
configure vendored
View File

@ -361,7 +361,7 @@ parse_options "$1"
shift shift
done done
ENVWORDS="MANDIR INFODIR LIBDIR INCLUDEDIR LOCALSTATEDIR ETCDIR SYSCONFDIR DATADIR DOCDIR LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX TARGET HOST BUILD INSTALL INSTALL_LIB INSTALL_MAN INSTALL_PROGRAM INSTALL_PROGRAM_STRIP INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA HOST_OS HOST_CPU BUILD_OS BUILD_CPU TARGET_OS TARGET_CPU VERSION VERSION_MAJOR VERSION_MINOR VERSION_PATCH VERSION_NUMBER PKGNAME VPATH CONTACT CONTACT_NAME CONTACT_MAIL CC CFLAGS CPPFLAGS LDFLAGS HAVE_LANG_C DEBUGGER HAVE_LIB_DL DL_LIBS PKGCONFIG HAVE_PATCH PATCH HAVE_GIT GIT HAVE_LIB_MAGIC USE_MAGIC USE_LIB_MAGIC LIBMAGIC LOADLIBS WANT_DYLINK HAVE_FORK WANT_PTRACE_WRAP WITH_LIBR USE_CS5 USE_CS4 WITH_CAPSTONE CAPSTONE_CFLAGS CAPSTONE_LDFLAGS HAVE_PKGCFG_CAPSTONE USE_CAPSTONE HAVE_LIB_Z HAVE_LIB_ZIP USE_ZIP USE_LIB_ZIP LIBZIP HAVE_LIB_XXHASH USE_XXHASH USE_LIB_XXHASH LIBXXHASH WITH_GPL HAVE_DECL_ADDR_NO_RANDOMIZE HAVE_DECL___GLIBC__ HAVE_ARC4RANDOM_UNIFORM HAVE_EXPLICIT_BZERO HAVE_EXPLICIT_MEMSET HAVE_CLOCK_NANOSLEEP HAVE_SIGACTION HAVE_CLOCK_GETTIME CLOCK_LDFLAGS SUPPORT_GNU99 HAVE_LIB_GMP HAVE_LIB_SSL SSL_CFLAGS SSL_LDFLAGS HAVE_PKGCFG_OPENSSL HAVE_OPENSSL WANT_OPENSSL HAVE_LIBUV_VERSION_1_0_0 LIBUV_CFLAGS LIBUV_LDFLAGS HAVE_PKGCFG_LIBUV HAVE_LIBUV WANT_LIBUV USE_RPATH USERCC USEROSTYPE LIBVERSION HAVE_JEMALLOC HAVE_PTRACE USE_PTRACE_WRAP R_CHECKS_LEVEL" ENVWORDS="MANDIR INFODIR LIBDIR INCLUDEDIR LOCALSTATEDIR ETCDIR SYSCONFDIR DATADIR DOCDIR LIBEXECDIR SBINDIR BINDIR EPREFIX PREFIX SPREFIX TARGET HOST BUILD INSTALL INSTALL_LIB INSTALL_MAN INSTALL_PROGRAM INSTALL_PROGRAM_STRIP INSTALL_DIR INSTALL_SCRIPT INSTALL_DATA HOST_OS HOST_CPU BUILD_OS BUILD_CPU TARGET_OS TARGET_CPU VERSION VERSION_MAJOR VERSION_MINOR VERSION_PATCH VERSION_NUMBER PKGNAME VPATH CONTACT CONTACT_NAME CONTACT_MAIL CC CFLAGS CPPFLAGS LDFLAGS HAVE_LANG_C DEBUGGER HAVE_LIB_DL DL_LIBS PKGCONFIG HAVE_PATCH PATCH HAVE_GIT GIT HAVE_GPERF GPERF HAVE_LIB_MAGIC USE_MAGIC USE_LIB_MAGIC LIBMAGIC LOADLIBS WANT_DYLINK HAVE_FORK WANT_PTRACE_WRAP WITH_LIBR USE_CS5 USE_CS4 WITH_CAPSTONE CAPSTONE_CFLAGS CAPSTONE_LDFLAGS HAVE_PKGCFG_CAPSTONE USE_CAPSTONE HAVE_LIB_Z HAVE_LIB_ZIP USE_ZIP USE_LIB_ZIP LIBZIP HAVE_LIB_XXHASH USE_XXHASH USE_LIB_XXHASH LIBXXHASH WITH_GPL HAVE_DECL_ADDR_NO_RANDOMIZE HAVE_DECL___GLIBC__ HAVE_ARC4RANDOM_UNIFORM HAVE_EXPLICIT_BZERO HAVE_EXPLICIT_MEMSET HAVE_CLOCK_NANOSLEEP HAVE_SIGACTION HAVE_CLOCK_GETTIME CLOCK_LDFLAGS SUPPORT_GNU99 HAVE_LIB_GMP HAVE_LIB_SSL SSL_CFLAGS SSL_LDFLAGS HAVE_PKGCFG_OPENSSL HAVE_OPENSSL WANT_OPENSSL HAVE_LIBUV_VERSION_1_0_0 LIBUV_CFLAGS LIBUV_LDFLAGS HAVE_PKGCFG_LIBUV HAVE_LIBUV WANT_LIBUV USE_RPATH USERCC USEROSTYPE LIBVERSION HAVE_JEMALLOC HAVE_PTRACE USE_PTRACE_WRAP R_CHECKS_LEVEL"
create_environ create_environ
@ -485,6 +485,32 @@ echo "Cannot find git" >&2
else else
echo no echo no
fi ; fi fi ; fi
ochof "checking for gperf... "
if [ -x "${GPERF}" ]; then
FIND=${GPERF}
else
FIND=""
IFS=:
for A in ${PATH} ; do
if [ -x "${A}/gperf" ]; then
FIND="${A}/gperf"
break;
fi
done
unset IFS
fi
if [ -n "${FIND}" ]; then
ocho ${FIND};
HAVE_GPERF=1
GPERF=${FIND}
else
HAVE_GPERF=0
GPERF=gperf
if [ "" = 1 ]; then
echo "Cannot find gperf" >&2
else
echo no
fi ; fi
check_library() { check_library() {
VAR=$1 VAR=$1
S="$" S="$"

View File

@ -94,6 +94,16 @@ generate_configh () {
done done
} }
generate_asmdinc() {
plugins=""
oldlib=""
for a in ${STATIC} ; do
lib=$(echo $a | cut -d . -f 1) # library
plg=$(echo $a | cut -d . -f 2) # plugin name
echo "(SdbGperf*)&gperf_${plg},"
done
}
generate_configmk () { generate_configmk () {
plugins="" plugins=""
oldlib="" oldlib=""
@ -132,13 +142,17 @@ generate_configmk () {
} }
generate () { generate () {
echo "configure-plugins: Generating libr/config.h .." echo "configure-plugins: Generating libr/config.h"
cat libr/config.h.head > libr/config.h cat libr/config.h.head > libr/config.h
echo "#define R2_LOADLIBS ${LOADLIBS}" >> libr/config.h echo "#define R2_LOADLIBS ${LOADLIBS}" >> libr/config.h
generate_configh >> libr/config.h generate_configh >> libr/config.h
cat libr/config.h.tail >> libr/config.h cat libr/config.h.tail >> libr/config.h
echo "configure-plugins: Generating libr/config.mk .." echo "configure-plugins: Generating libr/asm/d/config.inc"
generate_asmdinc >> libr/asm/d/config.inc
echo "configure-plugins: Generating libr/config.mk"
cat libr/config.mk.head > libr/config.mk cat libr/config.mk.head > libr/config.mk
generate_configmk >> libr/config.mk generate_configmk >> libr/config.mk
cat libr/config.mk.tail >> libr/config.mk cat libr/config.mk.tail >> libr/config.mk

View File

@ -13,6 +13,7 @@ USE_PKGCONFIG
CHKPRG! PATCH patch CHKPRG! PATCH patch
CHKPRG GIT git CHKPRG GIT git
CHKPRG GPERF gperf
CHKLIB magic CHKLIB magic
ARG_WITH USE_MAGIC sysmagic force to use system's magic ; ARG_WITH USE_MAGIC sysmagic force to use system's magic ;
IFAND HAVE_LIB_MAGIC USE_MAGIC { IFAND HAVE_LIB_MAGIC USE_MAGIC {

View File

@ -13,24 +13,65 @@ LDFLAGS+=${CS_LDFLAGS}
include $(STOP)/java/deps.mk include $(STOP)/java/deps.mk
include $(STOP)/capstone.mk include $(STOP)/capstone.mk
.PHONY: all plugins .PHONY: alle plugins
EXTRA_CLEAN=doclean alle:
$(MAKE) gperfs
$(MAKE) plugins
all: plugins SDB_PATH=../../../shlr/sdb
SDB=$(SDB_PATH)/sdb
doclean: gperfs:
$(MAKE) -C d clean PATH=$(SDB_PATH):$$PATH $(MAKE) -C d all
$(MAKE) -C p clean PATH=$(SDB_PATH):$$PATH cd d && for a in *.sdb ; do test $$a -nt `echo $$a.c|sed -e 's,.sdb,,'`; if [ $$? = 0 ]; then $(SDB) -C $$a ; fi ; done
rm -f `find arch/| grep '\.o$$'`
plugins: ${LIBSO} ${LIBAR} plugins: ${LIBSO} ${LIBAR}
$(MAKE) -C d all $(MAKE) -C d all
$(MAKE) -C p all $(MAKE) -C p all
alle2:
${MAKE} -C d all
${MAKE} -C p all
${MAKE} $(LIBSO)
${MAKE} $(LIBAR)
EXTRA_CLEAN=doclean
$(EXTRA_CLEAN):
$(MAKE) -C d clean
$(MAKE) -C p clean
rm -f `find arch/| grep '\.o$$'`
include ${STATIC_ASM_PLUGINS} include ${STATIC_ASM_PLUGINS}
STATIC_OBJS=$(subst ..,p/..,$(subst asm_,p/asm_,$(STATIC_OBJ))) STATIC_OBJS=$(subst ..,p/..,$(subst asm_,p/asm_,$(STATIC_OBJ)))
OBJS=${STATIC_OBJS} asm.o acode.o aop.o binutils_as.o OBJS=${STATIC_OBJS} asm.o acode.o aop.o binutils_as.o aplugs.o
OBJS+=${SHARED2_OBJ} OBJS+=${SHARED2_OBJ}
OBJS+=d/z80.o
OBJS+=d/6502.o
OBJS+=d/i4004.o
OBJS+=d/avr.o
OBJS+=d/chip8.o
OBJS+=d/i8080.o
OBJS+=d/java.o
OBJS+=d/lm32.o
OBJS+=d/m68k.o
OBJS+=d/malbolge.o
OBJS+=d/mips.o
OBJS+=d/ppc.o
OBJS+=d/riscv.o
OBJS+=d/sh.o
OBJS+=d/sparc.o
OBJS+=d/x86.o
OBJS+=d/v810.o
OBJS+=d/v850.o
OBJS+=d/8051.o
OBJS+=d/LH5801.o
OBJS+=d/arc.o
OBJS+=d/arm.o
OBJS+=d/msp430.o
OBJS+=d/propeller.o
OBJS+=d/pic18c.o
include $(TOP)/libr/rules.mk include $(TOP)/libr/rules.mk

79
libr/asm/aplugs.c Normal file
View File

@ -0,0 +1,79 @@
/* radare - LGPL - Copyright 2009-2021 - pancake, nibble */
#include <stdio.h>
#include <r_types.h>
#include <r_util.h>
#include <r_asm.h>
#if HAVE_GPERF
extern SdbGperf gperf_z80;
extern SdbGperf gperf_6502;
extern SdbGperf gperf_i4004;
extern SdbGperf gperf_avr;
extern SdbGperf gperf_chip8;
extern SdbGperf gperf_i8080;
extern SdbGperf gperf_java;
extern SdbGperf gperf_lm32;
extern SdbGperf gperf_m68k;
extern SdbGperf gperf_malbolge;
extern SdbGperf gperf_mips;
extern SdbGperf gperf_ppc;
extern SdbGperf gperf_riscv;
extern SdbGperf gperf_sh;
extern SdbGperf gperf_sparc;
extern SdbGperf gperf_x86;
extern SdbGperf gperf_v810;
extern SdbGperf gperf_v850;
extern SdbGperf gperf_8051;
extern SdbGperf gperf_LH5801;
extern SdbGperf gperf_arc;
extern SdbGperf gperf_arm;
extern SdbGperf gperf_msp430;
extern SdbGperf gperf_propeller;
extern SdbGperf gperf_pic18c;
static const SdbGperf *gperfs[] = {
&gperf_z80,
&gperf_6502,
&gperf_i4004,
&gperf_avr,
&gperf_chip8,
&gperf_i8080,
&gperf_java,
&gperf_lm32,
&gperf_m68k,
&gperf_malbolge,
&gperf_mips,
&gperf_ppc,
&gperf_riscv,
&gperf_sh,
&gperf_sparc,
&gperf_x86,
&gperf_v810,
&gperf_v850,
&gperf_8051,
&gperf_LH5801,
&gperf_arc,
&gperf_arm,
&gperf_msp430,
&gperf_propeller,
&gperf_pic18c,
NULL
};
R_API SdbGperf *r_asm_get_gperf(const char *k) {
SdbGperf **gp = (SdbGperf**)gperfs;
while (*gp) {
SdbGperf *g = *gp;
if (!strcmp (k, g->name)) {
return *gp;
}
gp++;
}
return NULL;
}
#else
R_API SdbGperf *r_asm_get_gperf(const char *k) {
return NULL;
}
#endif

View File

@ -328,6 +328,24 @@ R_API bool r_asm_use_assembler(RAsm *a, const char *name) {
return false; return false;
} }
static void load_plugin(RAsm *a, RAsmPlugin *p) {
SdbGperf *gp = r_asm_get_gperf (p->name);
if (gp) {
sdb_free (a->pair);
a->pair = sdb_new0 ();
sdb_open_gperf (a->pair, gp);
return;
}
char *r2prefix = r_str_r2_prefix (R2_SDB_OPCODES);
char *file = r_str_newf ("%s/%s.sdb", r_str_getf (r2prefix), p->arch);
if (file) {
sdb_free (a->pair);
a->pair = sdb_new (NULL, file, 0);
free (file);
}
free (r2prefix);
}
// TODO: this can be optimized using r_str_hash() // TODO: this can be optimized using r_str_hash()
R_API bool r_asm_use(RAsm *a, const char *name) { R_API bool r_asm_use(RAsm *a, const char *name) {
RAsmPlugin *h; RAsmPlugin *h;
@ -338,15 +356,9 @@ R_API bool r_asm_use(RAsm *a, const char *name) {
r_list_foreach (a->plugins, iter, h) { r_list_foreach (a->plugins, iter, h) {
if (!strcmp (h->name, name) && h->arch) { if (!strcmp (h->name, name) && h->arch) {
if (!a->cur || (a->cur && strcmp (a->cur->arch, h->arch))) { if (!a->cur || (a->cur && strcmp (a->cur->arch, h->arch))) {
char *r2prefix = r_str_r2_prefix (R2_SDB_OPCODES); r_asm_set_cpu (a, NULL);
char *file = r_str_newf ("%s/%s.sdb", r_str_getf (r2prefix), h->arch); load_plugin (a, h);
if (file) {
r_asm_set_cpu (a, NULL);
sdb_free (a->pair);
a->pair = sdb_new (NULL, file, 0);
free (file);
}
free (r2prefix);
} }
a->cur = h; a->cur = h;
return true; return true;

View File

@ -13,9 +13,10 @@ all: ${F_SDB}
%.sdb:%.sdb.txt %.sdb:%.sdb.txt
${SDB} $@ = < $< ${SDB} $@ = < $<
test -f $@ test -f $@
${SDB} -C $@
clean: clean:
rm -f *.sdb rm -f *.sdb *.c *.h *.gperf a.out
.PHONY: all clean install install-symlink symstall uninstall .PHONY: all clean install install-symlink symstall uninstall

View File

@ -2,6 +2,7 @@ r_asm_sources = [
'asm.c', 'asm.c',
'aop.c', 'aop.c',
'acode.c', 'acode.c',
'aplugs.c',
'binutils_as.c', 'binutils_as.c',
'p/asm_6502.c', 'p/asm_6502.c',
'p/asm_6502_cs.c', 'p/asm_6502_cs.c',

View File

@ -175,6 +175,7 @@ R_API int r_asm_sub_names_output(RAsm *a, const char *f);
R_API char *r_asm_describe(RAsm *a, const char* str); R_API char *r_asm_describe(RAsm *a, const char* str);
R_API RList* r_asm_get_plugins(RAsm *a); R_API RList* r_asm_get_plugins(RAsm *a);
R_API void r_asm_list_directives(void); R_API void r_asm_list_directives(void);
R_API SdbGperf *r_asm_get_gperf(const char *k);
/* code.c */ /* code.c */
R_API RAsmCode *r_asm_code_new(void); R_API RAsmCode *r_asm_code_new(void);

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2015 - pancake, qnix */ /* radare - LGPL - Copyright 2015-2021 - pancake, qnix */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

View File

@ -203,8 +203,49 @@ static char* get_cname(const char*name) {
return n; return n;
} }
static int sdb_grep_dump(const char *dbname, int fmt, bool grep, static void sdb_grep_dump_cb(int fmt, const char *k, const char *v, const char *comma) {
const char *expgrep) { switch (fmt) {
case MODE_JSON:
if (!strcmp (v, "true") || !strcmp (v, "false")) {
printf ("%s\"%s\":%s", comma, k, v);
} else if (sdb_isnum (v)) {
printf ("%s\"%s\":%"ULLFMT"u", comma, k, sdb_atoi (v));
} else if (*v == '{' || *v == '[') {
printf ("%s\"%s\":%s", comma, k, v);
} else {
printf ("%s\"%s\":\"%s\"", comma, k, v);
}
break;
case MODE_CGEN:
{
char *a = strdup (k);
char *b = strdup (v);
char *p = b;
while (*p) {
*p = (*p == '"')? '\'': *p;
p++;
}
for (p = a; *p; p++) {
if (*p == ',') {
eprintf ("Warning: Keys cant contain a comma in gperf.\n");
*p = '.';
}
}
printf ("%s,\"%s\"\n", a, b);
free (a);
free (b);
}
break;
case MODE_ZERO:
printf ("%s=%s", k, v);
break;
default:
printf ("%s=%s\n", k, v);
break;
}
}
static int sdb_grep_dump(const char *dbname, int fmt, bool grep, const char *expgrep) {
char *v, k[SDB_MAX_KEY] = { 0 }; char *v, k[SDB_MAX_KEY] = { 0 };
const char *comma = ""; const char *comma = "";
Sdb *db = sdb_new (NULL, dbname, 0); Sdb *db = sdb_new (NULL, dbname, 0);
@ -216,9 +257,6 @@ static int sdb_grep_dump(const char *dbname, int fmt, bool grep,
sdb_config (db, options); sdb_config (db, options);
sdb_dump_begin (db); sdb_dump_begin (db);
switch (fmt) { switch (fmt) {
case MODE_JSON:
printf ("{");
break;
case MODE_CGEN: case MODE_CGEN:
printf ("%%{\n"); printf ("%%{\n");
printf ("// gperf -aclEDCIG --null-strings -H sdb_hash_c_%s -N sdb_get_c_%s -t %s.gperf > %s.c\n", cname, cname, cname, cname); printf ("// gperf -aclEDCIG --null-strings -H sdb_hash_c_%s -N sdb_get_c_%s -t %s.gperf > %s.c\n", cname, cname, cname, cname);
@ -230,49 +268,33 @@ static int sdb_grep_dump(const char *dbname, int fmt, bool grep,
printf ("struct kv { const char *name; const char *value; };\n"); printf ("struct kv { const char *name; const char *value; };\n");
printf ("%%%%\n"); printf ("%%%%\n");
break; break;
case MODE_JSON:
printf ("{");
break;
} }
while (sdb_dump_dupnext (db, k, &v, NULL)) {
if (grep && !strstr (k, expgrep) && !strstr (v, expgrep)) { if (db->fd == -1) {
free (v); SdbList *l = sdb_foreach_list (db, true);
continue; SdbKv *kv;
} SdbListIter *it;
switch (fmt) { ls_foreach (l, it, kv) {
case MODE_JSON: if (grep && !strstr (k, expgrep) && !strstr (v, expgrep)) {
if (!strcmp (v, "true") || !strcmp (v, "false")) { continue;
printf ("%s\"%s\":%s", comma, k, v);
} else if (sdb_isnum (v)) {
printf ("%s\"%s\":%llu", comma, k, sdb_atoi (v));
} else if (*v == '{' || *v == '[') {
printf ("%s\"%s\":%s", comma, k, v);
} else {
printf ("%s\"%s\":\"%s\"", comma, k, v);
} }
sdb_grep_dump_cb (fmt, sdbkv_key (kv), sdbkv_value (kv), comma);
comma = ","; comma = ",";
break;
case MODE_CGEN:
{
char *p = v;
while (*p) {
*p = (*p == '"')? '\'': *p;
p++;
}
for (p = k; *p; p++) {
if (*p == ',') {
eprintf ("Keys cant contain a comma in gperf.\n");
*p = '.';
}
}
printf ("%s,\"%s\"\n", k, v);
}
break;
case MODE_ZERO:
printf ("%s=%s", k, v);
break;
default:
printf ("%s=%s\n", k, v);
break;
} }
free (v); ls_free (l);
} else {
while (sdb_dump_dupnext (db, k, &v, NULL)) {
if (grep && !strstr (k, expgrep) && !strstr (v, expgrep)) {
free (v);
continue;
}
sdb_grep_dump_cb (fmt, k, v, comma);
comma = ",";
free (v);
}
} }
switch (fmt) { switch (fmt) {
case MODE_ZERO: case MODE_ZERO:
@ -414,8 +436,8 @@ static int showusage(int o) {
if (o == 2) { if (o == 2) {
printf (" -0 terminate results with \\x00\n" printf (" -0 terminate results with \\x00\n"
" -c count the number of keys database\n" " -c count the number of keys database\n"
" -C create foo.c and foo.h for sdb embedding\n" " -C create foo.{c,h} for embedding (uses gperf)\n"
" -G output database for gperf processing\n" " -G print database in gperf format\n"
" -d decode base64 from stdin\n" " -d decode base64 from stdin\n"
" -D diff two databases\n" " -D diff two databases\n"
" -e encode stdin as base64\n" " -e encode stdin as base64\n"
@ -532,12 +554,18 @@ static int showcount(const char *db) {
static int gen_gperf(const char *file, const char *name) { static int gen_gperf(const char *file, const char *name) {
int (*_system)(const char *cmd); int (*_system)(const char *cmd);
const size_t bufsz = 4096; const size_t buf_size = 4096;
char *buf = malloc (bufsz); char *buf = malloc (buf_size);
char *out = malloc (strlen (file) + 32); size_t out_size = strlen (file) + 32;
snprintf (out, strlen (file) + 32, "%s.gperf", name); char *out = malloc (out_size);
int wd = open (out, O_RDWR); snprintf (out, out_size, "%s.gperf", name);
int rc = 0; int wd = open (out, O_RDWR, 0644);
if (wd == -1) {
wd = open (out, O_RDWR | O_CREAT, 0644);
} else {
ftruncate (wd, 0);
}
int rc = -1;
#if USE_DLSYSTEM #if USE_DLSYSTEM
_system = dlsym (NULL, "system"); _system = dlsym (NULL, "system");
if (!_system) { if (!_system) {
@ -548,25 +576,28 @@ static int gen_gperf(const char *file, const char *name) {
_system = system; _system = system;
#endif #endif
if (wd != -1) { if (wd != -1) {
ftruncate (wd, 0);
dup2 (1, 999); dup2 (1, 999);
dup2 (wd, 1); dup2 (wd, 1);
rc = sdb_dump (file, MODE_CGEN); rc = sdb_dump (file, MODE_CGEN);
fflush (stdout); fflush (stdout);
close (wd); close (wd);
dup2 (999, 1); dup2 (999, 1);
#if 0
} else { } else {
snprintf (buf, bufsz, "sdb -G %s > %s.gperf\n", file, name); snprintf (buf, bufsz, "sdb -G %s > %s.gperf\n", file, name);
rc = _system (buf); rc = _system (buf);
#endif
} else {
eprintf ("Cannot create .gperf%c", 10);
} }
if (rc == 0) { if (rc == 0) {
char *cname = get_cname (name); char *cname = get_cname (name);
snprintf (buf, bufsz, "gperf -aclEDCIG --null-strings -H sdb_hash_c_%s" snprintf (buf, buf_size, "gperf -aclEDCIG --null-strings -H sdb_hash_c_%s"
" -N sdb_get_c_%s -t %s.gperf > %s.c\n", cname, cname, name, name); " -N sdb_get_c_%s -t %s.gperf > %s.c\n", cname, cname, name, name);
free (cname); free (cname);
rc = _system (buf); rc = _system (buf);
if (rc == 0) { if (rc == 0) {
snprintf (buf, bufsz, "gcc -DMAIN=1 %s.c ; ./a.out > %s.h\n", name, name); snprintf (buf, buf_size, "gcc -DMAIN=1 %s.c ; ./a.out > %s.h\n", name, name);
rc = _system (buf); rc = _system (buf);
if (rc == 0) { if (rc == 0) {
eprintf ("Generated %s.c and %s.h\n", name, name); eprintf ("Generated %s.c and %s.h\n", name, name);
@ -578,7 +609,6 @@ static int gen_gperf(const char *file, const char *name) {
} else { } else {
eprintf ("Outdated sdb binary in PATH?\n"); eprintf ("Outdated sdb binary in PATH?\n");
} }
free (out);
free (buf); free (buf);
return rc; return rc;
} }

View File

@ -278,7 +278,7 @@ SDB_API const char *sdb_const_get_len(Sdb* s, const char *key, int *vlen, ut32 *
return NULL; return NULL;
} }
(void) cdb_findstart (&s->db); (void) cdb_findstart (&s->db);
if (cdb_findnext (&s->db, s->ht->opt.hashfn (key), key, keylen) < 1) { if (!s->ht || cdb_findnext (&s->db, s->ht->opt.hashfn (key), key, keylen) < 1) {
return NULL; return NULL;
} }
len = cdb_datalen (&s->db); len = cdb_datalen (&s->db);
@ -404,6 +404,13 @@ SDB_API int sdb_open_gperf(Sdb *s, SdbGperf *gp) {
return 0; return 0;
} }
static int sdb_open_text(Sdb *s, const char *file) {
if (!sdb_text_load (s, file)) {
return -1;
}
return s->fd;
}
SDB_API int sdb_open(Sdb *s, const char *file) { SDB_API int sdb_open(Sdb *s, const char *file) {
struct stat st; struct stat st;
if (!s) { if (!s) {
@ -411,7 +418,7 @@ SDB_API int sdb_open(Sdb *s, const char *file) {
} }
if (file) { if (file) {
if (sdb_text_check (s, file)) { if (sdb_text_check (s, file)) {
return sdb_text_load (s, file); return sdb_open_text (s, file);
} }
if (s->fd != -1) { if (s->fd != -1) {
close (s->fd); close (s->fd);
@ -970,7 +977,7 @@ SDB_API bool sdb_dump_dupnext(Sdb* s, char *key, char **value, int *_vlen) {
return true; return true;
} }
static inline ut64 parse_expire (ut64 e) { static inline ut64 parse_expire(ut64 e) {
const ut64 month = 30 * 24 * 60 * 60; const ut64 month = 30 * 24 * 60 * 60;
if (e > 0 && e < month) { if (e > 0 && e < month) {
e += sdb_now (); e += sdb_now ();

View File

@ -389,7 +389,7 @@ SDB_API bool sdb_text_load_buf(Sdb *s, char *buf, size_t sz) {
load_process_single_char (&ctx); load_process_single_char (&ctx);
} }
if (ctx.line_begin < ctx.bufsz && ctx.state != STATE_NEWLINE) { if (ctx.line_begin < ctx.bufsz && ctx.state != STATE_NEWLINE) {
load_process_final_line(&ctx); load_process_final_line (&ctx);
} }
load_ctx_fini (&ctx); load_ctx_fini (&ctx);
return ret; return ret;

View File

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <stdbool.h> #include <stdbool.h>
#include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#undef eprintf #undef eprintf
@ -38,22 +39,20 @@
#if __WIN32__ || __MINGW__ || __WINDOWS__ || _MSC_VER #if __WIN32__ || __MINGW__ || __WINDOWS__ || _MSC_VER
#define __SDB_WINDOWS__ 1 #define __SDB_WINDOWS__ 1
#undef DIRSEP
#define DIRSEP '\\' #define DIRSEP '\\'
#undef lseek
#define lseek _lseek #define lseek _lseek
#include <windows.h> #include <windows.h>
#include <io.h> #include <io.h>
#define ULLFMT "I64"
#define HAVE_MMAN 0
#else #else
// CYGWIN AND UNIX // CYGWIN AND UNIX
#define __SDB_WINDOWS__ 0 #define __SDB_WINDOWS__ 0
#undef DIRSEP
#define DIRSEP '/' #define DIRSEP '/'
#include <unistd.h> #include <unistd.h>
#endif
#include <inttypes.h>
#if __SDB_WINDOWS__ && !__CYGWIN__
#define HAVE_MMAN 0
#define ULLFMT "I64"
#else
#define HAVE_MMAN 1 #define HAVE_MMAN 1
#define ULLFMT "ll" #define ULLFMT "ll"
#endif #endif

View File

@ -1,4 +1,4 @@
/* sdb - MIT - Copyright 2011-2018 - pancake */ /* sdb - MIT - Copyright 2011-2021 - pancake */
#include "sdb.h" #include "sdb.h"
@ -14,11 +14,11 @@
#include <time.h> #include <time.h>
struct timezone { struct timezone {
int tz_minuteswest; /* minutes W of Greenwich */ int tz_minuteswest; /* minutes W of Greenwich */
int tz_dsttime; /* type of dst correction */ int tz_dsttime; /* type of dst correction */
}; };
SDB_API int gettimeofday (struct timeval* p, struct timezone * tz) { SDB_API int gettimeofday(struct timeval* p, struct timezone * tz) {
//ULARGE_INTEGER ul; // As specified on MSDN. //ULARGE_INTEGER ul; // As specified on MSDN.
ut64 ul = 0; ut64 ul = 0;
static int tzflag = 0; static int tzflag = 0;