Some udis86 anal fixes, update sdb and fix mingw32 build

This commit is contained in:
pancake 2013-05-14 01:53:52 +02:00
parent 5ba586da9d
commit 9c8692ab06
25 changed files with 215 additions and 97 deletions

View File

@ -4,6 +4,12 @@
#include <r_core.h> #include <r_core.h>
#include <signal.h> #include <signal.h>
#if __WINDOWS__
int main() {
eprintf ("r2agent: Not yet implemented for this platform.\n");
}
#else
#include "index.h" #include "index.h"
static int usage (int v) { static int usage (int v) {
@ -90,3 +96,4 @@ int main(int argc, char **argv) {
r_socket_free (s); r_socket_free (s);
return 0; return 0;
} }
#endif

View File

@ -2,6 +2,7 @@
DESTDIR= DESTDIR=
CC=@CC@ CC=@CC@
#HOST_CC=@HOST_CC@
PREFIX=@PREFIX@ PREFIX=@PREFIX@
BINDIR=@BINDIR@ BINDIR=@BINDIR@
LIBDIR=@LIBDIR@ LIBDIR=@LIBDIR@

View File

@ -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->type = R_ANAL_OP_TYPE_UNK;
op->length = oplen; op->length = oplen;
op->delay = 4; op->delay = 4;
op->code[0] = 0;
//r_mem_copyendian ((ut8*)&opcode, b, 4, !anal->big_endian); //r_mem_copyendian ((ut8*)&opcode, b, 4, !anal->big_endian);
memcpy (&opcode, b, 4); 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 case 2: // j
op->type = R_ANAL_OP_TYPE_JMP; op->type = R_ANAL_OP_TYPE_JMP;
op->jump = address; op->jump = address;
sprintf (op->code, "pc=0x%08"PFMT64x, address);
break; break;
case 3: // jal case 3: // jal
op->type = R_ANAL_OP_TYPE_CALL; op->type = R_ANAL_OP_TYPE_CALL;
op->jump = address; op->jump = address;
op->fail = addr+8; op->fail = addr+8;
sprintf (op->code, "lr=pc+4,pc=0x%08"PFMT64x, address);
break; break;
} }
family = 'J'; family = 'J';

View File

@ -10,6 +10,7 @@
static st64 getval(ud_operand_t *op); static st64 getval(ud_operand_t *op);
// XXX Copypasta from udis // XXX Copypasta from udis
#define UD_REG_TAB_SIZE (sizeof (ud_reg_tab)/sizeof (*ud_reg_tab))
static const char* ud_reg_tab[] = static const char* ud_reg_tab[] =
{ {
"al", "cl", "dl", "bl", "al", "cl", "dl", "bl",
@ -77,7 +78,9 @@ static int getarg(char *src, struct ud *u, int idx) {
else sprintf (src, "0x%"PFMT64x, n); else sprintf (src, "0x%"PFMT64x, n);
break; break;
case UD_OP_REG: case UD_OP_REG:
strcpy (src, ud_reg_tab[op->base - UD_R_AL]); idx = op->base-UD_R_AL;
if (idx>=0 && idx<UD_REG_TAB_SIZE)
strcpy (src, ud_reg_tab[op->base - UD_R_AL]);
break; break;
case UD_OP_PTR: case UD_OP_PTR:
strcpy (src, "ptr"); strcpy (src, "ptr");
@ -86,12 +89,11 @@ static int getarg(char *src, struct ud *u, int idx) {
n = getval (op); n = getval (op);
// TODO ->scale // TODO ->scale
if (op->base != UD_NONE) { if (op->base != UD_NONE) {
if (u->mnemonic == UD_Ilea) { idx = op->base-UD_R_AL;
sprintf (src, "%s+%d", ud_reg_tab[ if (idx>=0 && idx<UD_REG_TAB_SIZE) {
op->base-UD_R_AL], 0); // XXX if (u->mnemonic == UD_Ilea)
} else { sprintf (src, "%s+%d", ud_reg_tab[idx], 0);
sprintf (src, "[%s+%d]", ud_reg_tab[ else sprintf (src, "[%s+%d]", ud_reg_tab[idx], n);
op->base-UD_R_AL], n);
} }
} }
break; break;
@ -147,7 +149,7 @@ int x86_udis86_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len)
break; break;
case UD_Ijnz: // TODO: carry flag case UD_Ijnz: // TODO: carry flag
getarg (src, &u, 0); getarg (src, &u, 0);
sprintf (op->code, "?!zf,%s=%s", src); sprintf (op->code, "?!zf,%s=%s", pc, src);
break; break;
case UD_Ijmp: // TODO: carry flag case UD_Ijmp: // TODO: carry flag
getarg (src, &u, 0); getarg (src, &u, 0);

View File

@ -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)); else s = r_hashtable_lookup (p->ht, r_str_hash (domain));
if (s) { if (s) {
RList *list = r_list_new (); RList *list = r_list_new ();
char key[SDB_KSZ]; char *key, *val;
char val[SDB_VSZ];
list->free = (RListFree)r_pair_item_free; list->free = (RListFree)r_pair_item_free;
sdb_dump_begin (s); 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)); r_list_append (list, r_pair_item_new (key, val));
free (key);
free (val);
}
return list; return list;
} }
return NULL; return NULL;

