Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/nes-containers.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'Crc32Calculator.cs')
-rw-r--r--Crc32Calculator.cs63
1 files changed, 63 insertions, 0 deletions
diff --git a/Crc32Calculator.cs b/Crc32Calculator.cs
new file mode 100644
index 0000000..532c9c2
--- /dev/null
+++ b/Crc32Calculator.cs
@@ -0,0 +1,63 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace com.clusterrr.Famicom.Containers
+{
+ public static class Crc32Calculator
+ {
+ static uint[] table = new uint[256];
+
+ static Crc32Calculator()
+ {
+ // Calculate table
+ uint poly = 0xedb88320;
+ uint temp;
+ for (uint i = 0; i < table.Length; ++i)
+ {
+ temp = i;
+ for (int j = 8; j > 0; --j)
+ {
+ if ((temp & 1) == 1)
+ {
+ temp = (uint)((temp >> 1) ^ poly);
+ }
+ else
+ {
+ temp >>= 1;
+ }
+ }
+ table[i] = temp;
+ }
+ }
+
+ /// <summary>
+ /// Calculate CRC32 checksum
+ /// </summary>
+ /// <param name="data">Input data</param>
+ /// <param name="offset">Data offset</param>
+ /// <param name="length">Length</param>
+ /// <returns></returns>
+ public static uint CalculateCRC32(byte[] data, int offset, int length)
+ {
+ // Calculate CRC32
+ uint crc = 0xffffffff;
+ for (int i = offset; length > 0; i++, length--)
+ {
+ byte index = (byte)(((crc) & 0xff) ^ data[i]);
+ crc = (crc >> 8) ^ table[index];
+ }
+ return ~crc;
+ }
+
+ /// <summary>
+ /// Calculate CRC32 checksum
+ /// </summary>
+ /// <param name="data">Input date</param>
+ /// <returns></returns>
+ public static uint CalculateCRC32(byte[] data)
+ => CalculateCRC32(data, 0, data.Length);
+ }
+}