1 /* 2 * Copyright 2012 The Netty Project 3 * 4 * The Netty Project licenses this file to you under the Apache License, 5 * version 2.0 (the "License"); you may not use this file except in compliance 6 * with the License. You may obtain a copy of the License at: 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations 14 * under the License. 15 */ 16 /* 17 Copyright (c) 2000,2001,2002,2003 ymnk, JCraft,Inc. All rights reserved. 18 19 Redistribution and use in source and binary forms, with or without 20 modification, are permitted provided that the following conditions are met: 21 22 1. Redistributions of source code must retain the above copyright notice, 23 this list of conditions and the following disclaimer. 24 25 2. Redistributions in binary form must reproduce the above copyright 26 notice, this list of conditions and the following disclaimer in 27 the documentation and/or other materials provided with the distribution. 28 29 3. The names of the authors may not be used to endorse or promote products 30 derived from this software without specific prior written permission. 31 32 THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, 33 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 34 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, 35 INC. OR ANY CONTRIBUTORS TO THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, 36 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 37 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 38 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 39 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 40 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 41 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 42 */ 43 /* 44 * This program is based on zlib-1.1.3, so all credit should go authors 45 * Jean-loup Gailly(jloup@gzip.org) and Mark Adler(madler@alumni.caltech.edu) 46 * and contributors of zlib. 47 */ 48 49 package org.jboss.netty.util.internal.jzlib; 50 51 final class Adler32 { 52 53 // largest prime smaller than 65536 54 private static final int BASE = 65521; 55 // NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 56 private static final int NMAX = 5552; 57 58 static long adler32(long adler, byte[] buf, int index, int len) { 59 if (buf == null) { 60 return 1L; 61 } 62 63 long s1 = adler & 0xffff; 64 long s2 = adler >> 16 & 0xffff; 65 int k; 66 67 while (len > 0) { 68 k = len < NMAX? len : NMAX; 69 len -= k; 70 while (k >= 16) { 71 s1 += buf[index ++] & 0xff; 72 s2 += s1; 73 s1 += buf[index ++] & 0xff; 74 s2 += s1; 75 s1 += buf[index ++] & 0xff; 76 s2 += s1; 77 s1 += buf[index ++] & 0xff; 78 s2 += s1; 79 s1 += buf[index ++] & 0xff; 80 s2 += s1; 81 s1 += buf[index ++] & 0xff; 82 s2 += s1; 83 s1 += buf[index ++] & 0xff; 84 s2 += s1; 85 s1 += buf[index ++] & 0xff; 86 s2 += s1; 87 s1 += buf[index ++] & 0xff; 88 s2 += s1; 89 s1 += buf[index ++] & 0xff; 90 s2 += s1; 91 s1 += buf[index ++] & 0xff; 92 s2 += s1; 93 s1 += buf[index ++] & 0xff; 94 s2 += s1; 95 s1 += buf[index ++] & 0xff; 96 s2 += s1; 97 s1 += buf[index ++] & 0xff; 98 s2 += s1; 99 s1 += buf[index ++] & 0xff; 100 s2 += s1; 101 s1 += buf[index ++] & 0xff; 102 s2 += s1; 103 k -= 16; 104 } 105 if (k != 0) { 106 do { 107 s1 += buf[index ++] & 0xff; 108 s2 += s1; 109 } while (-- k != 0); 110 } 111 s1 %= BASE; 112 s2 %= BASE; 113 } 114 return s2 << 16 | s1; 115 } 116 117 private Adler32() { 118 // Utility class 119 } 120 }