Skip to content
Merged
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
26 changes: 26 additions & 0 deletions src/utils/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,32 @@ library LibBytes {
}
}

/// @dev Checks if `x` is in `a`. Assumes `a` has been checked.
function checkInCalldata(bytes calldata x, bytes calldata a) internal pure {
/// @solidity memory-safe-assembly
assembly {
if or(
or(lt(x.offset, a.offset), gt(add(x.offset, x.length), add(a.length, a.offset))),
shr(64, or(x.length, x.offset))
) { revert(0x00, 0x00) }
}
}

/// @dev Checks if `x` is in `a`. Assumes `a` has been checked.
function checkInCalldata(bytes[] calldata x, bytes calldata a) internal pure {
/// @solidity memory-safe-assembly
assembly {
let e := sub(add(a.length, a.offset), 0x20)
if or(lt(x.offset, a.offset), shr(64, x.offset)) { revert(0x00, 0x00) }
for { let i := 0 } iszero(eq(x.length, i)) { i := add(i, 1) } {
let o := calldataload(add(x.offset, shl(5, i)))
let t := add(o, x.offset)
let l := calldataload(t)
if or(shr(64, or(l, o)), gt(add(t, l), e)) { revert(0x00, 0x00) }
}
}
}

/// @dev Returns empty calldata bytes. For silencing the compiler.
function emptyCalldata() internal pure returns (bytes calldata result) {
/// @solidity memory-safe-assembly
Expand Down
26 changes: 26 additions & 0 deletions src/utils/g/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,32 @@ library LibBytes {
}
}

/// @dev Checks if `x` is in `a`. Assumes `a` has been checked.
function checkInCalldata(bytes calldata x, bytes calldata a) internal pure {
/// @solidity memory-safe-assembly
assembly {
if or(
or(lt(x.offset, a.offset), gt(add(x.offset, x.length), add(a.length, a.offset))),
shr(64, or(x.length, x.offset))
) { revert(0x00, 0x00) }
}
}

/// @dev Checks if `x` is in `a`. Assumes `a` has been checked.
function checkInCalldata(bytes[] calldata x, bytes calldata a) internal pure {
/// @solidity memory-safe-assembly
assembly {
let e := sub(add(a.length, a.offset), 0x20)
if or(lt(x.offset, a.offset), shr(64, x.offset)) { revert(0x00, 0x00) }
for { let i := 0 } iszero(eq(x.length, i)) { i := add(i, 1) } {
let o := calldataload(add(x.offset, shl(5, i)))
let t := add(o, x.offset)
let l := calldataload(t)
if or(shr(64, or(l, o)), gt(add(t, l), e)) { revert(0x00, 0x00) }
}
}
}

/// @dev Returns empty calldata bytes. For silencing the compiler.
function emptyCalldata() internal pure returns (bytes calldata result) {
/// @solidity memory-safe-assembly
Expand Down
44 changes: 44 additions & 0 deletions test/LibBytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -373,4 +373,48 @@ contract LibBytesTest is SoladyTest {
assertEq(uint160(LibBytes.msbToAddress(x)), msb);
assertEq(uint160(LibBytes.lsbToAddress(x)), lsb);
}

function testCheckInCalldata(bytes memory child) public view {
this.checkInCalldata(child, abi.encode(child));
}

function testCheckInCalldata() public pure {
LibBytes.checkInCalldata(msg.data, msg.data);
}

function checkInCalldata(bytes calldata expectedChild, bytes calldata encoded) public pure {
bytes calldata child;
/// @solidity memory-safe-assembly
assembly {
child.offset := add(0x20, add(encoded.offset, calldataload(encoded.offset)))
child.length := calldataload(add(encoded.offset, calldataload(encoded.offset)))
}
LibBytes.checkInCalldata(child, encoded);
LibBytes.checkInCalldata(child, msg.data);
LibBytes.checkInCalldata(encoded, msg.data);
require(keccak256(expectedChild) == keccak256(child));
}

function testCheckInCalldata(bytes[] memory children) public view {
this.checkInCalldata(children, abi.encode(children));
}

function checkInCalldata(bytes[] calldata expectedChildren, bytes calldata encoded)
public
pure
{
bytes[] calldata children;
/// @solidity memory-safe-assembly
assembly {
children.offset := add(0x20, add(encoded.offset, calldataload(encoded.offset)))
children.length := calldataload(add(encoded.offset, calldataload(encoded.offset)))
}
LibBytes.checkInCalldata(children, encoded);
LibBytes.checkInCalldata(expectedChildren, msg.data);
LibBytes.checkInCalldata(children, msg.data);
require(expectedChildren.length == children.length);
for (uint256 i; i < children.length; ++i) {
require(keccak256(expectedChildren[i]) == keccak256(children[i]));
}
}
}