Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
Crc32.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under Apache 2.0 License. See LICENSE.md for details.
3 //
4 // --------------------------------------------------------------------------------------------------------------------
5 // <copyright file="Crc32.cs" company="Matthew Leibowitz">
6 // Copyright (c) Matthew Leibowitz
7 // This code is licensed under the Apache 2.0 License
8 // http://www.apache.org/licenses/LICENSE-2.0.html
9 // </copyright>
10 // <summary>
11 // The crc 32.
12 // </summary>
13 // --------------------------------------------------------------------------------------------------------------------
14 
15 namespace System.IO.Compression.Zip
16 {
17  /// <summary>
18  /// The crc 32.
19  /// </summary>
20  public sealed class Crc32
21  {
22  #region Constants
23 
24  /// <summary>
25  /// The crc seed.
26  /// </summary>
27  private const uint CrcSeed = 0xFFFFFFFF;
28 
29  #endregion
30 
31  #region Constructors and Destructors
32 
33  /// <summary>
34  /// Initializes static members of the <see cref="Crc32"/> class.
35  /// Just invoked once in order to create the CRC32 lookup table.
36  /// </summary>
37  static Crc32()
38  {
39  // Generate CRC32 table
40  Crc32Table = new uint[256];
41  for (uint i = 0; i < Crc32Table.Length; i++)
42  {
43  uint c = i;
44 
45  for (int j = 0; j < 8; j++)
46  {
47  if ((c & 1) != 0)
48  {
49  c = 3988292384 ^ (c >> 1);
50  }
51  else
52  {
53  c >>= 1;
54  }
55  }
56 
57  Crc32Table[i] = c;
58  }
59  }
60 
61  #endregion
62 
63  #region Public Properties
64 
65  /// <summary>
66  /// Gets the CRC32 Table.
67  /// </summary>
68  public static uint[] Crc32Table { get; private set; }
69 
70  /// <summary>
71  /// Returns the CRC32 data checksum computed so far.
72  /// </summary>
73  public uint Value { get; set; }
74 
75  #endregion
76 
77  #region Public Methods and Operators
78 
79  /// <summary>
80  /// Resets the CRC32 data checksum as if no update was ever called.
81  /// </summary>
82  public void Reset()
83  {
84  this.Value = 0;
85  }
86 
87  /// <summary>
88  /// Updates the checksum with the int bval.
89  /// </summary>
90  /// <param name="value">
91  /// the byte is taken as the lower 8 bits of value
92  /// </param>
93  public void Update(int value)
94  {
95  this.Value ^= CrcSeed;
96  this.Value = Crc32Table[(this.Value ^ value) & 0xFF] ^ (this.Value >> 8);
97  this.Value ^= CrcSeed;
98  }
99 
100  /// <summary>
101  /// Adds the byte array to the data checksum.
102  /// </summary>
103  /// <param name="buffer">
104  /// The buffer which contains the data
105  /// </param>
106  /// <param name="offset">
107  /// The offset in the buffer where the data starts
108  /// </param>
109  /// <param name="count">
110  /// The number of data bytes to update the CRC with.
111  /// </param>
112  public void Update(byte[] buffer, int offset, int count)
113  {
114  if (buffer == null)
115  {
116  throw new ArgumentNullException("buffer");
117  }
118 
119  if (count < 0)
120  {
121  throw new ArgumentOutOfRangeException("count", "Count cannot be less than zero");
122  }
123 
124  if (offset < 0 || offset + count > buffer.Length)
125  {
126  throw new ArgumentOutOfRangeException("offset");
127  }
128 
129  this.Value ^= CrcSeed;
130 
131  while (--count >= 0)
132  {
133  this.Value = Crc32Table[(this.Value ^ buffer[offset++]) & 0xFF] ^ (this.Value >> 8);
134  }
135 
136  this.Value ^= CrcSeed;
137  }
138 
139  #endregion
140  }
141 }
void Reset()
Resets the CRC32 data checksum as if no update was ever called.
Definition: Crc32.cs:82
void Update(byte[] buffer, int offset, int count)
Adds the byte array to the data checksum.
Definition: Crc32.cs:112
void Update(int value)
Updates the checksum with the int bval.
Definition: Crc32.cs:93
_In_ size_t count
Definition: DirectXTexP.h:174
Compression
Compression method enumeration
Definition: Compression.cs:20