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