forked from deus-libri/preflate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreflate_predictor_state.cpp
448 lines (403 loc) · 15.2 KB
/
preflate_predictor_state.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/* Copyright 2018 Dirk Steinke
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "preflate_constants.h"
#include "preflate_predictor_state.h"
#include <algorithm>
PreflatePredictorState::PreflatePredictorState(
const PreflateHashChainExt& hash_,
const PreflateSeqChain& seq_,
const PreflateParserConfig& config_,
const int wbits,
const int mbits)
: hash(hash_)
, seq(seq_)
, windowBytes(1 << wbits)
, maxTokenCount((1 << (6 + mbits)) - 1)
, config(config_) {
}
/* deflate has four parameters:
* - strategy: the strategy can usually be guessed by looking on the given deflate stream
* (e.g. only stored blocks -> stored,
* max distance = 0 -> huffman-only,
* max distance = 1 -> rle,
* only fixed huffman trees -> fixed-huffman-tree,
* otherwise default)
* - window bits: known by max distance, less window bits would be impossible, more window
* bits would be pointless
* - mem level: used for hash calculation and number of tokens per block
* the latter can be used to put a lower limit on mem level
* - compression level: parameters for the reference finder
*
* When reencoding a deflate stream, the predictor has to make a token proposal (either to
* encode a literal or a (dist, len) pair. A correction data stream will either accept the
* proposal, or change it to the correct values. The corrected values are then fed to the
* deflate encoder, and to the predictor.
*
* The main problem is to find the missing deflate parameters (compression level and
* mem level) to minimize the number and complexity of required corrections.
* Data streams that were encoded with zlib should get perfect recognition,
* requiring only the detected deflate parameters to be encoded for perfect reconstruction.
* Data streams from other encoders (7zip, kzip, ...) should be reconstructible with minimal
* corrective instructions, similar to reflate.
*
* kzip does not limit block size to < 64k tokens, while zlib enforces it for various reasons
* (and defaults to max 16k tokens).
* Prediction for end-of-block is therefore independent of literal/reference prediction.
*
* Mixing or interpolating the prediction from different parameter packs is
* possible, but not planned right now.
*/
unsigned PreflatePredictorState::prefixCompare(
const unsigned char* s1,
const unsigned char* s2,
const unsigned bestLen,
const unsigned maxLen) {
if (s1[bestLen] != s2[bestLen]) {
return 0;
}
if (s1[0] != s2[0] || s1[1] != s2[1] || s1[2] != s2[2]) {
return 0;
}
const unsigned char* scan = s2 + 3;
const unsigned char* match = s1 + 3;
const unsigned char* scanend = s2 + maxLen;
/* while (scan < scanend
&& *++scan == *++match && *++scan == *++match
&& *++scan == *++match && *++scan == *++match
&& *++scan == *++match && *++scan == *++match
&& *++scan == *++match && *++scan == *++match) {
}*/
while (scan < scanend
&& *scan == *match) {
++scan;
++match;
}
return scan - s2;
}
unsigned PreflatePredictorState::suffixCompare(
const unsigned char* s1,
const unsigned char* s2,
const unsigned bestLen,
const unsigned maxLen) {
if (s1[bestLen] != s2[bestLen]) {
return 0;
}
unsigned len = 0;
while (s1[len] == s2[len] && ++len < maxLen) {
}
return len;
}
bool PreflatePredictorState::createMatchHelper(
MatchHelper& helper,
const unsigned prevLen,
const unsigned startPos,
const bool veryFarMatches,
const bool matchesToStart,
const unsigned maxDepth) {
helper.maxLen = std::min(totalInputSize() - startPos, (unsigned)PreflateConstants::MAX_MATCH);
if (helper.maxLen < std::max<uint32_t>(prevLen + 1, PreflateConstants::MIN_MATCH)) {
return false;
}
helper.startPos = startPos;
unsigned maxDistToStart = startPos - (matchesToStart ? 0 : 1);
if (veryFarMatches) {
helper.curMaxDistHop1Plus
= helper.curMaxDistHop0
= std::min(maxDistToStart, windowSize());
} else {
unsigned maxDist = windowSize() - PreflateConstants::MIN_LOOKAHEAD;
helper.curMaxDistHop0 = std::min(maxDistToStart, maxDist);
helper.curMaxDistHop1Plus = std::min(maxDistToStart, maxDist - 1);
}
if (maxDepth > 0) {
helper.maxChain = maxDepth;
helper.niceLen = helper.maxLen;
} else {
helper.maxChain = maxChainLength();/* max hash chain length */
helper.niceLen = std::min(niceMatchLength(), helper.maxLen);
if (prevLen >= goodMatchLength()) {
helper.maxChain >>= 2;
}
}
return true;
}
PreflateToken PreflatePredictorState::match(
const unsigned hashHead,
const unsigned prevLen,
const unsigned offset,
const bool veryFarMatches,
const bool matchesToStart,
const unsigned maxDepth) {
PreflateToken bestMatch(PreflateToken::NONE);
MatchHelper h;
if (!createMatchHelper(h, prevLen, currentInputPos() + offset,
veryFarMatches, matchesToStart, maxDepth)) {
return bestMatch;
}
PreflateHashIterator chainIt = iterateFromNode(hashHead, h.startPos, h.curMaxDistHop1Plus);
// Handle ZLIB quirk: the very first entry in the hash chain can have a larger
// distance than all following entries
if (chainIt.dist() > h.curMaxDistHop0) {
return bestMatch;
}
const unsigned char* input = inputCursor() + offset;
unsigned bestLen = prevLen;
do {
const unsigned char* match = input - chainIt.dist();
unsigned matchLength = prefixCompare(match, input, bestLen, h.maxLen);
if (matchLength > bestLen) {
bestLen = matchLength;
bestMatch = PreflateToken(PreflateToken::REFERENCE, matchLength, chainIt.dist());
if (bestLen >= h.niceLen) {
break;
}
}
} while (chainIt.next() && h.maxChain-- > 1);
return bestMatch;
}
PreflateToken PreflatePredictorState::seqMatch(
const unsigned startPos,
const unsigned hashHead,
const unsigned prevLen,
const bool veryFarMatches,
const bool matchesToStart,
const unsigned maxDepth) {
PreflateToken bestMatch(PreflateToken::NONE);
MatchHelper h;
if (!createMatchHelper(h, prevLen, startPos,
veryFarMatches, matchesToStart, maxDepth)) {
return bestMatch;
}
PreflateSeqIterator chainIt = seq.iterateFromPos(startPos);
if (!chainIt) {
return bestMatch;
}
unsigned curSeqLen = std::min<uint32_t>(seq.len(startPos), h.maxLen);
unsigned curMaxDist = h.curMaxDistHop1Plus;
unsigned bestLen = prevLen;
if (curSeqLen < PreflateConstants::MIN_MATCH) {
// startPos is part of a bigger sequence,
// and the ZLIB quirk does not apply, yeah!
curSeqLen = std::min(chainIt.len() - chainIt.dist(), h.maxLen);
if (curSeqLen > prevLen && 1 <= h.curMaxDistHop0) {
bestLen = curSeqLen;
bestMatch = PreflateToken(PreflateToken::REFERENCE, curSeqLen, 1);
}
if (bestLen >= h.niceLen || !chainIt.next()) {
return bestMatch;
}
if (chainIt.dist() > h.curMaxDistHop1Plus + chainIt.len() - PreflateConstants::MIN_MATCH) {
return bestMatch;
}
} else {
unsigned minDistOff = chainIt.len() - PreflateConstants::MIN_MATCH;
if (chainIt.dist() > h.curMaxDistHop1Plus + minDistOff) {
if (chainIt.dist() > h.curMaxDistHop0 + minDistOff) {
return bestMatch;
}
// Handle ZLIB quirk: the very first entry in the hash chain can have a larger
// distance than all following entries
unsigned latestPos = h.startPos - chainIt.dist() + minDistOff;
unsigned depth = hash.getRelPosDepth(latestPos, hashHead);
if (depth == 0) {
curMaxDist = h.curMaxDistHop0;
}
}
}
const unsigned char* input = inputCursor() + startPos - currentInputPos();
unsigned bestSeqLen = std::min(curSeqLen, bestLen);
do {
if (chainIt.len() < bestSeqLen) {
// If we do not even meet the already matched number of sequence bytes,
// we can just skip this
continue;
}
unsigned oldBestSeqLen = bestSeqLen;
bestSeqLen = std::min<uint32_t>(std::min<uint32_t>(curSeqLen, chainIt.len()), h.niceLen);
unsigned bestDist = chainIt.dist() - chainIt.len() + bestSeqLen;
unsigned error = 0;
if (bestDist > curMaxDist) {
// best subsequence is already beyond the search range
error = bestDist - curMaxDist;
if (error > chainIt.len() - PreflateConstants::MIN_MATCH) {
break;
}
}
unsigned bestChainDepth = hash.getRelPosDepth(h.startPos - bestDist + error, hashHead);
if (bestChainDepth >= h.maxChain) {
// best subsequence is already beyond the search range
error += bestChainDepth - h.maxChain + 1;
if (error > chainIt.len() - PreflateConstants::MIN_MATCH) {
break;
}
}
if (error) {
if (bestSeqLen > std::max<uint32_t>(oldBestSeqLen, PreflateConstants::MIN_MATCH - 1) + error) {
bestMatch = PreflateToken(PreflateToken::REFERENCE, bestSeqLen - error, bestDist - error);
}
// Since we had to correct the length down, we know that
// the comparer cannot find a better match
break;
}
if (bestSeqLen == h.maxLen) {
bestMatch = PreflateToken(PreflateToken::REFERENCE, bestSeqLen, bestDist);
break;
} else {
const unsigned char* match = input - bestDist;
unsigned matchLength = bestSeqLen + suffixCompare(match + bestSeqLen, input + bestSeqLen, std::max(bestLen, bestSeqLen) - bestSeqLen, h.maxLen - bestSeqLen);
if (matchLength > bestLen) {
bestLen = matchLength;
bestMatch = PreflateToken(PreflateToken::REFERENCE, matchLength, bestDist);
if (bestLen >= h.niceLen) {
break;
}
}
}
curMaxDist = h.curMaxDistHop1Plus;
} while (chainIt.next());
return bestMatch;
}
PreflateNextMatchInfo PreflatePredictorState::nextMatchInfo(
const unsigned hashHead,
const PreflateToken& targetReference,
const PreflateHashChainExt& hash) {
PreflateNextMatchInfo result;
result.nextChainDepth = (unsigned short)~0u;
result.nextLen = 0;
result.nextDist = 0xffff;
unsigned maxLen = std::min(availableInputSize(), (unsigned)PreflateConstants::MAX_MATCH);
if (maxLen < (unsigned)PreflateConstants::MIN_MATCH) {
return result;
}
unsigned maxDist = windowSize() - PreflateConstants::MIN_LOOKAHEAD - 1;
unsigned curPos = currentInputPos();
unsigned curMaxDist = std::min(curPos - 1, maxDist);
unsigned curMaxDistAlt = std::min(curPos - 1, windowSize() - PreflateConstants::MIN_LOOKAHEAD);
const unsigned char* input = inputCursor();
unsigned startDepth = hash.getNodeDepth(hashHead);
unsigned maxChainOrg = maxChainLength();/* max hash chain length */
PreflateHashIterator chainIt = hash.iterateFromPos(curPos - targetReference.dist, curPos, curMaxDist);
if (!chainIt.curPos || (hashHead == chainIt.curPos && chainIt.dist() > curMaxDistAlt)
|| (hashHead != chainIt.curPos && chainIt.dist() > curMaxDist)) {
return result;
}
unsigned endDepth = chainIt.depth();
unsigned maxChain = maxChainOrg - std::min(startDepth - endDepth, 0xffffu);/* max hash chain length */
unsigned bestLen = targetReference.len;
while (maxChain > 0) {
if (!chainIt.next()) {
break;
}
const unsigned char* match = input - chainIt.dist();
unsigned matchLength = prefixCompare(match, input, bestLen, maxLen);
if (matchLength > bestLen) {
result.nextLen = matchLength;
result.nextChainDepth = maxChainOrg - maxChain;
result.nextDist = chainIt.dist();
break;
}
--maxChain;
}
return result;
}
PreflateRematchInfo PreflatePredictorState::rematchInfo(
const unsigned hashHead,
const PreflateToken& targetReference) {
PreflateRematchInfo result;
result.firstMatchDepth = 0xffff;
result.requestedMatchDepth = 0xffff;
result.condensedHops = 0;
unsigned maxLen = std::min(availableInputSize(), (unsigned)PreflateConstants::MAX_MATCH);
if (maxLen < targetReference.len) {
return result;
}
unsigned maxDist = windowSize();
unsigned curPos = currentInputPos();
unsigned curMaxDist = std::min(curPos, maxDist);
PreflateHashIterator chainIt = hash.iterateFromNode(hashHead, curPos, curMaxDist);
if (!chainIt) {
return result;
}
const unsigned char* input = inputCursor();
unsigned maxChainOrg = 0xffff;/* max hash chain length */
unsigned maxChain = maxChainOrg;/* max hash chain length */
unsigned bestLen = targetReference.len;
do {
const unsigned char* match = input - chainIt.dist();
unsigned matchLength = prefixCompare(match, input, bestLen - 1, bestLen);
if (matchLength >= bestLen) {
result.firstMatchDepth = std::min((unsigned)result.firstMatchDepth, maxChainOrg - maxChain);
result.condensedHops++;
}
if (chainIt.dist() >= targetReference.dist) {
if (chainIt.dist() == targetReference.dist) {
result.requestedMatchDepth = maxChainOrg - maxChain;
}
return result;
}
chainIt.next();
} while (!!chainIt && maxChain-- > 1);
return result;
}
unsigned PreflatePredictorState::firstMatch(const unsigned len) {
unsigned maxLen = std::min(availableInputSize(), (unsigned)PreflateConstants::MAX_MATCH);
if (maxLen < std::max(len, (unsigned)PreflateConstants::MIN_MATCH)) {
return 0;
}
unsigned curPos = currentInputPos();
unsigned curMaxDist = std::min(curPos, windowSize());
unsigned hash = calculateHash();
PreflateHashIterator chainIt = iterateFromHead(hash, curPos, curMaxDist);
if (!chainIt) {
return 0;
}
const unsigned char* input = inputCursor();
do {
const unsigned char* match = input - chainIt.dist();
unsigned matchLength = prefixCompare(match, input, len - 1, len);
if (matchLength >= len) {
return chainIt.dist();
}
} while (chainIt.next());
return 0;
}
unsigned PreflatePredictorState::hopMatch(const PreflateToken& targetReference, const unsigned hops) {
if (hops == 0) {
return targetReference.dist;
}
unsigned curPos = currentInputPos();
unsigned errorDist = 0;
unsigned maxLen = std::min(availableInputSize(), (unsigned)PreflateConstants::MAX_MATCH);
if (maxLen < targetReference.len) {
return errorDist;
}
unsigned maxDist = windowSize();
unsigned curMaxDist = std::min(curPos, maxDist);
PreflateHashIterator chainIt = iterateFromDist(targetReference.dist, curPos, curMaxDist);
if (!chainIt) {
return 0;
}
const unsigned char* input = inputCursor();
unsigned bestLen = targetReference.len;
for (unsigned todo = hops; todo > 0; ) {
if (!chainIt.next()) {
break;
}
const unsigned char* match = input - chainIt.dist();
unsigned matchLength = prefixCompare(match, input - targetReference.dist, bestLen - 1, bestLen);
if (matchLength >= bestLen) {
if (--todo == 0) {
return chainIt.dist();
}
}
}
return errorDist;
}