CpioUtil.java

  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. import java.nio.charset.StandardCharsets;
  21. import java.util.Arrays;

  22. /**
  23.  * Package private utility class for Cpio
  24.  *
  25.  * @Immutable
  26.  */
  27. final class CpioUtil {

  28.     static final String DEFAULT_CHARSET_NAME = StandardCharsets.US_ASCII.name();

  29.     /**
  30.      * Converts a byte array to a long. Halfwords can be swapped by setting swapHalfWord=true.
  31.      *
  32.      * @param number       An array of bytes containing a number
  33.      * @param swapHalfWord Swap halfwords ([0][1][2][3]->[1][0][3][2])
  34.      * @return The long value
  35.      * @throws UnsupportedOperationException if number length is not a multiple of 2
  36.      */
  37.     static long byteArray2long(final byte[] number, final boolean swapHalfWord) {
  38.         if (number.length % 2 != 0) {
  39.             throw new UnsupportedOperationException();
  40.         }

  41.         long ret;
  42.         int pos = 0;
  43.         final byte[] tmpNumber = Arrays.copyOf(number, number.length);

  44.         if (!swapHalfWord) {
  45.             byte tmp = 0;
  46.             for (pos = 0; pos < tmpNumber.length; pos++) {
  47.                 tmp = tmpNumber[pos];
  48.                 tmpNumber[pos++] = tmpNumber[pos];
  49.                 tmpNumber[pos] = tmp;
  50.             }
  51.         }

  52.         ret = tmpNumber[0] & 0xFF;
  53.         for (pos = 1; pos < tmpNumber.length; pos++) {
  54.             ret <<= 8;
  55.             ret |= tmpNumber[pos] & 0xFF;
  56.         }
  57.         return ret;
  58.     }

  59.     /**
  60.      * Extracts the file type bits from a mode.
  61.      */
  62.     static long fileType(final long mode) {
  63.         return mode & CpioConstants.S_IFMT;
  64.     }

  65.     /**
  66.      * Converts a long number to a byte array Halfwords can be swapped by setting swapHalfWord=true.
  67.      *
  68.      * @param number       the input long number to be converted
  69.      *
  70.      * @param length       The length of the returned array
  71.      * @param swapHalfWord Swap halfwords ([0][1][2][3]->[1][0][3][2])
  72.      * @return The long value
  73.      * @throws UnsupportedOperationException if the length is not a positive multiple of two
  74.      */
  75.     static byte[] long2byteArray(final long number, final int length, final boolean swapHalfWord) {
  76.         final byte[] ret = new byte[length];
  77.         int pos = 0;
  78.         long tmp_number;

  79.         if (length % 2 != 0 || length < 2) {
  80.             throw new UnsupportedOperationException();
  81.         }

  82.         tmp_number = number;
  83.         for (pos = length - 1; pos >= 0; pos--) {
  84.             ret[pos] = (byte) (tmp_number & 0xFF);
  85.             tmp_number >>= 8;
  86.         }

  87.         if (!swapHalfWord) {
  88.             byte tmp = 0;
  89.             for (pos = 0; pos < length; pos++) {
  90.                 tmp = ret[pos];
  91.                 ret[pos++] = ret[pos];
  92.                 ret[pos] = tmp;
  93.             }
  94.         }

  95.         return ret;
  96.     }
  97. }