Rizin
unix-like reverse engineering framework and cli tools
sdb.c File Reference
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "sdb.h"
#include "sdb_private.h"

Go to the source code of this file.

Classes

struct  foreach_list_filter_t
 
struct  _match_sdb_user
 
struct  UnsetCallbackData
 
struct  LikeCallbackData
 

Macros

#define BUCKET_FOREACH(ht, bt, j, kv)    for ((j) = 0, (kv) = (SdbKv *)(bt)->arr; j < (bt)->count; (j)++, (kv) = next_kv(ht, kv))
 
#define BUCKET_FOREACH_SAFE(ht, bt, j, count, kv)
 

Functions

static SdbKvnext_kv (HtPP *ht, SdbKv *kv)
 
static int nextcas (void)
 
RZ_API void sdb_global_hook (SdbHook hook, void *user)
 
RZ_API Sdbsdb_new0 (void)
 
RZ_API Sdbsdb_new (const char *path, const char *name, int lock)
 
RZ_API void sdb_file (Sdb *s, const char *dir)
 
static bool sdb_merge_cb (void *user, const char *k, const char *v)
 
RZ_API bool sdb_merge (Sdb *d, Sdb *s)
 
RZ_API bool sdb_isempty (Sdb *s)
 
RZ_API int sdb_count (Sdb *s)
 
static void sdb_fini (Sdb *s, int donull)
 
RZ_API bool sdb_free (Sdb *s)
 
RZ_API const char * sdb_const_get_len (Sdb *s, const char *key, int *vlen, ut32 *cas)
 
RZ_API const char * sdb_const_get (Sdb *s, const char *key, ut32 *cas)
 
RZ_API char * sdb_get_len (Sdb *s, const char *key, int *vlen, ut32 *cas)
 
RZ_API char * sdb_get (Sdb *s, const char *key, ut32 *cas)
 
RZ_API int sdb_unset (Sdb *s, const char *key, ut32 cas)
 
RZ_API bool sdb_remove (Sdb *s, const char *key, ut32 cas)
 
RZ_API int sdb_uncat (Sdb *s, const char *key, const char *value, ut32 cas)
 
RZ_API int sdb_concat (Sdb *s, const char *key, const char *value, ut32 cas)
 
RZ_API int sdb_add (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API bool sdb_exists (Sdb *s, const char *key)
 
RZ_API int sdb_open (Sdb *s, const char *file)
 
RZ_API void sdb_close (Sdb *s)
 
RZ_API void sdb_reset (Sdb *s)
 
static char lastChar (const char *str)
 
static bool match (const char *str, const char *expr)
 
RZ_API bool sdbkv_match (SdbKv *kv, const char *expr)
 
RZ_API SdbKvsdbkv_new (const char *k, const char *v)
 
RZ_API SdbKvsdbkv_new2 (const char *k, int kl, const char *v, int vl)
 
RZ_API void sdbkv_free (SdbKv *kv)
 
static ut32 sdb_set_internal (Sdb *s, const char *key, char *val, int owned, ut32 cas)
 
RZ_API int sdb_set_owned (Sdb *s, const char *key, char *val, ut32 cas)
 
RZ_API int sdb_set (Sdb *s, const char *key, const char *val, ut32 cas)
 
static bool sdb_foreach_list_cb (void *user, const char *k, const char *v)
 
static int __cmp_asc (const void *a, const void *b)
 
RZ_API SdbListsdb_foreach_list (Sdb *s, bool sorted)
 
static bool sdb_foreach_list_filter_cb (void *user, const char *k, const char *v)
 
RZ_API SdbListsdb_foreach_list_filter_user (Sdb *s, SdbForeachCallback filter, bool sorted, void *user)
 
RZ_API SdbListsdb_foreach_list_filter (Sdb *s, SdbForeachCallback filter, bool sorted)
 
static bool sdb_foreach_match_cb (void *user, const char *k, const char *v)
 
RZ_API SdbListsdb_foreach_match (Sdb *s, const char *expr, bool single)
 
static int getbytes (Sdb *s, char *b, int len)
 
static bool sdb_foreach_end (Sdb *s, bool result)
 
static bool sdb_foreach_cdb (Sdb *s, SdbForeachCallback cb, SdbForeachCallback cb2, void *user)
 
RZ_API bool sdb_foreach (Sdb *s, SdbForeachCallback cb, void *user)
 
static bool _insert_into_disk (void *user, const char *key, const char *value)
 
static bool _remove_afer_insert (void *user, const char *k, const char *v)
 
RZ_API bool sdb_sync (Sdb *s)
 
RZ_API void sdb_dump_begin (Sdb *s)
 
RZ_API bool sdb_dump_hasnext (Sdb *s)
 
RZ_API bool sdb_stats (Sdb *s, ut32 *disk, ut32 *mem)
 
RZ_API bool sdb_dump_dupnext (Sdb *s, char *key, char **value, int *_vlen)
 
static ut64 parse_expire (ut64 e)
 
RZ_API bool sdb_expire_set (Sdb *s, const char *key, ut64 expire, ut32 cas)
 
RZ_API ut64 sdb_expire_get (Sdb *s, const char *key, ut32 *cas)
 
RZ_API bool sdb_hook (Sdb *s, SdbHook cb, void *user)
 
RZ_API bool sdb_unhook (Sdb *s, SdbHook h)
 
RZ_API int sdb_hook_call (Sdb *s, const char *k, const char *v)
 
RZ_API void sdb_hook_free (Sdb *s)
 
RZ_API void sdb_config (Sdb *s, int options)
 
RZ_API bool sdb_unlink (Sdb *s)
 
RZ_API void sdb_drain (Sdb *s, Sdb *f)
 
static bool copy_foreach_cb (void *user, const char *k, const char *v)
 
RZ_API void sdb_copy (Sdb *src, Sdb *dst)
 
static bool unset_cb (void *user, const char *k, const char *v)
 
RZ_API int sdb_unset_like (Sdb *s, const char *k)
 
static bool like_cb (void *user, const char *k, const char *v)
 
RZ_API char ** sdb_like (Sdb *s, const char *k, const char *v, SdbForeachCallback cb)
 

Variables

static SdbHook global_hook = NULL
 
static void * global_user = NULL
 

Macro Definition Documentation

◆ BUCKET_FOREACH

#define BUCKET_FOREACH (   ht,
  bt,
  j,
  kv 
)     for ((j) = 0, (kv) = (SdbKv *)(bt)->arr; j < (bt)->count; (j)++, (kv) = next_kv(ht, kv))

Definition at line 17 of file sdb.c.

◆ BUCKET_FOREACH_SAFE

#define BUCKET_FOREACH_SAFE (   ht,
  bt,
  j,
  count,
  kv 
)
Value:
if ((bt)->arr) \
for ((j) = 0, (kv) = (SdbKv *)(bt)->arr, (count) = (ht)->count; \
(j) < (bt)->count; \
(j) = (count) == (ht)->count ? j + 1 : j, (kv) = (count) == (ht)->count ? next_kv(ht, kv) : kv, (count) = (ht)->count)
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
static SdbKv * next_kv(HtPP *ht, SdbKv *kv)
Definition: sdb.c:13
Definition: sdbht.h:14

Definition at line 20 of file sdb.c.

Function Documentation

◆ __cmp_asc()

static int __cmp_asc ( const void *  a,
const void *  b 
)
static

Definition at line 625 of file sdb.c.

625  {
626  const SdbKv *ka = a, *kb = b;
627  return strcmp(sdbkv_key(ka), sdbkv_key(kb));
628 }
static char * sdbkv_key(const SdbKv *kv)
Definition: sdbht.h:21
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, b, and sdbkv_key().

Referenced by sdb_foreach_list(), and sdb_foreach_list_filter_user().

◆ _insert_into_disk()

static bool _insert_into_disk ( void *  user,
const char *  key,
const char *  value 
)
static

Definition at line 785 of file sdb.c.

785  {
786  Sdb *s = (Sdb *)user;
787  if (s) {
789  return true;
790  }
791  return false;
792 }
static int value
Definition: cmd_api.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
RZ_API bool sdb_disk_insert(Sdb *s, const char *key, const char *val)
Definition: disk.c:67
static RzSocket * s
Definition: rtr.c:28
Definition: sdb.h:63

References key, s, sdb_disk_insert(), and value.

Referenced by sdb_sync().

◆ _remove_afer_insert()

static bool _remove_afer_insert ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 794 of file sdb.c.

794  {
795  Sdb *s = (Sdb *)user;
796  if (s) {
797  sdb_ht_delete(s->ht, k);
798  return true;
799  }
800  return false;
801 }
const char * k
Definition: dsignal.c:11
RZ_API bool sdb_ht_delete(HtPP *ht, const char *key)
Definition: sdbht.c:67

References k, s, and sdb_ht_delete().

Referenced by sdb_sync().

◆ copy_foreach_cb()

static bool copy_foreach_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 1078 of file sdb.c.

1078  {
1079  Sdb *dst = user;
1080  sdb_set(dst, k, v, 0);
1081  return true;
1082 }
const char * v
Definition: dsignal.c:12
char * dst
Definition: lz4.h:724
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611

References dst, k, sdb_set(), and v.

Referenced by sdb_copy().

◆ getbytes()

static int getbytes ( Sdb s,
char *  b,
int  len 
)
static

Definition at line 717 of file sdb.c.

717  {
718  if (!cdb_read(&s->db, b, len, s->pos)) {
719  return -1;
720  }
721  s->pos += len;
722  return len;
723 }
size_t len
Definition: 6502dis.c:15
bool cdb_read(struct cdb *c, char *buf, ut32 len, ut32 pos)
Definition: cdb.c:89

