Rizin
unix-like reverse engineering framework and cli tools
sdb.h File Reference
#include <rz_types.h>
#include "sdbht.h"
#include "ls.h"
#include "cdb.h"
#include "cdb_make.h"

Go to the source code of this file.

Classes

struct  sdb_t
 
struct  sdb_ns_t
 
struct  sdb_diff_t
 

Macros

#define SDB_CDB_MIN_VALUE   1
 
#define SDB_CDB_MAX_VALUE   CDB_MAX_VALUE
 
#define SDB_CDB_MIN_KEY   1
 
#define SDB_CDB_MAX_KEY   CDB_MAX_KEY
 
#define SDB_MODE   0644
 
#define SDB_RS   ','
 
#define SDB_SS   ","
 
#define SDB_MAX_PATH   256
 
#define SDB_NUM_BASE   16
 
#define SDB_NUM_BUFSZ   64
 
#define SDB_OPTION_NONE   0
 
#define SDB_OPTION_ALL   0xff
 
#define SDB_OPTION_SYNC   (1 << 0)
 
#define SDB_OPTION_NOSTAMP   (1 << 1)
 
#define SDB_OPTION_FS   (1 << 2)
 
#define SDB_OPTION_JOURNAL   (1 << 3)
 
#define SDB_LIST_UNSORTED   0
 
#define SDB_LIST_SORTED   1
 
#define sdb_aforeach(x, y)
 
#define sdb_aforeach_next(x)
 

Typedefs

typedef struct sdb_t Sdb
 
typedef struct sdb_ns_t SdbNs
 
typedef bool(* SdbForeachCallback) (void *user, const char *k, const char *v)
 
typedef struct sdb_diff_t SdbDiff
 
typedef void(* SdbDiffCallback) (const SdbDiff *diff, void *user)
 
typedef void(* SdbHook) (Sdb *s, void *user, const char *k, const char *v)
 

Functions

RZ_API Sdbsdb_new0 (void)
 
RZ_API Sdbsdb_new (const char *path, const char *file, int lock)
 
RZ_API int sdb_open (Sdb *s, const char *file)
 
RZ_API void sdb_close (Sdb *s)
 
RZ_API void sdb_config (Sdb *s, int options)
 
RZ_API bool sdb_free (Sdb *s)
 
RZ_API void sdb_file (Sdb *s, const char *dir)
 
RZ_API bool sdb_merge (Sdb *d, Sdb *s)
 
RZ_API int sdb_count (Sdb *s)
 
RZ_API void sdb_reset (Sdb *s)
 
RZ_API void sdb_setup (Sdb *s, int options)
 
RZ_API void sdb_drain (Sdb *, Sdb *)
 
RZ_API void sdb_copy (Sdb *src, Sdb *dst)
 
RZ_API bool sdb_stats (Sdb *s, ut32 *disk, ut32 *mem)
 
RZ_API bool sdb_dump_hasnext (Sdb *s)
 
RZ_API bool sdb_foreach (Sdb *s, SdbForeachCallback cb, void *user)
 
RZ_API SdbListsdb_foreach_list (Sdb *s, bool sorted)
 
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)
 
RZ_API SdbListsdb_foreach_match (Sdb *s, const char *expr, bool sorted)
 
RZ_API int sdb_query (Sdb *s, const char *cmd)
 
RZ_API int sdb_queryf (Sdb *s, const char *fmt,...)
 
RZ_API int sdb_query_lines (Sdb *s, const char *cmd)
 
RZ_API char * sdb_querys (Sdb *s, char *buf, size_t len, const char *cmd)
 
RZ_API char * sdb_querysf (Sdb *s, char *buf, size_t buflen, const char *fmt,...)
 
RZ_API int sdb_query_file (Sdb *s, const char *file)
 
RZ_API bool sdb_exists (Sdb *, const char *key)
 
RZ_API bool sdb_remove (Sdb *, const char *key, ut32 cas)
 
RZ_API int sdb_unset (Sdb *, const char *key, ut32 cas)
 
RZ_API int sdb_unset_like (Sdb *s, const char *k)
 
RZ_API char ** sdb_like (Sdb *s, const char *k, const char *v, SdbForeachCallback cb)
 
RZ_API int sdb_diff_format (char *str, int size, const SdbDiff *diff)
 
RZ_API bool sdb_diff (Sdb *a, Sdb *b, SdbDiffCallback cb, void *cb_user)
 
RZ_API char * sdb_get (Sdb *, const char *key, ut32 *cas)
 
RZ_API char * sdb_get_len (Sdb *, const char *key, int *vlen, ut32 *cas)
 
RZ_API const char * sdb_const_get (Sdb *, const char *key, ut32 *cas)
 
RZ_API const char * sdb_const_get_len (Sdb *s, const char *key, int *vlen, ut32 *cas)
 
RZ_API int sdb_set (Sdb *, const char *key, const char *data, ut32 cas)
 
RZ_API int sdb_set_owned (Sdb *s, const char *key, char *val, ut32 cas)
 
RZ_API int sdb_concat (Sdb *s, const char *key, const char *value, ut32 cas)
 