View File

@ -17,7 +17,7 @@ OBJS=${STATIC_OBJS} fs.o file.o
#p/grub/main.o #p/grub/main.o
p/grub/libgrubfs.a: p/grub/libgrubfs.a:
cd p/grub && ${MAKE} libgrubfs.a cd p/grub && ${MAKE} libgrubfs.a CC=${CC}
pre: pre:
cd d && ${MAKE} cd d && ${MAKE}
@ -35,7 +35,7 @@ test:
gdb --args r2 -i test.rsc /tmp/test.fs.img gdb --args r2 -i test.rsc /tmp/test.fs.img
plugins: plugins:
cd p/grub && ${MAKE} libgrubfs.a cd p/grub && ${MAKE} libgrubfs.a CC=${CC}
cd p && ${MAKE} all cd p && ${MAKE} all
include ../rules.mk include ../rules.mk

View File

@ -68,7 +68,7 @@ all: ${KERNOBJS}
${BIN}: ${KERNOBJS} main.o ${BIN}: ${KERNOBJS} main.o
${CC} -o ${BIN} main.o ${CFLAGS} ${KERNOBJS} ${CC} -o ${BIN} main.o ${CFLAGS} ${KERNOBJS}
lib: all libgrubfs.a lib: libgrubfs.a
libgrubfs.a: ${KERNOBJS} libgrubfs.a: ${KERNOBJS}
rm -f libgrubfs.a rm -f libgrubfs.a
@ -101,4 +101,4 @@ fs: /tmp/fs
sync: sync:
echo "XXX: Implement syncing with burg project" echo "XXX: Implement syncing with burg project"
.PHONY: fs clean all lib .PHONY: fs clean all lib fs sync

View File

@ -181,7 +181,6 @@ grub_puts_ (const char *s)
return grub_puts (_(s)); return grub_puts (_(s));
} }
#if defined (APPLE_CC) && ! defined (GRUB_UTIL)
int int
grub_err_printf (const char *fmt, ...) grub_err_printf (const char *fmt, ...)
{ {
@ -194,12 +193,6 @@ grub_err_printf (const char *fmt, ...)
return ret; return ret;
} }
#endif
#if ! defined (APPLE_CC) && ! defined (GRUB_UTIL)
int grub_err_printf (const char *fmt, ...)
__attribute__ ((alias("grub_printf")));
#endif
void void
grub_real_dprintf (const char *file, const int line, const char *condition, grub_real_dprintf (const char *file, const int line, const char *condition,

View File

@ -1 +1 @@
#define SDB_VERSION "0.6.2" #define SDB_VERSION "0.6.4"

View File

@ -7,6 +7,10 @@
#define SDB_VISIBLE #define SDB_VISIBLE
#endif #endif
#if __WIN32__ || __CYGWIN__ || MINGW32
#include <windows.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -43,14 +47,11 @@ typedef struct sdb_t {
SdbList *ns; SdbList *ns;
} Sdb; } Sdb;
// XXX: use buckets here, drop these limits #define SDB_KSZ 0xff
#define SDB_BLOCK 4096
#define SDB_KSZ 64
#define SDB_VSZ (SDB_BLOCK-sizeof(ut64)-SDB_KEYSIZE)
typedef struct sdb_kv { typedef struct sdb_kv {
char key[SDB_KSZ]; // key = 64 char key[SDB_KSZ];
char value[SDB_VSZ]; // value = 4032 char *value;
ut64 expire; ut64 expire;
ut32 cas; ut32 cas;
} SdbKv; } SdbKv;
@ -84,6 +85,7 @@ int sdb_finish (Sdb *s);
/* iterate */ /* iterate */
void sdb_dump_begin (Sdb* s); void sdb_dump_begin (Sdb* s);
int sdb_dump_next (Sdb* s, char *key, char *value); // XXX: needs refactor? int sdb_dump_next (Sdb* s, char *key, char *value); // XXX: needs refactor?
int sdb_dump_dupnext (Sdb* s, char **key, char **value);
/* numeric */ /* numeric */
R_API char *sdb_itoa(ut64 n, char *s); R_API char *sdb_itoa(ut64 n, char *s);

