Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle null chunk type using enum value #83

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Snappier/Internal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ public enum ChunkType : byte
UncompressedData = 0x01,
SkippableChunk = 0x80, // If this bit is set, we can safely skip the chunk if unknown
Padding = 0xfe,
StreamIdentifier = 0xff
StreamIdentifier = 0xff,

// This is not part of the spec, but having this extra value representing null avoids
// the cost of wrapping in a Nullable<T>
Null = 0xfd,
}

public const byte Literal = 0b00;
Expand Down
10 changes: 5 additions & 5 deletions Snappier/Internal/SnappyStreamDecompressor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal sealed class SnappyStreamDecompressor : IDisposable

private readonly byte[] _scratch = new byte[ScratchBufferSize];
private int _scratchLength;
private Constants.ChunkType? _chunkType;
private Constants.ChunkType _chunkType = Constants.ChunkType.Null;
private int _chunkSize;
private int _chunkBytesProcessed;
private uint _expectedChunkCrc;
Expand All @@ -40,7 +40,7 @@ public int Decompress(Span<byte> buffer)
// ReSharper disable once SwitchStatementHandlesSomeKnownEnumValuesWithDefault
switch (_chunkType)
{
case null:
case Constants.ChunkType.Null:
// Not in a chunk, read the chunk type and size

uint rawChunkHeader = ReadChunkHeader(ref input);
Expand Down Expand Up @@ -106,7 +106,7 @@ public int Decompress(Span<byte> buffer)
if (_decompressor.EndOfFile)
{
// Completed reading the chunk
_chunkType = null;
_chunkType = Constants.ChunkType.Null;

uint crc = Crc32CAlgorithm.ApplyMask(_chunkCrc);
if (_expectedChunkCrc != crc)
Expand Down Expand Up @@ -149,7 +149,7 @@ public int Decompress(Span<byte> buffer)
if (_chunkBytesProcessed >= _chunkSize)
{
// Completed reading the chunk
_chunkType = null;
_chunkType = Constants.ChunkType.Null;

uint crc = Crc32CAlgorithm.ApplyMask(_chunkCrc);
if (_expectedChunkCrc != crc)
Expand All @@ -176,7 +176,7 @@ public int Decompress(Span<byte> buffer)
if (_chunkBytesProcessed >= _chunkSize)
{
// Completed reading the chunk
_chunkType = null;
_chunkType = Constants.ChunkType.Null;
}

break;
Expand Down