jdk/src/java.base/share/native/libzip/zlib-1.2.8/zadler32.c
changeset 44221 a26be46f6bac
parent 44170 40f9a2b0c304
parent 43915 4a79ad46e578
child 44223 14252cb702f5
equal deleted inserted replaced
44170:40f9a2b0c304 44221:a26be46f6bac
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.  Oracle designates this
       
     7  * particular file as subject to the "Classpath" exception as provided
       
     8  * by Oracle in the LICENSE file that accompanied this code.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13  * version 2 for more details (a copy is included in the LICENSE file that
       
    14  * accompanied this code).
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License version
       
    17  * 2 along with this work; if not, write to the Free Software Foundation,
       
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19  *
       
    20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    21  * or visit www.oracle.com if you need additional information or have any
       
    22  * questions.
       
    23  */
       
    24 
       
    25 /* adler32.c -- compute the Adler-32 checksum of a data stream
       
    26  * Copyright (C) 1995-2011 Mark Adler
       
    27  * For conditions of distribution and use, see copyright notice in zlib.h
       
    28  */
       
    29 
       
    30 /* @(#) $Id$ */
       
    31 
       
    32 #include "zutil.h"
       
    33 
       
    34 #define local static
       
    35 
       
    36 local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));
       
    37 
       
    38 #define BASE 65521      /* largest prime smaller than 65536 */
       
    39 #define NMAX 5552
       
    40 /* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
       
    41 
       
    42 #define DO1(buf,i)  {adler += (buf)[i]; sum2 += adler;}
       
    43 #define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
       
    44 #define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
       
    45 #define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
       
    46 #define DO16(buf)   DO8(buf,0); DO8(buf,8);
       
    47 
       
    48 /* use NO_DIVIDE if your processor does not do division in hardware --
       
    49    try it both ways to see which is faster */
       
    50 #ifdef NO_DIVIDE
       
    51 /* note that this assumes BASE is 65521, where 65536 % 65521 == 15
       
    52    (thank you to John Reiser for pointing this out) */
       
    53 #  define CHOP(a) \
       
    54     do { \
       
    55         unsigned long tmp = a >> 16; \
       
    56         a &= 0xffffUL; \
       
    57         a += (tmp << 4) - tmp; \
       
    58     } while (0)
       
    59 #  define MOD28(a) \
       
    60     do { \
       
    61         CHOP(a); \
       
    62         if (a >= BASE) a -= BASE; \
       
    63     } while (0)
       
    64 #  define MOD(a) \
       
    65     do { \
       
    66         CHOP(a); \
       
    67         MOD28(a); \
       
    68     } while (0)
       
    69 #  define MOD63(a) \
       
    70     do { /* this assumes a is not negative */ \
       
    71         z_off64_t tmp = a >> 32; \
       
    72         a &= 0xffffffffL; \
       
    73         a += (tmp << 8) - (tmp << 5) + tmp; \
       
    74         tmp = a >> 16; \
       
    75         a &= 0xffffL; \
       
    76         a += (tmp << 4) - tmp; \
       
    77         tmp = a >> 16; \
       
    78         a &= 0xffffL; \
       
    79         a += (tmp << 4) - tmp; \
       
    80         if (a >= BASE) a -= BASE; \
       
    81     } while (0)
       
    82 #else
       
    83 #  define MOD(a) a %= BASE
       
    84 #  define MOD28(a) a %= BASE
       
    85 #  define MOD63(a) a %= BASE
       
    86 #endif
       
    87 
       
    88 /* ========================================================================= */
       
    89 uLong ZEXPORT adler32(adler, buf, len)
       
    90     uLong adler;
       
    91     const Bytef *buf;
       
    92     uInt len;
       
    93 {
       
    94     unsigned long sum2;
       
    95     unsigned n;
       
    96 
       
    97     /* split Adler-32 into component sums */
       
    98     sum2 = (adler >> 16) & 0xffff;
       
    99     adler &= 0xffff;
       
   100 
       
   101     /* in case user likes doing a byte at a time, keep it fast */
       
   102     if (len == 1) {
       
   103         adler += buf[0];
       
   104         if (adler >= BASE)
       
   105             adler -= BASE;
       
   106         sum2 += adler;
       
   107         if (sum2 >= BASE)
       
   108             sum2 -= BASE;
       
   109         return adler | (sum2 << 16);
       
   110     }
       
   111 
       
   112     /* initial Adler-32 value (deferred check for len == 1 speed) */
       
   113     if (buf == Z_NULL)
       
   114         return 1L;
       
   115 
       
   116     /* in case short lengths are provided, keep it somewhat fast */
       
   117     if (len < 16) {
       
   118         while (len--) {
       
   119             adler += *buf++;
       
   120             sum2 += adler;
       
   121         }
       
   122         if (adler >= BASE)
       
   123             adler -= BASE;
       
   124         MOD28(sum2);            /* only added so many BASE's */
       
   125         return adler | (sum2 << 16);
       
   126     }
       
   127 
       
   128     /* do length NMAX blocks -- requires just one modulo operation */
       
   129     while (len >= NMAX) {
       
   130         len -= NMAX;
       
   131         n = NMAX / 16;          /* NMAX is divisible by 16 */
       
   132         do {
       
   133             DO16(buf);          /* 16 sums unrolled */
       
   134             buf += 16;
       
   135         } while (--n);
       
   136         MOD(adler);
       
   137         MOD(sum2);
       
   138     }
       
   139 
       
   140     /* do remaining bytes (less than NMAX, still just one modulo) */
       
   141     if (len) {                  /* avoid modulos if none remaining */
       
   142         while (len >= 16) {
       
   143             len -= 16;
       
   144             DO16(buf);
       
   145             buf += 16;
       
   146         }
       
   147         while (len--) {
       
   148             adler += *buf++;
       
   149             sum2 += adler;
       
   150         }
       
   151         MOD(adler);
       
   152         MOD(sum2);
       
   153     }
       
   154 
       
   155     /* return recombined sums */
       
   156     return adler | (sum2 << 16);
       
   157 }
       
   158 
       
   159 /* ========================================================================= */
       
   160 local uLong adler32_combine_(adler1, adler2, len2)
       
   161     uLong adler1;
       
   162     uLong adler2;
       
   163     z_off64_t len2;
       
   164 {
       
   165     unsigned long sum1;
       
   166     unsigned long sum2;
       
   167     unsigned rem;
       
   168 
       
   169     /* for negative len, return invalid adler32 as a clue for debugging */
       
   170     if (len2 < 0)
       
   171         return 0xffffffffUL;
       
   172 
       
   173     /* the derivation of this formula is left as an exercise for the reader */
       
   174     MOD63(len2);                /* assumes len2 >= 0 */
       
   175     rem = (unsigned)len2;
       
   176     sum1 = adler1 & 0xffff;
       
   177     sum2 = rem * sum1;
       
   178     MOD(sum2);
       
   179     sum1 += (adler2 & 0xffff) + BASE - 1;
       
   180     sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
       
   181     if (sum1 >= BASE) sum1 -= BASE;
       
   182     if (sum1 >= BASE) sum1 -= BASE;
       
   183     if (sum2 >= (BASE << 1)) sum2 -= (BASE << 1);
       
   184     if (sum2 >= BASE) sum2 -= BASE;
       
   185     return sum1 | (sum2 << 16);
       
   186 }
       
   187 
       
   188 /* ========================================================================= */
       
   189 uLong ZEXPORT adler32_combine(adler1, adler2, len2)
       
   190     uLong adler1;
       
   191     uLong adler2;
       
   192     z_off_t len2;
       
   193 {
       
   194     return adler32_combine_(adler1, adler2, len2);
       
   195 }
       
   196 
       
   197 uLong ZEXPORT adler32_combine64(adler1, adler2, len2)
       
   198     uLong adler1;
       
   199     uLong adler2;
       
   200     z_off64_t len2;
       
   201 {
       
   202     return adler32_combine_(adler1, adler2, len2);
       
   203 }