Optimize and improve r_name_filter calls ##core

This commit is contained in:
pancake 2021-07-26 00:03:00 +02:00
parent 469146d49a
commit ebd98157c9
6 changed files with 51 additions and 24 deletions

View File

@ -6,7 +6,7 @@ extern "C" {
#endif
R_API bool r_name_validate_print(const char ch);
R_API bool r_name_validate_char(const char ch);
// R_API bool r_name_validate_char(const char ch);
R_API bool r_name_validate_first(const char ch);
R_API bool r_name_check(const char *s);
R_API const char *r_name_filter_ro(const char *a);
@ -15,6 +15,13 @@ R_API bool r_name_filter_print(char *s);
R_API bool r_name_filter(char *name, int maxlen);
R_API char *r_name_filter2(const char *name);
static inline bool r_name_validate_char(const char ch) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || IS_DIGIT (ch)) {
return true;
}
return (ch == '.' || ch == ':' || ch == '_');
}
#ifdef __cplusplus
}
#endif

View File

@ -1,7 +1,9 @@
/* radare - LGPL - Copyright 2009-2020 - pancake */
/* radare - LGPL - Copyright 2009-2021 - pancake */
#include <r_util.h>
#define FAST_FILTER 1
/* Validate if char is printable , why not use ISPRINTABLE() ?? */
R_API bool r_name_validate_print(const char ch) {
// TODO: support utf8
@ -64,6 +66,7 @@ R_API bool r_name_validate_dash(const char ch) {
return false;
}
#if 0
R_API bool r_name_validate_char(const char ch) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || IS_DIGIT (ch)) {
return true;
@ -76,6 +79,7 @@ R_API bool r_name_validate_char(const char ch) {
}
return false;
}
#endif
R_API bool r_name_validate_first(const char ch) {
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
@ -131,24 +135,34 @@ R_API bool r_name_filter_print(char *s) {
R_API bool r_name_filter(char *s, int maxlen) {
// if maxlen == -1 : R_FLAG_NAME_SIZE
// maxlen is ignored, the function signature must change
if (maxlen > 0) {
int slen = strlen (s);
if (slen > maxlen) {
s[maxlen] = 0;
// char *os = s;
size_t count = 0;
r_str_trim_head (s);
if (!r_name_validate_first (*s)) {
*s = '_';
}
#if 0
while (*s) {
if (maxlen > 0 && count > maxlen) {
*s = 0;
break;
}
if (r_name_validate_char (*s)) {
break;
}
if (r_name_validate_dash (*s)) {
break;
}
r_str_cpy (s, s + 1);
count++;
}
}
char *os = s;
while (*s) {
if (r_name_validate_first (*s)) {
break;
}
if (r_name_validate_dash (*s)) {
*s = '_';
break;
}
r_str_cpy (s, s + 1);
}
#endif
for (s++; *s; s++) {
if (maxlen > 0 && count > maxlen) {
*s = 0;
break;
}
if (*s == '\\') {
if (is_special_char (s[1])) {
*s = '_';
@ -166,8 +180,14 @@ R_API bool r_name_filter(char *s, int maxlen) {
}
}
}
#if FAST_FILTER
// that flag should be trimmed and checked already
// we dont want to iterate over it again
return true;
#else
r_str_trim (os);
return r_name_check (os);
#endif
}
R_API char *r_name_filter2(const char *name) {

View File

@ -103,7 +103,7 @@ R_API const char *r_str_trim_head_wp(const char *str) {
* the string is changed in place */
R_API void r_str_trim_head(char *str) {
char *p = (char *)r_str_trim_head_ro (str);
if (p) {
if (p && p != str) {
memmove (str, p, strlen (p) + 1);
}
}

View File

@ -4414,10 +4414,10 @@ NAME=izzz*
FILE=bins/mach0/fatmach0-3true
CMDS=izzz*~appleca
EXPECT=<<EOF
f str._http:__www.apple.com_appleca_ro 41 @ 0x00002562
f str._http:__www.apple.com_appleca_root.crl0_r 41 @ 0x00002562
f str.https:__www.apple.com_appleca_0 32 @ 0x00002963
f str.http:__www.apple.com_appleca_0 31 @ 0x00002e47
f str._http:__www.apple.com_appleca_co 48 @ 0x00002f3c
f str._http:__www.apple.com_appleca_codesigning.crl0_r 48 @ 0x00002f3c
EOF
RUN

View File

@ -1039,7 +1039,7 @@ EXPECT=<<EOF
guess
utf8
utf16le
0x004016c0 mov edi, 0x40230c ; u"\u2520\u2542\u2500\u2500\u2542\u2528 is a fence with embedded zeros\n"
0x004016c0 mov edi, 0x40230c ; str.__B_ ; u"\u2520\u2542\u2500\u2500\u2542\u2528 is a fence with embedded zeros\n"
utf32le
0x004016ed mov edi, 0x40258c ; U"utf32le> \\u00a2\\u20ac\\U00010348 in cyan:\x1b[36m \xa2\u20ac\U00010348 \x1b[0m\n"

View File

@ -401,9 +401,9 @@ Cm 8 wwww @ 0x00000380
----
[{"offset":0,"type":"Cs","name":"aGVsbG8gd29ybGQ=","enc":"latin1","ascii":true}]
----
[{"offset":256,"type":"Cd","name":"","size":3}]
[{"offset":256,"type":"Cd","name":"_","size":3}]
----
[{"offset":512,"type":"Ch","name":""}]
[{"offset":512,"type":"Ch","name":"_"}]
----
[{"offset":768,"type":"Cf","name":"x"}]
----
@ -411,7 +411,7 @@ Cm 8 wwww @ 0x00000380
----
[{"offset":896,"type":"Cm","name":"wwww"}]
----
[{"offset":0,"type":"Cs","name":"aGVsbG8gd29ybGQ=","enc":"latin1","ascii":true},{"offset":0,"type":"CCu","name":"Hello!"},{"offset":256,"type":"Cd","name":"","size":3},{"offset":512,"type":"Ch","name":""},{"offset":768,"type":"Cf","name":"x"},{"offset":896,"type":"Cm","name":"wwww"}]
[{"offset":0,"type":"Cs","name":"aGVsbG8gd29ybGQ=","enc":"latin1","ascii":true},{"offset":0,"type":"CCu","name":"Hello!"},{"offset":256,"type":"Cd","name":"_","size":3},{"offset":512,"type":"Ch","name":"_"},{"offset":768,"type":"Cf","name":"x"},{"offset":896,"type":"Cm","name":"wwww"}]
EOF
RUN