1592N/A/*
1592N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1592N/A *
1592N/A * This code is free software; you can redistribute it and/or modify it
1592N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
1592N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
1592N/A *
1592N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1592N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1592N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1592N/A * version 2 for more details (a copy is included in the LICENSE file that
1592N/A * accompanied this code).
1592N/A *
1592N/A * You should have received a copy of the GNU General Public License version
1592N/A * 2 along with this work; if not, write to the Free Software Foundation,
1592N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1592N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
1592N/A */
1592N/A
1592N/A/* adler32.c -- compute the Adler-32 checksum of a data stream
1592N/A * Copyright (C) 1995-2004 Mark Adler
1592N/A * For conditions of distribution and use, see copyright notice in zlib.h
1592N/A */
1592N/A
1592N/A/* @(#) $Id$ */
1592N/A
1592N/A#define ZLIB_INTERNAL
1592N/A#include "zlib.h"
1592N/A
1592N/A#define BASE 65521UL /* largest prime smaller than 65536 */
1592N/A#define NMAX 5552
1592N/A/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
1592N/A
1592N/A#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}
1592N/A#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);
1592N/A#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);
1592N/A#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);
1592N/A#define DO16(buf) DO8(buf,0); DO8(buf,8);
1592N/A
1592N/A/* use NO_DIVIDE if your processor does not do division in hardware */
1592N/A#ifdef NO_DIVIDE
1592N/A# define MOD(a) \
1592N/A do { \
1592N/A if (a >= (BASE << 16)) a -= (BASE << 16); \
1592N/A if (a >= (BASE << 15)) a -= (BASE << 15); \
1592N/A if (a >= (BASE << 14)) a -= (BASE << 14); \
1592N/A if (a >= (BASE << 13)) a -= (BASE << 13); \
1592N/A if (a >= (BASE << 12)) a -= (BASE << 12); \
1592N/A if (a >= (BASE << 11)) a -= (BASE << 11); \
1592N/A if (a >= (BASE << 10)) a -= (BASE << 10); \
1592N/A if (a >= (BASE << 9)) a -= (BASE << 9); \
1592N/A if (a >= (BASE << 8)) a -= (BASE << 8); \
1592N/A if (a >= (BASE << 7)) a -= (BASE << 7); \
1592N/A if (a >= (BASE << 6)) a -= (BASE << 6); \
1592N/A if (a >= (BASE << 5)) a -= (BASE << 5); \
1592N/A if (a >= (BASE << 4)) a -= (BASE << 4); \
1592N/A if (a >= (BASE << 3)) a -= (BASE << 3); \
1592N/A if (a >= (BASE << 2)) a -= (BASE << 2); \
1592N/A if (a >= (BASE << 1)) a -= (BASE << 1); \
1592N/A if (a >= BASE) a -= BASE; \
1592N/A } while (0)
1592N/A# define MOD4(a) \
1592N/A do { \
1592N/A if (a >= (BASE << 4)) a -= (BASE << 4); \
1592N/A if (a >= (BASE << 3)) a -= (BASE << 3); \
1592N/A if (a >= (BASE << 2)) a -= (BASE << 2); \
1592N/A if (a >= (BASE << 1)) a -= (BASE << 1); \
1592N/A if (a >= BASE) a -= BASE; \
1592N/A } while (0)
1592N/A#else
1592N/A# define MOD(a) a %= BASE
1592N/A# define MOD4(a) a %= BASE
1592N/A#endif
1592N/A
1592N/A/* ========================================================================= */
1592N/AuLong ZEXPORT adler32(adler, buf, len)
1592N/A uLong adler;
1592N/A const Bytef *buf;
1592N/A uInt len;
1592N/A{
1592N/A unsigned long sum2;
1592N/A unsigned n;
1592N/A
1592N/A /* split Adler-32 into component sums */
1592N/A sum2 = (adler >> 16) & 0xffff;
1592N/A adler &= 0xffff;
1592N/A
1592N/A /* in case user likes doing a byte at a time, keep it fast */
1592N/A if (len == 1) {
1592N/A adler += buf[0];
1592N/A if (adler >= BASE)
1592N/A adler -= BASE;
1592N/A sum2 += adler;
1592N/A if (sum2 >= BASE)
1592N/A sum2 -= BASE;
1592N/A return adler | (sum2 << 16);
1592N/A }
1592N/A
1592N/A /* initial Adler-32 value (deferred check for len == 1 speed) */
1592N/A if (buf == Z_NULL)
1592N/A return 1L;
1592N/A
1592N/A /* in case short lengths are provided, keep it somewhat fast */
1592N/A if (len < 16) {
1592N/A while (len--) {
1592N/A adler += *buf++;
1592N/A sum2 += adler;
1592N/A }
1592N/A if (adler >= BASE)
1592N/A adler -= BASE;
1592N/A MOD4(sum2); /* only added so many BASE's */
1592N/A return adler | (sum2 << 16);
1592N/A }
1592N/A
1592N/A /* do length NMAX blocks -- requires just one modulo operation */
1592N/A while (len >= NMAX) {
1592N/A len -= NMAX;
1592N/A n = NMAX / 16; /* NMAX is divisible by 16 */
1592N/A do {
1592N/A DO16(buf); /* 16 sums unrolled */
1592N/A buf += 16;
1592N/A } while (--n);
1592N/A MOD(adler);
1592N/A MOD(sum2);
1592N/A }
1592N/A
1592N/A /* do remaining bytes (less than NMAX, still just one modulo) */
1592N/A if (len) { /* avoid modulos if none remaining */
1592N/A while (len >= 16) {
1592N/A len -= 16;
1592N/A DO16(buf);
1592N/A buf += 16;
1592N/A }
1592N/A while (len--) {
1592N/A adler += *buf++;
1592N/A sum2 += adler;
1592N/A }
1592N/A MOD(adler);
1592N/A MOD(sum2);
1592N/A }
1592N/A
1592N/A /* return recombined sums */
1592N/A return adler | (sum2 << 16);
1592N/A}
1592N/A
1592N/A/* ========================================================================= */
1592N/AuLong ZEXPORT adler32_combine(adler1, adler2, len2)
1592N/A uLong adler1;
1592N/A uLong adler2;
1592N/A z_off_t len2;
1592N/A{
1592N/A unsigned long sum1;
1592N/A unsigned long sum2;
1592N/A unsigned rem;
1592N/A
1592N/A /* the derivation of this formula is left as an exercise for the reader */
1592N/A rem = (unsigned)(len2 % BASE);
1592N/A sum1 = adler1 & 0xffff;
1592N/A sum2 = rem * sum1;
1592N/A MOD(sum2);
1592N/A sum1 += (adler2 & 0xffff) + BASE - 1;
1592N/A sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;
1592N/A if (sum1 > BASE) sum1 -= BASE;
1592N/A if (sum1 > BASE) sum1 -= BASE;
1592N/A if (sum2 > (BASE << 1)) sum2 -= (BASE << 1);
1592N/A if (sum2 > BASE) sum2 -= BASE;
1592N/A return sum1 | (sum2 << 16);
1592N/A}