References b, cdb_read(), len, and s.

Referenced by sdb_dump_dupnext().

◆ lastChar()

static char lastChar ( const char *  str)
static

Definition at line 445 of file sdb.c.

445  {
446  int len = strlen(str);
447  return str[(len > 0) ? len - 1 : 0];
448 }

References len, and cmd_descs_generate::str.

Referenced by match(), and RDG_fillLiteralDistrib().

◆ like_cb()

static bool like_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 1121 of file sdb.c.

1121  {
1122  LikeCallbackData *lcd = user;
1123  if (!user) {
1124  return false;
1125  }
1126  if (k && lcd->key && !sdb_match(k, lcd->key)) {
1127  return true;
1128  }
1129  if (v && lcd->val && !sdb_match(v, lcd->val)) {
1130  return true;
1131  }
1132  if (lcd->array) {
1133  int idx = lcd->array_index;
1134  int newsize = lcd->array_size + sizeof(char *) * 2;
1135  const char **newarray = (const char **)realloc((void *)lcd->array, newsize);
1136  if (!newarray) {
1137  return false;
1138  }
1139  lcd->array = newarray;
1140  lcd->array_size = newsize;
1141  // concatenate in array
1142  lcd->array[idx] = k;
1143  lcd->array[idx + 1] = v;
1144  lcd->array[idx + 2] = NULL;
1145  lcd->array[idx + 3] = NULL;
1146  lcd->array_index = idx + 2;
1147  } else {
1148  if (lcd->cb) {
1149  lcd->cb(lcd->sdb, k, v);
1150  }
1151  }
1152  return true;
1153 }
#define NULL
Definition: cris-opc.c:27
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
RZ_API bool sdb_match(const char *str, const char *glob)
Definition: match.c:95
int idx
Definition: setup.py:197
Sdb * sdb
Definition: sdb.c:1112
int array_size
Definition: sdb.c:1118
const char * val
Definition: sdb.c:1114
SdbForeachCallback cb
Definition: sdb.c:1115
int array_index
Definition: sdb.c:1117
const char ** array
Definition: sdb.c:1116
const char * key
Definition: sdb.c:1113

References LikeCallbackData::array, LikeCallbackData::array_index, LikeCallbackData::array_size, LikeCallbackData::cb, setup::idx, k, LikeCallbackData::key, NULL, realloc(), LikeCallbackData::sdb, sdb_match(), v, and LikeCallbackData::val.

Referenced by sdb_like().

◆ match()

static bool match ( const char *  str,
const char *  expr 
)
static

Definition at line 450 of file sdb.c.

450  {
451  bool startsWith = *expr == '^';
452  bool endsWith = lastChar(expr) == '$';
453  if (startsWith && endsWith) {
454  return strlen(str) == strlen(expr) - 2 &&
455  !strncmp(str, expr + 1, strlen(expr) - 2);
456  }
457  if (startsWith) {
458  return !strncmp(str, expr + 1, strlen(expr) - 1);
459  }
460  if (endsWith) {
461  int alen = strlen(str);
462  int blen = strlen(expr) - 1;
463  if (alen <= blen) {
464  return false;
465  }
466  const char *a = str + strlen(str) - blen;
467  return (!strncmp(a, expr, blen));
468  }
469  return strstr(str, expr);
470 }
static RzNumCalcValue expr(RzNum *, RzNumCalc *, int)
Definition: calc.c:167
static char lastChar(const char *str)
Definition: sdb.c:445

References a, expr(), lastChar(), and cmd_descs_generate::str.

Referenced by sdbkv_match().

◆ next_kv()

static SdbKv* next_kv ( HtPP *  ht,
SdbKv kv 
)
inlinestatic

Definition at line 13 of file sdb.c.

13  {
14  return (SdbKv *)((char *)kv + ht->opt.elem_size);
15 }

Referenced by delete(), and update_key().

◆ nextcas()

static int nextcas ( void  )
inlinestatic

Definition at line 26 of file sdb.c.

26  {
27  static ut32 cas = 1;
28  if (!cas) {
29  cas++;
30  }
31  return cas++;
32 }
uint32_t ut32

Referenced by sdb_set_internal(), and sdbkv_new2().

◆ parse_expire()

static ut64 parse_expire ( ut64  e)
inlinestatic

Definition at line 922 of file sdb.c.

922  {
923  const ut64 month = 30 * 24 * 60 * 60;
924  if (e > 0 && e < month) {
925  e += sdb_now();
926  }
927  return e;
928 }
#define e(frag)
RZ_API ut64 sdb_now(void)
Definition: util.c:211
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References e, sdb_now(), and ut64().

Referenced by sdb_expire_set().

◆ sdb_add()

RZ_API int sdb_add ( Sdb s,
const char *  key,
const char *  val,
ut32  cas 
)

Definition at line 351 of file sdb.c.

351  {
352  if (sdb_exists(s, key)) {
353  return 0;
354  }
355  return sdb_set(s, key, val, cas);
356 }
ut16 val
Definition: armass64_const.h:6
RZ_API bool sdb_exists(Sdb *s, const char *key)
Definition: sdb.c:358

References key, s, sdb_exists(), sdb_set(), and val.

Referenced by sdb_num_add().

◆ sdb_close()

RZ_API void sdb_close ( Sdb s)

Definition at line 416 of file sdb.c.

416  {
417  if (s) {
418  if (s->fd != -1) {
419  if (s->db.fd != -1 && s->db.fd == s->fd) {
420  /* close db fd as well */
421  s->db.fd = -1;
422  }
423  close(s->fd);
424  s->fd = -1;
425  }
426  if (s->dir) {
427  free(s->dir);
428  s->dir = NULL;
429  }
430  }
431 }
static static fork const void static count close
Definition: sflib.h:33
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References close, rz_socket_t::fd, free(), NULL, and s.

Referenced by __func_name_from_ord(), rz_analysis_function_cc_load_handler(), sdb_concat_by_path(), sdb_load_arch_platform_by_path(), sdb_load_arch_profile_by_path(), sdb_load_by_path(), sdb_load_from_string(), sdb_reset(), and session_sdb_save().

◆ sdb_concat()

RZ_API int sdb_concat ( Sdb s,
const char *  key,
const char *  value,
ut32  cas 
)

Definition at line 329 of file sdb.c.

329  {
330  int kl, vl;
331  const char *p;
332  char *o;
333  if (!s || !key || !*key || !value || !*value) {
334  return 0;
335  }
336  p = sdb_const_get_len(s, key, &kl, 0);
337  if (!p) {
338  return sdb_set(s, key, value, cas);
339  }
340  vl = strlen(value);
341  o = malloc(kl + vl + 1);
342  if (o) {
343  memcpy(o, p, kl);
344  memcpy(o + kl, value, vl + 1);
345  return sdb_set_owned(s, key, o, cas);
346  }
347  return 0;
348 }
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API int sdb_set_owned(Sdb *s, const char *key, char *val, ut32 cas)
Definition: sdb.c:607
RZ_API const char * sdb_const_get_len(Sdb *s, const char *key, int *vlen, ut32 *cas)
Definition: sdb.c:220

References key, malloc(), memcpy(), p, s, sdb_const_get_len(), sdb_set(), sdb_set_owned(), and value.

Referenced by rz_core_search_rop(), and sdb_querys().

◆ sdb_config()

RZ_API void sdb_config ( Sdb s,
int  options 
)

Definition at line 1041 of file sdb.c.

1041  {
1042  s->options = options;
1043  if (options & SDB_OPTION_SYNC) {
1044  // sync on every query
1045  }
1046  if (options & SDB_OPTION_JOURNAL) {
1047  // sync on every query
1049  // load journaling if exists
1052  } else {
1054  }
1055  if (options & SDB_OPTION_NOSTAMP) {
1056  // sync on every query
1057  s->last = 0LL;
1058  }
1059  if (options & SDB_OPTION_FS) {
1060  // have access to fs (handle '.' or not in query)
1061  }
1062 }
RZ_API bool sdb_journal_open(Sdb *s)
Definition: journal.c:23
RZ_API int sdb_journal_load(Sdb *s)
Definition: journal.c:38
RZ_API bool sdb_journal_clear(Sdb *s)
Definition: journal.c:96
RZ_API bool sdb_journal_close(Sdb *s)
Definition: journal.c:13
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#define SDB_OPTION_SYNC
Definition: sdb.h:55
#define SDB_OPTION_FS
Definition: sdb.h:57
#define SDB_OPTION_JOURNAL
Definition: sdb.h:58
#define SDB_OPTION_NOSTAMP
Definition: sdb.h:56

References options, s, sdb_journal_clear(), sdb_journal_close(), sdb_journal_load(), sdb_journal_open(), SDB_OPTION_FS, SDB_OPTION_JOURNAL, SDB_OPTION_NOSTAMP, and SDB_OPTION_SYNC.

Referenced by createdb(), main(), and sdb_grep_dump().

◆ sdb_const_get()

◆ sdb_const_get_len()

RZ_API const char* sdb_const_get_len ( Sdb s,
const char *  key,
int vlen,
ut32 cas 
)

Definition at line 220 of file sdb.c.

