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