Rizin
unix-like reverse engineering framework and cli tools
csign.c File Reference
#include <rz_core.h>
#include <rz_flirt.h>

Go to the source code of this file.

Classes

struct  map_string_value_t
 

Typedefs

typedef struct map_string_value_t MapStringValue
 

Functions

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. More...
 
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 found. More...
 
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_ALL if file_list is "any" and 0 if no valid value is not found. More...
 
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_ALL if file_list is "any" and 0 if no valid value is not found. More...
 
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_ALL if file_list is "any" and 0 if no valid value is not found. More...
 
static void flirt_print_module (const RzFlirtModule *module)
 
static void flirt_print_node_pattern (const RzFlirtNode *node)
 
static void flirt_print_indentation (int indent)
 
static void flirt_print_node (const RzFlirtNode *node, int indent)
 
RZ_API bool rz_core_flirt_dump_file (RZ_NONNULL const char *flirt_file)
 Dumps the contents of a FLIRT file. More...
 
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. More...
 
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. More...
 

Variables

const MapStringValue arch_map [29]
 
const MapStringValue file_map [25]
 
const MapStringValue os_map [6]
 
const MapStringValue app_map [10]
 

Typedef Documentation

◆ MapStringValue

Function Documentation

◆ flirt_print_indentation()

static void flirt_print_indentation ( int  indent)
static

Definition at line 338 of file csign.c.

338  {
339  rz_cons_printf("%s", rz_str_pad(' ', indent));
340 }
RZ_API int rz_cons_printf(const char *format,...)
Definition: cons.c:1202
RZ_API const char * rz_str_pad(const char ch, int len)
Definition: str.c:3236

References rz_cons_printf(), and rz_str_pad().

Referenced by flirt_print_node().

◆ flirt_print_module()

static void flirt_print_module ( const RzFlirtModule module)
static

Definition at line 287 of file csign.c.

287  {
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 }
Definition: sftypes.h:77
char name[RZ_FLIRT_NAME_MAX]
Definition: rz_flirt.h:158
struct rz_list_iter_t * n
Definition: rz_list.h:15

References rz_flirt_function_t::is_collision, rz_flirt_function_t::is_local, rz_list_iter_t::n, rz_flirt_function_t::name, rz_flirt_tail_byte_t::offset, rz_flirt_function_t::offset, rz_cons_printf(), and rz_flirt_tail_byte_t::value.

Referenced by flirt_print_node().

◆ flirt_print_node()

static void flirt_print_node ( const RzFlirtNode node,
int  indent 
)
static

Definition at line 342 of file csign.c.

342  {
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 }
lzma_index ** i
Definition: index.h:629
static void flirt_print_node(const RzFlirtNode *node, int indent)
Definition: csign.c:342
static void flirt_print_indentation(int indent)
Definition: csign.c:338
static void flirt_print_node_pattern(const RzFlirtNode *node)
Definition: csign.c:327
static void flirt_print_module(const RzFlirtModule *module)
Definition: csign.c:287
uint32_t ut32
RzList * child_list
Definition: rz_flirt.h:177
RzList * module_list
Definition: rz_flirt.h:178
ut8 * pattern_bytes
Definition: rz_flirt.h:181

References rz_flirt_node_t::child_list, flirt_print_indentation(), flirt_print_module(), flirt_print_node_pattern(), i, rz_flirt_node_t::module_list, rz_flirt_node_t::pattern_bytes, and rz_cons_printf().

Referenced by rz_core_flirt_dump_file().

◆ flirt_print_node_pattern()

static void flirt_print_node_pattern ( const RzFlirtNode node)
static

Definition at line 327 of file csign.c.

327  {
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 }
ut8 * pattern_mask
Definition: rz_flirt.h:182

References i, rz_flirt_node_t::length, rz_flirt_node_t::pattern_bytes, rz_flirt_node_t::pattern_mask, and rz_cons_printf().

Referenced by flirt_print_node().

◆ rz_core_flirt_app_from_option_list()

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_ALL if file_list is "any" and 0 if no valid value is not found.

Parameters
app_listThe app list to convert to flags
Returns
The FLIRT app flags.

Definition at line 258 of file csign.c.

258  {
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 }
#define NULL
Definition: cris-opc.c:27
const MapStringValue app_map[10]
Definition: csign.c:128
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_FLIRT_SIG_APP_ALL
Definition: rz_flirt.h:150
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68
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_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
Definition: z80asm.h:102

