Zarr.NET  0.6.1
Zarr reader and writer in .NET
Loading...
Searching...
No Matches
ZarrNET.Crc32cCodec Class Referencesealed

The "crc32c" bytes-to-bytes codec (Zarr v3 spec). More...

Inheritance diagram for ZarrNET.Crc32cCodec:
ZarrNET.IZarrCodec

Public Member Functions

Task< byte[]> DecodeAsync (byte[] input, CancellationToken ct=default)
 Decodes bytes produced by the previous codec step.
 
Task< byte[]> EncodeAsync (byte[] input, CancellationToken ct=default)
 Encodes bytes for the next codec step.
 
- Public Member Functions inherited from ZarrNET.IZarrCodec

Properties

string Name [get]
 Codec name as it appears in zarr.json (e.g. "gzip", "zstd", "bytes").
 
- Properties inherited from ZarrNET.IZarrCodec

Detailed Description

The "crc32c" bytes-to-bytes codec (Zarr v3 spec).

On encode: computes CRC32C over the input and appends 4 bytes (little-endian). On decode: reads the trailing 4-byte checksum, validates it against the payload, and returns the payload without the checksum bytes.

This codec is commonly used as an index codec in sharding configurations to protect the shard index integrity.

Uses a self-contained CRC32C implementation (Castagnoli polynomial 0x1EDC6F41) with no external NuGet dependencies.

Member Function Documentation

◆ DecodeAsync()

Task< byte[]> ZarrNET.Crc32cCodec.DecodeAsync ( byte[] input,
CancellationToken ct = default )

Decodes bytes produced by the previous codec step.

Implements ZarrNET.IZarrCodec.

25 {
26 ct.ThrowIfCancellationRequested();
27
28 if (input.Length < 4)
29 throw new InvalidOperationException(
30 $"crc32c decode: input is {input.Length} bytes, need at least 4 for the checksum.");
31
32 var payloadLength = input.Length - 4;
33 var storedHash = (uint)(input[payloadLength]
34 | (input[payloadLength + 1] << 8)
35 | (input[payloadLength + 2] << 16)
36 | (input[payloadLength + 3] << 24));
37
38 var computedHash = Crc32CHash.Compute(input, 0, payloadLength);
39
40 if (computedHash != storedHash)
41 throw new InvalidOperationException(
42 $"crc32c checksum mismatch: stored 0x{storedHash:X8}, " +
43 $"computed 0x{computedHash:X8}. The data may be corrupted.");
44
45 var result = new byte[payloadLength];
46 Array.Copy(input, 0, result, 0, payloadLength);
47
48 return Task.FromResult(result);
49 }

◆ EncodeAsync()

Task< byte[]> ZarrNET.Crc32cCodec.EncodeAsync ( byte[] input,
CancellationToken ct = default )

Encodes bytes for the next codec step.

Implements ZarrNET.IZarrCodec.

52 {
53 ct.ThrowIfCancellationRequested();
54
55 var hash = Crc32CHash.Compute(input, 0, input.Length);
56 var result = new byte[input.Length + 4];
57
58 Array.Copy(input, 0, result, 0, input.Length);
59
60 // Append little-endian uint32
61 result[input.Length] = (byte)(hash);
62 result[input.Length + 1] = (byte)(hash >> 8);
63 result[input.Length + 2] = (byte)(hash >> 16);
64 result[input.Length + 3] = (byte)(hash >> 24);
65
66 return Task.FromResult(result);
67 }

Property Documentation

◆ Name

string ZarrNET.Crc32cCodec.Name
get

Codec name as it appears in zarr.json (e.g. "gzip", "zstd", "bytes").

Implements ZarrNET.IZarrCodec.


The documentation for this class was generated from the following file: