Skip to content

Commit

Permalink
search: Use blue dot for nonprintable chars
Browse files Browse the repository at this point in the history
  • Loading branch information
kazarmy committed Feb 7, 2025
1 parent f2ddbd7 commit d6c0ccf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 33 deletions.
42 changes: 34 additions & 8 deletions librz/core/cmd/cmd_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ RZ_API int rz_core_search_preludes(RzCore *core, bool log) {
}

/* TODO: maybe move into util/str */
static char *getstring(char *b, int l) {
static char *getstring(char *b, int l, bool use_color) {
char *r, *res = malloc(l + 1);
int i;
if (!res) {
Expand All @@ -415,7 +415,7 @@ static char *getstring(char *b, int l) {
*r++ = *b;
} else {
// This is very ASCII-centric
*r++ = '.';
*r++ = use_color ? '\xff' : '.';
}
}
*r = 0;
Expand Down Expand Up @@ -449,19 +449,45 @@ static int _cb_hit(RzSearchKeyword *kw, void *user, ut64 addr) {
char *buf = calloc(1, len + 32 + ctx * 2);
type = "string";
rz_io_read_at(core->io, addr - prectx, (ut8 *)buf, len + (ctx * 2));
pre = getstring(buf, prectx);
pos = getstring(buf + prectx + len, ctx);
pre = getstring(buf, prectx, use_color);
pos = getstring(buf + prectx + len, ctx, use_color);
if (!pos) {
pos = rz_str_dup("");
}
if (param->outmode == RZ_MODE_JSON) {
wrd = getstring(buf + prectx, len);
wrd = getstring(buf + prectx, len, false);
s = rz_str_newf("%s%s%s", pre, wrd, pos);
} else {
wrd = rz_str_utf16_encode(buf + prectx, len);
s = rz_str_newf(use_color ? "\"%s" Color_YELLOW "%s" Color_RESET "%s\""
: "\"%s%s%s\"",
pre, wrd, pos);
if (use_color) {
char *ptr;
RzBuffer *pre_color = rz_buf_new_with_bytes(NULL, 0);
for (ptr = pre; *ptr; ptr++) {
if (*ptr == '\xff') {
rz_buf_append_string(pre_color, Color_BLUE "." Color_RESET);
} else {
rz_buf_append_bytes(pre_color, (const ut8 *)ptr, 1);
}
}
RzBuffer *pos_color = rz_buf_new_with_bytes(NULL, 0);
for (ptr = pos; *ptr; ptr++) {
if (*ptr == '\xff') {
rz_buf_append_string(pos_color, Color_BLUE "." Color_RESET);
} else {
rz_buf_append_bytes(pos_color, (const ut8 *)ptr, 1);
}
}
char *pre_color_str = rz_buf_to_string(pre_color);
char *pos_color_str = rz_buf_to_string(pos_color);
s = rz_str_newf("\"%s" Color_YELLOW "%s" Color_RESET "%s\"",
pre_color_str, wrd, pos_color_str);
free(pre_color_str);
free(pos_color_str);
rz_buf_free(pre_color);
rz_buf_free(pos_color);
} else {
s = rz_str_newf("\"%s%s%s\"", pre, wrd, pos);
}
}
free(buf);
free(pre);
Expand Down
50 changes: 25 additions & 25 deletions test/db/cmd/regexp
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,16 @@ echo -- 5 --
EOF
EXPECT=<<EOF
-- 1 --
0x00000001 hit0_0 "abcd............bccc"
0x00000010 hit0_1 "abcd............bcccde..............."
0x00000001 hit0_0 "abcd............bccc"
0x00000010 hit0_1 "abcd............bcccde..............."
-- 2 --
-- 3 --
0x00000001 hit2_0 "abcd............bccc"
0x00000010 hit2_1 "abcd............bcccde..............."
0x00000001 hit2_0 "abcd............bccc"
0x00000010 hit2_1 "abcd............bcccde..............."
-- 4 --
0x00000001 hit3_0 "abcd............bccc"
0x00000001 hit3_0 "abcd............bccc"
-- 5 --
0x00000010 hit4_0 "abcd............bcccde..............."
0x00000010 hit4_0 "abcd............bcccde..............."
EOF
EXPECT_ERR=<<EOF
Searching in [0x0,0x200)
Expand Down Expand Up @@ -101,10 +101,10 @@ echo ----
/e /b.*d$/
EOF
EXPECT=<<EOF
0x000000fe hit0_0 "................bcccde..............."
0x000001fe hit0_1 "................bd................"
0x000000fe hit0_0 "................bcccde..............."
0x000001fe hit0_1 "................bd................"
----
0x000001fe hit1_0 "................bd................"
0x000001fe hit1_0 "................bd................"
EOF
EXPECT_ERR=<<EOF
Searching in [0x0,0x200)
Expand All @@ -123,23 +123,23 @@ echo ----
/e /lib/
EOF
EXPECT=<<EOF
0x00400239 hit0_0 ".............../lib64/ld-linux-x86-"
0x00400f19 hit0_1 ".a..............libselinux.so.1._IT"
0x00400fae hit0_2 "etfilecon._fini.libacl.so.1.acl_get"
0x00400feb hit0_3 "l_extended_file.libc.so.6.fflush.st"
0x004013c3 hit0_4 "cmp.tcgetpgrp.__libc_start_main.dir"
0x0041769a hit0_5 "system call.../.libs/.lt-.........."
0x004186e0 hit0_6 "emory exhausted.lib/xstrtol.c...0 <"
0x00418e58 hit0_7 "xstrtoumax./usr/lib.ASCII.CHARSETAL"
0x00400239 hit0_0 ".............../lib64/ld-linux-x86-"
0x00400f19 hit0_1 ".a..............libselinux.so.1._IT"
0x00400fae hit0_2 "etfilecon._fini.libacl.so.1.acl_get"
0x00400feb hit0_3 "l_extended_file.libc.so.6.fflush.st"
0x004013c3 hit0_4 "cmp.tcgetpgrp.__libc_start_main.dir"
0x0041769a hit0_5 "system call.../.libs/.lt-.........."
0x004186e0 hit0_6 "emory exhausted.lib/xstrtol.c...0 <"
0x00418e58 hit0_7 "xstrtoumax./usr/lib.ASCII.CHARSETAL"
----
0x00400239 hit1_0 ".............../lib64/ld-linux-x86-"
0x00400f19 hit1_1 ".a..............libselinux.so.1._IT"
0x00400fae hit1_2 "etfilecon._fini.libacl.so.1.acl_get"
0x00400feb hit1_3 "l_extended_file.libc.so.6.fflush.st"
0x004013c3 hit1_4 "cmp.tcgetpgrp.__libc_start_main.dir"
0x0041769a hit1_5 "system call.../.libs/.lt-.........."
0x004186e0 hit1_6 "emory exhausted.lib/xstrtol.c...0 <"
0x00418e58 hit1_7 "xstrtoumax./usr/lib.ASCII.CHARSETAL"
0x00400239 hit1_0 ".............../lib64/ld-linux-x86-"
0x00400f19 hit1_1 ".a..............libselinux.so.1._IT"
0x00400fae hit1_2 "etfilecon._fini.libacl.so.1.acl_get"
0x00400feb hit1_3 "l_extended_file.libc.so.6.fflush.st"
0x004013c3 hit1_4 "cmp.tcgetpgrp.__libc_start_main.dir"
0x0041769a hit1_5 "system call.../.libs/.lt-.........."
0x004186e0 hit1_6 "emory exhausted.lib/xstrtol.c...0 <"
0x00418e58 hit1_7 "xstrtoumax./usr/lib.ASCII.CHARSETAL"
EOF
EXPECT_ERR=<<EOF
Searching 3 bytes in [0x61d368,0x61d720)
Expand Down

0 comments on commit d6c0ccf

Please sign in to comment.