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