Corrected poly, solved endian problem

This commit is contained in:
2015-02-02 17:40:02 +00:00
parent a817acbe4d
commit 0d7c2ce12b
2 changed files with 14 additions and 5 deletions

View File

@@ -1,3 +1,8 @@
2015-02-02 Natalia Portillo <claunia@claunia.com>
* Checksums/CRC16Context.cs:
Corrected poly, solved endian problem
2014-04-21 Natalia Portillo <claunia@claunia.com> 2014-04-21 Natalia Portillo <claunia@claunia.com>
* ImagePlugins/TeleDisk.cs: * ImagePlugins/TeleDisk.cs:

View File

@@ -46,8 +46,8 @@ namespace DiscImageChef.Checksums
/// </summary> /// </summary>
public class CRC16Context public class CRC16Context
{ {
const UInt16 crc16Poly = 0x8408; const UInt16 crc16Poly = 0xA001;
const UInt16 crc16Seed = 0xFFFF; const UInt16 crc16Seed = 0x0000;
UInt16[] table; UInt16[] table;
UInt16 hashInt; UInt16 hashInt;
@@ -80,7 +80,7 @@ namespace DiscImageChef.Checksums
public void Update(byte[] data, uint len) public void Update(byte[] data, uint len)
{ {
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
hashInt = (ushort)((hashInt >> 8) ^ table[data[i] ^ hashInt & 0xff]); hashInt = (ushort)((hashInt >> 8) ^ table[data[i] ^ (hashInt & 0xFF)]);
} }
/// <summary> /// <summary>
@@ -98,6 +98,7 @@ namespace DiscImageChef.Checksums
public byte[] Final() public byte[] Final()
{ {
hashInt ^= crc16Seed; hashInt ^= crc16Seed;
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
return BigEndianBitConverter.GetBytes(hashInt); return BigEndianBitConverter.GetBytes(hashInt);
} }
@@ -109,6 +110,7 @@ namespace DiscImageChef.Checksums
hashInt ^= crc16Seed; hashInt ^= crc16Seed;
StringBuilder crc16Output = new StringBuilder(); StringBuilder crc16Output = new StringBuilder();
BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++) for (int i = 0; i < BigEndianBitConverter.GetBytes(hashInt).Length; i++)
{ {
crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2")); crc16Output.Append(BigEndianBitConverter.GetBytes(hashInt)[i].ToString("x2"));
@@ -156,7 +158,8 @@ namespace DiscImageChef.Checksums
for (int i = 0; i < fileStream.Length; i++) for (int i = 0; i < fileStream.Length; i++)
localhashInt = (ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff]); localhashInt = (ushort)((localhashInt >> 8) ^ localTable[fileStream.ReadByte() ^ localhashInt & 0xff]);
hash = BitConverter.GetBytes(localhashInt); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt);
StringBuilder crc16Output = new StringBuilder(); StringBuilder crc16Output = new StringBuilder();
@@ -209,7 +212,8 @@ namespace DiscImageChef.Checksums
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
localhashInt = (ushort)((localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff]); localhashInt = (ushort)((localhashInt >> 8) ^ localTable[data[i] ^ localhashInt & 0xff]);
hash = BitConverter.GetBytes(localhashInt); BigEndianBitConverter.IsLittleEndian = BitConverter.IsLittleEndian;
hash = BigEndianBitConverter.GetBytes(localhashInt);
StringBuilder crc16Output = new StringBuilder(); StringBuilder crc16Output = new StringBuilder();