Rizin
unix-like reverse engineering framework and cli tools
fmt.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2014-2018 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include "sdb.h"
5 #include <stdarg.h>
6 #include <string.h>
7 
8 // TODO: convert into a function
9 // TODO: Add 'a' format for array of pointers null terminated??
10 // XXX SLOW CONCAT
11 #define concat(x) \
12  if (x) { \
13  int size = 2 + strlen(x ? x : "") + (out ? strlen(out) + 4 : 0); \
14  if (out) { \
15  char *o = realloc(out, size); \
16  if (o) { \
17  strcat(o, ","); \
18  strcat(o, x); \
19  out = o; \
20  } \
21  } else { \
22  out = strdup(x); \
23  } \
24  }
25 
26 RZ_API char *sdb_fmt(const char *fmt, ...) {
27 #define KL 256
28 #define KN 16
29  static char Key[KN][KL];
30  static int n = 0;
31  va_list ap;
32  va_start(ap, fmt);
33  n = (n + 1) % KN;
34  if (fmt) {
35  *Key[n] = 0;
36  vsnprintf(Key[n], KL - 1, fmt, ap);
37  Key[n][KL - 1] = 0;
38  }
39  va_end(ap);
40  return Key[n];
41 }
42 
43 RZ_API char *sdb_fmt_tostr(void *p, const char *fmt) {
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 }
82 
83 // TODO: return false if array length != fmt length
84 RZ_API int sdb_fmt_tobin(const char *_str, const char *fmt, void *stru) {
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 }
127 
128 RZ_API void sdb_fmt_free(void *stru, const char *fmt) {
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 }
150 
151 RZ_API int sdb_fmt_init(void *p, const char *fmt) {
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 }
169 
170 static const char *sdb_anext2(const char *str, const char **next) {
171  char *nxt, *p = strchr(str, SDB_RS);
172  if (p) {
173  nxt = p + 1;
174  } else {
175  nxt = NULL;
176  }
177  if (next) {
178  *next = nxt;
179  }
180  return str;
181 }
182 
183 // TODO: move this into fmt?
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 }
208 
209 RZ_API char **sdb_fmt_array(const char *list) {
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 }
size_t len
Definition: 6502dis.c:15
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
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 ut8
Definition: dcpu16.h:8
uint32_t ut32
RZ_API char * sdb_fmt_tostr(void *p, const char *fmt)
Definition: fmt.c:43
static const char * sdb_anext2(const char *str, const char **next)
Definition: fmt.c:170
RZ_API char ** sdb_fmt_array(const char *list)
Definition: fmt.c:209
RZ_API void sdb_fmt_free(void *stru, const char *fmt)
Definition: fmt.c:128
#define KL
RZ_API int sdb_fmt_init(void *p, const char *fmt)
Definition: fmt.c:151
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API int sdb_fmt_tobin(const char *_str, const char *fmt, void *stru)
Definition: fmt.c:84
#define concat(x)
Definition: fmt.c:11
#define KN
RZ_API ut64 * sdb_fmt_array_num(const char *list)
Definition: fmt.c:184
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
voidpf void * buf
Definition: ioapi.h:138
vsnprintf
Definition: kernel.h:366
uint8_t ut8
Definition: lh5801.h:11
return memset(p, 0, total)
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
void * malloc(size_t size)
Definition: malloc.c:123
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")
int n
Definition: mipsasm.c:19
int idx
Definition: setup.py:197
#define RZ_MAX(x, y)
RZ_API char * sdb_anext(char *str, char **next)
Definition: util.c:192
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
#define SDB_RS
Definition: sdb.h:47
RZ_API ut64 sdb_atoi(const char *s)
Definition: util.c:88
int size_t
Definition: sftypes.h:40
ut64(WINAPI *w32_GetEnabledXStateFeatures)()