Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 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.tar; |
| 20 | |
| 21 | /** |
| 22 | * This class provides static utility methods to work with byte streams. |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 23 | * |
Sebastian Bazley | 44dbd93 | 2009-03-28 00:03:11 +0000 | [diff] [blame] | 24 | * @Immutable |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 25 | */ |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 26 | // CheckStyle:HideUtilityClassConstructorCheck OFF (bc) |
| 27 | public class TarUtils { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 28 | |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 29 | private static final int BYTE_MASK = 255; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 30 | |
| 31 | /** |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 32 | * Parse an octal string from a buffer. |
| 33 | * Leading spaces are ignored. |
| 34 | * Parsing stops when a NUL is found, or a trailing space, |
| 35 | * or the buffer length is reached. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 36 | * |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 37 | * Behaviour with non-octal input is currently undefined. |
| 38 | * |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 39 | * @param buffer The buffer from which to parse. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 40 | * @param offset The offset into the buffer from which to parse. |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 41 | * @param length The maximum number of bytes to parse. |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 42 | * @return The long value of the octal string. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 43 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 44 | public static long parseOctal(byte[] buffer, final int offset, final int length) { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 45 | long result = 0; |
| 46 | boolean stillPadding = true; |
| 47 | int end = offset + length; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 48 | |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 49 | for (int i = offset; i < end; ++i) { |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 50 | final byte currentByte = buffer[i]; |
| 51 | if (currentByte == 0) { // Found trailing null |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 52 | break; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 53 | } |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 54 | |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 55 | // Ignore leading spaces ('0' can be ignored anyway) |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 56 | if (currentByte == (byte) ' ' || currentByte == '0') { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 57 | if (stillPadding) { |
| 58 | continue; |
| 59 | } |
| 60 | |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 61 | if (currentByte == (byte) ' ') { // Found trailing space |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | stillPadding = false; |
| 67 | // CheckStyle:MagicNumber OFF |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 68 | if (currentByte < '0' || currentByte > '7'){ |
| 69 | throw new IllegalArgumentException( |
| 70 | "Invalid octal digit at position "+i+" in '"+new String(buffer, offset, length)+"'"); |
| 71 | } |
| 72 | result = (result << 3) + (currentByte - '0');// TODO needs to reject invalid bytes |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 73 | // CheckStyle:MagicNumber ON |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 76 | return result; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 80 | * Parse an entry name from a buffer. |
| 81 | * Parsing stops when a NUL is found |
| 82 | * or the buffer length is reached. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 83 | * |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 84 | * @param buffer The buffer from which to parse. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 85 | * @param offset The offset into the buffer from which to parse. |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 86 | * @param length The maximum number of bytes to parse. |
| 87 | * @return The entry name. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 88 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 89 | public static String parseName(byte[] buffer, final int offset, final int length) { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 90 | StringBuffer result = new StringBuffer(length); |
| 91 | int end = offset + length; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 92 | |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 93 | for (int i = offset; i < end; ++i) { |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 94 | if (buffer[i] == 0) { // Trailing null |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 98 | result.append((char) buffer[i]); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 101 | return result.toString(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | /** |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 105 | * Copy a name (StringBuffer) into a buffer. |
| 106 | * Copies characters from the name into the buffer |
| 107 | * starting at the specified offset. |
| 108 | * If the buffer is longer than the name, the buffer |
| 109 | * is filled with trailing NULs. |
| 110 | * If the name is longer than the buffer, |
| 111 | * the output is truncated. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 112 | * |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 113 | * @param name The header name from which to copy the characters. |
| 114 | * @param buf The buffer where the name is to be stored. |
| 115 | * @param offset The starting offset into the buffer |
| 116 | * @param length The maximum number of header bytes to copy. |
| 117 | * @return The updated offset, i.e. offset + length |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 118 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 119 | public static int formatNameBytes(String name, byte[] buf, final int offset, final int length) { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 120 | int i; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 121 | |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 122 | // copy until end of input or output is reached. |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 123 | for (i = 0; i < length && i < name.length(); ++i) { |
| 124 | buf[offset + i] = (byte) name.charAt(i); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 127 | // Pad any remaining output bytes with NUL |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 128 | for (; i < length; ++i) { |
| 129 | buf[offset + i] = 0; |
| 130 | } |
| 131 | |
| 132 | return offset + length; |
| 133 | } |
| 134 | |
| 135 | /** |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 136 | * Fill buffer with unsigned octal number, padded with leading zeroes. |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 137 | * |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 138 | * @param value number to convert to octal - treated as unsigned |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 139 | * @param buffer destination buffer |
| 140 | * @param offset starting offset in buffer |
| 141 | * @param length length of buffer to fill |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 142 | * @throws IllegalArgumentException if the value will not fit in the buffer |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 143 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 144 | public static void formatUnsignedOctalString(final long value, byte[] buffer, |
| 145 | final int offset, final int length) { |
| 146 | int remaining = length; |
| 147 | remaining--; |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 148 | if (value == 0) { |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 149 | buffer[offset + remaining--] = (byte) '0'; |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 150 | } else { |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 151 | long val = value; |
| 152 | for (; remaining >= 0 && val != 0; --remaining) { |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 153 | // CheckStyle:MagicNumber OFF |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 154 | buffer[offset + remaining] = (byte) ((byte) '0' + (byte) (val & 7)); |
| 155 | val = val >>> 3; |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 156 | // CheckStyle:MagicNumber ON |
| 157 | } |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 158 | if (val != 0){ |
| 159 | throw new IllegalArgumentException |
| 160 | (value+"="+Long.toOctalString(value)+ " will not fit in octal number buffer of length "+length); |
| 161 | } |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 164 | for (; remaining >= 0; --remaining) { // leading zeros |
| 165 | buffer[offset + remaining] = (byte) '0'; |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
| 169 | /** |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 170 | * Write an octal integer into a buffer. |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 171 | * |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 172 | * Uses {@link #formatUnsignedOctalString} to format |
| 173 | * the value as an octal string with leading zeros. |
| 174 | * The converted number is followed by space and NUL |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 175 | * |
| 176 | * @param value The value to write |
| 177 | * @param buf The buffer to receive the output |
| 178 | * @param offset The starting offset into the buffer |
| 179 | * @param length The size of the output buffer |
| 180 | * @return The updated offset, i.e offset+length |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 181 | * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 182 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 183 | public static int formatOctalBytes(final long value, byte[] buf, final int offset, final int length) { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 184 | |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 185 | int idx=length-2; // For space and trailing null |
| 186 | formatUnsignedOctalString(value, buf, offset, idx); |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 187 | |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 188 | buf[offset + idx++] = (byte) ' '; // Trailing space |
| 189 | buf[offset + idx] = 0; // Trailing null |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 190 | |
| 191 | return offset + length; |
| 192 | } |
| 193 | |
| 194 | /** |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 195 | * Write an octal long integer into a buffer. |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 196 | * |
| 197 | * Uses {@link #formatUnsignedOctalString} to format |
| 198 | * the value as an octal string with leading zeros. |
| 199 | * The converted number is followed by a space. |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 200 | * |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 201 | * @param value The value to write as octal |
| 202 | * @param buf The destinationbuffer. |
| 203 | * @param offset The starting offset into the buffer. |
| 204 | * @param length The length of the buffer |
| 205 | * @return The updated offset |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 206 | * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 207 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 208 | public static int formatLongOctalBytes(final long value, byte[] buf, final int offset, final int length) { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 209 | |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 210 | int idx=length-1; // For space |
| 211 | |
| 212 | formatUnsignedOctalString(value, buf, offset, idx); |
| 213 | buf[offset + idx] = (byte) ' '; // Trailing space |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 214 | |
| 215 | return offset + length; |
| 216 | } |
| 217 | |
| 218 | /** |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 219 | * Writes an octal value into a buffer. |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 220 | * |
| 221 | * Uses {@link #formatUnsignedOctalString} to format |
| 222 | * the value as an octal string with leading zeros. |
| 223 | * The converted number is followed by NUL and then space. |
Sebastian Bazley | ae3c158 | 2009-03-31 16:24:28 +0000 | [diff] [blame] | 224 | * |
| 225 | * @param value The value to convert |
| 226 | * @param buf The destination buffer |
| 227 | * @param offset The starting offset into the buffer. |
| 228 | * @param length The size of the buffer. |
| 229 | * @return The updated value of offset, i.e. offset+length |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 230 | * @throws IllegalArgumentException if the value (and trailer) will not fit in the buffer |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 231 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 232 | public static int formatCheckSumOctalBytes(final long value, byte[] buf, final int offset, final int length) { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 233 | |
Sebastian Bazley | 11349c5 | 2009-03-31 23:49:20 +0000 | [diff] [blame] | 234 | int idx=length-2; // for NUL and space |
| 235 | formatUnsignedOctalString(value, buf, offset, idx); |
| 236 | |
| 237 | buf[offset + idx++] = 0; // Trailing null |
| 238 | buf[offset + idx] = (byte) ' '; // Trailing space |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 239 | |
| 240 | return offset + length; |
| 241 | } |
| 242 | |
| 243 | /** |
| 244 | * Compute the checksum of a tar entry header. |
| 245 | * |
| 246 | * @param buf The tar entry's header buffer. |
| 247 | * @return The computed checksum. |
| 248 | */ |
Sebastian Bazley | 1d55670 | 2009-04-02 18:45:02 +0000 | [diff] [blame^] | 249 | public static long computeCheckSum(final byte[] buf) { |
Torsten Curdt | 46ad24d | 2009-01-08 11:09:25 +0000 | [diff] [blame] | 250 | long sum = 0; |
| 251 | |
| 252 | for (int i = 0; i < buf.length; ++i) { |
| 253 | sum += BYTE_MASK & buf[i]; |
| 254 | } |
| 255 | |
| 256 | return sum; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 257 | } |
| 258 | } |