References app_map, flags, i, NULL, RZ_ARRAY_SIZE, RZ_FLIRT_SIG_APP_ALL, rz_list_free(), RZ_LOG_ERROR, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_str_split_duplist(), and map_string_value_t::value.

◆ rz_core_flirt_arch_from_id()

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 found.

Parameters
archThe arch to convert to string
Returns
The FLIRT arch name.

Definition at line 168 of file csign.c.

168  {
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 }
static int value
Definition: cmd_api.c:93
const MapStringValue arch_map[29]
Definition: csign.c:14
cs_arch arch
Definition: cstool.c:13
const char * name
Definition: csign.c:10

References arch, arch_map, i, map_string_value_t::name, RZ_ARRAY_SIZE, and value.

Referenced by rz_core_flirt_dump_file().

◆ rz_core_flirt_arch_from_name()

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.

Parameters
archThe arch to convert to id
Returns
The FLIRT arch id.

Definition at line 148 of file csign.c.

148  {
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 }
@ RZ_FLIRT_SIG_ARCH_ANY
Definition: rz_flirt.h:99

References arch, arch_map, i, RZ_ARRAY_SIZE, RZ_FLIRT_SIG_ARCH_ANY, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, and map_string_value_t::value.

Referenced by rz_core_analysis_sigdb_apply(), rz_core_flirt_convert_file(), rz_core_flirt_create_file(), and rz_flirt_scan_handler().

◆ rz_core_flirt_convert_file()

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.

Parameters
input_fileInput file
output_fileOutput file
optimizeOptimization value (expects an RZ_FLIRT_NODE_OPTIMIZE_* value)
Returns
true on success, false on failure

Definition at line 510 of file csign.c.

510  {
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 }
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 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
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
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
int ZEXPORT deflate(z_streamp strm, int flush)
Definition: deflate.c:804
uint8_t ut8
Definition: lh5801.h:11
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
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
RZ_API void rz_sign_flirt_node_free(RZ_NULLABLE RzFlirtNode *node)
Frees an RzFlirtNode struct.
Definition: flirt.c:299
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
@ RZ_FLIRT_NODE_OPTIMIZE_MAX
optimize the tree structure and drops the tail bytes
Definition: rz_flirt.h:188
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
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_LIBRARY_NAME_DFL
Definition: rz_flirt.h:19
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
RZ_API const char * rz_str_lchr(const char *str, char chr)
Definition: str.c:669
#define RZ_STR_ISEMPTY(x)
Definition: rz_str.h:67
#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
#define fail(test)
Definition: tests.h:29
ut64(WINAPI *w32_GetEnabledXStateFeatures)()

References deflate(), fail, NULL, O_CREAT, O_RDWR, O_TRUNC, options, rz_buf_free(), rz_buf_new_file(), rz_buf_new_slurp(), rz_config_get(), rz_config_get_b(), rz_config_get_i(), rz_core_flirt_arch_from_name(), rz_core_flirt_file_from_option_list(), rz_core_flirt_os_from_option_list(), RZ_FLIRT_LIBRARY_NAME_DFL, RZ_FLIRT_NODE_OPTIMIZE_MAX, RZ_FLIRT_SIG_APP_ALL, RZ_FLIRT_SIG_ARCH_ANY, RZ_LOG_ERROR, RZ_LOG_WARN, rz_return_val_if_fail, rz_sign_flirt_node_free(), rz_sign_flirt_parse_compressed_pattern_from_buffer(), rz_sign_flirt_parse_string_pattern_from_buffer(), rz_sign_flirt_write_compressed_pattern_to_buffer(), rz_sign_flirt_write_string_pattern_to_buffer(), RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, rz_str_lchr(), and ut64().

Referenced by rz_main_rz_sign().

◆ rz_core_flirt_create_file()

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.

Parameters
coreRzCore to use.
output_fileOutput file.
written_nodesWhen not NULL, returns the number of nodes written in the file.
Returns
true on success, false on failure

Definition at line 431 of file csign.c.

431  {
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 }
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
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

References deflate(), fail, O_CREAT, O_RDWR, O_TRUNC, options, rz_buf_free(), rz_buf_new_file(), rz_config_get(), rz_config_get_b(), rz_config_get_i(), rz_core_flirt_arch_from_name(), rz_core_flirt_file_from_option_list(), rz_core_flirt_os_from_option_list(), RZ_FLIRT_LIBRARY_NAME_DFL, RZ_FLIRT_NODE_OPTIMIZE_MAX, RZ_FLIRT_SIG_APP_ALL, RZ_FLIRT_SIG_ARCH_ANY, RZ_LOG_ERROR, RZ_LOG_WARN, rz_return_val_if_fail, rz_sign_flirt_node_count_nodes(), rz_sign_flirt_node_free(), rz_sign_flirt_node_new(), rz_sign_flirt_write_compressed_pattern_to_buffer(), rz_sign_flirt_write_string_pattern_to_buffer(), RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, rz_str_lchr(), and ut64().

