Rename the binstr apis and fix units

This commit is contained in:
pancake 2023-12-11 07:56:08 +01:00 committed by pancake
parent d0eb5e3dfc
commit fc8b7c9a6a
6 changed files with 84 additions and 69 deletions

View File

@ -58,6 +58,9 @@ R_API bool r_mem_is_printable(const ut8 *a, int la);
R_API bool r_mem_is_zero(const ut8 *b, int l);
R_API void *r_mem_mmap_resize(RMmap *m, ut64 newsize);
R_API int r_mem_fromstring_bin(const char* str, ut8 *buf, size_t len);
R_API char *r_mem_tostring_bin(const ut8* str, int len);
// 27bit middle endian parser
typedef ut32 ut27;
static inline ut27 r_read_me27(const ut8 *buf, int boff) {

View File

@ -266,8 +266,6 @@ 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 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) {

View File

@ -313,9 +313,12 @@ dotherax:
}
return true;
} else if (flags & (1 << 3)) { // -b
char *newstr = r_str_binstr2str ((const ut8*)str, strlen(str));
printf ("%s\n", newstr);
free (newstr);
ut8 out[256] = {0};
if (r_mem_fromstring_bin (str, out, sizeof (out) - 1)) {
printf ("%s\n", out); // TODO accept non null terminated strings
} else {
eprintf ("Invalid binary input string\n");
}
return true;
} else if (flags & (1 << 4)) { // -x
int h = r_str_hash (str);
@ -386,9 +389,9 @@ dotherax:
}
return true;
} else if (flags & (1 << 17)) { // -B (bin -> str)
char *binstr = r_str_str2binstr ((const ut8*)str, strlen (str));
printf ("%s\n", binstr);
free (binstr);
char *newstr = r_mem_tostring_bin ((const ut8*)str, strlen (str));
printf ("%s\n", newstr);
free (newstr);
return true;
} else if (flags & (1 << 16)) { // -w
ut64 n = r_num_calc (num, str, &errstr);

View File

@ -396,3 +396,52 @@ R_API void *r_mem_mmap_resize(RMmap *m, ut64 newsize) {
r_file_mmap_arch (m, m->filename, m->fd);
return m->buf;
}
R_API int r_mem_fromstring_bin(const char* str, ut8 *buf, size_t len) {
int i, j, k, ret;
ut8 *b = buf;
ut8 *e = buf + len;
for (i = 0; i < len && b < e; i += 8) {
ret = 0;
str = r_str_trim_head_ro (str);
if (i + 7 < len) {
for (k = 0, j = i + 7; j >= i; j--, k++) {
if (str[j] == ' ') {
continue;
}
if (str[j] == '1') {
ret |= (1 << k);
} else if (str[j] != '0') {
b[0] = 0; // null terminate if possible
return -1;
}
}
}
*b++ = ret;
}
b[1] = 0; // null terminate if possible
return b - buf;
}
R_API char *r_mem_tostring_bin(const ut8* str, int len) {
if (len < 0) {
len = strlen ((const char *)str);
}
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);
}

View File

@ -201,52 +201,6 @@ R_API ut64 r_str_bits_from_string(const char *buf, const char *bitz) {
return out;
}
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++;
}
if (i + 7 < len) {
for (k = 0, j = i + 7; j >= i; j--, k++) {
if (str[j] == ' ') {
continue;
}
if (str[j] == '1') {
ret |= (1 << k);
} else if (str[j] != '0') {
return r_strbuf_drain (buf);
}
}
}
r_strbuf_appendf (buf, "%c", ret);
}
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,
// etc.
R_API int r_str_rwx(const char *str) {

View File

@ -632,23 +632,31 @@ bool test_r_str_tok_r (void) {
mu_end;
}
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);
bool test_r_mem_fromstring_bin(void) {
int rc;
ut8 res[256];
const char *one = (const char*)res;
rc = r_mem_fromstring_bin ("0100100001100101011011000110110001101111001000000111010001101000011001010111001001100101", res, sizeof (res));
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");
rc = r_mem_fromstring_bin ("011100110111010101110000011001010111001000100000011000110110111101101111011011000010000001101101011001010111001101110011011000010110011101100101", res, sizeof (res));
mu_assert_streq (one, "super cool message", "two");
rc = r_mem_fromstring_bin (" 00100000001000000010000000100000001000000111001101110100011000010111001001110100011100110010000001110111011010010111010001101000001000000111001101110000011000010110001101100101", res, sizeof (res));
mu_assert_streq (one, " starts with space", "three");
*res = 0;
rc = r_mem_fromstring_bin ("01100110011010010110111001100100011100110010000001101110011011110111010000100000011000100110100101101110abcdef", res, sizeof (res));
mu_assert_streq (one, "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);
bool test_r_mem_tostring_bin(void) {
char *one = r_mem_tostring_bin ((const ut8*)"Hello there", -1);
char *two = r_mem_tostring_bin ((const ut8*)"super cool message", -1);
char *three = r_mem_tostring_bin ((const ut8*)" starts with space", -1);
char *four = r_mem_tostring_bin ((const ut8*)"super secret!?", -1);
mu_assert_streq (one, "0100100001100101011011000110110001101111001000000111010001101000011001010111001001100101", "one");
mu_assert_streq (two, "011100110111010101110000011001010111001000100000011000110110111101101111011011000010000001101101011001010111001101110011011000010110011101100101", "two");
mu_assert_streq (three, "00100000001000000010000000100000001000000111001101110100011000010111001001110100011100110010000001110111011010010111010001101000001000000111001101110000011000010110001101100101", "three");
@ -694,8 +702,8 @@ bool all_tests(void) {
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);
mu_run_test (test_r_mem_fromstring_bin);
mu_run_test (test_r_mem_tostring_bin);
return tests_passed != tests_run;
}