static int do_hash_internal(RHash *ctx, int hash, const ut8 *buf, int len, int rad, int print) { int dlen = r_hash_calculate (ctx, hash, buf, len); if (!dlen) return 0; if (!print) return 1; if (hash == R_HASH_ENTROPY) { double e = r_hash_entropy (buf, len); if (rad) { eprintf ("entropy: %10f\n", e); } else { printf ("0x%08"PFMT64x"-0x%08"PFMT64x" %10f: ", from, to, e); r_print_progressbar (NULL, 12.5 * e, 60); printf ("\n"); } } else do_hash_print (ctx, hash, dlen, rad); return 1; }
static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int rad, int ule) { ut64 j, fsize, algobit = r_hash_name_to_bits (algo); RHash *ctx; ut8 *buf; int i, first = 1; if (algobit == R_HASH_NONE) { eprintf ("rahash2: Invalid hashing algorithm specified\n"); return 1; } fsize = r_io_size (io); if (fsize <1) { eprintf ("rahash2: Invalid file size\n"); return 1; } if (bsize<0) bsize = fsize / -bsize; if (bsize == 0 || bsize > fsize) bsize = fsize; if (to == 0LL) to = fsize; if (from>to) { eprintf ("rahash2: Invalid -f -t range\n"); return 1; } if (fsize == -1LL) { eprintf ("rahash2: Unknown file size\n"); return 1; } buf = malloc (bsize+1); if (!buf) return 1; ctx = r_hash_new (R_TRUE, algobit); if (rad == 'j') printf ("["); if (incremental) { for (i=1; i<0x800000; i<<=1) { if (algobit & i) { int hashbit = i & algobit; int dlen = r_hash_size (hashbit); r_hash_do_begin (ctx, i); if (rad == 'j') { if (first) { first = 0; } else { printf (","); } } if (s.buf && s.prefix) { do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); } for (j=from; j<to; j+=bsize) { int len = ((j+bsize)>to)? (to-j): bsize; r_io_pread (io, j, buf, len); do_hash_internal (ctx, hashbit, buf, len, rad, 0, ule); } if (s.buf && !s.prefix) { do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); } r_hash_do_end (ctx, i); if (iterations>0) r_hash_do_spice (ctx, i, iterations, _s); if (!*r_hash_name (i)) continue; if (!quiet && rad != 'j') printf ("%s: ", file); do_hash_print (ctx, i, dlen, rad, ule); } } if (_s) free (_s->buf); } else { /* iterate over all algorithm bits */ if (s.buf) eprintf ("Warning: Seed ignored on per-block hashing.\n"); for (i=1; i<0x800000; i<<=1) { ut64 f, t, ofrom, oto; if (algobit & i) { int hashbit = i & algobit; ofrom = from; oto = to; f = from; t = to; for (j=f; j<t; j+=bsize) { int nsize = (j+bsize<fsize)? bsize: (fsize-j); r_io_pread (io, j, buf, bsize); from = j; to = j+bsize; if (to>fsize) to = fsize; do_hash_internal (ctx, hashbit, buf, nsize, rad, 1, ule); } from = ofrom; to = oto; } } } if (rad == 'j') printf ("]\n"); r_hash_free (ctx); free (buf); return 0; }
static int do_hash(const char *algo, RIO *io, int bsize, int rad) { ut8 *buf; RHash *ctx; ut64 j, fsize; int i; ut64 algobit = r_hash_name_to_bits (algo); if (algobit == R_HASH_NONE) { eprintf ("Invalid hashing algorithm specified\n"); return 1; } fsize = r_io_size (io); if (bsize == 0 || bsize > fsize) bsize = fsize; if (to == 0LL) to = fsize; if (from>to) { eprintf ("Invalid -f -t range\n"); return 1; } if (fsize == -1LL) { eprintf ("Unknown file size\n"); return 1; } buf = malloc (bsize+1); ctx = r_hash_new (R_TRUE, algobit); if (incremental) { for (i=1; i<0x800000; i<<=1) { if (algobit & i) { int hashbit = i & algobit; int dlen = r_hash_size (hashbit); r_hash_do_begin (ctx, i); for (j=from; j<to; j+=bsize) { r_io_read_at (io, j, buf, bsize); do_hash_internal (ctx, hashbit, buf, ((j+bsize)<fsize)? bsize: (fsize-j), rad, 0); } r_hash_do_end (ctx, i); do_hash_print (ctx, i, dlen, rad); } } } else { /* iterate over all algorithm bits */ for (i=1; i<0x800000; i<<=1) { ut64 f, t, ofrom, oto; if (algobit & i) { int hashbit = i & algobit; ofrom = from; oto = to; f = from; t = to; for (j=f; j<t; j+=bsize) { int nsize = (j+bsize<fsize)? bsize: (fsize-j); r_io_read_at (io, j, buf, bsize); from = j; to = j+bsize; do_hash_internal (ctx, hashbit, buf, nsize, rad, 1); } from = ofrom; to = oto; } } } r_hash_free (ctx); free (buf); return 0; }