mirror of
https://github.com/radareorg/radare2.git
synced 2025-02-02 11:43:23 +00:00
Update sdb
This commit is contained in:
parent
052fdb2a5d
commit
87fe959e26
@ -164,7 +164,7 @@ IOS_CC=$(shell xcrun --sdk iphoneos --find clang) -isysroot $(shell xcrun --sdk
|
||||
IOS_AR=$(shell xcrun --sdk iphoneos --find ar)
|
||||
IOS_RL=$(shell xcrun --sdk iphoneos --find ranlib)
|
||||
ios: src/sdb_version.h
|
||||
${MAKE} OS=Darwin ARCH=arm CC="${IOS_CC}" AR="${IOS_AR}" RANLIB="${IOS_RL}" HAVE_VALA= all
|
||||
${MAKE} CFLAGS=-DUSE_DLSYSTEM=1 OS=Darwin ARCH=arm CC="${IOS_CC}" AR="${IOS_AR}" RANLIB="${IOS_RL}" HAVE_VALA= all
|
||||
|
||||
.PHONY: all ${VALADIR} clean dist w32 ios
|
||||
.PHONY: install-dirs install uninstall deinstall symstall
|
||||
|
@ -302,7 +302,9 @@ SDB_API HT_(Kv)* Ht_(find_kv)(HtName_(Ht)* ht, const KEY_TYPE key, bool* found)
|
||||
*found = false;
|
||||
}
|
||||
if (!ht) {
|
||||
*found = false;
|
||||
if (found) {
|
||||
*found = false;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,9 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#if USE_DLSYSTEM
|
||||
#include <dlfcn.h>
|
||||
#endif
|
||||
#include "sdb.h"
|
||||
|
||||
#define MODE_ZERO '0'
|
||||
@ -168,24 +171,48 @@ static void synchronize(int sig UNUSED) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static int sdb_grep_dump(const char *dbname, int fmt, bool grep,
|
||||
const char *expgrep) {
|
||||
char cname[SDB_MAX_KEY];
|
||||
char *v, k[SDB_MAX_KEY] = { 0 };
|
||||
char *d = cname;
|
||||
const char *comma = "";
|
||||
static char* get_name(const char*name) {
|
||||
char *n = strdup (name);
|
||||
char *v, *d = n;
|
||||
// local db beacuse is readonly and we dont need to finalize in case of ^C
|
||||
for (v=(char*)dbname;*v;v++) {
|
||||
for (v=(char*)n; *v; v++) {
|
||||
if (*v == '.') {
|
||||
break;
|
||||
}
|
||||
*d++ = *v;
|
||||
}
|
||||
*d++ = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
static char* get_cname(const char*name) {
|
||||
char *n = strdup (name);
|
||||
char *v, *d = n;
|
||||
// local db beacuse is readonly and we dont need to finalize in case of ^C
|
||||
for (v=(char*)n; *v; v++) {
|
||||
if (*v == '/') {
|
||||
*d++ = '_';
|
||||
continue;
|
||||
}
|
||||
if (*v == '.') {
|
||||
break;
|
||||
}
|
||||
*d++ = *v;
|
||||
}
|
||||
*d++ = 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
static int sdb_grep_dump(const char *dbname, int fmt, bool grep,
|
||||
const char *expgrep) {
|
||||
char *v, k[SDB_MAX_KEY] = { 0 };
|
||||
const char *comma = "";
|
||||
Sdb *db = sdb_new (NULL, dbname, 0);
|
||||
if (!db) {
|
||||
return 1;
|
||||
}
|
||||
char *cname = get_cname (dbname);
|
||||
char *name = get_name (dbname);
|
||||
sdb_config (db, options);
|
||||
sdb_dump_begin (db);
|
||||
switch (fmt) {
|
||||
@ -223,7 +250,20 @@ static int sdb_grep_dump(const char *dbname, int fmt, bool grep,
|
||||
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);
|
||||
@ -242,32 +282,43 @@ static int sdb_grep_dump(const char *dbname, int fmt, bool grep,
|
||||
case MODE_CGEN:
|
||||
printf ("%%%%\n");
|
||||
printf ("// SDB-CGEN V"SDB_VERSION"\n");
|
||||
printf ("const char* %s_get(const char *s) {\n", cname);
|
||||
printf ("// %p\n", cname);
|
||||
printf ("const char* gperf_%s_get(const char *s) {\n", cname);
|
||||
printf ("\tconst struct kv *o = sdb_get_c_%s (s, strlen(s));\n", cname);
|
||||
printf ("\treturn o? o->value: NULL;\n");
|
||||
printf ("}\n");
|
||||
printf ("const unsigned int %s_hash(const char *s) {\n", cname);
|
||||
printf ("const unsigned int gperf_%s_hash(const char *s) {\n", cname);
|
||||
printf ("\treturn sdb_hash_c_%s(s, strlen (s));\n", cname);
|
||||
printf ("}\n");
|
||||
printf (
|
||||
printf (
|
||||
"struct {const char*name;void*get;void*hash;} gperf_%s = {\n"
|
||||
"\t.name = \"%s\",\n"
|
||||
"\t.get = &gperf_%s_get,\n"
|
||||
"\t.hash = &gperf_%s_hash\n"
|
||||
"};\n"
|
||||
"\n"
|
||||
"#if MAIN\n"
|
||||
"int main () {\n"
|
||||
" char line[1024];\n"
|
||||
" FILE *fd = fopen (\"%s.gperf\", \"r\");\n"
|
||||
" if (!fd) {\n"
|
||||
" fprintf (stderr, \"Cannot open %s.gperf\\n\");\n"
|
||||
" return 1;\n"
|
||||
" }\n"
|
||||
" int mode = 0;\n"
|
||||
" printf (\"#ifndef INCLUDE_%s_H\\n\");\n"
|
||||
" printf (\"#define INCLUDE_%s_H 1\\n\");\n"
|
||||
" while (!feof (fd)) {\n"
|
||||
" *line = 0;\n"
|
||||
" fgets(line,sizeof(line),fd);\n"
|
||||
" fgets (line, sizeof (line), fd);\n"
|
||||
" if (mode == 1) {\n"
|
||||
" char *comma = strchr (line, ',');\n"
|
||||
" if(comma) {\n"
|
||||
" if (comma) {\n"
|
||||
" *comma = 0;\n"
|
||||
" char *up = strdup(line);\n"
|
||||
" char *p = up;while(*p){*p=toupper(*p);p++;}\n"
|
||||
" printf (\"#define %s_%%s %%d\\n\",\n"
|
||||
" line, sdb_hash_c_%s (line, comma-line));\n"
|
||||
" char *up = strdup (line);\n"
|
||||
" char *p = up; while (*p) { *p = toupper (*p); p++; }\n"
|
||||
" printf (\"#define GPERF_%s_%%s %%d\\n\",\n"
|
||||
" line, sdb_hash_c_%s (line, comma - line));\n"
|
||||
" }\n"
|
||||
" }\n"
|
||||
" if (*line == '%%' && line[1] == '%%')\n"
|
||||
@ -276,10 +327,11 @@ printf (
|
||||
" printf (\"#endif\\n\");\n"
|
||||
"}\n"
|
||||
"#endif\n",
|
||||
cname, cname,
|
||||
cname, cname,
|
||||
cname
|
||||
);
|
||||
cname, cname, cname,
|
||||
cname, name, name,
|
||||
cname, cname,
|
||||
cname, cname
|
||||
);
|
||||
printf ("\n");
|
||||
break;
|
||||
case MODE_JSON:
|
||||
@ -287,6 +339,8 @@ cname
|
||||
break;
|
||||
}
|
||||
sdb_free (db);
|
||||
free (cname);
|
||||
free (name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -477,14 +531,23 @@ static int showcount(const char *db) {
|
||||
}
|
||||
|
||||
static int gen_gperf(const char *file, const char *name) {
|
||||
int (*_system)(const char *cmd);
|
||||
const size_t bufsz = 4096;
|
||||
char *buf = malloc (bufsz);
|
||||
|
||||
char *out = malloc (strlen (file) +32);
|
||||
snprintf(out, strlen (file) + 32, "%s.gperf", name);
|
||||
char *out = malloc (strlen (file) + 32);
|
||||
snprintf (out, strlen (file) + 32, "%s.gperf", name);
|
||||
int wd = open (out, O_RDWR);
|
||||
ftruncate (wd, 0);
|
||||
int rc = 0;
|
||||
#if USE_DLSYSTEM
|
||||
_system = dlsym (NULL, "system");
|
||||
if (!_system) {
|
||||
_system = puts;
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
_system = system;
|
||||
#endif
|
||||
if (wd != -1) {
|
||||
dup2 (1, 999);
|
||||
dup2 (wd, 1);
|
||||
@ -494,21 +557,23 @@ static int gen_gperf(const char *file, const char *name) {
|
||||
dup2 (999, 1);
|
||||
} else {
|
||||
snprintf (buf, bufsz, "sdb -G %s > %s.gperf\n", file, name);
|
||||
rc = system (buf);
|
||||
rc = _system (buf);
|
||||
}
|
||||
if (rc == 0) {
|
||||
char *cname = get_cname (name);
|
||||
snprintf (buf, bufsz, "gperf -aclEDCIG --null-strings -H sdb_hash_c_%s"
|
||||
" -N sdb_get_c_%s -t %s.gperf > %s.c\n", name, name, name, name);
|
||||
rc = system (buf);
|
||||
" -N sdb_get_c_%s -t %s.gperf > %s.c\n", cname, cname, name, name);
|
||||
free (cname);
|
||||
rc = _system (buf);
|
||||
if (rc == 0) {
|
||||
snprintf (buf, bufsz, "gcc -DMAIN=1 %s.c ; ./a.out > %s.h\n", name, name);
|
||||
rc = system (buf);
|
||||
rc = _system (buf);
|
||||
if (rc == 0) {
|
||||
eprintf ("Generated %s.c and %s.h\n", name, name);
|
||||
}
|
||||
} else {
|
||||
eprintf ("%s\n", buf);
|
||||
eprintf ("Cannot run gperf\n");
|
||||
eprintf ("%s\n", buf);
|
||||
}
|
||||
} else {
|
||||
eprintf ("Outdated sdb binary in PATH?\n");
|
||||
|
@ -245,27 +245,33 @@ SDB_API const char *sdb_const_get_len(Sdb* s, const char *key, int *vlen, ut32 *
|
||||
size_t keylen = strlen (key);
|
||||
|
||||
/* search in memory */
|
||||
SdbKv *kv = (SdbKv*) sdb_ht_find_kvp (s->ht, key, &found);
|
||||
if (found) {
|
||||
if (!sdbkv_value (kv) || !*sdbkv_value (kv)) {
|
||||
return NULL;
|
||||
}
|
||||
if (s->timestamped && kv->expire) {
|
||||
if (!now) {
|
||||
now = sdb_now ();
|
||||
}
|
||||
if (now > kv->expire) {
|
||||
sdb_unset (s, key, 0);
|
||||
if (s->ht) {
|
||||
SdbKv *kv = (SdbKv*) sdb_ht_find_kvp (s->ht, key, &found);
|
||||
if (found) {
|
||||
if (!sdbkv_value (kv) || !*sdbkv_value (kv)) {
|
||||
return NULL;
|
||||
}
|
||||
if (s->timestamped && kv->expire) {
|
||||
if (!now) {
|
||||
now = sdb_now ();
|
||||
}
|
||||
if (now > kv->expire) {
|
||||
sdb_unset (s, key, 0);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
if (cas) {
|
||||
*cas = kv->cas;
|
||||
}
|
||||
if (vlen) {
|
||||
*vlen = sdbkv_value_len (kv);
|
||||
}
|
||||
return sdbkv_value (kv);
|
||||
}
|
||||
if (cas) {
|
||||
*cas = kv->cas;
|
||||
}
|
||||
if (vlen) {
|
||||
*vlen = sdbkv_value_len (kv);
|
||||
}
|
||||
return sdbkv_value (kv);
|
||||
}
|
||||
/* search in gperf */
|
||||
if (s->gp && s->gp->get) {
|
||||
return s->gp->get (key);
|
||||
}
|
||||
/* search in disk */
|
||||
if (s->fd == -1) {
|
||||
@ -390,6 +396,14 @@ SDB_API bool sdb_exists(Sdb* s, const char *key) {
|
||||
return false;
|
||||
}
|
||||
|
||||
SDB_API int sdb_open_gperf(Sdb *s, SdbGperf *gp) {
|
||||
if (!s || !gp) {
|
||||
return -1;
|
||||
}
|
||||
s->gp = gp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDB_API int sdb_open(Sdb *s, const char *file) {
|
||||
struct stat st;
|
||||
if (!s) {
|
||||
@ -440,6 +454,7 @@ SDB_API void sdb_close(Sdb *s) {
|
||||
free (s->dir);
|
||||
s->dir = NULL;
|
||||
}
|
||||
s->gp = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
@ -626,7 +641,7 @@ static ut32 sdb_set_internal(Sdb* s, const char *key, char *val, bool owned, ut3
|
||||
sdb_hook_call (s, key, val);
|
||||
return cas;
|
||||
}
|
||||
// kv set failed, no need to callback sdb_hook_call (s, key, val);
|
||||
// kv set failed, no need to callback sdb_hook_call (s, key, val);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -81,6 +81,11 @@ extern char *strdup (const char *);
|
||||
#define SDB_KSZ 0xff
|
||||
#define SDB_VSZ 0xffffff
|
||||
|
||||
typedef struct sdb_gperf_t {
|
||||
const char *name;
|
||||
const char *(*get)(const char *k);
|
||||
unsigned int *(*hash)(const char *k);
|
||||
} SdbGperf;
|
||||
|
||||
typedef struct sdb_t {
|
||||
char *dir; // path+name
|
||||
@ -95,6 +100,7 @@ typedef struct sdb_t {
|
||||
HtPP *ht;
|
||||
ut32 eod;
|
||||
ut32 pos;
|
||||
SdbGperf *gp;
|
||||
int fdump;
|
||||
char *ndump;
|
||||
ut64 expire;
|
||||
@ -119,6 +125,7 @@ SDB_API Sdb* sdb_new0(void);
|
||||
SDB_API Sdb* sdb_new(const char *path, const char *file, int lock);
|
||||
|
||||
SDB_API int sdb_open(Sdb *s, const char *file);
|
||||
SDB_API int sdb_open_gperf(Sdb *s, SdbGperf *g);
|
||||
SDB_API void sdb_close(Sdb *s);
|
||||
|
||||
SDB_API void sdb_config(Sdb *s, int options);
|
||||
|
Loading…
x
Reference in New Issue
Block a user