Table of Contents

CrcLookupTableBuilder Class

Definition

Namespace
Bodu.IO.Hashing.Checksums
Assembly
Bodu.IO.Hashing.dll
Package
Bodu.IO.Hashing 1.0.0
Source
CrcLookupTableBuilder.cs

Generates the precomputed lookup tables that drive the byte-at-a-time inner loop of Crc, given the width, polynomial, and input-reflection mode of a CrcStandard.

public static class CrcLookupTableBuilder
Inheritance
CrcLookupTableBuilder
Inherited Members

Examples

// Build the lookup table for CRC-32/ISO-HDLC (the canonical CRC-32 used by Ethernet, PKZIP, gzip).
CrcStandard isoHdlc = CrcStandard.CRC32_ISOHDLC;
ulong[] table = CrcLookupTableBuilder.BuildLookupTable(
    size:       isoHdlc.Size,        // 32
    polynomial: isoHdlc.Polynomial,  // 0x04C11DB7
    reflectIn:  isoHdlc.ReflectIn);  // true

// table has 256 entries; each value is masked to 32 bits and suitable for
// direct XOR into the running CRC register.
Console.WriteLine(table[0xFF]); // 0x...  the precomputed contribution for byte 0xFF

Remarks

The naive bit-by-bit CRC algorithm is straightforward but processes only one input bit per iteration. The standard production technique is to precompute a small table of partial CRCs - one entry per possible input byte under the chosen polynomial - so the per-byte cost collapses to a single XOR plus a table lookup. CrcLookupTableBuilder is the routine that builds that table; Crc consumes it and CrcLookupTableCache caches it across instances so the construction cost is paid at most once per (width, polynomial, reflectIn) tuple.

When you would call this directly. Most callers should not - Crc resolves its lookup table automatically through GlobalCache. Reach for BuildLookupTable(int, ulong, bool) directly when implementing a custom CRC engine outside the Crc hierarchy, when running diagnostics against a known-good table, or when populating a hand-rolled cache for an unusual polynomial that the global cache is not the right home for.

Output shape. The returned table contains 1 << min(size, 8) entries (256 for any width of 8 bits or more, smaller for sub-byte widths). Each entry is masked to size bits and is suitable for direct XOR into a CRC register of the corresponding width. Output bit reflection (the ReflectOut step) is not applied here - that is a finalization step performed by the engine after the table-driven loop completes.

The method is pure, deterministic, and allocation-bounded by the table size; results are safe to cache and share. The implementation lives in the shared Bodu.IO.Hashing.Checksums.CrcCore source (compiled here and into the format readers that verify vendor CRC variants); this type is the public facade over it.

Methods

BuildLookupTable(int, ulong, bool)

Generates a CRC lookup table for a given bit size, polynomial, and reflection mode.

public static ulong[] BuildLookupTable(int size, ulong polynomial, bool reflectIn)

Parameters

size int

The number of bits in the CRC (e.g., 8, 16, 32, 64).

polynomial ulong

The CRC polynomial represented as an unsigned integer.

reflectIn bool

If true, input bytes are reflected (bit-reversed) before CRC processing; otherwise, bits are used as-is.

Returns

ulong[]

An array of ulong values representing the CRC lookup table, with 1 << min(size, 8) entries.

Remarks

This method is typically used to precompute a table of CRC values for efficient byte-wise CRC calculation. The reflection setting determines whether the bits of the input byte are reversed prior to processing, which is common in some CRC variants.

Exceptions

ArgumentOutOfRangeException

size is less than 1 or greater than 64.

Applies to

ProductVersions
.NET8, 10

See Also