Rizin
unix-like reverse engineering framework and cli tools
util.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include "sdb.h"
5 #include <rz_util/rz_time.h>
6 
7 RZ_API ut32 sdb_hash_len(const char *s, ut32 *len) {
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 }
21 
22 RZ_API ut32 sdb_hash(const char *s) {
23  return sdb_hash_len(s, NULL);
24 }
25 
26 RZ_API ut8 sdb_hash_byte(const char *s) {
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 }
31 
32 RZ_API const char *sdb_itoca(ut64 n) {
33  return sdb_itoa(n, sdb_fmt(NULL), 16);
34 }
35 
36 // assert (sizeof (s)>64)
37 // if s is null, the returned pointer must be freed!!
38 RZ_API char *sdb_itoa(ut64 n, char *s, int base) {
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 }
87 
88 RZ_API ut64 sdb_atoi(const char *s) {
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 }
97 
98 // NOTE: Reuses memory. probably not bindings friendly..
99 RZ_API char *sdb_array_compact(char *p) {
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 }
114 
115 // NOTE: Reuses memory. probably not bindings friendly..
116 RZ_API char *sdb_aslice(char *out, int from, int to) {
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 }
148 
149 // TODO: find better name for it
150 // TODO: optimize, because this is the main bottleneck for sdb_array_set()
151 RZ_API int sdb_alen(const char *str) {
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 }
166 
167 RZ_API int sdb_alen_ignore_empty(const char *str) {
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 }
191 
192 RZ_API char *sdb_anext(char *str, char **next) {
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 }
205 
206 RZ_API const char *sdb_const_anext(const char *str) {
207  const char *p = strchr(str, SDB_RS);
208  return p ? p + 1 : NULL;
209 }
210 
212  ut64 usec = rz_time_now();
213  return usec / 1000000;
214 }
215 
216 RZ_API int sdb_isnum(const char *s) {
217  const char vs = *s;
218  return ((vs == '-' || vs == '+') || (vs >= '0' && vs <= '9'));
219 }
220 
221 RZ_API int sdb_num_base(const char *s) {
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 }
230 
231 RZ_API const char *sdb_type(const char *k) {
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 }
size_t len
Definition: 6502dis.c:15
#define e(frag)
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define CDB_HASHSTART
Definition: cdb.h:18
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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
uint32_t ut32
const char * k
Definition: dsignal.c:11
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
RZ_API char * sdb_anext(char *str, char **next)
Definition: util.c:192
RZ_API ut8 sdb_hash_byte(const char *s)
Definition: util.c:26
RZ_API ut64 sdb_now(void)
Definition: util.c:211
RZ_API int sdb_isnum(const char *s)
Definition: util.c:216
RZ_API char * sdb_aslice(char *out, int from, int to)
Definition: util.c:116
RZ_API int sdb_alen(const char *str)
Definition: util.c:151
RZ_API char * sdb_itoa(ut64 n, char *s, int base)
Definition: util.c:38
RZ_API ut32 sdb_hash(const char *s)
Definition: util.c:22
RZ_API char * sdb_array_compact(char *p)
Definition: util.c:99
RZ_API const char * sdb_type(const char *k)
Definition: util.c:231
RZ_API const char * sdb_const_anext(const char *str)
Definition: util.c:206
RZ_API int sdb_alen_ignore_empty(const char *str)
Definition: util.c:167
RZ_API ut64 sdb_atoi(const char *s)
Definition: util.c:88
RZ_API const char * sdb_itoca(ut64 n)
Definition: util.c:32
RZ_API ut32 sdb_hash_len(const char *s, ut32 *len)
Definition: util.c:7
RZ_API int sdb_num_base(const char *s)
Definition: util.c:221
void * malloc(size_t size)
Definition: malloc.c:123
#define copy_string(type_code_str, str_for_copy)
int n
Definition: mipsasm.c:19
int idx
Definition: setup.py:197
static RzSocket * s
Definition: rtr.c:28
RZ_API ut64 rz_time_now(void)
Returns the current time in microseconds.
Definition: time.c:88
#define SDB_NUM_BASE
Definition: sdb.h:50
#define SDB_RS
Definition: sdb.h:47
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
#define h(i)
Definition: sha256.c:48
ut64(WINAPI *w32_GetEnabledXStateFeatures)()