Skip to content
Open
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
19 changes: 16 additions & 3 deletions docs/utils/libzip.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ FastLZ and calldata operations for convenience.

<!-- customintro:start --><!-- customintro:end -->

## Custom Errors

### TruncatedFastLZ()

```solidity
error TruncatedFastLZ()
```

The FastLZ payload ends before an instruction is complete.

## Fast LZ Operations

LZ77 implementation based on FastLZ.
Expand Down Expand Up @@ -39,7 +49,8 @@ function flzDecompress(bytes memory data)
returns (bytes memory result)
```

Returns the decompressed `data`.
Returns the decompressed `data`.
Reverts with `TruncatedFastLZ` if an instruction runs past the end of `data`.

## Calldata Operations

Expand Down Expand Up @@ -74,7 +85,8 @@ function cdDecompress(bytes memory data)
returns (bytes memory result)
```

Returns the decompressed `data`.
Returns the decompressed `data`.
A trailing `0x00` marker with no control byte is not expanded.

### cdFallback()

Expand All @@ -89,4 +101,5 @@ receive() external payable {}
Silence compiler warning to add a `receive` function.
```
For efficiency, this function will directly return the results, terminating the context.
If called internally, it must be called at the end of the function.
If called internally, it must be called at the end of the function.
A trailing `0x00` marker with no control byte is not expanded.
24 changes: 23 additions & 1 deletion src/utils/LibZip.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ pragma solidity ^0.8.4;
/// The accompanying solady.js library includes implementations of
/// FastLZ and calldata operations for convenience.
library LibZip {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// @dev The FastLZ payload ends before an instruction is complete.
error TruncatedFastLZ();

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* FAST LZ OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
Expand Down Expand Up @@ -108,9 +115,14 @@ library LibZip {
}

/// @dev Returns the decompressed `data`.
/// Reverts with `TruncatedFastLZ` if an instruction runs past the end of `data`.
function flzDecompress(bytes memory data) internal pure returns (bytes memory result) {
/// @solidity memory-safe-assembly
assembly {
function revertTruncated() {
mstore(0x00, 0x8bb83a49) // `TruncatedFastLZ()`.
revert(0x1c, 0x04)
}
result := mload(0x40)
let op := add(result, 0x20)
let end := add(add(data, 0x20), mload(data))
Expand All @@ -119,13 +131,17 @@ library LibZip {
let c := byte(0, w)
let t := shr(5, c)
if iszero(t) {
// Literal run: control byte plus `c + 1` payload bytes.
if lt(sub(end, data), add(2, c)) { revertTruncated() }
mstore(op, mload(add(data, 1)))
data := add(data, add(2, c))
op := add(op, add(1, c))
continue
}
let g := eq(t, 7)
// Short match is 2 bytes. Long match (`t == 7`) is 3 bytes.
if lt(sub(end, data), add(2, g)) { revertTruncated() }
for {
let g := eq(t, 7)
let l := add(2, xor(t, mul(g, xor(t, add(7, byte(1, w)))))) // M
let s := add(add(shl(8, and(0x1f, c)), byte(add(1, g), w)), 1) // R
let r := sub(op, s)
Expand Down Expand Up @@ -240,6 +256,7 @@ library LibZip {
}

/// @dev Returns the decompressed `data`.
/// A trailing `0x00` marker with no control byte is not expanded.
function cdDecompress(bytes memory data) internal pure returns (bytes memory result) {
/// @solidity memory-safe-assembly
assembly {
Expand All @@ -254,6 +271,8 @@ library LibZip {
for { let i := add(0x20, data) } 1 {} {
let c := mload(i)
if iszero(byte(0, c)) {
// Marker with no control byte. Do not read past `end`.
if iszero(lt(add(i, 1), end)) { break }
c := add(1, byte(1, c))
if iszero(gt(c, 0x80)) {
i := add(i, 2)
Expand Down Expand Up @@ -297,6 +316,7 @@ library LibZip {
/// ```
/// For efficiency, this function will directly return the results, terminating the context.
/// If called internally, it must be called at the end of the function.
/// A trailing `0x00` marker with no control byte is not expanded.
function cdFallback() internal {
/// @solidity memory-safe-assembly
assembly {
Expand All @@ -307,6 +327,8 @@ library LibZip {
let c := byte(0, xor(add(i, f), calldataload(i)))
i := add(i, 1)
if iszero(c) {
// Marker with no control byte. Do not read past calldata.
if iszero(lt(i, calldatasize())) { break }
let d := byte(0, xor(add(i, f), calldataload(i)))
i := add(i, 1)
// Fill with either 0xff or 0x00.
Expand Down
24 changes: 23 additions & 1 deletion src/utils/clz/LibZip.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ pragma solidity ^0.8.30;
/// The accompanying solady.js library includes implementations of
/// FastLZ and calldata operations for convenience.
library LibZip {
/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* CUSTOM ERRORS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/

/// @dev The FastLZ payload ends before an instruction is complete.
error TruncatedFastLZ();

/*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/
/* FAST LZ OPERATIONS */
/*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/
Expand Down Expand Up @@ -108,9 +115,14 @@ library LibZip {
}

/// @dev Returns the decompressed `data`.
/// Reverts with `TruncatedFastLZ` if an instruction runs past the end of `data`.
function flzDecompress(bytes memory data) internal pure returns (bytes memory result) {
/// @solidity memory-safe-assembly
assembly {
function revertTruncated() {
mstore(0x00, 0x8bb83a49) // `TruncatedFastLZ()`.
revert(0x1c, 0x04)
}
result := mload(0x40)
let op := add(result, 0x20)
let end := add(add(data, 0x20), mload(data))
Expand All @@ -119,13 +131,17 @@ library LibZip {
let c := byte(0, w)
let t := shr(5, c)
if iszero(t) {
// Literal run: control byte plus `c + 1` payload bytes.
if lt(sub(end, data), add(2, c)) { revertTruncated() }
mstore(op, mload(add(data, 1)))
data := add(data, add(2, c))
op := add(op, add(1, c))
continue
}
let g := eq(t, 7)
// Short match is 2 bytes. Long match (`t == 7`) is 3 bytes.
if lt(sub(end, data), add(2, g)) { revertTruncated() }
for {
let g := eq(t, 7)
let l := add(2, xor(t, mul(g, xor(t, add(7, byte(1, w)))))) // M
let s := add(add(shl(8, and(0x1f, c)), byte(add(1, g), w)), 1) // R
let r := sub(op, s)
Expand Down Expand Up @@ -238,6 +254,7 @@ library LibZip {
}

/// @dev Returns the decompressed `data`.
/// A trailing `0x00` marker with no control byte is not expanded.
function cdDecompress(bytes memory data) internal pure returns (bytes memory result) {
/// @solidity memory-safe-assembly
assembly {
Expand All @@ -252,6 +269,8 @@ library LibZip {
for { let i := add(0x20, data) } 1 {} {
let c := mload(i)
if iszero(byte(0, c)) {
// Marker with no control byte. Do not read past `end`.
if iszero(lt(add(i, 1), end)) { break }
c := add(1, byte(1, c))
if iszero(gt(c, 0x80)) {
i := add(i, 2)
Expand Down Expand Up @@ -295,6 +314,7 @@ library LibZip {
/// ```
/// For efficiency, this function will directly return the results, terminating the context.
/// If called internally, it must be called at the end of the function.
/// A trailing `0x00` marker with no control byte is not expanded.
function cdFallback() internal {
/// @solidity memory-safe-assembly
assembly {
Expand All @@ -305,6 +325,8 @@ library LibZip {
let c := byte(0, xor(add(i, f), calldataload(i)))
i := add(i, 1)
if iszero(c) {
// Marker with no control byte. Do not read past calldata.
if iszero(lt(i, calldatasize())) { break }
let d := byte(0, xor(add(i, f), calldataload(i)))
i := add(i, 1)
// Fill with either 0xff or 0x00.
Expand Down
Loading