Debian Codegraph @

src/org/tukaani/xz/lz/HC4.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.lz;
7
8 import org.tukaani.xz.ArrayCache;
9
10 // Hash Chain match finder with 2-, 3-, and 4-byte hashing
11 final class HC4 extends LZEncoder {
12 private final Hash234 hash;
13 private final int[] chain;
14 private final Matches matches;
15 private final int depthLimit;
16
17 private final int cyclicSize;
18 private int cyclicPos = -1;
19 private int lzPos;
20
21 /**
22 * Gets approximate memory usage of the match finder as kibibytes.
23 */
24 static int getMemoryUsage(int dictSize) {
25 return Hash234.getMemoryUsage(dictSize) + dictSize / (1024 / 4) + 10;
26 }
27
28 /**
29 * Creates a new LZEncoder with the HC4 match finder.
30 * See {@code LZEncoder.getInstance} for parameter descriptions.
31 */
32 HC4(int dictSize, int beforeSizeMin, int readAheadMax,
33 int niceLen, int matchLenMax, int depthLimit,
34 ArrayCache arrayCache) {
35 super(dictSize, beforeSizeMin, readAheadMax, niceLen, matchLenMax,
36 arrayCache);
37
38 hash = new Hash234(dictSize, arrayCache);
39
40 // +1 because we need dictSize bytes of history + the current byte.
41 cyclicSize = dictSize + 1;
42 chain = arrayCache.getIntArray(cyclicSize, false);
43 lzPos = cyclicSize;
44
45 // Subtracting 1 because the shortest match that this match
46 // finder can find is 2 bytes, so there's no need to reserve
47 // space for one-byte matches.
48 matches = new Matches(niceLen - 1);
49
50 // Use a default depth limit if no other value was specified.
51 // The default is just something based on experimentation;
52 // it's nothing magic.
53 this.depthLimit = (depthLimit > 0) ? depthLimit : 4 + niceLen / 4;
54 }
55
56 @Override
57 public void putArraysToCache(ArrayCache arrayCache) {
58 arrayCache.putArray(chain);
59 hash.putArraysToCache(arrayCache);
60 super.putArraysToCache(arrayCache);
61 }
62
63 /**
64 * Moves to the next byte, checks that there is enough available space,
65 * and possibly normalizes the hash tables and the hash chain.
66 *
67 * @return number of bytes available, including the current byte
68 */
69 private int movePos() {
70 int avail = movePos(4, 4);
71
72 if (avail != 0) {
73 if (++lzPos == Integer.MAX_VALUE) {
74 int normalizationOffset = Integer.MAX_VALUE - cyclicSize;
75 hash.normalize(normalizationOffset);
76 normalize(chain, cyclicSize, normalizationOffset);
77 lzPos -= normalizationOffset;
78 }
79
80 if (++cyclicPos == cyclicSize)
81 cyclicPos = 0;
82 }
83
84 return avail;
85 }
86
87 @Override
88 public Matches getMatches() {
89 matches.count = 0;
90 int matchLenLimit = matchLenMax;
91 int niceLenLimit = niceLen;
92 int avail = movePos();
93
94 if (avail < matchLenLimit) {
95 if (avail == 0)
96 return matches;
97
98 matchLenLimit = avail;
99 if (niceLenLimit > avail)
100 niceLenLimit = avail;
101 }
102
103 hash.calcHashes(buf, readPos);
104 int delta2 = lzPos - hash.getHash2Pos();
105 int delta3 = lzPos - hash.getHash3Pos();
106 int currentMatch = hash.getHash4Pos();
107 hash.updateTables(lzPos);
108
109 chain[cyclicPos] = currentMatch;
110
111 int lenBest = 0;
112
113 // See if the hash from the first two bytes found a match.
114 // The hashing algorithm guarantees that if the first byte
115 // matches, also the second byte does, so there's no need to
116 // test the second byte.
117 if (delta2 < cyclicSize && buf[readPos - delta2] == buf[readPos]) {
118 lenBest = 2;
119 matches.len[0] = 2;
120 matches.dist[0] = delta2 - 1;
121 matches.count = 1;
122 }
123
124 // See if the hash from the first three bytes found a match that
125 // is different from the match possibly found by the two-byte hash.
126 // Also here the hashing algorithm guarantees that if the first byte
127 // matches, also the next two bytes do.
128 if (delta2 != delta3 && delta3 < cyclicSize
129 && buf[readPos - delta3] == buf[readPos]) {
130 lenBest = 3;
131 matches.dist[matches.count++] = delta3 - 1;
132 delta2 = delta3;
133 }
134
135 // If a match was found, see how long it is.
136 if (matches.count > 0) {
137 lenBest = MatchLength.getLen(buf, readPos, delta2,
138 lenBest, matchLenLimit);
139 matches.len[matches.count - 1] = lenBest;
140
141 // Return if it is long enough (niceLen or reached the end of
142 // the dictionary).
143 if (lenBest >= niceLenLimit)
144 return matches;
145 }
146
147 // Long enough match wasn't found so easily. Look for better matches
148 // from the hash chain.
149 if (lenBest < 3)
150 lenBest = 3;
151
152 int depth = depthLimit;
153
154 while (true) {
155 int delta = lzPos - currentMatch;
156
157 // Return if the search depth limit has been reached or
158 // if the distance of the potential match exceeds the
159 // dictionary size.
160 if (depth-- == 0 || delta >= cyclicSize)
161 return matches;
162
163 currentMatch = chain[cyclicPos - delta
164 + (delta > cyclicPos ? cyclicSize : 0)];
165
166 // Test the first byte and the first new byte that would give us
167 // a match that is at least one byte longer than lenBest. This
168 // too short matches get quickly skipped.
169 if (buf[readPos + lenBest - delta] == buf[readPos + lenBest]
170 && buf[readPos - delta] == buf[readPos]) {
171 // Calculate the length of the match.
172 int len = MatchLength.getLen(buf, readPos, delta,
173 1, matchLenLimit);
174
175 // Use the match if and only if it is better than the longest
176 // match found so far.
177 if (len > lenBest) {
178 lenBest = len;
179 matches.len[matches.count] = len;
180 matches.dist[matches.count] = delta - 1;
181 ++matches.count;
182
183 // Return if it is long enough (niceLen or reached the
184 // end of the dictionary).
185 if (len >= niceLenLimit)
186 return matches;
187 }
188 }
189 }
190 }
191
192 @Override
193 public void skip(int len) {
194 assert len >= 0;
195
196 while (len-- > 0) {
197 if (movePos() != 0) {
198 // Update the hash chain and hash tables.
199 hash.calcHashes(buf, readPos);
200 chain[cyclicPos] = hash.getHash4Pos();
201 hash.updateTables(lzPos);
202 }
203 }
204 }
205 }