Rename bytes.rawAddress and add bytes.contentAddress

This commit is contained in:
Remco Bloemen 2018-06-13 17:20:18 +02:00
parent 88982f98ff
commit 2054cd78da
2 changed files with 25 additions and 8 deletions

View File

@ -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);

View File

@ -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)
@ -42,6 +45,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.
/// @param source memory address to copy bytes from.
@ -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
);
}