220  {
221  ut32 pos, len;
222  ut64 now = 0LL;
223  bool found;
224 
225  if (cas) {
226  *cas = 0;
227  }
228  if (vlen) {
229  *vlen = 0;
230  }
231  if (!s || !key) {
232  return NULL;
233  }
234  // TODO: optimize, iterate once
235  size_t keylen = strlen(key);
236 
237  /* search in memory */
238  SdbKv *kv = (SdbKv *)sdb_ht_find_kvp(s->ht, key, &found);
239  if (found) {
240  if (!sdbkv_value(kv) || !*sdbkv_value(kv)) {
241  return NULL;
242  }
243  if (s->timestamped && kv->expire) {
244  if (!now) {
245  now = sdb_now();
246  }
247  if (now > kv->expire) {
248  sdb_unset(s, key, 0);
249  return NULL;
250  }
251  }
252  if (cas) {
253  *cas = kv->cas;
254  }
255  if (vlen) {
256  *vlen = sdbkv_value_len(kv);
257  }
258  return sdbkv_value(kv);
259  }
260  /* search in disk */
261  if (s->fd == -1) {
262  return NULL;
263  }
264  (void)cdb_findstart(&s->db);
265  if (cdb_findnext(&s->db, s->ht->opt.hashfn(key), key, keylen) < 1) {
266  return NULL;
267  }
268  len = cdb_datalen(&s->db);
269  if (len < SDB_CDB_MIN_VALUE || len >= SDB_CDB_MAX_VALUE) {
270  return NULL;
271  }
272  if (vlen) {
273  *vlen = len;
274  }
275  pos = cdb_datapos(&s->db);
276  return s->db.map + pos;
277 }
void cdb_findstart(struct cdb *c)
Definition: cdb.c:42
int cdb_findnext(struct cdb *c, ut32 u, const char *key, ut32 len)
Definition: cdb.c:132
#define cdb_datalen(c)
Definition: cdb.h:42
#define cdb_datapos(c)
Definition: cdb.h:41
RZ_API const KEY_TYPE bool * found
Definition: ht_inc.h:130
RZ_API int sdb_unset(Sdb *s, const char *key, ut32 cas)
Definition: sdb.c:294
#define SDB_CDB_MAX_VALUE
Definition: sdb.h:19
RZ_API SdbKv * sdb_ht_find_kvp(HtPP *ht, const char *key, bool *found)
Definition: sdbht.c:55
static ut32 sdbkv_value_len(const SdbKv *kv)
Definition: sdbht.h:33
static char * sdbkv_value(const SdbKv *kv)
Definition: sdbht.h:25
ut64 expire
Definition: sdbht.h:18
ut32 cas
Definition: sdbht.h:17
int pos
Definition: main.c:11

References sdb_kv::cas, cdb_datalen, cdb_datapos, cdb_findnext(), cdb_findstart(), sdb_kv::expire, rz_socket_t::fd, found, key, len, NULL, pos, s, SDB_CDB_MAX_VALUE, sdb_ht_find_kvp(), sdb_now(), sdb_unset(), sdbkv_value(), sdbkv_value_len(), and ut64().

Referenced by sdb_array_add_sorted(), sdb_array_append(), sdb_array_insert(), sdb_array_prepend(), sdb_array_set(), sdb_concat(), sdb_const_get(), and sdb_get_len().

◆ sdb_copy()

RZ_API void sdb_copy ( Sdb src,
Sdb dst 
)

Definition at line 1084 of file sdb.c.

1084  {
1086  SdbListIter *it;
1087  SdbNs *ns;
1088  ls_foreach (src->ns, it, ns) {
1089  sdb_copy(ns->sdb, sdb_ns(dst, ns->name, true));
1090  }
1091 }
lzma_index * src
Definition: index.h:567
#define ls_foreach(list, it, pos)
Definition: ls.h:31
RZ_API Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186
static bool copy_foreach_cb(void *user, const char *k, const char *v)
Definition: sdb.c:1078
RZ_API void sdb_copy(Sdb *src, Sdb *dst)
Definition: sdb.c:1084
RZ_API bool sdb_foreach(Sdb *s, SdbForeachCallback cb, void *user)
Definition: sdb.c:758
Definition: ls.h:17
Definition: sdb.h:88
char * name
Definition: sdb.h:89
Sdb * sdb
Definition: sdb.h:91

References copy_foreach_cb(), dst, ls_foreach, sdb_ns_t::name, sdb_ns_t::sdb, sdb_foreach(), sdb_ns(), and src.

Referenced by rz_serialize_analysis_cc_load(), rz_serialize_analysis_cc_save(), rz_serialize_analysis_classes_load(), rz_serialize_analysis_classes_save(), rz_serialize_analysis_function_noreturn_load(), rz_serialize_analysis_function_noreturn_save(), rz_serialize_flag_load(), rz_serialize_flag_save(), and session_sdb_load_ns().

◆ sdb_count()

RZ_API int sdb_count ( Sdb s)

Definition at line 163 of file sdb.c.

163  {
164  int count = 0;
165  if (s) {
166  if (s->db.fd != -1) {
167  sdb_dump_begin(s);
168  while (sdb_dump_hasnext(s)) {
169  count++;
170  }
171  }
172  if (s->ht) {
173  count += s->ht->count;
174  }
175  }
176  return count;
177 }
RZ_API bool sdb_dump_hasnext(Sdb *s)
Definition: sdb.c:844
RZ_API void sdb_dump_begin(Sdb *s)
Definition: sdb.c:835

References count, rz_socket_t::fd, s, sdb_dump_begin(), and sdb_dump_hasnext().

Referenced by __update_modal(), and resolve_syscalls().

◆ sdb_drain()

RZ_API void sdb_drain ( Sdb s,
Sdb f 
)

Definition at line 1069 of file sdb.c.

1069  {
1070  if (s && f) {
1071  f->refs = s->refs;
1072  sdb_fini(s, 1);
1073  *s = *f;
1074  free(f);
1075  }
1076 }
static void sdb_fini(Sdb *s, int donull)
Definition: sdb.c:179
#define f(i)
Definition: sha256.c:46

References f, free(), s, and sdb_fini().

Referenced by rz_cmd_kuery().

◆ sdb_dump_begin()

RZ_API void sdb_dump_begin ( Sdb s)

Definition at line 835 of file sdb.c.

835  {
836  if (s->fd != -1) {
837  s->pos = sizeof(((struct cdb_make *)0)->final);
838  seek_set(s->fd, s->pos);
839  } else {
840  s->pos = 0;
841  }
842 }
static int seek_set(int fd, off_t pos)
Definition: sdb_private.h:21

References rz_socket_t::fd, s, and seek_set().

Referenced by sdb_count(), sdb_foreach_cdb(), sdb_grep_dump(), sdb_isempty(), and sdb_stats().

◆ sdb_dump_dupnext()

RZ_API bool sdb_dump_dupnext ( Sdb s,
char *  key,
char **  value,
int _vlen 
)

Definition at line 877 of file sdb.c.

877  {
878  ut32 vlen = 0, klen = 0;
879  if (value) {
880  *value = NULL;
881  }
882  if (_vlen) {
883  *_vlen = 0;
884  }
885  if (!cdb_getkvlen(&s->db, &klen, &vlen, s->pos)) {
886  return false;
887  }
888  s->pos += 4;
889  if (klen < 1 || vlen < 1) {
890  return false;
891  }
892  if (_vlen) {
893  *_vlen = vlen;
894  }
895  if (key) {
896  key[0] = 0;
897  if (klen > SDB_CDB_MIN_KEY && klen < SDB_CDB_MAX_KEY) {
898  if (getbytes(s, key, klen) == -1) {
899  return 0;
900  }
901  key[klen] = 0;
902  }
903  }
904  if (value) {
905  *value = 0;
906  if (vlen < SDB_CDB_MAX_VALUE) {
907  *value = malloc(vlen + 10);
908  if (!*value) {
909  return false;
910  }
911  if (getbytes(s, *value, vlen) == -1) {
912  free(*value);
913  *value = NULL;
914  return false;
915  }
916  (*value)[vlen] = 0;
917  }
918  }
919  return true;
920 }
bool cdb_getkvlen(struct cdb *c, ut32 *klen, ut32 *vlen, ut32 pos)
Definition: cdb.c:15
static int getbytes(Sdb *s, char *b, int len)
Definition: sdb.c:717
#define SDB_CDB_MIN_KEY
Definition: sdb.h:20
#define SDB_CDB_MAX_KEY
Definition: sdb.h:21

References cdb_getkvlen(), free(), getbytes(), key, malloc(), NULL, s, SDB_CDB_MAX_KEY, SDB_CDB_MAX_VALUE, SDB_CDB_MIN_KEY, and value.

Referenced by sdb_foreach_cdb(), and sdb_grep_dump().

◆ sdb_dump_hasnext()

RZ_API bool sdb_dump_hasnext ( Sdb s)

Definition at line 844 of file sdb.c.

844  {
845  ut32 k, v;
846  if (!cdb_getkvlen(&s->db, &k, &v, s->pos)) {
847  return false;
848  }
849  if (k < 1 || v < 1) {
850  return false;
851  }
852  s->pos += k + v + 4;
853  return true;
854 }

References cdb_getkvlen(), k, s, and v.

Referenced by sdb_count(), sdb_isempty(), and sdb_stats().

◆ sdb_exists()

RZ_API bool sdb_exists ( Sdb s,
const char *  key 
)

Definition at line 358 of file sdb.c.

358  {
359  ut32 pos;
360  char ch;
361  bool found;
362  int klen = strlen(key) + 1;
363  if (!s) {
364  return false;
365  }
366  SdbKv *kv = (SdbKv *)sdb_ht_find_kvp(s->ht, key, &found);
367  if (found && kv) {
368  char *v = sdbkv_value(kv);
369  return v && *v;
370  }
371  if (s->fd == -1) {
372  return false;
373  }
374  (void)cdb_findstart(&s->db);
375  if (cdb_findnext(&s->db, sdb_hash(key), key, klen)) {
376  pos = cdb_datapos(&s->db);
377  cdb_read(&s->db, &ch, 1, pos);
378  return ch != 0;
379  }
380  return false;
381 }
RZ_API ut32 sdb_hash(const char *key)
Definition: util.c:22

References cdb_datapos, cdb_findnext(), cdb_findstart(), cdb_read(), rz_socket_t::fd, found, key, pos, s, sdb_hash(), sdb_ht_find_kvp(), sdbkv_value(), and v.

Referenced by rz_analysis_class_create(), rz_analysis_class_delete_attr_raw(), rz_analysis_class_exists_raw(), and sdb_add().

◆ sdb_expire_get()

RZ_API ut64 sdb_expire_get ( Sdb s,
const char *  key,
ut32 cas 
)

Definition at line 971 of file sdb.c.

971  {
972  bool found = false;
973  SdbKv *kv = (SdbKv *)sdb_ht_find_kvp(s->ht, key, &found);
974  if (found && kv && *sdbkv_value(kv)) {
975  if (cas) {
976  *cas = kv->cas;
977  }
978  return kv->expire;
979  }
980  return 0LL;
981 }

References sdb_kv::cas, sdb_kv::expire, found, key, s, sdb_ht_find_kvp(), and sdbkv_value().

◆ sdb_expire_set()

RZ_API bool sdb_expire_set ( Sdb s,
const char *  key,
ut64  expire,
ut32  cas 
)

Definition at line 930 of file sdb.c.

930  {
931  char *buf;
932  ut32 pos, len;
933  SdbKv *kv;
934  bool found;
935  s->timestamped = true;
936  if (!key) {
937  s->expire = parse_expire(expire);
938  return true;
939  }
940  kv = (SdbKv *)sdb_ht_find_kvp(s->ht, key, &found);
941  if (found && kv) {
942  if (*sdbkv_value(kv)) {
943  if (!cas || cas == kv->cas) {
944  kv->expire = parse_expire(expire);
945  return true;
946  }
947  }
948  return false;
949  }
950  if (s->fd == -1) {
951  return false;
952  }
953  (void)cdb_findstart(&s->db);
954  if (!cdb_findnext(&s->db, sdb_hash(key), key, strlen(key) + 1)) {
955  return false;
956  }
957  pos = cdb_datapos(&s->db);
958  len = cdb_datalen(&s->db);
959  if (len < 1 || len >= INT32_MAX) {
960  return false;
961  }
962  if (!(buf = calloc(1, len + 1))) {
963  return false;
964  }
965  cdb_read(&s->db, buf, len, pos);
966  buf[len] = 0;
967  sdb_set_owned(s, key, buf, cas);
968  return sdb_expire_set(s, key, expire, cas); // recursive
969 }
voidpf void * buf
Definition: ioapi.h:138
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API bool sdb_expire_set(Sdb *s, const char *key, ut64 expire, ut32 cas)
Definition: sdb.c:930
static ut64 parse_expire(ut64 e)
Definition: sdb.c:922
#define INT32_MAX

References calloc(), sdb_kv::cas, cdb_datalen, cdb_datapos, cdb_findnext(), cdb_findstart(), cdb_read(), sdb_kv::expire, rz_socket_t::fd, found, INT32_MAX, key, len, parse_expire(), pos, s, sdb_hash(), sdb_ht_find_kvp(), sdb_set_owned(), and sdbkv_value().

◆ sdb_file()

RZ_API void sdb_file ( Sdb s,
const char *  dir 
)

Definition at line 128 of file sdb.c.

128  {
129  if (s->lock) {
130  sdb_unlock(sdb_lock_file(s->dir));
131  }
132  free(s->dir);
133  s->dir = (dir && *dir) ? strdup(dir) : NULL;
134  if (s->lock) {
135  sdb_lock(sdb_lock_file(s->dir));
136  }
137 }
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
RZ_API void sdb_unlock(const char *s)
Definition: lock.c:60
RZ_API bool sdb_lock(const char *s)
Definition: lock.c:29
RZ_API const char * sdb_lock_file(const char *f)
Definition: lock.c:14

References free(), NULL, s, sdb_lock(), sdb_lock_file(), sdb_unlock(), and strdup().

Referenced by rz_cmd_kuery(), and session_sdb_save().

◆ sdb_fini()

static void sdb_fini ( Sdb s,
int  donull 
)
static

Definition at line 179 of file sdb.c.

179  {
180  if (!s) {
181  return;
182  }
183  sdb_hook_free(s);
184  cdb_free(&s->db);
185  if (s->lock) {
186  sdb_unlock(sdb_lock_file(s->dir));
187  }
189  s->refs = 0;
190  free(s->name);
191  free(s->path);
192  ls_free(s->ns);
193  sdb_ht_free(s->ht);
195  if (s->fd != -1) {
196  close(s->fd);
197  s->fd = -1;
198  }
199  free(s->ndump);
200  free(s->dir);
201  if (donull) {
202  memset(s, 0, sizeof(Sdb));
203  }
204 }
void cdb_free(struct cdb *c)
Definition: cdb.c:30
return memset(p, 0, total)
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
RZ_API void sdb_ns_free_all(Sdb *s)
Definition: ns.c:74
RZ_API void sdb_hook_free(Sdb *s)
Definition: sdb.c:1036
RZ_API void sdb_ht_free(HtPP *ht)
Definition: sdbht.c:63

References cdb_free(), close, rz_socket_t::fd, free(), ls_free(), memset(), s, sdb_hook_free(), sdb_ht_free(), sdb_journal_close(), sdb_lock_file(), sdb_ns_free_all(), and sdb_unlock().

Referenced by sdb_drain(), sdb_free(), and sdb_unlink().

◆ sdb_foreach()

RZ_API bool sdb_foreach ( Sdb s,
SdbForeachCallback  cb,
void *  user 
)

Definition at line 758 of file sdb.c.

758  {
759  if (!s) {
760  return false;
761  }
762  s->depth++;
763  bool result = sdb_foreach_cdb(s, cb, NULL, user);
764  if (!result) {
765  return sdb_foreach_end(s, false);
766  }
767 
768  ut32 i;
769  for (i = 0; i < s->ht->size; ++i) {
770  HtPPBucket *bt = &s->ht->table[i];
771  SdbKv *kv;
772  ut32 j, count;
773 
774  BUCKET_FOREACH_SAFE(s->ht, bt, j, count, kv) {
775  if (kv && sdbkv_value(kv) && *sdbkv_value(kv)) {
776  if (!cb(user, sdbkv_key(kv), sdbkv_value(kv))) {
777  return sdb_foreach_end(s, false);
778  }
779  }
780  }
781  }
782  return sdb_foreach_end(s, true);
783 }
lzma_index ** i
Definition: index.h:629
static bool sdb_foreach_cdb(Sdb *s, SdbForeachCallback cb, SdbForeachCallback cb2, void *user)
Definition: sdb.c:730
static bool sdb_foreach_end(Sdb *s, bool result)
Definition: sdb.c:725
#define BUCKET_FOREACH_SAFE(ht, bt, j, count, kv)
Definition: sdb.c:20
static const char * cb[]
Definition: z80_tab.h:176

References BUCKET_FOREACH_SAFE, cb, count, i, NULL, s, sdb_foreach_cdb(), sdb_foreach_end(), sdbkv_key(), and sdbkv_value().

Referenced by deserialize_checkpoints(), deserialize_memory(), deserialize_registers(), load_flags(), rz_analysis_class_foreach(), rz_cmd_kuery(), rz_config_unserialize(), rz_debug_signal_list(), rz_display_opcode_handler(), rz_project_migrate_v1_v2(), rz_project_migrate_v2_v3(), rz_project_migrate_v3_v4(), rz_serialize_analysis_blocks_load(), rz_serialize_analysis_functions_load(), rz_serialize_analysis_global_var_load(), rz_serialize_analysis_hints_load(), rz_serialize_analysis_imports_load(), rz_serialize_analysis_meta_load(), rz_serialize_analysis_xrefs_load(), rz_serialize_bp_load(), rz_serialize_config_load(), rz_serialize_flag_zones_load(), rz_serialize_spaces_load(), rz_syscall_list(), sdb_copy(), sdb_diff_ctx(), sdb_diff_report(), sdb_foreach_list(), sdb_foreach_list_filter_user(), sdb_foreach_match(), sdb_like(), sdb_merge(), sdb_unset_like(), text_save(), and walk_namespace().

◆ sdb_foreach_cdb()

static bool sdb_foreach_cdb ( Sdb s,
SdbForeachCallback  cb,
SdbForeachCallback  cb2,
void *  user 
)
static

Definition at line 730 of file sdb.c.

730  {
731  char *v = NULL;
732  char k[SDB_CDB_MAX_KEY] = { 0 };
733  bool found;
734  sdb_dump_begin(s);
735  while (sdb_dump_dupnext(s, k, &v, NULL)) {
736  SdbKv *kv = sdb_ht_find_kvp(s->ht, k, &found);
737  if (found) {
738  free(v);
739  if (kv && sdbkv_key(kv) && sdbkv_value(kv)) {
740  if (!cb(user, sdbkv_key(kv), sdbkv_value(kv))) {
741  return false;
742  }
743  if (cb2 && !cb2(user, k, sdbkv_value(kv))) {
744  return false;
745  }
746  }
747  } else {
748  if (!cb(user, k, v)) {
749  free(v);
750  return false;
751  }
752  free(v);
753  }
754  }
755  return true;
756 }
RZ_API bool sdb_dump_dupnext(Sdb *s, char *key, char **value, int *_vlen)
Definition: sdb.c:877

