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