Rizin
unix-like reverse engineering framework and cli tools
array.c File Reference
#include "sdb.h"
#include <limits.h>

Go to the source code of this file.

Functions

static const char * Aindexof (const char *str, int idx)
 
static int astrcmp (const char *a, const char *b)
 
static int cstring_cmp (const void *a, const void *b)
 
static int int_cmp (const void *a, const void *b)
 
RZ_API ut64 sdb_array_get_num (Sdb *s, const char *key, int idx, ut32 *cas)
 
RZ_API char * sdb_array_get (Sdb *s, const char *key, int idx, 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_insert (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 int sdb_array_add_num (Sdb *s, const char *key, ut64 val, ut32 cas)
 
RZ_API int sdb_array_add (Sdb *s, const char *key, const char *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_unset (Sdb *s, const char *key, int idx, 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 int sdb_array_set (Sdb *s, const char *key, int idx, const char *val, ut32 cas)
 
RZ_API int sdb_array_remove_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_remove (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API int sdb_array_delete (Sdb *s, const char *key, int idx, ut32 cas)
 
RZ_API bool sdb_array_contains_num (Sdb *s, const char *key, ut64 num, ut32 *cas)
 
RZ_API bool sdb_array_contains (Sdb *s, const char *key, const char *val, ut32 *cas)
 
RZ_API int sdb_array_size (Sdb *s, const char *key)
 
RZ_API int sdb_array_length (Sdb *s, const char *key)
 
RZ_API int sdb_array_push_num (Sdb *s, const char *key, ut64 num, ut32 cas)
 
RZ_API bool sdb_array_push (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API bool sdb_array_prepend_num (Sdb *s, const char *key, ut64 num, ut32 cas)
 
RZ_API bool sdb_array_prepend (Sdb *s, const char *key, const char *val, ut32 cas)
 
RZ_API ut64 sdb_array_pop_num (Sdb *s, const char *key, ut32 *cas)
 
RZ_API char * sdb_array_pop (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_array_sort (Sdb *s, const char *key, ut32 cas)
 
RZ_API void sdb_array_sort_num (Sdb *s, const char *key, ut32 cas)
 

Function Documentation

◆ Aindexof()

static const char* Aindexof ( const char *  str,
int  idx 
)
static

Definition at line 9 of file array.c.

9  {
10  int len = 0;
11  const char *n, *p = str;
12  for (len = 0;; len++) {
13  if (len == idx) {
14  return p;
15  }
16  if (!(n = strchr(p, SDB_RS))) {
17  break;
18  }
19  p = n + 1;
20  }
21  return NULL;
22 }
size_t len
Definition: 6502dis.c:15
#define NULL
Definition: cris-opc.c:27
void * p
Definition: libc.cpp:67
int n
Definition: mipsasm.c:19
int idx
Definition: setup.py:197
#define SDB_RS
Definition: sdb.h:47

References setup::idx, len, n, NULL, p, SDB_RS, and cmd_descs_generate::str.

Referenced by sdb_array_insert(), and sdb_array_set().

◆ astrcmp()

static int astrcmp ( const char *  a,
const char *  b 
)
static

Definition at line 24 of file array.c.

24  {
25  register char va = *a;
26  register char vb = *b;
27  for (;;) {
28  if (va == '\0' || va == SDB_RS) {
29  if (vb == '\0' || vb == SDB_RS) {
30  return 0;
31  }
32  return -1;
33  }
34  if (vb == '\0' || vb == SDB_RS) {
35  return 1;
36  }
37  if (va != vb) {
38  return (va > vb) ? 1 : -1;
39  }
40  va = *(++a);
41  vb = *(++b);
42  }
43 }
#define b(i)
Definition: sha256.c:42
#define a(i)
Definition: sha256.c:41

References a, b, SDB_RS, and patch_major_os_version::vb.

Referenced by cstring_cmp(), sdb_array_add_sorted(), sdb_array_indexof(), and sdb_array_remove().

◆ cstring_cmp()

static int cstring_cmp ( const void *  a,
const void *  b 
)
inlinestatic

Definition at line 45 of file array.c.

45  {
46  const char **va = (const char **)a;
47  const char **vb = (const char **)b;
48  return astrcmp(*va, *vb);
49 }
static int astrcmp(const char *a, const char *b)
Definition: array.c:24

References a, astrcmp(), b, and patch_major_os_version::vb.

Referenced by sdb_array_add_sorted(), and sdb_array_sort().

◆ int_cmp()

static int int_cmp ( const void *  a,
const void *  b 
)
inlinestatic

Definition at line 51 of file array.c.

51  {
52  const ut64 va = *(const ut64 *)a;
53  const ut64 vb = *(const ut64 *)b;
54  if (va > vb) {
55  return 1;
56  }
57  if (va < vb) {
58  return -1;
59  }
60  return 0;
61 }
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References a, b, ut64(), and patch_major_os_version::vb.

Referenced by sdb_array_sort_num().

◆ 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 }
ut16 val
Definition: armass64_const.h:6
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
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

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

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 int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
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_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  num,
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  idx,
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 }
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_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
int x
Definition: mipsasm.c:20
#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_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

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  num,
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  idx,
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().