Make rz_base64_encode use the right types

This commit is contained in:
Florian Märkl 2021-02-03 14:49:03 +01:00 committed by Riccardo Schirone
parent febfdd6598
commit ed6d93d4d2
13 changed files with 56 additions and 58 deletions

View file

@ -288,14 +288,14 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
pj_kn(pj, "offset", start);
pj_ks(pj, "type", rz_meta_type_to_string(d->type));
if (d->type == 'H') {
if (d->type == RZ_META_TYPE_HIGHLIGHT) {
pj_k(pj, "color");
ut8 r = 0, g = 0, b = 0, A = 0;
const char *esc = strchr(d->str, '\x1b');
if (esc) {
rz_cons_rgb_parse(esc, &r, &g, &b, &A);
char *rgb_str = rz_cons_rgb_tostring(r, g, b);
base64_str = rz_base64_encode_dyn(rgb_str, -1);
base64_str = rz_base64_encode_dyn((const ut8 *)rgb_str, strlen(rgb_str));
if (d->type == 's' && base64_str) {
pj_s(pj, base64_str);
free(base64_str);
@ -308,7 +308,7 @@ RZ_API void rz_meta_print(RzAnalysis *a, RzAnalysisMetaItem *d, ut64 start, ut64
}
} else {
pj_k(pj, "name");
if (d->type == 's' && (base64_str = rz_base64_encode_dyn(d->str, -1))) {
if (d->type == 's' && (base64_str = rz_base64_encode_dyn((const ut8 *)d->str, strlen(d->str)))) {
pj_s(pj, base64_str);
} else {
pj_s(pj, str);

View file

@ -258,7 +258,7 @@ static void block_store(RZ_NONNULL Sdb *db, const char *key, RzAnalysisBlock *bl
pj_kn(j, "colorize", (ut64)block->colorize);
}
if (block->fingerprint) {
char *b64 = rz_base64_encode_dyn((const char *)block->fingerprint, block->size);
char *b64 = rz_base64_encode_dyn(block->fingerprint, block->size);
if (b64) {
pj_ks(j, "fingerprint", b64);
free(b64);
@ -885,7 +885,7 @@ static void function_store(RZ_NONNULL Sdb *db, const char *key, RzAnalysisFuncti
pj_kb(j, "noreturn", true);
}
if (function->fingerprint) {
char *b64 = rz_base64_encode_dyn((const char *)function->fingerprint, function->fingerprint_size);
char *b64 = rz_base64_encode_dyn(function->fingerprint, function->fingerprint_size);
if (b64) {
pj_ks(j, "fingerprint", b64);
free(b64);

View file

@ -1734,7 +1734,7 @@ static int core_analysis_graph_construct_nodes(RzCore *core, RzAnalysisFunction
if (is_star) {
char *title = get_title(bbi->addr);
char *body_b64 = rz_base64_encode_dyn(diffstr, -1);
char *body_b64 = rz_base64_encode_dyn((const ut8 *)diffstr, strlen(diffstr));
if (!title || !body_b64) {
free(body_b64);
free(title);
@ -1759,7 +1759,7 @@ static int core_analysis_graph_construct_nodes(RzCore *core, RzAnalysisFunction
} else {
if (is_star) {
char *title = get_title(bbi->addr);
char *body_b64 = rz_base64_encode_dyn(str, -1);
char *body_b64 = rz_base64_encode_dyn((const ut8 *)str, strlen(title));
int color = (bbi && bbi->diff) ? bbi->diff->type : 0;
if (!title || !body_b64) {
free(body_b64);
@ -1804,7 +1804,7 @@ static int core_analysis_graph_construct_nodes(RzCore *core, RzAnalysisFunction
nodes++;
if (is_star) {
char *title = get_title(bbi->addr);
char *body_b64 = rz_base64_encode_dyn(str, -1);
char *body_b64 = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
int color = (bbi && bbi->diff) ? bbi->diff->type : 0;
if (!title || !body_b64) {
free(body_b64);
@ -2078,7 +2078,7 @@ RZ_API int rz_core_print_bb_custom(RzCore *core, RzAnalysisFunction *fcn) {
}
char *title = get_title(bb->addr);
char *body = rz_core_cmd_strf(core, "pdb @ 0x%08" PFMT64x, bb->addr);
char *body_b64 = rz_base64_encode_dyn(body, -1);
char *body_b64 = rz_base64_encode_dyn((const ut8 *)body, strlen(body));
if (!title || !body || !body_b64) {
free(body_b64);
free(body);
@ -6047,7 +6047,7 @@ RZ_API void rz_core_analysis_esil_graph(RzCore *core, const char *expr) {
if (enode->type == RZ_ANALYSIS_ESIL_DFG_BLOCK_GENERATIVE) {
rz_strbuf_prepend(buf, "generative:");
}
char *b64_buf = rz_base64_encode_dyn(rz_strbuf_get(buf), buf->len);
char *b64_buf = rz_base64_encode_dyn((const ut8 *)rz_strbuf_get(buf), buf->len);
rz_cons_printf("agn %d base64:%s\n", enode->idx, b64_buf);
free(b64_buf);
free(esc_str);

View file

@ -259,7 +259,8 @@ RZ_API void rz_core_annotated_code_print(RzAnnotatedCode *code, RzVector *line_o
static bool foreach_offset_annotation(void *user, const ut64 offset, const void *val) {
RzAnnotatedCode *code = user;
const RzCodeAnnotation *annotation = val;
char *b64statement = rz_base64_encode_dyn(code->code + annotation->start, annotation->end - annotation->start);
char *b64statement = rz_base64_encode_dyn((const ut8 *)(code->code + annotation->start),
annotation->end - annotation->start);
rz_cons_printf("CCu base64:%s @ 0x%" PFMT64x "\n", b64statement, annotation->offset.offset);
free(b64statement);
return true;

View file

@ -537,7 +537,7 @@ RZ_IPI int rz_cmd_alias(void *data, const char *input) {
char **keys = rz_cmd_alias_keys(core->rcmd, &count);
for (i = 0; i < count; i++) {
char *v = rz_cmd_alias_get(core->rcmd, keys[i], 0);
char *q = rz_base64_encode_dyn(v, -1);
char *q = rz_base64_encode_dyn((const ut8 *)v, strlen(v));
if (buf[2] == '*') {
rz_cons_printf("%s=%s\n", keys[i], v);
} else {

View file

@ -7832,7 +7832,7 @@ static void agraph_print_node(RzANode *n, void *user) {
if (len > 0 && n->body[len - 1] == '\n') {
len--;
}
encbody = rz_base64_encode_dyn(n->body, len);
encbody = rz_base64_encode_dyn((const ut8 *)n->body, len);
cmd = rz_str_newf("agn \"%s\" base64:%s\n", n->title, encbody);
rz_cons_print(cmd);
free(cmd);
@ -8162,7 +8162,7 @@ static void print_graph_agg(RzGraph /*RzGraphNodeInfo*/ *graph) {
if (len > 0 && print_node->body[len - 1] == '\n') {
len--;
}
encbody = rz_base64_encode_dyn(print_node->body, len);
encbody = rz_base64_encode_dyn((const ut8 *)print_node->body, len);
rz_cons_printf("agn \"%s\" base64:%s\n", print_node->title, encbody);
free(encbody);
} else {
@ -10101,7 +10101,7 @@ static void cmd_analysis_aC(RzCore *core, const char *input) {
}
char *s = rz_strbuf_drain(sb);
if (is_aCer) {
char *u = rz_base64_encode_dyn(s, -1);
char *u = rz_base64_encode_dyn((const ut8 *)s, strlen(s));
if (u) {
rz_cons_printf("CCu base64:%s\n", u);
free(u);

View file

@ -532,7 +532,8 @@ RZ_IPI int rz_cmd_help(void *data, const char *input) {
if (input[3] == '-') {
rz_base64_decode((ut8 *)buf, input + 4, -1);
} else if (input[3] == ' ') {
rz_base64_encode(buf, (const ut8 *)input + 4, -1);
const char *s = input + 4;
rz_base64_encode(buf, (const ut8 *)s, strlen(s));
}
rz_cons_println(buf);
free(buf);

View file

@ -338,7 +338,7 @@ static bool print_flag_rad(RzFlagItem *flag, void *user) {
u->f->cb_printf("fs %s\n", u->fs ? u->fs->name : "*");
}
if (flag->comment && *flag->comment) {
comment_b64 = rz_base64_encode_dyn(flag->comment, -1);
comment_b64 = rz_base64_encode_dyn((const ut8 *)flag->comment, strlen(flag->comment));
// prefix the armored string with "base64:"
if (comment_b64) {
tmp = rz_str_newf("base64:%s", comment_b64);

View file

@ -5,10 +5,10 @@
extern "C" {
#endif
RZ_API int rz_base64_encode(char *bout, const ut8 *bin, int len);
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz);
RZ_API int rz_base64_decode(ut8 *bout, const char *bin, int len);
RZ_API ut8 *rz_base64_decode_dyn(const char *in, int len);
RZ_API char *rz_base64_encode_dyn(const char *str, int len);
RZ_API char *rz_base64_encode_dyn(const ut8 *bin, size_t sz);
#ifdef __cplusplus
}
#endif

View file

@ -1515,7 +1515,7 @@ RZ_API char *rz_str_encoded_json(const char *buf, int buf_size, int encoding) {
char *encoded_str;
if (encoding == PJ_ENCODING_STR_BASE64) {
encoded_str = rz_base64_encode_dyn(buf, buf_sz);
encoded_str = rz_base64_encode_dyn((const ut8 *)buf, buf_sz);
} else if (encoding == PJ_ENCODING_STR_HEX || encoding == PJ_ENCODING_STR_ARRAY) {
size_t loop = 0;
size_t i = 0;

View file

@ -75,40 +75,27 @@ RZ_API ut8 *rz_base64_decode_dyn(const char *in, int len) {
return bout;
}
RZ_API int rz_base64_encode(char *bout, const ut8 *bin, int len) {
int in, out;
if (len < 0) {
len = strlen((const char *)bin);
}
for (in = out = 0; in < len; in += 3, out += 4) {
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz) {
rz_return_val_if_fail(bin, 0);
size_t in, out;
for (in = out = 0; in < sz; in += 3, out += 4) {
local_b64_encode(bin + in, (char *)bout + out,
(len - in) > 3 ? 3 : len - in);
(sz - in) > 3 ? 3 : sz - in);
}
bout[out] = 0;
return out;
}
RZ_API char *rz_base64_encode_dyn(const char *str, int len) {
char *bout;
int in, out;
if (!str) {
RZ_API char *rz_base64_encode_dyn(const ut8 *bin, size_t sz) {
rz_return_val_if_fail(bin, NULL);
if (sz > (SIZE_MAX - 2) / 4) {
return NULL;
}
if (len < 0) {
len = strlen(str);
}
const int olen = (len * 4) + 2;
if (olen < len) {
return NULL;
}
bout = (char *)malloc(olen);
const size_t osz = (sz * 4) + 2;
char *bout = malloc(osz);
if (!bout) {
return NULL;
}
for (in = out = 0; in < len; in += 3, out += 4) {
local_b64_encode((const ut8 *)str + in, (char *)bout + out,
(len - in) > 3 ? 3 : len - in);
}
bout[out] = 0;
rz_base64_encode(bout, bin, sz);
return bout;
}

View file

@ -8328,7 +8328,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = malloc(memory_alloc);
if (str) {
snprintf(str, memory_alloc, "%s%s", name_str, desc_str);
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8365,7 +8365,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = malloc(memory_alloc);
if (str) {
snprintf(str, memory_alloc, "%s/%s%s", class_str, name_str, desc_str);
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8393,7 +8393,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = malloc(memory_alloc);
if (str) {
snprintf(str, memory_alloc, "\"%s\"", string_str);
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8413,7 +8413,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = malloc(34);
if (str) {
snprintf(str, 34, "0x%" PFMT64x, rz_bin_java_raw_to_long(item->info.cp_long.bytes.raw, 0));
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8421,7 +8421,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = malloc(1000);
if (str) {
snprintf(str, 1000, "%f", rz_bin_java_raw_to_double(item->info.cp_double.bytes.raw, 0));
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8429,7 +8429,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = calloc(34, 1);
if (str) {
snprintf(str, 34, "0x%08x", RZ_BIN_JAVA_UINT(item->info.cp_integer.bytes.raw, 0));
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8437,7 +8437,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = malloc(34);
if (str) {
snprintf(str, 34, "%f", RZ_BIN_JAVA_FLOAT(item->info.cp_float.bytes.raw, 0));
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8455,7 +8455,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
str = malloc(memory_alloc);
if (str) {
snprintf(str, memory_alloc, "%s %s", name_str, desc_str);
out = rz_base64_encode_dyn((const char *)str, strlen(str));
out = rz_base64_encode_dyn((const ut8 *)str, strlen(str));
free(str);
str = out;
}
@ -8467,7 +8467,7 @@ RZ_API char *rz_bin_java_resolve_b64_encode(RzBinJavaObj *BIN_OBJ, ut16 idx) {
free(desc_str);
}
} else {
str = rz_base64_encode_dyn((const char *)"(null)", 6);
str = rz_base64_encode_dyn((const ut8 *)"(null)", 6);
}
return str;
}

View file

@ -27,16 +27,25 @@ bool test_rz_base64_decode_invalid(void) {
}
int test_rz_base64_encode_dyn(void) {
char *hello = rz_base64_encode_dyn("hello", -1);
mu_assert_streq(hello, "aGVsbG8=", "base64_encode_dyn");
char *hello = rz_base64_encode_dyn((const ut8 *)"hello", 6);
mu_assert_streq(hello, "aGVsbG8A", "base64_encode_dyn");
free(hello);
hello = rz_base64_encode_dyn((const ut8 *)"hello1", 7);
mu_assert_streq(hello, "aGVsbG8xAA==", "base64_encode_dyn");
free(hello);
hello = rz_base64_encode_dyn((const ut8 *)"hello12", 8);
mu_assert_streq(hello, "aGVsbG8xMgA=", "base64_encode_dyn");
free(hello);
hello = rz_base64_encode_dyn((const ut8 *)"hello123", 9);
mu_assert_streq(hello, "aGVsbG8xMjMA", "base64_encode_dyn");
free(hello);
mu_end;
}
int test_rz_base64_encode(void) {
char *hello = malloc(50);
rz_base64_encode(hello, (ut8 *)"hello", -1);
mu_assert_streq(hello, "aGVsbG8=", "base64_encode_dyn");
rz_base64_encode(hello, (const ut8 *)"hello", 5);
mu_assert_streq(hello, "aGVsbG8=", "base64_encode");
free(hello);
mu_end;
}
@ -50,4 +59,4 @@ int all_tests() {
return tests_passed != tests_run;
}
mu_main(all_tests)
mu_main(all_tests)