References cb, found, free(), k, NULL, s, SDB_CDB_MAX_KEY, sdb_dump_begin(), sdb_dump_dupnext(), sdb_ht_find_kvp(), sdbkv_key(), sdbkv_value(), and v.

Referenced by sdb_foreach(), and sdb_sync().

◆ sdb_foreach_end()

static bool sdb_foreach_end ( Sdb s,
bool  result 
)
static

Definition at line 725 of file sdb.c.

725  {
726  s->depth--;
727  return result;
728 }

References s.

Referenced by sdb_foreach().

◆ sdb_foreach_list()

RZ_API SdbList* sdb_foreach_list ( Sdb s,
bool  sorted 
)

Definition at line 630 of file sdb.c.

630  {
633  if (sorted) {
635  }
636  return list;
637 }
static void list(RzEgg *egg)
Definition: rz-gg.c:52
RZ_API bool ls_sort(SdbList *list, SdbListComparator cmp)
Definition: ls.c:119
RZ_API SdbList * ls_newf(SdbListFree freefn)
Definition: ls.c:8
void(* SdbListFree)(void *ptr)
Definition: ls.h:14
static int __cmp_asc(const void *a, const void *b)
Definition: sdb.c:625
static bool sdb_foreach_list_cb(void *user, const char *k, const char *v)
Definition: sdb.c:615
RZ_API void sdbkv_free(SdbKv *kv)
Definition: sdb.c:524
Definition: ls.h:22

References __cmp_asc(), list(), ls_newf(), ls_sort(), s, sdb_foreach(), sdb_foreach_list_cb(), and sdbkv_free().

Referenced by __delete_almighty(), __exec_almighty(), __init_almighty_db(), __set_rcb(), __update_modal(), rop_kuery(), rz_analysis_calling_conventions(), rz_analysis_class_get_all(), rz_analysis_noreturn_functions(), rz_cmd_search(), rz_core_bin_export_info(), rz_flag_tags_list(), sdb_load_arch_profile(), sdb_load_base_types(), sdb_load_platform_profile(), sdb_load_sysregs(), sdb_querys(), text_save(), and typelinks_load_sdb().

◆ sdb_foreach_list_cb()

static bool sdb_foreach_list_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 615 of file sdb.c.

615  {
616  SdbList *list = (SdbList *)user;
617  SdbKv *kv = RZ_NEW0(SdbKv);
618  /* seems like some k/v are constructed in the stack and cant be used after returning */
619  kv->base.key = strdup(k);
620  kv->base.value = strdup(v);
621  ls_append(list, kv);
622  return 1;
623 }
RZ_API SdbListIter * ls_append(SdbList *list, void *data)
Definition: ls.c:200
#define RZ_NEW0(x)
Definition: rz_types.h:284
HtPPKv base
Definition: sdbht.h:16

References sdb_kv::base, k, list(), ls_append(), RZ_NEW0, strdup(), and v.

Referenced by sdb_foreach_list().

◆ sdb_foreach_list_filter()

RZ_API SdbList* sdb_foreach_list_filter ( Sdb s,
SdbForeachCallback  filter,
bool  sorted 
)

Definition at line 685 of file sdb.c.

685  {
686  return sdb_foreach_list_filter_user(s, filter, sorted, NULL);
687 }
RZ_API SdbList * sdb_foreach_list_filter_user(Sdb *s, SdbForeachCallback filter, bool sorted, void *user)
Definition: sdb.c:668

References NULL, s, and sdb_foreach_list_filter_user().

Referenced by rz_analysis_dwarf_integrate_functions(), and sdb_load_callables().

◆ sdb_foreach_list_filter_cb()

static bool sdb_foreach_list_filter_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 645 of file sdb.c.

645  {
646  struct foreach_list_filter_t *u = (struct foreach_list_filter_t *)user;
647  SdbList *list = u->list;
648  SdbKv *kv = NULL;
649 
650  if (!u->filter || u->filter(u->user, k, v)) {
651  kv = RZ_NEW0(SdbKv);
652  if (!kv) {
653  goto err;
654  }
655  kv->base.key = strdup(k);
656  kv->base.value = strdup(v);
657  if (!kv->base.key || !kv->base.value) {
658  goto err;
659  }
660  ls_append(list, kv);
661  }
662  return true;
663 err:
664  sdbkv_free(kv);
665  return false;
666 }
static bool err
Definition: armass.c:435
SdbList * list
Definition: sdb.c:641
SdbForeachCallback filter
Definition: sdb.c:640

References sdb_kv::base, err, foreach_list_filter_t::filter, k, list(), foreach_list_filter_t::list, ls_append(), NULL, RZ_NEW0, sdbkv_free(), strdup(), foreach_list_filter_t::user, and v.

Referenced by sdb_foreach_list_filter_user().

◆ sdb_foreach_list_filter_user()

RZ_API SdbList* sdb_foreach_list_filter_user ( Sdb s,
SdbForeachCallback  filter,
bool  sorted,
void *  user 
)

Definition at line 668 of file sdb.c.

668  {
669  struct foreach_list_filter_t u;
671 
672  if (!list) {
673  return NULL;
674  }
675  u.filter = filter;
676  u.list = list;
677  u.user = user;
679  if (sorted) {
681  }
682  return list;
683 }
static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char *data, char *str, int len, bool big_endian)
Definition: filter.c:185
static bool sdb_foreach_list_filter_cb(void *user, const char *k, const char *v)
Definition: sdb.c:645

References __cmp_asc(), filter(), foreach_list_filter_t::filter, list(), foreach_list_filter_t::list, ls_newf(), ls_sort(), NULL, s, sdb_foreach(), sdb_foreach_list_filter_cb(), sdbkv_free(), and foreach_list_filter_t::user.

Referenced by sdb_foreach_list_filter().

◆ sdb_foreach_match()

RZ_API SdbList* sdb_foreach_match ( Sdb s,
const char *  expr,
bool  single 
)

Definition at line 710 of file sdb.c.

710  {
712  _match_sdb_user o = { expr, list, single };
714  return list;
715 }
static bool sdb_foreach_match_cb(void *user, const char *k, const char *v)
Definition: sdb.c:695

References expr(), list(), ls_newf(), s, sdb_foreach(), sdb_foreach_match_cb(), and sdbkv_free().

Referenced by sdb_querys().

◆ sdb_foreach_match_cb()

static bool sdb_foreach_match_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 695 of file sdb.c.

695  {
696  _match_sdb_user *o = (_match_sdb_user *)user;
697  SdbKv tkv = { .base.key = (char *)k, .base.value = (char *)v };
698  if (sdbkv_match(&tkv, o->expr)) {
699  SdbKv *kv = RZ_NEW0(SdbKv);
700  kv->base.key = strdup(k);
701  kv->base.value = strdup(v);
702  ls_append(o->list, kv);
703  if (o->single) {
704  return false;
705  }
706  }
707  return true;
708 }
RZ_API bool sdbkv_match(SdbKv *kv, const char *expr)
Definition: sdb.c:472
const char * expr
Definition: sdb.c:690
SdbList * list
Definition: sdb.c:691
bool single
Definition: sdb.c:692

References sdb_kv::base, _match_sdb_user::expr, k, _match_sdb_user::list, ls_append(), RZ_NEW0, sdbkv_match(), _match_sdb_user::single, strdup(), and v.

Referenced by sdb_foreach_match().

◆ sdb_free()

RZ_API bool sdb_free ( Sdb s)

Definition at line 206 of file sdb.c.

206  {
207  if (s && s->ht && s->refs) {
208  s->refs--;
209  if (s->refs < 1) {
210  s->refs = 0;
211  sdb_fini(s, 0);
212  s->ht = NULL;
213  free(s);
214  return true;
215  }
216  }
217  return false;
218 }

References free(), NULL, s, and sdb_fini().

Referenced by _parse_resource_directory(), bin_pe_parse_imports(), config_path(), dbdiff(), dot_trace_traverse(), ds_free(), get_gnu_verneed(), get_gnu_versym(), get_vernaux_entry_sdb(), get_verneed_entry_sdb(), get_verneed_entry_sdb_aux(), ns_free_exc_list(), Pe_r_bin_store_string(), Pe_r_bin_store_string_table(), rz_agraph_free(), rz_analysis_esil_free(), rz_analysis_esil_stats(), rz_analysis_free(), rz_analysis_function_cc_load_handler(), rz_asm_free(), rz_asm_use(), rz_bin_elf_free(), rz_bin_elf_get_symbols_info(), rz_bin_file_free(), rz_bin_free(), rz_bin_mdmp_free_pe32_bin(), rz_bin_mdmp_free_pe64_bin(), rz_bin_object_set_items(), rz_core_bin_apply_relocs(), rz_core_fini(), rz_core_sysenv_begin(), rz_debug_free(), rz_debug_session_load(), rz_debug_session_save(), rz_egg_free(), rz_flag_free(), rz_main_rz_bin(), rz_project_free(), rz_project_load_file(), rz_project_load_file_raw(), rz_project_save_file(), rz_syscall_free(), rz_syscall_setup(), sdb_concat_by_path(), sdb_grep_dump(), sdb_load_arch_platform_by_path(), sdb_load_arch_profile_by_path(), sdb_load_by_path(), sdb_load_from_string(), sdb_ns_free(), sdb_ns_set(), session_sdb_load(), session_sdb_load_ns(), set_bin_relocs(), showcount(), and terminate().

