From e2278cc50fcd9054a7767522e58fe3518de2354c Mon Sep 17 00:00:00 2001 From: Riccardo Schirone Date: Fri, 5 Oct 2018 15:21:46 +0200 Subject: [PATCH] sync sdb: some refactoring on how to access base SdbKv fields (#11741) --- libr/anal/anal.c | 2 +- libr/anal/esil_trace.c | 2 +- libr/anal/meta.c | 6 +- libr/bin/dwarf.c | 6 +- libr/cons/pal.c | 2 +- libr/core/canal.c | 2 +- libr/core/cbin.c | 14 ++-- libr/core/cmd_print.c | 4 +- libr/core/cmd_search.c | 8 +-- libr/core/cmd_type.c | 22 +++---- libr/core/core.c | 28 ++++---- libr/flag/tags.c | 2 +- libr/include/sdb/sdb.h | 2 +- libr/include/sdb/sdbht.h | 23 ++++++- libr/util/ctype.c | 10 +-- shlr/sdb/Makefile | 3 +- shlr/sdb/meson.build | 18 ++++++ shlr/sdb/src/query.c | 7 +- shlr/sdb/src/sdb.c | 135 ++++++++++++++------------------------- shlr/sdb/src/sdb.h | 2 +- shlr/sdb/src/sdbht.c | 6 +- shlr/sdb/src/sdbht.h | 23 ++++++- 22 files changed, 169 insertions(+), 158 deletions(-) diff --git a/libr/anal/anal.c b/libr/anal/anal.c index f6d311ac21..e07fb62247 100644 --- a/libr/anal/anal.c +++ b/libr/anal/anal.c @@ -630,7 +630,7 @@ R_API void r_anal_build_range_on_hints(RAnal *a) { SdbList *sdb_range = sdb_foreach_list (a->sdb_hints, true); //just grab when hint->bit changes with the previous one ls_foreach (sdb_range, iter, kv) { - RAnalHint *hint = r_anal_hint_from_string (a, sdb_atoi (kv->key + 5), kv->value); + RAnalHint *hint = r_anal_hint_from_string (a, sdb_atoi (sdbkv_key (kv) + 5), sdbkv_value (kv)); if (hint->bits && range_bits != hint->bits) { RAnalRange *range = R_NEW0 (RAnalRange); if (range) { diff --git a/libr/anal/esil_trace.c b/libr/anal/esil_trace.c index 5f3e483e5a..72dacb20f7 100644 --- a/libr/anal/esil_trace.c +++ b/libr/anal/esil_trace.c @@ -129,7 +129,7 @@ R_API void r_anal_esil_trace_list (RAnalEsil *esil) { SdbListIter *iter; SdbList *list = sdb_foreach_list (esil->db_trace, true); ls_foreach (list, iter, kv) { - eprintf ("%s=%s\n", kv->key, kv->value); + eprintf ("%s=%s\n", sdbkv_key (kv), sdbkv_value (kv)); } ls_free (list); } diff --git a/libr/anal/meta.c b/libr/anal/meta.c index da891f4402..439107f1ba 100644 --- a/libr/anal/meta.c +++ b/libr/anal/meta.c @@ -718,11 +718,11 @@ R_API int r_meta_list_cb(RAnal *a, int type, int rad, SdbForeachCallback cb, voi } isFirst = true; // TODO: kill global ls_foreach (ls, lsi, kv) { - if (type == R_META_TYPE_ANY || (strlen (kv->key) > 5 && kv->key[5] == type)) { + if (type == R_META_TYPE_ANY || (strlen (sdbkv_key (kv)) > 5 && sdbkv_key (kv)[5] == type)) { if (cb) { - cb ((void *)&ui, kv->key, kv->value); + cb ((void *)&ui, sdbkv_key (kv), sdbkv_value (kv)); } else { - meta_print_item ((void *)&ui, kv->key, kv->value); + meta_print_item ((void *)&ui, sdbkv_key (kv), sdbkv_value (kv)); } } } diff --git a/libr/bin/dwarf.c b/libr/bin/dwarf.c index 09a35876cf..9bd98cd732 100644 --- a/libr/bin/dwarf.c +++ b/libr/bin/dwarf.c @@ -1670,11 +1670,11 @@ R_API RList *r_bin_dwarf_parse_line(RBin *a, int mode) { SdbKv *kv; SdbList *ls = sdb_foreach_list (binfile->sdb_addrinfo, false); ls_foreach (ls, iter, kv) { - if (!strncmp (kv->key, "0x", 2)) { + if (!strncmp (sdbkv_key (kv), "0x", 2)) { ut64 addr; RBinDwarfRow *row; int line; - char *file = strdup (kv->value); + char *file = strdup (sdbkv_value (kv)); if (!file) { free (buf); ls_free (ls); @@ -1685,7 +1685,7 @@ R_API RList *r_bin_dwarf_parse_line(RBin *a, int mode) { if (tok) { *tok++ = 0; line = atoi (tok); - addr = r_num_math (NULL, kv->key); + addr = r_num_math (NULL, sdbkv_key (kv)); row = r_bin_dwarf_row_new (addr, file, line, 0); r_list_append (list, row); } diff --git a/libr/cons/pal.c b/libr/cons/pal.c index 0836134bfd..c2900257c1 100644 --- a/libr/cons/pal.c +++ b/libr/cons/pal.c @@ -582,7 +582,7 @@ R_API void r_cons_pal_update_event() { r_cons_rainbow_free (); r_cons_rainbow_new (list->length); ls_foreach (list, iter, kv) { - r_cons_singleton ()->pal.rainbow[n++] = strdup (kv->key); + r_cons_singleton ()->pal.rainbow[n++] = strdup (sdbkv_key (kv)); } r_cons_singleton ()->pal.rainbow_sz = n; ls_free (list); diff --git a/libr/core/canal.c b/libr/core/canal.c index 6a170277ff..e1124619fa 100644 --- a/libr/core/canal.c +++ b/libr/core/canal.c @@ -1020,7 +1020,7 @@ R_API void r_core_anal_hint_list(RAnal *a, int mode) { SdbListIter *lsi; SdbKv *kv; ls_foreach (ls, lsi, kv) { - cb (&hls, kv->key, kv->value); + cb (&hls, sdbkv_key (kv), sdbkv_value (kv)); } ls_free (ls); #endif diff --git a/libr/core/cbin.c b/libr/core/cbin.c index 104e996682..a341d1cbb9 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -1047,7 +1047,7 @@ static int bin_source(RCore *r, int mode) { SdbKv *kv; SdbList *ls = sdb_foreach_list (binfile->sdb_addrinfo, false); ls_foreach (ls, iter, kv) { - char *v = kv->value; + char *v = sdbkv_value (kv); RList *list = r_str_split_list (v, "|"); srcline = r_list_get_bottom (list); if (srcline) { @@ -3344,8 +3344,8 @@ R_API void r_core_bin_export_info_rad(RCore *core) { // iterate over all keys SdbList *ls = sdb_foreach_list (db, false); ls_foreach (ls, iter, kv) { - char *k = kv->key; - char *v = kv->value; + char *k = sdbkv_key (kv); + char *v = sdbkv_value (kv); char *dup = strdup (k); //printf ("?e (%s) (%s)\n", k, v); if ((flagname = strstr (dup, ".offset"))) { @@ -3362,8 +3362,8 @@ R_API void r_core_bin_export_info_rad(RCore *core) { } R_FREE (offset); ls_foreach (ls, iter, kv) { - char *k = kv->key; - char *v = kv->value; + char *k = sdbkv_key (kv); + char *v = sdbkv_value (kv); char *dup = strdup (k); if ((flagname = strstr (dup, ".format"))) { *flagname = 0; @@ -3376,8 +3376,8 @@ R_API void r_core_bin_export_info_rad(RCore *core) { free (dup); } ls_foreach (ls, iter, kv) { - char *k = kv->key; - char *v = kv->value; + char *k = sdbkv_key (kv); + char *v = sdbkv_value (kv); char *dup = strdup (k); if ((flagname = strstr (dup, ".format"))) { *flagname = 0; diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index 08051d9537..eed4cb060a 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -1025,7 +1025,7 @@ static void cmd_print_format(RCore *core, const char *_input, const ut8* block, SdbKv *kv; SdbList *sdbls = sdb_foreach_list (sht, false); ls_foreach (sdbls, iter, kv) { - r_cons_println (kv->value); + r_cons_println (sdbkv_value (kv)); } } } else { @@ -1117,7 +1117,7 @@ static void cmd_print_format(RCore *core, const char *_input, const ut8* block, SdbKv *kv; SdbList *sdbls = sdb_foreach_list (core->print->formats, false); ls_foreach (sdbls, iter, kv) { - r_cons_printf ("pf.%s %s\n", kv->key, kv->value); + r_cons_printf ("pf.%s %s\n", sdbkv_key (kv), sdbkv_value (kv)); } /* delete a format */ } else if (input[1] && input[2] == '-') { diff --git a/libr/core/cmd_search.c b/libr/core/cmd_search.c index 97ed94c979..142ec880db 100644 --- a/libr/core/cmd_search.c +++ b/libr/core/cmd_search.c @@ -2262,7 +2262,7 @@ static void rop_kuery(void *data, const char *input) { ls_foreach (db_rop->ns, it, ns) { sdb_list = sdb_foreach_list (ns->sdb, false); ls_foreach (sdb_list, sdb_iter, kv) { - r_cons_printf ("%s ", kv->key); + r_cons_printf ("%s ", sdbkv_key (kv)); } } break; @@ -2271,7 +2271,7 @@ static void rop_kuery(void *data, const char *input) { ls_foreach (db_rop->ns, it, ns) { sdb_list = sdb_foreach_list (ns->sdb, false); ls_foreach (sdb_list, sdb_iter, kv) { - char *dup = strdup (kv->value); + char *dup = strdup (sdbkv_value (kv)); bool flag = false; // to free tok when doing strdup char *size = strtok (dup, " "); char *tok = strtok (NULL, "{}"); @@ -2286,7 +2286,7 @@ static void rop_kuery(void *data, const char *input) { r_cons_print (","); } r_cons_printf ("{\"address\":%s, \"size\":%s, \"type\":\"%s\", \"effect\":\"%s\"}", - kv->key, size, ns->name, tok); + sdbkv_key (kv), size, ns->name, tok); free (dup); if (flag) { free (tok); @@ -2744,7 +2744,7 @@ reread: goto beach; } - char *s = kv->value; + char *s = sdbkv_value (kv); ut64 addr; int opsz; int mode = 0; diff --git a/libr/core/cmd_type.c b/libr/core/cmd_type.c index 50b9138728..ced95a8a80 100644 --- a/libr/core/cmd_type.c +++ b/libr/core/cmd_type.c @@ -375,20 +375,20 @@ static void typesList(RCore *core, int mode) { SdbListIter *it; SdbKv *kv; ls_foreach (ls, it, kv) { - if (!strncmp (kv->value, "type", strlen ("type") + 1)) { + if (!strncmp (sdbkv_value (kv), "type", strlen ("type") + 1)) { ls_append (filtls, kv); } } if (mode == 'j') { r_cons_print ("["); ls_foreach (filtls, it, kv) { - sdbforcb_json ((void *)core->anal->sdb_types, kv->key, kv->value); + sdbforcb_json ((void *)core->anal->sdb_types, sdbkv_key (kv), sdbkv_value (kv)); if (it->n) { r_cons_print (","); } } r_cons_println ("]"); } else { ls_foreach (filtls, it, kv) { - sdbforcb_default ((void *)core->anal->sdb_types, kv->key, kv->value); + sdbforcb_default ((void *)core->anal->sdb_types, sdbkv_key (kv), sdbkv_value (kv)); } } ls_free (ls); @@ -757,10 +757,10 @@ static int cmd_type(void *data, const char *input) { SdbListIter *iter; SdbList *l = sdb_foreach_list (TDB, true); ls_foreach (l, iter, kv) { - if (!strcmp (kv->value, "enum")) { - if (!name || strcmp (kv->value, name)) { + if (!strcmp (sdbkv_value (kv), "enum")) { + if (!name || strcmp (sdbkv_value (kv), name)) { free (name); - name = strdup (kv->key); + name = strdup (sdbkv_key (kv)); r_cons_println (name); } } @@ -1096,8 +1096,8 @@ static int cmd_type(void *data, const char *input) { snprintf (tmp, tmp_len + 1, "%s.%s.", type, name); SdbList *l = sdb_foreach_list (TDB, true); ls_foreach (l, iter, kv) { - if (!strncmp (kv->key, tmp, tmp_len)) { - r_type_del (TDB, kv->key); + if (!strncmp (sdbkv_key (kv), tmp, tmp_len)) { + r_type_del (TDB, sdbkv_key (kv)); } } ls_free (l); @@ -1142,10 +1142,10 @@ static int cmd_type(void *data, const char *input) { SdbListIter *iter; SdbList *l = sdb_foreach_list (TDB, true); ls_foreach (l, iter, kv) { - if (!strcmp (kv->value, "typedef")) { - if (!name || strcmp (kv->value, name)) { + if (!strcmp (sdbkv_value (kv), "typedef")) { + if (!name || strcmp (sdbkv_value (kv), name)) { free (name); - name = strdup (kv->key); + name = strdup (sdbkv_key (kv)); r_cons_println (name); } } diff --git a/libr/core/core.c b/libr/core/core.c index c15a1621b7..b644003617 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -1573,14 +1573,14 @@ static int autocomplete(RLine *line) { int j = 0; ls_foreach (sls, iter, kv) { int len = strlen (line->buffer.data + chr); - int minlen = R_MIN (len, strlen (kv->key)); - if (!len || !strncmp (line->buffer.data + chr, kv->key, minlen)) { + int minlen = R_MIN (len, strlen (sdbkv_key (kv))); + if (!len || !strncmp (line->buffer.data + chr, sdbkv_key (kv), minlen)) { char *p = strchr (line->buffer.data + chr, '.'); if (p) { - j += autocomplete_pfele (core, kv->key, pfx, j, p + 1); + j += autocomplete_pfele (core, sdbkv_key (kv), pfx, j, p + 1); break; } else { - tmp_argv[j++] = r_str_newf ("pf%s.%s", pfx, kv->key); + tmp_argv[j++] = r_str_newf ("pf%s.%s", pfx, sdbkv_key (kv)); } } } @@ -1626,10 +1626,10 @@ static int autocomplete(RLine *line) { int chr = (line->buffer.data[1] == ' ')? 2: 3; ls_foreach (l, iter, kv) { int len = strlen (line->buffer.data + chr); - if (!len || !strncmp (line->buffer.data + chr, kv->key, len)) { - if (!strcmp (kv->value, "type") || !strcmp (kv->value, "enum") - || !strcmp (kv->value, "struct")) { - tmp_argv[i++] = strdup (kv->key); + if (!len || !strncmp (line->buffer.data + chr, sdbkv_key (kv), len)) { + if (!strcmp (sdbkv_value (kv), "type") || !strcmp (sdbkv_value (kv), "enum") + || !strcmp (sdbkv_value (kv), "struct")) { + tmp_argv[i++] = strdup (sdbkv_key (kv)); } } } @@ -1648,9 +1648,9 @@ static int autocomplete(RLine *line) { int chr = 3; ls_foreach (l, iter, kv) { int len = strlen (line->buffer.data + chr); - if (!len || !strncmp (line->buffer.data + chr, kv->key, len)) { - if (!strcmp (kv->value, "enum")) { - tmp_argv[i++] = strdup (kv->key); + if (!len || !strncmp (line->buffer.data + chr, sdbkv_key (kv), len)) { + if (!strcmp (sdbkv_value (kv), "enum")) { + tmp_argv[i++] = strdup (sdbkv_key (kv)); } } } @@ -1675,9 +1675,9 @@ static int autocomplete(RLine *line) { int chr = (line->buffer.data[2] == ' ')? 3: 4; ls_foreach (l, iter, kv) { int len = strlen (line->buffer.data + chr); - if (!len || !strncmp (line->buffer.data + chr, kv->key, len)) { - if (!strncmp (kv->value, "struct", strlen ("struct") + 1)) { - tmp_argv[i++] = strdup (kv->key); + if (!len || !strncmp (line->buffer.data + chr, sdbkv_key (kv), len)) { + if (!strncmp (sdbkv_value (kv), "struct", strlen ("struct") + 1)) { + tmp_argv[i++] = strdup (sdbkv_key (kv)); } } } diff --git a/libr/flag/tags.c b/libr/flag/tags.c index 750cf973e4..c596bd7251 100644 --- a/libr/flag/tags.c +++ b/libr/flag/tags.c @@ -14,7 +14,7 @@ R_API RList *r_flag_tags_list(RFlag *f) { SdbListIter *iter; SdbKv *kv; ls_foreach (o, iter, kv) { - const char *tag = kv->key; + const char *tag = sdbkv_key (kv); if (strlen (tag) < 5) { continue; } diff --git a/libr/include/sdb/sdb.h b/libr/include/sdb/sdb.h index db96f60592..ea7ad58e11 100644 --- a/libr/include/sdb/sdb.h +++ b/libr/include/sdb/sdb.h @@ -163,7 +163,7 @@ int sdb_concat(Sdb *s, const char *key, const char *value, ut32 cas); int sdb_uncat(Sdb *s, const char *key, const char *value, ut32 cas); int sdb_add(Sdb* s, const char *key, const char *val, ut32 cas); bool sdb_sync(Sdb*); -void sdb_kv_free(SdbKv *kv); +void sdbkv_free(SdbKv *kv); /* num.c */ int sdb_num_exists(Sdb*, const char *key); diff --git a/libr/include/sdb/sdbht.h b/libr/include/sdb/sdbht.h index ccc1374dc2..b1383315f5 100644 --- a/libr/include/sdb/sdbht.h +++ b/libr/include/sdb/sdbht.h @@ -14,10 +14,27 @@ typedef struct sdb_kv { ut64 expire; } SdbKv; -SDB_API SdbKv* sdb_kv_new2(const char *k, int kl, const char *v, int vl); -extern SdbKv* sdb_kv_new(const char *k, const char *v); +static inline char *sdbkv_key(const SdbKv *kv) { + return kv->key; +} + +static inline char *sdbkv_value(const SdbKv *kv) { + return kv->value; +} + +static inline ut32 sdbkv_key_len(const SdbKv *kv) { + return kv->key_len; +} + +static inline ut32 sdbkv_value_len(const SdbKv *kv) { + return kv->value_len; +} + +SDB_API SdbKv* sdbkv_new2(const char *k, int kl, const char *v, int vl); +extern SdbKv* sdbkv_new(const char *k, const char *v); +extern void sdbkv_free(SdbKv *kv); + extern ut32 sdb_hash(const char *key); -extern void sdb_kv_free(SdbKv *kv); SDB_API SdbHt* sdb_ht_new(void); // Destroy a hashtable and all of its entries. diff --git a/libr/util/ctype.c b/libr/util/ctype.c index 8bbab0421f..8ee4da90a6 100644 --- a/libr/util/ctype.c +++ b/libr/util/ctype.c @@ -221,8 +221,8 @@ R_API RList* r_type_get_by_offset(Sdb *TDB, ut64 offset) { SdbKv *kv; ls_foreach (ls, lsi, kv) { // TODO: Add unions support - if (!strncmp (kv->value, "struct", 6) && strncmp (kv->key, "struct.", 7)) { - char *res = r_type_get_struct_memb (TDB, kv->key, offset); + if (!strncmp (sdbkv_value (kv), "struct", 6) && strncmp (sdbkv_key (kv), "struct.", 7)) { + char *res = r_type_get_struct_memb (TDB, sdbkv_key (kv), offset); if (res) { r_list_append (offtypes, res); } @@ -245,13 +245,13 @@ R_API char *r_type_link_at (Sdb *TDB, ut64 addr) { SdbListIter *sdb_iter; SdbList *sdb_list = sdb_foreach_list (TDB, true); ls_foreach (sdb_list, sdb_iter, kv) { - if (strncmp (kv->key, "link.", strlen ("link."))) { + if (strncmp (sdbkv_key (kv), "link.", strlen ("link."))) { continue; } - const char *linkptr = sdb_fmt ("0x%s", kv->key + strlen ("link.")); + const char *linkptr = sdb_fmt ("0x%s", sdbkv_key (kv) + strlen ("link.")); ut64 baseaddr = r_num_math (NULL, linkptr); int delta = (addr > baseaddr)? addr - baseaddr: -1; - res = r_type_get_struct_memb (TDB, kv->value, delta); + res = r_type_get_struct_memb (TDB, sdbkv_value (kv), delta); if (res) { break; } diff --git a/shlr/sdb/Makefile b/shlr/sdb/Makefile index 74c6a77ee3..1b257fdce4 100644 --- a/shlr/sdb/Makefile +++ b/shlr/sdb/Makefile @@ -72,7 +72,8 @@ install-dirs: $(INSTALL_DIR) ${DESTDIR}${DATADIR}/vala/vapi ${DESTDIR}${INCDIR}/sdb INCFILES=src/sdb.h src/sdb_version.h src/cdb.h src/ht.h src/types.h -INCFILES+=src/ls.h src/cdb_make.h src/buffer.h src/config.h +INCFILES+=src/ls.h src/cdb_make.h src/buffer.h src/config.h src/sdbht.h +INCFILES+=src/dict.h install: pkgconfig install-dirs $(INSTALL_MAN) src/sdb.1 ${DESTDIR}${MANDIR} diff --git a/shlr/sdb/meson.build b/shlr/sdb/meson.build index 9c6c05e1c4..d126eb1c01 100644 --- a/shlr/sdb/meson.build +++ b/shlr/sdb/meson.build @@ -92,3 +92,21 @@ sdb_gen_cmd = [ '@OUTPUT@', '@INPUT@' ] + +pkgconfig_mod = import('pkgconfig') +pkgconfig_mod.generate( + libraries: [libsdb.get_shared_lib()], + subdirs: ['sdb', '.'], + version: sdb_version, + name: 'sdb', + filebase: 'sdb', + description: 'Simple DataBase' +) + +make_test_executable = find_program('make') +test('run tests', make_test_executable, + args: 'test', + env: ['BASEDIR=' + meson.current_build_dir()], + workdir: join_paths(meson.current_build_dir(), '..'), + depends: [sdb_exe, libsdb] +) diff --git a/shlr/sdb/src/query.c b/shlr/sdb/src/query.c index 05ff9f1a64..d0670c85b5 100644 --- a/shlr/sdb/src/query.c +++ b/shlr/sdb/src/query.c @@ -335,8 +335,7 @@ next_quote: SdbListIter *iter; SdbKv *kv; ls_foreach (list, iter, kv) { - //eprintf ("(%s)(%s)\n", kv->key, kv->value); - foreach_list_cb (&user, kv->key, kv->value); + foreach_list_cb (&user, sdbkv_key (kv), sdbkv_value (kv)); } ls_free (list); goto fail; @@ -376,9 +375,9 @@ next_quote: SdbListIter *li; SdbList *l = sdb_foreach_match (s, cmd + 2, false); ls_foreach (l, li, kv) { - strbuf_append (out, kv->key, 0); + strbuf_append (out, sdbkv_key (kv), 0); strbuf_append (out, "=", 0); - strbuf_append (out, kv->value, 1); + strbuf_append (out, sdbkv_value (kv), 1); } fflush (stdout); ls_free (l); diff --git a/shlr/sdb/src/sdb.c b/shlr/sdb/src/sdb.c index 35b377c390..62f9dcb71b 100644 --- a/shlr/sdb/src/sdb.c +++ b/shlr/sdb/src/sdb.c @@ -91,7 +91,6 @@ SDB_API Sdb* sdb_new(const char *path, const char *name, int lock) { } s->ht = sdb_ht_new (); s->lock = lock; - // s->ht->list->free = (SdbListFree)sdb_kv_free; // if open fails ignore if (global_hook) { sdb_hook (s, global_hook, global_user); @@ -184,7 +183,7 @@ static void sdb_fini(Sdb* s, int donull) { } free (s->ndump); free (s->dir); - free (s->tmpkv.value); + free (sdbkv_value (&s->tmpkv)); s->tmpkv.value_len = 0; if (donull) { memset (s, 0, sizeof (Sdb)); @@ -226,7 +225,7 @@ SDB_API const char *sdb_const_get_len(Sdb* s, const char *key, int *vlen, ut32 * /* search in memory */ kv = (SdbKv*) sdb_ht_find_kvp (s->ht, key, &found); if (found) { - if (!kv->value || !*kv->value) { + if (!sdbkv_value (kv) || !*sdbkv_value (kv)) { return NULL; } if (s->timestamped && kv->expire) { @@ -242,9 +241,9 @@ SDB_API const char *sdb_const_get_len(Sdb* s, const char *key, int *vlen, ut32 * *cas = kv->cas; } if (vlen) { - *vlen = kv->value_len; + *vlen = sdbkv_value_len (kv); } - return kv->value; + return sdbkv_value (kv); } /* search in disk */ if (s->fd == -1) { @@ -354,7 +353,7 @@ SDB_API bool sdb_exists(Sdb* s, const char *key) { } kv = (SdbKv*)sdb_ht_find_kvp (s->ht, key, &found); if (found && kv) { - return *kv->value; + return *sdbkv_value (kv); } if (s->fd == -1) { return false; @@ -453,7 +452,7 @@ static bool match(const char *str, const char *expr) { return strstr (str, expr); } -SDB_API bool sdb_kv_match(SdbKv *kv, const char *expr) { +SDB_API bool sdbkv_match(SdbKv *kv, const char *expr) { // TODO: add syntax to negate condition // TODO: add syntax to OR k/v instead of AND // [^]str[$]=[^]str[$] @@ -462,59 +461,19 @@ SDB_API bool sdb_kv_match(SdbKv *kv, const char *expr) { char *e = strdup (expr); char *ep = e + (eq - expr); *ep++ = 0; - bool res = !*e || match (kv->key, e); - bool res2 = !*ep || match (kv->value, ep); + bool res = !*e || match (sdbkv_key (kv), e); + bool res2 = !*ep || match (sdbkv_value (kv), ep); free (e); return res && res2; } - return match (kv->key, expr); + return match (sdbkv_key (kv), expr); } -SDB_API SdbKv* sdb_kv_new(const char *k, const char *v) { - return sdb_kv_new2 (k, strlen (k), v, strlen (v)); -#if 0 - SdbKv *kv; - int vl; - if (v) { - vl = strlen (v); - if (vl >= SDB_VSZ) { - return NULL; - } - } else { - vl = 0; - } - kv = R_NEW0 (SdbKv); - kv->key_len = strlen (k); - if (key_len >= SDB_KSZ) { - free (kv); - return NULL; - } - kv->key = malloc (kv->key_len + 1); - if (!kv->key) { - free (kv); - return NULL; - } - memcpy (kv->key, k, kv->key_len + 1); - kv->value_len = vl; - if (vl) { - kv->value = malloc (vl + 1); - if (!kv->value) { - free (kv->key); - free (kv); - return NULL; - } - memcpy (kv->value, v, vl + 1); - } else { - kv->value = NULL; - kv->value_len = 0; - } - kv->cas = nextcas (); - kv->expire = 0LL; - return kv; -#endif +SDB_API SdbKv* sdbkv_new(const char *k, const char *v) { + return sdbkv_new2 (k, strlen (k), v, strlen (v)); } -SDB_API SdbKv* sdb_kv_new2(const char *k, int kl, const char *v, int vl) { +SDB_API SdbKv* sdbkv_new2(const char *k, int kl, const char *v, int vl) { SdbKv *kv; if (v) { if (vl >= SDB_VSZ) { @@ -528,21 +487,21 @@ SDB_API SdbKv* sdb_kv_new2(const char *k, int kl, const char *v, int vl) { } kv = R_NEW0 (SdbKv); kv->key_len = kl; - kv->key = malloc (kv->key_len + 1); - if (!kv->key) { + kv->key = malloc (sdbkv_key_len (kv) + 1); + if (!sdbkv_key (kv)) { free (kv); return NULL; } - memcpy (kv->key, k, kv->key_len + 1); + memcpy (sdbkv_key (kv), k, sdbkv_key_len (kv) + 1); kv->value_len = vl; if (vl) { kv->value = malloc (vl + 1); - if (!kv->value) { - free (kv->key); + if (!sdbkv_value (kv)) { + free (sdbkv_key (kv)); free (kv); return NULL; } - memcpy (kv->value, v, vl + 1); + memcpy (sdbkv_value (kv), v, vl + 1); } else { kv->value = NULL; kv->value_len = 0; @@ -552,9 +511,9 @@ SDB_API SdbKv* sdb_kv_new2(const char *k, int kl, const char *v, int vl) { return kv; } -SDB_API void sdb_kv_free(SdbKv *kv) { - free (kv->key); - free (kv->value); +SDB_API void sdbkv_free(SdbKv *kv) { + free (sdbkv_key (kv)); + free (sdbkv_value (kv)); R_FREE (kv); } @@ -586,7 +545,7 @@ static ut32 sdb_set_internal(Sdb* s, const char *key, char *val, int owned, ut32 } cdb_findstart (&s->db); kv = sdb_ht_find_kvp (s->ht, key, &found); - if (found && kv->value) { + if (found && sdbkv_value (kv)) { if (cdb_findnext (&s->db, sdb_hash (key), key, klen)) { if (cas && kv->cas != cas) { if (owned) { @@ -594,21 +553,21 @@ static ut32 sdb_set_internal(Sdb* s, const char *key, char *val, int owned, ut32 } return 0; } - if (vlen == kv->value_len && !strcmp (kv->value, val)) { + if (vlen == sdbkv_value_len (kv) && !strcmp (sdbkv_value (kv), val)) { sdb_hook_call (s, key, val); return kv->cas; } kv->cas = cas = nextcas (); if (owned) { kv->value_len = vlen; - free (kv->value); + free (sdbkv_value (kv)); kv->value = val; // owned } else { - if ((ut32)vlen > kv->value_len) { - free (kv->value); + if ((ut32)vlen > sdbkv_value_len (kv)) { + free (sdbkv_value (kv)); kv->value = malloc (vlen + 1); } - memcpy (kv->value, val, vlen + 1); + memcpy (sdbkv_value (kv), val, vlen + 1); kv->value_len = vlen; } } else { @@ -620,13 +579,13 @@ static ut32 sdb_set_internal(Sdb* s, const char *key, char *val, int owned, ut32 // empty values are also stored // TODO store only the ones that are in the CDB if (owned) { - kv = sdb_kv_new2 (key, klen, NULL, 0); + kv = sdbkv_new2 (key, klen, NULL, 0); if (kv) { kv->value = val; kv->value_len = vlen; } } else { - kv = sdb_kv_new2 (key, klen, val, vlen); + kv = sdbkv_new2 (key, klen, val, vlen); } if (kv) { ut32 cas = kv->cas = nextcas (); @@ -659,11 +618,11 @@ static int sdb_foreach_list_cb(void *user, const char *k, const char *v) { static int __cmp_asc(const void *a, const void *b) { const SdbKv *ka = a; const SdbKv *kb = b; - return strcmp (ka->key, kb->key); + return strcmp (sdbkv_key (ka), sdbkv_key (kb)); } SDB_API SdbList *sdb_foreach_list(Sdb* s, bool sorted) { - SdbList *list = ls_newf ((SdbListFree)sdb_kv_free); + SdbList *list = ls_newf ((SdbListFree)sdbkv_free); sdb_foreach (s, sdb_foreach_list_cb, list); if (sorted) { ls_sort (list, __cmp_asc); @@ -680,7 +639,7 @@ typedef struct { static int sdb_foreach_match_cb(void *user, const char *k, const char *v) { _match_sdb_user *o = (_match_sdb_user*)user; SdbKv tkv = { .key = (char*)k, .value = (char*)v }; - if (sdb_kv_match (&tkv, o->expr)) { + if (sdbkv_match (&tkv, o->expr)) { SdbKv *kv = R_NEW0 (SdbKv); kv->key = strdup (k); kv->value = strdup (v); @@ -693,7 +652,7 @@ static int sdb_foreach_match_cb(void *user, const char *k, const char *v) { } SDB_API SdbList *sdb_foreach_match(Sdb* s, const char *expr, bool single) { - SdbList *list = ls_newf ((SdbListFree)sdb_kv_free); + SdbList *list = ls_newf ((SdbListFree)sdbkv_free); _match_sdb_user o = { expr, list, single }; sdb_foreach (s, sdb_foreach_match_cb, &o); #if 0 @@ -731,12 +690,12 @@ static bool sdb_foreach_cdb(Sdb *s, SdbForeachCallback cb, SdbKv *kv = sdb_ht_find_kvp (s->ht, k, &found); if (found) { free (v); - if (kv && kv->key && kv->value) { - if (!cb (user, kv->key, kv->value)) { + if (kv && sdbkv_key (kv) && sdbkv_value (kv)) { + if (!cb (user, sdbkv_key (kv), sdbkv_value (kv))) { return false; } if (cb2) { - cb2 (user, k, kv->value); + cb2 (user, k, sdbkv_value (kv)); } } } else { @@ -764,10 +723,10 @@ SDB_API bool sdb_foreach(Sdb* s, SdbForeachCallback cb, void *user) { } #if INSERTORDER ls_foreach (s->ht->list, iter, kv) { - if (!kv || !kv->value || !*kv->value) { + if (!kv || !sdbkv_value (kv) || !*sdbkv_value (kv)) { continue; } - if (!cb (user, kv->key, kv->value)) { + if (!cb (user, sdbkv_key (kv), sdbkv_value (kv))) { return sdb_foreach_end (s, false); } } @@ -775,10 +734,10 @@ SDB_API bool sdb_foreach(Sdb* s, SdbForeachCallback cb, void *user) { ut32 i; for (i = 0; i < s->ht->size; i++) { ls_foreach (s->ht->table[i], iter, kv) { - if (!kv || !kv->value || !*kv->value) { + if (!kv || !sdbkv_value (kv) || !*sdbkv_value (kv)) { continue; } - if (!cb (user, kv->key, kv->value)) { + if (!cb (user, sdbkv_key (kv), sdbkv_value (kv))) { return sdb_foreach_end (s, false); } } @@ -821,10 +780,10 @@ SDB_API bool sdb_sync(Sdb* s) { /* append new keyvalues */ for (i = 0; i < s->ht->size; ++i) { ls_foreach (s->ht->table[i], iter, kv) { - if (kv->key && kv->value && *kv->value && !kv->expire) { - if (sdb_disk_insert (s, kv->key, kv->value)) { + if (sdbkv_key (kv) && sdbkv_value (kv) && *sdbkv_value (kv) && !kv->expire) { + if (sdb_disk_insert (s, sdbkv_key (kv), sdbkv_value (kv))) { it.n = iter->n; - sdb_remove (s, kv->key, 0); + sdb_remove (s, sdbkv_key (kv), 0); iter = ⁢ } } @@ -854,9 +813,9 @@ SDB_API SdbKv *sdb_dump_next(Sdb* s) { return NULL; } vl--; - strncpy (s->tmpkv.key, k, SDB_KSZ - 1); + strncpy (sdbkv_key (&s->tmpkv), k, SDB_KSZ - 1); s->tmpkv.key[SDB_KSZ - 1] = '\0'; - free (s->tmpkv.value); + free (sdbkv_value (&s->tmpkv)); s->tmpkv.value = v; s->tmpkv.value_len = vl; return &s->tmpkv; @@ -960,7 +919,7 @@ SDB_API bool sdb_expire_set(Sdb* s, const char *key, ut64 expire, ut32 cas) { } kv = (SdbKv*)sdb_ht_find_kvp (s->ht, key, &found); if (found && kv) { - if (*kv->value) { + if (*sdbkv_value (kv)) { if (!cas || cas == kv->cas) { kv->expire = parse_expire (expire); return true; @@ -992,7 +951,7 @@ SDB_API bool sdb_expire_set(Sdb* s, const char *key, ut64 expire, ut32 cas) { SDB_API ut64 sdb_expire_get(Sdb* s, const char *key, ut32 *cas) { bool found = false; SdbKv *kv = (SdbKv*)sdb_ht_find_kvp (s->ht, key, &found); - if (found && kv && *kv->value) { + if (found && kv && *sdbkv_value (kv)) { if (cas) { *cas = kv->cas; } diff --git a/shlr/sdb/src/sdb.h b/shlr/sdb/src/sdb.h index db96f60592..ea7ad58e11 100644 --- a/shlr/sdb/src/sdb.h +++ b/shlr/sdb/src/sdb.h @@ -163,7 +163,7 @@ int sdb_concat(Sdb *s, const char *key, const char *value, ut32 cas); int sdb_uncat(Sdb *s, const char *key, const char *value, ut32 cas); int sdb_add(Sdb* s, const char *key, const char *val, ut32 cas); bool sdb_sync(Sdb*); -void sdb_kv_free(SdbKv *kv); +void sdbkv_free(SdbKv *kv); /* num.c */ int sdb_num_exists(Sdb*, const char *key); diff --git a/shlr/sdb/src/sdbht.c b/shlr/sdb/src/sdbht.c index 2cef50974f..e43c765461 100644 --- a/shlr/sdb/src/sdbht.c +++ b/shlr/sdb/src/sdbht.c @@ -1,7 +1,7 @@ #include "sdbht.h" SDB_API SdbHt* sdb_ht_new() { - return ht_new ((DupValue)strdup, (HtKvFreeFunc)sdb_kv_free, (CalcSize)strlen); + return ht_new ((DupValue)strdup, (HtKvFreeFunc)sdbkv_free, (CalcSize)strlen); } static bool sdb_ht_internal_insert(SdbHt* ht, const char* key, @@ -13,9 +13,9 @@ static bool sdb_ht_internal_insert(SdbHt* ht, const char* key, if (kvp) { kvp->key = strdup ((void *)key); kvp->value = strdup ((void *)value); - kvp->key_len = strlen ((void *)kvp->key); + kvp->key_len = strlen (sdbkv_key (kvp)); + kvp->value_len = strlen (sdbkv_value (kvp)); kvp->expire = 0; - kvp->value_len = strlen ((void *)kvp->value); return ht_insert_kv (ht, (HtKv*)kvp, update); } return false; diff --git a/shlr/sdb/src/sdbht.h b/shlr/sdb/src/sdbht.h index ccc1374dc2..b1383315f5 100644 --- a/shlr/sdb/src/sdbht.h +++ b/shlr/sdb/src/sdbht.h @@ -14,10 +14,27 @@ typedef struct sdb_kv { ut64 expire; } SdbKv; -SDB_API SdbKv* sdb_kv_new2(const char *k, int kl, const char *v, int vl); -extern SdbKv* sdb_kv_new(const char *k, const char *v); +static inline char *sdbkv_key(const SdbKv *kv) { + return kv->key; +} + +static inline char *sdbkv_value(const SdbKv *kv) { + return kv->value; +} + +static inline ut32 sdbkv_key_len(const SdbKv *kv) { + return kv->key_len; +} + +static inline ut32 sdbkv_value_len(const SdbKv *kv) { + return kv->value_len; +} + +SDB_API SdbKv* sdbkv_new2(const char *k, int kl, const char *v, int vl); +extern SdbKv* sdbkv_new(const char *k, const char *v); +extern void sdbkv_free(SdbKv *kv); + extern ut32 sdb_hash(const char *key); -extern void sdb_kv_free(SdbKv *kv); SDB_API SdbHt* sdb_ht_new(void); // Destroy a hashtable and all of its entries.