Rizin
unix-like reverse engineering framework and cli tools
lang_byte_array.c File Reference
#include <rz_util.h>

Go to the source code of this file.

Macros

#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE   16
 
#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR   "16"
 

Functions

static void lang_byte_array_rizin (RzStrBuf *sb, const ut8 *buffer, size_t size)
 
static void lang_byte_array_bash (RzStrBuf *sb, const ut8 *buffer, size_t size)
 
static void lang_byte_array_c_cpp (RzStrBuf *sb, const ut8 *buffer, size_t size, size_t n_bytes, bool big_endian)
 
static void lang_byte_array_asm (RzStrBuf *sb, const ut8 *buffer, size_t size)
 
static void lang_byte_array_golang (RzStrBuf *sb, const ut8 *buffer, size_t size)
 
static void lang_byte_array_java (RzStrBuf *sb, const ut8 *buffer, size_t size)
 
static char * lang_byte_array_json (const ut8 *buffer, size_t size)
 
static void lang_byte_array_kotlin (RzStrBuf *sb, const ut8 *buffer, size_t size)
 
static void lang_byte_array_nodejs (RzStrBuf *sb, const ut8 *buffer, size_t size)
 
static void lang_byte_array_objective_c_cpp (RzStrBuf *sb, const ut8 *buffer, size_t size)
 

Macro Definition Documentation

◆ RZ_LANG_BYTE_ARRAY_TRUNK_SIZE

#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE   16

Definition at line 6 of file lang_byte_array.c.

◆ RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR

#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR   "16"

Definition at line 7 of file lang_byte_array.c.

Function Documentation

◆ lang_byte_array_asm()

static void lang_byte_array_asm ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

Definition at line 82 of file lang_byte_array.c.

82  {
83  rz_strbuf_append(sb, "byte_array:");
84  for (size_t pos = 0; pos < size; pos++) {
86  rz_strbuf_appendf(sb, "\n.byte 0x%02x", buffer[pos]);
87  } else {
88  rz_strbuf_appendf(sb, ", 0x%02x", buffer[pos]);
89  }
90  }
91  rz_strbuf_appendf(sb, "\n.equ byte_array_len, %" PFMTSZd, size);
92 }
static SblHeader sb
Definition: bin_mbn.c:26
voidpf void uLong size
Definition: ioapi.h:138
#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
#define PFMTSZd
Definition: rz_types.h:398
Definition: buffer.h:15
int pos
Definition: main.c:11

References PFMTSZd, pos, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE, rz_strbuf_append(), rz_strbuf_appendf(), and sb.

◆ lang_byte_array_bash()

static void lang_byte_array_bash ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

Definition at line 23 of file lang_byte_array.c.

23  {
24  bool append = false;
25  rz_strbuf_append(sb, "printf \"");
26  for (size_t pos = 0; pos < size; pos++) {
27  if (pos > 0 && !(pos % RZ_LANG_BYTE_ARRAY_TRUNK_SIZE)) {
28  rz_strbuf_appendf(sb, "\" %s data.bin\nprintf \"", append ? ">>" : ">");
29  append = true;
30  }
31  rz_strbuf_appendf(sb, "\\%03o", buffer[pos]);
32  }
33  rz_strbuf_appendf(sb, "\" %s data.bin", append ? ">>" : ">");
34 }
#define append(x, y)
Definition: cmd_print.c:1740

References append, pos, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE, rz_strbuf_append(), rz_strbuf_appendf(), and sb.

◆ lang_byte_array_c_cpp()

static void lang_byte_array_c_cpp ( RzStrBuf sb,
const ut8 buffer,
size_t  size,
size_t  n_bytes,
bool  big_endian 
)
static

Definition at line 36 of file lang_byte_array.c.

36  {
37  const char *hex_c, *hex_e;
38  size_t n_bits = n_bytes * 8;
39  size_t max_print;
40  ut64 value;
41 
42  switch (n_bytes) {
43  case 2:
44  hex_c = " 0x%04" PFMT64x ",";
45  hex_e = " 0x%04" PFMT64x "\n};";
46  // ensure that is always aligned
47  size -= (size % n_bytes);
48  break;
49  case 4:
50  hex_c = " 0x%08" PFMT64x "u,";
51  hex_e = " 0x%08" PFMT64x "u\n};";
52  // ensure that is always aligned
53  size -= (size % n_bytes);
54  break;
55  case 8:
56  hex_c = " 0x%016" PFMT64x "ull,";
57  hex_e = " 0x%016" PFMT64x "ull\n};";
58  // ensure that is always aligned
59  size -= (size % n_bytes);
60  break;
61  default:
62  hex_c = " 0x%02" PFMT64x ",";
63  hex_e = " 0x%02" PFMT64x "\n};";
64  break;
65  }
66  max_print = RZ_LANG_BYTE_ARRAY_TRUNK_SIZE / n_bytes;
67 
68  if (size < 1 && n_bytes != 1) {
69  rz_strbuf_appendf(sb, "// Warning: the number of available bytes is less than %" PFMTSZd, n_bytes);
70  return;
71  }
72  rz_strbuf_appendf(sb, "#define ARRAY_SIZE %" PFMTSZd "\nconst uint%" PFMTSZd "_t array[ARRAY_SIZE] = {\n ", size / n_bytes, n_bits);
73  for (size_t pos = 0, n_print = 0; pos < size; pos += n_bytes, n_print++) {
74  if (n_print > 0 && !(n_print % max_print)) {
75  rz_strbuf_append(sb, "\n ");
76  }
77  value = rz_read_ble(buffer + pos, big_endian, n_bits);
78  rz_strbuf_appendf(sb, (pos + n_bytes) < size ? hex_c : hex_e, value);
79  }
80 }
static int value
Definition: cmd_api.c:93
static ut64 rz_read_ble(const void *src, bool big_endian, int size)
Definition: rz_endian.h:517
#define PFMT64x
Definition: rz_types.h:393
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References PFMT64x, PFMTSZd, pos, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE, rz_read_ble(), rz_strbuf_append(), rz_strbuf_appendf(), sb, ut64(), and value.

