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:
authorAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-11-08 09:53:44 +0300
committerAlexey 'Cluster' Avdyukhin <clusterrr@clusterrr.com>2022-11-08 09:53:44 +0300
commit0acfff00771d0f944db0c91ebd366e0cb3d01591 (patch)
treeb4e4dc362615d1b735b8897100d4c83d1274747c /Crc32.cs
parent530e8fa3a1ff5eb0614e7a54d5b2df66fd8eed26 (diff)
Padding fix, checksum fix
Diffstat (limited to 'Crc32.cs')
-rw-r--r--Crc32.cs65
1 files changed, 65 insertions, 0 deletions
diff --git a/Crc32.cs b/Crc32.cs
new file mode 100644
index 0000000..33acf83
--- /dev/null
+++ b/Crc32.cs
@@ -0,0 +1,65 @@
+using System;
+using System.Security.Cryptography;
+
+namespace com.clusterrr.Famicom.Containers
+{
+ /// <summary>
+ /// CRC32 calculator
+ /// </summary>
+ internal class Crc32 : HashAlgorithm
+ {
+ private readonly uint[] table = new uint[256];
+ private uint crc = 0xFFFFFFFF;
+
+ public Crc32()
+ {
+ // 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;
+ }
+ }
+
+ public override void Initialize()
+ {
+ }
+
+ public override bool CanReuseTransform => false;
+
+ protected override void HashCore(byte[] array, int ibStart, int cbSize)
+ {
+ while (cbSize > 0)
+ {
+ var pos = ibStart;
+ byte index = (byte)(((crc) & 0xff) ^ array[pos]);
+ crc = (crc >> 8) ^ table[index];
+ ibStart++;
+ cbSize--;
+ }
+ }
+
+ protected override byte[] HashFinal()
+ {
+ return BitConverter.GetBytes(~crc);
+ }
+
+ public override bool CanTransformMultipleBlocks => true;
+ public override byte[] Hash => BitConverter.GetBytes(~crc);
+ public override int HashSize => 32;
+
+ }
+}