MiloszKrajewski / K4os.Compression.LZ4

LZ4/LH4HC compression for .NET Standard 1.6/2.0 (formerly known as lz4net)
MIT License
675 stars 77 forks source link

Added Partial Decoding Support #90

Closed Sewer56 closed 1 year ago

Sewer56 commented 1 year ago

About

This pull request introduces public APIs to the LZ4Codec that introduce partial decompression support. Namely, it exposes the original LZ4's LZ4_decompress_safe_partial to the world.

The ability to perform partial decompression can be pretty useful in the presence of SOLID blocks; it's not currently here, but the ported code supports it; so I figure it'd be handy to expose it.

Added Public APIs

LZ4Codec

/// <summary>Decompresses data from the given buffer, stopping at <paramref name="targetLength"/>.</summary>
/// <param name="source">Input buffer.</param>
/// <param name="sourceLength">Input buffer length.</param>
/// <param name="target">Output buffer.</param>
/// <param name="targetLength">Output buffer length. Decoding stops at this amount.</param>
/// <returns>Number of bytes written, or a negative value if the output buffer is too small.</returns>
public static unsafe int DecodePartial(byte* source, int sourceLength, byte* target, int targetLength);

/// <summary>Decompresses data from the given buffer, stopping at the <paramref name="target"/> buffer length.</summary>
/// <param name="source">Input buffer.</param>
/// <param name="target">Output buffer. Decoding stops at the end of the buffer.</param>
/// <returns>Number of bytes written, or a negative value if the output buffer is too small.</returns>
public static unsafe int DecodePartial(ReadOnlySpan<byte> source, Span<byte> target);

I named these DecodePartial because the default is Decode, I wanted to keep the pattern from the original LZ4 library around.

Tests

Tests for partial decoding were added as PartialDecompressionTests.cs.
They cover a variety of test cases, including:

Miscellaneous

I made a minor fix for an incorrect error message in call to LZ4_decompress_safe_usingDict, when the function for the given target was not implemented.

MiloszKrajewski commented 1 year ago

Released in v1.3.7-beta