View File

@ -40,7 +40,9 @@ clean:
${SDB}: ${SDB}:
cp -rf $(SDBPATH)/src $(SDBPATH)/src-native cp -rf $(SDBPATH)/src $(SDBPATH)/src-native
#cd $(LTOP)/db/sdb/src-native && ${MAKE} -j ${MAKE_JOBS} ARCH=xxx CC=gcc clean sdb #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 ${MAKE} -j 1 ARCH=xxx CC=gcc clean sdb
cp -f $(SDBPATH)/src-native/sdb $(SDBPATH)/sdb cp -f $(SDBPATH)/src-native/sdb $(SDBPATH)/sdb
rm -rf $(SDBPATH)/src-native rm -rf $(SDBPATH)/src-native

View File

@ -1,8 +1,12 @@
MODS=udis86 java sdb MODS=udis86 java sdb
URL=git://github.com/radare/sdb URL=git://github.com/radare/sdb
PWD=$(shell pwd)
SDB_CONFIG=${PWD}/sdb-config.mk
all: 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: clean mrproper:
@for a in ${MODS} ; do ${MAKE} -C $$a clean ; done @for a in ${MODS} ; do ${MAKE} -C $$a clean ; done

View File

@ -22,7 +22,7 @@ EMCCFLAGS=-O2 -s ASM_JS=1
sdb.js: src/sdb-version.h 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 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 rm -f src/sdb-version.h
cd src && ${MAKE} clean cd src && ${MAKE} clean
cd memcache && ${MAKE} clean cd memcache && ${MAKE} clean
@ -46,7 +46,8 @@ install-dirs:
install: install-dirs install: install-dirs
cp -f src/sdb.1 ${MANDIR} 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.h ${PFX}/include/sdb
cp -f src/sdb-version.h ${PFX}/include/sdb cp -f src/sdb-version.h ${PFX}/include/sdb
cp -f src/cdb.h ${PFX}/include/sdb cp -f src/cdb.h ${PFX}/include/sdb

View File

@ -1,7 +1,7 @@
DESTDIR?= DESTDIR?=
PREFIX?=/usr PREFIX?=/usr
VERSION=0.6.2 VERSION=0.6.4
CFLAGS_STD?=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 CFLAGS_STD?=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700
CFLAGS+=${CFLAGS_STD} CFLAGS+=${CFLAGS_STD}
@ -34,6 +34,7 @@ CFLAGS+=-MMD
ifeq (${OS},Darwin) ifeq (${OS},Darwin)
SOEXT=dylib SOEXT=dylib
SOVER=dylib
LDFLAGS+=-dynamic LDFLAGS+=-dynamic
LDFLAGS_SHARED?=-fPIC -shared LDFLAGS_SHARED?=-fPIC -shared
ifeq (${ARCH},i386) ifeq (${ARCH},i386)
@ -42,7 +43,8 @@ LDFLAGS_SHARED?=-fPIC -shared
endif endif
else else
SOVERSION=0 SOVERSION=0
SOEXT=so.0.0.0 SOEXT=so
SOVER=${SOEXT}.${VERSION}
LDFLAGS_SHARED?=-fPIC -shared LDFLAGS_SHARED?=-fPIC -shared
LDFLAGS_SHARED+=-Wl,-soname,libsdb.so.$(SOVERSION) LDFLAGS_SHARED+=-Wl,-soname,libsdb.so.$(SOVERSION)
endif endif
@ -52,3 +54,7 @@ SILENT=1
else else
SILENT= SILENT=
endif endif
ifneq (${SDB_CONFIG},)
include ${SDB_CONFIG}
endif

