Reformat / cleanup
This commit is contained in:
@@ -3,34 +3,34 @@ using System.Buffers.Binary;
|
||||
namespace ZB.MOM.WW.CBDD.Core.Compression;
|
||||
|
||||
/// <summary>
|
||||
/// Fixed header prefix for compressed payload blobs.
|
||||
/// Fixed header prefix for compressed payload blobs.
|
||||
/// </summary>
|
||||
public readonly struct CompressedPayloadHeader
|
||||
{
|
||||
public const int Size = 16;
|
||||
|
||||
/// <summary>
|
||||
/// Compression codec used for payload bytes.
|
||||
/// Compression codec used for payload bytes.
|
||||
/// </summary>
|
||||
public CompressionCodec Codec { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Original uncompressed payload length.
|
||||
/// Original uncompressed payload length.
|
||||
/// </summary>
|
||||
public int OriginalLength { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Compressed payload length.
|
||||
/// Compressed payload length.
|
||||
/// </summary>
|
||||
public int CompressedLength { get; }
|
||||
|
||||
/// <summary>
|
||||
/// CRC32 checksum of compressed payload bytes.
|
||||
/// CRC32 checksum of compressed payload bytes.
|
||||
/// </summary>
|
||||
public uint Checksum { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="CompressedPayloadHeader"/> class.
|
||||
/// Initializes a new instance of the <see cref="CompressedPayloadHeader" /> class.
|
||||
/// </summary>
|
||||
/// <param name="codec">Compression codec used for payload bytes.</param>
|
||||
/// <param name="originalLength">Original uncompressed payload length.</param>
|
||||
@@ -50,19 +50,20 @@ public readonly struct CompressedPayloadHeader
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create.
|
||||
/// Create.
|
||||
/// </summary>
|
||||
/// <param name="codec">Compression codec used for payload bytes.</param>
|
||||
/// <param name="originalLength">Original uncompressed payload length.</param>
|
||||
/// <param name="compressedPayload">Compressed payload bytes.</param>
|
||||
public static CompressedPayloadHeader Create(CompressionCodec codec, int originalLength, ReadOnlySpan<byte> compressedPayload)
|
||||
public static CompressedPayloadHeader Create(CompressionCodec codec, int originalLength,
|
||||
ReadOnlySpan<byte> compressedPayload)
|
||||
{
|
||||
var checksum = ComputeChecksum(compressedPayload);
|
||||
uint checksum = ComputeChecksum(compressedPayload);
|
||||
return new CompressedPayloadHeader(codec, originalLength, compressedPayload.Length, checksum);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write To.
|
||||
/// Write To.
|
||||
/// </summary>
|
||||
/// <param name="destination">Destination span that receives the serialized header.</param>
|
||||
public void WriteTo(Span<byte> destination)
|
||||
@@ -80,7 +81,7 @@ public readonly struct CompressedPayloadHeader
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read From.
|
||||
/// Read From.
|
||||
/// </summary>
|
||||
/// <param name="source">Source span containing a serialized header.</param>
|
||||
public static CompressedPayloadHeader ReadFrom(ReadOnlySpan<byte> source)
|
||||
@@ -89,14 +90,14 @@ public readonly struct CompressedPayloadHeader
|
||||
throw new ArgumentException($"Source must be at least {Size} bytes.", nameof(source));
|
||||
|
||||
var codec = (CompressionCodec)source[0];
|
||||
var originalLength = BinaryPrimitives.ReadInt32LittleEndian(source.Slice(4, 4));
|
||||
var compressedLength = BinaryPrimitives.ReadInt32LittleEndian(source.Slice(8, 4));
|
||||
var checksum = BinaryPrimitives.ReadUInt32LittleEndian(source.Slice(12, 4));
|
||||
int originalLength = BinaryPrimitives.ReadInt32LittleEndian(source.Slice(4, 4));
|
||||
int compressedLength = BinaryPrimitives.ReadInt32LittleEndian(source.Slice(8, 4));
|
||||
uint checksum = BinaryPrimitives.ReadUInt32LittleEndian(source.Slice(12, 4));
|
||||
return new CompressedPayloadHeader(codec, originalLength, compressedLength, checksum);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Validate Checksum.
|
||||
/// Validate Checksum.
|
||||
/// </summary>
|
||||
/// <param name="compressedPayload">Compressed payload bytes to validate.</param>
|
||||
public bool ValidateChecksum(ReadOnlySpan<byte> compressedPayload)
|
||||
@@ -105,10 +106,13 @@ public readonly struct CompressedPayloadHeader
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Compute Checksum.
|
||||
/// Compute Checksum.
|
||||
/// </summary>
|
||||
/// <param name="payload">Payload bytes.</param>
|
||||
public static uint ComputeChecksum(ReadOnlySpan<byte> payload) => Crc32Calculator.Compute(payload);
|
||||
public static uint ComputeChecksum(ReadOnlySpan<byte> payload)
|
||||
{
|
||||
return Crc32Calculator.Compute(payload);
|
||||
}
|
||||
|
||||
private static class Crc32Calculator
|
||||
{
|
||||
@@ -116,15 +120,15 @@ public readonly struct CompressedPayloadHeader
|
||||
private static readonly uint[] Table = CreateTable();
|
||||
|
||||
/// <summary>
|
||||
/// Compute.
|
||||
/// Compute.
|
||||
/// </summary>
|
||||
/// <param name="payload">Payload bytes.</param>
|
||||
public static uint Compute(ReadOnlySpan<byte> payload)
|
||||
{
|
||||
uint crc = 0xFFFFFFFFu;
|
||||
for (int i = 0; i < payload.Length; i++)
|
||||
var crc = 0xFFFFFFFFu;
|
||||
for (var i = 0; i < payload.Length; i++)
|
||||
{
|
||||
var index = (crc ^ payload[i]) & 0xFF;
|
||||
uint index = (crc ^ payload[i]) & 0xFF;
|
||||
crc = (crc >> 8) ^ Table[index];
|
||||
}
|
||||
|
||||
@@ -137,10 +141,7 @@ public readonly struct CompressedPayloadHeader
|
||||
for (uint i = 0; i < table.Length; i++)
|
||||
{
|
||||
uint value = i;
|
||||
for (int bit = 0; bit < 8; bit++)
|
||||
{
|
||||
value = (value & 1) != 0 ? (value >> 1) ^ Polynomial : value >> 1;
|
||||
}
|
||||
for (var bit = 0; bit < 8; bit++) value = (value & 1) != 0 ? (value >> 1) ^ Polynomial : value >> 1;
|
||||
|
||||
table[i] = value;
|
||||
}
|
||||
@@ -148,4 +149,4 @@ public readonly struct CompressedPayloadHeader
|
||||
return table;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user