Rizin
unix-like reverse engineering framework and cli tools
lang_byte_array.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2022 RizinOrg <info@rizin.re>
2 // SPDX-FileCopyrightText: 2022 deroad <wargio@libero.it>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_util.h>
6 #define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE 16
7 #define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR "16"
8 
9 static void lang_byte_array_rizin(RzStrBuf *sb, const ut8 *buffer, size_t size) {
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 }
22 
23 static void lang_byte_array_bash(RzStrBuf *sb, const ut8 *buffer, size_t size) {
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 }
35 
36 static void lang_byte_array_c_cpp(RzStrBuf *sb, const ut8 *buffer, size_t size, size_t n_bytes, bool big_endian) {
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 }
81 
82 static void lang_byte_array_asm(RzStrBuf *sb, const ut8 *buffer, size_t size) {
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 }
93 
94 static void lang_byte_array_golang(RzStrBuf *sb, const ut8 *buffer, size_t size) {
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 }
104 
105 static void lang_byte_array_java(RzStrBuf *sb, const ut8 *buffer, size_t size) {
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 }
120 
121 static char *lang_byte_array_json(const ut8 *buffer, size_t size) {
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 }
133 
134 static void lang_byte_array_kotlin(RzStrBuf *sb, const ut8 *buffer, size_t size) {
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 }
149 
150 static void lang_byte_array_nodejs(RzStrBuf *sb, const ut8 *buffer, size_t size) {
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 }
164 
165 static void lang_byte_array_objective_c_cpp(RzStrBuf *sb, const ut8 *buffer, size_t size) {
166  rz_strbuf_append(sb, "NSData *byteArray = [[NSData alloc] initWithBytes:{\n ");
167  for (size_t pos = 0; pos < size; pos++) {
168  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
169  rz_strbuf_append(sb, "\n ");
170  }
171  rz_strbuf_appendf(sb, (pos + 1) < size ? " 0x%02x," : " 0x%02x\n}];", buffer[pos]);
172  }
173 }
174 
175 static void lang_byte_array_python(RzStrBuf *sb, const ut8 *buffer, size_t size) {
176  rz_strbuf_append(sb, "byteArray = b'");
177  for (size_t pos = 0; pos < size; pos++) {
178  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
179  rz_strbuf_append(sb, "'\nbyteArray += b'");
180  }
181  rz_strbuf_appendf(sb, "\\x%02x", buffer[pos]);
182  }
183  rz_strbuf_append(sb, "'");
184 }
185 
186 static void lang_byte_array_rust(RzStrBuf *sb, const ut8 *buffer, size_t size) {
187  rz_strbuf_appendf(sb, "let _: [u8; %" PFMTSZd "] = [\n ", size);
188  for (size_t pos = 0; pos < size; pos++) {
189  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
190  rz_strbuf_append(sb, "\n ");
191  }
192  rz_strbuf_appendf(sb, (pos + 1) < size ? " 0x%02x," : " 0x%02x\n];", buffer[pos]);
193  }
194 }
195 
196 static void lang_byte_array_swift(RzStrBuf *sb, const ut8 *buffer, size_t size) {
197  rz_strbuf_append(sb, "let byteArray : [UInt8] = [\n ");
198  for (size_t pos = 0; pos < size; pos++) {
199  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
200  rz_strbuf_append(sb, "\n ");
201  }
202  rz_strbuf_appendf(sb, (pos + 1) < size ? " 0x%02x," : " 0x%02x\n];", buffer[pos]);
203  }
204 }
205 
206 static void lang_byte_array_yara(RzStrBuf *sb, const ut8 *buffer, size_t size) {
207  rz_strbuf_append(sb, "$byteArray = {\n ");
208  for (size_t pos = 0; pos < size; pos++) {
209  if (pos > 0 && !(pos % (RZ_LANG_BYTE_ARRAY_TRUNK_SIZE))) {
210  rz_strbuf_append(sb, "\n ");
211  }
212  rz_strbuf_appendf(sb, " %02x", buffer[pos]);
213  }
214  rz_strbuf_appendf(sb, "\n};");
215 }
216 
228  RzStrBuf sb;
229  rz_strbuf_init(&sb);
230 
231  if (size == 0) {
232  return rz_strbuf_drain_nofree(&sb);
233  }
234 
235  switch (type) {
238  break;
241  break;
244  break;
246  lang_byte_array_c_cpp(&sb, buffer, size, 1, false);
247  break;
249  lang_byte_array_c_cpp(&sb, buffer, size, 2, true);
250  break;
252  lang_byte_array_c_cpp(&sb, buffer, size, 2, false);
253  break;
255  lang_byte_array_c_cpp(&sb, buffer, size, 4, true);
256  break;
258  lang_byte_array_c_cpp(&sb, buffer, size, 4, false);
259  break;
261  lang_byte_array_c_cpp(&sb, buffer, size, 8, true);
262  break;
264  lang_byte_array_c_cpp(&sb, buffer, size, 8, false);
265  break;
268  break;
271  break;
273  rz_strbuf_fini(&sb);
277  break;
280  break;
283  break;
285  lang_byte_array_python(&sb, buffer, size);
286  break;
288  lang_byte_array_rust(&sb, buffer, size);
289  break;
291  lang_byte_array_swift(&sb, buffer, size);
292  break;
294  lang_byte_array_yara(&sb, buffer, size);
295  break;
296  default:
297  rz_strbuf_fini(&sb);
299  return NULL;
300  }
301  return rz_strbuf_drain_nofree(&sb);
302 }
static SblHeader sb
Definition: bin_mbn.c:26
static int value
Definition: cmd_api.c:93
#define append(x, y)
Definition: cmd_print.c:1740
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void uLong size
Definition: ioapi.h:138
static char * lang_byte_array_json(const ut8 *buffer, size_t size)
static void lang_byte_array_asm(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_c_cpp(RzStrBuf *sb, const ut8 *buffer, size_t size, size_t n_bytes, bool big_endian)
#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE_STR
#define RZ_LANG_BYTE_ARRAY_TRUNK_SIZE
static void lang_byte_array_objective_c_cpp(RzStrBuf *sb, const ut8 *buffer, size_t size)
static void lang_byte_array_rizin(RzStrBuf *sb, 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_bash(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)
uint8_t ut8
Definition: lh5801.h:11
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
int type
Definition: mipsasm.c:17
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz)
Definition: ubase64.c:81
static ut64 rz_read_ble(const void *src, bool big_endian, int size)
Definition: rz_endian.h:517
RZ_API RZ_OWN char * rz_lang_byte_array(RZ_NONNULL const ut8 *buffer, size_t size, RzLangByteArrayType type)
@ RZ_LANG_BYTE_ARRAY_C_CPP_BYTES
@ RZ_LANG_BYTE_ARRAY_JAVA
@ RZ_LANG_BYTE_ARRAY_BASH
@ RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_BE
@ RZ_LANG_BYTE_ARRAY_KOTLIN
@ RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_BE
@ RZ_LANG_BYTE_ARRAY_RIZIN
@ RZ_LANG_BYTE_ARRAY_GOLANG
@ RZ_LANG_BYTE_ARRAY_PYTHON
@ RZ_LANG_BYTE_ARRAY_ASM
@ RZ_LANG_BYTE_ARRAY_C_CPP_WORDS_LE
@ RZ_LANG_BYTE_ARRAY_C_CPP_DOUBLEWORDS_LE
@ RZ_LANG_BYTE_ARRAY_OBJECTIVE_C
@ RZ_LANG_BYTE_ARRAY_YARA
@ RZ_LANG_BYTE_ARRAY_SWIFT
@ RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_BE
@ RZ_LANG_BYTE_ARRAY_RUST
@ RZ_LANG_BYTE_ARRAY_JSON
@ RZ_LANG_BYTE_ARRAY_NODEJS
@ RZ_LANG_BYTE_ARRAY_C_CPP_HALFWORDS_LE
enum rz_lang_byte_array_type_t RzLangByteArrayType
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
RZ_API RZ_OWN char * rz_strbuf_drain_nofree(RzStrBuf *sb)
Definition: strbuf.c:349
RZ_API bool rz_strbuf_append(RzStrBuf *sb, const char *s)
Definition: strbuf.c:222
RZ_API void rz_strbuf_fini(RzStrBuf *sb)
Definition: strbuf.c:365
RZ_API bool rz_strbuf_appendf(RzStrBuf *sb, const char *fmt,...) RZ_PRINTF_CHECK(2
RZ_API void rz_strbuf_init(RzStrBuf *sb)
Definition: strbuf.c:33
#define PFMTSZd
Definition: rz_types.h:398
#define RZ_OWN
Definition: rz_types.h:62
#define RZ_NONNULL
Definition: rz_types.h:64
#define PFMT64x
Definition: rz_types.h:393
Definition: buffer.h:15
Definition: rz_pj.h:12
int pos
Definition: main.c:11
ut64(WINAPI *w32_GetEnabledXStateFeatures)()