Referenced by rz_flirt_create_handler(), and rz_main_rz_sign().

◆ rz_core_flirt_dump_file()

RZ_API bool rz_core_flirt_dump_file ( RZ_NONNULL const char *  flirt_file)

Dumps the contents of a FLIRT file.

Parameters
flirt_fileFLIRT file name to dump

Definition at line 372 of file csign.c.

372  {
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 }
RzBinInfo * info(RzBinFile *bf)
Definition: bin_ne.c:86
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
#define rz_warn_if_reached()
Definition: rz_assert.h:29
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
@ RZ_FLIRT_NODE_OPTIMIZE_NORMAL
optimize the tree structure (keeps the tail bytes)
Definition: rz_flirt.h:187
@ 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
char * type
Definition: rz_bin.h:211

References flirt_print_node(), info(), NULL, rz_buf_free(), rz_buf_new_slurp(), rz_cons_printf(), rz_core_flirt_arch_from_id(), RZ_FLIRT_FILE_TYPE_PAT, RZ_FLIRT_FILE_TYPE_SIG, RZ_FLIRT_NODE_OPTIMIZE_NORMAL, RZ_FLIRT_SIG_ARCH_ANY, RZ_LOG_ERROR, rz_return_val_if_fail, rz_sign_flirt_info_fini(), rz_sign_flirt_node_free(), rz_sign_flirt_parse_compressed_pattern_from_buffer(), rz_sign_flirt_parse_string_pattern_from_buffer(), RZ_STR_ISEMPTY, RZ_STR_ISNOTEMPTY, rz_str_lchr(), rz_warn_if_reached, and rz_bin_info_t::type.

Referenced by rz_flirt_dump_handler(), and rz_main_rz_sign().

◆ rz_core_flirt_file_from_option_list()

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_ALL if file_list is "any" and 0 if no valid value is not found.

Parameters
file_listThe file list to convert to flags
Returns
The FLIRT file flags.

Definition at line 186 of file csign.c.

186  {
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 }
const MapStringValue file_map[25]
Definition: csign.c:91
#define RZ_FLIRT_SIG_FILE_ALL
Definition: rz_flirt.h:128

References file_map, flags, i, NULL, RZ_ARRAY_SIZE, RZ_FLIRT_SIG_FILE_ALL, rz_list_free(), RZ_LOG_ERROR, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_str_split_duplist(), and map_string_value_t::value.

Referenced by rz_core_flirt_convert_file(), and rz_core_flirt_create_file().

◆ rz_core_flirt_os_from_option_list()

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_ALL if file_list is "any" and 0 if no valid value is not found.

Parameters
os_listThe os list to convert to flags
Returns
The FLIRT os flags.

Definition at line 222 of file csign.c.

222  {
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 }
const MapStringValue os_map[6]
Definition: csign.c:119
#define RZ_FLIRT_SIG_OS_ALL
Definition: rz_flirt.h:137

References flags, i, NULL, os_map, RZ_ARRAY_SIZE, RZ_FLIRT_SIG_OS_ALL, rz_list_free(), RZ_LOG_ERROR, rz_return_val_if_fail, RZ_STR_ISNOTEMPTY, rz_str_split_duplist(), and map_string_value_t::value.

Referenced by rz_core_flirt_convert_file(), and rz_core_flirt_create_file().

Variable Documentation

◆ app_map

const MapStringValue app_map[10]
Initial value:
= {
{ "console", RZ_FLIRT_SIG_APP_CONSOLE },
{ "graphics", RZ_FLIRT_SIG_APP_GRAPHICS },
{ "thread:single", RZ_FLIRT_SIG_APP_SINGLE_THREADED },
{ "thread:multi", RZ_FLIRT_SIG_APP_MULTI_THREADED },
}
#define RZ_FLIRT_SIG_APP_16_BIT
Definition: rz_flirt.h:147
#define RZ_FLIRT_SIG_APP_GRAPHICS
Definition: rz_flirt.h:141
#define RZ_FLIRT_SIG_APP_CONSOLE
Definition: rz_flirt.h:140
#define RZ_FLIRT_SIG_APP_MULTI_THREADED
Definition: rz_flirt.h:146
#define RZ_FLIRT_SIG_APP_EXE
Definition: rz_flirt.h:142
#define RZ_FLIRT_SIG_APP_SINGLE_THREADED
Definition: rz_flirt.h:145
#define RZ_FLIRT_SIG_APP_DRV
Definition: rz_flirt.h:144
#define RZ_FLIRT_SIG_APP_64_BIT
Definition: rz_flirt.h:149
#define RZ_FLIRT_SIG_APP_32_BIT
Definition: rz_flirt.h:148
#define RZ_FLIRT_SIG_APP_DLL
Definition: rz_flirt.h:143

