|
| 1 | +/// @title Bytes |
| 2 | +/// @notice SPDX-License-Identifier: MIT |
| 3 | +/// @author Franfran <https://github.com/iFrostizz> |
| 4 | +/// @notice Low-level operations on bytes |
| 5 | +/// @notice Adapted from BytesLib (https://github.com/GNSPS/solidity-bytes-utils/blob/master/contracts/BytesLib.sol) |
| 6 | + |
| 7 | +/// @notice Concatenate two bytes arrays |
| 8 | +/// @notice Takes in two pointers of the bytes to concatenate that must be sorted |
| 9 | +/// @return Pointer of the new appended concatenated bytes array in the memory |
| 10 | +/// @dev Warning! This assumes that the pointer in the memory of the second bytes chunk is after mem_ptr1 + 0x20 |
| 11 | +#define macro CONCAT_MEMORY() = takes(2) returns(1) { |
| 12 | + // input stack // [mem_ptr1, mem_ptr2] |
| 13 | + |
| 14 | + // setup stack and memory for the next iterations |
| 15 | + dup2 mload swap1 // [mem_ptr1, len2, mem_ptr2] |
| 16 | + msize swap1 // [mem_ptr1, free_loc_pos, len2, mem_ptr2] |
| 17 | + dup1 mload dup4 // [len2, len1, mem_ptr1, free_loc_pos, len2, mem_ptr2] |
| 18 | + dup2 add msize mstore // [len1, mem_ptr1, free_loc_pos, len2, mem_ptr2] |
| 19 | + |
| 20 | + swap1 0x20 add // [index(i), len1, free_loc_pos, len2, mem_ptr2] |
| 21 | + msize // [index(j), index(i), len1, free_loc_pos, len2, mem_ptr2] |
| 22 | + swap2 0x00 // [is_sec_loop, len1, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 23 | + |
| 24 | + // i is the index where we get (mload) the array element and j is the index where we store (mstore) the array at j |
| 25 | + loop: // [is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 26 | + dup2 iszero empty_slot jumpi // [is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 27 | + |
| 28 | + dup3 mload // [word, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 29 | + dup3 0x20 gt iszero // [is_full_slot, word, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 30 | + full_slot jumpi // [word, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 31 | + |
| 32 | + // else it's not a full slot, we're hitting an end. Then clean memory slot and update j with a partial length |
| 33 | + dup3 0x20 sub // [pad_len, word, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 34 | + 0x08 mul swap1 dup2 // [shift, word, shift, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 35 | + shr // [left_padded_word, shift, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 36 | + swap1 shl // [clean_word, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 37 | + dup5 mstore // [is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 38 | + swap3 add swap2 // [is_sec_loop, index(i), index(j + 1), free_loc_pos, len2, mem_ptr2] |
| 39 | + |
| 40 | + // here we check if current loop is for the 2nd array |
| 41 | + swap1 pop // [is_sec_loop, index(j + 1), free_loc_pos, len2, mem_ptr2] |
| 42 | + iszero bridge jumpi // [index(j + 1), free_loc_pos, len2, mem_ptr2] |
| 43 | + pop break jump // [free_loc_pos, len2, mem_ptr2] |
| 44 | + |
| 45 | + empty_slot: // [is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 46 | + swap2 pop pop // [is_sec_loop, index(j), free_loc_pos, len2, mem_ptr2] |
| 47 | + iszero bridge jumpi // [index(j), free_loc_pos, len2, mem_ptr2] |
| 48 | + pop break jump // [free_loc_pos, len2, mem_ptr2] |
| 49 | + |
| 50 | + bridge: // [index(j), free_loc_pos, len2, mem_ptr2] |
| 51 | + dup4 0x20 add // [index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 52 | + dup5 // [len2, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 53 | + 0x01 // [is_sec_loop, len2, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 54 | + loop jump |
| 55 | + |
| 56 | + full_slot: // [word, is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 57 | + dup5 mstore // [is_sec_loop, len_left, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 58 | + swap1 0x20 swap1 sub // [len_left - 0x20, is_sec_loop, index(i), index(j), free_loc_pos, len2, mem_ptr2] |
| 59 | + swap2 0x20 add // [index(i + 1), is_sec_loop, len_left - 0x20, index(j), free_loc_pos, len2, mem_ptr2] |
| 60 | + swap3 0x20 add // [index(j + 1), is_sec_loop, len_left - 0x20, index(i + 1), free_loc_pos, len2, mem_ptr2] |
| 61 | + swap3 swap2 swap1 // [is_sec_loop, len_left - 0x20, index(i + 1), index(j + 1), free_loc_pos, len2, mem_ptr2] |
| 62 | + loop jump |
| 63 | + |
| 64 | + break: // [free_loc_pos, len2, mem_ptr2] |
| 65 | + swap2 pop pop // [free_loc_pos] |
| 66 | +} |
| 67 | + |
| 68 | +/// @param Pointer in memory of the start of the bytes array |
| 69 | +/// @param Start position of the slice relative to the array |
| 70 | +/// @param Length of the output slice |
| 71 | +/// @return Pointer of the new appended concatenated bytes array in the memory |
| 72 | +/// @dev Warning! This assumes that the length of the output slice is less or equal the length of the bytes array (bytes.len < slice.len) |
| 73 | +/// @dev Warning! This assumes that the start of the bytes array is not out of bounds (start < len + mem_ptr) |
| 74 | +#define macro SLICE_MEMORY() = takes(3) returns(1) { |
| 75 | + // input stack // [mem_ptr, start, length] |
| 76 | + |
| 77 | + msize dup4 msize mstore // [free_loc_pos, mem_ptr, start, length] |
| 78 | + msize swap4 // [length, free_loc_pos, mem_ptr, start, index(j)] |
| 79 | + // index(i) = mem_ptr + start + 0x20 |
| 80 | + swap1 swap3 // [start, length, mem_ptr, free_loc_pos, index(j)] |
| 81 | + swap1 swap2 // [mem_ptr, start, length, free_loc_pos, index(j)] |
| 82 | + 0x20 add add // [index(i), length, free_loc_pos, index(j)] |
| 83 | + |
| 84 | + // we load our slice chunk at i and store it in a free memory location at j |
| 85 | + loop: // [index(i), length_left, free_loc_pos, index(j)] |
| 86 | + dup1 mload // [slice_chunk, index(i), length_left, free_loc_pos, index(j)] |
| 87 | + |
| 88 | + // if current is not full slot, then load the last bytes and break |
| 89 | + 0x20 dup4 lt // [is_not_full_slot, slice_chunk, index(i), length_left, free_loc_pos, index(j)] |
| 90 | + break jumpi // [slice_chunk, index(i), length_left, free_loc_pos, index(j)] |
| 91 | + |
| 92 | + dup5 mstore // [index(i), length_left, free_loc_pos, index(j)] |
| 93 | + |
| 94 | + 0x20 add swap3 // [free_loc, length_left, free_loc_pos, index(i+1)] |
| 95 | + 0x20 add swap3 // [index(i+1), length_left, free_loc_pos, free_loc + 1] |
| 96 | + swap1 0x20 // [0x20, length_left, index(i+1), free_loc_pos, free_loc + 1] |
| 97 | + swap1 sub // [length_left - 1, index(i+1), free_loc_pos, free_loc + 1] |
| 98 | + swap1 // [index(i+1), length_left - 1, free_loc_pos, free_loc + 1] |
| 99 | + |
| 100 | + loop jump |
| 101 | + |
| 102 | + break: // [slice_chunk, index(i), length, free_loc_pos, index(j)] |
| 103 | + // store the remaining length |
| 104 | + dup3 0x20 sub // [zero_length, slice_chunk, index(i), length, free_loc_pos, index(j)] |
| 105 | + 0x08 mul swap1 dup2 // [shift, slice_chunk, shift, index(i), length, free_loc_pos, index(j)] |
| 106 | + shr // [left_pad_slice, shift, index(i), length, free_loc_pos, index(j)] |
| 107 | + swap1 shl // [slice_chunk, index(i), length, free_loc_pos, index(j)] |
| 108 | + dup5 mstore // [index(i), length, free_loc_pos, index(j)] |
| 109 | + |
| 110 | + pop pop swap1 pop // [free_loc_pos] |
| 111 | +} |
0 commit comments