]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
fixed handling of non-64 multiple md4 buffers
authorAndrew Tridgell <tridge@samba.org>
Thu, 28 Mar 2002 01:55:55 +0000 (02:55 +0100)
committerAndrew Tridgell <tridge@samba.org>
Thu, 28 Mar 2002 01:55:55 +0000 (02:55 +0100)
hash.c

diff --git a/hash.c b/hash.c
index 69c8bef8e8703ccdbcdd1d481d4812837578b359..a41ec34c6dc6c1f60b07dcbfa5a22ebd91386fff 100644 (file)
--- a/hash.c
+++ b/hash.c
 
 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++) {