Skip to content

Commit

Permalink
Add /V value range support in rafind2 ##search
Browse files Browse the repository at this point in the history
* Updated help message and usage info, ensured coding standards
  • Loading branch information
Saru2003 authored Oct 6, 2024
1 parent 1f5defe commit 09ee59c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
74 changes: 45 additions & 29 deletions libr/main/rafind2.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static int show_help(const char *argv0, int line) {
" -t [to] stop search at address 'to'\n"
" -q quiet: fewer output do not show headings or filenames.\n"
" -v print version and exit\n"
" -V [s:num] search for given value in given endian (-V 4:123)\n"
" -V [s:num | s:num1,num2] search for a value or range in the specified endian (-V 4:123 or -V 4:100,200)\n"
" -x [hex] search for hexpair string (909090) (can be used multiple times)\n"
" -X show hexdump of search results\n"
" -z search for zero-terminated strings\n"
Expand Down Expand Up @@ -498,44 +498,60 @@ R_API int r_main_rafind2(int argc, const char **argv) {
{
char *arg = strdup (opt.arg);
char *colon = strchr (arg, ':');
char *comma = NULL;
ut8 buf[8] = {0};
int size = (R_SYS_BITS & R_SYS_BITS_64)? 8: 4;
ut64 value = 0;
ut64 value, min_value = 0, max_value = 0;

if (colon) {
*colon++ = 0;
size = atoi (arg);
size = R_MIN (8, size);
size = R_MAX (1, size);
value = r_num_math (NULL, colon);
comma = strchr (colon, ',');

if (comma) {
*comma++ = 0;
min_value = r_num_math (NULL, colon);
max_value = r_num_math (NULL, comma);
} else {
min_value = r_num_math (NULL, colon);
max_value = min_value;
}
} else {
value = r_num_math (NULL, arg);
min_value = r_num_math (NULL, arg);
max_value = min_value;
}
switch (size) {
case 1:
buf[0] = value;
break;
case 2:
r_write_ble16 (buf, value, ro.bigendian);
break;
case 4:
r_write_ble32 (buf, value, ro.bigendian);
break;
case 8:
r_write_ble64 (buf, value, ro.bigendian);
break;
default:
R_LOG_ERROR ("Invalid value size. Must be 1, 2, 4 or 8");
rafind_options_fini (&ro);
return 1;
}
char *hexdata = r_hex_bin2strdup ((ut8*)buf, size);
if (hexdata) {
ro.align = size;
ro.mode = R_SEARCH_KEYWORD;
ro.hexstr = true;
ro.widestr = false;
r_list_append (ro.keywords, (void*)hexdata);
for (value = min_value; value <= max_value; value++) {
switch (size) {
case 1:
buf[0] = value;
break;
case 2:
r_write_ble16 (buf, value, ro.bigendian);
break;
case 4:
r_write_ble32 (buf, value, ro.bigendian);
break;
case 8:
r_write_ble64 (buf, value, ro.bigendian);
break;
default:
R_LOG_ERROR ("Invalid value size. Must be 1, 2, 4 or 8");
rafind_options_fini (&ro);
free (arg);
return 1;
}
char *hexdata = r_hex_bin2strdup ((ut8*)buf, size);
if (hexdata) {
ro.align = size;
ro.mode = R_SEARCH_KEYWORD;
ro.hexstr = true;
ro.widestr = false;
r_list_append (ro.keywords, (void*)hexdata);
}
}
free (arg);
}
break;
case 'v':
Expand Down
6 changes: 3 additions & 3 deletions man/rafind2.1
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
.Op Fl S Ar str
.Op Fl t Ar to
.Op Fl v
.Op Fl V Ar s:num
.Op Fl V Ar s:num | s:num1,num2
.Op Fl x Ar hex
.Op Fl X
.Op Fl z
Expand Down Expand Up @@ -74,8 +74,8 @@ Search for wide strings (Unicode) in the file(s).
Specify the ending address for the search. (See -f)
.It Fl v
Display the version of rafind2 and exit.
.It Fl V Ar s:num
Search for the given value using little-endian notation (e.g., -V 4:123).
.It Fl V Ar s:num | s:num1,num2
Search for the given value using little-endian notation. A single value can be specified (e.g., -V 4:123) or a range of values can be searched by providing two values separated by a comma (e.g., -V 4:100,200).
.It Fl x Ar hex
Search for the specified hex pattern(s) in the file(s).
.It Fl X
Expand Down

0 comments on commit 09ee59c

Please sign in to comment.