aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/utils/contracts/test/TestLibBytes/TestLibBytes.sol
blob: 00d861e6133dd451a29bc04fc54a5910bcdda04e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*

  Copyright 2018 ZeroEx Intl.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

*/

pragma solidity 0.4.24;

import "../../utils/LibBytes/LibBytes.sol";


contract TestLibBytes {
    
    using LibBytes for bytes;

    /// @dev Pops the last byte off of a byte array by modifying its length.
    /// @param b Byte array that will be modified.
    /// @return The byte that was popped off.
    function publicPopLastByte(bytes memory b)
        public
        pure
        returns (bytes memory, bytes1 result)
    {
        result = b.popLastByte();
        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.
    /// @return True if arrays are the same. False otherwise.
    function publicEquals(bytes memory lhs, bytes memory rhs)
        public
        pure
        returns (bool equal)
    {
        equal = lhs.equals(rhs);
        return equal;
    }
    
    function publicEqualsPop1(bytes memory lhs, bytes memory rhs)
        public
        pure
        returns (bool equal)
    {
        lhs.popLastByte();
        rhs.popLastByte();
        equal = lhs.equals(rhs);
        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.
    /// @return address from byte array.
    function publicReadAddress(
        bytes memory b,
        uint256 index
    )
        public
        pure
        returns (address result)
    {
        result = b.readAddress(index);
        return result;
    }

    /// @dev Writes an address into a specific position in a byte array.
    /// @param b Byte array to insert address into.
    /// @param index Index in byte array of address.
    /// @param input Address to put into byte array.
    function publicWriteAddress(
        bytes memory b,
        uint256 index,
        address input
    )
        public
        pure
        returns (bytes memory)
    {
        b.writeAddress(index, input);
        return b;
    }

    /// @dev Reads a bytes32 value from a position in a byte array.
    /// @param b Byte array containing a bytes32 value.
    /// @param index Index in byte array of bytes32 value.
    /// @return bytes32 value from byte array.
    function publicReadBytes32(
        bytes memory b,
        uint256 index
    )
        public
        pure
        returns (bytes32 result)
    {
        result = b.readBytes32(index);
        return result;
    }

    /// @dev Writes a bytes32 into 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 bytes32 to put into byte array.
    function publicWriteBytes32(
        bytes memory b,
        uint256 index,
        bytes32 input
    )
        public
        pure
        returns (bytes memory)
    {
        b.writeBytes32(index, input);
        return b;
    }

    /// @dev Reads a uint256 value from a position in a byte array.
    /// @param b Byte array containing a uint256 value.
    /// @param index Index in byte array of uint256 value.
    /// @return uint256 value from byte array.
    function publicReadUint256(
        bytes memory b,
        uint256 index
    )
        public
        pure
        returns (uint256 result)
    {
        result = b.readUint256(index);
        return result;
    }

    /// @dev Writes a uint256 into 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 uint256 to put into byte array.
    function publicWriteUint256(
        bytes memory b,
        uint256 index,
        uint256 input
    )
        public
        pure
        returns (bytes memory)
    {
        b.writeUint256(index, input);
        return b;
    }

    /// @dev Reads an unpadded bytes4 value from a position in a byte array.
    /// @param b Byte array containing a bytes4 value.
    /// @param index Index in byte array of bytes4 value.
    /// @return bytes4 value from byte array.
    function publicReadBytes4(
        bytes memory b,
        uint256 index
    )
        public
        pure
        returns (bytes4 result)
    {
        result = b.readBytes4(index);
        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>.
    /// @param source Source offset into <mem>.
    /// @param length Length of bytes to copy from <source> to <dest>
    /// @return mem Memory contents after calling memCopy.
    function testMemcpy(
        bytes mem,
        uint256 dest,
        uint256 source,
        uint256 length
    )
        public // not external, we need input in memory
        pure
        returns (bytes)
    {
        // Sanity check. Overflows are not checked.
        require(source + length <= mem.length);
        require(dest + length <= mem.length);

        // Get pointer to memory contents
        uint256 offset = mem.contentAddress();

        // Execute memCopy adjusted for memory array location
        LibBytes.memCopy(offset + dest, offset + source, length);

        // Return modified memory contents
        return mem;
    }
}