CrcStandard Class
Definition
Immutable parameter bundle that fully describes a CRC variant - width, polynomial, initial value, input/output bit reflection, and final XOR - and serves as the lookup key into the RevEng catalogue used by Crc.
public sealed class CrcStandard : ISerializable, IEquatable<CrcStandard>
- Inheritance
-
CrcStandard
- Implements
- Inherited Members
- Extension Methods
Examples
using Bodu.IO.Hashing.Checksums;
// 1. Direct named accessor - most callers want exactly this.
var crc = new Crc(CrcStandard.CRC32_ISOHDLC);
// 2. Data-driven look-up - pick the variant from a configuration value.
CrcStandards configured = Enum.Parse<CrcStandards>(config["CrcVariant"]);
var configuredCrc = new Crc(CrcStandard.Get(configured));
// 3. Look-up by alias - accept legacy or vendor names supplied by users.
CrcStandard pkzip = CrcStandard.FromName("PKZIP"); // same instance as CRC32_ISOHDLC
CrcStandard ccitt = CrcStandard.FromName("CRC-CCITT"); // resolves to CRC16_KERMIT
// 4. Custom variant (not in the RevEng catalogue): a 12-bit CRC with bespoke parameters.
var custom = new CrcStandard(
name: "CRC-12/MY-SPEC",
size: 12,
polynomial: 0x80F,
initialValue: 0x000,
reflectIn: false,
reflectOut: false,
xOrOut: 0x000);
Remarks
Every CRC variant in the wild differs from every other along the same five dimensions - the polynomial, the initial
register value, whether input bits are reflected as they enter the register, whether the final register is reflected
on the way out, and what value the register is XOR-ed with at the end. CRC-32/ISO-HDLC and
CRC-32/BZIP2 share a polynomial but disagree on reflection; CRC-16/MODBUS and CRC-16/USB share
nearly everything but the initial value. CrcStandard captures one such parameter bundle as an
immutable, equatable, serializable value, decoupling the choice of variant from the engine that runs it.
Three ways to obtain a standard.
- Catalogue properties CRC32_ISOHDLC, CRC32_ISCSI, CRC16_MODBUS, CRC16_KERMIT, CRC64_XZ, … - direct named accessors for the common entries that return the canonical, cached CrcStandard instance.
- Get(CrcStandards) Materializes any catalogue entry from its CrcStandards enum value - useful when the choice is data-driven (e.g. read from configuration).
- FromName(string)
Resolves canonical names and published aliases -
"CRC-32","PKZIP","CRC-32/XZ","CRC-32/ADCCP"all return the same CRC32_ISOHDLC instance. - Direct construction The CrcStandard(string, int, ulong, ulong, bool, bool, ulong) constructor accepts custom parameters for variants that are not in the RevEng catalogue. Width must lie within MinSize.. MaxSize.
How it pairs with Crc. The hash engine reads Polynomial and ReflectIn to fetch (or build) a precomputed lookup table from CrcLookupTableCache, seeds the running register with InitialValue, and applies ReflectOut + XOrOut when emitting the final digest. Multiple Crc instances configured with the same CrcStandard share the same lookup table, so creating fresh engines for the same standard is inexpensive.
Equality and serialization. CrcStandard is a value-style type: two instances are equal iff every parameter matches (the Name is informational only and does not affect identity). ISerializable support is provided so a chosen standard can survive process boundaries unchanged.
Coverage and limits. Sizes are constrained to MinSize..MaxSize bits;
any wider variant in the RevEng catalogue (currently only CRC-82/DARC) is intentionally omitted because it
will not fit in the ulong-backed register. Instances are immutable and therefore safe to share across
threads.
Constructors
CrcStandard(string, int, ulong, ulong, bool, bool, ulong)
Initializes a new instance of the CrcStandard class with the specified parameters.
public CrcStandard(string name, int size, ulong polynomial, ulong initialValue, bool reflectIn, bool reflectOut, ulong xOrOut)
Parameters
namestringThe name of the CRC standard.
sizeintThe size, in bits, of the CRC checksum.
polynomialulongThe CRC polynomial value.
initialValueulongThe initial value used for the CRC calculation.
reflectInboolIndicates whether to reflect the input during the CRC calculation.
reflectOutboolIndicates whether to reflect the output during the CRC calculation.
xOrOutulongThe value to XOR the final output with.
Exceptions
- ArgumentException
nameis null or empty.- ArgumentOutOfRangeException
sizeis less than MinSize or greater than MaxSize; orpolynomial,initialValue, orxOrOutdoes not fit insizebits.
Fields
MaxSize
The maximum size allowed for a CRC standard (in bits).
public const int MaxSize = 64
Field Value
MinSize
The minimum size allowed for a CRC standard (in bits).
public const int MinSize = 1
Field Value
Properties
All
Gets a snapshot of every supported CRC standard in the catalogue, in declaration order.
public static IReadOnlyList<CrcStandard> All { get; }
Property Value
- IReadOnlyList<CrcStandard>
A read-only list containing one CrcStandard per canonical entry.
Remarks
The backing array is built lazily on first access and then memoised, so merely loading this type does not construct any CrcStandard instances. Aliases are not included as separate entries; use FromName(string) to resolve an alias to its canonical instance.
CRC16_ARC
Gets the CRC-16/ARC CRC standard (aliases include CRC-16, CRC-IBM, CRC-16/LHA). The
de-facto generic 16-bit CRC.
public static CrcStandard CRC16_ARC { get; }
Property Value
- See Also
CRC16_IBM3740
Gets the CRC-16/IBM-3740 CRC standard (aliases CRC-16/AUTOSAR, CRC-16/CCITT-FALSE). The
widely-used "CCITT-false" variant.
public static CrcStandard CRC16_IBM3740 { get; }
Property Value
- See Also
CRC16_KERMIT
Gets the CRC-16/KERMIT CRC standard (aliases CRC-16/BLUETOOTH, CRC-16/CCITT,
CRC-CCITT). Used by Kermit and Bluetooth.
public static CrcStandard CRC16_KERMIT { get; }
Property Value
- See Also
CRC16_MODBUS
Gets the CRC-16/MODBUS CRC standard (alias MODBUS). Used by Modbus RTU over serial.
public static CrcStandard CRC16_MODBUS { get; }
Property Value
- See Also
CRC16_XMODEM
Gets the CRC-16/XMODEM CRC standard (aliases CRC-16/ACORN, XMODEM, ZMODEM). Used by
XMODEM and ZMODEM.
public static CrcStandard CRC16_XMODEM { get; }
Property Value
- See Also
CRC32_BZIP2
Gets the CRC-32/BZIP2 CRC standard (aliases CRC-32/AAL5, CRC-32/DECT-B, B-CRC-32).
Used by bzip2 and AAL5.
public static CrcStandard CRC32_BZIP2 { get; }
Property Value
- See Also
CRC32_ISCSI
Gets the CRC-32/ISCSI CRC standard (aliases CRC-32C, CRC-32/CASTAGNOLI, CRC-32/NVME,
CRC-32/BASE91-C, CRC-32/INTERLAKEN). Castagnoli polynomial used by iSCSI, SCTP, Btrfs, ext4, NVMe,
and many modern protocols.
public static CrcStandard CRC32_ISCSI { get; }
Property Value
- See Also
CRC32_ISOHDLC
Gets the CRC-32/ISO-HDLC CRC standard (aliases CRC-32, PKZIP, CRC-32/XZ,
CRC-32/ADCCP, CRC-32/V-42). The canonical CRC-32 used by zlib, PNG, Ethernet, PKZIP, and the
default standard used by Crc().
public static CrcStandard CRC32_ISOHDLC { get; }
Property Value
Remarks
Width 32, polynomial 0x04C11DB7, initial value 0xFFFFFFFF, reflected input and output, final XOR
0xFFFFFFFF.
- See Also
CRC64_ECMA182
Gets the CRC-64/ECMA-182 CRC standard (alias CRC-64). Specified by ECMA-182 for DLT tape formats.
public static CrcStandard CRC64_ECMA182 { get; }
Property Value
- See Also
CRC64_XZ
Gets the CRC-64/XZ CRC standard (alias CRC-64/GO-ECMA). Used by the XZ compressed file format and
the Go crc64 ECMA table.
public static CrcStandard CRC64_XZ { get; }
Property Value
- See Also
CRC8_MAXIMDOW
Gets the CRC-8/MAXIM-DOW CRC standard (aliases CRC-8/MAXIM, DOW-CRC). Used by Maxim /
Dallas 1-Wire devices.
public static CrcStandard CRC8_MAXIMDOW { get; }
Property Value
- See Also
CRC8_SMBUS
Gets the CRC-8/SMBUS CRC standard (alias CRC-8). Width 8, polynomial 0x07, initial value
0x00, no reflection, XOR out 0x00.
public static CrcStandard CRC8_SMBUS { get; }
Property Value
Remarks
The canonical "CRC-8" used by SMBus (System Management Bus) and the de-facto generic 8-bit CRC.
- See Also
InitialValue
Gets the initial value used in the CRC calculation.
public ulong InitialValue { get; }
Property Value
- ulong
The initial value for the CRC calculation.
Name
Gets the name of the CRC standard.
public string Name { get; }
Property Value
- string
The name of the CRC algorithm.
Polynomial
Gets the polynomial used in the CRC calculation.
public ulong Polynomial { get; }
Property Value
- ulong
The polynomial value used in the CRC calculation.
ReflectIn
Gets a value indicating whether the input data is reflected during the CRC calculation.
public bool ReflectIn { get; }
Property Value
ReflectOut
Gets a value indicating whether the CRC result is reflected before XORing with XOrOut.
public bool ReflectOut { get; }
Property Value
Size
Gets the size, in bits, of the CRC checksum.
public int Size { get; }
Property Value
- int
The size of the CRC in bits.
XOrOut
Gets the value to XOR the final CRC result with.
public ulong XOrOut { get; }
Property Value
- ulong
The XOR value for the final CRC result.
Methods
Equals(CrcStandard?)
Determines whether the current CrcStandard object is equal to another CrcStandard object.
public bool Equals(CrcStandard? other)
Parameters
otherCrcStandardThe other CrcStandard object to compare.
Returns
- bool
true if
otherhas the same identity-bearing parameters ( Size, Polynomial, InitialValue, ReflectIn, ReflectOut, and XOrOut); otherwise, false. Name is informational and is intentionally excluded from equality.
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
FromName(string)
Resolves a catalogue entry by its canonical name or any of its published aliases.
public static CrcStandard FromName(string name)
Parameters
namestringThe canonical CRC standard name, or an alias. Comparison is ordinal and case-sensitive.
Returns
- CrcStandard
The shared CrcStandard instance for
name.
Exceptions
- ArgumentNullException
nameis null.- KeyNotFoundException
No catalogue entry matching
nameexists.
Get(CrcStandards)
Materialises - or retrieves from cache - the CrcStandard identified by the given enum value.
public static CrcStandard Get(CrcStandards standard)
Parameters
standardCrcStandardsThe catalogue entry to resolve. Must be a defined CrcStandards value.
Returns
- CrcStandard
The shared CrcStandard instance for
standard.
Remarks
The first call for a given value constructs a new CrcStandard from the packed catalogue data and races to publish it into the per-entry cache. Subsequent calls - including concurrent calls - return the same instance, so reference equality is stable.
Exceptions
- ArgumentOutOfRangeException
standardis not a defined enum value.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
TryFromName(string?, out CrcStandard?)
Attempts to resolve a catalogue entry by its canonical name or alias without throwing.
public static bool TryFromName(string? name, out CrcStandard? standard)
Parameters
namestringThe canonical CRC standard name or alias to look up.
standardCrcStandardWhen this method returns, contains the matching CrcStandard if found; otherwise, null.
Returns
Explicit Interface Implementations
ISerializable.GetObjectData(SerializationInfo, StreamingContext)
Populates a SerializationInfo with the data needed to serialize the target object.
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoThe SerializationInfo to populate with data.
contextStreamingContextThe destination (see StreamingContext) for this serialization.
Exceptions
- SecurityException
The caller does not have the required permission.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |