-
Notifications
You must be signed in to change notification settings - Fork 11
/
UnsafeBytesCalldata.sol
51 lines (45 loc) · 1.89 KB
/
UnsafeBytesCalldata.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// SPDX-License-Identifier: MIT
pragma solidity 0.8.20;
/**
* @author Matter Labs
* @custom:security-contact security@matterlabs.dev
* @dev The library provides a set of functions that help read data from calldata bytes.
* @dev Each of the functions accepts the `bytes calldata` and the offset where data should be read and returns a value of a certain type.
*
* @dev WARNING!
* 1) Functions don't check the length of the bytes array, so it can go out of bounds.
* The user of the library must check for bytes length before using any functions from the library!
*
* 2) Read variables are not cleaned up - https://docs.soliditylang.org/en/v0.8.16/internals/variable_cleanup.html.
* Using data in inline assembly can lead to unexpected behavior!
*/
library UnsafeBytesCalldata {
function readUint16(bytes calldata _bytes, uint256 _start) internal pure returns (uint16 result) {
assembly {
let offset := sub(_bytes.offset, 30)
result := calldataload(add(offset, _start))
}
}
function readUint32(bytes calldata _bytes, uint256 _start) internal pure returns (uint32 result) {
assembly {
let offset := sub(_bytes.offset, 28)
result := calldataload(add(offset, _start))
}
}
function readUint64(bytes calldata _bytes, uint256 _start) internal pure returns (uint64 result) {
assembly {
let offset := sub(_bytes.offset, 24)
result := calldataload(add(offset, _start))
}
}
function readBytes32(bytes calldata _bytes, uint256 _start) internal pure returns (bytes32 result) {
assembly {
result := calldataload(add(_bytes.offset, _start))
}
}
function readUint256(bytes calldata _bytes, uint256 _start) internal pure returns (uint256 result) {
assembly {
result := calldataload(add(_bytes.offset, _start))
}
}
}