LZMA2Decoder.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.archivers.sevenz;

  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;

  21. import org.apache.commons.compress.MemoryLimitException;
  22. import org.tukaani.xz.FinishableWrapperOutputStream;
  23. import org.tukaani.xz.LZMA2InputStream;
  24. import org.tukaani.xz.LZMA2Options;

  25. final class LZMA2Decoder extends AbstractCoder {

  26.     LZMA2Decoder() {
  27.         super(LZMA2Options.class, Number.class);
  28.     }

  29.     @Override
  30.     InputStream decode(final String archiveName, final InputStream in, final long uncompressedLength, final Coder coder, final byte[] password,
  31.             final int maxMemoryLimitInKb) throws IOException {
  32.         try {
  33.             final int dictionarySize = getDictionarySize(coder);
  34.             final int memoryUsageInKb = LZMA2InputStream.getMemoryUsage(dictionarySize);
  35.             if (memoryUsageInKb > maxMemoryLimitInKb) {
  36.                 throw new MemoryLimitException(memoryUsageInKb, maxMemoryLimitInKb);
  37.             }
  38.             return new LZMA2InputStream(in, dictionarySize);
  39.         } catch (final IllegalArgumentException ex) { // NOSONAR
  40.             throw new IOException(ex);
  41.         }
  42.     }

  43.     @SuppressWarnings("resource") // Caller closes.
  44.     @Override
  45.     OutputStream encode(final OutputStream out, final Object opts) throws IOException {
  46.         return getOptions(opts).getOutputStream(new FinishableWrapperOutputStream(out));
  47.     }

  48.     private int getDictionarySize(final Coder coder) throws IOException {
  49.         if (coder.properties == null) {
  50.             throw new IOException("Missing LZMA2 properties");
  51.         }
  52.         if (coder.properties.length < 1) {
  53.             throw new IOException("LZMA2 properties too short");
  54.         }
  55.         final int dictionarySizeBits = 0xff & coder.properties[0];
  56.         if ((dictionarySizeBits & ~0x3f) != 0) {
  57.             throw new IOException("Unsupported LZMA2 property bits");
  58.         }
  59.         if (dictionarySizeBits > 40) {
  60.             throw new IOException("Dictionary larger than 4GiB maximum size");
  61.         }
  62.         if (dictionarySizeBits == 40) {
  63.             return 0xFFFFffff;
  64.         }
  65.         return (2 | dictionarySizeBits & 0x1) << dictionarySizeBits / 2 + 11;
  66.     }

  67.     private int getDictSize(final Object opts) {
  68.         if (opts instanceof LZMA2Options) {
  69.             return ((LZMA2Options) opts).getDictSize();
  70.         }
  71.         return numberOptionOrDefault(opts);
  72.     }

  73.     private LZMA2Options getOptions(final Object opts) throws IOException {
  74.         if (opts instanceof LZMA2Options) {
  75.             return (LZMA2Options) opts;
  76.         }
  77.         final LZMA2Options options = new LZMA2Options();
  78.         options.setDictSize(numberOptionOrDefault(opts));
  79.         return options;
  80.     }

  81.     @Override
  82.     byte[] getOptionsAsProperties(final Object opts) {
  83.         final int dictSize = getDictSize(opts);
  84.         final int lead = Integer.numberOfLeadingZeros(dictSize);
  85.         final int secondBit = (dictSize >>> 30 - lead) - 2;
  86.         return new byte[] { (byte) ((19 - lead) * 2 + secondBit) };
  87.     }

  88.     @Override
  89.     Object getOptionsFromCoder(final Coder coder, final InputStream in) throws IOException {
  90.         return getDictionarySize(coder);
  91.     }

  92.     private int numberOptionOrDefault(final Object opts) {
  93.         return toInt(opts, LZMA2Options.DICT_SIZE_DEFAULT);
  94.     }
  95. }