From: Andrew Tridgell Date: Thu, 28 Mar 2002 01:55:55 +0000 (+0100) Subject: fixed handling of non-64 multiple md4 buffers X-Git-Tag: v1.0~22 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=21886013d1a287c7df687d34f428a3ab1135516c;p=thirdparty%2Fccache.git fixed handling of non-64 multiple md4 buffers --- diff --git a/hash.c b/hash.c index 69c8bef8e..a41ec34c6 100644 --- a/hash.c +++ b/hash.c @@ -23,24 +23,59 @@ static struct mdfour md; -void hash_start(void) +void hash_buffer(const char *s, int len) { - mdfour_begin(&md); + static char tail[64]; + static int tail_len; + + /* s == NULL means push the last chunk */ + if (s == NULL) { + if (tail_len > 0) { + mdfour_update(&md, tail, tail_len); + tail_len = 0; + } + return; + } + + if (tail_len) { + int n = 64-tail_len; + if (n > len) n = len; + memcpy(tail+tail_len, s, n); + tail_len += n; + len -= n; + s += n; + if (tail_len == 64) { + mdfour_update(&md, tail, 64); + tail_len = 0; + } + } + + while (len >= 64) { + mdfour_update(&md, s, 64); + s += 64; + len -= 64; + } + + if (len) { + memcpy(tail, s, len); + tail_len = len; + } } -void hash_string(const char *s) + +void hash_start(void) { - mdfour_update(&md, s, strlen(s)); + mdfour_begin(&md); } -void hash_buffer(const char *s, int len) +void hash_string(const char *s) { - mdfour_update(&md, s, len); + hash_buffer(s, strlen(s)); } void hash_int(int x) { - mdfour_update(&md, (unsigned char *)&x, sizeof(x)); + hash_buffer((char *)&x, sizeof(x)); } /* add contents of a file to the hash */ @@ -56,7 +91,7 @@ void hash_file(const char *fname) } while ((n = read(fd, buf, sizeof(buf))) > 0) { - mdfour_update(&md, buf, n); + hash_buffer(buf, n); } close(fd); } @@ -68,6 +103,7 @@ char *hash_result(void) static char ret[33]; int i; + hash_buffer(NULL, 0); mdfour_result(&md, sum); for (i=0;i<16;i++) {