View Javadoc
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.zip;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.ByteOrder;
24  
25  import org.apache.commons.compress.compressors.lzw.LZWInputStream;
26  
27  /**
28   * Input stream that decompresses ZIP method 1 (unshrinking). A variation of the LZW algorithm, with some twists.
29   *
30   * @NotThreadSafe
31   * @since 1.7
32   */
33  final class UnshrinkingInputStream extends LZWInputStream {
34      private static final int MAX_CODE_SIZE = 13;
35      private static final int MAX_TABLE_SIZE = 1 << MAX_CODE_SIZE;
36      private final boolean[] isUsed;
37  
38      /**
39       * IOException is not actually thrown!
40       *
41       * @param inputStream
42       */
43      UnshrinkingInputStream(final InputStream inputStream) {
44          super(inputStream, ByteOrder.LITTLE_ENDIAN);
45          setClearCode(DEFAULT_CODE_SIZE);
46          initializeTables(MAX_CODE_SIZE);
47          isUsed = new boolean[getPrefixesLength()];
48          for (int i = 0; i < 1 << 8; i++) {
49              isUsed[i] = true;
50          }
51          setTableSize(getClearCode() + 1);
52      }
53  
54      @Override
55      protected int addEntry(final int previousCode, final byte character) throws IOException {
56          int tableSize = getTableSize();
57          while (tableSize < MAX_TABLE_SIZE && isUsed[tableSize]) {
58              tableSize++;
59          }
60          setTableSize(tableSize);
61          final int idx = addEntry(previousCode, character, MAX_TABLE_SIZE);
62          if (idx >= 0) {
63              isUsed[idx] = true;
64          }
65          return idx;
66      }
67  
68      @Override
69      protected int decompressNextSymbol() throws IOException {
70          //
71          // table entry table entry
72          // _____________ _____
73          // table entry / \ / \
74          // ____________/ \ \
75          // / / \ / \ \
76          // +---+---+---+---+---+---+---+---+---+---+
77          // | . | . | . | . | . | . | . | . | . | . |
78          // +---+---+---+---+---+---+---+---+---+---+
79          // |<--------->|<------------->|<----->|<->|
80          // symbol symbol symbol symbol
81          //
82          final int code = readNextCode();
83          if (code < 0) {
84              return -1;
85          }
86          if (code != getClearCode()) {
87              boolean addedUnfinishedEntry = false;
88              int effectiveCode = code;
89              if (!isUsed[code]) {
90                  effectiveCode = addRepeatOfPreviousCode();
91                  addedUnfinishedEntry = true;
92              }
93              return expandCodeToOutputStack(effectiveCode, addedUnfinishedEntry);
94          }
95          final int subCode = readNextCode();
96          if (subCode < 0) {
97              throw new IOException("Unexpected EOF;");
98          }
99          if (subCode == 1) {
100             if (getCodeSize() >= MAX_CODE_SIZE) {
101                 throw new IOException("Attempt to increase code size beyond maximum");
102             }
103             incrementCodeSize();
104         } else if (subCode == 2) {
105             partialClear();
106             setTableSize(getClearCode() + 1);
107         } else {
108             throw new IOException("Invalid clear code subcode " + subCode);
109         }
110         return 0;
111     }
112 
113     private void partialClear() {
114         final boolean[] isParent = new boolean[MAX_TABLE_SIZE];
115         for (int i = 0; i < isUsed.length; i++) {
116             if (isUsed[i] && getPrefix(i) != UNUSED_PREFIX) {
117                 isParent[getPrefix(i)] = true;
118             }
119         }
120         for (int i = getClearCode() + 1; i < isParent.length; i++) {
121             if (!isParent[i]) {
122                 isUsed[i] = false;
123                 setPrefix(i, UNUSED_PREFIX);
124             }
125         }
126     }
127 }