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:
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