how is it fucking nobody can write an actually functional CRC32 implementation in C#

This commit is contained in:
Maff 2019-08-27 02:29:43 +01:00
parent a606010f98
commit 091a3e5934
4 changed files with 17 additions and 8 deletions

View File

@ -5,15 +5,20 @@ namespace libsharperang {
public class DataTransforms { public class DataTransforms {
public class CrcSum : Crc32Base { public class CrcSum : Crc32Base {
public uint CrcKey; public uint CrcKey;
public CrcSum() : base(0x77c40d4d^0x35769521, 0xffffffff, 0xffffffff, false, false) => CrcKey=0x77c40d4d; public CrcSum(uint Mangled, uint Key) : base(0x04c11db7, Mangled, 0xffffffff, true, true) => CrcKey=Key;
public CrcSum(uint Key) : base(Key^0x35769521, 0xffffffff, 0xffffffff, false, false) => CrcKey=Key; }
public CrcSum(uint Key, bool _) : base(Key, 0xffffffff, 0xffffffff, false, false) => CrcKey=Key; private uint Bludgeon(uint iv) {
uint ivr=0;
for (int i=0;i<32;i++) {
uint bit=(iv>>i)&1;
ivr |= (bit<<(31-i));
}
return ~ivr;
} }
public CrcSum CRC; public CrcSum CRC;
public bool IsCrcInitialised() => (CRC!=null); public bool IsCrcInitialised() => (CRC!=null);
public void InitialiseCrc() => CRC=new CrcSum(); public void InitialiseCrc() => CRC=new CrcSum(Bludgeon(0x77c40d4d^0x35769521), 0x77c40d4d);
public void InitialiseCrc(uint Key) => CRC=new CrcSum(Key); public void InitialiseCrc(uint Key) => CRC=new CrcSum(Bludgeon(Key), Key);
public void InitialiseCrc(uint Key, bool _) => CRC=new CrcSum(Key, _);
public uint GetCrcKey() { public uint GetCrcKey() {
if (!IsCrcInitialised()) InitialiseCrc(); if (!IsCrcInitialised()) InitialiseCrc();
return CRC.CrcKey; return CRC.CrcKey;

View File

@ -7,6 +7,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CrcDotNET" Version="1.0.3" /> <PackageReference Include="CrcDotNET" Version="1.0.3" />
<PackageReference Include="LibUsbDotNet" Version="2.2.29" /> <PackageReference Include="LibUsbDotNet" Version="2.2.29" />
<PackageReference Include="Zlib.Portable.Signed" Version="1.11.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -99,7 +99,7 @@ namespace libsharperang {
} }
public byte[] BuildTransmitCrc() { public byte[] BuildTransmitCrc() {
DataTransforms _=new DataTransforms(); DataTransforms _=new DataTransforms();
_.InitialiseCrc(0x35769521, true); _.InitialiseCrc(0x35769521);
return Build(Opcode.CrcTransmit, transformer.GetCrcKeyBytes(), _); return Build(Opcode.CrcTransmit, transformer.GetCrcKeyBytes(), _);
} }
} }

View File

@ -27,6 +27,9 @@ namespace sharperang {
logger.Debug("ClaimUSB => " + printer?.Claim()); logger.Debug("ClaimUSB => " + printer?.Claim());
//logger.Debug("IUsb::Initialised => " + printer?.pUsb?.Initialised()); //logger.Debug("IUsb::Initialised => " + printer?.pUsb?.Initialised());
} }
private void BtTestLine_Click(object sender, RoutedEventArgs e) => logger.Debug("printer::TestCRC() => "+BitConverter.ToString(printer.builder.BuildTransmitCrc()).Replace('-',' ')); private void BtTestLine_Click(object sender, RoutedEventArgs e) {
logger.Debug("printer::TransmitCrc() => "+BitConverter.ToString(printer.builder.BuildTransmitCrc()).Replace('-', ' '));
logger.Debug("printer::TestCRC(0x0000) => "+BitConverter.ToString(printer.builder.Build(Frame.Opcode.SessionBegin, new byte[] { 0x00, 0x00 })).Replace('-',' '));
}
} }
} }