mirror of
https://github.com/radareorg/radare2.git
synced 2025-03-03 03:35:37 +00:00
Add RPrint.binFromStr and RStr.binstr2bin and RStr.binstr2str ##rax2
* Change -B and -b option to use new r_str_str2binstr and functions. * add test_r_str_binstr2str and test_r_str_str2binstr test * Change r_str_binstr2str and r_str_str2binstr to take parameters (const ut8* str, size_t len) * Change r_str_binstr2str and r_str_str2binstr to new API and freed them when done. * Change test_r_str_binstr2str and test_r_str_str2binstr with new API
This commit is contained in:
parent
d2d085a0f0
commit
a26d7fbdcc
@ -182,6 +182,7 @@ R_API void r_print_set_is_interrupted_cb(RPrintIsInterruptedCallback cb);
|
||||
/* ... */
|
||||
R_API char *r_print_hexpair(RPrint *p, const char *str, int idx);
|
||||
R_API void r_print_hex_from_bin(RPrint *p, char *bin_str);
|
||||
R_API void r_print_bin_from_str(RPrint *p, char *str);
|
||||
R_API RPrint *r_print_new(void);
|
||||
R_API void r_print_free(RPrint *p);
|
||||
R_API bool r_print_mute(RPrint *p, int x);
|
||||
|
@ -266,7 +266,8 @@ R_API void r_str_trim_path(char *s);
|
||||
R_API ut8 r_str_contains_macro(const char *input_value);
|
||||
R_API void r_str_truncate_cmd(char *string);
|
||||
R_API bool r_str_glob(const char *str, const char *glob);
|
||||
R_API int r_str_binstr2bin(const char *str, ut8 *out, int outlen);
|
||||
R_API char *r_str_binstr2str(const ut8* str, size_t len);
|
||||
R_API char *r_str_str2binstr(const ut8* str, size_t len);
|
||||
R_API char *r_str_between(const char *str, const char *prefix, const char *suffix);
|
||||
#undef r_str_startswith
|
||||
static inline size_t r_str_ncpy(char *dst, const char *src, size_t n) {
|
||||
|
@ -313,12 +313,9 @@ dotherax:
|
||||
}
|
||||
return true;
|
||||
} else if (flags & (1 << 3)) { // -b
|
||||
int i;
|
||||
ut8 buf[4096];
|
||||
const int n = r_str_binstr2bin (str, buf, sizeof (buf));
|
||||
for (i = 0; i < n; i++) {
|
||||
printf ("%c", buf[i]);
|
||||
}
|
||||
char *newstr = r_str_binstr2str (str, strlen(str));
|
||||
printf ("%s\n", newstr);
|
||||
free (newstr);
|
||||
return true;
|
||||
} else if (flags & (1 << 4)) { // -x
|
||||
int h = r_str_hash (str);
|
||||
@ -389,21 +386,9 @@ dotherax:
|
||||
}
|
||||
return true;
|
||||
} else if (flags & (1 << 17)) { // -B (bin -> str)
|
||||
int i = 0;
|
||||
// TODO: move to r_util
|
||||
for (i = 0; i < strlen (str); i++) {
|
||||
ut8 ch = str[i];
|
||||
printf ("%d%d%d%d"
|
||||
"%d%d%d%d",
|
||||
ch & 128? 1: 0,
|
||||
ch & 64? 1: 0,
|
||||
ch & 32? 1: 0,
|
||||
ch & 16? 1: 0,
|
||||
ch & 8? 1: 0,
|
||||
ch & 4? 1: 0,
|
||||
ch & 2? 1: 0,
|
||||
ch & 1? 1: 0);
|
||||
}
|
||||
char *binstr = r_str_str2binstr (str, strlen (str));
|
||||
printf ("%s", binstr);
|
||||
free (binstr);
|
||||
return true;
|
||||
} else if (flags & (1 << 16)) { // -w
|
||||
ut64 n = r_num_calc (num, str, &errstr);
|
||||
|
@ -2572,6 +2572,37 @@ R_API void r_print_hex_from_bin(RPrint *p, char *bin_str) {
|
||||
free (buf);
|
||||
}
|
||||
|
||||
R_API void r_print_bin_from_str(RPrint *p, char *str) {
|
||||
int i = 0;
|
||||
int len = strlen (str);
|
||||
for (i = 0; i < len; i++) {
|
||||
ut8 ch = str[i];
|
||||
if (p) {
|
||||
p->cb_eprintf ("%d%d%d%d"
|
||||
"%d%d%d%d",
|
||||
ch & 128? 1: 0,
|
||||
ch & 64? 1: 0,
|
||||
ch & 32? 1: 0,
|
||||
ch & 16? 1: 0,
|
||||
ch & 8? 1: 0,
|
||||
ch & 4? 1: 0,
|
||||
ch & 2? 1: 0,
|
||||
ch & 1? 1: 0);
|
||||
} else {
|
||||
printf ("%d%d%d%d"
|
||||
"%d%d%d%d",
|
||||
ch & 128? 1: 0,
|
||||
ch & 64? 1: 0,
|
||||
ch & 32? 1: 0,
|
||||
ch & 16? 1: 0,
|
||||
ch & 8? 1: 0,
|
||||
ch & 4? 1: 0,
|
||||
ch & 2? 1: 0,
|
||||
ch & 1? 1: 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
R_API RBraile r_print_braile(int u) {
|
||||
#define CH0(x) ((x) >> 8)
|
||||
#define CH1(x) ((x) & 0xff)
|
||||
|
@ -201,10 +201,11 @@ R_API ut64 r_str_bits_from_string(const char *buf, const char *bitz) {
|
||||
return out;
|
||||
}
|
||||
|
||||
R_API int r_str_binstr2bin(const char *str, ut8 *out, int outlen) {
|
||||
int n, i, j, k, ret, len;
|
||||
len = strlen (str);
|
||||
for (n = i = 0; i < len; i += 8) {
|
||||
R_API char *r_str_binstr2str(const ut8* str, size_t len) {
|
||||
RStrBuf *buf = r_strbuf_new (NULL);
|
||||
int i, j, k, ret;
|
||||
|
||||
for (i = 0; i < len; i += 8) {
|
||||
ret = 0;
|
||||
while (str[i] == ' ') {
|
||||
str++;
|
||||
@ -217,16 +218,33 @@ R_API int r_str_binstr2bin(const char *str, ut8 *out, int outlen) {
|
||||
if (str[j] == '1') {
|
||||
ret |= (1 << k);
|
||||
} else if (str[j] != '0') {
|
||||
return n;
|
||||
return r_strbuf_drain (buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
out[n++] = ret;
|
||||
if (n == outlen) {
|
||||
return n;
|
||||
}
|
||||
r_strbuf_appendf (buf, "%c", ret);
|
||||
}
|
||||
return n;
|
||||
|
||||
return r_strbuf_drain (buf);
|
||||
}
|
||||
|
||||
R_API char *r_str_str2binstr(const ut8* str, size_t len) {
|
||||
RStrBuf *buf = r_strbuf_new (NULL);
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
ut8 ch = str[i];
|
||||
r_strbuf_appendf (buf, "%c", ch & 128? '1': '0');
|
||||
r_strbuf_appendf (buf, "%c", ch & 64? '1': '0');
|
||||
r_strbuf_appendf (buf, "%c", ch & 32? '1': '0');
|
||||
r_strbuf_appendf (buf, "%c", ch & 16? '1': '0');
|
||||
r_strbuf_appendf (buf, "%c", ch & 8? '1': '0');
|
||||
r_strbuf_appendf (buf, "%c", ch & 4? '1': '0');
|
||||
r_strbuf_appendf (buf, "%c", ch & 2? '1': '0');
|
||||
r_strbuf_appendf (buf, "%c", ch & 1? '1': '0');
|
||||
}
|
||||
|
||||
return r_strbuf_drain (buf);
|
||||
}
|
||||
|
||||
// Returns the permissions as in integer given an input in the form of rwx, rx,
|
||||
@ -234,9 +252,9 @@ R_API int r_str_binstr2bin(const char *str, ut8 *out, int outlen) {
|
||||
R_API int r_str_rwx(const char *str) {
|
||||
int ret = atoi (str);
|
||||
if (!ret) {
|
||||
ret |= strchr (str, 'm') ? 16 : 0;
|
||||
ret |= strchr (str, 'r') ? 4 : 0;
|
||||
ret |= strchr (str, 'w') ? 2 : 0;
|
||||
ret |= strchr (str, 'm')? 16: 0;
|
||||
ret |= strchr (str, 'r')? 4: 0;
|
||||
ret |= strchr (str, 'w')? 2: 0;
|
||||
ret |= strchr (str, 'x') ? 1 : 0;
|
||||
} else if (ret < 0 || ret >= R_ARRAY_SIZE (rwxstr)) {
|
||||
ret = 0;
|
||||
|
@ -107,7 +107,9 @@ RUN
|
||||
NAME=rax2 -b 01101000011001010110110001101100011011110111011101101111011100100110110001100100
|
||||
FILE=-
|
||||
CMDS=!rax2 -b 01101000011001010110110001101100011011110111011101101111011100100110110001100100
|
||||
EXPECT=helloworld
|
||||
EXPECT=<<EOF
|
||||
helloworld
|
||||
EOF
|
||||
RUN
|
||||
|
||||
NAME=rax2 -B hello world
|
||||
@ -269,7 +271,10 @@ RUN
|
||||
NAME=rax2 -b 01000101 01110110
|
||||
FILE=-
|
||||
CMDS=!rax2 -b 01000101 01110110
|
||||
EXPECT=Ev
|
||||
EXPECT=<<EOF
|
||||
E
|
||||
v
|
||||
EOF
|
||||
RUN
|
||||
|
||||
NAME=rax2 -k 33+3
|
||||
|
@ -88,8 +88,6 @@ bool test_r_str_rwx(void) {
|
||||
mu_end;
|
||||
}
|
||||
|
||||
//TODO test r_str_binstr2bin
|
||||
|
||||
bool test_r_str_rwx_i(void) {
|
||||
const char* rwx = r_str_rwx_i (7);
|
||||
const char* rw = r_str_rwx_i (6);
|
||||
@ -634,7 +632,35 @@ bool test_r_str_tok_r (void) {
|
||||
mu_end;
|
||||
}
|
||||
|
||||
bool all_tests () {
|
||||
bool test_r_str_binstr2str(void) {
|
||||
const char *one = r_str_binstr2str ("0100100001100101011011000110110001101111001000000111010001101000011001010111001001100101", 88);
|
||||
const char *two = r_str_binstr2str ("011100110111010101110000011001010111001000100000011000110110111101101111011011000010000001101101011001010111001101110011011000010110011101100101", 144);
|
||||
const char *three = r_str_binstr2str (" 00100000001000000010000000100000001000000111001101110100011000010111001001110100011100110010000001110111011010010111010001101000001000000111001101110000011000010110001101100101", 187);
|
||||
const char *four = r_str_binstr2str ("01100110011010010110111001100100011100110010000001101110011011110111010000100000011000100110100101101110abcdef", 110);
|
||||
mu_assert_streq (one, "Hello there", "one");
|
||||
mu_assert_streq (two, "super cool message", "two");
|
||||
mu_assert_streq (three, " starts with space", "three");
|
||||
mu_assert_streq (four, "finds not bin", "four");
|
||||
mu_end;
|
||||
}
|
||||
|
||||
bool test_r_str_str2binstr(void) {
|
||||
const char *one = r_str_str2binstr ("Hello there", 11);
|
||||
char *two = r_str_str2binstr ("super cool message", 18);
|
||||
char *three = r_str_str2binstr (" starts with space", 22);
|
||||
char *four = r_str_str2binstr ("super secret!?", 14);
|
||||
mu_assert_streq (one, "0100100001100101011011000110110001101111001000000111010001101000011001010111001001100101", "one");
|
||||
mu_assert_streq (two, "011100110111010101110000011001010111001000100000011000110110111101101111011011000010000001101101011001010111001101110011011000010110011101100101", "two");
|
||||
mu_assert_streq (three, "00100000001000000010000000100000001000000111001101110100011000010111001001110100011100110010000001110111011010010111010001101000001000000111001101110000011000010110001101100101", "three");
|
||||
mu_assert_streq (four, "0111001101110101011100000110010101110010001000000111001101100101011000110111001001100101011101000010000100111111", "four");
|
||||
free (one);
|
||||
free (two);
|
||||
free (three);
|
||||
free (four);
|
||||
mu_end;
|
||||
}
|
||||
|
||||
bool all_tests(void) {
|
||||
mu_run_test (test_r_str_wrap);
|
||||
mu_run_test (test_r_str_newf);
|
||||
mu_run_test (test_r_str_replace_char_once);
|
||||
@ -668,6 +694,8 @@ bool all_tests () {
|
||||
mu_run_test (test_r_str_str_xy);
|
||||
mu_run_test (test_r_str_encoded_json);
|
||||
mu_run_test (test_r_str_tok_r);
|
||||
mu_run_test (test_r_str_binstr2str);
|
||||
mu_run_test (test_r_str_str2binstr);
|
||||
return tests_passed != tests_run;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user