Repository navigation
Decode linked blocks in LZ4FrameInputStream - #198
Merged
Merged
Conversation
LZ4FrameInputStream rejected frames with linked blocks (FLG.B.Indep = 0), although they are the default of liblz4's frame API (LZ4F_blockLinked = 0). In such frames, a block may reference up to 64 KB of the data decoded before it in the same frame. LZ4FrameOutputStream still writes independent blocks only. The templates generate a separate decompressWithPrefix copy of the safe decoder, for byte arrays, whose matches may also reach the prefixLen bytes before destOff in dest. The existing decompress methods keep the same bytecode: a variant sharing their code made the safe decoder 3 to 5% slower on independent blocks with JDK 21 and 25, a code generation effect. JNI gets a new native, LZ4_decompress_safe_usingDict, for which liblz4 detects the prefix in place. The existing natives are unchanged. decompressWithPrefix is package-private and not abstract on LZ4SafeDecompressor. Implementations outside this package keep working, and reject a non-empty prefix. LZ4FrameInputStream decodes a linked block right after the data decoded before it in the frame, and moves the last 64 KB of that data to the start of the buffer only when the block doesn't fit after it. The buffer grows to at most 64 KB + the maximum block size, still only as data arrives. Blocks without history (independent blocks, first block of a linked frame) still go through decompress(), which also keeps them working with an older native library from java.library.path. FLG.fromByte accepts linked blocks; the FLG constructor, used by LZ4FrameOutputStream, still rejects them. LZ4FrameLinkedBlocksTest writes linked frames by hand along with their content: matches up to offset 65535 across several blocks, matches before the frame start, no history across frames, random concatenated frames. It also decodes frames written by the lz4 CLI with -BD (each block size, -BX, --content-size, -9), checked to depend on the history, with the three decompressors. New fuzz targets decompress with a prefix twice, with different data before the prefix and after the output, and compare; LZ4TestAccess gives them access to the package-private method. The frame fuzz targets get a seed with linked frames. Decoding speed on silesia, lz4 CLI frames, JDK 25: unchanged for independent blocks. Linked vs independent blocks of the same size: JNI -0.4% with 256 KB and 4 MB blocks, +8% with 64 KB blocks (better ratio); safe decoder -3% with 256 KB and 4 MB blocks, +8% with 64 KB blocks.
An older liblz4-java loaded from java.library.path lacks LZ4_decompress_safe_usingDict, so decoding a linked block with history threw UnsatisfiedLinkError out of LZ4FrameInputStream. Wrap it in an LZ4Exception, which the stream reports as an IOException, as it did for these frames before linked blocks were supported. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yawkat
enabled auto-merge (squash)
November 26, 2026 10:02
yawkat
disabled auto-merge
November 26, 2026 10:03
LZ4FrameInputStream capped the buffer of a linked frame at 64 KB + the maximum block size, and moved the 64 KB history to its start whenever a block didn't fit after it. With small blocks, this moved the history for almost every block: about 64 KB copied per block, whatever its size. A crafted 10 MB frame of 1-byte stored blocks copied 131 GB, and ordinary streams with frequent flushes decoded several times slower, as each compressed block of 258 bytes or more reserves room for a full block. Like liblz4's frame decoder, which keeps 128 KB before its output buffer, the buffer now grows to 2 * 64 KB + the maximum block size, and the history is only moved once at least 64 KB before it is no longer needed. Copying the history then costs at most one byte per decompressed byte. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Now that FLG.fromByte accepts linked blocks, FLG 0x40 (version 01, no flags, as written by lz4 -BD --no-frame-crc) has an empty BitSet, and toByte threw ArrayIndexOutOfBoundsException on bitSet.toByteArray()[0]. Before, B.Indep was always set, so the BitSet was never empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The move test only used small compressed blocks of one size, which reach the buffer cap within two blocks, and observed the history through the decompressor, so stored blocks were not covered. Moving the history for each stored block, a lower move threshold, or no buffer cap all went unnoticed. The tests now look at where LZ4FrameInputStream decodes each block in its buffer: a frame of 1-byte stored blocks, frames of compressed blocks of 316, 2000 and 40000 bytes, and a 4 MB frame for the buffer cap. Each compressed block starts with a match at the largest offset. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
FLG.fromByte shifted the sign-extended version bits, so FLG bytes 0x80 to 0xFF reported version 67108862 or 67108863 instead of 2 or 3. These frames were still rejected. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
LZ4FrameInputStream cleared the uncompressed bit of a block header before checking for the EndMark, so 0x80000000, an empty uncompressed block, ended the frame. The rest of the frame was then read as the next frame and failed. The frame format specifies that only 0x00000000 is an EndMark and that an empty uncompressed block doesn't end the frame, for example to be used as a keep-alive. liblz4 had the same bug, and fixed it in v1.9.3. lz4-java accepted 0x80000000 as an EndMark since 2017. No encoder is known to write it: liblz4, pierrec/lz4, lz4_flex and LZ4FrameOutputStream write 0. A frame ending with 0x80000000 now fails as truncated, as with liblz4. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yawkat
reviewed
Nov 26, 2026
The history move tests read the private buffer field of LZ4FrameInputStream by reflection. A package-private accessor now gives them the buffer instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
yawkat
enabled auto-merge (squash)
November 27, 2026 10:43
Owner
|
Thanks! I will release this once I've merged #189 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
LZ4FrameInputStream now decodes frames with linked blocks, the default of liblz4's frame API. Decompression only: LZ4FrameOutputStream still writes independent blocks.
Existing decoders are unchanged. Details, tests and measurements are in the commit message.
Closes #197