Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 9 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 10 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 11 | /*-************************************* |
Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 12 | * Dependencies |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 13 | ***************************************/ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 14 | #include <string.h> /* memset */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 15 | #include "mem.h" |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 16 | #define FSE_STATIC_LINKING_ONLY /* FSE_encodeSymbol */ |
Yann Collet | d0e2cd1 | 2016-06-05 00:58:01 +0200 | [diff] [blame] | 17 | #include "fse.h" |
Yann Collet | 130fe11 | 2016-06-05 00:42:28 +0200 | [diff] [blame] | 18 | #define HUF_STATIC_LINKING_ONLY |
| 19 | #include "huf.h" |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 20 | #include "zstd_internal.h" /* includes zstd.h */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 21 | |
| 22 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 23 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 24 | * Constants |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 25 | ***************************************/ |
Yann Collet | bb60448 | 2016-03-19 15:18:42 +0100 | [diff] [blame] | 26 | static const U32 g_searchStrength = 8; /* control skip over incompressible data */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 27 | #define HASH_READ_SIZE 8 |
| 28 | typedef enum { ZSTDcs_created=0, ZSTDcs_init, ZSTDcs_ongoing, ZSTDcs_ending } ZSTD_compressionStage_e; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 29 | |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 30 | /* entropy tables always have same size */ |
| 31 | static size_t const hufCTable_size = HUF_CTABLE_SIZE(255); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 32 | static size_t const litlengthCTable_size = FSE_CTABLE_SIZE(LLFSELog, MaxLL); |
| 33 | static size_t const offcodeCTable_size = FSE_CTABLE_SIZE(OffFSELog, MaxOff); |
| 34 | static size_t const matchlengthCTable_size = FSE_CTABLE_SIZE(MLFSELog, MaxML); |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 35 | static size_t const entropyScratchSpace_size = HUF_WORKSPACE_SIZE; |
Yann Collet | a34a39c | 2017-04-20 18:17:58 -0700 | [diff] [blame] | 36 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 37 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 38 | /*-************************************* |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 39 | * Helper functions |
| 40 | ***************************************/ |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 41 | #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG==1) |
| 42 | # include <assert.h> |
| 43 | #else |
| 44 | # define assert(condition) ((void)0) |
| 45 | #endif |
| 46 | |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 47 | #define ZSTD_STATIC_ASSERT(c) { enum { ZSTD_static_assert = 1/(int)(!!(c)) }; } |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 48 | |
Yann Collet | 3f75d52 | 2017-03-31 17:11:38 -0700 | [diff] [blame] | 49 | size_t ZSTD_compressBound(size_t srcSize) { |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 50 | size_t const lowLimit = 256 KB; |
| 51 | size_t const margin = (srcSize < lowLimit) ? (lowLimit-srcSize) >> 12 : 0; /* from 64 to 0 */ |
Yann Collet | 3f75d52 | 2017-03-31 17:11:38 -0700 | [diff] [blame] | 52 | return srcSize + (srcSize >> 8) + margin; |
| 53 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 54 | |
| 55 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 56 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 57 | * Sequence storage |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 58 | ***************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 59 | static void ZSTD_resetSeqStore(seqStore_t* ssPtr) |
| 60 | { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 61 | ssPtr->lit = ssPtr->litStart; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 62 | ssPtr->sequences = ssPtr->sequencesStart; |
Yann Collet | 5d39357 | 2016-04-07 17:19:00 +0200 | [diff] [blame] | 63 | ssPtr->longLengthID = 0; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 67 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 68 | * Context memory management |
| 69 | ***************************************/ |
Yann Collet | aca113f | 2016-12-23 22:25:03 +0100 | [diff] [blame] | 70 | struct ZSTD_CCtx_s { |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 71 | const BYTE* nextSrc; /* next block here to continue on current prefix */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 72 | const BYTE* base; /* All regular indexes relative to this position */ |
| 73 | const BYTE* dictBase; /* extDict indexes relative to this position */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 74 | U32 dictLimit; /* below that point, need extDict */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 75 | U32 lowLimit; /* below that point, no more data */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 76 | U32 nextToUpdate; /* index from which to continue dictionary update */ |
inikep | cc52a97 | 2016-02-19 10:09:35 +0100 | [diff] [blame] | 77 | U32 nextToUpdate3; /* index from which to continue dictionary update */ |
inikep | 7adceef | 2016-03-23 15:53:38 +0100 | [diff] [blame] | 78 | U32 hashLog3; /* dispatch table : larger == faster, more memory */ |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 79 | U32 loadedDictEnd; /* index of end of dictionary */ |
| 80 | U32 forceWindow; /* force back-references to respect limit of 1<<wLog, even for dictionary */ |
Yann Collet | 14312d8 | 2017-02-23 23:42:12 -0800 | [diff] [blame] | 81 | U32 forceRawDict; /* Force loading dictionary in "content-only" mode (no header analysis) */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 82 | ZSTD_compressionStage_e stage; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 83 | U32 rep[ZSTD_REP_NUM]; |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 84 | U32 repToConfirm[ZSTD_REP_NUM]; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 85 | U32 dictID; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 86 | ZSTD_parameters params; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 87 | void* workSpace; |
| 88 | size_t workSpaceSize; |
Yann Collet | 120230b | 2015-12-02 14:00:45 +0100 | [diff] [blame] | 89 | size_t blockSize; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 90 | U64 frameContentSize; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 91 | U64 consumedSrcSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 92 | XXH64_state_t xxhState; |
inikep | 2866951 | 2016-06-02 13:04:18 +0200 | [diff] [blame] | 93 | ZSTD_customMem customMem; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 94 | |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 95 | seqStore_t seqStore; /* sequences storage ptrs */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 96 | U32* hashTable; |
inikep | cc52a97 | 2016-02-19 10:09:35 +0100 | [diff] [blame] | 97 | U32* hashTable3; |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 98 | U32* chainTable; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 99 | HUF_repeat hufCTable_repeatMode; |
| 100 | HUF_CElt* hufCTable; |
| 101 | U32 fseCTables_ready; |
Yann Collet | 71aaa32 | 2017-04-20 23:03:38 -0700 | [diff] [blame] | 102 | FSE_CTable* offcodeCTable; |
| 103 | FSE_CTable* matchlengthCTable; |
| 104 | FSE_CTable* litlengthCTable; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 105 | unsigned* entropyScratchSpace; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 108 | ZSTD_CCtx* ZSTD_createCCtx(void) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 109 | { |
inikep | 3763c77 | 2016-06-03 13:28:20 +0200 | [diff] [blame] | 110 | return ZSTD_createCCtx_advanced(defaultCustomMem); |
inikep | 50e82c0 | 2016-05-23 15:49:09 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) |
| 114 | { |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 115 | ZSTD_CCtx* cctx; |
inikep | 50e82c0 | 2016-05-23 15:49:09 +0200 | [diff] [blame] | 116 | |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 117 | if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; |
| 118 | if (!customMem.customAlloc || !customMem.customFree) return NULL; |
inikep | 107e243 | 2016-05-23 16:24:52 +0200 | [diff] [blame] | 119 | |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 120 | cctx = (ZSTD_CCtx*) ZSTD_malloc(sizeof(ZSTD_CCtx), customMem); |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 121 | if (!cctx) return NULL; |
| 122 | memset(cctx, 0, sizeof(ZSTD_CCtx)); |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 123 | cctx->customMem = customMem; |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 124 | return cctx; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 125 | } |
| 126 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 127 | size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 128 | { |
inikep | 3640396 | 2016-06-03 16:36:50 +0200 | [diff] [blame] | 129 | if (cctx==NULL) return 0; /* support free on NULL */ |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 130 | ZSTD_free(cctx->workSpace, cctx->customMem); |
| 131 | ZSTD_free(cctx, cctx->customMem); |
Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 132 | return 0; /* reserved as a potential error code in the future */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 133 | } |
| 134 | |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 135 | size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx) |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 136 | { |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 137 | if (cctx==NULL) return 0; /* support sizeof on NULL */ |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 138 | return sizeof(*cctx) + cctx->workSpaceSize; |
| 139 | } |
| 140 | |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 141 | size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) |
| 142 | { |
| 143 | switch(param) |
| 144 | { |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 145 | case ZSTD_p_forceWindow : cctx->forceWindow = value>0; cctx->loadedDictEnd = 0; return 0; |
Yann Collet | 14312d8 | 2017-02-23 23:42:12 -0800 | [diff] [blame] | 146 | case ZSTD_p_forceRawDict : cctx->forceRawDict = value>0; return 0; |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 147 | default: return ERROR(parameter_unknown); |
| 148 | } |
| 149 | } |
| 150 | |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 151 | const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) /* hidden interface */ |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 152 | { |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 153 | return &(ctx->seqStore); |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 156 | static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) |
| 157 | { |
| 158 | return cctx->params; |
| 159 | } |
| 160 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 161 | |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 162 | /** ZSTD_checkParams() : |
| 163 | ensure param values remain within authorized range. |
| 164 | @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] | 165 | size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams) |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 166 | { |
Yann Collet | ac8bace | 2016-09-07 14:54:23 +0200 | [diff] [blame] | 167 | # define CLAMPCHECK(val,min,max) { if ((val<min) | (val>max)) return ERROR(compressionParameter_unsupported); } |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 168 | CLAMPCHECK(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 169 | CLAMPCHECK(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 170 | CLAMPCHECK(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 171 | CLAMPCHECK(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
Yann Collet | 2e2e78d | 2017-03-29 16:02:47 -0700 | [diff] [blame] | 172 | CLAMPCHECK(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 173 | CLAMPCHECK(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 174 | if ((U32)(cParams.strategy) > (U32)ZSTD_btopt2) return ERROR(compressionParameter_unsupported); |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 179 | /** ZSTD_cycleLog() : |
| 180 | * condition for correct operation : hashLog > 1 */ |
| 181 | static U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat) |
| 182 | { |
| 183 | U32 const btScale = ((U32)strat >= (U32)ZSTD_btlazy2); |
| 184 | return hashLog - btScale; |
| 185 | } |
| 186 | |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 187 | /** ZSTD_adjustCParams() : |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 188 | optimize `cPar` for a given input (`srcSize` and `dictSize`). |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 189 | mostly downsizing to reduce memory consumption and initialization. |
| 190 | Both `srcSize` and `dictSize` are optional (use 0 if unknown), |
| 191 | but if both are 0, no optimization can be done. |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 192 | Note : cPar is considered validated at this stage. Use ZSTD_checkParams() to ensure that. */ |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 193 | ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 194 | { |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 195 | if (srcSize+dictSize == 0) return cPar; /* no size information available : no adjustment */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 196 | |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 197 | /* resize params, to use less memory when necessary */ |
Yann Collet | dd6466a | 2016-03-30 20:06:26 +0200 | [diff] [blame] | 198 | { U32 const minSrcSize = (srcSize==0) ? 500 : 0; |
| 199 | U64 const rSize = srcSize + dictSize + minSrcSize; |
Yann Collet | b59bf96 | 2016-04-04 14:53:16 +0200 | [diff] [blame] | 200 | if (rSize < ((U64)1<<ZSTD_WINDOWLOG_MAX)) { |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 201 | U32 const srcLog = MAX(ZSTD_HASHLOG_MIN, ZSTD_highbit32((U32)(rSize)-1) + 1); |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 202 | if (cPar.windowLog > srcLog) cPar.windowLog = srcLog; |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 203 | } } |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 204 | if (cPar.hashLog > cPar.windowLog) cPar.hashLog = cPar.windowLog; |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 205 | { U32 const cycleLog = ZSTD_cycleLog(cPar.chainLog, cPar.strategy); |
| 206 | if (cycleLog > cPar.windowLog) cPar.chainLog -= (cycleLog - cPar.windowLog); |
| 207 | } |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 208 | |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 209 | if (cPar.windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN) cPar.windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */ |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 210 | |
| 211 | return cPar; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | |
Yann Collet | 8847238 | 2016-07-14 17:05:38 +0200 | [diff] [blame] | 215 | size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams) |
Yann Collet | e74215e | 2016-03-19 16:09:09 +0100 | [diff] [blame] | 216 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 217 | size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << cParams.windowLog); |
| 218 | U32 const divider = (cParams.searchLength==3) ? 3 : 4; |
| 219 | size_t const maxNbSeq = blockSize / divider; |
| 220 | size_t const tokenSpace = blockSize + 11*maxNbSeq; |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 221 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 222 | size_t const chainSize = (cParams.strategy == ZSTD_fast) ? 0 : (1 << cParams.chainLog); |
| 223 | size_t const hSize = ((size_t)1) << cParams.hashLog; |
| 224 | U32 const hashLog3 = (cParams.searchLength>3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog); |
| 225 | size_t const h3Size = ((size_t)1) << hashLog3; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 226 | size_t const entropySpace = hufCTable_size + litlengthCTable_size |
Yann Collet | a408645 | 2017-04-20 23:09:39 -0700 | [diff] [blame] | 227 | + offcodeCTable_size + matchlengthCTable_size |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 228 | + entropyScratchSpace_size; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 229 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 230 | |
| 231 | size_t const optSpace = ((MaxML+1) + (MaxLL+1) + (MaxOff+1) + (1<<Litbits))*sizeof(U32) |
| 232 | + (ZSTD_OPT_NUM+1)*(sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t)); |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 233 | size_t const neededSpace = entropySpace + tableSpace + tokenSpace |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 234 | + (((cParams.strategy == ZSTD_btopt) || (cParams.strategy == ZSTD_btopt2)) ? optSpace : 0); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 235 | |
| 236 | return sizeof(ZSTD_CCtx) + neededSpace; |
Yann Collet | 2e91dde | 2016-03-08 12:22:11 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 239 | |
| 240 | static U32 ZSTD_equivalentParams(ZSTD_parameters param1, ZSTD_parameters param2) |
| 241 | { |
| 242 | return (param1.cParams.hashLog == param2.cParams.hashLog) |
| 243 | & (param1.cParams.chainLog == param2.cParams.chainLog) |
Yann Collet | edbcd9f | 2016-09-06 14:30:57 +0200 | [diff] [blame] | 244 | & (param1.cParams.strategy == param2.cParams.strategy) |
| 245 | & ((param1.cParams.searchLength==3) == (param2.cParams.searchLength==3)); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /*! ZSTD_continueCCtx() : |
| 249 | reuse CCtx without reset (note : requires no dictionary) */ |
| 250 | static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_parameters params, U64 frameContentSize) |
| 251 | { |
| 252 | U32 const end = (U32)(cctx->nextSrc - cctx->base); |
| 253 | cctx->params = params; |
| 254 | cctx->frameContentSize = frameContentSize; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 255 | cctx->consumedSrcSize = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 256 | cctx->lowLimit = end; |
| 257 | cctx->dictLimit = end; |
| 258 | cctx->nextToUpdate = end+1; |
| 259 | cctx->stage = ZSTDcs_init; |
| 260 | cctx->dictID = 0; |
| 261 | cctx->loadedDictEnd = 0; |
| 262 | { int i; for (i=0; i<ZSTD_REP_NUM; i++) cctx->rep[i] = repStartValue[i]; } |
Yann Collet | b624922 | 2016-09-06 09:54:22 +0200 | [diff] [blame] | 263 | cctx->seqStore.litLengthSum = 0; /* force reset of btopt stats */ |
| 264 | XXH64_reset(&cctx->xxhState, 0); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | typedef enum { ZSTDcrp_continue, ZSTDcrp_noMemset, ZSTDcrp_fullReset } ZSTD_compResetPolicy_e; |
| 269 | |
Yann Collet | 30fb499 | 2017-04-18 14:08:50 -0700 | [diff] [blame] | 270 | /*! ZSTD_resetCCtx_internal() : |
Sean Purcell | 3437bf2 | 2017-03-01 16:10:26 -0800 | [diff] [blame] | 271 | note : `params` must be validated */ |
Yann Collet | 30fb499 | 2017-04-18 14:08:50 -0700 | [diff] [blame] | 272 | static size_t ZSTD_resetCCtx_internal (ZSTD_CCtx* zc, |
Yann Collet | ea2ecdc | 2016-07-14 22:43:12 +0200 | [diff] [blame] | 273 | ZSTD_parameters params, U64 frameContentSize, |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 274 | ZSTD_compResetPolicy_e const crp) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 275 | { |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 276 | if (crp == ZSTDcrp_continue) |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 277 | if (ZSTD_equivalentParams(params, zc->params)) { |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 278 | zc->fseCTables_ready = 0; |
| 279 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 280 | return ZSTD_continueCCtx(zc, params, frameContentSize); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 281 | } |
inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 282 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 283 | { size_t const blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, (size_t)1 << params.cParams.windowLog); |
| 284 | U32 const divider = (params.cParams.searchLength==3) ? 3 : 4; |
| 285 | size_t const maxNbSeq = blockSize / divider; |
| 286 | size_t const tokenSpace = blockSize + 11*maxNbSeq; |
| 287 | size_t const chainSize = (params.cParams.strategy == ZSTD_fast) ? 0 : (1 << params.cParams.chainLog); |
| 288 | size_t const hSize = ((size_t)1) << params.cParams.hashLog; |
| 289 | U32 const hashLog3 = (params.cParams.searchLength>3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, params.cParams.windowLog); |
| 290 | size_t const h3Size = ((size_t)1) << hashLog3; |
| 291 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
| 292 | void* ptr; |
Yann Collet | e74215e | 2016-03-19 16:09:09 +0100 | [diff] [blame] | 293 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 294 | /* Check if workSpace is large enough, alloc a new one if needed */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 295 | { size_t const entropySpace = hufCTable_size + litlengthCTable_size |
Yann Collet | a408645 | 2017-04-20 23:09:39 -0700 | [diff] [blame] | 296 | + offcodeCTable_size + matchlengthCTable_size |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 297 | + entropyScratchSpace_size; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 298 | size_t const optPotentialSpace = ((MaxML+1) + (MaxLL+1) + (MaxOff+1) + (1<<Litbits)) * sizeof(U32) |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 299 | + (ZSTD_OPT_NUM+1) * (sizeof(ZSTD_match_t)+sizeof(ZSTD_optimal_t)); |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 300 | size_t const optSpace = ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) ? optPotentialSpace : 0; |
| 301 | size_t const neededSpace = entropySpace + optSpace + tableSpace + tokenSpace; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 302 | if (zc->workSpaceSize < neededSpace) { |
Yann Collet | 0181fef | 2017-04-06 01:25:26 -0700 | [diff] [blame] | 303 | zc->workSpaceSize = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 304 | ZSTD_free(zc->workSpace, zc->customMem); |
| 305 | zc->workSpace = ZSTD_malloc(neededSpace, zc->customMem); |
| 306 | if (zc->workSpace == NULL) return ERROR(memory_allocation); |
| 307 | zc->workSpaceSize = neededSpace; |
Yann Collet | 7bb60b1 | 2017-04-20 17:38:56 -0700 | [diff] [blame] | 308 | ptr = zc->workSpace; |
| 309 | |
| 310 | /* entropy space */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 311 | zc->hufCTable = (HUF_CElt*)ptr; |
| 312 | ptr = (char*)zc->hufCTable + hufCTable_size; /* note : HUF_CElt* is incomplete type, size is estimated via macro */ |
Yann Collet | 71aaa32 | 2017-04-20 23:03:38 -0700 | [diff] [blame] | 313 | zc->offcodeCTable = (FSE_CTable*) ptr; |
| 314 | ptr = (char*)ptr + offcodeCTable_size; |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 315 | zc->matchlengthCTable = (FSE_CTable*) ptr; |
Yann Collet | 71aaa32 | 2017-04-20 23:03:38 -0700 | [diff] [blame] | 316 | ptr = (char*)ptr + matchlengthCTable_size; |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 317 | zc->litlengthCTable = (FSE_CTable*) ptr; |
| 318 | ptr = (char*)ptr + litlengthCTable_size; |
| 319 | assert(((size_t)ptr & 3) == 0); /* ensure correct alignment */ |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 320 | zc->entropyScratchSpace = (unsigned*) ptr; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 321 | } } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 322 | |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 323 | /* init params */ |
| 324 | zc->params = params; |
| 325 | zc->blockSize = blockSize; |
| 326 | zc->frameContentSize = frameContentSize; |
| 327 | zc->consumedSrcSize = 0; |
| 328 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 329 | XXH64_reset(&zc->xxhState, 0); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 330 | zc->stage = ZSTDcs_init; |
| 331 | zc->dictID = 0; |
| 332 | zc->loadedDictEnd = 0; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 333 | zc->fseCTables_ready = 0; |
| 334 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 335 | zc->nextToUpdate = 1; |
| 336 | zc->nextSrc = NULL; |
| 337 | zc->base = NULL; |
| 338 | zc->dictBase = NULL; |
| 339 | zc->dictLimit = 0; |
| 340 | zc->lowLimit = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 341 | { int i; for (i=0; i<ZSTD_REP_NUM; i++) zc->rep[i] = repStartValue[i]; } |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 342 | zc->hashLog3 = hashLog3; |
| 343 | zc->seqStore.litLengthSum = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 344 | |
Yann Collet | 71aaa32 | 2017-04-20 23:03:38 -0700 | [diff] [blame] | 345 | /* ensure entropy tables are close together at the beginning */ |
| 346 | assert((void*)zc->hufCTable == zc->workSpace); |
| 347 | assert((char*)zc->offcodeCTable == (char*)zc->hufCTable + hufCTable_size); |
| 348 | assert((char*)zc->matchlengthCTable == (char*)zc->offcodeCTable + offcodeCTable_size); |
| 349 | assert((char*)zc->litlengthCTable == (char*)zc->matchlengthCTable + matchlengthCTable_size); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 350 | assert((char*)zc->entropyScratchSpace == (char*)zc->litlengthCTable + litlengthCTable_size); |
| 351 | ptr = (char*)zc->entropyScratchSpace + entropyScratchSpace_size; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 352 | |
| 353 | /* opt parser space */ |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 354 | if ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btopt2)) { |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 355 | assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 356 | zc->seqStore.litFreq = (U32*)ptr; |
| 357 | zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<<Litbits); |
| 358 | zc->seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (MaxLL+1); |
| 359 | zc->seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (MaxML+1); |
| 360 | ptr = zc->seqStore.offCodeFreq + (MaxOff+1); |
| 361 | zc->seqStore.matchTable = (ZSTD_match_t*)ptr; |
| 362 | ptr = zc->seqStore.matchTable + ZSTD_OPT_NUM+1; |
| 363 | zc->seqStore.priceTable = (ZSTD_optimal_t*)ptr; |
| 364 | ptr = zc->seqStore.priceTable + ZSTD_OPT_NUM+1; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 365 | } |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 366 | |
| 367 | /* table Space */ |
| 368 | if (crp!=ZSTDcrp_noMemset) memset(ptr, 0, tableSpace); /* reset tables only */ |
| 369 | assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ |
| 370 | zc->hashTable = (U32*)(ptr); |
| 371 | zc->chainTable = zc->hashTable + hSize; |
| 372 | zc->hashTable3 = zc->chainTable + chainSize; |
| 373 | ptr = zc->hashTable3 + h3Size; |
| 374 | |
| 375 | /* sequences storage */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 376 | zc->seqStore.sequencesStart = (seqDef*)ptr; |
| 377 | ptr = zc->seqStore.sequencesStart + maxNbSeq; |
| 378 | zc->seqStore.llCode = (BYTE*) ptr; |
| 379 | zc->seqStore.mlCode = zc->seqStore.llCode + maxNbSeq; |
| 380 | zc->seqStore.ofCode = zc->seqStore.mlCode + maxNbSeq; |
| 381 | zc->seqStore.litStart = zc->seqStore.ofCode + maxNbSeq; |
| 382 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 383 | return 0; |
Yann Collet | 72d706a | 2016-03-23 20:44:12 +0100 | [diff] [blame] | 384 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 385 | } |
| 386 | |
Yann Collet | 32dfae6 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 387 | /* ZSTD_invalidateRepCodes() : |
| 388 | * ensures next compression will not use repcodes from previous block. |
| 389 | * Note : only works with regular variant; |
| 390 | * do not use with extDict variant ! */ |
| 391 | void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx) { |
| 392 | int i; |
| 393 | for (i=0; i<ZSTD_REP_NUM; i++) cctx->rep[i] = 0; |
| 394 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 395 | |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 396 | |
| 397 | /*! ZSTD_copyCCtx_internal() : |
| 398 | * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. |
| 399 | * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). |
| 400 | * pledgedSrcSize=0 means "empty" if fParams.contentSizeFlag=1 |
| 401 | * @return : 0, or an error code */ |
| 402 | size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx, |
| 403 | ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize) |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 404 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 405 | if (srcCCtx->stage!=ZSTDcs_init) return ERROR(stage_wrong); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 406 | |
inikep | 2866951 | 2016-06-02 13:04:18 +0200 | [diff] [blame] | 407 | memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem)); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 408 | { ZSTD_parameters params = srcCCtx->params; |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 409 | params.fParams = fParams; |
Yann Collet | 30fb499 | 2017-04-18 14:08:50 -0700 | [diff] [blame] | 410 | ZSTD_resetCCtx_internal(dstCCtx, params, pledgedSrcSize, ZSTDcrp_noMemset); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 411 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 412 | |
| 413 | /* copy tables */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 414 | { size_t const chainSize = (srcCCtx->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << srcCCtx->params.cParams.chainLog); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 415 | size_t const hSize = (size_t)1 << srcCCtx->params.cParams.hashLog; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 416 | size_t const h3Size = (size_t)1 << srcCCtx->hashLog3; |
| 417 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 418 | assert((U32*)dstCCtx->chainTable == (U32*)dstCCtx->hashTable + hSize); /* chainTable must follow hashTable */ |
| 419 | assert((U32*)dstCCtx->hashTable3 == (U32*)dstCCtx->chainTable + chainSize); |
| 420 | memcpy(dstCCtx->hashTable, srcCCtx->hashTable, tableSpace); /* presumes all tables follow each other */ |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 421 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 422 | |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 423 | /* copy dictionary offsets */ |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 424 | dstCCtx->nextToUpdate = srcCCtx->nextToUpdate; |
| 425 | dstCCtx->nextToUpdate3= srcCCtx->nextToUpdate3; |
| 426 | dstCCtx->nextSrc = srcCCtx->nextSrc; |
| 427 | dstCCtx->base = srcCCtx->base; |
| 428 | dstCCtx->dictBase = srcCCtx->dictBase; |
| 429 | dstCCtx->dictLimit = srcCCtx->dictLimit; |
| 430 | dstCCtx->lowLimit = srcCCtx->lowLimit; |
| 431 | dstCCtx->loadedDictEnd= srcCCtx->loadedDictEnd; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 432 | dstCCtx->dictID = srcCCtx->dictID; |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 433 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 434 | /* copy entropy tables */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 435 | dstCCtx->fseCTables_ready = srcCCtx->fseCTables_ready; |
| 436 | if (srcCCtx->fseCTables_ready) { |
Yann Collet | a34a39c | 2017-04-20 18:17:58 -0700 | [diff] [blame] | 437 | memcpy(dstCCtx->litlengthCTable, srcCCtx->litlengthCTable, litlengthCTable_size); |
| 438 | memcpy(dstCCtx->matchlengthCTable, srcCCtx->matchlengthCTable, matchlengthCTable_size); |
| 439 | memcpy(dstCCtx->offcodeCTable, srcCCtx->offcodeCTable, offcodeCTable_size); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 440 | } |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 441 | dstCCtx->hufCTable_repeatMode = srcCCtx->hufCTable_repeatMode; |
| 442 | if (srcCCtx->hufCTable_repeatMode) { |
| 443 | memcpy(dstCCtx->hufCTable, srcCCtx->hufCTable, hufCTable_size); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 444 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 449 | /*! ZSTD_copyCCtx() : |
| 450 | * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. |
| 451 | * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). |
| 452 | * pledgedSrcSize==0 means "unknown". |
| 453 | * @return : 0, or an error code */ |
| 454 | size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx, unsigned long long pledgedSrcSize) |
| 455 | { |
| 456 | ZSTD_frameParameters fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
| 457 | fParams.contentSizeFlag = pledgedSrcSize>0; |
| 458 | |
| 459 | return ZSTD_copyCCtx_internal(dstCCtx, srcCCtx, fParams, pledgedSrcSize); |
| 460 | } |
| 461 | |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 462 | |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 463 | /*! ZSTD_reduceTable() : |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 464 | * reduce table indexes by `reducerValue` */ |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 465 | 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] | 466 | { |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 467 | U32 u; |
| 468 | for (u=0 ; u < size ; u++) { |
| 469 | if (table[u] < reducerValue) table[u] = 0; |
| 470 | else table[u] -= reducerValue; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 474 | /*! ZSTD_reduceIndex() : |
| 475 | * rescale all indexes to avoid future overflow (indexes are U32) */ |
| 476 | static void ZSTD_reduceIndex (ZSTD_CCtx* zc, const U32 reducerValue) |
| 477 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 478 | { U32 const hSize = 1 << zc->params.cParams.hashLog; |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 479 | ZSTD_reduceTable(zc->hashTable, hSize, reducerValue); } |
| 480 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 481 | { U32 const chainSize = (zc->params.cParams.strategy == ZSTD_fast) ? 0 : (1 << zc->params.cParams.chainLog); |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 482 | ZSTD_reduceTable(zc->chainTable, chainSize, reducerValue); } |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 483 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 484 | { U32 const h3Size = (zc->hashLog3) ? 1 << zc->hashLog3 : 0; |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 485 | ZSTD_reduceTable(zc->hashTable3, h3Size, reducerValue); } |
| 486 | } |
| 487 | |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 488 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 489 | /*-******************************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 490 | * Block entropic compression |
| 491 | *********************************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 492 | |
Przemyslaw Skibinski | 3ee94a7 | 2016-10-24 15:58:07 +0200 | [diff] [blame] | 493 | /* See doc/zstd_compression_format.md for detailed format description */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 494 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 495 | 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] | 496 | { |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 497 | if (srcSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 498 | memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize); |
| 499 | MEM_writeLE24(dst, (U32)(srcSize << 2) + (U32)bt_raw); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 500 | return ZSTD_blockHeaderSize+srcSize; |
| 501 | } |
| 502 | |
| 503 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 504 | 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] | 505 | { |
| 506 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 507 | U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 508 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 509 | if (srcSize + flSize > dstCapacity) return ERROR(dstSize_tooSmall); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 510 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 511 | switch(flSize) |
| 512 | { |
| 513 | case 1: /* 2 - 1 - 5 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 514 | ostart[0] = (BYTE)((U32)set_basic + (srcSize<<3)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 515 | break; |
| 516 | case 2: /* 2 - 2 - 12 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 517 | MEM_writeLE16(ostart, (U16)((U32)set_basic + (1<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 518 | break; |
| 519 | default: /*note : should not be necessary : flSize is within {1,2,3} */ |
| 520 | case 3: /* 2 - 2 - 20 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 521 | MEM_writeLE32(ostart, (U32)((U32)set_basic + (3<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | |
| 525 | memcpy(ostart + flSize, src, srcSize); |
| 526 | return srcSize + flSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 527 | } |
| 528 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 529 | 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] | 530 | { |
| 531 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 532 | U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 533 | |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 534 | (void)dstCapacity; /* dstCapacity already guaranteed to be >=4, hence large enough */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 535 | |
| 536 | switch(flSize) |
| 537 | { |
| 538 | case 1: /* 2 - 1 - 5 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 539 | ostart[0] = (BYTE)((U32)set_rle + (srcSize<<3)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 540 | break; |
| 541 | case 2: /* 2 - 2 - 12 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 542 | MEM_writeLE16(ostart, (U16)((U32)set_rle + (1<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 543 | break; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 544 | 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] | 545 | case 3: /* 2 - 2 - 20 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 546 | MEM_writeLE32(ostart, (U32)((U32)set_rle + (3<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 547 | break; |
| 548 | } |
| 549 | |
| 550 | ostart[flSize] = *(const BYTE*)src; |
| 551 | return flSize+1; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 552 | } |
| 553 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 554 | |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 555 | static size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 2; } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 556 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 557 | static size_t ZSTD_compressLiterals (ZSTD_CCtx* zc, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 558 | void* dst, size_t dstCapacity, |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 559 | const void* src, size_t srcSize) |
| 560 | { |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 561 | size_t const minGain = ZSTD_minGain(srcSize); |
| 562 | size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 563 | BYTE* const ostart = (BYTE*)dst; |
Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 564 | U32 singleStream = srcSize < 256; |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 565 | symbolEncodingType_e hType = set_compressed; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 566 | size_t cLitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 567 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 568 | |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 569 | /* small ? don't even attempt compression (speed opt) */ |
| 570 | # define LITERAL_NOENTROPY 63 |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 571 | { size_t const minLitSize = zc->hufCTable_repeatMode == HUF_repeat_valid ? 6 : LITERAL_NOENTROPY; |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 572 | if (srcSize <= minLitSize) return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); |
| 573 | } |
| 574 | |
| 575 | if (dstCapacity < lhSize+1) return ERROR(dstSize_tooSmall); /* not enough space for compression */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 576 | { HUF_repeat repeat = zc->hufCTable_repeatMode; |
Nick Terrell | 54c4bab | 2017-03-03 12:30:24 -0800 | [diff] [blame] | 577 | int const preferRepeat = zc->params.cParams.strategy < ZSTD_lazy ? srcSize <= 1024 : 0; |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 578 | if (repeat == HUF_repeat_valid && lhSize == 3) singleStream = 1; |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 579 | cLitSize = singleStream ? HUF_compress1X_repeat(ostart+lhSize, dstCapacity-lhSize, src, srcSize, 255, 11, |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 580 | zc->entropyScratchSpace, entropyScratchSpace_size, zc->hufCTable, &repeat, preferRepeat) |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 581 | : HUF_compress4X_repeat(ostart+lhSize, dstCapacity-lhSize, src, srcSize, 255, 11, |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 582 | zc->entropyScratchSpace, entropyScratchSpace_size, zc->hufCTable, &repeat, preferRepeat); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 583 | if (repeat != HUF_repeat_none) { hType = set_repeat; } /* reused the existing table */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 584 | else { zc->hufCTable_repeatMode = HUF_repeat_check; } /* now have a table to reuse */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 585 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 586 | |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 587 | if ((cLitSize==0) | (cLitSize >= srcSize - minGain)) { |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 588 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 589 | return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 590 | } |
| 591 | if (cLitSize==1) { |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 592 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 593 | return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 594 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 595 | |
| 596 | /* Build header */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 597 | switch(lhSize) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 598 | { |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 599 | case 3: /* 2 - 2 - 10 - 10 */ |
Yann Collet | c2e1a68 | 2016-07-22 17:30:52 +0200 | [diff] [blame] | 600 | { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<14); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 601 | MEM_writeLE24(ostart, lhc); |
| 602 | break; |
| 603 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 604 | case 4: /* 2 - 2 - 14 - 14 */ |
Yann Collet | 32faf6c | 2016-07-22 04:45:06 +0200 | [diff] [blame] | 605 | { U32 const lhc = hType + (2 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<18); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 606 | MEM_writeLE32(ostart, lhc); |
| 607 | break; |
| 608 | } |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 609 | default: /* should not be necessary, lhSize is only {3,4,5} */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 610 | case 5: /* 2 - 2 - 18 - 18 */ |
Yann Collet | 32faf6c | 2016-07-22 04:45:06 +0200 | [diff] [blame] | 611 | { U32 const lhc = hType + (3 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<22); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 612 | MEM_writeLE32(ostart, lhc); |
| 613 | ostart[4] = (BYTE)(cLitSize >> 10); |
| 614 | break; |
| 615 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 616 | } |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 617 | return lhSize+cLitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 618 | } |
| 619 | |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 620 | static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7, |
| 621 | 8, 9, 10, 11, 12, 13, 14, 15, |
| 622 | 16, 16, 17, 17, 18, 18, 19, 19, |
| 623 | 20, 20, 20, 20, 21, 21, 21, 21, |
| 624 | 22, 22, 22, 22, 22, 22, 22, 22, |
| 625 | 23, 23, 23, 23, 23, 23, 23, 23, |
| 626 | 24, 24, 24, 24, 24, 24, 24, 24, |
| 627 | 24, 24, 24, 24, 24, 24, 24, 24 }; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 628 | |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 629 | static const BYTE ML_Code[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 630 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 631 | 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, |
| 632 | 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, |
| 633 | 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, |
| 634 | 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, |
| 635 | 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, |
| 636 | 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42 }; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 637 | |
| 638 | |
| 639 | void ZSTD_seqToCodes(const seqStore_t* seqStorePtr) |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 640 | { |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 641 | BYTE const LL_deltaCode = 19; |
| 642 | BYTE const ML_deltaCode = 36; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 643 | const seqDef* const sequences = seqStorePtr->sequencesStart; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 644 | BYTE* const llCodeTable = seqStorePtr->llCode; |
| 645 | BYTE* const ofCodeTable = seqStorePtr->ofCode; |
| 646 | BYTE* const mlCodeTable = seqStorePtr->mlCode; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 647 | U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 648 | U32 u; |
| 649 | for (u=0; u<nbSeq; u++) { |
| 650 | U32 const llv = sequences[u].litLength; |
| 651 | U32 const mlv = sequences[u].matchLength; |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 652 | llCodeTable[u] = (llv> 63) ? (BYTE)ZSTD_highbit32(llv) + LL_deltaCode : LL_Code[llv]; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 653 | ofCodeTable[u] = (BYTE)ZSTD_highbit32(sequences[u].offset); |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 654 | mlCodeTable[u] = (mlv>127) ? (BYTE)ZSTD_highbit32(mlv) + ML_deltaCode : ML_Code[mlv]; |
Yann Collet | 5d39357 | 2016-04-07 17:19:00 +0200 | [diff] [blame] | 655 | } |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 656 | if (seqStorePtr->longLengthID==1) |
| 657 | llCodeTable[seqStorePtr->longLengthPos] = MaxLL; |
| 658 | if (seqStorePtr->longLengthID==2) |
| 659 | mlCodeTable[seqStorePtr->longLengthPos] = MaxML; |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 660 | } |
| 661 | |
Sean Purcell | 553f67e | 2017-03-02 15:15:31 -0800 | [diff] [blame] | 662 | MEM_STATIC size_t ZSTD_compressSequences (ZSTD_CCtx* zc, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 663 | void* dst, size_t dstCapacity, |
Sean Purcell | 553f67e | 2017-03-02 15:15:31 -0800 | [diff] [blame] | 664 | size_t srcSize) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 665 | { |
Sean Purcell | 553f67e | 2017-03-02 15:15:31 -0800 | [diff] [blame] | 666 | const int longOffsets = zc->params.cParams.windowLog > STREAM_ACCUMULATOR_MIN; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 667 | const seqStore_t* seqStorePtr = &(zc->seqStore); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 668 | U32 count[MaxSeq+1]; |
| 669 | S16 norm[MaxSeq+1]; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 670 | FSE_CTable* CTable_LitLength = zc->litlengthCTable; |
| 671 | FSE_CTable* CTable_OffsetBits = zc->offcodeCTable; |
| 672 | FSE_CTable* CTable_MatchLength = zc->matchlengthCTable; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 673 | U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */ |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 674 | const seqDef* const sequences = seqStorePtr->sequencesStart; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 675 | const BYTE* const ofCodeTable = seqStorePtr->ofCode; |
| 676 | const BYTE* const llCodeTable = seqStorePtr->llCode; |
| 677 | const BYTE* const mlCodeTable = seqStorePtr->mlCode; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 678 | BYTE* const ostart = (BYTE*)dst; |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 679 | BYTE* const oend = ostart + dstCapacity; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 680 | BYTE* op = ostart; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 681 | size_t const nbSeq = seqStorePtr->sequences - seqStorePtr->sequencesStart; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 682 | BYTE* seqHead; |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 683 | BYTE scratchBuffer[1<<MAX(MLFSELog,LLFSELog)]; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 684 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 685 | /* Compress literals */ |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 686 | { const BYTE* const literals = seqStorePtr->litStart; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 687 | size_t const litSize = seqStorePtr->lit - literals; |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 688 | size_t const cSize = ZSTD_compressLiterals(zc, op, dstCapacity, literals, litSize); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 689 | if (ZSTD_isError(cSize)) return cSize; |
| 690 | op += cSize; |
| 691 | } |
| 692 | |
| 693 | /* Sequences Header */ |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 694 | 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] | 695 | if (nbSeq < 0x7F) *op++ = (BYTE)nbSeq; |
| 696 | else if (nbSeq < LONGNBSEQ) op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2; |
| 697 | 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] | 698 | if (nbSeq==0) goto _check_compressibility; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 699 | |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 700 | /* seqHead : flags for FSE encoding type */ |
| 701 | seqHead = op++; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 702 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 703 | #define MIN_SEQ_FOR_DYNAMIC_FSE 64 |
| 704 | #define MAX_SEQ_FOR_STATIC_FSE 1000 |
| 705 | |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 706 | /* convert length/distances into codes */ |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 707 | ZSTD_seqToCodes(seqStorePtr); |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 708 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 709 | /* CTable for Literal Lengths */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 710 | { U32 max = MaxLL; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 711 | size_t const mostFrequent = FSE_countFast_wksp(count, &max, llCodeTable, nbSeq, zc->entropyScratchSpace); |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 712 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
| 713 | *op++ = llCodeTable[0]; |
| 714 | FSE_buildCTable_rle(CTable_LitLength, (BYTE)max); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 715 | LLtype = set_rle; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 716 | } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 717 | LLtype = set_repeat; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 718 | } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (LL_defaultNormLog-1)))) { |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 719 | FSE_buildCTable_wksp(CTable_LitLength, LL_defaultNorm, MaxLL, LL_defaultNormLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 720 | LLtype = set_basic; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 721 | } else { |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 722 | size_t nbSeq_1 = nbSeq; |
| 723 | const U32 tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max); |
| 724 | if (count[llCodeTable[nbSeq-1]]>1) { count[llCodeTable[nbSeq-1]]--; nbSeq_1--; } |
| 725 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 726 | { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ |
Yann Collet | 23776ce | 2017-03-23 17:59:50 -0700 | [diff] [blame] | 727 | if (FSE_isError(NCountSize)) return NCountSize; |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 728 | op += NCountSize; } |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 729 | FSE_buildCTable_wksp(CTable_LitLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 730 | LLtype = set_compressed; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 731 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 732 | |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 733 | /* CTable for Offsets */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 734 | { U32 max = MaxOff; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 735 | size_t const mostFrequent = FSE_countFast_wksp(count, &max, ofCodeTable, nbSeq, zc->entropyScratchSpace); |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 736 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 737 | *op++ = ofCodeTable[0]; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 738 | FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 739 | Offtype = set_rle; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 740 | } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 741 | Offtype = set_repeat; |
Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 742 | } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (OF_defaultNormLog-1)))) { |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 743 | FSE_buildCTable_wksp(CTable_OffsetBits, OF_defaultNorm, MaxOff, OF_defaultNormLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 744 | Offtype = set_basic; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 745 | } else { |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 746 | size_t nbSeq_1 = nbSeq; |
| 747 | const U32 tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max); |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 748 | if (count[ofCodeTable[nbSeq-1]]>1) { count[ofCodeTable[nbSeq-1]]--; nbSeq_1--; } |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 749 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 750 | { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ |
Yann Collet | 23776ce | 2017-03-23 17:59:50 -0700 | [diff] [blame] | 751 | if (FSE_isError(NCountSize)) return NCountSize; |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 752 | op += NCountSize; } |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 753 | FSE_buildCTable_wksp(CTable_OffsetBits, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 754 | Offtype = set_compressed; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 755 | } } |
| 756 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 757 | /* CTable for MatchLengths */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 758 | { U32 max = MaxML; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 759 | size_t const mostFrequent = FSE_countFast_wksp(count, &max, mlCodeTable, nbSeq, zc->entropyScratchSpace); |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 760 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
Yann Collet | 72d706a | 2016-03-23 20:44:12 +0100 | [diff] [blame] | 761 | *op++ = *mlCodeTable; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 762 | FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 763 | MLtype = set_rle; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 764 | } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 765 | MLtype = set_repeat; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 766 | } else if ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (ML_defaultNormLog-1)))) { |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 767 | FSE_buildCTable_wksp(CTable_MatchLength, ML_defaultNorm, MaxML, ML_defaultNormLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 768 | MLtype = set_basic; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 769 | } else { |
| 770 | size_t nbSeq_1 = nbSeq; |
| 771 | const U32 tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max); |
| 772 | if (count[mlCodeTable[nbSeq-1]]>1) { count[mlCodeTable[nbSeq-1]]--; nbSeq_1--; } |
| 773 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
| 774 | { size_t const NCountSize = FSE_writeNCount(op, oend-op, norm, max, tableLog); /* overflow protected */ |
Yann Collet | 23776ce | 2017-03-23 17:59:50 -0700 | [diff] [blame] | 775 | if (FSE_isError(NCountSize)) return NCountSize; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 776 | op += NCountSize; } |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 777 | FSE_buildCTable_wksp(CTable_MatchLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 778 | MLtype = set_compressed; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 779 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 780 | |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 781 | *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2)); |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 782 | zc->fseCTables_ready = 0; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 783 | |
| 784 | /* Encoding Sequences */ |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 785 | { BIT_CStream_t blockStream; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 786 | FSE_CState_t stateMatchLength; |
| 787 | FSE_CState_t stateOffsetBits; |
| 788 | FSE_CState_t stateLitLength; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 789 | |
Yann Collet | 95d07d7 | 2016-09-06 16:38:51 +0200 | [diff] [blame] | 790 | CHECK_E(BIT_initCStream(&blockStream, op, oend-op), dstSize_tooSmall); /* not enough space remaining */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 791 | |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 792 | /* first symbols */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 793 | FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]); |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 794 | FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]); |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 795 | FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 796 | BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 797 | if (MEM_32bits()) BIT_flushBits(&blockStream); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 798 | BIT_addBits(&blockStream, sequences[nbSeq-1].matchLength, ML_bits[mlCodeTable[nbSeq-1]]); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 799 | if (MEM_32bits()) BIT_flushBits(&blockStream); |
Sean Purcell | d44703d | 2017-03-01 14:36:25 -0800 | [diff] [blame] | 800 | if (longOffsets) { |
| 801 | U32 const ofBits = ofCodeTable[nbSeq-1]; |
| 802 | int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); |
| 803 | if (extraBits) { |
| 804 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset, extraBits); |
| 805 | BIT_flushBits(&blockStream); |
| 806 | } |
| 807 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset >> extraBits, |
| 808 | ofBits - extraBits); |
| 809 | } else { |
| 810 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset, ofCodeTable[nbSeq-1]); |
| 811 | } |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 812 | BIT_flushBits(&blockStream); |
| 813 | |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 814 | { size_t n; |
| 815 | for (n=nbSeq-2 ; n<nbSeq ; n--) { /* intentional underflow */ |
Yann Collet | 3c6b808 | 2016-07-30 03:20:47 +0200 | [diff] [blame] | 816 | BYTE const llCode = llCodeTable[n]; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 817 | BYTE const ofCode = ofCodeTable[n]; |
| 818 | BYTE const mlCode = mlCodeTable[n]; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 819 | U32 const llBits = LL_bits[llCode]; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 820 | U32 const ofBits = ofCode; /* 32b*/ /* 64b*/ |
Yann Collet | 3c6b808 | 2016-07-30 03:20:47 +0200 | [diff] [blame] | 821 | U32 const mlBits = ML_bits[mlCode]; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 822 | /* (7)*/ /* (7)*/ |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 823 | FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */ |
| 824 | FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */ |
| 825 | if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ |
| 826 | FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */ |
Yann Collet | 582933f | 2016-04-11 16:25:56 +0200 | [diff] [blame] | 827 | if (MEM_32bits() || (ofBits+mlBits+llBits >= 64-7-(LLFSELog+MLFSELog+OffFSELog))) |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 828 | BIT_flushBits(&blockStream); /* (7)*/ |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 829 | BIT_addBits(&blockStream, sequences[n].litLength, llBits); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 830 | if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 831 | BIT_addBits(&blockStream, sequences[n].matchLength, mlBits); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 832 | if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ |
Sean Purcell | d44703d | 2017-03-01 14:36:25 -0800 | [diff] [blame] | 833 | if (longOffsets) { |
| 834 | int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); |
| 835 | if (extraBits) { |
| 836 | BIT_addBits(&blockStream, sequences[n].offset, extraBits); |
| 837 | BIT_flushBits(&blockStream); /* (7)*/ |
| 838 | } |
| 839 | BIT_addBits(&blockStream, sequences[n].offset >> extraBits, |
| 840 | ofBits - extraBits); /* 31 */ |
| 841 | } else { |
| 842 | BIT_addBits(&blockStream, sequences[n].offset, ofBits); /* 31 */ |
| 843 | } |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 844 | BIT_flushBits(&blockStream); /* (7)*/ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 845 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 846 | |
| 847 | FSE_flushCState(&blockStream, &stateMatchLength); |
| 848 | FSE_flushCState(&blockStream, &stateOffsetBits); |
| 849 | FSE_flushCState(&blockStream, &stateLitLength); |
| 850 | |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 851 | { size_t const streamSize = BIT_closeCStream(&blockStream); |
| 852 | if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */ |
| 853 | op += streamSize; |
| 854 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 855 | |
| 856 | /* check compressibility */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 857 | _check_compressibility: |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 858 | { size_t const minGain = ZSTD_minGain(srcSize); |
| 859 | size_t const maxCSize = srcSize - minGain; |
| 860 | if ((size_t)(op-ostart) >= maxCSize) { |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 861 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 862 | return 0; |
| 863 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 864 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 865 | /* confirm repcodes */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 866 | { int i; for (i=0; i<ZSTD_REP_NUM; i++) zc->rep[i] = zc->repToConfirm[i]; } |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 867 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 868 | return op - ostart; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 869 | } |
| 870 | |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 871 | #if 0 /* for debug */ |
| 872 | # define STORESEQ_DEBUG |
| 873 | #include <stdio.h> /* fprintf */ |
| 874 | U32 g_startDebug = 0; |
| 875 | const BYTE* g_start = NULL; |
| 876 | #endif |
| 877 | |
Yann Collet | 95cd0c2 | 2016-03-08 18:24:21 +0100 | [diff] [blame] | 878 | /*! ZSTD_storeSeq() : |
| 879 | Store a sequence (literal length, literals, offset code and match length code) into seqStore_t. |
| 880 | `offsetCode` : distance to match, or 0 == repCode. |
| 881 | `matchCode` : matchLength - MINMATCH |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 882 | */ |
Yann Collet | d57dffb | 2016-07-03 01:48:26 +0200 | [diff] [blame] | 883 | MEM_STATIC void ZSTD_storeSeq(seqStore_t* seqStorePtr, size_t litLength, const void* literals, U32 offsetCode, size_t matchCode) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 884 | { |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 885 | #ifdef STORESEQ_DEBUG |
| 886 | if (g_startDebug) { |
| 887 | const U32 pos = (U32)((const BYTE*)literals - g_start); |
| 888 | if (g_start==NULL) g_start = (const BYTE*)literals; |
| 889 | if ((pos > 1895000) && (pos < 1895300)) |
| 890 | fprintf(stderr, "Cpos %6u :%5u literals & match %3u bytes at distance %6u \n", |
| 891 | pos, (U32)litLength, (U32)matchCode+MINMATCH, (U32)offsetCode); |
| 892 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 893 | #endif |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 894 | /* copy Literals */ |
| 895 | ZSTD_wildcopy(seqStorePtr->lit, literals, litLength); |
| 896 | seqStorePtr->lit += litLength; |
| 897 | |
| 898 | /* literal Length */ |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 899 | if (litLength>0xFFFF) { |
| 900 | seqStorePtr->longLengthID = 1; |
| 901 | seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); |
| 902 | } |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 903 | seqStorePtr->sequences[0].litLength = (U16)litLength; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 904 | |
| 905 | /* match offset */ |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 906 | seqStorePtr->sequences[0].offset = offsetCode + 1; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 907 | |
| 908 | /* match Length */ |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 909 | if (matchCode>0xFFFF) { |
| 910 | seqStorePtr->longLengthID = 2; |
| 911 | seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); |
| 912 | } |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 913 | seqStorePtr->sequences[0].matchLength = (U16)matchCode; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 914 | |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 915 | seqStorePtr->sequences++; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 916 | } |
| 917 | |
| 918 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 919 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 920 | * Match length counter |
| 921 | ***************************************/ |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 922 | static unsigned ZSTD_NbCommonBytes (register size_t val) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 923 | { |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 924 | if (MEM_isLittleEndian()) { |
| 925 | if (MEM_64bits()) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 926 | # if defined(_MSC_VER) && defined(_WIN64) |
| 927 | unsigned long r = 0; |
| 928 | _BitScanForward64( &r, (U64)val ); |
Yann Collet | d608088 | 2015-12-09 09:05:22 +0100 | [diff] [blame] | 929 | return (unsigned)(r>>3); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 930 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 931 | return (__builtin_ctzll((U64)val) >> 3); |
| 932 | # else |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 933 | static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, |
| 934 | 0, 3, 1, 3, 1, 4, 2, 7, |
| 935 | 0, 2, 3, 6, 1, 5, 3, 5, |
| 936 | 1, 3, 4, 4, 2, 5, 6, 7, |
| 937 | 7, 0, 1, 2, 3, 3, 4, 6, |
| 938 | 2, 6, 5, 5, 3, 4, 5, 6, |
| 939 | 7, 1, 2, 4, 6, 4, 4, 5, |
| 940 | 7, 2, 6, 5, 7, 6, 7, 7 }; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 941 | return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; |
| 942 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 943 | } else { /* 32 bits */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 944 | # if defined(_MSC_VER) |
| 945 | unsigned long r=0; |
| 946 | _BitScanForward( &r, (U32)val ); |
Yann Collet | d608088 | 2015-12-09 09:05:22 +0100 | [diff] [blame] | 947 | return (unsigned)(r>>3); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 948 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 949 | return (__builtin_ctz((U32)val) >> 3); |
| 950 | # else |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 951 | static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, |
| 952 | 3, 2, 2, 1, 3, 2, 0, 1, |
| 953 | 3, 3, 1, 2, 2, 2, 2, 0, |
| 954 | 3, 1, 2, 0, 1, 0, 1, 1 }; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 955 | return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; |
| 956 | # endif |
| 957 | } |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 958 | } else { /* Big Endian CPU */ |
| 959 | if (MEM_64bits()) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 960 | # if defined(_MSC_VER) && defined(_WIN64) |
| 961 | unsigned long r = 0; |
| 962 | _BitScanReverse64( &r, val ); |
| 963 | return (unsigned)(r>>3); |
| 964 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 965 | return (__builtin_clzll(val) >> 3); |
| 966 | # else |
| 967 | unsigned r; |
| 968 | const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */ |
| 969 | if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; } |
| 970 | if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } |
| 971 | r += (!val); |
| 972 | return r; |
| 973 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 974 | } else { /* 32 bits */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 975 | # if defined(_MSC_VER) |
| 976 | unsigned long r = 0; |
| 977 | _BitScanReverse( &r, (unsigned long)val ); |
| 978 | return (unsigned)(r>>3); |
| 979 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 980 | return (__builtin_clz((U32)val) >> 3); |
| 981 | # else |
| 982 | unsigned r; |
| 983 | if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } |
| 984 | r += (!val); |
| 985 | return r; |
| 986 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 987 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 991 | static size_t ZSTD_count(const BYTE* pIn, const BYTE* pMatch, const BYTE* const pInLimit) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 992 | { |
| 993 | const BYTE* const pStart = pIn; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 994 | const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 995 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 996 | while (pIn < pInLoopLimit) { |
Yann Collet | 7591a7f | 2016-05-20 11:44:43 +0200 | [diff] [blame] | 997 | size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 998 | if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; } |
| 999 | pIn += ZSTD_NbCommonBytes(diff); |
| 1000 | return (size_t)(pIn - pStart); |
| 1001 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1002 | if (MEM_64bits()) if ((pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; } |
| 1003 | if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; } |
| 1004 | if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++; |
| 1005 | return (size_t)(pIn - pStart); |
| 1006 | } |
| 1007 | |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 1008 | /** ZSTD_count_2segments() : |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 1009 | * can count match length with `ip` & `match` in 2 different segments. |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1010 | * convention : on reaching mEnd, match count continue starting from iStart |
| 1011 | */ |
| 1012 | static size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart) |
| 1013 | { |
Yann Collet | 7591a7f | 2016-05-20 11:44:43 +0200 | [diff] [blame] | 1014 | const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1015 | size_t const matchLength = ZSTD_count(ip, match, vEnd); |
| 1016 | if (match + matchLength != mEnd) return matchLength; |
| 1017 | return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1020 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1021 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1022 | * Hashes |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1023 | ***************************************/ |
inikep | cc52a97 | 2016-02-19 10:09:35 +0100 | [diff] [blame] | 1024 | static const U32 prime3bytes = 506832829U; |
| 1025 | static U32 ZSTD_hash3(U32 u, U32 h) { return ((u << (32-24)) * prime3bytes) >> (32-h) ; } |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1026 | MEM_STATIC size_t ZSTD_hash3Ptr(const void* ptr, U32 h) { return ZSTD_hash3(MEM_readLE32(ptr), h); } /* only in zstd_opt.h */ |
inikep | cc52a97 | 2016-02-19 10:09:35 +0100 | [diff] [blame] | 1027 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1028 | static const U32 prime4bytes = 2654435761U; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1029 | 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] | 1030 | 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] | 1031 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1032 | static const U64 prime5bytes = 889523592379ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1033 | 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] | 1034 | 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] | 1035 | |
| 1036 | static const U64 prime6bytes = 227718039650203ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1037 | 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] | 1038 | 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] | 1039 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1040 | static const U64 prime7bytes = 58295818150454627ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1041 | 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] | 1042 | 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] | 1043 | |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1044 | static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL; |
| 1045 | static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u) * prime8bytes) >> (64-h)) ; } |
| 1046 | static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h); } |
| 1047 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1048 | static size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls) |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1049 | { |
| 1050 | switch(mls) |
| 1051 | { |
Yann Collet | 2e2e78d | 2017-03-29 16:02:47 -0700 | [diff] [blame] | 1052 | //case 3: return ZSTD_hash3Ptr(p, hBits); |
| 1053 | default: |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1054 | case 4: return ZSTD_hash4Ptr(p, hBits); |
| 1055 | case 5: return ZSTD_hash5Ptr(p, hBits); |
| 1056 | case 6: return ZSTD_hash6Ptr(p, hBits); |
| 1057 | case 7: return ZSTD_hash7Ptr(p, hBits); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1058 | case 8: return ZSTD_hash8Ptr(p, hBits); |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1059 | } |
| 1060 | } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 1061 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1062 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1063 | /*-************************************* |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1064 | * Fast Scan |
| 1065 | ***************************************/ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1066 | static void ZSTD_fillHashTable (ZSTD_CCtx* zc, const void* end, const U32 mls) |
| 1067 | { |
| 1068 | U32* const hashTable = zc->hashTable; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1069 | U32 const hBits = zc->params.cParams.hashLog; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1070 | const BYTE* const base = zc->base; |
| 1071 | const BYTE* ip = base + zc->nextToUpdate; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1072 | const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; |
Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 1073 | const size_t fastHashFillStep = 3; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1074 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1075 | while(ip <= iend) { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1076 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base); |
Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 1077 | ip += fastHashFillStep; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1082 | FORCE_INLINE |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1083 | void ZSTD_compressBlock_fast_generic(ZSTD_CCtx* cctx, |
Yann Collet | 280f9a8 | 2016-08-08 00:44:00 +0200 | [diff] [blame] | 1084 | const void* src, size_t srcSize, |
| 1085 | const U32 mls) |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1086 | { |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1087 | U32* const hashTable = cctx->hashTable; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1088 | U32 const hBits = cctx->params.cParams.hashLog; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1089 | seqStore_t* seqStorePtr = &(cctx->seqStore); |
| 1090 | const BYTE* const base = cctx->base; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1091 | const BYTE* const istart = (const BYTE*)src; |
Yann Collet | 805a52a | 2015-11-06 10:52:17 +0100 | [diff] [blame] | 1092 | const BYTE* ip = istart; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1093 | const BYTE* anchor = istart; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1094 | const U32 lowestIndex = cctx->dictLimit; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1095 | const BYTE* const lowest = base + lowestIndex; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1096 | const BYTE* const iend = istart + srcSize; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1097 | const BYTE* const ilimit = iend - HASH_READ_SIZE; |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1098 | U32 offset_1=cctx->rep[0], offset_2=cctx->rep[1]; |
| 1099 | U32 offsetSaved = 0; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1100 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1101 | /* init */ |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1102 | ip += (ip==lowest); |
| 1103 | { U32 const maxRep = (U32)(ip-lowest); |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1104 | if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; |
| 1105 | if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1106 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1107 | |
| 1108 | /* Main Search Loop */ |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1109 | while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1110 | size_t mLength; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1111 | size_t const h = ZSTD_hashPtr(ip, hBits, mls); |
| 1112 | U32 const current = (U32)(ip-base); |
| 1113 | U32 const matchIndex = hashTable[h]; |
Yann Collet | d94efbf | 2015-12-29 14:29:08 +0100 | [diff] [blame] | 1114 | const BYTE* match = base + matchIndex; |
Yann Collet | 96ffa42 | 2016-01-02 01:16:28 +0100 | [diff] [blame] | 1115 | hashTable[h] = current; /* update hash table */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1116 | |
Yann Collet | 280f9a8 | 2016-08-08 00:44:00 +0200 | [diff] [blame] | 1117 | if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) { |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1118 | mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1119 | ip++; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1120 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
| 1121 | } else { |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1122 | U32 offset; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1123 | if ( (matchIndex <= lowestIndex) || (MEM_read32(match) != MEM_read32(ip)) ) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1124 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1125 | continue; |
| 1126 | } |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1127 | mLength = ZSTD_count(ip+4, match+4, iend) + 4; |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1128 | offset = (U32)(ip-match); |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1129 | while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1130 | offset_2 = offset_1; |
| 1131 | offset_1 = offset; |
inikep | 5945308 | 2016-03-16 15:35:14 +0100 | [diff] [blame] | 1132 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1133 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1134 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1135 | |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1136 | /* match found */ |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1137 | ip += mLength; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1138 | anchor = ip; |
| 1139 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1140 | if (ip <= ilimit) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1141 | /* Fill Table */ |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1142 | 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] | 1143 | hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); |
| 1144 | /* check immediate repcode */ |
| 1145 | while ( (ip <= ilimit) |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1146 | && ( (offset_2>0) |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1147 | & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1148 | /* store sequence */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1149 | size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1150 | { U32 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] | 1151 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base); |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1152 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH); |
| 1153 | ip += rLength; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1154 | anchor = ip; |
| 1155 | continue; /* faster when present ... (?) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1156 | } } } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1157 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1158 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1159 | cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved; |
| 1160 | cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1161 | |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 1162 | /* Last Literals */ |
| 1163 | { size_t const lastLLSize = iend - anchor; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1164 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1165 | seqStorePtr->lit += lastLLSize; |
| 1166 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1170 | static void ZSTD_compressBlock_fast(ZSTD_CCtx* ctx, |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1171 | const void* src, size_t srcSize) |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1172 | { |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 1173 | const U32 mls = ctx->params.cParams.searchLength; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1174 | switch(mls) |
| 1175 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1176 | default: /* includes case 3 */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1177 | case 4 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1178 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 4); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1179 | case 5 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1180 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 5); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1181 | case 6 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1182 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 6); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1183 | case 7 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1184 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 7); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1185 | } |
| 1186 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1187 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1188 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1189 | static void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx, |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1190 | const void* src, size_t srcSize, |
| 1191 | const U32 mls) |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1192 | { |
| 1193 | U32* hashTable = ctx->hashTable; |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 1194 | const U32 hBits = ctx->params.cParams.hashLog; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1195 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 1196 | const BYTE* const base = ctx->base; |
| 1197 | const BYTE* const dictBase = ctx->dictBase; |
| 1198 | const BYTE* const istart = (const BYTE*)src; |
| 1199 | const BYTE* ip = istart; |
| 1200 | const BYTE* anchor = istart; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1201 | const U32 lowestIndex = ctx->lowLimit; |
| 1202 | const BYTE* const dictStart = dictBase + lowestIndex; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1203 | const U32 dictLimit = ctx->dictLimit; |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1204 | const BYTE* const lowPrefixPtr = base + dictLimit; |
| 1205 | const BYTE* const dictEnd = dictBase + dictLimit; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1206 | const BYTE* const iend = istart + srcSize; |
| 1207 | const BYTE* const ilimit = iend - 8; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1208 | U32 offset_1=ctx->rep[0], offset_2=ctx->rep[1]; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1209 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1210 | /* Search Loop */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1211 | while (ip < ilimit) { /* < instead of <=, because (ip+1) */ |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1212 | const size_t h = ZSTD_hashPtr(ip, hBits, mls); |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1213 | const U32 matchIndex = hashTable[h]; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1214 | const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base; |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1215 | const BYTE* match = matchBase + matchIndex; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1216 | const U32 current = (U32)(ip-base); |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1217 | const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */ |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1218 | const BYTE* repBase = repIndex < dictLimit ? dictBase : base; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1219 | const BYTE* repMatch = repBase + repIndex; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1220 | size_t mLength; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1221 | hashTable[h] = current; /* update hash table */ |
| 1222 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1223 | if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1224 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1225 | const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1226 | mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4; |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1227 | ip++; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1228 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1229 | } else { |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1230 | if ( (matchIndex < lowestIndex) || |
Yann Collet | 5244738 | 2016-03-20 16:00:00 +0100 | [diff] [blame] | 1231 | (MEM_read32(match) != MEM_read32(ip)) ) { |
| 1232 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1233 | continue; |
| 1234 | } |
| 1235 | { const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1236 | const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1237 | U32 offset; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1238 | mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1239 | while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1240 | offset = current - matchIndex; |
| 1241 | offset_2 = offset_1; |
| 1242 | offset_1 = offset; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1243 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1244 | } } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1245 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1246 | /* found a match : store it */ |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1247 | ip += mLength; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1248 | anchor = ip; |
| 1249 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1250 | if (ip <= ilimit) { |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1251 | /* Fill Table */ |
Yann Collet | 3e21ec5 | 2016-09-06 15:36:19 +0200 | [diff] [blame] | 1252 | hashTable[ZSTD_hashPtr(base+current+2, hBits, mls)] = current+2; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1253 | hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); |
| 1254 | /* check immediate repcode */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1255 | while (ip <= ilimit) { |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 1256 | U32 const current2 = (U32)(ip-base); |
| 1257 | U32 const repIndex2 = current2 - offset_2; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1258 | const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1259 | if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */ |
| 1260 | && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1261 | const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1262 | size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1263 | U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ |
inikep | 7bc19b6 | 2016-04-06 09:46:01 +0200 | [diff] [blame] | 1264 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1265 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = current2; |
inikep | 7bc19b6 | 2016-04-06 09:46:01 +0200 | [diff] [blame] | 1266 | ip += repLength2; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1267 | anchor = ip; |
| 1268 | continue; |
| 1269 | } |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1270 | break; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1271 | } } } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1272 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1273 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1274 | ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1275 | |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1276 | /* Last Literals */ |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 1277 | { size_t const lastLLSize = iend - anchor; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1278 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1279 | seqStorePtr->lit += lastLLSize; |
| 1280 | } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1284 | static void ZSTD_compressBlock_fast_extDict(ZSTD_CCtx* ctx, |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1285 | const void* src, size_t srcSize) |
| 1286 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1287 | U32 const mls = ctx->params.cParams.searchLength; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1288 | switch(mls) |
| 1289 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1290 | default: /* includes case 3 */ |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1291 | case 4 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1292 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 4); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1293 | case 5 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1294 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 5); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1295 | case 6 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1296 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 6); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1297 | case 7 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1298 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 7); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1299 | } |
| 1300 | } |
| 1301 | |
| 1302 | |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 1303 | /*-************************************* |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1304 | * Double Fast |
| 1305 | ***************************************/ |
| 1306 | static void ZSTD_fillDoubleHashTable (ZSTD_CCtx* cctx, const void* end, const U32 mls) |
| 1307 | { |
| 1308 | U32* const hashLarge = cctx->hashTable; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1309 | U32 const hBitsL = cctx->params.cParams.hashLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1310 | U32* const hashSmall = cctx->chainTable; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1311 | U32 const hBitsS = cctx->params.cParams.chainLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1312 | const BYTE* const base = cctx->base; |
| 1313 | const BYTE* ip = base + cctx->nextToUpdate; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1314 | const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1315 | const size_t fastHashFillStep = 3; |
| 1316 | |
| 1317 | while(ip <= iend) { |
| 1318 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base); |
| 1319 | hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base); |
| 1320 | ip += fastHashFillStep; |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | FORCE_INLINE |
| 1326 | void ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx, |
| 1327 | const void* src, size_t srcSize, |
| 1328 | const U32 mls) |
| 1329 | { |
| 1330 | U32* const hashLong = cctx->hashTable; |
| 1331 | const U32 hBitsL = cctx->params.cParams.hashLog; |
| 1332 | U32* const hashSmall = cctx->chainTable; |
| 1333 | const U32 hBitsS = cctx->params.cParams.chainLog; |
| 1334 | seqStore_t* seqStorePtr = &(cctx->seqStore); |
| 1335 | const BYTE* const base = cctx->base; |
| 1336 | const BYTE* const istart = (const BYTE*)src; |
| 1337 | const BYTE* ip = istart; |
| 1338 | const BYTE* anchor = istart; |
| 1339 | const U32 lowestIndex = cctx->dictLimit; |
| 1340 | const BYTE* const lowest = base + lowestIndex; |
| 1341 | const BYTE* const iend = istart + srcSize; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1342 | const BYTE* const ilimit = iend - HASH_READ_SIZE; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1343 | U32 offset_1=cctx->rep[0], offset_2=cctx->rep[1]; |
| 1344 | U32 offsetSaved = 0; |
| 1345 | |
| 1346 | /* init */ |
| 1347 | ip += (ip==lowest); |
| 1348 | { U32 const maxRep = (U32)(ip-lowest); |
| 1349 | if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; |
| 1350 | if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; |
| 1351 | } |
| 1352 | |
| 1353 | /* Main Search Loop */ |
| 1354 | while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ |
| 1355 | size_t mLength; |
| 1356 | size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8); |
| 1357 | size_t const h = ZSTD_hashPtr(ip, hBitsS, mls); |
| 1358 | U32 const current = (U32)(ip-base); |
| 1359 | U32 const matchIndexL = hashLong[h2]; |
| 1360 | U32 const matchIndexS = hashSmall[h]; |
| 1361 | const BYTE* matchLong = base + matchIndexL; |
| 1362 | const BYTE* match = base + matchIndexS; |
| 1363 | hashLong[h2] = hashSmall[h] = current; /* update hash tables */ |
| 1364 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1365 | assert(offset_1 <= current); /* supposed guaranteed by construction */ |
| 1366 | if ((offset_1 > 0) & (MEM_read32(ip+1-offset_1) == MEM_read32(ip+1))) { |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1367 | /* favor repcode */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1368 | mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; |
| 1369 | ip++; |
| 1370 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
| 1371 | } else { |
Yann Collet | eed2081 | 2016-07-12 15:11:40 +0200 | [diff] [blame] | 1372 | U32 offset; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1373 | if ( (matchIndexL > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip)) ) { |
| 1374 | mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8; |
Yann Collet | eed2081 | 2016-07-12 15:11:40 +0200 | [diff] [blame] | 1375 | offset = (U32)(ip-matchLong); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1376 | while (((ip>anchor) & (matchLong>lowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ |
| 1377 | } else if ( (matchIndexS > lowestIndex) && (MEM_read32(match) == MEM_read32(ip)) ) { |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1378 | size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8); |
| 1379 | U32 const matchIndexL3 = hashLong[hl3]; |
| 1380 | const BYTE* matchL3 = base + matchIndexL3; |
| 1381 | hashLong[hl3] = current + 1; |
| 1382 | if ( (matchIndexL3 > lowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1)) ) { |
| 1383 | mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8; |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1384 | ip++; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1385 | offset = (U32)(ip-matchL3); |
| 1386 | while (((ip>anchor) & (matchL3>lowest)) && (ip[-1] == matchL3[-1])) { ip--; matchL3--; mLength++; } /* catch up */ |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1387 | } else { |
| 1388 | mLength = ZSTD_count(ip+4, match+4, iend) + 4; |
| 1389 | offset = (U32)(ip-match); |
| 1390 | while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
| 1391 | } |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1392 | } else { |
| 1393 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1394 | continue; |
| 1395 | } |
| 1396 | |
| 1397 | offset_2 = offset_1; |
| 1398 | offset_1 = offset; |
| 1399 | |
| 1400 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
| 1401 | } |
| 1402 | |
| 1403 | /* match found */ |
| 1404 | ip += mLength; |
| 1405 | anchor = ip; |
| 1406 | |
| 1407 | if (ip <= ilimit) { |
| 1408 | /* Fill Table */ |
| 1409 | hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = |
| 1410 | hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; /* here because current+2 could be > iend-8 */ |
| 1411 | hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = |
| 1412 | hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base); |
| 1413 | |
| 1414 | /* check immediate repcode */ |
| 1415 | while ( (ip <= ilimit) |
| 1416 | && ( (offset_2>0) |
| 1417 | & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { |
| 1418 | /* store sequence */ |
| 1419 | size_t const rLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; |
Yann Collet | eed2081 | 2016-07-12 15:11:40 +0200 | [diff] [blame] | 1420 | { U32 const tmpOff = offset_2; offset_2 = offset_1; offset_1 = tmpOff; } /* swap offset_2 <=> offset_1 */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1421 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base); |
| 1422 | hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base); |
| 1423 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH); |
| 1424 | ip += rLength; |
| 1425 | anchor = ip; |
| 1426 | continue; /* faster when present ... (?) */ |
| 1427 | } } } |
| 1428 | |
| 1429 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1430 | cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved; |
| 1431 | cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1432 | |
| 1433 | /* Last Literals */ |
| 1434 | { size_t const lastLLSize = iend - anchor; |
| 1435 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1436 | seqStorePtr->lit += lastLLSize; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | |
| 1441 | static void ZSTD_compressBlock_doubleFast(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 1442 | { |
| 1443 | const U32 mls = ctx->params.cParams.searchLength; |
| 1444 | switch(mls) |
| 1445 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1446 | default: /* includes case 3 */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1447 | case 4 : |
| 1448 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4); return; |
| 1449 | case 5 : |
| 1450 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5); return; |
| 1451 | case 6 : |
| 1452 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6); return; |
| 1453 | case 7 : |
| 1454 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7); return; |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | |
| 1459 | static void ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx, |
| 1460 | const void* src, size_t srcSize, |
| 1461 | const U32 mls) |
| 1462 | { |
| 1463 | U32* const hashLong = ctx->hashTable; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1464 | U32 const hBitsL = ctx->params.cParams.hashLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1465 | U32* const hashSmall = ctx->chainTable; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1466 | U32 const hBitsS = ctx->params.cParams.chainLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1467 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 1468 | const BYTE* const base = ctx->base; |
| 1469 | const BYTE* const dictBase = ctx->dictBase; |
| 1470 | const BYTE* const istart = (const BYTE*)src; |
| 1471 | const BYTE* ip = istart; |
| 1472 | const BYTE* anchor = istart; |
| 1473 | const U32 lowestIndex = ctx->lowLimit; |
| 1474 | const BYTE* const dictStart = dictBase + lowestIndex; |
| 1475 | const U32 dictLimit = ctx->dictLimit; |
| 1476 | const BYTE* const lowPrefixPtr = base + dictLimit; |
| 1477 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 1478 | const BYTE* const iend = istart + srcSize; |
| 1479 | const BYTE* const ilimit = iend - 8; |
| 1480 | U32 offset_1=ctx->rep[0], offset_2=ctx->rep[1]; |
| 1481 | |
| 1482 | /* Search Loop */ |
| 1483 | while (ip < ilimit) { /* < instead of <=, because (ip+1) */ |
| 1484 | const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls); |
| 1485 | const U32 matchIndex = hashSmall[hSmall]; |
| 1486 | const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base; |
| 1487 | const BYTE* match = matchBase + matchIndex; |
| 1488 | |
| 1489 | const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8); |
| 1490 | const U32 matchLongIndex = hashLong[hLong]; |
| 1491 | const BYTE* matchLongBase = matchLongIndex < dictLimit ? dictBase : base; |
| 1492 | const BYTE* matchLong = matchLongBase + matchLongIndex; |
| 1493 | |
| 1494 | const U32 current = (U32)(ip-base); |
| 1495 | const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */ |
| 1496 | const BYTE* repBase = repIndex < dictLimit ? dictBase : base; |
| 1497 | const BYTE* repMatch = repBase + repIndex; |
| 1498 | size_t mLength; |
| 1499 | hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */ |
| 1500 | |
| 1501 | if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) |
| 1502 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
| 1503 | const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; |
| 1504 | mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4; |
| 1505 | ip++; |
| 1506 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
| 1507 | } else { |
| 1508 | if ((matchLongIndex > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) { |
| 1509 | const BYTE* matchEnd = matchLongIndex < dictLimit ? dictEnd : iend; |
| 1510 | const BYTE* lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr; |
| 1511 | U32 offset; |
| 1512 | mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, lowPrefixPtr) + 8; |
| 1513 | offset = current - matchLongIndex; |
| 1514 | while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ |
| 1515 | offset_2 = offset_1; |
| 1516 | offset_1 = offset; |
| 1517 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1518 | |
Yann Collet | 73d74a0 | 2016-07-12 13:03:48 +0200 | [diff] [blame] | 1519 | } else if ((matchIndex > lowestIndex) && (MEM_read32(match) == MEM_read32(ip))) { |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1520 | size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8); |
| 1521 | U32 const matchIndex3 = hashLong[h3]; |
| 1522 | const BYTE* const match3Base = matchIndex3 < dictLimit ? dictBase : base; |
| 1523 | const BYTE* match3 = match3Base + matchIndex3; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1524 | U32 offset; |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1525 | hashLong[h3] = current + 1; |
| 1526 | if ( (matchIndex3 > lowestIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) { |
| 1527 | const BYTE* matchEnd = matchIndex3 < dictLimit ? dictEnd : iend; |
| 1528 | const BYTE* lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr; |
| 1529 | mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, lowPrefixPtr) + 8; |
| 1530 | ip++; |
| 1531 | offset = current+1 - matchIndex3; |
| 1532 | while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */ |
| 1533 | } else { |
| 1534 | const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; |
| 1535 | const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr; |
| 1536 | mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4; |
| 1537 | offset = current - matchIndex; |
| 1538 | while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
| 1539 | } |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1540 | offset_2 = offset_1; |
| 1541 | offset_1 = offset; |
| 1542 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1543 | |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1544 | } else { |
| 1545 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1546 | continue; |
| 1547 | } } |
| 1548 | |
| 1549 | /* found a match : store it */ |
| 1550 | ip += mLength; |
| 1551 | anchor = ip; |
| 1552 | |
| 1553 | if (ip <= ilimit) { |
| 1554 | /* Fill Table */ |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 1555 | hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; |
| 1556 | hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1557 | hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base); |
| 1558 | hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base); |
| 1559 | /* check immediate repcode */ |
| 1560 | while (ip <= ilimit) { |
| 1561 | U32 const current2 = (U32)(ip-base); |
| 1562 | U32 const repIndex2 = current2 - offset_2; |
| 1563 | const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; |
| 1564 | if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */ |
| 1565 | && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { |
| 1566 | const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 1567 | size_t const repLength2 = ZSTD_count_2segments(ip+4, repMatch2+4, iend, repEnd2, lowPrefixPtr) + 4; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1568 | U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ |
| 1569 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH); |
| 1570 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2; |
| 1571 | hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2; |
| 1572 | ip += repLength2; |
| 1573 | anchor = ip; |
| 1574 | continue; |
| 1575 | } |
| 1576 | break; |
| 1577 | } } } |
| 1578 | |
| 1579 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1580 | ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1581 | |
| 1582 | /* Last Literals */ |
| 1583 | { size_t const lastLLSize = iend - anchor; |
| 1584 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1585 | seqStorePtr->lit += lastLLSize; |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | |
| 1590 | static void ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx* ctx, |
| 1591 | const void* src, size_t srcSize) |
| 1592 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1593 | U32 const mls = ctx->params.cParams.searchLength; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1594 | switch(mls) |
| 1595 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1596 | default: /* includes case 3 */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1597 | case 4 : |
| 1598 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4); return; |
| 1599 | case 5 : |
| 1600 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5); return; |
| 1601 | case 6 : |
| 1602 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6); return; |
| 1603 | case 7 : |
| 1604 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7); return; |
| 1605 | } |
| 1606 | } |
| 1607 | |
| 1608 | |
| 1609 | /*-************************************* |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1610 | * Binary Tree search |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1611 | ***************************************/ |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 1612 | /** ZSTD_insertBt1() : add one or multiple positions to tree. |
| 1613 | * ip : assumed <= iend-8 . |
Yann Collet | 06eade5 | 2015-11-23 14:23:47 +0100 | [diff] [blame] | 1614 | * @return : nb of positions added */ |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1615 | static U32 ZSTD_insertBt1(ZSTD_CCtx* zc, const BYTE* const ip, const U32 mls, const BYTE* const iend, U32 nbCompares, |
| 1616 | U32 extDict) |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1617 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1618 | U32* const hashTable = zc->hashTable; |
| 1619 | U32 const hashLog = zc->params.cParams.hashLog; |
| 1620 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
| 1621 | U32* const bt = zc->chainTable; |
| 1622 | U32 const btLog = zc->params.cParams.chainLog - 1; |
| 1623 | U32 const btMask = (1 << btLog) - 1; |
| 1624 | U32 matchIndex = hashTable[h]; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1625 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 1626 | const BYTE* const base = zc->base; |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1627 | const BYTE* const dictBase = zc->dictBase; |
| 1628 | const U32 dictLimit = zc->dictLimit; |
| 1629 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 1630 | const BYTE* const prefixStart = base + dictLimit; |
Yann Collet | 2b361cf | 2016-10-14 16:03:34 -0700 | [diff] [blame] | 1631 | const BYTE* match; |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 1632 | const U32 current = (U32)(ip-base); |
Yann Collet | e9eba60 | 2015-11-08 15:08:03 +0100 | [diff] [blame] | 1633 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1634 | U32* smallerPtr = bt + 2*(current&btMask); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1635 | U32* largerPtr = smallerPtr + 1; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1636 | U32 dummy32; /* to be nullified at the end */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1637 | U32 const windowLow = zc->lowLimit; |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 1638 | U32 matchEndIdx = current+8; |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1639 | size_t bestLength = 8; |
Yann Collet | c093208 | 2016-06-30 14:07:30 +0200 | [diff] [blame] | 1640 | #ifdef ZSTD_C_PREDICT |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 1641 | U32 predictedSmall = *(bt + 2*((current-1)&btMask) + 0); |
| 1642 | U32 predictedLarge = *(bt + 2*((current-1)&btMask) + 1); |
| 1643 | predictedSmall += (predictedSmall>0); |
| 1644 | predictedLarge += (predictedLarge>0); |
Yann Collet | c093208 | 2016-06-30 14:07:30 +0200 | [diff] [blame] | 1645 | #endif /* ZSTD_C_PREDICT */ |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1646 | |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 1647 | hashTable[h] = current; /* Update Hash Table */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1648 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1649 | while (nbCompares-- && (matchIndex > windowLow)) { |
Yann Collet | 25f46dc | 2016-11-29 16:59:27 -0800 | [diff] [blame] | 1650 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1651 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
Yann Collet | 25f46dc | 2016-11-29 16:59:27 -0800 | [diff] [blame] | 1652 | |
Yann Collet | c093208 | 2016-06-30 14:07:30 +0200 | [diff] [blame] | 1653 | #ifdef ZSTD_C_PREDICT /* note : can create issues when hlog small <= 11 */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 1654 | 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] | 1655 | if (matchIndex == predictedSmall) { |
| 1656 | /* no need to check length, result known */ |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1657 | *smallerPtr = matchIndex; |
| 1658 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1659 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 1660 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 1661 | predictedSmall = predictPtr[1] + (predictPtr[1]>0); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1662 | continue; |
| 1663 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1664 | if (matchIndex == predictedLarge) { |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1665 | *largerPtr = matchIndex; |
| 1666 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1667 | largerPtr = nextPtr; |
| 1668 | matchIndex = nextPtr[0]; |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 1669 | predictedLarge = predictPtr[0] + (predictPtr[0]>0); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 1670 | continue; |
| 1671 | } |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 1672 | #endif |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1673 | if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1674 | match = base + matchIndex; |
| 1675 | if (match[matchLength] == ip[matchLength]) |
| 1676 | matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1677 | } else { |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1678 | match = dictBase + matchIndex; |
| 1679 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
| 1680 | if (matchIndex+matchLength >= dictLimit) |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 1681 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1682 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1683 | |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1684 | if (matchLength > bestLength) { |
| 1685 | bestLength = matchLength; |
| 1686 | if (matchLength > matchEndIdx - matchIndex) |
| 1687 | matchEndIdx = matchIndex + (U32)matchLength; |
| 1688 | } |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1689 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1690 | 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] | 1691 | 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] | 1692 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1693 | if (match[matchLength] < ip[matchLength]) { /* necessarily within correct buffer */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1694 | /* match is smaller than current */ |
| 1695 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 1696 | 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] | 1697 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1698 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1699 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1700 | } else { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1701 | /* match is larger than current */ |
| 1702 | *largerPtr = matchIndex; |
| 1703 | commonLengthLarger = matchLength; |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1704 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1705 | largerPtr = nextPtr; |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 1706 | matchIndex = nextPtr[0]; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1707 | } } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1708 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1709 | *smallerPtr = *largerPtr = 0; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1710 | if (bestLength > 384) return MIN(192, (U32)(bestLength - 384)); /* speed optimization */ |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1711 | if (matchEndIdx > current + 8) return matchEndIdx - current - 8; |
| 1712 | return 1; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1716 | static size_t ZSTD_insertBtAndFindBestMatch ( |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1717 | ZSTD_CCtx* zc, |
| 1718 | const BYTE* const ip, const BYTE* const iend, |
| 1719 | size_t* offsetPtr, |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1720 | U32 nbCompares, const U32 mls, |
| 1721 | U32 extDict) |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1722 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1723 | U32* const hashTable = zc->hashTable; |
| 1724 | U32 const hashLog = zc->params.cParams.hashLog; |
| 1725 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
| 1726 | U32* const bt = zc->chainTable; |
| 1727 | U32 const btLog = zc->params.cParams.chainLog - 1; |
| 1728 | U32 const btMask = (1 << btLog) - 1; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1729 | U32 matchIndex = hashTable[h]; |
| 1730 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 1731 | const BYTE* const base = zc->base; |
| 1732 | const BYTE* const dictBase = zc->dictBase; |
| 1733 | const U32 dictLimit = zc->dictLimit; |
| 1734 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 1735 | const BYTE* const prefixStart = base + dictLimit; |
| 1736 | const U32 current = (U32)(ip-base); |
| 1737 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
| 1738 | const U32 windowLow = zc->lowLimit; |
| 1739 | U32* smallerPtr = bt + 2*(current&btMask); |
| 1740 | U32* largerPtr = bt + 2*(current&btMask) + 1; |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 1741 | U32 matchEndIdx = current+8; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1742 | U32 dummy32; /* to be nullified at the end */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1743 | size_t bestLength = 0; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1744 | |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 1745 | hashTable[h] = current; /* Update Hash Table */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1746 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1747 | while (nbCompares-- && (matchIndex > windowLow)) { |
Yann Collet | 25f46dc | 2016-11-29 16:59:27 -0800 | [diff] [blame] | 1748 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1749 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 1750 | const BYTE* match; |
| 1751 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1752 | if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1753 | match = base + matchIndex; |
| 1754 | if (match[matchLength] == ip[matchLength]) |
| 1755 | matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1756 | } else { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1757 | match = dictBase + matchIndex; |
| 1758 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
Yann Collet | 225179d | 2015-11-23 16:52:22 +0100 | [diff] [blame] | 1759 | if (matchIndex+matchLength >= dictLimit) |
Nick Terrell | 5152fb2 | 2017-03-29 18:51:58 -0700 | [diff] [blame] | 1760 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1761 | } |
| 1762 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1763 | if (matchLength > bestLength) { |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1764 | if (matchLength > matchEndIdx - matchIndex) |
Yann Collet | 48da164 | 2015-12-29 23:40:02 +0100 | [diff] [blame] | 1765 | matchEndIdx = matchIndex + (U32)matchLength; |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 1766 | if ( (4*(int)(matchLength-bestLength)) > (int)(ZSTD_highbit32(current-matchIndex+1) - ZSTD_highbit32((U32)offsetPtr[0]+1)) ) |
inikep | 7571685 | 2016-04-06 12:34:42 +0200 | [diff] [blame] | 1767 | bestLength = matchLength, *offsetPtr = ZSTD_REP_MOVE + current - matchIndex; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1768 | if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ |
| 1769 | break; /* drop, to guarantee consistency (miss a little bit of compression) */ |
| 1770 | } |
| 1771 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1772 | if (match[matchLength] < ip[matchLength]) { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1773 | /* match is smaller than current */ |
| 1774 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 1775 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
| 1776 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1777 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 1778 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1779 | } else { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1780 | /* match is larger than current */ |
| 1781 | *largerPtr = matchIndex; |
| 1782 | commonLengthLarger = matchLength; |
| 1783 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 1784 | largerPtr = nextPtr; |
| 1785 | matchIndex = nextPtr[0]; |
Yann Collet | 768c6bc | 2016-02-10 14:01:49 +0100 | [diff] [blame] | 1786 | } } |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1787 | |
| 1788 | *smallerPtr = *largerPtr = 0; |
| 1789 | |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 1790 | zc->nextToUpdate = (matchEndIdx > current + 8) ? matchEndIdx - 8 : current+1; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1791 | return bestLength; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1792 | } |
| 1793 | |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1794 | |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1795 | 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] | 1796 | { |
| 1797 | const BYTE* const base = zc->base; |
| 1798 | const U32 target = (U32)(ip - base); |
| 1799 | U32 idx = zc->nextToUpdate; |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1800 | |
| 1801 | while(idx < target) |
| 1802 | idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 0); |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1803 | } |
| 1804 | |
Yann Collet | 5244738 | 2016-03-20 16:00:00 +0100 | [diff] [blame] | 1805 | /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */ |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1806 | static size_t ZSTD_BtFindBestMatch ( |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1807 | ZSTD_CCtx* zc, |
| 1808 | const BYTE* const ip, const BYTE* const iLimit, |
| 1809 | size_t* offsetPtr, |
| 1810 | const U32 maxNbAttempts, const U32 mls) |
| 1811 | { |
| 1812 | if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1813 | ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls); |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1814 | return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 0); |
| 1815 | } |
| 1816 | |
| 1817 | |
Yann Collet | 768c6bc | 2016-02-10 14:01:49 +0100 | [diff] [blame] | 1818 | static size_t ZSTD_BtFindBestMatch_selectMLS ( |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1819 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 1820 | const BYTE* ip, const BYTE* const iLimit, |
| 1821 | size_t* offsetPtr, |
| 1822 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1823 | { |
| 1824 | switch(matchLengthSearch) |
| 1825 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1826 | default : /* includes case 3 */ |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1827 | case 4 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 1828 | case 5 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1829 | case 7 : |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1830 | case 6 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1835 | static void ZSTD_updateTree_extDict(ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iend, const U32 nbCompares, const U32 mls) |
| 1836 | { |
| 1837 | const BYTE* const base = zc->base; |
| 1838 | const U32 target = (U32)(ip - base); |
| 1839 | U32 idx = zc->nextToUpdate; |
| 1840 | |
| 1841 | while (idx < target) idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 1); |
| 1842 | } |
| 1843 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1844 | |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1845 | /** Tree updater, providing best match */ |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1846 | static size_t ZSTD_BtFindBestMatch_extDict ( |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1847 | ZSTD_CCtx* zc, |
| 1848 | const BYTE* const ip, const BYTE* const iLimit, |
| 1849 | size_t* offsetPtr, |
| 1850 | const U32 maxNbAttempts, const U32 mls) |
| 1851 | { |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 1852 | if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 1853 | ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls); |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 1854 | return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 1); |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1855 | } |
| 1856 | |
| 1857 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1858 | static size_t ZSTD_BtFindBestMatch_selectMLS_extDict ( |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1859 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 1860 | const BYTE* ip, const BYTE* const iLimit, |
| 1861 | size_t* offsetPtr, |
| 1862 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1863 | { |
| 1864 | switch(matchLengthSearch) |
| 1865 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1866 | default : /* includes case 3 */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1867 | case 4 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 1868 | case 5 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1869 | case 7 : |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 1870 | case 6 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 1875 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1876 | /* ********************************* |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1877 | * Hash Chain |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1878 | ***********************************/ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1879 | #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & mask] |
| 1880 | |
| 1881 | /* Update chains up to ip (excluded) |
Anders Oleson | 517577b | 2017-02-20 12:08:59 -0800 | [diff] [blame] | 1882 | Assumption : always within prefix (i.e. not within extDict) */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1883 | FORCE_INLINE |
| 1884 | U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) |
| 1885 | { |
| 1886 | U32* const hashTable = zc->hashTable; |
| 1887 | const U32 hashLog = zc->params.cParams.hashLog; |
| 1888 | U32* const chainTable = zc->chainTable; |
| 1889 | const U32 chainMask = (1 << zc->params.cParams.chainLog) - 1; |
| 1890 | const BYTE* const base = zc->base; |
| 1891 | const U32 target = (U32)(ip - base); |
| 1892 | U32 idx = zc->nextToUpdate; |
| 1893 | |
Yann Collet | 22d7632 | 2016-06-21 08:01:51 +0200 | [diff] [blame] | 1894 | while(idx < target) { /* catch up */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1895 | size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls); |
| 1896 | NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; |
| 1897 | hashTable[h] = idx; |
| 1898 | idx++; |
| 1899 | } |
| 1900 | |
| 1901 | zc->nextToUpdate = target; |
| 1902 | return hashTable[ZSTD_hashPtr(ip, hashLog, mls)]; |
| 1903 | } |
| 1904 | |
| 1905 | |
| 1906 | |
| 1907 | FORCE_INLINE /* inlining is important to hardwire a hot branch (template emulation) */ |
| 1908 | size_t ZSTD_HcFindBestMatch_generic ( |
| 1909 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 1910 | const BYTE* const ip, const BYTE* const iLimit, |
| 1911 | size_t* offsetPtr, |
| 1912 | const U32 maxNbAttempts, const U32 mls, const U32 extDict) |
| 1913 | { |
| 1914 | U32* const chainTable = zc->chainTable; |
| 1915 | const U32 chainSize = (1 << zc->params.cParams.chainLog); |
| 1916 | const U32 chainMask = chainSize-1; |
| 1917 | const BYTE* const base = zc->base; |
| 1918 | const BYTE* const dictBase = zc->dictBase; |
| 1919 | const U32 dictLimit = zc->dictLimit; |
| 1920 | const BYTE* const prefixStart = base + dictLimit; |
| 1921 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 1922 | const U32 lowLimit = zc->lowLimit; |
| 1923 | const U32 current = (U32)(ip-base); |
| 1924 | const U32 minChain = current > chainSize ? current - chainSize : 0; |
| 1925 | int nbAttempts=maxNbAttempts; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 1926 | size_t ml=4-1; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1927 | |
| 1928 | /* HC4 match finder */ |
| 1929 | U32 matchIndex = ZSTD_insertAndFindFirstIndex (zc, ip, mls); |
| 1930 | |
Yann Collet | 22d7632 | 2016-06-21 08:01:51 +0200 | [diff] [blame] | 1931 | for ( ; (matchIndex>lowLimit) & (nbAttempts>0) ; nbAttempts--) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1932 | const BYTE* match; |
| 1933 | size_t currentMl=0; |
| 1934 | if ((!extDict) || matchIndex >= dictLimit) { |
| 1935 | match = base + matchIndex; |
| 1936 | if (match[ml] == ip[ml]) /* potentially better */ |
| 1937 | currentMl = ZSTD_count(ip, match, iLimit); |
| 1938 | } else { |
| 1939 | match = dictBase + matchIndex; |
| 1940 | if (MEM_read32(match) == MEM_read32(ip)) /* assumption : matchIndex <= dictLimit-4 (by table construction) */ |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 1941 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | /* save best solution */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1945 | if (currentMl > ml) { |
| 1946 | ml = currentMl; |
| 1947 | *offsetPtr = current - matchIndex + ZSTD_REP_MOVE; |
| 1948 | if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */ |
| 1949 | } |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1950 | |
| 1951 | if (matchIndex <= minChain) break; |
| 1952 | matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask); |
| 1953 | } |
| 1954 | |
| 1955 | return ml; |
| 1956 | } |
| 1957 | |
| 1958 | |
| 1959 | FORCE_INLINE size_t ZSTD_HcFindBestMatch_selectMLS ( |
| 1960 | ZSTD_CCtx* zc, |
| 1961 | const BYTE* ip, const BYTE* const iLimit, |
| 1962 | size_t* offsetPtr, |
| 1963 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1964 | { |
| 1965 | switch(matchLengthSearch) |
| 1966 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1967 | default : /* includes case 3 */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1968 | case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 0); |
| 1969 | case 5 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 0); |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1970 | case 7 : |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1971 | case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 0); |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | FORCE_INLINE size_t ZSTD_HcFindBestMatch_extDict_selectMLS ( |
| 1977 | ZSTD_CCtx* zc, |
| 1978 | const BYTE* ip, const BYTE* const iLimit, |
| 1979 | size_t* offsetPtr, |
| 1980 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 1981 | { |
| 1982 | switch(matchLengthSearch) |
| 1983 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1984 | default : /* includes case 3 */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1985 | case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 1); |
| 1986 | case 5 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5, 1); |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1987 | case 7 : |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1988 | case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 1); |
| 1989 | } |
| 1990 | } |
| 1991 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1992 | |
Yann Collet | 287b7d9 | 2015-11-22 13:24:05 +0100 | [diff] [blame] | 1993 | /* ******************************* |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1994 | * Common parser - lazy strategy |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 1995 | *********************************/ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1996 | FORCE_INLINE |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 1997 | void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, |
| 1998 | const void* src, size_t srcSize, |
| 1999 | const U32 searchMethod, const U32 depth) |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2000 | { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2001 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 2002 | const BYTE* const istart = (const BYTE*)src; |
| 2003 | const BYTE* ip = istart; |
| 2004 | const BYTE* anchor = istart; |
| 2005 | const BYTE* const iend = istart + srcSize; |
| 2006 | const BYTE* const ilimit = iend - 8; |
| 2007 | const BYTE* const base = ctx->base + ctx->dictLimit; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2008 | |
Yann Collet | 793c649 | 2016-04-09 20:32:00 +0200 | [diff] [blame] | 2009 | U32 const maxSearches = 1 << ctx->params.cParams.searchLog; |
| 2010 | U32 const mls = ctx->params.cParams.searchLength; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2011 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2012 | typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, |
| 2013 | size_t* offsetPtr, |
| 2014 | U32 maxNbAttempts, U32 matchLengthSearch); |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2015 | searchMax_f const searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2016 | U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1], savedOffset=0; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2017 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2018 | /* init */ |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2019 | ip += (ip==base); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2020 | ctx->nextToUpdate3 = ctx->nextToUpdate; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2021 | { U32 const maxRep = (U32)(ip-base); |
| 2022 | if (offset_2 > maxRep) savedOffset = offset_2, offset_2 = 0; |
| 2023 | if (offset_1 > maxRep) savedOffset = offset_1, offset_1 = 0; |
| 2024 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2025 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2026 | /* Match Loop */ |
| 2027 | while (ip < ilimit) { |
| 2028 | size_t matchLength=0; |
| 2029 | size_t offset=0; |
| 2030 | const BYTE* start=ip+1; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2031 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2032 | /* check repCode */ |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2033 | if ((offset_1>0) & (MEM_read32(ip+1) == MEM_read32(ip+1 - offset_1))) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2034 | /* repcode : we take it */ |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2035 | matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2036 | if (depth==0) goto _storeSequence; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2037 | } |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2038 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2039 | /* first search (depth 0) */ |
| 2040 | { size_t offsetFound = 99999999; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2041 | size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2042 | if (ml2 > matchLength) |
inikep | 7571685 | 2016-04-06 12:34:42 +0200 | [diff] [blame] | 2043 | matchLength = ml2, start = ip, offset=offsetFound; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2044 | } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2045 | |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2046 | if (matchLength < 4) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2047 | ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ |
| 2048 | continue; |
| 2049 | } |
| 2050 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2051 | /* let's try to find a better solution */ |
| 2052 | if (depth>=1) |
| 2053 | while (ip<ilimit) { |
| 2054 | ip ++; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2055 | if ((offset) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) { |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2056 | size_t const mlRep = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2057 | int const gain2 = (int)(mlRep * 3); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2058 | int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2059 | if ((mlRep >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2060 | matchLength = mlRep, offset = 0, start = ip; |
| 2061 | } |
| 2062 | { size_t offset2=99999999; |
| 2063 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2064 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2065 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2066 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2067 | matchLength = ml2, offset = offset2, start = ip; |
| 2068 | continue; /* search a better one */ |
| 2069 | } } |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2070 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2071 | /* let's find an even better one */ |
| 2072 | if ((depth==2) && (ip<ilimit)) { |
| 2073 | ip ++; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2074 | if ((offset) && ((offset_1>0) & (MEM_read32(ip) == MEM_read32(ip - offset_1)))) { |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2075 | size_t const ml2 = ZSTD_count(ip+4, ip+4-offset_1, iend) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2076 | int const gain2 = (int)(ml2 * 4); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2077 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2078 | if ((ml2 >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2079 | matchLength = ml2, offset = 0, start = ip; |
| 2080 | } |
| 2081 | { size_t offset2=99999999; |
| 2082 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2083 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2084 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2085 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2086 | matchLength = ml2, offset = offset2, start = ip; |
| 2087 | continue; |
| 2088 | } } } |
| 2089 | break; /* nothing found : store previous solution */ |
| 2090 | } |
| 2091 | |
| 2092 | /* catch up */ |
| 2093 | if (offset) { |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2094 | while ( (start > anchor) |
| 2095 | && (start > base+offset-ZSTD_REP_MOVE) |
| 2096 | && (start[-1] == start[-1-offset+ZSTD_REP_MOVE]) ) /* only search for offset within prefix */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2097 | { start--; matchLength++; } |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2098 | offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2099 | } |
| 2100 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2101 | /* store sequence */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2102 | _storeSequence: |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2103 | { size_t const litLength = start - anchor; |
Yann Collet | d57dffb | 2016-07-03 01:48:26 +0200 | [diff] [blame] | 2104 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength-MINMATCH); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2105 | anchor = ip = start + matchLength; |
| 2106 | } |
Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 2107 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2108 | /* check immediate repcode */ |
| 2109 | while ( (ip <= ilimit) |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2110 | && ((offset_2>0) |
| 2111 | & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2112 | /* store sequence */ |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2113 | matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2114 | offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap repcodes */ |
inikep | 7bc19b6 | 2016-04-06 09:46:01 +0200 | [diff] [blame] | 2115 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength-MINMATCH); |
| 2116 | ip += matchLength; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2117 | anchor = ip; |
| 2118 | continue; /* faster when present ... (?) */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2119 | } } |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2120 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2121 | /* Save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 2122 | ctx->repToConfirm[0] = offset_1 ? offset_1 : savedOffset; |
| 2123 | ctx->repToConfirm[1] = offset_2 ? offset_2 : savedOffset; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2124 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2125 | /* Last Literals */ |
| 2126 | { size_t const lastLLSize = iend - anchor; |
| 2127 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 2128 | seqStorePtr->lit += lastLLSize; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2129 | } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2130 | } |
| 2131 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2132 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2133 | static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2134 | { |
| 2135 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 1, 2); |
| 2136 | } |
| 2137 | |
| 2138 | static void ZSTD_compressBlock_lazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2139 | { |
| 2140 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 2); |
| 2141 | } |
| 2142 | |
| 2143 | static void ZSTD_compressBlock_lazy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2144 | { |
| 2145 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 1); |
| 2146 | } |
| 2147 | |
| 2148 | static void ZSTD_compressBlock_greedy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2149 | { |
| 2150 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 0); |
| 2151 | } |
| 2152 | |
| 2153 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2154 | FORCE_INLINE |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2155 | void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, |
| 2156 | const void* src, size_t srcSize, |
| 2157 | const U32 searchMethod, const U32 depth) |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2158 | { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2159 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 2160 | const BYTE* const istart = (const BYTE*)src; |
| 2161 | const BYTE* ip = istart; |
| 2162 | const BYTE* anchor = istart; |
| 2163 | const BYTE* const iend = istart + srcSize; |
| 2164 | const BYTE* const ilimit = iend - 8; |
| 2165 | const BYTE* const base = ctx->base; |
| 2166 | const U32 dictLimit = ctx->dictLimit; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2167 | const U32 lowestIndex = ctx->lowLimit; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2168 | const BYTE* const prefixStart = base + dictLimit; |
| 2169 | const BYTE* const dictBase = ctx->dictBase; |
| 2170 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 2171 | const BYTE* const dictStart = dictBase + ctx->lowLimit; |
| 2172 | |
| 2173 | const U32 maxSearches = 1 << ctx->params.cParams.searchLog; |
| 2174 | const U32 mls = ctx->params.cParams.searchLength; |
| 2175 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2176 | typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, |
| 2177 | size_t* offsetPtr, |
| 2178 | U32 maxNbAttempts, U32 matchLengthSearch); |
| 2179 | searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS_extDict : ZSTD_HcFindBestMatch_extDict_selectMLS; |
| 2180 | |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2181 | U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1]; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2182 | |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2183 | /* init */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2184 | ctx->nextToUpdate3 = ctx->nextToUpdate; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2185 | ip += (ip == prefixStart); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2186 | |
| 2187 | /* Match Loop */ |
| 2188 | while (ip < ilimit) { |
| 2189 | size_t matchLength=0; |
| 2190 | size_t offset=0; |
| 2191 | const BYTE* start=ip+1; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2192 | U32 current = (U32)(ip-base); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2193 | |
| 2194 | /* check repCode */ |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2195 | { const U32 repIndex = (U32)(current+1 - offset_1); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2196 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2197 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2198 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2199 | if (MEM_read32(ip+1) == MEM_read32(repMatch)) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2200 | /* repcode detected we should take it */ |
| 2201 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2202 | matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2203 | if (depth==0) goto _storeSequence; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2204 | } } |
| 2205 | |
| 2206 | /* first search (depth 0) */ |
| 2207 | { size_t offsetFound = 99999999; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2208 | size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2209 | if (ml2 > matchLength) |
inikep | 7571685 | 2016-04-06 12:34:42 +0200 | [diff] [blame] | 2210 | matchLength = ml2, start = ip, offset=offsetFound; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2211 | } |
| 2212 | |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2213 | if (matchLength < 4) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2214 | ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ |
| 2215 | continue; |
| 2216 | } |
| 2217 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2218 | /* let's try to find a better solution */ |
| 2219 | if (depth>=1) |
| 2220 | while (ip<ilimit) { |
| 2221 | ip ++; |
| 2222 | current++; |
| 2223 | /* check repCode */ |
| 2224 | if (offset) { |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2225 | const U32 repIndex = (U32)(current - offset_1); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2226 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2227 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2228 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2229 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
| 2230 | /* repcode detected */ |
| 2231 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2232 | size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2233 | int const gain2 = (int)(repLength * 3); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2234 | int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2235 | if ((repLength >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2236 | matchLength = repLength, offset = 0, start = ip; |
| 2237 | } } |
| 2238 | |
| 2239 | /* search match, depth 1 */ |
| 2240 | { size_t offset2=99999999; |
| 2241 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2242 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2243 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2244 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2245 | matchLength = ml2, offset = offset2, start = ip; |
| 2246 | continue; /* search a better one */ |
| 2247 | } } |
| 2248 | |
| 2249 | /* let's find an even better one */ |
| 2250 | if ((depth==2) && (ip<ilimit)) { |
| 2251 | ip ++; |
| 2252 | current++; |
| 2253 | /* check repCode */ |
| 2254 | if (offset) { |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2255 | const U32 repIndex = (U32)(current - offset_1); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2256 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2257 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2258 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2259 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
| 2260 | /* repcode detected */ |
| 2261 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2262 | size_t const repLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2263 | int const gain2 = (int)(repLength * 4); |
| 2264 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2265 | if ((repLength >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2266 | matchLength = repLength, offset = 0, start = ip; |
| 2267 | } } |
| 2268 | |
| 2269 | /* search match, depth 2 */ |
| 2270 | { size_t offset2=99999999; |
| 2271 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2272 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2273 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2274 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2275 | matchLength = ml2, offset = offset2, start = ip; |
| 2276 | continue; |
| 2277 | } } } |
| 2278 | break; /* nothing found : store previous solution */ |
| 2279 | } |
| 2280 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2281 | /* catch up */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2282 | if (offset) { |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2283 | U32 const matchIndex = (U32)((start-base) - (offset - ZSTD_REP_MOVE)); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2284 | const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex; |
| 2285 | const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2286 | while ((start>anchor) && (match>mStart) && (start[-1] == match[-1])) { start--; match--; matchLength++; } /* catch up */ |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2287 | offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE); |
Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 2288 | } |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2289 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2290 | /* store sequence */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2291 | _storeSequence: |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2292 | { size_t const litLength = start - anchor; |
Yann Collet | d57dffb | 2016-07-03 01:48:26 +0200 | [diff] [blame] | 2293 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength-MINMATCH); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2294 | anchor = ip = start + matchLength; |
| 2295 | } |
| 2296 | |
| 2297 | /* check immediate repcode */ |
| 2298 | while (ip <= ilimit) { |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2299 | const U32 repIndex = (U32)((ip-base) - offset_2); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2300 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2301 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2302 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2303 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
| 2304 | /* repcode detected we should take it */ |
| 2305 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame^] | 2306 | matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2307 | offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap offset history */ |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2308 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength-MINMATCH); |
| 2309 | ip += matchLength; |
| 2310 | anchor = ip; |
| 2311 | continue; /* faster when present ... (?) */ |
| 2312 | } |
| 2313 | break; |
| 2314 | } } |
| 2315 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2316 | /* Save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 2317 | ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2318 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2319 | /* Last Literals */ |
| 2320 | { size_t const lastLLSize = iend - anchor; |
| 2321 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 2322 | seqStorePtr->lit += lastLLSize; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2323 | } |
| 2324 | } |
| 2325 | |
| 2326 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2327 | 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] | 2328 | { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2329 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 0); |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 2330 | } |
| 2331 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2332 | 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] | 2333 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 2334 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 1); |
Yann Collet | b7fc88e | 2015-11-22 03:12:28 +0100 | [diff] [blame] | 2335 | } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 2336 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2337 | 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] | 2338 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 2339 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 2); |
Yann Collet | a85c77b | 2015-11-22 12:22:04 +0100 | [diff] [blame] | 2340 | } |
| 2341 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2342 | 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] | 2343 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 2344 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 1, 2); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 2345 | } |
| 2346 | |
inikep | ef51941 | 2016-04-21 11:08:43 +0200 | [diff] [blame] | 2347 | |
inikep | ef51941 | 2016-04-21 11:08:43 +0200 | [diff] [blame] | 2348 | /* The optimal parser */ |
| 2349 | #include "zstd_opt.h" |
| 2350 | |
| 2351 | static void ZSTD_compressBlock_btopt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2352 | { |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2353 | #ifdef ZSTD_OPT_H_91842398743 |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 2354 | ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 0); |
| 2355 | #else |
| 2356 | (void)ctx; (void)src; (void)srcSize; |
| 2357 | return; |
| 2358 | #endif |
| 2359 | } |
| 2360 | |
| 2361 | static void ZSTD_compressBlock_btopt2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2362 | { |
| 2363 | #ifdef ZSTD_OPT_H_91842398743 |
| 2364 | ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 1); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2365 | #else |
| 2366 | (void)ctx; (void)src; (void)srcSize; |
| 2367 | return; |
| 2368 | #endif |
inikep | ef51941 | 2016-04-21 11:08:43 +0200 | [diff] [blame] | 2369 | } |
| 2370 | |
inikep | d3b8d7a | 2016-02-22 10:06:17 +0100 | [diff] [blame] | 2371 | 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] | 2372 | { |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2373 | #ifdef ZSTD_OPT_H_91842398743 |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 2374 | ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 0); |
| 2375 | #else |
| 2376 | (void)ctx; (void)src; (void)srcSize; |
| 2377 | return; |
| 2378 | #endif |
| 2379 | } |
| 2380 | |
| 2381 | static void ZSTD_compressBlock_btopt2_extDict(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2382 | { |
| 2383 | #ifdef ZSTD_OPT_H_91842398743 |
| 2384 | ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 1); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2385 | #else |
| 2386 | (void)ctx; (void)src; (void)srcSize; |
| 2387 | return; |
| 2388 | #endif |
inikep | e2bfe24 | 2016-01-31 11:25:48 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
Yann Collet | 7a23179 | 2015-11-21 15:27:35 +0100 | [diff] [blame] | 2391 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2392 | 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] | 2393 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2394 | static ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 2395 | { |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 2396 | static const ZSTD_blockCompressor blockCompressor[2][8] = { |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2397 | { ZSTD_compressBlock_fast, ZSTD_compressBlock_doubleFast, ZSTD_compressBlock_greedy, |
| 2398 | ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, |
| 2399 | ZSTD_compressBlock_btopt, ZSTD_compressBlock_btopt2 }, |
| 2400 | { ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_doubleFast_extDict, ZSTD_compressBlock_greedy_extDict, |
| 2401 | ZSTD_compressBlock_lazy_extDict,ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, |
| 2402 | ZSTD_compressBlock_btopt_extDict, ZSTD_compressBlock_btopt2_extDict } |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 2403 | }; |
| 2404 | |
| 2405 | return blockCompressor[extDict][(U32)strat]; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2409 | 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] | 2410 | { |
Yann Collet | 19cab46 | 2016-06-17 12:54:52 +0200 | [diff] [blame] | 2411 | ZSTD_blockCompressor const blockCompressor = ZSTD_selectBlockCompressor(zc->params.cParams.strategy, zc->lowLimit < zc->dictLimit); |
inikep | 98e08cb | 2016-08-10 15:00:30 +0200 | [diff] [blame] | 2412 | const BYTE* const base = zc->base; |
| 2413 | const BYTE* const istart = (const BYTE*)src; |
| 2414 | const U32 current = (U32)(istart-base); |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2415 | if (srcSize < MIN_CBLOCK_SIZE+ZSTD_blockHeaderSize+1) return 0; /* don't even attempt compression below a certain srcSize */ |
Yann Collet | 19cab46 | 2016-06-17 12:54:52 +0200 | [diff] [blame] | 2416 | ZSTD_resetSeqStore(&(zc->seqStore)); |
inikep | 98e08cb | 2016-08-10 15:00:30 +0200 | [diff] [blame] | 2417 | if (current > zc->nextToUpdate + 384) |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2418 | zc->nextToUpdate = current - MIN(192, (U32)(current - zc->nextToUpdate - 384)); /* limited update after finding a very long match */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2419 | blockCompressor(zc, src, srcSize); |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2420 | return ZSTD_compressSequences(zc, dst, dstCapacity, srcSize); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 2421 | } |
| 2422 | |
| 2423 | |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2424 | /*! ZSTD_compress_generic() : |
| 2425 | * Compress a chunk of data into one or multiple blocks. |
| 2426 | * All blocks will be terminated, all input will be consumed. |
| 2427 | * Function will issue an error if there is not enough `dstCapacity` to hold the compressed content. |
| 2428 | * Frame is supposed already started (header already produced) |
| 2429 | * @return : compressed size, or an error code |
| 2430 | */ |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2431 | static size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, |
| 2432 | void* dst, size_t dstCapacity, |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2433 | const void* src, size_t srcSize, |
| 2434 | U32 lastFrameChunk) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2435 | { |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2436 | size_t blockSize = cctx->blockSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2437 | size_t remaining = srcSize; |
| 2438 | const BYTE* ip = (const BYTE*)src; |
| 2439 | BYTE* const ostart = (BYTE*)dst; |
| 2440 | BYTE* op = ostart; |
Yann Collet | d4180ca | 2016-07-27 21:21:36 +0200 | [diff] [blame] | 2441 | U32 const maxDist = 1 << cctx->params.cParams.windowLog; |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 2442 | |
Nick Terrell | 3b9cdf9 | 2016-10-12 20:54:42 -0700 | [diff] [blame] | 2443 | if (cctx->params.fParams.checksumFlag && srcSize) |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2444 | XXH64_update(&cctx->xxhState, src, srcSize); |
| 2445 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2446 | while (remaining) { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2447 | U32 const lastBlock = lastFrameChunk & (blockSize >= remaining); |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 2448 | size_t cSize; |
| 2449 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2450 | if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE) |
| 2451 | return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */ |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 2452 | if (remaining < blockSize) blockSize = remaining; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2453 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2454 | /* preemptive overflow correction */ |
Sean Purcell | 881abe4 | 2017-03-07 16:52:23 -0800 | [diff] [blame] | 2455 | if (cctx->lowLimit > (3U<<29)) { |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 2456 | U32 const cycleMask = (1 << ZSTD_cycleLog(cctx->params.cParams.hashLog, cctx->params.cParams.strategy)) - 1; |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 2457 | U32 const current = (U32)(ip - cctx->base); |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 2458 | U32 const newCurrent = (current & cycleMask) + (1 << cctx->params.cParams.windowLog); |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 2459 | U32 const correction = current - newCurrent; |
| 2460 | ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_64 <= 30); |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2461 | ZSTD_reduceIndex(cctx, correction); |
| 2462 | cctx->base += correction; |
| 2463 | cctx->dictBase += correction; |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 2464 | cctx->lowLimit -= correction; |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2465 | cctx->dictLimit -= correction; |
| 2466 | if (cctx->nextToUpdate < correction) cctx->nextToUpdate = 0; |
| 2467 | else cctx->nextToUpdate -= correction; |
| 2468 | } |
| 2469 | |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 2470 | if ((U32)(ip+blockSize - cctx->base) > cctx->loadedDictEnd + maxDist) { |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 2471 | /* enforce maxDist */ |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2472 | U32 const newLowLimit = (U32)(ip+blockSize - cctx->base) - maxDist; |
| 2473 | if (cctx->lowLimit < newLowLimit) cctx->lowLimit = newLowLimit; |
| 2474 | if (cctx->dictLimit < cctx->lowLimit) cctx->dictLimit = cctx->lowLimit; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 2475 | } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2476 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2477 | cSize = ZSTD_compressBlock_internal(cctx, op+ZSTD_blockHeaderSize, dstCapacity-ZSTD_blockHeaderSize, ip, blockSize); |
inikep | fb5df61 | 2016-05-24 15:36:37 +0200 | [diff] [blame] | 2478 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2479 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2480 | if (cSize == 0) { /* block is not compressible */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2481 | U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(blockSize << 3); |
| 2482 | if (blockSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); |
| 2483 | MEM_writeLE32(op, cBlockHeader24); /* no pb, 4th byte will be overwritten */ |
| 2484 | memcpy(op + ZSTD_blockHeaderSize, ip, blockSize); |
| 2485 | cSize = ZSTD_blockHeaderSize+blockSize; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2486 | } else { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2487 | U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3); |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 2488 | MEM_writeLE24(op, cBlockHeader24); |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2489 | cSize += ZSTD_blockHeaderSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2490 | } |
| 2491 | |
| 2492 | remaining -= blockSize; |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2493 | dstCapacity -= cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2494 | ip += blockSize; |
| 2495 | op += cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2498 | if (lastFrameChunk && (op>ostart)) cctx->stage = ZSTDcs_ending; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2499 | return op-ostart; |
| 2500 | } |
| 2501 | |
| 2502 | |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2503 | static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity, |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2504 | ZSTD_parameters params, U64 pledgedSrcSize, U32 dictID) |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2505 | { BYTE* const op = (BYTE*)dst; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2506 | U32 const dictIDSizeCode = (dictID>0) + (dictID>=256) + (dictID>=65536); /* 0-3 */ |
| 2507 | U32 const checksumFlag = params.fParams.checksumFlag>0; |
| 2508 | U32 const windowSize = 1U << params.cParams.windowLog; |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 2509 | U32 const singleSegment = params.fParams.contentSizeFlag && (windowSize >= pledgedSrcSize); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2510 | BYTE const windowLogByte = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) << 3); |
| 2511 | U32 const fcsCode = params.fParams.contentSizeFlag ? |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 2512 | (pledgedSrcSize>=256) + (pledgedSrcSize>=65536+256) + (pledgedSrcSize>=0xFFFFFFFFU) : /* 0-3 */ |
| 2513 | 0; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2514 | BYTE const frameHeaderDecriptionByte = (BYTE)(dictIDSizeCode + (checksumFlag<<2) + (singleSegment<<5) + (fcsCode<<6) ); |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2515 | size_t pos; |
| 2516 | |
| 2517 | if (dstCapacity < ZSTD_frameHeaderSize_max) return ERROR(dstSize_tooSmall); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2518 | |
| 2519 | MEM_writeLE32(dst, ZSTD_MAGICNUMBER); |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 2520 | op[4] = frameHeaderDecriptionByte; pos=5; |
Eric Biggers | e4d0265 | 2016-07-26 10:42:19 -0700 | [diff] [blame] | 2521 | if (!singleSegment) op[pos++] = windowLogByte; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2522 | switch(dictIDSizeCode) |
| 2523 | { |
| 2524 | default: /* impossible */ |
| 2525 | case 0 : break; |
| 2526 | case 1 : op[pos] = (BYTE)(dictID); pos++; break; |
Yann Collet | d4180ca | 2016-07-27 21:21:36 +0200 | [diff] [blame] | 2527 | case 2 : MEM_writeLE16(op+pos, (U16)dictID); pos+=2; break; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2528 | case 3 : MEM_writeLE32(op+pos, dictID); pos+=4; break; |
| 2529 | } |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 2530 | switch(fcsCode) |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2531 | { |
| 2532 | default: /* impossible */ |
Eric Biggers | e4d0265 | 2016-07-26 10:42:19 -0700 | [diff] [blame] | 2533 | case 0 : if (singleSegment) op[pos++] = (BYTE)(pledgedSrcSize); break; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 2534 | case 1 : MEM_writeLE16(op+pos, (U16)(pledgedSrcSize-256)); pos+=2; break; |
| 2535 | case 2 : MEM_writeLE32(op+pos, (U32)(pledgedSrcSize)); pos+=4; break; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2536 | case 3 : MEM_writeLE64(op+pos, (U64)(pledgedSrcSize)); pos+=8; break; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2537 | } |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2538 | return pos; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2539 | } |
| 2540 | |
| 2541 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2542 | static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 2543 | void* dst, size_t dstCapacity, |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2544 | const void* src, size_t srcSize, |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2545 | U32 frame, U32 lastFrameChunk) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2546 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2547 | const BYTE* const ip = (const BYTE*) src; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2548 | size_t fhSize = 0; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2549 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2550 | if (cctx->stage==ZSTDcs_created) return ERROR(stage_wrong); /* missing init (ZSTD_compressBegin) */ |
Yann Collet | d4180ca | 2016-07-27 21:21:36 +0200 | [diff] [blame] | 2551 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2552 | if (frame && (cctx->stage==ZSTDcs_init)) { |
| 2553 | fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, cctx->frameContentSize, cctx->dictID); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2554 | if (ZSTD_isError(fhSize)) return fhSize; |
| 2555 | dstCapacity -= fhSize; |
| 2556 | dst = (char*)dst + fhSize; |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2557 | cctx->stage = ZSTDcs_ongoing; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2558 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2559 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2560 | /* Check if blocks follow each other */ |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2561 | if (src != cctx->nextSrc) { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2562 | /* not contiguous */ |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2563 | ptrdiff_t const delta = cctx->nextSrc - ip; |
| 2564 | cctx->lowLimit = cctx->dictLimit; |
| 2565 | cctx->dictLimit = (U32)(cctx->nextSrc - cctx->base); |
| 2566 | cctx->dictBase = cctx->base; |
| 2567 | cctx->base -= delta; |
| 2568 | cctx->nextToUpdate = cctx->dictLimit; |
| 2569 | if (cctx->dictLimit - cctx->lowLimit < HASH_READ_SIZE) cctx->lowLimit = cctx->dictLimit; /* too small extDict */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2570 | } |
| 2571 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2572 | /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */ |
| 2573 | if ((ip+srcSize > cctx->dictBase + cctx->lowLimit) & (ip < cctx->dictBase + cctx->dictLimit)) { |
| 2574 | ptrdiff_t const highInputIdx = (ip + srcSize) - cctx->dictBase; |
| 2575 | U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)cctx->dictLimit) ? cctx->dictLimit : (U32)highInputIdx; |
| 2576 | cctx->lowLimit = lowLimitMax; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2577 | } |
| 2578 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2579 | cctx->nextSrc = ip + srcSize; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2580 | |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 2581 | if (srcSize) { |
| 2582 | size_t const cSize = frame ? |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2583 | ZSTD_compress_generic (cctx, dst, dstCapacity, src, srcSize, lastFrameChunk) : |
| 2584 | ZSTD_compressBlock_internal (cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2585 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2586 | cctx->consumedSrcSize += srcSize; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2587 | return cSize + fhSize; |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 2588 | } else |
| 2589 | return fhSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2590 | } |
| 2591 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2592 | |
Yann Collet | 5b56739 | 2016-07-28 01:17:22 +0200 | [diff] [blame] | 2593 | size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 2594 | void* dst, size_t dstCapacity, |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2595 | const void* src, size_t srcSize) |
| 2596 | { |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2597 | return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1 /* frame mode */, 0 /* last chunk */); |
Yann Collet | 5b56739 | 2016-07-28 01:17:22 +0200 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 2601 | size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx) |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2602 | { |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 2603 | return MIN (ZSTD_BLOCKSIZE_ABSOLUTEMAX, 1 << cctx->params.cParams.windowLog); |
| 2604 | } |
| 2605 | |
| 2606 | size_t ZSTD_compressBlock(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
| 2607 | { |
| 2608 | size_t const blockSizeMax = ZSTD_getBlockSizeMax(cctx); |
Yann Collet | 961b6a0 | 2016-07-15 11:56:53 +0200 | [diff] [blame] | 2609 | if (srcSize > blockSizeMax) return ERROR(srcSize_wrong); |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2610 | return ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 0 /* frame mode */, 0 /* last chunk */); |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2611 | } |
| 2612 | |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2613 | /*! ZSTD_loadDictionaryContent() : |
| 2614 | * @return : 0, or an error code |
| 2615 | */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2616 | 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] | 2617 | { |
| 2618 | const BYTE* const ip = (const BYTE*) src; |
| 2619 | const BYTE* const iend = ip + srcSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2620 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2621 | /* input becomes current prefix */ |
| 2622 | zc->lowLimit = zc->dictLimit; |
| 2623 | zc->dictLimit = (U32)(zc->nextSrc - zc->base); |
| 2624 | zc->dictBase = zc->base; |
| 2625 | zc->base += ip - zc->nextSrc; |
| 2626 | zc->nextToUpdate = zc->dictLimit; |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 2627 | zc->loadedDictEnd = zc->forceWindow ? 0 : (U32)(iend - zc->base); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2628 | |
| 2629 | zc->nextSrc = iend; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2630 | if (srcSize <= HASH_READ_SIZE) return 0; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2631 | |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2632 | switch(zc->params.cParams.strategy) |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2633 | { |
| 2634 | case ZSTD_fast: |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2635 | ZSTD_fillHashTable (zc, iend, zc->params.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2636 | break; |
| 2637 | |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 2638 | case ZSTD_dfast: |
| 2639 | ZSTD_fillDoubleHashTable (zc, iend, zc->params.cParams.searchLength); |
| 2640 | break; |
| 2641 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2642 | case ZSTD_greedy: |
| 2643 | case ZSTD_lazy: |
| 2644 | case ZSTD_lazy2: |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2645 | if (srcSize >= HASH_READ_SIZE) |
| 2646 | ZSTD_insertAndFindFirstIndex(zc, iend-HASH_READ_SIZE, zc->params.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2647 | break; |
| 2648 | |
| 2649 | case ZSTD_btlazy2: |
Yann Collet | cefef8c | 2016-02-15 07:21:54 +0100 | [diff] [blame] | 2650 | case ZSTD_btopt: |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 2651 | case ZSTD_btopt2: |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2652 | if (srcSize >= HASH_READ_SIZE) |
| 2653 | ZSTD_updateTree(zc, iend-HASH_READ_SIZE, iend, 1 << zc->params.cParams.searchLog, zc->params.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2654 | break; |
| 2655 | |
| 2656 | default: |
| 2657 | return ERROR(GENERIC); /* strategy doesn't exist; impossible */ |
| 2658 | } |
| 2659 | |
Nick Terrell | ecf90ca | 2017-02-13 18:27:34 -0800 | [diff] [blame] | 2660 | zc->nextToUpdate = (U32)(iend - zc->base); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2665 | /* Dictionaries that assign zero probability to symbols that show up causes problems |
| 2666 | when FSE encoding. Refuse dictionaries that assign zero probability to symbols |
| 2667 | that we may encounter during compression. |
| 2668 | NOTE: This behavior is not standard and could be improved in the future. */ |
| 2669 | static size_t ZSTD_checkDictNCount(short* normalizedCounter, unsigned dictMaxSymbolValue, unsigned maxSymbolValue) { |
| 2670 | U32 s; |
| 2671 | if (dictMaxSymbolValue < maxSymbolValue) return ERROR(dictionary_corrupted); |
| 2672 | for (s = 0; s <= maxSymbolValue; ++s) { |
| 2673 | if (normalizedCounter[s] == 0) return ERROR(dictionary_corrupted); |
| 2674 | } |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2679 | /* Dictionary format : |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 2680 | * See : |
| 2681 | * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#dictionary-format |
| 2682 | */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2683 | /*! ZSTD_loadZstdDictionary() : |
| 2684 | * @return : 0, or an error code |
| 2685 | * assumptions : magic number supposed already checked |
| 2686 | * dictSize supposed > 8 |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 2687 | */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2688 | static size_t ZSTD_loadZstdDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2689 | { |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2690 | const BYTE* dictPtr = (const BYTE*)dict; |
| 2691 | const BYTE* const dictEnd = dictPtr + dictSize; |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2692 | short offcodeNCount[MaxOff+1]; |
| 2693 | unsigned offcodeMaxValue = MaxOff; |
Yann Collet | 643d9a2 | 2016-12-01 16:24:04 -0800 | [diff] [blame] | 2694 | BYTE scratchBuffer[1<<MAX(MLFSELog,LLFSELog)]; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2695 | |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 2696 | dictPtr += 4; /* skip magic number */ |
| 2697 | cctx->dictID = cctx->params.fParams.noDictIDFlag ? 0 : MEM_readLE32(dictPtr); |
| 2698 | dictPtr += 4; |
| 2699 | |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 2700 | { size_t const hufHeaderSize = HUF_readCTable(cctx->hufCTable, 255, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2701 | if (HUF_isError(hufHeaderSize)) return ERROR(dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2702 | dictPtr += hufHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2703 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2704 | |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2705 | { unsigned offcodeLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2706 | size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2707 | if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 2708 | if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2709 | /* Defer checking offcodeMaxValue because we need to know the size of the dictionary content */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2710 | CHECK_E( FSE_buildCTable_wksp(cctx->offcodeCTable, offcodeNCount, offcodeMaxValue, offcodeLog, scratchBuffer, sizeof(scratchBuffer)), |
| 2711 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2712 | dictPtr += offcodeHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2713 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2714 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2715 | { short matchlengthNCount[MaxML+1]; |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 2716 | unsigned matchlengthMaxValue = MaxML, matchlengthLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2717 | size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2718 | if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 2719 | if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2720 | /* Every match length code must have non-zero probability */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2721 | CHECK_F( ZSTD_checkDictNCount(matchlengthNCount, matchlengthMaxValue, MaxML)); |
| 2722 | CHECK_E( FSE_buildCTable_wksp(cctx->matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, scratchBuffer, sizeof(scratchBuffer)), |
| 2723 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2724 | dictPtr += matchlengthHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2725 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2726 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2727 | { short litlengthNCount[MaxLL+1]; |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 2728 | unsigned litlengthMaxValue = MaxLL, litlengthLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2729 | size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2730 | if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 2731 | if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2732 | /* Every literal length code must have non-zero probability */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2733 | CHECK_F( ZSTD_checkDictNCount(litlengthNCount, litlengthMaxValue, MaxLL)); |
| 2734 | CHECK_E( FSE_buildCTable_wksp(cctx->litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog, scratchBuffer, sizeof(scratchBuffer)), |
| 2735 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2736 | dictPtr += litlengthHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2737 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2738 | |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2739 | if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted); |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2740 | cctx->rep[0] = MEM_readLE32(dictPtr+0); |
| 2741 | cctx->rep[1] = MEM_readLE32(dictPtr+4); |
| 2742 | cctx->rep[2] = MEM_readLE32(dictPtr+8); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 2743 | dictPtr += 12; |
| 2744 | |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 2745 | { size_t const dictContentSize = (size_t)(dictEnd - dictPtr); |
| 2746 | U32 offcodeMax = MaxOff; |
| 2747 | if (dictContentSize <= ((U32)-1) - 128 KB) { |
| 2748 | U32 const maxOffset = (U32)dictContentSize + 128 KB; /* The maximum offset that must be supported */ |
| 2749 | offcodeMax = ZSTD_highbit32(maxOffset); /* Calculate minimum offset code required to represent maxOffset */ |
Nick Terrell | b2c39a2 | 2016-10-24 14:11:27 -0700 | [diff] [blame] | 2750 | } |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 2751 | /* All offset values <= dictContentSize + 128 KB must be representable */ |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2752 | CHECK_F (ZSTD_checkDictNCount(offcodeNCount, offcodeMaxValue, MIN(offcodeMax, MaxOff))); |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 2753 | /* All repCodes must be <= dictContentSize and != 0*/ |
| 2754 | { U32 u; |
| 2755 | for (u=0; u<3; u++) { |
| 2756 | if (cctx->rep[u] == 0) return ERROR(dictionary_corrupted); |
| 2757 | if (cctx->rep[u] > dictContentSize) return ERROR(dictionary_corrupted); |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2758 | } } |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 2759 | |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 2760 | cctx->fseCTables_ready = 1; |
| 2761 | cctx->hufCTable_repeatMode = HUF_repeat_valid; |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2762 | return ZSTD_loadDictionaryContent(cctx, dictPtr, dictContentSize); |
| 2763 | } |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2764 | } |
| 2765 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2766 | /** ZSTD_compress_insertDictionary() : |
| 2767 | * @return : 0, or an error code */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2768 | static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2769 | { |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2770 | if ((dict==NULL) || (dictSize<=8)) return 0; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2771 | |
Yann Collet | 14312d8 | 2017-02-23 23:42:12 -0800 | [diff] [blame] | 2772 | /* dict as pure content */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2773 | if ((MEM_readLE32(dict) != ZSTD_DICT_MAGIC) || (cctx->forceRawDict)) |
| 2774 | return ZSTD_loadDictionaryContent(cctx, dict, dictSize); |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2775 | |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 2776 | /* dict as zstd dictionary */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2777 | return ZSTD_loadZstdDictionary(cctx, dict, dictSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2778 | } |
| 2779 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2780 | /*! ZSTD_compressBegin_internal() : |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2781 | * @return : 0, or an error code */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 2782 | static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2783 | const void* dict, size_t dictSize, |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2784 | ZSTD_parameters params, U64 pledgedSrcSize) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2785 | { |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 2786 | ZSTD_compResetPolicy_e const crp = dictSize ? ZSTDcrp_fullReset : ZSTDcrp_continue; |
Yann Collet | ab9162e | 2017-04-11 10:46:20 -0700 | [diff] [blame] | 2787 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | 30fb499 | 2017-04-18 14:08:50 -0700 | [diff] [blame] | 2788 | CHECK_F(ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize, crp)); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 2789 | return ZSTD_compress_insertDictionary(cctx, dict, dictSize); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2790 | } |
| 2791 | |
| 2792 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2793 | /*! ZSTD_compressBegin_advanced() : |
| 2794 | * @return : 0, or an error code */ |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2795 | size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2796 | const void* dict, size_t dictSize, |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 2797 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2798 | { |
| 2799 | /* compression parameters verification and optimization */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 2800 | CHECK_F(ZSTD_checkCParams(params.cParams)); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2801 | return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, pledgedSrcSize); |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2805 | size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2806 | { |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 2807 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2808 | return ZSTD_compressBegin_internal(cctx, dict, dictSize, params, 0); |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2809 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2810 | |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 2811 | |
Yann Collet | b05c482 | 2017-01-12 02:01:28 +0100 | [diff] [blame] | 2812 | size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2813 | { |
Yann Collet | b05c482 | 2017-01-12 02:01:28 +0100 | [diff] [blame] | 2814 | return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2815 | } |
| 2816 | |
| 2817 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2818 | /*! ZSTD_writeEpilogue() : |
| 2819 | * Ends a frame. |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2820 | * @return : nb of bytes written into dst (or an error code) */ |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2821 | static size_t ZSTD_writeEpilogue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity) |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2822 | { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2823 | BYTE* const ostart = (BYTE*)dst; |
| 2824 | BYTE* op = ostart; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2825 | size_t fhSize = 0; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2826 | |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 2827 | if (cctx->stage == ZSTDcs_created) return ERROR(stage_wrong); /* init missing */ |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 2828 | |
| 2829 | /* special case : empty frame */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2830 | if (cctx->stage == ZSTDcs_init) { |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2831 | fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->params, 0, 0); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2832 | if (ZSTD_isError(fhSize)) return fhSize; |
| 2833 | dstCapacity -= fhSize; |
| 2834 | op += fhSize; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2835 | cctx->stage = ZSTDcs_ongoing; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2836 | } |
| 2837 | |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2838 | if (cctx->stage != ZSTDcs_ending) { |
| 2839 | /* write one last empty block, make it the "last" block */ |
| 2840 | U32 const cBlockHeader24 = 1 /* last block */ + (((U32)bt_raw)<<1) + 0; |
| 2841 | if (dstCapacity<4) return ERROR(dstSize_tooSmall); |
| 2842 | MEM_writeLE32(op, cBlockHeader24); |
| 2843 | op += ZSTD_blockHeaderSize; |
| 2844 | dstCapacity -= ZSTD_blockHeaderSize; |
| 2845 | } |
| 2846 | |
| 2847 | if (cctx->params.fParams.checksumFlag) { |
| 2848 | U32 const checksum = (U32) XXH64_digest(&cctx->xxhState); |
| 2849 | if (dstCapacity<4) return ERROR(dstSize_tooSmall); |
| 2850 | MEM_writeLE32(op, checksum); |
| 2851 | op += 4; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2852 | } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2853 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2854 | cctx->stage = ZSTDcs_created; /* return to "created but no init" status */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2855 | return op-ostart; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2856 | } |
| 2857 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2858 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2859 | size_t ZSTD_compressEnd (ZSTD_CCtx* cctx, |
| 2860 | void* dst, size_t dstCapacity, |
| 2861 | const void* src, size_t srcSize) |
| 2862 | { |
| 2863 | size_t endResult; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2864 | size_t const cSize = ZSTD_compressContinue_internal(cctx, dst, dstCapacity, src, srcSize, 1 /* frame mode */, 1 /* last chunk */); |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2865 | if (ZSTD_isError(cSize)) return cSize; |
| 2866 | endResult = ZSTD_writeEpilogue(cctx, (char*)dst + cSize, dstCapacity-cSize); |
| 2867 | if (ZSTD_isError(endResult)) return endResult; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2868 | if (cctx->params.fParams.contentSizeFlag) { /* control src size */ |
| 2869 | if (cctx->frameContentSize != cctx->consumedSrcSize) return ERROR(srcSize_wrong); |
| 2870 | } |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2871 | return cSize + endResult; |
| 2872 | } |
| 2873 | |
| 2874 | |
Yann Collet | 19c1002 | 2016-07-28 01:25:46 +0200 | [diff] [blame] | 2875 | static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2876 | void* dst, size_t dstCapacity, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2877 | const void* src, size_t srcSize, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2878 | const void* dict,size_t dictSize, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2879 | ZSTD_parameters params) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2880 | { |
Yann Collet | 3e21ec5 | 2016-09-06 15:36:19 +0200 | [diff] [blame] | 2881 | CHECK_F(ZSTD_compressBegin_internal(cctx, dict, dictSize, params, srcSize)); |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2882 | return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2883 | } |
| 2884 | |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2885 | size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, |
| 2886 | void* dst, size_t dstCapacity, |
| 2887 | const void* src, size_t srcSize, |
| 2888 | const void* dict,size_t dictSize, |
| 2889 | ZSTD_parameters params) |
| 2890 | { |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 2891 | CHECK_F(ZSTD_checkCParams(params.cParams)); |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2892 | return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); |
| 2893 | } |
| 2894 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2895 | size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, |
| 2896 | const void* dict, size_t dictSize, int compressionLevel) |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2897 | { |
Yann Collet | 407a11f | 2016-11-03 15:52:01 -0700 | [diff] [blame] | 2898 | ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dict ? dictSize : 0); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2899 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2900 | return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2901 | } |
| 2902 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2903 | 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] | 2904 | { |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2905 | return ZSTD_compress_usingDict(ctx, dst, dstCapacity, src, srcSize, NULL, 0, compressionLevel); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2906 | } |
| 2907 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2908 | 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] | 2909 | { |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 2910 | size_t result; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2911 | ZSTD_CCtx ctxBody; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 2912 | memset(&ctxBody, 0, sizeof(ctxBody)); |
inikep | 2866951 | 2016-06-02 13:04:18 +0200 | [diff] [blame] | 2913 | memcpy(&ctxBody.customMem, &defaultCustomMem, sizeof(ZSTD_customMem)); |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2914 | result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel); |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2915 | ZSTD_free(ctxBody.workSpace, defaultCustomMem); /* can't free ctxBody itself, as it's on stack; free only heap content */ |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 2916 | return result; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2917 | } |
Yann Collet | fdcad6d | 2015-12-17 23:50:15 +0100 | [diff] [blame] | 2918 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2919 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2920 | /* ===== Dictionary API ===== */ |
| 2921 | |
| 2922 | struct ZSTD_CDict_s { |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2923 | void* dictBuffer; |
| 2924 | const void* dictContent; |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2925 | size_t dictContentSize; |
| 2926 | ZSTD_CCtx* refContext; |
David Lam | da9d3b7 | 2016-08-29 09:03:12 -0700 | [diff] [blame] | 2927 | }; /* typedef'd tp ZSTD_CDict within "zstd.h" */ |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2928 | |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 2929 | size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) |
| 2930 | { |
| 2931 | if (cdict==NULL) return 0; /* support sizeof on NULL */ |
Yann Collet | aca113f | 2016-12-23 22:25:03 +0100 | [diff] [blame] | 2932 | return ZSTD_sizeof_CCtx(cdict->refContext) + (cdict->dictBuffer ? cdict->dictContentSize : 0) + sizeof(*cdict); |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 2933 | } |
| 2934 | |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2935 | ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, unsigned byReference, |
| 2936 | ZSTD_parameters params, ZSTD_customMem customMem) |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2937 | { |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2938 | if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; |
| 2939 | if (!customMem.customAlloc || !customMem.customFree) return NULL; |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2940 | |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2941 | { ZSTD_CDict* const cdict = (ZSTD_CDict*) ZSTD_malloc(sizeof(ZSTD_CDict), customMem); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2942 | ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); |
| 2943 | |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2944 | if (!cdict || !cctx) { |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2945 | ZSTD_free(cdict, customMem); |
Przemyslaw Skibinski | d8114e5 | 2017-02-21 18:59:56 +0100 | [diff] [blame] | 2946 | ZSTD_freeCCtx(cctx); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2947 | return NULL; |
| 2948 | } |
| 2949 | |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2950 | if ((byReference) || (!dictBuffer) || (!dictSize)) { |
| 2951 | cdict->dictBuffer = NULL; |
| 2952 | cdict->dictContent = dictBuffer; |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2953 | } else { |
| 2954 | void* const internalBuffer = ZSTD_malloc(dictSize, customMem); |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 2955 | if (!internalBuffer) { ZSTD_free(cctx, customMem); ZSTD_free(cdict, customMem); return NULL; } |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2956 | memcpy(internalBuffer, dictBuffer, dictSize); |
| 2957 | cdict->dictBuffer = internalBuffer; |
| 2958 | cdict->dictContent = internalBuffer; |
Nick Terrell | 3b9cdf9 | 2016-10-12 20:54:42 -0700 | [diff] [blame] | 2959 | } |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2960 | |
| 2961 | { size_t const errorCode = ZSTD_compressBegin_advanced(cctx, cdict->dictContent, dictSize, params, 0); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2962 | if (ZSTD_isError(errorCode)) { |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2963 | ZSTD_free(cdict->dictBuffer, customMem); |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2964 | ZSTD_free(cdict, customMem); |
Przemyslaw Skibinski | d8114e5 | 2017-02-21 18:59:56 +0100 | [diff] [blame] | 2965 | ZSTD_freeCCtx(cctx); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2966 | return NULL; |
| 2967 | } } |
| 2968 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2969 | cdict->refContext = cctx; |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2970 | cdict->dictContentSize = dictSize; |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2971 | return cdict; |
| 2972 | } |
| 2973 | } |
| 2974 | |
| 2975 | ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) |
| 2976 | { |
| 2977 | ZSTD_customMem const allocator = { NULL, NULL, NULL }; |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 2978 | ZSTD_parameters params = ZSTD_getParams(compressionLevel, 0, dictSize); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2979 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2980 | return ZSTD_createCDict_advanced(dict, dictSize, 0, params, allocator); |
| 2981 | } |
| 2982 | |
| 2983 | ZSTD_CDict* ZSTD_createCDict_byReference(const void* dict, size_t dictSize, int compressionLevel) |
| 2984 | { |
| 2985 | ZSTD_customMem const allocator = { NULL, NULL, NULL }; |
| 2986 | ZSTD_parameters params = ZSTD_getParams(compressionLevel, 0, dictSize); |
| 2987 | params.fParams.contentSizeFlag = 1; |
| 2988 | return ZSTD_createCDict_advanced(dict, dictSize, 1, params, allocator); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2989 | } |
| 2990 | |
| 2991 | size_t ZSTD_freeCDict(ZSTD_CDict* cdict) |
| 2992 | { |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2993 | if (cdict==NULL) return 0; /* support free on NULL */ |
Yann Collet | 993060e | 2016-09-21 16:46:08 +0200 | [diff] [blame] | 2994 | { ZSTD_customMem const cMem = cdict->refContext->customMem; |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2995 | ZSTD_freeCCtx(cdict->refContext); |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 2996 | ZSTD_free(cdict->dictBuffer, cMem); |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2997 | ZSTD_free(cdict, cMem); |
| 2998 | return 0; |
| 2999 | } |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3000 | } |
| 3001 | |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 3002 | static ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { |
| 3003 | return ZSTD_getParamsFromCCtx(cdict->refContext); |
| 3004 | } |
| 3005 | |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 3006 | /* ZSTD_compressBegin_usingCDict_advanced() : |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3007 | * cdict must be != NULL */ |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 3008 | size_t ZSTD_compressBegin_usingCDict_advanced( |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3009 | ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, |
| 3010 | ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3011 | { |
Yann Collet | 7cf78f1 | 2017-04-04 12:38:14 -0700 | [diff] [blame] | 3012 | if (cdict==NULL) return ERROR(GENERIC); /* does not support NULL cdict */ |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3013 | if (cdict->dictContentSize) |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 3014 | CHECK_F( ZSTD_copyCCtx_internal(cctx, cdict->refContext, fParams, pledgedSrcSize) ) |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3015 | else { |
| 3016 | ZSTD_parameters params = cdict->refContext->params; |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3017 | params.fParams = fParams; |
Yann Collet | ab9162e | 2017-04-11 10:46:20 -0700 | [diff] [blame] | 3018 | CHECK_F(ZSTD_compressBegin_internal(cctx, NULL, 0, params, pledgedSrcSize)); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3019 | } |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3020 | return 0; |
| 3021 | } |
| 3022 | |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3023 | /* ZSTD_compressBegin_usingCDict() : |
| 3024 | * pledgedSrcSize=0 means "unknown" |
| 3025 | * if pledgedSrcSize>0, it will enable contentSizeFlag */ |
| 3026 | size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize) |
| 3027 | { |
| 3028 | ZSTD_frameParameters fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
| 3029 | fParams.contentSizeFlag = (pledgedSrcSize > 0); |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 3030 | return ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, pledgedSrcSize); |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3031 | } |
| 3032 | |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 3033 | /*! ZSTD_compress_usingCDict() : |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3034 | * Compression using a digested Dictionary. |
| 3035 | * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. |
| 3036 | * Note that compression parameters are decided at CDict creation time |
| 3037 | * while frame parameters are hardcoded */ |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3038 | size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, |
| 3039 | void* dst, size_t dstCapacity, |
| 3040 | const void* src, size_t srcSize, |
| 3041 | const ZSTD_CDict* cdict) |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3042 | { |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3043 | ZSTD_frameParameters const fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 3044 | CHECK_F (ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, srcSize)); /* will check if cdict != NULL */ |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 3045 | return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3046 | } |
| 3047 | |
| 3048 | |
| 3049 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3050 | /* ****************************************************************** |
| 3051 | * Streaming |
| 3052 | ********************************************************************/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3053 | |
| 3054 | typedef enum { zcss_init, zcss_load, zcss_flush, zcss_final } ZSTD_cStreamStage; |
| 3055 | |
| 3056 | struct ZSTD_CStream_s { |
Yann Collet | fa0c097 | 2016-09-15 14:11:01 +0200 | [diff] [blame] | 3057 | ZSTD_CCtx* cctx; |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 3058 | ZSTD_CDict* cdictLocal; |
| 3059 | const ZSTD_CDict* cdict; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3060 | char* inBuff; |
| 3061 | size_t inBuffSize; |
| 3062 | size_t inToCompress; |
| 3063 | size_t inBuffPos; |
| 3064 | size_t inBuffTarget; |
| 3065 | size_t blockSize; |
| 3066 | char* outBuff; |
| 3067 | size_t outBuffSize; |
| 3068 | size_t outBuffContentSize; |
| 3069 | size_t outBuffFlushedSize; |
| 3070 | ZSTD_cStreamStage stage; |
| 3071 | U32 checksum; |
| 3072 | U32 frameEnded; |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 3073 | U64 pledgedSrcSize; |
Yann Collet | ee5b725 | 2016-10-27 14:20:55 -0700 | [diff] [blame] | 3074 | ZSTD_parameters params; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3075 | ZSTD_customMem customMem; |
| 3076 | }; /* typedef'd to ZSTD_CStream within "zstd.h" */ |
| 3077 | |
| 3078 | ZSTD_CStream* ZSTD_createCStream(void) |
| 3079 | { |
| 3080 | return ZSTD_createCStream_advanced(defaultCustomMem); |
| 3081 | } |
| 3082 | |
| 3083 | ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem) |
| 3084 | { |
| 3085 | ZSTD_CStream* zcs; |
| 3086 | |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3087 | if (!customMem.customAlloc && !customMem.customFree) customMem = defaultCustomMem; |
| 3088 | if (!customMem.customAlloc || !customMem.customFree) return NULL; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3089 | |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3090 | zcs = (ZSTD_CStream*)ZSTD_malloc(sizeof(ZSTD_CStream), customMem); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3091 | if (zcs==NULL) return NULL; |
| 3092 | memset(zcs, 0, sizeof(ZSTD_CStream)); |
| 3093 | memcpy(&zcs->customMem, &customMem, sizeof(ZSTD_customMem)); |
Yann Collet | fa0c097 | 2016-09-15 14:11:01 +0200 | [diff] [blame] | 3094 | zcs->cctx = ZSTD_createCCtx_advanced(customMem); |
| 3095 | if (zcs->cctx == NULL) { ZSTD_freeCStream(zcs); return NULL; } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3096 | return zcs; |
| 3097 | } |
| 3098 | |
| 3099 | size_t ZSTD_freeCStream(ZSTD_CStream* zcs) |
| 3100 | { |
| 3101 | if (zcs==NULL) return 0; /* support free on NULL */ |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3102 | { ZSTD_customMem const cMem = zcs->customMem; |
Yann Collet | fa0c097 | 2016-09-15 14:11:01 +0200 | [diff] [blame] | 3103 | ZSTD_freeCCtx(zcs->cctx); |
Nick Terrell | eaf69b0 | 2017-03-21 13:20:59 -0700 | [diff] [blame] | 3104 | zcs->cctx = NULL; |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 3105 | ZSTD_freeCDict(zcs->cdictLocal); |
Nick Terrell | eaf69b0 | 2017-03-21 13:20:59 -0700 | [diff] [blame] | 3106 | zcs->cdictLocal = NULL; |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3107 | ZSTD_free(zcs->inBuff, cMem); |
Nick Terrell | eaf69b0 | 2017-03-21 13:20:59 -0700 | [diff] [blame] | 3108 | zcs->inBuff = NULL; |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3109 | ZSTD_free(zcs->outBuff, cMem); |
Nick Terrell | eaf69b0 | 2017-03-21 13:20:59 -0700 | [diff] [blame] | 3110 | zcs->outBuff = NULL; |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3111 | ZSTD_free(zcs, cMem); |
| 3112 | return 0; |
| 3113 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3117 | /*====== Initialization ======*/ |
| 3118 | |
| 3119 | size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; } |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3120 | |
| 3121 | size_t ZSTD_CStreamOutSize(void) |
| 3122 | { |
| 3123 | return ZSTD_compressBound(ZSTD_BLOCKSIZE_ABSOLUTEMAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; |
| 3124 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3125 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3126 | static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3127 | { |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 3128 | if (zcs->inBuffSize==0) return ERROR(stage_wrong); /* zcs has not been init at least once => can't reset */ |
Yann Collet | ee5b725 | 2016-10-27 14:20:55 -0700 | [diff] [blame] | 3129 | |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 3130 | if (zcs->cdict) CHECK_F(ZSTD_compressBegin_usingCDict_advanced(zcs->cctx, zcs->cdict, zcs->params.fParams, pledgedSrcSize)) |
Yann Collet | ab9162e | 2017-04-11 10:46:20 -0700 | [diff] [blame] | 3131 | else CHECK_F(ZSTD_compressBegin_internal(zcs->cctx, NULL, 0, zcs->params, pledgedSrcSize)); |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3132 | |
| 3133 | zcs->inToCompress = 0; |
| 3134 | zcs->inBuffPos = 0; |
| 3135 | zcs->inBuffTarget = zcs->blockSize; |
| 3136 | zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; |
| 3137 | zcs->stage = zcss_load; |
| 3138 | zcs->frameEnded = 0; |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 3139 | zcs->pledgedSrcSize = pledgedSrcSize; |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3140 | return 0; /* ready to go */ |
| 3141 | } |
| 3142 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3143 | size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) |
| 3144 | { |
| 3145 | |
| 3146 | zcs->params.fParams.contentSizeFlag = (pledgedSrcSize > 0); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3147 | return ZSTD_resetCStream_internal(zcs, pledgedSrcSize); |
| 3148 | } |
| 3149 | |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 3150 | /* ZSTD_initCStream_internal() : |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3151 | * params are supposed validated at this stage |
| 3152 | * and zcs->cdict is supposed to be correct */ |
| 3153 | static size_t ZSTD_initCStream_stage2(ZSTD_CStream* zcs, |
| 3154 | const ZSTD_parameters params, |
| 3155 | unsigned long long pledgedSrcSize) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3156 | { |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 3157 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3158 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3159 | /* allocate buffers */ |
| 3160 | { size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog; |
| 3161 | if (zcs->inBuffSize < neededInBuffSize) { |
Yann Collet | 02d37aa | 2017-04-05 14:53:51 -0700 | [diff] [blame] | 3162 | zcs->inBuffSize = 0; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 3163 | ZSTD_free(zcs->inBuff, zcs->customMem); |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3164 | zcs->inBuff = (char*) ZSTD_malloc(neededInBuffSize, zcs->customMem); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3165 | if (zcs->inBuff == NULL) return ERROR(memory_allocation); |
Yann Collet | 02d37aa | 2017-04-05 14:53:51 -0700 | [diff] [blame] | 3166 | zcs->inBuffSize = neededInBuffSize; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3167 | } |
| 3168 | zcs->blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, neededInBuffSize); |
| 3169 | } |
| 3170 | if (zcs->outBuffSize < ZSTD_compressBound(zcs->blockSize)+1) { |
Yann Collet | 02d37aa | 2017-04-05 14:53:51 -0700 | [diff] [blame] | 3171 | size_t const outBuffSize = ZSTD_compressBound(zcs->blockSize)+1; |
| 3172 | zcs->outBuffSize = 0; |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 3173 | ZSTD_free(zcs->outBuff, zcs->customMem); |
Yann Collet | 02d37aa | 2017-04-05 14:53:51 -0700 | [diff] [blame] | 3174 | zcs->outBuff = (char*) ZSTD_malloc(outBuffSize, zcs->customMem); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3175 | if (zcs->outBuff == NULL) return ERROR(memory_allocation); |
Yann Collet | 02d37aa | 2017-04-05 14:53:51 -0700 | [diff] [blame] | 3176 | zcs->outBuffSize = outBuffSize; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3177 | } |
| 3178 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3179 | zcs->checksum = params.fParams.checksumFlag > 0; |
Yann Collet | ee5b725 | 2016-10-27 14:20:55 -0700 | [diff] [blame] | 3180 | zcs->params = params; |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3181 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3182 | return ZSTD_resetCStream_internal(zcs, pledgedSrcSize); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3183 | } |
| 3184 | |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3185 | /* note : cdict must outlive compression session */ |
| 3186 | size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict) |
| 3187 | { |
| 3188 | if (!cdict) return ERROR(GENERIC); /* cannot handle NULL cdict (does not know what to do) */ |
Yann Collet | 4ee6b15 | 2017-04-11 11:59:44 -0700 | [diff] [blame] | 3189 | { ZSTD_parameters params = ZSTD_getParamsFromCDict(cdict); |
| 3190 | params.fParams.contentSizeFlag = 0; |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3191 | zcs->cdict = cdict; |
| 3192 | return ZSTD_initCStream_stage2(zcs, params, 0); |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | static size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, |
| 3197 | const void* dict, size_t dictSize, |
| 3198 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
| 3199 | { |
| 3200 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
| 3201 | zcs->cdict = NULL; |
| 3202 | |
| 3203 | if (dict && dictSize >= 8) { |
| 3204 | ZSTD_freeCDict(zcs->cdictLocal); |
| 3205 | zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, 0 /* copy */, params, zcs->customMem); |
| 3206 | if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); |
| 3207 | zcs->cdict = zcs->cdictLocal; |
| 3208 | } |
| 3209 | |
| 3210 | return ZSTD_initCStream_stage2(zcs, params, pledgedSrcSize); |
| 3211 | } |
| 3212 | |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 3213 | size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, |
| 3214 | const void* dict, size_t dictSize, |
| 3215 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
| 3216 | { |
| 3217 | CHECK_F( ZSTD_checkCParams(params.cParams) ); |
| 3218 | return ZSTD_initCStream_internal(zcs, dict, dictSize, params, pledgedSrcSize); |
| 3219 | } |
| 3220 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3221 | size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) |
| 3222 | { |
| 3223 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 3224 | return ZSTD_initCStream_internal(zcs, dict, dictSize, params, 0); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3225 | } |
| 3226 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 3227 | size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) |
| 3228 | { |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 3229 | ZSTD_parameters params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3230 | params.fParams.contentSizeFlag = (pledgedSrcSize>0); |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 3231 | return ZSTD_initCStream_internal(zcs, NULL, 0, params, pledgedSrcSize); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 3232 | } |
| 3233 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3234 | size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel) |
| 3235 | { |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3236 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); |
| 3237 | return ZSTD_initCStream_internal(zcs, NULL, 0, params, 0); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3238 | } |
| 3239 | |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 3240 | size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 3241 | { |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 3242 | if (zcs==NULL) return 0; /* support sizeof on NULL */ |
Nick Terrell | e06c303 | 2017-03-08 13:45:10 -0800 | [diff] [blame] | 3243 | return sizeof(*zcs) + ZSTD_sizeof_CCtx(zcs->cctx) + ZSTD_sizeof_CDict(zcs->cdictLocal) + zcs->outBuffSize + zcs->inBuffSize; |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 3244 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3245 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3246 | /*====== Compression ======*/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3247 | |
| 3248 | typedef enum { zsf_gather, zsf_flush, zsf_end } ZSTD_flush_e; |
| 3249 | |
| 3250 | MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
| 3251 | { |
| 3252 | size_t const length = MIN(dstCapacity, srcSize); |
| 3253 | memcpy(dst, src, length); |
| 3254 | return length; |
| 3255 | } |
| 3256 | |
| 3257 | static size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, |
| 3258 | void* dst, size_t* dstCapacityPtr, |
| 3259 | const void* src, size_t* srcSizePtr, |
| 3260 | ZSTD_flush_e const flush) |
| 3261 | { |
| 3262 | U32 someMoreWork = 1; |
| 3263 | const char* const istart = (const char*)src; |
| 3264 | const char* const iend = istart + *srcSizePtr; |
| 3265 | const char* ip = istart; |
| 3266 | char* const ostart = (char*)dst; |
| 3267 | char* const oend = ostart + *dstCapacityPtr; |
| 3268 | char* op = ostart; |
| 3269 | |
| 3270 | while (someMoreWork) { |
| 3271 | switch(zcs->stage) |
| 3272 | { |
| 3273 | case zcss_init: return ERROR(init_missing); /* call ZBUFF_compressInit() first ! */ |
| 3274 | |
| 3275 | case zcss_load: |
| 3276 | /* complete inBuffer */ |
| 3277 | { size_t const toLoad = zcs->inBuffTarget - zcs->inBuffPos; |
| 3278 | size_t const loaded = ZSTD_limitCopy(zcs->inBuff + zcs->inBuffPos, toLoad, ip, iend-ip); |
| 3279 | zcs->inBuffPos += loaded; |
| 3280 | ip += loaded; |
| 3281 | if ( (zcs->inBuffPos==zcs->inToCompress) || (!flush && (toLoad != loaded)) ) { |
| 3282 | someMoreWork = 0; break; /* not enough input to get a full block : stop there, wait for more */ |
| 3283 | } } |
| 3284 | /* compress current block (note : this stage cannot be stopped in the middle) */ |
| 3285 | { void* cDst; |
| 3286 | size_t cSize; |
| 3287 | size_t const iSize = zcs->inBuffPos - zcs->inToCompress; |
| 3288 | size_t oSize = oend-op; |
| 3289 | if (oSize >= ZSTD_compressBound(iSize)) |
| 3290 | cDst = op; /* compress directly into output buffer (avoid flush stage) */ |
| 3291 | else |
| 3292 | cDst = zcs->outBuff, oSize = zcs->outBuffSize; |
| 3293 | cSize = (flush == zsf_end) ? |
Yann Collet | fa0c097 | 2016-09-15 14:11:01 +0200 | [diff] [blame] | 3294 | ZSTD_compressEnd(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize) : |
| 3295 | ZSTD_compressContinue(zcs->cctx, cDst, oSize, zcs->inBuff + zcs->inToCompress, iSize); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3296 | if (ZSTD_isError(cSize)) return cSize; |
| 3297 | if (flush == zsf_end) zcs->frameEnded = 1; |
| 3298 | /* prepare next block */ |
| 3299 | zcs->inBuffTarget = zcs->inBuffPos + zcs->blockSize; |
| 3300 | if (zcs->inBuffTarget > zcs->inBuffSize) |
| 3301 | zcs->inBuffPos = 0, zcs->inBuffTarget = zcs->blockSize; /* note : inBuffSize >= blockSize */ |
| 3302 | zcs->inToCompress = zcs->inBuffPos; |
| 3303 | if (cDst == op) { op += cSize; break; } /* no need to flush */ |
| 3304 | zcs->outBuffContentSize = cSize; |
| 3305 | zcs->outBuffFlushedSize = 0; |
| 3306 | zcs->stage = zcss_flush; /* pass-through to flush stage */ |
| 3307 | } |
| 3308 | |
| 3309 | case zcss_flush: |
| 3310 | { size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; |
| 3311 | size_t const flushed = ZSTD_limitCopy(op, oend-op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush); |
| 3312 | op += flushed; |
| 3313 | zcs->outBuffFlushedSize += flushed; |
| 3314 | if (toFlush!=flushed) { someMoreWork = 0; break; } /* dst too small to store flushed data : stop there */ |
| 3315 | zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; |
| 3316 | zcs->stage = zcss_load; |
| 3317 | break; |
| 3318 | } |
| 3319 | |
| 3320 | case zcss_final: |
| 3321 | someMoreWork = 0; /* do nothing */ |
| 3322 | break; |
| 3323 | |
| 3324 | default: |
| 3325 | return ERROR(GENERIC); /* impossible */ |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | *srcSizePtr = ip - istart; |
| 3330 | *dstCapacityPtr = op - ostart; |
| 3331 | if (zcs->frameEnded) return 0; |
| 3332 | { size_t hintInSize = zcs->inBuffTarget - zcs->inBuffPos; |
| 3333 | if (hintInSize==0) hintInSize = zcs->blockSize; |
| 3334 | return hintInSize; |
| 3335 | } |
| 3336 | } |
| 3337 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3338 | size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3339 | { |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3340 | size_t sizeRead = input->size - input->pos; |
| 3341 | size_t sizeWritten = output->size - output->pos; |
| 3342 | size_t const result = ZSTD_compressStream_generic(zcs, |
| 3343 | (char*)(output->dst) + output->pos, &sizeWritten, |
| 3344 | (const char*)(input->src) + input->pos, &sizeRead, zsf_gather); |
| 3345 | input->pos += sizeRead; |
| 3346 | output->pos += sizeWritten; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3347 | return result; |
| 3348 | } |
| 3349 | |
| 3350 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3351 | /*====== Finalize ======*/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3352 | |
| 3353 | /*! ZSTD_flushStream() : |
| 3354 | * @return : amount of data remaining to flush */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3355 | size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3356 | { |
| 3357 | size_t srcSize = 0; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3358 | size_t sizeWritten = output->size - output->pos; |
Yann Collet | c411902 | 2016-08-17 01:50:54 +0200 | [diff] [blame] | 3359 | size_t const result = ZSTD_compressStream_generic(zcs, |
Yann Collet | 95d07d7 | 2016-09-06 16:38:51 +0200 | [diff] [blame] | 3360 | (char*)(output->dst) + output->pos, &sizeWritten, |
| 3361 | &srcSize, &srcSize, /* use a valid src address instead of NULL */ |
Yann Collet | c411902 | 2016-08-17 01:50:54 +0200 | [diff] [blame] | 3362 | zsf_flush); |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3363 | output->pos += sizeWritten; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3364 | if (ZSTD_isError(result)) return result; |
| 3365 | return zcs->outBuffContentSize - zcs->outBuffFlushedSize; /* remaining to flush */ |
| 3366 | } |
| 3367 | |
| 3368 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3369 | size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3370 | { |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3371 | BYTE* const ostart = (BYTE*)(output->dst) + output->pos; |
| 3372 | BYTE* const oend = (BYTE*)(output->dst) + output->size; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3373 | BYTE* op = ostart; |
| 3374 | |
| 3375 | if (zcs->stage != zcss_final) { |
| 3376 | /* flush whatever remains */ |
| 3377 | size_t srcSize = 0; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3378 | size_t sizeWritten = output->size - output->pos; |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3379 | size_t const notEnded = ZSTD_compressStream_generic(zcs, ostart, &sizeWritten, |
| 3380 | &srcSize /* use a valid src address instead of NULL */, &srcSize, zsf_end); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3381 | size_t const remainingToFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; |
| 3382 | op += sizeWritten; |
| 3383 | if (remainingToFlush) { |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3384 | output->pos += sizeWritten; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3385 | return remainingToFlush + ZSTD_BLOCKHEADERSIZE /* final empty block */ + (zcs->checksum * 4); |
| 3386 | } |
| 3387 | /* create epilogue */ |
| 3388 | zcs->stage = zcss_final; |
| 3389 | zcs->outBuffContentSize = !notEnded ? 0 : |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3390 | /* write epilogue, including final empty block, into outBuff */ |
| 3391 | ZSTD_compressEnd(zcs->cctx, zcs->outBuff, zcs->outBuffSize, NULL, 0); |
Yann Collet | 88009a8 | 2017-04-12 00:51:24 -0700 | [diff] [blame] | 3392 | if (ZSTD_isError(zcs->outBuffContentSize)) return zcs->outBuffContentSize; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | /* flush epilogue */ |
| 3396 | { size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; |
| 3397 | size_t const flushed = ZSTD_limitCopy(op, oend-op, zcs->outBuff + zcs->outBuffFlushedSize, toFlush); |
| 3398 | op += flushed; |
| 3399 | zcs->outBuffFlushedSize += flushed; |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3400 | output->pos += op-ostart; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3401 | if (toFlush==flushed) zcs->stage = zcss_init; /* end reached */ |
| 3402 | return toFlush - flushed; |
| 3403 | } |
| 3404 | } |
| 3405 | |
| 3406 | |
| 3407 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 3408 | /*-===== Pre-defined compression levels =====-*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3409 | |
Yann Collet | 236d94f | 2016-05-18 12:06:33 +0200 | [diff] [blame] | 3410 | #define ZSTD_DEFAULT_CLEVEL 1 |
inikep | 2c5eeea | 2016-04-15 13:44:46 +0200 | [diff] [blame] | 3411 | #define ZSTD_MAX_CLEVEL 22 |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 3412 | int ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 3413 | |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3414 | static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL+1] = { |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3415 | { /* "default" */ |
Yann Collet | 793c649 | 2016-04-09 20:32:00 +0200 | [diff] [blame] | 3416 | /* W, C, H, S, L, TL, strat */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3417 | { 18, 12, 12, 1, 7, 16, ZSTD_fast }, /* level 0 - never used */ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 3418 | { 19, 13, 14, 1, 7, 16, ZSTD_fast }, /* level 1 */ |
| 3419 | { 19, 15, 16, 1, 6, 16, ZSTD_fast }, /* level 2 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3420 | { 20, 16, 17, 1, 5, 16, ZSTD_dfast }, /* level 3.*/ |
| 3421 | { 20, 18, 18, 1, 5, 16, ZSTD_dfast }, /* level 4.*/ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 3422 | { 20, 15, 18, 3, 5, 16, ZSTD_greedy }, /* level 5 */ |
| 3423 | { 21, 16, 19, 2, 5, 16, ZSTD_lazy }, /* level 6 */ |
| 3424 | { 21, 17, 20, 3, 5, 16, ZSTD_lazy }, /* level 7 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3425 | { 21, 18, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 8 */ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 3426 | { 21, 20, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 9 */ |
| 3427 | { 21, 19, 21, 4, 5, 16, ZSTD_lazy2 }, /* level 10 */ |
| 3428 | { 22, 20, 22, 4, 5, 16, ZSTD_lazy2 }, /* level 11 */ |
| 3429 | { 22, 20, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 12 */ |
| 3430 | { 22, 21, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 13 */ |
| 3431 | { 22, 21, 22, 6, 5, 16, ZSTD_lazy2 }, /* level 14 */ |
| 3432 | { 22, 21, 21, 5, 5, 16, ZSTD_btlazy2 }, /* level 15 */ |
| 3433 | { 23, 22, 22, 5, 5, 16, ZSTD_btlazy2 }, /* level 16 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3434 | { 23, 21, 22, 4, 5, 24, ZSTD_btopt }, /* level 17 */ |
| 3435 | { 23, 23, 22, 6, 5, 32, ZSTD_btopt }, /* level 18 */ |
| 3436 | { 23, 23, 22, 6, 3, 48, ZSTD_btopt }, /* level 19 */ |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 3437 | { 25, 25, 23, 7, 3, 64, ZSTD_btopt2 }, /* level 20 */ |
| 3438 | { 26, 26, 23, 7, 3,256, ZSTD_btopt2 }, /* level 21 */ |
| 3439 | { 27, 27, 25, 9, 3,512, ZSTD_btopt2 }, /* level 22 */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3440 | }, |
| 3441 | { /* for srcSize <= 256 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3442 | /* W, C, H, S, L, T, strat */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3443 | { 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - not used */ |
Yann Collet | a2cdffe | 2016-08-24 19:42:15 +0200 | [diff] [blame] | 3444 | { 18, 13, 14, 1, 6, 8, ZSTD_fast }, /* level 1 */ |
Yann Collet | 24b68a5 | 2016-08-24 14:22:26 +0200 | [diff] [blame] | 3445 | { 18, 14, 13, 1, 5, 8, ZSTD_dfast }, /* level 2 */ |
| 3446 | { 18, 16, 15, 1, 5, 8, ZSTD_dfast }, /* level 3 */ |
| 3447 | { 18, 15, 17, 1, 5, 8, ZSTD_greedy }, /* level 4.*/ |
| 3448 | { 18, 16, 17, 4, 5, 8, ZSTD_greedy }, /* level 5.*/ |
| 3449 | { 18, 16, 17, 3, 5, 8, ZSTD_lazy }, /* level 6.*/ |
| 3450 | { 18, 17, 17, 4, 4, 8, ZSTD_lazy }, /* level 7 */ |
| 3451 | { 18, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ |
| 3452 | { 18, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ |
| 3453 | { 18, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ |
| 3454 | { 18, 18, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 11.*/ |
| 3455 | { 18, 18, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 12.*/ |
| 3456 | { 18, 19, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13 */ |
Yann Collet | 78267d1 | 2016-04-08 12:36:19 +0200 | [diff] [blame] | 3457 | { 18, 18, 18, 4, 4, 16, ZSTD_btopt }, /* level 14.*/ |
Yann Collet | 24b68a5 | 2016-08-24 14:22:26 +0200 | [diff] [blame] | 3458 | { 18, 18, 18, 4, 3, 16, ZSTD_btopt }, /* level 15.*/ |
| 3459 | { 18, 19, 18, 6, 3, 32, ZSTD_btopt }, /* level 16.*/ |
| 3460 | { 18, 19, 18, 8, 3, 64, ZSTD_btopt }, /* level 17.*/ |
Yann Collet | 78267d1 | 2016-04-08 12:36:19 +0200 | [diff] [blame] | 3461 | { 18, 19, 18, 9, 3,128, ZSTD_btopt }, /* level 18.*/ |
| 3462 | { 18, 19, 18, 10, 3,256, ZSTD_btopt }, /* level 19.*/ |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 3463 | { 18, 19, 18, 11, 3,512, ZSTD_btopt2 }, /* level 20.*/ |
| 3464 | { 18, 19, 18, 12, 3,512, ZSTD_btopt2 }, /* level 21.*/ |
| 3465 | { 18, 19, 18, 13, 3,512, ZSTD_btopt2 }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3466 | }, |
| 3467 | { /* for srcSize <= 128 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3468 | /* W, C, H, S, L, T, strat */ |
Yann Collet | 5894ea8 | 2016-07-22 14:36:46 +0200 | [diff] [blame] | 3469 | { 17, 12, 12, 1, 7, 8, ZSTD_fast }, /* level 0 - not used */ |
| 3470 | { 17, 12, 13, 1, 6, 8, ZSTD_fast }, /* level 1 */ |
| 3471 | { 17, 13, 16, 1, 5, 8, ZSTD_fast }, /* level 2 */ |
| 3472 | { 17, 16, 16, 2, 5, 8, ZSTD_dfast }, /* level 3 */ |
| 3473 | { 17, 13, 15, 3, 4, 8, ZSTD_greedy }, /* level 4 */ |
| 3474 | { 17, 15, 17, 4, 4, 8, ZSTD_greedy }, /* level 5 */ |
| 3475 | { 17, 16, 17, 3, 4, 8, ZSTD_lazy }, /* level 6 */ |
| 3476 | { 17, 15, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 7 */ |
| 3477 | { 17, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ |
| 3478 | { 17, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ |
| 3479 | { 17, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ |
| 3480 | { 17, 17, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 11 */ |
| 3481 | { 17, 17, 17, 8, 4, 8, ZSTD_lazy2 }, /* level 12 */ |
| 3482 | { 17, 18, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13.*/ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3483 | { 17, 17, 17, 7, 3, 8, ZSTD_btopt }, /* level 14.*/ |
| 3484 | { 17, 17, 17, 7, 3, 16, ZSTD_btopt }, /* level 15.*/ |
| 3485 | { 17, 18, 17, 7, 3, 32, ZSTD_btopt }, /* level 16.*/ |
| 3486 | { 17, 18, 17, 7, 3, 64, ZSTD_btopt }, /* level 17.*/ |
| 3487 | { 17, 18, 17, 7, 3,256, ZSTD_btopt }, /* level 18.*/ |
| 3488 | { 17, 18, 17, 8, 3,256, ZSTD_btopt }, /* level 19.*/ |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 3489 | { 17, 18, 17, 9, 3,256, ZSTD_btopt2 }, /* level 20.*/ |
| 3490 | { 17, 18, 17, 10, 3,256, ZSTD_btopt2 }, /* level 21.*/ |
| 3491 | { 17, 18, 17, 11, 3,512, ZSTD_btopt2 }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3492 | }, |
| 3493 | { /* for srcSize <= 16 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3494 | /* W, C, H, S, L, T, strat */ |
Yann Collet | 2b1a363 | 2016-07-13 15:16:00 +0200 | [diff] [blame] | 3495 | { 14, 12, 12, 1, 7, 6, ZSTD_fast }, /* level 0 - not used */ |
Yann Collet | e557fd5 | 2016-07-17 16:21:37 +0200 | [diff] [blame] | 3496 | { 14, 14, 14, 1, 6, 6, ZSTD_fast }, /* level 1 */ |
Yann Collet | 2b1a363 | 2016-07-13 15:16:00 +0200 | [diff] [blame] | 3497 | { 14, 14, 14, 1, 4, 6, ZSTD_fast }, /* level 2 */ |
| 3498 | { 14, 14, 14, 1, 4, 6, ZSTD_dfast }, /* level 3.*/ |
| 3499 | { 14, 14, 14, 4, 4, 6, ZSTD_greedy }, /* level 4.*/ |
| 3500 | { 14, 14, 14, 3, 4, 6, ZSTD_lazy }, /* level 5.*/ |
| 3501 | { 14, 14, 14, 4, 4, 6, ZSTD_lazy2 }, /* level 6 */ |
| 3502 | { 14, 14, 14, 5, 4, 6, ZSTD_lazy2 }, /* level 7 */ |
| 3503 | { 14, 14, 14, 6, 4, 6, ZSTD_lazy2 }, /* level 8.*/ |
| 3504 | { 14, 15, 14, 6, 4, 6, ZSTD_btlazy2 }, /* level 9.*/ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3505 | { 14, 15, 14, 3, 3, 6, ZSTD_btopt }, /* level 10.*/ |
| 3506 | { 14, 15, 14, 6, 3, 8, ZSTD_btopt }, /* level 11.*/ |
| 3507 | { 14, 15, 14, 6, 3, 16, ZSTD_btopt }, /* level 12.*/ |
| 3508 | { 14, 15, 14, 6, 3, 24, ZSTD_btopt }, /* level 13.*/ |
| 3509 | { 14, 15, 15, 6, 3, 48, ZSTD_btopt }, /* level 14.*/ |
| 3510 | { 14, 15, 15, 6, 3, 64, ZSTD_btopt }, /* level 15.*/ |
| 3511 | { 14, 15, 15, 6, 3, 96, ZSTD_btopt }, /* level 16.*/ |
| 3512 | { 14, 15, 15, 6, 3,128, ZSTD_btopt }, /* level 17.*/ |
| 3513 | { 14, 15, 15, 6, 3,256, ZSTD_btopt }, /* level 18.*/ |
| 3514 | { 14, 15, 15, 7, 3,256, ZSTD_btopt }, /* level 19.*/ |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 3515 | { 14, 15, 15, 8, 3,256, ZSTD_btopt2 }, /* level 20.*/ |
| 3516 | { 14, 15, 15, 9, 3,256, ZSTD_btopt2 }, /* level 21.*/ |
| 3517 | { 14, 15, 15, 10, 3,256, ZSTD_btopt2 }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3518 | }, |
| 3519 | }; |
| 3520 | |
Yann Collet | 236d94f | 2016-05-18 12:06:33 +0200 | [diff] [blame] | 3521 | /*! ZSTD_getCParams() : |
| 3522 | * @return ZSTD_compressionParameters structure for a selected compression level, `srcSize` and `dictSize`. |
| 3523 | * Size values are optional, provide 0 if not known or unused */ |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 3524 | ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long srcSize, size_t dictSize) |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3525 | { |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 3526 | ZSTD_compressionParameters cp; |
Yann Collet | 1005fc1 | 2016-04-04 13:28:28 +0200 | [diff] [blame] | 3527 | size_t const addedSize = srcSize ? 0 : 500; |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 3528 | U64 const rSize = srcSize+dictSize ? srcSize+dictSize+addedSize : (U64)-1; |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3529 | U32 const tableID = (rSize <= 256 KB) + (rSize <= 128 KB) + (rSize <= 16 KB); /* intentional underflow for srcSizeHint == 0 */ |
Yann Collet | 236d94f | 2016-05-18 12:06:33 +0200 | [diff] [blame] | 3530 | if (compressionLevel <= 0) compressionLevel = ZSTD_DEFAULT_CLEVEL; /* 0 == default; no negative compressionLevel yet */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3531 | if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 3532 | cp = ZSTD_defaultCParameters[tableID][compressionLevel]; |
Yann Collet | 1005fc1 | 2016-04-04 13:28:28 +0200 | [diff] [blame] | 3533 | if (MEM_32bits()) { /* auto-correction, for 32-bits mode */ |
| 3534 | if (cp.windowLog > ZSTD_WINDOWLOG_MAX) cp.windowLog = ZSTD_WINDOWLOG_MAX; |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 3535 | if (cp.chainLog > ZSTD_CHAINLOG_MAX) cp.chainLog = ZSTD_CHAINLOG_MAX; |
Yann Collet | 1005fc1 | 2016-04-04 13:28:28 +0200 | [diff] [blame] | 3536 | if (cp.hashLog > ZSTD_HASHLOG_MAX) cp.hashLog = ZSTD_HASHLOG_MAX; |
| 3537 | } |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 3538 | cp = ZSTD_adjustCParams(cp, srcSize, dictSize); |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 3539 | return cp; |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3540 | } |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 3541 | |
| 3542 | /*! ZSTD_getParams() : |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 3543 | * same as ZSTD_getCParams(), but @return a `ZSTD_parameters` object (instead of `ZSTD_compressionParameters`). |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 3544 | * All fields of `ZSTD_frameParameters` are set to default (0) */ |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 3545 | ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long srcSize, size_t dictSize) { |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 3546 | ZSTD_parameters params; |
| 3547 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, srcSize, dictSize); |
| 3548 | memset(¶ms, 0, sizeof(params)); |
| 3549 | params.cParams = cParams; |
| 3550 | return params; |
| 3551 | } |