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