From 9c8692ab065dd6f7d3208f6126d1ba752aee32a5 Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 14 May 2013 01:53:52 +0200 Subject: [PATCH] Some udis86 anal fixes, update sdb and fix mingw32 build --- binr/r2agent/r2agent.c | 7 +++ config-user.mk.acr | 1 + libr/anal/p/anal_mips.c | 3 ++ libr/anal/p/anal_x86_udis.c | 18 +++---- libr/db/pair.c | 8 ++-- libr/fs/Makefile | 4 +- libr/fs/p/grub/Makefile | 4 +- libr/fs/p/grub/kern/misc.c | 7 --- libr/include/sdb/sdb-version.h | 2 +- libr/include/sdb/sdb.h | 14 +++--- libr/syscall/d/Makefile | 4 +- shlr/Makefile | 6 ++- shlr/sdb/Makefile | 5 +- shlr/sdb/config.mk | 10 +++- shlr/sdb/config.mkfoo | 6 ++- shlr/sdb/src/Makefile | 13 +++-- shlr/sdb/src/cdb.c | 2 + shlr/sdb/src/cdb_make.c | 8 +++- shlr/sdb/src/main.c | 71 +++++++++++++++++++++------ shlr/sdb/src/query.c | 5 +- shlr/sdb/src/sdb-version.h | 2 +- shlr/sdb/src/sdb.c | 87 ++++++++++++++++++++++++---------- shlr/sdb/src/sdb.h | 15 +++--- shlr/sdb/src/sdba.c | 8 ++-- sys/mingw32.sh | 2 +- 25 files changed, 215 insertions(+), 97 deletions(-) diff --git a/binr/r2agent/r2agent.c b/binr/r2agent/r2agent.c index 136be1ef5f..afe35c9c67 100644 --- a/binr/r2agent/r2agent.c +++ b/binr/r2agent/r2agent.c @@ -4,6 +4,12 @@ #include #include + +#if __WINDOWS__ +int main() { + eprintf ("r2agent: Not yet implemented for this platform.\n"); +} +#else #include "index.h" static int usage (int v) { @@ -90,3 +96,4 @@ int main(int argc, char **argv) { r_socket_free (s); return 0; } +#endif diff --git a/config-user.mk.acr b/config-user.mk.acr index 9c52da7b03..fe3cc002a5 100644 --- a/config-user.mk.acr +++ b/config-user.mk.acr @@ -2,6 +2,7 @@ DESTDIR= CC=@CC@ +#HOST_CC=@HOST_CC@ PREFIX=@PREFIX@ BINDIR=@BINDIR@ LIBDIR=@LIBDIR@ diff --git a/libr/anal/p/anal_mips.c b/libr/anal/p/anal_mips.c index 8ba7679c25..f1f1dc8bb4 100644 --- a/libr/anal/p/anal_mips.c +++ b/libr/anal/p/anal_mips.c @@ -18,6 +18,7 @@ static int mips_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *b, int len) { op->type = R_ANAL_OP_TYPE_UNK; op->length = oplen; op->delay = 4; + op->code[0] = 0; //r_mem_copyendian ((ut8*)&opcode, b, 4, !anal->big_endian); memcpy (&opcode, b, 4); @@ -134,11 +135,13 @@ static int mips_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *b, int len) { case 2: // j op->type = R_ANAL_OP_TYPE_JMP; op->jump = address; + sprintf (op->code, "pc=0x%08"PFMT64x, address); break; case 3: // jal op->type = R_ANAL_OP_TYPE_CALL; op->jump = address; op->fail = addr+8; + sprintf (op->code, "lr=pc+4,pc=0x%08"PFMT64x, address); break; } family = 'J'; diff --git a/libr/anal/p/anal_x86_udis.c b/libr/anal/p/anal_x86_udis.c index 8ba0cb939f..f67e6d4c3d 100644 --- a/libr/anal/p/anal_x86_udis.c +++ b/libr/anal/p/anal_x86_udis.c @@ -10,6 +10,7 @@ static st64 getval(ud_operand_t *op); // XXX Copypasta from udis +#define UD_REG_TAB_SIZE (sizeof (ud_reg_tab)/sizeof (*ud_reg_tab)) static const char* ud_reg_tab[] = { "al", "cl", "dl", "bl", @@ -77,7 +78,9 @@ static int getarg(char *src, struct ud *u, int idx) { else sprintf (src, "0x%"PFMT64x, n); break; case UD_OP_REG: - strcpy (src, ud_reg_tab[op->base - UD_R_AL]); + idx = op->base-UD_R_AL; + if (idx>=0 && idxbase - UD_R_AL]); break; case UD_OP_PTR: strcpy (src, "ptr"); @@ -86,12 +89,11 @@ static int getarg(char *src, struct ud *u, int idx) { n = getval (op); // TODO ->scale if (op->base != UD_NONE) { - if (u->mnemonic == UD_Ilea) { - sprintf (src, "%s+%d", ud_reg_tab[ - op->base-UD_R_AL], 0); // XXX - } else { - sprintf (src, "[%s+%d]", ud_reg_tab[ - op->base-UD_R_AL], n); + idx = op->base-UD_R_AL; + if (idx>=0 && idxmnemonic == UD_Ilea) + sprintf (src, "%s+%d", ud_reg_tab[idx], 0); + else sprintf (src, "[%s+%d]", ud_reg_tab[idx], n); } } break; @@ -147,7 +149,7 @@ int x86_udis86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len) break; case UD_Ijnz: // TODO: carry flag getarg (src, &u, 0); - sprintf (op->code, "?!zf,%s=%s", src); + sprintf (op->code, "?!zf,%s=%s", pc, src); break; case UD_Ijmp: // TODO: carry flag getarg (src, &u, 0); diff --git a/libr/db/pair.c b/libr/db/pair.c index 344c72cbdd..7d74feca13 100644 --- a/libr/db/pair.c +++ b/libr/db/pair.c @@ -156,12 +156,14 @@ R_API RList *r_pair_list (RPair *p, const char *domain) { else s = r_hashtable_lookup (p->ht, r_str_hash (domain)); if (s) { RList *list = r_list_new (); - char key[SDB_KSZ]; - char val[SDB_VSZ]; + char *key, *val; list->free = (RListFree)r_pair_item_free; sdb_dump_begin (s); - while (sdb_dump_next (s, key, val)) + while (sdb_dump_dupnext (s, &key, &val)) { r_list_append (list, r_pair_item_new (key, val)); + free (key); + free (val); + } return list; } return NULL; diff --git a/libr/fs/Makefile b/libr/fs/Makefile index 687da5c7fc..340e4126fc 100644 --- a/libr/fs/Makefile +++ b/libr/fs/Makefile @@ -17,7 +17,7 @@ OBJS=${STATIC_OBJS} fs.o file.o #p/grub/main.o p/grub/libgrubfs.a: - cd p/grub && ${MAKE} libgrubfs.a + cd p/grub && ${MAKE} libgrubfs.a CC=${CC} pre: cd d && ${MAKE} @@ -35,7 +35,7 @@ test: gdb --args r2 -i test.rsc /tmp/test.fs.img plugins: - cd p/grub && ${MAKE} libgrubfs.a + cd p/grub && ${MAKE} libgrubfs.a CC=${CC} cd p && ${MAKE} all include ../rules.mk diff --git a/libr/fs/p/grub/Makefile b/libr/fs/p/grub/Makefile index 351fbe85b1..13fdf5ac48 100644 --- a/libr/fs/p/grub/Makefile +++ b/libr/fs/p/grub/Makefile @@ -68,7 +68,7 @@ all: ${KERNOBJS} ${BIN}: ${KERNOBJS} main.o ${CC} -o ${BIN} main.o ${CFLAGS} ${KERNOBJS} -lib: all libgrubfs.a +lib: libgrubfs.a libgrubfs.a: ${KERNOBJS} rm -f libgrubfs.a @@ -101,4 +101,4 @@ fs: /tmp/fs sync: echo "XXX: Implement syncing with burg project" -.PHONY: fs clean all lib +.PHONY: fs clean all lib fs sync diff --git a/libr/fs/p/grub/kern/misc.c b/libr/fs/p/grub/kern/misc.c index 5e33770646..2d495f9806 100644 --- a/libr/fs/p/grub/kern/misc.c +++ b/libr/fs/p/grub/kern/misc.c @@ -181,7 +181,6 @@ grub_puts_ (const char *s) return grub_puts (_(s)); } -#if defined (APPLE_CC) && ! defined (GRUB_UTIL) int grub_err_printf (const char *fmt, ...) { @@ -194,12 +193,6 @@ grub_err_printf (const char *fmt, ...) return ret; } -#endif - -#if ! defined (APPLE_CC) && ! defined (GRUB_UTIL) -int grub_err_printf (const char *fmt, ...) -__attribute__ ((alias("grub_printf"))); -#endif void grub_real_dprintf (const char *file, const int line, const char *condition, diff --git a/libr/include/sdb/sdb-version.h b/libr/include/sdb/sdb-version.h index 8078159976..876c971528 100644 --- a/libr/include/sdb/sdb-version.h +++ b/libr/include/sdb/sdb-version.h @@ -1 +1 @@ -#define SDB_VERSION "0.6.2" +#define SDB_VERSION "0.6.4" diff --git a/libr/include/sdb/sdb.h b/libr/include/sdb/sdb.h index 5569cd8d1e..350ae9a7d4 100644 --- a/libr/include/sdb/sdb.h +++ b/libr/include/sdb/sdb.h @@ -7,6 +7,10 @@ #define SDB_VISIBLE #endif +#if __WIN32__ || __CYGWIN__ || MINGW32 +#include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -43,14 +47,11 @@ typedef struct sdb_t { SdbList *ns; } Sdb; -// XXX: use buckets here, drop these limits -#define SDB_BLOCK 4096 -#define SDB_KSZ 64 -#define SDB_VSZ (SDB_BLOCK-sizeof(ut64)-SDB_KEYSIZE) +#define SDB_KSZ 0xff typedef struct sdb_kv { - char key[SDB_KSZ]; // key = 64 - char value[SDB_VSZ]; // value = 4032 + char key[SDB_KSZ]; + char *value; ut64 expire; ut32 cas; } SdbKv; @@ -84,6 +85,7 @@ int sdb_finish (Sdb *s); /* iterate */ void sdb_dump_begin (Sdb* s); int sdb_dump_next (Sdb* s, char *key, char *value); // XXX: needs refactor? +int sdb_dump_dupnext (Sdb* s, char **key, char **value); /* numeric */ R_API char *sdb_itoa(ut64 n, char *s); diff --git a/libr/syscall/d/Makefile b/libr/syscall/d/Makefile index 3408098f92..7c1e15b36d 100644 --- a/libr/syscall/d/Makefile +++ b/libr/syscall/d/Makefile @@ -40,7 +40,9 @@ clean: ${SDB}: cp -rf $(SDBPATH)/src $(SDBPATH)/src-native #cd $(LTOP)/db/sdb/src-native && ${MAKE} -j ${MAKE_JOBS} ARCH=xxx CC=gcc clean sdb - cd $(SDBPATH)/src-native && CFLAGS='${CFLAGS}' \ + #${CFLAGS}' + # TODO use HOST_CC + cd $(SDBPATH)/src-native && CFLAGS='-w' \ ${MAKE} -j 1 ARCH=xxx CC=gcc clean sdb cp -f $(SDBPATH)/src-native/sdb $(SDBPATH)/sdb rm -rf $(SDBPATH)/src-native diff --git a/shlr/Makefile b/shlr/Makefile index d8a7111e0b..c7d358a6a2 100644 --- a/shlr/Makefile +++ b/shlr/Makefile @@ -1,8 +1,12 @@ MODS=udis86 java sdb URL=git://github.com/radare/sdb +PWD=$(shell pwd) +SDB_CONFIG=${PWD}/sdb-config.mk all: - @for a in ${MODS} ; do ${MAKE} -C $$a HAVE_VALA=; done + @for a in ${MODS} ; do \ + ${MAKE} -C $$a HAVE_VALA= SDB_CONFIG=${SDB_CONFIG} ROOT=${PWD}/../ ; \ + done clean mrproper: @for a in ${MODS} ; do ${MAKE} -C $$a clean ; done diff --git a/shlr/sdb/Makefile b/shlr/sdb/Makefile index 93679672f4..b9f3721cd9 100644 --- a/shlr/sdb/Makefile +++ b/shlr/sdb/Makefile @@ -22,7 +22,7 @@ EMCCFLAGS=-O2 -s ASM_JS=1 sdb.js: src/sdb-version.h cd src ; emcc ${EMCCFLAGS} -I. -o ../sdb.js *.c json/api.c json/js0n.c json/json.c json/rangstr.c -mrproper clean: +clean: rm -f src/sdb-version.h cd src && ${MAKE} clean cd memcache && ${MAKE} clean @@ -46,7 +46,8 @@ install-dirs: install: install-dirs cp -f src/sdb.1 ${MANDIR} - cp -f src/libsdb.* ${PFX}/lib + cp -f src/libsdb.${SOEXT} ${PFX}/lib + cp -f src/libsdb.${SOVER} ${PFX}/lib cp -f src/sdb.h ${PFX}/include/sdb cp -f src/sdb-version.h ${PFX}/include/sdb cp -f src/cdb.h ${PFX}/include/sdb diff --git a/shlr/sdb/config.mk b/shlr/sdb/config.mk index 445dea8ef3..93d978f8bd 100644 --- a/shlr/sdb/config.mk +++ b/shlr/sdb/config.mk @@ -1,7 +1,7 @@ DESTDIR?= PREFIX?=/usr -VERSION=0.6.2 +VERSION=0.6.4 CFLAGS_STD?=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 CFLAGS+=${CFLAGS_STD} @@ -34,6 +34,7 @@ CFLAGS+=-MMD ifeq (${OS},Darwin) SOEXT=dylib +SOVER=dylib LDFLAGS+=-dynamic LDFLAGS_SHARED?=-fPIC -shared ifeq (${ARCH},i386) @@ -42,7 +43,8 @@ LDFLAGS_SHARED?=-fPIC -shared endif else SOVERSION=0 -SOEXT=so.0.0.0 +SOEXT=so +SOVER=${SOEXT}.${VERSION} LDFLAGS_SHARED?=-fPIC -shared LDFLAGS_SHARED+=-Wl,-soname,libsdb.so.$(SOVERSION) endif @@ -52,3 +54,7 @@ SILENT=1 else SILENT= endif + +ifneq (${SDB_CONFIG},) +include ${SDB_CONFIG} +endif diff --git a/shlr/sdb/config.mkfoo b/shlr/sdb/config.mkfoo index 9a6deb1655..8b6dc20b4c 100644 --- a/shlr/sdb/config.mkfoo +++ b/shlr/sdb/config.mkfoo @@ -1,7 +1,7 @@ DESTDIR?= PREFIX?=/usr -VERSION=0.6.2 +VERSION=0.6.4 CFLAGS_STD?=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 CFLAGS+=${CFLAGS_STD} @@ -34,6 +34,7 @@ CFLAGS+=-MMD ifeq (${OS},Darwin) SOEXT=dylib +SOVER=dylib LDFLAGS+=-dynamic LDFLAGS_SHARED?=-fPIC -shared ifeq (${ARCH},i386) @@ -42,7 +43,8 @@ LDFLAGS_SHARED?=-fPIC -shared endif else SOVERSION=0 -SOEXT=so.0.0.0 +SOEXT=so +SOVER=${SOEXT}.${VERSION} LDFLAGS_SHARED?=-fPIC -shared LDFLAGS_SHARED+=-Wl,-soname,libsdb.so.$(SOVERSION) endif diff --git a/shlr/sdb/src/Makefile b/shlr/sdb/src/Makefile index 1a001c3928..fdf8d705d4 100644 --- a/shlr/sdb/src/Makefile +++ b/shlr/sdb/src/Makefile @@ -19,7 +19,7 @@ sdb-version.h: cd .. ; ${MAKE} src/sdb-version.h shared: sdb-version.h - ${MAKE} libsdb.${SOEXT} + ${MAKE} libsdb.${SOVER} static: sdb-version.h ${MAKE} libsdb.a @@ -32,9 +32,12 @@ endif ${AR} -r libsdb.a ${OBJ} ${RANLIB} libsdb.a -libsdb.${SOEXT}: ${SOBJ} +libsdb.${SOVER}: ${SOBJ} +ifneq ($(SOEXT),${SOVER}) + ln -fs libsdb.${SOVER} libsdb.${SOEXT} +endif ifneq ($(SILENT),) - @echo LIB libsdb.${SOEXT} + @echo LIB libsdb.${SOVER} endif ${CC} ${LDFLAGS} $(LDFLAGS_SHARED) -o $@ ${SOBJ} @@ -44,9 +47,9 @@ ifneq ($(SILENT),) endif ${CC} ${LDFLAGS} -o ${BIN} main.o ${OBJ} -clean: +mrproper clean: rm -rf ${OBJ} ${SOBJ} main.o libsdb.a a.out ${BIN} sdb.dSYM - rm -rf *.d *._d json/*.d json/*._d *.sdb *.db *.${SOEXT} + rm -rf *.d *._d json/*.d json/*._d *.sdb *.db *.${SOEXT} *.${SOEXT}.* # rules # .c: diff --git a/shlr/sdb/src/cdb.c b/shlr/sdb/src/cdb.c index d672de2760..51494f84e1 100644 --- a/shlr/sdb/src/cdb.c +++ b/shlr/sdb/src/cdb.c @@ -121,6 +121,8 @@ int cdb_findnext(struct cdb *c, ut32 u, const char *key, unsigned int len) { return -1; if (!getkvlen (c->fd, &u, &c->dlen)) return -1; + if (u == 0) + return -1; if (u == len) { if ((m = match (c, key, len, pos + KVLSZ))==-1) return 0; diff --git a/shlr/sdb/src/cdb_make.c b/shlr/sdb/src/cdb_make.c index 84faf6bb1a..7b9cefc4d4 100644 --- a/shlr/sdb/src/cdb_make.c +++ b/shlr/sdb/src/cdb_make.c @@ -65,16 +65,20 @@ int cdb_make_addend(struct cdb_make *c, ut32 keylen, ut32 datalen, ut32 h) { return posplus (c, KVLSZ+keylen+datalen); } -static void pack_kvlen(ut8 *buf, ut32 klen, ut32 vlen) { +static int pack_kvlen(ut8 *buf, ut32 klen, ut32 vlen) { + if (klen>=0xff) return 0; + if (vlen>=0xffffff) return 0; buf[0] = (ut8)klen; buf[1] = (ut8)((vlen ) & 255); buf[2] = (ut8)((vlen>>8 ) & 255); buf[3] = (ut8)((vlen>>16) & 255); + return 1; } int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen) { ut8 buf[KVLSZ]; - pack_kvlen (buf, keylen, datalen); + if (!pack_kvlen (buf, keylen, datalen)) + return 0; return buffer_putalign (&c->b, (const char *)buf, KVLSZ); } diff --git a/shlr/sdb/src/main.c b/shlr/sdb/src/main.c index 9ea4a455d0..cd3975ad2c 100644 --- a/shlr/sdb/src/main.c +++ b/shlr/sdb/src/main.c @@ -17,6 +17,52 @@ static void terminate(int sig UNUSED) { exit (0); } +static char *stdin_gets() { + static char buf[96096]; + fgets (buf, sizeof (buf)-1, stdin); + if (feof (stdin)) return NULL; + buf[strlen (buf)-1] = 0; + return strdup (buf); +#if 0 + static char *previn = NULL; + int n, l=0, size = 128; // increase for performance + char *p, *tmp, *in = malloc (128); + for (;;) { + if (previn) { + strcpy (in, previn); + n = strlen (previn); + free (previn); + previn = NULL; + } else { + n = read (0, in+l, size); + if (n <1) { + free (in); + return NULL; + } + } + p = strchr (in+l, '\n'); + if (p) { + free (previn); + previn = strdup (p+1); + n = (int)(size_t)(p-in+l); + l += n+1; + break; + } + l += n; + if (n!=size) break; + if (in[l-1]=='\n') break; + tmp = realloc (in, l+1); + if (!tmp) { + free (in); + return NULL; + } + in = tmp; + } + in[l>0?l-1:0] = 0; + return in; +#endif +} + #if USE_MMAN static void syncronize(int sig UNUSED) { // TODO: must be in sdb_sync() or wat? @@ -29,34 +75,33 @@ static void syncronize(int sig UNUSED) { #endif static int sdb_dump (const char *db) { - char k[SDB_KSZ]; - char v[SDB_VSZ]; + char *k, *v; Sdb *s = sdb_new (db, 0); if (!s) return 1; sdb_dump_begin (s); - while (sdb_dump_next (s, k, v)) + while (sdb_dump_dupnext (s, &k, &v)) { printf ("%s=%s\n", k, v); + free (k); + free (v); + } sdb_free (s); s = NULL; return 0; } static void createdb(const char *f) { - char line[SDB_VSZ]; - char *eq; + char *line, *eq; s = sdb_new (f, 0); if (!sdb_create (s)) { printf ("Cannot create database\n"); exit (1); } - for (;;) { - if (!fgets (line, sizeof line, stdin) || feof (stdin)) - break; - line[strlen (line)-1] = 0; + for (;(line = stdin_gets ());) { if ((eq = strchr (line, '='))) { *eq = 0; sdb_append (s, line, eq+1); } + free (line); } sdb_finish (s); } @@ -78,6 +123,7 @@ static void showfeatures(void) { } int main(int argc, const char **argv) { + char *line; int i; if (argc<2) showusage (1); @@ -100,13 +146,10 @@ int main(int argc, const char **argv) { if (!strcmp (argv[2], "=")) createdb (argv[1]); else if (!strcmp (argv[2], "-")) { - char line[SDB_VSZ+SDB_KSZ]; // XXX can overflow stack if ((s = sdb_new (argv[1], 0))) - for (;;) { - if (!fgets (line, sizeof line, stdin) || feof (stdin)) - break; - line[strlen (line)-1] = 0; + for (;(line = stdin_gets ());) { save = sdb_query (s, line); + free (line); } } else if ((s = sdb_new (argv[1], 0))) for (i=2; ilen) { - buf = malloc (64); - snprintf (buf, 64, "%"ULLFMT"d", n); + buf = malloc (0xff); + snprintf (buf, 0xff, "%"ULLFMT"d", n); } return buf; } else if (*cmd == '(') { @@ -66,6 +66,7 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) { *p = 0; eq = strchr (p+1, '='); if (cmd[1]=='?') { + // if (!eq) { ... alength = sdb_alength (s, p+1); w = snprintf (buf, len, "%d", alength); if (w<0 || (size_t)w>len) { diff --git a/shlr/sdb/src/sdb-version.h b/shlr/sdb/src/sdb-version.h index 8078159976..8db49888ce 100644 --- a/shlr/sdb/src/sdb-version.h +++ b/shlr/sdb/src/sdb-version.h @@ -1 +1 @@ -#define SDB_VERSION "0.6.2" +#define SDB_VERSION "0.9.5git" diff --git a/shlr/sdb/src/sdb.c b/shlr/sdb/src/sdb.c index f7fea6ec46..844f60b525 100644 --- a/shlr/sdb/src/sdb.c +++ b/shlr/sdb/src/sdb.c @@ -13,10 +13,6 @@ #define O_BINARY 0 #endif -#if __WINDOWS__ -#include -#endif - // must be deprecated static ut32 eod, pos; // what about lseek? @@ -198,15 +194,18 @@ SDB_VISIBLE void sdb_reset (Sdb *s) { // TODO: too many allocs here. use slices SdbKv* sdb_kv_new (const char *k, const char *v) { - SdbKv *kv = R_NEW (struct sdb_kv); + int vl = strlen (v)+1; + SdbKv *kv = R_NEW (SdbKv); strncpy (kv->key, k, sizeof (kv->key)-1); - strncpy (kv->value, v, sizeof (kv->value)-1); + kv->value = malloc (vl); + memcpy (kv->value, v, vl); kv->cas = nextcas (); kv->expire = 0LL; return kv; } -SDB_VISIBLE void sdb_kv_free (struct sdb_kv *kv) { +SDB_VISIBLE void sdb_kv_free (SdbKv *kv) { + free (kv->value); free (kv); } @@ -222,30 +221,32 @@ SDB_VISIBLE int sdb_set (Sdb* s, const char *key, const char *val, ut32 cas) { e = ht_search (s->ht, hash); if (e) { if (cdb_findnext (&s->db, hash, key, klen)) { + int vl = strlen (val)+1; kv = e->data; if (cas && kv->cas != cas) return 0; kv->cas = cas = nextcas (); - strcpy (kv->value, val); // XXX overflow + free (kv->value); + kv->value = malloc (vl); + memcpy (kv->value, val, vl); } else ht_remove_entry (s->ht, e); return cas; } kv = sdb_kv_new (key, val); kv->cas = nextcas (); ht_insert (s->ht, hash, kv, NULL); - return *val? kv->cas: 0; + return kv->cas; } SDB_VISIBLE int sdb_sync (Sdb* s) { SdbKv *kv; SdbListIter it, *iter; - char k[SDB_KSZ]; - char v[SDB_VSZ]; + char *k, *v; if (!sdb_create (s)) return 0; sdb_dump_begin (s); - while (sdb_dump_next (s, k, v)) { + while (sdb_dump_dupnext (s, &k, &v)) { ut32 hash = sdb_hash (k, 0); SdbHashEntry *hte = ht_search (s->ht, hash); if (hte) { @@ -259,6 +260,8 @@ SDB_VISIBLE int sdb_sync (Sdb* s) { ht_remove_entry (s->ht, hte); } else if (*v) sdb_append (s, k, v); + free (k); + free (v); } /* append new keyvalues */ ls_foreach (s->ht->list, iter, kv) { @@ -284,6 +287,15 @@ static ut32 getnum(int fd) { return ret; } +#if 0 +static int skipbytes(int fd, int len) { + int addr = lseek (fd, len, SEEK_CUR); + if (addr == -1) return -1; + pos += len; + return len; +} +#endif + static int getbytes(int fd, char *b, int len) { if (read (fd, b, len) != len) return -1; @@ -300,21 +312,43 @@ SDB_VISIBLE void sdb_dump_begin (Sdb* s) { } else eod = pos = 0; } -// XXX: overflow if caller doesnt respects sizes -// TODO: add support for readonly dump next here -SDB_VISIBLE int sdb_dump_next (Sdb* s, char *key, char *value) { - ut32 dlen, klen; - if (s->fd==-1 || !getkvlen (s->fd, &klen, &dlen)) +SDB_VISIBLE int sdb_dump_dupnext (Sdb* s, char **key, char **value) { + ut32 vlen, klen; + if (s->fd==-1 || !getkvlen (s->fd, &klen, &vlen)) return 0; - if (klen >= SDB_KSZ || dlen >= SDB_VSZ) + if (klen<1 || vlen<1) return 0; - pos += 4; - if (key && getbytes (s->fd, key, klen)>0) - if (value && getbytes (s->fd, value, dlen)>0) { - key[klen] = value[dlen] = 0; - return 1; + if (key) { + *key = 0; + if (klen>0) { + *key = malloc (klen+1); + if (getbytes (s->fd, *key, klen) == -1) { + free (*key); + *key = NULL; + return 0; + } + (*key)[klen] = 0; + } } - return 0; + + if (value) { + *value = 0; + if (vlen>0) { + *value = malloc (vlen+10); + if (getbytes (s->fd, *value, vlen)==-1) { + if (key) { + free (*key); + *key = NULL; + } + free (*value); + *value = NULL; + return 0; + } + (*value)[vlen] = 0; + } + } + pos += 4; // XXX no + return 1; } SDB_VISIBLE ut64 sdb_now () { @@ -389,7 +423,10 @@ SDB_VISIBLE void sdb_flush(Sdb* s) { } #if __WINDOWS__ #define r_sys_mkdir(x) (CreateDirectory(x,NULL)!=0) -#define r_sys_mkdir_failed() (GetLastError () != ERROR_ALREADY_EXISTS) +#ifndef ERROR_ALREADY_EXISTS +#define ERROR_ALREADY_EXISTS 183 +#endif +#define r_sys_mkdir_failed() (GetLastError () != 183) #else #define r_sys_mkdir(x) (mkdir(x,0755)!=-1) #define r_sys_mkdir_failed() (errno != EEXIST) diff --git a/shlr/sdb/src/sdb.h b/shlr/sdb/src/sdb.h index 5569cd8d1e..2013bd9c55 100644 --- a/shlr/sdb/src/sdb.h +++ b/shlr/sdb/src/sdb.h @@ -7,6 +7,11 @@ #define SDB_VISIBLE #endif +#if __WIN32__ || __CYGWIN__ || MINGW32 +#define __WINDOWS__ 1 +#include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -43,14 +48,11 @@ typedef struct sdb_t { SdbList *ns; } Sdb; -// XXX: use buckets here, drop these limits -#define SDB_BLOCK 4096 -#define SDB_KSZ 64 -#define SDB_VSZ (SDB_BLOCK-sizeof(ut64)-SDB_KEYSIZE) +#define SDB_KSZ 0xff typedef struct sdb_kv { - char key[SDB_KSZ]; // key = 64 - char value[SDB_VSZ]; // value = 4032 + char key[SDB_KSZ]; + char *value; ut64 expire; ut32 cas; } SdbKv; @@ -84,6 +86,7 @@ int sdb_finish (Sdb *s); /* iterate */ void sdb_dump_begin (Sdb* s); int sdb_dump_next (Sdb* s, char *key, char *value); // XXX: needs refactor? +int sdb_dump_dupnext (Sdb* s, char **key, char **value); /* numeric */ R_API char *sdb_itoa(ut64 n, char *s); diff --git a/shlr/sdb/src/sdba.c b/shlr/sdb/src/sdba.c index f2e7b032ef..06640df48c 100644 --- a/shlr/sdb/src/sdba.c +++ b/shlr/sdb/src/sdba.c @@ -75,14 +75,14 @@ SDB_VISIBLE char *sdb_aget(Sdb *s, const char *key, int idx, ut32 *cas) { SDB_VISIBLE int sdb_ainsn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) { char valstr[64]; - return sdb_ains(s, key, idx, sdb_itoa (val, valstr), cas); + return sdb_ains (s, key, idx, sdb_itoa (val, valstr), cas); } // TODO: done, but there's room for improvement SDB_VISIBLE int sdb_ains(Sdb *s, const char *key, int idx, const char *val, ut32 cas) { const char *str = sdb_getc (s, key, 0); int lnstr, lstr, lval, ret; - char *x, *ptr, *nstr = NULL; + char *x, *ptr; if (!str || !*str) return sdb_set (s, key, val, cas); lval = strlen (val); @@ -97,7 +97,7 @@ SDB_VISIBLE int sdb_ains(Sdb *s, const char *key, int idx, const char *val, ut32 x[lval] = SDB_RS; memcpy (x+lval+1, str, lstr+1); } else { - nstr = strdup (str); + char *nstr = strdup (str); ptr = sdb_aindex_nc (nstr, idx); if (ptr) { *(ptr-1) = 0; @@ -108,9 +108,9 @@ SDB_VISIBLE int sdb_ains(Sdb *s, const char *key, int idx, const char *val, ut32 x[lnstr+lval+1] = SDB_RS; memcpy (x+lval+2+lnstr, ptr, strlen (ptr)+1); } else ret = 0; + free (nstr); } ret = sdb_set (s, key, x, cas); - free (nstr); free (x); return ret; } diff --git a/sys/mingw32.sh b/sys/mingw32.sh index 3fd7157276..9a9cdbdd0a 100755 --- a/sys/mingw32.sh +++ b/sys/mingw32.sh @@ -9,7 +9,7 @@ MAKE_JOBS=8 OLD_LDFLAGS="${LDFLAGS}" unset LDFLAGS -CFGFLAGS="--without-gmp --without-ewf --without-ssl --with-ostype=windows" +CFGFLAGS="--without-ewf --with-ostype=windows" if [ -x /usr/bin/pacman ]; then make clean