-
Notifications
You must be signed in to change notification settings - Fork 7
/
AddressChunks.sol
46 lines (43 loc) · 1.38 KB
/
AddressChunks.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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.22;
/**
* @title AddressChunks
* @author @xtremetom
* @notice Reads chunk pointers and merges their values
*/
library AddressChunks {
function mergeChunks(address[] memory chunks)
internal
view
returns (bytes memory o_code)
{
unchecked {
assembly {
let len := mload(chunks)
let totalSize := 0x20
let size := 0
o_code := mload(0x40)
// loop through all chunk addresses
// - get address
// - get data size
// - get code and add to o_code
// - update total size
let targetChunk := 0
for {
let i := 0
} lt(i, len) {
i := add(i, 1)
} {
targetChunk := mload(add(chunks, add(0x20, mul(i, 0x20))))
size := sub(extcodesize(targetChunk), 1)
extcodecopy(targetChunk, add(o_code, totalSize), 1, size)
totalSize := add(totalSize, size)
}
// update o_code size
mstore(o_code, sub(totalSize, 0x20))
// store o_code
mstore(0x40, add(o_code, and(add(totalSize, 0x1f), not(0x1f))))
}
}
}
}