-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBytesUtils.sol
42 lines (36 loc) · 1.37 KB
/
BytesUtils.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
// SPDX-FileCopyrightText: 2021 Lido <[email protected]>
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;
/// @author psirex
/// @notice Contains methods to extract primitive types from bytes
library BytesUtils {
function bytes24At(bytes memory data, uint256 location) internal pure returns (bytes24 result) {
uint256 word = uint256At(data, location);
assembly {
result := word
}
}
function addressAt(bytes memory data, uint256 location) internal pure returns (address result) {
uint256 word = uint256At(data, location);
assembly {
result := div(
and(word, 0xffffffffffffffffffffffffffffffffffffffff000000000000000000000000),
0x1000000000000000000000000
)
}
}
function uint32At(bytes memory _data, uint256 _location) internal pure returns (uint32 result) {
uint256 word = uint256At(_data, _location);
assembly {
result := div(
and(word, 0xffffffff00000000000000000000000000000000000000000000000000000000),
0x100000000000000000000000000000000000000000000000000000000
)
}
}
function uint256At(bytes memory data, uint256 location) internal pure returns (uint256 result) {
assembly {
result := mload(add(data, add(0x20, location)))
}
}
}