00001 // Adler32.cs - Computes Adler32 data checksum of a data stream 00002 // Copyright (C) 2001 Mike Krueger 00003 // 00004 // This file was translated from java, it was part of the GNU Classpath 00005 // Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. 00006 // 00007 // This program is free software; you can redistribute it and/or 00008 // modify it under the terms of the GNU General Public License 00009 // as published by the Free Software Foundation; either version 2 00010 // of the License, or (at your option) any later version. 00011 // 00012 // This program is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // GNU General Public License for more details. 00016 // 00017 // You should have received a copy of the GNU General Public License 00018 // along with this program; if not, write to the Free Software 00019 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00020 // 00021 // Linking this library statically or dynamically with other modules is 00022 // making a combined work based on this library. Thus, the terms and 00023 // conditions of the GNU General Public License cover the whole 00024 // combination. 00025 // 00026 // As a special exception, the copyright holders of this library give you 00027 // permission to link this library with independent modules to produce an 00028 // executable, regardless of the license terms of these independent 00029 // modules, and to copy and distribute the resulting executable under 00030 // terms of your choice, provided that you also meet, for each linked 00031 // independent module, the terms and conditions of the license of that 00032 // module. An independent module is a module which is not derived from 00033 // or based on this library. If you modify this library, you may extend 00034 // this exception to your version of the library, but you are not 00035 // obligated to do so. If you do not wish to do so, delete this 00036 // exception statement from your version. 00037 00038 using System; 00039 00040 namespace ICSharpCode.SharpZipLib.Checksums 00041 { 00042 00089 public sealed class Adler32 : IChecksum 00090 { 00094 readonly static uint BASE = 65521; 00095 00096 uint checksum; 00097 00101 public long Value { 00102 get { 00103 return checksum; 00104 } 00105 } 00106 00111 public Adler32() 00112 { 00113 Reset(); 00114 } 00115 00119 public void Reset() 00120 { 00121 checksum = 1; 00122 } 00123 00130 public void Update(int bval) 00131 { 00132 // We could make a length 1 byte array and call update again, but I 00133 // would rather not have that overhead 00134 uint s1 = checksum & 0xFFFF; 00135 uint s2 = checksum >> 16; 00136 00137 s1 = (s1 + ((uint)bval & 0xFF)) % BASE; 00138 s2 = (s1 + s2) % BASE; 00139 00140 checksum = (s2 << 16) + s1; 00141 } 00142 00149 public void Update(byte[] buffer) 00150 { 00151 Update(buffer, 0, buffer.Length); 00152 } 00153 00166 public void Update(byte[] buf, int off, int len) 00167 { 00168 if (buf == null) { 00169 throw new ArgumentNullException("buf"); 00170 } 00171 00172 if (off < 0 || len < 0 || off + len > buf.Length) { 00173 throw new ArgumentOutOfRangeException(); 00174 } 00175 00176 //(By Per Bothner) 00177 uint s1 = checksum & 0xFFFF; 00178 uint s2 = checksum >> 16; 00179 00180 while (len > 0) { 00181 // We can defer the modulo operation: 00182 // s1 maximally grows from 65521 to 65521 + 255 * 3800 00183 // s2 maximally grows by 3800 * median(s1) = 2090079800 < 2^31 00184 int n = 3800; 00185 if (n > len) { 00186 n = len; 00187 } 00188 len -= n; 00189 while (--n >= 0) { 00190 s1 = s1 + (uint)(buf[off++] & 0xFF); 00191 s2 = s2 + s1; 00192 } 00193 s1 %= BASE; 00194 s2 %= BASE; 00195 } 00196 00197 checksum = (s2 << 16) | s1; 00198 } 00199 } 00200 }