Home | History | Annotate | Line # | Download | only in zlib
adler32.c revision 1.1
      1  1.1  christos /*	$NetBSD: adler32.c,v 1.1 2006/01/14 20:10:24 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /* adler32.c -- compute the Adler-32 checksum of a data stream
      4  1.1  christos  * Copyright (C) 1995-2004 Mark Adler
      5  1.1  christos  * For conditions of distribution and use, see copyright notice in zlib.h
      6  1.1  christos  */
      7  1.1  christos 
      8  1.1  christos /* @(#) Id */
      9  1.1  christos 
     10  1.1  christos #define ZLIB_INTERNAL
     11  1.1  christos #include "zlib.h"
     12  1.1  christos 
     13  1.1  christos #define BASE 65521UL    /* largest prime smaller than 65536 */
     14  1.1  christos #define NMAX 5552
     15  1.1  christos /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
     16  1.1  christos 
     17  1.1  christos #define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
     18  1.1  christos #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
     19  1.1  christos #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
     20  1.1  christos #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
     21  1.1  christos #define DO16(buf)   DO8(buf,0); DO8(buf,8);
     22  1.1  christos 
     23  1.1  christos /* use NO_DIVIDE if your processor does not do division in hardware */
     24  1.1  christos #ifdef NO_DIVIDE
     25  1.1  christos #  define MOD(a) \
     26  1.1  christos     do { \
     27  1.1  christos         if (a >= (BASE << 16)) a -= (BASE << 16); \
     28  1.1  christos         if (a >= (BASE << 15)) a -= (BASE << 15); \
     29  1.1  christos         if (a >= (BASE << 14)) a -= (BASE << 14); \
     30  1.1  christos         if (a >= (BASE << 13)) a -= (BASE << 13); \
     31  1.1  christos         if (a >= (BASE << 12)) a -= (BASE << 12); \
     32  1.1  christos         if (a >= (BASE << 11)) a -= (BASE << 11); \
     33  1.1  christos         if (a >= (BASE << 10)) a -= (BASE << 10); \
     34  1.1  christos         if (a >= (BASE << 9)) a -= (BASE << 9); \
     35  1.1  christos         if (a >= (BASE << 8)) a -= (BASE << 8); \
     36  1.1  christos         if (a >= (BASE << 7)) a -= (BASE << 7); \
     37  1.1  christos         if (a >= (BASE << 6)) a -= (BASE << 6); \
     38  1.1  christos         if (a >= (BASE << 5)) a -= (BASE << 5); \
     39  1.1  christos         if (a >= (BASE << 4)) a -= (BASE << 4); \
     40  1.1  christos         if (a >= (BASE << 3)) a -= (BASE << 3); \
     41  1.1  christos         if (a >= (BASE << 2)) a -= (BASE << 2); \
     42  1.1  christos         if (a >= (BASE << 1)) a -= (BASE << 1); \
     43  1.1  christos         if (a >= BASE) a -= BASE; \
     44  1.1  christos     } while (0)
     45  1.1  christos #  define MOD4(a) \
     46  1.1  christos     do { \
     47  1.1  christos         if (a >= (BASE << 4)) a -= (BASE << 4); \
     48  1.1  christos         if (a >= (BASE << 3)) a -= (BASE << 3); \
     49  1.1  christos         if (a >= (BASE << 2)) a -= (BASE << 2); \
     50  1.1  christos         if (a >= (BASE << 1)) a -= (BASE << 1); \
     51  1.1  christos         if (a >= BASE) a -= BASE; \
     52  1.1  christos     } while (0)
     53  1.1  christos #else
     54  1.1  christos #  define MOD(a) a %= BASE
     55  1.1  christos #  define MOD4(a) a %= BASE
     56  1.1  christos #endif
     57  1.1  christos 
     58  1.1  christos /* ========================================================================= */
     59  1.1  christos uLong ZEXPORT adler32(adler, buf, len)
     60  1.1  christos     uLong adler;
     61  1.1  christos     const Bytef *buf;
     62  1.1  christos     uInt len;
     63  1.1  christos {
     64  1.1  christos     unsigned long sum2;
     65  1.1  christos     unsigned n;
     66  1.1  christos 
     67  1.1  christos     /* split Adler-32 into component sums */
     68  1.1  christos     sum2 = (adler >> 16) & 0xffff;
     69  1.1  christos     adler &= 0xffff;
     70  1.1  christos 
     71  1.1  christos     /* in case user likes doing a byte at a time, keep it fast */
     72  1.1  christos     if (len == 1) {
     73  1.1  christos         adler += buf[0];
     74  1.1  christos         if (adler >= BASE)
     75  1.1  christos             adler -= BASE;
     76  1.1  christos         sum2 += adler;
     77  1.1  christos         if (sum2 >= BASE)
     78  1.1  christos             sum2 -= BASE;
     79  1.1  christos         return adler | (sum2 << 16);
     80  1.1  christos     }
     81  1.1  christos 
     82  1.1  christos     /* initial Adler-32 value (deferred check for len == 1 speed) */
     83  1.1  christos     if (buf == Z_NULL)
     84  1.1  christos         return 1L;
     85  1.1  christos 
     86  1.1  christos     /* in case short lengths are provided, keep it somewhat fast */
     87  1.1  christos     if (len < 16) {
     88  1.1  christos         while (len--) {
     89  1.1  christos             adler += *buf++;
     90  1.1  christos             sum2 += adler;
     91  1.1  christos         }
     92  1.1  christos         if (adler >= BASE)
     93  1.1  christos             adler -= BASE;
     94  1.1  christos         MOD4(sum2);             /* only added so many BASE's */
     95  1.1  christos         return adler | (sum2 << 16);
     96  1.1  christos     }
     97  1.1  christos 
     98  1.1  christos     /* do length NMAX blocks -- requires just one modulo operation */
     99  1.1  christos     while (len >= NMAX) {
    100  1.1  christos         len -= NMAX;
    101  1.1  christos         n = NMAX / 16;          /* NMAX is divisible by 16 */
    102  1.1  christos         do {
    103  1.1  christos             DO16(buf);          /* 16 sums unrolled */
    104  1.1  christos             buf += 16;
    105  1.1  christos         } while (--n);
    106  1.1  christos         MOD(adler);
    107  1.1  christos         MOD(sum2);
    108  1.1  christos     }
    109  1.1  christos 
    110  1.1  christos     /* do remaining bytes (less than NMAX, still just one modulo) */
    111  1.1  christos     if (len) {                  /* avoid modulos if none remaining */
    112  1.1  christos         while (len >= 16) {
    113  1.1  christos             len -= 16;
    114  1.1  christos             DO16(buf);
    115  1.1  christos             buf += 16;
    116  1.1  christos         }
    117  1.1  christos         while (len--) {
    118  1.1  christos             adler += *buf++;
    119  1.1  christos             sum2 += adler;
    120  1.1  christos         }
    121  1.1  christos         MOD(adler);
    122  1.1  christos         MOD(sum2);
    123  1.1  christos     }
    124  1.1  christos 
    125  1.1  christos     /* return recombined sums */
    126  1.1  christos     return adler | (sum2 << 16);
    127  1.1  christos }
    128  1.1  christos 
    129  1.1  christos /* ========================================================================= */
    130  1.1  christos uLong ZEXPORT adler32_combine(adler1, adler2, len2)
    131  1.1  christos     uLong adler1;
    132  1.1  christos     uLong adler2;
    133  1.1  christos     z_off_t len2;
    134  1.1  christos {
    135  1.1  christos     unsigned long sum1;
    136  1.1  christos     unsigned long sum2;
    137  1.1  christos     unsigned rem;
    138  1.1  christos 
    139  1.1  christos     /* the derivation of this formula is left as an exercise for the reader */
    140  1.1  christos     rem = (unsigned)(len2 % BASE);
    141  1.1  christos     sum1 = adler1 & 0xffff;
    142  1.1  christos     sum2 = rem * sum1;
    143  1.1  christos     MOD(sum2);
    144  1.1  christos     sum1 += (adler2 & 0xffff) + BASE - 1;
    145  1.1  christos     sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
    146  1.1  christos     if (sum1 > BASE) sum1 -= BASE;
    147  1.1  christos     if (sum1 > BASE) sum1 -= BASE;
    148  1.1  christos     if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
    149  1.1  christos     if (sum2 > BASE) sum2 -= BASE;
    150  1.1  christos     return sum1 | (sum2 << 16);
    151  1.1  christos }
    152