-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,51 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.13; | ||
|
||
type CalldataReader is uint256; | ||
|
||
using CalldataReaderLib for CalldataReader global; | ||
|
||
/// @author philogy <https://github.com/philogy> | ||
library CalldataReaderLib { | ||
function from(bytes calldata data) internal pure returns (CalldataReader reader) { | ||
assembly { | ||
reader := data.offset | ||
} | ||
} | ||
|
||
function shifted(CalldataReader reader, uint256 n) internal pure returns (CalldataReader newReader) { | ||
assembly { | ||
newReader := add(reader, n) | ||
} | ||
} | ||
|
||
function readBool(CalldataReader reader) internal pure returns (bool value) { | ||
assembly { | ||
value := byte(0, calldataload(reader)) | ||
} | ||
} | ||
|
||
function readU16(CalldataReader reader) internal pure returns (uint16 value) { | ||
assembly { | ||
value := shr(240, calldataload(reader)) | ||
} | ||
} | ||
|
||
function readU40(CalldataReader reader) internal pure returns (uint40 value) { | ||
assembly { | ||
value := shr(216, calldataload(reader)) | ||
} | ||
} | ||
|
||
function readU64(CalldataReader reader) internal pure returns (uint64 value) { | ||
assembly { | ||
value := shr(192, calldataload(reader)) | ||
} | ||
} | ||
|
||
function readU128(CalldataReader reader) internal pure returns (uint128 value) { | ||
assembly { | ||
value := shr(128, calldataload(reader)) | ||
} | ||
} | ||
} |