Geant4 Cross Reference |
1 /* adler32.c -- compute the Adler-32 checksum 1 /* adler32.c -- compute the Adler-32 checksum of a data stream 2 * Copyright (C) 1995-2011, 2016 Mark Adler 2 * Copyright (C) 1995-2011, 2016 Mark Adler 3 * For conditions of distribution and use, see 3 * For conditions of distribution and use, see copyright notice in zlib.h 4 */ 4 */ 5 5 6 /* @(#) $Id$ */ << 7 6 8 #include "zutil.h" 7 #include "zutil.h" 9 8 10 local uLong adler32_combine_ OF((uLong adler1, 9 local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2)); 11 10 12 #define BASE 65521U /* largest prime small 11 #define BASE 65521U /* largest prime smaller than 65536 */ 13 #define NMAX 5552 12 #define NMAX 5552 14 /* NMAX is the largest n such that 255n(n+1)/2 13 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */ 15 14 16 #define DO1(buf,i) {adler += (buf)[i]; sum2 + 15 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;} 17 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 16 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1); 18 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 17 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2); 19 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 18 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4); 20 #define DO16(buf) DO8(buf,0); DO8(buf,8); 19 #define DO16(buf) DO8(buf,0); DO8(buf,8); 21 20 22 /* use NO_DIVIDE if your processor does not do 21 /* use NO_DIVIDE if your processor does not do division in hardware -- 23 try it both ways to see which is faster */ 22 try it both ways to see which is faster */ 24 #ifdef NO_DIVIDE 23 #ifdef NO_DIVIDE 25 /* note that this assumes BASE is 65521, where 24 /* note that this assumes BASE is 65521, where 65536 % 65521 == 15 26 (thank you to John Reiser for pointing this 25 (thank you to John Reiser for pointing this out) */ 27 # define CHOP(a) \ 26 # define CHOP(a) \ 28 do { \ 27 do { \ 29 unsigned long tmp = a >> 16; \ 28 unsigned long tmp = a >> 16; \ 30 a &= 0xffffUL; \ 29 a &= 0xffffUL; \ 31 a += (tmp << 4) - tmp; \ 30 a += (tmp << 4) - tmp; \ 32 } while (0) 31 } while (0) 33 # define MOD28(a) \ 32 # define MOD28(a) \ 34 do { \ 33 do { \ 35 CHOP(a); \ 34 CHOP(a); \ 36 if (a >= BASE) a -= BASE; \ 35 if (a >= BASE) a -= BASE; \ 37 } while (0) 36 } while (0) 38 # define MOD(a) \ 37 # define MOD(a) \ 39 do { \ 38 do { \ 40 CHOP(a); \ 39 CHOP(a); \ 41 MOD28(a); \ 40 MOD28(a); \ 42 } while (0) 41 } while (0) 43 # define MOD63(a) \ 42 # define MOD63(a) \ 44 do { /* this assumes a is not negative */ 43 do { /* this assumes a is not negative */ \ 45 z_off64_t tmp = a >> 32; \ 44 z_off64_t tmp = a >> 32; \ 46 a &= 0xffffffffL; \ 45 a &= 0xffffffffL; \ 47 a += (tmp << 8) - (tmp << 5) + tmp; \ 46 a += (tmp << 8) - (tmp << 5) + tmp; \ 48 tmp = a >> 16; \ 47 tmp = a >> 16; \ 49 a &= 0xffffL; \ 48 a &= 0xffffL; \ 50 a += (tmp << 4) - tmp; \ 49 a += (tmp << 4) - tmp; \ 51 tmp = a >> 16; \ 50 tmp = a >> 16; \ 52 a &= 0xffffL; \ 51 a &= 0xffffL; \ 53 a += (tmp << 4) - tmp; \ 52 a += (tmp << 4) - tmp; \ 54 if (a >= BASE) a -= BASE; \ 53 if (a >= BASE) a -= BASE; \ 55 } while (0) 54 } while (0) 56 #else 55 #else 57 # define MOD(a) a %= BASE 56 # define MOD(a) a %= BASE 58 # define MOD28(a) a %= BASE 57 # define MOD28(a) a %= BASE 59 # define MOD63(a) a %= BASE 58 # define MOD63(a) a %= BASE 60 #endif 59 #endif 61 60 62 /* =========================================== 61 /* ========================================================================= */ 63 uLong ZEXPORT adler32_z(adler, buf, len) 62 uLong ZEXPORT adler32_z(adler, buf, len) 64 uLong adler; 63 uLong adler; 65 const Bytef *buf; 64 const Bytef *buf; 66 z_size_t len; 65 z_size_t len; 67 { 66 { 68 unsigned long sum2; 67 unsigned long sum2; 69 unsigned n; 68 unsigned n; 70 69 71 /* split Adler-32 into component sums */ 70 /* split Adler-32 into component sums */ 72 sum2 = (adler >> 16) & 0xffff; 71 sum2 = (adler >> 16) & 0xffff; 73 adler &= 0xffff; 72 adler &= 0xffff; 74 73 75 /* in case user likes doing a byte at a ti 74 /* in case user likes doing a byte at a time, keep it fast */ 76 if (len == 1) { 75 if (len == 1) { 77 adler += buf[0]; 76 adler += buf[0]; 78 if (adler >= BASE) 77 if (adler >= BASE) 79 adler -= BASE; 78 adler -= BASE; 80 sum2 += adler; 79 sum2 += adler; 81 if (sum2 >= BASE) 80 if (sum2 >= BASE) 82 sum2 -= BASE; 81 sum2 -= BASE; 83 return adler | (sum2 << 16); 82 return adler | (sum2 << 16); 84 } 83 } 85 84 86 /* initial Adler-32 value (deferred check 85 /* initial Adler-32 value (deferred check for len == 1 speed) */ 87 if (buf == Z_NULL) 86 if (buf == Z_NULL) 88 return 1L; 87 return 1L; 89 88 90 /* in case short lengths are provided, kee 89 /* in case short lengths are provided, keep it somewhat fast */ 91 if (len < 16) { 90 if (len < 16) { 92 while (len--) { 91 while (len--) { 93 adler += *buf++; 92 adler += *buf++; 94 sum2 += adler; 93 sum2 += adler; 95 } 94 } 96 if (adler >= BASE) 95 if (adler >= BASE) 97 adler -= BASE; 96 adler -= BASE; 98 MOD28(sum2); /* only added 97 MOD28(sum2); /* only added so many BASE's */ 99 return adler | (sum2 << 16); 98 return adler | (sum2 << 16); 100 } 99 } 101 100 102 /* do length NMAX blocks -- requires just 101 /* do length NMAX blocks -- requires just one modulo operation */ 103 while (len >= NMAX) { 102 while (len >= NMAX) { 104 len -= NMAX; 103 len -= NMAX; 105 n = NMAX / 16; /* NMAX is div 104 n = NMAX / 16; /* NMAX is divisible by 16 */ 106 do { 105 do { 107 DO16(buf); /* 16 sums unr 106 DO16(buf); /* 16 sums unrolled */ 108 buf += 16; 107 buf += 16; 109 } while (--n); 108 } while (--n); 110 MOD(adler); 109 MOD(adler); 111 MOD(sum2); 110 MOD(sum2); 112 } 111 } 113 112 114 /* do remaining bytes (less than NMAX, sti 113 /* do remaining bytes (less than NMAX, still just one modulo) */ 115 if (len) { /* avoid modul 114 if (len) { /* avoid modulos if none remaining */ 116 while (len >= 16) { 115 while (len >= 16) { 117 len -= 16; 116 len -= 16; 118 DO16(buf); 117 DO16(buf); 119 buf += 16; 118 buf += 16; 120 } 119 } 121 while (len--) { 120 while (len--) { 122 adler += *buf++; 121 adler += *buf++; 123 sum2 += adler; 122 sum2 += adler; 124 } 123 } 125 MOD(adler); 124 MOD(adler); 126 MOD(sum2); 125 MOD(sum2); 127 } 126 } 128 127 129 /* return recombined sums */ 128 /* return recombined sums */ 130 return adler | (sum2 << 16); 129 return adler | (sum2 << 16); 131 } 130 } 132 131 133 /* =========================================== 132 /* ========================================================================= */ 134 uLong ZEXPORT adler32(adler, buf, len) 133 uLong ZEXPORT adler32(adler, buf, len) 135 uLong adler; 134 uLong adler; 136 const Bytef *buf; 135 const Bytef *buf; 137 uInt len; 136 uInt len; 138 { 137 { 139 return adler32_z(adler, buf, len); 138 return adler32_z(adler, buf, len); 140 } 139 } 141 140 142 /* =========================================== 141 /* ========================================================================= */ 143 local uLong adler32_combine_(adler1, adler2, l 142 local uLong adler32_combine_(adler1, adler2, len2) 144 uLong adler1; 143 uLong adler1; 145 uLong adler2; 144 uLong adler2; 146 z_off64_t len2; 145 z_off64_t len2; 147 { 146 { 148 unsigned long sum1; 147 unsigned long sum1; 149 unsigned long sum2; 148 unsigned long sum2; 150 unsigned rem; 149 unsigned rem; 151 150 152 /* for negative len, return invalid adler3 151 /* for negative len, return invalid adler32 as a clue for debugging */ 153 if (len2 < 0) 152 if (len2 < 0) 154 return 0xffffffffUL; 153 return 0xffffffffUL; 155 154 156 /* the derivation of this formula is left 155 /* the derivation of this formula is left as an exercise for the reader */ 157 MOD63(len2); /* assumes len 156 MOD63(len2); /* assumes len2 >= 0 */ 158 rem = (unsigned)len2; 157 rem = (unsigned)len2; 159 sum1 = adler1 & 0xffff; 158 sum1 = adler1 & 0xffff; 160 sum2 = rem * sum1; 159 sum2 = rem * sum1; 161 MOD(sum2); 160 MOD(sum2); 162 sum1 += (adler2 & 0xffff) + BASE - 1; 161 sum1 += (adler2 & 0xffff) + BASE - 1; 163 sum2 += ((adler1 >> 16) & 0xffff) + ((adle 162 sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem; 164 if (sum1 >= BASE) sum1 -= BASE; 163 if (sum1 >= BASE) sum1 -= BASE; 165 if (sum1 >= BASE) sum1 -= BASE; 164 if (sum1 >= BASE) sum1 -= BASE; 166 if (sum2 >= ((unsigned long)BASE << 1)) su 165 if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1); 167 if (sum2 >= BASE) sum2 -= BASE; 166 if (sum2 >= BASE) sum2 -= BASE; 168 return sum1 | (sum2 << 16); 167 return sum1 | (sum2 << 16); 169 } 168 } 170 169 171 /* =========================================== 170 /* ========================================================================= */ 172 uLong ZEXPORT adler32_combine(adler1, adler2, 171 uLong ZEXPORT adler32_combine(adler1, adler2, len2) 173 uLong adler1; 172 uLong adler1; 174 uLong adler2; 173 uLong adler2; 175 z_off_t len2; 174 z_off_t len2; 176 { 175 { 177 return adler32_combine_(adler1, adler2, le 176 return adler32_combine_(adler1, adler2, len2); 178 } 177 } 179 178 180 uLong ZEXPORT adler32_combine64(adler1, adler2 179 uLong ZEXPORT adler32_combine64(adler1, adler2, len2) 181 uLong adler1; 180 uLong adler1; 182 uLong adler2; 181 uLong adler2; 183 z_off64_t len2; 182 z_off64_t len2; 184 { 183 { 185 return adler32_combine_(adler1, adler2, le 184 return adler32_combine_(adler1, adler2, len2); 186 } 185 } 187 186