| 1 | /* adler32_p.h -- Private inline functions and macros shared with |
| 2 | * different computation of the Adler-32 checksum |
| 3 | * of a data stream. |
| 4 | * Copyright (C) 1995-2011, 2016 Mark Adler |
| 5 | * For conditions of distribution and use, see copyright notice in zlib.h |
| 6 | */ |
| 7 | |
| 8 | #ifndef ADLER32_P_H |
| 9 | #define ADLER32_P_H |
| 10 | |
| 11 | #define BASE 65521U /* largest prime smaller than 65536 */ |
| 12 | #define NMAX 5552 |
| 13 | /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ |
| 14 | |
| 15 | /* use NO_DIVIDE if your processor does not do division in hardware -- |
| 16 | try it both ways to see which is faster */ |
| 17 | #ifdef NO_DIVIDE |
| 18 | /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 |
| 19 | (thank you to John Reiser for pointing this out) */ |
| 20 | # define CHOP(a) \ |
| 21 | do { \ |
| 22 | uint32_t tmp = a >> 16; \ |
| 23 | a &= 0xffff; \ |
| 24 | a += (tmp << 4) - tmp; \ |
| 25 | } while (0) |
| 26 | # define MOD28(a) \ |
| 27 | do { \ |
| 28 | CHOP(a); \ |
| 29 | if (a >= BASE) a -= BASE; \ |
| 30 | } while (0) |
| 31 | # define MOD(a) \ |
| 32 | do { \ |
| 33 | CHOP(a); \ |
| 34 | MOD28(a); \ |
| 35 | } while (0) |
| 36 | # define MOD63(a) \ |
| 37 | do { /* this assumes a is not negative */ \ |
| 38 | z_off64_t tmp = a >> 32; \ |
| 39 | a &= 0xffffffffL; \ |
| 40 | a += (tmp << 8) - (tmp << 5) + tmp; \ |
| 41 | tmp = a >> 16; \ |
| 42 | a &= 0xffffL; \ |
| 43 | a += (tmp << 4) - tmp; \ |
| 44 | tmp = a >> 16; \ |
| 45 | a &= 0xffffL; \ |
| 46 | a += (tmp << 4) - tmp; \ |
| 47 | if (a >= BASE) a -= BASE; \ |
| 48 | } while (0) |
| 49 | #else |
| 50 | # define MOD(a) a %= BASE |
| 51 | # define MOD28(a) a %= BASE |
| 52 | # define MOD63(a) a %= BASE |
| 53 | #endif |
| 54 | |
| 55 | static inline uint32_t adler32_len_1(uint32_t adler, const unsigned char *buf, uint32_t sum2) { |
| 56 | adler += buf[0]; |
| 57 | if (adler >= BASE) |
| 58 | adler -= BASE; |
| 59 | sum2 += adler; |
| 60 | if (sum2 >= BASE) |
| 61 | sum2 -= BASE; |
| 62 | return adler | (sum2 << 16); |
| 63 | } |
| 64 | |
| 65 | static inline uint32_t adler32_len_16(uint32_t adler, const unsigned char *buf, size_t len, uint32_t sum2) { |
| 66 | while (len) { |
| 67 | --len; |
| 68 | adler += *buf++; |
| 69 | sum2 += adler; |
| 70 | } |
| 71 | if (adler >= BASE) |
| 72 | adler -= BASE; |
| 73 | MOD28(sum2); /* only added so many BASE's */ |
| 74 | return adler | (sum2 << 16); |
| 75 | } |
| 76 | |
| 77 | #endif /* ADLER32_P_H */ |
| 78 | |