2017-09-02 01:28:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2017-09-02 01:28:35 +00:00
|
|
|
*/
|
|
|
|
|
2017-11-08 00:15:23 +00:00
|
|
|
#include "zstd_compress_internal.h"
|
2017-09-02 01:28:35 +00:00
|
|
|
#include "zstd_fast.h"
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
void ZSTD_fillHashTable(ZSTD_matchState_t* ms,
|
|
|
|
ZSTD_compressionParameters const* cParams,
|
2018-04-02 21:41:30 +00:00
|
|
|
void const* end, ZSTD_dictTableLoadMethod_e dtlm)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2017-12-13 00:51:00 +00:00
|
|
|
U32* const hashTable = ms->hashTable;
|
|
|
|
U32 const hBits = cParams->hashLog;
|
|
|
|
U32 const mls = cParams->searchLength;
|
2018-02-24 00:48:18 +00:00
|
|
|
const BYTE* const base = ms->window.base;
|
2017-12-13 00:51:00 +00:00
|
|
|
const BYTE* ip = base + ms->nextToUpdate;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE;
|
2018-01-12 20:26:47 +00:00
|
|
|
const U32 fastHashFillStep = 3;
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2018-01-12 20:26:47 +00:00
|
|
|
/* Always insert every fastHashFillStep position into the hash table.
|
|
|
|
* Insert the other positions if their hash entry is empty.
|
|
|
|
*/
|
|
|
|
for (; ip + fastHashFillStep - 1 <= iend; ip += fastHashFillStep) {
|
|
|
|
U32 const current = (U32)(ip - base);
|
|
|
|
U32 i;
|
|
|
|
for (i = 0; i < fastHashFillStep; ++i) {
|
|
|
|
size_t const hash = ZSTD_hashPtr(ip + i, hBits, mls);
|
|
|
|
if (i == 0 || hashTable[hash] == 0)
|
|
|
|
hashTable[hash] = current + i;
|
2018-04-02 21:41:30 +00:00
|
|
|
/* Only load extra positions for ZSTD_dtlm_full */
|
|
|
|
if (dtlm == ZSTD_dtlm_fast)
|
|
|
|
break;
|
2018-01-12 20:26:47 +00:00
|
|
|
}
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FORCE_INLINE_TEMPLATE
|
2017-12-13 00:51:00 +00:00
|
|
|
size_t ZSTD_compressBlock_fast_generic(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
|
|
|
void const* src, size_t srcSize,
|
2018-04-28 04:42:37 +00:00
|
|
|
U32 const hlog, U32 const stepSize, U32 const mls,
|
2018-05-09 19:14:12 +00:00
|
|
|
ZSTD_dictMode_e const dictMode)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2017-12-13 00:51:00 +00:00
|
|
|
U32* const hashTable = ms->hashTable;
|
2018-02-24 00:48:18 +00:00
|
|
|
const BYTE* const base = ms->window.base;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const istart = (const BYTE*)src;
|
|
|
|
const BYTE* ip = istart;
|
|
|
|
const BYTE* anchor = istart;
|
2018-05-04 17:08:07 +00:00
|
|
|
const U32 localLowestIndex = ms->window.dictLimit;
|
|
|
|
const BYTE* const localLowest = base + localLowestIndex;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const iend = istart + srcSize;
|
|
|
|
const BYTE* const ilimit = iend - HASH_READ_SIZE;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32 offset_1=rep[0], offset_2=rep[1];
|
2017-09-02 01:28:35 +00:00
|
|
|
U32 offsetSaved = 0;
|
|
|
|
|
2018-04-28 04:42:37 +00:00
|
|
|
const ZSTD_matchState_t* const dms = ms->dictMatchState;
|
2018-05-09 19:14:12 +00:00
|
|
|
const U32* const dictHashTable = dictMode == ZSTD_dictMatchState ?
|
2018-04-28 04:42:37 +00:00
|
|
|
dms->hashTable : NULL;
|
2018-05-09 19:14:12 +00:00
|
|
|
const U32 lowestDictIndex = dictMode == ZSTD_dictMatchState ?
|
2018-04-28 04:42:37 +00:00
|
|
|
dms->window.dictLimit : 0;
|
2018-05-09 19:14:12 +00:00
|
|
|
const BYTE* const dictBase = dictMode == ZSTD_dictMatchState ?
|
2018-04-28 04:42:37 +00:00
|
|
|
dms->window.base : NULL;
|
2018-05-09 19:14:12 +00:00
|
|
|
const BYTE* const dictLowest = dictMode == ZSTD_dictMatchState ?
|
2018-05-02 19:12:18 +00:00
|
|
|
dictBase + lowestDictIndex : NULL;
|
2018-05-09 19:14:12 +00:00
|
|
|
const BYTE* const dictEnd = dictMode == ZSTD_dictMatchState ?
|
2018-04-28 04:42:37 +00:00
|
|
|
dms->window.nextSrc : NULL;
|
2018-05-09 19:14:12 +00:00
|
|
|
const U32 dictIndexDelta = dictMode == ZSTD_dictMatchState ?
|
2018-05-09 17:14:20 +00:00
|
|
|
localLowestIndex - (U32)(dictEnd - dictBase) :
|
2018-05-02 19:12:18 +00:00
|
|
|
0;
|
2018-05-09 19:14:12 +00:00
|
|
|
ptrdiff_t dictLowestLocalIndex = dictMode == ZSTD_dictMatchState ?
|
2018-05-02 19:12:18 +00:00
|
|
|
lowestDictIndex + dictIndexDelta :
|
2018-05-04 17:08:07 +00:00
|
|
|
localLowestIndex;
|
2018-04-28 04:42:37 +00:00
|
|
|
|
2018-05-09 19:14:12 +00:00
|
|
|
assert(dictMode == ZSTD_noDict || dictMode == ZSTD_dictMatchState);
|
2018-05-02 21:34:34 +00:00
|
|
|
|
2017-09-02 01:28:35 +00:00
|
|
|
/* init */
|
2018-05-09 19:14:12 +00:00
|
|
|
ip += (dictMode == ZSTD_noDict && ip == localLowest);
|
|
|
|
{ U32 const maxRep = dictMode == ZSTD_dictMatchState ?
|
2018-05-03 00:30:03 +00:00
|
|
|
(U32)(ip - dictLowest) :
|
2018-05-04 17:08:07 +00:00
|
|
|
(U32)(ip - localLowest);
|
2017-09-02 01:28:35 +00:00
|
|
|
if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0;
|
|
|
|
if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Main Search Loop */
|
|
|
|
while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */
|
|
|
|
size_t mLength;
|
2017-12-13 00:51:00 +00:00
|
|
|
size_t const h = ZSTD_hashPtr(ip, hlog, mls);
|
2017-09-02 01:28:35 +00:00
|
|
|
U32 const current = (U32)(ip-base);
|
|
|
|
U32 const matchIndex = hashTable[h];
|
|
|
|
const BYTE* match = base + matchIndex;
|
2018-05-04 17:08:07 +00:00
|
|
|
const ptrdiff_t repIndex = (ptrdiff_t)current + 1 - offset_1;
|
2018-05-09 19:14:12 +00:00
|
|
|
const BYTE* repBase = (dictMode == ZSTD_dictMatchState
|
2018-05-04 17:08:07 +00:00
|
|
|
&& repIndex < (ptrdiff_t)localLowestIndex) ?
|
|
|
|
dictBase - dictIndexDelta : base;
|
2018-05-01 20:21:18 +00:00
|
|
|
const BYTE* repMatch = repBase + repIndex;
|
2017-09-02 01:28:35 +00:00
|
|
|
hashTable[h] = current; /* update hash table */
|
|
|
|
|
2018-05-09 19:14:12 +00:00
|
|
|
if (dictMode == ZSTD_dictMatchState
|
2018-05-04 17:08:07 +00:00
|
|
|
&& (((U32)((localLowestIndex-1) - repIndex) >= 3 /* intentional underflow */)
|
|
|
|
& (repIndex > dictLowestLocalIndex))
|
2018-05-01 20:21:18 +00:00
|
|
|
&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
|
2018-05-04 17:08:07 +00:00
|
|
|
const BYTE* repMatchEnd = repIndex < (ptrdiff_t)localLowestIndex ? dictEnd : iend;
|
2018-05-01 20:21:18 +00:00
|
|
|
mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, istart) + 4;
|
|
|
|
ip++;
|
|
|
|
ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
|
2018-05-09 19:14:12 +00:00
|
|
|
} else if ( dictMode == ZSTD_noDict
|
2018-05-09 22:40:23 +00:00
|
|
|
&& (offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) {
|
2017-09-02 01:28:35 +00:00
|
|
|
mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4;
|
|
|
|
ip++;
|
2017-12-13 00:51:00 +00:00
|
|
|
ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
|
2018-05-04 17:08:07 +00:00
|
|
|
} else if ( (matchIndex <= localLowestIndex)
|
|
|
|
|| (MEM_read32(match) != MEM_read32(ip)) ) {
|
2018-05-09 19:14:12 +00:00
|
|
|
if (dictMode == ZSTD_dictMatchState) {
|
2018-05-04 17:08:07 +00:00
|
|
|
U32 const dictMatchIndex = dictHashTable[h];
|
|
|
|
const BYTE* dictMatch = dictBase + dictMatchIndex;
|
|
|
|
if (dictMatchIndex <= lowestDictIndex ||
|
|
|
|
MEM_read32(dictMatch) != MEM_read32(ip)) {
|
2018-04-28 04:42:37 +00:00
|
|
|
assert(stepSize >= 1);
|
|
|
|
ip += ((ip-anchor) >> kSearchStrength) + stepSize;
|
|
|
|
continue;
|
2018-05-04 17:08:07 +00:00
|
|
|
} else {
|
|
|
|
/* found a dict match */
|
|
|
|
U32 const offset = (U32)(current-dictMatchIndex-dictIndexDelta);
|
|
|
|
mLength = ZSTD_count_2segments(ip+4, dictMatch+4, iend, dictEnd, istart) + 4;
|
2018-05-10 17:46:19 +00:00
|
|
|
while (((ip>anchor) & (dictMatch>dictLowest))
|
2018-05-04 17:08:07 +00:00
|
|
|
&& (ip[-1] == dictMatch[-1])) {
|
|
|
|
ip--; dictMatch--; mLength++;
|
|
|
|
} /* catch up */
|
|
|
|
offset_2 = offset_1;
|
|
|
|
offset_1 = offset;
|
|
|
|
ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
|
2018-04-28 04:42:37 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-05-04 17:08:07 +00:00
|
|
|
assert(stepSize >= 1);
|
|
|
|
ip += ((ip-anchor) >> kSearchStrength) + stepSize;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* found a regular match */
|
|
|
|
U32 const offset = (U32)(ip-match);
|
|
|
|
mLength = ZSTD_count(ip+4, match+4, iend) + 4;
|
|
|
|
while (((ip>anchor) & (match>localLowest))
|
|
|
|
&& (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
|
|
|
|
offset_2 = offset_1;
|
|
|
|
offset_1 = offset;
|
|
|
|
ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
|
|
|
|
}
|
2017-09-02 01:28:35 +00:00
|
|
|
|
|
|
|
/* match found */
|
|
|
|
ip += mLength;
|
|
|
|
anchor = ip;
|
|
|
|
|
|
|
|
if (ip <= ilimit) {
|
|
|
|
/* Fill Table */
|
2017-12-13 00:51:00 +00:00
|
|
|
hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2; /* here because current+2 could be > iend-8 */
|
|
|
|
hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
|
2018-05-01 20:21:18 +00:00
|
|
|
|
2018-05-10 17:46:19 +00:00
|
|
|
/* check immediate repcode */
|
2018-05-09 19:14:12 +00:00
|
|
|
if (dictMode == ZSTD_dictMatchState) {
|
2018-05-04 17:08:07 +00:00
|
|
|
while (ip <= ilimit) {
|
|
|
|
U32 const current2 = (U32)(ip-base);
|
|
|
|
ptrdiff_t const repIndex2 = (ptrdiff_t)current2 - offset_2;
|
2018-05-09 19:14:12 +00:00
|
|
|
const BYTE* repMatch2 = dictMode == ZSTD_dictMatchState
|
2018-05-04 17:08:07 +00:00
|
|
|
&& repIndex2 < (ptrdiff_t)localLowestIndex ?
|
|
|
|
dictBase - dictIndexDelta + repIndex2 :
|
|
|
|
base + repIndex2;
|
|
|
|
if ( (((U32)((localLowestIndex-1) - (U32)repIndex2) >= 3 /* intentional overflow */)
|
|
|
|
& (repIndex2 > dictLowestLocalIndex))
|
|
|
|
&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
|
|
|
|
const BYTE* const repEnd2 = repIndex2 < (ptrdiff_t)localLowestIndex ? dictEnd : iend;
|
|
|
|
size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, istart) + 4;
|
|
|
|
U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
|
|
|
|
ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
|
|
|
|
hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
|
|
|
|
ip += repLength2;
|
|
|
|
anchor = ip;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2018-05-01 20:21:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-09 19:14:12 +00:00
|
|
|
if (dictMode == ZSTD_noDict) {
|
2018-05-04 17:08:07 +00:00
|
|
|
while ( (ip <= ilimit)
|
|
|
|
&& (ip - offset_2 >= istart)
|
|
|
|
&& ( (offset_2>0)
|
|
|
|
& (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) {
|
|
|
|
/* store sequence */
|
|
|
|
size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4;
|
|
|
|
U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */
|
|
|
|
hashTable[ZSTD_hashPtr(ip, hlog, mls)] = (U32)(ip-base);
|
|
|
|
ZSTD_storeSeq(seqStore, 0, anchor, 0, rLength-MINMATCH);
|
|
|
|
ip += rLength;
|
|
|
|
anchor = ip;
|
|
|
|
continue; /* faster when present ... (?) */
|
2018-05-01 20:21:18 +00:00
|
|
|
} } } }
|
2017-09-02 01:28:35 +00:00
|
|
|
|
|
|
|
/* save reps for next block */
|
2017-12-13 00:51:00 +00:00
|
|
|
rep[0] = offset_1 ? offset_1 : offsetSaved;
|
|
|
|
rep[1] = offset_2 ? offset_2 : offsetSaved;
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2017-09-06 22:56:32 +00:00
|
|
|
/* Return the last literals size */
|
|
|
|
return iend - anchor;
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
size_t ZSTD_compressBlock_fast(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
|
|
|
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2017-12-13 00:51:00 +00:00
|
|
|
U32 const hlog = cParams->hashLog;
|
|
|
|
U32 const mls = cParams->searchLength;
|
2018-03-11 12:21:53 +00:00
|
|
|
U32 const stepSize = cParams->targetLength;
|
2018-05-02 21:10:51 +00:00
|
|
|
assert(ms->dictMatchState == NULL);
|
|
|
|
switch(mls)
|
|
|
|
{
|
|
|
|
default: /* includes case 3 */
|
|
|
|
case 4 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 4, ZSTD_noDict);
|
2018-05-02 21:10:51 +00:00
|
|
|
case 5 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 5, ZSTD_noDict);
|
2018-05-02 21:10:51 +00:00
|
|
|
case 6 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 6, ZSTD_noDict);
|
2018-05-02 21:10:51 +00:00
|
|
|
case 7 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 7, ZSTD_noDict);
|
2018-05-02 21:10:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-02 21:34:34 +00:00
|
|
|
size_t ZSTD_compressBlock_fast_dictMatchState(
|
2018-05-02 21:10:51 +00:00
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
|
|
|
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
|
|
|
|
{
|
|
|
|
U32 const hlog = cParams->hashLog;
|
|
|
|
U32 const mls = cParams->searchLength;
|
|
|
|
U32 const stepSize = cParams->targetLength;
|
|
|
|
assert(ms->dictMatchState != NULL);
|
|
|
|
switch(mls)
|
|
|
|
{
|
|
|
|
default: /* includes case 3 */
|
|
|
|
case 4 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 4, ZSTD_dictMatchState);
|
2018-05-02 21:10:51 +00:00
|
|
|
case 5 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 5, ZSTD_dictMatchState);
|
2018-05-02 21:10:51 +00:00
|
|
|
case 6 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 6, ZSTD_dictMatchState);
|
2018-05-02 21:10:51 +00:00
|
|
|
case 7 :
|
2018-05-02 21:34:34 +00:00
|
|
|
return ZSTD_compressBlock_fast_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 7, ZSTD_dictMatchState);
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
static size_t ZSTD_compressBlock_fast_extDict_generic(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
|
|
|
void const* src, size_t srcSize,
|
2018-03-11 12:21:53 +00:00
|
|
|
U32 const hlog, U32 const stepSize, U32 const mls)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2017-12-13 00:51:00 +00:00
|
|
|
U32* hashTable = ms->hashTable;
|
2018-02-24 00:48:18 +00:00
|
|
|
const BYTE* const base = ms->window.base;
|
|
|
|
const BYTE* const dictBase = ms->window.dictBase;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const istart = (const BYTE*)src;
|
|
|
|
const BYTE* ip = istart;
|
|
|
|
const BYTE* anchor = istart;
|
2018-02-24 00:48:18 +00:00
|
|
|
const U32 lowestIndex = ms->window.lowLimit;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const dictStart = dictBase + lowestIndex;
|
2018-02-24 00:48:18 +00:00
|
|
|
const U32 dictLimit = ms->window.dictLimit;
|
2017-09-02 01:28:35 +00:00
|
|
|
const BYTE* const lowPrefixPtr = base + dictLimit;
|
|
|
|
const BYTE* const dictEnd = dictBase + dictLimit;
|
|
|
|
const BYTE* const iend = istart + srcSize;
|
|
|
|
const BYTE* const ilimit = iend - 8;
|
2017-12-13 00:51:00 +00:00
|
|
|
U32 offset_1=rep[0], offset_2=rep[1];
|
2017-09-02 01:28:35 +00:00
|
|
|
|
|
|
|
/* Search Loop */
|
|
|
|
while (ip < ilimit) { /* < instead of <=, because (ip+1) */
|
2017-12-13 00:51:00 +00:00
|
|
|
const size_t h = ZSTD_hashPtr(ip, hlog, mls);
|
2017-09-02 01:28:35 +00:00
|
|
|
const U32 matchIndex = hashTable[h];
|
|
|
|
const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base;
|
|
|
|
const BYTE* match = matchBase + matchIndex;
|
|
|
|
const U32 current = (U32)(ip-base);
|
|
|
|
const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */
|
|
|
|
const BYTE* repBase = repIndex < dictLimit ? dictBase : base;
|
|
|
|
const BYTE* repMatch = repBase + repIndex;
|
|
|
|
size_t mLength;
|
|
|
|
hashTable[h] = current; /* update hash table */
|
|
|
|
|
|
|
|
if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex))
|
|
|
|
&& (MEM_read32(repMatch) == MEM_read32(ip+1)) ) {
|
|
|
|
const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend;
|
|
|
|
mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4;
|
|
|
|
ip++;
|
2017-12-13 00:51:00 +00:00
|
|
|
ZSTD_storeSeq(seqStore, ip-anchor, anchor, 0, mLength-MINMATCH);
|
2017-09-02 01:28:35 +00:00
|
|
|
} else {
|
|
|
|
if ( (matchIndex < lowestIndex) ||
|
|
|
|
(MEM_read32(match) != MEM_read32(ip)) ) {
|
2018-03-11 12:21:53 +00:00
|
|
|
assert(stepSize >= 1);
|
|
|
|
ip += ((ip-anchor) >> kSearchStrength) + stepSize;
|
2017-09-02 01:28:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
{ const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend;
|
|
|
|
const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr;
|
|
|
|
U32 offset;
|
|
|
|
mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4;
|
|
|
|
while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */
|
|
|
|
offset = current - matchIndex;
|
|
|
|
offset_2 = offset_1;
|
|
|
|
offset_1 = offset;
|
2017-12-13 00:51:00 +00:00
|
|
|
ZSTD_storeSeq(seqStore, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH);
|
2017-09-02 01:28:35 +00:00
|
|
|
} }
|
|
|
|
|
|
|
|
/* found a match : store it */
|
|
|
|
ip += mLength;
|
|
|
|
anchor = ip;
|
|
|
|
|
|
|
|
if (ip <= ilimit) {
|
|
|
|
/* Fill Table */
|
2017-12-13 00:51:00 +00:00
|
|
|
hashTable[ZSTD_hashPtr(base+current+2, hlog, mls)] = current+2;
|
|
|
|
hashTable[ZSTD_hashPtr(ip-2, hlog, mls)] = (U32)(ip-2-base);
|
2017-09-02 01:28:35 +00:00
|
|
|
/* check immediate repcode */
|
|
|
|
while (ip <= ilimit) {
|
|
|
|
U32 const current2 = (U32)(ip-base);
|
|
|
|
U32 const repIndex2 = current2 - offset_2;
|
|
|
|
const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2;
|
|
|
|
if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */
|
|
|
|
&& (MEM_read32(repMatch2) == MEM_read32(ip)) ) {
|
|
|
|
const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend;
|
|
|
|
size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4;
|
|
|
|
U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */
|
2017-12-13 00:51:00 +00:00
|
|
|
ZSTD_storeSeq(seqStore, 0, anchor, 0, repLength2-MINMATCH);
|
|
|
|
hashTable[ZSTD_hashPtr(ip, hlog, mls)] = current2;
|
2017-09-02 01:28:35 +00:00
|
|
|
ip += repLength2;
|
|
|
|
anchor = ip;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
} } }
|
|
|
|
|
|
|
|
/* save reps for next block */
|
2017-12-13 00:51:00 +00:00
|
|
|
rep[0] = offset_1;
|
|
|
|
rep[1] = offset_2;
|
2017-09-02 01:28:35 +00:00
|
|
|
|
2017-09-06 22:56:32 +00:00
|
|
|
/* Return the last literals size */
|
|
|
|
return iend - anchor;
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-12-13 00:51:00 +00:00
|
|
|
size_t ZSTD_compressBlock_fast_extDict(
|
|
|
|
ZSTD_matchState_t* ms, seqStore_t* seqStore, U32 rep[ZSTD_REP_NUM],
|
|
|
|
ZSTD_compressionParameters const* cParams, void const* src, size_t srcSize)
|
2017-09-02 01:28:35 +00:00
|
|
|
{
|
2017-12-13 00:51:00 +00:00
|
|
|
U32 const hlog = cParams->hashLog;
|
|
|
|
U32 const mls = cParams->searchLength;
|
2018-03-11 12:21:53 +00:00
|
|
|
U32 const stepSize = cParams->targetLength;
|
2017-09-02 01:28:35 +00:00
|
|
|
switch(mls)
|
|
|
|
{
|
|
|
|
default: /* includes case 3 */
|
|
|
|
case 4 :
|
2018-03-11 12:21:53 +00:00
|
|
|
return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 4);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 5 :
|
2018-03-11 12:21:53 +00:00
|
|
|
return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 5);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 6 :
|
2018-03-11 12:21:53 +00:00
|
|
|
return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 6);
|
2017-09-02 01:28:35 +00:00
|
|
|
case 7 :
|
2018-03-11 12:21:53 +00:00
|
|
|
return ZSTD_compressBlock_fast_extDict_generic(ms, seqStore, rep, src, srcSize, hlog, stepSize, 7);
|
2017-09-02 01:28:35 +00:00
|
|
|
}
|
|
|
|
}
|