Debian Codegraph @

src/org/tukaani/xz/lzma/LZMAEncoderFast.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.ArrayCache;
9 import org.tukaani.xz.lz.LZEncoder;
10 import org.tukaani.xz.lz.Matches;
11 import org.tukaani.xz.rangecoder.RangeEncoder;
12
13 final class LZMAEncoderFast extends LZMAEncoder {
14 private static final int EXTRA_SIZE_BEFORE = 1;
15 private static final int EXTRA_SIZE_AFTER = MATCH_LEN_MAX - 1;
16
17 private Matches matches = null;
18
19 static int getMemoryUsage(int dictSize, int extraSizeBefore, int mf) {
20 return LZEncoder.getMemoryUsage(
21 dictSize, Math.max(extraSizeBefore, EXTRA_SIZE_BEFORE),
22 EXTRA_SIZE_AFTER, MATCH_LEN_MAX, mf);
23 }
24
25 LZMAEncoderFast(RangeEncoder rc, int lc, int lp, int pb,
26 int dictSize, int extraSizeBefore,
27 int niceLen, int mf, int depthLimit,
28 ArrayCache arrayCache) {
29 super(rc, LZEncoder.getInstance(dictSize,
30 Math.max(extraSizeBefore,
31 EXTRA_SIZE_BEFORE),
32 EXTRA_SIZE_AFTER,
33 niceLen, MATCH_LEN_MAX,
34 mf, depthLimit, arrayCache),
35 lc, lp, pb, dictSize, niceLen);
36 }
37
38 private boolean changePair(int smallDist, int bigDist) {
39 return smallDist < (bigDist >>> 7);
40 }
41
42 @Override
43 int getNextSymbol() {
44 // Get the matches for the next byte unless readAhead indicates
45 // that we already got the new matches during the previous call
46 // to this function.
47 if (readAhead == -1)
48 matches = getMatches();
49
50 back = -1;
51
52 // Get the number of bytes available in the dictionary, but
53 // not more than the maximum match length. If there aren't
54 // enough bytes remaining to encode a match at all, return
55 // immediately to encode this byte as a literal.
56 int avail = Math.min(lz.getAvail(), MATCH_LEN_MAX);
57 if (avail < MATCH_LEN_MIN)
58 return 1;
59
60 // Look for a match from the previous four match distances.
61 int bestRepLen = 0;
62 int bestRepIndex = 0;
63 for (int rep = 0; rep < REPS; ++rep) {
64 int len = lz.getMatchLen(reps[rep], avail);
65 if (len < MATCH_LEN_MIN)
66 continue;
67
68 // If it is long enough, return it.
69 if (len >= niceLen) {
70 back = rep;
71 skip(len - 1);
72 return len;
73 }
74
75 // Remember the index and length of the best repeated match.
76 if (len > bestRepLen) {
77 bestRepIndex = rep;
78 bestRepLen = len;
79 }
80 }
81
82 int mainLen = 0;
83 int mainDist = 0;
84
85 if (matches.count > 0) {
86 mainLen = matches.len[matches.count - 1];
87 mainDist = matches.dist[matches.count - 1];
88
89 if (mainLen >= niceLen) {
90 back = mainDist + REPS;
91 skip(mainLen - 1);
92 return mainLen;
93 }
94
95 while (matches.count > 1
96 && mainLen == matches.len[matches.count - 2] + 1) {
97 if (!changePair(matches.dist[matches.count - 2], mainDist))
98 break;
99
100 --matches.count;
101 mainLen = matches.len[matches.count - 1];
102 mainDist = matches.dist[matches.count - 1];
103 }
104
105 if (mainLen == MATCH_LEN_MIN && mainDist >= 0x80)
106 mainLen = 1;
107 }
108
109 if (bestRepLen >= MATCH_LEN_MIN) {
110 if (bestRepLen + 1 >= mainLen
111 || (bestRepLen + 2 >= mainLen && mainDist >= (1 << 9))
112 || (bestRepLen + 3 >= mainLen && mainDist >= (1 << 15))) {
113 back = bestRepIndex;
114 skip(bestRepLen - 1);
115 return bestRepLen;
116 }
117 }
118
119 if (mainLen < MATCH_LEN_MIN || avail <= MATCH_LEN_MIN)
120 return 1;
121
122 // Get the next match. Test if it is better than the current match.
123 // If so, encode the current byte as a literal.
124 matches = getMatches();
125
126 if (matches.count > 0) {
127 int newLen = matches.len[matches.count - 1];
128 int newDist = matches.dist[matches.count - 1];
129
130 if ((newLen >= mainLen && newDist < mainDist)
131 || (newLen == mainLen + 1
132 && !changePair(mainDist, newDist))
133 || newLen > mainLen + 1
134 || (newLen + 1 >= mainLen
135 && mainLen >= MATCH_LEN_MIN + 1
136 && changePair(newDist, mainDist)))
137 return 1;
138 }
139
140 int limit = Math.max(mainLen - 1, MATCH_LEN_MIN);
141 for (int rep = 0; rep < REPS; ++rep)
142 if (lz.getMatchLen(reps[rep], limit) == limit)
143 return 1;
144
145 back = mainDist + REPS;
146 skip(mainLen - 2);
147 return mainLen;
148 }
149 }