10c16b537SWarner Losh /* 20c16b537SWarner Losh * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh 11*9cbefe25SConrad Meyer #include "zstd_compress_internal.h" /* ZSTD_hashPtr, ZSTD_count, ZSTD_storeSeq */ 120c16b537SWarner Losh #include "zstd_fast.h" 130c16b537SWarner Losh 140c16b537SWarner Losh 1519fcbaf1SConrad Meyer void ZSTD_fillHashTable(ZSTD_matchState_t* ms, 164d3f1eafSConrad Meyer const void* const end, 174d3f1eafSConrad Meyer ZSTD_dictTableLoadMethod_e dtlm) 180c16b537SWarner Losh { 190f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams; 2019fcbaf1SConrad Meyer U32* const hashTable = ms->hashTable; 2119fcbaf1SConrad Meyer U32 const hBits = cParams->hashLog; 22a0483764SConrad Meyer U32 const mls = cParams->minMatch; 2319fcbaf1SConrad Meyer const BYTE* const base = ms->window.base; 2419fcbaf1SConrad Meyer const BYTE* ip = base + ms->nextToUpdate; 250c16b537SWarner Losh const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; 2619fcbaf1SConrad Meyer const U32 fastHashFillStep = 3; 270c16b537SWarner Losh 2819fcbaf1SConrad Meyer /* Always insert every fastHashFillStep position into the hash table. 2919fcbaf1SConrad Meyer * Insert the other positions if their hash entry is empty. 3019fcbaf1SConrad Meyer */ 31a0483764SConrad Meyer for ( ; ip + fastHashFillStep < iend + 2; ip += fastHashFillStep) { 3219fcbaf1SConrad Meyer U32 const current = (U32)(ip - base); 33a0483764SConrad Meyer size_t const hash0 = ZSTD_hashPtr(ip, hBits, mls); 34a0483764SConrad Meyer hashTable[hash0] = current; 35a0483764SConrad Meyer if (dtlm == ZSTD_dtlm_fast) continue; 360f743729SConrad Meyer /* Only load extra positions for ZSTD_dtlm_full */ 37a0483764SConrad Meyer { U32 p; 38a0483764SConrad Meyer for (p = 1; p < fastHashFillStep; ++p) { 39a0483764SConrad Meyer size_t const hash = ZSTD_hashPtr(ip + p, hBits, mls); 40a0483764SConrad Meyer if (hashTable[hash] == 0) { /* not yet filled */ 41a0483764SConrad Meyer hashTable[hash] = current + p; 42a0483764SConrad Meyer } } } } 4319fcbaf1SConrad Meyer } 440c16b537SWarner Losh 454d3f1eafSConrad Meyer 46*9cbefe25SConrad Meyer FORCE_INLINE_TEMPLATE size_t 47*9cbefe25SConrad Meyer ZSTD_compressBlock_fast_generic( 4819fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 4919fcbaf1SConrad Meyer void const* src, size_t srcSize, 502b9c00cbSConrad Meyer U32 const mls) 512b9c00cbSConrad Meyer { 522b9c00cbSConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams; 532b9c00cbSConrad Meyer U32* const hashTable = ms->hashTable; 542b9c00cbSConrad Meyer U32 const hlog = cParams->hashLog; 552b9c00cbSConrad Meyer /* support stepSize of 0 */ 562b9c00cbSConrad Meyer size_t const stepSize = cParams->targetLength + !(cParams->targetLength) + 1; 572b9c00cbSConrad Meyer const BYTE* const base = ms->window.base; 582b9c00cbSConrad Meyer const BYTE* const istart = (const BYTE*)src; 592b9c00cbSConrad Meyer /* We check ip0 (ip + 0) and ip1 (ip + 1) each loop */ 602b9c00cbSConrad Meyer const BYTE* ip0 = istart; 612b9c00cbSConrad Meyer const BYTE* ip1; 622b9c00cbSConrad Meyer const BYTE* anchor = istart; 634d3f1eafSConrad Meyer const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); 644d3f1eafSConrad Meyer const U32 maxDistance = 1U << cParams->windowLog; 654d3f1eafSConrad Meyer const U32 validStartIndex = ms->window.dictLimit; 664d3f1eafSConrad Meyer const U32 prefixStartIndex = (endIndex - validStartIndex > maxDistance) ? endIndex - maxDistance : validStartIndex; 672b9c00cbSConrad Meyer const BYTE* const prefixStart = base + prefixStartIndex; 682b9c00cbSConrad Meyer const BYTE* const iend = istart + srcSize; 692b9c00cbSConrad Meyer const BYTE* const ilimit = iend - HASH_READ_SIZE; 702b9c00cbSConrad Meyer U32 offset_1=rep[0], offset_2=rep[1]; 712b9c00cbSConrad Meyer U32 offsetSaved = 0; 722b9c00cbSConrad Meyer 732b9c00cbSConrad Meyer /* init */ 74*9cbefe25SConrad Meyer DEBUGLOG(5, "ZSTD_compressBlock_fast_generic"); 752b9c00cbSConrad Meyer ip0 += (ip0 == prefixStart); 762b9c00cbSConrad Meyer ip1 = ip0 + 1; 77*9cbefe25SConrad Meyer { U32 const maxRep = (U32)(ip0 - prefixStart); 782b9c00cbSConrad Meyer if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; 792b9c00cbSConrad Meyer if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; 802b9c00cbSConrad Meyer } 812b9c00cbSConrad Meyer 822b9c00cbSConrad Meyer /* Main Search Loop */ 832b9c00cbSConrad Meyer while (ip1 < ilimit) { /* < instead of <=, because check at ip0+2 */ 842b9c00cbSConrad Meyer size_t mLength; 852b9c00cbSConrad Meyer BYTE const* ip2 = ip0 + 2; 862b9c00cbSConrad Meyer size_t const h0 = ZSTD_hashPtr(ip0, hlog, mls); 872b9c00cbSConrad Meyer U32 const val0 = MEM_read32(ip0); 882b9c00cbSConrad Meyer size_t const h1 = ZSTD_hashPtr(ip1, hlog, mls); 892b9c00cbSConrad Meyer U32 const val1 = MEM_read32(ip1); 902b9c00cbSConrad Meyer U32 const current0 = (U32)(ip0-base); 912b9c00cbSConrad Meyer U32 const current1 = (U32)(ip1-base); 922b9c00cbSConrad Meyer U32 const matchIndex0 = hashTable[h0]; 932b9c00cbSConrad Meyer U32 const matchIndex1 = hashTable[h1]; 942b9c00cbSConrad Meyer BYTE const* repMatch = ip2-offset_1; 952b9c00cbSConrad Meyer const BYTE* match0 = base + matchIndex0; 962b9c00cbSConrad Meyer const BYTE* match1 = base + matchIndex1; 972b9c00cbSConrad Meyer U32 offcode; 982b9c00cbSConrad Meyer hashTable[h0] = current0; /* update hash table */ 992b9c00cbSConrad Meyer hashTable[h1] = current1; /* update hash table */ 1002b9c00cbSConrad Meyer 1012b9c00cbSConrad Meyer assert(ip0 + 1 == ip1); 1022b9c00cbSConrad Meyer 1032b9c00cbSConrad Meyer if ((offset_1 > 0) & (MEM_read32(repMatch) == MEM_read32(ip2))) { 1042b9c00cbSConrad Meyer mLength = ip2[-1] == repMatch[-1] ? 1 : 0; 1052b9c00cbSConrad Meyer ip0 = ip2 - mLength; 1062b9c00cbSConrad Meyer match0 = repMatch - mLength; 1072b9c00cbSConrad Meyer offcode = 0; 1082b9c00cbSConrad Meyer goto _match; 1092b9c00cbSConrad Meyer } 1102b9c00cbSConrad Meyer if ((matchIndex0 > prefixStartIndex) && MEM_read32(match0) == val0) { 1112b9c00cbSConrad Meyer /* found a regular match */ 1122b9c00cbSConrad Meyer goto _offset; 1132b9c00cbSConrad Meyer } 1142b9c00cbSConrad Meyer if ((matchIndex1 > prefixStartIndex) && MEM_read32(match1) == val1) { 1152b9c00cbSConrad Meyer /* found a regular match after one literal */ 1162b9c00cbSConrad Meyer ip0 = ip1; 1172b9c00cbSConrad Meyer match0 = match1; 1182b9c00cbSConrad Meyer goto _offset; 1192b9c00cbSConrad Meyer } 120*9cbefe25SConrad Meyer { size_t const step = ((size_t)(ip0-anchor) >> (kSearchStrength - 1)) + stepSize; 1212b9c00cbSConrad Meyer assert(step >= 2); 1222b9c00cbSConrad Meyer ip0 += step; 1232b9c00cbSConrad Meyer ip1 += step; 1242b9c00cbSConrad Meyer continue; 1252b9c00cbSConrad Meyer } 1262b9c00cbSConrad Meyer _offset: /* Requires: ip0, match0 */ 1272b9c00cbSConrad Meyer /* Compute the offset code */ 1282b9c00cbSConrad Meyer offset_2 = offset_1; 1292b9c00cbSConrad Meyer offset_1 = (U32)(ip0-match0); 1302b9c00cbSConrad Meyer offcode = offset_1 + ZSTD_REP_MOVE; 1312b9c00cbSConrad Meyer mLength = 0; 1322b9c00cbSConrad Meyer /* Count the backwards match length */ 1332b9c00cbSConrad Meyer while (((ip0>anchor) & (match0>prefixStart)) 1342b9c00cbSConrad Meyer && (ip0[-1] == match0[-1])) { ip0--; match0--; mLength++; } /* catch up */ 1352b9c00cbSConrad Meyer 1362b9c00cbSConrad Meyer _match: /* Requires: ip0, match0, offcode */ 1372b9c00cbSConrad Meyer /* Count the forward length */ 1382b9c00cbSConrad Meyer mLength += ZSTD_count(ip0+mLength+4, match0+mLength+4, iend) + 4; 139*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, (size_t)(ip0-anchor), anchor, iend, offcode, mLength-MINMATCH); 1402b9c00cbSConrad Meyer /* match found */ 1412b9c00cbSConrad Meyer ip0 += mLength; 1422b9c00cbSConrad Meyer anchor = ip0; 1432b9c00cbSConrad Meyer ip1 = ip0 + 1; 1442b9c00cbSConrad Meyer 1452b9c00cbSConrad Meyer if (ip0 <= ilimit) { 1462b9c00cbSConrad Meyer /* Fill Table */ 1472b9c00cbSConrad Meyer assert(base+current0+2 > istart); /* check base overflow */ 1482b9c00cbSConrad Meyer hashTable[ZSTD_hashPtr(base+current0+2, hlog, mls)] = current0+2; /* here because current+2 could be > iend-8 */ 1492b9c00cbSConrad Meyer hashTable[ZSTD_hashPtr(ip0-2, hlog, mls)] = (U32)(ip0-2-base); 1502b9c00cbSConrad Meyer 151*9cbefe25SConrad Meyer while ( ((ip0 <= ilimit) & (offset_2>0)) /* offset_2==0 means offset_2 is invalidated */ 152*9cbefe25SConrad Meyer && (MEM_read32(ip0) == MEM_read32(ip0 - offset_2)) ) { 1532b9c00cbSConrad Meyer /* store sequence */ 1542b9c00cbSConrad Meyer size_t const rLength = ZSTD_count(ip0+4, ip0+4-offset_2, iend) + 4; 155*9cbefe25SConrad Meyer { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */ 1562b9c00cbSConrad Meyer hashTable[ZSTD_hashPtr(ip0, hlog, mls)] = (U32)(ip0-base); 1572b9c00cbSConrad Meyer ip0 += rLength; 1582b9c00cbSConrad Meyer ip1 = ip0 + 1; 159*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, 0 /*litLen*/, anchor, iend, 0 /*offCode*/, rLength-MINMATCH); 1602b9c00cbSConrad Meyer anchor = ip0; 1612b9c00cbSConrad Meyer continue; /* faster when present (confirmed on gcc-8) ... (?) */ 1622b9c00cbSConrad Meyer } 1632b9c00cbSConrad Meyer } 1642b9c00cbSConrad Meyer } 1652b9c00cbSConrad Meyer 1662b9c00cbSConrad Meyer /* save reps for next block */ 1672b9c00cbSConrad Meyer rep[0] = offset_1 ? offset_1 : offsetSaved; 1682b9c00cbSConrad Meyer rep[1] = offset_2 ? offset_2 : offsetSaved; 1692b9c00cbSConrad Meyer 1702b9c00cbSConrad Meyer /* Return the last literals size */ 1714d3f1eafSConrad Meyer return (size_t)(iend - anchor); 1722b9c00cbSConrad Meyer } 1732b9c00cbSConrad Meyer 1742b9c00cbSConrad Meyer 1752b9c00cbSConrad Meyer size_t ZSTD_compressBlock_fast( 1762b9c00cbSConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 1772b9c00cbSConrad Meyer void const* src, size_t srcSize) 1782b9c00cbSConrad Meyer { 179*9cbefe25SConrad Meyer U32 const mls = ms->cParams.minMatch; 1802b9c00cbSConrad Meyer assert(ms->dictMatchState == NULL); 1812b9c00cbSConrad Meyer switch(mls) 1822b9c00cbSConrad Meyer { 1832b9c00cbSConrad Meyer default: /* includes case 3 */ 1842b9c00cbSConrad Meyer case 4 : 1852b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 4); 1862b9c00cbSConrad Meyer case 5 : 1872b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 5); 1882b9c00cbSConrad Meyer case 6 : 1892b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 6); 1902b9c00cbSConrad Meyer case 7 : 1912b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, 7); 1922b9c00cbSConrad Meyer } 1932b9c00cbSConrad Meyer } 1942b9c00cbSConrad Meyer 1952b9c00cbSConrad Meyer FORCE_INLINE_TEMPLATE 1962b9c00cbSConrad Meyer size_t ZSTD_compressBlock_fast_dictMatchState_generic( 1972b9c00cbSConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 1982b9c00cbSConrad Meyer void const* src, size_t srcSize, U32 const mls) 1990c16b537SWarner Losh { 2000f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams; 20119fcbaf1SConrad Meyer U32* const hashTable = ms->hashTable; 2020f743729SConrad Meyer U32 const hlog = cParams->hashLog; 2030f743729SConrad Meyer /* support stepSize of 0 */ 2040f743729SConrad Meyer U32 const stepSize = cParams->targetLength + !(cParams->targetLength); 20519fcbaf1SConrad Meyer const BYTE* const base = ms->window.base; 2060c16b537SWarner Losh const BYTE* const istart = (const BYTE*)src; 2070c16b537SWarner Losh const BYTE* ip = istart; 2080c16b537SWarner Losh const BYTE* anchor = istart; 2090f743729SConrad Meyer const U32 prefixStartIndex = ms->window.dictLimit; 2100f743729SConrad Meyer const BYTE* const prefixStart = base + prefixStartIndex; 2110c16b537SWarner Losh const BYTE* const iend = istart + srcSize; 2120c16b537SWarner Losh const BYTE* const ilimit = iend - HASH_READ_SIZE; 21319fcbaf1SConrad Meyer U32 offset_1=rep[0], offset_2=rep[1]; 2140c16b537SWarner Losh U32 offsetSaved = 0; 2150c16b537SWarner Losh 2160f743729SConrad Meyer const ZSTD_matchState_t* const dms = ms->dictMatchState; 2172b9c00cbSConrad Meyer const ZSTD_compressionParameters* const dictCParams = &dms->cParams ; 2182b9c00cbSConrad Meyer const U32* const dictHashTable = dms->hashTable; 2192b9c00cbSConrad Meyer const U32 dictStartIndex = dms->window.dictLimit; 2202b9c00cbSConrad Meyer const BYTE* const dictBase = dms->window.base; 2212b9c00cbSConrad Meyer const BYTE* const dictStart = dictBase + dictStartIndex; 2222b9c00cbSConrad Meyer const BYTE* const dictEnd = dms->window.nextSrc; 2232b9c00cbSConrad Meyer const U32 dictIndexDelta = prefixStartIndex - (U32)(dictEnd - dictBase); 2240f743729SConrad Meyer const U32 dictAndPrefixLength = (U32)(ip - prefixStart + dictEnd - dictStart); 2252b9c00cbSConrad Meyer const U32 dictHLog = dictCParams->hashLog; 2260f743729SConrad Meyer 2274d3f1eafSConrad Meyer /* if a dictionary is still attached, it necessarily means that 2284d3f1eafSConrad Meyer * it is within window size. So we just check it. */ 2294d3f1eafSConrad Meyer const U32 maxDistance = 1U << cParams->windowLog; 2304d3f1eafSConrad Meyer const U32 endIndex = (U32)((size_t)(ip - base) + srcSize); 2314d3f1eafSConrad Meyer assert(endIndex - prefixStartIndex <= maxDistance); 2324d3f1eafSConrad Meyer (void)maxDistance; (void)endIndex; /* these variables are not used when assert() is disabled */ 2334d3f1eafSConrad Meyer 2344d3f1eafSConrad Meyer /* ensure there will be no no underflow 2354d3f1eafSConrad Meyer * when translating a dict index into a local index */ 2362b9c00cbSConrad Meyer assert(prefixStartIndex >= (U32)(dictEnd - dictBase)); 2370f743729SConrad Meyer 2380c16b537SWarner Losh /* init */ 239*9cbefe25SConrad Meyer DEBUGLOG(5, "ZSTD_compressBlock_fast_dictMatchState_generic"); 2400f743729SConrad Meyer ip += (dictAndPrefixLength == 0); 2410f743729SConrad Meyer /* dictMatchState repCode checks don't currently handle repCode == 0 2420f743729SConrad Meyer * disabling. */ 2430f743729SConrad Meyer assert(offset_1 <= dictAndPrefixLength); 2440f743729SConrad Meyer assert(offset_2 <= dictAndPrefixLength); 2450c16b537SWarner Losh 2460c16b537SWarner Losh /* Main Search Loop */ 2470c16b537SWarner Losh while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ 2480c16b537SWarner Losh size_t mLength; 24919fcbaf1SConrad Meyer size_t const h = ZSTD_hashPtr(ip, hlog, mls); 2500c16b537SWarner Losh U32 const current = (U32)(ip-base); 2510c16b537SWarner Losh U32 const matchIndex = hashTable[h]; 2520c16b537SWarner Losh const BYTE* match = base + matchIndex; 2530f743729SConrad Meyer const U32 repIndex = current + 1 - offset_1; 2542b9c00cbSConrad Meyer const BYTE* repMatch = (repIndex < prefixStartIndex) ? 2550f743729SConrad Meyer dictBase + (repIndex - dictIndexDelta) : 2560f743729SConrad Meyer base + repIndex; 2570c16b537SWarner Losh hashTable[h] = current; /* update hash table */ 2580c16b537SWarner Losh 2592b9c00cbSConrad Meyer if ( ((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow : ensure repIndex isn't overlapping dict + prefix */ 2600f743729SConrad Meyer && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { 2610f743729SConrad Meyer const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; 2620f743729SConrad Meyer mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, prefixStart) + 4; 2630f743729SConrad Meyer ip++; 264*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, mLength-MINMATCH); 2650f743729SConrad Meyer } else if ( (matchIndex <= prefixStartIndex) ) { 2660f743729SConrad Meyer size_t const dictHash = ZSTD_hashPtr(ip, dictHLog, mls); 2670f743729SConrad Meyer U32 const dictMatchIndex = dictHashTable[dictHash]; 2680f743729SConrad Meyer const BYTE* dictMatch = dictBase + dictMatchIndex; 2690f743729SConrad Meyer if (dictMatchIndex <= dictStartIndex || 2700f743729SConrad Meyer MEM_read32(dictMatch) != MEM_read32(ip)) { 2710f743729SConrad Meyer assert(stepSize >= 1); 2720f743729SConrad Meyer ip += ((ip-anchor) >> kSearchStrength) + stepSize; 2730f743729SConrad Meyer continue; 2740c16b537SWarner Losh } else { 2750f743729SConrad Meyer /* found a dict match */ 2760f743729SConrad Meyer U32 const offset = (U32)(current-dictMatchIndex-dictIndexDelta); 2770f743729SConrad Meyer mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, prefixStart) + 4; 2780f743729SConrad Meyer while (((ip>anchor) & (dictMatch>dictStart)) 2790f743729SConrad Meyer && (ip[-1] == dictMatch[-1])) { 2800f743729SConrad Meyer ip--; dictMatch--; mLength++; 2810f743729SConrad Meyer } /* catch up */ 2820f743729SConrad Meyer offset_2 = offset_1; 2830f743729SConrad Meyer offset_1 = offset; 284*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH); 2850f743729SConrad Meyer } 2860f743729SConrad Meyer } else if (MEM_read32(match) != MEM_read32(ip)) { 2870f743729SConrad Meyer /* it's not a match, and we're not going to check the dictionary */ 2880f743729SConrad Meyer assert(stepSize >= 1); 2890f743729SConrad Meyer ip += ((ip-anchor) >> kSearchStrength) + stepSize; 2900f743729SConrad Meyer continue; 2910f743729SConrad Meyer } else { 2920f743729SConrad Meyer /* found a regular match */ 2930f743729SConrad Meyer U32 const offset = (U32)(ip-match); 2940c16b537SWarner Losh mLength = ZSTD_count(ip+4, match+4, iend) + 4; 2950f743729SConrad Meyer while (((ip>anchor) & (match>prefixStart)) 2960f743729SConrad Meyer && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ 2970c16b537SWarner Losh offset_2 = offset_1; 2980c16b537SWarner Losh offset_1 = offset; 299*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH); 3000f743729SConrad Meyer } 3010c16b537SWarner Losh 3020c16b537SWarner Losh /* match found */ 3030c16b537SWarner Losh ip += mLength; 3040c16b537SWarner Losh anchor = ip; 3050c16b537SWarner Losh 3060c16b537SWarner Losh if (ip <= ilimit) { 3070c16b537SWarner Losh /* Fill Table */ 3080f743729SConrad Meyer assert(base+current+2 > istart); /* check base overflow */ 30919fcbaf1SConrad Meyer hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2; /* here because current+2 could be > iend-8 */ 31019fcbaf1SConrad Meyer hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base); 3110f743729SConrad Meyer 3120c16b537SWarner Losh /* check immediate repcode */ 3130f743729SConrad Meyer while (ip <= ilimit) { 3140f743729SConrad Meyer U32 const current2 = (U32)(ip-base); 3150f743729SConrad Meyer U32 const repIndex2 = current2 - offset_2; 3160f743729SConrad Meyer const BYTE* repMatch2 = repIndex2 < prefixStartIndex ? 3170f743729SConrad Meyer dictBase - dictIndexDelta + repIndex2 : 3180f743729SConrad Meyer base + repIndex2; 3190f743729SConrad Meyer if ( ((U32)((prefixStartIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */) 3200f743729SConrad Meyer && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { 3210f743729SConrad Meyer const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; 3220f743729SConrad Meyer size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; 3230f743729SConrad Meyer U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ 324*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, 0, anchor, iend, 0, repLength2-MINMATCH); 3250f743729SConrad Meyer hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2; 3260f743729SConrad Meyer ip += repLength2; 3270f743729SConrad Meyer anchor = ip; 3280f743729SConrad Meyer continue; 3290f743729SConrad Meyer } 3300f743729SConrad Meyer break; 3310f743729SConrad Meyer } 3320f743729SConrad Meyer } 3332b9c00cbSConrad Meyer } 3340c16b537SWarner Losh 3350c16b537SWarner Losh /* save reps for next block */ 33619fcbaf1SConrad Meyer rep[0] = offset_1 ? offset_1 : offsetSaved; 33719fcbaf1SConrad Meyer rep[1] = offset_2 ? offset_2 : offsetSaved; 3380c16b537SWarner Losh 3390c16b537SWarner Losh /* Return the last literals size */ 3404d3f1eafSConrad Meyer return (size_t)(iend - anchor); 3410c16b537SWarner Losh } 3420c16b537SWarner Losh 3430f743729SConrad Meyer size_t ZSTD_compressBlock_fast_dictMatchState( 3440f743729SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 3450f743729SConrad Meyer void const* src, size_t srcSize) 3460f743729SConrad Meyer { 347*9cbefe25SConrad Meyer U32 const mls = ms->cParams.minMatch; 3480f743729SConrad Meyer assert(ms->dictMatchState != NULL); 3490f743729SConrad Meyer switch(mls) 3500f743729SConrad Meyer { 3510f743729SConrad Meyer default: /* includes case 3 */ 3520f743729SConrad Meyer case 4 : 3532b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 4); 3540f743729SConrad Meyer case 5 : 3552b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 5); 3560f743729SConrad Meyer case 6 : 3572b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 6); 3580f743729SConrad Meyer case 7 : 3592b9c00cbSConrad Meyer return ZSTD_compressBlock_fast_dictMatchState_generic(ms, seqStore, rep, src, srcSize, 7); 3600c16b537SWarner Losh } 3610c16b537SWarner Losh } 3620c16b537SWarner Losh 3630c16b537SWarner Losh 36419fcbaf1SConrad Meyer static size_t ZSTD_compressBlock_fast_extDict_generic( 36519fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 3660f743729SConrad Meyer void const* src, size_t srcSize, U32 const mls) 3670c16b537SWarner Losh { 3680f743729SConrad Meyer const ZSTD_compressionParameters* const cParams = &ms->cParams; 3690f743729SConrad Meyer U32* const hashTable = ms->hashTable; 3700f743729SConrad Meyer U32 const hlog = cParams->hashLog; 3710f743729SConrad Meyer /* support stepSize of 0 */ 3720f743729SConrad Meyer U32 const stepSize = cParams->targetLength + !(cParams->targetLength); 37319fcbaf1SConrad Meyer const BYTE* const base = ms->window.base; 37419fcbaf1SConrad Meyer const BYTE* const dictBase = ms->window.dictBase; 3750c16b537SWarner Losh const BYTE* const istart = (const BYTE*)src; 3760c16b537SWarner Losh const BYTE* ip = istart; 3770c16b537SWarner Losh const BYTE* anchor = istart; 3784d3f1eafSConrad Meyer const U32 endIndex = (U32)((size_t)(istart - base) + srcSize); 379*9cbefe25SConrad Meyer const U32 lowLimit = ZSTD_getLowestMatchIndex(ms, endIndex, cParams->windowLog); 3804d3f1eafSConrad Meyer const U32 dictStartIndex = lowLimit; 3810f743729SConrad Meyer const BYTE* const dictStart = dictBase + dictStartIndex; 3824d3f1eafSConrad Meyer const U32 dictLimit = ms->window.dictLimit; 3834d3f1eafSConrad Meyer const U32 prefixStartIndex = dictLimit < lowLimit ? lowLimit : dictLimit; 3840f743729SConrad Meyer const BYTE* const prefixStart = base + prefixStartIndex; 3850f743729SConrad Meyer const BYTE* const dictEnd = dictBase + prefixStartIndex; 3860c16b537SWarner Losh const BYTE* const iend = istart + srcSize; 3870c16b537SWarner Losh const BYTE* const ilimit = iend - 8; 38819fcbaf1SConrad Meyer U32 offset_1=rep[0], offset_2=rep[1]; 3890c16b537SWarner Losh 390*9cbefe25SConrad Meyer DEBUGLOG(5, "ZSTD_compressBlock_fast_extDict_generic"); 391*9cbefe25SConrad Meyer 3924d3f1eafSConrad Meyer /* switch to "regular" variant if extDict is invalidated due to maxDistance */ 3934d3f1eafSConrad Meyer if (prefixStartIndex == dictStartIndex) 3944d3f1eafSConrad Meyer return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, mls); 3954d3f1eafSConrad Meyer 3960c16b537SWarner Losh /* Search Loop */ 3970c16b537SWarner Losh while (ip < ilimit) { /* < instead of <=, because (ip+1) */ 39819fcbaf1SConrad Meyer const size_t h = ZSTD_hashPtr(ip, hlog, mls); 3990c16b537SWarner Losh const U32 matchIndex = hashTable[h]; 4000f743729SConrad Meyer const BYTE* const matchBase = matchIndex < prefixStartIndex ? dictBase : base; 4010c16b537SWarner Losh const BYTE* match = matchBase + matchIndex; 4020c16b537SWarner Losh const U32 current = (U32)(ip-base); 4030f743729SConrad Meyer const U32 repIndex = current + 1 - offset_1; 4040f743729SConrad Meyer const BYTE* const repBase = repIndex < prefixStartIndex ? dictBase : base; 4050f743729SConrad Meyer const BYTE* const repMatch = repBase + repIndex; 4060c16b537SWarner Losh hashTable[h] = current; /* update hash table */ 4070f743729SConrad Meyer assert(offset_1 <= current +1); /* check repIndex */ 4080c16b537SWarner Losh 4090f743729SConrad Meyer if ( (((U32)((prefixStartIndex-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > dictStartIndex)) 4100c16b537SWarner Losh && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { 411*9cbefe25SConrad Meyer const BYTE* const repMatchEnd = repIndex < prefixStartIndex ? dictEnd : iend; 412*9cbefe25SConrad Meyer size_t const rLength = ZSTD_count_2segments(ip+1 +4, repMatch +4, iend, repMatchEnd, prefixStart) + 4; 4130c16b537SWarner Losh ip++; 414*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, 0, rLength-MINMATCH); 415*9cbefe25SConrad Meyer ip += rLength; 416*9cbefe25SConrad Meyer anchor = ip; 4170c16b537SWarner Losh } else { 4180f743729SConrad Meyer if ( (matchIndex < dictStartIndex) || 4190c16b537SWarner Losh (MEM_read32(match) != MEM_read32(ip)) ) { 42019fcbaf1SConrad Meyer assert(stepSize >= 1); 42119fcbaf1SConrad Meyer ip += ((ip-anchor) >> kSearchStrength) + stepSize; 4220c16b537SWarner Losh continue; 4230c16b537SWarner Losh } 424*9cbefe25SConrad Meyer { const BYTE* const matchEnd = matchIndex < prefixStartIndex ? dictEnd : iend; 425*9cbefe25SConrad Meyer const BYTE* const lowMatchPtr = matchIndex < prefixStartIndex ? dictStart : prefixStart; 426*9cbefe25SConrad Meyer U32 const offset = current - matchIndex; 427*9cbefe25SConrad Meyer size_t mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, prefixStart) + 4; 4280c16b537SWarner Losh while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ 429*9cbefe25SConrad Meyer offset_2 = offset_1; offset_1 = offset; /* update offset history */ 430*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, (size_t)(ip-anchor), anchor, iend, offset + ZSTD_REP_MOVE, mLength-MINMATCH); 4310c16b537SWarner Losh ip += mLength; 4320c16b537SWarner Losh anchor = ip; 433*9cbefe25SConrad Meyer } } 4340c16b537SWarner Losh 4350c16b537SWarner Losh if (ip <= ilimit) { 4360c16b537SWarner Losh /* Fill Table */ 43719fcbaf1SConrad Meyer hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2; 43819fcbaf1SConrad Meyer hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base); 4390c16b537SWarner Losh /* check immediate repcode */ 4400c16b537SWarner Losh while (ip <= ilimit) { 4410c16b537SWarner Losh U32 const current2 = (U32)(ip-base); 4420c16b537SWarner Losh U32 const repIndex2 = current2 - offset_2; 443*9cbefe25SConrad Meyer const BYTE* const repMatch2 = repIndex2 < prefixStartIndex ? dictBase + repIndex2 : base + repIndex2; 4440f743729SConrad Meyer if ( (((U32)((prefixStartIndex-1) - repIndex2) >= 3) & (repIndex2 > dictStartIndex)) /* intentional overflow */ 4450c16b537SWarner Losh && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { 4460f743729SConrad Meyer const BYTE* const repEnd2 = repIndex2 < prefixStartIndex ? dictEnd : iend; 4470f743729SConrad Meyer size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, prefixStart) + 4; 448*9cbefe25SConrad Meyer { U32 const tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; } /* swap offset_2 <=> offset_1 */ 449*9cbefe25SConrad Meyer ZSTD_storeSeq(seqStore, 0 /*litlen*/, anchor, iend, 0 /*offcode*/, repLength2-MINMATCH); 45019fcbaf1SConrad Meyer hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2; 4510c16b537SWarner Losh ip += repLength2; 4520c16b537SWarner Losh anchor = ip; 4530c16b537SWarner Losh continue; 4540c16b537SWarner Losh } 4550c16b537SWarner Losh break; 4560c16b537SWarner Losh } } } 4570c16b537SWarner Losh 4580c16b537SWarner Losh /* save reps for next block */ 45919fcbaf1SConrad Meyer rep[0] = offset_1; 46019fcbaf1SConrad Meyer rep[1] = offset_2; 4610c16b537SWarner Losh 4620c16b537SWarner Losh /* Return the last literals size */ 4634d3f1eafSConrad Meyer return (size_t)(iend - anchor); 4640c16b537SWarner Losh } 4650c16b537SWarner Losh 4660c16b537SWarner Losh 46719fcbaf1SConrad Meyer size_t ZSTD_compressBlock_fast_extDict( 46819fcbaf1SConrad Meyer ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM], 4690f743729SConrad Meyer void const* src, size_t srcSize) 4700c16b537SWarner Losh { 471*9cbefe25SConrad Meyer U32 const mls = ms->cParams.minMatch; 4720c16b537SWarner Losh switch(mls) 4730c16b537SWarner Losh { 4740c16b537SWarner Losh default: /* includes case 3 */ 4750c16b537SWarner Losh case 4 : 4760f743729SConrad Meyer return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 4); 4770c16b537SWarner Losh case 5 : 4780f743729SConrad Meyer return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 5); 4790c16b537SWarner Losh case 6 : 4800f743729SConrad Meyer return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 6); 4810c16b537SWarner Losh case 7 : 4820f743729SConrad Meyer return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, 7); 4830c16b537SWarner Losh } 4840c16b537SWarner Losh } 485