1
#include "base/basictypes.h"
2
#include "util/hash/hash.h"
7
// WARNING - may read one more byte! Fine if the input is a null-terminated string.
8
// Implementation from Wikipedia.
9
uint32_t Fletcher(const uint8_t *data_uint8, size_t length) {
10
const uint16_t *data = (const uint16_t *)data_uint8;
11
size_t len = (length + 1) / 2;
12
uint32_t sum1 = 0xffff, sum2 = 0xffff;
15
size_t tlen = len > 360 ? 360 : len;
23
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
24
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
27
/* Second reduction step to reduce sums to 16 bits */
28
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
29
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
30
return sum2 << 16 | sum1;
33
// Implementation from Wikipedia
34
// Slightly slower than Fletcher above, but slighly more reliable.
35
#define MOD_ADLER 65521
36
// data: Pointer to the data to be summed; len is in bytes
37
uint32_t Adler32(const uint8_t *data, size_t len) {
38
uint32_t a = 1, b = 0;
40
size_t tlen = len > 5550 ? 5550 : len;
47
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
48
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
51
// It can be shown that a <= 0x1013a here, so a single subtract will do.
56
// It can be shown that b can reach 0xfff87 here.
57
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);