Path: blob/master/src/java.base/share/native/libzip/zlib/zadler32.c
41153 views
/*1* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.2*3* This code is free software; you can redistribute it and/or modify it4* under the terms of the GNU General Public License version 2 only, as5* published by the Free Software Foundation. Oracle designates this6* particular file as subject to the "Classpath" exception as provided7* by Oracle in the LICENSE file that accompanied this code.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/* adler32.c -- compute the Adler-32 checksum of a data stream25* Copyright (C) 1995-2011, 2016 Mark Adler26* For conditions of distribution and use, see copyright notice in zlib.h27*/2829/* @(#) $Id$ */3031#include "zutil.h"3233local uLong adler32_combine_ OF((uLong adler1, uLong adler2, z_off64_t len2));3435#define BASE 65521U /* largest prime smaller than 65536 */36#define NMAX 555237/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */3839#define DO1(buf,i) {adler += (buf)[i]; sum2 += adler;}40#define DO2(buf,i) DO1(buf,i); DO1(buf,i+1);41#define DO4(buf,i) DO2(buf,i); DO2(buf,i+2);42#define DO8(buf,i) DO4(buf,i); DO4(buf,i+4);43#define DO16(buf) DO8(buf,0); DO8(buf,8);4445/* use NO_DIVIDE if your processor does not do division in hardware --46try it both ways to see which is faster */47#ifdef NO_DIVIDE48/* note that this assumes BASE is 65521, where 65536 % 65521 == 1549(thank you to John Reiser for pointing this out) */50# define CHOP(a) \51do { \52unsigned long tmp = a >> 16; \53a &= 0xffffUL; \54a += (tmp << 4) - tmp; \55} while (0)56# define MOD28(a) \57do { \58CHOP(a); \59if (a >= BASE) a -= BASE; \60} while (0)61# define MOD(a) \62do { \63CHOP(a); \64MOD28(a); \65} while (0)66# define MOD63(a) \67do { /* this assumes a is not negative */ \68z_off64_t tmp = a >> 32; \69a &= 0xffffffffL; \70a += (tmp << 8) - (tmp << 5) + tmp; \71tmp = a >> 16; \72a &= 0xffffL; \73a += (tmp << 4) - tmp; \74tmp = a >> 16; \75a &= 0xffffL; \76a += (tmp << 4) - tmp; \77if (a >= BASE) a -= BASE; \78} while (0)79#else80# define MOD(a) a %= BASE81# define MOD28(a) a %= BASE82# define MOD63(a) a %= BASE83#endif8485/* ========================================================================= */86uLong ZEXPORT adler32_z(adler, buf, len)87uLong adler;88const Bytef *buf;89z_size_t len;90{91unsigned long sum2;92unsigned n;9394/* split Adler-32 into component sums */95sum2 = (adler >> 16) & 0xffff;96adler &= 0xffff;9798/* in case user likes doing a byte at a time, keep it fast */99if (len == 1) {100adler += buf[0];101if (adler >= BASE)102adler -= BASE;103sum2 += adler;104if (sum2 >= BASE)105sum2 -= BASE;106return adler | (sum2 << 16);107}108109/* initial Adler-32 value (deferred check for len == 1 speed) */110if (buf == Z_NULL)111return 1L;112113/* in case short lengths are provided, keep it somewhat fast */114if (len < 16) {115while (len--) {116adler += *buf++;117sum2 += adler;118}119if (adler >= BASE)120adler -= BASE;121MOD28(sum2); /* only added so many BASE's */122return adler | (sum2 << 16);123}124125/* do length NMAX blocks -- requires just one modulo operation */126while (len >= NMAX) {127len -= NMAX;128n = NMAX / 16; /* NMAX is divisible by 16 */129do {130DO16(buf); /* 16 sums unrolled */131buf += 16;132} while (--n);133MOD(adler);134MOD(sum2);135}136137/* do remaining bytes (less than NMAX, still just one modulo) */138if (len) { /* avoid modulos if none remaining */139while (len >= 16) {140len -= 16;141DO16(buf);142buf += 16;143}144while (len--) {145adler += *buf++;146sum2 += adler;147}148MOD(adler);149MOD(sum2);150}151152/* return recombined sums */153return adler | (sum2 << 16);154}155156/* ========================================================================= */157uLong ZEXPORT adler32(adler, buf, len)158uLong adler;159const Bytef *buf;160uInt len;161{162return adler32_z(adler, buf, len);163}164165/* ========================================================================= */166local uLong adler32_combine_(adler1, adler2, len2)167uLong adler1;168uLong adler2;169z_off64_t len2;170{171unsigned long sum1;172unsigned long sum2;173unsigned rem;174175/* for negative len, return invalid adler32 as a clue for debugging */176if (len2 < 0)177return 0xffffffffUL;178179/* the derivation of this formula is left as an exercise for the reader */180MOD63(len2); /* assumes len2 >= 0 */181rem = (unsigned)len2;182sum1 = adler1 & 0xffff;183sum2 = rem * sum1;184MOD(sum2);185sum1 += (adler2 & 0xffff) + BASE - 1;186sum2 += ((adler1 >> 16) & 0xffff) + ((adler2 >> 16) & 0xffff) + BASE - rem;187if (sum1 >= BASE) sum1 -= BASE;188if (sum1 >= BASE) sum1 -= BASE;189if (sum2 >= ((unsigned long)BASE << 1)) sum2 -= ((unsigned long)BASE << 1);190if (sum2 >= BASE) sum2 -= BASE;191return sum1 | (sum2 << 16);192}193194/* ========================================================================= */195uLong ZEXPORT adler32_combine(adler1, adler2, len2)196uLong adler1;197uLong adler2;198z_off_t len2;199{200return adler32_combine_(adler1, adler2, len2);201}202203uLong ZEXPORT adler32_combine64(adler1, adler2, len2)204uLong adler1;205uLong adler2;206z_off64_t len2;207{208return adler32_combine_(adler1, adler2, len2);209}210211212