Debian Codegraph @

src/org/tukaani/xz/lzma/LZMADecoder.java

raw symbols SCIP
1 // SPDX-License-Identifier: 0BSD
2 // SPDX-FileCopyrightText: The XZ for Java authors and contributors
3 // SPDX-FileContributor: Lasse Collin <[email protected]>
4 // SPDX-FileContributor: Igor Pavlov <https://7-zip.org/>
5
6 package org.tukaani.xz.lzma;
7
8 import java.io.IOException;
9 import org.tukaani.xz.lz.LZDecoder;
10 import org.tukaani.xz.rangecoder.RangeDecoder;
11
12 public final class LZMADecoder extends LZMACoder {
13 private final LZDecoder lz;
14 private final RangeDecoder rc;
15 private final LiteralDecoder literalDecoder;
16 private final LengthDecoder matchLenDecoder = new LengthDecoder();
17 private final LengthDecoder repLenDecoder = new LengthDecoder();
18
19 public LZMADecoder(LZDecoder lz, RangeDecoder rc, int lc, int lp, int pb) {
20 super(pb);
21 this.lz = lz;
22 this.rc = rc;
23 this.literalDecoder = new LiteralDecoder(lc, lp);
24 reset();
25 }
26
27 @Override
28 public void reset() {
29 super.reset();
30 literalDecoder.reset();
31 matchLenDecoder.reset();
32 repLenDecoder.reset();
33 }
34
35 /**
36 * Returns true if LZMA end marker was detected. It is encoded as
37 * the maximum match distance which with signed ints becomes -1. This
38 * function is needed only for LZMA1. LZMA2 doesn't use the end marker
39 * in the LZMA layer.
40 */
41 public boolean endMarkerDetected() {
42 return reps[0] == -1;
43 }
44
45 public void decode() throws IOException {
46 lz.repeatPending();
47
48 while (lz.hasSpace()) {
49 int posState = lz.getPos() & posMask;
50
51 if (rc.decodeBit(isMatch[state.get()], posState) == 0) {
52 literalDecoder.decode();
53 } else {
54 int len = rc.decodeBit(isRep, state.get()) == 0
55 ? decodeMatch(posState)
56 : decodeRepMatch(posState);
57
58 // NOTE: With LZMA1 streams that have the end marker,
59 // this will throw CorruptedInputException. LZMAInputStream
60 // handles it specially.
61 lz.repeat(reps[0], len);
62 }
63 }
64
65 rc.normalize();
66 }
67
68 private int decodeMatch(int posState) throws IOException {
69 state.updateMatch();
70
71 reps[3] = reps[2];
72 reps[2] = reps[1];
73 reps[1] = reps[0];
74
75 int len = matchLenDecoder.decode(posState);
76 int distSlot = rc.decodeBitTree(distSlots[getDistState(len)]);
77
78 if (distSlot < DIST_MODEL_START) {
79 reps[0] = distSlot;
80 } else {
81 int limit = (distSlot >> 1) - 1;
82 reps[0] = (2 | (distSlot & 1)) << limit;
83
84 if (distSlot < DIST_MODEL_END) {
85 reps[0] |= rc.decodeReverseBitTree(
86 distSpecial[distSlot - DIST_MODEL_START]);
87 } else {
88 reps[0] |= rc.decodeDirectBits(limit - ALIGN_BITS)
89 << ALIGN_BITS;
90 reps[0] |= rc.decodeReverseBitTree(distAlign);
91 }
92 }
93
94 return len;
95 }
96
97 private int decodeRepMatch(int posState) throws IOException {
98 if (rc.decodeBit(isRep0, state.get()) == 0) {
99 if (rc.decodeBit(isRep0Long[state.get()], posState) == 0) {
100 state.updateShortRep();
101 return 1;
102 }
103 } else {
104 int tmp;
105
106 if (rc.decodeBit(isRep1, state.get()) == 0) {
107 tmp = reps[1];
108 } else {
109 if (rc.decodeBit(isRep2, state.get()) == 0) {
110 tmp = reps[2];
111 } else {
112 tmp = reps[3];
113 reps[3] = reps[2];
114 }
115
116 reps[2] = reps[1];
117 }
118
119 reps[1] = reps[0];
120 reps[0] = tmp;
121 }
122
123 state.updateLongRep();
124
125 return repLenDecoder.decode(posState);
126 }
127
128
129 private class LiteralDecoder extends LiteralCoder {
130 private final LiteralSubdecoder[] subdecoders;
131
132 LiteralDecoder(int lc, int lp) {
133 super(lc, lp);
134
135 subdecoders = new LiteralSubdecoder[1 << (lc + lp)];
136 for (int i = 0; i < subdecoders.length; ++i)
137 subdecoders[i] = new LiteralSubdecoder();
138 }
139
140 void reset() {
141 for (int i = 0; i < subdecoders.length; ++i)
142 subdecoders[i].reset();
143 }
144
145 void decode() throws IOException {
146 int i = getSubcoderIndex(lz.getByte(0), lz.getPos());
147 subdecoders[i].decode();
148 }
149
150
151 private class LiteralSubdecoder extends LiteralSubcoder {
152 void decode() throws IOException {
153 int symbol = 1;
154
155 if (state.isLiteral()) {
156 do {
157 symbol = (symbol << 1) | rc.decodeBit(probs, symbol);
158 } while (symbol < 0x100);
159
160 } else {
161 int matchByte = lz.getByte(reps[0]);
162 int offset = 0x100;
163 int matchBit;
164 int bit;
165
166 do {
167 matchByte <<= 1;
168 matchBit = matchByte & offset;
169 bit = rc.decodeBit(probs, offset + matchBit + symbol);
170 symbol = (symbol << 1) | bit;
171 offset &= (0 - bit) ^ ~matchBit;
172 } while (symbol < 0x100);
173 }
174
175 lz.putByte((byte)symbol);
176 state.updateLiteral();
177 }
178 }
179 }
180
181
182 private class LengthDecoder extends LengthCoder {
183 int decode(int posState) throws IOException {
184 if (rc.decodeBit(choice, 0) == 0)
185 return rc.decodeBitTree(low[posState]) + MATCH_LEN_MIN;
186
187 if (rc.decodeBit(choice, 1) == 0)
188 return rc.decodeBitTree(mid[posState])
189 + MATCH_LEN_MIN + LOW_SYMBOLS;
190
191 return rc.decodeBitTree(high)
192 + MATCH_LEN_MIN + LOW_SYMBOLS + MID_SYMBOLS;
193 }
194 }
195 }