Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1 | /* |
| 2 | ZSTD HC - High Compression Mode of Zstandard |
| 3 | Copyright (C) 2015, Yann Collet. |
| 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 */ |
| 42 | # pragma warning(disable : 4324) /* disable: C4324: padded structure */ |
| 43 | #else |
| 44 | # define GCC_VERSION (__GNUC__ * 100 + __GNUC_MINOR__) |
| 45 | # ifdef __GNUC__ |
| 46 | # define FORCE_INLINE static inline __attribute__((always_inline)) |
| 47 | # else |
| 48 | # define FORCE_INLINE static inline |
| 49 | # endif |
| 50 | #endif |
| 51 | |
| 52 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 53 | /* ************************************* |
| 54 | * Includes |
| 55 | ***************************************/ |
| 56 | #include <stdlib.h> /* malloc */ |
| 57 | #include <string.h> /* memset */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 58 | #include "zstdhc_static.h" |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 59 | #include "zstd_static.h" |
Yann Collet | 3d9cf7a | 2015-10-29 17:15:14 +0100 | [diff] [blame] | 60 | #include "zstd_internal.h" |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 61 | #include "mem.h" |
| 62 | |
| 63 | |
| 64 | /* ************************************* |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 65 | * Local Constants |
| 66 | ***************************************/ |
| 67 | #define MINMATCH 4 |
Yann Collet | 53fff6c | 2015-10-24 13:48:37 +0100 | [diff] [blame] | 68 | #define MAXD_LOG 26 |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 69 | |
| 70 | #define KB *1024 |
| 71 | #define MB *1024*1024 |
| 72 | #define GB *(1ULL << 30) |
| 73 | |
| 74 | /* ************************************* |
| 75 | * Local Types |
| 76 | ***************************************/ |
| 77 | #define BLOCKSIZE (128 KB) /* define, for static allocation */ |
| 78 | #define WORKPLACESIZE (BLOCKSIZE*3) |
| 79 | |
| 80 | struct ZSTD_HC_CCtx_s |
| 81 | { |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 82 | const BYTE* end; /* next block here to continue on current prefix */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 83 | const BYTE* base; /* All regular indexes relative to this position */ |
| 84 | const BYTE* dictBase; /* extDict indexes relative to this position */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 85 | U32 dictLimit; /* below that point, need extDict */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 86 | U32 lowLimit; /* below that point, no more data */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 87 | U32 nextToUpdate; /* index from which to continue dictionary update */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 88 | ZSTD_HC_parameters params; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 89 | void* workSpace; |
| 90 | size_t workSpaceSize; |
| 91 | |
| 92 | seqStore_t seqStore; /* sequences storage ptrs */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 93 | U32* hashTable; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 94 | U32* contentTable; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | |
| 98 | ZSTD_HC_CCtx* ZSTD_HC_createCCtx(void) |
| 99 | { |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 100 | return (ZSTD_HC_CCtx*) calloc(1, sizeof(ZSTD_HC_CCtx)); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 103 | size_t ZSTD_HC_freeCCtx(ZSTD_HC_CCtx* cctx) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 104 | { |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 105 | free(cctx->workSpace); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 106 | free(cctx); |
| 107 | return 0; |
| 108 | } |
| 109 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 110 | |
| 111 | /** ZSTD_HC_validateParams |
| 112 | correct params value to remain within authorized range |
| 113 | optimize for srcSize if srcSize > 0 */ |
| 114 | void ZSTD_HC_validateParams(ZSTD_HC_parameters* params, size_t srcSize) |
| 115 | { |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 116 | const U32 btPlus = (params->strategy == ZSTD_HC_btlazy2); |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 117 | |
| 118 | /* validate params */ |
| 119 | if (params->windowLog > ZSTD_HC_WINDOWLOG_MAX) params->windowLog = ZSTD_HC_WINDOWLOG_MAX; |
| 120 | if (params->windowLog < ZSTD_HC_WINDOWLOG_MIN) params->windowLog = ZSTD_HC_WINDOWLOG_MIN; |
| 121 | |
| 122 | /* correct params, to use less memory */ |
| 123 | if (srcSize > 0) |
| 124 | { |
| 125 | U32 srcLog = ZSTD_highbit((U32)srcSize-1) + 1; |
| 126 | if (params->windowLog > srcLog) params->windowLog = srcLog; |
| 127 | } |
| 128 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 129 | if (params->contentLog > params->windowLog+btPlus) params->contentLog = params->windowLog+btPlus; /* <= ZSTD_HC_CONTENTLOG_MAX */ |
| 130 | if (params->contentLog < ZSTD_HC_CONTENTLOG_MIN) params->contentLog = ZSTD_HC_CONTENTLOG_MIN; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 131 | if (params->hashLog > ZSTD_HC_HASHLOG_MAX) params->hashLog = ZSTD_HC_HASHLOG_MAX; |
| 132 | if (params->hashLog < ZSTD_HC_HASHLOG_MIN) params->hashLog = ZSTD_HC_HASHLOG_MIN; |
| 133 | if (params->searchLog > ZSTD_HC_SEARCHLOG_MAX) params->searchLog = ZSTD_HC_SEARCHLOG_MAX; |
| 134 | if (params->searchLog < ZSTD_HC_SEARCHLOG_MIN) params->searchLog = ZSTD_HC_SEARCHLOG_MIN; |
| 135 | if (params->searchLength> ZSTD_HC_SEARCHLENGTH_MAX) params->searchLength = ZSTD_HC_SEARCHLENGTH_MAX; |
| 136 | if (params->searchLength< ZSTD_HC_SEARCHLENGTH_MIN) params->searchLength = ZSTD_HC_SEARCHLENGTH_MIN; |
| 137 | if ((U32)params->strategy>(U32)ZSTD_HC_btlazy2) params->strategy = ZSTD_HC_btlazy2; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 141 | static size_t ZSTD_HC_resetCCtx_advanced (ZSTD_HC_CCtx* zc, |
| 142 | ZSTD_HC_parameters params) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 143 | { |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 144 | ZSTD_HC_validateParams(¶ms, 0); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 145 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 146 | /* reserve table memory */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 147 | { |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 148 | const U32 contentLog = params.strategy == ZSTD_HC_fast ? 1 : params.contentLog; |
| 149 | const size_t tableSpace = ((1 << contentLog) + (1 << params.hashLog)) * sizeof(U32); |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 150 | const size_t neededSpace = tableSpace + WORKPLACESIZE; |
| 151 | if (zc->workSpaceSize < neededSpace) |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 152 | { |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 153 | free(zc->workSpace); |
| 154 | zc->workSpaceSize = neededSpace; |
| 155 | zc->workSpace = malloc(neededSpace); |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 156 | if (zc->workSpace == NULL) return ERROR(memory_allocation); |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 157 | } |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 158 | zc->hashTable = (U32*)zc->workSpace; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 159 | zc->contentTable = zc->hashTable + ((size_t)1 << params.hashLog); |
| 160 | zc->seqStore.buffer = (void*) (zc->contentTable + ((size_t)1 << contentLog)); |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 161 | memset(zc->hashTable, 0, tableSpace ); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 162 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 163 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 164 | zc->nextToUpdate = 0; |
| 165 | zc->end = NULL; |
| 166 | zc->base = NULL; |
| 167 | zc->dictBase = NULL; |
| 168 | zc->dictLimit = 0; |
| 169 | zc->lowLimit = 0; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 170 | zc->params = params; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 171 | zc->seqStore.offsetStart = (U32*) (zc->seqStore.buffer); |
| 172 | zc->seqStore.offCodeStart = (BYTE*) (zc->seqStore.offsetStart + (BLOCKSIZE>>2)); |
| 173 | zc->seqStore.litStart = zc->seqStore.offCodeStart + (BLOCKSIZE>>2); |
| 174 | zc->seqStore.litLengthStart = zc->seqStore.litStart + BLOCKSIZE; |
| 175 | zc->seqStore.matchLengthStart = zc->seqStore.litLengthStart + (BLOCKSIZE>>2); |
| 176 | zc->seqStore.dumpsStart = zc->seqStore.matchLengthStart + (BLOCKSIZE>>2); |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 177 | |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 178 | return 0; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 181 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 182 | /* ************************************* |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 183 | * Inline functions and Macros |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 184 | ***************************************/ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 185 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 186 | static const U32 prime4bytes = 2654435761U; |
| 187 | static U32 ZSTD_HC_hash4(U32 u, U32 h) { return (u * prime4bytes) >> (32-h) ; } |
| 188 | static size_t ZSTD_HC_hash4Ptr(const void* ptr, U32 h) { return ZSTD_HC_hash4(MEM_read32(ptr), h); } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 189 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 190 | static const U64 prime5bytes = 889523592379ULL; |
| 191 | static size_t ZSTD_HC_hash5(U64 u, U32 h) { return (size_t)((u * prime5bytes) << (64-40) >> (64-h)) ; } |
| 192 | static size_t ZSTD_HC_hash5Ptr(const void* p, U32 h) { return ZSTD_HC_hash5(MEM_read64(p), h); } |
| 193 | |
| 194 | static const U64 prime6bytes = 227718039650203ULL; |
| 195 | static size_t ZSTD_HC_hash6(U64 u, U32 h) { return (size_t)((u * prime6bytes) << (64-48) >> (64-h)) ; } |
| 196 | static size_t ZSTD_HC_hash6Ptr(const void* p, U32 h) { return ZSTD_HC_hash6(MEM_read64(p), h); } |
| 197 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 198 | static const U64 prime7bytes = 58295818150454627ULL; |
| 199 | static size_t ZSTD_HC_hash7(U64 u, U32 h) { return (size_t)((u * prime7bytes) << (64-56) >> (64-h)) ; } |
| 200 | static size_t ZSTD_HC_hash7Ptr(const void* p, U32 h) { return ZSTD_HC_hash7(MEM_read64(p), h); } |
| 201 | |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 202 | static size_t ZSTD_HC_hashPtr(const void* p, U32 hBits, U32 mls) |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 203 | { |
| 204 | switch(mls) |
| 205 | { |
| 206 | default: |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 207 | case 4: return ZSTD_HC_hash4Ptr(p, hBits); |
| 208 | case 5: return ZSTD_HC_hash5Ptr(p, hBits); |
| 209 | case 6: return ZSTD_HC_hash6Ptr(p, hBits); |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 210 | case 7: return ZSTD_HC_hash7Ptr(p, hBits); |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 211 | } |
| 212 | } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 213 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 214 | /* ************************************* |
| 215 | * Fast Scan |
| 216 | ***************************************/ |
| 217 | |
| 218 | FORCE_INLINE |
| 219 | size_t ZSTD_HC_compressBlock_fast_generic(ZSTD_HC_CCtx* ctx, |
| 220 | void* dst, size_t maxDstSize, |
| 221 | const void* src, size_t srcSize, |
| 222 | const U32 mls) |
| 223 | { |
| 224 | U32* hashTable = ctx->hashTable; |
| 225 | const U32 hBits = ctx->params.hashLog; |
| 226 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 227 | const BYTE* const base = ctx->base; |
| 228 | |
| 229 | const BYTE* const istart = (const BYTE*)src; |
| 230 | const BYTE* ip = istart + 1; |
| 231 | const BYTE* anchor = istart; |
| 232 | const BYTE* const iend = istart + srcSize; |
| 233 | const BYTE* const ilimit = iend - 8; |
| 234 | |
| 235 | size_t offset_2=4, offset_1=4; |
| 236 | |
| 237 | |
| 238 | /* init */ |
| 239 | if (ip == base) |
| 240 | { |
| 241 | hashTable[ZSTD_HC_hashPtr(base+1, hBits, mls)] = 1; |
| 242 | hashTable[ZSTD_HC_hashPtr(base+2, hBits, mls)] = 2; |
| 243 | hashTable[ZSTD_HC_hashPtr(base+3, hBits, mls)] = 3; |
| 244 | ip = base+4; |
| 245 | } |
| 246 | ZSTD_resetSeqStore(seqStorePtr); |
| 247 | |
| 248 | /* Main Search Loop */ |
| 249 | while (ip < ilimit) /* < instead of <=, because unconditionnal ZSTD_addPtr(ip+1) */ |
| 250 | { |
| 251 | const size_t h = ZSTD_HC_hashPtr(ip, hBits, mls); |
| 252 | const BYTE* match = base + hashTable[h]; |
| 253 | hashTable[h] = (U32)(ip-base); |
| 254 | |
| 255 | if (MEM_read32(ip-offset_2) == MEM_read32(ip)) match = ip-offset_2; |
| 256 | if (MEM_read32(match) != MEM_read32(ip)) { ip += ((ip-anchor) >> g_searchStrength) + 1; offset_2 = offset_1; continue; } |
| 257 | while ((ip>anchor) && (match>base) && (ip[-1] == match[-1])) { ip--; match--; } /* catch up */ |
| 258 | |
| 259 | { |
| 260 | size_t litLength = ip-anchor; |
| 261 | size_t matchLength = ZSTD_count(ip+MINMATCH, match+MINMATCH, iend); |
| 262 | size_t offsetCode = ip-match; |
| 263 | if (offsetCode == offset_2) offsetCode = 0; |
| 264 | offset_2 = offset_1; |
| 265 | offset_1 = ip-match; |
| 266 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, offsetCode, matchLength); |
| 267 | |
| 268 | /* Fill Table */ |
| 269 | hashTable[ZSTD_HC_hashPtr(ip+1, hBits, mls)] = (U32)(ip+1-base); |
| 270 | ip += matchLength + MINMATCH; |
| 271 | anchor = ip; |
| 272 | if (ip < ilimit) /* same test as loop, for speed */ |
| 273 | hashTable[ZSTD_HC_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* Last Literals */ |
| 278 | { |
| 279 | size_t lastLLSize = iend - anchor; |
| 280 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 281 | seqStorePtr->lit += lastLLSize; |
| 282 | } |
| 283 | |
| 284 | /* Finale compression stage */ |
| 285 | return ZSTD_compressSequences((BYTE*)dst, maxDstSize, |
| 286 | seqStorePtr, srcSize); |
| 287 | } |
| 288 | |
| 289 | |
| 290 | size_t ZSTD_HC_compressBlock_fast(ZSTD_HC_CCtx* ctx, |
| 291 | void* dst, size_t maxDstSize, |
| 292 | const void* src, size_t srcSize) |
| 293 | { |
| 294 | const U32 mls = ctx->params.searchLength; |
| 295 | switch(mls) |
| 296 | { |
| 297 | default: |
| 298 | case 4 : |
| 299 | return ZSTD_HC_compressBlock_fast_generic(ctx, dst, maxDstSize, src, srcSize, 4); |
| 300 | case 5 : |
| 301 | return ZSTD_HC_compressBlock_fast_generic(ctx, dst, maxDstSize, src, srcSize, 5); |
| 302 | case 6 : |
| 303 | return ZSTD_HC_compressBlock_fast_generic(ctx, dst, maxDstSize, src, srcSize, 6); |
| 304 | case 7 : |
| 305 | return ZSTD_HC_compressBlock_fast_generic(ctx, dst, maxDstSize, src, srcSize, 7); |
| 306 | } |
| 307 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 308 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 309 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 310 | /* ************************************* |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 311 | * Binary Tree search |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 312 | ***************************************/ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 313 | /** ZSTD_HC_insertBt1 : add one ptr to tree |
| 314 | @ip : assumed <= iend-8 */ |
| 315 | static void ZSTD_HC_insertBt1(ZSTD_HC_CCtx* zc, const BYTE* const ip, const U32 mls, const BYTE* const iend, U32 nbCompares) |
| 316 | { |
| 317 | U32* const hashTable = zc->hashTable; |
| 318 | const U32 hashLog = zc->params.hashLog; |
| 319 | const size_t h = ZSTD_HC_hashPtr(ip, hashLog, mls); |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 320 | U32* const bt = zc->contentTable; |
| 321 | const U32 btLog = zc->params.contentLog - 1; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 322 | const U32 btMask= (1 << btLog) - 1; |
| 323 | U32 matchIndex = hashTable[h]; |
| 324 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 325 | const BYTE* const base = zc->base; |
| 326 | const U32 current = (U32)(ip-base); |
| 327 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
| 328 | U32* smallerPtr = bt + 2*(current&btMask); |
| 329 | U32* largerPtr = bt + 2*(current&btMask) + 1; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 330 | U32 dummy32; /* to be nullified at the end */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 331 | const U32 windowSize = 1 << zc->params.windowLog; |
| 332 | const U32 windowLow = windowSize >= current ? 0 : current - windowSize; |
| 333 | |
| 334 | hashTable[h] = (U32)(ip-base); /* Update Hash Table */ |
| 335 | |
| 336 | while (nbCompares-- && (matchIndex > windowLow)) |
| 337 | { |
| 338 | U32* nextPtr = bt + 2*(matchIndex & btMask); |
| 339 | const BYTE* match = base + matchIndex; |
| 340 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 341 | |
| 342 | matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend); |
| 343 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 344 | if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ |
| 345 | break; /* just drop , to guarantee consistency (miss a bit of compression; if someone knows better, please tell) */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 346 | |
| 347 | if (match[matchLength] < ip[matchLength]) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 348 | { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 349 | /* match is smaller than current */ |
| 350 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 351 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 352 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 353 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 354 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 355 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 356 | else |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 357 | { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 358 | /* match is larger than current */ |
| 359 | *largerPtr = matchIndex; |
| 360 | commonLengthLarger = matchLength; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 361 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 362 | largerPtr = nextPtr; |
| 363 | matchIndex = nextPtr[0]; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 364 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 365 | } |
| 366 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 367 | *smallerPtr = *largerPtr = 0; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
| 371 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
| 372 | size_t ZSTD_HC_insertBtAndFindBestMatch ( |
| 373 | ZSTD_HC_CCtx* zc, |
| 374 | const BYTE* const ip, const BYTE* const iend, |
| 375 | size_t* offsetPtr, |
| 376 | U32 nbCompares, const U32 mls) |
| 377 | { |
| 378 | U32* const hashTable = zc->hashTable; |
| 379 | const U32 hashLog = zc->params.hashLog; |
| 380 | const size_t h = ZSTD_HC_hashPtr(ip, hashLog, mls); |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 381 | U32* const bt = zc->contentTable; |
| 382 | const U32 btLog = zc->params.contentLog - 1; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 383 | const U32 btMask= (1 << btLog) - 1; |
| 384 | U32 matchIndex = hashTable[h]; |
| 385 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 386 | const BYTE* const base = zc->base; |
| 387 | const U32 current = (U32)(ip-base); |
| 388 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
| 389 | const U32 windowSize = 1 << zc->params.windowLog; |
| 390 | const U32 windowLow = windowSize >= current ? 0 : current - windowSize; |
| 391 | U32* smallerPtr = bt + 2*(current&btMask); |
| 392 | U32* largerPtr = bt + 2*(current&btMask) + 1; |
Yann Collet | b241e9d | 2015-11-04 13:57:24 +0100 | [diff] [blame] | 393 | size_t bestLength = 0; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 394 | U32 dummy32; /* to be nullified at the end */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 395 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 396 | hashTable[h] = (U32)(ip-base); /* Update Hash Table */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 397 | |
| 398 | while (nbCompares-- && (matchIndex > windowLow)) |
| 399 | { |
| 400 | U32* nextPtr = bt + 2*(matchIndex & btMask); |
| 401 | const BYTE* match = base + matchIndex; |
| 402 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 403 | |
| 404 | matchLength += ZSTD_count(ip+matchLength, match+matchLength, iend); |
| 405 | |
| 406 | if (matchLength > bestLength) |
| 407 | { |
Yann Collet | e8455f5 | 2015-11-04 17:41:20 +0100 | [diff] [blame] | 408 | if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit(current-matchIndex+1) - ZSTD_highbit((U32)offsetPtr[0]+1)) ) |
Yann Collet | b241e9d | 2015-11-04 13:57:24 +0100 | [diff] [blame] | 409 | bestLength = matchLength, *offsetPtr = current - matchIndex; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 410 | if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ |
| 411 | break; /* drop, next to null, to guarantee consistency (is there a way to do better ?) */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | if (match[matchLength] < ip[matchLength]) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 415 | { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 416 | /* match is smaller than current */ |
| 417 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 418 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 419 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 420 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 421 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 422 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 423 | else |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 424 | { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 425 | /* match is larger than current */ |
| 426 | *largerPtr = matchIndex; |
| 427 | commonLengthLarger = matchLength; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 428 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 429 | largerPtr = nextPtr; |
| 430 | matchIndex = nextPtr[0]; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 431 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 432 | } |
| 433 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 434 | *smallerPtr = *largerPtr = 0; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 435 | |
| 436 | zc->nextToUpdate = current+1; /* current has been inserted */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 437 | if (bestLength < MINMATCH) return 0; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 438 | return bestLength; |
| 439 | } |
| 440 | |
| 441 | |
| 442 | static void ZSTD_HC_updateTree(ZSTD_HC_CCtx* zc, const BYTE* const ip, const BYTE* const iend, const U32 nbCompares, const U32 mls) |
| 443 | { |
| 444 | const BYTE* const base = zc->base; |
| 445 | const U32 target = (U32)(ip - base); |
| 446 | U32 idx = zc->nextToUpdate; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 447 | //size_t dummy; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 448 | |
| 449 | for( ; idx < target ; idx++) |
| 450 | ZSTD_HC_insertBt1(zc, base+idx, mls, iend, nbCompares); |
| 451 | //ZSTD_HC_insertBtAndFindBestMatch(zc, base+idx, iend, &dummy, nbCompares, mls); |
| 452 | |
| 453 | zc->nextToUpdate = target; |
| 454 | } |
| 455 | |
| 456 | |
| 457 | /** Tree updater, providing best match */ |
| 458 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
| 459 | size_t ZSTD_HC_BtFindBestMatch ( |
| 460 | ZSTD_HC_CCtx* zc, |
| 461 | const BYTE* const ip, const BYTE* const iLimit, |
| 462 | size_t* offsetPtr, |
| 463 | const U32 maxNbAttempts, const U32 mls) |
| 464 | { |
| 465 | ZSTD_HC_updateTree(zc, ip, iLimit, maxNbAttempts, mls); |
| 466 | return ZSTD_HC_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | FORCE_INLINE size_t ZSTD_HC_BtFindBestMatch_selectMLS ( |
| 471 | ZSTD_HC_CCtx* zc, /* Index table will be updated */ |
| 472 | const BYTE* ip, const BYTE* const iLimit, |
| 473 | size_t* offsetPtr, |
| 474 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 475 | { |
| 476 | switch(matchLengthSearch) |
| 477 | { |
| 478 | default : |
| 479 | case 4 : return ZSTD_HC_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 480 | case 5 : return ZSTD_HC_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
| 481 | case 6 : return ZSTD_HC_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 486 | /* *********************** |
| 487 | * Hash Chain |
| 488 | *************************/ |
| 489 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 490 | #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & mask] |
| 491 | |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 492 | /* Update chains up to ip (excluded) */ |
| 493 | static U32 ZSTD_HC_insertAndFindFirstIndex (ZSTD_HC_CCtx* zc, const BYTE* ip, U32 mls) |
| 494 | { |
| 495 | U32* const hashTable = zc->hashTable; |
| 496 | const U32 hashLog = zc->params.hashLog; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 497 | U32* const chainTable = zc->contentTable; |
| 498 | const U32 chainMask = (1 << zc->params.contentLog) - 1; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 499 | const BYTE* const base = zc->base; |
| 500 | const U32 target = (U32)(ip - base); |
| 501 | U32 idx = zc->nextToUpdate; |
| 502 | |
| 503 | while(idx < target) |
| 504 | { |
| 505 | size_t h = ZSTD_HC_hashPtr(base+idx, hashLog, mls); |
| 506 | NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; |
| 507 | hashTable[h] = idx; |
| 508 | idx++; |
| 509 | } |
| 510 | |
| 511 | zc->nextToUpdate = target; |
| 512 | return hashTable[ZSTD_HC_hashPtr(ip, hashLog, mls)]; |
| 513 | } |
| 514 | |
| 515 | |
| 516 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 517 | size_t ZSTD_HC_HcFindBestMatch ( |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 518 | ZSTD_HC_CCtx* zc, /* Index table will be updated */ |
| 519 | const BYTE* const ip, const BYTE* const iLimit, |
| 520 | size_t* offsetPtr, |
| 521 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 522 | { |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 523 | U32* const chainTable = zc->contentTable; |
| 524 | const U32 chainSize = (1 << zc->params.contentLog); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 525 | const U32 chainMask = chainSize-1; |
| 526 | const BYTE* const base = zc->base; |
| 527 | const BYTE* const dictBase = zc->dictBase; |
| 528 | const U32 dictLimit = zc->dictLimit; |
| 529 | const U32 maxDistance = (1 << zc->params.windowLog); |
| 530 | const U32 lowLimit = (zc->lowLimit + maxDistance > (U32)(ip-base)) ? zc->lowLimit : (U32)(ip - base) - (maxDistance - 1); |
| 531 | U32 matchIndex; |
| 532 | const BYTE* match; |
| 533 | int nbAttempts=maxNbAttempts; |
| 534 | size_t ml=0; |
| 535 | |
| 536 | /* HC4 match finder */ |
| 537 | matchIndex = ZSTD_HC_insertAndFindFirstIndex (zc, ip, matchLengthSearch); |
| 538 | |
| 539 | while ((matchIndex>lowLimit) && (nbAttempts)) |
| 540 | { |
| 541 | nbAttempts--; |
| 542 | if (matchIndex >= dictLimit) |
| 543 | { |
| 544 | match = base + matchIndex; |
| 545 | if ( (match[ml] == ip[ml]) |
| 546 | && (MEM_read32(match) == MEM_read32(ip)) ) /* ensures minimum match of 4 */ |
| 547 | { |
| 548 | const size_t mlt = ZSTD_count(ip+MINMATCH, match+MINMATCH, iLimit) + MINMATCH; |
| 549 | if (mlt > ml) |
| 550 | //if (((int)(4*mlt) - (int)ZSTD_highbit((U32)(ip-match)+1)) > ((int)(4*ml) - (int)ZSTD_highbit((U32)((*offsetPtr)+1)))) |
| 551 | { |
| 552 | ml = mlt; *offsetPtr = ip-match; |
| 553 | if (ip+ml >= iLimit) break; |
| 554 | } |
| 555 | } |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | match = dictBase + matchIndex; |
| 560 | if (MEM_read32(match) == MEM_read32(ip)) |
| 561 | { |
| 562 | size_t mlt; |
| 563 | const BYTE* vLimit = ip + (dictLimit - matchIndex); |
| 564 | if (vLimit > iLimit) vLimit = iLimit; |
| 565 | mlt = ZSTD_count(ip+MINMATCH, match+MINMATCH, vLimit) + MINMATCH; |
| 566 | if ((ip+mlt == vLimit) && (vLimit < iLimit)) |
| 567 | mlt += ZSTD_count(ip+mlt, base+dictLimit, iLimit); |
| 568 | if (mlt > ml) { ml = mlt; *offsetPtr = (ip-base) - matchIndex; } |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if (base + matchIndex <= ip - chainSize) break; |
| 573 | matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask); |
| 574 | } |
| 575 | |
| 576 | return ml; |
| 577 | } |
| 578 | |
| 579 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 580 | FORCE_INLINE size_t ZSTD_HC_HcFindBestMatch_selectMLS ( |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 581 | ZSTD_HC_CCtx* zc, /* Index table will be updated */ |
| 582 | const BYTE* ip, const BYTE* const iLimit, |
| 583 | size_t* offsetPtr, |
| 584 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 585 | { |
| 586 | switch(matchLengthSearch) |
| 587 | { |
| 588 | default : |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 589 | case 4 : return ZSTD_HC_HcFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 590 | case 5 : return ZSTD_HC_HcFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
| 591 | case 6 : return ZSTD_HC_HcFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 592 | } |
| 593 | } |
| 594 | |
| 595 | |
Yann Collet | 9036105 | 2015-11-05 15:03:12 +0100 | [diff] [blame] | 596 | /* common lazy function, to be inlined */ |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 597 | FORCE_INLINE |
| 598 | size_t ZSTD_HC_compressBlock_lazy_generic(ZSTD_HC_CCtx* ctx, |
| 599 | void* dst, size_t maxDstSize, const void* src, size_t srcSize, |
| 600 | const U32 searchMethod, const U32 deep) /* 0 : hc; 1 : bt */ |
| 601 | { |
| 602 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 603 | const BYTE* const istart = (const BYTE*)src; |
| 604 | const BYTE* ip = istart; |
| 605 | const BYTE* anchor = istart; |
| 606 | const BYTE* const iend = istart + srcSize; |
| 607 | const BYTE* const ilimit = iend - 8; |
| 608 | |
| 609 | size_t offset_2=REPCODE_STARTVALUE, offset_1=REPCODE_STARTVALUE; |
| 610 | const U32 maxSearches = 1 << ctx->params.searchLog; |
| 611 | const U32 mls = ctx->params.searchLength; |
| 612 | |
| 613 | typedef size_t (*searchMax_f)(ZSTD_HC_CCtx* zc, const BYTE* ip, const BYTE* iLimit, |
| 614 | size_t* offsetPtr, |
| 615 | U32 maxNbAttempts, U32 matchLengthSearch); |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 616 | searchMax_f searchMax = searchMethod ? ZSTD_HC_BtFindBestMatch_selectMLS : ZSTD_HC_HcFindBestMatch_selectMLS; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 617 | |
| 618 | /* init */ |
| 619 | ZSTD_resetSeqStore(seqStorePtr); |
| 620 | if (((ip-ctx->base) - ctx->dictLimit) < REPCODE_STARTVALUE) ip += REPCODE_STARTVALUE; |
| 621 | |
| 622 | /* Match Loop */ |
| 623 | while (ip <= ilimit) |
| 624 | { |
| 625 | size_t matchLength; |
| 626 | size_t offset=999999; |
| 627 | const BYTE* start; |
| 628 | |
| 629 | /* try to find a first match */ |
| 630 | if (MEM_read32(ip) == MEM_read32(ip - offset_2)) |
| 631 | { |
| 632 | /* repcode : we take it*/ |
| 633 | size_t offtmp = offset_2; |
| 634 | size_t litLength = ip - anchor; |
| 635 | matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend); |
| 636 | offset_2 = offset_1; |
| 637 | offset_1 = offtmp; |
| 638 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength); |
| 639 | ip += matchLength+MINMATCH; |
| 640 | anchor = ip; |
| 641 | continue; |
| 642 | } |
| 643 | |
| 644 | offset_2 = offset_1; |
| 645 | matchLength = searchMax(ctx, ip, iend, &offset, maxSearches, mls); |
| 646 | if (!matchLength) { ip++; continue; } |
| 647 | |
| 648 | /* let's try to find a better solution */ |
| 649 | start = ip; |
| 650 | |
| 651 | while (ip<ilimit) |
| 652 | { |
| 653 | ip ++; |
| 654 | if (MEM_read32(ip) == MEM_read32(ip - offset_1)) |
| 655 | { |
| 656 | size_t ml2 = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH; |
| 657 | int gain2 = (int)(ml2 * 3); |
| 658 | int gain1 = (int)(matchLength*3 - ZSTD_highbit((U32)offset+1) + 1); |
| 659 | if (gain2 > gain1) |
| 660 | matchLength = ml2, offset = 0, start = ip; |
| 661 | } |
| 662 | { |
| 663 | size_t offset2=999999; |
| 664 | size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
| 665 | int gain2 = (int)(ml2*(3+deep) - ZSTD_highbit((U32)offset2+1)); /* raw approx */ |
| 666 | int gain1 = (int)(matchLength*(3+deep) - ZSTD_highbit((U32)offset+1) + (3+deep)); |
| 667 | if (gain2 > gain1) |
| 668 | { |
| 669 | matchLength = ml2, offset = offset2, start = ip; |
| 670 | continue; /* search a better one */ |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | /* let's find an even better one */ |
| 675 | if (deep && (ip<ilimit)) |
| 676 | { |
| 677 | ip ++; |
| 678 | if (MEM_read32(ip) == MEM_read32(ip - offset_1)) |
| 679 | { |
| 680 | size_t ml2 = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_1, iend) + MINMATCH; |
| 681 | int gain2 = (int)(ml2 * 4); |
| 682 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 1); |
| 683 | if (gain2 > gain1) |
| 684 | matchLength = ml2, offset = 0, start = ip; |
| 685 | } |
| 686 | { |
| 687 | size_t offset2=999999; |
| 688 | size_t ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
| 689 | int gain2 = (int)(ml2*4 - ZSTD_highbit((U32)offset2+1)); /* raw approx */ |
| 690 | int gain1 = (int)(matchLength*4 - ZSTD_highbit((U32)offset+1) + 7); |
| 691 | if (gain2 > gain1) |
| 692 | { |
| 693 | matchLength = ml2, offset = offset2, start = ip; |
| 694 | continue; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | break; /* nothing found : store previous solution */ |
| 699 | } |
| 700 | |
| 701 | /* store sequence */ |
| 702 | { |
| 703 | size_t litLength = start - anchor; |
| 704 | if (offset) offset_1 = offset; |
| 705 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset, matchLength-MINMATCH); |
| 706 | ip = start + matchLength; |
| 707 | anchor = ip; |
| 708 | } |
| 709 | |
| 710 | } |
| 711 | |
| 712 | /* Last Literals */ |
| 713 | { |
| 714 | size_t lastLLSize = iend - anchor; |
| 715 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 716 | seqStorePtr->lit += lastLLSize; |
| 717 | } |
| 718 | |
| 719 | /* Final compression stage */ |
| 720 | return ZSTD_compressSequences((BYTE*)dst, maxDstSize, |
| 721 | seqStorePtr, srcSize); |
| 722 | } |
| 723 | |
| 724 | size_t ZSTD_HC_compressBlock_btlazy2(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 725 | { |
| 726 | return ZSTD_HC_compressBlock_lazy_generic(ctx, dst, maxDstSize, src, srcSize, 1, 1); |
| 727 | } |
| 728 | |
Yann Collet | 47b6890 | 2015-11-05 15:14:17 +0100 | [diff] [blame] | 729 | size_t ZSTD_HC_compressBlock_lazy2(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 730 | { |
| 731 | return ZSTD_HC_compressBlock_lazy_generic(ctx, dst, maxDstSize, src, srcSize, 0, 1); |
| 732 | } |
| 733 | |
| 734 | size_t ZSTD_HC_compressBlock_lazy(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 735 | { |
| 736 | return ZSTD_HC_compressBlock_lazy_generic(ctx, dst, maxDstSize, src, srcSize, 0, 0); |
| 737 | } |
| 738 | |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 739 | |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 740 | size_t ZSTD_HC_compressBlock_greedy(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 741 | { |
| 742 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 743 | const BYTE* const istart = (const BYTE*)src; |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 744 | const BYTE* ip = istart; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 745 | const BYTE* anchor = istart; |
| 746 | const BYTE* const iend = istart + srcSize; |
| 747 | const BYTE* const ilimit = iend - 8; |
| 748 | |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 749 | size_t offset_2=REPCODE_STARTVALUE, offset_1=REPCODE_STARTVALUE; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 750 | const U32 maxSearches = 1 << ctx->params.searchLog; |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 751 | const U32 mls = ctx->params.searchLength; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 752 | |
| 753 | /* init */ |
| 754 | ZSTD_resetSeqStore(seqStorePtr); |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 755 | if (((ip-ctx->base) - ctx->dictLimit) < REPCODE_STARTVALUE) ip += REPCODE_STARTVALUE; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 756 | |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 757 | /* Match Loop */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 758 | while (ip < ilimit) |
| 759 | { |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 760 | /* repcode */ |
| 761 | if (MEM_read32(ip) == MEM_read32(ip - offset_2)) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 762 | { |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 763 | /* store sequence */ |
Yann Collet | 342892c | 2015-10-26 17:44:04 +0100 | [diff] [blame] | 764 | size_t matchLength = ZSTD_count(ip+MINMATCH, ip+MINMATCH-offset_2, iend); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 765 | size_t litLength = ip-anchor; |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 766 | size_t offset = offset_2; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 767 | offset_2 = offset_1; |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 768 | offset_1 = offset; |
| 769 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength); |
| 770 | ip += matchLength+MINMATCH; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 771 | anchor = ip; |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 772 | continue; |
| 773 | } |
| 774 | |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 775 | offset_2 = offset_1; /* failed once : necessarily offset_1 now */ |
| 776 | |
Yann Collet | 342892c | 2015-10-26 17:44:04 +0100 | [diff] [blame] | 777 | /* repcode at ip+1 */ |
| 778 | if (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1)) |
| 779 | { |
| 780 | size_t matchLength = ZSTD_count(ip+1+MINMATCH, ip+1+MINMATCH-offset_1, iend); |
| 781 | size_t litLength = ip+1-anchor; |
Yann Collet | 342892c | 2015-10-26 17:44:04 +0100 | [diff] [blame] | 782 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, 0, matchLength); |
| 783 | ip += 1+matchLength+MINMATCH; |
| 784 | anchor = ip; |
| 785 | continue; |
| 786 | } |
| 787 | |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 788 | /* search */ |
| 789 | { |
Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 790 | size_t offset=999999; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 791 | size_t matchLength = ZSTD_HC_HcFindBestMatch_selectMLS(ctx, ip, iend, &offset, maxSearches, mls); |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 792 | if (!matchLength) { ip++; continue; } |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 793 | /* store sequence */ |
| 794 | { |
| 795 | size_t litLength = ip-anchor; |
Yann Collet | 050efba | 2015-11-03 09:49:30 +0100 | [diff] [blame] | 796 | offset_1 = offset; |
Yann Collet | 342892c | 2015-10-26 17:44:04 +0100 | [diff] [blame] | 797 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, offset_1, matchLength-MINMATCH); |
Yann Collet | ed0a781 | 2015-10-23 19:25:06 +0100 | [diff] [blame] | 798 | ip += matchLength; |
| 799 | anchor = ip; |
| 800 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | |
| 804 | /* Last Literals */ |
| 805 | { |
| 806 | size_t lastLLSize = iend - anchor; |
| 807 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 808 | seqStorePtr->lit += lastLLSize; |
| 809 | } |
| 810 | |
Yann Collet | 2c6992e | 2015-10-27 12:18:00 +0100 | [diff] [blame] | 811 | /* Final compression stage */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 812 | return ZSTD_compressSequences((BYTE*)dst, maxDstSize, |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 813 | |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 814 | seqStorePtr, srcSize); |
| 815 | } |
| 816 | |
| 817 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 818 | typedef size_t (*ZSTD_HC_blockCompressor) (ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize); |
| 819 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 820 | static ZSTD_HC_blockCompressor ZSTD_HC_selectBlockCompressor(ZSTD_HC_strategy strat) |
| 821 | { |
| 822 | switch(strat) |
| 823 | { |
| 824 | default : |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 825 | case ZSTD_HC_fast: |
| 826 | return ZSTD_HC_compressBlock_fast; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 827 | case ZSTD_HC_greedy: |
| 828 | return ZSTD_HC_compressBlock_greedy; |
| 829 | case ZSTD_HC_lazy: |
| 830 | return ZSTD_HC_compressBlock_lazy; |
Yann Collet | 47b6890 | 2015-11-05 15:14:17 +0100 | [diff] [blame] | 831 | case ZSTD_HC_lazy2: |
| 832 | return ZSTD_HC_compressBlock_lazy2; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 833 | case ZSTD_HC_btlazy2: |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 834 | return ZSTD_HC_compressBlock_btlazy2; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
| 838 | |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 839 | size_t ZSTD_HC_compressBlock(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize) |
| 840 | { |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 841 | ZSTD_HC_blockCompressor blockCompressor = ZSTD_HC_selectBlockCompressor(ctx->params.strategy); |
| 842 | return blockCompressor(ctx, dst, maxDstSize, src, srcSize); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 846 | static size_t ZSTD_HC_compress_generic (ZSTD_HC_CCtx* ctxPtr, |
| 847 | void* dst, size_t maxDstSize, |
| 848 | const void* src, size_t srcSize) |
| 849 | { |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 850 | size_t blockSize = BLOCKSIZE; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 851 | size_t remaining = srcSize; |
| 852 | const BYTE* ip = (const BYTE*)src; |
| 853 | BYTE* const ostart = (BYTE*)dst; |
| 854 | BYTE* op = ostart; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 855 | const ZSTD_HC_blockCompressor blockCompressor = ZSTD_HC_selectBlockCompressor(ctxPtr->params.strategy); |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 856 | |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 857 | while (remaining) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 858 | { |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 859 | size_t cSize; |
| 860 | |
Yann Collet | 3137d1a | 2015-11-04 23:36:36 +0100 | [diff] [blame] | 861 | if (maxDstSize < 3 + 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] | 862 | |
| 863 | if (remaining < blockSize) blockSize = remaining; |
| 864 | cSize = blockCompressor(ctxPtr, op+3, maxDstSize-3, ip, blockSize); |
| 865 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 866 | |
| 867 | if (cSize == 0) |
| 868 | { |
| 869 | cSize = ZSTD_noCompressBlock(op, maxDstSize, ip, blockSize); /* block is not compressible */ |
| 870 | } |
| 871 | else |
| 872 | { |
| 873 | op[0] = (BYTE)(cSize>>16); |
| 874 | op[1] = (BYTE)(cSize>>8); |
| 875 | op[2] = (BYTE)cSize; |
| 876 | op[0] += (BYTE)(bt_compressed << 6); /* is a compressed block */ |
| 877 | cSize += 3; |
| 878 | } |
| 879 | |
| 880 | remaining -= blockSize; |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 881 | maxDstSize -= cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 882 | ip += blockSize; |
| 883 | op += cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | return op-ostart; |
| 887 | } |
| 888 | |
| 889 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 890 | size_t ZSTD_HC_compressContinue (ZSTD_HC_CCtx* ctxPtr, |
| 891 | void* dst, size_t dstSize, |
| 892 | const void* src, size_t srcSize) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 893 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 894 | const BYTE* const ip = (const BYTE*) src; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 895 | |
| 896 | /* Check if blocks follow each other */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 897 | if (ip != ctxPtr->end) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 898 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 899 | if (ctxPtr->end != NULL) |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 900 | ZSTD_HC_resetCCtx_advanced(ctxPtr, ctxPtr->params); /* just reset, but no need to re-alloc */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 901 | ctxPtr->base = ip; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 902 | } |
| 903 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 904 | ctxPtr->end = ip + srcSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 905 | return ZSTD_HC_compress_generic (ctxPtr, dst, dstSize, src, srcSize); |
| 906 | } |
| 907 | |
| 908 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 909 | size_t ZSTD_HC_compressBegin_advanced(ZSTD_HC_CCtx* ctx, |
| 910 | void* dst, size_t maxDstSize, |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 911 | const ZSTD_HC_parameters params) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 912 | { |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 913 | size_t errorCode; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 914 | if (maxDstSize < 4) return ERROR(dstSize_tooSmall); |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 915 | errorCode = ZSTD_HC_resetCCtx_advanced(ctx, params); |
| 916 | if (ZSTD_isError(errorCode)) return errorCode; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 917 | MEM_writeLE32(dst, ZSTD_magicNumber); /* Write Header */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 918 | return 4; |
| 919 | } |
| 920 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 921 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 922 | size_t ZSTD_HC_compressBegin(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, int compressionLevel) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 923 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 924 | if (compressionLevel<=0) compressionLevel = 1; |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 925 | if (compressionLevel > ZSTD_HC_MAX_CLEVEL) compressionLevel = ZSTD_HC_MAX_CLEVEL; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 926 | return ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, ZSTD_HC_defaultParameters[compressionLevel]); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 930 | size_t ZSTD_HC_compressEnd(ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize) |
| 931 | { |
| 932 | BYTE* op = (BYTE*)dst; |
| 933 | |
| 934 | /* Sanity check */ |
| 935 | (void)ctx; |
| 936 | if (maxDstSize < 3) return ERROR(dstSize_tooSmall); |
| 937 | |
| 938 | /* End of frame */ |
| 939 | op[0] = (BYTE)(bt_end << 6); |
| 940 | op[1] = 0; |
| 941 | op[2] = 0; |
| 942 | |
| 943 | return 3; |
| 944 | } |
| 945 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 946 | size_t ZSTD_HC_compress_advanced (ZSTD_HC_CCtx* ctx, |
| 947 | void* dst, size_t maxDstSize, |
| 948 | const void* src, size_t srcSize, |
| 949 | ZSTD_HC_parameters params) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 950 | { |
| 951 | BYTE* const ostart = (BYTE*)dst; |
| 952 | BYTE* op = ostart; |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 953 | size_t oSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 954 | |
Yann Collet | 71bcdb5 | 2015-10-29 17:08:03 +0100 | [diff] [blame] | 955 | /* correct params, to use less memory */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame^] | 956 | { |
| 957 | U32 srcLog = ZSTD_highbit((U32)srcSize-1) + 1; |
| 958 | U32 contentBtPlus = (ctx->params.strategy == ZSTD_HC_btlazy2); |
| 959 | if (params.windowLog > srcLog) params.windowLog = srcLog; |
| 960 | if (params.contentLog > srcLog+contentBtPlus) params.contentLog = srcLog+contentBtPlus; |
| 961 | } |
Yann Collet | 71bcdb5 | 2015-10-29 17:08:03 +0100 | [diff] [blame] | 962 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 963 | /* Header */ |
Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 964 | oSize = ZSTD_HC_compressBegin_advanced(ctx, dst, maxDstSize, params); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 965 | if(ZSTD_isError(oSize)) return oSize; |
| 966 | op += oSize; |
| 967 | maxDstSize -= oSize; |
| 968 | |
| 969 | /* body (compression) */ |
Yann Collet | 3d9cf7a | 2015-10-29 17:15:14 +0100 | [diff] [blame] | 970 | ctx->base = (const BYTE*)src; |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 971 | oSize = ZSTD_HC_compress_generic (ctx, op, maxDstSize, src, srcSize); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 972 | if(ZSTD_isError(oSize)) return oSize; |
| 973 | op += oSize; |
| 974 | maxDstSize -= oSize; |
| 975 | |
| 976 | /* Close frame */ |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 977 | oSize = ZSTD_HC_compressEnd(ctx, op, maxDstSize); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 978 | if(ZSTD_isError(oSize)) return oSize; |
| 979 | op += oSize; |
| 980 | |
| 981 | return (op - ostart); |
| 982 | } |
| 983 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 984 | size_t ZSTD_HC_compressCCtx (ZSTD_HC_CCtx* ctx, void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 985 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 986 | if (compressionLevel<=1) return ZSTD_compress(dst, maxDstSize, src, srcSize); /* fast mode */ |
Yann Collet | 786f5b5 | 2015-10-26 15:45:58 +0100 | [diff] [blame] | 987 | if (compressionLevel > ZSTD_HC_MAX_CLEVEL) compressionLevel = ZSTD_HC_MAX_CLEVEL; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 988 | return ZSTD_HC_compress_advanced(ctx, dst, maxDstSize, src, srcSize, ZSTD_HC_defaultParameters[compressionLevel]); |
| 989 | } |
| 990 | |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 991 | size_t ZSTD_HC_compress(void* dst, size_t maxDstSize, const void* src, size_t srcSize, int compressionLevel) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 992 | { |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 993 | size_t result; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 994 | ZSTD_HC_CCtx ctxBody; |
| 995 | memset(&ctxBody, 0, sizeof(ctxBody)); |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 996 | result = ZSTD_HC_compressCCtx(&ctxBody, dst, maxDstSize, src, srcSize, compressionLevel); |
| 997 | free(ctxBody.workSpace); |
| 998 | return result; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 999 | } |