Skip to content

Commit 00c9069

Browse files
committed
improved api for Crc32 calculation, to make it more usable in external projects
1 parent ab9dc15 commit 00c9069

File tree

8 files changed

+90
-11
lines changed

8 files changed

+90
-11
lines changed

Blazer.Benchmark/Program.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,18 @@ private static void BenchCrc32C()
4040

4141
//warm-up
4242
Crc32C.Crc32CAlgorithm.Compute(new byte[1]);
43-
hardware.Calculate(new byte[1], 0, 1);
44-
software.Calculate(new byte[1], 0, 1);
43+
hardware.Calculate(0, new byte[1], 0, 1);
44+
software.Calculate(0, new byte[1], 0, 1);
4545

4646
var sw = new Stopwatch();
4747
sw.Start();
4848
var cr = Crc32C.Crc32CAlgorithm.Compute(array);
4949
Console.WriteLine("Crc32C.Net: " + ((array.Length / sw.Elapsed.TotalSeconds) / (1024 * 1024)).ToString("0") + " MB/s");
5050
sw.Restart();
51-
var hr = hardware.Calculate(array, 0, array.Length);
51+
var hr = hardware.Calculate(0, array, 0, array.Length);
5252
Console.WriteLine("Hardware: " + ((array.Length / sw.Elapsed.TotalSeconds) / (1024 * 1024)).ToString("0") + " MB/s");
5353
sw.Restart();
54-
var sr = software.Calculate(array, 0, array.Length);
54+
var sr = software.Calculate(0, array, 0, array.Length);
5555
Console.WriteLine("Software: " + ((array.Length / sw.Elapsed.TotalSeconds) / (1024 * 1024)).ToString("0") + " MB/s");
5656
if (hr != cr)
5757
Console.WriteLine("Error in hardware realization");

Blazer.Net.Tests/Blazer.Net.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<WarningLevel>4</WarningLevel>
3333
</PropertyGroup>
3434
<ItemGroup>
35+
<Reference Include="Crc32C.NET">
36+
<HintPath>..\packages\Crc32C.NET.1.0.5.0\lib\net20\Crc32C.NET.dll</HintPath>
37+
</Reference>
3538
<Reference Include="nunit.framework">
3639
<HintPath>..\packages\NUnit.2.6.4\lib\nunit.framework.dll</HintPath>
3740
</Reference>
@@ -45,6 +48,7 @@
4548
</ItemGroup>
4649
<ItemGroup>
4750
<Compile Include="BigDataTests.cs" />
51+
<Compile Include="Crc32Tests.cs" />
4852
<Compile Include="EncryptionTests.cs" />
4953
<Compile Include="IntegrityHelper.cs" />
5054
<Compile Include="IntegrityTests.cs" />

Blazer.Net.Tests/Crc32Tests.cs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.Linq;
3+
using System.Text;
4+
5+
using Crc32C;
6+
7+
using NUnit.Framework;
8+
9+
using E = Force.Blazer.Algorithms.Crc32C.Crc32C;
10+
11+
namespace Blazer.Net.Tests
12+
{
13+
[TestFixture]
14+
public class Crc32Tests
15+
{
16+
[TestCase("Hello", 3)]
17+
[TestCase("Nazdar", 0)]
18+
[TestCase("Ahoj", 1)]
19+
[TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 0)]
20+
[TestCase("Very long text.Very long text.Very long text.Very long text.Very long text.Very long text.Very long text", 3)]
21+
public void ResultConsistency(string text, int offset)
22+
{
23+
var bytes = Encoding.ASCII.GetBytes(text);
24+
25+
var crc1 = E.Calculate(bytes.Skip(offset).ToArray());
26+
var crc2 = Crc32CAlgorithm.Append(0, bytes, offset, bytes.Length - offset);
27+
Assert.That(crc2, Is.EqualTo(crc1));
28+
}
29+
30+
[Test]
31+
public void ResultConsistencyLong()
32+
{
33+
var bytes = new byte[30000];
34+
new Random().NextBytes(bytes);
35+
var crc1 = E.Calculate(bytes, 0, bytes.Length);
36+
var crc2 = Crc32CAlgorithm.Append(0, bytes, 0, bytes.Length);
37+
Assert.That(crc2, Is.EqualTo(crc1));
38+
}
39+
40+
[Test]
41+
public void ResultConsistency2()
42+
{
43+
Assert.That(E.Calculate(new byte[] { 1 }), Is.EqualTo(Crc32CAlgorithm.Compute(new byte[] { 1 })));
44+
Assert.That(E.Calculate(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }), Is.EqualTo(Crc32CAlgorithm.Compute(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 })));
45+
}
46+
47+
[Test]
48+
public void PartIsWhole()
49+
{
50+
var bytes = new byte[30000];
51+
new Random().NextBytes(bytes);
52+
var r1 = E.Calculate(0, bytes, 0, 15000);
53+
var r2 = E.Calculate(r1, bytes, 15000, 15000);
54+
var r3 = E.Calculate(0, bytes, 0, 30000);
55+
Assert.That(r2, Is.EqualTo(r3));
56+
}
57+
}
58+
}

