| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
package org.apache.commons.compress.archivers.cpio; |
| 20 |
|
| 21 |
import java.nio.charset.StandardCharsets; |
| 22 |
import java.util.Arrays; |
| 23 |
|
| 24 |
/** |
| 25 |
* Package private utility class for Cpio |
| 26 |
* |
| 27 |
* @Immutable |
| 28 |
*/ |
| 29 |
final class CpioUtil { |
| 30 |
|
| 31 |
static final String DEFAULT_CHARSET_NAME = StandardCharsets.US_ASCII.name(); |
| 32 |
|
| 33 |
/** |
| 34 |
* Converts a byte array to a long. Halfwords can be swapped by setting swapHalfWord=true. |
| 35 |
* |
| 36 |
* @param number An array of bytes containing a number |
| 37 |
* @param swapHalfWord Swap halfwords ([0][1][2][3]->[1][0][3][2]) |
| 38 |
* @return The long value |
| 39 |
* @throws UnsupportedOperationException if number length is not a multiple of 2 |
| 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 |
* Extracts the file type bits from a mode. |
| 69 |
*/ |
| 70 |
static long fileType(final long mode) { |
| 71 |
return mode & CpioConstants.S_IFMT; |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Converts a long number to a byte array Halfwords can be swapped by setting swapHalfWord=true. |
| 76 |
* |
| 77 |
* @param number the input long number to be converted |
| 78 |
* |
| 79 |
* @param length The length of the returned array |
| 80 |
* @param swapHalfWord Swap halfwords ([0][1][2][3]->[1][0][3][2]) |
| 81 |
* @return The long value |
| 82 |
* @throws UnsupportedOperationException if the length is not a positive multiple of two |
| 83 |
*/ |
| 84 |
static byte[] long2byteArray(final long number, final int length, final boolean swapHalfWord) { |
| 85 |
final byte[] ret = new byte[length]; |
| 86 |
int pos = 0; |
| 87 |
long tmp_number; |
| 88 |
|
| 89 |
if (length % 2 != 0 || length < 2) { |
| 90 |
throw new UnsupportedOperationException(); |
| 91 |
} |
| 92 |
|
| 93 |
tmp_number = number; |
| 94 |
for (pos = length - 1; pos >= 0; pos--) { |
| 95 |
ret[pos] = (byte) (tmp_number & 0xFF); |
| 96 |
tmp_number >>= 8; |
| 97 |
} |
| 98 |
|
| 99 |
if (!swapHalfWord) { |
| 100 |
byte tmp = 0; |
| 101 |
for (pos = 0; pos < length; pos++) { |
| 102 |
tmp = ret[pos]; |
| 103 |
ret[pos++] = ret[pos]; |
| 104 |
ret[pos] = tmp; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return ret; |
| 109 |
} |
| 110 |
} |