diff --git a/CUETools.Codecs/CRCs/CRC16.cs b/CUETools.Codecs/CRCs/CRC16.cs new file mode 100644 index 0000000..c2bde94 --- /dev/null +++ b/CUETools.Codecs/CRCs/CRC16.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CUETools.Codecs +{ + public class Crc16 + { + ushort[] table = new ushort[256]; + + public ushort ComputeChecksum(byte[] bytes, int pos, int count) + { + ushort crc = 0; + for (int i = pos; i < pos + count; i++) + { + crc = (ushort)(((crc << 8) & 0xffff) ^ table[(crc >> 8) ^ bytes[i]]); + } + return crc; + } + + public unsafe ushort ComputeChecksum(byte* bytes, int pos, int count) + { + ushort crc = 0; + fixed (ushort* t = table) + for (int i = pos; i < pos + count; i++) + crc = (ushort)(((crc << 8) & 0xffff) ^ t[(crc >> 8) ^ bytes[i]]); + return crc; + } + + public Crc16() + { + int bits = 16; + int poly16 = 0x8005; + int poly = (poly16 + (1 << bits)); + for (ushort i = 0; i < table.Length; i++) + { + int crc = i; + for (int j = 0; j < bits; j++) + { + if ((crc & (1U << (bits - 1))) != 0) + crc = ((crc << 1) ^ poly); + else + crc <<= 1; + } + //table[i] = (crc & ((1<> 8) ^ (0xff & bytes[i]))]); + } + return crc; + } + + public byte[] ComputeChecksumBytes(byte[] bytes, int pos, int count) + { + ushort crc = ComputeChecksum(bytes, pos, count); + return new byte[] { (byte)(crc >> 8), (byte)(crc & 0x00ff) }; + } + + public Crc16Ccitt(InitialCrcValue initialValue) + { + this.initialValue = (ushort)initialValue; + ushort temp, a; + for (int i = 0; i < table.Length; i++) + { + temp = 0; + a = (ushort)(i << 8); + for (int j = 0; j < 8; j++) + { + if (((temp ^ a) & 0x8000) != 0) + { + temp = (ushort)((temp << 1) ^ poly); + } + else + { + temp <<= 1; + } + a <<= 1; + } + table[i] = temp; + } + } + } +} diff --git a/CUETools.Codecs/CRCs/CRC32.cs b/CUETools.Codecs/CRCs/CRC32.cs new file mode 100644 index 0000000..99161f8 --- /dev/null +++ b/CUETools.Codecs/CRCs/CRC32.cs @@ -0,0 +1,80 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CUETools.Codecs +{ + public class Crc32 + { + uint[] table = new uint[256]; + + public uint ComputeChecksum(uint crc, byte val) + { + return (crc >> 8) ^ table[(crc & 0xff) ^ val]; + } + + public uint ComputeChecksum(uint crc, byte[] bytes, int pos, int count) + { + for (int i = pos; i < pos + count; i++) + crc = ComputeChecksum(crc, bytes[i]); + return crc; + } + + public uint ComputeChecksum(uint crc, uint s) + { + return ComputeChecksum(ComputeChecksum(ComputeChecksum(ComputeChecksum( + crc, (byte)s), (byte)(s >> 8)), (byte)(s >> 16)), (byte)(s >> 24)); + } + + public unsafe uint ComputeChecksum(uint crc, int * samples, uint count) + { + for (uint i = 0; i < count; i++) + { + int s1 = samples[2 * i], s2 = samples[2 * i + 1]; + crc = ComputeChecksum(ComputeChecksum(ComputeChecksum(ComputeChecksum( + crc, (byte)s1), (byte)(s1 >> 8)), (byte)s2), (byte)(s2 >> 8)); + } + return crc; + } + + public unsafe uint ComputeChecksumWONULL(uint crc, int* samples, uint count) + { + for (uint i = 0; i < count; i++) + { + int s1 = samples[2 * i], s2 = samples[2 * i + 1]; + if (s1 != 0) + crc = ComputeChecksum(ComputeChecksum(crc, (byte)s1), (byte)(s1 >> 8)); + if (s2 != 0) + crc = ComputeChecksum(ComputeChecksum(crc, (byte)s2), (byte)(s2 >> 8)); + } + return crc; + } + + uint Reflect(uint val, int ch) + { + uint value = 0; + // Swap bit 0 for bit 7 + // bit 1 for bit 6, etc. + for (int i = 1; i < (ch + 1); i++) + { + if (0 != (val & 1)) + value |= 1U << (ch - i); + val >>= 1; + } + return value; + } + + const uint ulPolynomial = 0x04c11db7; + + public Crc32() + { + for (uint i = 0; i < table.Length; i++) + { + table[i] = Reflect(i, 8) << 24; + for (int j = 0; j < 8; j++) + table[i] = (table[i] << 1) ^ ((table[i] & (1U << 31)) == 0 ? 0 : ulPolynomial); + table[i] = Reflect(table[i], 32); + } + } + } +} diff --git a/CUETools.Codecs/CRCs/CRC8.cs b/CUETools.Codecs/CRCs/CRC8.cs new file mode 100644 index 0000000..68a2209 --- /dev/null +++ b/CUETools.Codecs/CRCs/CRC8.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace CUETools.Codecs +{ + + public class Crc8 + { + const ushort poly8 = 0x07; + ushort[] table = new ushort[256]; + + public byte ComputeChecksum(byte[] bytes, int pos, int count) + { + ushort crc = 0; + for (int i = pos; i < pos + count; i++) + crc = table[crc ^ bytes[i]]; + return (byte) crc; + } + + public unsafe byte ComputeChecksum(byte * bytes, int pos, int count) + { + ushort crc = 0; + for (int i = pos; i < pos + count; i++) + crc = table[crc ^ bytes[i]]; + return (byte)crc; + } + + public Crc8() + { + int bits = 8; + ushort poly = (ushort) (poly8 + (1U << bits)); + for (ushort i = 0; i < table.Length; i++) + { + ushort crc = i; + for (int j = 0; j < bits; j++) + { + if ((crc & (1U << (bits - 1))) != 0) + crc = (ushort)((crc << 1) ^ poly); + else + crc <<= 1; + } + table[i] = (ushort)(crc & 0x00ff); + } + } + } +}