00001
00002
00003
00004
00005
00006
00007
00008 #include "zutil.h"
00009
00010 #define local static
00011
00012 local uLong adler32_combine_(uLong adler1, uLong adler2, z_off64_t len2);
00013
00014 #define BASE 65521UL
00015 #define NMAX 5552
00016
00017
00018 #define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
00019 #define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
00020 #define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
00021 #define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
00022 #define DO16(buf) DO8(buf,0); DO8(buf,8);
00023
00024
00025 #ifdef NO_DIVIDE
00026 # define MOD(a) \
00027 do { \
00028 if (a >= (BASE << 16)) a -= (BASE << 16); \
00029 if (a >= (BASE << 15)) a -= (BASE << 15); \
00030 if (a >= (BASE << 14)) a -= (BASE << 14); \
00031 if (a >= (BASE << 13)) a -= (BASE << 13); \
00032 if (a >= (BASE << 12)) a -= (BASE << 12); \
00033 if (a >= (BASE << 11)) a -= (BASE << 11); \
00034 if (a >= (BASE << 10)) a -= (BASE << 10); \
00035 if (a >= (BASE << 9)) a -= (BASE << 9); \
00036 if (a >= (BASE << 8)) a -= (BASE << 8); \
00037 if (a >= (BASE << 7)) a -= (BASE << 7); \
00038 if (a >= (BASE << 6)) a -= (BASE << 6); \
00039 if (a >= (BASE << 5)) a -= (BASE << 5); \
00040 if (a >= (BASE << 4)) a -= (BASE << 4); \
00041 if (a >= (BASE << 3)) a -= (BASE << 3); \
00042 if (a >= (BASE << 2)) a -= (BASE << 2); \
00043 if (a >= (BASE << 1)) a -= (BASE << 1); \
00044 if (a >= BASE) a -= BASE; \
00045 } while (0)
00046 # define MOD4(a) \
00047 do { \
00048 if (a >= (BASE << 4)) a -= (BASE << 4); \
00049 if (a >= (BASE << 3)) a -= (BASE << 3); \
00050 if (a >= (BASE << 2)) a -= (BASE << 2); \
00051 if (a >= (BASE << 1)) a -= (BASE << 1); \
00052 if (a >= BASE) a -= BASE; \
00053 } while (0)
00054 #else
00055 # define MOD(a) a %= BASE
00056 # define MOD4(a) a %= BASE
00057 #endif
00058
00059
00060 uLong ZEXPORT adler32(adler, buf, len)
00061 uLong adler;
00062 const Bytef *buf;
00063 uInt len;
00064 {
00065 unsigned long sum2;
00066 unsigned n;
00067
00068
00069 sum2 = (adler >> 16) & 0xffff;
00070 adler &= 0xffff;
00071
00072
00073 if (len == 1) {
00074 adler += buf[0];
00075 if (adler >= BASE)
00076 adler -= BASE;
00077 sum2 += adler;
00078 if (sum2 >= BASE)
00079 sum2 -= BASE;
00080 return adler | (sum2 << 16);
00081 }
00082
00083
00084 if (buf == Z_NULL)
00085 return 1L;
00086
00087
00088 if (len < 16) {
00089 while (len--) {
00090 adler += *buf++;
00091 sum2 += adler;
00092 }
00093 if (adler >= BASE)
00094 adler -= BASE;
00095 MOD4(sum2);
00096 return adler | (sum2 << 16);
00097 }
00098
00099
00100 while (len >= NMAX) {
00101 len -= NMAX;
00102 n = NMAX / 16;
00103 do {
00104 DO16(buf);
00105 buf += 16;
00106 } while (--n);
00107 MOD(adler);
00108 MOD(sum2);
00109 }
00110
00111
00112 if (len) {
00113 while (len >= 16) {
00114 len -= 16;
00115 DO16(buf);
00116 buf += 16;
00117 }
00118 while (len--) {
00119 adler += *buf++;
00120 sum2 += adler;
00121 }
00122 MOD(adler);
00123 MOD(sum2);
00124 }
00125
00126
00127 return adler | (sum2 << 16);
00128 }
00129
00130
00131 local uLong adler32_combine_(adler1, adler2, len2)
00132 uLong adler1;
00133 uLong adler2;
00134 z_off64_t len2;
00135 {
00136 unsigned long sum1;
00137 unsigned long sum2;
00138 unsigned rem;
00139
00140
00141 rem = (unsigned)(len2 % BASE);
00142 sum1 = adler1 & 0xffff;
00143 sum2 = rem * sum1;
00144 MOD(sum2);
00145 sum1 += (adler2 & 0xffff) + BASE - 1;
00146 sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
00147 if (sum1 >= BASE) sum1 -= BASE;
00148 if (sum1 >= BASE) sum1 -= BASE;
00149 if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
00150 if (sum2 >= BASE) sum2 -= BASE;
00151 return sum1 | (sum2 << 16);
00152 }
00153
00154
00155 uLong ZEXPORT adler32_combine(adler1, adler2, len2)
00156 uLong adler1;
00157 uLong adler2;
00158 z_off_t len2;
00159 {
00160 return adler32_combine_(adler1, adler2, len2);
00161 }
00162
00163 uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
00164 uLong adler1;
00165 uLong adler2;
00166 z_off64_t len2;
00167 {
00168 return adler32_combine_(adler1, adler2, len2);
00169 }