Debian Codegraph @

src/org/tukaani/xz/lzma/LZMACoder.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 org.tukaani.xz.rangecoder.RangeCoder;
9
10 abstract class LZMACoder {
11 static final int POS_STATES_MAX = 1 << 4;
12
13 static final int MATCH_LEN_MIN = 2;
14 static final int MATCH_LEN_MAX = MATCH_LEN_MIN + LengthCoder.LOW_SYMBOLS
15 + LengthCoder.MID_SYMBOLS
16 + LengthCoder.HIGH_SYMBOLS - 1;
17
18 static final int DIST_STATES = 4;
19 static final int DIST_SLOTS = 1 << 6;
20 static final int DIST_MODEL_START = 4;
21 static final int DIST_MODEL_END = 14;
22 static final int FULL_DISTANCES = 1 << (DIST_MODEL_END / 2);
23
24 static final int ALIGN_BITS = 4;
25 static final int ALIGN_SIZE = 1 << ALIGN_BITS;
26 static final int ALIGN_MASK = ALIGN_SIZE - 1;
27
28 static final int REPS = 4;
29
30 final int posMask;
31
32 final int[] reps = new int[REPS];
33 final State state = new State();
34
35 final short[][] isMatch = new short[State.STATES][POS_STATES_MAX];
36 final short[] isRep = new short[State.STATES];
37 final short[] isRep0 = new short[State.STATES];
38 final short[] isRep1 = new short[State.STATES];
39 final short[] isRep2 = new short[State.STATES];
40 final short[][] isRep0Long = new short[State.STATES][POS_STATES_MAX];
41 final short[][] distSlots = new short[DIST_STATES][DIST_SLOTS];
42 final short[][] distSpecial = { new short[2], new short[2],
43 new short[4], new short[4],
44 new short[8], new short[8],
45 new short[16], new short[16],
46 new short[32], new short[32] };
47 final short[] distAlign = new short[ALIGN_SIZE];
48
49 static final int getDistState(int len) {
50 return len < DIST_STATES + MATCH_LEN_MIN
51 ? len - MATCH_LEN_MIN
52 : DIST_STATES - 1;
53 }
54
55 LZMACoder(int pb) {
56 posMask = (1 << pb) - 1;
57 }
58
59 void reset() {
60 reps[0] = 0;
61 reps[1] = 0;
62 reps[2] = 0;
63 reps[3] = 0;
64 state.reset();
65
66 for (int i = 0; i < isMatch.length; ++i)
67 RangeCoder.initProbs(isMatch[i]);
68
69 RangeCoder.initProbs(isRep);
70 RangeCoder.initProbs(isRep0);
71 RangeCoder.initProbs(isRep1);
72 RangeCoder.initProbs(isRep2);
73
74 for (int i = 0; i < isRep0Long.length; ++i)
75 RangeCoder.initProbs(isRep0Long[i]);
76
77 for (int i = 0; i < distSlots.length; ++i)
78 RangeCoder.initProbs(distSlots[i]);
79
80 for (int i = 0; i < distSpecial.length; ++i)
81 RangeCoder.initProbs(distSpecial[i]);
82
83 RangeCoder.initProbs(distAlign);
84 }
85
86
87 abstract class LiteralCoder {
88 private final int lc;
89 private final int literalPosMask;
90
91 LiteralCoder(int lc, int lp) {
92 this.lc = lc;
93 this.literalPosMask = (1 << lp) - 1;
94 }
95
96 final int getSubcoderIndex(int prevByte, int pos) {
97 int low = prevByte >> (8 - lc);
98 int high = (pos & literalPosMask) << lc;
99 return low + high;
100 }
101
102
103 abstract class LiteralSubcoder {
104 final short[] probs = new short[0x300];
105
106 void reset() {
107 RangeCoder.initProbs(probs);
108 }
109 }
110 }
111
112
113 abstract class LengthCoder {
114 static final int LOW_SYMBOLS = 1 << 3;
115 static final int MID_SYMBOLS = 1 << 3;
116 static final int HIGH_SYMBOLS = 1 << 8;
117
118 final short[] choice = new short[2];
119 final short[][] low = new short[POS_STATES_MAX][LOW_SYMBOLS];
120 final short[][] mid = new short[POS_STATES_MAX][MID_SYMBOLS];
121 final short[] high = new short[HIGH_SYMBOLS];
122
123 void reset() {
124 RangeCoder.initProbs(choice);
125
126 for (int i = 0; i < low.length; ++i)
127 RangeCoder.initProbs(low[i]);
128
129 for (int i = 0; i < mid.length; ++i)
130 RangeCoder.initProbs(mid[i]);
131
132 RangeCoder.initProbs(high);
133 }
134 }
135 }