Rizin
unix-like reverse engineering framework and cli tools
csign.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 RizinOrg <info@rizin.re>
2 // SPDX-FileCopyrightText: 2021 deroad <wargio@libero.it>
3 // SPDX-FileCopyrightText: 2014-2016 jfrankowski <jody.frankowski@gmail.com>
4 // SPDX-License-Identifier: LGPL-3.0-only
5 
6 #include <rz_core.h>
7 #include <rz_flirt.h>
8 
9 typedef struct map_string_value_t {
10  const char *name;
13 
14 const MapStringValue arch_map[29] = {
15  { "x86", RZ_FLIRT_SIG_ARCH_386 },
16  { "z80", RZ_FLIRT_SIG_ARCH_Z80 },
17  // { "i860", RZ_FLIRT_SIG_ARCH_I860 },
18  { "8051", RZ_FLIRT_SIG_ARCH_8051 },
19  // { "tms", RZ_FLIRT_SIG_ARCH_TMS },
20  { "6502", RZ_FLIRT_SIG_ARCH_6502 },
21  // { "pdp", RZ_FLIRT_SIG_ARCH_PDP },
22  { "m68k", RZ_FLIRT_SIG_ARCH_68K },
23  { "java", RZ_FLIRT_SIG_ARCH_JAVA },
24  { "m680x", RZ_FLIRT_SIG_ARCH_6800 },
25  // { "st7", RZ_FLIRT_SIG_ARCH_ST7 },
26  // { "mc6812", RZ_FLIRT_SIG_ARCH_MC6812 },
27  { "mips", RZ_FLIRT_SIG_ARCH_MIPS },
28  { "arm", RZ_FLIRT_SIG_ARCH_ARM },
29  // { "tmsc6", RZ_FLIRT_SIG_ARCH_TMSC6 },
30  { "ppc", RZ_FLIRT_SIG_ARCH_PPC },
31  // { "80196", RZ_FLIRT_SIG_ARCH_80196 },
32  // { "z8", RZ_FLIRT_SIG_ARCH_Z8 },
33  { "sh", RZ_FLIRT_SIG_ARCH_SH },
34  // { "net", RZ_FLIRT_SIG_ARCH_NET },
35  { "avr", RZ_FLIRT_SIG_ARCH_AVR },
36  { "h8300", RZ_FLIRT_SIG_ARCH_H8 },
37  { "pic", RZ_FLIRT_SIG_ARCH_PIC },
38  { "sparc", RZ_FLIRT_SIG_ARCH_SPARC },
39  // { "alpha", RZ_FLIRT_SIG_ARCH_ALPHA },
40  { "hppa", RZ_FLIRT_SIG_ARCH_HPPA },
41  // { "h8500", RZ_FLIRT_SIG_ARCH_H8500 },
42  { "tricore", RZ_FLIRT_SIG_ARCH_TRICORE },
43  // { "dsp56k", RZ_FLIRT_SIG_ARCH_DSP56K },
44  // { "c166", RZ_FLIRT_SIG_ARCH_C166 },
45  // { "st20", RZ_FLIRT_SIG_ARCH_ST20 },
46  // { "ia64", RZ_FLIRT_SIG_ARCH_IA64 },
47  // { "i960", RZ_FLIRT_SIG_ARCH_I960 },
48  // { "f2mc", RZ_FLIRT_SIG_ARCH_F2MC },
49  // { "tms320c54", RZ_FLIRT_SIG_ARCH_TMS320C54 },
50  { "tms320", RZ_FLIRT_SIG_ARCH_TMS320C55 },
51  // { "trimedia", RZ_FLIRT_SIG_ARCH_TRIMEDIA },
52  // { "m32r", RZ_FLIRT_SIG_ARCH_M32R },
53  // { "nec_78k0", RZ_FLIRT_SIG_ARCH_NEC_78K0 },
54  // { "nec_78k0s", RZ_FLIRT_SIG_ARCH_NEC_78K0S },
55  // { "m740", RZ_FLIRT_SIG_ARCH_M740 },
56  // { "m7700", RZ_FLIRT_SIG_ARCH_M7700 },
57  // { "st9", RZ_FLIRT_SIG_ARCH_ST9 },
58  // { "fr", RZ_FLIRT_SIG_ARCH_FR },
59  // { "mc6816", RZ_FLIRT_SIG_ARCH_MC6816 },
60  // { "m7900", RZ_FLIRT_SIG_ARCH_M7900 },
61  // { "tms320c3", RZ_FLIRT_SIG_ARCH_TMS320C3 },
62  // { "kr1878", RZ_FLIRT_SIG_ARCH_KR1878 },
63  // { "ad218x", RZ_FLIRT_SIG_ARCH_AD218X },
64  // { "oakdsp", RZ_FLIRT_SIG_ARCH_OAKDSP },
65  // { "tlcs900", RZ_FLIRT_SIG_ARCH_TLCS900 },
66  // { "c39", RZ_FLIRT_SIG_ARCH_C39 },
67  { "cr16", RZ_FLIRT_SIG_ARCH_CR16 },
68  // { "mn102l00", RZ_FLIRT_SIG_ARCH_MN102L00 },
69  // { "tms320c1x", RZ_FLIRT_SIG_ARCH_TMS320C1X },
70  { "v850", RZ_FLIRT_SIG_ARCH_NEC_V850X },
71  // { "scr_adpt", RZ_FLIRT_SIG_ARCH_SCR_ADPT },
72  { "ebc", RZ_FLIRT_SIG_ARCH_EBC },
73  { "msp430", RZ_FLIRT_SIG_ARCH_MSP430 },
74  // { "spu", RZ_FLIRT_SIG_ARCH_SPU },
75  { "dalvik", RZ_FLIRT_SIG_ARCH_DALVIK },
76  // { "65c816", RZ_FLIRT_SIG_ARCH_65C816 },
77  // { "ad2106x", RZ_FLIRT_SIG_ARCH_AD2106X },
78  { "arc", RZ_FLIRT_SIG_ARCH_ARC },
79  // { "dsp96k", RZ_FLIRT_SIG_ARCH_DSP96K },
80  // { "m16c", RZ_FLIRT_SIG_ARCH_M16C },
81  { "pic", RZ_FLIRT_SIG_ARCH_PIC16 },
82  { "riscv", RZ_FLIRT_SIG_ARCH_RISCV },
83  // { "rl78", RZ_FLIRT_SIG_ARCH_RL78 },
84  { "sysz", RZ_FLIRT_SIG_ARCH_S390 },
85  { "spc700", RZ_FLIRT_SIG_ARCH_SPC700 },
86  // { "tms320c28", RZ_FLIRT_SIG_ARCH_TMS320C28 },
87  // { "unsp", RZ_FLIRT_SIG_ARCH_UNSP },
88  { "xtensa", RZ_FLIRT_SIG_ARCH_XTENSA },
89 };
90 
91 const MapStringValue file_map[25] = {
92  { "dos:exe:old", RZ_FLIRT_SIG_FILE_DOS_EXE_OLD },
93  { "dos:com:old", RZ_FLIRT_SIG_FILE_DOS_COM_OLD },
94  { "bin", RZ_FLIRT_SIG_FILE_BIN },
95  { "dosdrv", RZ_FLIRT_SIG_FILE_DOSDRV },
96  { "ne", RZ_FLIRT_SIG_FILE_NE },
97  { "intelhex", RZ_FLIRT_SIG_FILE_INTELHEX },
98  { "moshex", RZ_FLIRT_SIG_FILE_MOSHEX },
99  { "lx", RZ_FLIRT_SIG_FILE_LX },
100  { "le", RZ_FLIRT_SIG_FILE_LE },
101  { "nlm", RZ_FLIRT_SIG_FILE_NLM },
102  { "coff", RZ_FLIRT_SIG_FILE_COFF },
103  { "pe", RZ_FLIRT_SIG_FILE_PE },
104  { "omf", RZ_FLIRT_SIG_FILE_OMF },
105  { "srec", RZ_FLIRT_SIG_FILE_SREC },
106  { "zip", RZ_FLIRT_SIG_FILE_ZIP },
107  { "omflib", RZ_FLIRT_SIG_FILE_OMFLIB },
108  { "ar", RZ_FLIRT_SIG_FILE_AR },
109  { "loader", RZ_FLIRT_SIG_FILE_LOADER },
110  { "elf", RZ_FLIRT_SIG_FILE_ELF },
111  { "w32run", RZ_FLIRT_SIG_FILE_W32RUN },
112  { "aout", RZ_FLIRT_SIG_FILE_AOUT },
113  { "pilot", RZ_FLIRT_SIG_FILE_PILOT },
114  { "dos:exe", RZ_FLIRT_SIG_FILE_DOS_EXE },
115  { "dos:com", RZ_FLIRT_SIG_FILE_DOS_COM },
116  { "aixar", RZ_FLIRT_SIG_FILE_AIXAR },
117 };
118 
119 const MapStringValue os_map[6] = {
120  { "msdos", RZ_FLIRT_SIG_OS_MSDOS },
121  { "win", RZ_FLIRT_SIG_OS_WIN },
122  { "os2", RZ_FLIRT_SIG_OS_OS2 },
123  { "netware", RZ_FLIRT_SIG_OS_NETWARE },
124  { "unix", RZ_FLIRT_SIG_OS_UNIX },
125  { "other", RZ_FLIRT_SIG_OS_OTHER },
126 };
127 
128 const MapStringValue app_map[10] = {
129  { "console", RZ_FLIRT_SIG_APP_CONSOLE },
130  { "graphics", RZ_FLIRT_SIG_APP_GRAPHICS },
131  { "exe", RZ_FLIRT_SIG_APP_EXE },
132  { "dll", RZ_FLIRT_SIG_APP_DLL },
133  { "drv", RZ_FLIRT_SIG_APP_DRV },
134  { "thread:single", RZ_FLIRT_SIG_APP_SINGLE_THREADED },
135  { "thread:multi", RZ_FLIRT_SIG_APP_MULTI_THREADED },
136  { "16bit", RZ_FLIRT_SIG_APP_16_BIT },
137  { "32bit", RZ_FLIRT_SIG_APP_32_BIT },
138  { "64bit", RZ_FLIRT_SIG_APP_64_BIT },
139 };
140 
150 
151  for (ut32 i = 0; i < RZ_ARRAY_SIZE(arch_map); ++i) {
152  if (strcmp(arch, arch_map[i].name)) {
153  continue;
154  }
155  return arch_map[i].value;
156  }
157 
158  return RZ_FLIRT_SIG_ARCH_ANY;
159 }
160 
169  for (ut32 i = 0; i < RZ_ARRAY_SIZE(arch_map); ++i) {
170  if (arch != arch_map[i].value) {
171  continue;
172  }
173  return arch_map[i].name;
174  }
175 
176  return "unknown";
177 }
178 
188  ut32 flags = 0;
189  const char *tk;
190  RzListIter *it;
191  RzList *tokens = NULL;
192 
193  if (strstr(file_list, "all")) {
194  return RZ_FLIRT_SIG_FILE_ALL;
195  }
196 
197  tokens = rz_str_split_duplist(file_list, ",", true);
198  if (!tokens) {
199  RZ_LOG_ERROR("cannot allocate token list\n");
200  return 0;
201  }
202 
203  rz_list_foreach (tokens, it, tk) {
204  for (ut32 i = 0; i < RZ_ARRAY_SIZE(file_map); ++i) {
205  if (strcmp(tk, file_map[i].name)) {
206  continue;
207  }
208  flags |= file_map[i].value;
209  }
210  }
211  rz_list_free(tokens);
212  return flags;
213 }
214 
224  ut32 flags = 0;
225  const char *tk;
226  RzListIter *it;
227  RzList *tokens = NULL;
228 
229  if (strstr(os_list, "all")) {
230  return RZ_FLIRT_SIG_OS_ALL;
231  }
232 
233  tokens = rz_str_split_duplist(os_list, ",", true);
234  if (!tokens) {
235  RZ_LOG_ERROR("cannot allocate token list\n");
236  return 0;
237  }
238 
239  rz_list_foreach (tokens, it, tk) {
240  for (ut32 i = 0; i < RZ_ARRAY_SIZE(os_map); ++i) {
241  if (strcmp(tk, os_map[i].name)) {
242  continue;
243  }
244  flags |= os_map[i].value;
245  }
246  }
247  rz_list_free(tokens);
248  return flags;
249 }
250 
260  ut32 flags = 0;
261  const char *tk;
262  RzListIter *it;
263  RzList *tokens = NULL;
264 
265  if (strstr(app_list, "all")) {
266  return RZ_FLIRT_SIG_APP_ALL;
267  }
268 
269  tokens = rz_str_split_duplist(app_list, ",", true);
270  if (!tokens) {
271  RZ_LOG_ERROR("cannot allocate token list\n");
272  return 0;
273  }
274 
275  rz_list_foreach (tokens, it, tk) {
276  for (ut32 i = 0; i < RZ_ARRAY_SIZE(app_map); ++i) {
277  if (strcmp(tk, app_map[i].name)) {
278  continue;
279  }
280  flags |= app_map[i].value;
281  }
282  }
283  rz_list_free(tokens);
284  return flags;
285 }
286 
288  RzListIter *pub_func_it, *ref_func_it, *tail_bytes_it;
289  RzFlirtFunction *func, *ref_func;
290  RzFlirtTailByte *tail_byte;
291 
292  rz_cons_printf("%02X %04X %04X ", module->crc_length, module->crc16, module->length);
293  rz_list_foreach (module->public_functions, pub_func_it, func) {
294  if (func->is_local || func->is_collision) {
295  rz_cons_printf("(");
296  if (func->is_local) {
297  rz_cons_printf("l");
298  }
299  if (func->is_collision) {
300  rz_cons_printf("!");
301  }
302  rz_cons_printf(")");
303  }
304  rz_cons_printf("%04X:%s", func->offset, func->name);
305  if (pub_func_it->n) {
306  rz_cons_printf(" ");
307  }
308  }
309  if (module->tail_bytes) {
310  rz_list_foreach (module->tail_bytes, tail_bytes_it, tail_byte) {
311  rz_cons_printf(" (%04X: %02X)", tail_byte->offset, tail_byte->value);
312  }
313  }
314  if (module->referenced_functions) {
315  rz_cons_printf(" (REF ");
316  rz_list_foreach (module->referenced_functions, ref_func_it, ref_func) {
317  rz_cons_printf("%04X: %s", ref_func->offset, ref_func->name);
318  if (ref_func_it->n) {
319  rz_cons_printf(" ");
320  }
321  }
322  rz_cons_printf(")");
323  }
324  rz_cons_printf("\n");
325 }
326 
327 static void flirt_print_node_pattern(const RzFlirtNode *node) {
328  for (ut32 i = 0; i < node->length; i++) {
329  if (node->pattern_mask[i]) {
330  rz_cons_printf("%02X", node->pattern_bytes[i]);
331  } else {
332  rz_cons_printf("..");
333  }
334  }
335  rz_cons_printf(":\n");
336 }
337 
338 static void flirt_print_indentation(int indent) {
339  rz_cons_printf("%s", rz_str_pad(' ', indent));
340 }
341 
342 static void flirt_print_node(const RzFlirtNode *node, int indent) {
343  /* Prints a signature node. The output is similar to dumpsig */
344  RzListIter *child_it, *module_it;
345  RzFlirtNode *child;
347 
348  if (node->pattern_bytes) { // avoid printing the root node
349  flirt_print_indentation(indent);
351  }
352  if (node->child_list) {
353  rz_list_foreach (node->child_list, child_it, child) {
354  flirt_print_node(child, indent + 1);
355  }
356  } else if (node->module_list) {
357  ut32 i = 0;
358  rz_list_foreach (node->module_list, module_it, module) {
359  flirt_print_indentation(indent + 1);
360  rz_cons_printf("%d. ", i);
362  i++;
363  }
364  }
365 }
366 
372 RZ_API bool rz_core_flirt_dump_file(RZ_NONNULL const char *flirt_file) {
373  rz_return_val_if_fail(RZ_STR_ISNOTEMPTY(flirt_file), false);
374 
375  const char *extension = rz_str_lchr(flirt_file, '.');
376  if (RZ_STR_ISEMPTY(extension) || (strcmp(extension, ".sig") != 0 && strcmp(extension, ".pat") != 0)) {
377  RZ_LOG_ERROR("FLIRT: unknown extension '%s'\n", extension);
378  return false;
379  }
380 
381  RzFlirtInfo info = { 0 };
382  RzBuffer *buffer = NULL;
383  RzFlirtNode *node = NULL;
384 
385  if (!(buffer = rz_buf_new_slurp(flirt_file))) {
386  RZ_LOG_ERROR("FLIRT: cannot open %s (read mode)\n", flirt_file);
387  return false;
388  } else if (!strcmp(extension, ".pat")) {
390  } else {
392  }
394 
395  if (!node) {
396  RZ_LOG_ERROR("FLIRT: we encountered an error while parsing the file. Sorry.\n");
397  return false;
398  }
399 
400  switch (info.type) {
402  rz_cons_printf("SIG format\n");
403  rz_cons_printf("Signature: %s, %u modules\n", info.u.sig.name ? info.u.sig.name : "", info.u.sig.n_modules);
404  rz_cons_printf("Version: %u\n", info.u.sig.version);
405  rz_cons_printf("Architecture: %u (%s)\n", info.u.sig.architecture, rz_core_flirt_arch_from_id(info.u.sig.architecture));
406  break;
408  rz_cons_printf("PAT format\n");
409  rz_cons_printf("Signature: %u modules\n", info.u.pat.n_modules);
410  break;
411  default:
413  break;
414  }
415 
416  flirt_print_node(node, -1);
419  return true;
420 }
421 
431 RZ_API bool rz_core_flirt_create_file(RZ_NONNULL RzCore *core, RZ_NONNULL const char *output_file, RZ_NULLABLE ut32 *written_nodes) {
432  rz_return_val_if_fail(core && RZ_STR_ISNOTEMPTY(output_file), false);
433 
434  const char *extension = rz_str_lchr(output_file, '.');
435  if (RZ_STR_ISEMPTY(extension) || (strcmp(extension, ".sig") != 0 && strcmp(extension, ".pat") != 0)) {
436  RZ_LOG_ERROR("missing or unknown extension '%s'. supported only .pat and .sig\n", extension);
437  return false;
438  }
439 
440  ut64 optimize = rz_config_get_i(core->config, "flirt.node.optimize");
441  if (optimize > RZ_FLIRT_NODE_OPTIMIZE_MAX) {
442  RZ_LOG_ERROR("config 'flirt.node.optimize' is set to an invalid value.\n");
443  return false;
444  }
445 
446  RzFlirtNode *node = rz_sign_flirt_node_new(core->analysis, optimize);
447  if (!node) {
448  return false;
449  }
450 
451  RzBuffer *buffer = rz_buf_new_file(output_file, O_RDWR | O_CREAT | O_TRUNC, 0644);
452  if (!buffer) {
453  RZ_LOG_ERROR("cannot create file '%s'\n", output_file);
454  return false;
455  }
456 
457  bool result = false;
458  if (!strcmp(extension, ".pat")) {
460  } else if (!strcmp(extension, ".sig")) {
461  ut64 hdr_version = rz_config_get_i(core->config, "flirt.sig.version");
462  const char *hdr_arch = rz_config_get(core->config, "asm.arch");
463  const char *hdr_file = rz_config_get(core->config, "flirt.sig.file");
464  const char *hdr_os = rz_config_get(core->config, "flirt.sig.os");
465  const char *hdr_lib = rz_config_get(core->config, "flirt.sig.library");
466  bool deflate = rz_config_get_b(core->config, "flirt.sig.deflate");
467  ut8 architecture = rz_core_flirt_arch_from_name(hdr_arch);
468 
469  if (RZ_STR_ISEMPTY(hdr_lib)) {
470  RZ_LOG_WARN("config 'flirt.sig.library' is empty. using default value '" RZ_FLIRT_LIBRARY_NAME_DFL "'\n");
471  hdr_lib = RZ_FLIRT_LIBRARY_NAME_DFL;
472  } else if (architecture >= RZ_FLIRT_SIG_ARCH_ANY) {
473  RZ_LOG_ERROR("FLIRT: architecture '%s' is not supported as .sig file, we suggest to use the .pat format.\n", hdr_arch);
474  RZ_LOG_ERROR("FLIRT: we suggest to open an issue to discuss with the rizin team about it.\n");
475  result = false;
476  goto fail;
477  }
478 
480  .version = hdr_version,
481  .arch = rz_core_flirt_arch_from_name(hdr_arch),
482  .file = rz_core_flirt_file_from_option_list(hdr_file),
483  .os = rz_core_flirt_os_from_option_list(hdr_os),
484  .app = RZ_FLIRT_SIG_APP_ALL,
485  .deflate = deflate,
486  .libname = hdr_lib,
487  };
489  }
490 
491  if (written_nodes) {
492  *written_nodes = rz_sign_flirt_node_count_nodes(node);
493  }
494 
495 fail:
498  return result;
499 }
500 
510 RZ_API bool rz_core_flirt_convert_file(RZ_NONNULL RzCore *core, RZ_NONNULL const char *input_file, RZ_NONNULL const char *output_file) {
511  rz_return_val_if_fail(core && RZ_STR_ISNOTEMPTY(input_file) && RZ_STR_ISNOTEMPTY(output_file), false);
512 
513  const char *in_extension = rz_str_lchr(input_file, '.');
514  if (RZ_STR_ISEMPTY(in_extension) || (strcmp(in_extension, ".sig") != 0 && strcmp(in_extension, ".pat") != 0)) {
515  RZ_LOG_ERROR("FLIRT: unknown input extension '%s'\n", in_extension);
516  return false;
517  }
518 
519  const char *out_extension = rz_str_lchr(output_file, '.');
520  if (RZ_STR_ISEMPTY(out_extension) || (strcmp(out_extension, ".sig") != 0 && strcmp(out_extension, ".pat") != 0)) {
521  RZ_LOG_ERROR("FLIRT: unknown output extension '%s'\n", out_extension);
522  return false;
523  }
524 
525  if (!strcmp(out_extension, in_extension)) {
526  RZ_LOG_ERROR("FLIRT: cannot convert '%s' to '%s' because the format is the same\n", input_file, output_file);
527  return false;
528  }
529 
530  RzBuffer *buffer = NULL;
531  RzFlirtNode *node = NULL;
532 
533  ut64 optimize = rz_config_get_i(core->config, "flirt.node.optimize");
534  if (optimize > RZ_FLIRT_NODE_OPTIMIZE_MAX) {
535  RZ_LOG_ERROR("config 'flirt.node.optimize' is set to an invalid value.\n");
536  return false;
537  }
538 
539  if (!(buffer = rz_buf_new_slurp(input_file))) {
540  RZ_LOG_ERROR("FLIRT: cannot open %s (read mode)\n", input_file);
541  return false;
542  } else if (!strcmp(in_extension, ".pat")) {
544  } else {
546  }
548 
549  if (!node) {
550  RZ_LOG_ERROR("FLIRT: we encountered an error while parsing the file. Sorry.\n");
551  return false;
552  }
553 
554  bool result = false;
555  if (!(buffer = rz_buf_new_file(output_file, O_RDWR | O_CREAT | O_TRUNC, 0644))) {
556  RZ_LOG_ERROR("FLIRT: cannot open %s (write mode)\n", output_file);
557  return false;
558  } else if (!strcmp(out_extension, ".pat")) {
560  } else {
561  ut64 hdr_version = rz_config_get_i(core->config, "flirt.sig.version");
562  const char *hdr_arch = rz_config_get(core->config, "asm.arch");
563  const char *hdr_file = rz_config_get(core->config, "flirt.sig.file");
564  const char *hdr_os = rz_config_get(core->config, "flirt.sig.os");
565  const char *hdr_lib = rz_config_get(core->config, "flirt.sig.library");
566  bool deflate = rz_config_get_b(core->config, "flirt.sig.deflate");
567  ut8 architecture = rz_core_flirt_arch_from_name(hdr_arch);
568 
569  if (RZ_STR_ISEMPTY(hdr_lib)) {
570  RZ_LOG_WARN("config 'flirt.sig.library' is empty. using default value '" RZ_FLIRT_LIBRARY_NAME_DFL "'\n");
571  hdr_lib = RZ_FLIRT_LIBRARY_NAME_DFL;
572  } else if (architecture >= RZ_FLIRT_SIG_ARCH_ANY) {
573  RZ_LOG_ERROR("FLIRT: architecture '%s' is not supported as .sig file, we suggest to use the .pat format.\n", hdr_arch);
574  RZ_LOG_ERROR("FLIRT: we suggest to open an issue to discuss with the rizin team about it.\n");
575  result = false;
576  goto fail;
577  }
578 
580  .version = hdr_version,
581  .arch = architecture,
582  .file = rz_core_flirt_file_from_option_list(hdr_file),
583  .os = rz_core_flirt_os_from_option_list(hdr_os),
584  .app = RZ_FLIRT_SIG_APP_ALL,
585  .deflate = deflate,
586  .libname = hdr_lib,
587  };
589  }
590 
591 fail:
594  return result;
595 }
lzma_index ** i
Definition: index.h:629
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
static int value
Definition: cmd_api.c:93
RZ_API ut64 rz_config_get_i(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:119
RZ_API bool rz_config_get_b(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:142
RZ_API RZ_BORROW const char * rz_config_get(RzConfig *cfg, RZ_NONNULL const char *name)
Definition: config.c:75
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
#define RZ_API
#define NULL
Definition: cris-opc.c:27
RZ_API ut16 rz_core_flirt_os_from_option_list(RZ_NONNULL const char *os_list)
Returns the FLIRT file flags from a given list (comma spaced) of file types Returns RZ_FLIRT_SIG_OS_A...
Definition: csign.c:222
const MapStringValue file_map[25]
Definition: csign.c:91
static void flirt_print_node(const RzFlirtNode *node, int indent)
Definition: csign.c:342
RZ_API ut16 rz_core_flirt_app_from_option_list(RZ_NONNULL const char *app_list)
Returns the FLIRT file flags from a given list (comma spaced) of file types Returns RZ_FLIRT_SIG_APP_...
Definition: csign.c:258
const MapStringValue app_map[10]
Definition: csign.c:128
const MapStringValue arch_map[29]
Definition: csign.c:14
static void flirt_print_indentation(int indent)
Definition: csign.c:338
static void flirt_print_node_pattern(const RzFlirtNode *node)
Definition: csign.c:327
RZ_API bool rz_core_flirt_convert_file(RZ_NONNULL RzCore *core, RZ_NONNULL const char *input_file, RZ_NONNULL const char *output_file)
converts a FLIRT file to the other format.
Definition: csign.c:510
RZ_API const char * rz_core_flirt_arch_from_id(ut8 arch)
Returns the FLIRT arch name (string format) from a given arch id Returns "unknown" if name is not fou...
Definition: csign.c:168
RZ_API ut32 rz_core_flirt_file_from_option_list(RZ_NONNULL const char *file_list)
Returns the FLIRT file flags from a given list (comma spaced) of file types Returns RZ_FLIRT_SIG_FILE...
Definition: csign.c:186
static void flirt_print_module(const RzFlirtModule *module)
Definition: csign.c:287
RZ_API ut8 rz_core_flirt_arch_from_name(RZ_NONNULL const char *arch)
Returns the FLIRT arch id from a given arch name Returns RZ_FLIRT_SIG_ARCH_ANY if name is not found.
Definition: csign.c:148
RZ_API bool rz_core_flirt_create_file(RZ_NONNULL RzCore *core, RZ_NONNULL const char *output_file, RZ_NULLABLE ut32 *written_nodes)
Generates a new FLIRT file from a given RzCore structure.
Definition: csign.c:431
struct map_string_value_t MapStringValue
const MapStringValue os_map[6]
Definition: csign.c:119
RZ_API bool rz_core_flirt_dump_file(RZ_NONNULL const char *flirt_file)
Dumps the contents of a FLIRT file.
Definition: csign.c:372
cs_arch arch
Definition: cstool.c:13
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:804
uint16_t ut16
uint32_t ut32
uint8_t ut8
Definition: lh5801.h:11
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#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 RZ_OWN RzBuffer * rz_buf_new_slurp(const char *file)
Creates a new buffer from a file.
Definition: buf.c:384
RZ_API void rz_buf_free(RzBuffer *b)
Free all internal data hold by the buffer and the buffer.
Definition: buf.c:1253
RZ_API RZ_OWN RzBuffer * rz_buf_new_file(const char *file, int perm, int mode)
Creates a new buffer from a file.
Definition: buf.c:317
RZ_API RZ_OWN RzFlirtNode * rz_sign_flirt_parse_string_pattern_from_buffer(RZ_NONNULL RzBuffer *flirt_buf, ut32 optimization, RZ_NULLABLE RzFlirtInfo *info)
Parses the RzBuffer containing a FLIRT signature in string format and returns an RzFlirtNode.
Definition: pat.c:356
#define RZ_FLIRT_SIG_OS_OTHER
Definition: rz_flirt.h:136
RZ_API void rz_sign_flirt_node_free(RZ_NULLABLE RzFlirtNode *node)
Frees an RzFlirtNode struct.
Definition: flirt.c:299
#define RZ_FLIRT_SIG_FILE_NE
Definition: rz_flirt.h:107
#define RZ_FLIRT_SIG_FILE_SREC
Definition: rz_flirt.h:116
#define RZ_FLIRT_SIG_APP_16_BIT
Definition: rz_flirt.h:147
#define RZ_FLIRT_SIG_OS_MSDOS
Definition: rz_flirt.h:131
RZ_API RZ_OWN RzFlirtNode * rz_sign_flirt_node_new(RZ_NONNULL RzAnalysis *analysis, ut32 optimization)
Generates the FLIRT signatures and returns an RzFlirtNode.
Definition: create.c:355
#define RZ_FLIRT_SIG_APP_GRAPHICS
Definition: rz_flirt.h:141
#define RZ_FLIRT_SIG_FILE_COFF
Definition: rz_flirt.h:113
#define RZ_FLIRT_SIG_FILE_W32RUN
Definition: rz_flirt.h:122
#define RZ_FLIRT_SIG_FILE_DOS_COM
Definition: rz_flirt.h:126
RZ_API void rz_sign_flirt_info_fini(RZ_NULLABLE RzFlirtInfo *info)
Frees an RzFlirtInfo struct elements without freeing the pointer.
Definition: flirt.c:315
#define RZ_FLIRT_SIG_FILE_DOS_EXE_OLD
Definition: rz_flirt.h:103
#define RZ_FLIRT_SIG_FILE_ELF
Definition: rz_flirt.h:121
RZ_API ut32 rz_sign_flirt_node_count_nodes(RZ_NONNULL const RzFlirtNode *node)
Counts the number of FLIRT signatures in the node.
Definition: flirt.c:1334
#define RZ_FLIRT_SIG_APP_CONSOLE
Definition: rz_flirt.h:140
#define RZ_FLIRT_SIG_APP_ALL
Definition: rz_flirt.h:150
RZ_API bool rz_sign_flirt_write_string_pattern_to_buffer(RZ_NONNULL const RzFlirtNode *node, RZ_NONNULL RzBuffer *buffer)
Writes in the the RzBuffer the FLIRT signatures in string format.
Definition: pat.c:529
#define RZ_FLIRT_SIG_APP_MULTI_THREADED
Definition: rz_flirt.h:146
#define RZ_FLIRT_SIG_FILE_AR
Definition: rz_flirt.h:119
@ RZ_FLIRT_NODE_OPTIMIZE_NORMAL
optimize the tree structure (keeps the tail bytes)
Definition: rz_flirt.h:187
@ RZ_FLIRT_NODE_OPTIMIZE_MAX
optimize the tree structure and drops the tail bytes
Definition: rz_flirt.h:188
#define RZ_FLIRT_SIG_APP_EXE
Definition: rz_flirt.h:142
#define RZ_FLIRT_SIG_FILE_OMFLIB
Definition: rz_flirt.h:118
#define RZ_FLIRT_SIG_APP_SINGLE_THREADED
Definition: rz_flirt.h:145
#define RZ_FLIRT_SIG_FILE_DOSDRV
Definition: rz_flirt.h:106
#define RZ_FLIRT_SIG_OS_ALL
Definition: rz_flirt.h:137
#define RZ_FLIRT_SIG_APP_DRV
Definition: rz_flirt.h:144
#define RZ_FLIRT_SIG_FILE_INTELHEX
Definition: rz_flirt.h:108
#define RZ_FLIRT_SIG_FILE_LOADER
Definition: rz_flirt.h:120
#define RZ_FLIRT_SIG_FILE_PILOT
Definition: rz_flirt.h:124
#define RZ_FLIRT_SIG_OS_NETWARE
Definition: rz_flirt.h:134
#define RZ_FLIRT_SIG_FILE_AOUT
Definition: rz_flirt.h:123
#define RZ_FLIRT_SIG_APP_64_BIT
Definition: rz_flirt.h:149
#define RZ_FLIRT_SIG_FILE_PE
Definition: rz_flirt.h:114
#define RZ_FLIRT_SIG_FILE_MOSHEX
Definition: rz_flirt.h:109
#define RZ_FLIRT_SIG_OS_WIN
Definition: rz_flirt.h:132
#define RZ_FLIRT_SIG_FILE_NLM
Definition: rz_flirt.h:112
RZ_API RZ_OWN RzFlirtNode * rz_sign_flirt_parse_compressed_pattern_from_buffer(RZ_NONNULL RzBuffer *flirt_buf, ut8 expected_arch, RZ_NULLABLE RzFlirtInfo *info)
Parses the RzBuffer containing a FLIRT structure and returns an RzFlirtNode.
Definition: flirt.c:1136
#define RZ_FLIRT_SIG_FILE_LE
Definition: rz_flirt.h:111
#define RZ_FLIRT_SIG_FILE_OMF
Definition: rz_flirt.h:115
RZ_API bool rz_sign_flirt_write_compressed_pattern_to_buffer(RZ_NONNULL const RzFlirtNode *node, RZ_NONNULL RzBuffer *buffer, RzFlirtCompressedOptions *options)
Writes in the the RzBuffer the FLIRT signatures in compressed format.
Definition: flirt.c:1580
#define RZ_FLIRT_SIG_OS_OS2
Definition: rz_flirt.h:133
@ RZ_FLIRT_SIG_ARCH_SPC700
Definition: rz_flirt.h:91
@ RZ_FLIRT_SIG_ARCH_6502
Definition: rz_flirt.h:29
@ RZ_FLIRT_SIG_ARCH_386
Definition: rz_flirt.h:24
@ RZ_FLIRT_SIG_ARCH_EBC
Definition: rz_flirt.h:81
@ RZ_FLIRT_SIG_ARCH_68K
Definition: rz_flirt.h:31
@ RZ_FLIRT_SIG_ARCH_ARM
Definition: rz_flirt.h:37
@ RZ_FLIRT_SIG_ARCH_8051
Definition: rz_flirt.h:27
@ RZ_FLIRT_SIG_ARCH_S390
Definition: rz_flirt.h:94
@ RZ_FLIRT_SIG_ARCH_SH
Definition: rz_flirt.h:42
@ RZ_FLIRT_SIG_ARCH_ANY
Definition: rz_flirt.h:99
@ RZ_FLIRT_SIG_ARCH_NEC_V850X
Definition: rz_flirt.h:79
@ RZ_FLIRT_SIG_ARCH_TMS320C55
Definition: rz_flirt.h:59
@ RZ_FLIRT_SIG_ARCH_RISCV
Definition: rz_flirt.h:96
@ RZ_FLIRT_SIG_ARCH_SPARC
Definition: rz_flirt.h:47
@ RZ_FLIRT_SIG_ARCH_TRICORE
Definition: rz_flirt.h:51
@ RZ_FLIRT_SIG_ARCH_Z80
Definition: rz_flirt.h:25
@ RZ_FLIRT_SIG_ARCH_DALVIK
Definition: rz_flirt.h:84
@ RZ_FLIRT_SIG_ARCH_6800
Definition: rz_flirt.h:33
@ RZ_FLIRT_SIG_ARCH_PIC16
Definition: rz_flirt.h:93
@ RZ_FLIRT_SIG_ARCH_AVR
Definition: rz_flirt.h:44
@ RZ_FLIRT_SIG_ARCH_HPPA
Definition: rz_flirt.h:49
@ RZ_FLIRT_SIG_ARCH_ARC
Definition: rz_flirt.h:87
@ RZ_FLIRT_SIG_ARCH_MSP430
Definition: rz_flirt.h:82
@ RZ_FLIRT_SIG_ARCH_H8
Definition: rz_flirt.h:45
@ RZ_FLIRT_SIG_ARCH_PPC
Definition: rz_flirt.h:39
@ RZ_FLIRT_SIG_ARCH_PIC
Definition: rz_flirt.h:46
@ RZ_FLIRT_SIG_ARCH_MIPS
Definition: rz_flirt.h:36
@ RZ_FLIRT_SIG_ARCH_JAVA
Definition: rz_flirt.h:32
@ RZ_FLIRT_SIG_ARCH_XTENSA
Definition: rz_flirt.h:95
@ RZ_FLIRT_SIG_ARCH_CR16
Definition: rz_flirt.h:76
@ RZ_FLIRT_FILE_TYPE_PAT
.pat text format pattern file
Definition: rz_flirt.h:194
@ RZ_FLIRT_FILE_TYPE_SIG
.sig compressed pattern file
Definition: rz_flirt.h:193
#define RZ_FLIRT_SIG_FILE_DOS_COM_OLD
Definition: rz_flirt.h:104
#define RZ_FLIRT_SIG_FILE_AIXAR
Definition: rz_flirt.h:127
#define RZ_FLIRT_SIG_APP_32_BIT
Definition: rz_flirt.h:148
#define RZ_FLIRT_SIG_OS_UNIX
Definition: rz_flirt.h:135
#define RZ_FLIRT_SIG_FILE_ZIP
Definition: rz_flirt.h:117
#define RZ_FLIRT_SIG_FILE_ALL
Definition: rz_flirt.h:128
#define RZ_FLIRT_SIG_FILE_DOS_EXE
Definition: rz_flirt.h:125
#define RZ_FLIRT_LIBRARY_NAME_DFL
Definition: rz_flirt.h:19
#define RZ_FLIRT_SIG_FILE_LX
Definition: rz_flirt.h:110
#define RZ_FLIRT_SIG_APP_DLL
Definition: rz_flirt.h:143
#define RZ_FLIRT_SIG_FILE_BIN
Definition: rz_flirt.h:105
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669
RZ_API RzList * rz_str_split_duplist(const char *str, const char *c, bool trim)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3464
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
RZ_API const char * rz_str_pad(const char ch, int len)
Definition: str.c:3236
#define RZ_NULLABLE
Definition: rz_types.h:65
#define RZ_NONNULL
Definition: rz_types.h:64
#define RZ_ARRAY_SIZE(x)
Definition: rz_types.h:300
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
#define O_CREAT
Definition: sftypes.h:489
#define O_RDWR
Definition: sftypes.h:488
#define O_TRUNC
Definition: sftypes.h:492
Definition: buffer.h:15
const char * name
Definition: csign.c:10
Definition: sftypes.h:77
Definition: z80asm.h:102
char * type
Definition: rz_bin.h:211
char name[RZ_FLIRT_NAME_MAX]
Definition: rz_flirt.h:158
RzList * child_list
Definition: rz_flirt.h:177
RzList * module_list
Definition: rz_flirt.h:178
ut8 * pattern_mask
Definition: rz_flirt.h:182
ut8 * pattern_bytes
Definition: rz_flirt.h:181
struct rz_list_iter_t * n
Definition: rz_list.h:15
#define fail(test)
Definition: tests.h:29
ut64(WINAPI *w32_GetEnabledXStateFeatures)()