X-Git-Url: https://git.distorted.org.uk/u/mdw/catacomb/blobdiff_plain/5b69c389f8528045a8aabf848818dcbd1c420881..e9926004f7caf25abbfb87ebd921a01e6bf865dc:/hash.h diff --git a/hash.h b/hash.h index 1d659d3..f5d21de 100644 --- a/hash.h +++ b/hash.h @@ -1,6 +1,6 @@ /* -*-c-*- * - * $Id: hash.h,v 1.3 2000/06/17 11:23:27 mdw Exp $ + * $Id: hash.h,v 1.4 2004/03/21 22:42:27 mdw Exp $ * * Generic handling for message digest functions * @@ -30,6 +30,9 @@ /*----- Revision history --------------------------------------------------* * * $Log: hash.h,v $ + * Revision 1.4 2004/03/21 22:42:27 mdw + * Test hashing on long strings. + * * Revision 1.3 2000/06/17 11:23:27 mdw * Portability fix for broken compilers. * @@ -179,6 +182,8 @@ #include #include +#define HASH_BUFLEN 100000 + #define HASH_TEST(PRE, pre) \ \ static int verify(dstr *v) \ @@ -227,8 +232,47 @@ static int verify(dstr *v) \ return (ok); \ } \ \ +static int verifyrep(dstr *v) \ +{ \ + pre##_ctx ctx; \ + size_t len = v[0].len; \ + int n = *(int *)v[1].buf; \ + int nd = 0; \ + int nn = len; \ + int ok = 1; \ + octet *p, *q; \ + dstr d = DSTR_INIT; \ + \ + while (nn < HASH_BUFLEN && (n & 1) == 0) { nd++; nn <<= 1; n >>= 1; } \ + p = xmalloc(nn); \ + memcpy(p, v[0].buf, len); \ + q = p + len; \ + while (nd--) { memcpy(q, p, len); q += len; len <<= 1; } \ + \ + dstr_ensure(&d, PRE##_HASHSZ); \ + d.len = PRE##_HASHSZ; \ + pre##_init(&ctx); \ + while (n--) pre##_hash(&ctx, p, len); \ + pre##_done(&ctx, d.buf); \ + \ + if (memcmp(d.buf, v[2].buf, PRE##_HASHSZ) != 0) { \ + printf("\nfail:\n\tinput = `%s'\n\treps = `%i'\n\texpected = ", \ + v[0].buf, *(int *)v[1].buf); \ + type_hex.dump(&v[2], stdout); \ + fputs("\n\tcomputed = ", stdout); \ + type_hex.dump(&d, stdout); \ + putchar('\n'); \ + ok = 0; \ + } \ + free(p); \ + dstr_destroy(&d); \ + return (ok); \ +} \ + \ static test_chunk defs[] = { \ { #pre, verify, { &type_string, &type_hex, 0 } }, \ + { #pre "-rep", verifyrep, \ + { &type_string, &type_int, &type_hex, 0 } }, \ { 0, 0, { 0 } } \ }; \ \