View File

@ -1,7 +1,7 @@
DESTDIR?= DESTDIR?=
PREFIX?=/usr PREFIX?=/usr
VERSION=0.6.2 VERSION=0.6.4
CFLAGS_STD?=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700 CFLAGS_STD?=-D_POSIX_C_SOURCE=200809L -D_XOPEN_SOURCE=700
CFLAGS+=${CFLAGS_STD} CFLAGS+=${CFLAGS_STD}
@ -34,6 +34,7 @@ CFLAGS+=-MMD
ifeq (${OS},Darwin) ifeq (${OS},Darwin)
SOEXT=dylib SOEXT=dylib
SOVER=dylib
LDFLAGS+=-dynamic LDFLAGS+=-dynamic
LDFLAGS_SHARED?=-fPIC -shared LDFLAGS_SHARED?=-fPIC -shared
ifeq (${ARCH},i386) ifeq (${ARCH},i386)
@ -42,7 +43,8 @@ LDFLAGS_SHARED?=-fPIC -shared
endif endif
else else
SOVERSION=0 SOVERSION=0
SOEXT=so.0.0.0 SOEXT=so
SOVER=${SOEXT}.${VERSION}
LDFLAGS_SHARED?=-fPIC -shared LDFLAGS_SHARED?=-fPIC -shared
LDFLAGS_SHARED+=-Wl,-soname,libsdb.so.$(SOVERSION) LDFLAGS_SHARED+=-Wl,-soname,libsdb.so.$(SOVERSION)
endif endif

View File

