-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Expand file tree
/
Copy pathAbiDecode.sol
More file actions
63 lines (57 loc) · 2.5 KB
/
AbiDecode.sol
File metadata and controls
63 lines (57 loc) · 2.5 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import {Calldata} from "./Calldata.sol";
import {Memory} from "./Memory.sol";
/// @dev Utilities to decode ABI-encoded data without reverting.
library AbiDecode {
using Memory for *;
/**
* @dev Attempts to decode a `bytes` value from a bytes input (in memory). Returns a boolean indicating success
* and a slice pointing to the decoded buffer. If decoding fails, returns an empty slice.
*/
function tryDecodeBytes(bytes memory input) internal pure returns (bool success, Memory.Slice output) {
return tryDecodeBytes(input.asSlice());
}
/**
* @dev Attempts to decode a `bytes` value from a Memory.Slice input. Returns a boolean indicating success and a
* slice pointing to the decoded buffer. If decoding fails, returns an empty slice.
*/
function tryDecodeBytes(Memory.Slice input) internal pure returns (bool success, Memory.Slice output) {
unchecked {
uint256 inputLength = input.length();
if (inputLength < 0x20) {
return (false, Memory.emptySlice());
}
uint256 offset = uint256(input.load(0));
if (inputLength - 0x20 < offset) {
return (false, Memory.emptySlice());
}
uint256 length = uint256(input.load(offset));
if (inputLength - 0x20 - offset < length) {
return (false, Memory.emptySlice());
}
return (true, input.slice(0x20 + offset, length));
}
}
/**
* @dev Attempts to decode a `bytes` value from a bytes input (in calldata). Returns a boolean indicating success
* and a slice pointing to the decoded buffer. If decoding fails, returns an empty slice.
*/
function tryDecodeBytesCalldata(bytes calldata input) internal pure returns (bool success, bytes calldata output) {
unchecked {
uint256 inputLength = input.length;
if (inputLength < 0x20) {
return (false, Calldata.emptyBytes());
}
uint256 offset = uint256(bytes32(input[0x00:0x20]));
if (inputLength - 0x20 < offset) {
return (false, Calldata.emptyBytes());
}
uint256 length = uint256(bytes32(input[offset:offset + 0x20]));
if (inputLength - 0x20 - offset < length) {
return (false, Calldata.emptyBytes());
}
return (true, input[0x20 + offset:0x20 + offset + length]);
}
}
}