Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | ZSTD HC - High Compression Mode of Zstandard |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 3 | Copyright (C) 2015-2016, Yann Collet. |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 4 | |
| 5 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
| 6 | |
| 7 | Redistribution and use in source and binary forms, with or without |
| 8 | modification, are permitted provided that the following conditions are |
| 9 | met: |
| 10 | |
| 11 | * Redistributions of source code must retain the above copyright |
| 12 | notice, this list of conditions and the following disclaimer. |
| 13 | * Redistributions in binary form must reproduce the above |
| 14 | copyright notice, this list of conditions and the following disclaimer |
| 15 | in the documentation and/or other materials provided with the |
| 16 | distribution. |
| 17 | |
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | You can contact the author at : |
| 31 | - Zstd source repository : https://www.zstd.net |
| 32 | */ |
| 33 | |
| 34 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 35 | /* ******************************************************* |
| 36 | * Compiler specifics |
| 37 | *********************************************************/ |
| 38 | #ifdef _MSC_VER /* Visual Studio */ |
| 39 | # define FORCE_INLINE static __forceinline |
| 40 | # include <intrin.h> /* For Visual 2005 */ |
| 41 | # pragma warning(disable : 4127) /* disable: C4127: conditional expression is constant */ |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 42 | #else |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 43 | # ifdef __GNUC__ |
| 44 | # define FORCE_INLINE static inline __attribute__((always_inline)) |
| 45 | # else |
| 46 | # define FORCE_INLINE static inline |
| 47 | # endif |
| 48 | #endif |
| 49 | |
| 50 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 51 | /* ************************************* |
Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 52 | * Dependencies |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 53 | ***************************************/ |
| 54 | #include <stdlib.h> /* malloc */ |
| 55 | #include <string.h> /* memset */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 56 | #include "mem.h" |
| 57 | #include "fse_static.h" |
Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 58 | #include "huff0_static.h" |
Yann Collet | 3d9cf7a | 2015-10-29 17:15:14 +0100 | [diff] [blame] | 59 | #include "zstd_internal.h" |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 60 | |
| 61 | |
| 62 | /* ************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 63 | * Constants |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 64 | ***************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 65 | static const U32 g_searchStrength = 8; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 66 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 67 | |
| 68 | /* ************************************* |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 69 | * Helper functions |
| 70 | ***************************************/ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 71 | size_t ZSTD_compressBound(size_t srcSize) { return FSE_compressBound(srcSize) + 12; } |
| 72 | |
| 73 | |
| 74 | /* ************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 75 | * Sequence storage |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 76 | ***************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 77 | typedef struct { |
| 78 | void* buffer; |
| 79 | U32* offsetStart; |
| 80 | U32* offset; |
| 81 | BYTE* offCodeStart; |
| 82 | BYTE* offCode; |
| 83 | BYTE* litStart; |
| 84 | BYTE* lit; |
| 85 | BYTE* litLengthStart; |
| 86 | BYTE* litLength; |
| 87 | BYTE* matchLengthStart; |
| 88 | BYTE* matchLength; |
| 89 | BYTE* dumpsStart; |
| 90 | BYTE* dumps; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 91 | /* opt */ |
inikep | 70b0545 | 2016-02-03 22:56:55 +0100 | [diff] [blame] | 92 | U32* matchLengthFreq; |
| 93 | U32* litLengthFreq; |
| 94 | U32* litFreq; |
| 95 | U32* offCodeFreq; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 96 | U32 matchLengthSum; |
| 97 | U32 litLengthSum; |
| 98 | U32 litSum; |
| 99 | U32 offCodeSum; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 100 | } seqStore_t; |
| 101 | |
inikep | 27e1c6a | 2016-02-04 11:11:08 +0100 | [diff] [blame] | 102 | static void ZSTD_resetFreqs(seqStore_t* ssPtr) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 103 | { |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 104 | unsigned u; |
| 105 | ssPtr->matchLengthSum = 512; // (1<<MLbits); |
| 106 | ssPtr->litLengthSum = 256; // (1<<LLbits); |
inikep | 3bfcfc7 | 2016-02-03 18:47:30 +0100 | [diff] [blame] | 107 | ssPtr->litSum = (1<<Litbits); |
| 108 | ssPtr->offCodeSum = (1<<Offbits); |
| 109 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 110 | for (u=0; u<=MaxLit; u++) |
| 111 | ssPtr->litFreq[u] = 1; |
| 112 | for (u=0; u<=MaxLL; u++) |
| 113 | ssPtr->litLengthFreq[u] = 1; |
| 114 | for (u=0; u<=MaxML; u++) |
| 115 | ssPtr->matchLengthFreq[u] = 1; |
| 116 | for (u=0; u<=MaxOff; u++) |
| 117 | ssPtr->offCodeFreq[u] = 1; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 120 | static void ZSTD_resetSeqStore(seqStore_t* ssPtr) |
| 121 | { |
| 122 | ssPtr->offset = ssPtr->offsetStart; |
| 123 | ssPtr->lit = ssPtr->litStart; |
| 124 | ssPtr->litLength = ssPtr->litLengthStart; |
| 125 | ssPtr->matchLength = ssPtr->matchLengthStart; |
| 126 | ssPtr->dumps = ssPtr->dumpsStart; |
inikep | 27e1c6a | 2016-02-04 11:11:08 +0100 | [diff] [blame] | 127 | |
| 128 | ZSTD_resetFreqs(ssPtr); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
| 132 | /* ************************************* |
| 133 | * Context memory management |
| 134 | ***************************************/ |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 135 | struct ZSTD_CCtx_s |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 136 | { |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 137 | const BYTE* nextSrc; /* next block here to continue on current prefix */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 138 | const BYTE* base; /* All regular indexes relative to this position */ |
| 139 | const BYTE* dictBase; /* extDict indexes relative to this position */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 140 | U32 dictLimit; /* below that point, need extDict */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 141 | U32 lowLimit; /* below that point, no more data */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 142 | U32 nextToUpdate; /* index from which to continue dictionary update */ |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 143 | U32 loadedDictEnd; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 144 | U32 stage; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 145 | ZSTD_parameters params; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 146 | void* workSpace; |
| 147 | size_t workSpaceSize; |
Yann Collet | 120230b | 2015-12-02 14:00:45 +0100 | [diff] [blame] | 148 | size_t blockSize; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 149 | size_t hbSize; |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 150 | char headerBuffer[ZSTD_frameHeaderSize_max]; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 151 | |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 152 | seqStore_t seqStore; /* sequences storage ptrs */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 153 | U32* hashTable; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 154 | U32* contentTable; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 155 | HUF_CElt* hufTable; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 156 | U32 flagStaticTables; |
| 157 | FSE_CTable offcodeCTable [FSE_CTABLE_SIZE_U32(OffFSELog, MaxOff)]; |
| 158 | FSE_CTable matchlengthCTable [FSE_CTABLE_SIZE_U32(MLFSELog, MaxML)]; |
| 159 | FSE_CTable litlengthCTable [FSE_CTABLE_SIZE_U32(LLFSELog, MaxLL)]; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 163 | ZSTD_CCtx* ZSTD_createCCtx(void) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 164 | { |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 165 | return (ZSTD_CCtx*) calloc(1, sizeof(ZSTD_CCtx)); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 168 | size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 169 | { |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 170 | free(cctx->workSpace); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 171 | free(cctx); |
Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 172 | return 0; /* reserved as a potential error code in the future */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 173 | } |
| 174 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 175 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 176 | static unsigned ZSTD_highbit(U32 val); |
| 177 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 178 | #define CLAMP(val,min,max) { if (val<min) val=min; else if (val>max) val=max; } |
| 179 | |
| 180 | /** ZSTD_validateParams() |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 181 | correct params value to remain within authorized range |
| 182 | optimize for srcSize if srcSize > 0 */ |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 183 | void ZSTD_validateParams(ZSTD_parameters* params) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 184 | { |
inikep | c71568f | 2016-01-30 12:15:56 +0100 | [diff] [blame] | 185 | const U32 btPlus = (params->strategy == ZSTD_btlazy2) || (params->strategy == ZSTD_opt_bt); |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 186 | |
| 187 | /* validate params */ |
Yann Collet | 00fd7a2 | 2015-11-28 16:03:22 +0100 | [diff] [blame] | 188 | if (MEM_32bits()) if (params->windowLog > 25) params->windowLog = 25; /* 32 bits mode cannot flush > 24 bits */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 189 | CLAMP(params->windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
| 190 | CLAMP(params->contentLog, ZSTD_CONTENTLOG_MIN, ZSTD_CONTENTLOG_MAX); |
| 191 | CLAMP(params->hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 192 | CLAMP(params->searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
| 193 | CLAMP(params->searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
Yann Collet | bd828d9 | 2016-02-11 04:38:55 +0100 | [diff] [blame^] | 194 | CLAMP(params->targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 195 | if ((U32)params->strategy>(U32)ZSTD_opt_bt) params->strategy = ZSTD_opt_bt; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 196 | |
| 197 | /* correct params, to use less memory */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 198 | if ((params->srcSize > 0) && (params->srcSize < (1<<ZSTD_WINDOWLOG_MAX))) { |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 199 | U32 srcLog = ZSTD_highbit((U32)(params->srcSize)-1) + 1; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 200 | if (params->windowLog > srcLog) params->windowLog = srcLog; |
| 201 | } |
Yann Collet | 00fd7a2 | 2015-11-28 16:03:22 +0100 | [diff] [blame] | 202 | if (params->windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN) params->windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */ |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 203 | if (params->contentLog > params->windowLog+btPlus) params->contentLog = params->windowLog+btPlus; /* <= ZSTD_CONTENTLOG_MAX */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 207 | static size_t ZSTD_resetCCtx_advanced (ZSTD_CCtx* zc, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 208 | ZSTD_parameters params) |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 209 | { /* note : params considered validated here */ |
Yann Collet | 120230b | 2015-12-02 14:00:45 +0100 | [diff] [blame] | 210 | const size_t blockSize = MIN(BLOCKSIZE, (size_t)1 << params.windowLog); |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 211 | /* reserve table memory */ |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 212 | const U32 contentLog = (params.strategy == ZSTD_fast) ? 1 : params.contentLog; |
| 213 | const size_t tableSpace = ((1 << contentLog) + (1 << params.hashLog)) * sizeof(U32); |
inikep | 70b0545 | 2016-02-03 22:56:55 +0100 | [diff] [blame] | 214 | const size_t neededSpace = tableSpace + (256*sizeof(U32)) + (3*blockSize) + ((1<<MLbits) + (1<<LLbits) + (1<<Offbits) + (1<<Litbits))*sizeof(U32); |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 215 | if (zc->workSpaceSize < neededSpace) { |
| 216 | free(zc->workSpace); |
| 217 | zc->workSpace = malloc(neededSpace); |
| 218 | if (zc->workSpace == NULL) return ERROR(memory_allocation); |
| 219 | zc->workSpaceSize = neededSpace; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 220 | } |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 221 | memset(zc->workSpace, 0, tableSpace ); /* reset only tables */ |
| 222 | zc->hashTable = (U32*)(zc->workSpace); |
| 223 | zc->contentTable = zc->hashTable + ((size_t)1 << params.hashLog); |
| 224 | zc->seqStore.buffer = zc->contentTable + ((size_t)1 << contentLog); |
| 225 | zc->hufTable = (HUF_CElt*)zc->seqStore.buffer; |
| 226 | zc->flagStaticTables = 0; |
| 227 | zc->seqStore.buffer = (U32*)(zc->seqStore.buffer) + 256; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 228 | |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 229 | zc->nextToUpdate = 1; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 230 | zc->nextSrc = NULL; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 231 | zc->base = NULL; |
| 232 | zc->dictBase = NULL; |
| 233 | zc->dictLimit = 0; |
| 234 | zc->lowLimit = 0; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 235 | zc->params = params; |
Yann Collet | 120230b | 2015-12-02 14:00:45 +0100 | [diff] [blame] | 236 | zc->blockSize = blockSize; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 237 | |
| 238 | zc->seqStore.litFreq = (U32*) (zc->seqStore.buffer); |
| 239 | zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<<Litbits); |
| 240 | zc->seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (1<<LLbits); |
| 241 | zc->seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (1<<MLbits); |
| 242 | |
| 243 | zc->seqStore.offsetStart = zc->seqStore.offCodeFreq + (1<<Offbits); |
Yann Collet | 120230b | 2015-12-02 14:00:45 +0100 | [diff] [blame] | 244 | zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart + (blockSize>>2)); |
| 245 | zc->seqStore.litStart = zc->seqStore.offCodeStart + (blockSize>>2); |
| 246 | zc->seqStore.litLengthStart = zc->seqStore.litStart + blockSize; |
| 247 | zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (blockSize>>2); |
| 248 | zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (blockSize>>2); |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 249 | // zc->seqStore.XXX = zc->seqStore.dumpsStart + (blockSize>>4); |
inikep | 3bfcfc7 | 2016-02-03 18:47:30 +0100 | [diff] [blame] | 250 | |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 251 | zc->hbSize = 0; |
Yann Collet | 6009627 | 2016-01-08 17:27:50 +0100 | [diff] [blame] | 252 | zc->stage = 0; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 253 | zc->loadedDictEnd = 0; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 254 | |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 255 | return 0; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 258 | |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 259 | /*! ZSTD_copyCCtx |
| 260 | * Duplicate an existing context @srcCCtx into another one @dstCCtx. |
| 261 | * Only works during stage 0 (i.e. before first call to ZSTD_compressContinue()) |
| 262 | * @return : 0, or an error code */ |
| 263 | size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx) |
| 264 | { |
| 265 | const U32 contentLog = (srcCCtx->params.strategy == ZSTD_fast) ? 1 : srcCCtx->params.contentLog; |
| 266 | const size_t tableSpace = ((1 << contentLog) + (1 << srcCCtx->params.hashLog)) * sizeof(U32); |
| 267 | |
| 268 | if (srcCCtx->stage!=0) return ERROR(stage_wrong); |
| 269 | |
| 270 | ZSTD_resetCCtx_advanced(dstCCtx, srcCCtx->params); |
| 271 | |
| 272 | /* copy tables */ |
| 273 | memcpy(dstCCtx->hashTable, srcCCtx->hashTable, tableSpace); |
| 274 | |
| 275 | /* copy frame header */ |
| 276 | dstCCtx->hbSize = srcCCtx->hbSize; |
| 277 | memcpy(dstCCtx->headerBuffer , srcCCtx->headerBuffer, srcCCtx->hbSize); |
| 278 | |
| 279 | /* copy dictionary pointers */ |
| 280 | dstCCtx->nextToUpdate= srcCCtx->nextToUpdate; |
| 281 | dstCCtx->nextSrc = srcCCtx->nextSrc; |
| 282 | dstCCtx->base = srcCCtx->base; |
| 283 | dstCCtx->dictBase = srcCCtx->dictBase; |
| 284 | dstCCtx->dictLimit = srcCCtx->dictLimit; |
| 285 | dstCCtx->lowLimit = srcCCtx->lowLimit; |
Yann Collet | 7a6343f | 2016-02-02 16:00:50 +0100 | [diff] [blame] | 286 | dstCCtx->loadedDictEnd = srcCCtx->loadedDictEnd; |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 287 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 288 | /* copy entropy tables */ |
| 289 | dstCCtx->flagStaticTables = srcCCtx->flagStaticTables; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 290 | if (srcCCtx->flagStaticTables) { |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 291 | memcpy(dstCCtx->hufTable, srcCCtx->hufTable, 256*4); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 292 | memcpy(dstCCtx->litlengthCTable, srcCCtx->litlengthCTable, sizeof(dstCCtx->litlengthCTable)); |
| 293 | memcpy(dstCCtx->matchlengthCTable, srcCCtx->matchlengthCTable, sizeof(dstCCtx->matchlengthCTable)); |
| 294 | memcpy(dstCCtx->offcodeCTable, srcCCtx->offcodeCTable, sizeof(dstCCtx->offcodeCTable)); |
| 295 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
| 300 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 301 | /*! ZSTD_reduceIndex |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 302 | * rescale indexes to avoid future overflow (indexes are U32) */ |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 303 | static void ZSTD_reduceIndex (ZSTD_CCtx* zc, |
| 304 | const U32 reducerValue) |
| 305 | { |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 306 | const U32 contentLog = (zc->params.strategy == ZSTD_fast) ? 1 : zc->params.contentLog; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 307 | const U32 tableSpaceU32 = (1 << contentLog) + (1 << zc->params.hashLog); |
| 308 | U32* table32 = zc->hashTable; |
| 309 | U32 index; |
| 310 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 311 | for (index=0 ; index < tableSpaceU32 ; index++) { |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 312 | if (table32[index] < reducerValue) table32[index] = 0; |
| 313 | else table32[index] -= reducerValue; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 318 | /*-******************************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 319 | * Block entropic compression |
| 320 | *********************************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 321 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 322 | /* Block format description |
| 323 | |
| 324 | Block = Literal Section - Sequences Section |
| 325 | Prerequisite : size of (compressed) block, maximum size of regenerated data |
| 326 | |
| 327 | 1) Literal Section |
| 328 | |
| 329 | 1.1) Header : 1-5 bytes |
| 330 | flags: 2 bits |
| 331 | 00 compressed by Huff0 |
| 332 | 01 unused |
| 333 | 10 is Raw (uncompressed) |
| 334 | 11 is Rle |
| 335 | Note : using 01 => Huff0 with precomputed table ? |
| 336 | Note : delta map ? => compressed ? |
| 337 | |
| 338 | 1.1.1) Huff0-compressed literal block : 3-5 bytes |
Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 339 | srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 340 | srcSize < 1 KB => 3 bytes (2-2-10-10) |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 341 | srcSize < 16KB => 4 bytes (2-2-14-14) |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 342 | else => 5 bytes (2-2-18-18) |
| 343 | big endian convention |
| 344 | |
| 345 | 1.1.2) Raw (uncompressed) literal block header : 1-3 bytes |
| 346 | size : 5 bits: (IS_RAW<<6) + (0<<4) + size |
| 347 | 12 bits: (IS_RAW<<6) + (2<<4) + (size>>8) |
| 348 | size&255 |
| 349 | 20 bits: (IS_RAW<<6) + (3<<4) + (size>>16) |
| 350 | size>>8&255 |
| 351 | size&255 |
| 352 | |
| 353 | 1.1.3) Rle (repeated single byte) literal block header : 1-3 bytes |
| 354 | size : 5 bits: (IS_RLE<<6) + (0<<4) + size |
| 355 | 12 bits: (IS_RLE<<6) + (2<<4) + (size>>8) |
| 356 | size&255 |
| 357 | 20 bits: (IS_RLE<<6) + (3<<4) + (size>>16) |
| 358 | size>>8&255 |
| 359 | size&255 |
| 360 | |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 361 | 1.1.4) Huff0-compressed literal block, using precomputed CTables : 3-5 bytes |
| 362 | srcSize < 1 KB => 3 bytes (2-2-10-10) => single stream |
| 363 | srcSize < 1 KB => 3 bytes (2-2-10-10) |
| 364 | srcSize < 16KB => 4 bytes (2-2-14-14) |
| 365 | else => 5 bytes (2-2-18-18) |
| 366 | big endian convention |
| 367 | |
| 368 | 1- CTable available (stored into workspace ?) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 369 | 2- Small input (fast heuristic ? Full comparison ? depend on clevel ?) |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 370 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 371 | |
| 372 | 1.2) Literal block content |
| 373 | |
| 374 | 1.2.1) Huff0 block, using sizes from header |
| 375 | See Huff0 format |
| 376 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 377 | 1.2.2) Huff0 block, using prepared table |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 378 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 379 | 1.2.3) Raw content |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 380 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 381 | 1.2.4) single byte |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 382 | |
| 383 | |
| 384 | 2) Sequences section |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 385 | |
| 386 | - Nb Sequences : 2 bytes, little endian |
| 387 | - Control Token : 1 byte (see below) |
| 388 | - Dumps Length : 1 or 2 bytes (depending on control token) |
| 389 | - Dumps : as stated by dumps length |
| 390 | - Literal Lengths FSE table (as needed depending on encoding method) |
| 391 | - Offset Codes FSE table (as needed depending on encoding method) |
| 392 | - Match Lengths FSE table (as needed depending on encoding method) |
| 393 | |
| 394 | 2.1) Control Token |
| 395 | 8 bits, divided as : |
| 396 | 0-1 : dumpsLength |
| 397 | 2-3 : MatchLength, FSE encoding method |
| 398 | 4-5 : Offset Codes, FSE encoding method |
| 399 | 6-7 : Literal Lengths, FSE encoding method |
| 400 | |
| 401 | FSE encoding method : |
| 402 | FSE_ENCODING_RAW : uncompressed; no header |
| 403 | FSE_ENCODING_RLE : single repeated value; header 1 byte |
| 404 | FSE_ENCODING_STATIC : use prepared table; no header |
| 405 | FSE_ENCODING_DYNAMIC : read NCount |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 406 | */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 407 | |
| 408 | size_t ZSTD_noCompressBlock (void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 409 | { |
| 410 | BYTE* const ostart = (BYTE* const)dst; |
| 411 | |
| 412 | if (srcSize + ZSTD_blockHeaderSize > maxDstSize) return ERROR(dstSize_tooSmall); |
| 413 | memcpy(ostart + ZSTD_blockHeaderSize, src, srcSize); |
| 414 | |
| 415 | /* Build header */ |
| 416 | ostart[0] = (BYTE)(srcSize>>16); |
| 417 | ostart[1] = (BYTE)(srcSize>>8); |
| 418 | ostart[2] = (BYTE) srcSize; |
| 419 | ostart[0] += (BYTE)(bt_raw<<6); /* is a raw (uncompressed) block */ |
| 420 | |
| 421 | return ZSTD_blockHeaderSize+srcSize; |
| 422 | } |
| 423 | |
| 424 | |
| 425 | static size_t ZSTD_noCompressLiterals (void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 426 | { |
| 427 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 428 | const U32 flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 429 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 430 | if (srcSize + flSize > maxDstSize) return ERROR(dstSize_tooSmall); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 431 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 432 | switch(flSize) |
| 433 | { |
| 434 | case 1: /* 2 - 1 - 5 */ |
| 435 | ostart[0] = (BYTE)((IS_RAW<<6) + (0<<5) + srcSize); |
| 436 | break; |
| 437 | case 2: /* 2 - 2 - 12 */ |
| 438 | ostart[0] = (BYTE)((IS_RAW<<6) + (2<<4) + (srcSize >> 8)); |
| 439 | ostart[1] = (BYTE)srcSize; |
| 440 | break; |
| 441 | default: /*note : should not be necessary : flSize is within {1,2,3} */ |
| 442 | case 3: /* 2 - 2 - 20 */ |
| 443 | ostart[0] = (BYTE)((IS_RAW<<6) + (3<<4) + (srcSize >> 16)); |
| 444 | ostart[1] = (BYTE)(srcSize>>8); |
| 445 | ostart[2] = (BYTE)srcSize; |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | memcpy(ostart + flSize, src, srcSize); |
| 450 | return srcSize + flSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | static size_t ZSTD_compressRleLiteralsBlock (void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 454 | { |
| 455 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 456 | U32 flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 457 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 458 | (void)maxDstSize; /* maxDstSize guaranteed to be >=4, hence large enough */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 459 | |
| 460 | switch(flSize) |
| 461 | { |
| 462 | case 1: /* 2 - 1 - 5 */ |
| 463 | ostart[0] = (BYTE)((IS_RLE<<6) + (0<<5) + srcSize); |
| 464 | break; |
| 465 | case 2: /* 2 - 2 - 12 */ |
| 466 | ostart[0] = (BYTE)((IS_RLE<<6) + (2<<4) + (srcSize >> 8)); |
| 467 | ostart[1] = (BYTE)srcSize; |
| 468 | break; |
| 469 | default: /*note : should not be necessary : flSize is necessary within {1,2,3} */ |
| 470 | case 3: /* 2 - 2 - 20 */ |
| 471 | ostart[0] = (BYTE)((IS_RLE<<6) + (3<<4) + (srcSize >> 16)); |
| 472 | ostart[1] = (BYTE)(srcSize>>8); |
| 473 | ostart[2] = (BYTE)srcSize; |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | ostart[flSize] = *(const BYTE*)src; |
| 478 | return flSize+1; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 479 | } |
| 480 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 481 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 482 | size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 2; } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 483 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 484 | static size_t ZSTD_compressLiterals (ZSTD_CCtx* zc, |
| 485 | void* dst, size_t maxDstSize, |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 486 | const void* src, size_t srcSize) |
| 487 | { |
| 488 | const size_t minGain = ZSTD_minGain(srcSize); |
| 489 | BYTE* const ostart = (BYTE*)dst; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 490 | const size_t lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB); |
Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 491 | U32 singleStream = srcSize < 256; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 492 | U32 hType = IS_HUF; |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 493 | size_t clitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 494 | |
Yann Collet | 35f7de5 | 2016-01-31 02:51:03 +0100 | [diff] [blame] | 495 | if (maxDstSize < lhSize+1) return ERROR(dstSize_tooSmall); /* not enough space for compression */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 496 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 497 | if (zc->flagStaticTables && (lhSize==3)) { |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 498 | hType = IS_PCH; |
| 499 | singleStream = 1; |
| 500 | clitSize = HUF_compress1X_usingCTable(ostart+lhSize, maxDstSize-lhSize, src, srcSize, zc->hufTable); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 501 | } else { |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 502 | clitSize = singleStream ? HUF_compress1X(ostart+lhSize, maxDstSize-lhSize, src, srcSize, 255, 12) |
| 503 | : HUF_compress2 (ostart+lhSize, maxDstSize-lhSize, src, srcSize, 255, 12); |
| 504 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 505 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 506 | if ((clitSize==0) || (clitSize >= srcSize - minGain)) return ZSTD_noCompressLiterals(dst, maxDstSize, src, srcSize); |
| 507 | if (clitSize==1) return ZSTD_compressRleLiteralsBlock(dst, maxDstSize, src, srcSize); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 508 | |
| 509 | /* Build header */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 510 | switch(lhSize) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 511 | { |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 512 | case 3: /* 2 - 2 - 10 - 10 */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 513 | ostart[0] = (BYTE)((srcSize>>6) + (singleStream << 4) + (hType<<6)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 514 | ostart[1] = (BYTE)((srcSize<<2) + (clitSize>>8)); |
| 515 | ostart[2] = (BYTE)(clitSize); |
| 516 | break; |
| 517 | case 4: /* 2 - 2 - 14 - 14 */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 518 | ostart[0] = (BYTE)((srcSize>>10) + (2<<4) + (hType<<6)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 519 | ostart[1] = (BYTE)(srcSize>> 2); |
| 520 | ostart[2] = (BYTE)((srcSize<<6) + (clitSize>>8)); |
| 521 | ostart[3] = (BYTE)(clitSize); |
| 522 | break; |
| 523 | default: /* should not be necessary, lhSize is {3,4,5} */ |
| 524 | case 5: /* 2 - 2 - 18 - 18 */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 525 | ostart[0] = (BYTE)((srcSize>>14) + (3<<4) + (hType<<6)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 526 | ostart[1] = (BYTE)(srcSize>>6); |
| 527 | ostart[2] = (BYTE)((srcSize<<2) + (clitSize>>16)); |
| 528 | ostart[3] = (BYTE)(clitSize>>8); |
| 529 | ostart[4] = (BYTE)(clitSize); |
| 530 | break; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 533 | return lhSize+clitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | |
Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 537 | #define LITERAL_NOENTROPY 63 /* don't even attempt to compress literals below this threshold (cheap heuristic) */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 538 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 539 | size_t ZSTD_compressSequences(ZSTD_CCtx* zc, |
| 540 | void* dst, size_t maxDstSize, |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 541 | size_t srcSize) |
| 542 | { |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 543 | const seqStore_t* seqStorePtr = &(zc->seqStore); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 544 | U32 count[MaxSeq+1]; |
| 545 | S16 norm[MaxSeq+1]; |
| 546 | size_t mostFrequent; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 547 | U32 max; |
| 548 | FSE_CTable* CTable_LitLength = zc->litlengthCTable; |
| 549 | FSE_CTable* CTable_OffsetBits = zc->offcodeCTable; |
| 550 | FSE_CTable* CTable_MatchLength = zc->matchlengthCTable; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 551 | U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */ |
| 552 | const BYTE* const op_lit_start = seqStorePtr->litStart; |
| 553 | const BYTE* const llTable = seqStorePtr->litLengthStart; |
| 554 | const BYTE* const llPtr = seqStorePtr->litLength; |
| 555 | const BYTE* const mlTable = seqStorePtr->matchLengthStart; |
| 556 | const U32* const offsetTable = seqStorePtr->offsetStart; |
| 557 | BYTE* const offCodeTable = seqStorePtr->offCodeStart; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 558 | BYTE* const ostart = (BYTE*)dst; |
| 559 | BYTE* op = ostart; |
| 560 | BYTE* const oend = ostart + maxDstSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 561 | const size_t nbSeq = llPtr - llTable; |
| 562 | const size_t minGain = ZSTD_minGain(srcSize); |
| 563 | const size_t maxCSize = srcSize - minGain; |
| 564 | BYTE* seqHead; |
| 565 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 566 | /* Compress literals */ |
| 567 | { |
| 568 | size_t cSize; |
| 569 | size_t litSize = seqStorePtr->lit - op_lit_start; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 570 | const size_t minLitSize = zc->flagStaticTables ? 6 : LITERAL_NOENTROPY; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 571 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 572 | if (litSize <= minLitSize) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 573 | cSize = ZSTD_noCompressLiterals(op, maxDstSize, op_lit_start, litSize); |
| 574 | else |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 575 | cSize = ZSTD_compressLiterals(zc, op, maxDstSize, op_lit_start, litSize); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 576 | if (ZSTD_isError(cSize)) return cSize; |
| 577 | op += cSize; |
| 578 | } |
| 579 | |
| 580 | /* Sequences Header */ |
Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 581 | if ((oend-op) < MIN_SEQUENCES_SIZE) return ERROR(dstSize_tooSmall); |
| 582 | if (nbSeq < 128) *op++ = (BYTE)nbSeq; |
| 583 | else { |
Yann Collet | 35f7de5 | 2016-01-31 02:51:03 +0100 | [diff] [blame] | 584 | op[0] = (BYTE)((nbSeq>>8) + 128); op[1] = (BYTE)nbSeq; op+=2; |
Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 585 | } |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 586 | if (nbSeq==0) goto _check_compressibility; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 587 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 588 | /* dumps : contains rests of large lengths */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 589 | if ((oend-op) < 3 /* dumps */ + 1 /*seqHead*/) |
| 590 | return ERROR(dstSize_tooSmall); |
| 591 | seqHead = op; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 592 | { |
| 593 | size_t dumpsLength = seqStorePtr->dumps - seqStorePtr->dumpsStart; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 594 | if (dumpsLength < 512) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 595 | op[0] = (BYTE)(dumpsLength >> 8); |
| 596 | op[1] = (BYTE)(dumpsLength); |
| 597 | op += 2; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 598 | } else { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 599 | op[0] = 2; |
| 600 | op[1] = (BYTE)(dumpsLength>>8); |
| 601 | op[2] = (BYTE)(dumpsLength); |
| 602 | op += 3; |
| 603 | } |
| 604 | if ((size_t)(oend-op) < dumpsLength+6) return ERROR(dstSize_tooSmall); |
| 605 | memcpy(op, seqStorePtr->dumpsStart, dumpsLength); |
| 606 | op += dumpsLength; |
| 607 | } |
| 608 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 609 | #define MIN_SEQ_FOR_DYNAMIC_FSE 64 |
| 610 | #define MAX_SEQ_FOR_STATIC_FSE 1000 |
| 611 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 612 | /* CTable for Literal Lengths */ |
| 613 | max = MaxLL; |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 614 | mostFrequent = FSE_countFast(count, &max, llTable, nbSeq); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 615 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 616 | *op++ = llTable[0]; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 617 | FSE_buildCTable_rle(CTable_LitLength, (BYTE)max); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 618 | LLtype = FSE_ENCODING_RLE; |
| 619 | } else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
| 620 | LLtype = FSE_ENCODING_STATIC; |
| 621 | } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (LLbits-1)))) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 622 | FSE_buildCTable_raw(CTable_LitLength, LLbits); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 623 | LLtype = FSE_ENCODING_RAW; |
| 624 | } else { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 625 | size_t NCountSize; |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 626 | size_t nbSeq_1 = nbSeq; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 627 | U32 tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max); |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 628 | if (count[llTable[nbSeq-1]]>1) { count[llTable[nbSeq-1]]--; nbSeq_1--; } |
| 629 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 630 | NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ |
| 631 | if (FSE_isError(NCountSize)) return ERROR(GENERIC); |
| 632 | op += NCountSize; |
| 633 | FSE_buildCTable(CTable_LitLength, norm, max, tableLog); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 634 | LLtype = FSE_ENCODING_DYNAMIC; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 635 | } |
| 636 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 637 | /* CTable for Offset codes */ |
| 638 | { /* create Offset codes */ |
| 639 | size_t i; for (i=0; i<nbSeq; i++) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 640 | offCodeTable[i] = (BYTE)ZSTD_highbit(offsetTable[i]) + 1; |
| 641 | if (offsetTable[i]==0) offCodeTable[i]=0; |
| 642 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 643 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 644 | max = MaxOff; |
| 645 | mostFrequent = FSE_countFast(count, &max, offCodeTable, nbSeq); |
| 646 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 647 | *op++ = offCodeTable[0]; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 648 | FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 649 | Offtype = FSE_ENCODING_RLE; |
| 650 | } else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
| 651 | Offtype = FSE_ENCODING_STATIC; |
| 652 | } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (Offbits-1)))) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 653 | FSE_buildCTable_raw(CTable_OffsetBits, Offbits); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 654 | Offtype = FSE_ENCODING_RAW; |
| 655 | } else { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 656 | size_t NCountSize; |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 657 | size_t nbSeq_1 = nbSeq; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 658 | U32 tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max); |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 659 | if (count[offCodeTable[nbSeq-1]]>1) { count[offCodeTable[nbSeq-1]]--; nbSeq_1--; } |
| 660 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 661 | NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ |
| 662 | if (FSE_isError(NCountSize)) return ERROR(GENERIC); |
| 663 | op += NCountSize; |
| 664 | FSE_buildCTable(CTable_OffsetBits, norm, max, tableLog); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 665 | Offtype = FSE_ENCODING_DYNAMIC; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | /* CTable for MatchLengths */ |
| 669 | max = MaxML; |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 670 | mostFrequent = FSE_countFast(count, &max, mlTable, nbSeq); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 671 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 672 | *op++ = *mlTable; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 673 | FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 674 | MLtype = FSE_ENCODING_RLE; |
| 675 | } else if ((zc->flagStaticTables) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
| 676 | MLtype = FSE_ENCODING_STATIC; |
| 677 | } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (MLbits-1)))) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 678 | FSE_buildCTable_raw(CTable_MatchLength, MLbits); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 679 | MLtype = FSE_ENCODING_RAW; |
| 680 | } else { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 681 | size_t NCountSize; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 682 | U32 tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 683 | FSE_normalizeCount(norm, tableLog, count, nbSeq, max); |
| 684 | NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ |
| 685 | if (FSE_isError(NCountSize)) return ERROR(GENERIC); |
| 686 | op += NCountSize; |
| 687 | FSE_buildCTable(CTable_MatchLength, norm, max, tableLog); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 688 | MLtype = FSE_ENCODING_DYNAMIC; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | seqHead[0] += (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2)); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 692 | zc->flagStaticTables = 0; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 693 | |
| 694 | /* Encoding Sequences */ |
| 695 | { |
| 696 | size_t streamSize, errorCode; |
| 697 | BIT_CStream_t blockStream; |
| 698 | FSE_CState_t stateMatchLength; |
| 699 | FSE_CState_t stateOffsetBits; |
| 700 | FSE_CState_t stateLitLength; |
| 701 | int i; |
| 702 | |
| 703 | errorCode = BIT_initCStream(&blockStream, op, oend-op); |
| 704 | if (ERR_isError(errorCode)) return ERROR(dstSize_tooSmall); /* not enough space remaining */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 705 | |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 706 | /* first symbols */ |
| 707 | FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlTable[nbSeq-1]); |
| 708 | FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, offCodeTable[nbSeq-1]); |
| 709 | FSE_initCState2(&stateLitLength, CTable_LitLength, llTable[nbSeq-1]); |
| 710 | BIT_addBits(&blockStream, offsetTable[nbSeq-1], offCodeTable[nbSeq-1] ? (offCodeTable[nbSeq-1]-1) : 0); |
| 711 | BIT_flushBits(&blockStream); |
| 712 | |
| 713 | for (i=(int)nbSeq-2; i>=0; i--) { |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 714 | BYTE mlCode = mlTable[i]; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 715 | U32 offset = offsetTable[i]; |
| 716 | BYTE offCode = offCodeTable[i]; /* 32b*/ /* 64b*/ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 717 | U32 nbBits = (offCode-1) + (!offCode); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 718 | BYTE litLength = llTable[i]; /* (7)*/ /* (7)*/ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 719 | FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 17 */ /* 17 */ |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 720 | if (MEM_32bits()) BIT_flushBits(&blockStream); /* 7 */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 721 | FSE_encodeSymbol(&blockStream, &stateLitLength, litLength); /* 26 */ /* 61 */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 722 | FSE_encodeSymbol(&blockStream, &stateOffsetBits, offCode); /* 16 */ /* 51 */ |
| 723 | if (MEM_32bits()) BIT_flushBits(&blockStream); /* 7 */ |
| 724 | BIT_addBits(&blockStream, offset, nbBits); /* 31 */ /* 42 */ /* 24 bits max in 32-bits mode */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 725 | BIT_flushBits(&blockStream); /* 7 */ /* 7 */ |
| 726 | } |
| 727 | |
| 728 | FSE_flushCState(&blockStream, &stateMatchLength); |
| 729 | FSE_flushCState(&blockStream, &stateOffsetBits); |
| 730 | FSE_flushCState(&blockStream, &stateLitLength); |
| 731 | |
| 732 | streamSize = BIT_closeCStream(&blockStream); |
| 733 | if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */ |
| 734 | op += streamSize; |
| 735 | } |
| 736 | |
| 737 | /* check compressibility */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 738 | _check_compressibility: |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 739 | if ((size_t)(op-ostart) >= maxCSize) return 0; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 740 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 741 | return op - ostart; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 745 | /*! ZSTD_storeSeq |
| 746 | Store a sequence (literal length, literals, offset code and match length code) into seqStore_t |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 747 | @offsetCode : distance to match, or 0 == repCode |
| 748 | @matchCode : matchLength - MINMATCH |
| 749 | */ |
| 750 | MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const BYTE* literals, size_t offsetCode, size_t matchCode) |
| 751 | { |
Yann Collet | 7d8e6bd | 2016-02-02 17:30:37 +0100 | [diff] [blame] | 752 | #if 0 /* for debug */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 753 | static const BYTE* g_start = NULL; |
| 754 | if (g_start==NULL) g_start = literals; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 755 | //if (literals - g_start == 8695) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 756 | printf("pos %6u : %3u literals & match %3u bytes at distance %6u \n", |
| 757 | (U32)(literals - g_start), (U32)litLength, (U32)matchCode+4, (U32)offsetCode); |
| 758 | #endif |
| 759 | |
| 760 | /* copy Literals */ |
| 761 | ZSTD_wildcopy(seqStorePtr->lit, literals, litLength); |
| 762 | seqStorePtr->lit += litLength; |
| 763 | |
| 764 | /* literal Length */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 765 | if (litLength >= MaxLL) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 766 | *(seqStorePtr->litLength++) = MaxLL; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 767 | if (litLength<255 + MaxLL) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 768 | *(seqStorePtr->dumps++) = (BYTE)(litLength - MaxLL); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 769 | } else { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 770 | *(seqStorePtr->dumps++) = 255; |
Yann Collet | 7d8e6bd | 2016-02-02 17:30:37 +0100 | [diff] [blame] | 771 | if (litLength < (1<<15)) { |
| 772 | MEM_writeLE16(seqStorePtr->dumps, (U16)(litLength<<1)); |
| 773 | seqStorePtr->dumps += 2; |
| 774 | } else { |
| 775 | MEM_writeLE32(seqStorePtr->dumps, (U32)((litLength<<1)+1)); |
| 776 | seqStorePtr->dumps += 3; |
| 777 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 778 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 779 | else *(seqStorePtr->litLength++) = (BYTE)litLength; |
| 780 | |
| 781 | /* match offset */ |
| 782 | *(seqStorePtr->offset++) = (U32)offsetCode; |
| 783 | |
| 784 | /* match Length */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 785 | if (matchCode >= MaxML) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 786 | *(seqStorePtr->matchLength++) = MaxML; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 787 | if (matchCode < 255+MaxML) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 788 | *(seqStorePtr->dumps++) = (BYTE)(matchCode - MaxML); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 789 | } else { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 790 | *(seqStorePtr->dumps++) = 255; |
Yann Collet | 7d8e6bd | 2016-02-02 17:30:37 +0100 | [diff] [blame] | 791 | if (matchCode < (1<<15)) { |
| 792 | MEM_writeLE16(seqStorePtr->dumps, (U16)(matchCode<<1)); |
| 793 | seqStorePtr->dumps += 2; |
| 794 | } else { |
| 795 | MEM_writeLE32(seqStorePtr->dumps, (U32)((matchCode<<1)+1)); |
| 796 | seqStorePtr->dumps += 3; |
| 797 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 798 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 799 | else *(seqStorePtr->matchLength++) = (BYTE)matchCode; |
| 800 | } |
| 801 | |
| 802 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 803 | /* ************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 804 | * Match length counter |
| 805 | ***************************************/ |
| 806 | static size_t ZSTD_read_ARCH(const void* p) { size_t r; memcpy(&r, p, sizeof(r)); return r; } |
| 807 | |
| 808 | static unsigned ZSTD_highbit(U32 val) |
| 809 | { |
| 810 | # if defined(_MSC_VER) /* Visual */ |
| 811 | unsigned long r=0; |
| 812 | _BitScanReverse(&r, val); |
| 813 | return (unsigned)r; |
| 814 | # elif defined(__GNUC__) && (__GNUC__ >= 3) /* GCC Intrinsic */ |
| 815 | return 31 - __builtin_clz(val); |
| 816 | # else /* Software version */ |
| 817 | static const int DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; |
| 818 | U32 v = val; |
| 819 | int r; |
| 820 | v |= v >> 1; |
| 821 | v |= v >> 2; |
| 822 | v |= v >> 4; |
| 823 | v |= v >> 8; |
| 824 | v |= v >> 16; |
| 825 | r = DeBruijnClz[(U32)(v * 0x07C4ACDDU) >> 27]; |
| 826 | return r; |
| 827 | # endif |
| 828 | } |
| 829 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 830 | static unsigned ZSTD_NbCommonBytes (register size_t val) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 831 | { |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 832 | if (MEM_isLittleEndian()) { |
| 833 | if (MEM_64bits()) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 834 | # if defined(_MSC_VER) && defined(_WIN64) |
| 835 | unsigned long r = 0; |
| 836 | _BitScanForward64( &r, (U64)val ); |
Yann Collet | d608088 | 2015-12-09 09:05:22 +0100 | [diff] [blame] | 837 | return (unsigned)(r>>3); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 838 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 839 | return (__builtin_ctzll((U64)val) >> 3); |
| 840 | # else |
| 841 | static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, 0, 3, 1, 3, 1, 4, 2, 7, 0, 2, 3, 6, 1, 5, 3, 5, 1, 3, 4, 4, 2, 5, 6, 7, 7, 0, 1, 2, 3, 3, 4, 6, 2, 6, 5, 5, 3, 4, 5, 6, 7, 1, 2, 4, 6, 4, 4, 5, 7, 2, 6, 5, 7, 6, 7, 7 }; |
| 842 | return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; |
| 843 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 844 | } else { /* 32 bits */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 845 | # if defined(_MSC_VER) |
| 846 | unsigned long r=0; |
| 847 | _BitScanForward( &r, (U32)val ); |
Yann Collet | d608088 | 2015-12-09 09:05:22 +0100 | [diff] [blame] | 848 | return (unsigned)(r>>3); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 849 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 850 | return (__builtin_ctz((U32)val) >> 3); |
| 851 | # else |
| 852 | static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, 3, 2, 2, 1, 3, 2, 0, 1, 3, 3, 1, 2, 2, 2, 2, 0, 3, 1, 2, 0, 1, 0, 1, 1 }; |
| 853 | return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; |
| 854 | # endif |
| 855 | } |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 856 | } else { /* Big Endian CPU */ |
| 857 | if (MEM_64bits()) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 858 | # if defined(_MSC_VER) && defined(_WIN64) |
| 859 | unsigned long r = 0; |
| 860 | _BitScanReverse64( &r, val ); |
| 861 | return (unsigned)(r>>3); |
| 862 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 863 | return (__builtin_clzll(val) >> 3); |
| 864 | # else |
| 865 | unsigned r; |
| 866 | const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */ |
| 867 | if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; } |
| 868 | if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } |
| 869 | r += (!val); |
| 870 | return r; |
| 871 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 872 | } else { /* 32 bits */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 873 | # if defined(_MSC_VER) |
| 874 | unsigned long r = 0; |
| 875 | _BitScanReverse( &r, (unsigned long)val ); |
| 876 | return (unsigned)(r>>3); |
| 877 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 878 | return (__builtin_clz((U32)val) >> 3); |
| 879 | # else |
| 880 | unsigned r; |
| 881 | if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } |
| 882 | r += (!val); |
| 883 | return r; |
| 884 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 885 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 889 | static size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* pInLimit) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 890 | { |
| 891 | const BYTE* const pStart = pIn; |
| 892 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 893 | while ((pIn<pInLimit-(sizeof(size_t)-1))) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 894 | size_t diff = ZSTD_read_ARCH(pMatch) ^ ZSTD_read_ARCH(pIn); |
| 895 | if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; } |
| 896 | pIn += ZSTD_NbCommonBytes(diff); |
| 897 | return (size_t)(pIn - pStart); |
| 898 | } |
| 899 | |
| 900 | if (MEM_64bits()) if ((pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; } |
| 901 | if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; } |
| 902 | if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++; |
| 903 | return (size_t)(pIn - pStart); |
| 904 | } |
| 905 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 906 | /** ZSTD_count_2segments |
| 907 | * can count match length with ip & match in potentially 2 different segments. |
| 908 | * convention : on reaching mEnd, match count continue starting from iStart |
| 909 | */ |
| 910 | static size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart) |
| 911 | { |
| 912 | size_t matchLength; |
| 913 | const BYTE* vEnd = ip + (mEnd - match); |
| 914 | if (vEnd > iEnd) vEnd = iEnd; |
| 915 | matchLength = ZSTD_count(ip, match, vEnd); |
| 916 | if (match + matchLength == mEnd) |
| 917 | matchLength += ZSTD_count(ip+matchLength, iStart, iEnd); |
| 918 | return matchLength; |
| 919 | } |
| 920 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 921 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 922 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 923 | * Hashes |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 924 | ***************************************/ |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 925 | static const U32 prime4bytes = 2654435761U; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 926 | static U32 ZSTD_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; } |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 927 | static size_t ZSTD_hash4Ptr(const void* ptr, U32 h) { return ZSTD_hash4(MEM_read32(ptr), h); } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 928 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 929 | static const U64 prime5bytes = 889523592379ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 930 | static size_t ZSTD_hash5(U64 u, U32 h) { return (size_t)(((u << (64-40)) * prime5bytes) >> (64-h)) ; } |
Yann Collet | 4f0a393 | 2016-02-07 04:00:27 +0100 | [diff] [blame] | 931 | static size_t ZSTD_hash5Ptr(const void* p, U32 h) { return ZSTD_hash5(MEM_readLE64(p), h); } |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 932 | |
| 933 | static const U64 prime6bytes = 227718039650203ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 934 | static size_t ZSTD_hash6(U64 u, U32 h) { return (size_t)(((u << (64-48)) * prime6bytes) >> (64-h)) ; } |
Yann Collet | 4f0a393 | 2016-02-07 04:00:27 +0100 | [diff] [blame] | 935 | static size_t ZSTD_hash6Ptr(const void* p, U32 h) { return ZSTD_hash6(MEM_readLE64(p), h); } |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 936 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 937 | static const U64 prime7bytes = 58295818150454627ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 938 | static size_t ZSTD_hash7(U64 u, U32 h) { return (size_t)(((u << (64-56)) * prime7bytes) >> (64-h)) ; } |
Yann Collet | 4f0a393 | 2016-02-07 04:00:27 +0100 | [diff] [blame] | 939 | static size_t ZSTD_hash7Ptr(const void* p, U32 h) { return ZSTD_hash7(MEM_readLE64(p), h); } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 940 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 941 | static size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls) |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 942 | { |
| 943 | switch(mls) |
| 944 | { |
| 945 | default: |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 946 | case 4: return ZSTD_hash4Ptr(p, hBits); |
| 947 | case 5: return ZSTD_hash5Ptr(p, hBits); |
| 948 | case 6: return ZSTD_hash6Ptr(p, hBits); |
| 949 | case 7: return ZSTD_hash7Ptr(p, hBits); |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 950 | } |
| 951 | } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 952 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 953 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 954 | /*-************************************* |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 955 | * Fast Scan |
| 956 | ***************************************/ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 957 | #define FILLHASHSTEP 3 |
| 958 | static void ZSTD_fillHashTable (ZSTD_CCtx* zc, const void* end, const U32 mls) |
| 959 | { |
| 960 | U32* const hashTable = zc->hashTable; |
| 961 | const U32 hBits = zc->params.hashLog; |
| 962 | const BYTE* const base = zc->base; |
| 963 | const BYTE* ip = base + zc->nextToUpdate; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 964 | const BYTE* const iend = ((const BYTE*)end) - 8; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 965 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 966 | while(ip <= iend) { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 967 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base); |
| 968 | ip += FILLHASHSTEP; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 973 | FORCE_INLINE |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 974 | void ZSTD_compressBlock_fast_generic(ZSTD_CCtx* zc, |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 975 | const void* src, size_t srcSize, |
| 976 | const U32 mls) |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 977 | { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 978 | U32* const hashTable = zc->hashTable; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 979 | const U32 hBits = zc->params.hashLog; |
| 980 | seqStore_t* seqStorePtr = &(zc->seqStore); |
| 981 | const BYTE* const base = zc->base; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 982 | const BYTE* const istart = (const BYTE*)src; |
Yann Collet | 805a52a | 2015-11-06 10:52:17 +0100 | [diff] [blame] | 983 | const BYTE* ip = istart; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 984 | const BYTE* anchor = istart; |
Yann Collet | d94efbf | 2015-12-29 14:29:08 +0100 | [diff] [blame] | 985 | const U32 lowIndex = zc->dictLimit; |
| 986 | const BYTE* const lowest = base + lowIndex; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 987 | const BYTE* const iend = istart + srcSize; |
| 988 | const BYTE* const ilimit = iend - 8; |
| 989 | |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 990 | size_t offset_2=REPCODE_STARTVALUE, offset_1=REPCODE_STARTVALUE; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 991 | |
| 992 | |
| 993 | /* init */ |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 994 | ZSTD_resetSeqStore(seqStorePtr); |
Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 995 | if (ip < lowest+REPCODE_STARTVALUE) ip = lowest+REPCODE_STARTVALUE; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 996 | |
| 997 | /* Main Search Loop */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 998 | while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 999 | size_t mlCode; |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1000 | size_t offset; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1001 | const size_t h = ZSTD_hashPtr(ip, hBits, mls); |
Yann Collet | d94efbf | 2015-12-29 14:29:08 +0100 | [diff] [blame] | 1002 | const U32 matchIndex = hashTable[h]; |
| 1003 | const BYTE* match = base + matchIndex; |
Yann Collet | 96ffa42 | 2016-01-02 01:16:28 +0100 | [diff] [blame] | 1004 | const U32 current = (U32)(ip-base); |
| 1005 | hashTable[h] = current; /* update hash table */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1006 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1007 | if (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1)) { /* note : by construction, offset_1 <= current */ |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1008 | mlCode = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend); |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1009 | ip++; |
| 1010 | offset = 0; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1011 | } else { |
Yann Collet | d94efbf | 2015-12-29 14:29:08 +0100 | [diff] [blame] | 1012 | if ( (matchIndex <= lowIndex) || |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1013 | (MEM_read32(match) != MEM_read32(ip)) ) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1014 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1015 | continue; |
| 1016 | } |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1017 | mlCode = ZSTD_count(ip+MINMATCH, match+MINMATCH, iend); |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1018 | offset = ip-match; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1019 | while ((ip>anchor) && (match>lowest) && (ip[-1] == match[-1])) { ip--; match--; mlCode++; } /* catch up */ |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1020 | offset_2 = offset_1; |
| 1021 | offset_1 = offset; |
| 1022 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1023 | |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1024 | /* match found */ |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1025 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset, mlCode); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1026 | ip += mlCode + MINMATCH; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1027 | anchor = ip; |
| 1028 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1029 | if (ip <= ilimit) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1030 | /* Fill Table */ |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1031 | hashTable[ZSTD_hashPtr(base+current+2, hBits, mls)] = current+2; /* here because current+2 could be > iend-8 */ |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1032 | hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); |
| 1033 | /* check immediate repcode */ |
| 1034 | while ( (ip <= ilimit) |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1035 | && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1036 | /* store sequence */ |
Yann Collet | 06eade5 | 2015-11-23 14:23:47 +0100 | [diff] [blame] | 1037 | size_t rlCode = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend); |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1038 | size_t tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; /* swap offset_2 <=> offset_1 */ |
| 1039 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base); |
Yann Collet | 06eade5 | 2015-11-23 14:23:47 +0100 | [diff] [blame] | 1040 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rlCode); |
| 1041 | ip += rlCode+MINMATCH; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1042 | anchor = ip; |
| 1043 | continue; /* faster when present ... (?) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1044 | } } } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1045 | |
Yann Collet | 4488661 | 2016-02-11 04:17:50 +0100 | [diff] [blame] | 1046 | { /* Last Literals */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1047 | size_t lastLLSize = iend - anchor; |
| 1048 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1049 | seqStorePtr->lit += lastLLSize; |
| 1050 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1054 | void ZSTD_compressBlock_fast(ZSTD_CCtx* ctx, |
| 1055 | const void* src, size_t srcSize) |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1056 | { |
| 1057 | const U32 mls = ctx->params.searchLength; |
| 1058 | switch(mls) |
| 1059 | { |
| 1060 | default: |
| 1061 | case 4 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1062 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 4); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1063 | case 5 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1064 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 5); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1065 | case 6 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1066 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 6); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1067 | case 7 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1068 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 7); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1069 | } |
| 1070 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1071 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1072 | |
Yann Collet | 93a823c | 2015-11-13 15:08:43 +0100 | [diff] [blame] | 1073 | //FORCE_INLINE |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1074 | void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx, |
| 1075 | const void* src, size_t srcSize, |
| 1076 | const U32 mls) |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1077 | { |
| 1078 | U32* hashTable = ctx->hashTable; |
| 1079 | const U32 hBits = ctx->params.hashLog; |
| 1080 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 1081 | const BYTE* const base = ctx->base; |
| 1082 | const BYTE* const dictBase = ctx->dictBase; |
| 1083 | const BYTE* const istart = (const BYTE*)src; |
| 1084 | const BYTE* ip = istart; |
| 1085 | const BYTE* anchor = istart; |
| 1086 | const U32 lowLimit = ctx->lowLimit; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1087 | const BYTE* const dictStart = dictBase + lowLimit; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1088 | const U32 dictLimit = ctx->dictLimit; |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1089 | const BYTE* const lowPrefixPtr = base + dictLimit; |
| 1090 | const BYTE* const dictEnd = dictBase + dictLimit; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1091 | const BYTE* const iend = istart + srcSize; |
| 1092 | const BYTE* const ilimit = iend - 8; |
| 1093 | |
Yann Collet | 138e89c | 2015-11-17 14:26:54 +0100 | [diff] [blame] | 1094 | U32 offset_2=REPCODE_STARTVALUE, offset_1=REPCODE_STARTVALUE; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1095 | |
| 1096 | |
| 1097 | /* init */ |
| 1098 | ZSTD_resetSeqStore(seqStorePtr); |
Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 1099 | /* skip first position to avoid read overflow during repcode match check */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1100 | hashTable[ZSTD_hashPtr(ip+0, hBits, mls)] = (U32)(ip-base+0); |
Yann Collet | 61e16ce | 2016-01-31 02:04:15 +0100 | [diff] [blame] | 1101 | ip += REPCODE_STARTVALUE; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1102 | |
| 1103 | /* Main Search Loop */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1104 | while (ip < ilimit) { /* < instead of <=, because (ip+1) */ |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1105 | const size_t h = ZSTD_hashPtr(ip, hBits, mls); |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1106 | const U32 matchIndex = hashTable[h]; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1107 | const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base; |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1108 | const BYTE* match = matchBase + matchIndex; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1109 | const U32 current = (U32)(ip-base); |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1110 | const U32 repIndex = current + 1 - offset_1; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1111 | const BYTE* repBase = repIndex < dictLimit ? dictBase : base; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1112 | const BYTE* repMatch = repBase + repIndex; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1113 | size_t mlCode; |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1114 | U32 offset; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1115 | hashTable[h] = current; /* update hash table */ |
| 1116 | |
| 1117 | if ( ((repIndex <= dictLimit-4) || (repIndex >= dictLimit)) |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1118 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1119 | const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1120 | mlCode = ZSTD_count_2segments(ip+1+MINMATCH, repMatch+MINMATCH, iend, repMatchEnd, lowPrefixPtr); |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1121 | ip++; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1122 | offset = 0; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1123 | } else { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1124 | if ( (matchIndex < lowLimit) || |
| 1125 | (MEM_read32(match) != MEM_read32(ip)) ) |
| 1126 | { ip += ((ip-anchor) >> g_searchStrength) + 1; continue; } |
| 1127 | { |
| 1128 | const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1129 | const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr; |
| 1130 | mlCode = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iend, matchEnd, lowPrefixPtr); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1131 | while ((ip>anchor) && (match>lowMatchPtr) && (ip[-1] == match[-1])) { ip--; match--; mlCode++; } /* catch up */ |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1132 | offset = current - matchIndex; |
| 1133 | offset_2 = offset_1; |
| 1134 | offset_1 = offset; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1135 | } } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1136 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1137 | /* found a match : store it */ |
| 1138 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset, mlCode); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1139 | ip += mlCode + MINMATCH; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1140 | anchor = ip; |
| 1141 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1142 | if (ip <= ilimit) { |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1143 | /* Fill Table */ |
Yann Collet | 239cc28 | 2015-11-23 16:17:21 +0100 | [diff] [blame] | 1144 | hashTable[ZSTD_hashPtr(base+current+2, hBits, mls)] = current+2; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1145 | hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); |
| 1146 | /* check immediate repcode */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1147 | while (ip <= ilimit) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1148 | U32 current2 = (U32)(ip-base); |
| 1149 | const U32 repIndex2 = current2 - offset_2; |
| 1150 | const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1151 | if ( ((repIndex2 <= dictLimit-4) || (repIndex2 >= dictLimit)) |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1152 | && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1153 | const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend; |
| 1154 | size_t repLength2 = ZSTD_count_2segments(ip+MINMATCH, repMatch2+MINMATCH, iend, repEnd2, lowPrefixPtr); |
| 1155 | U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ |
| 1156 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2); |
| 1157 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = current2; |
| 1158 | ip += repLength2+MINMATCH; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1159 | anchor = ip; |
| 1160 | continue; |
| 1161 | } |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1162 | break; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1163 | } } } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1164 | |
| 1165 | /* Last Literals */ |
| 1166 | { |
| 1167 | size_t lastLLSize = iend - anchor; |
| 1168 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1169 | seqStorePtr->lit += lastLLSize; |
| 1170 | } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1174 | void ZSTD_compressBlock_fast_extDict(ZSTD_CCtx* ctx, |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1175 | const void* src, size_t srcSize) |
| 1176 | { |
| 1177 | const U32 mls = ctx->params.searchLength; |
| 1178 | switch(mls) |
| 1179 | { |
| 1180 | default: |
| 1181 | case 4 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1182 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 4); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1183 | case 5 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1184 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 5); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1185 | case 6 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1186 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 6); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1187 | case 7 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1188 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 7); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1193 | /* ************************************* |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1194 | * Binary Tree search |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1195 | ***************************************/ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 1196 | /** ZSTD_insertBt1() : add one or multiple positions to tree |
| 1197 | * ip : assumed <= iend-8 |
Yann Collet | 06eade5 | 2015-11-23 14:23:47 +0100 | [diff] [blame] | 1198 | * @return : nb of positions added */ |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1199 | static U32 ZSTD_insertBt1(ZSTD_CCtx* zc, const BYTE* const ip, const U32 mls, const BYTE* const iend, U32 nbCompares, |
| 1200 | U32 extDict) |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1201 | { |
| 1202 | U32* const hashTable = zc->hashTable; |
| 1203 | const U32 hashLog = zc->params.hashLog; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1204 | const size_t h = ZSTD_hashPtr(ip, hashLog, mls); |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1205 | U32* const bt = zc->contentTable; |
| 1206 | const U32 btLog = zc->params.contentLog - 1; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1207 | const U32 btMask= (1 << btLog) - 1; |
| 1208 | U32 matchIndex = hashTable[h]; |
| 1209 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 1210 | const BYTE* const base = zc->base; |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1211 | const BYTE* const dictBase = zc->dictBase; |
| 1212 | const U32 dictLimit = zc->dictLimit; |
| 1213 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 1214 | const BYTE* const prefixStart = base + dictLimit; |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1215 | const BYTE* match = base + matchIndex; |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 1216 | const U32 current = (U32)(ip-base); |
Yann Collet | e9eba60 | 2015-11-08 15:08:03 +0100 | [diff] [blame] | 1217 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1218 | U32* smallerPtr = bt + 2*(current&btMask); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1219 | U32* largerPtr = smallerPtr + 1; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1220 | U32 dummy32; /* to be nullified at the end */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1221 | const U32 windowLow = zc->lowLimit; |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 1222 | U32 matchEndIdx = current+8; |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 1223 | U32 predictedSmall = *(bt + 2*((current-1)&btMask) + 0); |
| 1224 | U32 predictedLarge = *(bt + 2*((current-1)&btMask) + 1); |
| 1225 | predictedSmall += (predictedSmall>0); |
| 1226 | predictedLarge += (predictedLarge>0); |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1227 | |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 1228 | hashTable[h] = current; /* Update Hash Table */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1229 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1230 | while (nbCompares-- && (matchIndex > windowLow)) { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1231 | U32* nextPtr = bt + 2*(matchIndex & btMask); |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1232 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 1233 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 1234 | const U32* predictPtr = bt + 2*((matchIndex-1) & btMask); /* written this way, as bt is a roll buffer */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1235 | if (matchIndex == predictedSmall) { |
| 1236 | /* no need to check length, result known */ |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1237 | *smallerPtr = matchIndex; |
| 1238 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1239 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 1240 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 1241 | predictedSmall = predictPtr[1] + (predictPtr[1]>0); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1242 | continue; |
| 1243 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1244 | if (matchIndex == predictedLarge) { |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1245 | *largerPtr = matchIndex; |
| 1246 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1247 | largerPtr = nextPtr; |
| 1248 | matchIndex = nextPtr[0]; |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 1249 | predictedLarge = predictPtr[0] + (predictPtr[0]>0); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1250 | continue; |
| 1251 | } |
| 1252 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1253 | if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1254 | match = base + matchIndex; |
| 1255 | if (match[matchLength] == ip[matchLength]) |
| 1256 | matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1257 | } else { |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1258 | match = dictBase + matchIndex; |
| 1259 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
| 1260 | if (matchIndex+matchLength >= dictLimit) |
| 1261 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
| 1262 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1263 | |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1264 | if (matchLength > matchEndIdx - matchIndex) |
Yann Collet | 48da164 | 2015-12-29 23:40:02 +0100 | [diff] [blame] | 1265 | matchEndIdx = matchIndex + (U32)matchLength; |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1266 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1267 | if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1268 | break; /* drop , to guarantee consistency ; miss a bit of compression, but other solutions can corrupt the tree */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1269 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1270 | if (match[matchLength] < ip[matchLength]) { /* necessarily within correct buffer */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1271 | /* match is smaller than current */ |
| 1272 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 1273 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1274 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1275 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1276 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1277 | } else { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1278 | /* match is larger than current */ |
| 1279 | *largerPtr = matchIndex; |
| 1280 | commonLengthLarger = matchLength; |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1281 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1282 | largerPtr = nextPtr; |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1283 | matchIndex = nextPtr[0]; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1284 | } } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1285 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1286 | *smallerPtr = *largerPtr = 0; |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 1287 | return (matchEndIdx > current + 8) ? matchEndIdx - current - 8 : 1; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1291 | static void ZSTD_updateTree(ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iend, const U32 nbCompares, const U32 mls) |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1292 | { |
| 1293 | const BYTE* const base = zc->base; |
| 1294 | const U32 target = (U32)(ip - base); |
| 1295 | U32 idx = zc->nextToUpdate; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1296 | |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1297 | for( ; idx < target ; ) |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1298 | idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 0); |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1299 | } |
| 1300 | |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1301 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1302 | size_t ZSTD_insertBtAndFindBestMatch ( |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1303 | ZSTD_CCtx* zc, |
| 1304 | const BYTE* const ip, const BYTE* const iend, |
| 1305 | size_t* offsetPtr, |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1306 | U32 nbCompares, const U32 mls, |
| 1307 | U32 extDict) |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1308 | { |
| 1309 | U32* const hashTable = zc->hashTable; |
| 1310 | const U32 hashLog = zc->params.hashLog; |
| 1311 | const size_t h = ZSTD_hashPtr(ip, hashLog, mls); |
| 1312 | U32* const bt = zc->contentTable; |
| 1313 | const U32 btLog = zc->params.contentLog - 1; |
| 1314 | const U32 btMask= (1 << btLog) - 1; |
| 1315 | U32 matchIndex = hashTable[h]; |
| 1316 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 1317 | const BYTE* const base = zc->base; |
| 1318 | const BYTE* const dictBase = zc->dictBase; |
| 1319 | const U32 dictLimit = zc->dictLimit; |
| 1320 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 1321 | const BYTE* const prefixStart = base + dictLimit; |
| 1322 | const U32 current = (U32)(ip-base); |
| 1323 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
| 1324 | const U32 windowLow = zc->lowLimit; |
| 1325 | U32* smallerPtr = bt + 2*(current&btMask); |
| 1326 | U32* largerPtr = bt + 2*(current&btMask) + 1; |
| 1327 | size_t bestLength = 0; |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 1328 | U32 matchEndIdx = current+8; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1329 | U32 dummy32; /* to be nullified at the end */ |
| 1330 | |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 1331 | hashTable[h] = current; /* Update Hash Table */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1332 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1333 | while (nbCompares-- && (matchIndex > windowLow)) { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1334 | U32* nextPtr = bt + 2*(matchIndex & btMask); |
| 1335 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 1336 | const BYTE* match; |
| 1337 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1338 | if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1339 | match = base + matchIndex; |
| 1340 | if (match[matchLength] == ip[matchLength]) |
| 1341 | matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1342 | } else { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1343 | match = dictBase + matchIndex; |
| 1344 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
Yann Collet | 225179d | 2015-11-23 16:52:22 +0100 | [diff] [blame] | 1345 | if (matchIndex+matchLength >= dictLimit) |
| 1346 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1347 | } |
| 1348 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1349 | if (matchLength > bestLength) { |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1350 | if (matchLength > matchEndIdx - matchIndex) |
Yann Collet | 48da164 | 2015-12-29 23:40:02 +0100 | [diff] [blame] | 1351 | matchEndIdx = matchIndex + (U32)matchLength; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1352 | if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit(current-matchIndex+1) - ZSTD_highbit((U32)offsetPtr[0]+1)) ) |
| 1353 | bestLength = matchLength, *offsetPtr = current - matchIndex; |
| 1354 | if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ |
| 1355 | break; /* drop, to guarantee consistency (miss a little bit of compression) */ |
| 1356 | } |
| 1357 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1358 | if (match[matchLength] < ip[matchLength]) { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1359 | /* match is smaller than current */ |
| 1360 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 1361 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
| 1362 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1363 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 1364 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1365 | } else { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1366 | /* match is larger than current */ |
| 1367 | *largerPtr = matchIndex; |
| 1368 | commonLengthLarger = matchLength; |
| 1369 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1370 | largerPtr = nextPtr; |
| 1371 | matchIndex = nextPtr[0]; |
Yann Collet | 768c6bc | 2016-02-10 14:01:49 +0100 | [diff] [blame] | 1372 | } } |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1373 | |
| 1374 | *smallerPtr = *largerPtr = 0; |
| 1375 | |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 1376 | zc->nextToUpdate = (matchEndIdx > current + 8) ? matchEndIdx - 8 : current+1; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1377 | return bestLength; |
| 1378 | } |
| 1379 | |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1380 | |
| 1381 | /** Tree updater, providing best match */ |
| 1382 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
| 1383 | size_t ZSTD_BtFindBestMatch ( |
| 1384 | ZSTD_CCtx* zc, |
| 1385 | const BYTE* const ip, const BYTE* const iLimit, |
| 1386 | size_t* offsetPtr, |
| 1387 | const U32 maxNbAttempts, const U32 mls) |
| 1388 | { |
| 1389 | if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ |
| 1390 | ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls); |
| 1391 | return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 0); |
| 1392 | } |
| 1393 | |
| 1394 | |
Yann Collet | 768c6bc | 2016-02-10 14:01:49 +0100 | [diff] [blame] | 1395 | static size_t ZSTD_BtFindBestMatch_selectMLS ( |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1396 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 1397 | const BYTE* ip, const BYTE* const iLimit, |
| 1398 | size_t* offsetPtr, |
| 1399 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1400 | { |
| 1401 | switch(matchLengthSearch) |
| 1402 | { |
| 1403 | default : |
| 1404 | case 4 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 1405 | case 5 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
| 1406 | case 6 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
| 1407 | } |
| 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | static void ZSTD_updateTree_extDict(ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iend, const U32 nbCompares, const U32 mls) |
| 1412 | { |
| 1413 | const BYTE* const base = zc->base; |
| 1414 | const U32 target = (U32)(ip - base); |
| 1415 | U32 idx = zc->nextToUpdate; |
| 1416 | |
| 1417 | for( ; idx < target ; ) |
| 1418 | idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 1); |
| 1419 | } |
| 1420 | |
| 1421 | |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1422 | /** Tree updater, providing best match */ |
| 1423 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
| 1424 | size_t ZSTD_BtFindBestMatch_extDict ( |
| 1425 | ZSTD_CCtx* zc, |
| 1426 | const BYTE* const ip, const BYTE* const iLimit, |
| 1427 | size_t* offsetPtr, |
| 1428 | const U32 maxNbAttempts, const U32 mls) |
| 1429 | { |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1430 | if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ |
| 1431 | ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls); |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1432 | return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 1); |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
| 1435 | |
| 1436 | FORCE_INLINE size_t ZSTD_BtFindBestMatch_selectMLS_extDict ( |
| 1437 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 1438 | const BYTE* ip, const BYTE* const iLimit, |
| 1439 | size_t* offsetPtr, |
| 1440 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1441 | { |
| 1442 | switch(matchLengthSearch) |
| 1443 | { |
| 1444 | default : |
| 1445 | case 4 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 1446 | case 5 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
| 1447 | case 6 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1452 | /* *********************** |
| 1453 | * Hash Chain |
| 1454 | *************************/ |
| 1455 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1456 | #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & mask] |
| 1457 | |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1458 | /* Update chains up to ip (excluded) |
Yann Collet | 06eade5 | 2015-11-23 14:23:47 +0100 | [diff] [blame] | 1459 | Assumption : always within prefix (ie. not within extDict) */ |
| 1460 | static U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1461 | { |
| 1462 | U32* const hashTable = zc->hashTable; |
| 1463 | const U32 hashLog = zc->params.hashLog; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1464 | U32* const chainTable = zc->contentTable; |
| 1465 | const U32 chainMask = (1 << zc->params.contentLog) - 1; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1466 | const BYTE* const base = zc->base; |
| 1467 | const U32 target = (U32)(ip - base); |
| 1468 | U32 idx = zc->nextToUpdate; |
| 1469 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1470 | while(idx < target) { |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1471 | size_t h = ZSTD_hashPtr(base+idx, hashLog, mls); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1472 | NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; |
| 1473 | hashTable[h] = idx; |
| 1474 | idx++; |
| 1475 | } |
| 1476 | |
| 1477 | zc->nextToUpdate = target; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1478 | return hashTable[ZSTD_hashPtr(ip, hashLog, mls)]; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1479 | } |
| 1480 | |
| 1481 | |
| 1482 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
Yann Collet | c1e52f0 | 2015-11-23 14:37:59 +0100 | [diff] [blame] | 1483 | size_t ZSTD_HcFindBestMatch_generic ( |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1484 | ZSTD_CCtx* zc, /* Index table will be updated */ |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1485 | const BYTE* const ip, const BYTE* const iLimit, |
| 1486 | size_t* offsetPtr, |
Yann Collet | c1e52f0 | 2015-11-23 14:37:59 +0100 | [diff] [blame] | 1487 | const U32 maxNbAttempts, const U32 mls, const U32 extDict) |
Yann Collet | 287b7d9 | 2015-11-22 13:24:05 +0100 | [diff] [blame] | 1488 | { |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1489 | U32* const chainTable = zc->contentTable; |
| 1490 | const U32 chainSize = (1 << zc->params.contentLog); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1491 | const U32 chainMask = chainSize-1; |
| 1492 | const BYTE* const base = zc->base; |
| 1493 | const BYTE* const dictBase = zc->dictBase; |
| 1494 | const U32 dictLimit = zc->dictLimit; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1495 | const BYTE* const prefixStart = base + dictLimit; |
| 1496 | const BYTE* const dictEnd = dictBase + dictLimit; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1497 | const U32 lowLimit = zc->lowLimit; |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1498 | const U32 current = (U32)(ip-base); |
| 1499 | const U32 minChain = current > chainSize ? current - chainSize : 0; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1500 | U32 matchIndex; |
| 1501 | const BYTE* match; |
| 1502 | int nbAttempts=maxNbAttempts; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1503 | size_t ml=MINMATCH-1; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1504 | |
| 1505 | /* HC4 match finder */ |
Yann Collet | c1e52f0 | 2015-11-23 14:37:59 +0100 | [diff] [blame] | 1506 | matchIndex = ZSTD_insertAndFindFirstIndex (zc, ip, mls); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1507 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1508 | while ((matchIndex>lowLimit) && (nbAttempts)) { |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1509 | size_t currentMl=0; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1510 | nbAttempts--; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1511 | if ((!extDict) || matchIndex >= dictLimit) { |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1512 | match = base + matchIndex; |
Yann Collet | 4baee50 | 2015-11-09 03:19:33 +0100 | [diff] [blame] | 1513 | if (match[ml] == ip[ml]) /* potentially better */ |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1514 | currentMl = ZSTD_count(ip, match, iLimit); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1515 | } else { |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1516 | match = dictBase + matchIndex; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1517 | if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */ |
| 1518 | currentMl = ZSTD_count_2segments(ip+MINMATCH, match+MINMATCH, iLimit, dictEnd, prefixStart) + MINMATCH; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1521 | /* save best solution */ |
Yann Collet | 239cc28 | 2015-11-23 16:17:21 +0100 | [diff] [blame] | 1522 | if (currentMl > ml) { ml = currentMl; *offsetPtr = current - matchIndex; if (ip+currentMl == iLimit) break; /* best possible, and avoid read overflow*/ } |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1523 | |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1524 | if (matchIndex <= minChain) break; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1525 | matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask); |
| 1526 | } |
| 1527 | |
| 1528 | return ml; |
| 1529 | } |
| 1530 | |
| 1531 | |
Yann Collet | c1e52f0 | 2015-11-23 14:37:59 +0100 | [diff] [blame] | 1532 | FORCE_INLINE size_t ZSTD_HcFindBestMatch_selectMLS ( |
| 1533 | ZSTD_CCtx* zc, |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1534 | const BYTE* ip, const BYTE* const iLimit, |
| 1535 | size_t* offsetPtr, |
| 1536 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1537 | { |
| 1538 | switch(matchLengthSearch) |
| 1539 | { |
| 1540 | default : |
Yann Collet | c1e52f0 | 2015-11-23 14:37:59 +0100 | [diff] [blame] | 1541 | case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 0); |
| 1542 | case 5 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 0); |
| 1543 | case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 0); |
| 1544 | } |
| 1545 | } |
| 1546 | |
| 1547 | |
| 1548 | FORCE_INLINE size_t ZSTD_HcFindBestMatch_extDict_selectMLS ( |
| 1549 | ZSTD_CCtx* zc, |
| 1550 | const BYTE* ip, const BYTE* const iLimit, |
| 1551 | size_t* offsetPtr, |
| 1552 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1553 | { |
| 1554 | switch(matchLengthSearch) |
| 1555 | { |
| 1556 | default : |
| 1557 | case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 1); |
| 1558 | case 5 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 1); |
| 1559 | case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 1); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1560 | } |
| 1561 | } |
| 1562 | |
| 1563 | |
Yann Collet | 287b7d9 | 2015-11-22 13:24:05 +0100 | [diff] [blame] | 1564 | /* ******************************* |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1565 | * Common parser - lazy strategy |
Yann Collet | 287b7d9 | 2015-11-22 13:24:05 +0100 | [diff] [blame] | 1566 | *********************************/ |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1567 | FORCE_INLINE |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1568 | void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, |
| 1569 | const void* src, size_t srcSize, |
Yann Collet | 007c1c6 | 2015-11-22 02:42:28 +0100 | [diff] [blame] | 1570 | const U32 searchMethod, const U32 depth) |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1571 | { |
| 1572 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 1573 | const BYTE* const istart = (const BYTE*)src; |
| 1574 | const BYTE* ip = istart; |
| 1575 | const BYTE* anchor = istart; |
| 1576 | const BYTE* const iend = istart + srcSize; |
| 1577 | const BYTE* const ilimit = iend - 8; |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1578 | const BYTE* const base = ctx->base + ctx->dictLimit; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1579 | |
| 1580 | size_t offset_2=REPCODE_STARTVALUE, offset_1=REPCODE_STARTVALUE; |
| 1581 | const U32 maxSearches = 1 << ctx->params.searchLog; |
| 1582 | const U32 mls = ctx->params.searchLength; |
| 1583 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1584 | typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1585 | size_t* offsetPtr, |
| 1586 | U32 maxNbAttempts, U32 matchLengthSearch); |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1587 | searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1588 | |
| 1589 | /* init */ |
| 1590 | ZSTD_resetSeqStore(seqStorePtr); |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1591 | if ((ip-base) < REPCODE_STARTVALUE) ip = base + REPCODE_STARTVALUE; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1592 | |
| 1593 | /* Match Loop */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1594 | while (ip < ilimit) { |
Yann Collet | 7a23179 | 2015-11-21 15:27:35 +0100 | [diff] [blame] | 1595 | size_t matchLength=0; |
| 1596 | size_t offset=0; |
| 1597 | const BYTE* start=ip+1; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1598 | |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1599 | /* check repCode */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1600 | if (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1)) { |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1601 | /* repcode : we take it */ |
Yann Collet | 7a23179 | 2015-11-21 15:27:35 +0100 | [diff] [blame] | 1602 | matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend) + MINMATCH; |
Yann Collet | 007c1c6 | 2015-11-22 02:42:28 +0100 | [diff] [blame] | 1603 | if (depth==0) goto _storeSequence; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
Yann Collet | fc2afcf | 2015-11-06 15:40:14 +0100 | [diff] [blame] | 1606 | { |
Yann Collet | 239cc28 | 2015-11-23 16:17:21 +0100 | [diff] [blame] | 1607 | /* first search (depth 0) */ |
| 1608 | size_t offsetFound = 99999999; |
| 1609 | size_t ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); |
| 1610 | if (ml2 > matchLength) |
| 1611 | matchLength = ml2, start = ip, offset=offsetFound; |
| 1612 | } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1613 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1614 | if (matchLength < MINMATCH) { |
Yann Collet | 239cc28 | 2015-11-23 16:17:21 +0100 | [diff] [blame] | 1615 | ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ |
| 1616 | continue; |
| 1617 | } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1618 | |
| 1619 | /* let's try to find a better solution */ |
Yann Collet | 007c1c6 | 2015-11-22 02:42:28 +0100 | [diff] [blame] | 1620 | if (depth>=1) |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1621 | while (ip<ilimit) { |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1622 | ip ++; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1623 | if ((offset) && (MEM_read32(ip) == MEM_read32(ip - offset_1))) { |
Yann Collet | 007c1c6 | 2015-11-22 02:42:28 +0100 | [diff] [blame] | 1624 | size_t mlRep = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH; |
| 1625 | int gain2 = (int)(mlRep * 3); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1626 | int gain1 = (int)(matchLength*3 - ZSTD_highbit((U32)offset+1) + 1); |
Yann Collet | 007c1c6 | 2015-11-22 02:42:28 +0100 | [diff] [blame] | 1627 | if ((mlRep >= MINMATCH) && (gain2 > gain1)) |
| 1628 | matchLength = mlRep, offset = 0, start = ip; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1629 | } |
| 1630 | { |
| 1631 | size_t offset2=999999; |
| 1632 | size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 007c1c6 | 2015-11-22 02:42:28 +0100 | [diff] [blame] | 1633 | int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ |
| 1634 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1635 | if ((ml2 >= MINMATCH) && (gain2 > gain1)) { |
Yann Collet | 628065c | 2015-11-06 18:44:54 +0100 | [diff] [blame] | 1636 | matchLength = ml2, offset = offset2, start = ip; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1637 | continue; /* search a better one */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1638 | } } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* let's find an even better one */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1641 | if ((depth==2) && (ip<ilimit)) { |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1642 | ip ++; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1643 | if ((offset) && (MEM_read32(ip) == MEM_read32(ip - offset_1))) { |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1644 | size_t ml2 = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH; |
| 1645 | int gain2 = (int)(ml2 * 4); |
| 1646 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 1); |
Yann Collet | 4baee50 | 2015-11-09 03:19:33 +0100 | [diff] [blame] | 1647 | if ((ml2 >= MINMATCH) && (gain2 > gain1)) |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1648 | matchLength = ml2, offset = 0, start = ip; |
| 1649 | } |
| 1650 | { |
| 1651 | size_t offset2=999999; |
| 1652 | size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 628065c | 2015-11-06 18:44:54 +0100 | [diff] [blame] | 1653 | int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ |
| 1654 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1655 | if ((ml2 >= MINMATCH) && (gain2 > gain1)) { |
Yann Collet | 628065c | 2015-11-06 18:44:54 +0100 | [diff] [blame] | 1656 | matchLength = ml2, offset = offset2, start = ip; |
| 1657 | continue; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1658 | } } } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1659 | break; /* nothing found : store previous solution */ |
| 1660 | } |
| 1661 | |
Yann Collet | 768c6bc | 2016-02-10 14:01:49 +0100 | [diff] [blame] | 1662 | /* catch up */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1663 | if (offset) { |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 1664 | while ((start>anchor) && (start>base+offset) && (start[-1] == start[-1-offset])) /* only search for offset within prefix */ |
Yann Collet | 4baee50 | 2015-11-09 03:19:33 +0100 | [diff] [blame] | 1665 | { start--; matchLength++; } |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1666 | offset_2 = offset_1; offset_1 = offset; |
Yann Collet | 4baee50 | 2015-11-09 03:19:33 +0100 | [diff] [blame] | 1667 | } |
| 1668 | |
| 1669 | /* store sequence */ |
Yann Collet | 7a23179 | 2015-11-21 15:27:35 +0100 | [diff] [blame] | 1670 | _storeSequence: |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1671 | { |
| 1672 | size_t litLength = start - anchor; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1673 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH); |
Yann Collet | 4baee50 | 2015-11-09 03:19:33 +0100 | [diff] [blame] | 1674 | anchor = ip = start + matchLength; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1675 | } |
| 1676 | |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1677 | /* check immediate repcode */ |
| 1678 | while ( (ip <= ilimit) |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1679 | && (MEM_read32(ip) == MEM_read32(ip - offset_2)) ) { |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1680 | /* store sequence */ |
| 1681 | matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend); |
| 1682 | offset = offset_2; |
| 1683 | offset_2 = offset_1; |
| 1684 | offset_1 = offset; |
| 1685 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength); |
| 1686 | ip += matchLength+MINMATCH; |
| 1687 | anchor = ip; |
| 1688 | continue; /* faster when present ... (?) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1689 | } } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1690 | |
| 1691 | /* Last Literals */ |
| 1692 | { |
| 1693 | size_t lastLLSize = iend - anchor; |
| 1694 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1695 | seqStorePtr->lit += lastLLSize; |
| 1696 | } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1697 | } |
| 1698 | |
Yann Collet | 3b63f7f | 2016-02-10 15:05:12 +0100 | [diff] [blame] | 1699 | #include "zstd_opt.h" |
inikep | e2bfe24 | 2016-01-31 11:25:48 +0100 | [diff] [blame] | 1700 | |
| 1701 | static void ZSTD_compressBlock_opt_bt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 1702 | { |
| 1703 | ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 1, 2); |
| 1704 | } |
| 1705 | |
| 1706 | static void ZSTD_compressBlock_opt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 1707 | { |
| 1708 | ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 0, 2); |
| 1709 | } |
| 1710 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1711 | static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1712 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1713 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 1, 2); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1714 | } |
| 1715 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1716 | static void ZSTD_compressBlock_lazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1717 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1718 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 2); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1719 | } |
| 1720 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1721 | static void ZSTD_compressBlock_lazy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1722 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1723 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 1); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1724 | } |
| 1725 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1726 | static void ZSTD_compressBlock_greedy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1727 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1728 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 0); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 1729 | } |
| 1730 | |
| 1731 | |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1732 | FORCE_INLINE |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1733 | void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, |
| 1734 | const void* src, size_t srcSize, |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1735 | const U32 searchMethod, const U32 depth) |
| 1736 | { |
| 1737 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 1738 | const BYTE* const istart = (const BYTE*)src; |
| 1739 | const BYTE* ip = istart; |
| 1740 | const BYTE* anchor = istart; |
| 1741 | const BYTE* const iend = istart + srcSize; |
| 1742 | const BYTE* const ilimit = iend - 8; |
| 1743 | const BYTE* const base = ctx->base; |
| 1744 | const U32 dictLimit = ctx->dictLimit; |
| 1745 | const BYTE* const prefixStart = base + dictLimit; |
| 1746 | const BYTE* const dictBase = ctx->dictBase; |
| 1747 | const BYTE* const dictEnd = dictBase + dictLimit; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 1748 | const BYTE* const dictStart = dictBase + ctx->lowLimit; |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1749 | |
| 1750 | size_t offset_2=REPCODE_STARTVALUE, offset_1=REPCODE_STARTVALUE; |
| 1751 | const U32 maxSearches = 1 << ctx->params.searchLog; |
| 1752 | const U32 mls = ctx->params.searchLength; |
| 1753 | |
| 1754 | typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, |
| 1755 | size_t* offsetPtr, |
| 1756 | U32 maxNbAttempts, U32 matchLengthSearch); |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1757 | searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS_extDict : ZSTD_HcFindBestMatch_extDict_selectMLS; |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1758 | |
| 1759 | /* init */ |
| 1760 | ZSTD_resetSeqStore(seqStorePtr); |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 1761 | if ((ip - prefixStart) < REPCODE_STARTVALUE) ip += REPCODE_STARTVALUE; |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1762 | |
| 1763 | /* Match Loop */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1764 | while (ip < ilimit) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1765 | size_t matchLength=0; |
| 1766 | size_t offset=0; |
| 1767 | const BYTE* start=ip+1; |
| 1768 | U32 current = (U32)(ip-base); |
| 1769 | |
| 1770 | /* check repCode */ |
| 1771 | { |
| 1772 | const U32 repIndex = (U32)(current+1 - offset_1); |
| 1773 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 1774 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1775 | if ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1776 | if (MEM_read32(ip+1) == MEM_read32(repMatch)) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1777 | /* repcode detected we should take it */ |
| 1778 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1779 | matchLength = ZSTD_count_2segments(ip+1+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1780 | if (depth==0) goto _storeSequence; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1781 | } } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1782 | |
| 1783 | { |
Yann Collet | 239cc28 | 2015-11-23 16:17:21 +0100 | [diff] [blame] | 1784 | /* first search (depth 0) */ |
| 1785 | size_t offsetFound = 99999999; |
| 1786 | size_t ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); |
| 1787 | if (ml2 > matchLength) |
| 1788 | matchLength = ml2, start = ip, offset=offsetFound; |
| 1789 | } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1790 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1791 | if (matchLength < MINMATCH) { |
Yann Collet | 239cc28 | 2015-11-23 16:17:21 +0100 | [diff] [blame] | 1792 | ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ |
| 1793 | continue; |
| 1794 | } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1795 | |
| 1796 | /* let's try to find a better solution */ |
| 1797 | if (depth>=1) |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1798 | while (ip<ilimit) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1799 | ip ++; |
| 1800 | current++; |
| 1801 | /* check repCode */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1802 | if (offset) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1803 | const U32 repIndex = (U32)(current - offset_1); |
| 1804 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 1805 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1806 | if ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1807 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1808 | /* repcode detected */ |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1809 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1810 | size_t repLength = ZSTD_count_2segments(ip+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; |
| 1811 | int gain2 = (int)(repLength * 3); |
| 1812 | int gain1 = (int)(matchLength*3 - ZSTD_highbit((U32)offset+1) + 1); |
| 1813 | if ((repLength >= MINMATCH) && (gain2 > gain1)) |
| 1814 | matchLength = repLength, offset = 0, start = ip; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1815 | } } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1816 | |
| 1817 | /* search match, depth 1 */ |
| 1818 | { |
| 1819 | size_t offset2=999999; |
| 1820 | size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
| 1821 | int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ |
| 1822 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 4); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1823 | if ((ml2 >= MINMATCH) && (gain2 > gain1)) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1824 | matchLength = ml2, offset = offset2, start = ip; |
| 1825 | continue; /* search a better one */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1826 | } } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1827 | |
| 1828 | /* let's find an even better one */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1829 | if ((depth==2) && (ip<ilimit)) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1830 | ip ++; |
| 1831 | current++; |
| 1832 | /* check repCode */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1833 | if (offset) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1834 | const U32 repIndex = (U32)(current - offset_1); |
| 1835 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 1836 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1837 | if ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1838 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1839 | /* repcode detected */ |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1840 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1841 | size_t repLength = ZSTD_count_2segments(ip+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; |
| 1842 | int gain2 = (int)(repLength * 4); |
| 1843 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 1); |
| 1844 | if ((repLength >= MINMATCH) && (gain2 > gain1)) |
| 1845 | matchLength = repLength, offset = 0, start = ip; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1846 | } } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1847 | |
| 1848 | /* search match, depth 2 */ |
| 1849 | { |
| 1850 | size_t offset2=999999; |
| 1851 | size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
| 1852 | int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ |
| 1853 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1854 | if ((ml2 >= MINMATCH) && (gain2 > gain1)) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1855 | matchLength = ml2, offset = offset2, start = ip; |
| 1856 | continue; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1857 | } } } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1858 | break; /* nothing found : store previous solution */ |
| 1859 | } |
| 1860 | |
| 1861 | /* catch up */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1862 | if (offset) { |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 1863 | U32 matchIndex = (U32)((start-base) - offset); |
| 1864 | const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex; |
| 1865 | const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart; |
| 1866 | while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */ |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1867 | offset_2 = offset_1; offset_1 = offset; |
| 1868 | } |
| 1869 | |
| 1870 | /* store sequence */ |
| 1871 | _storeSequence: |
| 1872 | { |
| 1873 | size_t litLength = start - anchor; |
| 1874 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH); |
| 1875 | anchor = ip = start + matchLength; |
| 1876 | } |
| 1877 | |
| 1878 | /* check immediate repcode */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1879 | while (ip <= ilimit) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1880 | const U32 repIndex = (U32)((ip-base) - offset_2); |
| 1881 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 1882 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 239cc28 | 2015-11-23 16:17:21 +0100 | [diff] [blame] | 1883 | if ((U32)((dictLimit-1) - repIndex) >= 3) /* intentional overflow */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1884 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1885 | /* repcode detected we should take it */ |
| 1886 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1887 | matchLength = ZSTD_count_2segments(ip+MINMATCH, repMatch+MINMATCH, iend, repEnd, prefixStart) + MINMATCH; |
| 1888 | offset = offset_2; offset_2 = offset_1; offset_1 = offset; /* swap offset history */ |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1889 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength-MINMATCH); |
| 1890 | ip += matchLength; |
| 1891 | anchor = ip; |
| 1892 | continue; /* faster when present ... (?) */ |
| 1893 | } |
| 1894 | break; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1895 | } } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1896 | |
| 1897 | /* Last Literals */ |
| 1898 | { |
| 1899 | size_t lastLLSize = iend - anchor; |
| 1900 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1901 | seqStorePtr->lit += lastLLSize; |
| 1902 | } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1903 | } |
| 1904 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1905 | void ZSTD_compressBlock_greedy_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1906 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1907 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 0); |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1908 | } |
| 1909 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1910 | static void ZSTD_compressBlock_lazy_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | b7fc88e | 2015-11-22 03:12:28 +0100 | [diff] [blame] | 1911 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1912 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 1); |
Yann Collet | b7fc88e | 2015-11-22 03:12:28 +0100 | [diff] [blame] | 1913 | } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 1914 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1915 | static void ZSTD_compressBlock_lazy2_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | a85c77b | 2015-11-22 12:22:04 +0100 | [diff] [blame] | 1916 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1917 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 2); |
Yann Collet | a85c77b | 2015-11-22 12:22:04 +0100 | [diff] [blame] | 1918 | } |
| 1919 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1920 | static void ZSTD_compressBlock_btlazy2_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1921 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1922 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 1, 2); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1923 | } |
| 1924 | |
inikep | e2bfe24 | 2016-01-31 11:25:48 +0100 | [diff] [blame] | 1925 | static void ZSTD_compressBlock_opt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 1926 | { |
inikep | be77f33 | 2016-02-09 23:00:41 +0100 | [diff] [blame] | 1927 | ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 0, 2); |
inikep | e2bfe24 | 2016-01-31 11:25:48 +0100 | [diff] [blame] | 1928 | } |
| 1929 | |
| 1930 | static void ZSTD_compressBlock_opt_bt_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 1931 | { |
inikep | be77f33 | 2016-02-09 23:00:41 +0100 | [diff] [blame] | 1932 | ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 1, 2); |
inikep | e2bfe24 | 2016-01-31 11:25:48 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
Yann Collet | 7a23179 | 2015-11-21 15:27:35 +0100 | [diff] [blame] | 1935 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1936 | typedef void (*ZSTD_blockCompressor) (ZSTD_CCtx* ctx, const void* src, size_t srcSize); |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1937 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1938 | static ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1939 | { |
inikep | e2bfe24 | 2016-01-31 11:25:48 +0100 | [diff] [blame] | 1940 | static const ZSTD_blockCompressor blockCompressor[2][7] = { |
| 1941 | { ZSTD_compressBlock_fast, ZSTD_compressBlock_greedy, ZSTD_compressBlock_lazy,ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, ZSTD_compressBlock_opt, ZSTD_compressBlock_opt_bt }, |
| 1942 | { ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_greedy_extDict, ZSTD_compressBlock_lazy_extDict,ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, ZSTD_compressBlock_opt_extDict, ZSTD_compressBlock_opt_bt_extDict } |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 1943 | }; |
| 1944 | |
| 1945 | return blockCompressor[extDict][(U32)strat]; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 1949 | static size_t ZSTD_compressBlock_internal(ZSTD_CCtx* zc, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 1950 | { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1951 | ZSTD_blockCompressor blockCompressor = ZSTD_selectBlockCompressor(zc->params.strategy, zc->lowLimit < zc->dictLimit); |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1952 | if (srcSize < MIN_CBLOCK_SIZE+ZSTD_blockHeaderSize+1) return 0; /* don't even attempt compression below a certain srcSize */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1953 | blockCompressor(zc, src, srcSize); |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1954 | return ZSTD_compressSequences(zc, dst, maxDstSize, srcSize); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1958 | static size_t ZSTD_compress_generic (ZSTD_CCtx* zc, |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1959 | void* dst, size_t maxDstSize, |
| 1960 | const void* src, size_t srcSize) |
| 1961 | { |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1962 | size_t blockSize = zc->blockSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1963 | size_t remaining = srcSize; |
| 1964 | const BYTE* ip = (const BYTE*)src; |
| 1965 | BYTE* const ostart = (BYTE*)dst; |
| 1966 | BYTE* op = ostart; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1967 | const U32 maxDist = 1 << zc->params.windowLog; |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 1968 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1969 | while (remaining) { |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 1970 | size_t cSize; |
| 1971 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1972 | if (maxDstSize < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE) return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */ |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 1973 | if (remaining < blockSize) blockSize = remaining; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1974 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1975 | if ((U32)(ip+blockSize - zc->base) > zc->loadedDictEnd + maxDist) { /* enforce maxDist */ |
Yann Collet | 7a6343f | 2016-02-02 16:00:50 +0100 | [diff] [blame] | 1976 | U32 newLowLimit = (U32)(ip+blockSize - zc->base) - maxDist; |
| 1977 | if (zc->lowLimit < newLowLimit) zc->lowLimit = newLowLimit; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1978 | if (zc->dictLimit < zc->lowLimit) zc->dictLimit = zc->lowLimit; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 1979 | } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1980 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1981 | cSize = ZSTD_compressBlock_internal(zc, op+ZSTD_blockHeaderSize, maxDstSize-ZSTD_blockHeaderSize, ip, blockSize); |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 1982 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1983 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1984 | if (cSize == 0) { /* block is not compressible */ |
| 1985 | cSize = ZSTD_noCompressBlock(op, maxDstSize, ip, blockSize); |
Yann Collet | 523b594 | 2016-01-09 02:10:40 +0100 | [diff] [blame] | 1986 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1987 | } else { |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1988 | op[0] = (BYTE)(cSize>>16); |
| 1989 | op[1] = (BYTE)(cSize>>8); |
| 1990 | op[2] = (BYTE)cSize; |
Yann Collet | c95f899 | 2015-11-19 17:28:35 +0100 | [diff] [blame] | 1991 | op[0] += (BYTE)(bt_compressed << 6); /* is a compressed block */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1992 | cSize += 3; |
| 1993 | } |
| 1994 | |
| 1995 | remaining -= blockSize; |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 1996 | maxDstSize -= cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1997 | ip += blockSize; |
| 1998 | op += cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | return op-ostart; |
| 2002 | } |
| 2003 | |
| 2004 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2005 | static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* zc, |
| 2006 | void* dst, size_t dstSize, |
| 2007 | const void* src, size_t srcSize, |
| 2008 | U32 frame) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2009 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2010 | const BYTE* const ip = (const BYTE*) src; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2011 | size_t hbSize = 0; |
| 2012 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2013 | if (frame && (zc->stage==0)) { |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2014 | hbSize = zc->hbSize; |
| 2015 | if (dstSize <= hbSize) return ERROR(dstSize_tooSmall); |
| 2016 | zc->stage = 1; |
| 2017 | memcpy(dst, zc->headerBuffer, hbSize); |
| 2018 | dstSize -= hbSize; |
| 2019 | dst = (char*)dst + hbSize; |
| 2020 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2021 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2022 | /* Check if blocks follow each other */ |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2023 | if (src != zc->nextSrc) { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2024 | /* not contiguous */ |
| 2025 | size_t delta = zc->nextSrc - ip; |
| 2026 | zc->lowLimit = zc->dictLimit; |
| 2027 | zc->dictLimit = (U32)(zc->nextSrc - zc->base); |
| 2028 | zc->dictBase = zc->base; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2029 | zc->base -= delta; |
| 2030 | zc->nextToUpdate = zc->dictLimit; |
Yann Collet | e47c4e5 | 2015-12-05 09:23:53 +0100 | [diff] [blame] | 2031 | if (zc->dictLimit - zc->lowLimit < 8) zc->lowLimit = zc->dictLimit; /* too small extDict */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2032 | } |
| 2033 | |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2034 | /* preemptive overflow correction */ |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2035 | if (zc->lowLimit > (1<<30)) { |
inikep | c71568f | 2016-01-30 12:15:56 +0100 | [diff] [blame] | 2036 | U32 btplus = (zc->params.strategy == ZSTD_btlazy2) || (zc->params.strategy == ZSTD_opt_bt); |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 2037 | U32 contentMask = (1 << (zc->params.contentLog - btplus)) - 1; |
| 2038 | U32 newLowLimit = zc->lowLimit & contentMask; /* preserve position % contentSize */ |
| 2039 | U32 correction = zc->lowLimit - newLowLimit; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 2040 | ZSTD_reduceIndex(zc, correction); |
| 2041 | zc->base += correction; |
| 2042 | zc->dictBase += correction; |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 2043 | zc->lowLimit = newLowLimit; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 2044 | zc->dictLimit -= correction; |
| 2045 | if (zc->nextToUpdate < correction) zc->nextToUpdate = 0; |
| 2046 | else zc->nextToUpdate -= correction; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2047 | } |
| 2048 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2049 | /* if input and dictionary overlap : reduce dictionary (presumed modified by input) */ |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2050 | if ((ip+srcSize > zc->dictBase + zc->lowLimit) && (ip < zc->dictBase + zc->dictLimit)) { |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 2051 | zc->lowLimit = (U32)(ip + srcSize - zc->dictBase); |
| 2052 | if (zc->lowLimit > zc->dictLimit) zc->lowLimit = zc->dictLimit; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 2055 | zc->nextSrc = ip + srcSize; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2056 | { |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2057 | size_t cSize; |
| 2058 | if (frame) cSize = ZSTD_compress_generic (zc, dst, dstSize, src, srcSize); |
| 2059 | else cSize = ZSTD_compressBlock_internal (zc, dst, dstSize, src, srcSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2060 | if (ZSTD_isError(cSize)) return cSize; |
| 2061 | return cSize + hbSize; |
| 2062 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2063 | } |
| 2064 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2065 | |
| 2066 | size_t ZSTD_compressContinue (ZSTD_CCtx* zc, |
| 2067 | void* dst, size_t dstSize, |
| 2068 | const void* src, size_t srcSize) |
| 2069 | { |
| 2070 | return ZSTD_compressContinue_internal(zc, dst, dstSize, src, srcSize, 1); |
| 2071 | } |
| 2072 | |
| 2073 | |
| 2074 | size_t ZSTD_compressBlock(ZSTD_CCtx* zc, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 2075 | { |
| 2076 | if (srcSize > BLOCKSIZE) return ERROR(srcSize_wrong); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2077 | return ZSTD_compressContinue_internal(zc, dst, maxDstSize, src, srcSize, 0); |
| 2078 | } |
| 2079 | |
| 2080 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2081 | static size_t ZSTD_loadDictionaryContent(ZSTD_CCtx* zc, const void* src, size_t srcSize) |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2082 | { |
| 2083 | const BYTE* const ip = (const BYTE*) src; |
| 2084 | const BYTE* const iend = ip + srcSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2085 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2086 | /* input becomes current prefix */ |
| 2087 | zc->lowLimit = zc->dictLimit; |
| 2088 | zc->dictLimit = (U32)(zc->nextSrc - zc->base); |
| 2089 | zc->dictBase = zc->base; |
| 2090 | zc->base += ip - zc->nextSrc; |
| 2091 | zc->nextToUpdate = zc->dictLimit; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2092 | zc->loadedDictEnd = (U32)(iend - zc->base); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2093 | |
| 2094 | zc->nextSrc = iend; |
| 2095 | if (srcSize <= 8) return 0; |
| 2096 | |
| 2097 | switch(zc->params.strategy) |
| 2098 | { |
| 2099 | case ZSTD_fast: |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2100 | ZSTD_fillHashTable (zc, iend, zc->params.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2101 | break; |
| 2102 | |
| 2103 | case ZSTD_greedy: |
| 2104 | case ZSTD_lazy: |
| 2105 | case ZSTD_lazy2: |
inikep | be77f33 | 2016-02-09 23:00:41 +0100 | [diff] [blame] | 2106 | case ZSTD_opt: |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2107 | ZSTD_insertAndFindFirstIndex (zc, iend-8, zc->params.searchLength); |
| 2108 | break; |
| 2109 | |
| 2110 | case ZSTD_btlazy2: |
inikep | be77f33 | 2016-02-09 23:00:41 +0100 | [diff] [blame] | 2111 | case ZSTD_opt_bt: |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2112 | ZSTD_updateTree(zc, iend-8, iend, 1 << zc->params.searchLog, zc->params.searchLength); |
| 2113 | break; |
| 2114 | |
| 2115 | default: |
| 2116 | return ERROR(GENERIC); /* strategy doesn't exist; impossible */ |
| 2117 | } |
| 2118 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2119 | zc->nextToUpdate = zc->loadedDictEnd; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2120 | return 0; |
| 2121 | } |
| 2122 | |
| 2123 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2124 | /* Dictionary format : |
| 2125 | Magic == ZSTD_DICT_MAGIC (4 bytes) |
| 2126 | Huff0 CTable (256 * 4 bytes) => to be changed to read from writeCTable |
| 2127 | Dictionary content |
| 2128 | */ |
| 2129 | /*! ZSTD_loadDictEntropyStats |
| 2130 | @return : size read from dictionary */ |
| 2131 | static size_t ZSTD_loadDictEntropyStats(ZSTD_CCtx* zc, const void* dict, size_t dictSize) |
| 2132 | { |
| 2133 | /* note : magic number already checked */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2134 | size_t offcodeHeaderSize, matchlengthHeaderSize, litlengthHeaderSize, errorCode; |
| 2135 | short offcodeNCount[MaxOff+1]; |
| 2136 | unsigned offcodeMaxValue = MaxOff, offcodeLog = OffFSELog; |
| 2137 | short matchlengthNCount[MaxML+1]; |
| 2138 | unsigned matchlengthMaxValue = MaxML, matchlengthLog = MLFSELog; |
| 2139 | short litlengthNCount[MaxLL+1]; |
| 2140 | unsigned litlengthMaxValue = MaxLL, litlengthLog = LLFSELog; |
| 2141 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2142 | const size_t hufHeaderSize = HUF_readCTable(zc->hufTable, 255, dict, dictSize); |
| 2143 | if (HUF_isError(hufHeaderSize)) return ERROR(dictionary_corrupted); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2144 | zc->flagStaticTables = 1; |
| 2145 | dict = (const char*)dict + hufHeaderSize; |
| 2146 | dictSize -= hufHeaderSize; |
| 2147 | |
| 2148 | offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dict, dictSize); |
| 2149 | if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); |
| 2150 | errorCode = FSE_buildCTable(zc->offcodeCTable, offcodeNCount, offcodeMaxValue, offcodeLog); |
| 2151 | if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); |
| 2152 | dict = (const char*)dict + offcodeHeaderSize; |
| 2153 | dictSize -= offcodeHeaderSize; |
| 2154 | |
| 2155 | matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dict, dictSize); |
| 2156 | if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); |
| 2157 | errorCode = FSE_buildCTable(zc->matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog); |
| 2158 | if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); |
| 2159 | dict = (const char*)dict + matchlengthHeaderSize; |
| 2160 | dictSize -= matchlengthHeaderSize; |
| 2161 | |
| 2162 | litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dict, dictSize); |
| 2163 | if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); |
| 2164 | errorCode = FSE_buildCTable(zc->litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog); |
| 2165 | if (FSE_isError(errorCode)) return ERROR(dictionary_corrupted); |
| 2166 | |
| 2167 | return hufHeaderSize + offcodeHeaderSize + matchlengthHeaderSize + litlengthHeaderSize; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2168 | } |
| 2169 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2170 | |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2171 | static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* zc, const void* dict, size_t dictSize) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2172 | { |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2173 | if (dict && (dictSize>4)) { |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 2174 | U32 magic = MEM_readLE32(dict); |
| 2175 | size_t eSize; |
| 2176 | if (magic != ZSTD_DICT_MAGIC) |
| 2177 | return ZSTD_loadDictionaryContent(zc, dict, dictSize); |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2178 | |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 2179 | eSize = ZSTD_loadDictEntropyStats(zc, (const char*)dict+4, dictSize-4) + 4; |
| 2180 | if (ZSTD_isError(eSize)) return eSize; |
| 2181 | return ZSTD_loadDictionaryContent(zc, (const char*)dict+eSize, dictSize-eSize); |
| 2182 | } |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2187 | /*! ZSTD_compressBegin_advanced |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2188 | * @return : 0, or an error code */ |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2189 | size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* zc, |
| 2190 | const void* dict, size_t dictSize, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2191 | ZSTD_parameters params) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2192 | { |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 2193 | size_t errorCode; |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2194 | |
| 2195 | ZSTD_validateParams(¶ms); |
| 2196 | |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2197 | errorCode = ZSTD_resetCCtx_advanced(zc, params); |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 2198 | if (ZSTD_isError(errorCode)) return errorCode; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2199 | |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2200 | MEM_writeLE32(zc->headerBuffer, ZSTD_MAGICNUMBER); /* Write Header */ |
| 2201 | ((BYTE*)zc->headerBuffer)[4] = (BYTE)(params.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN); |
| 2202 | zc->hbSize = ZSTD_frameHeaderSize_min; |
| 2203 | zc->stage = 0; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2204 | |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2205 | return ZSTD_compress_insertDictionary(zc, dict, dictSize); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2206 | } |
| 2207 | |
| 2208 | |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2209 | size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* zc, const void* dict, size_t dictSize, int compressionLevel) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2210 | { |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 2211 | return ZSTD_compressBegin_advanced(zc, dict, dictSize, ZSTD_getParams(compressionLevel, MAX(128 KB, dictSize))); |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2212 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2213 | |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2214 | size_t ZSTD_compressBegin(ZSTD_CCtx* zc, int compressionLevel) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2215 | { |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2216 | return ZSTD_compressBegin_advanced(zc, NULL, 0, ZSTD_getParams(compressionLevel, 0)); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2217 | } |
| 2218 | |
| 2219 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2220 | /*! ZSTD_compressEnd |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2221 | * Write frame epilogue |
| 2222 | * @return : nb of bytes written into dst (or an error code) */ |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2223 | size_t ZSTD_compressEnd(ZSTD_CCtx* zc, void* dst, size_t maxDstSize) |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2224 | { |
| 2225 | BYTE* op = (BYTE*)dst; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2226 | size_t hbSize = 0; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2227 | |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2228 | /* empty frame */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2229 | if (zc->stage==0) { |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2230 | hbSize = zc->hbSize; |
| 2231 | if (maxDstSize <= hbSize) return ERROR(dstSize_tooSmall); |
| 2232 | zc->stage = 1; |
| 2233 | memcpy(dst, zc->headerBuffer, hbSize); |
| 2234 | maxDstSize -= hbSize; |
| 2235 | op += hbSize; |
| 2236 | } |
| 2237 | |
| 2238 | /* frame epilogue */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2239 | if (maxDstSize < 3) return ERROR(dstSize_tooSmall); |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2240 | op[0] = (BYTE)(bt_end << 6); |
| 2241 | op[1] = 0; |
| 2242 | op[2] = 0; |
| 2243 | |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2244 | return 3+hbSize; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2245 | } |
| 2246 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2247 | |
| 2248 | size_t ZSTD_compress_usingPreparedCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, |
| 2249 | void* dst, size_t maxDstSize, |
| 2250 | const void* src, size_t srcSize) |
| 2251 | { |
| 2252 | size_t outSize; |
| 2253 | size_t errorCode = ZSTD_copyCCtx(cctx, preparedCCtx); |
| 2254 | if (ZSTD_isError(errorCode)) return errorCode; |
| 2255 | errorCode = ZSTD_compressContinue(cctx, dst, maxDstSize, src, srcSize); |
| 2256 | if (ZSTD_isError(errorCode)) return errorCode; |
| 2257 | outSize = errorCode; |
| 2258 | errorCode = ZSTD_compressEnd(cctx, (char*)dst+outSize, maxDstSize-outSize); |
| 2259 | if (ZSTD_isError(errorCode)) return errorCode; |
| 2260 | outSize += errorCode; |
| 2261 | return outSize; |
| 2262 | } |
| 2263 | |
| 2264 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2265 | size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2266 | void* dst, size_t maxDstSize, |
| 2267 | const void* src, size_t srcSize, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2268 | const void* dict,size_t dictSize, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2269 | ZSTD_parameters params) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2270 | { |
| 2271 | BYTE* const ostart = (BYTE*)dst; |
| 2272 | BYTE* op = ostart; |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 2273 | size_t oSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2274 | |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2275 | /* Init */ |
| 2276 | oSize = ZSTD_compressBegin_advanced(ctx, dict, dictSize, params); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2277 | if(ZSTD_isError(oSize)) return oSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2278 | |
| 2279 | /* body (compression) */ |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2280 | oSize = ZSTD_compressContinue (ctx, op, maxDstSize, src, srcSize); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2281 | if(ZSTD_isError(oSize)) return oSize; |
| 2282 | op += oSize; |
| 2283 | maxDstSize -= oSize; |
| 2284 | |
| 2285 | /* Close frame */ |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2286 | oSize = ZSTD_compressEnd(ctx, op, maxDstSize); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2287 | if(ZSTD_isError(oSize)) return oSize; |
| 2288 | op += oSize; |
| 2289 | |
| 2290 | return (op - ostart); |
| 2291 | } |
| 2292 | |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2293 | size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, const void* dict, size_t dictSize, int compressionLevel) |
| 2294 | { |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2295 | return ZSTD_compress_advanced(ctx, dst, maxDstSize, src, srcSize, dict, dictSize, ZSTD_getParams(compressionLevel, srcSize)); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2296 | } |
| 2297 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2298 | size_t ZSTD_compressCCtx (ZSTD_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2299 | { |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2300 | return ZSTD_compress_advanced(ctx, dst, maxDstSize, src, srcSize, NULL, 0, ZSTD_getParams(compressionLevel, srcSize)); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2301 | } |
| 2302 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2303 | size_t ZSTD_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2304 | { |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 2305 | size_t result; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2306 | ZSTD_CCtx ctxBody; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 2307 | memset(&ctxBody, 0, sizeof(ctxBody)); |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2308 | result = ZSTD_compressCCtx(&ctxBody, dst, maxDstSize, src, srcSize, compressionLevel); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2309 | free(ctxBody.workSpace); /* can't free ctxBody, since it's on stack; just free heap content */ |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 2310 | return result; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2311 | } |
Yann Collet | fdcad6d | 2015-12-17 23:50:15 +0100 | [diff] [blame] | 2312 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2313 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 2314 | /*-===== Pre-defined compression levels =====-*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2315 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 2316 | #define ZSTD_MAX_CLEVEL 25 |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 2317 | unsigned ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } |
| 2318 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2319 | static const ZSTD_parameters ZSTD_defaultParameters[4][ZSTD_MAX_CLEVEL+1] = { |
| 2320 | { /* "default" */ |
inikep | f2fee4c | 2016-02-05 19:45:25 +0100 | [diff] [blame] | 2321 | /* SL, W, C, H, S, L, strat */ |
| 2322 | { 0, 0, 18, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */ |
| 2323 | { 0, 0, 19, 13, 14, 1, 7, ZSTD_fast }, /* level 1 */ |
| 2324 | { 0, 0, 19, 15, 16, 1, 6, ZSTD_fast }, /* level 2 */ |
| 2325 | { 0, 0, 20, 18, 20, 1, 6, ZSTD_fast }, /* level 3 */ |
| 2326 | { 0, 0, 21, 19, 21, 1, 6, ZSTD_fast }, /* level 4 */ |
| 2327 | { 0, 0, 20, 14, 18, 3, 5, ZSTD_greedy }, /* level 5 */ |
| 2328 | { 0, 0, 20, 18, 19, 3, 5, ZSTD_greedy }, /* level 6 */ |
| 2329 | { 0, 0, 21, 17, 20, 3, 5, ZSTD_lazy }, /* level 7 */ |
| 2330 | { 0, 0, 21, 19, 20, 3, 5, ZSTD_lazy }, /* level 8 */ |
| 2331 | { 0, 0, 21, 20, 20, 3, 5, ZSTD_lazy2 }, /* level 9 */ |
| 2332 | { 0, 0, 21, 19, 21, 4, 5, ZSTD_lazy2 }, /* level 10 */ |
| 2333 | { 0, 0, 22, 20, 22, 4, 5, ZSTD_lazy2 }, /* level 11 */ // 42498419 |
| 2334 | { 0, 0, 22, 20, 22, 5, 5, ZSTD_lazy2 }, /* level 12 */ |
| 2335 | { 0, 0, 22, 21, 22, 5, 5, ZSTD_lazy2 }, /* level 13 */ |
| 2336 | { 0, 0, 22, 22, 23, 5, 5, ZSTD_lazy2 }, /* level 14 */ |
| 2337 | { 0, 0, 23, 23, 23, 5, 5, ZSTD_lazy2 }, /* level 15 */ |
| 2338 | { 0, 0, 23, 21, 22, 5, 5, ZSTD_btlazy2 }, /* level 16 */ // 42113689 |
| 2339 | { 0, 0, 23, 24, 23, 4, 5, ZSTD_btlazy2 }, /* level 17 */ |
| 2340 | { 0, 0, 25, 24, 23, 5, 5, ZSTD_btlazy2 }, /* level 18 */ |
| 2341 | { 0, 0, 25, 26, 23, 5, 5, ZSTD_btlazy2 }, /* level 19 */ |
| 2342 | { 0, 0, 26, 27, 25, 9, 5, ZSTD_btlazy2 }, /* level 20 */ |
Yann Collet | b79a0b3 | 2016-02-10 17:07:37 +0100 | [diff] [blame] | 2343 | { 0, 0, 23, 21, 22, 5, 4, ZSTD_btlazy2 }, /* level 21 = 16 + L=4 */ // 41233150 btlazy1=41560211 norep1=42322286 |
| 2344 | { 0, 12, 23, 21, 22, 5, 4, ZSTD_opt }, /* level 22 */ |
inikep | f2fee4c | 2016-02-05 19:45:25 +0100 | [diff] [blame] | 2345 | { 0, 32, 23, 21, 22, 5, 4, ZSTD_opt }, /* level 23 */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 2346 | { 0, 32, 23, 21, 22, 5, 4, ZSTD_opt_bt }, /* level 24 = 16 + btopt */ |
| 2347 | { 0, 64, 26, 27, 25, 10, 4, ZSTD_opt_bt }, /* level 25 = 20 + btopt */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2348 | }, |
| 2349 | { /* for srcSize <= 256 KB */ |
inikep | f2fee4c | 2016-02-05 19:45:25 +0100 | [diff] [blame] | 2350 | /* SL, W, C, H, S, L, strat */ |
| 2351 | { 0, 0, 18, 13, 14, 1, 7, ZSTD_fast }, /* level 0 - never used */ |
| 2352 | { 0, 0, 18, 14, 15, 1, 6, ZSTD_fast }, /* level 1 */ |
| 2353 | { 0, 0, 18, 14, 15, 1, 5, ZSTD_fast }, /* level 2 */ |
| 2354 | { 0, 0, 18, 12, 15, 3, 4, ZSTD_greedy }, /* level 3 */ |
| 2355 | { 0, 0, 18, 13, 15, 4, 4, ZSTD_greedy }, /* level 4 */ |
| 2356 | { 0, 0, 18, 14, 15, 5, 4, ZSTD_greedy }, /* level 5 */ |
| 2357 | { 0, 0, 18, 13, 15, 4, 4, ZSTD_lazy }, /* level 6 */ |
| 2358 | { 0, 0, 18, 14, 16, 5, 4, ZSTD_lazy }, /* level 7 */ |
| 2359 | { 0, 0, 18, 15, 16, 6, 4, ZSTD_lazy }, /* level 8 */ |
| 2360 | { 0, 0, 18, 15, 15, 7, 4, ZSTD_lazy }, /* level 9 */ |
| 2361 | { 0, 0, 18, 16, 16, 7, 4, ZSTD_lazy }, /* level 10 */ |
| 2362 | { 0, 0, 18, 16, 16, 8, 4, ZSTD_lazy }, /* level 11 */ |
| 2363 | { 0, 0, 18, 17, 16, 8, 4, ZSTD_lazy }, /* level 12 */ |
| 2364 | { 0, 0, 18, 17, 16, 9, 4, ZSTD_lazy }, /* level 13 */ |
| 2365 | { 0, 0, 18, 18, 16, 9, 4, ZSTD_lazy }, /* level 14 */ |
| 2366 | { 0, 0, 18, 17, 17, 9, 4, ZSTD_lazy2 }, /* level 15 */ |
| 2367 | { 0, 0, 18, 18, 18, 9, 4, ZSTD_lazy2 }, /* level 16 */ |
| 2368 | { 0, 0, 18, 18, 18, 10, 4, ZSTD_lazy2 }, /* level 17 */ |
| 2369 | { 0, 0, 18, 18, 18, 11, 4, ZSTD_lazy2 }, /* level 18 */ |
| 2370 | { 0, 0, 18, 18, 18, 12, 4, ZSTD_lazy2 }, /* level 19 */ |
| 2371 | { 0, 0, 18, 18, 18, 13, 4, ZSTD_lazy2 }, /* level 20 */ |
| 2372 | { 0, 0, 18, 18, 18, 10, 4, ZSTD_lazy2 }, /* level ??? */ |
| 2373 | { 0, 0, 18, 18, 18, 11, 4, ZSTD_lazy2 }, /* level ??? */ |
| 2374 | { 0, 0, 18, 18, 18, 12, 4, ZSTD_lazy2 }, /* level ??? */ |
| 2375 | { 0, 0, 18, 18, 18, 13, 4, ZSTD_lazy2 }, /* level ??? */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2376 | }, |
| 2377 | { /* for srcSize <= 128 KB */ |
| 2378 | /* W, C, H, S, L, strat */ |
inikep | f2fee4c | 2016-02-05 19:45:25 +0100 | [diff] [blame] | 2379 | { 0, 0, 17, 12, 12, 1, 4, ZSTD_fast }, /* level 0 - never used */ |
| 2380 | { 0, 0, 17, 12, 13, 1, 6, ZSTD_fast }, /* level 1 */ |
| 2381 | { 0, 0, 17, 14, 16, 1, 5, ZSTD_fast }, /* level 2 */ |
| 2382 | { 0, 0, 17, 15, 17, 1, 5, ZSTD_fast }, /* level 3 */ |
| 2383 | { 0, 0, 17, 13, 15, 2, 4, ZSTD_greedy }, /* level 4 */ |
| 2384 | { 0, 0, 17, 15, 17, 3, 4, ZSTD_greedy }, /* level 5 */ |
| 2385 | { 0, 0, 17, 14, 17, 3, 4, ZSTD_lazy }, /* level 6 */ |
| 2386 | { 0, 0, 17, 16, 17, 4, 4, ZSTD_lazy }, /* level 7 */ |
| 2387 | { 0, 0, 17, 16, 17, 4, 4, ZSTD_lazy2 }, /* level 8 */ |
| 2388 | { 0, 0, 17, 17, 16, 5, 4, ZSTD_lazy2 }, /* level 9 */ |
| 2389 | { 0, 0, 17, 17, 16, 6, 4, ZSTD_lazy2 }, /* level 10 */ |
| 2390 | { 0, 0, 17, 17, 16, 7, 4, ZSTD_lazy2 }, /* level 11 */ |
| 2391 | { 0, 0, 17, 17, 16, 8, 4, ZSTD_lazy2 }, /* level 12 */ |
| 2392 | { 0, 0, 17, 18, 16, 4, 4, ZSTD_btlazy2 }, /* level 13 */ |
| 2393 | { 0, 0, 17, 18, 16, 5, 4, ZSTD_btlazy2 }, /* level 14 */ |
| 2394 | { 0, 0, 17, 18, 16, 6, 4, ZSTD_btlazy2 }, /* level 15 */ |
| 2395 | { 0, 0, 17, 18, 16, 7, 4, ZSTD_btlazy2 }, /* level 16 */ |
| 2396 | { 0, 0, 17, 18, 16, 8, 4, ZSTD_btlazy2 }, /* level 17 */ |
| 2397 | { 0, 0, 17, 18, 16, 9, 4, ZSTD_btlazy2 }, /* level 18 */ |
| 2398 | { 0, 0, 17, 18, 16, 10, 4, ZSTD_btlazy2 }, /* level 19 */ |
| 2399 | { 0, 0, 17, 18, 18, 12, 4, ZSTD_btlazy2 }, /* level 20 */ |
| 2400 | { 0, 0, 17, 18, 16, 8, 4, ZSTD_btlazy2 }, /* level ??? */ |
| 2401 | { 0, 0, 17, 18, 16, 9, 4, ZSTD_btlazy2 }, /* level ??? */ |
| 2402 | { 0, 0, 17, 18, 16, 10, 4, ZSTD_btlazy2 }, /* level ??? */ |
| 2403 | { 0, 0, 17, 18, 18, 12, 4, ZSTD_btlazy2 }, /* level ??? */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2404 | }, |
| 2405 | { /* for srcSize <= 16 KB */ |
| 2406 | /* W, C, H, S, L, strat */ |
inikep | f2fee4c | 2016-02-05 19:45:25 +0100 | [diff] [blame] | 2407 | { 0, 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - never used */ |
| 2408 | { 0, 0, 14, 14, 14, 1, 4, ZSTD_fast }, /* level 1 */ |
| 2409 | { 0, 0, 14, 14, 16, 1, 4, ZSTD_fast }, /* level 2 */ |
| 2410 | { 0, 0, 14, 14, 14, 5, 4, ZSTD_greedy }, /* level 3 */ |
| 2411 | { 0, 0, 14, 14, 14, 8, 4, ZSTD_greedy }, /* level 4 */ |
| 2412 | { 0, 0, 14, 11, 14, 6, 4, ZSTD_lazy }, /* level 5 */ |
| 2413 | { 0, 0, 14, 14, 13, 6, 5, ZSTD_lazy }, /* level 6 */ |
| 2414 | { 0, 0, 14, 14, 14, 7, 6, ZSTD_lazy }, /* level 7 */ |
| 2415 | { 0, 0, 14, 14, 14, 8, 4, ZSTD_lazy }, /* level 8 */ |
| 2416 | { 0, 0, 14, 14, 15, 9, 4, ZSTD_lazy }, /* level 9 */ |
| 2417 | { 0, 0, 14, 14, 15, 10, 4, ZSTD_lazy }, /* level 10 */ |
| 2418 | { 0, 0, 14, 15, 15, 6, 4, ZSTD_btlazy2 }, /* level 11 */ |
| 2419 | { 0, 0, 14, 15, 15, 7, 4, ZSTD_btlazy2 }, /* level 12 */ |
| 2420 | { 0, 0, 14, 15, 15, 8, 4, ZSTD_btlazy2 }, /* level 13 */ |
| 2421 | { 0, 0, 14, 15, 15, 9, 4, ZSTD_btlazy2 }, /* level 14 */ |
| 2422 | { 0, 0, 14, 15, 15, 10, 4, ZSTD_btlazy2 }, /* level 15 */ |
| 2423 | { 0, 0, 14, 15, 15, 11, 4, ZSTD_btlazy2 }, /* level 16 */ |
| 2424 | { 0, 0, 14, 15, 15, 12, 4, ZSTD_btlazy2 }, /* level 17 */ |
| 2425 | { 0, 0, 14, 15, 15, 13, 4, ZSTD_btlazy2 }, /* level 18 */ |
| 2426 | { 0, 0, 14, 15, 15, 14, 4, ZSTD_btlazy2 }, /* level 19 */ |
| 2427 | { 0, 0, 14, 15, 15, 15, 4, ZSTD_btlazy2 }, /* level 20 */ |
| 2428 | { 0, 0, 14, 15, 15, 12, 4, ZSTD_btlazy2 }, /* level ??? */ |
| 2429 | { 0, 0, 14, 15, 15, 13, 4, ZSTD_btlazy2 }, /* level ??? */ |
| 2430 | { 0, 0, 14, 15, 15, 14, 4, ZSTD_btlazy2 }, /* level ??? */ |
| 2431 | { 0, 0, 14, 15, 15, 15, 4, ZSTD_btlazy2 }, /* level ??? */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2432 | }, |
| 2433 | }; |
| 2434 | |
| 2435 | /*! ZSTD_getParams |
| 2436 | * @return ZSTD_parameters structure for a selected compression level and srcSize. |
| 2437 | * @srcSizeHint value is optional, select 0 if not known */ |
| 2438 | ZSTD_parameters ZSTD_getParams(int compressionLevel, U64 srcSizeHint) |
| 2439 | { |
| 2440 | ZSTD_parameters result; |
| 2441 | int tableID = ((srcSizeHint-1) <= 256 KB) + ((srcSizeHint-1) <= 128 KB) + ((srcSizeHint-1) <= 16 KB); /* intentional underflow for srcSizeHint == 0 */ |
| 2442 | if (compressionLevel<=0) compressionLevel = 1; |
| 2443 | if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; |
inikep | 3379c5d | 2016-02-05 09:21:20 +0100 | [diff] [blame] | 2444 | #if ZSTD_OPT_DEBUG >= 1 |
| 2445 | tableID=0; |
| 2446 | #endif |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2447 | result = ZSTD_defaultParameters[tableID][compressionLevel]; |
| 2448 | result.srcSize = srcSizeHint; |
| 2449 | return result; |
| 2450 | } |
| 2451 | |