Definition at line 128 of file csign.c.

Referenced by rz_core_flirt_app_from_option_list().

◆ arch_map

const MapStringValue arch_map[29]

Definition at line 14 of file csign.c.

Referenced by rz_core_flirt_arch_from_id(), and rz_core_flirt_arch_from_name().

◆ file_map

const MapStringValue file_map[25]
Initial value:
= {
{ "dos:exe:old", RZ_FLIRT_SIG_FILE_DOS_EXE_OLD },
{ "dos:com:old", RZ_FLIRT_SIG_FILE_DOS_COM_OLD },
{ "dosdrv", RZ_FLIRT_SIG_FILE_DOSDRV },
{ "intelhex", RZ_FLIRT_SIG_FILE_INTELHEX },
{ "moshex", RZ_FLIRT_SIG_FILE_MOSHEX },
{ "omflib", RZ_FLIRT_SIG_FILE_OMFLIB },
{ "loader", RZ_FLIRT_SIG_FILE_LOADER },
{ "w32run", RZ_FLIRT_SIG_FILE_W32RUN },
{ "dos:exe", RZ_FLIRT_SIG_FILE_DOS_EXE },
{ "dos:com", RZ_FLIRT_SIG_FILE_DOS_COM },
}
#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_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
#define RZ_FLIRT_SIG_FILE_DOS_EXE_OLD
Definition: rz_flirt.h:103
#define RZ_FLIRT_SIG_FILE_ELF
Definition: rz_flirt.h:121
#define RZ_FLIRT_SIG_FILE_AR
Definition: rz_flirt.h:119
#define RZ_FLIRT_SIG_FILE_OMFLIB
Definition: rz_flirt.h:118
#define RZ_FLIRT_SIG_FILE_DOSDRV
Definition: rz_flirt.h:106
#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_FILE_AOUT
Definition: rz_flirt.h:123
#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_FILE_NLM
Definition: rz_flirt.h:112
#define RZ_FLIRT_SIG_FILE_LE
Definition: rz_flirt.h:111
#define RZ_FLIRT_SIG_FILE_OMF
Definition: rz_flirt.h:115
#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_FILE_ZIP
Definition: rz_flirt.h:117
#define RZ_FLIRT_SIG_FILE_DOS_EXE
Definition: rz_flirt.h:125
#define RZ_FLIRT_SIG_FILE_LX
Definition: rz_flirt.h:110
#define RZ_FLIRT_SIG_FILE_BIN
Definition: rz_flirt.h:105

Definition at line 91 of file csign.c.

Referenced by rz_core_flirt_file_from_option_list().

◆ os_map

const MapStringValue os_map[6]
Initial value:
= {
{ "msdos", RZ_FLIRT_SIG_OS_MSDOS },
{ "win", RZ_FLIRT_SIG_OS_WIN },
{ "os2", RZ_FLIRT_SIG_OS_OS2 },
{ "netware", RZ_FLIRT_SIG_OS_NETWARE },
{ "unix", RZ_FLIRT_SIG_OS_UNIX },
{ "other", RZ_FLIRT_SIG_OS_OTHER },
}
#define RZ_FLIRT_SIG_OS_OTHER
Definition: rz_flirt.h:136
#define RZ_FLIRT_SIG_OS_MSDOS
Definition: rz_flirt.h:131
#define RZ_FLIRT_SIG_OS_NETWARE
Definition: rz_flirt.h:134
#define RZ_FLIRT_SIG_OS_WIN
Definition: rz_flirt.h:132
#define RZ_FLIRT_SIG_OS_OS2
Definition: rz_flirt.h:133
#define RZ_FLIRT_SIG_OS_UNIX
Definition: rz_flirt.h:135

Definition at line 119 of file csign.c.

Referenced by rz_core_flirt_os_from_option_list().