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_double_fast.h"
13 
14 
ZSTD_fillDoubleHashTable(ZSTD_matchState_t * ms,void const * end,ZSTD_dictTableLoadMethod_e dtlm)15 void ZSTD_fillDoubleHashTable(ZSTD_matchState_t* ms,
16                               void const* end, ZSTD_dictTableLoadMethod_e dtlm)
17 {
18     const ZSTD_compressionParameters* const cParams = &ms->cParams;
19     U32* const hashLarge = ms->hashTable;
20     U32  const hBitsL = cParams->hashLog;
21     U32  const mls = cParams->minMatch;
22     U32* const hashSmall = ms->chainTable;
23     U32  const hBitsS = cParams->chainLog;
24     const BYTE* const base = ms->window.base;
25     const BYTE* ip = base + ms->nextToUpdate;
26     const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
27     const U32 fastHashFillStep = 3;
28 
29     /* Always insert every fastHashFillStep position into the hash tables.
30      * Insert the other positions into the large hash table if their entry
31      * is empty.
32      */
33     for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
34         U32 const current = (U32)(ip - base);
35         U32 i;
36         for (i = 0; i < fastHashFillStep; ++i) {
37             size_t const smHash = ZSTD_hashPtr(ip + i, hBitsS, mls);
38             size_t const lgHash = ZSTD_hashPtr(ip + i, hBitsL, 8);
39             if (i == 0)
40                 hashSmall[smHash] = current + i;
41             if (i == 0 || hashLarge[lgHash] == 0)
42                 hashLarge[lgHash] = current + i;
43             /* Only load extra positions for ZSTD_dtlm_full */
44             if (dtlm == ZSTD_dtlm_fast)
45                 break;
46     }   }
47 }
48 
49 
50 FORCE_INLINE_TEMPLATE
ZSTD_compressBlock_doubleFast_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize,U32 const mls,ZSTD_dictMode_e const dictMode)51 size_t ZSTD_compressBlock_doubleFast_generic(
52         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
53         void const* src, size_t srcSize,
54         U32 const mls /* template */, ZSTD_dictMode_e const dictMode)
55 {
56     ZSTD_compressionParameters const* cParams = &ms->cParams;
57     U32* const hashLong = ms->hashTable;
58     const U32 hBitsL = cParams->hashLog;
59     U32* const hashSmall = ms->chainTable;
60     const U32 hBitsS = cParams->chainLog;
61     const BYTE* const base = ms->window.base;
62     const BYTE* const istart = (const BYTE*)src;
63     const BYTE* ip = istart;
64     const BYTE* anchor = istart;
65     const U32 endIndex = (U32)((size_t)(istart - base) + srcSize);
66     const U32 lowestValid = ms->window.dictLimit;
67     const U32 maxDistance = 1U << cParams->windowLog;
68     /* presumes that, if there is a dictionary, it must be using Attach mode */
69     const U32 prefixLowestIndex = (endIndex - lowestValid > maxDistance) ? endIndex - maxDistance : lowestValid;
70     const BYTE* const prefixLowest = base + prefixLowestIndex;
71     const BYTE* const iend = istart + srcSize;
72     const BYTE* const ilimit = iend - HASH_READ_SIZE;
73     U32 offset_1=rep[0], offset_2=rep[1];
74     U32 offsetSaved = 0;
75 
76     const ZSTD_matchState_t* const dms = ms->dictMatchState;
77     const ZSTD_compressionParameters* const dictCParams =
78                                      dictMode == ZSTD_dictMatchState ?
79                                      &dms->cParams : NULL;
80     const U32* const dictHashLong  = dictMode == ZSTD_dictMatchState ?
81                                      dms->hashTable : NULL;
82     const U32* const dictHashSmall = dictMode == ZSTD_dictMatchState ?
83                                      dms->chainTable : NULL;
84     const U32 dictStartIndex       = dictMode == ZSTD_dictMatchState ?
85                                      dms->window.dictLimit : 0;
86     const BYTE* const dictBase     = dictMode == ZSTD_dictMatchState ?
87                                      dms->window.base : NULL;
88     const BYTE* const dictStart    = dictMode == ZSTD_dictMatchState ?
89                                      dictBase + dictStartIndex : NULL;
90     const BYTE* const dictEnd      = dictMode == ZSTD_dictMatchState ?
91                                      dms->window.nextSrc : NULL;
92     const U32 dictIndexDelta       = dictMode == ZSTD_dictMatchState ?
93                                      prefixLowestIndex - (U32)(dictEnd - dictBase) :
94                                      0;
95     const U32 dictHBitsL           = dictMode == ZSTD_dictMatchState ?
96                                      dictCParams->hashLog : hBitsL;
97     const U32 dictHBitsS           = dictMode == ZSTD_dictMatchState ?
98                                      dictCParams->chainLog : hBitsS;
99     const U32 dictAndPrefixLength  = (U32)(ip - prefixLowest + dictEnd - dictStart);
100 
101     DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_generic");
102 
103     assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
104 
105     /* if a dictionary is attached, it must be within window range */
106     if (dictMode == ZSTD_dictMatchState) {
107         assert(lowestValid + maxDistance >= endIndex);
108     }
109 
110     /* init */
111     ip += (dictAndPrefixLength == 0);
112     if (dictMode == ZSTD_noDict) {
113         U32 const maxRep = (U32)(ip - prefixLowest);
114         if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
115         if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
116     }
117     if (dictMode == ZSTD_dictMatchState) {
118         /* dictMatchState repCode checks don't currently handle repCode == 0
119          * disabling. */
120         assert(offset_1 <= dictAndPrefixLength);
121         assert(offset_2 <= dictAndPrefixLength);
122     }
123 
124     /* Main Search Loop */
125     while (ip < ilimit) {   /* < instead of <=, because repcode check at (ip+1) */
126         size_t mLength;
127         U32 offset;
128         size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8);
129         size_t const h = ZSTD_hashPtr(ip, hBitsS, mls);
130         size_t const dictHL = ZSTD_hashPtr(ip, dictHBitsL, 8);
131         size_t const dictHS = ZSTD_hashPtr(ip, dictHBitsS, mls);
132         U32 const current = (U32)(ip-base);
133         U32 const matchIndexL = hashLong[h2];
134         U32 matchIndexS = hashSmall[h];
135         const BYTE* matchLong = base + matchIndexL;
136         const BYTE* match = base + matchIndexS;
137         const U32 repIndex = current + 1 - offset_1;
138         const BYTE* repMatch = (dictMode == ZSTD_dictMatchState
139                             && repIndex < prefixLowestIndex) ?
140                                dictBase + (repIndex - dictIndexDelta) :
141                                base + repIndex;
142         hashLong[h2] = hashSmall[h] = current;   /* update hash tables */
143 
144         /* check dictMatchState repcode */
145         if (dictMode == ZSTD_dictMatchState
146             && ((U32)((prefixLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
147             && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
148             const BYTE* repMatchEnd = repIndex < prefixLowestIndex ? dictEnd : iend;
149             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixLowest) + 4;
150             ip++;
151             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
152             goto _match_stored;
153         }
154 
155         /* check noDict repcode */
156         if ( dictMode == ZSTD_noDict
157           && ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)))) {
158             mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
159             ip++;
160             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
161             goto _match_stored;
162         }
163 
164         if (matchIndexL > prefixLowestIndex) {
165             /* check prefix long match */
166             if (MEM_read64(matchLong) == MEM_read64(ip)) {
167                 mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8;
168                 offset = (U32)(ip-matchLong);
169                 while (((ip>anchor) & (matchLong>prefixLowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */
170                 goto _match_found;
171             }
172         } else if (dictMode == ZSTD_dictMatchState) {
173             /* check dictMatchState long match */
174             U32 const dictMatchIndexL = dictHashLong[dictHL];
175             const BYTE* dictMatchL = dictBase + dictMatchIndexL;
176             assert(dictMatchL < dictEnd);
177 
178             if (dictMatchL > dictStart && MEM_read64(dictMatchL) == MEM_read64(ip)) {
179                 mLength = ZSTD_count_2segments(ip+8, dictMatchL+8, iend, dictEnd, prefixLowest) + 8;
180                 offset = (U32)(current - dictMatchIndexL - dictIndexDelta);
181                 while (((ip>anchor) & (dictMatchL>dictStart)) && (ip[-1] == dictMatchL[-1])) { ip--; dictMatchL--; mLength++; } /* catch up */
182                 goto _match_found;
183         }   }
184 
185         if (matchIndexS > prefixLowestIndex) {
186             /* check prefix short match */
187             if (MEM_read32(match) == MEM_read32(ip)) {
188                 goto _search_next_long;
189             }
190         } else if (dictMode == ZSTD_dictMatchState) {
191             /* check dictMatchState short match */
192             U32 const dictMatchIndexS = dictHashSmall[dictHS];
193             match = dictBase + dictMatchIndexS;
194             matchIndexS = dictMatchIndexS + dictIndexDelta;
195 
196             if (match > dictStart && MEM_read32(match) == MEM_read32(ip)) {
197                 goto _search_next_long;
198         }   }
199 
200         ip += ((ip-anchor) >> kSearchStrength) + 1;
201         continue;
202 
203 _search_next_long:
204 
205         {   size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
206             size_t const dictHLNext = ZSTD_hashPtr(ip+1, dictHBitsL, 8);
207             U32 const matchIndexL3 = hashLong[hl3];
208             const BYTE* matchL3 = base + matchIndexL3;
209             hashLong[hl3] = current + 1;
210 
211             /* check prefix long +1 match */
212             if (matchIndexL3 > prefixLowestIndex) {
213                 if (MEM_read64(matchL3) == MEM_read64(ip+1)) {
214                     mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8;
215                     ip++;
216                     offset = (U32)(ip-matchL3);
217                     while (((ip>anchor) & (matchL3>prefixLowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */
218                     goto _match_found;
219                 }
220             } else if (dictMode == ZSTD_dictMatchState) {
221                 /* check dict long +1 match */
222                 U32 const dictMatchIndexL3 = dictHashLong[dictHLNext];
223                 const BYTE* dictMatchL3 = dictBase + dictMatchIndexL3;
224                 assert(dictMatchL3 < dictEnd);
225                 if (dictMatchL3 > dictStart && MEM_read64(dictMatchL3) == MEM_read64(ip+1)) {
226                     mLength = ZSTD_count_2segments(ip+1+8, dictMatchL3+8, iend, dictEnd, prefixLowest) + 8;
227                     ip++;
228                     offset = (U32)(current + 1 - dictMatchIndexL3 - dictIndexDelta);
229                     while (((ip>anchor) & (dictMatchL3>dictStart)) && (ip[-1] == dictMatchL3[-1])) { ip--; dictMatchL3--; mLength++; } /* catch up */
230                     goto _match_found;
231         }   }   }
232 
233         /* if no long +1 match, explore the short match we found */
234         if (dictMode == ZSTD_dictMatchState && matchIndexS < prefixLowestIndex) {
235             mLength = ZSTD_count_2segments(ip+4, match+4, iend, dictEnd, prefixLowest) + 4;
236             offset = (U32)(current - matchIndexS);
237             while (((ip>anchor) & (match>dictStart)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
238         } else {
239             mLength = ZSTD_count(ip+4, match+4, iend) + 4;
240             offset = (U32)(ip - match);
241             while (((ip>anchor) & (match>prefixLowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
242         }
243 
244         /* fall-through */
245 
246 _match_found:
247         offset_2 = offset_1;
248         offset_1 = offset;
249 
250         ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
251 
252 _match_stored:
253         /* match found */
254         ip += mLength;
255         anchor = ip;
256 
257         if (ip <= ilimit) {
258             /* Complementary insertion */
259             /* done after iLimit test, as candidates could be > iend-8 */
260             {   U32 const indexToInsert = current+2;
261                 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
262                 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
263                 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
264                 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
265             }
266 
267             /* check immediate repcode */
268             if (dictMode == ZSTD_dictMatchState) {
269                 while (ip <= ilimit) {
270                     U32 const current2 = (U32)(ip-base);
271                     U32 const repIndex2 = current2 - offset_2;
272                     const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
273                         && repIndex2 < prefixLowestIndex ?
274                             dictBase - dictIndexDelta + repIndex2 :
275                             base + repIndex2;
276                     if ( ((U32)((prefixLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
277                        && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
278                         const BYTE* const repEnd2 = repIndex2 < prefixLowestIndex ? dictEnd : iend;
279                         size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixLowest) + 4;
280                         U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
281                         ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
282                         hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
283                         hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
284                         ip += repLength2;
285                         anchor = ip;
286                         continue;
287                     }
288                     break;
289             }   }
290 
291             if (dictMode == ZSTD_noDict) {
292                 while ( (ip <= ilimit)
293                      && ( (offset_2>0)
294                         & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
295                     /* store sequence */
296                     size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
297                     U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff;  /* swap offset_2 <=> offset_1 */
298                     hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base);
299                     hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base);
300                     ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, rLength-MINMATCH);
301                     ip += rLength;
302                     anchor = ip;
303                     continue;   /* faster when present ... (?) */
304         }   }   }
305     }   /* while (ip < ilimit) */
306 
307     /* save reps for next block */
308     rep[0] = offset_1 ? offset_1 : offsetSaved;
309     rep[1] = offset_2 ? offset_2 : offsetSaved;
310 
311     /* Return the last literals size */
312     return (size_t)(iend - anchor);
313 }
314 
315 
ZSTD_compressBlock_doubleFast(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)316 size_t ZSTD_compressBlock_doubleFast(
317         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
318         void const* src, size_t srcSize)
319 {
320     const U32 mls = ms->cParams.minMatch;
321     switch(mls)
322     {
323     default: /* includes case 3 */
324     case 4 :
325         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_noDict);
326     case 5 :
327         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_noDict);
328     case 6 :
329         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_noDict);
330     case 7 :
331         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_noDict);
332     }
333 }
334 
335 
ZSTD_compressBlock_doubleFast_dictMatchState(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)336 size_t ZSTD_compressBlock_doubleFast_dictMatchState(
337         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
338         void const* src, size_t srcSize)
339 {
340     const U32 mls = ms->cParams.minMatch;
341     switch(mls)
342     {
343     default: /* includes case 3 */
344     case 4 :
345         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 4, ZSTD_dictMatchState);
346     case 5 :
347         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 5, ZSTD_dictMatchState);
348     case 6 :
349         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 6, ZSTD_dictMatchState);
350     case 7 :
351         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, 7, ZSTD_dictMatchState);
352     }
353 }
354 
355 
ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize,U32 const mls)356 static size_t ZSTD_compressBlock_doubleFast_extDict_generic(
357         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
358         void const* src, size_t srcSize,
359         U32 const mls /* template */)
360 {
361     ZSTD_compressionParameters const* cParams = &ms->cParams;
362     U32* const hashLong = ms->hashTable;
363     U32  const hBitsL = cParams->hashLog;
364     U32* const hashSmall = ms->chainTable;
365     U32  const hBitsS = cParams->chainLog;
366     const BYTE* const istart = (const BYTE*)src;
367     const BYTE* ip = istart;
368     const BYTE* anchor = istart;
369     const BYTE* const iend = istart + srcSize;
370     const BYTE* const ilimit = iend - 8;
371     const BYTE* const base = ms->window.base;
372     const U32   endIndex = (U32)((size_t)(istart - base) + srcSize);
373     const U32   lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog);
374     const U32   dictStartIndex = lowLimit;
375     const U32   dictLimit = ms->window.dictLimit;
376     const U32   prefixStartIndex = (dictLimit > lowLimit) ? dictLimit : lowLimit;
377     const BYTE* const prefixStart = base + prefixStartIndex;
378     const BYTE* const dictBase = ms->window.dictBase;
379     const BYTE* const dictStart = dictBase + dictStartIndex;
380     const BYTE* const dictEnd = dictBase + prefixStartIndex;
381     U32 offset_1=rep[0], offset_2=rep[1];
382 
383     DEBUGLOG(5, "ZSTD_compressBlock_doubleFast_extDict_generic (srcSize=%zu)", srcSize);
384 
385     /* if extDict is invalidated due to maxDistance, switch to "regular" variant */
386     if (prefixStartIndex == dictStartIndex)
387         return ZSTD_compressBlock_doubleFast_generic(ms, seqStore, rep, src, srcSize, mls, ZSTD_noDict);
388 
389     /* Search Loop */
390     while (ip < ilimit) {  /* < instead of <=, because (ip+1) */
391         const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls);
392         const U32 matchIndex = hashSmall[hSmall];
393         const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base;
394         const BYTE* match = matchBase + matchIndex;
395 
396         const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8);
397         const U32 matchLongIndex = hashLong[hLong];
398         const BYTE* const matchLongBase = matchLongIndex < prefixStartIndex ? dictBase : base;
399         const BYTE* matchLong = matchLongBase + matchLongIndex;
400 
401         const U32 current = (U32)(ip-base);
402         const U32 repIndex = current + 1 - offset_1;   /* offset_1 expected <= current +1 */
403         const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base;
404         const BYTE* const repMatch = repBase + repIndex;
405         size_t mLength;
406         hashSmall[hSmall] = hashLong[hLong] = current;   /* update hash table */
407 
408         if ((((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex doesn't overlap dict + prefix */
409             & (repIndex > dictStartIndex))
410           && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
411             const BYTE* repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend;
412             mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4;
413             ip++;
414             ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH);
415         } else {
416             if ((matchLongIndex > dictStartIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) {
417                 const BYTE* const matchEnd = matchLongIndex < prefixStartIndex ? dictEnd : iend;
418                 const BYTE* const lowMatchPtr = matchLongIndex < prefixStartIndex ? dictStart : prefixStart;
419                 U32 offset;
420                 mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, prefixStart) + 8;
421                 offset = current - matchLongIndex;
422                 while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; }   /* catch up */
423                 offset_2 = offset_1;
424                 offset_1 = offset;
425                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
426 
427             } else if ((matchIndex > dictStartIndex) && (MEM_read32(match) == MEM_read32(ip))) {
428                 size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8);
429                 U32 const matchIndex3 = hashLong[h3];
430                 const BYTE* const match3Base = matchIndex3 < prefixStartIndex ? dictBase : base;
431                 const BYTE* match3 = match3Base + matchIndex3;
432                 U32 offset;
433                 hashLong[h3] = current + 1;
434                 if ( (matchIndex3 > dictStartIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) {
435                     const BYTE* const matchEnd = matchIndex3 < prefixStartIndex ? dictEnd : iend;
436                     const BYTE* const lowMatchPtr = matchIndex3 < prefixStartIndex ? dictStart : prefixStart;
437                     mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, prefixStart) + 8;
438                     ip++;
439                     offset = current+1 - matchIndex3;
440                     while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */
441                 } else {
442                     const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend;
443                     const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart;
444                     mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4;
445                     offset = current - matchIndex;
446                     while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; }   /* catch up */
447                 }
448                 offset_2 = offset_1;
449                 offset_1 = offset;
450                 ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
451 
452             } else {
453                 ip += ((ip-anchor) >> kSearchStrength) + 1;
454                 continue;
455         }   }
456 
457         /* move to next sequence start */
458         ip += mLength;
459         anchor = ip;
460 
461         if (ip <= ilimit) {
462             /* Complementary insertion */
463             /* done after iLimit test, as candidates could be > iend-8 */
464             {   U32 const indexToInsert = current+2;
465                 hashLong[ZSTD_hashPtr(base+indexToInsert, hBitsL, 8)] = indexToInsert;
466                 hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base);
467                 hashSmall[ZSTD_hashPtr(base+indexToInsert, hBitsS, mls)] = indexToInsert;
468                 hashSmall[ZSTD_hashPtr(ip-1, hBitsS, mls)] = (U32)(ip-1-base);
469             }
470 
471             /* check immediate repcode */
472             while (ip <= ilimit) {
473                 U32 const current2 = (U32)(ip-base);
474                 U32 const repIndex2 = current2 - offset_2;
475                 const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2;
476                 if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3)   /* intentional overflow : ensure repIndex2 doesn't overlap dict + prefix */
477                     & (repIndex2 > dictStartIndex))
478                   && (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
479                     const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend;
480                     size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4;
481                     U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset;   /* swap offset_2 <=> offset_1 */
482                     ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH);
483                     hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2;
484                     hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2;
485                     ip += repLength2;
486                     anchor = ip;
487                     continue;
488                 }
489                 break;
490     }   }   }
491 
492     /* save reps for next block */
493     rep[0] = offset_1;
494     rep[1] = offset_2;
495 
496     /* Return the last literals size */
497     return (size_t)(iend - anchor);
498 }
499 
500 
ZSTD_compressBlock_doubleFast_extDict(ZSTD_matchState_t * ms,seqStore_t * seqStore,U32 rep[ZSTD_REP_NUM],void const * src,size_t srcSize)501 size_t ZSTD_compressBlock_doubleFast_extDict(
502         ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
503         void const* src, size_t srcSize)
504 {
505     U32 const mls = ms->cParams.minMatch;
506     switch(mls)
507     {
508     default: /* includes case 3 */
509     case 4 :
510         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 4);
511     case 5 :
512         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 5);
513     case 6 :
514         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 6);
515     case 7 :
516         return ZSTD_compressBlock_doubleFast_extDict_generic(ms, seqStore, rep, src, srcSize, 7);
517     }
518 }
519