Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jefffhaynes committed Jul 19, 2019
1 parent edca96a commit 542da09
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions BinarySerializer/Crc.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;

namespace BinarySerialization
{
internal abstract class Crc<T>
{
private const int BitsPerByte = 8;

// ReSharper disable once StaticMemberInGenericType
private static readonly int TableSize = (int) Math.Pow(2, BitsPerByte);

private static readonly Dictionary<T, T[]> Tables = new Dictionary<T, T[]>();

// ReSharper disable once StaticMemberInGenericType
Expand Down Expand Up @@ -53,12 +59,12 @@ public void Compute(byte[] buffer, int offset, int count)

if (IsDataReflected)
{
b = (byte) Reflect(b, 8);
b = (byte) Reflect(b, BitsPerByte);
}

var data = (byte) (b ^ (remainder >> (Width - 8)));
var data = (byte) (b ^ (remainder >> (Width - BitsPerByte)));

remainder = ToUInt32(_table[data]) ^ (remainder << 8);
remainder = ToUInt32(_table[data]) ^ (remainder << BitsPerByte);
}

_crc = FromUInt32(remainder);
Expand All @@ -83,11 +89,11 @@ public T ComputeFinal()

private T[] BuildTable(T polynomial)
{
var table = new T[256];
var table = new T[TableSize];

var poly = ToUInt32(polynomial);

var padWidth = Width - 8;
var padWidth = Width - BitsPerByte;

var topBit = 1 << (Width - 1);

Expand All @@ -98,7 +104,7 @@ private T[] BuildTable(T polynomial)
var remainder = dividend << padWidth;

// Perform modulo-2 division, a bit at a time.
for (uint bit = 8; bit > 0; bit--)
for (uint bit = BitsPerByte; bit > 0; bit--)
{
// Try to divide the current data bit.
if ((remainder & topBit) != 0)
Expand Down

0 comments on commit 542da09

Please sign in to comment.