Remove unused CRC32 implementations and rename ParallelCRC to Crc32.

lua-bytecode
Icedream 2014-12-11 02:48:59 +01:00
parent e0a64208b2
commit fb68de711b
6 changed files with 12 additions and 215 deletions

View File

@ -94,7 +94,7 @@ namespace GarrysMod.AddonCreator.Addon
stream.Position = 0; stream.Position = 0;
stream.Read(baseAddon, 0, baseAddon.Length); stream.Read(baseAddon, 0, baseAddon.Length);
var baseAddonHash = sr.ReadInt32(); var baseAddonHash = sr.ReadInt32();
var calcAddonHash = ParallelCRC.Compute(baseAddon); var calcAddonHash = Crc32.Compute(baseAddon);
Debug.WriteLine("\tCalculated hash: {0}; Wanted hash: {1}", calcAddonHash, baseAddonHash); Debug.WriteLine("\tCalculated hash: {0}; Wanted hash: {1}", calcAddonHash, baseAddonHash);
Debug.Assert(calcAddonHash == baseAddonHash, "CRC32 hash mismatch", Debug.Assert(calcAddonHash == baseAddonHash, "CRC32 hash mismatch",
"Calculated CRC32 hash is different from the one saved within the addon. Causes could be a corrupted file or the edge case bug https://github.com/icedream/gmadsharp/issues/2."); "Calculated CRC32 hash is different from the one saved within the addon. Causes could be a corrupted file or the edge case bug https://github.com/icedream/gmadsharp/issues/2.");
@ -189,7 +189,7 @@ namespace GarrysMod.AddonCreator.Addon
} }
// CRC check for this file // CRC check for this file
var fileCalcHash = ParallelCRC.Compute(fileContent); var fileCalcHash = Crc32.Compute(fileContent);
Debug.WriteLine("\t\tCalculated hash: {0}; Wanted hash: {1}", fileCalcHash, fileHash); Debug.WriteLine("\t\tCalculated hash: {0}; Wanted hash: {1}", fileCalcHash, fileHash);
if (fileCalcHash != fileHash) if (fileCalcHash != fileHash)
{ {
@ -320,7 +320,7 @@ namespace GarrysMod.AddonCreator.Addon
} }
// Addon CRC // Addon CRC
var addonHash = ParallelCRC.Compute(stream.ToArray()); var addonHash = Crc32.Compute(stream.ToArray());
sw.Write(addonHash); sw.Write(addonHash);
using (var outfile = File.Create(path)) using (var outfile = File.Create(path))

View File

@ -20,7 +20,7 @@ namespace GarrysMod.AddonCreator.Addon
/// </summary> /// </summary>
public virtual int Crc32Hash public virtual int Crc32Hash
{ {
get { return _hash.HasValue ? _hash.Value : (_hash = ParallelCRC.Compute(GetContents())).Value; } get { return _hash.HasValue ? _hash.Value : (_hash = Crc32.Compute(GetContents())).Value; }
} }
/// <summary> /// <summary>

View File

@ -55,13 +55,11 @@
<Compile Include="Addon\AddonWhitelist.cs" /> <Compile Include="Addon\AddonWhitelist.cs" />
<Compile Include="Extensions.cs" /> <Compile Include="Extensions.cs" />
<Compile Include="Addon\JsonAddonFileInfo.cs" /> <Compile Include="Addon\JsonAddonFileInfo.cs" />
<Compile Include="Hashing\OptimizedCRC.cs" /> <Compile Include="Hashing\Crc32.cs" />
<Compile Include="Hashing\ParallelCRC.cs" />
<Compile Include="Addon\PhysicalAddonFileInfo.cs" /> <Compile Include="Addon\PhysicalAddonFileInfo.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Addon\SegmentedAddonFileInfo.cs" /> <Compile Include="Addon\SegmentedAddonFileInfo.cs" />
<Compile Include="Hashing\TraditionalCRC.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<None Include="packages.config" /> <None Include="packages.config" />

View File

