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