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