@ -3,7 +3,7 @@ using System.Threading;
namespace GarrysMod.AddonCreator.Hashing namespace GarrysMod.AddonCreator.Hashing
{ {
public class ParallelCRC public class Crc32
{ {
private const uint kCrcPoly = 0xEDB88320; private const uint kCrcPoly = 0xEDB88320;
private const uint kInitial = 0xFFFFFFFF; private const uint kInitial = 0xFFFFFFFF;
@ -16,7 +16,7 @@ namespace GarrysMod.AddonCreator.Hashing
private uint value; private uint value;
static ParallelCRC() static Crc32()
{ {
unchecked unchecked
{ {
@ -37,7 +37,7 @@ namespace GarrysMod.AddonCreator.Hashing
} }
} }
public ParallelCRC() public Crc32()
{ {
Init(); Init();
} }
@ -150,7 +150,7 @@ namespace GarrysMod.AddonCreator.Hashing
public static int Compute(byte[] data, int offset, int count) public static int Compute(byte[] data, int offset, int count)
{ {
var crc = new ParallelCRC(); var crc = new Crc32();
crc.Update(data, offset, count); crc.Update(data, offset, count);
return crc.Value; return crc.Value;
} }
@ -270,13 +270,13 @@ namespace GarrysMod.AddonCreator.Hashing
private class Job private class Job
{ {
private readonly ParallelCRC accumulator; private readonly Crc32 accumulator;
private readonly Job waitForJob; private readonly Job waitForJob;
private ArraySegment<byte> data; private ArraySegment<byte> data;
private ManualResetEventSlim finished; private ManualResetEventSlim finished;
public Job(ArraySegment<byte> data, ParallelCRC accumulator, Job waitForJob) public Job(ArraySegment<byte> data, Crc32 accumulator, Job waitForJob)
{ {
this.data = data; this.data = data;
this.accumulator = accumulator; this.accumulator = accumulator;
@ -298,7 +298,7 @@ namespace GarrysMod.AddonCreator.Hashing
Dispose(); Dispose();
} }
public void Dispose() private void Dispose()
{ {
if (finished != null) finished.Dispose(); if (finished != null) finished.Dispose();
finished = null; finished = null;

View File

@ -1,126 +0,0 @@
using System;
namespace GarrysMod.AddonCreator.Hashing
{
public class OptimizedCRC
{
private const uint kCrcPoly = 0xEDB88320;
private const uint kInitial = 0xFFFFFFFF;
private const uint CRC_NUM_TABLES = 8;
private static readonly uint[] Table;
private uint value;
static OptimizedCRC()
{
unchecked
{
Table = new uint[256*CRC_NUM_TABLES];
uint i;
for (i = 0; i < 256; i++)
{
var r = i;
for (var j = 0; j < 8; j++)
r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
Table[i] = r;
}
for (; i < 256*CRC_NUM_TABLES; i++)
{
var r = Table[i - 256];
Table[i] = Table[r & 0xFF] ^ (r >> 8);
}
}
}
public OptimizedCRC()
{
Init();
}
public int Value
{
get { return (int) ~value; }
}
/// <summary>
/// Reset CRC
/// </summary>
public void Init()
{
value = kInitial;
}
public void UpdateByte(byte b)
{
value = (value >> 8) ^ Table[(byte) value ^ b];
}
public void Update(byte[] data, int offset, int count)
{
new ArraySegment<byte>(data, offset, count); // check arguments
if (count == 0) return;
var table = Table; // important for performance!
var crc = value;
for (; (offset & 7) != 0 && count != 0; count--)
crc = (crc >> 8) ^ table[(byte) crc ^ data[offset++]];
if (count >= 8)
{
/*
* Idea from 7-zip project sources (http://7-zip.org/sdk.html)
*/
var to = (count - 8) & ~7;
count -= to;
to += offset;
while (offset != to)
{
crc ^=
(uint)
(data[offset] + (data[offset + 1] << 8) + (data[offset + 2] << 16) +
(data[offset + 3] << 24));
var high =
(uint)
(data[offset + 4] + (data[offset + 5] << 8) + (data[offset + 6] << 16) +
(data[offset + 7] << 24));
offset += 8;
crc = table[(byte) crc + 0x700]
^ table[(byte) (crc >>= 8) + 0x600]
^ table[(byte) (crc >>= 8) + 0x500]
^ table[ /*(byte)*/(crc >> 8) + 0x400]
^ table[(byte) (high) + 0x300]
^ table[(byte) (high >>= 8) + 0x200]
^ table[(byte) (high >>= 8) + 0x100]
^ table[ /*(byte)*/(high >> 8) + 0x000];
}
}
while (count-- != 0)
crc = (crc >> 8) ^ table[(byte) crc ^ data[offset++]];
value = crc;
}
public static int Compute(byte[] data, int offset, int size)
{
var crc = new OptimizedCRC();
crc.Update(data, offset, size);
return crc.Value;
}
public static int Compute(byte[] data)
{
return Compute(data, 0, data.Length);
}
public static int Compute(ArraySegment<byte> block)
{
return Compute(block.Array, block.Offset, block.Count);
}
}
}

View File

@ -1,75 +0,0 @@
using System;
namespace GarrysMod.AddonCreator.Hashing
{
public class TraditionalCRC
{
private const uint kCrcPoly = 0xEDB88320;
private const uint kInitial = 0xFFFFFFFF;
private static readonly uint[] Table;
private uint value;
static TraditionalCRC()
{
unchecked
{
Table = new uint[256];
for (uint i = 0; i < 256; i++)
{
var r = i;
for (var j = 0; j < 8; j++)
r = (r >> 1) ^ (kCrcPoly & ~((r & 1) - 1));
Table[i] = r;
}
}
}
public TraditionalCRC()
{
Init();
}
public int Value
{
get { return (int) ~value; }
}
/// <summary>
/// Reset CRC
/// </summary>
public void Init()
{
value = kInitial;
}
public void UpdateByte(byte b)
{
value = (value >> 8) ^ Table[(byte) value ^ b];
}
public void Update(byte[] data, int offset, int count)
{
if (count < 0) throw new ArgumentOutOfRangeException("count");
while (count-- != 0)
value = (value >> 8) ^ Table[(byte) value ^ data[offset++]];
}
public static int Compute(byte[] data, int offset, int count)
{
var crc = new TraditionalCRC();
crc.Update(data, offset, count);
return crc.Value;
}
public static int Compute(byte[] data)
{
return Compute(data, 0, data.Length);
}
public static int Compute(ArraySegment<byte> block)
{
return Compute(block.Array, block.Offset, block.Count);
}
}
}