version 1.1.1.1, 2012/02/21 16:23:02
|
version 1.1.1.2, 2013/07/22 10:46:14
|
Line 1
|
Line 1
|
/* adler32.c -- compute the Adler-32 checksum of a data stream |
/* adler32.c -- compute the Adler-32 checksum of a data stream |
* Copyright (C) 1995-2007 Mark Adler | * Copyright (C) 1995-2011 Mark Adler |
* For conditions of distribution and use, see copyright notice in zlib.h |
* For conditions of distribution and use, see copyright notice in zlib.h |
*/ |
*/ |
|
|
Line 9
|
Line 9
|
|
|
#define local static |
#define local static |
|
|
local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2); | local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); |
|
|
#define BASE 65521UL /* largest prime smaller than 65536 */ | #define BASE 65521 /* largest prime smaller than 65536 */ |
#define NMAX 5552 |
#define NMAX 5552 |
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ |
/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ |
|
|
Line 21 local uLong adler32_combine_(uLong adler1, uLong adler
|
Line 21 local uLong adler32_combine_(uLong adler1, uLong adler
|
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); |
#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); |
#define DO16(buf) DO8(buf,0); DO8(buf,8); |
#define DO16(buf) DO8(buf,0); DO8(buf,8); |
|
|
/* use NO_DIVIDE if your processor does not do division in hardware */ | /* use NO_DIVIDE if your processor does not do division in hardware -- |
| try it both ways to see which is faster */ |
#ifdef NO_DIVIDE |
#ifdef NO_DIVIDE |
# define MOD(a) \ | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 |
| (thank you to John Reiser for pointing this out) */ |
| # define CHOP(a) \ |
do { \ |
do { \ |
if (a >= (BASE << 16)) a -= (BASE << 16); \ | unsigned long tmp = a >> 16; \ |
if (a >= (BASE << 15)) a -= (BASE << 15); \ | a &= 0xffffUL; \ |
if (a >= (BASE << 14)) a -= (BASE << 14); \ | a += (tmp << 4) - tmp; \ |
if (a >= (BASE << 13)) a -= (BASE << 13); \ | } while (0) |
if (a >= (BASE << 12)) a -= (BASE << 12); \ | # define MOD28(a) \ |
if (a >= (BASE << 11)) a -= (BASE << 11); \ | do { \ |
if (a >= (BASE << 10)) a -= (BASE << 10); \ | CHOP(a); \ |
if (a >= (BASE << 9)) a -= (BASE << 9); \ | |
if (a >= (BASE << 8)) a -= (BASE << 8); \ | |
if (a >= (BASE << 7)) a -= (BASE << 7); \ | |
if (a >= (BASE << 6)) a -= (BASE << 6); \ | |
if (a >= (BASE << 5)) a -= (BASE << 5); \ | |
if (a >= (BASE << 4)) a -= (BASE << 4); \ | |
if (a >= (BASE << 3)) a -= (BASE << 3); \ | |
if (a >= (BASE << 2)) a -= (BASE << 2); \ | |
if (a >= (BASE << 1)) a -= (BASE << 1); \ | |
if (a >= BASE) a -= BASE; \ |
if (a >= BASE) a -= BASE; \ |
} while (0) |
} while (0) |
# define MOD4(a) \ | # define MOD(a) \ |
do { \ |
do { \ |
if (a >= (BASE << 4)) a -= (BASE << 4); \ | CHOP(a); \ |
if (a >= (BASE << 3)) a -= (BASE << 3); \ | MOD28(a); \ |
if (a >= (BASE << 2)) a -= (BASE << 2); \ | } while (0) |
if (a >= (BASE << 1)) a -= (BASE << 1); \ | # define MOD63(a) \ |
| do { /* this assumes a is not negative */ \ |
| z_off64_t tmp = a >> 32; \ |
| a &= 0xffffffffL; \ |
| a += (tmp << 8) - (tmp << 5) + tmp; \ |
| tmp = a >> 16; \ |
| a &= 0xffffL; \ |
| a += (tmp << 4) - tmp; \ |
| tmp = a >> 16; \ |
| a &= 0xffffL; \ |
| a += (tmp << 4) - tmp; \ |
if (a >= BASE) a -= BASE; \ |
if (a >= BASE) a -= BASE; \ |
} while (0) |
} while (0) |
#else |
#else |
# define MOD(a) a %= BASE |
# define MOD(a) a %= BASE |
# define MOD4(a) a %= BASE | # define MOD28(a) a %= BASE |
| # define MOD63(a) a %= BASE |
#endif |
#endif |
|
|
/* ========================================================================= */ |
/* ========================================================================= */ |
Line 92 uLong ZEXPORT adler32(adler, buf, len)
|
Line 97 uLong ZEXPORT adler32(adler, buf, len)
|
} |
} |
if (adler >= BASE) |
if (adler >= BASE) |
adler -= BASE; |
adler -= BASE; |
MOD4(sum2); /* only added so many BASE's */ | MOD28(sum2); /* only added so many BASE's */ |
return adler | (sum2 << 16); |
return adler | (sum2 << 16); |
} |
} |
|
|
Line 137 local uLong adler32_combine_(adler1, adler2, len2)
|
Line 142 local uLong adler32_combine_(adler1, adler2, len2)
|
unsigned long sum2; |
unsigned long sum2; |
unsigned rem; |
unsigned rem; |
|
|
|
/* for negative len, return invalid adler32 as a clue for debugging */ |
|
if (len2 < 0) |
|
return 0xffffffffUL; |
|
|
/* the derivation of this formula is left as an exercise for the reader */ |
/* the derivation of this formula is left as an exercise for the reader */ |
rem = (unsigned)(len2 % BASE); | MOD63(len2); /* assumes len2 >= 0 */ |
| rem = (unsigned)len2; |
sum1 = adler1 & 0xffff; |
sum1 = adler1 & 0xffff; |
sum2 = rem * sum1; |
sum2 = rem * sum1; |
MOD(sum2); |
MOD(sum2); |