DumpArchiveUtil.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.dump;

  20. import java.io.IOException;
  21. import java.util.Arrays;

  22. import org.apache.commons.compress.archivers.zip.ZipEncoding;
  23. import org.apache.commons.compress.utils.ByteUtils;

  24. /**
  25.  * Various utilities for dump archives.
  26.  */
  27. final class DumpArchiveUtil {

  28.     /**
  29.      * Calculate checksum for buffer.
  30.      *
  31.      * @param buffer buffer containing tape segment header
  32.      * @return checksum
  33.      */
  34.     public static int calculateChecksum(final byte[] buffer) {
  35.         int calc = 0;
  36.         for (int i = 0; i < 256; i++) {
  37.             calc += convert32(buffer, 4 * i);
  38.         }
  39.         return DumpArchiveConstants.CHECKSUM - (calc - convert32(buffer, 28));
  40.     }

  41.     /**
  42.      * Reads 2-byte integer from buffer.
  43.      *
  44.      * @param buffer The source buffer.
  45.      * @param offset Where to start reading.
  46.      * @return the 2-byte entry as an int.
  47.      */
  48.     public static int convert16(final byte[] buffer, final int offset) {
  49.         return (int) ByteUtils.fromLittleEndian(buffer, offset, 2);
  50.     }

  51.     /**
  52.      * Reads 4-byte integer from buffer.
  53.      *
  54.      * @param buffer The source buffer.
  55.      * @param offset Where to start reading.
  56.      * @return the 4-byte entry as an int.
  57.      */
  58.     public static int convert32(final byte[] buffer, final int offset) {
  59.         return (int) ByteUtils.fromLittleEndian(buffer, offset, 4);
  60.     }

  61.     /**
  62.      * Reads 8-byte integer from buffer.
  63.      *
  64.      * @param buffer The source buffer.
  65.      * @param offset Where to start reading.
  66.      * @return the 8-byte entry as a long.
  67.      */
  68.     public static long convert64(final byte[] buffer, final int offset) {
  69.         return ByteUtils.fromLittleEndian(buffer, offset, 8);
  70.     }

  71.     /**
  72.      * Decodes a byte array to a string.
  73.      */
  74.     static String decode(final ZipEncoding encoding, final byte[] b, final int offset, final int len) throws IOException {
  75.             if (offset > offset + len) {
  76.                 throw new IOException("Invalid offset/length combination");
  77.             }
  78.             return encoding.decode(Arrays.copyOfRange(b, offset, offset + len));
  79.     }

  80.     /**
  81.      * Gets the ino associated with this buffer.
  82.      *
  83.      * @param buffer The source buffer.
  84.      * @return the ino associated with this buffer.
  85.      */
  86.     public static int getIno(final byte[] buffer) {
  87.         return convert32(buffer, 20);
  88.     }

  89.     /**
  90.      * Verifies that the buffer contains a tape segment header.
  91.      *
  92.      * @param buffer The source buffer.
  93.      * @return Whether the buffer contains a tape segment header.
  94.      */
  95.     public static boolean verify(final byte[] buffer) {
  96.         if (buffer == null) {
  97.             return false;
  98.         }
  99.         // verify magic. for now only accept NFS_MAGIC.
  100.         final int magic = convert32(buffer, 24);
  101.         if (magic != DumpArchiveConstants.NFS_MAGIC) {
  102.             return false;
  103.         }
  104.         // verify checksum...
  105.         final int checksum = convert32(buffer, 28);
  106.         return checksum == calculateChecksum(buffer);
  107.     }

  108.     /**
  109.      * Private constructor to prevent instantiation.
  110.      */
  111.     private DumpArchiveUtil() {
  112.     }
  113. }