1 /*
2  * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
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.
9  */
10 
11 #include "zstd_compress_internal.h"
12 #include "zstd_fast.h"
13 
14 
15 void ZSTD_fillHashTable(ZSTD_matchState_t* ms,
16                         void const* end, ZSTD_dictTableLoadMethod_e dtlm)
17 {
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;
26 
27     /* Always insert every fastHashFillStep position into the hash table.
28      * Insert the other positions if their hash entry is empty.
29      */
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 */
36         {   U32 p;
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;
41     }   }   }   }
42 }
43 
44 FORCE_INLINE_TEMPLATE
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)
49 {
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];
64     U32 offsetSaved = 0;
65 
66     const ZSTD_matchState_t* const dms = ms->dictMatchState;
67     const ZSTD_compressionParameters* const dictCParams =
68                                      dictMode == ZSTD_dictMatchState ?
69                                      &dms->cParams : NULL;
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) :
82                                      0;
83     const U32 dictAndPrefixLength  = (U32)(ip - prefixStart + dictEnd - dictStart);
84     const U32 dictHLog             = dictMode == ZSTD_dictMatchState ?
85                                      dictCParams->hashLog : hlog;
86 
87     assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
88 
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));
93 
94     /* init */
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;
100     }
101     if (dictMode == ZSTD_dictMatchState) {
102         /* dictMatchState repCode checks don't currently handle repCode == 0
103          * disabling. */
104         assert(offset_1 <= dictAndPrefixLength);
105         assert(offset_2 <= dictAndPrefixLength);
106     }
107 
108     /* Main Search Loop */
109     while (ip < ilimit) {   /* < instead of <=, because repcode check at (ip+1) */
110         size_t mLength;
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) :
119                                base + repIndex;
120         hashTable[h] = current;   /* update hash table */
121 
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;
127             ip++;
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;
132             ip++;
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;
143                     continue;
144                 } else {
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++;
151                     } /* catch up */
152                     offset_2 = offset_1;
153                     offset_1 = offset;
154                     ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
155                 }
156             } else {
157                 assert(stepSize >= 1);
158                 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
159                 continue;
160             }
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;
165             continue;
166         } else {
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 */
172             offset_2 = offset_1;
173             offset_1 = offset;
174             ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
175         }
176 
177         /* match found */
178         ip += mLength;
179         anchor = ip;
180 
181         if (ip <= ilimit) {
182             /* Fill Table */
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);
186 
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 :
194                             base + 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;
202                         ip += repLength2;
203                         anchor = ip;
204                         continue;
205                     }
206                     break;
207                 }
208             }
209 
210             if (dictMode == ZSTD_noDict) {
211                 while ( (ip <= ilimit)
212                      && ( (offset_2>0)
213                         & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
214                     /* store sequence */
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);
219                     ip += rLength;
220                     anchor = ip;
221                     continue;   /* faster when present ... (?) */
222     }   }   }   }
223 
224     /* save reps for next block */
225     rep[0] = offset_1 ? offset_1 : offsetSaved;
226     rep[1] = offset_2 ? offset_2 : offsetSaved;
227 
228     /* Return the last literals size */
229     return iend - anchor;
230 }
231 
232 
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)
236 {
237     ZSTD_compressionParameters const* cParams = &ms->cParams;
238     U32 const mls = cParams->minMatch;
239     assert(ms->dictMatchState == NULL);
240     switch(mls)
241     {
242     default: /* includes case 3 */
243     case 4 :
244         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
245     case 5 :
246         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
247     case 6 :
248         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
249     case 7 :
250         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
251     }
252 }
253 
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)
257 {
258     ZSTD_compressionParameters const* cParams = &ms->cParams;
259     U32 const mls = cParams->minMatch;
260     assert(ms->dictMatchState != NULL);
261     switch(mls)
262     {
263     default: /* includes case 3 */
264     case 4 :
265         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
266     case 5 :
267         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
268     case 6 :
269         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
270     case 7 :
271         return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
272     }
273 }
274 
275 
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)
279 {
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];
298 
299     /* Search Loop */
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;
309         size_t mLength;
310         hashTable[h] = current;   /* update hash table */
311         assert(offset_1 <= current +1);   /* check repIndex */
312 
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;
317             ip++;
318             ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
319         } else {
320             if ( (matchIndex < dictStartIndex) ||
321                  (MEM_read32(match) != MEM_read32(ip)) ) {
322                 assert(stepSize >= 1);
323                 ip += ((ip-anchor) >> kSearchStrength) + stepSize;
324                 continue;
325             }
326             {   const BYTE* matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
327                 const BYTE* lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
328                 U32 offset;
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;
332                 offset_2 = offset_1;
333                 offset_1 = offset;
334                 ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
335         }   }
336 
337         /* found a match : store it */
338         ip += mLength;
339         anchor = ip;
340 
341         if (ip <= ilimit) {
342             /* Fill Table */
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;
357                     ip += repLength2;
358                     anchor = ip;
359                     continue;
360                 }
361                 break;
362     }   }   }
363 
364     /* save reps for next block */
365     rep[0] = offset_1;
366     rep[1] = offset_2;
367 
368     /* Return the last literals size */
369     return iend - anchor;
370 }
371 
372 
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)
376 {
377     ZSTD_compressionParameters const* cParams = &ms->cParams;
378     U32 const mls = cParams->minMatch;
379     switch(mls)
380     {
381     default: /* includes case 3 */
382     case 4 :
383         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
384     case 5 :
385         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
386     case 6 :
387         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
388     case 7 :
389         return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
390     }
391 }
392