◆ sdb_get()

◆ sdb_get_len()

RZ_API char* sdb_get_len ( Sdb s,
const char *  key,
int vlen,
ut32 cas 
)

Definition at line 285 of file sdb.c.

285  {
286  const char *value = sdb_const_get_len(s, key, vlen, cas);
287  return value ? strdup(value) : NULL;
288 }

References key, NULL, s, sdb_const_get_len(), strdup(), and value.

Referenced by sdb_array_sort(), sdb_get(), and sdb_uncat().

◆ sdb_global_hook()

RZ_API void sdb_global_hook ( SdbHook  hook,
void *  user 
)

Definition at line 37 of file sdb.c.

37  {
38  global_hook = hook;
39  global_user = user;
40 }
static void * global_user
Definition: sdb.c:35
static SdbHook global_hook
Definition: sdb.c:34

References global_hook, and global_user.

◆ sdb_hook()

RZ_API bool sdb_hook ( Sdb s,
SdbHook  cb,
void *  user 
)

Definition at line 983 of file sdb.c.

983  {
984  int i = 0;
985  SdbHook hook;
986  SdbListIter *iter;
987  if (s->hooks) {
988  ls_foreach (s->hooks, iter, hook) {
989  if (!(i % 2) && (hook == cb)) {
990  return false;
991  }
992  i++;
993  }
994  } else {
995  s->hooks = ls_new();
996  s->hooks->free = NULL;
997  }
998  ls_append(s->hooks, (void *)cb);
999  ls_append(s->hooks, user);
1000  return true;
1001 }
RZ_API SdbList * ls_new(void)
Definition: ls.c:16
void(* SdbHook)(Sdb *s, void *user, const char *k, const char *v)
Definition: sdb.h:308

References cb, i, ls_append(), ls_foreach, ls_new(), NULL, and s.

Referenced by sdb_new().

◆ sdb_hook_call()

RZ_API int sdb_hook_call ( Sdb s,
const char *  k,
const char *  v 
)

Definition at line 1019 of file sdb.c.

1019  {
1020  SdbListIter *iter;
1021  SdbHook hook;
1022  int i = 0;
1023  if (s->timestamped && s->last) {
1024  s->last = sdb_now();
1025  }
1026  ls_foreach (s->hooks, iter, hook) {
1027  if (!(i % 2) && k && iter->n) {
1028  void *u = iter->n->data;
1029  hook(s, u, k, v);
1030  }
1031  i++;
1032  }
1033  return i >> 1;
1034 }

References i, k, ls_foreach, s, sdb_now(), and v.

Referenced by sdb_set_internal().

◆ sdb_hook_free()

RZ_API void sdb_hook_free ( Sdb s)

Definition at line 1036 of file sdb.c.

1036  {
1037  ls_free(s->hooks);
1038  s->hooks = NULL;
1039 }

References ls_free(), NULL, and s.

Referenced by sdb_fini().

◆ sdb_isempty()

RZ_API bool sdb_isempty ( Sdb s)

Definition at line 148 of file sdb.c.

148  {
149  if (s) {
150  if (s->db.fd != -1) {
151  sdb_dump_begin(s);
152  while (sdb_dump_hasnext(s)) {
153  return false;
154  }
155  }
156  if (s->ht && s->ht->count > 0) {
157  return false;
158  }
159  }
160  return true;
161 }

References rz_socket_t::fd, s, sdb_dump_begin(), and sdb_dump_hasnext().

Referenced by rz_core_analysis_cc_init().

◆ sdb_like()

RZ_API char** sdb_like ( Sdb s,
const char *  k,
const char *  v,
SdbForeachCallback  cb 
)

Definition at line 1155 of file sdb.c.

1155  {
1156  LikeCallbackData lcd = { s, k, v, cb, NULL, 0, 0 };
1157  if (cb) {
1158  sdb_foreach(s, like_cb, &lcd);
1159  return NULL;
1160  }
1161  if (k && !*k) {
1162  lcd.key = NULL;
1163  }
1164  if (v && !*v) {
1165  lcd.val = NULL;
1166  }
1167  lcd.array_size = sizeof(char *) * 2;
1168  lcd.array = calloc(lcd.array_size, 1);
1169  if (!lcd.array) {
1170  return NULL;
1171  }
1172  lcd.array_index = 0;
1173  sdb_foreach(s, like_cb, &lcd);
1174  if (lcd.array_index == 0) {
1175  free((void *)lcd.array);
1176  return NULL;
1177  }
1178  return (char **)lcd.array;
1179 }
static bool like_cb(void *user, const char *k, const char *v)
Definition: sdb.c:1121

References LikeCallbackData::array, LikeCallbackData::array_index, LikeCallbackData::array_size, calloc(), cb, free(), k, LikeCallbackData::key, like_cb(), NULL, s, sdb_foreach(), v, and LikeCallbackData::val.

◆ sdb_merge()

RZ_API bool sdb_merge ( Sdb d,
Sdb s 
)

Definition at line 144 of file sdb.c.

144  {
145  return sdb_foreach(s, sdb_merge_cb, d);
146 }
static bool sdb_merge_cb(void *user, const char *k, const char *v)
Definition: sdb.c:139
#define d(i)
Definition: sha256.c:44

References d, s, sdb_foreach(), and sdb_merge_cb().

Referenced by rz_analysis_function_cc_load_handler(), and sdb_concat_by_path().

◆ sdb_merge_cb()

static bool sdb_merge_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 139 of file sdb.c.

139  {
140  sdb_set(user, k, v, 0);
141  return true;
142 }

References k, sdb_set(), and v.

Referenced by sdb_merge().

◆ sdb_new()

RZ_API Sdb* sdb_new ( const char *  path,
const char *  name,
int  lock 
)

Definition at line 47 of file sdb.c.

47  {
48  Sdb *s = RZ_NEW0(Sdb);
49  if (!s) {
50  return NULL;
51  }
52  s->db.fd = -1;
53  s->fd = -1;
54  s->refs = 1;
55  if (path && !*path) {
56  path = NULL;
57  }
58  if (name && *name && strcmp(name, "-")) {
59  if (path && *path) {
60  int plen = strlen(path);
61  int nlen = strlen(name);
62  s->dir = malloc(plen + nlen + 2);
63  if (!s->dir) {
64  free(s);
65  return NULL;
66  }
67  memcpy(s->dir, path, plen);
68  s->dir[plen] = '/';
69  memcpy(s->dir + plen + 1, name, nlen + 1);
70  s->path = strdup(path);
71  } else {
72  s->dir = strdup(name);
73  }
74  switch (lock) {
75  case 1:
76  if (!sdb_lock(sdb_lock_file(s->dir))) {
77  goto fail;
78  }
79  break;
80  case 2:
81  if (!sdb_lock_wait(sdb_lock_file(s->dir))) {
82  goto fail;
83  }
84  break;
85  }
86  if (sdb_open(s, s->dir) == -1) {
87  s->last = s->timestamped ? sdb_now() : 0LL;
88  // TODO: must fail if we cant open for write in sync
89  }
90  s->name = strdup(name);
91  } else {
92  s->last = s->timestamped ? sdb_now() : 0LL;
93  s->fd = -1;
94  }
95  s->journal = -1;
96  s->fdump = -1;
97  s->depth = 0;
98  s->ndump = NULL;
99  s->ns = ls_new(); // TODO: should be NULL
100  if (!s->ns) {
101  goto fail;
102  }
103  s->ns->free = NULL;
104  if (!s->ns) {
105  goto fail;
106  }
107  s->ht = sdb_ht_new();
108  s->lock = lock;
109  // if open fails ignore
110  if (global_hook) {
112  }
113  cdb_init(&s->db, s->fd);
114  return s;
115 fail:
116  if (s->fd != -1) {
117  close(s->fd);
118  s->fd = -1;
119  }
120  free(s->dir);
121  free(s->name);
122  free(s->path);
123  free(s);
124  return NULL;
125 }
bool cdb_init(struct cdb *c, int fd)
Definition: cdb.c:51
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
RZ_API int sdb_lock_wait(const char *s)
Definition: lock.c:50
RZ_API int sdb_open(Sdb *s, const char *file)
Definition: sdb.c:383
RZ_API bool sdb_hook(Sdb *s, SdbHook cb, void *user)
Definition: sdb.c:983
RZ_API HtPP * sdb_ht_new(void)
Definition: sdbht.c:11
Definition: z80asm.h:102
#define fail(test)
Definition: tests.h:29
static void lock(volatile int *lk)
Definition: malloc.c:61

References cdb_init(), close, fail, rz_socket_t::fd, free(), global_hook, global_user, lock(), ls_new(), malloc(), memcpy(), NULL, path, RZ_NEW0, s, sdb_hook(), sdb_ht_new(), sdb_lock(), sdb_lock_file(), sdb_lock_wait(), sdb_now(), sdb_open(), and strdup().

Referenced by __func_name_from_ord(), bin_pe_parse_imports(), config_path(), createdb(), dbdiff(), load_sdb(), main(), rz_analysis_function_cc_load_handler(), rz_asm_use(), rz_cmd_kuery(), rz_core_init(), rz_core_sysenv_begin(), rz_debug_signal_init(), rz_egg_new(), rz_main_rz_bin(), sdb_concat_by_path(), sdb_grep_dump(), sdb_load_arch_platform_by_path(), sdb_load_arch_profile_by_path(), sdb_load_by_path(), sdb_new0(), set_bin_relocs(), and showcount().

