Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Commit

Permalink
Merge pull request #2265 from 0xProject/fix/3.0/c18
Browse files Browse the repository at this point in the history
Fix/3.0/c18
  • Loading branch information
jalextowle committed Oct 15, 2019
2 parents 5128295 + 7283a16 commit 17faeae
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 526 deletions.
4 changes: 4 additions & 0 deletions contracts/utils/CHANGELOG.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@
{
"note": "Emit an event in `transferOwnership`",
"pr": 2253
},
{
"note": "Removed `deepCopyBytes`, `popLast20Bytes`, `readBytesWithLength`, and `writeBytesWithLength` in `LibBytes`.",
"pr": 2265
}
],
"timestamp": 1570135330
Expand Down
121 changes: 0 additions & 121 deletions contracts/utils/contracts/src/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -271,33 +271,6 @@ library LibBytes {
return result;
}

/// @dev Pops the last 20 bytes off of a byte array by modifying its length.
/// @param b Byte array that will be modified.
/// @return The 20 byte address that was popped off.
function popLast20Bytes(bytes memory b)
internal
pure
returns (address result)
{
if (b.length < 20) {
LibRichErrors.rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsTwentyRequired,
b.length,
20 // 20 is length of address
));
}

// Store last 20 bytes.
result = readAddress(b, b.length - 20);

assembly {
// Subtract 20 from byte array length.
let newLen := sub(mload(b), 20)
mstore(b, newLen)
}
return result;
}

/// @dev Tests equality of two byte arrays.
/// @param lhs First byte array to compare.
/// @param rhs Second byte array to compare.
Expand Down Expand Up @@ -523,100 +496,6 @@ library LibBytes {
return result;
}

/// @dev Reads nested bytes from a specific position.
/// @dev NOTE: the returned value overlaps with the input value.
/// Both should be treated as immutable.
/// @param b Byte array containing nested bytes.
/// @param index Index of nested bytes.
/// @return result Nested bytes.
function readBytesWithLength(
bytes memory b,
uint256 index
)
internal
pure
returns (bytes memory result)
{
// Read length of nested bytes
uint256 nestedBytesLength = readUint256(b, index);
index += 32;

// Assert length of <b> is valid, given
// length of nested bytes
if (b.length < index + nestedBytesLength) {
LibRichErrors.rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors
.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsNestedBytesLengthRequired,
b.length,
index + nestedBytesLength
));
}

// Return a pointer to the byte array as it exists inside `b`
assembly {
result := add(b, index)
}
return result;
}

/// @dev Inserts bytes at a specific position in a byte array.
/// @param b Byte array to insert <input> into.
/// @param index Index in byte array of <input>.
/// @param input bytes to insert.
function writeBytesWithLength(
bytes memory b,
uint256 index,
bytes memory input
)
internal
pure
{
// Assert length of <b> is valid, given
// length of input
if (b.length < index + 32 + input.length) {
LibRichErrors.rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors
.InvalidByteOperationErrorCodes.LengthGreaterThanOrEqualsNestedBytesLengthRequired,
b.length,
index + 32 + input.length // 32 bytes to store length
));
}

// Copy <input> into <b>
memCopy(
b.contentAddress() + index,
input.rawAddress(), // includes length of <input>
input.length + 32 // +32 bytes to store <input> length
);
}

/// @dev Performs a deep copy of a byte array onto another byte array of greater than or equal length.
/// @param dest Byte array that will be overwritten with source bytes.
/// @param source Byte array to copy onto dest bytes.
function deepCopyBytes(
bytes memory dest,
bytes memory source
)
internal
pure
{
uint256 sourceLen = source.length;
// Dest length must be >= source length, or some bytes would not be copied.
if (dest.length < sourceLen) {
LibRichErrors.rrevert(LibBytesRichErrors.InvalidByteOperationError(
LibBytesRichErrors
.InvalidByteOperationErrorCodes.DestinationLengthGreaterThanOrEqualSourceLengthRequired,
dest.length,
sourceLen
));
}
memCopy(
dest.contentAddress(),
source.contentAddress(),
sourceLen
);
}

/// @dev Writes a new length to a byte array.
/// Decreasing length will lead to removing the corresponding lower order bytes from the byte array.
/// Increasing length may lead to appending adjacent in-memory bytes to the end of the byte array.
Expand Down
63 changes: 1 addition & 62 deletions contracts/utils/contracts/test/TestLibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,6 @@ contract TestLibBytes {
return (b, result);
}

/// @dev Pops the last 20 bytes off of a byte array by modifying its length.
/// @param b Byte array that will be modified.
/// @return The 20 byte address that was popped off.
function publicPopLast20Bytes(bytes memory b)
public
pure
returns (bytes memory, address result)
{
result = b.popLast20Bytes();
return (b, result);
}

/// @dev Tests equality of two byte arrays.
/// @param lhs First byte array to compare.
/// @param rhs Second byte array to compare.
Expand All @@ -73,21 +61,6 @@ contract TestLibBytes {
return equal;
}

/// @dev Performs a deep copy of a byte array onto another byte array of greater than or equal length.
/// @param dest Byte array that will be overwritten with source bytes.
/// @param source Byte array to copy onto dest bytes.
function publicDeepCopyBytes(
bytes memory dest,
bytes memory source
)
public
pure
returns (bytes memory)
{
LibBytes.deepCopyBytes(dest, source);
return dest;
}

/// @dev Reads an address from a position in a byte array.
/// @param b Byte array containing an address.
/// @param index Index in byte array of address.
Expand Down Expand Up @@ -203,40 +176,6 @@ contract TestLibBytes {
return result;
}

/// @dev Reads nested bytes from a specific position.
/// @param b Byte array containing nested bytes.
/// @param index Index of nested bytes.
/// @return result Nested bytes.
function publicReadBytesWithLength(
bytes memory b,
uint256 index
)
public
pure
returns (bytes memory result)
{
result = b.readBytesWithLength(index);
return result;
}

/// @dev Inserts bytes at a specific position in a byte array.
/// @param b Byte array to insert <input> into.
/// @param index Index in byte array of <input>.
/// @param input bytes to insert.
/// @return b Updated input byte array
function publicWriteBytesWithLength(
bytes memory b,
uint256 index,
bytes memory input
)
public
pure
returns (bytes memory)
{
b.writeBytesWithLength(index, input);
return b;
}

/// @dev Copies a block of memory from one location to another.
/// @param mem Memory contents we want to apply memCopy to
/// @param dest Destination offset into <mem>.
Expand Down Expand Up @@ -305,7 +244,7 @@ contract TestLibBytes {
}

/// @dev Returns a byte array with an updated length.
/// @dev Writes a new length to a byte array.
/// @dev Writes a new length to a byte array.
/// Decreasing length will lead to removing the corresponding lower order bytes from the byte array.
/// Increasing length may lead to appending adjacent in-memory bytes to the end of the byte array.
/// @param b Bytes array to write new length to.
Expand Down
Loading

0 comments on commit 17faeae

Please sign in to comment.