Added r_file_globsearch and zfs **.sig support

Updated zf? help

Removed debug eprintf
This commit is contained in:
cyanpencil 2018-07-21 20:31:36 +02:00 committed by Anton Kochkov
parent e1c2af7570
commit b9be6c9a8c
5 changed files with 79 additions and 3 deletions

View File

@ -1471,12 +1471,12 @@ R_API void r_sign_flirt_scan(const RAnal *anal, const char *flirt_file) {
r_buf_free (flirt_buf);
if (node) {
if (!node_match_functions (anal, node)) {
eprintf ("Error while scanning the file\n");
eprintf ("Error while scanning the file %s\n", flirt_file);
}
node_free (node);
return;
} else {
eprintf ("We encountered an error while parsing the file. Sorry.\n");
eprintf ("We encountered an error while parsing the file %s. Sorry.\n", flirt_file);
return;
}
}

View File

@ -2650,6 +2650,7 @@ R_API int r_core_config_init(RCore *core) {
SETPREF ("diff.levenstein", "false", "Use faster (and buggy) levenstein algorithm for buffer distance diffing");
/* dir */
SETI ("dir.depth", 10, "Maximum depth when searching recursively for files");
SETCB ("dir.dbgsnap", ".", &cb_dbgsnap, "Path to session dump files");
{
char *path = r_str_newf (R_JOIN_2_PATHS ("%s", R2_SDB_MAGIC), r_config_get (core->config, "dir.prefix"));

View File

@ -45,6 +45,7 @@ static const char *help_msg_zf[] = {
"Usage:", "zf[dsz] filename ", "# Manage FLIRT signatures",
"zfd ", "filename", "open FLIRT file and dump",
"zfs ", "filename", "open FLIRT file and scan",
"zfs ", "/path/**.sig", "recursively search for FLIRT files and scan them (see dir.depth)",
"zfz ", "filename", "open FLIRT file and get sig commands (zfz flirt_file > zignatures.sig)",
NULL
};
@ -507,7 +508,14 @@ static int cmdFlirt(void *data, const char *input) {
eprintf ("usage: zfs filename\n");
return false;
}
r_sign_flirt_scan (core->anal, input + 2);
int depth = r_config_get_i (core->config, "dir.depth");
char *file;
RListIter *iter;
RList *files = r_file_globsearch (input + 2, depth);
r_list_foreach (files, iter, file) {
r_sign_flirt_scan (core->anal, file);
}
r_list_free (files);
break;
case 'z':
// TODO

View File

@ -43,6 +43,7 @@ R_API int r_file_mkstemp(const char *prefix, char **oname);
R_API char *r_file_tmpdir(void);
R_API char *r_file_readlink(const char *path);
R_API bool r_file_copy (const char *src, const char *dst);
R_API RList* r_file_globsearch (char *globbed_path, int maxdepth);
#ifdef __cplusplus
}

View File

@ -1037,3 +1037,69 @@ R_API bool r_file_copy (const char *src, const char *dst) {
return rc == 0;
#endif
}
static void recursive_search_glob (const char *path, const char *glob, RList* list, int depth) {
if (depth < 1) {
return;
}
char* file;
RListIter *iter;
RList *dir = r_sys_dir (path);
r_list_foreach (dir, iter, file) {
if (!strcmp (file, ".") || !strcmp (file, "..")) {
continue;
}
char *filename = malloc (strlen (path) + strlen (file) + 2);
strcpy (filename, path);
strcat (filename, file);
if (r_file_is_directory (filename)) {
strcat (filename, R_SYS_DIR);
recursive_search_glob (filename, glob, list, depth - 1);
free (filename);
} else if (r_str_glob (file, glob)) {
r_list_append (list, filename);
} else {
free (filename);
}
}
r_list_free (dir);
}
R_API RList* r_file_globsearch (char *globbed_path, int maxdepth) {
RList *files = r_list_newf (free);
char *glob = strchr (globbed_path, '*');
if (!glob) {
r_list_append (files, strdup (globbed_path));
} else {
*glob = '\0';
char *last_slash = (char *)r_str_last (globbed_path, R_SYS_DIR);
*glob = '*';
char *path, *glob_ptr;
if (last_slash) {
glob_ptr = last_slash + 1;
if (globbed_path[0] == '~') {
char *rpath = strndup (globbed_path + 2, last_slash - globbed_path - 1);
path = r_str_home (rpath);
free (rpath);
} else {
path = strndup (globbed_path, last_slash - globbed_path + 1);
}
} else {
glob_ptr = globbed_path;
path = r_str_newf (".%s", R_SYS_DIR);
}
if (!path) {
r_list_free (files);
return NULL;
}
if (*(glob + 1) == '*') { // "**"
recursive_search_glob (path, glob_ptr, files, maxdepth);
} else { // "*"
recursive_search_glob (path, glob_ptr, files, 1);
}
free (path);
}
return files;
}