Rizin
unix-like reverse engineering framework and cli tools
util.c File Reference
#include "sdb.h"
#include <rz_util/rz_time.h>

Go to the source code of this file.

Functions

RZ_API ut32 sdb_hash_len (const char *s, ut32 *len)
 
RZ_API ut32 sdb_hash (const char *s)
 
RZ_API ut8 sdb_hash_byte (const char *s)
 
RZ_API const char * sdb_itoca (ut64 n)
 
RZ_API char * sdb_itoa (ut64 n, char *s, int base)
 
RZ_API ut64 sdb_atoi (const char *s)
 
RZ_API char * sdb_array_compact (char *p)
 
RZ_API char * sdb_aslice (char *out, int from, int to)
 
RZ_API int sdb_alen (const char *str)
 
RZ_API int sdb_alen_ignore_empty (const char *str)
 
RZ_API char * sdb_anext (char *str, char **next)
 
RZ_API const char * sdb_const_anext (const char *str)
 
RZ_API ut64 sdb_now (void)
 
RZ_API int sdb_isnum (const char *s)
 
RZ_API int sdb_num_base (const char *s)
 
RZ_API const char * sdb_type (const char *k)
 

Function Documentation

◆ 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_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_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
int idx
Definition: setup.py:197
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
int size_t
Definition: sftypes.h:40

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 }
static RzSocket * s
Definition: rtr.c:28
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

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_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_hash()

RZ_API ut32 sdb_hash ( const char *  s)

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 }
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
#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 *  s,
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
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

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

Referenced by sdb_hash(), and sdb_hash_byte().

◆ 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 }
lzma_index ** i
Definition: index.h:629
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * malloc(size_t size)
Definition: malloc.c:123
#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_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_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 }
#define SDB_NUM_BASE
Definition: sdb.h:50

References s, and SDB_NUM_BASE.

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

◆ 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 }
const char * k
Definition: dsignal.c:11
RZ_API int sdb_isnum(const char *s)
Definition: util.c:216

References k, and sdb_isnum().

Referenced by sdb_querys().