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