| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package org.apache.commons.compress.archivers.cpio; |
| 20 |
|
| 21 |
import java.nio.charset.StandardCharsets; |
| 22 |
import java.util.Arrays; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
final class CpioUtil { |
| 30 |
|
| 31 |
static final String DEFAULT_CHARSET_NAME = StandardCharsets.US_ASCII.name(); |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static long byteArray2long(final byte[] number, final boolean swapHalfWord) { |
| 42 |
if (number.length % 2 != 0) { |
| 43 |
throw new UnsupportedOperationException(); |
| 44 |
} |
| 45 |
|
| 46 |
long ret; |
| 47 |
int pos = 0; |
| 48 |
final byte[] tmpNumber = Arrays.copyOf(number, number.length); |
| 49 |
|
| 50 |
if (!swapHalfWord) { |
| 51 |
byte tmp = 0; |
| 52 |
for (pos = 0; pos < tmpNumber.length; pos++) { |
| 53 |
tmp = tmpNumber[pos]; |
| 54 |
tmpNumber[pos++] = tmpNumber[pos]; |
| 55 |
tmpNumber[pos] = tmp; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
ret = tmpNumber[0] & 0xFF; |
| 60 |
for (pos = 1; pos < tmpNumber.length; pos++) { |
| 61 |
ret <<= 8; |
| 62 |
ret |= tmpNumber[pos] & 0xFF; |
| 63 |
} |
| 64 |
return ret; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static long fileType(final long mode) { |
| 71 |
return mode & CpioConstants.S_IFMT; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
static byte[] long2byteArray(final long number, final int length, final boolean swapHalfWord) { |
| 84 |
final byte[] ret = new byte[length]; |
| 85 |
int pos = 0; |
| 86 |
long tmp_number; |
| 87 |
|
| 88 |
if (length % 2 != 0 || length < 2) { |
| 89 |
throw new UnsupportedOperationException(); |
| 90 |
} |
| 91 |
|
| 92 |
tmp_number = number; |
| 93 |
for (pos = length - 1; pos >= 0; pos--) { |
| 94 |
ret[pos] = (byte) (tmp_number & 0xFF); |
| 95 |
tmp_number >>= 8; |
| 96 |
} |
| 97 |
|
| 98 |
if (!swapHalfWord) { |
| 99 |
byte tmp = 0; |
| 100 |
for (pos = 0; pos < length; pos++) { |
| 101 |
tmp = ret[pos]; |
| 102 |
ret[pos++] = ret[pos]; |
| 103 |
ret[pos] = tmp; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
return ret; |
| 108 |
} |
| 109 |
} |