2 * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
5 * This source code is licensed under both the BSD-style license (found in the
6 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7 * in the COPYING file in the root directory of this source tree).
8 * You may select, at your option, one of the above-listed licenses.
11 #include "zstd_compress_internal.h"
12 #include "zstd_fast.h"
15 void ZSTD_fillHashTable(ZSTD_matchState_t* ms,
16 void const* end, ZSTD_dictTableLoadMethod_e dtlm)
18 const ZSTD_compressionParameters* const cParams = &ms->cParams;
19 U32* const hashTable = ms->hashTable;
20 U32 const hBits = cParams->hashLog;
21 U32 const mls = cParams->minMatch;
22 const BYTE* const base = ms->window.base;
23 const BYTE* ip = base + ms->nextToUpdate;
24 const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
25 const U32 fastHashFillStep = 3;
27 /* Always insert every fastHashFillStep position into the hash table.
28 * Insert the other positions if their hash entry is empty.
30 for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) {
31 U32 const current = (U32)(ip - base);
32 size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls);
33 hashTable[hash0] = current;
34 if (dtlm == ZSTD_dtlm_fast) continue;
35 /* Only load extra positions for ZSTD_dtlm_full */
37 for (p = 1; p < fastHashFillStep; ++p) {
38 size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls);
39 if (hashTable[hash] == 0) { /* not yet filled */
40 hashTable[hash] = current + p;
45 size_t ZSTD_compressBlock_fast_generic(
46 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
47 void const* src, size_t srcSize,
48 U32 const mls, ZSTD_dictMode_e const dictMode)
50 const ZSTD_compressionParameters* const cParams = &ms->cParams;
51 U32* const hashTable = ms->hashTable;
52 U32 const hlog = cParams->hashLog;
53 /* support stepSize of 0 */
54 U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
55 const BYTE* const base = ms->window.base;
56 const BYTE* const istart = (const BYTE*)src;
57 const BYTE* ip = istart;
58 const BYTE* anchor = istart;
59 const U32 prefixStartIndex = ms->window.dictLimit;
60 const BYTE* const prefixStart = base + prefixStartIndex;
61 const BYTE* const iend = istart + srcSize;
62 const BYTE* const ilimit = iend - HASH_READ_SIZE;
63 U32 offset_1=rep[0], offset_2=rep[1];
66 const ZSTD_matchState_t* const dms = ms->dictMatchState;
67 const ZSTD_compressionParameters* const dictCParams =
68 dictMode == ZSTD_dictMatchState ?
70 const U32* const dictHashTable = dictMode == ZSTD_dictMatchState ?
71 dms->hashTable : NULL;
72 const U32 dictStartIndex = dictMode == ZSTD_dictMatchState ?
73 dms->window.dictLimit : 0;
74 const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ?
75 dms->window.base : NULL;
76 const BYTE* const dictStart = dictMode == ZSTD_dictMatchState ?
77 dictBase + dictStartIndex : NULL;
78 const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ?
79 dms->window.nextSrc : NULL;
80 const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ?
81 prefixStartIndex - (U32)(dictEnd - dictBase) :
83 const U32 dictAndPrefixLength = (U32)(ip - prefixStart + dictEnd - dictStart);
84 const U32 dictHLog = dictMode == ZSTD_dictMatchState ?
85 dictCParams->hashLog : hlog;
87 assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
89 /* otherwise, we would get index underflow when translating a dict index
90 * into a local index */
91 assert(dictMode != ZSTD_dictMatchState
92 || prefixStartIndex >= (U32)(dictEnd - dictBase));
95 ip += (dictAndPrefixLength == 0);
96 if (dictMode == ZSTD_noDict) {
97 U32 const maxRep = (U32)(ip - prefixStart);
98 if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
99 if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
101 if (dictMode == ZSTD_dictMatchState) {
102 /* dictMatchState repCode checks don't currently handle repCode == 0
104 assert(offset_1 <= dictAndPrefixLength);
105 assert(offset_2 <= dictAndPrefixLength);
108 /* Main Search Loop */
109 while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
111 size_t const h = ZSTD_hashPtr(ip, hlog, mls);
112 U32 const current = (U32)(ip-base);
113 U32 const matchIndex = hashTable[h];
114 const BYTE* match = base + matchIndex;
115 const U32 repIndex = current + 1 - offset_1;
116 const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
117 && repIndex < prefixStartIndex) ?
118 dictBase + (repIndex - dictIndexDelta) :
120 hashTable[h] = current; /* update hash table */
122 if ( (dictMode == ZSTD_dictMatchState)
123 && ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */
124 && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
125 const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
126 mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
128 ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
129 } else if ( dictMode == ZSTD_noDict
130 && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
131 mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
133 ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
134 } else if ( (matchIndex <= prefixStartIndex) ) {
135 if (dictMode == ZSTD_dictMatchState) {
136 size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls);
137 U32 const dictMatchIndex = dictHashTable[dictHash];
138 const BYTE* dictMatch = dictBase + dictMatchIndex;
139 if (dictMatchIndex <= dictStartIndex ||
140 MEM_read32(dictMatch) != MEM_read32(ip)) {
141 assert(stepSize >= 1);
142 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
145 /* found a dict match */
146 U32 const offset = (U32)(current-dictMatchIndex-dictIndexDelta);
147 mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4;
148 while (((ip>anchor) & (dictMatch>dictStart))
149 && (ip[-1] == dictMatch[-1])) {
150 ip--; dictMatch--; mLength++;
154 ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
157 assert(stepSize >= 1);
158 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
161 } else if (MEM_read32(match) != MEM_read32(ip)) {
162 /* it's not a match, and we're not going to check the dictionary */
163 assert(stepSize >= 1);
164 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
167 /* found a regular match */
168 U32 const offset = (U32)(ip-match);
169 mLength = ZSTD_count(ip+4, match+4, iend) + 4;
170 while (((ip>anchor) & (match>prefixStart))
171 && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
174 ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
183 assert(base+current+2 > istart); /* check base overflow */
184 hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2; /* here because current+2 could be > iend-8 */
185 hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
187 /* check immediate repcode */
188 if (dictMode == ZSTD_dictMatchState) {
189 while (ip <= ilimit) {
190 U32 const current2 = (U32)(ip-base);
191 U32 const repIndex2 = current2 - offset_2;
192 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ?
193 dictBase - dictIndexDelta + repIndex2 :
195 if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
196 && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
197 const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
198 size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
199 U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
200 ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
201 hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
210 if (dictMode == ZSTD_noDict) {
211 while ( (ip <= ilimit)
213 & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
215 size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
216 U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
217 hashTable[ZSTD_hashPtr(ip, hlog, mls)] = (U32)(ip-base);
218 ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
221 continue; /* faster when present ... (?) */
224 /* save reps for next block */
225 rep[0] = offset_1 ? offset_1 : offsetSaved;
226 rep[1] = offset_2 ? offset_2 : offsetSaved;
228 /* Return the last literals size */
229 return iend - anchor;
233 size_t ZSTD_compressBlock_fast(
234 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
235 void const* src, size_t srcSize)
237 ZSTD_compressionParameters const* cParams = &ms->cParams;
238 U32 const mls = cParams->minMatch;
239 assert(ms->dictMatchState == NULL);
242 default: /* includes case 3 */
244 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
246 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
248 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
250 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
254 size_t ZSTD_compressBlock_fast_dictMatchState(
255 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
256 void const* src, size_t srcSize)
258 ZSTD_compressionParameters const* cParams = &ms->cParams;
259 U32 const mls = cParams->minMatch;
260 assert(ms->dictMatchState != NULL);
263 default: /* includes case 3 */
265 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
267 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
269 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
271 return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
276 static size_t ZSTD_compressBlock_fast_extDict_generic(
277 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
278 void const* src, size_t srcSize, U32 const mls)
280 const ZSTD_compressionParameters* const cParams = &ms->cParams;
281 U32* const hashTable = ms->hashTable;
282 U32 const hlog = cParams->hashLog;
283 /* support stepSize of 0 */
284 U32 const stepSize = cParams->targetLength + !(cParams->targetLength);
285 const BYTE* const base = ms->window.base;
286 const BYTE* const dictBase = ms->window.dictBase;
287 const BYTE* const istart = (const BYTE*)src;
288 const BYTE* ip = istart;
289 const BYTE* anchor = istart;
290 const U32 dictStartIndex = ms->window.lowLimit;
291 const BYTE* const dictStart = dictBase + dictStartIndex;
292 const U32 prefixStartIndex = ms->window.dictLimit;
293 const BYTE* const prefixStart = base + prefixStartIndex;
294 const BYTE* const dictEnd = dictBase + prefixStartIndex;
295 const BYTE* const iend = istart + srcSize;
296 const BYTE* const ilimit = iend - 8;
297 U32 offset_1=rep[0], offset_2=rep[1];
300 while (ip < ilimit) { /* < instead of <=, because (ip+1) */
301 const size_t h = ZSTD_hashPtr(ip, hlog, mls);
302 const U32 matchIndex = hashTable[h];
303 const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
304 const BYTE* match = matchBase + matchIndex;
305 const U32 current = (U32)(ip-base);
306 const U32 repIndex = current + 1 - offset_1;
307 const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
308 const BYTE* const repMatch = repBase + repIndex;
310 hashTable[h] = current; /* update hash table */
311 assert(offset_1 <= current +1); /* check repIndex */
313 if ( (((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > dictStartIndex))
314 && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
315 const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
316 mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
318 ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
320 if ( (matchIndex < dictStartIndex) ||
321 (MEM_read32(match) != MEM_read32(ip)) ) {
322 assert(stepSize >= 1);
323 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
326 { const BYTE* matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
327 const BYTE* lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
329 mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
330 while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
331 offset = current - matchIndex;
334 ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
337 /* found a match : store it */
343 hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2;
344 hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
345 /* check immediate repcode */
346 while (ip <= ilimit) {
347 U32 const current2 = (U32)(ip-base);
348 U32 const repIndex2 = current2 - offset_2;
349 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
350 if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (repIndex2 > dictStartIndex)) /* intentional overflow */
351 && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
352 const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
353 size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
354 U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
355 ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
356 hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
364 /* save reps for next block */
368 /* Return the last literals size */
369 return iend - anchor;
373 size_t ZSTD_compressBlock_fast_extDict(
374 ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
375 void const* src, size_t srcSize)
377 ZSTD_compressionParameters const* cParams = &ms->cParams;
378 U32 const mls = cParams->minMatch;
381 default: /* includes case 3 */
383 return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
385 return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
387 return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
389 return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);