From 022e62a0e1d0cad7144f7a4cfb11153d84f878fd Mon Sep 17 00:00:00 2001 From: Pyrdacor Date: Mon, 15 Nov 2021 16:11:03 +0100 Subject: [PATCH] Added missing file PngCrc.cs --- Ambermoon.Common/PngCrc.cs | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 Ambermoon.Common/PngCrc.cs diff --git a/Ambermoon.Common/PngCrc.cs b/Ambermoon.Common/PngCrc.cs new file mode 100644 index 00000000..6920e617 --- /dev/null +++ b/Ambermoon.Common/PngCrc.cs @@ -0,0 +1,67 @@ +namespace Ambermoon +{ + public class PngCrc + { + uint[] table = null; + uint lastCrc = 0; + + void EnsureTable() + { + if (table != null) + return; + + table = new uint[256]; + uint c; + uint k; + + for (uint n = 0; n < 256; ++n) + { + c = n; + + for (k = 0; k < 8; ++k) + { + if ((c & 1) != 0) + c = 0xedb88320 ^ (c >> 1); + else + c >>= 1; + } + + table[n] = c; + } + } + + uint Update(uint crc, byte[] buffer, int length) + { + EnsureTable(); + uint c = crc; + + for (int n = 0; n < length; ++n) + { + c = table[(c ^ buffer[n]) & 0xff] ^ (c >> 8); + } + + return c; + } + + public uint Calculate(byte[] buffer) + { + return Calculate(buffer, buffer.Length); + } + + public uint Calculate(byte[] buffer, int length) + { + return lastCrc = Update(lastCrc ^ 0xffffffff, buffer, length) ^ 0xffffffff; + } + + public uint Calculate(uint crc, byte[] buffer) + { + return Calculate(crc, buffer, buffer.Length); + } + + public uint Calculate(uint crc, byte[] buffer, int length) + { + lastCrc = crc; + return Calculate(buffer, length); + } + } +}