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