Fix the file hash computation ##bin

This commit is contained in:
David CARLIER 2019-02-10 00:27:07 +00:00 committed by radare
parent 517f9d3d3b
commit bbfc4a8747
5 changed files with 48 additions and 37 deletions

View File

@ -849,23 +849,22 @@ R_API bool r_bin_file_close(RBin *bin, int bd) {
return false;
}
R_API int r_bin_file_hash(RBin *bin, ut64 limit, const char *file) {
R_API bool r_bin_file_hash(RBin *bin, ut64 limit, const char *file) {
char hash[128], *p;
RHash *ctx;
ut8* buf;
ut64 buf_len = 0;
ut64 buf_len = 0, r = 0;
int i;
RBinFile *bf = bin->cur;
if (!bf) {
return 0;
return false;
}
RBinObject *o = bf->o;
if (!o) {
return 0;
if (!o || !o->info) {
return false;
}
RIODesc *iod = r_io_desc_get (bin->iob.io, bf->fd);
if (!iod) {
return 0;
return false;
}
if (!file && iod) {
@ -876,36 +875,40 @@ R_API int r_bin_file_hash(RBin *bin, ut64 limit, const char *file) {
// By SLURP_LIMIT normally cannot compute ...
if (buf_len > limit) {
eprintf ("Cannot compute hash\n");
return -1;
return false;
}
// XXX should use io api not raw file slurping
int blen = 0;
buf = (ut8*)r_file_slurp (file, &blen);
const size_t blocksize = 64000;
ut8 *buf = malloc (blocksize);
if (!buf) {
eprintf ("Cannot allocate computation buffer\n");
return false;
}
buf_len = blen;
if (!o->info) {
return false;
ctx = r_hash_new (false, R_HASH_MD5 | R_HASH_SHA1);
while (r+blocksize < buf_len) {
r_io_desc_seek (iod, r, R_IO_SEEK_SET);
int b = r_io_desc_read (iod, buf, blocksize);
(void)r_hash_do_md5 (ctx, buf, blocksize);
(void)r_hash_do_sha1 (ctx, buf, blocksize);
r += b;
}
if (buf) {
ctx = r_hash_new (false, R_HASH_MD5 | R_HASH_SHA1);
#define BLK_SIZE_OFF 1024
for (i = 0; i < buf_len; i += BLK_SIZE_OFF) {
(void)r_hash_do_md5 (ctx, &buf[i], R_MIN (buf_len-i, BLK_SIZE_OFF));
(void)r_hash_do_sha1 (ctx, &buf[i], R_MIN (buf_len-i, BLK_SIZE_OFF));
}
r_hash_do_end (ctx, R_HASH_MD5);
p = hash;
r_hex_bin2str (ctx->digest, R_HASH_SIZE_MD5, p);
o->info->hashes = r_strbuf_new ("");
r_strbuf_appendf (o->info->hashes, "md5 %s", hash);
r_hash_do_end (ctx, R_HASH_SHA1);
p = hash;
r_hex_bin2str (ctx->digest, R_HASH_SIZE_SHA1, p);
r_strbuf_appendf (o->info->hashes, "\nsha1 %s", hash);
r_hash_free (ctx);
if (r < buf_len) {
r_io_desc_seek (iod, r, R_IO_SEEK_SET);
const size_t rem_len = buf_len-r;
int b = r_io_desc_read (iod, buf, rem_len);
(void)r_hash_do_md5 (ctx, buf, rem_len);
(void)r_hash_do_sha1 (ctx, buf, rem_len);
}
r_hash_do_end (ctx, R_HASH_MD5);
p = hash;
r_hex_bin2str (ctx->digest, R_HASH_SIZE_MD5, p);
RStrBuf *sbuf = r_strbuf_new ("");
r_strbuf_appendf (sbuf, "md5 %s", hash);
r_hash_do_end (ctx, R_HASH_SHA1);
p = hash;
r_hex_bin2str (ctx->digest, R_HASH_SIZE_SHA1, p);
r_strbuf_appendf (sbuf, "\nsha1 %s", hash);
o->info->hashes = r_strbuf_drain (sbuf);
free (buf);
r_hash_free (ctx);
return true;
}

View File

@ -124,7 +124,7 @@ R_API void r_bin_info_free(RBinInfo *rb) {
if (!rb) {
return;
}
r_strbuf_free (rb->hashes);
free (rb->hashes);
free (rb->intrp);
free (rb->file);
free (rb->type);

View File

@ -3588,7 +3588,7 @@ static int bin_hashes(RCore *r, int mode) {
RIODesc *iod = r_io_desc_get (r->io, r->file->fd);
if (iod) {
r_bin_file_hash (r->bin, lim, iod->name);
const char *hashes = r_strbuf_get (r->bin->cur->o->info->hashes);
char *hashes = r->bin->cur->o->info->hashes;
if (IS_MODE_JSON (mode)) {
PJ *pj = pj_new ();
if (!pj) {

View File

@ -458,9 +458,17 @@ static int cmd_info(void *data, const char *input) {
}
case 'F': // "iF"
if (input[1] == 'j') {
r_cons_printf ("{\"%s\"}", r_strbuf_get (core->bin->cur->o->info->hashes));
PJ *pj = pj_new ();
if (!pj) {
eprintf ("JSON mode failed\n");
return 0;
}
pj_ks (pj, "values", core->bin->cur->o->info->hashes);
pj_end (pj);
r_cons_printf ("%s", pj_string (pj));
pj_free (pj);
} else {
r_cons_printf (r_strbuf_get (core->bin->cur->o->info->hashes));
r_cons_printf ("%s", core->bin->cur->o->info->hashes);
}
break;
case 'Z': // "iZ"

View File

@ -197,7 +197,7 @@ typedef struct r_bin_info_t {
char *guid;
char *debug_file_name;
const char *lang;
RStrBuf *hashes;
char *hashes;
int bits;
int has_va;
int has_pi; // pic/pie
@ -701,7 +701,7 @@ R_API bool r_bin_file_set_cur_by_name(RBin *bin, const char *name);
R_API bool r_bin_file_close(RBin *bin, int bd);
R_API int r_bin_file_delete_all(RBin *bin);
R_API int r_bin_file_delete(RBin *bin, ut32 bin_fd);
R_API int r_bin_file_hash(RBin *bin, ut64 limit, const char *file);
R_API bool r_bin_file_hash(RBin *bin, ut64 limit, const char *file);
// binobject functions
R_API int r_bin_object_set_items(RBinFile *binfile, RBinObject *o);