diff options
author | Remco Bloemen <remco@wicked.ventures> | 2018-06-13 23:20:18 +0800 |
---|---|---|
committer | Remco Bloemen <remco@wicked.ventures> | 2018-06-23 19:53:38 +0800 |
commit | 2054cd78da9c39e5572cd76bb9fff7a4cd581903 (patch) | |
tree | 8ceaa5148f2b82ea4b1985521af364526e3588f0 /packages/contracts | |
parent | 88982f98ff2ece93831b1442d2c4e1ef0878d919 (diff) | |
download | dexon-sol-tools-2054cd78da9c39e5572cd76bb9fff7a4cd581903.tar dexon-sol-tools-2054cd78da9c39e5572cd76bb9fff7a4cd581903.tar.gz dexon-sol-tools-2054cd78da9c39e5572cd76bb9fff7a4cd581903.tar.bz2 dexon-sol-tools-2054cd78da9c39e5572cd76bb9fff7a4cd581903.tar.lz dexon-sol-tools-2054cd78da9c39e5572cd76bb9fff7a4cd581903.tar.xz dexon-sol-tools-2054cd78da9c39e5572cd76bb9fff7a4cd581903.tar.zst dexon-sol-tools-2054cd78da9c39e5572cd76bb9fff7a4cd581903.zip |
Rename bytes.rawAddress and add bytes.contentAddress
Diffstat (limited to 'packages/contracts')
-rw-r--r-- | packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol | 2 | ||||
-rw-r--r-- | packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol | 31 |
2 files changed, 25 insertions, 8 deletions
diff --git a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol index 84b9e4673..299452c96 100644 --- a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol +++ b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol @@ -243,7 +243,7 @@ contract TestLibBytes { require(dest + length <= mem.length); // Get pointer to memory contents - uint256 offset = mem.getMemAddress() + 32; + uint256 offset = mem.contentAddress(); // Execute memCopy adjusted for memory array location LibBytes.memCopy(offset + dest, offset + source, length); diff --git a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol index ad3a35aef..d824449a2 100644 --- a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol +++ b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol @@ -19,6 +19,7 @@ pragma solidity ^0.4.24; library LibBytes { + using LibBytes for bytes; // Revert reasons string constant GREATER_THAN_ZERO_LENGTH_REQUIRED = "GREATER_THAN_ZERO_LENGTH_REQUIRED"; @@ -30,8 +31,10 @@ library LibBytes { /// @dev Gets the memory address for a byte array. /// @param input Byte array to lookup. - /// @return memoryAddress Memory address of byte array. - function getMemAddress(bytes memory input) + /// @return memoryAddress Memory address of byte array. This + /// points to the header of the byte array which contains + /// the length. + function rawAddress(bytes memory input) internal pure returns (uint256 memoryAddress) @@ -41,6 +44,20 @@ library LibBytes { } return memoryAddress; } + + /// @dev Gets the memory address for the contents of a byte array. + /// @param input Byte array to lookup. + /// @return memoryAddress Memory address of the contents of the byte array. + function contentAddress(bytes memory input) + internal + pure + returns (uint256 memoryAddress) + { + assembly { + memoryAddress := add(input, 32) + } + return memoryAddress; + } /// @dev Copies `length` bytes from memory location `source` to `dest`. /// @param dest memory address to copy bytes to. @@ -393,8 +410,8 @@ library LibBytes { // Allocate memory and copy value to result result = new bytes(nestedBytesLength); memCopy( - getMemAddress(result) + 32, // +32 skips array length - getMemAddress(b) + index + 32, + result.contentAddress(), + b.contentAddress() + index, nestedBytesLength ); @@ -422,9 +439,9 @@ library LibBytes { // Copy <input> into <b> memCopy( - getMemAddress(b) + 32 + index, // +32 to skip length of <b> - getMemAddress(input), // includes length of <input> - input.length + 32 // +32 bytes to store <input> length + b.contentAddress() + index, + input.rawAddress(), // includes length of <input> + input.length + 32 // +32 bytes to store <input> length ); } |