]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
crc32*() didn't return a standard CRC32 value.
authorTimo Sirainen <tss@iki.fi>
Fri, 5 Feb 2010 22:36:11 +0000 (00:36 +0200)
committerTimo Sirainen <tss@iki.fi>
Fri, 5 Feb 2010 22:36:11 +0000 (00:36 +0200)
--HG--
branch : HEAD

src/lib/Makefile.am
src/lib/crc32.c
src/lib/test-crc32.c [new file with mode: 0644]
src/lib/test-lib.c
src/lib/test-lib.h

index 35152843448700d990425d2803171e7d14aeb3b1..ccfbec39c4c3f4e8a7cd5c00b5ea68b19c70c031 100644 (file)
@@ -222,6 +222,7 @@ test_lib_SOURCES = \
        test-base64.c \
        test-bsearch-insert-pos.c \
        test-buffer.c \
+       test-crc32.c \
        test-hex-binary.c \
        test-istream-concat.c \
        test-istream-crlf.c \
index 9a7312d1616ac136b387995cf8f2c76f5eccfe92..6ac28ad2bfb68280fbc8ce578abbf7baf86fb216 100644 (file)
@@ -60,28 +60,32 @@ static uint32_t crc32tab[256] = {
 
 uint32_t crc32_data(const void *data, size_t size)
 {
-       return crc32_data_more((uint32_t)-1, data, size);
+       return crc32_data_more(0, data, size);
 }
 
 uint32_t crc32_data_more(uint32_t crc, const void *data, size_t size)
 {
        const uint8_t *p = data, *end = p + size;
 
+       crc ^= 0xffffffff;
        for (; p != end; p++)
                crc = (crc >> 8) ^ crc32tab[((crc ^ *p) & 0xff)];
+       crc ^= 0xffffffff;
        return crc;
 }
 
 uint32_t crc32_str(const char *str)
 {
-       return crc32_str_more((uint32_t)-1, str);
+       return crc32_str_more(0, str);
 }
 
 uint32_t crc32_str_more(uint32_t crc, const char *str)
 {
        const uint8_t *p = (const uint8_t *)str;
 
+       crc ^= 0xffffffff;
        for (; *p != '\0'; p++)
                crc = (crc >> 8) ^ crc32tab[((crc ^ *p) & 0xff)];
+       crc ^= 0xffffffff;
        return crc;
 }
diff --git a/src/lib/test-crc32.c b/src/lib/test-crc32.c
new file mode 100644 (file)
index 0000000..01cc35b
--- /dev/null
@@ -0,0 +1,14 @@
+/* Copyright (c) 2010 Dovecot authors, see the included COPYING file */
+
+#include "test-lib.h"
+#include "crc32.h"
+
+void test_crc32(void)
+{
+       const char str[] = "foo\0bar";
+
+       test_begin("crc32");
+       test_assert(crc32_str(str) == 0x8c736521);
+       test_assert(crc32_data(str, sizeof(str)) == 0x32c9723d);
+       test_end();
+}
index 4ab0f87afaf5396627556349c8aab662abf675d9..f1183e02d19f2f7a71412548bfc73ecdb7f404f2 100644 (file)
@@ -10,6 +10,7 @@ int main(void)
                test_base64,
                test_bsearch_insert_pos,
                test_buffer,
+               test_crc32,
                test_hex_binary,
                test_istream_concat,
                test_istream_crlf,
index cd074c7f30ca1d4b53ed05dbb28330e076e6f723..50a880a7a7d67bab4feee34004e9c722c9b2ce79 100644 (file)
@@ -9,6 +9,7 @@ void test_array(void);
 void test_base64(void);
 void test_bsearch_insert_pos(void);
 void test_buffer(void);
+void test_crc32(void);
 void test_hex_binary(void);
 void test_istream_concat(void);
 void test_istream_crlf(void);