◆ sdb_new0()

◆ sdb_open()

RZ_API int sdb_open ( Sdb s,
const char *  file 
)

Definition at line 383 of file sdb.c.

383  {
384  struct stat st;
385  if (!s) {
386  return -1;
387  }
388  if (file) {
389  if (s->fd != -1) {
390  close(s->fd);
391  s->fd = -1;
392  }
393  s->fd = open(file, O_RDONLY | O_BINARY);
394  if (file != s->dir) {
395  free(s->dir);
396  s->dir = strdup(file);
397  s->path = NULL; // TODO: path is important
398  }
399  }
400  s->last = 0LL;
401  if (s->fd != -1 && fstat(s->fd, &st) != -1) {
402  if ((S_IFREG & st.st_mode) != S_IFREG) {
403  eprintf("Database must be a file\n");
404  close(s->fd);
405  s->fd = -1;
406  return -1;
407  }
408  s->last = st.st_mtime;
409  }
410  if (s->fd != -1) {
411  cdb_init(&s->db, s->fd);
412  }
413  return s->fd;
414 }
#define O_BINARY
Definition: cpipe.c:13
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset fstat
Definition: sflib.h:107
#define eprintf(x, y...)
Definition: rlcc.c:7
#define O_RDONLY
Definition: sftypes.h:486
Definition: gzappend.c:170
Definition: sftypes.h:80

References cdb_init(), close, eprintf, rz_socket_t::fd, free(), fstat, NULL, O_BINARY, O_RDONLY, s, and strdup().

Referenced by load_sdb(), sdb_disk_finish(), sdb_new(), and session_sdb_load_ns().

◆ sdb_remove()

RZ_API bool sdb_remove ( Sdb s,
const char *  key,
ut32  cas 
)

◆ sdb_reset()

RZ_API void sdb_reset ( Sdb s)

Definition at line 433 of file sdb.c.

433  {
434  if (!s) {
435  return;
436  }
437  /* ignore disk cache, file is not removed, but we will ignore
438  * its values when syncing again */
439  sdb_close(s);
440  /* empty memory hashtable */
441  sdb_ht_free(s->ht);
442  s->ht = sdb_ht_new();
443 }
RZ_API void sdb_close(Sdb *s)
Definition: sdb.c:416

References s, sdb_close(), sdb_ht_free(), and sdb_ht_new().

Referenced by cmd_analysis_esil(), load_sdb(), rz_agraph_reset(), rz_analysis_esil_stats(), rz_analysis_purge(), rz_core_analysis_cc_init(), rz_core_analysis_esil_deinit(), rz_flag_tags_reset(), rz_serialize_analysis_classes_load(), rz_serialize_analysis_function_noreturn_load(), and rz_type_cc_del_all_handler().

◆ sdb_set()

RZ_API int sdb_set ( Sdb s,
const char *  key,
const char *  val,
ut32  cas 
)

Definition at line 611 of file sdb.c.

611  {
612  return sdb_set_internal(s, key, (char *)val, 0, cas);
613 }
static ut32 sdb_set_internal(Sdb *s, const char *key, char *val, int owned, ut32 cas)
Definition: sdb.c:532

References key, s, sdb_set_internal(), and val.

Referenced by __init_sdb(), _store_resource_sdb(), art_header_load(), bin_pe_init_hdr(), block_store(), bootimg_header_load(), copy_foreach_cb(), core_analysis_graph_construct_edges(), core_analysis_graph_construct_nodes(), core_analysis_graph_nodes(), file_save(), file_save_cb(), flag_save_cb(), function_store(), get_bbnodes(), get_bbupdate(), get_class_ro_t(), get_elf_intrp(), get_gnu_versym(), get_libs(), get_sdb(), get_vernaux_entry_sdb(), get_verneed_entry_sdb_aux(), hints_acc_store_cb(), info(), init_dt_dynamic_sdb(), init_ehdr_sdb(), init_items(), init_phdr_sdb(), init_sdb_formats(), init_shdr_sdb(), java_set_sdb(), kv_loadlibs(), lmf_header_load(), load_process_line(), parse_segments(), parse_signature(), Pe_r_bin_store_string(), Pe_r_bin_store_string_table(), rename_key(), rop_classify(), rz_agraph_del_node(), rz_agraph_set_curnode(), rz_agraph_set_title(), rz_analysis_cc_set(), rz_analysis_cc_set_error(), rz_analysis_cc_set_self(), rz_analysis_class_create(), rz_analysis_class_rename_attr_raw(), rz_analysis_class_set_attr_raw(), rz_analysis_set_cc_default(), rz_analysis_set_syscc_default(), rz_bin_mdmp_init_directory(), rz_bin_mdmp_init_directory_entry(), rz_bin_mdmp_init_hdr(), rz_bin_mdmp_init_parsing(), rz_bin_mz_init_hdr(), rz_bin_te_init_hdr(), rz_config_serialize(), rz_core_analysis_esil_init_mem(), rz_debug_signal_init(), rz_egg_option_set(), rz_flag_tags_set(), rz_project_migrate_v4_v5(), rz_project_save(), rz_serialize_analysis_global_var_save(), rz_serialize_analysis_imports_save(), rz_serialize_bp_save(), rz_serialize_config_save(), rz_serialize_core_save(), rz_serialize_flag_save(), rz_serialize_flag_zones_save(), rz_serialize_spaces_save(), save_atomic_type(), save_callable(), save_enum(), save_struct(), save_typedef(), save_typelink(), save_union(), sdb_add(), sdb_array_add_sorted_num(), sdb_array_append(), sdb_array_insert(), sdb_array_pop_head(), sdb_array_prepend(), sdb_array_set(), sdb_bool_set(), sdb_concat(), sdb_journal_load(), sdb_merge_cb(), sdb_num_dec(), sdb_num_set(), sdb_querys(), sdb_save_dwarf_function(), sdb_set_enc(), sdb_unset(), serialize_checkpoints(), serialize_memory_cb(), serialize_register_cb(), store_xrefs_list_cb(), v1_v2_types_foreach_cb(), and v2_v3_types_foreach_cb().

◆ sdb_set_internal()

static ut32 sdb_set_internal ( Sdb s,
const char *  key,
char *  val,
int  owned,
ut32  cas 
)
static

Definition at line 532 of file sdb.c.

532  {
533  ut32 vlen, klen;
534  SdbKv *kv;
535  bool found;
536  if (!s || !key) {
537  return 0;
538  }
539  if (!val) {
540  if (owned) {
541  val = strdup("");
542  } else {
543  val = "";
544  }
545  }
546  // XXX strlen computed twice.. because of check_*()
547  klen = strlen(key);
548  vlen = strlen(val);
549  if (s->journal != -1) {
551  }
552  cdb_findstart(&s->db);
553  kv = sdb_ht_find_kvp(s->ht, key, &found);
554  if (found && sdbkv_value(kv)) {
555  if (cdb_findnext(&s->db, sdb_hash(key), key, klen)) {
556  if (cas && kv->cas != cas) {
557  if (owned) {
558  free(val);
559  }
560  return 0;
561  }
562  if (vlen == sdbkv_value_len(kv) && !strcmp(sdbkv_value(kv), val)) {
563  sdb_hook_call(s, key, val);
564  return kv->cas;
565  }
566  kv->cas = cas = nextcas();
567  if (owned) {
568  kv->base.value_len = vlen;
569  free(kv->base.value);
570  kv->base.value = val; // owned
571  } else {
572  if ((ut32)vlen > kv->base.value_len) {
573  free(kv->base.value);
574  kv->base.value = malloc(vlen + 1);
575  }
576  memcpy(kv->base.value, val, vlen + 1);
577  kv->base.value_len = vlen;
578  }
579  } else {
580  sdb_ht_delete(s->ht, key);
581  }
582  sdb_hook_call(s, key, val);
583  return cas;
584  }
585  // empty values are also stored
586  // TODO store only the ones that are in the CDB
587  if (owned) {
588  kv = sdbkv_new2(key, klen, NULL, 0);
589  if (kv) {
590  kv->base.value = val;
591  kv->base.value_len = vlen;
592  }
593  } else {
594  kv = sdbkv_new2(key, klen, val, vlen);
595  }
596  if (kv) {
597  ut32 cas = kv->cas = nextcas();
598  sdb_ht_insert_kvp(s->ht, kv, true /*update*/);
599  free(kv);
600  sdb_hook_call(s, key, val);
601  return cas;
602  }
603  // kv set failed, no need to callback sdb_hook_call (s, key, val);
604  return 0;
605 }
RZ_API bool sdb_journal_log(Sdb *s, const char *key, const char *val)
Definition: journal.c:81
RZ_API SdbKv * sdbkv_new2(const char *k, int kl, const char *v, int vl)
Definition: sdb.c:493
RZ_API int sdb_hook_call(Sdb *s, const char *k, const char *v)
Definition: sdb.c:1019
static int nextcas(void)
Definition: sdb.c:26
RZ_API bool sdb_ht_insert_kvp(HtPP *ht, SdbKv *kvp, bool update)
Definition: sdbht.c:47

References sdb_kv::base, sdb_kv::cas, cdb_findnext(), cdb_findstart(), found, free(), key, malloc(), memcpy(), nextcas(), NULL, s, sdb_hash(), sdb_hook_call(), sdb_ht_delete(), sdb_ht_find_kvp(), sdb_ht_insert_kvp(), sdb_journal_log(), sdbkv_new2(), sdbkv_value(), sdbkv_value_len(), strdup(), and val.

Referenced by sdb_set(), and sdb_set_owned().

