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

Go to the source code of this file.

Macros

#define concat(x)
 
#define KL   256
 
#define KN   16
 

Functions

RZ_API char * sdb_fmt (const char *fmt,...)
 
RZ_API char * sdb_fmt_tostr (void *p, const char *fmt)
 
RZ_API int sdb_fmt_tobin (const char *_str, const char *fmt, void *stru)
 
RZ_API void sdb_fmt_free (void *stru, const char *fmt)
 
RZ_API int sdb_fmt_init (void *p, const char *fmt)
 
static const char * sdb_anext2 (const char *str, const char **next)
 
RZ_API ut64sdb_fmt_array_num (const char *list)
 
RZ_API char ** sdb_fmt_array (const char *list)
 

Macro Definition Documentation

◆ concat

#define concat (   x)
Value:
if (x) { \
int size = 2 + strlen(x ? x : "") + (out ? strlen(out) + 4 : 0); \
if (out) { \
char *o = realloc(out, size); \
if (o) { \
strcat(o, ","); \
strcat(o, x); \
out = o; \
} \
} else { \
out = strdup(x); \
} \
}
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
voidpf void uLong size
Definition: ioapi.h:138
void * realloc(void *ptr, size_t size)
Definition: malloc.c:144
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 x
Definition: mipsasm.c:20

Definition at line 11 of file fmt.c.

◆ KL

#define KL   256

◆ KN

#define KN   16

Function Documentation

◆ sdb_anext2()

static const char* sdb_anext2 ( const char *  str,
const char **  next 
)
static

Definition at line 170 of file fmt.c.

170  {
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 }
#define NULL
Definition: cris-opc.c:27
void * p
Definition: libc.cpp:67
#define SDB_RS
Definition: sdb.h:47

References NULL, p, SDB_RS, and cmd_descs_generate::str.

Referenced by sdb_fmt_array(), and sdb_fmt_array_num().

◆ sdb_fmt()

RZ_API char* sdb_fmt ( const char *  fmt,
  ... 
)

Definition at line 26 of file fmt.c.

26  {
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 }
#define KL
#define KN
vsnprintf
Definition: kernel.h:366
int n
Definition: mipsasm.c:19

References KL, KN, n, and vsnprintf.

Referenced by _6502Disass(), __add_menu(), __getMethod(), __io_kill(), __system(), __update_menu(), _cb_hit(), _fill_bin_symbol(), _store_resource_sdb(), add_string_ref(), add_tls_callbacks(), addptr(), agraph_set_layout(), annotated_hexdump(), append_shortcut(), art_header_load(), bin_elf_versioninfo_verneed(), bin_elf_versioninfo_versym(), bin_pe_store_tls_callbacks(), bin_pe_versioninfo(), cmd_print_blocks(), dalvik_disassemble(), decompile_vm(), disassemble(), do_asm_search(), ds_disassemble(), extract_arg(), flags_to_json(), gdbr_connect(), gdbr_step(), get_bbnodes(), get_bbupdate(), get_class_ro_t(), get_libs(), i4004dis(), info(), init_items(), insn_to_str(), kv_loadlibs(), lmf_header_load(), MACH0_(), mem(), parse_format(), parse_segments(), pic_baseline_disassemble(), pic_midrange_disassemble(), pic_pic18_disassemble(), print_rop(), resolve_mig_subsystem(), resolveModuleOrdinal(), riscv_disassemble(), rz_agraph_add_edge(), rz_agraph_add_edge_at(), rz_agraph_add_node_with_color(), rz_agraph_del_edge(), rz_agraph_del_node(), rz_analysis_cc_arg(), rz_analysis_cc_error(), rz_analysis_cc_get(), rz_analysis_cc_max_arg(), rz_analysis_cc_ret(), rz_analysis_cc_self(), rz_analysis_cc_set(), rz_analysis_function_get_json(), rz_asm_disassemble(), rz_bin_file_add_method(), rz_bin_filter_sym(), rz_bin_mdmp_init_directory_entry(), rz_bin_object_rebuild_classes_ht(), rz_bin_symbol_name(), rz_cmd_debug(), rz_core_analysis_coderefs(), rz_core_analysis_datarefs(), rz_core_analysis_esil(), rz_core_bin_method_flags_str(), rz_core_magic_at(), rz_core_rtr_cmd(), rz_core_sysenv_begin(), rz_core_visual_define(), rz_debug_select(), rz_debug_trace_add(), rz_debug_trace_get(), rz_flag_tags_get(), rz_flag_tags_set(), rz_project_save(), rz_syscall_get(), rz_table_visual_list(), sdb_itoca(), sdb_journal_filename(), sdb_journal_log(), sdb_querys(), serialize_checkpoints(), serialize_memory_cb(), serialize_register_cb(), set_fcn_name_from_flag(), snesDisass(), symbols(), symbols_from_stubs(), syscallNumber(), type_match(), windbg_map_get(), and z80Disass().

