Rizin
unix-like reverse engineering framework and cli tools
rz-ax.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2007-2020 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
4 #include <rz_main.h>
5 #include <rz_util.h>
6 #include <rz_util/rz_print.h>
7 
8 #define RZ_AX_FLAG_HEX_TO_RAW (1ull << 0) // -s (hexstr -> raw)
9 #define RZ_AX_FLAG_SWAP_ENDIANNESS (1ull << 1) // -e (swap endianness)
10 #define RZ_AX_FLAG_RAW_TO_HEX (1ull << 2) // -S (raw -> hexstr)
11 #define RZ_AX_FLAG_BIN_TO_STR (1ull << 3) // -b (bin -> str)
12 #define RZ_AX_FLAG_STR_TO_DJB2 (1ull << 4) // -x (str -> djb2 hash)
13 #define RZ_AX_FLAG_KEEP_BASE (1ull << 5) // -k (keep base)
14 #define RZ_AX_FLAG_FLOATING_POINT (1ull << 6) // -f (floating point)
15 #define RZ_AX_FLAG_FORCE_INTEGER (1ull << 7) // -d (force integer)
16 #define RZ_AX_FLAG_NUMBER_TO_HEX (1ull << 9) // -n (num -> hex)
17 #define RZ_AX_FLAG_UNITS (1ull << 10) // -u (units)
18 #define RZ_AX_FLAG_TIMESTAMP_TO_STR (1ull << 11) // -t (timestamp -> str)
19 #define RZ_AX_FLAG_BASE64_ENCODE (1ull << 12) // -E (base64 encode)
20 #define RZ_AX_FLAG_BASE64_DECODE (1ull << 13) // -D (base64 decode)
21 #define RZ_AX_FLAG_RAW_TO_LANGBYTES (1ull << 14) // -F (raw -> C or JS or Python bytes)
22 #define RZ_AX_FLAG_NUMBER_TO_HEXSTR (1ull << 15) // -N (num -> escaped hex string)
23 #define RZ_AX_FLAG_SIGNED_WORD (1ull << 16) // -w (signed word)
24 #define RZ_AX_FLAG_STR_TO_BIN (1ull << 17) // -B (str -> bin)
25 #define RZ_AX_FLAG_RIZIN_CMD (1ull << 18) // -r (rizin commands)
26 #define RZ_AX_FLAG_BIN_TO_BIGNUM (1ull << 19) // -L (bin -> hex(bignum))
27 #define RZ_AX_FLAG_DUMP_C_BYTES (1ull << 21) // -i (dump as C byte array)
28 #define RZ_AX_FLAG_OCTAL_TO_RAW (1ull << 22) // -o (octalstr -> raw)
29 #define RZ_AX_FLAG_IPADDR_TO_LONG (1ull << 23) // -I (IP address <-> LONG)
30 #define RZ_AX_FLAG_SET_BITS (1ull << 24) // -p (find position of set bits)
31 
32 #define has_flag(f, x) (f & x)
33 
34 // don't use fixed sized buffers
35 #define STDIN_BUFFER_SIZE 354096
36 static int rax(RzNum *num, char *str, int len, int last, ut64 *flags, int *fm);
37 
38 static int use_stdin(RzNum *num, ut64 *flags, int *fm) {
39  if (!flags) {
40  return 0;
41  }
42  char *buf = calloc(1, STDIN_BUFFER_SIZE + 1);
43  int l;
44  if (!buf) {
45  return 0;
46  }
48  for (l = 0; l >= 0 && l < STDIN_BUFFER_SIZE; l++) {
49  // make sure we don't read beyond boundaries
50  int n = read(0, buf + l, STDIN_BUFFER_SIZE - l);
51  if (n < 1) {
52  break;
53  }
54  l += n;
55  if (buf[l - 1] == 0) {
56  l--;
57  continue;
58  }
59  buf[n] = 0;
60  // if (sflag && strlen (buf) < STDIN_BUFFER_SIZE) // -S
61  buf[STDIN_BUFFER_SIZE] = '\0';
62  if (!rax(num, buf, l, 0, flags, fm)) {
63  break;
64  }
65  l = -1;
66  }
67  } else {
68  l = 1;
69  }
70  if (l > 0) {
71  rax(num, buf, l, 0, flags, fm);
72  }
73  free(buf);
74  return 0;
75 }
76 
77 static int format_output(RzNum *num, char mode, const char *s, int force_mode, ut64 flags) {
78  ut64 n = rz_num_math(num, s);
79  char strbits[65];
80  if (force_mode) {
81  mode = force_mode;
82  }
84  ut64 n2 = n;
85  rz_mem_swapendian((ut8 *)&n, (ut8 *)&n2, 8);
86  if (!(int)n) {
87  n >>= 32;
88  }
89  }
90  switch (mode) {
91  case 'I':
92  printf("%" PFMT64d "\n", n);
93  break;
94  case '0':
95  printf("0x%" PFMT64x "\n", n);
96  break;
97  case 'F': {
98  int n2 = (int)n;
99  float *f = (float *)&n2;
100  printf("%ff\n", *f);
101  } break;
102  case 'f': printf("%.01lf\n", num->fvalue); break;
103  case 'l':
104  RZ_STATIC_ASSERT(sizeof(float) == 4);
105  float f = (float)num->fvalue;
106  ut32 *p = (ut32 *)&f;
107  printf("Fx%08x\n", *p);
108  break;
109  case 'O': printf("0%" PFMT64o "\n", n); break;
110  case 'B':
111  if (n) {
112  rz_num_to_bits(strbits, n);
113  printf("%sb\n", strbits);
114  } else {
115  printf("0b\n");
116  }
117  break;
118  case 'T':
119  if (n) {
120  rz_num_to_trits(strbits, n);
121  printf("%st\n", strbits);
122  } else {
123  printf("0t\n");
124  }
125  break;
126  default:
127  eprintf("Unknown output mode %d\n", mode);
128  break;
129  }
130  return true;
131 }
132 
133 static void print_hex_from_base2(char *base2) {
134  bool first = true;
135  const int len = strlen(base2);
136  if (len < 1) {
137  return;
138  }
139 
140  // we split each section by 8 bits and have bytes.
141  ut32 bytes_size = (len >> 3) + (len & 7 ? 1 : 0);
142  ut8 *bytes = calloc(bytes_size, sizeof(ut8));
143  if (!bytes) {
144  eprintf("cannot allocate %d bytes\n", bytes_size);
145  return;
146  }
147 
148  int c = len & 7;
149  if (c) {
150  // align counter to 8 bits
151  c = 8 - c;
152  }
153  for (int i = 0, j = 0; i < len && j < bytes_size; i++, c++) {
154  if (base2[i] != '1' && base2[i] != '0') {
155  eprintf("invalid base2 number %c at char %d\n", base2[i], i);
156  free(bytes);
157  return;
158  }
159  // c & 7 is c % 8
160  if (c > 0 && !(c & 7)) {
161  j++;
162  }
163  bytes[j] <<= 1;
164  bytes[j] |= base2[i] - '0';
165  }
166 
167  printf("0x");
168  for (int i = 0; i < bytes_size; ++i) {
169  if (first) {
170  if (i != (bytes_size - 1) && !bytes[i]) {
171  continue;
172  }
173  printf("%x", bytes[i]);
174  first = false;
175  } else {
176  printf("%02x", bytes[i]);
177  }
178  }
179  printf("\n");
180  free(bytes);
181 }
182 
183 static void print_ascii_table(void) {
184  printf("%s", ret_ascii_table());
185 }
186 
187 static int help(void) {
188  printf(
189  " =[base] ; rz-ax =10 0x46 -> output in base 10\n"
190  " int -> hex ; rz-ax 10\n"
191  " hex -> int ; rz-ax 0xa\n"
192  " -int -> hex ; rz-ax -77\n"
193  " -hex -> int ; rz-ax 0xffffffb3\n"
194  " int -> bin ; rz-ax b30\n"
195  " int -> ternary ; rz-ax t42\n"
196  " bin -> int ; rz-ax 1010d\n"
197  " ternary -> int ; rz-ax 1010dt\n"
198  " float -> hex ; rz-ax 3.33f\n"
199  " hex -> float ; rz-ax Fx40551ed8\n"
200  " oct -> hex ; rz-ax 35o\n"
201  " hex -> oct ; rz-ax Ox12 (O is a letter)\n"
202  " bin -> hex ; rz-ax 1100011b\n"
203  " hex -> bin ; rz-ax Bx63\n"
204  " ternary -> hex ; rz-ax 212t\n"
205  " hex -> ternary ; rz-ax Tx23\n"
206  " raw -> hex ; rz-ax -S < /binfile\n"
207  " hex -> raw ; rz-ax -s 414141\n"
208  " -l ; append newline to output (for -E/-D/-r/..\n"
209  " -a show ascii table ; rz-ax -a\n"
210  " -b bin -> str ; rz-ax -b 01000101 01110110\n"
211  " -B str -> bin ; rz-ax -B hello\n"
212  " -d force integer ; rz-ax -d 3 -> 3 instead of 0x3\n"
213  " -e swap endianness ; rz-ax -e 0x33\n"
214  " -D base64 decode ;\n"
215  " -E base64 encode ;\n"
216  " -f floating point ; rz-ax -f 6.3+2.1\n"
217  " -F stdin slurp code hex ; rz-ax -F < shellcode.[c/py/js]\n"
218  " -h help ; rz-ax -h\n"
219  " -i dump as C byte array ; rz-ax -i < bytes\n"
220  " -I IP address <-> LONG ; rz-ax -I 3530468537\n"
221  " -k keep base ; rz-ax -k 33+3 -> 36\n"
222  " -L bin -> hex(bignum) ; rz-ax -L 111111111 # 0x1ff\n"
223  " -n binary number ; rz-ax -n 0x1234 # 34120000\n"
224  " -o octalstr -> raw ; rz-ax -o \\162 \\172 # rz\n"
225  " -N binary number ; rz-ax -N 0x1234 # \\x34\\x12\\x00\\x00\n"
226  " -r rz style output ; rz-ax -r 0x1234\n"
227  " -s hexstr -> raw ; rz-ax -s 43 4a 50\n"
228  " -S raw -> hexstr ; rz-ax -S < /bin/ls > ls.hex\n"
229  " -t tstamp -> str ; rz-ax -t 1234567890\n"
230  " -x hash string ; rz-ax -x linux osx\n"
231  " -u units ; rz-ax -u 389289238 # 317.0M\n"
232  " -w signed word ; rz-ax -w 16 0xffff\n"
233  " -v version ; rz-ax -v\n"
234  " -p position of set bits ; rz-ax -p 0xb3\n");
235  return true;
236 }
237 
238 static int rax(RzNum *num, char *str, int len, int last, ut64 *_flags, int *fm) {
239  ut64 flags = *_flags;
240  const char *nl = "";
241  ut8 *buf;
242  char *p, out_mode = has_flag(flags, RZ_AX_FLAG_FORCE_INTEGER) ? 'I' : '0';
243  int i;
245  len = strlen(str);
246  }
248  goto dotherax;
249  }
250  if (*str == '=') {
251  int force_mode = 0;
252  switch (atoi(str + 1)) {
253  case 2: force_mode = 'B'; break;
254  case 3: force_mode = 'T'; break;
255  case 8: force_mode = 'O'; break;
256  case 10: force_mode = 'I'; break;
257  case 16: force_mode = '0'; break;
258  case 0: force_mode = str[1]; break;
259  }
260  *fm = force_mode;
261  return true;
262  }
263 
264  if (*str == '-') {
265  while (str[1] && str[1] != ' ') {
266  switch (str[1]) {
267  case 'l': break;
268  case 'a': print_ascii_table(); return 0;
269  case 's': flags ^= RZ_AX_FLAG_HEX_TO_RAW; break;
270  case 'e': flags ^= RZ_AX_FLAG_SWAP_ENDIANNESS; break;
271  case 'S': flags ^= RZ_AX_FLAG_RAW_TO_HEX; break;
272  case 'b': flags ^= RZ_AX_FLAG_BIN_TO_STR; break;
273  case 'B': flags ^= RZ_AX_FLAG_STR_TO_BIN; break;
274  case 'p': flags ^= RZ_AX_FLAG_SET_BITS; break;
275  case 'x': flags ^= RZ_AX_FLAG_STR_TO_DJB2; break;
276  case 'k': flags ^= RZ_AX_FLAG_KEEP_BASE; break;
277  case 'f': flags ^= RZ_AX_FLAG_FLOATING_POINT; break;
278  case 'd': flags ^= RZ_AX_FLAG_FORCE_INTEGER; break;
279  case 'n': flags ^= RZ_AX_FLAG_NUMBER_TO_HEX; break;
280  case 'u': flags ^= RZ_AX_FLAG_UNITS; break;
281  case 't': flags ^= RZ_AX_FLAG_TIMESTAMP_TO_STR; break;
282  case 'E': flags ^= RZ_AX_FLAG_BASE64_ENCODE; break;
283  case 'D': flags ^= RZ_AX_FLAG_BASE64_DECODE; break;
284  case 'F': flags ^= RZ_AX_FLAG_RAW_TO_LANGBYTES; break;
285  case 'N': flags ^= RZ_AX_FLAG_NUMBER_TO_HEXSTR; break;
286  case 'w': flags ^= RZ_AX_FLAG_SIGNED_WORD; break;
287  case 'r': flags ^= RZ_AX_FLAG_RIZIN_CMD; break;
288  case 'L': flags ^= RZ_AX_FLAG_BIN_TO_BIGNUM; break;
289  case 'i': flags ^= RZ_AX_FLAG_DUMP_C_BYTES; break;
290  case 'o': flags ^= RZ_AX_FLAG_OCTAL_TO_RAW; break;
291  case 'I': flags ^= RZ_AX_FLAG_IPADDR_TO_LONG; break;
292  case 'v': return rz_main_version_print("rz-ax");
293  case '\0':
294  *_flags = flags;
295  return !use_stdin(num, _flags, fm);
296  default:
297  /* not as complete as for positive numbers */
298  out_mode = (flags ^ RZ_AX_FLAG_KEEP_BASE) ? '0' : 'I';
299  if (str[1] >= '0' && str[1] <= '9') {
300  if (str[2] == 'x') {
301  out_mode = 'I';
302  } else if (rz_str_endswith(str, "f")) {
303  out_mode = 'l';
304  }
305  return format_output(num, out_mode, str, *fm, flags);
306  }
307  printf("Usage: rz-ax [options] [expr ...]\n");
308  return help();
309  }
310  str++;
311  }
312  *_flags = flags;
313  if (last) {
314  return !use_stdin(num, _flags, fm);
315  }
316  return true;
317  }
318  *_flags = flags;
319  if (!flags && rz_str_nlen(str, 2) == 1) {
320  if (*str == 'q') {
321  return false;
322  }
323  if (*str == 'h' || *str == '?') {
324  help();
325  return false;
326  }
327  }
328 dotherax:
329  if (has_flag(flags, RZ_AX_FLAG_HEX_TO_RAW)) { // -s
330  int n = ((strlen(str)) >> 1) + 1;
331  buf = malloc(n);
332  if (buf) {
333  memset(buf, '\0', n);
334  n = rz_hex_str2bin(str, (ut8 *)buf);
335  if (n > 0) {
336  fwrite(buf, n, 1, stdout);
337  }
338 #if __EMSCRIPTEN__
339  puts("");
340 #else
341  if (nl && *nl) {
342  puts("");
343  }
344 #endif
345  fflush(stdout);
346  free(buf);
347  }
348  return true;
349  }
350  if (has_flag(flags, RZ_AX_FLAG_RAW_TO_HEX)) { // -S
351  for (i = 0; i < len; i++) {
352  printf("%02x", (ut8)str[i]);
353  }
354  printf("\n");
355  return true;
356  } else if (has_flag(flags, RZ_AX_FLAG_BIN_TO_STR)) { // -b
357  int i;
358  ut8 buf[4096];
359  const int n = rz_str_binstr2bin(str, buf, sizeof(buf));
360  for (i = 0; i < n; i++) {
361  printf("%c", buf[i]);
362  }
363  return true;
364  } else if (has_flag(flags, RZ_AX_FLAG_STR_TO_DJB2)) { // -x
365  int h = rz_str_djb2_hash(str);
366  printf("0x%x\n", h);
367  return true;
368  } else if (has_flag(flags, RZ_AX_FLAG_KEEP_BASE)) { // -k
369  out_mode = 'I';
370  } else if (has_flag(flags, RZ_AX_FLAG_FLOATING_POINT)) { // -f
371  out_mode = 'f';
372  } else if (has_flag(flags, RZ_AX_FLAG_NUMBER_TO_HEX)) { // -n
373  ut64 n = rz_num_math(num, str);
374  if (n >> 32) {
375  /* is 64 bit value */
377  fwrite(&n, sizeof(n), 1, stdout);
378  } else {
379  int i;
380  for (i = 0; i < 8; i++) {
381  printf("%02x", (int)(n & 0xff));
382  n >>= 8;
383  }
384  printf("\n");
385  }
386  } else {
387  /* is 32 bit value */
388  ut32 n32 = (ut32)n;
390  fwrite(&n32, sizeof(n32), 1, stdout);
391  } else {
392  int i;
393  for (i = 0; i < 4; i++) {
394  printf("%02x", n32 & 0xff);
395  n32 >>= 8;
396  }
397  printf("\n");
398  }
399  }
400  return true;
401  } else if (has_flag(flags, RZ_AX_FLAG_STR_TO_BIN)) { // -B (bin -> str)
402  int i = 0;
403  for (i = 0; i < strlen(str); i++) {
404  ut8 ch = str[i];
405  printf("%d%d%d%d"
406  "%d%d%d%d",
407  ch & 128 ? 1 : 0,
408  ch & 64 ? 1 : 0,
409  ch & 32 ? 1 : 0,
410  ch & 16 ? 1 : 0,
411  ch & 8 ? 1 : 0,
412  ch & 4 ? 1 : 0,
413  ch & 2 ? 1 : 0,
414  ch & 1 ? 1 : 0);
415  }
416  return true;
417  } else if (has_flag(flags, RZ_AX_FLAG_SET_BITS)) { // -p (find position of set bits)
418  ut64 n = rz_num_math(num, str);
419  char strbits[65] = { 0 };
420  int i = 0, set_bits_ctr = 0;
421  rz_num_to_bits(strbits, n);
422  rz_str_reverse(strbits); // because we count Right to Left
423  char last_char = 0;
424  while (strbits[i] != '\0') {
425  if (strbits[i] == '1') {
426  ++set_bits_ctr;
427  if (i == 0) {
428  printf("[%d", i);
429  } else if (strbits[i] == '1' && last_char == '0') {
430  printf("[%d", i);
431  }
432  }
433  if (strbits[i] == '0' && last_char == '1') {
434  if (set_bits_ctr == 1) {
435  printf("]: 1\n");
436  } else if (strbits[i + 1] == '\0') {
437  printf("-%d]: 1\n", i);
438  } else
439  printf("-%d]: 1\n", i - 1);
440  set_bits_ctr = 0;
441  } else if (strbits[i] == '1' && strbits[i + 1] == '\0') {
442  if (set_bits_ctr == 1) {
443  printf("]: 1\n");
444  } else
445  printf("-%d]: 1\n", i);
446  set_bits_ctr = 0;
447  }
448  last_char = strbits[i];
449  ++i;
450  }
451  return true;
452  } else if (has_flag(flags, RZ_AX_FLAG_SIGNED_WORD)) { // -w
453  ut64 n = rz_num_math(num, str);
454  if (n >> 31) {
455  // is >32bit
456  n = (st64)(st32)n;
457  } else if (n >> 14) {
458  n = (st64)(st16)n;
459  } else if (n >> 7) {
460  n = (st64)(st8)n;
461  }
462  printf("%" PFMT64d "\n", n);
463  return true;
464  } else if (has_flag(flags, RZ_AX_FLAG_NUMBER_TO_HEXSTR)) { // -N
465  ut64 n = rz_num_math(num, str);
466  if (n >> 32) {
467  /* is 64 bit value */
469  fwrite(&n, sizeof(n), 1, stdout);
470  } else {
471  int i;
472  for (i = 0; i < 8; i++) {
473  printf("\\x%02x", (int)(n & 0xff));
474  n >>= 8;
475  }
476  printf("\n");
477  }
478  } else {
479  /* is 32 bit value */
480  ut32 n32 = (ut32)n;
482  fwrite(&n32, sizeof(n32), 1, stdout);
483  } else {
484  int i;
485  for (i = 0; i < 4; i++) {
486  printf("\\x%02x", n32 & 0xff);
487  n32 >>= 8;
488  }
489  printf("\n");
490  }
491  }
492  return true;
493  } else if (has_flag(flags, RZ_AX_FLAG_UNITS)) { // -u
494  char buf[8];
495  rz_num_units(buf, sizeof(buf), rz_num_math(NULL, str));
496  printf("%s\n", buf);
497  return true;
498  } else if (has_flag(flags, RZ_AX_FLAG_TIMESTAMP_TO_STR)) { // -t
499  RzList *split = rz_str_split_list(str, "GMT", 0);
500  char *ts = rz_list_head(split)->data;
501  const char *gmt = NULL;
502  if (rz_list_length(split) >= 2 && strlen(rz_list_head(split)->n->data) >= 2) {
503  gmt = (const char *)rz_list_head(split)->n->data;
504  }
505  ut32 n = rz_num_math(num, ts);
506  int timezone = (int)rz_num_math(num, gmt);
507  n += timezone * (60 * 60);
508  char *date = rz_time_date_unix_to_string(n);
509  printf("%s\n", date);
510  fflush(stdout);
511  free(date);
512  rz_list_free(split);
513  return true;
514  } else if (has_flag(flags, RZ_AX_FLAG_BASE64_ENCODE)) { // -E
515  const int n = strlen(str);
516  /* http://stackoverflow.com/questions/4715415/base64-what-is-the-worst-possible-increase-in-space-usage */
517  char *out = calloc(1, (n + 2) / 3 * 4 + 1); // ceil(n/3)*4 plus 1 for NUL
518  if (out) {
519  rz_base64_encode(out, (const ut8 *)str, n);
520  printf("%s%s", out, nl);
521  fflush(stdout);
522  free(out);
523  }
524  return true;
525  } else if (has_flag(flags, RZ_AX_FLAG_BASE64_DECODE)) { // -D
526  const int n = strlen(str);
527  ut8 *out = calloc(1, n / 4 * 3 + 1);
528  if (out) {
530  printf("%s%s", out, nl);
531  fflush(stdout);
532  free(out);
533  }
534  return true;
535  } else if (has_flag(flags, RZ_AX_FLAG_RAW_TO_LANGBYTES)) { // -F
536  char *s = rz_stdin_slurp(NULL);
537  if (s) {
538  char *res = rz_hex_from_code(s);
539  if (res) {
540  printf("%s\n", res);
541  fflush(stdout);
542  free(res);
543  } else {
544  eprintf("Invalid input.\n");
545  }
546  free(s);
547  }
548  return false;
549  } else if (has_flag(flags, RZ_AX_FLAG_RIZIN_CMD)) { // -r
550  char *asnum, unit[8];
551  char out[128];
552  ut32 n32, s, a;
553  double d;
554  float f;
555  ut64 n = rz_num_math(num, str);
556 
557  if (num->dbz) {
558  eprintf("RzNum ERROR: Division by Zero\n");
559  return false;
560  }
561  n32 = (ut32)(n & UT32_MAX);
562  asnum = rz_num_as_string(NULL, n, false);
563  memcpy(&f, &n32, sizeof(f));
564  memcpy(&d, &n, sizeof(d));
565 
566  /* decimal, hexa, octal */
567  s = n >> 16 << 12;
568  a = n & 0x0fff;
569  rz_num_units(unit, sizeof(unit), n);
570 #if 0
571  eprintf ("%" PFMT64d " 0x%" PFMT64x " 0%" PFMT64o
572  " %s %04x:%04x ",
573  n, n, n, unit, s, a);
574 
575  if (n >> 32) {
576  eprintf ("%" PFMT64d " ", (st64) n);
577  } else {
578  eprintf ("%d ", (st32) n);
579  }
580  if (asnum) {
581  eprintf ("\"%s\" ", asnum);
582  free (asnum);
583  }
584  /* binary and floating point */
585  rz_str_bits (out, (const ut8 *) &n, sizeof (n), NULL);
586  eprintf ("%s %.01lf %ff %lf\n",
587  out, num->fvalue, f, d);
588 #endif
589  printf("hex 0x%" PFMT64x "\n", n);
590  printf("octal 0%" PFMT64o "\n", n);
591  printf("unit %s\n", unit);
592  printf("segment %04x:%04x\n", s, a);
593  if (n >> 32) {
594  printf("int64 %" PFMT64d "\n", (st64)n);
595  } else {
596  printf("int32 %d\n", (st32)n);
597  }
598  if (asnum) {
599  printf("string \"%s\"\n", asnum);
600  free(asnum);
601  }
602  /* binary and floating point */
603  rz_str_bits64(out, n);
604  memcpy(&f, &n, sizeof(f));
605  memcpy(&d, &n, sizeof(d));
606  printf("binary 0b%s\n", out);
607  printf("float: %ff\n", f);
608  printf("double: %lf\n", d);
609 
610  /* ternary */
612  printf("trits 0t%s\n", out);
613 
614  return true;
615  } else if (has_flag(flags, RZ_AX_FLAG_BIN_TO_BIGNUM)) { // -L
617  return true;
618  } else if (has_flag(flags, RZ_AX_FLAG_DUMP_C_BYTES)) { // -i
619  static const char start[] = "unsigned char buf[] = {";
620  printf(start);
621  /* reasonable amount of bytes per line */
622  const int byte_per_col = 12;
623  for (i = 0; i < len - 1; i++) {
624  /* wrapping every N bytes */
625  if (i % byte_per_col == 0) {
626  printf("\n ");
627  }
628  printf("0x%02x, ", (ut8)str[i]);
629  }
630  /* some care for the last element */
631  if (i % byte_per_col == 0) {
632  printf("\n ");
633  }
634  printf("0x%02x\n", (ut8)str[len - 1]);
635  printf("};\n");
636  printf("unsigned int buf_len = %d;\n", len);
637  return true;
638  } else if (has_flag(flags, RZ_AX_FLAG_OCTAL_TO_RAW)) { // -o
639  // check -r
640  char *modified_str;
641 
642  // To distinguish octal values.
643  if (*str != '0') {
644  modified_str = rz_str_newf("0%s", str);
645  } else {
646  modified_str = rz_str_new(str);
647  }
648 
649  ut64 n = rz_num_math(num, modified_str);
650  free(modified_str);
651  if (num->dbz) {
652  eprintf("RzNum ERROR: Division by Zero\n");
653  return false;
654  }
655 
656  char *asnum = rz_num_as_string(NULL, n, false);
657  if (asnum) {
658  printf("%s", asnum);
659  free(asnum);
660  } else {
661  eprintf("No String Possible\n");
662  return false;
663  }
664  return true;
665  } else if (has_flag(flags, RZ_AX_FLAG_IPADDR_TO_LONG)) { // -I
666  if (strchr(str, '.')) {
667  ut8 ip[4];
668  sscanf(str, "%hhd.%hhd.%hhd.%hhd", ip, ip + 1, ip + 2, ip + 3);
669  ut32 ip32 = ip[0] | (ip[1] << 8) | (ip[2] << 16) | (ip[3] << 24);
670  printf("0x%08x\n", ip32);
671  } else {
672  ut32 ip32 = (ut32)rz_num_math(NULL, str);
673  ut8 ip[4] = { ip32 & 0xff, (ip32 >> 8) & 0xff, (ip32 >> 16) & 0xff, ip32 >> 24 };
674  printf("%d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
675  }
676  return true;
677  }
678 
679  if (str[0] == '0' && (tolower(str[1]) == 'x')) {
680  out_mode = (has_flag(flags, RZ_AX_FLAG_KEEP_BASE)) ? '0' : 'I';
681  } else if (rz_str_startswith(str, "b")) {
682  out_mode = 'B';
683  str++;
684  } else if (rz_str_startswith(str, "t")) {
685  out_mode = 'T';
686  str++;
687  } else if (rz_str_startswith(str, "Fx")) {
688  out_mode = 'F';
689  *str = '0';
690  } else if (rz_str_startswith(str, "Bx")) {
691  out_mode = 'B';
692  *str = '0';
693  } else if (rz_str_startswith(str, "Tx")) {
694  out_mode = 'T';
695  *str = '0';
696  } else if (rz_str_startswith(str, "Ox")) {
697  out_mode = 'O';
698  *str = '0';
699  } else if (rz_str_endswith(str, "d")) {
700  out_mode = 'I';
701  str[strlen(str) - 1] = 'b';
702  // TODO: Move print into format_output
703  } else if (rz_str_endswith(str, "f")) {
704  out_mode = 'l';
705  } else if (rz_str_endswith(str, "dt")) {
706  out_mode = 'I';
707  str[strlen(str) - 2] = 't';
708  str[strlen(str) - 1] = '\0';
709  }
710  while ((p = strchr(str, ' '))) {
711  *p = 0;
712  format_output(num, out_mode, str, *fm, flags);
713  str = p + 1;
714  }
715  if (*str) {
716  format_output(num, out_mode, str, *fm, flags);
717  }
718  return true;
719 }
720 
721 RZ_API int rz_main_rz_ax(int argc, const char **argv) {
722  int i, fm = 0;
724  if (argc == 1) {
725  use_stdin(num, 0, &fm);
726  } else {
727  ut64 flags = 0;
728  for (i = 1; i < argc; i++) {
729  char *argv_i = strdup(argv[i]);
730  rz_str_unescape(argv_i);
731  rax(num, argv_i, 0, i == argc - 1, &flags, &fm);
732  free(argv_i);
733  }
734  }
735  rz_num_free(num);
736  num = NULL;
737  return 0;
738 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static ut8 bytes[32]
Definition: asm_arc.c:23
static int bytes_size
Definition: asm_vax.c:21
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
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
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 static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void start
Definition: sflib.h:133
uint32_t ut32
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char int mode
Definition: ioapi.h:137
voidpf void * buf
Definition: ioapi.h:138
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 print_ascii_table(void)
Definition: rz-ax.c:183
#define RZ_AX_FLAG_BASE64_ENCODE
Definition: rz-ax.c:19
static int rax(RzNum *num, char *str, int len, int last, ut64 *flags, int *fm)
Definition: rz-ax.c:238
#define RZ_AX_FLAG_TIMESTAMP_TO_STR
Definition: rz-ax.c:18
#define RZ_AX_FLAG_FORCE_INTEGER
Definition: rz-ax.c:15
static void print_hex_from_base2(char *base2)
Definition: rz-ax.c:133
static int help(void)
Definition: rz-ax.c:187
#define RZ_AX_FLAG_STR_TO_DJB2
Definition: rz-ax.c:12
#define RZ_AX_FLAG_UNITS
Definition: rz-ax.c:17
#define RZ_AX_FLAG_BIN_TO_BIGNUM
Definition: rz-ax.c:26
#define RZ_AX_FLAG_IPADDR_TO_LONG
Definition: rz-ax.c:29
RZ_API int rz_main_rz_ax(int argc, const char **argv)
Definition: rz-ax.c:721
#define RZ_AX_FLAG_HEX_TO_RAW
Definition: rz-ax.c:8
static int use_stdin(RzNum *num, ut64 *flags, int *fm)
Definition: rz-ax.c:38
#define has_flag(f, x)
Definition: rz-ax.c:32
#define RZ_AX_FLAG_STR_TO_BIN
Definition: rz-ax.c:24
#define RZ_AX_FLAG_RIZIN_CMD
Definition: rz-ax.c:25
#define RZ_AX_FLAG_OCTAL_TO_RAW
Definition: rz-ax.c:28
#define RZ_AX_FLAG_DUMP_C_BYTES
Definition: rz-ax.c:27
#define RZ_AX_FLAG_NUMBER_TO_HEX
Definition: rz-ax.c:16
#define RZ_AX_FLAG_NUMBER_TO_HEXSTR
Definition: rz-ax.c:22
#define RZ_AX_FLAG_SET_BITS
Definition: rz-ax.c:30
#define RZ_AX_FLAG_SIGNED_WORD
Definition: rz-ax.c:23
#define RZ_AX_FLAG_RAW_TO_HEX
Definition: rz-ax.c:10
#define RZ_AX_FLAG_BIN_TO_STR
Definition: rz-ax.c:11
#define RZ_AX_FLAG_RAW_TO_LANGBYTES
Definition: rz-ax.c:21
#define RZ_AX_FLAG_SWAP_ENDIANNESS
Definition: rz-ax.c:9
#define RZ_AX_FLAG_FLOATING_POINT
Definition: rz-ax.c:14
static int format_output(RzNum *num, char mode, const char *s, int force_mode, ut64 flags)
Definition: rz-ax.c:77
#define RZ_AX_FLAG_BASE64_DECODE
Definition: rz-ax.c:20
#define RZ_AX_FLAG_KEEP_BASE
Definition: rz-ax.c:13
#define STDIN_BUFFER_SIZE
Definition: rz-ax.c:35
RZ_API ut32 rz_list_length(RZ_NONNULL const RzList *list)
Returns the length of the list.
Definition: list.c:109
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
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")
#define ip
int n
Definition: mipsasm.c:19
#define eprintf(x, y...)
Definition: rlcc.c:7
static RzSocket * s
Definition: rtr.c:28
RZ_API const char * ret_ascii_table(void)
Definition: ascii_table.c:78
#define RZ_STATIC_ASSERT(x)
Definition: rz_assert.h:10
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz)
Definition: ubase64.c:81
RZ_API int rz_base64_decode(ut8 *bout, const char *bin, int len)
Definition: ubase64.c:48
RZ_API char * rz_stdin_slurp(int *sz)
Definition: file.c:408
RZ_API int rz_hex_str2bin(const char *in, ut8 *out)
Convert an input string in into the binary form in out.
Definition: hex.c:444
RZ_API char * rz_hex_from_code(const char *code)
Definition: hex.c:342
RZ_API int rz_main_version_print(const char *program)
Definition: main.c:49
RZ_API void rz_mem_swapendian(ut8 *dest, const ut8 *orig, int size)
Definition: mem.c:202
RZ_API RzNum * rz_num_new(RzNumCallback cb, RzNumCallback2 cb2, void *ptr)
Definition: unum.c:75
RZ_API int rz_num_to_bits(char *out, ut64 num)
Definition: unum.c:542
RZ_API ut64 rz_num_math(RzNum *num, const char *str)
Definition: unum.c:456
RZ_API int rz_num_to_trits(char *out, ut64 num)
Definition: unum.c:578
RZ_API char * rz_num_units(char *buf, size_t len, ut64 number)
Definition: unum.c:108
RZ_API char * rz_num_as_string(RzNum *___, ut64 n, bool printable_only)
Definition: unum.c:705
RZ_API void rz_num_free(RzNum *num)
Definition: unum.c:87
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
RZ_API ut64 rz_str_djb2_hash(const char *str)
Definition: str.c:383
RZ_API char * rz_str_new(const char *str)
Definition: str.c:865
RZ_API int rz_str_binstr2bin(const char *str, ut8 *out, int outlen)
Definition: str.c:284
RZ_API int rz_str_bits64(char *strout, ut64 in)
Definition: str.c:244
RZ_API int rz_str_bits(char *strout, const ut8 *buf, int len, const char *bitz)
Definition: str.c:195
RZ_API size_t rz_str_nlen(const char *s, size_t n)
Definition: str.c:1949
RZ_API bool rz_str_startswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string starts with a specifc sequence of characters (case sensitive)
Definition: str.c:3286
RZ_API RzList * rz_str_split_list(char *str, const char *c, int n)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3429
RZ_API int rz_str_unescape(char *buf)
Definition: str.c:1300
RZ_API bool rz_str_endswith(RZ_NONNULL const char *str, RZ_NONNULL const char *needle)
Checks if a string ends with a specifc sequence of characters (case sensitive)
Definition: str.c:3329
RZ_API void rz_str_reverse(char *str)
Definition: str.c:183
#define rz_time_date_unix_to_string
Definition: rz_time.h:38
#define PFMT64d
Definition: rz_types.h:394
#define PFMT64o
Definition: rz_types.h:396
#define PFMT64x
Definition: rz_types.h:393
#define st8
Definition: rz_types_base.h:16
#define st64
Definition: rz_types_base.h:10
#define UT32_MAX
Definition: rz_types_base.h:99
#define st16
Definition: rz_types_base.h:14
#define st32
Definition: rz_types_base.h:12
#define tolower(c)
Definition: safe-ctype.h:149
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
static int
Definition: sfsocketcall.h:114
#define d(i)
Definition: sha256.c:44
#define f(i)
Definition: sha256.c:46
#define c(i)
Definition: sha256.c:43
#define a(i)
Definition: sha256.c:41
#define h(i)
Definition: sha256.c:48
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115