◆ lang_byte_array_golang()

static void lang_byte_array_golang ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

Definition at line 94 of file lang_byte_array.c.

94  {
95  rz_strbuf_append(sb, "byteArray := []byte{\n ");
96  for (size_t pos = 0; pos < size; pos++) {
97  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
98  rz_strbuf_append(sb, "\n ");
99  }
100  rz_strbuf_appendf(sb, " 0x%02x,", buffer[pos]);
101  }
102  rz_strbuf_append(sb, "\n}");
103 }

References pos, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE, rz_strbuf_append(), rz_strbuf_appendf(), and sb.

◆ lang_byte_array_java()

static void lang_byte_array_java ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

Definition at line 105 of file lang_byte_array.c.

105  {
106  int value = 0;
107  rz_strbuf_append(sb, "byte[] byteArray = new byte[] {\n ");
108  for (size_t pos = 0; pos < size; pos++) {
109  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
110  rz_strbuf_append(sb, "\n ");
111  }
112  value = buffer[pos];
113  if (value > 127) {
114  value -= 256;
115  }
116  rz_strbuf_appendf(sb, " %4d,", value);
117  }
118  rz_strbuf_append(sb, "\n};");
119 }

References pos, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE, rz_strbuf_append(), rz_strbuf_appendf(), sb, and value.

◆ lang_byte_array_json()

static char* lang_byte_array_json ( const ut8 buffer,
size_t  size 
)
static

Definition at line 121 of file lang_byte_array.c.

121  {
122  PJ *pj = pj_new();
123  if (!pj) {
124  return NULL;
125  }
126  pj_a(pj);
127  for (size_t pos = 0; pos < size; pos++) {
128  pj_i(pj, buffer[pos]);
129  }
130  pj_end(pj);
131  return pj_drain(pj);
132 }
#define NULL
Definition: cris-opc.c:27
RZ_API PJ * pj_new(void)
Definition: pj.c:25
RZ_API char * pj_drain(PJ *j)
Definition: pj.c:50
RZ_API PJ * pj_end(PJ *j)
Definition: pj.c:87
RZ_API PJ * pj_i(PJ *j, int d)
Definition: pj.c:284
RZ_API PJ * pj_a(PJ *j)
Definition: pj.c:81
Definition: rz_pj.h:12

References NULL, pj_a(), pj_drain(), pj_end(), pj_i(), pj_new(), and pos.

◆ lang_byte_array_kotlin()

static void lang_byte_array_kotlin ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

Definition at line 134 of file lang_byte_array.c.

134  {
135  int value = 0;
136  rz_strbuf_append(sb, "val byteArray = byteArrayOf(\n ");
137  for (size_t pos = 0; pos < size; pos++) {
138  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
139  rz_strbuf_append(sb, "\n ");
140  }
141  value = buffer[pos];
142  if (value > 127) {
143  value -= 256;
144  }
145  rz_strbuf_appendf(sb, " %4d,", value);
146  }
147  rz_strbuf_append(sb, "\n);");
148 }

References pos, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE, rz_strbuf_append(), rz_strbuf_appendf(), sb, and value.

◆ lang_byte_array_nodejs()

static void lang_byte_array_nodejs ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

Definition at line 150 of file lang_byte_array.c.

150  {
151  if (size == 0) {
152  rz_strbuf_append(sb, "var byteArray = new Buffer('', 'base64');");
153  return;
154  }
155 
156  char *base64 = calloc(size, 3);
157  if (!base64) {
158  return;
159  }
160  rz_base64_encode(base64, buffer, size);
161  rz_strbuf_appendf(sb, "var byteArray = new Buffer('%s', 'base64');", base64);
162  free(base64);
163 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz)
Definition: ubase64.c:81

References calloc(), free(), rz_base64_encode(), rz_strbuf_append(), rz_strbuf_appendf(), and sb.

◆ lang_byte_array_objective_c_cpp()

static void lang_byte_array_objective_c_cpp ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

◆ lang_byte_array_rizin()

static void lang_byte_array_rizin ( RzStrBuf sb,
const ut8 buffer,
size_t  size 
)
static

Definition at line 9 of file lang_byte_array.c.

9  {
10  size_t pos = 0;
11  rz_strbuf_append(sb, "wx ");
12  for (pos = 0; pos < size; pos++) {
13  if (pos > 0 && !(pos % RZ_LANG_BYTE_ARRAY_TRUNK_SIZE)) {
15  }
16  rz_strbuf_appendf(sb, "%02x", buffer[pos]);
17  }
19  rz_strbuf_appendf(sb, " ; sd -%" PFMTSZd, pos);
20  }
21 }
#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR

References PFMTSZd, pos, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE, RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR, rz_strbuf_append(), rz_strbuf_appendf(), and sb.