1:   // CRC32.cs - Computes CRC32 data checksum of a data stream
2:   // Copyright (C) 2001 Mike Krueger
3:   //
4:   // This file was translated from java, it was part of the GNU Classpath
5:   // Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
6:   //
7:   // This program is free software; you can redistribute it and/or
8:   // modify it under the terms of the GNU General Public License
9:   // as published by the Free Software Foundation; either version 2
10:   // of the License, or (at your option) any later version.
11:   //
12:   // This program is distributed in the hope that it will be useful,
13:   // but WITHOUT ANY WARRANTY; without even the implied warranty of
14:   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15:   // GNU General Public License for more details.
16:   //
17:   // You should have received a copy of the GNU General Public License
18:   // along with this program; if not, write to the Free Software
19:   // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20:   //
21:   // Linking this library statically or dynamically with other modules is
22:   // making a combined work based on this library.  Thus, the terms and
23:   // conditions of the GNU General Public License cover the whole
24:   // combination.
25:   // 
26:   // As a special exception, the copyright holders of this library give you
27:   // permission to link this library with independent modules to produce an
28:   // executable, regardless of the license terms of these independent
29:   // modules, and to copy and distribute the resulting executable under
30:   // terms of your choice, provided that you also meet, for each linked
31:   // independent module, the terms and conditions of the license of that
32:   // module.  An independent module is a module which is not derived from
33:   // or based on this library.  If you modify this library, you may extend
34:   // this exception to your version of the library, but you are not
35:   // obligated to do so.  If you do not wish to do so, delete this
36:   // exception statement from your version.
37:  
38:   using System;
39:  
40:   namespace ICSharpCode.SharpZipLib.Checksums {
41:       
42:       /// <summary>
43:       /// Generate a table for a byte-wise 32-bit CRC calculation on the polynomial:
44:       /// x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1.
45:       ///
46:       /// Polynomials over GF(2) are represented in binary, one bit per coefficient,
47:       /// with the lowest powers in the most significant bit.  Then adding polynomials
48:       /// is just exclusive-or, and multiplying a polynomial by x is a right shift by
49:       /// one.  If we call the above polynomial p, and represent a byte as the
50:       /// polynomial q, also with the lowest power in the most significant bit (so the
51:       /// byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p,
52:       /// where a mod b means the remainder after dividing a by b.
53:       ///
54:       /// This calculation is done using the shift-register method of multiplying and
55:       /// taking the remainder.  The register is initialized to zero, and for each
56:       /// incoming bit, x^32 is added mod p to the register if the bit is a one (where
57:       /// x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by
58:       /// x (which is shifting right by one and adding x^32 mod p if the bit shifted
59:       /// out is a one).  We start with the highest power (least significant bit) of
60:       /// q and repeat for all eight bits of q.
61:       ///
62:       /// The table is simply the CRC of all possible eight bit values.  This is all
63:       /// the information needed to generate CRC's on data a byte at a time for all
64:       /// combinations of CRC register values and incoming bytes.
65:       /// </summary>
66:       public sealed class Crc32 : IChecksum
67:       {
68:           readonly static uint CrcSeed 0xFFFFFFFF;
69:           
70:           readonly static uint[] CrcTable new uint[] {
71:               0x000000000x770730960xEE0E612C0x990951BA0x076DC419,
72:               0x706AF48F0xE963A5350x9E6495A30x0EDB88320x79DCB8A4,
73:               0xE0D5E91E0x97D2D9880x09B64C2B0x7EB17CBD0xE7B82D07,
74:               0x90BF1D910x1DB710640x6AB020F20xF3B971480x84BE41DE,
75:               0x1ADAD47D0x6DDDE4EB0xF4D4B5510x83D385C70x136C9856,
76:               0x646BA8C00xFD62F97A0x8A65C9EC0x14015C4F0x63066CD9,
77:               0xFA0F3D630x8D080DF50x3B6E20C80x4C69105E0xD56041E4,
78:               0xA26771720x3C03E4D10x4B04D4470xD20D85FD0xA50AB56B,
79:               0x35B5A8FA0x42B2986C0xDBBBC9D60xACBCF9400x32D86CE3,
80:               0x45DF5C750xDCD60DCF0xABD13D590x26D930AC0x51DE003A,
81:               0xC8D751800xBFD061160x21B4F4B50x56B3C4230xCFBA9599,
82:               0xB8BDA50F0x2802B89E0x5F0588080xC60CD9B20xB10BE924,
83:               0x2F6F7C870x58684C110xC1611DAB0xB6662D3D0x76DC4190,
84:               0x01DB71060x98D220BC0xEFD5102A0x71B185890x06B6B51F,
85:               0x9FBFE4A50xE8B8D4330x7807C9A20x0F00F9340x9609A88E,
86:               0xE10E98180x7F6A0DBB0x086D3D2D0x91646C970xE6635C01,
87:               0x6B6B51F40x1C6C61620x856530D80xF262004E0x6C0695ED,
88:               0x1B01A57B0x8208F4C10xF50FC4570x65B0D9C60x12B7E950,
89:               0x8BBEB8EA0xFCB9887C0x62DD1DDF0x15DA2D490x8CD37CF3,
90:               0xFBD44C650x4DB261580x3AB551CE0xA3BC00740xD4BB30E2,
91:               0x4ADFA5410x3DD895D70xA4D1C46D0xD3D6F4FB0x4369E96A,
92:               0x346ED9FC0xAD6788460xDA60B8D00x44042D730x33031DE5,
93:               0xAA0A4C5F0xDD0D7CC90x5005713C0x270241AA0xBE0B1010,
94:               0xC90C20860x5768B5250x206F85B30xB966D4090xCE61E49F,
95:               0x5EDEF90E0x29D9C9980xB0D098220xC7D7A8B40x59B33D17,
96:               0x2EB40D810xB7BD5C3B0xC0BA6CAD0xEDB883200x9ABFB3B6,
97:               0x03B6E20C0x74B1D29A0xEAD547390x9DD277AF0x04DB2615,
98:               0x73DC16830xE3630B120x94643B840x0D6D6A3E0x7A6A5AA8,
99:               0xE40ECF0B0x9309FF9D0x0A00AE270x7D079EB10xF00F9344
100:               0x8708A3D20x1E01F2680x6906C2FE0xF762575D0x806567CB,
101:               0x196C36710x6E6B06E70xFED41B760x89D32BE00x10DA7A5A,
102:               0x67DD4ACC0xF9B9DF6F0x8EBEEFF90x17B7BE430x60B08ED5,
103:               0xD6D6A3E80xA1D1937E0x38D8C2C40x4FDFF2520xD1BB67F1,
104:               0xA6BC57670x3FB506DD0x48B2364B0xD80D2BDA0xAF0A1B4C,
105:               0x36034AF60x41047A600xDF60EFC30xA867DF550x316E8EEF,
106:               0x4669BE790xCB61B38C0xBC66831A0x256FD2A00x5268E236,
107:               0xCC0C77950xBB0B47030x220216B90x5505262F0xC5BA3BBE,
108:               0xB2BD0B280x2BB45A920x5CB36A040xC2D7FFA70xB5D0CF31,
109:               0x2CD99E8B0x5BDEAE1D0x9B64C2B00xEC63F2260x756AA39C,
110:               0x026D930A0x9C0906A90xEB0E363F0x720767850x05005713,
111:               0x95BF4A820xE2B87A140x7BB12BAE0x0CB61B380x92D28E9B,
112:               0xE5D5BE0D0x7CDCEFB70x0BDBDF210x86D3D2D40xF1D4E242,
113:               0x68DDB3F80x1FDA836E0x81BE16CD0xF6B9265B0x6FB077E1,
114:               0x18B747770x88085AE60xFF0F6A700x66063BCA0x11010B5C,
115:               0x8F659EFF0xF862AE690x616BFFD30x166CCF450xA00AE278,
116:               0xD70DD2EE0x4E0483540x3903B3C20xA76726610xD06016F7,
117:               0x4969474D0x3E6E77DB0xAED16A4A0xD9D65ADC0x40DF0B66,
118:               0x37D83BF00xA9BCAE530xDEBB9EC50x47B2CF7F0x30B5FFE9,
119:               0xBDBDF21C0xCABAC28A0x53B393300x24B4A3A60xBAD03605,
120:               0xCDD706930x54DE57290x23D967BF0xB3667A2E0xC4614AB8,
121:               0x5D681B020x2A6F2B940xB40BBE370xC30C8EA10x5A05DF1B
122:               0x2D02EF8D
123:           };
124:           
125:           /// <summary>
126:           /// The crc data checksum so far.
127:           /// </summary>
128:           uint crc 0;
129:           
130:           /// <summary>
131:           /// Returns the CRC32 data checksum computed so far.
132:           /// </summary>
133:           public long Value {
134:               get {
135:                   return (long)crc;
136:               }
137:           }
138:           
139:           /// <summary>
140:           /// Resets the CRC32 data checksum as if no update was ever called.
141:           /// </summary>
142:           public void Reset() 
143:           
144:               crc 0
145:           }
146:           
147:           /// <summary>
148:           /// Updates the checksum with the int bval.
149:           /// </summary>
150:           /// <param name = "bval">
151:           /// the byte is taken as the lower 8 bits of bval
152:           /// </param>
153:           public void Update(int bval)
154:           {
155:               crc ^= CrcSeed;
156:               crc  CrcTable[(crc bval) & 0xFF] ^ (crc >> 8);
157:               crc ^= CrcSeed;
158:           }
159:           
160:           /// <summary>
161:           /// Updates the checksum with the bytes taken from the array.
162:           /// </summary>
163:           /// <param name="buffer">
164:           /// buffer an array of bytes
165:           /// </param>
166:           public void Update(byte[] buffer)
167:           {
168:               Update(buffer0buffer.Length);
169:           }
170:           
171:           /// <summary>
172:           /// Adds the byte array to the data checksum.
173:           /// </summary>
174:           /// <param name = "buf">
175:           /// the buffer which contains the data
176:           /// </param>
177:           /// <param name = "off">
178:           /// the offset in the buffer where the data starts
179:           /// </param>
180:           /// <param name = "len">
181:           /// the length of the data
182:           /// </param>
183:           public void Update(byte[] bufint offint len)
184:           {
185:               if (buf == null) {
186:                   throw new ArgumentNullException("buf");
187:               }
188:               
189:               if (off || len || off len buf.Length) {
190:                   throw new ArgumentOutOfRangeException();
191:               }
192:               
193:               crc ^= CrcSeed;
194:               
195:               while (--len >= 0) {
196:                   crc CrcTable[(crc buf[off++]) & 0xFF] ^ (crc >> 8);
197:               }
198:               
199:               crc ^= CrcSeed;
200:           }
201:       }
202:   }

This page was automatically generated by SharpDevelop.