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 21 import java.io.IOException; 22 import java.util.Arrays; 23 import org.apache.commons.compress.archivers.zip.ZipEncoding; 24 import org.apache.commons.compress.utils.ByteUtils; 25 26 /** 27 * Various utilities for dump archives. 28 */ 29 class DumpArchiveUtil { 30 /** 31 * Private constructor to prevent instantiation. 32 */ DumpArchiveUtil()33 private DumpArchiveUtil() { 34 } 35 36 /** 37 * Calculate checksum for buffer. 38 * 39 * @param buffer buffer containing tape segment header 40 * @returns checksum 41 */ calculateChecksum(final byte[] buffer)42 public static int calculateChecksum(final byte[] buffer) { 43 int calc = 0; 44 45 for (int i = 0; i < 256; i++) { 46 calc += DumpArchiveUtil.convert32(buffer, 4 * i); 47 } 48 49 return DumpArchiveConstants.CHECKSUM - 50 (calc - DumpArchiveUtil.convert32(buffer, 28)); 51 } 52 53 /** 54 * Verify that the buffer contains a tape segment header. 55 * 56 * @param buffer 57 */ verify(final byte[] buffer)58 public static final boolean verify(final byte[] buffer) { 59 // verify magic. for now only accept NFS_MAGIC. 60 final int magic = convert32(buffer, 24); 61 62 if (magic != DumpArchiveConstants.NFS_MAGIC) { 63 return false; 64 } 65 66 //verify checksum... 67 final int checksum = convert32(buffer, 28); 68 69 return checksum == calculateChecksum(buffer); 70 } 71 72 /** 73 * Get the ino associated with this buffer. 74 * 75 * @param buffer 76 */ getIno(final byte[] buffer)77 public static final int getIno(final byte[] buffer) { 78 return convert32(buffer, 20); 79 } 80 81 /** 82 * Read 8-byte integer from buffer. 83 * 84 * @param buffer 85 * @param offset 86 * @return the 8-byte entry as a long 87 */ convert64(final byte[] buffer, final int offset)88 public static final long convert64(final byte[] buffer, final int offset) { 89 return ByteUtils.fromLittleEndian(buffer, offset, 8); 90 } 91 92 /** 93 * Read 4-byte integer from buffer. 94 * 95 * @param buffer 96 * @param offset 97 * @return the 4-byte entry as an int 98 */ convert32(final byte[] buffer, final int offset)99 public static final int convert32(final byte[] buffer, final int offset) { 100 return (int) ByteUtils.fromLittleEndian(buffer, offset, 4); 101 } 102 103 /** 104 * Read 2-byte integer from buffer. 105 * 106 * @param buffer 107 * @param offset 108 * @return the 2-byte entry as an int 109 */ convert16(final byte[] buffer, final int offset)110 public static final int convert16(final byte[] buffer, final int offset) { 111 return (int) ByteUtils.fromLittleEndian(buffer, offset, 2); 112 } 113 114 /** 115 * Decodes a byte array to a string. 116 */ decode(final ZipEncoding encoding, final byte[] b, final int offset, final int len)117 static String decode(final ZipEncoding encoding, final byte[] b, final int offset, final int len) 118 throws IOException { 119 return encoding.decode(Arrays.copyOfRange(b, offset, offset + len)); 120 } 121 } 122