| 1 | // SPDX-License-Identifier: 0BSD |
| 2 | // SPDX-FileCopyrightText: The XZ for Java authors and contributors |
| 3 | // SPDX-FileContributor: Lasse Collin <[email protected]> |
| 4 | |
| 5 | package org.tukaani.xz; |
| 6 | |
| 7 | import java.io.InputStream; |
| 8 | import java.io.DataInputStream; |
| 9 | import java.io.IOException; |
| 10 | import java.io.EOFException; |
| 11 | import org.tukaani.xz.common.DecoderUtil; |
| 12 | |
| 13 | /** |
| 14 | * Decompresses a .xz file in streamed mode (no seeking). |
| 15 | * <p> |
| 16 | * Use this to decompress regular standalone .xz files. This reads from |
| 17 | * its input stream until the end of the input or until an error occurs. |
| 18 | * This supports decompressing concatenated .xz files. |
| 19 | * |
| 20 | * <h2>Typical use cases</h2> |
| 21 | * <p> |
| 22 | * Getting an input stream to decompress a .xz file: |
| 23 | * <blockquote><pre> |
| 24 | * InputStream infile = new FileInputStream("foo.xz"); |
| 25 | * XZInputStream inxz = new XZInputStream(infile); |
| 26 | * </pre></blockquote> |
| 27 | * <p> |
| 28 | * It's important to keep in mind that decompressor memory usage depends |
| 29 | * on the settings used to compress the file. The worst-case memory usage |
| 30 | * of XZInputStream is currently 1.5 GiB. Still, very few files will |
| 31 | * require more than about 65 MiB because that's how much decompressing |
| 32 | * a file created with the highest preset level will need, and only a few |
| 33 | * people use settings other than the predefined presets. |
| 34 | * <p> |
| 35 | * It is possible to specify a memory usage limit for {@code XZInputStream}. |
| 36 | * If decompression requires more memory than the specified limit, |
| 37 | * {@code MemoryLimitException} will be thrown when reading from the stream. |
| 38 | * For example, the following sets the memory usage limit to 100 MiB: |
| 39 | * <blockquote><pre> |
| 40 | * InputStream infile = new FileInputStream("foo.xz"); |
| 41 | * XZInputStream inxz = new XZInputStream(infile, 100 * 1024); |
| 42 | * </pre></blockquote> |
| 43 | * |
| 44 | * <h2>When uncompressed size is known beforehand</h2> |
| 45 | * <p> |
| 46 | * If you are decompressing complete files and your application knows |
| 47 | * exactly how much uncompressed data there should be, it is good to try |
| 48 | * reading one more byte by calling {@code read()} and checking that it |
| 49 | * returns {@code -1}. This way the decompressor will parse the file footers |
| 50 | * and verify the integrity checks, giving the caller more confidence that |
| 51 | * the uncompressed data is valid. (This advice seems to apply to |
| 52 | * {@link java.util.zip.GZIPInputStream java.util.zip.GZIPInputStream} too.) |
| 53 | * |
| 54 | * @see SingleXZInputStream |
| 55 | */ |
| 56 | public class XZInputStream extends InputStream { |
| 57 | private final ArrayCache arrayCache; |
| 58 | |
| 59 | private final int memoryLimit; |
| 60 | private InputStream in; |
| 61 | private SingleXZInputStream xzIn; |
| 62 | private final boolean verifyCheck; |
| 63 | private boolean endReached = false; |
| 64 | private IOException exception = null; |
| 65 | |
| 66 | private final byte[] tempBuf = new byte[1]; |
| 67 | |
| 68 | /** |
| 69 | * Creates a new XZ decompressor without a memory usage limit. |
| 70 | * <p> |
| 71 | * This constructor reads and parses the XZ Stream Header (12 bytes) |
| 72 | * from {@code in}. The header of the first Block is not read |
| 73 | * until {@code read} is called. |
| 74 | * |
| 75 | * @param in input stream from which XZ-compressed |
| 76 | * data is read |
| 77 | * |
| 78 | * @throws XZFormatException |
| 79 | * input is not in the XZ format |
| 80 | * |
| 81 | * @throws CorruptedInputException |
| 82 | * XZ header CRC32 doesn't match |
| 83 | * |
| 84 | * @throws UnsupportedOptionsException |
| 85 | * XZ header is valid but specifies options |
| 86 | * not supported by this implementation |
| 87 | * |
| 88 | * @throws EOFException |
| 89 | * less than 12 bytes of input was available |
| 90 | * from {@code in} |
| 91 | * |
| 92 | * @throws IOException may be thrown by {@code in} |
| 93 | */ |
| 94 | public XZInputStream(InputStream in) throws IOException { |
| 95 | this(in, -1); |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates a new XZ decompressor without a memory usage limit. |
| 100 | * <p> |
| 101 | * This is identical to {@code XZInputStream(InputStream)} |
| 102 | * except that this takes also the {@code arrayCache} argument. |
| 103 | * |
| 104 | * @param in input stream from which XZ-compressed |
| 105 | * data is read |
| 106 | * |
| 107 | * @param arrayCache cache to be used for allocating large arrays |
| 108 | * |
| 109 | * @throws XZFormatException |
| 110 | * input is not in the XZ format |
| 111 | * |
| 112 | * @throws CorruptedInputException |
| 113 | * XZ header CRC32 doesn't match |
| 114 | * |
| 115 | * @throws UnsupportedOptionsException |
| 116 | * XZ header is valid but specifies options |
| 117 | * not supported by this implementation |
| 118 | * |
| 119 | * @throws EOFException |
| 120 | * less than 12 bytes of input was available |
| 121 | * from {@code in} |
| 122 | * |
| 123 | * @throws IOException may be thrown by {@code in} |
| 124 | * |
| 125 | * @since 1.7 |
| 126 | */ |
| 127 | public XZInputStream(InputStream in, ArrayCache arrayCache) |
| 128 | throws IOException { |
| 129 | this(in, -1, arrayCache); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Creates a new XZ decompressor with an optional memory usage limit. |
| 134 | * <p> |
| 135 | * This is identical to {@code XZInputStream(InputStream)} except |
| 136 | * that this takes also the {@code memoryLimit} argument. |
| 137 | * |
| 138 | * @param in input stream from which XZ-compressed |
| 139 | * data is read |
| 140 | * |
| 141 | * @param memoryLimit memory usage limit in kibibytes (KiB) |
| 142 | * or {@code -1} to impose no |
| 143 | * memory usage limit |
| 144 | * |
| 145 | * @throws XZFormatException |
| 146 | * input is not in the XZ format |
| 147 | * |
| 148 | * @throws CorruptedInputException |
| 149 | * XZ header CRC32 doesn't match |
| 150 | * |
| 151 | * @throws UnsupportedOptionsException |
| 152 | * XZ header is valid but specifies options |
| 153 | * not supported by this implementation |
| 154 | * |
| 155 | * @throws EOFException |
| 156 | * less than 12 bytes of input was available |
| 157 | * from {@code in} |
| 158 | * |
| 159 | * @throws IOException may be thrown by {@code in} |
| 160 | */ |
| 161 | public XZInputStream(InputStream in, int memoryLimit) throws IOException { |
| 162 | this(in, memoryLimit, true); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Creates a new XZ decompressor with an optional memory usage limit. |
| 167 | * <p> |
| 168 | * This is identical to {@code XZInputStream(InputStream)} except |
| 169 | * that this takes also the {@code memoryLimit} and |
| 170 | * {@code arrayCache} arguments. |
| 171 | * |
| 172 | * @param in input stream from which XZ-compressed |
| 173 | * data is read |
| 174 | * |
| 175 | * @param memoryLimit memory usage limit in kibibytes (KiB) |
| 176 | * or {@code -1} to impose no |
| 177 | * memory usage limit |
| 178 | * |
| 179 | * @param arrayCache cache to be used for allocating large arrays |
| 180 | * |
| 181 | * @throws XZFormatException |
| 182 | * input is not in the XZ format |
| 183 | * |
| 184 | * @throws CorruptedInputException |
| 185 | * XZ header CRC32 doesn't match |
| 186 | * |
| 187 | * @throws UnsupportedOptionsException |
| 188 | * XZ header is valid but specifies options |
| 189 | * not supported by this implementation |
| 190 | * |
| 191 | * @throws EOFException |
| 192 | * less than 12 bytes of input was available |
| 193 | * from {@code in} |
| 194 | * |
| 195 | * @throws IOException may be thrown by {@code in} |
| 196 | * |
| 197 | * @since 1.7 |
| 198 | */ |
| 199 | public XZInputStream(InputStream in, int memoryLimit, |
| 200 | ArrayCache arrayCache) throws IOException { |
| 201 | this(in, memoryLimit, true, arrayCache); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Creates a new XZ decompressor with an optional memory usage limit |
| 206 | * and ability to disable verification of integrity checks. |
| 207 | * <p> |
| 208 | * This is identical to {@code XZInputStream(InputStream,int)} except |
| 209 | * that this takes also the {@code verifyCheck} argument. |
| 210 | * <p> |
| 211 | * Note that integrity check verification should almost never be disabled. |
| 212 | * Possible reasons to disable integrity check verification: |
| 213 | * <ul> |
| 214 | * <li>Trying to recover data from a corrupt .xz file.</li> |
| 215 | * <li>Speeding up decompression. This matters mostly with SHA-256 |
| 216 | * or with files that have compressed extremely well. It's recommended |
| 217 | * that integrity checking isn't disabled for performance reasons |
| 218 | * unless the file integrity is verified externally in some other |
| 219 | * way.</li> |
| 220 | * </ul> |
| 221 | * <p> |
| 222 | * {@code verifyCheck} only affects the integrity check of the actual |
| 223 | * compressed data. The CRC32 fields in the headers are always verified. |
| 224 | * |
| 225 | * @param in input stream from which XZ-compressed |
| 226 | * data is read |
| 227 | * |
| 228 | * @param memoryLimit memory usage limit in kibibytes (KiB) |
| 229 | * or {@code -1} to impose no |
| 230 | * memory usage limit |
| 231 | * |
| 232 | * @param verifyCheck if {@code true}, the integrity checks |
| 233 | * will be verified; this should almost never |
| 234 | * be set to {@code false} |
| 235 | * |
| 236 | * @throws XZFormatException |
| 237 | * input is not in the XZ format |
| 238 | * |
| 239 | * @throws CorruptedInputException |
| 240 | * XZ header CRC32 doesn't match |
| 241 | * |
| 242 | * @throws UnsupportedOptionsException |
| 243 | * XZ header is valid but specifies options |
| 244 | * not supported by this implementation |
| 245 | * |
| 246 | * @throws EOFException |
| 247 | * less than 12 bytes of input was available |
| 248 | * from {@code in} |
| 249 | * |
| 250 | * @throws IOException may be thrown by {@code in} |
| 251 | * |
| 252 | * @since 1.6 |
| 253 | */ |
| 254 | public XZInputStream(InputStream in, int memoryLimit, boolean verifyCheck) |
| 255 | throws IOException { |
| 256 | this(in, memoryLimit, verifyCheck, ArrayCache.getDefaultCache()); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Creates a new XZ decompressor with an optional memory usage limit |
| 261 | * and ability to disable verification of integrity checks. |
| 262 | * <p> |
| 263 | * This is identical to {@code XZInputStream(InputStream,int,boolean)} |
| 264 | * except that this takes also the {@code arrayCache} argument. |
| 265 | * |
| 266 | * @param in input stream from which XZ-compressed |
| 267 | * data is read |
| 268 | * |
| 269 | * @param memoryLimit memory usage limit in kibibytes (KiB) |
| 270 | * or {@code -1} to impose no |
| 271 | * memory usage limit |
| 272 | * |
| 273 | * @param verifyCheck if {@code true}, the integrity checks |
| 274 | * will be verified; this should almost never |
| 275 | * be set to {@code false} |
| 276 | * |
| 277 | * @param arrayCache cache to be used for allocating large arrays |
| 278 | * |
| 279 | * @throws XZFormatException |
| 280 | * input is not in the XZ format |
| 281 | * |
| 282 | * @throws CorruptedInputException |
| 283 | * XZ header CRC32 doesn't match |
| 284 | * |
| 285 | * @throws UnsupportedOptionsException |
| 286 | * XZ header is valid but specifies options |
| 287 | * not supported by this implementation |
| 288 | * |
| 289 | * @throws EOFException |
| 290 | * less than 12 bytes of input was available |
| 291 | * from {@code in} |
| 292 | * |
| 293 | * @throws IOException may be thrown by {@code in} |
| 294 | * |
| 295 | * @since 1.7 |
| 296 | */ |
| 297 | public XZInputStream(InputStream in, int memoryLimit, boolean verifyCheck, |
| 298 | ArrayCache arrayCache) throws IOException { |
| 299 | this.arrayCache = arrayCache; |
| 300 | this.in = in; |
| 301 | this.memoryLimit = memoryLimit; |
| 302 | this.verifyCheck = verifyCheck; |
| 303 | this.xzIn = new SingleXZInputStream(in, memoryLimit, verifyCheck, |
| 304 | arrayCache); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Decompresses the next byte from this input stream. |
| 309 | * <p> |
| 310 | * Reading lots of data with {@code read()} from this input stream |
| 311 | * may be inefficient. Wrap it in {@link java.io.BufferedInputStream} |
| 312 | * if you need to read lots of data one byte at a time. |
| 313 | * |
| 314 | * @return the next decompressed byte, or {@code -1} |
| 315 | * to indicate the end of the compressed stream |
| 316 | * |
| 317 | * @throws CorruptedInputException |
| 318 | * @throws UnsupportedOptionsException |
| 319 | * @throws MemoryLimitException |
| 320 | * |
| 321 | * @throws XZIOException if the stream has been closed |
| 322 | * |
| 323 | * @throws EOFException |
| 324 | * compressed input is truncated or corrupt |
| 325 | * |
| 326 | * @throws IOException may be thrown by {@code in} |
| 327 | */ |
| 328 | @Override |
| 329 | public int read() throws IOException { |
| 330 | return read(tempBuf, 0, 1) == -1 ? -1 : (tempBuf[0] & 0xFF); |
| 331 | } |
| 332 | |
| 333 | /** |
| 334 | * Decompresses into an array of bytes. |
| 335 | * <p> |
| 336 | * If {@code len} is zero, no bytes are read and {@code 0} |
| 337 | * is returned. Otherwise this will try to decompress {@code len} |
| 338 | * bytes of uncompressed data. Less than {@code len} bytes may |
| 339 | * be read only in the following situations: |
| 340 | * <ul> |
| 341 | * <li>The end of the compressed data was reached successfully.</li> |
| 342 | * <li>An error is detected after at least one but less {@code len} |
| 343 | * bytes have already been successfully decompressed. |
| 344 | * The next call with non-zero {@code len} will immediately |
| 345 | * throw the pending exception.</li> |
| 346 | * <li>An exception is thrown.</li> |
| 347 | * </ul> |
| 348 | * |
| 349 | * @param buf target buffer for uncompressed data |
| 350 | * @param off start offset in {@code buf} |
| 351 | * @param len maximum number of uncompressed bytes to read |
| 352 | * |
| 353 | * @return number of bytes read, or {@code -1} to indicate |
| 354 | * the end of the compressed stream |
| 355 | * |
| 356 | * @throws CorruptedInputException |
| 357 | * @throws UnsupportedOptionsException |
| 358 | * @throws MemoryLimitException |
| 359 | * |
| 360 | * @throws XZIOException if the stream has been closed |
| 361 | * |
| 362 | * @throws EOFException |
| 363 | * compressed input is truncated or corrupt |
| 364 | * |
| 365 | * @throws IOException may be thrown by {@code in} |
| 366 | */ |
| 367 | @Override |
| 368 | public int read(byte[] buf, int off, int len) throws IOException { |
| 369 | if (off < 0 || len < 0 || off + len < 0 || off + len > buf.length) |
| 370 | throw new IndexOutOfBoundsException(); |
| 371 | |
| 372 | if (len == 0) |
| 373 | return 0; |
| 374 | |
| 375 | if (in == null) |
| 376 | throw new XZIOException("Stream closed"); |
| 377 | |
| 378 | if (exception != null) |
| 379 | throw exception; |
| 380 | |
| 381 | if (endReached) |
| 382 | return -1; |
| 383 | |
| 384 | int size = 0; |
| 385 | |
| 386 | try { |
| 387 | while (len > 0) { |
| 388 | if (xzIn == null) { |
| 389 | prepareNextStream(); |
| 390 | if (endReached) |
| 391 | return size == 0 ? -1 : size; |
| 392 | } |
| 393 | |
| 394 | int ret = xzIn.read(buf, off, len); |
| 395 | |
| 396 | if (ret > 0) { |
| 397 | size += ret; |
| 398 | off += ret; |
| 399 | len -= ret; |
| 400 | } else if (ret == -1) { |
| 401 | xzIn = null; |
| 402 | } |
| 403 | } |
| 404 | } catch (IOException e) { |
| 405 | exception = e; |
| 406 | if (size == 0) |
| 407 | throw e; |
| 408 | } |
| 409 | |
| 410 | return size; |
| 411 | } |
| 412 | |
| 413 | private void prepareNextStream() throws IOException { |
| 414 | DataInputStream inData = new DataInputStream(in); |
| 415 | byte[] buf = new byte[DecoderUtil.STREAM_HEADER_SIZE]; |
| 416 | |
| 417 | // The size of Stream Padding must be a multiple of four bytes, |
| 418 | // all bytes zero. |
| 419 | do { |
| 420 | // First try to read one byte to see if we have reached the end |
| 421 | // of the file. |
| 422 | int ret = inData.read(buf, 0, 1); |
| 423 | if (ret == -1) { |
| 424 | endReached = true; |
| 425 | return; |
| 426 | } |
| 427 | |
| 428 | // Since we got one byte of input, there must be at least |
| 429 | // three more available in a valid file. |
| 430 | inData.readFully(buf, 1, 3); |
| 431 | |
| 432 | } while (buf[0] == 0 && buf[1] == 0 && buf[2] == 0 && buf[3] == 0); |
| 433 | |
| 434 | // Not all bytes are zero. In a valid Stream it indicates the |
| 435 | // beginning of the next Stream. Read the rest of the Stream Header |
| 436 | // and initialize the XZ decoder. |
| 437 | inData.readFully(buf, 4, DecoderUtil.STREAM_HEADER_SIZE - 4); |
| 438 | |
| 439 | try { |
| 440 | xzIn = new SingleXZInputStream(in, memoryLimit, verifyCheck, buf, |
| 441 | arrayCache); |
| 442 | } catch (XZFormatException e) { |
| 443 | // Since this isn't the first .xz Stream, it is more |
| 444 | // logical to tell that the data is corrupt. |
| 445 | throw new CorruptedInputException( |
| 446 | "Garbage after a valid XZ Stream"); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Returns the number of uncompressed bytes that can be read |
| 452 | * without blocking. The value is returned with an assumption |
| 453 | * that the compressed input data will be valid. If the compressed |
| 454 | * data is corrupt, {@code CorruptedInputException} may get |
| 455 | * thrown before the number of bytes claimed to be available have |
| 456 | * been read from this input stream. |
| 457 | * |
| 458 | * @return the number of uncompressed bytes that can be read |
| 459 | * without blocking |
| 460 | */ |
| 461 | @Override |
| 462 | public int available() throws IOException { |
| 463 | if (in == null) |
| 464 | throw new XZIOException("Stream closed"); |
| 465 | |
| 466 | if (exception != null) |
| 467 | throw exception; |
| 468 | |
| 469 | return xzIn == null ? 0 : xzIn.available(); |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * Closes the stream and calls {@code in.close()}. |
| 474 | * If the stream was already closed, this does nothing. |
| 475 | * <p> |
| 476 | * This is equivalent to {@code close(true)}. |
| 477 | * |
| 478 | * @throws IOException if thrown by {@code in.close()} |
| 479 | */ |
| 480 | @Override |
| 481 | public void close() throws IOException { |
| 482 | close(true); |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Closes the stream and optionally calls {@code in.close()}. |
| 487 | * If the stream was already closed, this does nothing. |
| 488 | * If {@code close(false)} has been called, a further call of |
| 489 | * {@code close(true)} does nothing (it doesn't call {@code in.close()}). |
| 490 | * <p> |
| 491 | * If you don't want to close the underlying {@code InputStream}, |
| 492 | * there is usually no need to worry about closing this stream either; |
| 493 | * it's fine to do nothing and let the garbage collector handle it. |
| 494 | * However, if you are using {@link ArrayCache}, {@code close(false)} |
| 495 | * can be useful to put the allocated arrays back to the cache without |
| 496 | * closing the underlying {@code InputStream}. |
| 497 | * <p> |
| 498 | * Note that if you successfully reach the end of the stream |
| 499 | * ({@code read} returns {@code -1}), the arrays are automatically |
| 500 | * put back to the cache by that {@code read} call. In this |
| 501 | * situation {@code close(false)} is redundant (but harmless). |
| 502 | * |
| 503 | * @throws IOException if thrown by {@code in.close()} |
| 504 | * |
| 505 | * @since 1.7 |
| 506 | */ |
| 507 | public void close(boolean closeInput) throws IOException { |
| 508 | if (in != null) { |
| 509 | if (xzIn != null) { |
| 510 | xzIn.close(false); |
| 511 | xzIn = null; |
| 512 | } |
| 513 | |
| 514 | try { |
| 515 | if (closeInput) |
| 516 | in.close(); |
| 517 | } finally { |
| 518 | in = null; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |