47 lines
1.8 KiB
Solidity
47 lines
1.8 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
|
|
pragma solidity ^0.8.13;
|
|
|
|
/**
|
|
* @author Matter Labs
|
|
* @custom:security-contact security@matterlabs.dev
|
|
* @dev The library provides a set of functions that help read data from an "abi.encodePacked" byte array.
|
|
* @dev Each of the functions accepts the `bytes memory` 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 UnsafeBytes {
|
|
function readUint32(bytes memory _bytes, uint256 _start) internal pure returns (uint32 result, uint256 offset) {
|
|
assembly {
|
|
offset := add(_start, 4)
|
|
result := mload(add(_bytes, offset))
|
|
}
|
|
}
|
|
|
|
function readAddress(bytes memory _bytes, uint256 _start) internal pure returns (address result, uint256 offset) {
|
|
assembly {
|
|
offset := add(_start, 20)
|
|
result := mload(add(_bytes, offset))
|
|
}
|
|
}
|
|
|
|
function readUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256 result, uint256 offset) {
|
|
assembly {
|
|
offset := add(_start, 32)
|
|
result := mload(add(_bytes, offset))
|
|
}
|
|
}
|
|
|
|
function readBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32 result, uint256 offset) {
|
|
assembly {
|
|
offset := add(_start, 32)
|
|
result := mload(add(_bytes, offset))
|
|
}
|
|
}
|
|
}
|