RZ_API int sdb_uncat (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_sync (Sdb *)
 
RZ_API void sdbkv_free (SdbKv *kv)
 
RZ_API bool sdb_num_exists (Sdb *, const char *key)
 
RZ_API int sdb_num_base (const char *s)
 
RZ_API ut64 sdb_num_get (Sdb *s, const char *key, ut32 *cas)
 
RZ_API int sdb_num_set (Sdb *s, const char *key, ut64 v, ut32 cas)
 
RZ_API int sdb_num_add (Sdb *s, const char *key, ut64 v, ut32 cas)
 
RZ_API ut64 sdb_num_inc (Sdb *s, const char *key, ut64 n, ut32 cas)
 
RZ_API ut64 sdb_num_dec (Sdb *s, const char *key, ut64 n, ut32 cas)
 
RZ_API int sdb_num_min (Sdb *s, const char *key, ut64 v, ut32 cas)
 
RZ_API int sdb_num_max (Sdb *s, const char *key, ut64 v, ut32 cas)
 
RZ_API int sdb_ptr_set (Sdb *db, const char *key, void *p, ut32 cas)
 
RZ_API void * sdb_ptr_get (Sdb *db, const char *key, ut32 *cas)
 
RZ_API bool sdb_disk_create (Sdb *s)
 
RZ_API bool sdb_disk_insert (Sdb *s, const char *key, const char *val)
 
RZ_API bool sdb_disk_finish (Sdb *s)
 
RZ_API bool sdb_disk_unlink (Sdb *s)
 
RZ_API bool sdb_text_save_fd (Sdb *s, int fd, bool sort)
 
RZ_API bool sdb_text_save (Sdb *s, const char *file, bool sort)
 
RZ_API bool sdb_text_load_buf (Sdb *s, char *buf, size_t sz)
 
RZ_API bool sdb_text_load (Sdb *s, const char *file)
 
RZ_API void sdb_dump_begin (Sdb *s)
 
RZ_API SdbKvsdb_dump_next (Sdb *s)
 
RZ_API bool sdb_dump_dupnext (Sdb *s, char *key, char **value, int *_vlen)
 
RZ_API bool sdb_journal_close (Sdb *s)
 
RZ_API bool sdb_journal_open (Sdb *s)
 
RZ_API int sdb_journal_load (Sdb *s)
 
RZ_API bool sdb_journal_log (Sdb *s, const char *key, const char *val)
 
RZ_API bool sdb_journal_clear (Sdb *s)
 
RZ_API bool sdb_journal_unlink (Sdb *s)
 
RZ_API char * sdb_itoa (ut64 n, char *s, int base)
 
RZ_API ut64 sdb_atoi (const char *s)
 
RZ_API const char * sdb_itoca (ut64 n)
 
RZ_API bool sdb_lock (const char *s)
 
RZ_API const char * sdb_lock_file (const char *f)
 
RZ_API void sdb_unlock (const char *s)
 
RZ_API bool sdb_unlink (Sdb *s)
 
RZ_API int sdb_lock_wait (RZ_UNUSED const char *s)
 
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 ut64 sdb_now (void)
 
RZ_API ut64 sdb_unow (void)
 
RZ_API ut32 sdb_hash (const char *key)
 
RZ_API ut32 sdb_hash_len (const char *key, ut32 *len)
 
RZ_API ut8 sdb_hash_byte (const char *s)
 
RZ_API Sdbsdb_ns (Sdb *s, const char *name, int create)
 
RZ_API Sdbsdb_ns_path (Sdb *s, const char *path, int create)
 
RZ_API void sdb_ns_init (Sdb *s)
 
RZ_API void sdb_ns_free_all (Sdb *s)
 
RZ_API void sdb_ns_lock (Sdb *s, int lock, int depth)
 
RZ_API void sdb_ns_sync (Sdb *s)
 
RZ_API int sdb_ns_set (Sdb *s, const char *name, Sdb *r)
 
RZ_API bool sdb_ns_unset (Sdb *s, const char *name, Sdb *r)
 
RZ_API bool sdb_array_contains (Sdb *s, const char *key, const char *val, ut32 *cas)
 
RZ_API bool sdb_array_contains_num (Sdb *s, const char *key, ut64 val, ut32 *cas)
 
RZ_API int sdb_array_indexof (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API int sdb_array_set (Sdb *s, const char *key, int idx, const char *val, ut32 cas)
 
RZ_API int sdb_array_set_num (Sdb *s, const char *key, int idx, ut64 val, ut32 cas)
 
RZ_API bool sdb_array_append (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API bool sdb_array_append_num (Sdb *s, const char *key, ut64 val, ut32 cas)
 
RZ_API bool sdb_array_prepend (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API bool sdb_array_prepend_num (Sdb *s, const char *key, ut64 val, ut32 cas)
 
RZ_API char * sdb_array_get (Sdb *s, const char *key, int idx, ut32 *cas)
 
RZ_API ut64 sdb_array_get_num (Sdb *s, const char *key, int idx, ut32 *cas)
 
RZ_API int sdb_array_get_idx (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API int sdb_array_insert (Sdb *s, const char *key, int idx, const char *val, ut32 cas)
 
RZ_API int sdb_array_insert_num (Sdb *s, const char *key, int idx, ut64 val, ut32 cas)
 
RZ_API int sdb_array_unset (Sdb *s, const char *key, int n, ut32 cas)
 
RZ_API int sdb_array_delete (Sdb *s, const char *key, int n, ut32 cas)
 
RZ_API void sdb_array_sort (Sdb *s, const char *key, ut32 cas)
 
RZ_API void sdb_array_sort_num (Sdb *s, const char *key, ut32 cas)
 
RZ_API int sdb_array_add (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API int sdb_array_add_num (Sdb *s, const char *key, ut64 val, ut32 cas)
 
RZ_API int sdb_array_add_sorted (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API int sdb_array_add_sorted_num (Sdb *s, const char *key, ut64 val, ut32 cas)
 
RZ_API int sdb_array_remove (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API int sdb_array_remove_num (Sdb *s, const char *key, ut64 val, ut32 cas)
 
RZ_API char * sdb_anext (char *str, char **next)
 
RZ_API const char * sdb_const_anext (const char *str)
 
RZ_API int sdb_alen (const char *str)
 
RZ_API int sdb_alen_ignore_empty (const char *str)
 
RZ_API int sdb_array_size (Sdb *s, const char *key)
 
RZ_API int sdb_array_length (Sdb *s, const char *key)
 
int sdb_array_list (Sdb *s, const char *key)
 
RZ_API bool sdb_array_push (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API char * sdb_array_pop (Sdb *s, const char *key, ut32 *cas)
 
RZ_API int sdb_array_push_num (Sdb *s, const char *key, ut64 num, ut32 cas)
 
RZ_API ut64 sdb_array_pop_num (Sdb *s, const char *key, ut32 *cas)
 
RZ_API char * sdb_array_pop_head (Sdb *s, const char *key, ut32 *cas)
 
RZ_API char * sdb_array_pop_tail (Sdb *s, const char *key, ut32 *cas)
 
RZ_API void sdb_global_hook (SdbHook hook, void *user)
 
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 int sdb_isnum (const char *s)
 
RZ_API bool sdb_isempty (Sdb *s)
 
RZ_API const char * sdb_type (const char *k)
 
RZ_API bool sdb_match (const char *str, const char *glob)
 
RZ_API int sdb_bool_set (Sdb *db, const char *str, bool v, ut32 cas)
 
RZ_API bool sdb_bool_get (Sdb *db, const char *str, ut32 *cas)
 
RZ_API ut8sdb_decode (const char *in, int *len)
 
RZ_API char * sdb_encode (const ut8 *bin, int len)
 
RZ_API void sdb_encode_raw (char *bout, const ut8 *bin, int len)
 
RZ_API int sdb_decode_raw (ut8 *bout, const char *bin, int len)
 
RZ_API char * sdb_fmt (const char *fmt,...) RZ_PRINTF_CHECK(1
 
RZ_API char RZ_API int sdb_fmt_init (void *p, const char *fmt)
 
RZ_API void sdb_fmt_free (void *p, const char *fmt)
 
RZ_API int sdb_fmt_tobin (const char *_str, const char *fmt, void *stru)
 
RZ_API char * sdb_fmt_tostr (void *stru, const char *fmt)
 
RZ_API char ** sdb_fmt_array (const char *list)
 
RZ_API ut64sdb_fmt_array_num (const char *list)
 
RZ_API char * sdb_array_compact (char *p)
 
RZ_API char * sdb_aslice (char *out, int from, int to)
 

Macro Definition Documentation

◆ sdb_aforeach

#define sdb_aforeach (   x,
 
)
Value:
{ \
char *next; \
if (y) \
for (x = y;;) { \
x = sdb_anext(x, &next);
int x
Definition: mipsasm.c:20
RZ_API char * sdb_anext(char *str, char **next)
Definition: util.c:192

Definition at line 342 of file sdb.h.

◆ sdb_aforeach_next

#define sdb_aforeach_next (   x)
Value:
if (!next) \
break; \
*(next - 1) = ','; \
x = next; \
} \
}

Definition at line 348 of file sdb.h.

◆ SDB_CDB_MAX_KEY

#define SDB_CDB_MAX_KEY   CDB_MAX_KEY

Definition at line 21 of file sdb.h.

◆ SDB_CDB_MAX_VALUE

#define SDB_CDB_MAX_VALUE   CDB_MAX_VALUE

Definition at line 19 of file sdb.h.

◆ SDB_CDB_MIN_KEY

#define SDB_CDB_MIN_KEY   1

Definition at line 20 of file sdb.h.

◆ SDB_CDB_MIN_VALUE

#define SDB_CDB_MIN_VALUE   1

Definition at line 18 of file sdb.h.

◆ SDB_LIST_SORTED

#define SDB_LIST_SORTED   1

Definition at line 61 of file sdb.h.

◆ SDB_LIST_UNSORTED

#define SDB_LIST_UNSORTED   0

Definition at line 60 of file sdb.h.

◆ SDB_MAX_PATH

#define SDB_MAX_PATH   256

Definition at line 49 of file sdb.h.

◆ SDB_MODE

#define SDB_MODE   0644

Definition at line 42 of file sdb.h.

◆ SDB_NUM_BASE

#define SDB_NUM_BASE   16

Definition at line 50 of file sdb.h.

◆ SDB_NUM_BUFSZ

#define SDB_NUM_BUFSZ   64

Definition at line 51 of file sdb.h.

◆ SDB_OPTION_ALL

#define SDB_OPTION_ALL   0xff

Definition at line 54 of file sdb.h.

◆ SDB_OPTION_FS

#define SDB_OPTION_FS   (1 << 2)

Definition at line 57 of file sdb.h.

◆ SDB_OPTION_JOURNAL

#define SDB_OPTION_JOURNAL   (1 << 3)

Definition at line 58 of file sdb.h.

◆ SDB_OPTION_NONE

#define SDB_OPTION_NONE   0

Definition at line 53 of file sdb.h.

◆ SDB_OPTION_NOSTAMP

#define SDB_OPTION_NOSTAMP   (1 << 1)

Definition at line 56 of file sdb.h.

◆ SDB_OPTION_SYNC

#define SDB_OPTION_SYNC   (1 << 0)

Definition at line 55 of file sdb.h.

◆ SDB_RS

#define SDB_RS   ','

Definition at line 47 of file sdb.h.

◆ SDB_SS

#define SDB_SS   ","

Definition at line 48 of file sdb.h.

Typedef Documentation

◆ Sdb

typedef struct sdb_t Sdb

◆ SdbDiff

typedef struct sdb_diff_t SdbDiff

◆ SdbDiffCallback

typedef void(* SdbDiffCallback) (const SdbDiff *diff, void *user)

Definition at line 145 of file sdb.h.

◆ SdbForeachCallback

typedef bool(* SdbForeachCallback) (void *user, const char *k, const char *v)

Definition at line 115 of file sdb.h.

◆ SdbHook

typedef void(* SdbHook) (Sdb *s, void *user, const char *k, const char *v)

Definition at line 308 of file sdb.h.

◆ SdbNs

typedef struct sdb_ns_t SdbNs

Function Documentation

◆ 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
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
static RzSocket * s
Definition: rtr.c:28
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
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_alen()

RZ_API int sdb_alen ( const char *  str)

Definition at line 151 of file util.c.

151  {
152  int len = 1;
153  const char *n, *p = str;
154  if (!p || !*p) {
155  return 0;
156  }
157  for (len = 0;; len++) {
158  n = strchr(p, SDB_RS);
159  if (!n) {
160  break;
161  }
162  p = n + 1;
163  }
164  return ++len;
165 }
size_t len
Definition: 6502dis.c:15
void * p
Definition: libc.cpp:67
int n
Definition: mipsasm.c:19
#define SDB_RS
Definition: sdb.h:47

References len, n, p, SDB_RS, and cmd_descs_generate::str.

Referenced by get_enum_type(), get_struct_type(), get_union_type(), rz_analysis_class_base_get_all(), rz_analysis_class_method_get_all(), rz_analysis_class_vtable_get_all(), sdb_array_delete(), sdb_array_get(), sdb_array_set(), sdb_array_size(), sdb_fmt_array(), and sdb_fmt_array_num().

◆ sdb_alen_ignore_empty()

RZ_API int sdb_alen_ignore_empty ( const char *  str)

Definition at line 167 of file util.c.

167  {
168  int len = 1;
169  const char *n, *p = str;
170  if (!p || !*p) {
171  return 0;
172  }
173  while (*p == SDB_RS) {
174  p++;
175  }
176  for (len = 0;;) {
177  n = strchr(p, SDB_RS);
178  if (!n) {
179  break;
180  }
181  p = n + 1;
182  if (*(p) == SDB_RS) {
183  continue;
184  }
185  len++;
186  }
187  if (*p)
188  len++;
189  return len;
190 }

References len, n, p, SDB_RS, and cmd_descs_generate::str.

Referenced by sdb_array_length().

◆ sdb_anext()

RZ_API char* sdb_anext ( char *  str,
char **  next 
)

Definition at line 192 of file util.c.

192  {
193  char *nxt, *p = strchr(str, SDB_RS);
194  if (p) {
195  *p = 0;
196  nxt = p + 1;
197  } else {
198  nxt = NULL;
199  }
200  if (next) {
201  *next = nxt;
202  }
203  return str;
204 }
#define NULL
Definition: cris-opc.c:27

References NULL, p, SDB_RS, and cmd_descs_generate::str.

Referenced by get_callable_type(), get_struct_type(), get_union_type(), rz_analysis_class_base_get(), rz_analysis_class_method_get(), rz_analysis_class_vtable_get(), rz_analysis_dwarf_integrate_functions(), and sdb_fmt_tobin().

◆ sdb_array_add()

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

Definition at line 224 of file array.c.

224  {
225  if (sdb_array_contains(s, key, val, NULL)) {
226  return 0;
227  }
228  return sdb_array_insert(s, key, -1, val, cas);
229 }
RZ_API int sdb_array_insert(Sdb *s, const char *key, int idx, const char *val, ut32 cas)
Definition: array.c:139
RZ_API bool sdb_array_contains(Sdb *s, const char *key, const char *val, ut32 *cas)
Definition: array.c:497

References key, NULL, s, sdb_array_contains(), sdb_array_insert(), and val.

Referenced by hook_command(), hook_flag_read(), hook_reg_read(), hook_reg_write(), rz_agraph_add_edge(), rz_agraph_add_node_with_color(), rz_analysis_class_rename_attr_raw(), rz_analysis_class_set_attr_raw(), sdb_array_add_num(), and sdb_querys().

◆ sdb_array_add_num()

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

Definition at line 211 of file array.c.

211  {
212  char buf[SDB_NUM_BUFSZ];
213  char *v = sdb_itoa(val, buf, SDB_NUM_BASE);
214  if (!sdb_array_contains(s, key, v, NULL)) {
215  if (val < 256) {
216  char *v = sdb_itoa(val, buf, 10);
217  return sdb_array_add(s, key, v, cas);
218  }
219  }
220  return sdb_array_add(s, key, v, cas);
221 }
RZ_API int sdb_array_add(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:224
const char * v
Definition: dsignal.c:12
voidpf void * buf
Definition: ioapi.h:138
RZ_API char * sdb_itoa(ut64 n, char *s, int base)
Definition: util.c:38
#define SDB_NUM_BASE
Definition: sdb.h:50
#define SDB_NUM_BUFSZ
Definition: sdb.h:51

References key, NULL, s, sdb_array_add(), sdb_array_contains(), sdb_itoa(), SDB_NUM_BASE, SDB_NUM_BUFSZ, v, and val.

Referenced by core_analysis_graph_construct_edges(), hook_mem_read(), and hook_mem_write().

◆ sdb_array_add_sorted()

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

Definition at line 231 of file array.c.

231  {
232  int lstr, lval, i, j;
233  const char *str_e, *str_lp, *str_p, *str = sdb_const_get_len(s, key, &lstr, 0);
234  char *nstr, *nstr_p, **vals;
235  const char null = '\0';
236  if (!str || !*str) {
237  str = &null;
238  lstr = 0;
239  }
240  str_e = str + lstr;
241  str_lp = str_p = str;
242  if (!val || !*val) {
243  return 1;
244  }
245  lval = strlen(val);
246  vals = sdb_fmt_array(val);
247  for (i = 0; vals[i]; i++) {
248  /* empty */
249  }
250  if (i > 1) {
251  qsort(vals, i, sizeof(ut64 *), cstring_cmp);
252  }
253  nstr_p = nstr = malloc(lstr + lval + 3);
254  if (!nstr) {
255  return 1;
256  }
257  for (i = 0; vals[i]; i++) {
258  while (str_p < str_e) {
259  if (astrcmp(vals[i], str_p) < 0) {
260  break;
261  }
262  str_p = sdb_const_anext(str_p);
263  if (!str_p) {
264  str_p = str_e;
265  }
266  }
267  memcpy(nstr_p, str_lp, str_p - str_lp);
268  nstr_p += str_p - str_lp;
269  if (str_p == str_e && str_lp != str_e) {
270  *(nstr_p++) = SDB_RS;
271  }
272  str_lp = str_p;
273  j = strlen(vals[i]);
274  memcpy(nstr_p, vals[i], j);
275  nstr_p += j;
276  *(nstr_p++) = SDB_RS;
277  }
278  if (str_lp < str_e) {
279  memcpy(nstr_p, str_lp, str_e - str_lp);
280  nstr_p += str_e - str_lp;
281  *(nstr_p) = '\0';
282  } else {
283  *(--nstr_p) = '\0';
284  }
285  sdb_set_owned(s, key, nstr, cas);
286  free(vals);
287  return 0;
288 }
lzma_index ** i
Definition: index.h:629
static int cstring_cmp(const void *a, const void *b)
Definition: array.c:45
static int astrcmp(const char *a, const char *b)
Definition: array.c:24
RZ_API char ** sdb_fmt_array(const char *list)
Definition: fmt.c:209
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * malloc(size_t size)
Definition: malloc.c:123
void qsort(void *a, size_t n, size_t es, int(*cmp)(const void *, const void *))
Definition: qsort.h:130
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
RZ_API const char * sdb_const_anext(const char *str)
Definition: util.c:206
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References astrcmp(), cstring_cmp(), free(), i, key, malloc(), memcpy(), qsort(), s, sdb_const_anext(), sdb_const_get_len(), sdb_fmt_array(), SDB_RS, sdb_set_owned(), cmd_descs_generate::str, ut64(), and val.

Referenced by sdb_querys().

◆ sdb_array_add_sorted_num()

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

Definition at line 290 of file array.c.

291  {
292  int i;
293  char valstr[SDB_NUM_BUFSZ];
294  const char *str = sdb_const_get(s, key, 0);
295  const char *n = str;
296  if (!str || !*str) {
297  return sdb_set(s, key, sdb_itoa(val, valstr, SDB_NUM_BASE), cas);
298  }
299  for (i = 0; n; i++) {
300  if (val <= sdb_atoi(n)) {
301  break;
302  }
303  n = sdb_const_anext(n);
304  }
305  return sdb_array_insert_num(s, key, n ? i : -1, val, cas);
306 }
RZ_API int sdb_array_insert_num(Sdb *s, const char *key, int idx, ut64 val, ut32 cas)
Definition: array.c:131
RZ_API const char * sdb_const_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:279
RZ_API ut64 sdb_atoi(const char *s)
Definition: util.c:88

References i, key, n, s, sdb_array_insert_num(), sdb_atoi(), sdb_const_anext(), sdb_const_get(), sdb_itoa(), SDB_NUM_BASE, SDB_NUM_BUFSZ, sdb_set(), cmd_descs_generate::str, and val.

Referenced by sdb_querys().

◆ sdb_array_append()

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

Definition at line 312 of file array.c.

313  {
314  int str_len = 0;
315  ut32 kas = cas;
316  const char *str = sdb_const_get_len(s, key, &str_len, &kas);
317  if (!val || (cas && cas != kas)) {
318  return false;
319  }
320  cas = kas;
321  if (str && *str && str_len > 0) {
322  int val_len = strlen(val);
323  char *newval = malloc(str_len + val_len + 2);
324  if (!newval) {
325  return false;
326  }
327  memcpy(newval, str, str_len);
328  newval[str_len] = SDB_RS;
329  memcpy(newval + str_len + 1, val, val_len);
330  newval[str_len + val_len + 1] = 0;
331  sdb_set_owned(s, key, newval, cas);
332  } else {
333  sdb_set(s, key, val, cas);
334  }
335  return true;
336 }
uint32_t ut32

References key, malloc(), memcpy(), s, sdb_const_get_len(), SDB_RS, sdb_set(), sdb_set_owned(), cmd_descs_generate::str, and val.

◆ sdb_array_append_num()

RZ_API bool sdb_array_append_num ( Sdb s,
const char *  key,
ut64  val,
ut32  cas 
)

Definition at line 338 of file array.c.

338  {
339  return sdb_array_set_num(s, key, -1, val, cas);
340 }
RZ_API int sdb_array_set_num(Sdb *s, const char *key, int idx, ut64 val, ut32 cas)
Definition: array.c:204

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

◆ sdb_array_compact()

RZ_API char* sdb_array_compact ( char *  p)

Definition at line 99 of file util.c.

99  {
100  char *e;
101  // remove empty elements
102  while (*p) {
103  if (!strncmp(p, ",,", 2)) {
104  p++;
105  for (e = p + 1; *e == ','; e++) {
106  };
107  memmove(p, e, strlen(e) + 1);
108  } else {
109  p++;
110  }
111  }
112  return p;
113 }
#define e(frag)

References e, and p.

◆ sdb_array_contains()

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

Definition at line 497 of file array.c.

497  {
498  if (!s || !key || !val) {
499  return false;
500  }
501  const char *next, *ptr = sdb_const_get(s, key, cas);
502  if (ptr && *ptr) {
503  size_t vlen = strlen(val);
504  while (1) {
505  next = strchr(ptr, SDB_RS);
506  size_t len = next ? (size_t)(next - ptr) : strlen(ptr);
507  if (len == vlen && !memcmp(ptr, val, len)) {
508  return true;
509  }
510  if (!next) {
511  break;
512  }
513  ptr = next + 1;
514  }
515  }
516  return false;
517 }
int size_t
Definition: sftypes.h:40

References key, len, s, sdb_const_get(), SDB_RS, and val.

Referenced by rz_analysis_class_rename_attr_raw(), rz_analysis_class_unique_attr_id_raw(), sdb_array_add(), sdb_array_add_num(), and sdb_array_contains_num().

◆ sdb_array_contains_num()

RZ_API bool sdb_array_contains_num ( Sdb s,
const char *  key,
ut64  val,
ut32 cas 
)

Definition at line 491 of file array.c.

491  {
492  char val[SDB_NUM_BUFSZ];
493  char *nval = sdb_itoa(num, val, SDB_NUM_BASE);
494  return sdb_array_contains(s, key, nval, cas);
495 }
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126

References key, num, s, sdb_array_contains(), sdb_itoa(), SDB_NUM_BASE, SDB_NUM_BUFSZ, and val.

◆ sdb_array_delete()

RZ_API int sdb_array_delete ( Sdb s,
const char *  key,
int  n,
ut32  cas 
)

Definition at line 456 of file array.c.

456  {
457  int i;
458  char *p, *n, *str = sdb_get(s, key, 0);
459  p = str;
460  if (!str || !*str) {
461  free(str);
462  return 0;
463  }
464  if (idx < 0) {
465  idx = sdb_alen(str);
466  if (idx)
467  idx--;
468  }
469  for (i = 0; i < idx; i++) {
470  if ((n = strchr(p, SDB_RS))) {
471  p = n + 1;
472  } else {
473  free(str);
474  return 0;
475  }
476  }
477  n = strchr(p, SDB_RS);
478  if (n) {
479  memmove(p, n + 1, strlen(n));
480  } else {
481  if (p != str)
482  p--; // remove tailing SDB_RS
483  *p = 0;
484  p[1] = 0;
485  }
486  sdb_set_owned(s, key, str, cas);
487  return 1;
488 }
int idx
Definition: setup.py:197
RZ_API char * sdb_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:290
RZ_API int sdb_alen(const char *str)
Definition: util.c:151

References free(), i, setup::idx, key, n, p, s, sdb_alen(), sdb_get(), SDB_RS, sdb_set_owned(), and cmd_descs_generate::str.

Referenced by sdb_array_remove(), sdb_array_remove_num(), and sdb_querys().

◆ sdb_array_get()

RZ_API char* sdb_array_get ( Sdb s,
const char *  key,
int  idx,
ut32 cas 
)

Definition at line 81 of file array.c.

81  {
82  const char *str = sdb_const_get(s, key, cas);
83  const char *p = str;
84  char *o, *n;
85  int i, len;
86  if (!str || !*str) {
87  return NULL;
88  }
89  if (idx < 0) {
90  int len = sdb_alen(str);
91  idx = -idx;
92  if (idx > len) {
93  return NULL;
94  }
95  idx = len - idx;
96  }
97  if (!idx) {
98  n = strchr(str, SDB_RS);
99  if (!n) {
100  return strdup(str);
101  }
102  len = n - str;
103  o = malloc(len + 1);
104  if (!o) {
105  return NULL;
106  }
107  memcpy(o, str, len);
108  o[len] = 0;
109  return o;
110  }
111  for (i = 0; i < idx; i++) {
112  n = strchr(p, SDB_RS);
113  if (!n)
114  return NULL;
115  p = n + 1;
116  }
117  n = strchr(p, SDB_RS);
118  if (!n) {
119  return strdup(p);
120  }
121  len = n - p;
122  o = malloc(len + 1);
123  if (o) {
124  memcpy(o, p, len);
125  o[len] = 0;
126  return o;
127  }
128  return NULL;
129 }
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")

References i, setup::idx, key, len, malloc(), memcpy(), n, NULL, p, s, sdb_alen(), sdb_const_get(), SDB_RS, cmd_descs_generate::str, and strdup().

Referenced by sdb_querys().

◆ sdb_array_get_idx()

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

◆ sdb_array_get_num()

RZ_API ut64 sdb_array_get_num ( Sdb s,
const char *  key,
int  idx,
ut32 cas 
)

Definition at line 63 of file array.c.

63  {
64  int i;
65  const char *n, *str = sdb_const_get(s, key, cas);
66  if (!str || !*str) {
67  return 0LL;
68  }
69  if (idx) {
70  for (i = 0; i < idx; i++) {
71  n = strchr(str, SDB_RS);
72  if (!n) {
73  return 0LL;
74  }
75  str = n + 1;
76  }
77  }
78  return sdb_atoi(str);
79 }

References i, setup::idx, key, n, s, sdb_atoi(), sdb_const_get(), SDB_RS, and cmd_descs_generate::str.

Referenced by rz_syscall_get_num(), and sdb_querys().

◆ sdb_array_indexof()

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

Definition at line 415 of file array.c.

416  {
417  const char *str = sdb_const_get(s, key, 0);
418  const char *n, *p = str;
419  int i;
420  for (i = 0;; i++) {
421  if (!p) {
422  break;
423  }
424  if (!astrcmp(p, val)) {
425  return i;
426  }
427  n = strchr(p, SDB_RS);
428  if (!n)
429  break;
430  p = n + 1;
431  }
432  return -1;
433 }

References astrcmp(), i, key, n, p, s, sdb_const_get(), SDB_RS, cmd_descs_generate::str, and val.

◆ sdb_array_insert()

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

Definition at line 139 of file array.c.

140  {
141  int lnstr, lstr;
142  size_t lval;
143  char *x, *ptr;
144  const char *str = sdb_const_get_len(s, key, &lstr, 0);
145  if (!str || !*str) {
146  return sdb_set(s, key, val, cas);
147  }
148  lval = strlen(val);
149  lstr--;
150  // XXX: lstr is wrongly computed in sdb_const_get_with an off-by-one
151  // we can optimize this by caching value len in memory . add
152  // sdb_const_get_size()
153  lstr = strlen(str);
154 
155  // When removing strlen this conversion should be checked
156  size_t lstr_tmp = lstr;
157  if (SZT_ADD_OVFCHK(lval, lstr_tmp) || SZT_ADD_OVFCHK(lval + lstr_tmp, 2)) {
158  return false;
159  }
160  x = malloc(lval + lstr_tmp + 2);
161  if (!x) {
162  return false;
163  }
164 
165  if (idx == -1) {
166  memcpy(x, str, lstr);
167  x[lstr] = SDB_RS;
168  memcpy(x + lstr + 1, val, lval + 1);
169  } else if (!idx) {
170  memcpy(x, val, lval);
171  x[lval] = SDB_RS;
172  memcpy(x + lval + 1, str, lstr + 1);
173  } else {
174  char *nstr = malloc(lstr + 1);
175  if (!nstr) {
176  free(x);
177  return false;
178  }
179  memcpy(nstr, str, lstr + 1);
180  ptr = (char *)Aindexof(nstr, idx);
181  if (ptr) {
182  int lptr = (nstr + lstr + 1) - ptr;
183  char *p_1 = ptr > nstr ? ptr - 1 : ptr;
184  *p_1 = 0;
185  lnstr = ptr - nstr - 1;
186  memcpy(x, nstr, lnstr);
187  x[lnstr] = SDB_RS;
188  memcpy(x + lnstr + 1, val, lval);
189  x[lnstr + lval + 1] = SDB_RS;
190  // TODO: this strlen hurts performance
191  memcpy(x + lval + 2 + lnstr, ptr, lptr); // strlen (ptr)+1);
192  free(nstr);
193  } else {
194  // this is not efficient
195  free(nstr);
196  free(x);
197  // fallback for empty buckets
198  return sdb_array_set(s, key, idx, val, cas);
199  }
200  }
201  return sdb_set_owned(s, key, x, cas);
202 }
static const char * Aindexof(const char *str, int idx)
Definition: array.c:9
RZ_API int sdb_array_set(Sdb *s, const char *key, int idx, const char *val, ut32 cas)
Definition: array.c:342
#define SZT_ADD_OVFCHK(x, y)

References Aindexof(), free(), setup::idx, key, malloc(), memcpy(), s, sdb_array_set(), sdb_const_get_len(), SDB_RS, sdb_set(), sdb_set_owned(), cmd_descs_generate::str, SZT_ADD_OVFCHK, val, and x.

Referenced by rz_agraph_add_edge_at(), sdb_array_add(), sdb_array_insert_num(), sdb_array_set(), and sdb_querys().

◆ sdb_array_insert_num()

RZ_API int sdb_array_insert_num ( Sdb s,
const char *  key,
int  idx,
ut64  val,
ut32  cas 
)

Definition at line 131 of file array.c.

132  {
133  char valstr[64];
134  return sdb_array_insert(s, key, idx,
135  sdb_itoa(val, valstr, SDB_NUM_BASE), cas);
136 }

References setup::idx, key, s, sdb_array_insert(), sdb_itoa(), SDB_NUM_BASE, and val.

Referenced by sdb_array_add_sorted_num().

◆ sdb_array_length()

RZ_API int sdb_array_length ( Sdb s,
const char *  key 
)

Definition at line 524 of file array.c.

524  {
526 }
RZ_API int sdb_alen_ignore_empty(const char *str)
Definition: util.c:167

References key, s, sdb_alen_ignore_empty(), and sdb_const_get().

Referenced by sdb_querys().

◆ sdb_array_list()

int sdb_array_list ( Sdb s,
const char *  key 
)

◆ sdb_array_pop()

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

Definition at line 589 of file array.c.

589  {
590  return sdb_array_pop_head(s, key, cas);
591 }
RZ_API char * sdb_array_pop_head(Sdb *s, const char *key, ut32 *cas)
Definition: array.c:593

References key, s, and sdb_array_pop_head().

Referenced by sdb_array_pop_num(), and sdb_querys().

◆ sdb_array_pop_head()

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

Definition at line 593 of file array.c.

593  {
594  // remove last element in
595  ut32 kas;
596  char *end, *str = sdb_get(s, key, &kas);
597  if (!str || !*str) {
598  free(str);
599  return NULL;
600  }
601  if (cas && *cas != kas) {
602  *cas = kas;
603  }
604  end = strchr(str, SDB_RS);
605  if (end) {
606  *end = 0;
607  sdb_set(s, key, end + 1, 0);
608  } else {
609  sdb_unset(s, key, 0);
610  }
611  return str;
612 }
RZ_API int sdb_unset(Sdb *s, const char *key, ut32 cas)
Definition: sdb.c:294

References test_evm::end, free(), key, NULL, s, sdb_get(), SDB_RS, sdb_set(), sdb_unset(), and cmd_descs_generate::str.

Referenced by sdb_array_pop().

◆ sdb_array_pop_num()

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

Definition at line 572 of file array.c.

572  {
573  ut64 ret;
574  char *a = sdb_array_pop(s, key, cas);
575  if (!a) {
576  if (cas) {
577  *cas = UT32_MAX; // invalid
578  }
579  return UT64_MAX;
580  }
581  if (cas) {
582  *cas = 0;
583  }
584  ret = sdb_atoi(a);
585  free(a);
586  return ret;
587 }
RZ_API char * sdb_array_pop(Sdb *s, const char *key, ut32 *cas)
Definition: array.c:589
#define UT32_MAX
Definition: rz_types_base.h:99
#define UT64_MAX
Definition: rz_types_base.h:86
#define a(i)
Definition: sha256.c:41

References a, free(), key, s, sdb_array_pop(), sdb_atoi(), UT32_MAX, ut64(), and UT64_MAX.

◆ sdb_array_pop_tail()

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

Definition at line 614 of file array.c.

614  {
615  ut32 kas;
616  char *end, *str = sdb_get(s, key, &kas);
617  if (!str || !*str) {
618  free(str);
619  return NULL;
620  }
621  if (cas && *cas != kas) {
622  *cas = kas;
623  }
624  for (end = str + strlen(str) - 1; end > str && *end != SDB_RS; end--) {
625  // nothing to see here
626  }
627  if (*end == SDB_RS) {
628  *end++ = 0;
629  }
630  sdb_set_owned(s, key, str, 0);
631  // XXX: probably wrong
632  return strdup(end);
633 }

References test_evm::end, free(), key, NULL, s, sdb_get(), SDB_RS, sdb_set_owned(), cmd_descs_generate::str, and strdup().

◆ sdb_array_prepend()

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

Definition at line 543 of file array.c.

543  {
544  if (!s || !key || !val) {
545  return false;
546  }
547  int str_len = 0;
548  ut32 kas = cas;
549  const char *str = sdb_const_get_len(s, key, &str_len, &kas);
550  if (!val || (cas && cas != kas)) {
551  return false;
552  }
553  cas = kas;
554  if (str && *str) {
555  int val_len = strlen(val);
556  char *newval = malloc(str_len + val_len + 2);
557  if (!newval) {
558  return false;
559  }
560  memcpy(newval, val, val_len);
561  newval[val_len] = SDB_RS;
562  memcpy(newval + val_len + 1, str, str_len);
563  newval[str_len + val_len + 1] = 0;
564  // TODO: optimize this because we already have allocated and strlened everything
565  sdb_set_owned(s, key, newval, cas);
566  } else {
567  sdb_set(s, key, val, cas);
568  }
569  return true;
570 }

References key, malloc(), memcpy(), s, sdb_const_get_len(), SDB_RS, sdb_set(), sdb_set_owned(), cmd_descs_generate::str, and val.

Referenced by sdb_array_push().

◆ sdb_array_prepend_num()

RZ_API bool sdb_array_prepend_num ( Sdb s,
const char *  key,
ut64  val,
ut32  cas 
)

Definition at line 537 of file array.c.

537  {
538  char buf[SDB_NUM_BUFSZ];
539  char *n = sdb_itoa(num, buf, SDB_NUM_BASE);
540  return sdb_array_push(s, key, n, cas);
541 }
RZ_API bool sdb_array_push(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:533

References key, n, num, s, sdb_array_push(), sdb_itoa(), SDB_NUM_BASE, and SDB_NUM_BUFSZ.

◆ sdb_array_push()

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

Definition at line 533 of file array.c.

533  {
534  return sdb_array_prepend(s, key, val, cas);
535 }
RZ_API bool sdb_array_prepend(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:543

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

Referenced by sdb_array_prepend_num(), sdb_array_push_num(), and sdb_querys().

◆ sdb_array_push_num()

RZ_API int sdb_array_push_num ( Sdb s,
const char *  key,
ut64  num,
ut32  cas 
)

Definition at line 528 of file array.c.

528  {
530  return sdb_array_push(s, key, n, cas);
531 }

References key, n, num, s, sdb_array_push(), sdb_itoa(), SDB_NUM_BASE, and SDB_NUM_BUFSZ.

Referenced by core_analysis_graph_construct_nodes().

◆ sdb_array_remove()

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

Definition at line 436 of file array.c.

437  {
438  const char *str = sdb_const_get(s, key, 0);
439  const char *n, *p = str;
440  int idx;
441  if (p) {
442  for (idx = 0;; idx++) {
443  if (!astrcmp(p, val)) {
444  return sdb_array_delete(s, key, idx, cas);
445  }
446  n = strchr(p, SDB_RS);
447  if (!n) {
448  break;
449  }
450  p = n + 1;
451  }
452  }
453  return 0;
454 }
RZ_API int sdb_array_delete(Sdb *s, const char *key, int idx, ut32 cas)
Definition: array.c:456

References astrcmp(), setup::idx, key, n, p, s, sdb_array_delete(), sdb_const_get(), SDB_RS, cmd_descs_generate::str, and val.

Referenced by rz_agraph_del_edge(), rz_agraph_del_node(), rz_analysis_class_delete_attr_raw(), rz_analysis_class_rename_attr_raw(), and sdb_querys().

◆ sdb_array_remove_num()

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

Definition at line 394 of file array.c.

394  {
395  const char *n, *p, *str = sdb_const_get(s, key, 0);
396  int idx = 0;
397  ut64 num;
398  if (str) {
399  for (p = str;; idx++) {
400  num = sdb_atoi(p);
401  if (num == val) {
402  return sdb_array_delete(s, key, idx, cas);
403  }
404  n = strchr(p, SDB_RS);
405  if (!n) {
406  break;
407  }
408  p = n + 1;
409  }
410  }
411  return 0;
412 }

References setup::idx, key, n, num, p, s, sdb_array_delete(), sdb_atoi(), sdb_const_get(), SDB_RS, cmd_descs_generate::str, ut64(), and val.

◆ sdb_array_set()

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

Definition at line 342 of file array.c.

343  {
344  int lstr, lval, len;
345  const char *usr, *str = sdb_const_get_len(s, key, &lstr, 0);
346  char *ptr;
347 
348  if (!str || !*str) {
349  return sdb_set(s, key, val, cas);
350  }
351  // XXX: should we cache sdb_alen value inside kv?
352  len = sdb_alen(str);
353  lstr--;
354  if (idx < 0 || idx == len) { // append
355  return sdb_array_insert(s, key, -1, val, cas);
356  }
357  lval = strlen(val);
358  if (idx > len) {
359  int ret, i, ilen = idx - len;
360  char *newkey = malloc(ilen + lval + 1);
361  if (!newkey) {
362  return 0;
363  }
364  for (i = 0; i < ilen; i++) {
365  newkey[i] = SDB_RS;
366  }
367  memcpy(newkey + i, val, lval + 1);
368  ret = sdb_array_insert(s, key, -1, newkey, cas);
369  free(newkey);
370  return ret;
371  }
372  // lstr = strlen (str);
373  ptr = (char *)Aindexof(str, idx);
374  if (ptr) {
375  int diff = ptr - str;
376  char *nstr = malloc(lstr + lval + 2);
377  if (!nstr) {
378  return false;
379  }
380  ptr = nstr + diff;
381  // memcpy (nstr, str, lstr+1);
382  memcpy(nstr, str, diff);
383  memcpy(ptr, val, lval + 1);
384  usr = Aindexof(str, idx + 1);
385  if (usr) {
386  ptr[lval] = SDB_RS;
387  strcpy(ptr + lval + 1, usr);
388  }
389  return sdb_set_owned(s, key, nstr, 0);
390  }
391  return 0;
392 }

References Aindexof(), free(), i, setup::idx, key, len, malloc(), memcpy(), s, sdb_alen(), sdb_array_insert(), sdb_const_get_len(), SDB_RS, sdb_set(), sdb_set_owned(), cmd_descs_generate::str, and val.

Referenced by sdb_array_insert(), sdb_array_set_num(), sdb_array_unset(), and sdb_querys().

◆ sdb_array_set_num()

RZ_API int sdb_array_set_num ( Sdb s,
const char *  key,
int  idx,
ut64  val,
ut32  cas 
)

Definition at line 204 of file array.c.

205  {
206  char valstr[SDB_NUM_BUFSZ];
207  return sdb_array_set(s, key, idx, sdb_itoa(val, valstr, SDB_NUM_BASE),
208  cas);
209 }

References setup::idx, key, s, sdb_array_set(), sdb_itoa(), SDB_NUM_BASE, SDB_NUM_BUFSZ, and val.

Referenced by sdb_array_append_num(), and sdb_querys().

◆ sdb_array_size()

RZ_API int sdb_array_size ( Sdb s,
const char *  key 
)

Definition at line 519 of file array.c.

519  {
520  return sdb_alen(sdb_const_get(s, key, 0));
521 }

References key, s, sdb_alen(), and sdb_const_get().

◆ sdb_array_sort()

RZ_API void sdb_array_sort ( Sdb s,
const char *  key,
ut32  cas 
)

Definition at line 635 of file array.c.

635  {
636  char *nstr, *str, **strs;
637  int lstr, j, i;
638  str = sdb_get_len(s, key, &lstr, 0);
639  if (!str) {
640  return;
641  }
642  if (!*str) {
643  free(str);
644  return;
645  }
646  strs = sdb_fmt_array(str);
647  for (i = 0; strs[i]; i++) {
648  // nothing to see here
649  }
650  qsort(strs, i, sizeof(char *), cstring_cmp);
651  nstr = str;
652  for (i = 0; strs[i]; i++) {
653  j = strlen(strs[i]);
654  memcpy(nstr, strs[i], j);
655  nstr += j;
656  *(nstr++) = SDB_RS;
657  }
658  if (nstr > str) {
659  *(--nstr) = '\0';
660  } else {
661  *nstr = '\0';
662  }
663  sdb_set_owned(s, key, str, cas);
664  free(strs);
665 }
RZ_API char * sdb_get_len(Sdb *s, const char *key, int *vlen, ut32 *cas)
Definition: sdb.c:285

References cstring_cmp(), free(), i, key, memcpy(), qsort(), s, sdb_fmt_array(), sdb_get_len(), SDB_RS, sdb_set_owned(), and cmd_descs_generate::str.

Referenced by sdb_querys().

◆ sdb_array_sort_num()

RZ_API void sdb_array_sort_num ( Sdb s,
const char *  key,
ut32  cas 
)

Definition at line 667 of file array.c.

667  {
668  char *ret, *nstr;
669 
670  char *str = sdb_get(s, key, 0);
671  if (!str) {
672  return;
673  }
674  if (!*str) {
675  free(str);
676  return;
677  }
678  ut64 *nums = sdb_fmt_array_num(str);
679  free(str);
680  if (!nums) {
681  return;
682  }
683 
684  qsort(nums + 1, (int)*nums, sizeof(ut64), int_cmp);
685 
686  nstr = malloc(*nums + 1);
687  if (!nstr) {
688  free(nums);
689  return;
690  }
691  memset(nstr, 'q', *nums);
692  nstr[*nums] = '\0';
693 
694  ret = sdb_fmt_tostr(nums + 1, nstr);
695  sdb_set_owned(s, key, ret, cas);
696 
697  free(nstr);
698  free(nums);
699  return;
700 }
static int int_cmp(const void *a, const void *b)
Definition: array.c:51
RZ_API char * sdb_fmt_tostr(void *p, const char *fmt)
Definition: fmt.c:43
RZ_API ut64 * sdb_fmt_array_num(const char *list)
Definition: fmt.c:184
return memset(p, 0, total)

References free(), int_cmp(), key, malloc(), memset(), qsort(), s, sdb_fmt_array_num(), sdb_fmt_tostr(), sdb_get(), sdb_set_owned(), cmd_descs_generate::str, and ut64().

Referenced by sdb_querys().

◆ sdb_array_unset()

RZ_API int sdb_array_unset ( Sdb s,
const char *  key,
int  n,
ut32  cas 
)

Definition at line 308 of file array.c.

308  {
309  return sdb_array_set(s, key, idx, "", cas);
310 }

References setup::idx, key, s, and sdb_array_set().

◆ sdb_aslice()

RZ_API char* sdb_aslice ( char *  out,
int  from,
int  to 
)

Definition at line 116 of file util.c.

116  {
117  int len, idx = 0;
118  char *str = NULL;
119  char *end = NULL;
120  char *p = out;
121  if (from >= to) {
122  return NULL;
123  }
124  while (*p) {
125  if (!str && idx == from) {
126  str = p;
127  }
128  if (idx == to) {
129  end = p;
130  break;
131  }
132  if (*p == ',') {
133  idx++;
134  }
135  p++;
136  }
137  if (str) {
138  if (!end) {
139  end = str + strlen(str);
140  }
141  len = (size_t)(end - str);
142  memmove(out, str, len);
143  out[len] = 0;
144  return out;
145  }
146  return NULL;
147 }
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr from
Definition: sfsocketcall.h:123
static struct sockaddr static addrlen static backlog const void static flags void struct sockaddr socklen_t static fromlen const void const struct sockaddr to
Definition: sfsocketcall.h:125

References test_evm::end, from, setup::idx, len, NULL, out, p, cmd_descs_generate::str, and to.

◆ sdb_atoi()

RZ_API ut64 sdb_atoi ( const char *  s)

Definition at line 88 of file util.c.

88  {
89  char *p;
90  ut64 ret;
91  if (!s || *s == '-') {
92  return 0LL;
93  }
94  ret = strtoull(s, &p, 0);
95  return p ? ret : 0LL;
96 }

References p, s, and ut64().

Referenced by __computeTotal(), deserialize_checkpoints_cb(), deserialize_memory_cb(), deserialize_registers_cb(), rz_core_analysis_esil_init_mem(), sdb_array_add_sorted_num(), sdb_array_get_num(), sdb_array_pop_num(), sdb_array_remove_num(), sdb_fmt_array_num(), sdb_fmt_tobin(), sdb_num_get(), sdb_num_max(), sdb_num_min(), and sdb_querys().

◆ sdb_bool_get()

RZ_API bool sdb_bool_get ( Sdb db,
const char *  str,
ut32 cas 
)

Definition at line 76 of file num.c.

76  {
77  const char *b = sdb_const_get(db, str, cas);
78  return b && (!strcmp(b, "1") || !strcmp(b, "true"));
79 }
#define b(i)
Definition: sha256.c:42

References b, sdb_const_get(), and cmd_descs_generate::str.

Referenced by get_callable_type(), rz_analysis_is_noreturn(), and rz_analysis_noreturn_at_addr().

◆ sdb_bool_set()

RZ_API int sdb_bool_set ( Sdb db,
const char *  str,
bool  v,
ut32  cas 
)

Definition at line 72 of file num.c.

72  {
73  return sdb_set(db, str, v ? "true" : "false", cas);
74 }

References sdb_set(), cmd_descs_generate::str, and v.

Referenced by agraph_sdb_init(), rz_analysis_noreturn_add(), and save_callable().

◆ 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

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 }
static int value
Definition: cmd_api.c:93

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_anext()

RZ_API const char* sdb_const_anext ( const char *  str)

Definition at line 206 of file util.c.

206  {
207  const char *p = strchr(str, SDB_RS);
208  return p ? p + 1 : NULL;
209 }

References NULL, p, SDB_RS, and cmd_descs_generate::str.

Referenced by sdb_array_add_sorted(), and sdb_array_add_sorted_num().

◆ 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 ut64 sdb_now(void)
Definition: util.c:211
#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
Definition: sdbht.h:14
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
char * dst
Definition: lz4.h:724
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 }
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
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_decode()

RZ_API ut8* sdb_decode ( const char *  in,
int len 
)

Definition at line 37 of file base64.c.

37  {
38  ut8 *out;
39  ut32 size;
40  int olen, ilen;
41  if (len) {
42  *len = 0;
43  }
44  if (!in) {
45  return NULL;
46  }
47  ilen = strlen(in);
48  if (!ilen) {
49  return NULL;
50  }
51  size = (ilen * 3) + 16;
52  if (size < (ut32)ilen) {
53  return NULL;
54  }
55  out = calloc(1, size);
56  if (!out) {
57  return NULL;
58  }
59  olen = sdb_decode_raw(out, in, ilen);
60  if (!olen) {
61  free(out);
62  return NULL;
63  }
64  out[olen] = 0;
65  if (len) {
66  *len = olen;
67  }
68  return out;
69 }
RZ_API int sdb_decode_raw(ut8 *bout, const char *bin, int len)
Definition: base64.c:14
const lzma_allocator const uint8_t * in
Definition: block.h:527
voidpf void uLong size
Definition: ioapi.h:138
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102

References calloc(), free(), in, len, NULL, out, and sdb_decode_raw().

Referenced by base64decode(), bin_pe_versioninfo(), compareString(), deserialize_checkpoints_cb(), foreach_list_cb(), meta_variable_comment_append(), rz_comment_unique_handler(), sdb_fmt_tobin(), and sdb_querys().

◆ sdb_decode_raw()

RZ_API int sdb_decode_raw ( ut8 bout,
const char *  bin,
int  len 
)

Definition at line 14 of file base64.c.

14  {
15  return rz_base64_decode(bout, bin, len);
16 }
RZ_API int rz_base64_decode(ut8 *bout, const char *bin, int len)
Definition: ubase64.c:48
Definition: malloc.c:26

References len, and rz_base64_decode().

Referenced by sdb_decode().

◆ sdb_diff()

RZ_API bool sdb_diff ( Sdb a,
Sdb b,
SdbDiffCallback  cb,
void *  cb_user 
)

Definition at line 159 of file diff.c.

159  {
160  SdbDiffCtx ctx;
161  ctx.a = a;
162  ctx.b = b;
163  ctx.equal = true;
164  ctx.cb = cb;
165  ctx.cb_user = cb_user;
166  ctx.path = ls_new();
167  if (!ctx.path) {
168  return false;
169  }
170  sdb_diff_ctx(&ctx);
171  ls_free(ctx.path);
172  return ctx.equal;
173 }
RZ_API SdbList * ls_new(void)
Definition: ls.c:16
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
static void sdb_diff_ctx(SdbDiffCtx *ctx)
Definition: diff.c:117
static const char * cb[]
Definition: z80_tab.h:176

References a, b, cb, ls_free(), ls_new(), and sdb_diff_ctx().

Referenced by dbdiff().

◆ sdb_diff_format()

RZ_API int sdb_diff_format ( char *  str,
int  size,
const SdbDiff diff 
)

Definition at line 6 of file diff.c.

6  {
7  int r = 0;
8 #define APPENDF(...) \
9  do { \
10  int sr = snprintf(str, size, __VA_ARGS__); \
11  if (sr < 0) { \
12  return sr; \
13  } \
14  r += sr; \
15  if (sr >= size) { \
16  /* no space left, only measure from now on */ \
17  str = NULL; \
18  size = 0; \
19  } else { \
20  str += sr; \
21  size -= sr; \
22  } \
23  } while (0)
24 
25  APPENDF("%c%s ", diff->add ? '+' : '-', diff->v ? " " : "NS");
26 
27  SdbListIter *it;
28  const char *component;
29  ls_foreach (diff->path, it, component) {
30  APPENDF("%s/", component);
31  }
32 
33  if (diff->v) {
34  APPENDF("%s=%s", diff->k, diff->v);
35  } else {
36  APPENDF("%s", diff->k);
37  }
38 
39 #undef APPENDF
40  return r;
41 }
#define r
Definition: crypto_rc6.c:12
const char * k
Definition: sdb.h:137
const char * v
Definition: sdb.h:138
const SdbList * path
Definition: sdb.h:136
bool add
Definition: sdb.h:139
#define APPENDF(...)

References sdb_diff_t::add, APPENDF, sdb_diff_t::k, ls_foreach, sdb_diff_t::path, r, and sdb_diff_t::v.

Referenced by dbdiff_cb().

◆ sdb_disk_create()

RZ_API bool sdb_disk_create ( Sdb s)

Definition at line 17 of file disk.c.

17  {
18  int nlen;
19  char *str;
20  const char *dir;
21  if (!s || s->fdump >= 0) {
22  return false; // cannot re-create
23  }
24  if (!s->dir && s->name) {
25  s->dir = strdup(s->name);
26  }
27  dir = s->dir ? s->dir : "./";
28  RZ_FREE(s->ndump);
29  nlen = strlen(dir);
30  str = malloc(nlen + 5);
31  if (!str) {
32  return false;
33  }
34  memcpy(str, dir, nlen + 1);
35  char *dirname = rz_file_dirname(str);
36  if (!dirname) {
37  free(str);
38  return false;
39  }
40  rz_sys_mkdirp(dirname);
41  free(dirname);
42  memcpy(str + nlen, ".tmp", 5);
43  if (s->fdump != -1) {
44  close(s->fdump);
45  }
46 #if __WINDOWS__
47  wchar_t *wstr = rz_utf8_to_utf16(str);
48  if (wstr) {
49  s->fdump = _wopen(wstr, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, SDB_MODE);
50  free(wstr);
51  } else {
52  s->fdump = -1;
53  }
54 #else
55  s->fdump = open(str, O_BINARY | O_RDWR | O_CREAT | O_TRUNC, SDB_MODE);
56 #endif
57  if (s->fdump == -1) {
58  eprintf("sdb: Cannot open '%s' for writing.\n", str);
59  free(str);
60  return false;
61  }
62  cdb_make_start(&s->m, s->fdump);
63  s->ndump = str;
64  return true;
65 }
int cdb_make_start(struct cdb_make *c, int fd)
Definition: cdb_make.c:37
#define O_BINARY
Definition: cpipe.c:13
#define eprintf(x, y...)
Definition: rlcc.c:7
RZ_API char * rz_file_dirname(const char *path)
Definition: file.c:120
RZ_API bool rz_sys_mkdirp(const char *dir)
Definition: sys.c:691
#define RZ_FREE(x)
Definition: rz_types.h:369
#define SDB_MODE
Definition: sdb.h:42
#define O_CREAT
Definition: sftypes.h:489
#define O_RDWR
Definition: sftypes.h:488
#define O_TRUNC
Definition: sftypes.h:492

References cdb_make_start(), close, eprintf, free(), malloc(), memcpy(), O_BINARY, O_CREAT, O_RDWR, O_TRUNC, rz_file_dirname(), RZ_FREE, rz_sys_mkdirp(), s, SDB_MODE, cmd_descs_generate::str, and strdup().

Referenced by sdb_sync().

◆ sdb_disk_finish()

RZ_API bool sdb_disk_finish ( Sdb s)

Definition at line 79 of file disk.c.

79  {
80  bool reopen = false, ret = true;
81  IFRET(!cdb_make_finish(&s->m));
82 #if HAVE_HEADER_SYS_MMAN_H
83  IFRET(fsync(s->fdump));
84 #endif
85  IFRET(close(s->fdump));
86  s->fdump = -1;
87  // close current fd to avoid sharing violations
88  if (s->fd != -1) {
89  close(s->fd);
90  s->fd = -1;
91  reopen = true;
92  }
93 #if __WINDOWS__
94  wchar_t *ndump_ = rz_utf8_to_utf16(s->ndump);
95  wchar_t *dir_ = rz_utf8_to_utf16(s->dir);
96 
97  if (!MoveFileExW(ndump_, dir_, MOVEFILE_REPLACE_EXISTING)) {
98  rz_sys_perror("MoveFileExW SDB file to finale location");
99  }
100  free(ndump_);
101  free(dir_);
102 #else
103  if (s->ndump && s->dir) {
104  IFRET(rename(s->ndump, s->dir));
105  }
106 #endif
107  free(s->ndump);
108  s->ndump = NULL;
109  // reopen if was open before
110  reopen = true; // always reopen if possible
111  if (reopen) {
112  int rr = sdb_open(s, s->dir);
113  if (ret && rr < 0) {
114  ret = false;
115  }
116  cdb_init(&s->db, s->fd);
117  }
118  return ret;
119 }
bool cdb_init(struct cdb *c, int fd)
Definition: cdb.c:51
int cdb_make_finish(struct cdb_make *c)
Definition: cdb_make.c:124
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 fsync
Definition: sflib.h:79
#define IFRET(x)
Definition: disk.c:76
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync rename
Definition: sflib.h:69
#define rz_sys_perror(x)
Definition: rz_types.h:336
RZ_API int sdb_open(Sdb *s, const char *file)
Definition: sdb.c:383

References cdb_init(), cdb_make_finish(), close, rz_socket_t::fd, free(), fsync, IFRET, NULL, rename, rz_sys_perror, s, and sdb_open().

Referenced by sdb_sync().

◆ sdb_disk_insert()

RZ_API bool sdb_disk_insert ( Sdb s,
const char *  key,
const char *  val 
)

Definition at line 67 of file disk.c.

67  {
68  struct cdb_make *c = &s->m;
69  if (!key || !val) {
70  return false;
71  }
72  // if (!*val) return 0; //undefine variable if no value
73  return cdb_make_add(c, key, strlen(key), val, strlen(val));
74 }
int cdb_make_add(struct cdb_make *c, const char *key, ut32 keylen, const char *data, ut32 datalen)
Definition: cdb_make.c:108
#define c(i)
Definition: sha256.c:43

References c, cdb_make_add(), key, s, and val.

Referenced by _insert_into_disk(), insertkeys(), and sdb_sync().

◆ sdb_disk_unlink()

RZ_API bool sdb_disk_unlink ( Sdb s)

Definition at line 121 of file disk.c.

121  {
122  return (s->dir && *(s->dir) && unlink(s->dir) != -1);
123 }
static static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41

References s, and unlink.

Referenced by sdb_unlink().

◆ 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 }
const char * k
Definition: dsignal.c:11

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

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

◆ sdb_dump_next()

RZ_API SdbKv* sdb_dump_next ( Sdb s)

◆ sdb_encode()

RZ_API char* sdb_encode ( const ut8 bin,
int  len 
)

Definition at line 18 of file base64.c.

18  {
19  char *out;
20  if (!bin) {
21  return NULL;
22  }
23  if (len < 0) {
24  len = strlen((const char *)bin);
25  }
26  if (!len) {
27  return strdup("");
28  }
29  out = calloc(8 + (len * 2), sizeof(char));
30  if (!out) {
31  return NULL;
32  }
34  return out;
35 }
RZ_API void sdb_encode_raw(char *bout, const ut8 *bin, int len)
Definition: base64.c:10

References calloc(), len, NULL, out, sdb_encode_raw(), and strdup().

Referenced by base64encode(), core_analysis_graph_nodes(), meta_variable_comment_print(), Pe_r_bin_store_string(), Pe_r_bin_store_string_table(), rz_agraph_add_node_with_color(), rz_core_meta_print(), rz_core_print_disasm_json(), sdb_fmt_tostr(), sdb_querys(), sdb_set_enc(), and serialize_checkpoints().

◆ sdb_encode_raw()

RZ_API void sdb_encode_raw ( char *  bout,
const ut8 bin,
int  len 
)

Definition at line 10 of file base64.c.

10  {
11  rz_base64_encode(bout, bin, len);
12 }
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz)
Definition: ubase64.c:81

References len, and rz_base64_encode().

Referenced by sdb_encode().

◆ 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 }
bool cdb_read(struct cdb *c, char *buf, ut32 len, ut32 pos)
Definition: cdb.c:89
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 }
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 }
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_fmt()

RZ_API char* sdb_fmt ( const char *  fmt,
  ... 
)

◆ sdb_fmt_array()

RZ_API char** sdb_fmt_array ( const char *  list)

Definition at line 209 of file fmt.c.

209  {
210  char *_s, **retp, **ret = NULL;
211  const char *next, *ptr = list;
212  if (list && *list) {
213  int len = sdb_alen(list);
214  retp = ret = (char **)malloc(2 * strlen(list) +
215  ((len + 1) * sizeof(char *)) + 1);
216  _s = (char *)ret + ((len + 1) * sizeof(char *));
217  if (!ret) {
218  return NULL;
219  }
220  do {
221  const char *str = sdb_anext2(ptr, &next);
222  int slen = next ? (next - str) - 1 : (int)strlen(str) + 1;
223  memcpy(_s, str, slen);
224  _s[slen] = 0;
225  *retp++ = _s;
226  _s += slen + 1;
227  ptr = next;
228  } while (next);
229  *retp = NULL;
230  }
231  return ret;
232 }
static const char * sdb_anext2(const char *str, const char **next)
Definition: fmt.c:170
static void list(RzEgg *egg)
Definition: rz-gg.c:52

References len, list(), malloc(), memcpy(), NULL, sdb_alen(), sdb_anext2(), and cmd_descs_generate::str.

Referenced by sdb_array_add_sorted(), and sdb_array_sort().

◆ sdb_fmt_array_num()

RZ_API ut64* sdb_fmt_array_num ( const char *  list)

Definition at line 184 of file fmt.c.

184  {
185  ut64 *retp, *ret = NULL;
186  ut32 size;
187  const char *next, *ptr = list;
188  if (list && *list) {
189  ut32 len = (ut32)sdb_alen(list);
190  size = sizeof(ut64) * (len + 1);
191  if (size < len) {
192  return NULL;
193  }
194  retp = ret = (ut64 *)malloc(size);
195  if (!ret) {
196  return NULL;
197  }
198  *retp++ = len;
199  do {
200  const char *str = sdb_anext2(ptr, &next);
201  ut64 n = sdb_atoi(str);
202  *retp++ = n;
203  ptr = next;
204  } while (next);
205  }
206  return ret;
207 }

References len, list(), malloc(), n, NULL, sdb_alen(), sdb_anext2(), sdb_atoi(), cmd_descs_generate::str, and ut64().

Referenced by sdb_array_sort_num().

◆ sdb_fmt_free()

RZ_API void sdb_fmt_free ( void *  p,
const char *  fmt 
)

Definition at line 128 of file fmt.c.

128  {
129  int n, len = 0;
130  for (; *fmt; fmt++) {
131  n = 4;
132  switch (*fmt) {
133  case 'p': // TODO: leak or wat
134  case 'b':
135  case 'h':
136  case 'd':
137  /* do nothing */
138  break;
139  case 'q':
140  n = 8;
141  break;
142  case 'z':
143  case 's':
144  free((void *)*((char **)((ut8 *)stru + len)));
145  break;
146  }
147  len += RZ_MAX((long)sizeof(void *), n); // align
148  }
149 }
#define RZ_MAX(x, y)

References free(), len, n, and RZ_MAX.

◆ sdb_fmt_init()

RZ_API char RZ_API int sdb_fmt_init ( void *  p,
const char *  fmt 
)

Definition at line 151 of file fmt.c.

151  {
152  int len = 0;
153  for (; *fmt; fmt++) {
154  switch (*fmt) {
155  case 'b': len += sizeof(ut8); break; // 1
156  case 'h': len += sizeof(short); break; // 2
157  case 'd': len += sizeof(ut32); break; // 4
158  case 'q': len += sizeof(ut64); break; // 8
159  case 'z': len += sizeof(char *); break; // void*
160  case 's': len += sizeof(char *); break; // void*
161  case 'p': len += sizeof(char *); break; // void *
162  }
163  }
164  if (p) {
165  memset(p, 0, len);
166  }
167  return len;
168 }
#define ut8
Definition: dcpu16.h:8

References len, memset(), p, ut64(), and ut8.

◆ sdb_fmt_tobin()

RZ_API int sdb_fmt_tobin ( const char *  _str,
const char *  fmt,
void *  stru 
)

Definition at line 84 of file fmt.c.

84  {
85  int n, idx = 0, items = 0;
86  char *stru8 = (char *)stru;
87  char *next, *str, *ptr, *word, *e_str;
88  if (!_str || !*_str || !fmt) {
89  return 0;
90  }
91  str = ptr = strdup(_str);
92  for (; *fmt; fmt++) {
93  word = sdb_anext(ptr, &next);
94  if (!word || !*word) {
95  break;
96  }
97  items++;
98  n = 4; // ALIGN
99  switch (*fmt) {
100  case 'b': *((ut8 *)(stru8 + idx)) = (ut8)sdb_atoi(word); break;
101  case 'd': *((int *)(stru8 + idx)) = (int)sdb_atoi(word); break;
102  case 'q':
103  *((ut64 *)(stru8 + idx)) = sdb_atoi(word);
104  n = 8;
105  break;
106  case 'h': *((short *)(stru8 + idx)) = (short)sdb_atoi(word); break;
107  case 's':
108  e_str = (char *)sdb_decode(word, 0);
109  *((char **)(stru8 + idx)) = e_str ? e_str : strdup(word);
110  break;
111  case 'z':
112  *((char **)(stru8 + idx)) = (char *)strdup(word);
113  break;
114  case 'p':
115  *((void **)(stru8 + idx)) = (void *)(size_t)sdb_atoi(word);
116  break;
117  }
118  idx += RZ_MAX((long)sizeof(void *), n); // align
119  if (!next) {
120  break;
121  }
122  ptr = next;
123  }
124  free(str);
125  return items;
126 }
RZ_API ut8 * sdb_decode(const char *in, int *len)
Definition: base64.c:37

References free(), setup::idx, n, RZ_MAX, sdb_anext(), sdb_atoi(), sdb_decode(), cmd_descs_generate::str, strdup(), and ut64().

◆ sdb_fmt_tostr()

RZ_API char* sdb_fmt_tostr ( void *  stru,
const char *  fmt 
)

Definition at line 43 of file fmt.c.

43  {
44  char buf[128], *e_str, *out = NULL;
45  int n, len = 0;
46  if (!p || !fmt) {
47  return NULL;
48  }
49  for (; *fmt; fmt++) {
50  n = 4;
51  switch (*fmt) {
52  case 'b':
53  concat(sdb_itoa((ut64) * ((ut8 *)p + len), buf, 10));
54  break;
55  case 'h':
56  concat(sdb_itoa((ut64) * ((short *)((ut8 *)p + len)), buf, 10));
57  break;
58  case 'd':
59  concat(sdb_itoa((ut64) * ((int *)((ut8 *)p + len)), buf, 10));
60  break;
61  case 'q':
62  concat(sdb_itoa(*((ut64 *)((ut8 *)p + len)), buf, 10));
63  n = 8;
64  break;
65  case 'z':
66  concat((char *)p + len);
67  break;
68  case 's':
69  e_str = sdb_encode((const ut8 *)*((char **)((ut8 *)p + len)), -1);
70  concat(e_str);
71  free(e_str);
72  break;
73  case 'p':
74  concat(sdb_itoa((ut64) * ((size_t *)((ut8 *)p + len)), buf, 16));
75  n = sizeof(size_t);
76  break;
77  }
78  len += RZ_MAX((long)sizeof(void *), n); // align
79  }
80  return out;
81 }
RZ_API char * sdb_encode(const ut8 *bin, int len)
Definition: base64.c:18
#define concat(x)
Definition: fmt.c:11

References concat, free(), len, n, NULL, out, p, RZ_MAX, sdb_encode(), sdb_itoa(), and ut64().

Referenced by sdb_array_sort_num().

◆ 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 }
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 char * sdbkv_key(const SdbKv *kv)
Definition: sdbht.h:21

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_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 }
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_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_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  sorted 
)

Definition at line 710 of file sdb.c.

710  {
712  _match_sdb_user o = { expr, list, single };
714  return list;
715 }
static RzNumCalcValue expr(RzNum *, RzNumCalc *, int)
Definition: calc.c:167
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_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_hash()

RZ_API ut32 sdb_hash ( const char *  key)

Definition at line 22 of file util.c.

22  {
23  return sdb_hash_len(s, NULL);
24 }
RZ_API ut32 sdb_hash_len(const char *s, ut32 *len)
Definition: util.c:7

References NULL, s, and sdb_hash_len().

Referenced by __hashify(), cdb_make_add(), HtName_(), rz_il_vm_init(), rz_line_ns_completion_result_new(), sdb_exists(), sdb_expire_set(), sdb_ns(), sdb_ns_set(), sdb_querys(), sdb_set_internal(), and sigdb_entry_hash().

◆ sdb_hash_byte()

RZ_API ut8 sdb_hash_byte ( const char *  s)

Definition at line 26 of file util.c.

26  {
27  const ut32 hash = sdb_hash_len(s, NULL);
28  const ut8 *h = (const ut8 *)&hash;
29  return h[0] ^ h[1] ^ h[2] ^ h[3];
30 }
#define h(i)
Definition: sha256.c:48

References h, NULL, s, and sdb_hash_len().

◆ sdb_hash_len()

RZ_API ut32 sdb_hash_len ( const char *  key,
ut32 len 
)

Definition at line 7 of file util.c.

7  {
9  ut32 count = 0;
10  if (s) {
11  while (*s) {
12  h = (h + (h << 5)) ^ *s++;
13  count++;
14  }
15  }
16  if (len) {
17  *len = count;
18  }
19  return h;
20 }
#define CDB_HASHSTART
Definition: cdb.h:18

References CDB_HASHSTART, count, h, len, and s.

Referenced by sdb_hash(), and sdb_hash_byte().

◆ 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 SdbListIter * ls_append(SdbList *list, void *data)
Definition: ls.c:200
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_isnum()

RZ_API int sdb_isnum ( const char *  s)

Definition at line 216 of file util.c.

216  {
217  const char vs = *s;
218  return ((vs == '-' || vs == '+') || (vs >= '0' && vs <= '9'));
219 }

References s.

Referenced by sdb_querys(), and sdb_type().

◆ sdb_itoa()

RZ_API char* sdb_itoa ( ut64  n,
char *  s,
int  base 
)

Definition at line 38 of file util.c.

38  {
39  static const char *lookup = "0123456789abcdef";
40  const int imax = 62;
41  int i = imax, copy_string = 1, alloc = 0;
42  if (s) {
43  *s = 0;
44  } else {
45  alloc = 1;
46  s = malloc(64);
47  if (!s) {
48  return NULL;
49  }
50  }
51  if (base < 0) {
52  copy_string = 0;
53  base = -base;
54  }
55  if ((base > 16) || (base < 1)) {
56  if (alloc) {
57  free(s);
58  }
59  return NULL;
60  }
61  if (!n) {
62  strcpy(s, "0");
63  return s;
64  }
65  s[imax + 1] = '\0';
66  if (base <= 10) {
67  for (; n && i > 0; n /= base) {
68  s[i--] = (n % base) + '0';
69  }
70  } else {
71  for (; n && i > 0; n /= base) {
72  s[i--] = lookup[(n % base)];
73  }
74  if (i != imax) {
75  s[i--] = 'x';
76  }
77  s[i--] = '0';
78  }
79  if (copy_string || alloc) {
80  // unnecessary memmove in case we use the return value
81  // return s + i + 1;
82  memmove(s, s + i + 1, strlen(s + i + 1) + 1);
83  return s;
84  }
85  return s + i + 1;
86 }
#define copy_string(type_code_str, str_for_copy)

References copy_string, free(), i, malloc(), n, NULL, and s.

Referenced by rz_core_analysis_esil_init_mem(), rz_open_binary_list_ascii_handler(), rz_table_tofancystring(), rz_table_tosimplestring(), sdb_array_add_num(), sdb_array_add_sorted_num(), sdb_array_contains_num(), sdb_array_insert_num(), sdb_array_prepend_num(), sdb_array_push_num(), sdb_array_set_num(), sdb_fmt_tostr(), sdb_itoca(), sdb_lock(), sdb_num_add(), sdb_num_set(), sdb_querys(), signature(), and xrefs_graph().

◆ sdb_itoca()

RZ_API const char* sdb_itoca ( ut64  n)

Definition at line 32 of file util.c.

32  {
33  return sdb_itoa(n, sdb_fmt(NULL), 16);
34 }
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API char * sdb_itoa(ut64 n, char *s, int base)
Definition: util.c:38

References n, NULL, sdb_fmt(), and sdb_itoa().

◆ sdb_journal_clear()

RZ_API bool sdb_journal_clear ( Sdb s)

Definition at line 96 of file journal.c.

96  {
97  if (s->journal != -1) {
98  return !ftruncate(s->journal, 0);
99  }
100  return false;
101 }
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 ftruncate
Definition: sflib.h:113

References ftruncate, and s.

Referenced by sdb_config(), and sdb_sync().

◆ sdb_journal_close()

RZ_API bool sdb_journal_close ( Sdb s)

Definition at line 13 of file journal.c.

13  {
14  if (s->journal == -1) {
15  return false;
16  }
17  close(s->journal);
18  s->journal = -1;
20  return true;
21 }
static const char * sdb_journal_filename(Sdb *s)
Definition: journal.c:7

References close, s, sdb_journal_filename(), and unlink.

Referenced by sdb_config(), sdb_fini(), and sdb_journal_unlink().

◆ sdb_journal_load()

RZ_API int sdb_journal_load ( Sdb s)

Definition at line 38 of file journal.c.

38  {
39  int rr, sz, fd, changes = 0;
40  char *eq, *str, *cur, *ptr = NULL;
41  if (!s) {
42  return 0;
43  }
44  fd = s->journal;
45  if (fd == -1) {
46  return 0;
47  }
48  sz = lseek(fd, 0, SEEK_END);
49  if (sz < 1) {
50  return 0;
51  }
52  lseek(fd, 0, SEEK_SET);
53  str = malloc(sz + 1);
54  if (!str) {
55  return 0;
56  }
57  rr = read(fd, str, sz);
58  if (rr < 0) {
59  free(str);
60  return 0;
61  }
62  str[sz] = 0;
63  for (cur = str;;) {
64  ptr = strchr(cur, '\n');
65  if (!ptr) {
66  break;
67  }
68  *ptr = 0;
69  eq = strchr(cur, '=');
70  if (eq) {
71  *eq++ = 0;
72  sdb_set(s, cur, eq, 0);
73  changes++;
74  }
75  cur = ptr + 1;
76  }
77  free(str);
78  return changes;
79 }
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 lseek
Definition: sflib.h:113
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define SEEK_SET
Definition: zip.c:88
#define SEEK_END
Definition: zip.c:84
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References eq, fd, free(), lseek, malloc(), NULL, read(), s, sdb_set(), SEEK_END, SEEK_SET, and cmd_descs_generate::str.

Referenced by sdb_config().

◆ sdb_journal_log()

RZ_API bool sdb_journal_log ( Sdb s,
const char *  key,
const char *  val 
)

Definition at line 81 of file journal.c.

81  {
82  if (s->journal == -1) {
83  return false;
84  }
85  const char *str = sdb_fmt("%s=%s\n", key, val);
86  int len = strlen(str);
87  if (write(s->journal, str, len) != len) {
88  return false;
89  }
90 #if HAVE_HEADER_SYS_MMAN_H
91  (void)fsync(s->journal);
92 #endif
93  return true;
94 }
static static fork write
Definition: sflib.h:33

References fsync, key, len, s, sdb_fmt(), cmd_descs_generate::str, val, and write.

Referenced by sdb_set_internal().

◆ sdb_journal_open()

RZ_API bool sdb_journal_open ( Sdb s)

Definition at line 23 of file journal.c.

23  {
24  const char *filename;
25  if (!s || !s->name) {
26  return false;
27  }
29  if (!filename) {
30  return false;
31  }
32  close(s->journal);
33  s->journal = open(filename, O_CREAT | O_RDWR | O_APPEND, 0600);
34  return s->journal != -1;
35 }
const char * filename
Definition: ioapi.h:137
#define O_APPEND
Definition: sftypes.h:493

References close, O_APPEND, O_CREAT, O_RDWR, s, and sdb_journal_filename().

Referenced by sdb_config().

◆ sdb_journal_unlink()

RZ_API bool sdb_journal_unlink ( Sdb s)

Definition at line 103 of file journal.c.

103  {
104  const char *filename = sdb_journal_filename(s);
106  if (filename) {
107  return !unlink(filename);
108  }
109  return false;
110 }

References s, sdb_journal_close(), sdb_journal_filename(), and unlink.

◆ 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
int array_size
Definition: sdb.c:1118
const char * val
Definition: sdb.c:1114
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, calloc(), cb, free(), k, LikeCallbackData::key, like_cb(), NULL, s, sdb_foreach(), v, and LikeCallbackData::val.

◆ sdb_lock()

RZ_API bool sdb_lock ( const char *  s)

Definition at line 29 of file lock.c.

29  {
30  int fd;
31  char *pid, pidstr[64];
32  if (!s) {
33  return false;
34  }
35  fd = open(s, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL, SDB_MODE);
36  if (fd == -1) {
37  return false;
38  }
39  pid = sdb_itoa(rz_sys_getpid(), pidstr, 10);
40  if (pid) {
41  if ((write(fd, pid, strlen(pid)) < 0) || (write(fd, "\n", 1) < 0)) {
42  close(fd);
43  return false;
44  }
45  }
46  close(fd);
47  return true;
48 }
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc pid
Definition: sflib.h:64
RZ_API int rz_sys_getpid(void)
Definition: sys.c:1164
#define O_WRONLY
Definition: sftypes.h:487
#define O_EXCL
Definition: sftypes.h:490

References close, fd, O_CREAT, O_EXCL, O_TRUNC, O_WRONLY, pid, rz_sys_getpid(), s, sdb_itoa(), SDB_MODE, and write.

Referenced by sdb_file(), sdb_lock_wait(), and sdb_new().

◆ sdb_lock_file()

RZ_API const char* sdb_lock_file ( const char *  f)

Definition at line 14 of file lock.c.

14  {
15  static char buf[128];
16  size_t len;
17  if (!f || !*f) {
18  return NULL;
19  }
20  len = strlen(f);
21  if (len + 10 > sizeof buf) {
22  return NULL;
23  }
24  memcpy(buf, f, len);
25  strcpy(buf + len, ".lock");
26  return buf;
27 }

References f, len, memcpy(), and NULL.

Referenced by sdb_file(), sdb_fini(), and sdb_new().

◆ sdb_lock_wait()

RZ_API int sdb_lock_wait ( RZ_UNUSED const char *  s)

◆ sdb_match()

RZ_API bool sdb_match ( const char *  str,
const char *  glob 
)

Definition at line 95 of file match.c.

95  {
96  int glob_len, flags = SDB_LIKE_NONE;
97  if (!str || !glob) {
98  return false;
99  }
100  glob_len = strlen(glob);
101  if (haveSuffix(glob, glob_len, "?i")) {
102  glob_len -= 2;
104  }
105  if (havePrefix(glob, glob_len, "%")) {
106  glob++;
107  glob_len--;
109  }
110  if (havePrefix(glob, glob_len, "^")) {
111  glob++;
112  glob_len--;
114  }
115  if (haveSuffix(glob, glob_len, "$")) {
116  glob_len--;
117  flags |= SDB_LIKE_END;
118  }
119  return compareString(str, glob, glob_len, flags);
120 }
static bool compareString(const char *a, const char *b, int blen, int flags)
Definition: match.c:52
static int havePrefix(const char *glob, int glob_len, const char *pfx)
Definition: match.c:12
@ SDB_LIKE_ICASE
Definition: match.c:19
@ SDB_LIKE_END
Definition: match.c:21
@ SDB_LIKE_NONE
Definition: match.c:18
@ SDB_LIKE_BASE64
Definition: match.c:22
@ SDB_LIKE_START
Definition: match.c:20
static int haveSuffix(const char *glob, int glob_len, const char *sfx)
Definition: match.c:7
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123

References compareString(), flags, havePrefix(), haveSuffix(), SDB_LIKE_BASE64, SDB_LIKE_END, SDB_LIKE_ICASE, SDB_LIKE_NONE, SDB_LIKE_START, and cmd_descs_generate::str.

Referenced by like_cb(), and unset_cb().

◆ 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_new()

RZ_API Sdb* sdb_new ( const char *  path,
const char *  file,
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 }
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
#define RZ_NEW0(x)
Definition: rz_types.h:284
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
Definition: sdb.h:63
#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_now()

RZ_API ut64 sdb_now ( void  )

Definition at line 211 of file util.c.

211  {
212  ut64 usec = rz_time_now();
213  return usec / 1000000;
214 }
RZ_API ut64 rz_time_now(void)
Returns the current time in microseconds.
Definition: time.c:88

References rz_time_now(), and ut64().

Referenced by parse_expire(), sdb_const_get_len(), sdb_hook_call(), and sdb_new().

◆ sdb_ns()

RZ_API Sdb* sdb_ns ( Sdb s,
const char *  name,
int  create 
)

Definition at line 186 of file ns.c.

186  {
187  SdbListIter *it;
188  SdbNs *ns;
189  ut32 hash;
190  if (!s || !name || !*name) {
191  return NULL;
192  }
193  hash = sdb_hash(name);
194  ls_foreach (s->ns, it, ns) {
195  if (ns->hash == hash) {
196  return ns->sdb;
197  }
198  }
199  if (!create) {
200  return NULL;
201  }
202  if (s->ns_lock) {
203  return NULL;
204  }
205  ns = sdb_ns_new(s, name, hash);
206  if (!ns) {
207  return NULL;
208  }
209  ls_append(s->ns, ns);
210  return ns->sdb;
211 }
static RzBuffer * create(RzBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RzBinArchOptions *opt)
Definition: bin_cgc.c:16
static SdbNs * sdb_ns_new(Sdb *s, const char *name, ut32 hash)
Definition: ns.c:87
ut32 hash
Definition: sdb.h:90

References create(), sdb_ns_t::hash, ls_append(), ls_foreach, NULL, s, sdb_ns_t::sdb, sdb_hash(), and sdb_ns_new().

Referenced by core_analysis_graph_nodes(), file_save_cb(), load_process_line(), print_rop(), rop_classify(), rop_kuery(), rz_analysis_dwarf_process_info(), rz_analysis_new(), rz_cmd_kuery(), rz_cmd_search(), rz_core_analysis_everything(), rz_core_bin_export_info(), rz_core_bin_get_compile_time(), rz_core_search_rop(), rz_debug_session_serialize(), rz_project_load(), rz_project_migrate_v1_v2(), rz_project_migrate_v2_v3(), rz_project_migrate_v3_v4(), rz_project_migrate_v5_v6(), rz_project_save(), rz_serialize_analysis_classes_load(), rz_serialize_analysis_meta_load(), rz_serialize_analysis_meta_save(), rz_serialize_analysis_save(), rz_serialize_core_save(), rz_serialize_debug_save(), rz_serialize_flag_load(), rz_serialize_flag_save(), rz_serialize_io_files_save(), rz_serialize_io_save(), rz_serialize_spaces_load(), rz_serialize_spaces_save(), sdb_copy(), sdb_diff_ctx(), sdb_ns_path(), sdb_querys(), and update_sdb().

◆ sdb_ns_free_all()

RZ_API void sdb_ns_free_all ( Sdb s)

Definition at line 74 of file ns.c.

74  {
75  SdbList *list;
76  if (!s) {
77  return;
78  }
79  list = ls_new();
80  list->free = NULL;
82  ls_free(list);
83  ls_free(s->ns);
84  s->ns = NULL;
85 }
static void ns_free_exc_list(Sdb *s, SdbList *list)
Definition: ns.c:29

References list(), ls_free(), ls_new(), ns_free_exc_list(), NULL, and s.

Referenced by sdb_fini().

◆ sdb_ns_init()

RZ_API void sdb_ns_init ( Sdb s)

◆ sdb_ns_lock()

RZ_API void sdb_ns_lock ( Sdb s,
int  lock,
int  depth 
)

Definition at line 6 of file ns.c.

6  {
7  SdbListIter *it;
8  SdbNs *ns;
9  s->ns_lock = lock;
10  if (depth) { // handles -1 as infinite
11  ls_foreach (s->ns, it, ns) {
12  sdb_ns_lock(ns->sdb, lock, depth - 1);
13  }
14  }
15 }
RZ_API void sdb_ns_lock(Sdb *s, int lock, int depth)
Definition: ns.c:6

References lock(), ls_foreach, s, and sdb_ns_t::sdb.

◆ sdb_ns_path()

RZ_API Sdb* sdb_ns_path ( Sdb s,
const char *  path,
int  create 
)

Definition at line 213 of file ns.c.

213  {
214  char *ptr, *str;
215  char *slash;
216 
217  if (!s || !path || !*path)
218  return s;
219  ptr = str = strdup(path);
220  do {
221  slash = strchr(ptr, '/');
222  if (slash)
223  *slash = 0;
224  s = sdb_ns(s, ptr, create);
225  if (!s)
226  break;
227  if (slash)
228  ptr = slash + 1;
229  } while (slash);
230  free(str);
231  return s;
232 }

References create(), free(), path, s, sdb_ns(), cmd_descs_generate::str, and strdup().

Referenced by bin_elf_versioninfo_verneed(), bin_elf_versioninfo_versym(), bin_pe_versioninfo(), rz_cmd_kuery(), rz_core_bin_apply_resources(), and session_sdb_load_ns().

◆ sdb_ns_set()

RZ_API int sdb_ns_set ( Sdb s,
const char *  name,
Sdb r 
)

Definition at line 156 of file ns.c.

156  {
157  SdbNs *ns;
158  SdbListIter *it;
159  ut32 hash = sdb_hash(name);
160  if (!s || !r || !name) {
161  return 0;
162  }
163  ls_foreach (s->ns, it, ns) {
164  if (ns->hash == hash) {
165  if (ns->sdb == r) {
166  return 0;
167  }
168  sdb_free(ns->sdb);
169  r->refs++; // sdb_ref / sdb_unref //
170  ns->sdb = r;
171  return 1;
172  }
173  }
174  if (s->ns_lock) {
175  return 0;
176  }
177  ns = RZ_NEW(SdbNs);
178  ns->name = strdup(name);
179  ns->hash = hash;
180  ns->sdb = r;
181  r->refs++;
182  ls_append(s->ns, ns);
183  return 1;
184 }
#define RZ_NEW(x)
Definition: rz_types.h:285
RZ_API bool sdb_free(Sdb *s)
Definition: sdb.c:206

References sdb_ns_t::hash, ls_append(), ls_foreach, sdb_ns_t::name, r, RZ_NEW, s, sdb_ns_t::sdb, sdb_free(), sdb_hash(), and strdup().

Referenced by _parse_resource_directory(), _store_resource_sdb(), get_gnu_verneed(), get_sdb(), get_verneed_entry_sdb(), init_symbols_info_aux(), load(), load_buffer(), Pe_r_bin_store_resource_version_info(), Pe_r_bin_store_string_file_info(), Pe_r_bin_store_string_table(), Pe_r_bin_store_var_file_info(), rz_bin_elf_get_symbols_info(), rz_bin_object_new(), update_sdb(), and update_syscall_ns().

◆ sdb_ns_sync()

RZ_API void sdb_ns_sync ( Sdb s)

Definition at line 248 of file ns.c.

248  {
249  SdbList *list = ls_new();
250  ns_sync(s, list);
251  list->free = NULL;
252  ls_free(list);
253 }
static void ns_sync(Sdb *s, SdbList *list)
Definition: ns.c:234

References list(), ls_free(), ls_new(), ns_sync(), NULL, and s.

◆ sdb_ns_unset()

RZ_API bool sdb_ns_unset ( Sdb s,
const char *  name,
Sdb r 
)

Definition at line 136 of file ns.c.

136  {
137  SdbNs *ns;
138  SdbListIter *it;
139  if (s && (name || r)) {
140  ls_foreach (s->ns, it, ns) {
141  if (name && (!strcmp(name, ns->name))) {
142  sdb_ns_free(ns);
143  ls_delete(s->ns, it);
144  return true;
145  }
146  if (r && ns->sdb == r) {
147  sdb_ns_free(ns);
148  ls_delete(s->ns, it);
149  return true;
150  }
151  }
152  }
153  return false;
154 }
RZ_API void ls_delete(SdbList *list, SdbListIter *iter)
Definition: ls.c:133
static void sdb_ns_free(SdbNs *ns)
Definition: ns.c:130

References ls_delete(), ls_foreach, sdb_ns_t::name, r, s, sdb_ns_t::sdb, and sdb_ns_free().

Referenced by rz_project_migrate_v6_v7(), rz_project_migrate_v7_v8(), and update_syscall_ns().

◆ sdb_num_add()

RZ_API int sdb_num_add ( Sdb s,
const char *  key,
ut64  v,
ut32  cas 
)

Definition at line 18 of file num.c.

18  {
19  char *val, b[SDB_NUM_BUFSZ];
20  int numbase = sdb_num_base(sdb_const_get(s, key, NULL));
21  val = sdb_itoa(v, b, numbase);
22  return sdb_add(s, key, val, cas);
23 }
RZ_API int sdb_add(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:351
RZ_API int sdb_num_base(const char *s)
Definition: util.c:221

References b, key, NULL, s, sdb_add(), sdb_const_get(), sdb_itoa(), sdb_num_base(), SDB_NUM_BUFSZ, v, and val.

Referenced by rz_core_bin_apply_all_info().

◆ sdb_num_base()

RZ_API int sdb_num_base ( const char *  s)

Definition at line 221 of file util.c.

221  {
222  if (!s) {
223  return SDB_NUM_BASE;
224  }
225  if (!strncmp(s, "0x", 2)) {
226  return 16;
227  }
228  return (*s == '0' && s[1]) ? 8 : 10;
229 }

References s, and SDB_NUM_BASE.

Referenced by sdb_num_add(), sdb_num_set(), and sdb_querys().

◆ sdb_num_dec()

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

Definition at line 43 of file num.c.

43  {
44  ut32 c;
45  ut64 n = sdb_num_get(s, key, &c);
46  if (cas && c != cas) {
47  return 0LL;
48  }
49  if (n2 > n) {
50  sdb_set(s, key, "0", cas);
51  return 0LL; // XXX must be -1LL?
52  }
53  n -= n2;
54  sdb_num_set(s, key, n, cas);
55  return n;
56 }
RZ_API int sdb_num_set(Sdb *s, const char *key, ut64 v, ut32 cas)
Definition: num.c:25
RZ_API ut64 sdb_num_get(Sdb *s, const char *key, ut32 *cas)
Definition: num.c:13

References c, key, n, s, sdb_num_get(), sdb_num_set(), sdb_set(), and ut64().

Referenced by sdb_querys().

◆ sdb_num_exists()

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

Definition at line 8 of file num.c.

8  {
9  const char *o = sdb_const_get(s, key, NULL);
10  return o ? (*o >= '0' && *o <= '9') : false;
11 }

References key, NULL, s, and sdb_const_get().

◆ sdb_num_get()

◆ sdb_num_inc()

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

Definition at line 32 of file num.c.

32  {
33  ut32 c;
34  ut64 n = sdb_num_get(s, key, &c);
35  ut64 res = n + n2;
36  if ((cas && c != cas) || res < n) {
37  return 0LL;
38  }
39  sdb_num_set(s, key, res, cas);
40  return res;
41 }

References c, key, n, s, sdb_num_get(), sdb_num_set(), and ut64().

Referenced by sdb_querys(), and ssa_set().

◆ sdb_num_max()

RZ_API int sdb_num_max ( Sdb s,
const char *  key,
ut64  v,
ut32  cas 
)

Definition at line 65 of file num.c.

65  {
66  const char *a = sdb_const_get(db, k, NULL);
67  return (!a || n > sdb_atoi(a))
68  ? sdb_num_set(db, k, n, cas)
69  : 0;
70 }

References a, k, n, NULL, sdb_atoi(), sdb_const_get(), and sdb_num_set().

◆ sdb_num_min()

RZ_API int sdb_num_min ( Sdb s,
const char *  key,
ut64  v,
ut32  cas 
)

Definition at line 58 of file num.c.

58  {
59  const char *a = sdb_const_get(db, k, NULL);
60  return (!a || n < sdb_atoi(a))
61  ? sdb_num_set(db, k, n, cas)
62  : 0;
63 }

References a, k, n, NULL, sdb_atoi(), sdb_const_get(), and sdb_num_set().

◆ sdb_num_set()

◆ 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 }
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 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_ptr_get()

RZ_API void* sdb_ptr_get ( Sdb db,
const char *  key,
ut32 cas 
)

Definition at line 87 of file num.c.

87  {
88  return (void *)(size_t)sdb_num_get(db, key, cas);
89 }

References key, and sdb_num_get().

Referenced by __create_almighty(), __exec_almighty(), __set_rcb(), and rz_analysis_esil_fire_trap().

◆ sdb_ptr_set()

RZ_API int sdb_ptr_set ( Sdb db,
const char *  key,
void *  p,
ut32  cas 
)

Definition at line 83 of file num.c.

83  {
84  return sdb_num_set(db, key, (ut64)(size_t)p, cas);
85 }

References key, p, sdb_num_set(), and ut64().

Referenced by __init_almighty_db(), and __init_rotate_db().

◆ sdb_query()

RZ_API int sdb_query ( Sdb s,
const char *  cmd 
)

Definition at line 785 of file query.c.

785  {
786  char buf[1024], *out;
787  int must_save = ((*cmd == '~') || strchr(cmd, '='));
788  out = sdb_querys(s, buf, sizeof(buf) - 1, cmd);
789  if (out) {
790  if (*out) {
791  puts(out);
792  }
793  if (out != buf) {
794  free(out);
795  }
796  }
797  return must_save;
798 }
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 cmd
Definition: sflib.h:79
RZ_API char * sdb_querys(Sdb *r, char *buf, size_t len, const char *_cmd)
Definition: query.c:164

References cmd, free(), out, s, and sdb_querys().

Referenced by __sdb_prompt(), init_msg_types(), insertkeys(), main(), rz_main_rz_bin(), sdb_query_lines(), and sdb_queryf().

◆ sdb_query_file()

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

Definition at line 856 of file query.c.

856  {
857  int ret = 0;
858  char *txt = slurp(file);
859  if (txt) {
860  ret = sdb_query_lines(s, txt);
861  free(txt);
862  }
863  return ret;
864 }
static char * slurp(const char *file)
Definition: query.c:824
RZ_API int sdb_query_lines(Sdb *s, const char *cmd)
Definition: query.c:800

References free(), s, sdb_query_lines(), and slurp().

Referenced by sdb_querys().

◆ sdb_query_lines()

RZ_API int sdb_query_lines ( Sdb s,
const char *  cmd 
)

Definition at line 800 of file query.c.

800  {
801  char *o, *p, *op;
802  if (!s || !cmd) {
803  return 0;
804  }
805  op = strdup(cmd);
806  if (!op) {
807  return 0;
808  }
809  p = op;
810  do {
811  o = strchr(p, '\n');
812  if (o) {
813  *o = 0;
814  }
815  (void)sdb_query(s, p);
816  if (o) {
817  p = o + 1;
818  }
819  } while (o);
820  free(op);
821  return 1;
822 }
ut8 op
Definition: 6502dis.c:13
RZ_API int sdb_query(Sdb *s, const char *cmd)
Definition: query.c:785
Definition: dis.c:32

References cmd, free(), op, p, s, sdb_query(), and strdup().

Referenced by sdb_load_from_string(), and sdb_query_file().

◆ sdb_queryf()

RZ_API int sdb_queryf ( Sdb s,
const char *  fmt,
  ... 
)

Definition at line 57 of file query.c.

57  {
58  char string[4096];
59  int ret;
60  va_list ap;
61  va_start(ap, fmt);
62  vsnprintf(string, sizeof(string), fmt, ap);
63  ret = sdb_query(s, string);
64  va_end(ap);
65  return ret;
66 }
vsnprintf
Definition: kernel.h:366

References s, sdb_query(), and vsnprintf.

Referenced by rz_debug_signal_setup().

◆ sdb_querys()

RZ_API char* sdb_querys ( Sdb s,
char *  buf,
size_t  len,
const char *  cmd 
)

Definition at line 164 of file query.c.

164  {
165  int i, d, ok, w, alength, bufset = 0, is_ref = 0, encode = 0;
166  const char *p, *q, *val = NULL;
167  char *eq, *next, *quot, *slash, *res,
168  *cmd, *newcmd = NULL, *original_cmd = NULL;
169  StrBuf *out;
170  Sdb *s = r;
171  ut64 n;
172  if (!s || (!_cmd && !buf)) {
173  return NULL;
174  }
175  out = strbuf_new();
176  if ((int)len < 1 || !buf) {
177  bufset = 1;
178  buf = malloc((len = 64));
179  if (!buf) {
180  strbuf_free(out);
181  return NULL;
182  }
183  }
184  if (_cmd) {
185  cmd = original_cmd = strdup(_cmd);
186  if (!cmd) {
187  free(out);
188  if (bufset) {
189  free(buf);
190  }
191  return NULL;
192  }
193  } else {
194  cmd = buf;
195  }
196  // if cmd is null, we take buf as cmd
197  next = NULL;
198 repeat:
199  /* skip spaces */
200  while (*cmd && (*cmd == ' ' || *cmd == '\t')) {
201  cmd++;
202  }
203  s = r;
204  p = cmd;
205  eq = NULL;
206  encode = 0;
207  is_ref = 0;
208  quot = NULL;
209  if (*p == '#') {
210  p++;
211  next = strchr(p, ';');
212  if (next) {
213  *next = 0;
214  }
215  out_concat(sdb_fmt("0x%08x\n", sdb_hash(p)));
216  if (next) {
217  *next = ';';
218  }
219  goto runNext;
220  } else if (*p == '%') {
221  encode = 1;
222  cmd++;
223  p++;
224  }
225  if (next)
226  *next = ';';
227  eq = strchr(p, '=');
228  if (eq) {
229  d = 1;
230  *eq++ = 0;
231  if (*eq == '$') {
232  next = strchr(eq + 1, ';');
233  if (next)
234  *next = 0;
235  val = sdb_const_get(s, eq + 1, 0);
236  if (!val) {
237  eprintf("No value for '%s'\n", eq + 1);
238  goto fail;
239  }
240  if (next)
241  *next = ';';
242  is_ref = 1; // protect readonly buffer from being processed
243  } else {
244  val = eq;
245  }
246  } else {
247  val = NULL;
248  d = 0;
249  }
250  if (!is_ref) {
251  next = strchr(val ? val : cmd, ';');
252  }
253  // if (!val) val = eq;
254  if (!is_ref && val && *val == '"') {
255  val++;
256  // TODO: escape \" too
257  quot = (char *)val;
258  next_quote:
259  quot = strchr(quot, '"');
260  if (quot) {
261  if (*(quot - 1) == '\\') {
262  memmove(quot - 1, quot, strlen(quot) + 1);
263  goto next_quote;
264  }
265  *quot++ = 0; // crash on read only mem!!
266  } else {
267  eprintf("Missing quote\n");
268  *eq++ = 0;
269  out = strbuf_free(out);
270  goto fail;
271  }
272  next = strchr(quot, ';');
273  } else {
274  quot = NULL;
275  }
276  if (next) {
277  *next = 0;
278  }
279  slash = strchr(cmd, '/');
280  while (slash) {
281  *slash = 0;
282  s = sdb_ns(s, cmd, eq ? 1 : 0);
283  if (!s) {
284  eprintf("Cant find namespace %s\n", cmd);
285  out = strbuf_free(out);
286  goto fail;
287  }
288  cmd = slash + 1;
289  slash = strchr(cmd, '/');
290  }
291  if (*cmd == '?') {
292  const char *val = sdb_const_get(s, cmd + 1, 0);
293  const char *type = sdb_type(val);
294  out_concat(type);
295  } else if (*cmd == '*') {
296  if (!strcmp(cmd, "***")) {
297  char root[1024]; // limit namespace length?
298  SdbListIter *it;
299  SdbNs *ns;
300  ls_foreach (s->ns, it, ns) {
301  int name_len = strlen(ns->name);
302  if (name_len < (long)sizeof(root)) {
303  memcpy(root, ns->name, name_len + 1);
305  sizeof(root) - name_len,
306  root + name_len, ns, encode);
307  } else {
308  eprintf("TODO: Namespace too long\n");
309  }
310  }
311  goto fail;
312  } else if (!strcmp(cmd, "**")) {
313  SdbListIter *it;
314  SdbNs *ns;
315  ls_foreach (s->ns, it, ns) {
316  out_concat(ns->name);
317  }
318  goto fail;
319  } else if (!strcmp(cmd, "*")) {
320  ForeachListUser user = { out, encode, NULL };
321  SdbList *list = sdb_foreach_list(s, true);
322  SdbListIter *iter;
323  SdbKv *kv;
324  ls_foreach (list, iter, kv) {
325  foreach_list_cb(&user, sdbkv_key(kv), sdbkv_value(kv));
326  }
327  ls_free(list);
328  goto fail;
329  }
330  }
331  if (*cmd == '[') {
332  char *tp = strchr(cmd, ']');
333  if (!tp) {
334  eprintf("Missing ']'.\n");
335  goto fail;
336  }
337  *tp++ = 0;
338  p = (const char *)tp;
339  } else {
340  p = cmd;
341  }
342  if (*cmd == '$') {
343  free(newcmd);
344  char *nc = sdb_get(s, cmd + 1, 0);
345  cmd = newcmd = (nc) ? nc : strdup("");
346  }
347  // cmd = val
348  // cmd is key and val is value
349  if (*cmd == '.') {
350  if (s->options & SDB_OPTION_FS) {
351  if (!sdb_query_file(s, cmd + 1)) {
352  eprintf("sdb: cannot open '%s'\n", cmd + 1);
353  goto fail;
354  }
355  } else {
356  eprintf("sdb: filesystem access disabled in config\n");
357  }
358  } else if (*cmd == '~') { // delete
359  if (cmd[1] == '~') { // grep
360  SdbKv *kv;
361  SdbListIter *li;
362  SdbList *l = sdb_foreach_match(s, cmd + 2, false);
363  ls_foreach (l, li, kv) {
364  strbuf_append(out, sdbkv_key(kv), 0);
365  strbuf_append(out, "=", 0);
366  strbuf_append(out, sdbkv_value(kv), 1);
367  }
368  fflush(stdout);
369  ls_free(l);
370  } else {
371  d = 1;
372  sdb_unset_like(s, cmd + 1);
373  }
374  } else if (*cmd == '+' || *cmd == '-') {
375  d = 1;
376  if (!buf) {
377  buf = calloc(1, len);
378  if (!buf) {
379  goto fail;
380  }
381  bufset = 1;
382  }
383  *buf = 0;
384  if (cmd[1] == '[') {
385  const char *eb = strchr(cmd, ']');
386  if (!eb) {
387  eprintf("Missing ']'.\n");
388  goto fail;
389  }
390  int idx = sdb_atoi(cmd + 2);
391  /* +[idx]key=n */
392  /* -[idx]key=n */
393  ut64 curnum = sdb_array_get_num(s,
394  eb + 1, idx, 0);
395  if (eq) {
396  /* +[idx]key=n --> key[idx] += n */
397  /* -[idx]key=n --> key[idx] -= n */
398  st64 n = sdb_atoi(eq);
399  if (*cmd == '+') {
400  curnum += n;
401  } else if (*cmd == '-') {
402  curnum -= n;
403  } else {
404  // never happens
405  }
406  sdb_array_set_num(s, eb + 1, idx, curnum, 0);
407  } else {
408  /* +[idx]key --> key[idx] + 1 */
409  /* -[idx]key --> key[idx] - 1 */
410  char *nstr, numstr[128];
411  if (*cmd == '+') {
412  curnum++;
413  } else if (*cmd == '-') {
414  curnum--;
415  } else {
416  // never happens
417  }
418  nstr = sdb_itoa(curnum, numstr, 10);
419  strbuf_append(out, nstr, 1);
420  }
421  } else if (val) {
422  if (sdb_isnum(val)) {
423  int op = *cmd;
424  if (*val == '-') {
425  if (*cmd == '-') {
426  op = '+';
427  } else {
428  op = '-';
429  }
430  d = sdb_atoi(val + 1);
431  } else {
432  d = sdb_atoi(val);
433  }
434  if (op == '+') {
435  sdb_num_inc(s, cmd + 1, d, 0);
436  } else {
437  sdb_num_dec(s, cmd + 1, d, 0);
438  }
439  } else {
440  if (*cmd == '+') {
441  sdb_concat(s, cmd + 1, val, 0);
442  } else {
443  sdb_uncat(s, cmd + 1, val, 0);
444  }
445  }
446  } else {
447  int base = sdb_num_base(sdb_const_get(s, cmd + 1, 0));
448  if (*cmd == '+') {
449  n = sdb_num_inc(s, cmd + 1, d, 0);
450  } else {
451  n = sdb_num_dec(s, cmd + 1, d, 0);
452  }
453  // keep base
454  if (base == 16) {
455  w = snprintf(buf, len - 1, "0x%" PFMT64x "x", n);
456  if (w < 0 || (size_t)w > len) {
457  if (bufset && len < 0xff) {
458  free(buf);
459  buf = malloc(len = 0xff);
460  if (!buf) {
461  goto fail;
462  }
463  }
464  bufset = 1;
465  snprintf(buf, 0xff, "0x%" PFMT64x "x", n);
466  }
467  } else {
468  w = snprintf(buf, len - 1, "%" PFMT64x "d", n);
469  if (w < 0 || (size_t)w > len) {
470  if (bufset && len < 0xff) {
471  free(buf);
472  buf = malloc(len = 0xff);
473  if (!buf) {
474  goto fail;
475  }
476  }
477  bufset = 1;
478  snprintf(buf, 0xff, "%" PFMT64x "d", n);
479  }
480  }
481  }
482  out_concat(buf);
483  } else if (*cmd == '[') {
484  // [?] - count elements of array
485  if (cmd[1] == '?') {
486  // if (!eq) ...
487  alength = sdb_array_length(s, p);
488  if (!buf) {
489  buf = malloc(++len);
490  if (!buf) {
491  goto fail;
492  }
493  bufset = 1;
494  }
495  w = snprintf(buf, len, "%d", alength);
496  if (w < 0 || (size_t)w > len) {
497  if (bufset) {
498  free(buf);
499  }
500  buf = malloc(len = 32);
501  bufset = 1;
502  snprintf(buf, 31, "%d", alength);
503  }
504  out_concat(buf);
505  } else if (cmd[1] == '!') {
506  if (cmd[2] == '+') {
507  // [!+]key=aa # add_sorted
508  sdb_array_add_sorted(s, p, val, 0);
509  } else {
510  // [!]key # sort
511  sdb_array_sort(s, p, 0);
512  }
513  } else if (cmd[1] == '#') {
514  // [#+]key=num # add_sorted_num
515  if (cmd[2] == '+') {
516  // [#]key # sort_num
518  } else {
519  sdb_array_sort_num(s, p, 0);
520  }
521  } else if (cmd[1] == '+' || cmd[1] == '-') {
522  if (cmd[1] == cmd[2]) {
523  // stack
524  if (cmd[1] == '-' && eq) {
525  /* invalid syntax */
526  } else if (cmd[1] == '+' && !eq) {
527  /* invalid syntax */
528  } else {
529  if (eq) {
530  sdb_array_push(s, p, val, 0);
531  } else {
532  char *ret = sdb_array_pop(s, p, 0);
533  out_concat(ret);
534  free(ret);
535  }
536  }
537  } else
538  // [+]foo remove first element */
539  // [+]foo=bar ADD */
540  // [-]foo POP */
541  // [-]foo=xx REMOVE (=xx ignored) */
542  if (!cmd[2] || cmd[2] == ']') {
543  // insert
544  if (eq) {
545  if (cmd[1] == '+') {
546  // [+]K=1
547  sdb_array_add(s, p, val, 0);
548  } else {
549  // [-]K= = remove first element
550  sdb_array_remove(s, p, val, 0);
551  }
552  // return NULL;
553  } else {
554  char *ret;
555  if (cmd[1] == '+') {
556  // [+]K = remove first element
557  // XXX: this is a little strange syntax to remove an item
558  ret = sdb_array_get(s, p, 0, 0);
559  if (ret && *ret) {
560  out_concat(ret);
561  }
562  // (+)foo :: remove first element
563  sdb_array_delete(s, p, 0, 0);
564  } else {
565  // [-]K = remove last element
566  ret = sdb_array_get(s, p, -1, 0);
567  if (ret && *ret) {
568  out_concat(ret);
569  }
570  // (-)foo :: remove last element
571  sdb_array_delete(s, p, -1, 0);
572  }
573  free(ret);
574  }
575  } else {
576  // get/set specific element in array
577  i = atoi(cmd + 1);
578  if (eq) {
579  /* [+3]foo=bla */
580  if (i < 0) {
581  char *tmp = sdb_array_get(s, p, -i, NULL);
582  if (tmp) {
583  if (encode) {
584  char *newtmp = (void *)sdb_decode(tmp, NULL);
585  if (!newtmp) {
586  goto fail;
587  }
588  free(tmp);
589  tmp = newtmp;
590  }
591  ok = 0;
592  out_concat(tmp);
593  sdb_array_delete(s, p, -i, 0);
594  free(tmp);
595  } else
596  goto fail;
597  } else {
598  if (encode) {
599  val = sdb_encode((const ut8 *)val, -1);
600  }
601  ok = cmd[1] ? ((cmd[1] == '+') ? sdb_array_insert(s, p, i, val, 0) : sdb_array_set(s, p, i, val, 0)) : sdb_array_delete(s, p, i, 0);
602  if (encode) {
603  free((void *)val);
604  val = NULL;
605  }
606  }
607  if (ok && buf)
608  *buf = 0;
609  else
610  buf = NULL;
611  } else {
612  if (i == 0) {
613  /* [-b]foo */
614  if (cmd[1] == '-') {
615  sdb_array_remove(s, p, cmd + 2, 0);
616  } else {
617  eprintf("TODO: [b]foo -> get index of b key inside foo array\n");
618  // sdb_array_dels (s, p, cmd+1, 0);
619  }
620  } else if (i < 0) {
621  /* [-3]foo */
622  char *tmp = sdb_array_get(s, p, -i, NULL);
623  if (tmp && *tmp) {
624  out_concat(tmp);
625  sdb_array_delete(s, p, -i, 0);
626  }
627  free(tmp);
628  } else {
629  /* [+3]foo */
630  char *tmp = sdb_array_get(s, p, i, NULL);
631  if (tmp && *tmp) {
632  out_concat(tmp);
633  }
634  free(tmp);
635  }
636  }
637  }
638  } else {
639  if (eq) {
640  /* [3]foo=bla */
641  char *sval = (char *)val;
642  if (encode) {
643  sval = sdb_encode((const ut8 *)val, -1);
644  }
645  if (cmd[1]) {
646  int idx = atoi(cmd + 1);
647  ok = sdb_array_set(s, p, idx, sval, 0);
648  // TODO: handle when idx > sdb_alen
649  if (encode)
650  free(sval);
651  } else {
652  if (encode) {
653  ok = sdb_set_owned(s, p, sval, 0);
654  } else {
655  ok = sdb_set(s, p, sval, 0);
656  }
657  }
658  if (ok && buf) {
659  *buf = 0;
660  }
661  } else {
662  /* [3]foo */
663  const char *sval = sdb_const_get(s, p, 0);
664  size_t wl;
665  if (cmd[1]) {
666  i = atoi(cmd + 1);
667  buf = sdb_array_get(s, p, i, NULL);
668  if (buf) {
669  bufset = 1;
670  len = strlen(buf) + 1;
671  }
672  if (encode) {
673  char *newbuf = (void *)sdb_decode(buf, NULL);
674  if (newbuf) {
675  free(buf);
676  buf = newbuf;
677  len = strlen(buf) + 1;
678  }
679  }
680  out_concat(buf);
681  } else {
682  if (!sval) {
683  goto fail;
684  }
685  wl = strlen(sval);
686  if (!buf || wl >= len) {
687  buf = malloc(wl + 2);
688  if (!buf) {
689  free(out->buf);
690  out->buf = NULL;
691  goto fail;
692  }
693  bufset = 1;
694  len = wl + 2;
695  }
696  for (i = 0; sval[i]; i++) {
697  if (sval[i + 1]) {
698  buf[i] = (sval[i] == SDB_RS)
699  ? '\n'
700  : sval[i];
701  } else {
702  buf[i] = sval[i];
703  }
704  }
705  buf[i] = 0;
706  if (encode) {
707  char *newbuf = (void *)sdb_decode(buf, NULL);
708  if (newbuf) {
709  if (bufset) {
710  free(buf);
711  }
712  buf = newbuf;
713  len = strlen(buf) + 1;
714  }
715  }
716  out_concat(buf);
717  }
718  }
719  }
720  } else {
721  if (eq) {
722  // 1 0 kvpath=value
723  if (encode) {
724  val = sdb_encode((const ut8 *)val, -1);
725  }
726  while (*val && isspace(*val)) {
727  val++;
728  }
729  int i = strlen(cmd) - 1;
730  while (i >= 0 && isspace(cmd[i])) {
731  cmd[i] = '\0';
732  i--;
733  }
734  ok = sdb_set(s, cmd, val, 0);
735  if (encode) {
736  free((void *)val);
737  val = NULL;
738  }
739  if (ok && buf) {
740  *buf = 0;
741  }
742  } else {
743  // 0 0 kvpath
744  // sdbget
745  if ((q = sdb_const_get(s, cmd, 0))) {
746  if (encode) {
747  q = (void *)sdb_decode(q, NULL);
748  }
749  out_concat(q);
750  if (encode) {
751  free((void *)q);
752  }
753  }
754  }
755  }
756 runNext:
757  if (next) {
758  if (bufset) {
759  free(buf);
760  buf = NULL;
761  bufset = 0;
762  }
763  cmd = next + 1;
764  encode = 0;
765  goto repeat;
766  }
767  if (eq) {
768  *--eq = '=';
769  }
770 fail:
771  if (bufset) {
772  free(buf);
773  }
774  if (out) {
775  res = out->buf;
776  free(out);
777  } else {
778  res = NULL;
779  }
780  free(original_cmd);
781  free(newcmd);
782  return res;
783 }
RZ_API int sdb_array_length(Sdb *s, const char *key)
Definition: array.c:524
RZ_API char * sdb_array_get(Sdb *s, const char *key, int idx, ut32 *cas)
Definition: array.c:81
RZ_API int sdb_array_add_sorted_num(Sdb *s, const char *key, ut64 val, ut32 cas)
Definition: array.c:290
RZ_API int sdb_array_add_sorted(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:231
RZ_API void sdb_array_sort(Sdb *s, const char *key, ut32 cas)
Definition: array.c:635
RZ_API int sdb_array_remove(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:436
RZ_API void sdb_array_sort_num(Sdb *s, const char *key, ut32 cas)
Definition: array.c:667
RZ_API ut64 sdb_array_get_num(Sdb *s, const char *key, int idx, ut32 *cas)
Definition: array.c:63
#define w
Definition: crypto_rc6.c:13
int root
Definition: enough.c:226
static void encode(size_t size, lzma_action action)
Definition: full_flush.c:25
snprintf
Definition: kernel.h:364
static bool foreach_list_cb(void *user, const char *k, const char *v)
Definition: query.c:91
static StrBuf * strbuf_free(StrBuf *sb)
Definition: query.c:51
static StrBuf * strbuf_new(void)
Definition: query.c:18
static StrBuf * strbuf_append(StrBuf *sb, const char *str, const int nl)
Definition: query.c:22
RZ_API int sdb_query_file(Sdb *s, const char *file)
Definition: query.c:856
static void walk_namespace(StrBuf *sb, char *root, int left, char *p, SdbNs *ns, int encode)
Definition: query.c:136
#define out_concat(x)
Definition: query.c:80
@ ok
Definition: lz4.c:1706
int type
Definition: mipsasm.c:17
RZ_API ut64 sdb_num_dec(Sdb *s, const char *key, ut64 n2, ut32 cas)
Definition: num.c:43
RZ_API ut64 sdb_num_inc(Sdb *s, const char *key, ut64 n2, ut32 cas)
Definition: num.c:32
static void repeat(struct parse *, sopno, int, int)
Definition: regcomp.c:1155
#define PFMT64x
Definition: rz_types.h:393
#define st64
Definition: rz_types_base.h:10
#define isspace(c)
Definition: safe-ctype.h:141
RZ_API SdbList * sdb_foreach_match(Sdb *s, const char *expr, bool single)
Definition: sdb.c:710
RZ_API int sdb_uncat(Sdb *s, const char *key, const char *value, ut32 cas)
Definition: sdb.c:304
RZ_API int sdb_unset_like(Sdb *s, const char *k)
Definition: sdb.c:1106
RZ_API SdbList * sdb_foreach_list(Sdb *s, bool sorted)
Definition: sdb.c:630
RZ_API int sdb_concat(Sdb *s, const char *key, const char *value, ut32 cas)
Definition: sdb.c:329
RZ_API int sdb_isnum(const char *s)
Definition: util.c:216
RZ_API const char * sdb_type(const char *k)
Definition: util.c:231
Definition: query.c:12

References calloc(), cmd, d, encode(), eprintf, eq, fail, foreach_list_cb(), free(), i, setup::idx, isspace, len, list(), ls_foreach, ls_free(), malloc(), memcpy(), n, sdb_ns_t::name, NULL, ok, out, out_concat, p, PFMT64x, r, repeat(), root, s, sdb_array_add(), sdb_array_add_sorted(), sdb_array_add_sorted_num(), sdb_array_delete(), sdb_array_get(), sdb_array_get_num(), sdb_array_insert(), sdb_array_length(), sdb_array_pop(), sdb_array_push(), sdb_array_remove(), sdb_array_set(), sdb_array_set_num(), sdb_array_sort(), sdb_array_sort_num(), sdb_atoi(), sdb_concat(), sdb_const_get(), sdb_decode(), sdb_encode(), sdb_fmt(), sdb_foreach_list(), sdb_foreach_match(), sdb_get(), sdb_hash(), sdb_isnum(), sdb_itoa(), sdb_ns(), sdb_num_base(), sdb_num_dec(), sdb_num_inc(), SDB_OPTION_FS, sdb_query_file(), SDB_RS, sdb_set(), sdb_set_owned(), sdb_type(), sdb_uncat(), sdb_unset_like(), sdbkv_key(), sdbkv_value(), snprintf, st64, strbuf_append(), strbuf_free(), strbuf_new(), strdup(), autogen_x86imm::tmp, type, ut64(), val, w, and walk_namespace().

Referenced by autocomplete_sdb(), cmd_analysis_esil(), DEFINE_HANDLE_TS_FCN_AND_SYMBOL(), num_callback(), rop_classify_arithmetic(), rop_classify_arithmetic_const(), rop_classify_constant(), rop_classify_mov(), rop_classify_nops(), rop_kuery(), rz_cmd_info_kuery(), rz_cmd_kuery(), rz_core_agraph_print_sdb(), rz_core_cmd_foreach(), rz_core_cmd_subst_i(), rz_core_graph_print(), rz_core_kuery_print(), sdb_query(), and sdb_querysf().

◆ sdb_querysf()

RZ_API char* sdb_querysf ( Sdb s,
char *  buf,
size_t  buflen,
const char *  fmt,
  ... 
)

Definition at line 68 of file query.c.

68  {
69  char string[4096];
70  char *ret;
71  va_list ap;
72  va_start(ap, fmt);
73  vsnprintf(string, sizeof(string), fmt, ap);
74  ret = sdb_querys(s, buf, buflen, string);
75  va_end(ap);
76  return ret;
77 }
ut64 buflen
Definition: core.c:76

References buflen, s, sdb_querys(), and vsnprintf.

◆ sdb_remove()

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

Definition at line 299 of file sdb.c.

299  {
300  return sdb_ht_delete(s->ht, key);
301 }
RZ_API bool sdb_ht_delete(HtPP *ht, const char *key)
Definition: sdbht.c:67

References key, s, and sdb_ht_delete().

Referenced by __delete_almighty(), rename_key(), rz_analysis_class_delete(), rz_analysis_class_delete_attr_raw(), rz_analysis_class_rename_attr_raw(), and sdb_sync().

◆ 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
RZ_API void sdb_ht_free(HtPP *ht)
Definition: sdbht.c:63

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 *  data,
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_owned()

◆ sdb_setup()

RZ_API void sdb_setup ( Sdb s,
int  options 
)

◆ 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_insert(Sdb *s, const char *key, const char *val)
Definition: disk.c:67
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_text_load()

RZ_API bool sdb_text_load ( Sdb s,
const char *  file 
)

Definition at line 405 of file text.c.

405  {
406  int fd = open(file, O_RDONLY | O_BINARY);
407  if (fd < 0) {
408  return false;
409  }
410  bool r = false;
411  struct stat st;
412  if (fstat(fd, &st) || !st.st_size) {
413  goto beach;
414  }
415 #if HAVE_HEADER_SYS_MMAN_H
416  char *x = mmap(0, st.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
417  if (x == MAP_FAILED) {
418  goto beach;
419  }
420 #else
421  char *x = calloc(1, st.st_size);
422  if (!x) {
423  goto beach;
424  }
425  if (read(fd, x, st.st_size) != st.st_size) {
426  free(x);
427  goto beach;
428  }
429 #endif
430  r = sdb_text_load_buf(s, x, st.st_size);
431 #if HAVE_HEADER_SYS_MMAN_H
432  munmap(x, st.st_size);
433 #else
434  free(x);
435 #endif
436 beach:
437  close(fd);
438  return r;
439 }
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags mmap
Definition: sflib.h:115
static const void static count static fd struct stat static buf struct pollfd unsigned static timeout void static offset munmap
Definition: sflib.h:43
#define PROT_READ
Definition: sftypes.h:95
#define PROT_WRITE
Definition: sftypes.h:96
#define MAP_PRIVATE
Definition: sftypes.h:102
RZ_API bool sdb_text_load_buf(Sdb *s, char *buf, size_t sz)
Definition: text.c:386

References calloc(), close, fd, free(), fstat, MAP_PRIVATE, mmap, munmap, O_BINARY, O_RDONLY, PROT_READ, PROT_WRITE, r, read(), s, sdb_text_load_buf(), and x.

Referenced by createdb(), and rz_project_load_file_raw().

◆ sdb_text_load_buf()

RZ_API bool sdb_text_load_buf ( Sdb s,
char *  buf,
size_t  sz 
)

Definition at line 386 of file text.c.

386  {
387  if (!sz) {
388  return true;
389  }
390  LoadCtx ctx;
391  if (!load_ctx_init(&ctx, s, buf, sz)) {
392  return false;
393  }
394  bool ret = true;
395  while (ctx.pos < ctx.bufsz) {
397  }
398  if (ctx.line_begin < ctx.bufsz && ctx.state != STATE_NEWLINE) {
400  }
401  load_ctx_fini(&ctx);
402  return ret;
403 }
Definition: text.c:216
static bool load_ctx_init(LoadCtx *ctx, Sdb *s, char *buf, size_t sz)
Definition: text.c:366
static void load_process_single_char(LoadCtx *ctx)
Definition: text.c:288
static void load_ctx_fini(LoadCtx *ctx)
Definition: text.c:362
@ STATE_NEWLINE
Definition: text.c:210
static bool load_process_final_line(LoadCtx *ctx)
Definition: text.c:336

References load_ctx_fini(), load_ctx_init(), load_process_final_line(), load_process_single_char(), s, and STATE_NEWLINE.

Referenced by createdb(), and sdb_text_load().

◆ sdb_text_save()

RZ_API bool sdb_text_save ( Sdb s,
const char *  file,
bool  sort 
)

Definition at line 199 of file text.c.

199  {
200  int fd = open(file, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
201  if (fd < 0) {
202  return false;
203  }
204  bool r = sdb_text_save_fd(s, fd, sort);
205  close(fd);
206  return r;
207 }
RZ_API bool sdb_text_save_fd(Sdb *s, int fd, bool sort)
Definition: text.c:189

References close, fd, O_BINARY, O_CREAT, O_TRUNC, O_WRONLY, r, s, and sdb_text_save_fd().

Referenced by rz_project_save_file().

◆ sdb_text_save_fd()

RZ_API bool sdb_text_save_fd ( Sdb s,
int  fd,
bool  sort 
)

Definition at line 189 of file text.c.

189  {
190  SdbList *path = ls_new();
191  if (!path) {
192  return false;
193  }
194  bool r = text_save(s, fd, sort, path);
195  ls_free(path);
196  return r;
197 }
static bool text_save(Sdb *s, int fd, bool sort, SdbList *path)
Definition: text.c:148

References fd, ls_free(), ls_new(), path, r, s, and text_save().

Referenced by sdb_text_save().

◆ sdb_type()

RZ_API const char* sdb_type ( const char *  k)

Definition at line 231 of file util.c.

231  {
232  if (!k || !*k) {
233  return "undefined";
234  }
235  if (sdb_isnum(k)) {
236  return "number";
237  }
238  if (strchr(k, ',')) {
239  return "array";
240  }
241  if (!strcmp(k, "true") || !strcmp(k, "false")) {
242  return "boolean";
243  }
244  return "string";
245 }
RZ_API int sdb_isnum(const char *s)
Definition: util.c:216

References k, and sdb_isnum().

Referenced by sdb_querys().

◆ 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 }

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_unlock()

RZ_API void sdb_unlock ( const char *  s)

Definition at line 60 of file lock.c.

60  {
61  // flock (fd, LOCK_UN);
62  unlink(s);
63 }

References s, and unlink.

Referenced by sdb_file(), and sdb_fini().

◆ sdb_unow()

RZ_API ut64 sdb_unow ( void  )

◆ 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 }