Blazer.Net.Tests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Crc32C.NET" version="1.0.5.0" targetFramework="net40" />
34
<package id="NUnit" version="2.6.4" targetFramework="net40" />
45
</packages>

Blazer.Net/Algorithms/Crc32C/Crc32C.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,31 @@ static Crc32C()
1919
/// </summary>
2020
public static uint Calculate(byte[] buffer, int offset, int count)
2121
{
22-
return _calculator.Calculate(buffer, offset, count);
22+
return Calculate(0, buffer, offset, count);
23+
}
24+
25+
/// <summary>
26+
/// Calculates Crc32C data of given buffer, updates existing crc
27+
/// </summary>
28+
public static uint Calculate(uint currentCrc, byte[] buffer)
29+
{
30+
return Calculate(currentCrc, buffer, 0, buffer.Length);
31+
}
32+
33+
/// <summary>
34+
/// Calculates Crc32C data of given buffer, updates existing crc
35+
/// </summary>
36+
public static uint Calculate(uint currentCrc, byte[] buffer, int offset, int count)
37+
{
38+
return _calculator.Calculate(currentCrc, buffer, offset, count);
2339
}
2440

2541
/// <summary>
2642
/// Calculates Crc32C data of given buffer
2743
/// </summary>
2844
public static uint Calculate(byte[] buffer)
2945
{
30-
return _calculator.Calculate(buffer, 0, buffer.Length);
46+
return Calculate(0, buffer, 0, buffer.Length);
3147
}
3248
}
3349
}

Blazer.Net/Algorithms/Crc32C/Crc32CHardware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public Crc32CHardware()
2222
throw new InvalidOperationException("You have no right for hardware implementation");
2323
}
2424

25-
uint ICrc32CCalculator.Calculate(byte[] buffer, int offset, int count)
25+
uint ICrc32CCalculator.Calculate(uint crc, byte[] buffer, int offset, int count)
2626
{
2727
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
28-
var res = crc32c_append(0, handle.AddrOfPinnedObject() + offset, count);
28+
var res = crc32c_append(crc, handle.AddrOfPinnedObject() + offset, count);
2929
handle.Free();
3030
return res;
3131
}

Blazer.Net/Algorithms/Crc32C/Crc32CSoftware.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ static Crc32CSoftware()
2121
}
2222
}
2323

24-
uint ICrc32CCalculator.Calculate(byte[] buffer, int offset, int count)
24+
uint ICrc32CCalculator.Calculate(uint crc, byte[] buffer, int offset, int count)
2525
{
26-
uint crcLocal = uint.MaxValue;
26+
uint crcLocal = crc ^ uint.MaxValue;
2727

2828
uint[] table = _table;
2929
while (count >= 16)

Blazer.Net/Algorithms/Crc32C/ICrc32CCalculator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ public interface ICrc32CCalculator
88
/// <summary>
99
/// Calculates Crc32C data for buffer
1010
/// </summary>
11-
uint Calculate(byte[] buffer, int offset, int count);
11+
uint Calculate(uint crc, byte[] buffer, int offset, int count);
1212
}
1313
}

0 commit comments

Comments
 (0)