Rizin
unix-like reverse engineering framework and cli tools
query.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdarg.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <ctype.h>
10 #include "sdb.h"
11 
12 typedef struct {
13  char *buf;
14  int len;
15  int size;
16 } StrBuf;
17 
18 static StrBuf *strbuf_new(void) {
19  return calloc(sizeof(StrBuf), 1);
20 }
21 
22 static StrBuf *strbuf_append(StrBuf *sb, const char *str, const int nl) {
23  if (!sb || !str || nl < 0) {
24  return sb;
25  }
26  int len = strlen(str);
27  if ((sb->len + len + 2) >= sb->size) {
28  int newsize = sb->size + len + 256;
29  char *b = realloc(sb->buf, newsize);
31  if (!b) {
32  return NULL;
33  }
34  sb->buf = b;
35  sb->size = newsize;
36  }
37  if (sb->buf && str) {
38  memcpy(sb->buf + sb->len, str, len);
39  sb->len += len;
40  }
41  if (sb->buf && nl) {
42  sb->buf[sb->len++] = '\n';
43  len++;
44  }
45  if (sb->buf) {
46  sb->buf[sb->len] = 0;
47  }
48  return sb;
49 }
50 
52  free(sb->buf);
53  free(sb);
54  return NULL;
55 }
56 
57 RZ_API int sdb_queryf(Sdb *s, const char *fmt, ...) {
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 }
67 
68 RZ_API char *sdb_querysf(Sdb *s, char *buf, size_t buflen, const char *fmt, ...) {
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 }
78 
79 // TODO: Reimplement as a function with optimized concat
80 #define out_concat(x) \
81  if (x && *x) { \
82  strbuf_append(out, x, 1); \
83  }
84 
85 typedef struct {
87  int encode;
88  char *root;
90 
91 static bool foreach_list_cb(void *user, const char *k, const char *v) {
92  ForeachListUser *rlu = user;
93  char *line, *root;
94  int rlen, klen, vlen;
95  ut8 *v2 = NULL;
96  if (!rlu) {
97  return false;
98  }
99  root = rlu->root;
100  klen = strlen(k);
101  if (rlu->encode) {
102  v2 = sdb_decode(v, NULL);
103  if (v2) {
104  v = (const char *)v2;
105  }
106  }
107  vlen = strlen(v);
108  if (root) {
109  rlen = strlen(root);
110  line = malloc(klen + vlen + rlen + 3);
111  if (!line) {
112  free(v2);
113  return false;
114  }
115  memcpy(line, root, rlen);
116  line[rlen] = '/'; /*append the '/' at the end of the namespace */
117  memcpy(line + rlen + 1, k, klen);
118  line[rlen + klen + 1] = '=';
119  memcpy(line + rlen + klen + 2, v, vlen + 1);
120  } else {
121  line = malloc(klen + vlen + 2);
122  if (!line) {
123  free(v2);
124  return false;
125  }
126  memcpy(line, k, klen);
127  line[klen] = '=';
128  memcpy(line + klen + 1, v, vlen + 1);
129  }
130  strbuf_append(rlu->out, line, 1);
131  free(v2);
132  free(line);
133  return true;
134 }
135 
136 static void walk_namespace(StrBuf *sb, char *root, int left, char *p, SdbNs *ns, int encode) {
137  int len;
138  SdbListIter *it;
139  char *_out, *out = sb->buf;
140  SdbNs *n;
141  ForeachListUser user = { sb, encode, root };
142  char *roote = root + strlen(root);
143  if (!ns->sdb) {
144  return;
145  }
146  /*Pick all key=value in the local ns*/
147  sdb_foreach(ns->sdb, foreach_list_cb, &user);
148 
149  /*Pick "sub"-ns*/
150  ls_foreach (ns->sdb->ns, it, n) {
151  len = strlen(n->name);
152  p[0] = '/';
153  if (len + 2 < left) {
154  memcpy(p + 1, n->name, len + 1);
155  left -= len + 2;
156  }
157  _out = out;
158  walk_namespace(sb, root, left,
159  roote + len + 1, n, encode);
160  out = _out;
161  }
162 }
163 
164 RZ_API char *sdb_querys(Sdb *r, char *buf, size_t len, const char *_cmd) {
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 }
784 
785 RZ_API int sdb_query(Sdb *s, const char *cmd) {
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 }
799 
800 RZ_API int sdb_query_lines(Sdb *s, const char *cmd) {
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 }
823 
824 static char *slurp(const char *file) {
825  int ret, fd;
826  char *text;
827  long sz;
828  if (!file || !*file)
829  return NULL;
830  fd = open(file, O_RDONLY);
831  if (fd == -1) {
832  return NULL;
833  }
834  sz = lseek(fd, 0, SEEK_END);
835  if (sz < 0) {
836  close(fd);
837  return NULL;
838  }
839  lseek(fd, 0, SEEK_SET);
840  text = malloc(sz + 1);
841  if (!text) {
842  close(fd);
843  return NULL;
844  }
845  ret = read(fd, text, sz);
846  if (ret != sz) {
847  free(text);
848  text = NULL;
849  } else {
850  text[sz] = 0;
851  }
852  close(fd);
853  return text;
854 }
855 
856 RZ_API int sdb_query_file(Sdb *s, const char *file) {
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 }
size_t len
Definition: 6502dis.c:15
ut8 op
Definition: 6502dis.c:13
lzma_index ** i
Definition: index.h:629
ut16 val
Definition: armass64_const.h:6
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_set(Sdb *s, const char *key, int idx, const char *val, ut32 cas)
Definition: array.c:342
RZ_API int sdb_array_add(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:224
RZ_API bool sdb_array_push(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:533
RZ_API int sdb_array_insert(Sdb *s, const char *key, int idx, const char *val, ut32 cas)
Definition: array.c:139
RZ_API char * sdb_array_pop(Sdb *s, const char *key, ut32 *cas)
Definition: array.c:589
RZ_API int sdb_array_remove(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: array.c:436
RZ_API int sdb_array_delete(Sdb *s, const char *key, int idx, ut32 cas)
Definition: array.c:456
RZ_API void sdb_array_sort_num(Sdb *s, const char *key, ut32 cas)
Definition: array.c:667
RZ_API int sdb_array_set_num(Sdb *s, const char *key, int idx, ut64 val, ut32 cas)
Definition: array.c:204
RZ_API ut64 sdb_array_get_num(Sdb *s, const char *key, int idx, ut32 *cas)
Definition: array.c:63
RZ_API ut8 * sdb_decode(const char *in, int *len)
Definition: base64.c:37
RZ_API char * sdb_encode(const ut8 *bin, int len)
Definition: base64.c:18
static SblHeader sb
Definition: bin_mbn.c:26
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
#define w
Definition: crypto_rc6.c:13
static static fork const void static count close
Definition: sflib.h:33
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
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
const char * k
Definition: dsignal.c:11
const char * v
Definition: dsignal.c:12
int root
Definition: enough.c:226
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
static void encode(size_t size, lzma_action action)
Definition: full_flush.c:25
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
snprintf
Definition: kernel.h:364
vsnprintf
Definition: kernel.h:366
uint8_t ut8
Definition: lh5801.h:11
void * p
Definition: libc.cpp:67
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
static void list(RzEgg *egg)
Definition: rz-gg.c:52
static char * slurp(const char *file)
Definition: query.c:824
RZ_API char * sdb_querysf(Sdb *s, char *buf, size_t buflen, const char *fmt,...)
Definition: query.c:68
static bool foreach_list_cb(void *user, const char *k, const char *v)
Definition: query.c:91
RZ_API int sdb_queryf(Sdb *s, const char *fmt,...)
Definition: query.c:57
RZ_API int sdb_query(Sdb *s, const char *cmd)
Definition: query.c:785
static StrBuf * strbuf_free(StrBuf *sb)
Definition: query.c:51
static StrBuf * strbuf_new(void)
Definition: query.c:18
RZ_API char * sdb_querys(Sdb *r, char *buf, size_t len, const char *_cmd)
Definition: query.c:164
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
RZ_API int sdb_query_lines(Sdb *s, const char *cmd)
Definition: query.c:800
#define out_concat(x)
Definition: query.c:80
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")
RZ_API void ls_free(SdbList *list)
Definition: ls.c:191
#define ls_foreach(list, it, pos)
Definition: ls.h:31
@ ok
Definition: lz4.c:1706
int n
Definition: mipsasm.c:19
int type
Definition: mipsasm.c:17
line
Definition: setup.py:34
int idx
Definition: setup.py:197
RZ_API Sdb * sdb_ns(Sdb *s, const char *name, int create)
Definition: ns.c:186
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 eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
#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 int sdb_set_owned(Sdb *s, const char *key, char *val, ut32 cas)
Definition: sdb.c:607
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
RZ_API char * sdb_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:290
RZ_API SdbList * sdb_foreach_match(Sdb *s, const char *expr, bool single)
Definition: sdb.c:710
RZ_API const char * sdb_const_get(Sdb *s, const char *key, ut32 *cas)
Definition: sdb.c:279
RZ_API bool sdb_foreach(Sdb *s, SdbForeachCallback cb, void *user)
Definition: sdb.c:758
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
#define SDB_OPTION_FS
Definition: sdb.h:57
RZ_API int sdb_isnum(const char *s)
Definition: util.c:216
RZ_API char * sdb_itoa(ut64 n, char *s, int base)
Definition: util.c:38
RZ_API const char * sdb_type(const char *k)
Definition: util.c:231
RZ_API ut32 sdb_hash(const char *key)
Definition: util.c:22
#define SDB_RS
Definition: sdb.h:47
RZ_API ut64 sdb_atoi(const char *s)
Definition: util.c:88
RZ_API int sdb_num_base(const char *s)
Definition: util.c:221
static char * sdbkv_key(const SdbKv *kv)
Definition: sdbht.h:21
static char * sdbkv_value(const SdbKv *kv)
Definition: sdbht.h:25
#define O_RDONLY
Definition: sftypes.h:486
#define d(i)
Definition: sha256.c:44
#define b(i)
Definition: sha256.c:42
int encode
Definition: query.c:87
char * root
Definition: query.c:88
StrBuf * out
Definition: query.c:86
Definition: query.c:12
int size
Definition: query.c:15
char * buf
Definition: query.c:13
int len
Definition: query.c:14
Definition: gzappend.c:170
Definition: ls.h:17
Definition: ls.h:22
Definition: sdbht.h:14
Definition: sdb.h:88
char * name
Definition: sdb.h:89
Sdb * sdb
Definition: sdb.h:91
Definition: sdb.h:63
SdbList * ns
Definition: sdb.h:82
ut64 buflen
Definition: core.c:76
#define fail(test)
Definition: tests.h:29
Definition: dis.c:32
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
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