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 /** 22 * Package private utility class for Cpio 23 * 24 * @Immutable 25 */ 26 class CpioUtil { 27 28 /** 29 * Extracts the file type bits from a mode. 30 */ 31 static long fileType(final long mode) { 32 return mode & CpioConstants.S_IFMT; 33 } 34 35 /** 36 * Converts a byte array to a long. Halfwords can be swapped by setting 37 * swapHalfWord=true. 38 * 39 * @param number 40 * An array of bytes containing a number 41 * @param swapHalfWord 42 * Swap halfwords ([0][1][2][3]->[1][0][3][2]) 43 * @return The long value 44 * @throws UnsupportedOperationException if number length is not a multiple of 2 45 */ 46 static long byteArray2long(final byte[] number, final boolean swapHalfWord) { 47 if (number.length % 2 != 0) { 48 throw new UnsupportedOperationException(); 49 } 50 51 long ret = 0; 52 int pos = 0; 53 final byte tmp_number[] = new byte[number.length]; 54 System.arraycopy(number, 0, tmp_number, 0, number.length); 55 56 if (!swapHalfWord) { 57 byte tmp = 0; 58 for (pos = 0; pos < tmp_number.length; pos++) { 59 tmp = tmp_number[pos]; 60 tmp_number[pos++] = tmp_number[pos]; 61 tmp_number[pos] = tmp; 62 } 63 } 64 65 ret = tmp_number[0] & 0xFF; 66 for (pos = 1; pos < tmp_number.length; pos++) { 67 ret <<= 8; 68 ret |= tmp_number[pos] & 0xFF; 69 } 70 return ret; 71 } 72 73 /** 74 * Converts a long number to a byte array 75 * Halfwords can be swapped by setting swapHalfWord=true. 76 * 77 * @param number 78 * the input long number to be converted 79 * 80 * @param length 81 * The length of the returned array 82 * @param swapHalfWord 83 * Swap halfwords ([0][1][2][3]->[1][0][3][2]) 84 * @return The long value 85 * @throws UnsupportedOperationException if the length is not a positive multiple of two 86 */ 87 static byte[] long2byteArray(final long number, final int length, 88 final boolean swapHalfWord) { 89 final byte[] ret = new byte[length]; 90 int pos = 0; 91 long tmp_number = 0; 92 93 if (length % 2 != 0 || length < 2) { 94 throw new UnsupportedOperationException(); 95 } 96 97 tmp_number = number; 98 for (pos = length - 1; pos >= 0; pos--) { 99 ret[pos] = (byte) (tmp_number & 0xFF); 100 tmp_number >>= 8; 101 } 102 103 if (!swapHalfWord) { 104 byte tmp = 0; 105 for (pos = 0; pos < length; pos++) { 106 tmp = ret[pos]; 107 ret[pos++] = ret[pos]; 108 ret[pos] = tmp; 109 } 110 } 111 112 return ret; 113 } 114 } 115