◆ sdb_set_owned()

◆ sdb_stats()

RZ_API bool sdb_stats ( Sdb s,
ut32 disk,
ut32 mem 
)

Definition at line 856 of file sdb.c.

856  {
857  if (!s) {
858  return false;
859  }
860  if (disk) {
861  ut32 count = 0;
862  if (s->fd != -1) {
863  sdb_dump_begin(s);
864  while (sdb_dump_hasnext(s)) {
865  count++;
866  }
867  }
868  *disk = count;
869  }
870  if (mem) {
871  *mem = s->ht->count;
872  }
873  return disk || mem;
874 }
void * mem
Definition: libc.cpp:91

References count, rz_socket_t::fd, mem, s, sdb_dump_begin(), and sdb_dump_hasnext().

Referenced by showcount().

◆ sdb_sync()

RZ_API bool sdb_sync ( Sdb s)

Definition at line 803 of file sdb.c.

803  {
804  bool result;
805  ut32 i;
806 
807  if (!s || !sdb_disk_create(s)) {
808  return false;
809  }
811  if (!result) {
812  return false;
813  }
814 
815  /* append new keyvalues */
816  for (i = 0; i < s->ht->size; ++i) {
817  HtPPBucket *bt = &s->ht->table[i];
818  SdbKv *kv;
819  ut32 j, count;
820 
821  BUCKET_FOREACH_SAFE(s->ht, bt, j, count, kv) {
822  if (sdbkv_key(kv) && sdbkv_value(kv) && *sdbkv_value(kv) && !kv->expire) {
823  if (sdb_disk_insert(s, sdbkv_key(kv), sdbkv_value(kv))) {
824  sdb_remove(s, sdbkv_key(kv), 0);
825  }
826  }
827  }
828  }
831  // TODO: sdb_reset memory state?
832  return true;
833 }
RZ_API bool sdb_disk_finish(Sdb *s)
Definition: disk.c:79
RZ_API bool sdb_disk_create(Sdb *s)
Definition: disk.c:17
static bool _insert_into_disk(void *user, const char *key, const char *value)
Definition: sdb.c:785
RZ_API bool sdb_remove(Sdb *s, const char *key, ut32 cas)
Definition: sdb.c:299
static bool _remove_afer_insert(void *user, const char *k, const char *v)
Definition: sdb.c:794

References _insert_into_disk(), _remove_afer_insert(), BUCKET_FOREACH_SAFE, count, sdb_kv::expire, i, s, sdb_disk_create(), sdb_disk_finish(), sdb_disk_insert(), sdb_foreach_cdb(), sdb_journal_clear(), sdb_remove(), sdbkv_key(), and sdbkv_value().

Referenced by config_path(), createdb(), ns_sync(), rz_cmd_kuery(), rz_core_sysenv_begin(), session_sdb_save(), and terminate().

◆ sdb_uncat()

RZ_API int sdb_uncat ( Sdb s,
const char *  key,
const char *  value,
ut32  cas 
)

Definition at line 304 of file sdb.c.

304  {
305  // remove 'value' from current key value.
306  // TODO: cas is ignored here
307  int vlen = 0, valen;
308  char *p, *v = sdb_get_len(s, key, &vlen, NULL);
309  int mod = 0;
310  if (!v || !key || !value) {
311  free(v);
312  return 0;
313  }
314  valen = strlen(value);
315  if (valen > 0) {
316  while ((p = strstr(v, value))) {
317  memmove(p, p + valen, strlen(p + valen) + 1);
318  mod = 1;
319  }
320  }
321  if (mod) {
322  sdb_set_owned(s, key, v, 0);
323  } else {
324  free(v);
325  }
326  return 0;
327 }
int mod(int a, int b)
Definition: crypto_rot.c:8

References free(), key, mod(), NULL, p, s, sdb_get_len(), sdb_set_owned(), v, and value.

Referenced by sdb_querys().

◆ sdb_unhook()

RZ_API bool sdb_unhook ( Sdb s,
SdbHook  h 
)

Definition at line 1003 of file sdb.c.

1003  {
1004  int i = 0;
1005  SdbHook hook;
1006  SdbListIter *iter, *iter2;
1007  ls_foreach (s->hooks, iter, hook) {
1008  if (!(i % 2) && (hook == h)) {
1009  iter2 = iter->n;
1010  ls_delete(s->hooks, iter);
1011  ls_delete(s->hooks, iter2);
1012  return true;
1013  }
1014  i++;
1015  }
1016  return false;
1017 }
RZ_API void ls_delete(SdbList *list, SdbListIter *iter)
Definition: ls.c:133
#define h(i)
Definition: sha256.c:48

References h, i, ls_delete(), ls_foreach, and s.

◆ sdb_unlink()

RZ_API bool sdb_unlink ( Sdb s)

Definition at line 1064 of file sdb.c.

1064  {
1065  sdb_fini(s, 1);
1066  return sdb_disk_unlink(s);
1067 }
RZ_API bool sdb_disk_unlink(Sdb *s)
Definition: disk.c:121

References s, sdb_disk_unlink(), and sdb_fini().

◆ sdb_unset()

◆ sdb_unset_like()

RZ_API int sdb_unset_like ( Sdb s,
const char *  k 
)

Definition at line 1106 of file sdb.c.

1106  {
1107  UnsetCallbackData ucd = { s, k };
1108  return sdb_foreach(s, unset_cb, &ucd);
1109 }
static bool unset_cb(void *user, const char *k, const char *v)
Definition: sdb.c:1098

References k, s, sdb_foreach(), and unset_cb().

Referenced by sdb_querys().

◆ sdbkv_free()

RZ_API void sdbkv_free ( SdbKv kv)

Definition at line 524 of file sdb.c.

524  {
525  if (kv) {
526  free(sdbkv_key(kv));
527  free(sdbkv_value(kv));
528  RZ_FREE(kv);
529  }
530 }
#define RZ_FREE(x)
Definition: rz_types.h:369

References free(), RZ_FREE, sdbkv_key(), and sdbkv_value().

Referenced by sdb_foreach_list(), sdb_foreach_list_filter_cb(), sdb_foreach_list_filter_user(), and sdb_foreach_match().

◆ sdbkv_match()

RZ_API bool sdbkv_match ( SdbKv kv,
const char *  expr 
)

Definition at line 472 of file sdb.c.

472  {
473  // TODO: add syntax to negate condition
474  // TODO: add syntax to OR k/v instead of AND
475  // [^]str[$]=[^]str[$]
476  const char *eq = strchr(expr, '=');
477  if (eq) {
478  char *e = strdup(expr);
479  char *ep = e + (eq - expr);
480  *ep++ = 0;
481  bool res = !*e || match(sdbkv_key(kv), e);
482  bool res2 = !*ep || match(sdbkv_value(kv), ep);
483  free(e);
484  return res && res2;
485  }
486  return match(sdbkv_key(kv), expr);
487 }
static bool match(const char *str, const char *expr)
Definition: sdb.c:450

References e, eq, expr(), free(), match(), sdbkv_key(), sdbkv_value(), and strdup().

Referenced by sdb_foreach_match_cb().

◆ sdbkv_new()

RZ_API SdbKv* sdbkv_new ( const char *  k,
const char *  v 
)

Definition at line 489 of file sdb.c.

489  {
490  return sdbkv_new2(k, strlen(k), v, strlen(v));
491 }

References k, sdbkv_new2(), and v.

◆ sdbkv_new2()

RZ_API SdbKv* sdbkv_new2 ( const char *  k,
int  kl,
const char *  v,
int  vl 
)

Definition at line 493 of file sdb.c.

493  {
494  SdbKv *kv;
495  if (!v) {
496  vl = 0;
497  }
498  kv = RZ_NEW0(SdbKv);
499  kv->base.key_len = kl;
500  kv->base.key = malloc(kv->base.key_len + 1);
501  if (!kv->base.key) {
502  free(kv);
503  return NULL;
504  }
505  memcpy(kv->base.key, k, kv->base.key_len + 1);
506  kv->base.value_len = vl;
507  if (vl) {
508  kv->base.value = malloc(vl + 1);
509  if (!kv->base.value) {
510  free(kv->base.key);
511  free(kv);
512  return NULL;
513  }
514  memcpy(kv->base.value, v, vl + 1);
515  } else {
516  kv->base.value = NULL;
517  kv->base.value_len = 0;
518  }
519  kv->cas = nextcas();
520  kv->expire = 0LL;
521  return kv;
522 }

References sdb_kv::base, sdb_kv::cas, sdb_kv::expire, free(), k, malloc(), memcpy(), nextcas(), NULL, RZ_NEW0, and v.

Referenced by sdb_set_internal(), and sdbkv_new().

◆ unset_cb()

static bool unset_cb ( void *  user,
const char *  k,
const char *  v 
)
static

Definition at line 1098 of file sdb.c.

1098  {
1099  UnsetCallbackData *ucd = user;
1100  if (sdb_match(k, ucd->key)) {
1101  sdb_unset(ucd->sdb, k, 0);
1102  }
1103  return true;
1104 }
const char * key
Definition: sdb.c:1095

References k, UnsetCallbackData::key, UnsetCallbackData::sdb, sdb_match(), and sdb_unset().

Referenced by sdb_unset_like().

Variable Documentation

◆ global_hook

SdbHook global_hook = NULL
static

Definition at line 34 of file sdb.c.

Referenced by sdb_global_hook(), and sdb_new().

◆ global_user

void* global_user = NULL
static

Definition at line 35 of file sdb.c.

Referenced by sdb_global_hook(), and sdb_new().