◆ sdb_fmt_array()

RZ_API char** sdb_fmt_array ( const char *  list)

Definition at line 209 of file fmt.c.

209  {
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
static const char * sdb_anext2(const char *str, const char **next)
Definition: fmt.c:170
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
RZ_API int sdb_alen(const char *str)
Definition: util.c:151

References len, list(), malloc(), memcpy(), NULL, sdb_alen(), sdb_anext2(), and cmd_descs_generate::str.

Referenced by sdb_array_add_sorted(), and sdb_array_sort().

◆ sdb_fmt_array_num()

RZ_API ut64* sdb_fmt_array_num ( const char *  list)

Definition at line 184 of file fmt.c.

184  {
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 }
uint32_t ut32
RZ_API ut64 sdb_atoi(const char *s)
Definition: util.c:88
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References len, list(), malloc(), n, NULL, sdb_alen(), sdb_anext2(), sdb_atoi(), cmd_descs_generate::str, and ut64().

Referenced by sdb_array_sort_num().

◆ sdb_fmt_free()

RZ_API void sdb_fmt_free ( void *  stru,
const char *  fmt 
)

Definition at line 128 of file fmt.c.

128  {
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 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
#define RZ_MAX(x, y)

References free(), len, n, and RZ_MAX.

◆ sdb_fmt_init()

RZ_API int sdb_fmt_init ( void *  p,
const char *  fmt 
)

Definition at line 151 of file fmt.c.

151  {
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 }
#define ut8
Definition: dcpu16.h:8
return memset(p, 0, total)

References len, memset(), p, ut64(), and ut8.

◆ sdb_fmt_tobin()

RZ_API int sdb_fmt_tobin ( const char *  _str,
const char *  fmt,
void *  stru 
)

Definition at line 84 of file fmt.c.

84  {
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 }
RZ_API ut8 * sdb_decode(const char *in, int *len)
Definition: base64.c:37
int idx
Definition: setup.py:197
RZ_API char * sdb_anext(char *str, char **next)
Definition: util.c:192
int size_t
Definition: sftypes.h:40

References free(), setup::idx, n, RZ_MAX, sdb_anext(), sdb_atoi(), sdb_decode(), cmd_descs_generate::str, strdup(), and ut64().

◆ sdb_fmt_tostr()

RZ_API char* sdb_fmt_tostr ( void *  p,
const char *  fmt 
)

Definition at line 43 of file fmt.c.

43  {
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 }
RZ_API char * sdb_encode(const ut8 *bin, int len)
Definition: base64.c:18
#define concat(x)
Definition: fmt.c:11
voidpf void * buf
Definition: ioapi.h:138
RZ_API char * sdb_itoa(ut64 n, char *s, int base)
Definition: util.c:38

References concat, free(), len, n, NULL, out, p, RZ_MAX, sdb_encode(), sdb_itoa(), and ut64().

Referenced by sdb_array_sort_num().