From a5a7217c8f24fe98275fd9927ca8c5e5f140c6f3 Mon Sep 17 00:00:00 2001 From: Amir Bandeali Date: Wed, 9 May 2018 16:27:50 -0700 Subject: Add deepCopyBytes method to LibBytes --- .../current/test/TestLibBytes/TestLibBytes.sol | 17 +++++++++ .../contracts/current/utils/LibBytes/LibBytes.sol | 41 ++++++++++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'packages/contracts/src') diff --git a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol index 5a6801262..b3ed1f620 100644 --- a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol +++ b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol @@ -25,6 +25,23 @@ contract TestLibBytes is LibBytes { + /// @dev Performs a deep copy of a section of a byte array. + /// @param b Byte array that will be sliced. + /// @param startIndex Index of first byte to copy. + /// @param endIndex Index of last byte to copy + 1. + /// @return A deep copy of b from startIndex to endIndex. + function publicDeepCopyBytes( + bytes memory b, + uint256 startIndex, + uint256 endIndex) + public + pure + returns (bytes memory copy) + { + copy = deepCopyBytes(b, startIndex, endIndex); + return copy; + } + /// @dev Tests equality of two byte arrays. /// @param lhs First byte array to compare. /// @param rhs Second byte array to compare. diff --git a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol index 975565773..527f7a7a9 100644 --- a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol +++ b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol @@ -21,9 +21,50 @@ pragma solidity ^0.4.24; contract LibBytes { // Revert reasons + string constant GT_ZERO_LENGTH_REQUIRED = "Length must be greater than 0."; string constant GTE_4_LENGTH_REQUIRED = "Length must be greater than or equal to 4."; string constant GTE_20_LENGTH_REQUIRED = "Length must be greater than or equal to 20."; string constant GTE_32_LENGTH_REQUIRED = "Length must be greater than or equal to 32."; + string constant INDEX_OUT_OF_BOUNDS = "Specified array index is out of bounds."; + + /// @dev Performs a deep copy of a section of a byte array. + /// @param b Byte array that will be copied. + /// @param index Index of first byte to copy. + /// @param len Number of bytes to copy starting from index. + /// @return A deep copy `len` bytes of b starting from index. + function deepCopyBytes( + bytes memory b, + uint256 index, + uint256 len) + internal + pure + returns (bytes memory) + { + require( + len > 0, + GT_ZERO_LENGTH_REQUIRED + ); + require( + b.length >= index + len, + INDEX_OUT_OF_BOUNDS + ); + + bytes memory copy = new bytes(len); + + // Arrays are prefixed by a 256 bit length parameter + index += 32; + + assembly { + // Start storing onto copy after length field + let storeStartIndex := add(copy, 32) + // Start loading from b at index + let startLoadIndex := add(b, index) + for {let i := 0} lt(i, len) {i := add(i, 32)} { + mstore(add(storeStartIndex, i), mload(add(startLoadIndex, i))) + } + } + return copy; + } /// @dev Tests equality of two byte arrays. /// @param lhs First byte array to compare. -- cgit