diff --git a/src/utils/LibZip.sol b/src/utils/LibZip.sol index 9fe31a8771..a0e6bf6af0 100644 --- a/src/utils/LibZip.sol +++ b/src/utils/LibZip.sol @@ -228,16 +228,6 @@ library LibZip { function cdDecompress(bytes memory data) internal pure returns (bytes memory result) { /// @solidity memory-safe-assembly assembly { - function countLeadingZeroBytes(x_) -> _r { - _r := shl(7, lt(0xffffffffffffffffffffffffffffffff, x_)) - _r := or(_r, shl(6, lt(0xffffffffffffffff, shr(_r, x_)))) - _r := or(_r, shl(5, lt(0xffffffff, shr(_r, x_)))) - _r := or(_r, shl(4, lt(0xffff, shr(_r, x_)))) - _r := xor(31, or(shr(3, _r), lt(0xff, shr(_r, x_)))) - } - function min(x_, y_) -> _z { - _z := xor(x_, mul(xor(x_, y_), lt(y_, x_))) - } if mload(data) { result := mload(0x40) let s := add(data, 4) @@ -265,10 +255,17 @@ library LibZip { } mstore(o, c) c := not(or(or(add(and(c, m), m), c), m)) - c := add(iszero(c), countLeadingZeroBytes(c)) - o := add(min(sub(end, i), c), o) - i := add(c, i) - if iszero(lt(i, end)) { break } + let r := shl(7, lt(0xffffffffffffffffffffffffffffffff, c)) + r := or(r, shl(6, lt(0xffffffffffffffff, shr(r, c)))) + // forgefmt: disable-next-item + c := add(iszero(c), xor(byte(and(0x1f, shr(byte(24, mul(0x02040810204081, shr(r, c))), + 0x8421084210842108cc6318c6db6d54be)), + 0x1819191a191d1a1b191d1c1d1a1b1c1e191a1d1a1c1c1b1e1a1a1c1b1f1f1f1f), shr(3, r))) + o := add(o, c) + i := add(i, c) + if lt(i, end) { continue } + if gt(i, end) { o := sub(o, sub(i, end)) } + break } mstore(s, v) // Restore the first 4 bytes. mstore(result, sub(o, add(result, 0x20))) // Store the length.