mirror of
https://github.com/claunia/cuetools.net.git
synced 2025-12-16 18:14:25 +00:00
all CRCs in one place
This commit is contained in:
50
CUETools.Codecs/CRCs/CRC16.cs
Normal file
50
CUETools.Codecs/CRCs/CRC16.cs
Normal file
@@ -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<<bits)-1));
|
||||||
|
table[i] = (ushort)(crc & 0xffff);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
55
CUETools.Codecs/CRCs/CRC16CCITT.cs
Normal file
55
CUETools.Codecs/CRCs/CRC16CCITT.cs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace CUETools.Codecs
|
||||||
|
{
|
||||||
|
public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
|
||||||
|
|
||||||
|
public class Crc16Ccitt
|
||||||
|
{
|
||||||
|
const ushort poly = 4129;
|
||||||
|
ushort[] table = new ushort[256];
|
||||||
|
ushort initialValue = 0;
|
||||||
|
|
||||||
|
public ushort ComputeChecksum(byte[] bytes, int pos, int count)
|
||||||
|
{
|
||||||
|
ushort crc = this.initialValue;
|
||||||
|
for (int i = pos; i < pos + count; i++)
|
||||||
|
{
|
||||||
|
crc = (ushort)((crc << 8) ^ table[((crc >> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
80
CUETools.Codecs/CRCs/CRC32.cs
Normal file
80
CUETools.Codecs/CRCs/CRC32.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
47
CUETools.Codecs/CRCs/CRC8.cs
Normal file
47
CUETools.Codecs/CRCs/CRC8.cs
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user