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