@ -19,7 +19,7 @@ sdb-version.h:
cd .. ; ${MAKE} src/sdb-version.h cd .. ; ${MAKE} src/sdb-version.h
shared: sdb-version.h shared: sdb-version.h
${MAKE} libsdb.${SOEXT} ${MAKE} libsdb.${SOVER}
static: sdb-version.h static: sdb-version.h
${MAKE} libsdb.a ${MAKE} libsdb.a
@ -32,9 +32,12 @@ endif
${AR} -r libsdb.a ${OBJ} ${AR} -r libsdb.a ${OBJ}
${RANLIB} libsdb.a ${RANLIB} libsdb.a
libsdb.${SOEXT}: ${SOBJ} libsdb.${SOVER}: ${SOBJ}
ifneq ($(SOEXT),${SOVER})
ln -fs libsdb.${SOVER} libsdb.${SOEXT}
endif
ifneq ($(SILENT),) ifneq ($(SILENT),)
@echo LIB libsdb.${SOEXT} @echo LIB libsdb.${SOVER}
endif endif
${CC} ${LDFLAGS} $(LDFLAGS_SHARED) -o $@ ${SOBJ} ${CC} ${LDFLAGS} $(LDFLAGS_SHARED) -o $@ ${SOBJ}
@ -44,9 +47,9 @@ ifneq ($(SILENT),)
endif endif
${CC} ${LDFLAGS} -o ${BIN} main.o ${OBJ} ${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 ${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 # # rules #
.c: .c:

View File

@ -121,6 +121,8 @@ int cdb_findnext(struct cdb *c, ut32 u, const char *key, unsigned int len) {
return -1; return -1;
if (!getkvlen (c->fd, &u, &c->dlen)) if (!getkvlen (c->fd, &u, &c->dlen))
return -1; return -1;
if (u == 0)
return -1;
if (u == len) { if (u == len) {
if ((m = match (c, key, len, pos + KVLSZ))==-1) if ((m = match (c, key, len, pos + KVLSZ))==-1)
return 0; return 0;

View File

@ -65,16 +65,20 @@ int cdb_make_addend(struct cdb_make *c, ut32 keylen, ut32 datalen, ut32 h) {
return posplus (c, KVLSZ+keylen+datalen); 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[0] = (ut8)klen;
buf[1] = (ut8)((vlen ) & 255); buf[1] = (ut8)((vlen ) & 255);
buf[2] = (ut8)((vlen>>8 ) & 255); buf[2] = (ut8)((vlen>>8 ) & 255);
buf[3] = (ut8)((vlen>>16) & 255); buf[3] = (ut8)((vlen>>16) & 255);
return 1;
} }
int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen) { int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen) {
ut8 buf[KVLSZ]; 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); return buffer_putalign (&c->b, (const char *)buf, KVLSZ);
} }

View File

@ -17,6 +17,52 @@ static void terminate(int sig UNUSED) {
exit (0); 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 #if USE_MMAN
static void syncronize(int sig UNUSED) { static void syncronize(int sig UNUSED) {
// TODO: must be in sdb_sync() or wat? // TODO: must be in sdb_sync() or wat?
@ -29,34 +75,33 @@ static void syncronize(int sig UNUSED) {
#endif #endif
static int sdb_dump (const char *db) { static int sdb_dump (const char *db) {
char k[SDB_KSZ]; char *k, *v;
char v[SDB_VSZ];
Sdb *s = sdb_new (db, 0); Sdb *s = sdb_new (db, 0);
if (!s) return 1; if (!s) return 1;
sdb_dump_begin (s); sdb_dump_begin (s);
while (sdb_dump_next (s, k, v)) while (sdb_dump_dupnext (s, &k, &v)) {
printf ("%s=%s\n", k, v); printf ("%s=%s\n", k, v);
free (k);
free (v);
}
sdb_free (s); sdb_free (s);
s = NULL; s = NULL;
return 0; return 0;
} }
static void createdb(const char *f) { static void createdb(const char *f) {
char line[SDB_VSZ]; char *line, *eq;
char *eq;
s = sdb_new (f, 0); s = sdb_new (f, 0);
if (!sdb_create (s)) { if (!sdb_create (s)) {
printf ("Cannot create database\n"); printf ("Cannot create database\n");
exit (1); exit (1);
} }
for (;;) { for (;(line = stdin_gets ());) {
if (!fgets (line, sizeof line, stdin) || feof (stdin))
break;
line[strlen (line)-1] = 0;
if ((eq = strchr (line, '='))) { if ((eq = strchr (line, '='))) {
*eq = 0; *eq = 0;
sdb_append (s, line, eq+1); sdb_append (s, line, eq+1);
} }
free (line);
} }
sdb_finish (s); sdb_finish (s);
} }
@ -78,6 +123,7 @@ static void showfeatures(void) {
} }
int main(int argc, const char **argv) { int main(int argc, const char **argv) {
char *line;
int i; int i;
if (argc<2) showusage (1); if (argc<2) showusage (1);
@ -100,13 +146,10 @@ int main(int argc, const char **argv) {
if (!strcmp (argv[2], "=")) if (!strcmp (argv[2], "="))
createdb (argv[1]); createdb (argv[1]);
else if (!strcmp (argv[2], "-")) { else if (!strcmp (argv[2], "-")) {
char line[SDB_VSZ+SDB_KSZ]; // XXX can overflow stack
if ((s = sdb_new (argv[1], 0))) if ((s = sdb_new (argv[1], 0)))
for (;;) { for (;(line = stdin_gets ());) {
if (!fgets (line, sizeof line, stdin) || feof (stdin))
break;
line[strlen (line)-1] = 0;
save = sdb_query (s, line); save = sdb_query (s, line);
free (line);
} }
} else if ((s = sdb_new (argv[1], 0))) } else if ((s = sdb_new (argv[1], 0)))
for (i=2; i<argc; i++) for (i=2; i<argc; i++)

View File

@ -53,8 +53,8 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) {
} }
w = snprintf (buf, sizeof (buf), "%"ULLFMT"d", n); w = snprintf (buf, sizeof (buf), "%"ULLFMT"d", n);
if (w<0 || (size_t)w>len) { if (w<0 || (size_t)w>len) {
buf = malloc (64); buf = malloc (0xff);
snprintf (buf, 64, "%"ULLFMT"d", n); snprintf (buf, 0xff, "%"ULLFMT"d", n);
} }
return buf; return buf;
} else if (*cmd == '(') { } else if (*cmd == '(') {
@ -66,6 +66,7 @@ SDB_VISIBLE char *sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd) {
*p = 0; *p = 0;
eq = strchr (p+1, '='); eq = strchr (p+1, '=');
if (cmd[1]=='?') { if (cmd[1]=='?') {
// if (!eq) { ...
alength = sdb_alength (s, p+1); alength = sdb_alength (s, p+1);
w = snprintf (buf, len, "%d", alength); w = snprintf (buf, len, "%d", alength);
if (w<0 || (size_t)w>len) { if (w<0 || (size_t)w>len) {

View File

@ -1 +1 @@
#define SDB_VERSION "0.6.2" #define SDB_VERSION "0.9.5git"

View File

@ -13,10 +13,6 @@
#define O_BINARY 0 #define O_BINARY 0
#endif #endif
#if __WINDOWS__
#include <windows.h>
#endif
// must be deprecated // must be deprecated
static ut32 eod, pos; // what about lseek? 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 // TODO: too many allocs here. use slices
SdbKv* sdb_kv_new (const char *k, const char *v) { 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->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->cas = nextcas ();
kv->expire = 0LL; kv->expire = 0LL;
return kv; 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); 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); e = ht_search (s->ht, hash);
if (e) { if (e) {
if (cdb_findnext (&s->db, hash, key, klen)) { if (cdb_findnext (&s->db, hash, key, klen)) {
int vl = strlen (val)+1;
kv = e->data; kv = e->data;
if (cas && kv->cas != cas) if (cas && kv->cas != cas)
return 0; return 0;
kv->cas = cas = nextcas (); 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); } else ht_remove_entry (s->ht, e);
return cas; return cas;
} }
kv = sdb_kv_new (key, val); kv = sdb_kv_new (key, val);
kv->cas = nextcas (); kv->cas = nextcas ();
ht_insert (s->ht, hash, kv, NULL); ht_insert (s->ht, hash, kv, NULL);
return *val? kv->cas: 0; return kv->cas;
} }
SDB_VISIBLE int sdb_sync (Sdb* s) { SDB_VISIBLE int sdb_sync (Sdb* s) {
SdbKv *kv; SdbKv *kv;
SdbListIter it, *iter; SdbListIter it, *iter;
char k[SDB_KSZ]; char *k, *v;
char v[SDB_VSZ];
if (!sdb_create (s)) if (!sdb_create (s))
return 0; return 0;
sdb_dump_begin (s); sdb_dump_begin (s);
while (sdb_dump_next (s, k, v)) { while (sdb_dump_dupnext (s, &k, &v)) {
ut32 hash = sdb_hash (k, 0); ut32 hash = sdb_hash (k, 0);
SdbHashEntry *hte = ht_search (s->ht, hash); SdbHashEntry *hte = ht_search (s->ht, hash);
if (hte) { if (hte) {
@ -259,6 +260,8 @@ SDB_VISIBLE int sdb_sync (Sdb* s) {
ht_remove_entry (s->ht, hte); ht_remove_entry (s->ht, hte);
} else if (*v) } else if (*v)
sdb_append (s, k, v); sdb_append (s, k, v);
free (k);
free (v);
} }
/* append new keyvalues */ /* append new keyvalues */
ls_foreach (s->ht->list, iter, kv) { ls_foreach (s->ht->list, iter, kv) {
@ -284,6 +287,15 @@ static ut32 getnum(int fd) {
return ret; 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) { static int getbytes(int fd, char *b, int len) {
if (read (fd, b, len) != len) if (read (fd, b, len) != len)
return -1; return -1;
@ -300,21 +312,43 @@ SDB_VISIBLE void sdb_dump_begin (Sdb* s) {
} else eod = pos = 0; } else eod = pos = 0;
} }
// XXX: overflow if caller doesnt respects sizes SDB_VISIBLE int sdb_dump_dupnext (Sdb* s, char **key, char **value) {
// TODO: add support for readonly dump next here ut32 vlen, klen;
SDB_VISIBLE int sdb_dump_next (Sdb* s, char *key, char *value) { if (s->fd==-1 || !getkvlen (s->fd, &klen, &vlen))
ut32 dlen, klen;
if (s->fd==-1 || !getkvlen (s->fd, &klen, &dlen))
return 0; return 0;
if (klen >= SDB_KSZ || dlen >= SDB_VSZ) if (klen<1 || vlen<1)
return 0; return 0;
pos += 4; if (key) {
if (key && getbytes (s->fd, key, klen)>0) *key = 0;
if (value && getbytes (s->fd, value, dlen)>0) { if (klen>0) {
key[klen] = value[dlen] = 0; *key = malloc (klen+1);
return 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 () { SDB_VISIBLE ut64 sdb_now () {
@ -389,7 +423,10 @@ SDB_VISIBLE void sdb_flush(Sdb* s) {
} }
#if __WINDOWS__ #if __WINDOWS__
#define r_sys_mkdir(x) (CreateDirectory(x,NULL)!=0) #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 #else
#define r_sys_mkdir(x) (mkdir(x,0755)!=-1) #define r_sys_mkdir(x) (mkdir(x,0755)!=-1)
#define r_sys_mkdir_failed() (errno != EEXIST) #define r_sys_mkdir_failed() (errno != EEXIST)

View File

@ -7,6 +7,11 @@
#define SDB_VISIBLE #define SDB_VISIBLE
#endif #endif
#if __WIN32__ || __CYGWIN__ || MINGW32
#define __WINDOWS__ 1
#include <windows.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
@ -43,14 +48,11 @@ typedef struct sdb_t {
SdbList *ns; SdbList *ns;
} Sdb; } Sdb;
// XXX: use buckets here, drop these limits #define SDB_KSZ 0xff
#define SDB_BLOCK 4096
#define SDB_KSZ 64
#define SDB_VSZ (SDB_BLOCK-sizeof(ut64)-SDB_KEYSIZE)
typedef struct sdb_kv { typedef struct sdb_kv {
char key[SDB_KSZ]; // key = 64 char key[SDB_KSZ];
char value[SDB_VSZ]; // value = 4032 char *value;
ut64 expire; ut64 expire;
ut32 cas; ut32 cas;
} SdbKv; } SdbKv;
@ -84,6 +86,7 @@ int sdb_finish (Sdb *s);
/* iterate */ /* iterate */
void sdb_dump_begin (Sdb* s); void sdb_dump_begin (Sdb* s);
int sdb_dump_next (Sdb* s, char *key, char *value); // XXX: needs refactor? int sdb_dump_next (Sdb* s, char *key, char *value); // XXX: needs refactor?
int sdb_dump_dupnext (Sdb* s, char **key, char **value);
/* numeric */ /* numeric */
R_API char *sdb_itoa(ut64 n, char *s); R_API char *sdb_itoa(ut64 n, char *s);

View File

@ -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) { SDB_VISIBLE int sdb_ainsn(Sdb *s, const char *key, int idx, ut64 val, ut32 cas) {
char valstr[64]; 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 // 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) { 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); const char *str = sdb_getc (s, key, 0);
int lnstr, lstr, lval, ret; int lnstr, lstr, lval, ret;
char *x, *ptr, *nstr = NULL; char *x, *ptr;
if (!str || !*str) if (!str || !*str)
return sdb_set (s, key, val, cas); return sdb_set (s, key, val, cas);
lval = strlen (val); 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; x[lval] = SDB_RS;
memcpy (x+lval+1, str, lstr+1); memcpy (x+lval+1, str, lstr+1);
} else { } else {
nstr = strdup (str); char *nstr = strdup (str);
ptr = sdb_aindex_nc (nstr, idx); ptr = sdb_aindex_nc (nstr, idx);
if (ptr) { if (ptr) {
*(ptr-1) = 0; *(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; x[lnstr+lval+1] = SDB_RS;
memcpy (x+lval+2+lnstr, ptr, strlen (ptr)+1); memcpy (x+lval+2+lnstr, ptr, strlen (ptr)+1);
} else ret = 0; } else ret = 0;
free (nstr);
} }
ret = sdb_set (s, key, x, cas); ret = sdb_set (s, key, x, cas);
free (nstr);
free (x); free (x);
return ret; return ret;
} }

View File

@ -9,7 +9,7 @@ MAKE_JOBS=8
OLD_LDFLAGS="${LDFLAGS}" OLD_LDFLAGS="${LDFLAGS}"
unset 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 if [ -x /usr/bin/pacman ]; then
make clean make clean