Repository navigation
Expand file tree
/
Copy pathByteUtil.java
More file actions
396 lines (341 loc) · 11.9 KB
/
Copy pathByteUtil.java
File metadata and controls
396 lines (341 loc) · 11.9 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (c) [2016] [ <ether.camp> ]
* This file is part of the ethereumJ library.
*
* The ethereumJ library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The ethereumJ library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with the ethereumJ library. If not, see <http://www.gnu.org/licenses/>.
*/
package org.tron.common.utils;
import com.google.common.base.Preconditions;
import com.google.common.primitives.UnsignedBytes;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.Arrays;
import org.spongycastle.util.encoders.Hex;
public class ByteUtil {
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final byte[] ZERO_BYTE_ARRAY = new byte[]{0};
/**
* return a cloned byte array. return null if parameter data is null
*/
public static byte[] cloneBytes(byte[] data) {
if (data == null) {
return null;
}
int length = data.length;
byte[] rc = new byte[length];
if (length > 0) {
System.arraycopy(data, 0, rc, 0, length);
}
return rc;
}
/**
* The regular {@link java.math.BigInteger#toByteArray()} method isn't quite what we often need:
* it appends a leading zero to indicate that the number is positive and may need padding.
*
* @param b the integer to format into a byte array
* @param numBytes the desired size of the resulting byte array
* @return numBytes byte long array.
*/
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes) {
if (b == null) {
return null;
}
byte[] bytes = new byte[numBytes];
byte[] biBytes = b.toByteArray();
int start = (biBytes.length == numBytes + 1) ? 1 : 0;
int length = Math.min(biBytes.length, numBytes);
System.arraycopy(biBytes, start, bytes, numBytes - length, length);
return bytes;
}
/**
* Omitting sign indication byte. <br><br> Instead of {@link org.spongycastle.util.BigIntegers#asUnsignedByteArray(BigInteger)}
* <br>we use this custom method to avoid an empty array in case of BigInteger.ZERO
*
* @param value - any big integer number. A <code>null</code>-value will return <code>null</code>
* @return A byte array without a leading zero byte if present in the signed encoding.
* BigInteger.ZERO will return an array with length 1 and byte-value 0.
*/
public static byte[] bigIntegerToBytes(BigInteger value) {
if (value == null) {
return null;
}
byte[] data = value.toByteArray();
if (data.length != 1 && data[0] == 0) {
byte[] tmp = new byte[data.length - 1];
System.arraycopy(data, 1, tmp, 0, tmp.length);
data = tmp;
}
return data;
}
/**
* merge arrays.
*
* @param arrays - arrays to merge
* @return - merged array
*/
public static byte[] merge(byte[]... arrays) {
int count = 0;
for (byte[] array : arrays) {
count += array.length;
}
// Create new array and copy all array contents
byte[] mergedArray = new byte[count];
int start = 0;
for (byte[] array : arrays) {
System.arraycopy(array, 0, mergedArray, start, array.length);
start += array.length;
}
return mergedArray;
}
/**
* Creates a copy of bytes and appends b to the end of it.
*/
public static byte[] appendByte(byte[] bytes, byte b) {
byte[] result = Arrays.copyOf(bytes, bytes.length + 1);
result[result.length - 1] = b;
return result;
}
/**
* Turn nibbles to a pretty looking output string Example. [ 1, 2, 3, 4, 5 ] becomes
* '\x11\x23\x45'
*
* @param nibbles - getting byte of data [ 04 ] and turning it to a '\x04' representation
* @return pretty string of nibbles
*/
public static String nibblesToPrettyString(byte[] nibbles) {
StringBuilder builder = new StringBuilder();
for (byte nibble : nibbles) {
final String nibbleString = oneByteToHexString(nibble);
builder.append("\\x").append(nibbleString);
}
return builder.toString();
}
/**
* get hex string data from byte data.
*/
public static String oneByteToHexString(byte value) {
String retVal = Integer.toString(value & 0xFF, 16);
if (retVal.length() == 1) {
retVal = "0" + retVal;
}
return retVal;
}
/**
* Convert a byte-array into a hex String.<br> Works similar to {@link Hex#toHexString} but allows
* for <code>null</code>
*
* @param data - byte-array to convert to a hex-string
* @return hex representation of the data.<br> Returns an empty String if the input is
* <code>null</code>
* @see Hex#toHexString
*/
public static String toHexString(byte[] data) {
return data == null ? "" : Hex.toHexString(data);
}
/**
* Cast hex encoded value from byte[] to int Limited to Integer.MAX_VALUE: 2^32-1 (4 bytes)
*
* @param b array contains the values
* @return unsigned positive int value.
*/
public static int byteArrayToInt(byte[] b) {
if (b == null || b.length == 0) {
return 0;
}
return new BigInteger(1, b).intValue();
}
public static boolean isSingleZero(byte[] array) {
return (array.length == 1 && array[0] == 0);
}
/**
* Converts a int value into a byte array.
*
* @param val - int value to convert
* @return value with leading byte that are zeroes striped
*/
public static byte[] intToBytesNoLeadZeroes(int val) {
if (val == 0) {
return EMPTY_BYTE_ARRAY;
}
int lenght = 0;
int tmpVal = val;
while (tmpVal != 0) {
tmpVal = tmpVal >>> 8;
++lenght;
}
byte[] result = new byte[lenght];
int index = result.length - 1;
while (val != 0) {
result[index] = (byte) (val & 0xFF);
val = val >>> 8;
index -= 1;
}
return result;
}
/**
* Converts int value into a byte array.
*
* @param val - int value to convert
* @return <code>byte[]</code> of length 4, representing the int value
*/
public static byte[] intToBytes(int val) {
return ByteBuffer.allocate(4).putInt(val).array();
}
/**
* Cast hex encoded value from byte[] to BigInteger null is parsed like byte[0]
*
* @param bb byte array contains the values
* @return unsigned positive BigInteger value.
*/
public static BigInteger bytesToBigInteger(byte[] bb) {
return (bb == null || bb.length == 0) ? BigInteger.ZERO : new BigInteger(1, bb);
}
/**
* Cast hex encoded value from byte[] to long null is parsed like byte[0]
*
* Limited to Long.MAX_VALUE: 2<sup>63</sup>-1 (8 bytes)
*
* @param b array contains the values
* @return unsigned positive long value.
*/
public static long byteArrayToLong(byte[] b) {
if (b == null || b.length == 0) {
return 0;
}
return new BigInteger(1, b).longValueExact();
}
public static int firstNonZeroByte(byte[] data) {
for (int i = 0; i < data.length; ++i) {
if (data[i] != 0) {
return i;
}
}
return -1;
}
public static byte[] stripLeadingZeroes(byte[] data) {
if (data == null) {
return null;
}
final int firstNonZero = firstNonZeroByte(data);
switch (firstNonZero) {
case -1:
return ZERO_BYTE_ARRAY;
case 0:
return data;
default:
byte[] result = new byte[data.length - firstNonZero];
System.arraycopy(data, firstNonZero, result, 0, data.length - firstNonZero);
return result;
}
}
/**
* Utility function to copy a byte array into a new byte array with given size. If the src length
* is smaller than the given size, the result will be left-padded with zeros.
*
* @param value - a BigInteger with a maximum value of 2^256-1
* @return Byte array of given size with a copy of the <code>src</code>
*/
public static byte[] copyToArray(BigInteger value) {
byte[] dest = ByteBuffer.allocate(32).array();
byte[] src = ByteUtil.bigIntegerToBytes(value);
if (src != null) {
System.arraycopy(src, 0, dest, dest.length - src.length, src.length);
}
return dest;
}
/**
* Returns a number of zero bits preceding the highest-order ("leftmost") one-bit interpreting
* input array as a big-endian integer value
*/
public static int numberOfLeadingZeros(byte[] bytes) {
int i = firstNonZeroByte(bytes);
if (i == -1) {
return bytes.length * 8;
} else {
int byteLeadingZeros = Integer.numberOfLeadingZeros((int) bytes[i] & 0xff) - 24;
return i * 8 + byteLeadingZeros;
}
}
/**
* Parses fixed number of bytes starting from {@code offset} in {@code input} array. If {@code
* input} has not enough bytes return array will be right padded with zero bytes. I.e. if {@code
* offset} is higher than {@code input.length} then zero byte array of length {@code len} will be
* returned
*/
public static byte[] parseBytes(byte[] input, int offset, int len) {
if (offset >= input.length || len == 0) {
return EMPTY_BYTE_ARRAY;
}
byte[] bytes = new byte[len];
System.arraycopy(input, offset, bytes, 0, Math.min(input.length - offset, len));
return bytes;
}
/**
* Parses 32-bytes word from given input. Uses {@link #parseBytes(byte[], int, int)} method, thus,
* result will be right-padded with zero bytes if there is not enough bytes in {@code input}
*
* @param idx an index of the word starting from {@code 0}
*/
public static byte[] parseWord(byte[] input, int idx) {
return parseBytes(input, 32 * idx, 32);
}
/**
* Parses 32-bytes word from given input. Uses {@link #parseBytes(byte[], int, int)} method, thus,
* result will be right-padded with zero bytes if there is not enough bytes in {@code input}
*
* @param idx an index of the word starting from {@code 0}
* @param offset an offset in {@code input} array to start parsing from
*/
public static byte[] parseWord(byte[] input, int offset, int idx) {
return parseBytes(input, offset + 32 * idx, 32);
}
public static boolean greater(byte[] bytes1, byte[] bytes2) {
return compare(bytes1, bytes2) > 0;
}
public static boolean greaterOrEquals(byte[] bytes1, byte[] bytes2) {
return compare(bytes1, bytes2) >= 0;
}
public static boolean less(byte[] bytes1, byte[] bytes2) {
return compare(bytes1, bytes2) < 0;
}
public static boolean lessOrEquals(byte[] bytes1, byte[] bytes2) {
return compare(bytes1, bytes2) <= 0;
}
public static boolean equals(byte[] bytes1, byte[] bytes2) {
return compare(bytes1, bytes2) == 0;
}
// lexicographical order
public static int compare(byte[] bytes1, byte[] bytes2) {
Preconditions.checkNotNull(bytes1);
Preconditions.checkNotNull(bytes2);
Preconditions.checkArgument(bytes1.length == bytes2.length);
int length = bytes1.length;
for (int i = 0; i < length; ++i) {
int ret = UnsignedBytes.compare(bytes1[i], bytes2[i]);
if (ret != 0) {
return ret;
}
}
return 0;
}
public static byte[] hexToBytes(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i + 1), 16));
}
return data;
}
}