-
Notifications
You must be signed in to change notification settings - Fork 28
Compression libraries: FastLZ, LZ4 and Snappy #218
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
Open
Amxx
wants to merge
1
commit into
master
Choose a base branch
from
feature/decompression
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
/** | ||
* @dev Library for decompressing data using FastLZ. | ||
* | ||
* See https://ariya.github.io/FastLZ/ | ||
*/ | ||
library FastLZ { | ||
/** | ||
* @dev FastLZ level 1 decompression. | ||
* | ||
* Based on the reference implementation available here: | ||
* https://github.com/ariya/FastLZ?tab=readme-ov-file#decompressor-reference-implementation | ||
*/ | ||
function decompress(bytes memory input) internal pure returns (bytes memory output) { | ||
assembly ("memory-safe") { | ||
let inputPtr := add(input, 0x20) | ||
let inputEnd := add(add(input, 0x20), mload(input)) | ||
|
||
// Use new memory allocate at the FMP | ||
output := mload(0x40) | ||
let outputPtr := add(output, 0x20) | ||
|
||
for {} lt(inputPtr, inputEnd) {} { | ||
let chunk := mload(inputPtr) | ||
let first := byte(0, chunk) | ||
let type_ := shr(5, first) | ||
|
||
switch type_ | ||
case 0 { | ||
mstore(outputPtr, mload(add(inputPtr, 1))) | ||
inputPtr := add(inputPtr, add(2, first)) | ||
outputPtr := add(outputPtr, add(1, first)) | ||
} | ||
case 7 { | ||
let len := add(9, byte(1, chunk)) | ||
for { | ||
let i := 0 | ||
let ofs := add(add(shl(8, and(first, 31)), byte(2, chunk)), 1) | ||
let ref := sub(outputPtr, ofs) | ||
let step := xor(len, mul(lt(ofs, len), xor(ofs, len))) | ||
} lt(i, len) { | ||
i := add(i, step) | ||
} { | ||
mcopy(add(outputPtr, i), add(ref, i), step) | ||
} | ||
inputPtr := add(inputPtr, 3) | ||
outputPtr := add(outputPtr, len) | ||
} | ||
default { | ||
let len := add(2, type_) | ||
for { | ||
let i := 0 | ||
let ofs := add(add(shl(8, and(first, 31)), byte(1, chunk)), 1) | ||
let ref := sub(outputPtr, ofs) | ||
let step := xor(len, mul(lt(ofs, len), xor(ofs, len))) | ||
} lt(i, len) { | ||
i := add(i, step) | ||
} { | ||
mcopy(add(outputPtr, i), add(ref, i), step) | ||
} | ||
inputPtr := add(inputPtr, 2) | ||
outputPtr := add(outputPtr, len) | ||
} | ||
} | ||
if iszero(eq(inputPtr, inputEnd)) { | ||
revert(0, 0) | ||
} | ||
|
||
mstore(output, sub(outputPtr, add(output, 0x20))) | ||
mstore(0x40, outputPtr) | ||
} | ||
} | ||
|
||
function decompressCalldata(bytes calldata input) internal pure returns (bytes memory output) { | ||
assembly ("memory-safe") { | ||
let inputPtr := input.offset | ||
let inputEnd := add(input.offset, input.length) | ||
|
||
// Use new memory allocate at the FMP | ||
output := mload(0x40) | ||
let outputPtr := add(output, 0x20) | ||
|
||
for {} lt(inputPtr, inputEnd) {} { | ||
let chunk := calldataload(inputPtr) | ||
let first := byte(0, chunk) | ||
let type_ := shr(5, first) | ||
|
||
switch type_ | ||
case 0 { | ||
mstore(outputPtr, calldataload(add(inputPtr, 1))) | ||
inputPtr := add(inputPtr, add(2, first)) | ||
outputPtr := add(outputPtr, add(1, first)) | ||
} | ||
case 7 { | ||
let len := add(9, byte(1, chunk)) | ||
for { | ||
let i := 0 | ||
let ofs := add(add(shl(8, and(first, 31)), byte(2, chunk)), 1) | ||
let ref := sub(outputPtr, ofs) | ||
let step := xor(len, mul(lt(ofs, len), xor(ofs, len))) | ||
} lt(i, len) { | ||
i := add(i, step) | ||
} { | ||
mcopy(add(outputPtr, i), add(ref, i), step) | ||
} | ||
inputPtr := add(inputPtr, 3) | ||
outputPtr := add(outputPtr, len) | ||
} | ||
default { | ||
let len := add(2, type_) | ||
for { | ||
let i := 0 | ||
let ofs := add(add(shl(8, and(first, 31)), byte(1, chunk)), 1) | ||
let ref := sub(outputPtr, ofs) | ||
let step := xor(len, mul(lt(ofs, len), xor(ofs, len))) | ||
} lt(i, len) { | ||
i := add(i, step) | ||
} { | ||
mcopy(add(outputPtr, i), add(ref, i), step) | ||
} | ||
inputPtr := add(inputPtr, 2) | ||
outputPtr := add(outputPtr, len) | ||
} | ||
} | ||
if iszero(eq(inputPtr, inputEnd)) { | ||
revert(0, 0) | ||
} | ||
|
||
mstore(output, sub(outputPtr, add(output, 0x20))) | ||
mstore(0x40, outputPtr) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix FastLZ type‑7 byte ordering.
For type‑7 matches the format is
ctrl | offsetLow | lengthExtra
. The code currently readsbyte(1, chunk)
as the length byte andbyte(2, chunk)
as the offset byte, then advancesinputPtr
by 3. This swaps the order: valid streams produced by the reference encoder/LibZip place the offset immediately afterctrl
, so we subtract the wrong offset (using the length byte) and read the real offset as the next chunk’s first byte. Result: long matches (>=9 bytes) decode garbage or revert when the pointer check at Line 68 trips.Reorder the reads so we pull the offset from
byte(1, chunk)
, the length extension frombyte(2, chunk)
, and only consume the extra length byte when we actually use it. One possible fix:(and mirror the change in the calldata loop).
📝 Committable suggestion
🤖 Prompt for AI Agents