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 | |
Yann Collet | bf99150 | 2017-06-19 12:56:25 -0700 | [diff] [blame] | 18 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 19 | /*-************************************* |
Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 20 | * Dependencies |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 21 | ***************************************/ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 22 | #include <string.h> /* memset */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 23 | #include "mem.h" |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 24 | #define FSE_STATIC_LINKING_ONLY /* FSE_encodeSymbol */ |
Yann Collet | d0e2cd1 | 2016-06-05 00:58:01 +0200 | [diff] [blame] | 25 | #include "fse.h" |
Yann Collet | 130fe11 | 2016-06-05 00:42:28 +0200 | [diff] [blame] | 26 | #define HUF_STATIC_LINKING_ONLY |
| 27 | #include "huf.h" |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 28 | #include "zstd_internal.h" /* includes zstd.h */ |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 29 | #include "zstdmt_compress.h" |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 30 | |
| 31 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 32 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 33 | * Constants |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 34 | ***************************************/ |
Yann Collet | bb60448 | 2016-03-19 15:18:42 +0100 | [diff] [blame] | 35 | static const U32 g_searchStrength = 8; /* control skip over incompressible data */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 36 | #define HASH_READ_SIZE 8 |
| 37 | 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] | 38 | |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 39 | /* entropy tables always have same size */ |
| 40 | static size_t const hufCTable_size = HUF_CTABLE_SIZE(255); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 41 | static size_t const litlengthCTable_size = FSE_CTABLE_SIZE(LLFSELog, MaxLL); |
| 42 | static size_t const offcodeCTable_size = FSE_CTABLE_SIZE(OffFSELog, MaxOff); |
| 43 | static size_t const matchlengthCTable_size = FSE_CTABLE_SIZE(MLFSELog, MaxML); |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 44 | static size_t const entropyScratchSpace_size = HUF_WORKSPACE_SIZE; |
Yann Collet | a34a39c | 2017-04-20 18:17:58 -0700 | [diff] [blame] | 45 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 46 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 47 | /*-************************************* |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 48 | * Helper functions |
| 49 | ***************************************/ |
Yann Collet | 3f75d52 | 2017-03-31 17:11:38 -0700 | [diff] [blame] | 50 | size_t ZSTD_compressBound(size_t srcSize) { |
Yann Collet | 30c7698 | 2017-03-31 18:27:03 -0700 | [diff] [blame] | 51 | size_t const lowLimit = 256 KB; |
| 52 | 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] | 53 | return srcSize + (srcSize >> 8) + margin; |
| 54 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 55 | |
| 56 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 57 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 58 | * Sequence storage |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 59 | ***************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 60 | static void ZSTD_resetSeqStore(seqStore_t* ssPtr) |
| 61 | { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 62 | ssPtr->lit = ssPtr->litStart; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 63 | ssPtr->sequences = ssPtr->sequencesStart; |
Yann Collet | 5d39357 | 2016-04-07 17:19:00 +0200 | [diff] [blame] | 64 | ssPtr->longLengthID = 0; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 68 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 69 | * Context memory management |
| 70 | ***************************************/ |
Yann Collet | 466f92e | 2017-06-20 16:25:29 -0700 | [diff] [blame] | 71 | typedef enum { zcss_init=0, zcss_load, zcss_flush } ZSTD_cStreamStage; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 72 | |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 73 | struct ZSTD_CDict_s { |
| 74 | void* dictBuffer; |
| 75 | const void* dictContent; |
| 76 | size_t dictContentSize; |
| 77 | ZSTD_CCtx* refContext; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 78 | }; /* typedef'd to ZSTD_CDict within "zstd.h" */ |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 79 | |
Yann Collet | aca113f | 2016-12-23 22:25:03 +0100 | [diff] [blame] | 80 | struct ZSTD_CCtx_s { |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 81 | const BYTE* nextSrc; /* next block here to continue on current prefix */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 82 | const BYTE* base; /* All regular indexes relative to this position */ |
| 83 | const BYTE* dictBase; /* extDict indexes relative to this position */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 84 | U32 dictLimit; /* below that point, need extDict */ |
Yann Collet | eeb8ba1 | 2015-10-22 16:55:40 +0100 | [diff] [blame] | 85 | U32 lowLimit; /* below that point, no more data */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 86 | U32 nextToUpdate; /* index from which to continue dictionary update */ |
inikep | cc52a97 | 2016-02-19 10:09:35 +0100 | [diff] [blame] | 87 | U32 nextToUpdate3; /* index from which to continue dictionary update */ |
inikep | 7adceef | 2016-03-23 15:53:38 +0100 | [diff] [blame] | 88 | U32 hashLog3; /* dispatch table : larger == faster, more memory */ |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 89 | U32 loadedDictEnd; /* index of end of dictionary */ |
| 90 | U32 forceWindow; /* force back-references to respect limit of 1<<wLog, even for dictionary */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 91 | ZSTD_dictMode_e dictMode; /* select restricting dictionary to "rawContent" or "fullDict" only */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 92 | ZSTD_compressionStage_e stage; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 93 | U32 rep[ZSTD_REP_NUM]; |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 94 | U32 repToConfirm[ZSTD_REP_NUM]; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 95 | U32 dictID; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 96 | int compressionLevel; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 97 | ZSTD_parameters requestedParams; |
| 98 | ZSTD_parameters appliedParams; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 99 | void* workSpace; |
| 100 | size_t workSpaceSize; |
Yann Collet | 120230b | 2015-12-02 14:00:45 +0100 | [diff] [blame] | 101 | size_t blockSize; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 102 | U64 pledgedSrcSizePlusOne; /* this way, 0 (default) == unknown */ |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 103 | U64 consumedSrcSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 104 | XXH64_state_t xxhState; |
inikep | 2866951 | 2016-06-02 13:04:18 +0200 | [diff] [blame] | 105 | ZSTD_customMem customMem; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 106 | size_t staticSize; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 107 | |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 108 | seqStore_t seqStore; /* sequences storage ptrs */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 109 | U32* hashTable; |
inikep | cc52a97 | 2016-02-19 10:09:35 +0100 | [diff] [blame] | 110 | U32* hashTable3; |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 111 | U32* chainTable; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 112 | HUF_repeat hufCTable_repeatMode; |
| 113 | HUF_CElt* hufCTable; |
| 114 | U32 fseCTables_ready; |
Yann Collet | 71aaa32 | 2017-04-20 23:03:38 -0700 | [diff] [blame] | 115 | FSE_CTable* offcodeCTable; |
| 116 | FSE_CTable* matchlengthCTable; |
| 117 | FSE_CTable* litlengthCTable; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 118 | unsigned* entropyScratchSpace; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 119 | |
| 120 | /* streaming */ |
| 121 | ZSTD_CDict* cdictLocal; |
| 122 | const ZSTD_CDict* cdict; |
| 123 | char* inBuff; |
| 124 | size_t inBuffSize; |
| 125 | size_t inToCompress; |
| 126 | size_t inBuffPos; |
| 127 | size_t inBuffTarget; |
| 128 | char* outBuff; |
| 129 | size_t outBuffSize; |
| 130 | size_t outBuffContentSize; |
| 131 | size_t outBuffFlushedSize; |
| 132 | ZSTD_cStreamStage streamStage; |
| 133 | U32 frameEnded; |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 134 | |
| 135 | /* Multi-threading */ |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 136 | U32 nbThreads; |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 137 | ZSTDMT_CCtx* mtctx; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 140 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 141 | ZSTD_CCtx* ZSTD_createCCtx(void) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 142 | { |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 143 | return ZSTD_createCCtx_advanced(ZSTD_defaultCMem); |
inikep | 50e82c0 | 2016-05-23 15:49:09 +0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) |
| 147 | { |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 148 | ZSTD_CCtx* cctx; |
inikep | 50e82c0 | 2016-05-23 15:49:09 +0200 | [diff] [blame] | 149 | |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 150 | if (!customMem.customAlloc ^ !customMem.customFree) return NULL; |
inikep | 107e243 | 2016-05-23 16:24:52 +0200 | [diff] [blame] | 151 | |
Yann Collet | c4f46b9 | 2017-05-30 17:45:37 -0700 | [diff] [blame] | 152 | cctx = (ZSTD_CCtx*) ZSTD_calloc(sizeof(ZSTD_CCtx), customMem); |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 153 | if (!cctx) return NULL; |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 154 | cctx->customMem = customMem; |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 155 | cctx->compressionLevel = ZSTD_CLEVEL_DEFAULT; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 156 | ZSTD_STATIC_ASSERT(zcss_init==0); |
Yann Collet | d3de3d5 | 2017-06-16 16:51:33 -0700 | [diff] [blame] | 157 | ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_UNKNOWN==(0ULL - 1)); |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 158 | return cctx; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 159 | } |
| 160 | |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 161 | ZSTD_CCtx* ZSTD_initStaticCCtx(void *workspace, size_t workspaceSize) |
| 162 | { |
| 163 | ZSTD_CCtx* cctx = (ZSTD_CCtx*) workspace; |
| 164 | if (workspaceSize <= sizeof(ZSTD_CCtx)) return NULL; /* minimum size */ |
| 165 | if ((size_t)workspace & 7) return NULL; /* must be 8-aligned */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 166 | memset(workspace, 0, workspaceSize); /* may be a bit generous, could memset be smaller ? */ |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 167 | cctx->staticSize = workspaceSize; |
| 168 | cctx->workSpace = (void*)(cctx+1); |
| 169 | cctx->workSpaceSize = workspaceSize - sizeof(ZSTD_CCtx); |
| 170 | |
| 171 | /* entropy space (never moves) */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 172 | /* note : this code should be shared with resetCCtx, rather than copy/pasted */ |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 173 | { void* ptr = cctx->workSpace; |
| 174 | cctx->hufCTable = (HUF_CElt*)ptr; |
Yann Collet | 0fdc71c | 2017-05-24 17:41:41 -0700 | [diff] [blame] | 175 | ptr = (char*)cctx->hufCTable + hufCTable_size; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 176 | cctx->offcodeCTable = (FSE_CTable*) ptr; |
| 177 | ptr = (char*)ptr + offcodeCTable_size; |
| 178 | cctx->matchlengthCTable = (FSE_CTable*) ptr; |
| 179 | ptr = (char*)ptr + matchlengthCTable_size; |
| 180 | cctx->litlengthCTable = (FSE_CTable*) ptr; |
| 181 | ptr = (char*)ptr + litlengthCTable_size; |
| 182 | assert(((size_t)ptr & 3) == 0); /* ensure correct alignment */ |
| 183 | cctx->entropyScratchSpace = (unsigned*) ptr; |
| 184 | } |
| 185 | |
| 186 | return cctx; |
| 187 | } |
| 188 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 189 | size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 190 | { |
inikep | 3640396 | 2016-06-03 16:36:50 +0200 | [diff] [blame] | 191 | if (cctx==NULL) return 0; /* support free on NULL */ |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 192 | if (cctx->staticSize) return ERROR(memory_allocation); /* not compatible with static CCtx */ |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 193 | ZSTD_free(cctx->workSpace, cctx->customMem); |
Yann Collet | 7855366 | 2017-05-08 17:15:00 -0700 | [diff] [blame] | 194 | cctx->workSpace = NULL; |
| 195 | ZSTD_freeCDict(cctx->cdictLocal); |
| 196 | cctx->cdictLocal = NULL; |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 197 | ZSTDMT_freeCCtx(cctx->mtctx); |
| 198 | cctx->mtctx = NULL; |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 199 | ZSTD_free(cctx, cctx->customMem); |
Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 200 | return 0; /* reserved as a potential error code in the future */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 201 | } |
| 202 | |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 203 | size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx) |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 204 | { |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 205 | if (cctx==NULL) return 0; /* support sizeof on NULL */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 206 | DEBUGLOG(5, "sizeof(*cctx) : %u", (U32)sizeof(*cctx)); |
| 207 | DEBUGLOG(5, "workSpaceSize : %u", (U32)cctx->workSpaceSize); |
| 208 | DEBUGLOG(5, "streaming buffers : %u", (U32)(cctx->outBuffSize + cctx->inBuffSize)); |
| 209 | DEBUGLOG(5, "inner MTCTX : %u", (U32)ZSTDMT_sizeof_CCtx(cctx->mtctx)); |
Yann Collet | 791d744 | 2017-05-08 16:17:30 -0700 | [diff] [blame] | 210 | return sizeof(*cctx) + cctx->workSpaceSize |
| 211 | + ZSTD_sizeof_CDict(cctx->cdictLocal) |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 212 | + cctx->outBuffSize + cctx->inBuffSize |
| 213 | + ZSTDMT_sizeof_CCtx(cctx->mtctx); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 216 | size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) |
| 217 | { |
| 218 | return ZSTD_sizeof_CCtx(zcs); /* same object */ |
| 219 | } |
| 220 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 221 | /* private API call, for dictBuilder only */ |
| 222 | const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); } |
| 223 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 224 | static ZSTD_parameters ZSTD_getParamsFromCCtx(const ZSTD_CCtx* cctx) { return cctx->appliedParams; } |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 225 | |
Yann Collet | ef738c1 | 2017-05-12 13:53:46 -0700 | [diff] [blame] | 226 | /* older variant; will be deprecated */ |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 227 | size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value) |
| 228 | { |
| 229 | switch(param) |
| 230 | { |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 231 | case ZSTD_p_forceWindow : cctx->forceWindow = value>0; cctx->loadedDictEnd = 0; return 0; |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 232 | ZSTD_STATIC_ASSERT(ZSTD_dm_auto==0); |
| 233 | ZSTD_STATIC_ASSERT(ZSTD_dm_rawContent==1); |
| 234 | case ZSTD_p_forceRawDict : cctx->dictMode = (ZSTD_dictMode_e)(value>0); return 0; |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 235 | default: return ERROR(parameter_unknown); |
| 236 | } |
| 237 | } |
| 238 | |
Yann Collet | add66f8 | 2017-05-12 15:59:48 -0700 | [diff] [blame] | 239 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 240 | #define ZSTD_CLEVEL_CUSTOM 999 |
Yann Collet | add66f8 | 2017-05-12 15:59:48 -0700 | [diff] [blame] | 241 | static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) |
| 242 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 243 | if (cctx->compressionLevel==ZSTD_CLEVEL_CUSTOM) return; |
| 244 | cctx->requestedParams.cParams = ZSTD_getCParams(cctx->compressionLevel, |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 245 | cctx->pledgedSrcSizePlusOne-1, 0); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 246 | cctx->compressionLevel = ZSTD_CLEVEL_CUSTOM; |
Yann Collet | add66f8 | 2017-05-12 15:59:48 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Yann Collet | c3bce24 | 2017-06-20 16:09:11 -0700 | [diff] [blame] | 249 | #define CLAMPCHECK(val,min,max) { \ |
| 250 | if (((val)<(min)) | ((val)>(max))) { \ |
| 251 | return ERROR(compressionParameter_outOfBound); \ |
| 252 | } } |
| 253 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 254 | 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] | 255 | { |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 256 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 257 | |
| 258 | switch(param) |
| 259 | { |
| 260 | case ZSTD_p_compressionLevel : |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 261 | if ((int)value > ZSTD_maxCLevel()) value = ZSTD_maxCLevel(); /* cap max compression level */ |
| 262 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 263 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 264 | cctx->compressionLevel = value; |
| 265 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 266 | |
| 267 | case ZSTD_p_windowLog : |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 268 | DEBUGLOG(5, "setting ZSTD_p_windowLog = %u (cdict:%u)", |
| 269 | value, (cctx->cdict!=NULL)); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 270 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 271 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 272 | CLAMPCHECK(value, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
| 273 | ZSTD_cLevelToCParams(cctx); |
| 274 | cctx->requestedParams.cParams.windowLog = value; |
| 275 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 276 | |
| 277 | case ZSTD_p_hashLog : |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 278 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 279 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 280 | CLAMPCHECK(value, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 281 | ZSTD_cLevelToCParams(cctx); |
| 282 | cctx->requestedParams.cParams.hashLog = value; |
| 283 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 284 | |
| 285 | case ZSTD_p_chainLog : |
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" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 287 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 288 | CLAMPCHECK(value, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); |
| 289 | ZSTD_cLevelToCParams(cctx); |
| 290 | cctx->requestedParams.cParams.chainLog = value; |
| 291 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 292 | |
| 293 | case ZSTD_p_searchLog : |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 294 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 295 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 296 | CLAMPCHECK(value, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
| 297 | ZSTD_cLevelToCParams(cctx); |
| 298 | cctx->requestedParams.cParams.searchLog = value; |
| 299 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 300 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 301 | case ZSTD_p_minMatch : |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 302 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 303 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 304 | CLAMPCHECK(value, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
| 305 | ZSTD_cLevelToCParams(cctx); |
| 306 | cctx->requestedParams.cParams.searchLength = value; |
| 307 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 308 | |
| 309 | case ZSTD_p_targetLength : |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 310 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 311 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 312 | CLAMPCHECK(value, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
| 313 | ZSTD_cLevelToCParams(cctx); |
| 314 | cctx->requestedParams.cParams.targetLength = value; |
| 315 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 316 | |
| 317 | case ZSTD_p_compressionStrategy : |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 318 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 319 | if (cctx->cdict) return ERROR(stage_wrong); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 320 | CLAMPCHECK(value, (unsigned)ZSTD_fast, (unsigned)ZSTD_btultra); |
| 321 | ZSTD_cLevelToCParams(cctx); |
| 322 | cctx->requestedParams.cParams.strategy = (ZSTD_strategy)value; |
| 323 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 324 | |
| 325 | #if 0 |
| 326 | case ZSTD_p_windowSize : /* 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 | #endif |
| 329 | |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 330 | case ZSTD_p_contentSizeFlag : |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 331 | DEBUGLOG(5, "set content size flag = %u", (value>0)); |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 332 | /* Content size written in frame header _when known_ (default:1) */ |
| 333 | cctx->requestedParams.fParams.contentSizeFlag = value>0; |
| 334 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 335 | |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 336 | case ZSTD_p_checksumFlag : |
| 337 | /* A 32-bits content checksum will be calculated and written at end of frame (default:0) */ |
| 338 | cctx->requestedParams.fParams.checksumFlag = value>0; |
| 339 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 340 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 341 | 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] | 342 | DEBUGLOG(5, "set dictIDFlag = %u", (value>0)); |
| 343 | cctx->requestedParams.fParams.noDictIDFlag = (value==0); |
| 344 | return 0; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 345 | |
| 346 | case ZSTD_p_refDictContent : /* to be done later */ |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 347 | return ERROR(compressionParameter_unsupported); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 348 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 349 | case ZSTD_p_dictMode : |
| 350 | /* restrict dictionary mode, to "rawContent" or "fullDict" only */ |
| 351 | ZSTD_STATIC_ASSERT((U32)ZSTD_dm_fullDict > (U32)ZSTD_dm_rawContent); |
| 352 | if (value > (unsigned)ZSTD_dm_fullDict) |
| 353 | return ERROR(compressionParameter_outOfBound); |
| 354 | cctx->dictMode = (ZSTD_dictMode_e)value; |
| 355 | return 0; |
| 356 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 357 | case ZSTD_p_forceMaxWindow : /* Force back-references to remain < windowSize, |
| 358 | * even when referencing into Dictionary content |
| 359 | * default : 0 when using a CDict, 1 when using a Prefix */ |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 360 | cctx->forceWindow = value>0; |
| 361 | cctx->loadedDictEnd = 0; |
| 362 | return 0; |
| 363 | |
| 364 | case ZSTD_p_nbThreads: |
| 365 | if (value==0) return 0; |
Yann Collet | 33873f0 | 2017-06-16 12:04:21 -0700 | [diff] [blame] | 366 | DEBUGLOG(5, " setting nbThreads : %u", value); |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 367 | #ifndef ZSTD_MULTITHREAD |
| 368 | if (value > 1) return ERROR(compressionParameter_unsupported); |
| 369 | #endif |
| 370 | if ((value>1) && (cctx->nbThreads != value)) { |
Yann Collet | 05ae4b2 | 2017-06-15 18:03:34 -0700 | [diff] [blame] | 371 | if (cctx->staticSize) /* MT not compatible with static alloc */ |
| 372 | return ERROR(compressionParameter_unsupported); |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 373 | ZSTDMT_freeCCtx(cctx->mtctx); |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 374 | cctx->nbThreads = 1; |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 375 | cctx->mtctx = ZSTDMT_createCCtx(value); |
| 376 | if (cctx->mtctx == NULL) return ERROR(memory_allocation); |
Yann Collet | 33873f0 | 2017-06-16 12:04:21 -0700 | [diff] [blame] | 377 | } |
| 378 | cctx->nbThreads = value; |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 379 | return 0; |
| 380 | |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 381 | case ZSTD_p_jobSize: |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 382 | if (cctx->nbThreads <= 1) return ERROR(compressionParameter_unsupported); |
| 383 | assert(cctx->mtctx != NULL); |
| 384 | return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_sectionSize, value); |
| 385 | |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 386 | case ZSTD_p_overlapSizeLog: |
Yann Collet | 33873f0 | 2017-06-16 12:04:21 -0700 | [diff] [blame] | 387 | DEBUGLOG(5, " setting overlap with nbThreads == %u", cctx->nbThreads); |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 388 | if (cctx->nbThreads <= 1) return ERROR(compressionParameter_unsupported); |
| 389 | assert(cctx->mtctx != NULL); |
| 390 | return ZSTDMT_setMTCtxParameter(cctx->mtctx, ZSTDMT_p_overlapSectionLog, value); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 391 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 392 | default: return ERROR(parameter_unknown); |
| 393 | } |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 394 | } |
| 395 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 396 | 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] | 397 | { |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 398 | DEBUGLOG(5, " setting pledgedSrcSize to %u", (U32)pledgedSrcSize); |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 399 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 400 | cctx->pledgedSrcSizePlusOne = pledgedSrcSize+1; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 401 | return 0; |
| 402 | } |
| 403 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 404 | ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) |
| 405 | { |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 406 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 407 | if (cctx->staticSize) return ERROR(memory_allocation); /* no malloc for static CCtx */ |
Yann Collet | d7a3bff | 2017-06-19 11:53:01 -0700 | [diff] [blame] | 408 | DEBUGLOG(5, "load dictionary of size %u", (U32)dictSize); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 409 | ZSTD_freeCDict(cctx->cdictLocal); /* in case one already exists */ |
| 410 | if (dict==NULL || dictSize==0) { /* no dictionary mode */ |
| 411 | cctx->cdictLocal = NULL; |
| 412 | cctx->cdict = NULL; |
| 413 | } else { |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 414 | ZSTD_compressionParameters const cParams = |
| 415 | cctx->compressionLevel == ZSTD_CLEVEL_CUSTOM ? |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 416 | cctx->requestedParams.cParams : |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 417 | ZSTD_getCParams(cctx->compressionLevel, 0, dictSize); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 418 | cctx->cdictLocal = ZSTD_createCDict_advanced( |
| 419 | dict, dictSize, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 420 | 0 /* byReference */, cctx->dictMode, |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 421 | cParams, cctx->customMem); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 422 | cctx->cdict = cctx->cdictLocal; |
| 423 | if (cctx->cdictLocal == NULL) |
| 424 | return ERROR(memory_allocation); |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 429 | /* Not ready yet ! */ |
Yann Collet | bd18c88 | 2017-06-16 10:17:50 -0700 | [diff] [blame] | 430 | 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] | 431 | { |
| 432 | (void)cctx; (void)prefix; (void)prefixSize; /* to be done later */ |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 433 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 434 | return ERROR(compressionParameter_unsupported); |
| 435 | } |
| 436 | |
Yann Collet | bd18c88 | 2017-06-16 10:17:50 -0700 | [diff] [blame] | 437 | size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 438 | { |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 439 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
| 440 | cctx->cdict = cdict; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 441 | return ERROR(compressionParameter_unsupported); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 442 | } |
| 443 | |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 444 | static void ZSTD_startNewCompression(ZSTD_CCtx* cctx) |
Yann Collet | bd18c88 | 2017-06-16 10:17:50 -0700 | [diff] [blame] | 445 | { |
| 446 | cctx->streamStage = zcss_init; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 447 | cctx->pledgedSrcSizePlusOne = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /*! ZSTD_CCtx_reset() : |
| 451 | * Also dumps dictionary */ |
| 452 | void ZSTD_CCtx_reset(ZSTD_CCtx* cctx) |
| 453 | { |
| 454 | ZSTD_startNewCompression(cctx); |
Yann Collet | bd18c88 | 2017-06-16 10:17:50 -0700 | [diff] [blame] | 455 | cctx->cdict = NULL; |
| 456 | } |
| 457 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 458 | /** ZSTD_checkCParams() : |
| 459 | control CParam values remain within authorized range. |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 460 | @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] | 461 | size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams) |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 462 | { |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 463 | CLAMPCHECK(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 464 | CLAMPCHECK(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 465 | CLAMPCHECK(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 466 | CLAMPCHECK(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
Yann Collet | 2e2e78d | 2017-03-29 16:02:47 -0700 | [diff] [blame] | 467 | CLAMPCHECK(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 468 | CLAMPCHECK(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 469 | if ((U32)(cParams.strategy) > (U32)ZSTD_btultra) return ERROR(compressionParameter_unsupported); |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 470 | return 0; |
| 471 | } |
| 472 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 473 | /** ZSTD_clampCParams() : |
| 474 | * make CParam values within valid range. |
| 475 | * @return : valid CParams */ |
| 476 | static ZSTD_compressionParameters ZSTD_clampCParams(ZSTD_compressionParameters cParams) |
| 477 | { |
| 478 | # define CLAMP(val,min,max) { \ |
| 479 | if (val<min) val=min; \ |
| 480 | else if (val>max) val=max; \ |
| 481 | } |
| 482 | CLAMP(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
| 483 | CLAMP(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); |
| 484 | CLAMP(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 485 | CLAMP(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
| 486 | CLAMP(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
| 487 | CLAMP(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
| 488 | if ((U32)(cParams.strategy) > (U32)ZSTD_btultra) cParams.strategy = ZSTD_btultra; |
| 489 | return cParams; |
| 490 | } |
| 491 | |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 492 | /** ZSTD_cycleLog() : |
| 493 | * condition for correct operation : hashLog > 1 */ |
| 494 | static U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat) |
| 495 | { |
| 496 | U32 const btScale = ((U32)strat >= (U32)ZSTD_btlazy2); |
| 497 | return hashLog - btScale; |
| 498 | } |
| 499 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 500 | /** ZSTD_adjustCParams_internal() : |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 501 | optimize `cPar` for a given input (`srcSize` and `dictSize`). |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 502 | mostly downsizing to reduce memory consumption and initialization. |
| 503 | Both `srcSize` and `dictSize` are optional (use 0 if unknown), |
| 504 | but if both are 0, no optimization can be done. |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 505 | Note : cPar is considered validated at this stage. Use ZSTD_checkParams() to ensure that. */ |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 506 | ZSTD_compressionParameters ZSTD_adjustCParams_internal(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 507 | { |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 508 | assert(ZSTD_checkCParams(cPar)==0); |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 509 | if (srcSize+dictSize == 0) return cPar; /* no size information available : no adjustment */ |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 510 | |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 511 | /* resize params, to use less memory when necessary */ |
Yann Collet | dd6466a | 2016-03-30 20:06:26 +0200 | [diff] [blame] | 512 | { U32 const minSrcSize = (srcSize==0) ? 500 : 0; |
| 513 | U64 const rSize = srcSize + dictSize + minSrcSize; |
Yann Collet | b59bf96 | 2016-04-04 14:53:16 +0200 | [diff] [blame] | 514 | if (rSize < ((U64)1<<ZSTD_WINDOWLOG_MAX)) { |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 515 | 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] | 516 | if (cPar.windowLog > srcLog) cPar.windowLog = srcLog; |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 517 | } } |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 518 | if (cPar.hashLog > cPar.windowLog) cPar.hashLog = cPar.windowLog; |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 519 | { U32 const cycleLog = ZSTD_cycleLog(cPar.chainLog, cPar.strategy); |
| 520 | if (cycleLog > cPar.windowLog) cPar.chainLog -= (cycleLog - cPar.windowLog); |
| 521 | } |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 522 | |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 523 | 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] | 524 | |
| 525 | return cPar; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 526 | } |
| 527 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 528 | ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize) |
| 529 | { |
| 530 | cPar = ZSTD_clampCParams(cPar); |
| 531 | return ZSTD_adjustCParams_internal(cPar, srcSize, dictSize); |
| 532 | } |
| 533 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 534 | |
Yann Collet | 8847238 | 2016-07-14 17:05:38 +0200 | [diff] [blame] | 535 | size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams) |
Yann Collet | e74215e | 2016-03-19 16:09:09 +0100 | [diff] [blame] | 536 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 537 | 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] | 538 | U32 const divider = (cParams.searchLength==3) ? 3 : 4; |
| 539 | size_t const maxNbSeq = blockSize / divider; |
| 540 | size_t const tokenSpace = blockSize + 11*maxNbSeq; |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 541 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 542 | size_t const chainSize = (cParams.strategy == ZSTD_fast) ? 0 : (1 << cParams.chainLog); |
| 543 | size_t const hSize = ((size_t)1) << cParams.hashLog; |
| 544 | U32 const hashLog3 = (cParams.searchLength>3) ? 0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog); |
| 545 | size_t const h3Size = ((size_t)1) << hashLog3; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 546 | size_t const entropySpace = hufCTable_size + litlengthCTable_size |
Yann Collet | a408645 | 2017-04-20 23:09:39 -0700 | [diff] [blame] | 547 | + offcodeCTable_size + matchlengthCTable_size |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 548 | + entropyScratchSpace_size; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 549 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 550 | |
Yann Collet | fc51459 | 2017-05-08 17:07:59 -0700 | [diff] [blame] | 551 | 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] | 552 | + (ZSTD_OPT_NUM+1)*(sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t)); |
Nick Terrell | 5f2c721 | 2017-05-10 16:49:58 -0700 | [diff] [blame] | 553 | 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] | 554 | size_t const neededSpace = entropySpace + tableSpace + tokenSpace + optSpace; |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 555 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 556 | DEBUGLOG(5, "sizeof(ZSTD_CCtx) : %u", (U32)sizeof(ZSTD_CCtx)); |
| 557 | DEBUGLOG(5, "estimate workSpace : %u", (U32)neededSpace); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 558 | return sizeof(ZSTD_CCtx) + neededSpace; |
Yann Collet | 2e91dde | 2016-03-08 12:22:11 +0100 | [diff] [blame] | 559 | } |
| 560 | |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 561 | size_t ZSTD_estimateCStreamSize(ZSTD_compressionParameters cParams) |
| 562 | { |
| 563 | size_t const CCtxSize = ZSTD_estimateCCtxSize(cParams); |
| 564 | size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog); |
| 565 | size_t const inBuffSize = ((size_t)1 << cParams.windowLog) + blockSize; |
| 566 | size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1; |
| 567 | size_t const streamingSize = inBuffSize + outBuffSize; |
| 568 | |
| 569 | return CCtxSize + streamingSize; |
| 570 | } |
| 571 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 572 | |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 573 | static U32 ZSTD_equivalentParams(ZSTD_compressionParameters cParams1, |
| 574 | ZSTD_compressionParameters cParams2) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 575 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 576 | U32 bslog1 = MIN(cParams1.windowLog, ZSTD_BLOCKSIZELOG_MAX); |
| 577 | U32 bslog2 = MIN(cParams2.windowLog, ZSTD_BLOCKSIZELOG_MAX); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 578 | return (bslog1 == bslog2) /* same block size */ |
| 579 | & (cParams1.hashLog == cParams2.hashLog) |
| 580 | & (cParams1.chainLog == cParams2.chainLog) |
| 581 | & (cParams1.strategy == cParams2.strategy) /* opt parser space */ |
| 582 | & ((cParams1.searchLength==3) == (cParams2.searchLength==3)); /* hashlog3 space */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /*! ZSTD_continueCCtx() : |
Yann Collet | c08e649 | 2017-06-19 18:25:35 -0700 | [diff] [blame] | 586 | * reuse CCtx without reset (note : requires no dictionary) */ |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 587 | static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_parameters params, U64 pledgedSrcSize) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 588 | { |
| 589 | U32 const end = (U32)(cctx->nextSrc - cctx->base); |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 590 | DEBUGLOG(5, "continue mode"); |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 591 | cctx->appliedParams = params; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 592 | cctx->pledgedSrcSizePlusOne = pledgedSrcSize+1; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 593 | cctx->consumedSrcSize = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 594 | if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 595 | cctx->appliedParams.fParams.contentSizeFlag = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 596 | DEBUGLOG(5, "pledged content size : %u ; flag : %u", |
| 597 | (U32)pledgedSrcSize, cctx->appliedParams.fParams.contentSizeFlag); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 598 | cctx->lowLimit = end; |
| 599 | cctx->dictLimit = end; |
| 600 | cctx->nextToUpdate = end+1; |
| 601 | cctx->stage = ZSTDcs_init; |
| 602 | cctx->dictID = 0; |
| 603 | cctx->loadedDictEnd = 0; |
| 604 | { 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] | 605 | cctx->seqStore.litLengthSum = 0; /* force reset of btopt stats */ |
| 606 | XXH64_reset(&cctx->xxhState, 0); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 607 | return 0; |
| 608 | } |
| 609 | |
Yann Collet | b0739bc | 2017-05-22 17:45:15 -0700 | [diff] [blame] | 610 | typedef enum { ZSTDcrp_continue, ZSTDcrp_noMemset } ZSTD_compResetPolicy_e; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 611 | |
Yann Collet | 30fb499 | 2017-04-18 14:08:50 -0700 | [diff] [blame] | 612 | /*! ZSTD_resetCCtx_internal() : |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 613 | note : `params` are assumed fully validated at this stage */ |
| 614 | static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc, |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 615 | ZSTD_parameters params, U64 pledgedSrcSize, |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 616 | ZSTD_compResetPolicy_e const crp, |
| 617 | ZSTD_buffered_policy_e const zbuff) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 618 | { |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 619 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 620 | |
Yann Collet | b0739bc | 2017-05-22 17:45:15 -0700 | [diff] [blame] | 621 | if (crp == ZSTDcrp_continue) { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 622 | if (ZSTD_equivalentParams(params.cParams, zc->appliedParams.cParams)) { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 623 | DEBUGLOG(5, "ZSTD_equivalentParams()==1"); |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 624 | zc->fseCTables_ready = 0; |
| 625 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 626 | return ZSTD_continueCCtx(zc, params, pledgedSrcSize); |
Yann Collet | b0739bc | 2017-05-22 17:45:15 -0700 | [diff] [blame] | 627 | } } |
inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 628 | |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 629 | { 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] | 630 | U32 const divider = (params.cParams.searchLength==3) ? 3 : 4; |
| 631 | size_t const maxNbSeq = blockSize / divider; |
| 632 | size_t const tokenSpace = blockSize + 11*maxNbSeq; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 633 | size_t const chainSize = (params.cParams.strategy == ZSTD_fast) ? |
| 634 | 0 : (1 << params.cParams.chainLog); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 635 | size_t const hSize = ((size_t)1) << params.cParams.hashLog; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 636 | U32 const hashLog3 = (params.cParams.searchLength>3) ? |
| 637 | 0 : MIN(ZSTD_HASHLOG3_MAX, params.cParams.windowLog); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 638 | size_t const h3Size = ((size_t)1) << hashLog3; |
| 639 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 640 | size_t const buffOutSize = (zbuff==ZSTDb_buffered) ? ZSTD_compressBound(blockSize)+1 : 0; |
| 641 | size_t const buffInSize = (zbuff==ZSTDb_buffered) ? ((size_t)1 << params.cParams.windowLog) + blockSize : 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 642 | void* ptr; |
Yann Collet | e74215e | 2016-03-19 16:09:09 +0100 | [diff] [blame] | 643 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 644 | /* Check if workSpace is large enough, alloc a new one if needed */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 645 | { size_t const entropySpace = hufCTable_size + litlengthCTable_size |
Yann Collet | a408645 | 2017-04-20 23:09:39 -0700 | [diff] [blame] | 646 | + offcodeCTable_size + matchlengthCTable_size |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 647 | + entropyScratchSpace_size; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 648 | 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] | 649 | + (ZSTD_OPT_NUM+1) * (sizeof(ZSTD_match_t)+sizeof(ZSTD_optimal_t)); |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 650 | size_t const optSpace = ( (params.cParams.strategy == ZSTD_btopt) |
| 651 | || (params.cParams.strategy == ZSTD_btultra)) ? |
| 652 | optPotentialSpace : 0; |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 653 | size_t const bufferSpace = buffInSize + buffOutSize; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 654 | size_t const neededSpace = entropySpace + optSpace + tableSpace |
| 655 | + tokenSpace + bufferSpace; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 656 | |
| 657 | if (zc->workSpaceSize < neededSpace) { /* too small : resize /*/ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 658 | DEBUGLOG(5, "Need to update workSpaceSize from %uK to %uK \n", |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 659 | (unsigned)zc->workSpaceSize>>10, |
| 660 | (unsigned)neededSpace>>10); |
| 661 | /* static cctx : no resize, error out */ |
| 662 | if (zc->staticSize) return ERROR(memory_allocation); |
| 663 | |
Yann Collet | 0181fef | 2017-04-06 01:25:26 -0700 | [diff] [blame] | 664 | zc->workSpaceSize = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 665 | ZSTD_free(zc->workSpace, zc->customMem); |
| 666 | zc->workSpace = ZSTD_malloc(neededSpace, zc->customMem); |
| 667 | if (zc->workSpace == NULL) return ERROR(memory_allocation); |
| 668 | zc->workSpaceSize = neededSpace; |
Yann Collet | 7bb60b1 | 2017-04-20 17:38:56 -0700 | [diff] [blame] | 669 | ptr = zc->workSpace; |
| 670 | |
| 671 | /* entropy space */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 672 | zc->hufCTable = (HUF_CElt*)ptr; |
| 673 | 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] | 674 | zc->offcodeCTable = (FSE_CTable*) ptr; |
| 675 | ptr = (char*)ptr + offcodeCTable_size; |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 676 | zc->matchlengthCTable = (FSE_CTable*) ptr; |
Yann Collet | 71aaa32 | 2017-04-20 23:03:38 -0700 | [diff] [blame] | 677 | ptr = (char*)ptr + matchlengthCTable_size; |
Yann Collet | 7271203 | 2017-04-20 23:21:19 -0700 | [diff] [blame] | 678 | zc->litlengthCTable = (FSE_CTable*) ptr; |
| 679 | ptr = (char*)ptr + litlengthCTable_size; |
| 680 | assert(((size_t)ptr & 3) == 0); /* ensure correct alignment */ |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 681 | zc->entropyScratchSpace = (unsigned*) ptr; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 682 | } } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 683 | |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 684 | /* init params */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 685 | zc->appliedParams = params; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 686 | zc->pledgedSrcSizePlusOne = pledgedSrcSize+1; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 687 | zc->consumedSrcSize = 0; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 688 | if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) |
Yann Collet | cc9f9b7 | 2017-06-15 18:17:10 -0700 | [diff] [blame] | 689 | zc->appliedParams.fParams.contentSizeFlag = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 690 | DEBUGLOG(5, "pledged content size : %u ; flag : %u", |
| 691 | (U32)pledgedSrcSize, zc->appliedParams.fParams.contentSizeFlag); |
Yann Collet | cc9f9b7 | 2017-06-15 18:17:10 -0700 | [diff] [blame] | 692 | zc->blockSize = blockSize; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 693 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 694 | XXH64_reset(&zc->xxhState, 0); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 695 | zc->stage = ZSTDcs_init; |
| 696 | zc->dictID = 0; |
| 697 | zc->loadedDictEnd = 0; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 698 | zc->fseCTables_ready = 0; |
| 699 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 700 | zc->nextToUpdate = 1; |
| 701 | zc->nextSrc = NULL; |
| 702 | zc->base = NULL; |
| 703 | zc->dictBase = NULL; |
| 704 | zc->dictLimit = 0; |
| 705 | zc->lowLimit = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 706 | { 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] | 707 | zc->hashLog3 = hashLog3; |
| 708 | zc->seqStore.litLengthSum = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 709 | |
Yann Collet | 71aaa32 | 2017-04-20 23:03:38 -0700 | [diff] [blame] | 710 | /* ensure entropy tables are close together at the beginning */ |
| 711 | assert((void*)zc->hufCTable == zc->workSpace); |
| 712 | assert((char*)zc->offcodeCTable == (char*)zc->hufCTable + hufCTable_size); |
| 713 | assert((char*)zc->matchlengthCTable == (char*)zc->offcodeCTable + offcodeCTable_size); |
| 714 | assert((char*)zc->litlengthCTable == (char*)zc->matchlengthCTable + matchlengthCTable_size); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 715 | assert((char*)zc->entropyScratchSpace == (char*)zc->litlengthCTable + litlengthCTable_size); |
| 716 | ptr = (char*)zc->entropyScratchSpace + entropyScratchSpace_size; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 717 | |
| 718 | /* opt parser space */ |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 719 | if ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btultra)) { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 720 | DEBUGLOG(5, "reserving optimal parser space"); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 721 | assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 722 | zc->seqStore.litFreq = (U32*)ptr; |
| 723 | zc->seqStore.litLengthFreq = zc->seqStore.litFreq + (1<<Litbits); |
| 724 | zc->seqStore.matchLengthFreq = zc->seqStore.litLengthFreq + (MaxLL+1); |
| 725 | zc->seqStore.offCodeFreq = zc->seqStore.matchLengthFreq + (MaxML+1); |
| 726 | ptr = zc->seqStore.offCodeFreq + (MaxOff+1); |
| 727 | zc->seqStore.matchTable = (ZSTD_match_t*)ptr; |
| 728 | ptr = zc->seqStore.matchTable + ZSTD_OPT_NUM+1; |
| 729 | zc->seqStore.priceTable = (ZSTD_optimal_t*)ptr; |
| 730 | ptr = zc->seqStore.priceTable + ZSTD_OPT_NUM+1; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 731 | } |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 732 | |
| 733 | /* table Space */ |
| 734 | if (crp!=ZSTDcrp_noMemset) memset(ptr, 0, tableSpace); /* reset tables only */ |
| 735 | assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ |
| 736 | zc->hashTable = (U32*)(ptr); |
| 737 | zc->chainTable = zc->hashTable + hSize; |
| 738 | zc->hashTable3 = zc->chainTable + chainSize; |
| 739 | ptr = zc->hashTable3 + h3Size; |
| 740 | |
| 741 | /* sequences storage */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 742 | zc->seqStore.sequencesStart = (seqDef*)ptr; |
| 743 | ptr = zc->seqStore.sequencesStart + maxNbSeq; |
| 744 | zc->seqStore.llCode = (BYTE*) ptr; |
| 745 | zc->seqStore.mlCode = zc->seqStore.llCode + maxNbSeq; |
| 746 | zc->seqStore.ofCode = zc->seqStore.mlCode + maxNbSeq; |
| 747 | zc->seqStore.litStart = zc->seqStore.ofCode + maxNbSeq; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 748 | ptr = zc->seqStore.litStart + blockSize; |
| 749 | |
| 750 | /* buffers */ |
| 751 | zc->inBuffSize = buffInSize; |
| 752 | zc->inBuff = (char*)ptr; |
| 753 | zc->outBuffSize = buffOutSize; |
| 754 | zc->outBuff = zc->inBuff + buffInSize; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 755 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 756 | return 0; |
Yann Collet | 72d706a | 2016-03-23 20:44:12 +0100 | [diff] [blame] | 757 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 758 | } |
| 759 | |
Yann Collet | 32dfae6 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 760 | /* ZSTD_invalidateRepCodes() : |
| 761 | * ensures next compression will not use repcodes from previous block. |
| 762 | * Note : only works with regular variant; |
| 763 | * do not use with extDict variant ! */ |
| 764 | void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx) { |
| 765 | int i; |
| 766 | for (i=0; i<ZSTD_REP_NUM; i++) cctx->rep[i] = 0; |
| 767 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 768 | |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 769 | |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 770 | /*! ZSTD_copyCCtx_internal() : |
| 771 | * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. |
| 772 | * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). |
| 773 | * pledgedSrcSize=0 means "empty" if fParams.contentSizeFlag=1 |
| 774 | * @return : 0, or an error code */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 775 | static size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx, |
| 776 | const ZSTD_CCtx* srcCCtx, |
| 777 | ZSTD_frameParameters fParams, |
| 778 | unsigned long long pledgedSrcSize) |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 779 | { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 780 | DEBUGLOG(5, "ZSTD_copyCCtx_internal"); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 781 | if (srcCCtx->stage!=ZSTDcs_init) return ERROR(stage_wrong); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 782 | |
inikep | 2866951 | 2016-06-02 13:04:18 +0200 | [diff] [blame] | 783 | memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem)); |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 784 | { ZSTD_buffered_policy_e const zbuff = srcCCtx->inBuffSize ? |
| 785 | ZSTDb_buffered : ZSTDb_not_buffered; |
| 786 | ZSTD_parameters params = srcCCtx->appliedParams; |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 787 | params.fParams = fParams; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 788 | ZSTD_resetCCtx_internal(dstCCtx, params, pledgedSrcSize, |
| 789 | ZSTDcrp_noMemset, zbuff); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 790 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 791 | |
| 792 | /* copy tables */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 793 | { size_t const chainSize = (srcCCtx->appliedParams.cParams.strategy == ZSTD_fast) ? 0 : (1 << srcCCtx->appliedParams.cParams.chainLog); |
| 794 | size_t const hSize = (size_t)1 << srcCCtx->appliedParams.cParams.hashLog; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 795 | size_t const h3Size = (size_t)1 << srcCCtx->hashLog3; |
| 796 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 797 | assert((U32*)dstCCtx->chainTable == (U32*)dstCCtx->hashTable + hSize); /* chainTable must follow hashTable */ |
| 798 | assert((U32*)dstCCtx->hashTable3 == (U32*)dstCCtx->chainTable + chainSize); |
| 799 | memcpy(dstCCtx->hashTable, srcCCtx->hashTable, tableSpace); /* presumes all tables follow each other */ |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 800 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 801 | |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 802 | /* copy dictionary offsets */ |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 803 | dstCCtx->nextToUpdate = srcCCtx->nextToUpdate; |
| 804 | dstCCtx->nextToUpdate3= srcCCtx->nextToUpdate3; |
| 805 | dstCCtx->nextSrc = srcCCtx->nextSrc; |
| 806 | dstCCtx->base = srcCCtx->base; |
| 807 | dstCCtx->dictBase = srcCCtx->dictBase; |
| 808 | dstCCtx->dictLimit = srcCCtx->dictLimit; |
| 809 | dstCCtx->lowLimit = srcCCtx->lowLimit; |
| 810 | dstCCtx->loadedDictEnd= srcCCtx->loadedDictEnd; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 811 | dstCCtx->dictID = srcCCtx->dictID; |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 812 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 813 | /* copy entropy tables */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 814 | dstCCtx->fseCTables_ready = srcCCtx->fseCTables_ready; |
| 815 | if (srcCCtx->fseCTables_ready) { |
Yann Collet | a34a39c | 2017-04-20 18:17:58 -0700 | [diff] [blame] | 816 | memcpy(dstCCtx->litlengthCTable, srcCCtx->litlengthCTable, litlengthCTable_size); |
| 817 | memcpy(dstCCtx->matchlengthCTable, srcCCtx->matchlengthCTable, matchlengthCTable_size); |
| 818 | memcpy(dstCCtx->offcodeCTable, srcCCtx->offcodeCTable, offcodeCTable_size); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 819 | } |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 820 | dstCCtx->hufCTable_repeatMode = srcCCtx->hufCTable_repeatMode; |
| 821 | if (srcCCtx->hufCTable_repeatMode) { |
| 822 | memcpy(dstCCtx->hufCTable, srcCCtx->hufCTable, hufCTable_size); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 823 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 824 | |
| 825 | return 0; |
| 826 | } |
| 827 | |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 828 | /*! ZSTD_copyCCtx() : |
| 829 | * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. |
| 830 | * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). |
| 831 | * pledgedSrcSize==0 means "unknown". |
| 832 | * @return : 0, or an error code */ |
| 833 | size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx, unsigned long long pledgedSrcSize) |
| 834 | { |
| 835 | ZSTD_frameParameters fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
| 836 | fParams.contentSizeFlag = pledgedSrcSize>0; |
| 837 | |
| 838 | return ZSTD_copyCCtx_internal(dstCCtx, srcCCtx, fParams, pledgedSrcSize); |
| 839 | } |
| 840 | |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 841 | |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 842 | /*! ZSTD_reduceTable() : |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 843 | * reduce table indexes by `reducerValue` */ |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 844 | 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] | 845 | { |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 846 | U32 u; |
| 847 | for (u=0 ; u < size ; u++) { |
| 848 | if (table[u] < reducerValue) table[u] = 0; |
| 849 | else table[u] -= reducerValue; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 850 | } |
| 851 | } |
| 852 | |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 853 | /*! ZSTD_reduceIndex() : |
| 854 | * rescale all indexes to avoid future overflow (indexes are U32) */ |
| 855 | static void ZSTD_reduceIndex (ZSTD_CCtx* zc, const U32 reducerValue) |
| 856 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 857 | { U32 const hSize = 1 << zc->appliedParams.cParams.hashLog; |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 858 | ZSTD_reduceTable(zc->hashTable, hSize, reducerValue); } |
| 859 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 860 | { 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] | 861 | ZSTD_reduceTable(zc->chainTable, chainSize, reducerValue); } |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 862 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 863 | { U32 const h3Size = (zc->hashLog3) ? 1 << zc->hashLog3 : 0; |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 864 | ZSTD_reduceTable(zc->hashTable3, h3Size, reducerValue); } |
| 865 | } |
| 866 | |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 867 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 868 | /*-******************************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 869 | * Block entropic compression |
| 870 | *********************************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 871 | |
Przemyslaw Skibinski | 3ee94a7 | 2016-10-24 15:58:07 +0200 | [diff] [blame] | 872 | /* See doc/zstd_compression_format.md for detailed format description */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 873 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 874 | 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] | 875 | { |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 876 | if (srcSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 877 | memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize); |
| 878 | MEM_writeLE24(dst, (U32)(srcSize << 2) + (U32)bt_raw); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 879 | return ZSTD_blockHeaderSize+srcSize; |
| 880 | } |
| 881 | |
| 882 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 883 | 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] | 884 | { |
| 885 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 886 | U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 887 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 888 | if (srcSize + flSize > dstCapacity) return ERROR(dstSize_tooSmall); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 889 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 890 | switch(flSize) |
| 891 | { |
| 892 | case 1: /* 2 - 1 - 5 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 893 | ostart[0] = (BYTE)((U32)set_basic + (srcSize<<3)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 894 | break; |
| 895 | case 2: /* 2 - 2 - 12 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 896 | MEM_writeLE16(ostart, (U16)((U32)set_basic + (1<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 897 | break; |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 898 | case 3: /* 2 - 2 - 20 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 899 | MEM_writeLE32(ostart, (U32)((U32)set_basic + (3<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 900 | break; |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 901 | default: /* not necessary : flSize is {1,2,3} */ |
| 902 | assert(0); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | memcpy(ostart + flSize, src, srcSize); |
| 906 | return srcSize + flSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 907 | } |
| 908 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 909 | 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] | 910 | { |
| 911 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 912 | U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 913 | |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 914 | (void)dstCapacity; /* dstCapacity already guaranteed to be >=4, hence large enough */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 915 | |
| 916 | switch(flSize) |
| 917 | { |
| 918 | case 1: /* 2 - 1 - 5 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 919 | ostart[0] = (BYTE)((U32)set_rle + (srcSize<<3)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 920 | break; |
| 921 | case 2: /* 2 - 2 - 12 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 922 | MEM_writeLE16(ostart, (U16)((U32)set_rle + (1<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 923 | break; |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 924 | case 3: /* 2 - 2 - 20 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 925 | MEM_writeLE32(ostart, (U32)((U32)set_rle + (3<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 926 | break; |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 927 | default: /* not necessary : flSize is {1,2,3} */ |
| 928 | assert(0); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | ostart[flSize] = *(const BYTE*)src; |
| 932 | return flSize+1; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 935 | |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 936 | static size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 2; } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 937 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 938 | static size_t ZSTD_compressLiterals (ZSTD_CCtx* zc, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 939 | void* dst, size_t dstCapacity, |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 940 | const void* src, size_t srcSize) |
| 941 | { |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 942 | size_t const minGain = ZSTD_minGain(srcSize); |
| 943 | size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 944 | BYTE* const ostart = (BYTE*)dst; |
Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 945 | U32 singleStream = srcSize < 256; |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 946 | symbolEncodingType_e hType = set_compressed; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 947 | size_t cLitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 948 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 949 | |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 950 | /* small ? don't even attempt compression (speed opt) */ |
| 951 | # define LITERAL_NOENTROPY 63 |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 952 | { 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] | 953 | if (srcSize <= minLitSize) return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); |
| 954 | } |
| 955 | |
| 956 | 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] | 957 | { HUF_repeat repeat = zc->hufCTable_repeatMode; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 958 | int const preferRepeat = zc->appliedParams.cParams.strategy < ZSTD_lazy ? srcSize <= 1024 : 0; |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 959 | if (repeat == HUF_repeat_valid && lhSize == 3) singleStream = 1; |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 960 | 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] | 961 | zc->entropyScratchSpace, entropyScratchSpace_size, zc->hufCTable, &repeat, preferRepeat) |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 962 | : HUF_compress4X_repeat(ostart+lhSize, dstCapacity-lhSize, src, srcSize, 255, 11, |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 963 | zc->entropyScratchSpace, entropyScratchSpace_size, zc->hufCTable, &repeat, preferRepeat); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 964 | if (repeat != HUF_repeat_none) { hType = set_repeat; } /* reused the existing table */ |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 965 | 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] | 966 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 967 | |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 968 | if ((cLitSize==0) | (cLitSize >= srcSize - minGain)) { |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 969 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 970 | return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 971 | } |
| 972 | if (cLitSize==1) { |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 973 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 974 | return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 975 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 976 | |
| 977 | /* Build header */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 978 | switch(lhSize) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 979 | { |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 980 | case 3: /* 2 - 2 - 10 - 10 */ |
Yann Collet | c2e1a68 | 2016-07-22 17:30:52 +0200 | [diff] [blame] | 981 | { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<14); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 982 | MEM_writeLE24(ostart, lhc); |
| 983 | break; |
| 984 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 985 | case 4: /* 2 - 2 - 14 - 14 */ |
Yann Collet | 32faf6c | 2016-07-22 04:45:06 +0200 | [diff] [blame] | 986 | { U32 const lhc = hType + (2 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<18); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 987 | MEM_writeLE32(ostart, lhc); |
| 988 | break; |
| 989 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 990 | case 5: /* 2 - 2 - 18 - 18 */ |
Yann Collet | 32faf6c | 2016-07-22 04:45:06 +0200 | [diff] [blame] | 991 | { U32 const lhc = hType + (3 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<22); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 992 | MEM_writeLE32(ostart, lhc); |
| 993 | ostart[4] = (BYTE)(cLitSize >> 10); |
| 994 | break; |
| 995 | } |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 996 | default: /* not possible : lhSize is {3,4,5} */ |
| 997 | assert(0); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 998 | } |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 999 | return lhSize+cLitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1000 | } |
| 1001 | |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 1002 | static const BYTE LL_Code[64] = { 0, 1, 2, 3, 4, 5, 6, 7, |
| 1003 | 8, 9, 10, 11, 12, 13, 14, 15, |
| 1004 | 16, 16, 17, 17, 18, 18, 19, 19, |
| 1005 | 20, 20, 20, 20, 21, 21, 21, 21, |
| 1006 | 22, 22, 22, 22, 22, 22, 22, 22, |
| 1007 | 23, 23, 23, 23, 23, 23, 23, 23, |
| 1008 | 24, 24, 24, 24, 24, 24, 24, 24, |
| 1009 | 24, 24, 24, 24, 24, 24, 24, 24 }; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1010 | |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 1011 | static const BYTE ML_Code[128] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, |
| 1012 | 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, |
| 1013 | 32, 32, 33, 33, 34, 34, 35, 35, 36, 36, 36, 36, 37, 37, 37, 37, |
| 1014 | 38, 38, 38, 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, 39, 39, 39, |
| 1015 | 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, |
| 1016 | 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, 41, |
| 1017 | 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, 42, |
| 1018 | 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] | 1019 | |
| 1020 | |
| 1021 | void ZSTD_seqToCodes(const seqStore_t* seqStorePtr) |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 1022 | { |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1023 | BYTE const LL_deltaCode = 19; |
| 1024 | BYTE const ML_deltaCode = 36; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1025 | const seqDef* const sequences = seqStorePtr->sequencesStart; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1026 | BYTE* const llCodeTable = seqStorePtr->llCode; |
| 1027 | BYTE* const ofCodeTable = seqStorePtr->ofCode; |
| 1028 | BYTE* const mlCodeTable = seqStorePtr->mlCode; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1029 | U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1030 | U32 u; |
| 1031 | for (u=0; u<nbSeq; u++) { |
| 1032 | U32 const llv = sequences[u].litLength; |
| 1033 | U32 const mlv = sequences[u].matchLength; |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 1034 | 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] | 1035 | ofCodeTable[u] = (BYTE)ZSTD_highbit32(sequences[u].offset); |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 1036 | 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] | 1037 | } |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1038 | if (seqStorePtr->longLengthID==1) |
| 1039 | llCodeTable[seqStorePtr->longLengthPos] = MaxLL; |
| 1040 | if (seqStorePtr->longLengthID==2) |
| 1041 | mlCodeTable[seqStorePtr->longLengthPos] = MaxML; |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 1042 | } |
| 1043 | |
Sean Purcell | 553f67e | 2017-03-02 15:15:31 -0800 | [diff] [blame] | 1044 | MEM_STATIC size_t ZSTD_compressSequences (ZSTD_CCtx* zc, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1045 | void* dst, size_t dstCapacity, |
Sean Purcell | 553f67e | 2017-03-02 15:15:31 -0800 | [diff] [blame] | 1046 | size_t srcSize) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1047 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1048 | const int longOffsets = zc->appliedParams.cParams.windowLog > STREAM_ACCUMULATOR_MIN; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1049 | const seqStore_t* seqStorePtr = &(zc->seqStore); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1050 | U32 count[MaxSeq+1]; |
| 1051 | S16 norm[MaxSeq+1]; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1052 | FSE_CTable* CTable_LitLength = zc->litlengthCTable; |
| 1053 | FSE_CTable* CTable_OffsetBits = zc->offcodeCTable; |
| 1054 | FSE_CTable* CTable_MatchLength = zc->matchlengthCTable; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1055 | U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */ |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1056 | const seqDef* const sequences = seqStorePtr->sequencesStart; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1057 | const BYTE* const ofCodeTable = seqStorePtr->ofCode; |
| 1058 | const BYTE* const llCodeTable = seqStorePtr->llCode; |
| 1059 | const BYTE* const mlCodeTable = seqStorePtr->mlCode; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1060 | BYTE* const ostart = (BYTE*)dst; |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1061 | BYTE* const oend = ostart + dstCapacity; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1062 | BYTE* op = ostart; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1063 | size_t const nbSeq = seqStorePtr->sequences - seqStorePtr->sequencesStart; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1064 | BYTE* seqHead; |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 1065 | BYTE scratchBuffer[1<<MAX(MLFSELog,LLFSELog)]; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1066 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1067 | /* Compress literals */ |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 1068 | { const BYTE* const literals = seqStorePtr->litStart; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1069 | size_t const litSize = seqStorePtr->lit - literals; |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 1070 | size_t const cSize = ZSTD_compressLiterals(zc, op, dstCapacity, literals, litSize); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1071 | if (ZSTD_isError(cSize)) return cSize; |
| 1072 | op += cSize; |
| 1073 | } |
| 1074 | |
| 1075 | /* Sequences Header */ |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 1076 | 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] | 1077 | if (nbSeq < 0x7F) *op++ = (BYTE)nbSeq; |
| 1078 | else if (nbSeq < LONGNBSEQ) op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2; |
| 1079 | 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] | 1080 | if (nbSeq==0) goto _check_compressibility; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1081 | |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 1082 | /* seqHead : flags for FSE encoding type */ |
| 1083 | seqHead = op++; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1084 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1085 | #define MIN_SEQ_FOR_DYNAMIC_FSE 64 |
| 1086 | #define MAX_SEQ_FOR_STATIC_FSE 1000 |
| 1087 | |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 1088 | /* convert length/distances into codes */ |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1089 | ZSTD_seqToCodes(seqStorePtr); |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 1090 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1091 | /* CTable for Literal Lengths */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1092 | { U32 max = MaxLL; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 1093 | 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] | 1094 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
| 1095 | *op++ = llCodeTable[0]; |
| 1096 | FSE_buildCTable_rle(CTable_LitLength, (BYTE)max); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1097 | LLtype = set_rle; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 1098 | } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1099 | LLtype = set_repeat; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1100 | } 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] | 1101 | 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] | 1102 | LLtype = set_basic; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1103 | } else { |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1104 | size_t nbSeq_1 = nbSeq; |
| 1105 | const U32 tableLog = FSE_optimalTableLog(LLFSELog, nbSeq, max); |
| 1106 | if (count[llCodeTable[nbSeq-1]]>1) { count[llCodeTable[nbSeq-1]]--; nbSeq_1--; } |
| 1107 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 1108 | { 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] | 1109 | if (FSE_isError(NCountSize)) return NCountSize; |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 1110 | op += NCountSize; } |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 1111 | FSE_buildCTable_wksp(CTable_LitLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1112 | LLtype = set_compressed; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1113 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1114 | |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 1115 | /* CTable for Offsets */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1116 | { U32 max = MaxOff; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 1117 | 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] | 1118 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 1119 | *op++ = ofCodeTable[0]; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1120 | FSE_buildCTable_rle(CTable_OffsetBits, (BYTE)max); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1121 | Offtype = set_rle; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 1122 | } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1123 | Offtype = set_repeat; |
Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 1124 | } 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] | 1125 | 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] | 1126 | Offtype = set_basic; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1127 | } else { |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1128 | size_t nbSeq_1 = nbSeq; |
| 1129 | const U32 tableLog = FSE_optimalTableLog(OffFSELog, nbSeq, max); |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 1130 | if (count[ofCodeTable[nbSeq-1]]>1) { count[ofCodeTable[nbSeq-1]]--; nbSeq_1--; } |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1131 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 1132 | { 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] | 1133 | if (FSE_isError(NCountSize)) return NCountSize; |
Yann Collet | add08d6 | 2016-03-23 01:32:41 +0100 | [diff] [blame] | 1134 | op += NCountSize; } |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 1135 | FSE_buildCTable_wksp(CTable_OffsetBits, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1136 | Offtype = set_compressed; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1137 | } } |
| 1138 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1139 | /* CTable for MatchLengths */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1140 | { U32 max = MaxML; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 1141 | 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] | 1142 | if ((mostFrequent == nbSeq) && (nbSeq > 2)) { |
Yann Collet | 72d706a | 2016-03-23 20:44:12 +0100 | [diff] [blame] | 1143 | *op++ = *mlCodeTable; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1144 | FSE_buildCTable_rle(CTable_MatchLength, (BYTE)max); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1145 | MLtype = set_rle; |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 1146 | } else if ((zc->fseCTables_ready) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1147 | MLtype = set_repeat; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1148 | } 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] | 1149 | 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] | 1150 | MLtype = set_basic; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1151 | } else { |
| 1152 | size_t nbSeq_1 = nbSeq; |
| 1153 | const U32 tableLog = FSE_optimalTableLog(MLFSELog, nbSeq, max); |
| 1154 | if (count[mlCodeTable[nbSeq-1]]>1) { count[mlCodeTable[nbSeq-1]]--; nbSeq_1--; } |
| 1155 | FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max); |
| 1156 | { 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] | 1157 | if (FSE_isError(NCountSize)) return NCountSize; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1158 | op += NCountSize; } |
Yann Collet | d79a9a0 | 2016-11-30 15:52:20 -0800 | [diff] [blame] | 1159 | FSE_buildCTable_wksp(CTable_MatchLength, norm, max, tableLog, scratchBuffer, sizeof(scratchBuffer)); |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1160 | MLtype = set_compressed; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1161 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1162 | |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 1163 | *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2)); |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 1164 | zc->fseCTables_ready = 0; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1165 | |
| 1166 | /* Encoding Sequences */ |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 1167 | { BIT_CStream_t blockStream; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1168 | FSE_CState_t stateMatchLength; |
| 1169 | FSE_CState_t stateOffsetBits; |
| 1170 | FSE_CState_t stateLitLength; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1171 | |
Yann Collet | 95d07d7 | 2016-09-06 16:38:51 +0200 | [diff] [blame] | 1172 | 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] | 1173 | |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 1174 | /* first symbols */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1175 | FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]); |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 1176 | FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]); |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 1177 | FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1178 | BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1179 | if (MEM_32bits()) BIT_flushBits(&blockStream); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1180 | BIT_addBits(&blockStream, sequences[nbSeq-1].matchLength, ML_bits[mlCodeTable[nbSeq-1]]); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1181 | if (MEM_32bits()) BIT_flushBits(&blockStream); |
Sean Purcell | d44703d | 2017-03-01 14:36:25 -0800 | [diff] [blame] | 1182 | if (longOffsets) { |
| 1183 | U32 const ofBits = ofCodeTable[nbSeq-1]; |
| 1184 | int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); |
| 1185 | if (extraBits) { |
| 1186 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset, extraBits); |
| 1187 | BIT_flushBits(&blockStream); |
| 1188 | } |
| 1189 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset >> extraBits, |
| 1190 | ofBits - extraBits); |
| 1191 | } else { |
| 1192 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset, ofCodeTable[nbSeq-1]); |
| 1193 | } |
Yann Collet | 597847a | 2016-03-20 19:14:22 +0100 | [diff] [blame] | 1194 | BIT_flushBits(&blockStream); |
| 1195 | |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1196 | { size_t n; |
| 1197 | for (n=nbSeq-2 ; n<nbSeq ; n--) { /* intentional underflow */ |
Yann Collet | 3c6b808 | 2016-07-30 03:20:47 +0200 | [diff] [blame] | 1198 | BYTE const llCode = llCodeTable[n]; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1199 | BYTE const ofCode = ofCodeTable[n]; |
| 1200 | BYTE const mlCode = mlCodeTable[n]; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1201 | U32 const llBits = LL_bits[llCode]; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1202 | U32 const ofBits = ofCode; /* 32b*/ /* 64b*/ |
Yann Collet | 3c6b808 | 2016-07-30 03:20:47 +0200 | [diff] [blame] | 1203 | U32 const mlBits = ML_bits[mlCode]; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1204 | /* (7)*/ /* (7)*/ |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1205 | FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */ |
| 1206 | FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */ |
| 1207 | if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ |
| 1208 | FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */ |
Yann Collet | 582933f | 2016-04-11 16:25:56 +0200 | [diff] [blame] | 1209 | if (MEM_32bits() || (ofBits+mlBits+llBits >= 64-7-(LLFSELog+MLFSELog+OffFSELog))) |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1210 | BIT_flushBits(&blockStream); /* (7)*/ |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1211 | BIT_addBits(&blockStream, sequences[n].litLength, llBits); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1212 | if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1213 | BIT_addBits(&blockStream, sequences[n].matchLength, mlBits); |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1214 | if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ |
Sean Purcell | d44703d | 2017-03-01 14:36:25 -0800 | [diff] [blame] | 1215 | if (longOffsets) { |
| 1216 | int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); |
| 1217 | if (extraBits) { |
| 1218 | BIT_addBits(&blockStream, sequences[n].offset, extraBits); |
| 1219 | BIT_flushBits(&blockStream); /* (7)*/ |
| 1220 | } |
| 1221 | BIT_addBits(&blockStream, sequences[n].offset >> extraBits, |
| 1222 | ofBits - extraBits); /* 31 */ |
| 1223 | } else { |
| 1224 | BIT_addBits(&blockStream, sequences[n].offset, ofBits); /* 31 */ |
| 1225 | } |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1226 | BIT_flushBits(&blockStream); /* (7)*/ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1227 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1228 | |
| 1229 | FSE_flushCState(&blockStream, &stateMatchLength); |
| 1230 | FSE_flushCState(&blockStream, &stateOffsetBits); |
| 1231 | FSE_flushCState(&blockStream, &stateLitLength); |
| 1232 | |
Yann Collet | b915140 | 2016-03-26 17:18:11 +0100 | [diff] [blame] | 1233 | { size_t const streamSize = BIT_closeCStream(&blockStream); |
| 1234 | if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */ |
| 1235 | op += streamSize; |
| 1236 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1237 | |
| 1238 | /* check compressibility */ |
Yann Collet | e93d6ce | 2016-01-31 00:58:06 +0100 | [diff] [blame] | 1239 | _check_compressibility: |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 1240 | { size_t const minGain = ZSTD_minGain(srcSize); |
| 1241 | size_t const maxCSize = srcSize - minGain; |
| 1242 | if ((size_t)(op-ostart) >= maxCSize) { |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 1243 | zc->hufCTable_repeatMode = HUF_repeat_none; |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 1244 | return 0; |
| 1245 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1246 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1247 | /* confirm repcodes */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1248 | { 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] | 1249 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1250 | return op - ostart; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1251 | } |
| 1252 | |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 1253 | |
Yann Collet | 95cd0c2 | 2016-03-08 18:24:21 +0100 | [diff] [blame] | 1254 | /*! ZSTD_storeSeq() : |
| 1255 | Store a sequence (literal length, literals, offset code and match length code) into seqStore_t. |
| 1256 | `offsetCode` : distance to match, or 0 == repCode. |
| 1257 | `matchCode` : matchLength - MINMATCH |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1258 | */ |
Yann Collet | d57dffb | 2016-07-03 01:48:26 +0200 | [diff] [blame] | 1259 | 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] | 1260 | { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 1261 | #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG >= 6) |
| 1262 | static const BYTE* g_start = NULL; |
| 1263 | U32 const pos = (U32)((const BYTE*)literals - g_start); |
| 1264 | if (g_start==NULL) g_start = (const BYTE*)literals; |
| 1265 | if ((pos > 0) && (pos < 1000000000)) |
| 1266 | DEBUGLOG(6, "Cpos %6u :%5u literals & match %3u bytes at distance %6u", |
| 1267 | pos, (U32)litLength, (U32)matchCode+MINMATCH, (U32)offsetCode); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1268 | #endif |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1269 | /* copy Literals */ |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 1270 | assert(seqStorePtr->lit + litLength <= seqStorePtr->litStart + 128 KB); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1271 | ZSTD_wildcopy(seqStorePtr->lit, literals, litLength); |
| 1272 | seqStorePtr->lit += litLength; |
| 1273 | |
| 1274 | /* literal Length */ |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1275 | if (litLength>0xFFFF) { |
| 1276 | seqStorePtr->longLengthID = 1; |
| 1277 | seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); |
| 1278 | } |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1279 | seqStorePtr->sequences[0].litLength = (U16)litLength; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1280 | |
| 1281 | /* match offset */ |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1282 | seqStorePtr->sequences[0].offset = offsetCode + 1; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1283 | |
| 1284 | /* match Length */ |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1285 | if (matchCode>0xFFFF) { |
| 1286 | seqStorePtr->longLengthID = 2; |
| 1287 | seqStorePtr->longLengthPos = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); |
| 1288 | } |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1289 | seqStorePtr->sequences[0].matchLength = (U16)matchCode; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1290 | |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1291 | seqStorePtr->sequences++; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 1295 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1296 | * Match length counter |
| 1297 | ***************************************/ |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1298 | static unsigned ZSTD_NbCommonBytes (register size_t val) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1299 | { |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1300 | if (MEM_isLittleEndian()) { |
| 1301 | if (MEM_64bits()) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1302 | # if defined(_MSC_VER) && defined(_WIN64) |
| 1303 | unsigned long r = 0; |
| 1304 | _BitScanForward64( &r, (U64)val ); |
Yann Collet | d608088 | 2015-12-09 09:05:22 +0100 | [diff] [blame] | 1305 | return (unsigned)(r>>3); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1306 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 1307 | return (__builtin_ctzll((U64)val) >> 3); |
| 1308 | # else |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 1309 | static const int DeBruijnBytePos[64] = { 0, 0, 0, 0, 0, 1, 1, 2, |
| 1310 | 0, 3, 1, 3, 1, 4, 2, 7, |
| 1311 | 0, 2, 3, 6, 1, 5, 3, 5, |
| 1312 | 1, 3, 4, 4, 2, 5, 6, 7, |
| 1313 | 7, 0, 1, 2, 3, 3, 4, 6, |
| 1314 | 2, 6, 5, 5, 3, 4, 5, 6, |
| 1315 | 7, 1, 2, 4, 6, 4, 4, 5, |
| 1316 | 7, 2, 6, 5, 7, 6, 7, 7 }; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1317 | return DeBruijnBytePos[((U64)((val & -(long long)val) * 0x0218A392CDABBD3FULL)) >> 58]; |
| 1318 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1319 | } else { /* 32 bits */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1320 | # if defined(_MSC_VER) |
| 1321 | unsigned long r=0; |
| 1322 | _BitScanForward( &r, (U32)val ); |
Yann Collet | d608088 | 2015-12-09 09:05:22 +0100 | [diff] [blame] | 1323 | return (unsigned)(r>>3); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1324 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 1325 | return (__builtin_ctz((U32)val) >> 3); |
| 1326 | # else |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 1327 | static const int DeBruijnBytePos[32] = { 0, 0, 3, 0, 3, 1, 3, 0, |
| 1328 | 3, 2, 2, 1, 3, 2, 0, 1, |
| 1329 | 3, 3, 1, 2, 2, 2, 2, 0, |
| 1330 | 3, 1, 2, 0, 1, 0, 1, 1 }; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1331 | return DeBruijnBytePos[((U32)((val & -(S32)val) * 0x077CB531U)) >> 27]; |
| 1332 | # endif |
| 1333 | } |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1334 | } else { /* Big Endian CPU */ |
| 1335 | if (MEM_64bits()) { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1336 | # if defined(_MSC_VER) && defined(_WIN64) |
| 1337 | unsigned long r = 0; |
| 1338 | _BitScanReverse64( &r, val ); |
| 1339 | return (unsigned)(r>>3); |
| 1340 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 1341 | return (__builtin_clzll(val) >> 3); |
| 1342 | # else |
| 1343 | unsigned r; |
| 1344 | const unsigned n32 = sizeof(size_t)*4; /* calculate this way due to compiler complaining in 32-bits mode */ |
| 1345 | if (!(val>>n32)) { r=4; } else { r=0; val>>=n32; } |
| 1346 | if (!(val>>16)) { r+=2; val>>=8; } else { val>>=24; } |
| 1347 | r += (!val); |
| 1348 | return r; |
| 1349 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1350 | } else { /* 32 bits */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1351 | # if defined(_MSC_VER) |
| 1352 | unsigned long r = 0; |
| 1353 | _BitScanReverse( &r, (unsigned long)val ); |
| 1354 | return (unsigned)(r>>3); |
| 1355 | # elif defined(__GNUC__) && (__GNUC__ >= 3) |
| 1356 | return (__builtin_clz((U32)val) >> 3); |
| 1357 | # else |
| 1358 | unsigned r; |
| 1359 | if (!(val>>16)) { r=2; val>>=8; } else { r=0; val>>=24; } |
| 1360 | r += (!val); |
| 1361 | return r; |
| 1362 | # endif |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1363 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1364 | } |
| 1365 | |
| 1366 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1367 | 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] | 1368 | { |
| 1369 | const BYTE* const pStart = pIn; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1370 | const BYTE* const pInLoopLimit = pInLimit - (sizeof(size_t)-1); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1371 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1372 | while (pIn < pInLoopLimit) { |
Yann Collet | 7591a7f | 2016-05-20 11:44:43 +0200 | [diff] [blame] | 1373 | size_t const diff = MEM_readST(pMatch) ^ MEM_readST(pIn); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1374 | if (!diff) { pIn+=sizeof(size_t); pMatch+=sizeof(size_t); continue; } |
| 1375 | pIn += ZSTD_NbCommonBytes(diff); |
| 1376 | return (size_t)(pIn - pStart); |
| 1377 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1378 | if (MEM_64bits()) if ((pIn<(pInLimit-3)) && (MEM_read32(pMatch) == MEM_read32(pIn))) { pIn+=4; pMatch+=4; } |
| 1379 | if ((pIn<(pInLimit-1)) && (MEM_read16(pMatch) == MEM_read16(pIn))) { pIn+=2; pMatch+=2; } |
| 1380 | if ((pIn<pInLimit) && (*pMatch == *pIn)) pIn++; |
| 1381 | return (size_t)(pIn - pStart); |
| 1382 | } |
| 1383 | |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 1384 | /** ZSTD_count_2segments() : |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 1385 | * can count match length with `ip` & `match` in 2 different segments. |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1386 | * convention : on reaching mEnd, match count continue starting from iStart |
| 1387 | */ |
| 1388 | static size_t ZSTD_count_2segments(const BYTE* ip, const BYTE* match, const BYTE* iEnd, const BYTE* mEnd, const BYTE* iStart) |
| 1389 | { |
Yann Collet | 7591a7f | 2016-05-20 11:44:43 +0200 | [diff] [blame] | 1390 | const BYTE* const vEnd = MIN( ip + (mEnd - match), iEnd); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1391 | size_t const matchLength = ZSTD_count(ip, match, vEnd); |
| 1392 | if (match + matchLength != mEnd) return matchLength; |
| 1393 | return matchLength + ZSTD_count(ip+matchLength, iStart, iEnd); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1394 | } |
| 1395 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1396 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1397 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1398 | * Hashes |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1399 | ***************************************/ |
inikep | cc52a97 | 2016-02-19 10:09:35 +0100 | [diff] [blame] | 1400 | static const U32 prime3bytes = 506832829U; |
| 1401 | 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] | 1402 | 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] | 1403 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1404 | static const U32 prime4bytes = 2654435761U; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1405 | 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] | 1406 | 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] | 1407 | |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1408 | static const U64 prime5bytes = 889523592379ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1409 | 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] | 1410 | 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] | 1411 | |
| 1412 | static const U64 prime6bytes = 227718039650203ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1413 | 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] | 1414 | 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] | 1415 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1416 | static const U64 prime7bytes = 58295818150454627ULL; |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1417 | 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] | 1418 | 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] | 1419 | |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1420 | static const U64 prime8bytes = 0xCF1BBCDCB7A56463ULL; |
| 1421 | static size_t ZSTD_hash8(U64 u, U32 h) { return (size_t)(((u) * prime8bytes) >> (64-h)) ; } |
| 1422 | static size_t ZSTD_hash8Ptr(const void* p, U32 h) { return ZSTD_hash8(MEM_readLE64(p), h); } |
| 1423 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1424 | static size_t ZSTD_hashPtr(const void* p, U32 hBits, U32 mls) |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1425 | { |
| 1426 | switch(mls) |
| 1427 | { |
| 1428 | default: |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 1429 | case 4: return ZSTD_hash4Ptr(p, hBits); |
| 1430 | case 5: return ZSTD_hash5Ptr(p, hBits); |
| 1431 | case 6: return ZSTD_hash6Ptr(p, hBits); |
| 1432 | case 7: return ZSTD_hash7Ptr(p, hBits); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1433 | case 8: return ZSTD_hash8Ptr(p, hBits); |
Yann Collet | 4b100f4 | 2015-10-30 15:49:48 +0100 | [diff] [blame] | 1434 | } |
| 1435 | } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 1436 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1437 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1438 | /*-************************************* |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1439 | * Fast Scan |
| 1440 | ***************************************/ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1441 | static void ZSTD_fillHashTable (ZSTD_CCtx* zc, const void* end, const U32 mls) |
| 1442 | { |
| 1443 | U32* const hashTable = zc->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1444 | U32 const hBits = zc->appliedParams.cParams.hashLog; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1445 | const BYTE* const base = zc->base; |
| 1446 | const BYTE* ip = base + zc->nextToUpdate; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1447 | const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; |
Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 1448 | const size_t fastHashFillStep = 3; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1449 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1450 | while(ip <= iend) { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1451 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip - base); |
Yann Collet | 37f3d1b | 2016-03-19 15:11:42 +0100 | [diff] [blame] | 1452 | ip += fastHashFillStep; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
| 1456 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1457 | FORCE_INLINE |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1458 | void ZSTD_compressBlock_fast_generic(ZSTD_CCtx* cctx, |
Yann Collet | 280f9a8 | 2016-08-08 00:44:00 +0200 | [diff] [blame] | 1459 | const void* src, size_t srcSize, |
| 1460 | const U32 mls) |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1461 | { |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1462 | U32* const hashTable = cctx->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1463 | U32 const hBits = cctx->appliedParams.cParams.hashLog; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1464 | seqStore_t* seqStorePtr = &(cctx->seqStore); |
| 1465 | const BYTE* const base = cctx->base; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1466 | const BYTE* const istart = (const BYTE*)src; |
Yann Collet | 805a52a | 2015-11-06 10:52:17 +0100 | [diff] [blame] | 1467 | const BYTE* ip = istart; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1468 | const BYTE* anchor = istart; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1469 | const U32 lowestIndex = cctx->dictLimit; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1470 | const BYTE* const lowest = base + lowestIndex; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1471 | const BYTE* const iend = istart + srcSize; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1472 | const BYTE* const ilimit = iend - HASH_READ_SIZE; |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1473 | U32 offset_1=cctx->rep[0], offset_2=cctx->rep[1]; |
| 1474 | U32 offsetSaved = 0; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1475 | |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1476 | /* init */ |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1477 | ip += (ip==lowest); |
| 1478 | { U32 const maxRep = (U32)(ip-lowest); |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1479 | if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; |
| 1480 | if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1481 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1482 | |
| 1483 | /* Main Search Loop */ |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1484 | while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1485 | size_t mLength; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1486 | size_t const h = ZSTD_hashPtr(ip, hBits, mls); |
| 1487 | U32 const current = (U32)(ip-base); |
| 1488 | U32 const matchIndex = hashTable[h]; |
Yann Collet | d94efbf | 2015-12-29 14:29:08 +0100 | [diff] [blame] | 1489 | const BYTE* match = base + matchIndex; |
Yann Collet | 96ffa42 | 2016-01-02 01:16:28 +0100 | [diff] [blame] | 1490 | hashTable[h] = current; /* update hash table */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1491 | |
Yann Collet | 280f9a8 | 2016-08-08 00:44:00 +0200 | [diff] [blame] | 1492 | 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] | 1493 | 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] | 1494 | ip++; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1495 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
| 1496 | } else { |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1497 | U32 offset; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1498 | if ( (matchIndex <= lowestIndex) || (MEM_read32(match) != MEM_read32(ip)) ) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1499 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1500 | continue; |
| 1501 | } |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1502 | mLength = ZSTD_count(ip+4, match+4, iend) + 4; |
Yann Collet | 92d7566 | 2016-07-03 01:10:53 +0200 | [diff] [blame] | 1503 | offset = (U32)(ip-match); |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1504 | 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] | 1505 | offset_2 = offset_1; |
| 1506 | offset_1 = offset; |
inikep | 5945308 | 2016-03-16 15:35:14 +0100 | [diff] [blame] | 1507 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1508 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1509 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1510 | |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1511 | /* match found */ |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1512 | ip += mLength; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1513 | anchor = ip; |
| 1514 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1515 | if (ip <= ilimit) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1516 | /* Fill Table */ |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1517 | 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] | 1518 | hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); |
| 1519 | /* check immediate repcode */ |
| 1520 | while ( (ip <= ilimit) |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1521 | && ( (offset_2>0) |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1522 | & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1523 | /* store sequence */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1524 | 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] | 1525 | { 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] | 1526 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = (U32)(ip-base); |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1527 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH); |
| 1528 | ip += rLength; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1529 | anchor = ip; |
| 1530 | continue; /* faster when present ... (?) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1531 | } } } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1532 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1533 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1534 | cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved; |
| 1535 | cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1536 | |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 1537 | /* Last Literals */ |
| 1538 | { size_t const lastLLSize = iend - anchor; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1539 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1540 | seqStorePtr->lit += lastLLSize; |
| 1541 | } |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1545 | static void ZSTD_compressBlock_fast(ZSTD_CCtx* ctx, |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1546 | const void* src, size_t srcSize) |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1547 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1548 | const U32 mls = ctx->appliedParams.cParams.searchLength; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1549 | switch(mls) |
| 1550 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1551 | default: /* includes case 3 */ |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1552 | case 4 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1553 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 4); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1554 | case 5 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1555 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 5); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1556 | case 6 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1557 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 6); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1558 | case 7 : |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1559 | ZSTD_compressBlock_fast_generic(ctx, src, srcSize, 7); return; |
Yann Collet | 1f44b3f | 2015-11-05 17:32:18 +0100 | [diff] [blame] | 1560 | } |
| 1561 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1562 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1563 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1564 | static void ZSTD_compressBlock_fast_extDict_generic(ZSTD_CCtx* ctx, |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1565 | const void* src, size_t srcSize, |
| 1566 | const U32 mls) |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1567 | { |
| 1568 | U32* hashTable = ctx->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1569 | const U32 hBits = ctx->appliedParams.cParams.hashLog; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1570 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 1571 | const BYTE* const base = ctx->base; |
| 1572 | const BYTE* const dictBase = ctx->dictBase; |
| 1573 | const BYTE* const istart = (const BYTE*)src; |
| 1574 | const BYTE* ip = istart; |
| 1575 | const BYTE* anchor = istart; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1576 | const U32 lowestIndex = ctx->lowLimit; |
| 1577 | const BYTE* const dictStart = dictBase + lowestIndex; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1578 | const U32 dictLimit = ctx->dictLimit; |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1579 | const BYTE* const lowPrefixPtr = base + dictLimit; |
| 1580 | const BYTE* const dictEnd = dictBase + dictLimit; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1581 | const BYTE* const iend = istart + srcSize; |
| 1582 | const BYTE* const ilimit = iend - 8; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1583 | U32 offset_1=ctx->rep[0], offset_2=ctx->rep[1]; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1584 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1585 | /* Search Loop */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1586 | while (ip < ilimit) { /* < instead of <=, because (ip+1) */ |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1587 | const size_t h = ZSTD_hashPtr(ip, hBits, mls); |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1588 | const U32 matchIndex = hashTable[h]; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1589 | const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base; |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1590 | const BYTE* match = matchBase + matchIndex; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1591 | const U32 current = (U32)(ip-base); |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1592 | const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */ |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1593 | const BYTE* repBase = repIndex < dictLimit ? dictBase : base; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1594 | const BYTE* repMatch = repBase + repIndex; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1595 | size_t mLength; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1596 | hashTable[h] = current; /* update hash table */ |
| 1597 | |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1598 | if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1599 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1600 | const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1601 | 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] | 1602 | ip++; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1603 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1604 | } else { |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 1605 | if ( (matchIndex < lowestIndex) || |
Yann Collet | 5244738 | 2016-03-20 16:00:00 +0100 | [diff] [blame] | 1606 | (MEM_read32(match) != MEM_read32(ip)) ) { |
| 1607 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1608 | continue; |
| 1609 | } |
| 1610 | { const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1611 | const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1612 | U32 offset; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1613 | mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1614 | 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] | 1615 | offset = current - matchIndex; |
| 1616 | offset_2 = offset_1; |
| 1617 | offset_1 = offset; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1618 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1619 | } } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1620 | |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1621 | /* found a match : store it */ |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 1622 | ip += mLength; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1623 | anchor = ip; |
| 1624 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1625 | if (ip <= ilimit) { |
Yann Collet | 6bcdeac | 2015-11-26 11:43:00 +0100 | [diff] [blame] | 1626 | /* Fill Table */ |
Yann Collet | 3e21ec5 | 2016-09-06 15:36:19 +0200 | [diff] [blame] | 1627 | hashTable[ZSTD_hashPtr(base+current+2, hBits, mls)] = current+2; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1628 | hashTable[ZSTD_hashPtr(ip-2, hBits, mls)] = (U32)(ip-2-base); |
| 1629 | /* check immediate repcode */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1630 | while (ip <= ilimit) { |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 1631 | U32 const current2 = (U32)(ip-base); |
| 1632 | U32 const repIndex2 = current2 - offset_2; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1633 | const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1634 | if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */ |
| 1635 | && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1636 | const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1637 | 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] | 1638 | 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] | 1639 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1640 | hashTable[ZSTD_hashPtr(ip, hBits, mls)] = current2; |
inikep | 7bc19b6 | 2016-04-06 09:46:01 +0200 | [diff] [blame] | 1641 | ip += repLength2; |
Yann Collet | 402fdcf | 2015-11-20 12:46:08 +0100 | [diff] [blame] | 1642 | anchor = ip; |
| 1643 | continue; |
| 1644 | } |
Yann Collet | 743402c | 2015-11-20 12:03:53 +0100 | [diff] [blame] | 1645 | break; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1646 | } } } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1647 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1648 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1649 | ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 1650 | |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1651 | /* Last Literals */ |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 1652 | { size_t const lastLLSize = iend - anchor; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1653 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1654 | seqStorePtr->lit += lastLLSize; |
| 1655 | } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 1659 | static void ZSTD_compressBlock_fast_extDict(ZSTD_CCtx* ctx, |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1660 | const void* src, size_t srcSize) |
| 1661 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1662 | U32 const mls = ctx->appliedParams.cParams.searchLength; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1663 | switch(mls) |
| 1664 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1665 | default: /* includes case 3 */ |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1666 | case 4 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1667 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 4); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1668 | case 5 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1669 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 5); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1670 | case 6 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1671 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 6); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1672 | case 7 : |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 1673 | ZSTD_compressBlock_fast_extDict_generic(ctx, src, srcSize, 7); return; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 1678 | /*-************************************* |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1679 | * Double Fast |
| 1680 | ***************************************/ |
| 1681 | static void ZSTD_fillDoubleHashTable (ZSTD_CCtx* cctx, const void* end, const U32 mls) |
| 1682 | { |
| 1683 | U32* const hashLarge = cctx->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1684 | U32 const hBitsL = cctx->appliedParams.cParams.hashLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1685 | U32* const hashSmall = cctx->chainTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1686 | U32 const hBitsS = cctx->appliedParams.cParams.chainLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1687 | const BYTE* const base = cctx->base; |
| 1688 | const BYTE* ip = base + cctx->nextToUpdate; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1689 | const BYTE* const iend = ((const BYTE*)end) - HASH_READ_SIZE; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1690 | const size_t fastHashFillStep = 3; |
| 1691 | |
| 1692 | while(ip <= iend) { |
| 1693 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip - base); |
| 1694 | hashLarge[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip - base); |
| 1695 | ip += fastHashFillStep; |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | FORCE_INLINE |
| 1701 | void ZSTD_compressBlock_doubleFast_generic(ZSTD_CCtx* cctx, |
| 1702 | const void* src, size_t srcSize, |
| 1703 | const U32 mls) |
| 1704 | { |
| 1705 | U32* const hashLong = cctx->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1706 | const U32 hBitsL = cctx->appliedParams.cParams.hashLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1707 | U32* const hashSmall = cctx->chainTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1708 | const U32 hBitsS = cctx->appliedParams.cParams.chainLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1709 | seqStore_t* seqStorePtr = &(cctx->seqStore); |
| 1710 | const BYTE* const base = cctx->base; |
| 1711 | const BYTE* const istart = (const BYTE*)src; |
| 1712 | const BYTE* ip = istart; |
| 1713 | const BYTE* anchor = istart; |
| 1714 | const U32 lowestIndex = cctx->dictLimit; |
| 1715 | const BYTE* const lowest = base + lowestIndex; |
| 1716 | const BYTE* const iend = istart + srcSize; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1717 | const BYTE* const ilimit = iend - HASH_READ_SIZE; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1718 | U32 offset_1=cctx->rep[0], offset_2=cctx->rep[1]; |
| 1719 | U32 offsetSaved = 0; |
| 1720 | |
| 1721 | /* init */ |
| 1722 | ip += (ip==lowest); |
| 1723 | { U32 const maxRep = (U32)(ip-lowest); |
| 1724 | if (offset_2 > maxRep) offsetSaved = offset_2, offset_2 = 0; |
| 1725 | if (offset_1 > maxRep) offsetSaved = offset_1, offset_1 = 0; |
| 1726 | } |
| 1727 | |
| 1728 | /* Main Search Loop */ |
| 1729 | while (ip < ilimit) { /* < instead of <=, because repcode check at (ip+1) */ |
| 1730 | size_t mLength; |
| 1731 | size_t const h2 = ZSTD_hashPtr(ip, hBitsL, 8); |
| 1732 | size_t const h = ZSTD_hashPtr(ip, hBitsS, mls); |
| 1733 | U32 const current = (U32)(ip-base); |
| 1734 | U32 const matchIndexL = hashLong[h2]; |
| 1735 | U32 const matchIndexS = hashSmall[h]; |
| 1736 | const BYTE* matchLong = base + matchIndexL; |
| 1737 | const BYTE* match = base + matchIndexS; |
| 1738 | hashLong[h2] = hashSmall[h] = current; /* update hash tables */ |
| 1739 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1740 | assert(offset_1 <= current); /* supposed guaranteed by construction */ |
| 1741 | 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] | 1742 | /* favor repcode */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1743 | mLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; |
| 1744 | ip++; |
| 1745 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
| 1746 | } else { |
Yann Collet | eed2081 | 2016-07-12 15:11:40 +0200 | [diff] [blame] | 1747 | U32 offset; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1748 | if ( (matchIndexL > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip)) ) { |
| 1749 | mLength = ZSTD_count(ip+8, matchLong+8, iend) + 8; |
Yann Collet | eed2081 | 2016-07-12 15:11:40 +0200 | [diff] [blame] | 1750 | offset = (U32)(ip-matchLong); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1751 | while (((ip>anchor) & (matchLong>lowest)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ |
| 1752 | } else if ( (matchIndexS > lowestIndex) && (MEM_read32(match) == MEM_read32(ip)) ) { |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1753 | size_t const hl3 = ZSTD_hashPtr(ip+1, hBitsL, 8); |
| 1754 | U32 const matchIndexL3 = hashLong[hl3]; |
| 1755 | const BYTE* matchL3 = base + matchIndexL3; |
| 1756 | hashLong[hl3] = current + 1; |
| 1757 | if ( (matchIndexL3 > lowestIndex) && (MEM_read64(matchL3) == MEM_read64(ip+1)) ) { |
| 1758 | mLength = ZSTD_count(ip+9, matchL3+8, iend) + 8; |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1759 | ip++; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1760 | offset = (U32)(ip-matchL3); |
| 1761 | 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] | 1762 | } else { |
| 1763 | mLength = ZSTD_count(ip+4, match+4, iend) + 4; |
| 1764 | offset = (U32)(ip-match); |
| 1765 | while (((ip>anchor) & (match>lowest)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
| 1766 | } |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1767 | } else { |
| 1768 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1769 | continue; |
| 1770 | } |
| 1771 | |
| 1772 | offset_2 = offset_1; |
| 1773 | offset_1 = offset; |
| 1774 | |
| 1775 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
| 1776 | } |
| 1777 | |
| 1778 | /* match found */ |
| 1779 | ip += mLength; |
| 1780 | anchor = ip; |
| 1781 | |
| 1782 | if (ip <= ilimit) { |
| 1783 | /* Fill Table */ |
| 1784 | hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = |
| 1785 | hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; /* here because current+2 could be > iend-8 */ |
| 1786 | hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = |
| 1787 | hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base); |
| 1788 | |
| 1789 | /* check immediate repcode */ |
| 1790 | while ( (ip <= ilimit) |
| 1791 | && ( (offset_2>0) |
| 1792 | & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { |
| 1793 | /* store sequence */ |
| 1794 | 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] | 1795 | { 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] | 1796 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = (U32)(ip-base); |
| 1797 | hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = (U32)(ip-base); |
| 1798 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, rLength-MINMATCH); |
| 1799 | ip += rLength; |
| 1800 | anchor = ip; |
| 1801 | continue; /* faster when present ... (?) */ |
| 1802 | } } } |
| 1803 | |
| 1804 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1805 | cctx->repToConfirm[0] = offset_1 ? offset_1 : offsetSaved; |
| 1806 | cctx->repToConfirm[1] = offset_2 ? offset_2 : offsetSaved; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1807 | |
| 1808 | /* Last Literals */ |
| 1809 | { size_t const lastLLSize = iend - anchor; |
| 1810 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1811 | seqStorePtr->lit += lastLLSize; |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | |
| 1816 | static void ZSTD_compressBlock_doubleFast(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 1817 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1818 | const U32 mls = ctx->appliedParams.cParams.searchLength; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1819 | switch(mls) |
| 1820 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1821 | default: /* includes case 3 */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1822 | case 4 : |
| 1823 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 4); return; |
| 1824 | case 5 : |
| 1825 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 5); return; |
| 1826 | case 6 : |
| 1827 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 6); return; |
| 1828 | case 7 : |
| 1829 | ZSTD_compressBlock_doubleFast_generic(ctx, src, srcSize, 7); return; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | |
| 1834 | static void ZSTD_compressBlock_doubleFast_extDict_generic(ZSTD_CCtx* ctx, |
| 1835 | const void* src, size_t srcSize, |
| 1836 | const U32 mls) |
| 1837 | { |
| 1838 | U32* const hashLong = ctx->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1839 | U32 const hBitsL = ctx->appliedParams.cParams.hashLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1840 | U32* const hashSmall = ctx->chainTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1841 | U32 const hBitsS = ctx->appliedParams.cParams.chainLog; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1842 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 1843 | const BYTE* const base = ctx->base; |
| 1844 | const BYTE* const dictBase = ctx->dictBase; |
| 1845 | const BYTE* const istart = (const BYTE*)src; |
| 1846 | const BYTE* ip = istart; |
| 1847 | const BYTE* anchor = istart; |
| 1848 | const U32 lowestIndex = ctx->lowLimit; |
| 1849 | const BYTE* const dictStart = dictBase + lowestIndex; |
| 1850 | const U32 dictLimit = ctx->dictLimit; |
| 1851 | const BYTE* const lowPrefixPtr = base + dictLimit; |
| 1852 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 1853 | const BYTE* const iend = istart + srcSize; |
| 1854 | const BYTE* const ilimit = iend - 8; |
| 1855 | U32 offset_1=ctx->rep[0], offset_2=ctx->rep[1]; |
| 1856 | |
| 1857 | /* Search Loop */ |
| 1858 | while (ip < ilimit) { /* < instead of <=, because (ip+1) */ |
| 1859 | const size_t hSmall = ZSTD_hashPtr(ip, hBitsS, mls); |
| 1860 | const U32 matchIndex = hashSmall[hSmall]; |
| 1861 | const BYTE* matchBase = matchIndex < dictLimit ? dictBase : base; |
| 1862 | const BYTE* match = matchBase + matchIndex; |
| 1863 | |
| 1864 | const size_t hLong = ZSTD_hashPtr(ip, hBitsL, 8); |
| 1865 | const U32 matchLongIndex = hashLong[hLong]; |
| 1866 | const BYTE* matchLongBase = matchLongIndex < dictLimit ? dictBase : base; |
| 1867 | const BYTE* matchLong = matchLongBase + matchLongIndex; |
| 1868 | |
| 1869 | const U32 current = (U32)(ip-base); |
| 1870 | const U32 repIndex = current + 1 - offset_1; /* offset_1 expected <= current +1 */ |
| 1871 | const BYTE* repBase = repIndex < dictLimit ? dictBase : base; |
| 1872 | const BYTE* repMatch = repBase + repIndex; |
| 1873 | size_t mLength; |
| 1874 | hashSmall[hSmall] = hashLong[hLong] = current; /* update hash table */ |
| 1875 | |
| 1876 | if ( (((U32)((dictLimit-1) - repIndex) >= 3) /* intentional underflow */ & (repIndex > lowestIndex)) |
| 1877 | && (MEM_read32(repMatch) == MEM_read32(ip+1)) ) { |
| 1878 | const BYTE* repMatchEnd = repIndex < dictLimit ? dictEnd : iend; |
| 1879 | mLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repMatchEnd, lowPrefixPtr) + 4; |
| 1880 | ip++; |
| 1881 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, 0, mLength-MINMATCH); |
| 1882 | } else { |
| 1883 | if ((matchLongIndex > lowestIndex) && (MEM_read64(matchLong) == MEM_read64(ip))) { |
| 1884 | const BYTE* matchEnd = matchLongIndex < dictLimit ? dictEnd : iend; |
| 1885 | const BYTE* lowMatchPtr = matchLongIndex < dictLimit ? dictStart : lowPrefixPtr; |
| 1886 | U32 offset; |
| 1887 | mLength = ZSTD_count_2segments(ip+8, matchLong+8, iend, matchEnd, lowPrefixPtr) + 8; |
| 1888 | offset = current - matchLongIndex; |
| 1889 | while (((ip>anchor) & (matchLong>lowMatchPtr)) && (ip[-1] == matchLong[-1])) { ip--; matchLong--; mLength++; } /* catch up */ |
| 1890 | offset_2 = offset_1; |
| 1891 | offset_1 = offset; |
| 1892 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1893 | |
Yann Collet | 73d74a0 | 2016-07-12 13:03:48 +0200 | [diff] [blame] | 1894 | } else if ((matchIndex > lowestIndex) && (MEM_read32(match) == MEM_read32(ip))) { |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1895 | size_t const h3 = ZSTD_hashPtr(ip+1, hBitsL, 8); |
| 1896 | U32 const matchIndex3 = hashLong[h3]; |
| 1897 | const BYTE* const match3Base = matchIndex3 < dictLimit ? dictBase : base; |
| 1898 | const BYTE* match3 = match3Base + matchIndex3; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1899 | U32 offset; |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1900 | hashLong[h3] = current + 1; |
| 1901 | if ( (matchIndex3 > lowestIndex) && (MEM_read64(match3) == MEM_read64(ip+1)) ) { |
| 1902 | const BYTE* matchEnd = matchIndex3 < dictLimit ? dictEnd : iend; |
| 1903 | const BYTE* lowMatchPtr = matchIndex3 < dictLimit ? dictStart : lowPrefixPtr; |
| 1904 | mLength = ZSTD_count_2segments(ip+9, match3+8, iend, matchEnd, lowPrefixPtr) + 8; |
| 1905 | ip++; |
| 1906 | offset = current+1 - matchIndex3; |
| 1907 | while (((ip>anchor) & (match3>lowMatchPtr)) && (ip[-1] == match3[-1])) { ip--; match3--; mLength++; } /* catch up */ |
| 1908 | } else { |
| 1909 | const BYTE* matchEnd = matchIndex < dictLimit ? dictEnd : iend; |
| 1910 | const BYTE* lowMatchPtr = matchIndex < dictLimit ? dictStart : lowPrefixPtr; |
| 1911 | mLength = ZSTD_count_2segments(ip+4, match+4, iend, matchEnd, lowPrefixPtr) + 4; |
| 1912 | offset = current - matchIndex; |
| 1913 | while (((ip>anchor) & (match>lowMatchPtr)) && (ip[-1] == match[-1])) { ip--; match--; mLength++; } /* catch up */ |
| 1914 | } |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1915 | offset_2 = offset_1; |
| 1916 | offset_1 = offset; |
| 1917 | ZSTD_storeSeq(seqStorePtr, ip-anchor, anchor, offset + ZSTD_REP_MOVE, mLength-MINMATCH); |
Yann Collet | c54692f | 2016-08-24 01:10:42 +0200 | [diff] [blame] | 1918 | |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1919 | } else { |
| 1920 | ip += ((ip-anchor) >> g_searchStrength) + 1; |
| 1921 | continue; |
| 1922 | } } |
| 1923 | |
| 1924 | /* found a match : store it */ |
| 1925 | ip += mLength; |
| 1926 | anchor = ip; |
| 1927 | |
| 1928 | if (ip <= ilimit) { |
| 1929 | /* Fill Table */ |
Nick Terrell | f35ef5c | 2017-03-09 12:51:33 -0800 | [diff] [blame] | 1930 | hashSmall[ZSTD_hashPtr(base+current+2, hBitsS, mls)] = current+2; |
| 1931 | hashLong[ZSTD_hashPtr(base+current+2, hBitsL, 8)] = current+2; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1932 | hashSmall[ZSTD_hashPtr(ip-2, hBitsS, mls)] = (U32)(ip-2-base); |
| 1933 | hashLong[ZSTD_hashPtr(ip-2, hBitsL, 8)] = (U32)(ip-2-base); |
| 1934 | /* check immediate repcode */ |
| 1935 | while (ip <= ilimit) { |
| 1936 | U32 const current2 = (U32)(ip-base); |
| 1937 | U32 const repIndex2 = current2 - offset_2; |
| 1938 | const BYTE* repMatch2 = repIndex2 < dictLimit ? dictBase + repIndex2 : base + repIndex2; |
| 1939 | if ( (((U32)((dictLimit-1) - repIndex2) >= 3) & (repIndex2 > lowestIndex)) /* intentional overflow */ |
| 1940 | && (MEM_read32(repMatch2) == MEM_read32(ip)) ) { |
| 1941 | const BYTE* const repEnd2 = repIndex2 < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 1942 | 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] | 1943 | U32 tmpOffset = offset_2; offset_2 = offset_1; offset_1 = tmpOffset; /* swap offset_2 <=> offset_1 */ |
| 1944 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, repLength2-MINMATCH); |
| 1945 | hashSmall[ZSTD_hashPtr(ip, hBitsS, mls)] = current2; |
| 1946 | hashLong[ZSTD_hashPtr(ip, hBitsL, 8)] = current2; |
| 1947 | ip += repLength2; |
| 1948 | anchor = ip; |
| 1949 | continue; |
| 1950 | } |
| 1951 | break; |
| 1952 | } } } |
| 1953 | |
| 1954 | /* save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 1955 | ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1956 | |
| 1957 | /* Last Literals */ |
| 1958 | { size_t const lastLLSize = iend - anchor; |
| 1959 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1960 | seqStorePtr->lit += lastLLSize; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | |
| 1965 | static void ZSTD_compressBlock_doubleFast_extDict(ZSTD_CCtx* ctx, |
| 1966 | const void* src, size_t srcSize) |
| 1967 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1968 | U32 const mls = ctx->appliedParams.cParams.searchLength; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1969 | switch(mls) |
| 1970 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 1971 | default: /* includes case 3 */ |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1972 | case 4 : |
| 1973 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 4); return; |
| 1974 | case 5 : |
| 1975 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 5); return; |
| 1976 | case 6 : |
| 1977 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 6); return; |
| 1978 | case 7 : |
| 1979 | ZSTD_compressBlock_doubleFast_extDict_generic(ctx, src, srcSize, 7); return; |
| 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | |
| 1984 | /*-************************************* |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1985 | * Binary Tree search |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1986 | ***************************************/ |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 1987 | /** ZSTD_insertBt1() : add one or multiple positions to tree. |
| 1988 | * ip : assumed <= iend-8 . |
Yann Collet | 06eade5 | 2015-11-23 14:23:47 +0100 | [diff] [blame] | 1989 | * @return : nb of positions added */ |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 1990 | static U32 ZSTD_insertBt1(ZSTD_CCtx* zc, const BYTE* const ip, const U32 mls, const BYTE* const iend, U32 nbCompares, |
| 1991 | U32 extDict) |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 1992 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1993 | U32* const hashTable = zc->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1994 | U32 const hashLog = zc->appliedParams.cParams.hashLog; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1995 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
| 1996 | U32* const bt = zc->chainTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1997 | U32 const btLog = zc->appliedParams.cParams.chainLog - 1; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1998 | U32 const btMask = (1 << btLog) - 1; |
| 1999 | U32 matchIndex = hashTable[h]; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2000 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 2001 | const BYTE* const base = zc->base; |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 2002 | const BYTE* const dictBase = zc->dictBase; |
| 2003 | const U32 dictLimit = zc->dictLimit; |
| 2004 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 2005 | const BYTE* const prefixStart = base + dictLimit; |
Yann Collet | 2b361cf | 2016-10-14 16:03:34 -0700 | [diff] [blame] | 2006 | const BYTE* match; |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 2007 | const U32 current = (U32)(ip-base); |
Yann Collet | e9eba60 | 2015-11-08 15:08:03 +0100 | [diff] [blame] | 2008 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2009 | U32* smallerPtr = bt + 2*(current&btMask); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 2010 | U32* largerPtr = smallerPtr + 1; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 2011 | U32 dummy32; /* to be nullified at the end */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2012 | U32 const windowLow = zc->lowLimit; |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 2013 | U32 matchEndIdx = current+8; |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2014 | size_t bestLength = 8; |
Yann Collet | c093208 | 2016-06-30 14:07:30 +0200 | [diff] [blame] | 2015 | #ifdef ZSTD_C_PREDICT |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 2016 | U32 predictedSmall = *(bt + 2*((current-1)&btMask) + 0); |
| 2017 | U32 predictedLarge = *(bt + 2*((current-1)&btMask) + 1); |
| 2018 | predictedSmall += (predictedSmall>0); |
| 2019 | predictedLarge += (predictedLarge>0); |
Yann Collet | c093208 | 2016-06-30 14:07:30 +0200 | [diff] [blame] | 2020 | #endif /* ZSTD_C_PREDICT */ |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 2021 | |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 2022 | hashTable[h] = current; /* Update Hash Table */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2023 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2024 | while (nbCompares-- && (matchIndex > windowLow)) { |
Yann Collet | 25f46dc | 2016-11-29 16:59:27 -0800 | [diff] [blame] | 2025 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2026 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
Yann Collet | 25f46dc | 2016-11-29 16:59:27 -0800 | [diff] [blame] | 2027 | |
Yann Collet | c093208 | 2016-06-30 14:07:30 +0200 | [diff] [blame] | 2028 | #ifdef ZSTD_C_PREDICT /* note : can create issues when hlog small <= 11 */ |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 2029 | 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] | 2030 | if (matchIndex == predictedSmall) { |
| 2031 | /* no need to check length, result known */ |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 2032 | *smallerPtr = matchIndex; |
| 2033 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 2034 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 2035 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 2036 | predictedSmall = predictPtr[1] + (predictPtr[1]>0); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 2037 | continue; |
| 2038 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2039 | if (matchIndex == predictedLarge) { |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 2040 | *largerPtr = matchIndex; |
| 2041 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 2042 | largerPtr = nextPtr; |
| 2043 | matchIndex = nextPtr[0]; |
Yann Collet | 7beaa05 | 2016-01-21 11:57:45 +0100 | [diff] [blame] | 2044 | predictedLarge = predictPtr[0] + (predictPtr[0]>0); |
Yann Collet | a87278a | 2016-01-17 00:12:55 +0100 | [diff] [blame] | 2045 | continue; |
| 2046 | } |
Yann Collet | 04b12d8 | 2016-02-11 06:23:24 +0100 | [diff] [blame] | 2047 | #endif |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2048 | if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 2049 | match = base + matchIndex; |
| 2050 | if (match[matchLength] == ip[matchLength]) |
| 2051 | matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2052 | } else { |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 2053 | match = dictBase + matchIndex; |
| 2054 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
| 2055 | if (matchIndex+matchLength >= dictLimit) |
Nick Terrell | f35ef5c | 2017-03-09 12:51:33 -0800 | [diff] [blame] | 2056 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
Yann Collet | 1358f91 | 2016-01-01 07:29:39 +0100 | [diff] [blame] | 2057 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2058 | |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2059 | if (matchLength > bestLength) { |
| 2060 | bestLength = matchLength; |
| 2061 | if (matchLength > matchEndIdx - matchIndex) |
| 2062 | matchEndIdx = matchIndex + (U32)matchLength; |
| 2063 | } |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 2064 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 2065 | 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] | 2066 | 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] | 2067 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2068 | if (match[matchLength] < ip[matchLength]) { /* necessarily within correct buffer */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2069 | /* match is smaller than current */ |
| 2070 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 2071 | 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] | 2072 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2073 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 2074 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2075 | } else { |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2076 | /* match is larger than current */ |
| 2077 | *largerPtr = matchIndex; |
| 2078 | commonLengthLarger = matchLength; |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 2079 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2080 | largerPtr = nextPtr; |
Yann Collet | f48e35c | 2015-11-07 01:13:31 +0100 | [diff] [blame] | 2081 | matchIndex = nextPtr[0]; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2082 | } } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2083 | |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 2084 | *smallerPtr = *largerPtr = 0; |
Yann Collet | a436a52 | 2016-06-20 23:34:04 +0200 | [diff] [blame] | 2085 | if (bestLength > 384) return MIN(192, (U32)(bestLength - 384)); /* speed optimization */ |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2086 | if (matchEndIdx > current + 8) return matchEndIdx - current - 8; |
| 2087 | return 1; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2088 | } |
| 2089 | |
| 2090 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 2091 | static size_t ZSTD_insertBtAndFindBestMatch ( |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2092 | ZSTD_CCtx* zc, |
| 2093 | const BYTE* const ip, const BYTE* const iend, |
| 2094 | size_t* offsetPtr, |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2095 | U32 nbCompares, const U32 mls, |
| 2096 | U32 extDict) |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2097 | { |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2098 | U32* const hashTable = zc->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2099 | U32 const hashLog = zc->appliedParams.cParams.hashLog; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2100 | size_t const h = ZSTD_hashPtr(ip, hashLog, mls); |
| 2101 | U32* const bt = zc->chainTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2102 | U32 const btLog = zc->appliedParams.cParams.chainLog - 1; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2103 | U32 const btMask = (1 << btLog) - 1; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2104 | U32 matchIndex = hashTable[h]; |
| 2105 | size_t commonLengthSmaller=0, commonLengthLarger=0; |
| 2106 | const BYTE* const base = zc->base; |
| 2107 | const BYTE* const dictBase = zc->dictBase; |
| 2108 | const U32 dictLimit = zc->dictLimit; |
| 2109 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 2110 | const BYTE* const prefixStart = base + dictLimit; |
| 2111 | const U32 current = (U32)(ip-base); |
| 2112 | const U32 btLow = btMask >= current ? 0 : current - btMask; |
| 2113 | const U32 windowLow = zc->lowLimit; |
| 2114 | U32* smallerPtr = bt + 2*(current&btMask); |
| 2115 | U32* largerPtr = bt + 2*(current&btMask) + 1; |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 2116 | U32 matchEndIdx = current+8; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2117 | U32 dummy32; /* to be nullified at the end */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2118 | size_t bestLength = 0; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2119 | |
Yann Collet | 6c3e2e7 | 2015-12-11 10:44:07 +0100 | [diff] [blame] | 2120 | hashTable[h] = current; /* Update Hash Table */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2121 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2122 | while (nbCompares-- && (matchIndex > windowLow)) { |
Yann Collet | 25f46dc | 2016-11-29 16:59:27 -0800 | [diff] [blame] | 2123 | U32* const nextPtr = bt + 2*(matchIndex & btMask); |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2124 | size_t matchLength = MIN(commonLengthSmaller, commonLengthLarger); /* guaranteed minimum nb of common bytes */ |
| 2125 | const BYTE* match; |
| 2126 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2127 | if ((!extDict) || (matchIndex+matchLength >= dictLimit)) { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2128 | match = base + matchIndex; |
| 2129 | if (match[matchLength] == ip[matchLength]) |
| 2130 | matchLength += ZSTD_count(ip+matchLength+1, match+matchLength+1, iend) +1; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2131 | } else { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2132 | match = dictBase + matchIndex; |
| 2133 | matchLength += ZSTD_count_2segments(ip+matchLength, match+matchLength, iend, dictEnd, prefixStart); |
Yann Collet | 225179d | 2015-11-23 16:52:22 +0100 | [diff] [blame] | 2134 | if (matchIndex+matchLength >= dictLimit) |
Nick Terrell | f35ef5c | 2017-03-09 12:51:33 -0800 | [diff] [blame] | 2135 | match = base + matchIndex; /* to prepare for next usage of match[matchLength] */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2136 | } |
| 2137 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2138 | if (matchLength > bestLength) { |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 2139 | if (matchLength > matchEndIdx - matchIndex) |
Yann Collet | 48da164 | 2015-12-29 23:40:02 +0100 | [diff] [blame] | 2140 | matchEndIdx = matchIndex + (U32)matchLength; |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2141 | 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] | 2142 | bestLength = matchLength, *offsetPtr = ZSTD_REP_MOVE + current - matchIndex; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2143 | if (ip+matchLength == iend) /* equal : no way to know if inf or sup */ |
| 2144 | break; /* drop, to guarantee consistency (miss a little bit of compression) */ |
| 2145 | } |
| 2146 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2147 | if (match[matchLength] < ip[matchLength]) { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2148 | /* match is smaller than current */ |
| 2149 | *smallerPtr = matchIndex; /* update smaller idx */ |
| 2150 | commonLengthSmaller = matchLength; /* all smaller will now have at least this guaranteed common length */ |
| 2151 | if (matchIndex <= btLow) { smallerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 2152 | smallerPtr = nextPtr+1; /* new "smaller" => larger of match */ |
| 2153 | matchIndex = nextPtr[1]; /* new matchIndex larger than previous (closer to current) */ |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 2154 | } else { |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2155 | /* match is larger than current */ |
| 2156 | *largerPtr = matchIndex; |
| 2157 | commonLengthLarger = matchLength; |
| 2158 | if (matchIndex <= btLow) { largerPtr=&dummy32; break; } /* beyond tree size, stop the search */ |
| 2159 | largerPtr = nextPtr; |
| 2160 | matchIndex = nextPtr[0]; |
Yann Collet | 768c6bc | 2016-02-10 14:01:49 +0100 | [diff] [blame] | 2161 | } } |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2162 | |
| 2163 | *smallerPtr = *largerPtr = 0; |
| 2164 | |
Yann Collet | 72e84cf | 2015-12-31 19:08:44 +0100 | [diff] [blame] | 2165 | zc->nextToUpdate = (matchEndIdx > current + 8) ? matchEndIdx - 8 : current+1; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2166 | return bestLength; |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2167 | } |
| 2168 | |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2169 | |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2170 | 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] | 2171 | { |
| 2172 | const BYTE* const base = zc->base; |
| 2173 | const U32 target = (U32)(ip - base); |
| 2174 | U32 idx = zc->nextToUpdate; |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2175 | |
| 2176 | while(idx < target) |
| 2177 | idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 0); |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 2178 | } |
| 2179 | |
Yann Collet | 5244738 | 2016-03-20 16:00:00 +0100 | [diff] [blame] | 2180 | /** ZSTD_BtFindBestMatch() : Tree updater, providing best match */ |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 2181 | static size_t ZSTD_BtFindBestMatch ( |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2182 | ZSTD_CCtx* zc, |
| 2183 | const BYTE* const ip, const BYTE* const iLimit, |
| 2184 | size_t* offsetPtr, |
| 2185 | const U32 maxNbAttempts, const U32 mls) |
| 2186 | { |
| 2187 | if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2188 | ZSTD_updateTree(zc, ip, iLimit, maxNbAttempts, mls); |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2189 | return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 0); |
| 2190 | } |
| 2191 | |
| 2192 | |
Yann Collet | 768c6bc | 2016-02-10 14:01:49 +0100 | [diff] [blame] | 2193 | static size_t ZSTD_BtFindBestMatch_selectMLS ( |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2194 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 2195 | const BYTE* ip, const BYTE* const iLimit, |
| 2196 | size_t* offsetPtr, |
| 2197 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 2198 | { |
| 2199 | switch(matchLengthSearch) |
| 2200 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 2201 | default : /* includes case 3 */ |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2202 | case 4 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 2203 | case 5 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 2204 | case 7 : |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2205 | case 6 : return ZSTD_BtFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
| 2206 | } |
| 2207 | } |
| 2208 | |
| 2209 | |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2210 | static void ZSTD_updateTree_extDict(ZSTD_CCtx* zc, const BYTE* const ip, const BYTE* const iend, const U32 nbCompares, const U32 mls) |
| 2211 | { |
| 2212 | const BYTE* const base = zc->base; |
| 2213 | const U32 target = (U32)(ip - base); |
| 2214 | U32 idx = zc->nextToUpdate; |
| 2215 | |
| 2216 | while (idx < target) idx += ZSTD_insertBt1(zc, base+idx, mls, iend, nbCompares, 1); |
| 2217 | } |
| 2218 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2219 | |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2220 | /** Tree updater, providing best match */ |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 2221 | static size_t ZSTD_BtFindBestMatch_extDict ( |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2222 | ZSTD_CCtx* zc, |
| 2223 | const BYTE* const ip, const BYTE* const iLimit, |
| 2224 | size_t* offsetPtr, |
| 2225 | const U32 maxNbAttempts, const U32 mls) |
| 2226 | { |
Yann Collet | ee3f451 | 2015-12-29 22:26:09 +0100 | [diff] [blame] | 2227 | if (ip < zc->base + zc->nextToUpdate) return 0; /* skipped area */ |
Yann Collet | b8a6f68 | 2016-02-15 17:06:29 +0100 | [diff] [blame] | 2228 | ZSTD_updateTree_extDict(zc, ip, iLimit, maxNbAttempts, mls); |
Yann Collet | 2cc12cb | 2016-01-01 07:47:58 +0100 | [diff] [blame] | 2229 | return ZSTD_insertBtAndFindBestMatch(zc, ip, iLimit, offsetPtr, maxNbAttempts, mls, 1); |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2230 | } |
| 2231 | |
| 2232 | |
Yann Collet | 82260dd | 2016-02-11 07:14:25 +0100 | [diff] [blame] | 2233 | static size_t ZSTD_BtFindBestMatch_selectMLS_extDict ( |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2234 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 2235 | const BYTE* ip, const BYTE* const iLimit, |
| 2236 | size_t* offsetPtr, |
| 2237 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 2238 | { |
| 2239 | switch(matchLengthSearch) |
| 2240 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 2241 | default : /* includes case 3 */ |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2242 | case 4 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4); |
| 2243 | case 5 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 5); |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 2244 | case 7 : |
Yann Collet | 03526e1 | 2015-11-23 15:29:15 +0100 | [diff] [blame] | 2245 | case 6 : return ZSTD_BtFindBestMatch_extDict(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6); |
| 2246 | } |
| 2247 | } |
| 2248 | |
| 2249 | |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2250 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2251 | /* ********************************* |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2252 | * Hash Chain |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2253 | ***********************************/ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2254 | #define NEXT_IN_CHAIN(d, mask) chainTable[(d) & mask] |
| 2255 | |
| 2256 | /* Update chains up to ip (excluded) |
Anders Oleson | 517577b | 2017-02-20 12:08:59 -0800 | [diff] [blame] | 2257 | Assumption : always within prefix (i.e. not within extDict) */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2258 | FORCE_INLINE |
| 2259 | U32 ZSTD_insertAndFindFirstIndex (ZSTD_CCtx* zc, const BYTE* ip, U32 mls) |
| 2260 | { |
| 2261 | U32* const hashTable = zc->hashTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2262 | const U32 hashLog = zc->appliedParams.cParams.hashLog; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2263 | U32* const chainTable = zc->chainTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2264 | const U32 chainMask = (1 << zc->appliedParams.cParams.chainLog) - 1; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2265 | const BYTE* const base = zc->base; |
| 2266 | const U32 target = (U32)(ip - base); |
| 2267 | U32 idx = zc->nextToUpdate; |
| 2268 | |
Yann Collet | 22d7632 | 2016-06-21 08:01:51 +0200 | [diff] [blame] | 2269 | while(idx < target) { /* catch up */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2270 | size_t const h = ZSTD_hashPtr(base+idx, hashLog, mls); |
| 2271 | NEXT_IN_CHAIN(idx, chainMask) = hashTable[h]; |
| 2272 | hashTable[h] = idx; |
| 2273 | idx++; |
| 2274 | } |
| 2275 | |
| 2276 | zc->nextToUpdate = target; |
| 2277 | return hashTable[ZSTD_hashPtr(ip, hashLog, mls)]; |
| 2278 | } |
| 2279 | |
| 2280 | |
Nick Terrell | 55fc1f9 | 2017-05-24 13:50:10 -0700 | [diff] [blame] | 2281 | /* inlining is important to hardwire a hot branch (template emulation) */ |
| 2282 | FORCE_INLINE |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2283 | size_t ZSTD_HcFindBestMatch_generic ( |
| 2284 | ZSTD_CCtx* zc, /* Index table will be updated */ |
| 2285 | const BYTE* const ip, const BYTE* const iLimit, |
| 2286 | size_t* offsetPtr, |
| 2287 | const U32 maxNbAttempts, const U32 mls, const U32 extDict) |
| 2288 | { |
| 2289 | U32* const chainTable = zc->chainTable; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2290 | const U32 chainSize = (1 << zc->appliedParams.cParams.chainLog); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2291 | const U32 chainMask = chainSize-1; |
| 2292 | const BYTE* const base = zc->base; |
| 2293 | const BYTE* const dictBase = zc->dictBase; |
| 2294 | const U32 dictLimit = zc->dictLimit; |
| 2295 | const BYTE* const prefixStart = base + dictLimit; |
| 2296 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 2297 | const U32 lowLimit = zc->lowLimit; |
| 2298 | const U32 current = (U32)(ip-base); |
| 2299 | const U32 minChain = current > chainSize ? current - chainSize : 0; |
| 2300 | int nbAttempts=maxNbAttempts; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2301 | size_t ml=4-1; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2302 | |
| 2303 | /* HC4 match finder */ |
| 2304 | U32 matchIndex = ZSTD_insertAndFindFirstIndex (zc, ip, mls); |
| 2305 | |
Yann Collet | 22d7632 | 2016-06-21 08:01:51 +0200 | [diff] [blame] | 2306 | for ( ; (matchIndex>lowLimit) & (nbAttempts>0) ; nbAttempts--) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2307 | const BYTE* match; |
| 2308 | size_t currentMl=0; |
| 2309 | if ((!extDict) || matchIndex >= dictLimit) { |
| 2310 | match = base + matchIndex; |
| 2311 | if (match[ml] == ip[ml]) /* potentially better */ |
| 2312 | currentMl = ZSTD_count(ip, match, iLimit); |
| 2313 | } else { |
| 2314 | match = dictBase + matchIndex; |
| 2315 | 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] | 2316 | currentMl = ZSTD_count_2segments(ip+4, match+4, iLimit, dictEnd, prefixStart) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | /* save best solution */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2320 | if (currentMl > ml) { |
| 2321 | ml = currentMl; |
| 2322 | *offsetPtr = current - matchIndex + ZSTD_REP_MOVE; |
| 2323 | if (ip+currentMl == iLimit) break; /* best possible, avoids read overflow on next attempt */ |
| 2324 | } |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2325 | |
| 2326 | if (matchIndex <= minChain) break; |
| 2327 | matchIndex = NEXT_IN_CHAIN(matchIndex, chainMask); |
| 2328 | } |
| 2329 | |
| 2330 | return ml; |
| 2331 | } |
| 2332 | |
| 2333 | |
| 2334 | FORCE_INLINE size_t ZSTD_HcFindBestMatch_selectMLS ( |
| 2335 | ZSTD_CCtx* zc, |
| 2336 | const BYTE* ip, const BYTE* const iLimit, |
| 2337 | size_t* offsetPtr, |
| 2338 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 2339 | { |
| 2340 | switch(matchLengthSearch) |
| 2341 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 2342 | default : /* includes case 3 */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2343 | case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 0); |
| 2344 | 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] | 2345 | case 7 : |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2346 | case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 0); |
| 2347 | } |
| 2348 | } |
| 2349 | |
| 2350 | |
| 2351 | FORCE_INLINE size_t ZSTD_HcFindBestMatch_extDict_selectMLS ( |
| 2352 | ZSTD_CCtx* zc, |
| 2353 | const BYTE* ip, const BYTE* const iLimit, |
| 2354 | size_t* offsetPtr, |
| 2355 | const U32 maxNbAttempts, const U32 matchLengthSearch) |
| 2356 | { |
| 2357 | switch(matchLengthSearch) |
| 2358 | { |
Yann Collet | 933ce4a | 2017-03-29 14:32:15 -0700 | [diff] [blame] | 2359 | default : /* includes case 3 */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2360 | case 4 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 4, 1); |
| 2361 | 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] | 2362 | case 7 : |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2363 | case 6 : return ZSTD_HcFindBestMatch_generic(zc, ip, iLimit, offsetPtr, maxNbAttempts, 6, 1); |
| 2364 | } |
| 2365 | } |
| 2366 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2367 | |
Yann Collet | 287b7d9 | 2015-11-22 13:24:05 +0100 | [diff] [blame] | 2368 | /* ******************************* |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2369 | * Common parser - lazy strategy |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2370 | *********************************/ |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2371 | FORCE_INLINE |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2372 | void ZSTD_compressBlock_lazy_generic(ZSTD_CCtx* ctx, |
| 2373 | const void* src, size_t srcSize, |
| 2374 | const U32 searchMethod, const U32 depth) |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2375 | { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2376 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 2377 | const BYTE* const istart = (const BYTE*)src; |
| 2378 | const BYTE* ip = istart; |
| 2379 | const BYTE* anchor = istart; |
| 2380 | const BYTE* const iend = istart + srcSize; |
| 2381 | const BYTE* const ilimit = iend - 8; |
| 2382 | const BYTE* const base = ctx->base + ctx->dictLimit; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2383 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2384 | U32 const maxSearches = 1 << ctx->appliedParams.cParams.searchLog; |
| 2385 | U32 const mls = ctx->appliedParams.cParams.searchLength; |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2386 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2387 | typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, |
| 2388 | size_t* offsetPtr, |
| 2389 | U32 maxNbAttempts, U32 matchLengthSearch); |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2390 | searchMax_f const searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS : ZSTD_HcFindBestMatch_selectMLS; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2391 | U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1], savedOffset=0; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2392 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2393 | /* init */ |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2394 | ip += (ip==base); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2395 | ctx->nextToUpdate3 = ctx->nextToUpdate; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2396 | { U32 const maxRep = (U32)(ip-base); |
| 2397 | if (offset_2 > maxRep) savedOffset = offset_2, offset_2 = 0; |
| 2398 | if (offset_1 > maxRep) savedOffset = offset_1, offset_1 = 0; |
| 2399 | } |
Yann Collet | 96b9f0b | 2015-11-04 03:52:54 +0100 | [diff] [blame] | 2400 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2401 | /* Match Loop */ |
| 2402 | while (ip < ilimit) { |
| 2403 | size_t matchLength=0; |
| 2404 | size_t offset=0; |
| 2405 | const BYTE* start=ip+1; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2406 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2407 | /* check repCode */ |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2408 | 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] | 2409 | /* repcode : we take it */ |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2410 | matchLength = ZSTD_count(ip+1+4, ip+1+4-offset_1, iend) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2411 | if (depth==0) goto _storeSequence; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2412 | } |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2413 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2414 | /* first search (depth 0) */ |
| 2415 | { size_t offsetFound = 99999999; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2416 | size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2417 | if (ml2 > matchLength) |
inikep | 7571685 | 2016-04-06 12:34:42 +0200 | [diff] [blame] | 2418 | matchLength = ml2, start = ip, offset=offsetFound; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2419 | } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2420 | |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2421 | if (matchLength < 4) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2422 | ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ |
| 2423 | continue; |
| 2424 | } |
| 2425 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2426 | /* let's try to find a better solution */ |
| 2427 | if (depth>=1) |
| 2428 | while (ip<ilimit) { |
| 2429 | ip ++; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2430 | 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] | 2431 | 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] | 2432 | int const gain2 = (int)(mlRep * 3); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2433 | int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2434 | if ((mlRep >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2435 | matchLength = mlRep, offset = 0, start = ip; |
| 2436 | } |
| 2437 | { size_t offset2=99999999; |
| 2438 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2439 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2440 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2441 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2442 | matchLength = ml2, offset = offset2, start = ip; |
| 2443 | continue; /* search a better one */ |
| 2444 | } } |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2445 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2446 | /* let's find an even better one */ |
| 2447 | if ((depth==2) && (ip<ilimit)) { |
| 2448 | ip ++; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2449 | 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] | 2450 | 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] | 2451 | int const gain2 = (int)(ml2 * 4); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2452 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2453 | if ((ml2 >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2454 | matchLength = ml2, offset = 0, start = ip; |
| 2455 | } |
| 2456 | { size_t offset2=99999999; |
| 2457 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2458 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2459 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2460 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2461 | matchLength = ml2, offset = offset2, start = ip; |
| 2462 | continue; |
| 2463 | } } } |
| 2464 | break; /* nothing found : store previous solution */ |
| 2465 | } |
| 2466 | |
| 2467 | /* catch up */ |
| 2468 | if (offset) { |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2469 | while ( (start > anchor) |
| 2470 | && (start > base+offset-ZSTD_REP_MOVE) |
| 2471 | && (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] | 2472 | { start--; matchLength++; } |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2473 | offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2476 | /* store sequence */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2477 | _storeSequence: |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2478 | { size_t const litLength = start - anchor; |
Yann Collet | d57dffb | 2016-07-03 01:48:26 +0200 | [diff] [blame] | 2479 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength-MINMATCH); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2480 | anchor = ip = start + matchLength; |
| 2481 | } |
Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 2482 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2483 | /* check immediate repcode */ |
| 2484 | while ( (ip <= ilimit) |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2485 | && ((offset_2>0) |
| 2486 | & (MEM_read32(ip) == MEM_read32(ip - offset_2)) )) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2487 | /* store sequence */ |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2488 | matchLength = ZSTD_count(ip+4, ip+4-offset_2, iend) + 4; |
Yann Collet | 9634f67 | 2016-07-03 01:23:58 +0200 | [diff] [blame] | 2489 | offset = offset_2; offset_2 = offset_1; offset_1 = (U32)offset; /* swap repcodes */ |
inikep | 7bc19b6 | 2016-04-06 09:46:01 +0200 | [diff] [blame] | 2490 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength-MINMATCH); |
| 2491 | ip += matchLength; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2492 | anchor = ip; |
| 2493 | continue; /* faster when present ... (?) */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2494 | } } |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2495 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2496 | /* Save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 2497 | ctx->repToConfirm[0] = offset_1 ? offset_1 : savedOffset; |
| 2498 | ctx->repToConfirm[1] = offset_2 ? offset_2 : savedOffset; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2499 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2500 | /* Last Literals */ |
| 2501 | { size_t const lastLLSize = iend - anchor; |
| 2502 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 2503 | seqStorePtr->lit += lastLLSize; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2504 | } |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2505 | } |
| 2506 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2507 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2508 | static void ZSTD_compressBlock_btlazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2509 | { |
| 2510 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 1, 2); |
| 2511 | } |
| 2512 | |
| 2513 | static void ZSTD_compressBlock_lazy2(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2514 | { |
| 2515 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 2); |
| 2516 | } |
| 2517 | |
| 2518 | static void ZSTD_compressBlock_lazy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2519 | { |
| 2520 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 1); |
| 2521 | } |
| 2522 | |
| 2523 | static void ZSTD_compressBlock_greedy(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2524 | { |
| 2525 | ZSTD_compressBlock_lazy_generic(ctx, src, srcSize, 0, 0); |
| 2526 | } |
| 2527 | |
| 2528 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2529 | FORCE_INLINE |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2530 | void ZSTD_compressBlock_lazy_extDict_generic(ZSTD_CCtx* ctx, |
| 2531 | const void* src, size_t srcSize, |
| 2532 | const U32 searchMethod, const U32 depth) |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2533 | { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2534 | seqStore_t* seqStorePtr = &(ctx->seqStore); |
| 2535 | const BYTE* const istart = (const BYTE*)src; |
| 2536 | const BYTE* ip = istart; |
| 2537 | const BYTE* anchor = istart; |
| 2538 | const BYTE* const iend = istart + srcSize; |
| 2539 | const BYTE* const ilimit = iend - 8; |
| 2540 | const BYTE* const base = ctx->base; |
| 2541 | const U32 dictLimit = ctx->dictLimit; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2542 | const U32 lowestIndex = ctx->lowLimit; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2543 | const BYTE* const prefixStart = base + dictLimit; |
| 2544 | const BYTE* const dictBase = ctx->dictBase; |
| 2545 | const BYTE* const dictEnd = dictBase + dictLimit; |
| 2546 | const BYTE* const dictStart = dictBase + ctx->lowLimit; |
| 2547 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2548 | const U32 maxSearches = 1 << ctx->appliedParams.cParams.searchLog; |
| 2549 | const U32 mls = ctx->appliedParams.cParams.searchLength; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2550 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2551 | typedef size_t (*searchMax_f)(ZSTD_CCtx* zc, const BYTE* ip, const BYTE* iLimit, |
| 2552 | size_t* offsetPtr, |
| 2553 | U32 maxNbAttempts, U32 matchLengthSearch); |
| 2554 | searchMax_f searchMax = searchMethod ? ZSTD_BtFindBestMatch_selectMLS_extDict : ZSTD_HcFindBestMatch_extDict_selectMLS; |
| 2555 | |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2556 | U32 offset_1 = ctx->rep[0], offset_2 = ctx->rep[1]; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2557 | |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2558 | /* init */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2559 | ctx->nextToUpdate3 = ctx->nextToUpdate; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2560 | ip += (ip == prefixStart); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2561 | |
| 2562 | /* Match Loop */ |
| 2563 | while (ip < ilimit) { |
| 2564 | size_t matchLength=0; |
| 2565 | size_t offset=0; |
| 2566 | const BYTE* start=ip+1; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2567 | U32 current = (U32)(ip-base); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2568 | |
| 2569 | /* check repCode */ |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2570 | { const U32 repIndex = (U32)(current+1 - offset_1); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2571 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2572 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2573 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2574 | if (MEM_read32(ip+1) == MEM_read32(repMatch)) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2575 | /* repcode detected we should take it */ |
| 2576 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2577 | matchLength = ZSTD_count_2segments(ip+1+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2578 | if (depth==0) goto _storeSequence; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2579 | } } |
| 2580 | |
| 2581 | /* first search (depth 0) */ |
| 2582 | { size_t offsetFound = 99999999; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2583 | size_t const ml2 = searchMax(ctx, ip, iend, &offsetFound, maxSearches, mls); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2584 | if (ml2 > matchLength) |
inikep | 7571685 | 2016-04-06 12:34:42 +0200 | [diff] [blame] | 2585 | matchLength = ml2, start = ip, offset=offsetFound; |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2586 | } |
| 2587 | |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2588 | if (matchLength < 4) { |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2589 | ip += ((ip-anchor) >> g_searchStrength) + 1; /* jump faster over incompressible sections */ |
| 2590 | continue; |
| 2591 | } |
| 2592 | |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2593 | /* let's try to find a better solution */ |
| 2594 | if (depth>=1) |
| 2595 | while (ip<ilimit) { |
| 2596 | ip ++; |
| 2597 | current++; |
| 2598 | /* check repCode */ |
| 2599 | if (offset) { |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2600 | const U32 repIndex = (U32)(current - offset_1); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2601 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2602 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2603 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2604 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
| 2605 | /* repcode detected */ |
| 2606 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2607 | 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] | 2608 | int const gain2 = (int)(repLength * 3); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2609 | int const gain1 = (int)(matchLength*3 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2610 | if ((repLength >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2611 | matchLength = repLength, offset = 0, start = ip; |
| 2612 | } } |
| 2613 | |
| 2614 | /* search match, depth 1 */ |
| 2615 | { size_t offset2=99999999; |
| 2616 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2617 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2618 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 4); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2619 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2620 | matchLength = ml2, offset = offset2, start = ip; |
| 2621 | continue; /* search a better one */ |
| 2622 | } } |
| 2623 | |
| 2624 | /* let's find an even better one */ |
| 2625 | if ((depth==2) && (ip<ilimit)) { |
| 2626 | ip ++; |
| 2627 | current++; |
| 2628 | /* check repCode */ |
| 2629 | if (offset) { |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2630 | const U32 repIndex = (U32)(current - offset_1); |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2631 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2632 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2633 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2634 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
| 2635 | /* repcode detected */ |
| 2636 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2637 | 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] | 2638 | int const gain2 = (int)(repLength * 4); |
| 2639 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 1); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2640 | if ((repLength >= 4) && (gain2 > gain1)) |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2641 | matchLength = repLength, offset = 0, start = ip; |
| 2642 | } } |
| 2643 | |
| 2644 | /* search match, depth 2 */ |
| 2645 | { size_t offset2=99999999; |
| 2646 | size_t const ml2 = searchMax(ctx, ip, iend, &offset2, maxSearches, mls); |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 2647 | int const gain2 = (int)(ml2*4 - ZSTD_highbit32((U32)offset2+1)); /* raw approx */ |
| 2648 | int const gain1 = (int)(matchLength*4 - ZSTD_highbit32((U32)offset+1) + 7); |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2649 | if ((ml2 >= 4) && (gain2 > gain1)) { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2650 | matchLength = ml2, offset = offset2, start = ip; |
| 2651 | continue; |
| 2652 | } } } |
| 2653 | break; /* nothing found : store previous solution */ |
| 2654 | } |
| 2655 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2656 | /* catch up */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2657 | if (offset) { |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2658 | U32 const matchIndex = (U32)((start-base) - (offset - ZSTD_REP_MOVE)); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2659 | const BYTE* match = (matchIndex < dictLimit) ? dictBase + matchIndex : base + matchIndex; |
| 2660 | const BYTE* const mStart = (matchIndex < dictLimit) ? dictStart : prefixStart; |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2661 | 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] | 2662 | offset_2 = offset_1; offset_1 = (U32)(offset - ZSTD_REP_MOVE); |
Yann Collet | 4853716 | 2016-04-07 15:24:29 +0200 | [diff] [blame] | 2663 | } |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2664 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2665 | /* store sequence */ |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2666 | _storeSequence: |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2667 | { size_t const litLength = start - anchor; |
Yann Collet | d57dffb | 2016-07-03 01:48:26 +0200 | [diff] [blame] | 2668 | ZSTD_storeSeq(seqStorePtr, litLength, anchor, (U32)offset, matchLength-MINMATCH); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2669 | anchor = ip = start + matchLength; |
| 2670 | } |
| 2671 | |
| 2672 | /* check immediate repcode */ |
| 2673 | while (ip <= ilimit) { |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2674 | const U32 repIndex = (U32)((ip-base) - offset_2); |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2675 | const BYTE* const repBase = repIndex < dictLimit ? dictBase : base; |
| 2676 | const BYTE* const repMatch = repBase + repIndex; |
Yann Collet | 43dfe01 | 2016-06-13 21:43:06 +0200 | [diff] [blame] | 2677 | if (((U32)((dictLimit-1) - repIndex) >= 3) & (repIndex > lowestIndex)) /* intentional overflow */ |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2678 | if (MEM_read32(ip) == MEM_read32(repMatch)) { |
| 2679 | /* repcode detected we should take it */ |
| 2680 | const BYTE* const repEnd = repIndex < dictLimit ? dictEnd : iend; |
Yann Collet | e42afbc | 2017-04-26 11:39:35 -0700 | [diff] [blame] | 2681 | matchLength = ZSTD_count_2segments(ip+4, repMatch+4, iend, repEnd, prefixStart) + 4; |
Yann Collet | 302ff03 | 2016-07-03 01:28:16 +0200 | [diff] [blame] | 2682 | 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] | 2683 | ZSTD_storeSeq(seqStorePtr, 0, anchor, 0, matchLength-MINMATCH); |
| 2684 | ip += matchLength; |
| 2685 | anchor = ip; |
| 2686 | continue; /* faster when present ... (?) */ |
| 2687 | } |
| 2688 | break; |
| 2689 | } } |
| 2690 | |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2691 | /* Save reps for next block */ |
Yann Collet | b459aad | 2017-01-19 17:33:37 -0800 | [diff] [blame] | 2692 | ctx->repToConfirm[0] = offset_1; ctx->repToConfirm[1] = offset_2; |
Yann Collet | 4266c0a | 2016-06-14 01:49:25 +0200 | [diff] [blame] | 2693 | |
inikep | faa8d8a | 2016-04-05 19:01:10 +0200 | [diff] [blame] | 2694 | /* Last Literals */ |
| 2695 | { size_t const lastLLSize = iend - anchor; |
| 2696 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 2697 | seqStorePtr->lit += lastLLSize; |
Yann Collet | 5106a76 | 2015-11-05 15:00:24 +0100 | [diff] [blame] | 2698 | } |
| 2699 | } |
| 2700 | |
| 2701 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2702 | 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] | 2703 | { |
inikep | 64d7bcb | 2016-04-07 19:14:09 +0200 | [diff] [blame] | 2704 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 0); |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 2705 | } |
| 2706 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2707 | 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] | 2708 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 2709 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 1); |
Yann Collet | b7fc88e | 2015-11-22 03:12:28 +0100 | [diff] [blame] | 2710 | } |
Yann Collet | 9a24e59 | 2015-11-22 02:53:43 +0100 | [diff] [blame] | 2711 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2712 | 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] | 2713 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 2714 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 0, 2); |
Yann Collet | a85c77b | 2015-11-22 12:22:04 +0100 | [diff] [blame] | 2715 | } |
| 2716 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2717 | 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] | 2718 | { |
Yann Collet | a1249dc | 2016-01-25 04:22:03 +0100 | [diff] [blame] | 2719 | ZSTD_compressBlock_lazy_extDict_generic(ctx, src, srcSize, 1, 2); |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 2720 | } |
| 2721 | |
inikep | ef51941 | 2016-04-21 11:08:43 +0200 | [diff] [blame] | 2722 | |
inikep | ef51941 | 2016-04-21 11:08:43 +0200 | [diff] [blame] | 2723 | /* The optimal parser */ |
| 2724 | #include "zstd_opt.h" |
| 2725 | |
| 2726 | static void ZSTD_compressBlock_btopt(ZSTD_CCtx* ctx, const void* src, size_t srcSize) |
| 2727 | { |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2728 | #ifdef ZSTD_OPT_H_91842398743 |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 2729 | ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 0); |
| 2730 | #else |
| 2731 | (void)ctx; (void)src; (void)srcSize; |
| 2732 | return; |
| 2733 | #endif |
| 2734 | } |
| 2735 | |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 2736 | 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] | 2737 | { |
| 2738 | #ifdef ZSTD_OPT_H_91842398743 |
| 2739 | ZSTD_compressBlock_opt_generic(ctx, src, srcSize, 1); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2740 | #else |
| 2741 | (void)ctx; (void)src; (void)srcSize; |
| 2742 | return; |
| 2743 | #endif |
inikep | ef51941 | 2016-04-21 11:08:43 +0200 | [diff] [blame] | 2744 | } |
| 2745 | |
inikep | d3b8d7a | 2016-02-22 10:06:17 +0100 | [diff] [blame] | 2746 | 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] | 2747 | { |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2748 | #ifdef ZSTD_OPT_H_91842398743 |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 2749 | ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 0); |
| 2750 | #else |
| 2751 | (void)ctx; (void)src; (void)srcSize; |
| 2752 | return; |
| 2753 | #endif |
| 2754 | } |
| 2755 | |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 2756 | 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] | 2757 | { |
| 2758 | #ifdef ZSTD_OPT_H_91842398743 |
| 2759 | ZSTD_compressBlock_opt_extDict_generic(ctx, src, srcSize, 1); |
Yann Collet | d4f4e58 | 2016-06-27 01:31:35 +0200 | [diff] [blame] | 2760 | #else |
| 2761 | (void)ctx; (void)src; (void)srcSize; |
| 2762 | return; |
| 2763 | #endif |
inikep | e2bfe24 | 2016-01-31 11:25:48 +0100 | [diff] [blame] | 2764 | } |
| 2765 | |
Yann Collet | 7a23179 | 2015-11-21 15:27:35 +0100 | [diff] [blame] | 2766 | |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 2767 | /* ZSTD_selectBlockCompressor() : |
| 2768 | * assumption : strat is a valid strategy */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 2769 | typedef void (*ZSTD_blockCompressor) (ZSTD_CCtx* ctx, const void* src, size_t srcSize); |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2770 | static ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 2771 | { |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 2772 | static const ZSTD_blockCompressor blockCompressor[2][(unsigned)ZSTD_btultra+1] = { |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 2773 | { ZSTD_compressBlock_fast /* default for 0 */, |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 2774 | ZSTD_compressBlock_fast, ZSTD_compressBlock_doubleFast, ZSTD_compressBlock_greedy, |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2775 | ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, |
Nick Terrell | 5f2c721 | 2017-05-10 16:49:58 -0700 | [diff] [blame] | 2776 | ZSTD_compressBlock_btopt, ZSTD_compressBlock_btultra }, |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 2777 | { ZSTD_compressBlock_fast_extDict /* default for 0 */, |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 2778 | ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_doubleFast_extDict, ZSTD_compressBlock_greedy_extDict, |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2779 | ZSTD_compressBlock_lazy_extDict,ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, |
Nick Terrell | 5f2c721 | 2017-05-10 16:49:58 -0700 | [diff] [blame] | 2780 | ZSTD_compressBlock_btopt_extDict, ZSTD_compressBlock_btultra_extDict } |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 2781 | }; |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 2782 | ZSTD_STATIC_ASSERT((unsigned)ZSTD_fast == 1); |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 2783 | assert((U32)strat >= (U32)ZSTD_fast); |
| 2784 | assert((U32)strat <= (U32)ZSTD_btultra); |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 2785 | |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 2786 | return blockCompressor[extDict!=0][(U32)strat]; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 2787 | } |
| 2788 | |
| 2789 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2790 | 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] | 2791 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2792 | 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] | 2793 | const BYTE* const base = zc->base; |
| 2794 | const BYTE* const istart = (const BYTE*)src; |
| 2795 | const U32 current = (U32)(istart-base); |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2796 | 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] | 2797 | ZSTD_resetSeqStore(&(zc->seqStore)); |
inikep | 98e08cb | 2016-08-10 15:00:30 +0200 | [diff] [blame] | 2798 | if (current > zc->nextToUpdate + 384) |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2799 | 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] | 2800 | blockCompressor(zc, src, srcSize); |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2801 | return ZSTD_compressSequences(zc, dst, dstCapacity, srcSize); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 2802 | } |
| 2803 | |
| 2804 | |
Yann Collet | db8e21d | 2017-05-12 13:46:49 -0700 | [diff] [blame] | 2805 | /*! ZSTD_compress_frameChunk() : |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2806 | * Compress a chunk of data into one or multiple blocks. |
| 2807 | * All blocks will be terminated, all input will be consumed. |
| 2808 | * Function will issue an error if there is not enough `dstCapacity` to hold the compressed content. |
| 2809 | * Frame is supposed already started (header already produced) |
| 2810 | * @return : compressed size, or an error code |
| 2811 | */ |
Yann Collet | db8e21d | 2017-05-12 13:46:49 -0700 | [diff] [blame] | 2812 | static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx, |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2813 | void* dst, size_t dstCapacity, |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2814 | const void* src, size_t srcSize, |
| 2815 | U32 lastFrameChunk) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2816 | { |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2817 | size_t blockSize = cctx->blockSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2818 | size_t remaining = srcSize; |
| 2819 | const BYTE* ip = (const BYTE*)src; |
| 2820 | BYTE* const ostart = (BYTE*)dst; |
| 2821 | BYTE* op = ostart; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2822 | U32 const maxDist = 1 << cctx->appliedParams.cParams.windowLog; |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 2823 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2824 | if (cctx->appliedParams.fParams.checksumFlag && srcSize) |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2825 | XXH64_update(&cctx->xxhState, src, srcSize); |
| 2826 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2827 | while (remaining) { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2828 | U32 const lastBlock = lastFrameChunk & (blockSize >= remaining); |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 2829 | size_t cSize; |
| 2830 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2831 | if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE) |
| 2832 | return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */ |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 2833 | if (remaining < blockSize) blockSize = remaining; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2834 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2835 | /* preemptive overflow correction */ |
Sean Purcell | 881abe4 | 2017-03-07 16:52:23 -0800 | [diff] [blame] | 2836 | if (cctx->lowLimit > (3U<<29)) { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2837 | 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] | 2838 | U32 const current = (U32)(ip - cctx->base); |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2839 | U32 const newCurrent = (current & cycleMask) + (1 << cctx->appliedParams.cParams.windowLog); |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 2840 | U32 const correction = current - newCurrent; |
| 2841 | ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_64 <= 30); |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2842 | ZSTD_reduceIndex(cctx, correction); |
| 2843 | cctx->base += correction; |
| 2844 | cctx->dictBase += correction; |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 2845 | cctx->lowLimit -= correction; |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2846 | cctx->dictLimit -= correction; |
| 2847 | if (cctx->nextToUpdate < correction) cctx->nextToUpdate = 0; |
| 2848 | else cctx->nextToUpdate -= correction; |
| 2849 | } |
| 2850 | |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 2851 | if ((U32)(ip+blockSize - cctx->base) > cctx->loadedDictEnd + maxDist) { |
Yann Collet | 70e4577 | 2016-03-19 18:08:32 +0100 | [diff] [blame] | 2852 | /* enforce maxDist */ |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2853 | U32 const newLowLimit = (U32)(ip+blockSize - cctx->base) - maxDist; |
| 2854 | if (cctx->lowLimit < newLowLimit) cctx->lowLimit = newLowLimit; |
| 2855 | if (cctx->dictLimit < cctx->lowLimit) cctx->dictLimit = cctx->lowLimit; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 2856 | } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2857 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2858 | cSize = ZSTD_compressBlock_internal(cctx, op+ZSTD_blockHeaderSize, dstCapacity-ZSTD_blockHeaderSize, ip, blockSize); |
inikep | fb5df61 | 2016-05-24 15:36:37 +0200 | [diff] [blame] | 2859 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2860 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2861 | if (cSize == 0) { /* block is not compressible */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2862 | U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(blockSize << 3); |
| 2863 | if (blockSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); |
| 2864 | MEM_writeLE32(op, cBlockHeader24); /* no pb, 4th byte will be overwritten */ |
| 2865 | memcpy(op + ZSTD_blockHeaderSize, ip, blockSize); |
| 2866 | cSize = ZSTD_blockHeaderSize+blockSize; |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 2867 | } else { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2868 | U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3); |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 2869 | MEM_writeLE24(op, cBlockHeader24); |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2870 | cSize += ZSTD_blockHeaderSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | remaining -= blockSize; |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2874 | dstCapacity -= cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2875 | ip += blockSize; |
| 2876 | op += cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2877 | } |
| 2878 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2879 | if (lastFrameChunk && (op>ostart)) cctx->stage = ZSTDcs_ending; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2880 | return op-ostart; |
| 2881 | } |
| 2882 | |
| 2883 | |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2884 | static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity, |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2885 | ZSTD_parameters params, U64 pledgedSrcSize, U32 dictID) |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2886 | { BYTE* const op = (BYTE*)dst; |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 2887 | U32 const dictIDSizeCodeLength = (dictID>0) + (dictID>=256) + (dictID>=65536); /* 0-3 */ |
| 2888 | U32 const dictIDSizeCode = params.fParams.noDictIDFlag ? 0 : dictIDSizeCodeLength; /* 0-3 */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2889 | U32 const checksumFlag = params.fParams.checksumFlag>0; |
| 2890 | U32 const windowSize = 1U << params.cParams.windowLog; |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 2891 | U32 const singleSegment = params.fParams.contentSizeFlag && (windowSize >= pledgedSrcSize); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2892 | BYTE const windowLogByte = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) << 3); |
| 2893 | U32 const fcsCode = params.fParams.contentSizeFlag ? |
Nick Terrell | 55fc1f9 | 2017-05-24 13:50:10 -0700 | [diff] [blame] | 2894 | (pledgedSrcSize>=256) + (pledgedSrcSize>=65536+256) + (pledgedSrcSize>=0xFFFFFFFFU) : 0; /* 0-3 */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2895 | BYTE const frameHeaderDecriptionByte = (BYTE)(dictIDSizeCode + (checksumFlag<<2) + (singleSegment<<5) + (fcsCode<<6) ); |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2896 | size_t pos; |
| 2897 | |
| 2898 | if (dstCapacity < ZSTD_frameHeaderSize_max) return ERROR(dstSize_tooSmall); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2899 | DEBUGLOG(5, "ZSTD_writeFrameHeader : dictIDFlag : %u ; dictID : %u ; dictIDSizeCode : %u", |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 2900 | !params.fParams.noDictIDFlag, dictID, dictIDSizeCode); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2901 | |
| 2902 | MEM_writeLE32(dst, ZSTD_MAGICNUMBER); |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 2903 | op[4] = frameHeaderDecriptionByte; pos=5; |
Eric Biggers | e4d0265 | 2016-07-26 10:42:19 -0700 | [diff] [blame] | 2904 | if (!singleSegment) op[pos++] = windowLogByte; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2905 | switch(dictIDSizeCode) |
| 2906 | { |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 2907 | default: assert(0); /* impossible */ |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2908 | case 0 : break; |
| 2909 | case 1 : op[pos] = (BYTE)(dictID); pos++; break; |
Yann Collet | d4180ca | 2016-07-27 21:21:36 +0200 | [diff] [blame] | 2910 | case 2 : MEM_writeLE16(op+pos, (U16)dictID); pos+=2; break; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2911 | case 3 : MEM_writeLE32(op+pos, dictID); pos+=4; break; |
| 2912 | } |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 2913 | switch(fcsCode) |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2914 | { |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 2915 | default: assert(0); /* impossible */ |
Eric Biggers | e4d0265 | 2016-07-26 10:42:19 -0700 | [diff] [blame] | 2916 | case 0 : if (singleSegment) op[pos++] = (BYTE)(pledgedSrcSize); break; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 2917 | case 1 : MEM_writeLE16(op+pos, (U16)(pledgedSrcSize-256)); pos+=2; break; |
| 2918 | case 2 : MEM_writeLE32(op+pos, (U32)(pledgedSrcSize)); pos+=4; break; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2919 | case 3 : MEM_writeLE64(op+pos, (U64)(pledgedSrcSize)); pos+=8; break; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2920 | } |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2921 | return pos; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2925 | static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 2926 | void* dst, size_t dstCapacity, |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2927 | const void* src, size_t srcSize, |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2928 | U32 frame, U32 lastFrameChunk) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2929 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2930 | const BYTE* const ip = (const BYTE*) src; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2931 | size_t fhSize = 0; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2932 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2933 | 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] | 2934 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2935 | if (frame && (cctx->stage==ZSTDcs_init)) { |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 2936 | fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, |
| 2937 | cctx->pledgedSrcSizePlusOne-1, cctx->dictID); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2938 | if (ZSTD_isError(fhSize)) return fhSize; |
| 2939 | dstCapacity -= fhSize; |
| 2940 | dst = (char*)dst + fhSize; |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2941 | cctx->stage = ZSTDcs_ongoing; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2942 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2943 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2944 | /* Check if blocks follow each other */ |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2945 | if (src != cctx->nextSrc) { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 2946 | /* not contiguous */ |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2947 | ptrdiff_t const delta = cctx->nextSrc - ip; |
| 2948 | cctx->lowLimit = cctx->dictLimit; |
| 2949 | cctx->dictLimit = (U32)(cctx->nextSrc - cctx->base); |
| 2950 | cctx->dictBase = cctx->base; |
| 2951 | cctx->base -= delta; |
| 2952 | cctx->nextToUpdate = cctx->dictLimit; |
| 2953 | 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] | 2954 | } |
| 2955 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2956 | /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */ |
| 2957 | if ((ip+srcSize > cctx->dictBase + cctx->lowLimit) & (ip < cctx->dictBase + cctx->dictLimit)) { |
| 2958 | ptrdiff_t const highInputIdx = (ip + srcSize) - cctx->dictBase; |
| 2959 | U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)cctx->dictLimit) ? cctx->dictLimit : (U32)highInputIdx; |
| 2960 | cctx->lowLimit = lowLimitMax; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2961 | } |
| 2962 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2963 | cctx->nextSrc = ip + srcSize; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 2964 | |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 2965 | if (srcSize) { |
| 2966 | size_t const cSize = frame ? |
Yann Collet | db8e21d | 2017-05-12 13:46:49 -0700 | [diff] [blame] | 2967 | ZSTD_compress_frameChunk (cctx, dst, dstCapacity, src, srcSize, lastFrameChunk) : |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 2968 | ZSTD_compressBlock_internal (cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2969 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2970 | cctx->consumedSrcSize += srcSize; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2971 | return cSize + fhSize; |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 2972 | } else |
| 2973 | return fhSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2974 | } |
| 2975 | |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2976 | |
Yann Collet | 5b56739 | 2016-07-28 01:17:22 +0200 | [diff] [blame] | 2977 | size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 2978 | void* dst, size_t dstCapacity, |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2979 | const void* src, size_t srcSize) |
| 2980 | { |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2981 | 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] | 2982 | } |
| 2983 | |
| 2984 | |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 2985 | size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 2986 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 2987 | U32 const cLevel = cctx->compressionLevel; |
| 2988 | ZSTD_compressionParameters cParams = (cLevel == ZSTD_CLEVEL_CUSTOM) ? |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2989 | cctx->appliedParams.cParams : |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 2990 | ZSTD_getCParams(cLevel, 0, 0); |
| 2991 | return MIN (ZSTD_BLOCKSIZE_MAX, 1 << cParams.windowLog); |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 2992 | } |
| 2993 | |
| 2994 | size_t ZSTD_compressBlock(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
| 2995 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 2996 | size_t const blockSizeMax = ZSTD_getBlockSize(cctx); |
Yann Collet | 961b6a0 | 2016-07-15 11:56:53 +0200 | [diff] [blame] | 2997 | if (srcSize > blockSizeMax) return ERROR(srcSize_wrong); |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 2998 | 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] | 2999 | } |
| 3000 | |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3001 | /*! ZSTD_loadDictionaryContent() : |
| 3002 | * @return : 0, or an error code |
| 3003 | */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 3004 | 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] | 3005 | { |
| 3006 | const BYTE* const ip = (const BYTE*) src; |
| 3007 | const BYTE* const iend = ip + srcSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 3008 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3009 | /* input becomes current prefix */ |
| 3010 | zc->lowLimit = zc->dictLimit; |
| 3011 | zc->dictLimit = (U32)(zc->nextSrc - zc->base); |
| 3012 | zc->dictBase = zc->base; |
| 3013 | zc->base += ip - zc->nextSrc; |
| 3014 | zc->nextToUpdate = zc->dictLimit; |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 3015 | zc->loadedDictEnd = zc->forceWindow ? 0 : (U32)(iend - zc->base); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3016 | |
| 3017 | zc->nextSrc = iend; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 3018 | if (srcSize <= HASH_READ_SIZE) return 0; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3019 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3020 | switch(zc->appliedParams.cParams.strategy) |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3021 | { |
| 3022 | case ZSTD_fast: |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3023 | ZSTD_fillHashTable (zc, iend, zc->appliedParams.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3024 | break; |
| 3025 | |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 3026 | case ZSTD_dfast: |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3027 | ZSTD_fillDoubleHashTable (zc, iend, zc->appliedParams.cParams.searchLength); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 3028 | break; |
| 3029 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3030 | case ZSTD_greedy: |
| 3031 | case ZSTD_lazy: |
| 3032 | case ZSTD_lazy2: |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3033 | if (srcSize >= HASH_READ_SIZE) |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3034 | ZSTD_insertAndFindFirstIndex(zc, iend-HASH_READ_SIZE, zc->appliedParams.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3035 | break; |
| 3036 | |
| 3037 | case ZSTD_btlazy2: |
Yann Collet | cefef8c | 2016-02-15 07:21:54 +0100 | [diff] [blame] | 3038 | case ZSTD_btopt: |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 3039 | case ZSTD_btultra: |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3040 | if (srcSize >= HASH_READ_SIZE) |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3041 | 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] | 3042 | break; |
| 3043 | |
| 3044 | default: |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 3045 | assert(0); /* not possible : not a valid strategy id */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3046 | } |
| 3047 | |
Nick Terrell | ecf90ca | 2017-02-13 18:27:34 -0800 | [diff] [blame] | 3048 | zc->nextToUpdate = (U32)(iend - zc->base); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 3049 | return 0; |
| 3050 | } |
| 3051 | |
| 3052 | |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3053 | /* Dictionaries that assign zero probability to symbols that show up causes problems |
| 3054 | when FSE encoding. Refuse dictionaries that assign zero probability to symbols |
| 3055 | that we may encounter during compression. |
| 3056 | NOTE: This behavior is not standard and could be improved in the future. */ |
| 3057 | static size_t ZSTD_checkDictNCount(short* normalizedCounter, unsigned dictMaxSymbolValue, unsigned maxSymbolValue) { |
| 3058 | U32 s; |
| 3059 | if (dictMaxSymbolValue < maxSymbolValue) return ERROR(dictionary_corrupted); |
| 3060 | for (s = 0; s <= maxSymbolValue; ++s) { |
| 3061 | if (normalizedCounter[s] == 0) return ERROR(dictionary_corrupted); |
| 3062 | } |
| 3063 | return 0; |
| 3064 | } |
| 3065 | |
| 3066 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 3067 | /* Dictionary format : |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 3068 | * See : |
| 3069 | * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#dictionary-format |
| 3070 | */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3071 | /*! ZSTD_loadZstdDictionary() : |
| 3072 | * @return : 0, or an error code |
| 3073 | * assumptions : magic number supposed already checked |
| 3074 | * dictSize supposed > 8 |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 3075 | */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3076 | 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] | 3077 | { |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3078 | const BYTE* dictPtr = (const BYTE*)dict; |
| 3079 | const BYTE* const dictEnd = dictPtr + dictSize; |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3080 | short offcodeNCount[MaxOff+1]; |
| 3081 | unsigned offcodeMaxValue = MaxOff; |
Yann Collet | 643d9a2 | 2016-12-01 16:24:04 -0800 | [diff] [blame] | 3082 | BYTE scratchBuffer[1<<MAX(MLFSELog,LLFSELog)]; |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 3083 | |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 3084 | dictPtr += 4; /* skip magic number */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3085 | cctx->dictID = cctx->appliedParams.fParams.noDictIDFlag ? 0 : MEM_readLE32(dictPtr); |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 3086 | dictPtr += 4; |
| 3087 | |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 3088 | { size_t const hufHeaderSize = HUF_readCTable(cctx->hufCTable, 255, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3089 | if (HUF_isError(hufHeaderSize)) return ERROR(dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3090 | dictPtr += hufHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3091 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 3092 | |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3093 | { unsigned offcodeLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3094 | size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3095 | if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 3096 | if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3097 | /* 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] | 3098 | CHECK_E( FSE_buildCTable_wksp(cctx->offcodeCTable, offcodeNCount, offcodeMaxValue, offcodeLog, scratchBuffer, sizeof(scratchBuffer)), |
| 3099 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3100 | dictPtr += offcodeHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3101 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 3102 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3103 | { short matchlengthNCount[MaxML+1]; |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 3104 | unsigned matchlengthMaxValue = MaxML, matchlengthLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3105 | size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3106 | if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 3107 | if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3108 | /* Every match length code must have non-zero probability */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3109 | CHECK_F( ZSTD_checkDictNCount(matchlengthNCount, matchlengthMaxValue, MaxML)); |
| 3110 | CHECK_E( FSE_buildCTable_wksp(cctx->matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, scratchBuffer, sizeof(scratchBuffer)), |
| 3111 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3112 | dictPtr += matchlengthHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3113 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 3114 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3115 | { short litlengthNCount[MaxLL+1]; |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 3116 | unsigned litlengthMaxValue = MaxLL, litlengthLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3117 | size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3118 | if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 3119 | if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3120 | /* Every literal length code must have non-zero probability */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3121 | CHECK_F( ZSTD_checkDictNCount(litlengthNCount, litlengthMaxValue, MaxLL)); |
| 3122 | CHECK_E( FSE_buildCTable_wksp(cctx->litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog, scratchBuffer, sizeof(scratchBuffer)), |
| 3123 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3124 | dictPtr += litlengthHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3125 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 3126 | |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3127 | if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted); |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3128 | cctx->rep[0] = MEM_readLE32(dictPtr+0); |
| 3129 | cctx->rep[1] = MEM_readLE32(dictPtr+4); |
| 3130 | cctx->rep[2] = MEM_readLE32(dictPtr+8); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 3131 | dictPtr += 12; |
| 3132 | |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 3133 | { size_t const dictContentSize = (size_t)(dictEnd - dictPtr); |
| 3134 | U32 offcodeMax = MaxOff; |
| 3135 | if (dictContentSize <= ((U32)-1) - 128 KB) { |
| 3136 | U32 const maxOffset = (U32)dictContentSize + 128 KB; /* The maximum offset that must be supported */ |
| 3137 | offcodeMax = ZSTD_highbit32(maxOffset); /* Calculate minimum offset code required to represent maxOffset */ |
Nick Terrell | b2c39a2 | 2016-10-24 14:11:27 -0700 | [diff] [blame] | 3138 | } |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 3139 | /* All offset values <= dictContentSize + 128 KB must be representable */ |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3140 | CHECK_F (ZSTD_checkDictNCount(offcodeNCount, offcodeMaxValue, MIN(offcodeMax, MaxOff))); |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 3141 | /* All repCodes must be <= dictContentSize and != 0*/ |
| 3142 | { U32 u; |
| 3143 | for (u=0; u<3; u++) { |
| 3144 | if (cctx->rep[u] == 0) return ERROR(dictionary_corrupted); |
| 3145 | if (cctx->rep[u] > dictContentSize) return ERROR(dictionary_corrupted); |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3146 | } } |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 3147 | |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 3148 | cctx->fseCTables_ready = 1; |
| 3149 | cctx->hufCTable_repeatMode = HUF_repeat_valid; |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3150 | return ZSTD_loadDictionaryContent(cctx, dictPtr, dictContentSize); |
| 3151 | } |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 3152 | } |
| 3153 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 3154 | /** ZSTD_compress_insertDictionary() : |
| 3155 | * @return : 0, or an error code */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3156 | static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* cctx, |
| 3157 | const void* dict, size_t dictSize, |
| 3158 | ZSTD_dictMode_e dictMode) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 3159 | { |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 3160 | if ((dict==NULL) || (dictSize<=8)) return 0; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 3161 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3162 | /* dict restricted modes */ |
| 3163 | if (dictMode==ZSTD_dm_rawContent) |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3164 | return ZSTD_loadDictionaryContent(cctx, dict, dictSize); |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 3165 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3166 | if (MEM_readLE32(dict) != ZSTD_DICT_MAGIC) { |
| 3167 | if (dictMode == ZSTD_dm_auto) |
| 3168 | return ZSTD_loadDictionaryContent(cctx, dict, dictSize); |
| 3169 | if (dictMode == ZSTD_dm_fullDict) |
| 3170 | return ERROR(dictionary_wrong); |
| 3171 | assert(0); /* impossible */ |
| 3172 | } |
| 3173 | |
| 3174 | /* dict as full zstd dictionary */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 3175 | return ZSTD_loadZstdDictionary(cctx, dict, dictSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 3176 | } |
| 3177 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 3178 | /*! ZSTD_compressBegin_internal() : |
Yann Collet | c3bce24 | 2017-06-20 16:09:11 -0700 | [diff] [blame] | 3179 | * @return : 0, or an error code */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3180 | static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 3181 | const void* dict, size_t dictSize, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3182 | ZSTD_dictMode_e dictMode, |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 3183 | const ZSTD_CDict* cdict, |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3184 | ZSTD_parameters params, U64 pledgedSrcSize, |
| 3185 | ZSTD_buffered_policy_e zbuff) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 3186 | { |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3187 | /* params are supposed to be fully validated at this point */ |
Yann Collet | ab9162e | 2017-04-11 10:46:20 -0700 | [diff] [blame] | 3188 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 3189 | assert(!((dict) && (cdict))); /* either dict or cdict, not both */ |
| 3190 | |
| 3191 | if (cdict && cdict->dictContentSize>0) |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3192 | return ZSTD_copyCCtx_internal(cctx, cdict->refContext, |
| 3193 | params.fParams, pledgedSrcSize); |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 3194 | |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3195 | CHECK_F(ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize, |
| 3196 | ZSTDcrp_continue, zbuff)); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3197 | return ZSTD_compress_insertDictionary(cctx, dict, dictSize, dictMode); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 3198 | } |
| 3199 | |
| 3200 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 3201 | /*! ZSTD_compressBegin_advanced() : |
| 3202 | * @return : 0, or an error code */ |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3203 | size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 3204 | const void* dict, size_t dictSize, |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 3205 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 3206 | { |
| 3207 | /* compression parameters verification and optimization */ |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 3208 | CHECK_F(ZSTD_checkCParams(params.cParams)); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3209 | return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3210 | params, pledgedSrcSize, ZSTDb_not_buffered); |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3214 | 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] | 3215 | { |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 3216 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3217 | return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3218 | params, 0, ZSTDb_not_buffered); |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 3219 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 3220 | |
inikep | 19bd48f | 2016-04-04 12:10:00 +0200 | [diff] [blame] | 3221 | |
Yann Collet | b05c482 | 2017-01-12 02:01:28 +0100 | [diff] [blame] | 3222 | size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 3223 | { |
Yann Collet | b05c482 | 2017-01-12 02:01:28 +0100 | [diff] [blame] | 3224 | return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 3228 | /*! ZSTD_writeEpilogue() : |
| 3229 | * Ends a frame. |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 3230 | * @return : nb of bytes written into dst (or an error code) */ |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 3231 | 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] | 3232 | { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 3233 | BYTE* const ostart = (BYTE*)dst; |
| 3234 | BYTE* op = ostart; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 3235 | size_t fhSize = 0; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 3236 | |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3237 | DEBUGLOG(5, "ZSTD_writeEpilogue"); |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 3238 | if (cctx->stage == ZSTDcs_created) return ERROR(stage_wrong); /* init missing */ |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 3239 | |
| 3240 | /* special case : empty frame */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 3241 | if (cctx->stage == ZSTDcs_init) { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3242 | fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, 0, 0); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 3243 | if (ZSTD_isError(fhSize)) return fhSize; |
| 3244 | dstCapacity -= fhSize; |
| 3245 | op += fhSize; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 3246 | cctx->stage = ZSTDcs_ongoing; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 3247 | } |
| 3248 | |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 3249 | if (cctx->stage != ZSTDcs_ending) { |
| 3250 | /* write one last empty block, make it the "last" block */ |
| 3251 | U32 const cBlockHeader24 = 1 /* last block */ + (((U32)bt_raw)<<1) + 0; |
| 3252 | if (dstCapacity<4) return ERROR(dstSize_tooSmall); |
| 3253 | MEM_writeLE32(op, cBlockHeader24); |
| 3254 | op += ZSTD_blockHeaderSize; |
| 3255 | dstCapacity -= ZSTD_blockHeaderSize; |
| 3256 | } |
| 3257 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3258 | if (cctx->appliedParams.fParams.checksumFlag) { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 3259 | U32 const checksum = (U32) XXH64_digest(&cctx->xxhState); |
| 3260 | if (dstCapacity<4) return ERROR(dstSize_tooSmall); |
| 3261 | MEM_writeLE32(op, checksum); |
| 3262 | op += 4; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 3263 | } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 3264 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 3265 | cctx->stage = ZSTDcs_created; /* return to "created but no init" status */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 3266 | return op-ostart; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 3267 | } |
| 3268 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3269 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 3270 | size_t ZSTD_compressEnd (ZSTD_CCtx* cctx, |
| 3271 | void* dst, size_t dstCapacity, |
| 3272 | const void* src, size_t srcSize) |
| 3273 | { |
| 3274 | size_t endResult; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3275 | size_t const cSize = ZSTD_compressContinue_internal(cctx, |
| 3276 | dst, dstCapacity, src, srcSize, |
| 3277 | 1 /* frame mode */, 1 /* last chunk */); |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 3278 | if (ZSTD_isError(cSize)) return cSize; |
| 3279 | endResult = ZSTD_writeEpilogue(cctx, (char*)dst + cSize, dstCapacity-cSize); |
| 3280 | if (ZSTD_isError(endResult)) return endResult; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3281 | if (cctx->appliedParams.fParams.contentSizeFlag) { /* control src size */ |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 3282 | DEBUGLOG(5, "end of frame : controlling src size"); |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 3283 | if (cctx->pledgedSrcSizePlusOne != cctx->consumedSrcSize+1) { |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 3284 | DEBUGLOG(5, "error : pledgedSrcSize = %u, while realSrcSize = %u", |
Yann Collet | aee916e | 2017-06-16 17:01:46 -0700 | [diff] [blame] | 3285 | (U32)cctx->pledgedSrcSizePlusOne-1, (U32)cctx->consumedSrcSize); |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 3286 | return ERROR(srcSize_wrong); |
Yann Collet | 9e73f2f | 2017-06-16 12:24:01 -0700 | [diff] [blame] | 3287 | } } |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 3288 | return cSize + endResult; |
| 3289 | } |
| 3290 | |
| 3291 | |
Yann Collet | 19c1002 | 2016-07-28 01:25:46 +0200 | [diff] [blame] | 3292 | static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 3293 | void* dst, size_t dstCapacity, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 3294 | const void* src, size_t srcSize, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 3295 | const void* dict,size_t dictSize, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 3296 | ZSTD_parameters params) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 3297 | { |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3298 | CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, |
| 3299 | params, srcSize, ZSTDb_not_buffered) ); |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 3300 | return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 3301 | } |
| 3302 | |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 3303 | size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, |
| 3304 | void* dst, size_t dstCapacity, |
| 3305 | const void* src, size_t srcSize, |
| 3306 | const void* dict,size_t dictSize, |
| 3307 | ZSTD_parameters params) |
| 3308 | { |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 3309 | CHECK_F(ZSTD_checkCParams(params.cParams)); |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 3310 | return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); |
| 3311 | } |
| 3312 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3313 | size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, |
| 3314 | const void* dict, size_t dictSize, int compressionLevel) |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 3315 | { |
Yann Collet | 407a11f | 2016-11-03 15:52:01 -0700 | [diff] [blame] | 3316 | ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dict ? dictSize : 0); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3317 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 3318 | return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 3319 | } |
| 3320 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 3321 | 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] | 3322 | { |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 3323 | return ZSTD_compress_usingDict(ctx, dst, dstCapacity, src, srcSize, NULL, 0, compressionLevel); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 3324 | } |
| 3325 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 3326 | 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] | 3327 | { |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 3328 | size_t result; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 3329 | ZSTD_CCtx ctxBody; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 3330 | memset(&ctxBody, 0, sizeof(ctxBody)); |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 3331 | ctxBody.customMem = ZSTD_defaultCMem; |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 3332 | result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel); |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 3333 | 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] | 3334 | return result; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 3335 | } |
Yann Collet | fdcad6d | 2015-12-17 23:50:15 +0100 | [diff] [blame] | 3336 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3337 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3338 | /* ===== Dictionary API ===== */ |
| 3339 | |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 3340 | /*! ZSTD_estimateCDictSize() : |
| 3341 | * 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] | 3342 | size_t ZSTD_estimateCDictSize(ZSTD_compressionParameters cParams, size_t dictSize, unsigned byReference) |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 3343 | { |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3344 | DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); |
| 3345 | DEBUGLOG(5, "CCtx estimate : %u", (U32)ZSTD_estimateCCtxSize(cParams)); |
Yann Collet | 25989e3 | 2017-05-25 15:07:37 -0700 | [diff] [blame] | 3346 | return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize(cParams) |
| 3347 | + (byReference ? 0 : dictSize); |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 3350 | size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) |
| 3351 | { |
| 3352 | if (cdict==NULL) return 0; /* support sizeof on NULL */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3353 | DEBUGLOG(5, "sizeof(*cdict) : %u", (U32)sizeof(*cdict)); |
| 3354 | DEBUGLOG(5, "ZSTD_sizeof_CCtx : %u", (U32)ZSTD_sizeof_CCtx(cdict->refContext)); |
Yann Collet | aca113f | 2016-12-23 22:25:03 +0100 | [diff] [blame] | 3355 | 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] | 3356 | } |
| 3357 | |
Yann Collet | 1c3ab0c | 2017-04-27 12:57:11 -0700 | [diff] [blame] | 3358 | static ZSTD_parameters ZSTD_makeParams(ZSTD_compressionParameters cParams, ZSTD_frameParameters fParams) |
| 3359 | { |
| 3360 | ZSTD_parameters params; |
| 3361 | params.cParams = cParams; |
| 3362 | params.fParams = fParams; |
| 3363 | return params; |
| 3364 | } |
| 3365 | |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3366 | static size_t ZSTD_initCDict_internal( |
| 3367 | ZSTD_CDict* cdict, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3368 | const void* dictBuffer, size_t dictSize, |
| 3369 | unsigned byReference, ZSTD_dictMode_e dictMode, |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3370 | ZSTD_compressionParameters cParams) |
| 3371 | { |
| 3372 | if ((byReference) || (!dictBuffer) || (!dictSize)) { |
| 3373 | cdict->dictBuffer = NULL; |
| 3374 | cdict->dictContent = dictBuffer; |
| 3375 | } else { |
| 3376 | void* const internalBuffer = ZSTD_malloc(dictSize, cdict->refContext->customMem); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3377 | cdict->dictBuffer = internalBuffer; |
| 3378 | cdict->dictContent = internalBuffer; |
Yann Collet | c3bce24 | 2017-06-20 16:09:11 -0700 | [diff] [blame] | 3379 | if (!internalBuffer) return ERROR(memory_allocation); |
| 3380 | memcpy(internalBuffer, dictBuffer, dictSize); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3381 | } |
| 3382 | cdict->dictContentSize = dictSize; |
| 3383 | |
| 3384 | { ZSTD_frameParameters const fParams = { 0 /* contentSizeFlag */, |
| 3385 | 0 /* checksumFlag */, 0 /* noDictIDFlag */ }; /* dummy */ |
| 3386 | ZSTD_parameters const params = ZSTD_makeParams(cParams, fParams); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3387 | CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, |
| 3388 | cdict->dictContent, dictSize, dictMode, |
| 3389 | NULL, |
| 3390 | params, ZSTD_CONTENTSIZE_UNKNOWN, |
| 3391 | ZSTDb_not_buffered) ); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3392 | } |
| 3393 | |
| 3394 | return 0; |
| 3395 | } |
| 3396 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3397 | ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, |
| 3398 | unsigned byReference, ZSTD_dictMode_e dictMode, |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 3399 | ZSTD_compressionParameters cParams, ZSTD_customMem customMem) |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3400 | { |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 3401 | DEBUGLOG(5, "ZSTD_createCDict_advanced"); |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 3402 | if (!customMem.customAlloc ^ !customMem.customFree) return NULL; |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3403 | |
Yann Collet | 466f92e | 2017-06-20 16:25:29 -0700 | [diff] [blame] | 3404 | { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3405 | ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); |
| 3406 | |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 3407 | if (!cdict || !cctx) { |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3408 | ZSTD_free(cdict, customMem); |
Przemyslaw Skibinski | d8114e5 | 2017-02-21 18:59:56 +0100 | [diff] [blame] | 3409 | ZSTD_freeCCtx(cctx); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3410 | return NULL; |
| 3411 | } |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3412 | cdict->refContext = cctx; |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3413 | |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3414 | if (ZSTD_isError( ZSTD_initCDict_internal(cdict, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3415 | dictBuffer, dictSize, |
| 3416 | byReference, dictMode, |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3417 | cParams) )) { |
| 3418 | ZSTD_freeCDict(cdict); |
| 3419 | return NULL; |
Nick Terrell | 3b9cdf9 | 2016-10-12 20:54:42 -0700 | [diff] [blame] | 3420 | } |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 3421 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3422 | return cdict; |
| 3423 | } |
| 3424 | } |
| 3425 | |
| 3426 | ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) |
| 3427 | { |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 3428 | ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3429 | return ZSTD_createCDict_advanced(dict, dictSize, |
| 3430 | 0 /* byReference */, ZSTD_dm_auto, |
| 3431 | cParams, ZSTD_defaultCMem); |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 3432 | } |
| 3433 | |
| 3434 | ZSTD_CDict* ZSTD_createCDict_byReference(const void* dict, size_t dictSize, int compressionLevel) |
| 3435 | { |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 3436 | ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3437 | return ZSTD_createCDict_advanced(dict, dictSize, |
| 3438 | 1 /* byReference */, ZSTD_dm_auto, |
| 3439 | cParams, ZSTD_defaultCMem); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3440 | } |
| 3441 | |
| 3442 | size_t ZSTD_freeCDict(ZSTD_CDict* cdict) |
| 3443 | { |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3444 | if (cdict==NULL) return 0; /* support free on NULL */ |
Yann Collet | 993060e | 2016-09-21 16:46:08 +0200 | [diff] [blame] | 3445 | { ZSTD_customMem const cMem = cdict->refContext->customMem; |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3446 | ZSTD_freeCCtx(cdict->refContext); |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 3447 | ZSTD_free(cdict->dictBuffer, cMem); |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 3448 | ZSTD_free(cdict, cMem); |
| 3449 | return 0; |
| 3450 | } |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3451 | } |
| 3452 | |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3453 | /*! ZSTD_initStaticCDict_advanced() : |
| 3454 | * Generate a digested dictionary in provided memory area. |
| 3455 | * workspace: The memory area to emplace the dictionary into. |
| 3456 | * Provided pointer must 8-bytes aligned. |
| 3457 | * It must outlive dictionary usage. |
| 3458 | * workspaceSize: Use ZSTD_estimateCDictSize() |
| 3459 | * to determine how large workspace must be. |
| 3460 | * cParams : use ZSTD_getCParams() to transform a compression level |
| 3461 | * into its relevants cParams. |
| 3462 | * @return : pointer to ZSTD_CDict*, or NULL if error (size too small) |
| 3463 | * Note : there is no corresponding "free" function. |
| 3464 | * Since workspace was allocated externally, it must be freed externally. |
| 3465 | */ |
| 3466 | ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3467 | const void* dict, size_t dictSize, |
| 3468 | unsigned byReference, ZSTD_dictMode_e dictMode, |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3469 | ZSTD_compressionParameters cParams) |
| 3470 | { |
| 3471 | size_t const cctxSize = ZSTD_estimateCCtxSize(cParams); |
| 3472 | size_t const neededSize = sizeof(ZSTD_CDict) + (byReference ? 0 : dictSize) |
| 3473 | + cctxSize; |
| 3474 | ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; |
| 3475 | void* ptr; |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 3476 | DEBUGLOG(5, "(size_t)workspace & 7 : %u", (U32)(size_t)workspace & 7); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3477 | if ((size_t)workspace & 7) return NULL; /* 8-aligned */ |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 3478 | DEBUGLOG(5, "(workspaceSize < neededSize) : (%u < %u) => %u", |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3479 | (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); |
| 3480 | if (workspaceSize < neededSize) return NULL; |
| 3481 | |
| 3482 | if (!byReference) { |
| 3483 | memcpy(cdict+1, dict, dictSize); |
| 3484 | dict = cdict+1; |
| 3485 | ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; |
| 3486 | } else { |
| 3487 | ptr = cdict+1; |
| 3488 | } |
| 3489 | cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); |
| 3490 | |
| 3491 | if (ZSTD_isError( ZSTD_initCDict_internal(cdict, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3492 | dict, dictSize, |
| 3493 | 1 /* byReference */, dictMode, |
| 3494 | cParams) )) |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 3495 | return NULL; |
| 3496 | |
| 3497 | return cdict; |
| 3498 | } |
| 3499 | |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3500 | ZSTD_parameters ZSTD_getParamsFromCDict(const ZSTD_CDict* cdict) { |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 3501 | return ZSTD_getParamsFromCCtx(cdict->refContext); |
| 3502 | } |
| 3503 | |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 3504 | /* ZSTD_compressBegin_usingCDict_advanced() : |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3505 | * cdict must be != NULL */ |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 3506 | size_t ZSTD_compressBegin_usingCDict_advanced( |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3507 | ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, |
| 3508 | ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3509 | { |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3510 | if (cdict==NULL) return ERROR(dictionary_wrong); |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 3511 | { ZSTD_parameters params = cdict->refContext->appliedParams; |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3512 | params.fParams = fParams; |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 3513 | DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3514 | return ZSTD_compressBegin_internal(cctx, |
| 3515 | NULL, 0, ZSTD_dm_auto, |
| 3516 | cdict, |
| 3517 | params, pledgedSrcSize, |
| 3518 | ZSTDb_not_buffered); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3519 | } |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3520 | } |
| 3521 | |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3522 | /* ZSTD_compressBegin_usingCDict() : |
| 3523 | * pledgedSrcSize=0 means "unknown" |
| 3524 | * if pledgedSrcSize>0, it will enable contentSizeFlag */ |
Yann Collet | 768df12 | 2017-04-26 15:42:10 -0700 | [diff] [blame] | 3525 | size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3526 | { |
Yann Collet | 768df12 | 2017-04-26 15:42:10 -0700 | [diff] [blame] | 3527 | ZSTD_frameParameters const fParams = { 0 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3528 | DEBUGLOG(5, "ZSTD_compressBegin_usingCDict : dictIDFlag == %u", !fParams.noDictIDFlag); |
Yann Collet | 768df12 | 2017-04-26 15:42:10 -0700 | [diff] [blame] | 3529 | return ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, 0); |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3530 | } |
| 3531 | |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 3532 | size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, |
| 3533 | void* dst, size_t dstCapacity, |
| 3534 | const void* src, size_t srcSize, |
| 3535 | const ZSTD_CDict* cdict, ZSTD_frameParameters fParams) |
| 3536 | { |
| 3537 | CHECK_F (ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, srcSize)); /* will check if cdict != NULL */ |
| 3538 | return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3539 | } |
| 3540 | |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 3541 | /*! ZSTD_compress_usingCDict() : |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3542 | * Compression using a digested Dictionary. |
| 3543 | * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. |
| 3544 | * Note that compression parameters are decided at CDict creation time |
| 3545 | * while frame parameters are hardcoded */ |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3546 | size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, |
| 3547 | void* dst, size_t dstCapacity, |
| 3548 | const void* src, size_t srcSize, |
| 3549 | const ZSTD_CDict* cdict) |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3550 | { |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 3551 | ZSTD_frameParameters const fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 3552 | return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, fParams); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 3553 | } |
| 3554 | |
| 3555 | |
| 3556 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3557 | /* ****************************************************************** |
| 3558 | * Streaming |
| 3559 | ********************************************************************/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3560 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3561 | ZSTD_CStream* ZSTD_createCStream(void) |
| 3562 | { |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 3563 | return ZSTD_createCStream_advanced(ZSTD_defaultCMem); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem) |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 3567 | { /* CStream and CCtx are now same object */ |
Yann Collet | 6fb2f24 | 2017-05-10 11:06:06 -0700 | [diff] [blame] | 3568 | return ZSTD_createCCtx_advanced(customMem); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3569 | } |
| 3570 | |
| 3571 | size_t ZSTD_freeCStream(ZSTD_CStream* zcs) |
| 3572 | { |
Yann Collet | 7855366 | 2017-05-08 17:15:00 -0700 | [diff] [blame] | 3573 | return ZSTD_freeCCtx(zcs); /* same object */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3574 | } |
| 3575 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3576 | |
| 3577 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3578 | /*====== Initialization ======*/ |
| 3579 | |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 3580 | size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX; } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3581 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3582 | size_t ZSTD_CStreamOutSize(void) |
| 3583 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 3584 | return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 3585 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3586 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3587 | static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, |
| 3588 | ZSTD_parameters params, |
| 3589 | unsigned long long pledgedSrcSize) |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3590 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3591 | DEBUGLOG(5, "ZSTD_resetCStream_internal"); |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 3592 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3593 | CHECK_F( ZSTD_compressBegin_internal(zcs, |
| 3594 | NULL, 0, ZSTD_dm_auto, |
| 3595 | zcs->cdict, |
| 3596 | params, pledgedSrcSize, |
| 3597 | ZSTDb_buffered) ); |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3598 | |
| 3599 | zcs->inToCompress = 0; |
| 3600 | zcs->inBuffPos = 0; |
| 3601 | zcs->inBuffTarget = zcs->blockSize; |
| 3602 | zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 3603 | zcs->streamStage = zcss_load; |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 3604 | zcs->frameEnded = 0; |
| 3605 | return 0; /* ready to go */ |
| 3606 | } |
| 3607 | |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3608 | size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) |
| 3609 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3610 | ZSTD_parameters params = zcs->requestedParams; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3611 | params.fParams.contentSizeFlag = (pledgedSrcSize > 0); |
Yann Collet | b0739bc | 2017-05-22 17:45:15 -0700 | [diff] [blame] | 3612 | DEBUGLOG(5, "ZSTD_resetCStream"); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3613 | if (zcs->compressionLevel != ZSTD_CLEVEL_CUSTOM) { |
| 3614 | params.cParams = ZSTD_getCParams(zcs->compressionLevel, pledgedSrcSize, 0 /* dictSize */); |
| 3615 | } |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3616 | return ZSTD_resetCStream_internal(zcs, params, pledgedSrcSize); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3617 | } |
| 3618 | |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3619 | size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, |
| 3620 | const void* dict, size_t dictSize, const ZSTD_CDict* cdict, |
| 3621 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3622 | { |
| 3623 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3624 | assert(!((dict) && (cdict))); /* either dict or cdict, not both */ |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3625 | |
| 3626 | if (dict && dictSize >= 8) { |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 3627 | if (zcs->staticSize) { /* static CCtx : never uses malloc */ |
| 3628 | /* incompatible with internal cdict creation */ |
| 3629 | return ERROR(memory_allocation); |
| 3630 | } |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3631 | ZSTD_freeCDict(zcs->cdictLocal); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame^] | 3632 | zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, |
| 3633 | 0 /* byReference */, ZSTD_dm_auto, |
| 3634 | params.cParams, zcs->customMem); |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3635 | zcs->cdict = zcs->cdictLocal; |
Yann Collet | 466f92e | 2017-06-20 16:25:29 -0700 | [diff] [blame] | 3636 | if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3637 | } else { |
| 3638 | if (cdict) { |
| 3639 | ZSTD_parameters const cdictParams = ZSTD_getParamsFromCDict(cdict); |
| 3640 | params.cParams = cdictParams.cParams; /* cParams are enforced from cdict */ |
| 3641 | } |
Yann Collet | 466f92e | 2017-06-20 16:25:29 -0700 | [diff] [blame] | 3642 | ZSTD_freeCDict(zcs->cdictLocal); |
| 3643 | zcs->cdictLocal = NULL; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3644 | zcs->cdict = cdict; |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3645 | } |
| 3646 | |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3647 | zcs->requestedParams = params; |
| 3648 | zcs->compressionLevel = ZSTD_CLEVEL_CUSTOM; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 3649 | return ZSTD_resetCStream_internal(zcs, params, pledgedSrcSize); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 3650 | } |
| 3651 | |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3652 | /* ZSTD_initCStream_usingCDict_advanced() : |
| 3653 | * same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ |
| 3654 | size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, |
| 3655 | const ZSTD_CDict* cdict, |
| 3656 | ZSTD_frameParameters fParams, |
| 3657 | unsigned long long pledgedSrcSize) |
| 3658 | { /* cannot handle NULL cdict (does not know what to do) */ |
| 3659 | if (!cdict) return ERROR(dictionary_wrong); |
| 3660 | { ZSTD_parameters params = ZSTD_getParamsFromCDict(cdict); |
| 3661 | params.fParams = fParams; |
| 3662 | return ZSTD_initCStream_internal(zcs, |
| 3663 | NULL, 0, cdict, |
| 3664 | params, pledgedSrcSize); |
| 3665 | } |
| 3666 | } |
| 3667 | |
| 3668 | /* note : cdict must outlive compression session */ |
| 3669 | size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict) |
| 3670 | { |
| 3671 | ZSTD_frameParameters const fParams = { 0 /* contentSize */, 0 /* checksum */, 0 /* hideDictID */ }; |
| 3672 | return ZSTD_initCStream_usingCDict_advanced(zcs, cdict, fParams, 0); /* note : will check that cdict != NULL */ |
| 3673 | } |
| 3674 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3675 | size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, |
| 3676 | const void* dict, size_t dictSize, |
| 3677 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
| 3678 | { |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 3679 | CHECK_F( ZSTD_checkCParams(params.cParams) ); |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3680 | zcs->requestedParams = params; |
| 3681 | zcs->compressionLevel = ZSTD_CLEVEL_CUSTOM; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3682 | return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, params, pledgedSrcSize); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 3683 | } |
| 3684 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3685 | size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) |
| 3686 | { |
| 3687 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3688 | zcs->compressionLevel = compressionLevel; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3689 | return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, params, 0); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3690 | } |
| 3691 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 3692 | size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) |
| 3693 | { |
Yann Collet | d564faa | 2016-12-18 21:39:15 +0100 | [diff] [blame] | 3694 | ZSTD_parameters params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3695 | params.fParams.contentSizeFlag = (pledgedSrcSize>0); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3696 | return ZSTD_initCStream_internal(zcs, NULL, 0, NULL, params, pledgedSrcSize); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 3697 | } |
| 3698 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3699 | size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel) |
| 3700 | { |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 3701 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, 0); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3702 | return ZSTD_initCStream_internal(zcs, NULL, 0, NULL, params, 0); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3703 | } |
| 3704 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3705 | /*====== Compression ======*/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3706 | |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3707 | MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, |
| 3708 | const void* src, size_t srcSize) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3709 | { |
| 3710 | size_t const length = MIN(dstCapacity, srcSize); |
Yann Collet | 18ab5af | 2017-05-31 09:59:22 -0700 | [diff] [blame] | 3711 | if (length) memcpy(dst, src, length); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3712 | return length; |
| 3713 | } |
| 3714 | |
| 3715 | static size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3716 | ZSTD_outBuffer* output, |
| 3717 | ZSTD_inBuffer* input, |
| 3718 | ZSTD_EndDirective const flushMode) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3719 | { |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3720 | const char* const istart = (const char*)input->src; |
| 3721 | const char* const iend = istart + input->size; |
| 3722 | const char* ip = istart + input->pos; |
| 3723 | char* const ostart = (char*)output->dst; |
| 3724 | char* const oend = ostart + output->size; |
| 3725 | char* op = ostart + output->pos; |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 3726 | U32 someMoreWork = 1; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3727 | |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 3728 | /* check expectations */ |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 3729 | DEBUGLOG(5, "ZSTD_compressStream_generic"); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3730 | assert(zcs->inBuff != NULL); |
| 3731 | assert(zcs->outBuff!= NULL); |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 3732 | assert(output->pos <= output->size); |
| 3733 | assert(input->pos <= input->size); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3734 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3735 | while (someMoreWork) { |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 3736 | switch(zcs->streamStage) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3737 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3738 | case zcss_init: |
| 3739 | /* call ZSTD_initCStream() first ! */ |
| 3740 | return ERROR(init_missing); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3741 | |
| 3742 | case zcss_load: |
| 3743 | /* complete inBuffer */ |
| 3744 | { size_t const toLoad = zcs->inBuffTarget - zcs->inBuffPos; |
Yann Collet | 06589fe | 2017-05-31 10:03:20 -0700 | [diff] [blame] | 3745 | size_t const loaded = ZSTD_limitCopy( |
| 3746 | zcs->inBuff + zcs->inBuffPos, toLoad, |
| 3747 | ip, iend-ip); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3748 | zcs->inBuffPos += loaded; |
| 3749 | ip += loaded; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3750 | if ( (flushMode == ZSTD_e_continue) |
| 3751 | && (zcs->inBuffPos < zcs->inBuffTarget) ) { |
| 3752 | /* not enough input to fill full block : stop here */ |
| 3753 | someMoreWork = 0; break; |
| 3754 | } |
| 3755 | if ( (flushMode == ZSTD_e_flush) |
| 3756 | && (zcs->inBuffPos == zcs->inToCompress) ) { |
| 3757 | /* empty */ |
| 3758 | someMoreWork = 0; break; |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 3759 | } |
| 3760 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3761 | /* compress current block (note : this stage cannot be stopped in the middle) */ |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3762 | DEBUGLOG(5, "stream compression stage (flushMode==%u)", flushMode); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3763 | { void* cDst; |
| 3764 | size_t cSize; |
| 3765 | size_t const iSize = zcs->inBuffPos - zcs->inToCompress; |
| 3766 | size_t oSize = oend-op; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3767 | unsigned const lastBlock = (flushMode == ZSTD_e_end) && (ip==iend); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3768 | if (oSize >= ZSTD_compressBound(iSize)) |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 3769 | cDst = op; /* compress into output buffer, to skip flush stage */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3770 | else |
| 3771 | cDst = zcs->outBuff, oSize = zcs->outBuffSize; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3772 | cSize = lastBlock ? |
| 3773 | ZSTD_compressEnd(zcs, cDst, oSize, |
| 3774 | zcs->inBuff + zcs->inToCompress, iSize) : |
| 3775 | ZSTD_compressContinue(zcs, cDst, oSize, |
| 3776 | zcs->inBuff + zcs->inToCompress, iSize); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3777 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3778 | zcs->frameEnded = lastBlock; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3779 | /* prepare next block */ |
| 3780 | zcs->inBuffTarget = zcs->inBuffPos + zcs->blockSize; |
| 3781 | if (zcs->inBuffTarget > zcs->inBuffSize) |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3782 | zcs->inBuffPos = 0, zcs->inBuffTarget = zcs->blockSize; |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 3783 | DEBUGLOG(5, "inBuffTarget:%u / inBuffSize:%u", |
| 3784 | (U32)zcs->inBuffTarget, (U32)zcs->inBuffSize); |
| 3785 | if (!lastBlock) |
| 3786 | assert(zcs->inBuffTarget <= zcs->inBuffSize); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3787 | zcs->inToCompress = zcs->inBuffPos; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3788 | if (cDst == op) { /* no need to flush */ |
| 3789 | op += cSize; |
| 3790 | if (zcs->frameEnded) { |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 3791 | DEBUGLOG(5, "Frame completed directly in outBuffer"); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3792 | someMoreWork = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 3793 | ZSTD_startNewCompression(zcs); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3794 | } |
| 3795 | break; |
| 3796 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3797 | zcs->outBuffContentSize = cSize; |
| 3798 | zcs->outBuffFlushedSize = 0; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3799 | zcs->streamStage = zcss_flush; /* pass-through to flush stage */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3800 | } |
Jos Collin | 7cd7a75 | 2017-05-11 13:17:20 +0530 | [diff] [blame] | 3801 | /* fall-through */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3802 | case zcss_flush: |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3803 | DEBUGLOG(5, "flush stage"); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3804 | { size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3805 | size_t const flushed = ZSTD_limitCopy(op, oend-op, |
| 3806 | zcs->outBuff + zcs->outBuffFlushedSize, toFlush); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3807 | DEBUGLOG(5, "toFlush: %u ; flushed: %u", (U32)toFlush, (U32)flushed); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3808 | op += flushed; |
| 3809 | zcs->outBuffFlushedSize += flushed; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3810 | if (toFlush!=flushed) { |
| 3811 | /* dst too small to store flushed data : stop there */ |
| 3812 | someMoreWork = 0; |
| 3813 | break; |
| 3814 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3815 | zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3816 | if (zcs->frameEnded) { |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 3817 | DEBUGLOG(5, "Frame completed on flush"); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3818 | someMoreWork = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 3819 | ZSTD_startNewCompression(zcs); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3820 | break; |
| 3821 | } |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 3822 | zcs->streamStage = zcss_load; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3823 | break; |
| 3824 | } |
| 3825 | |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 3826 | default: /* impossible */ |
| 3827 | assert(0); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3831 | input->pos = ip - istart; |
| 3832 | output->pos = op - ostart; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3833 | if (zcs->frameEnded) return 0; |
| 3834 | { size_t hintInSize = zcs->inBuffTarget - zcs->inBuffPos; |
| 3835 | if (hintInSize==0) hintInSize = zcs->blockSize; |
| 3836 | return hintInSize; |
| 3837 | } |
| 3838 | } |
| 3839 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3840 | 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] | 3841 | { |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3842 | /* check conditions */ |
| 3843 | if (output->pos > output->size) return ERROR(GENERIC); |
| 3844 | if (input->pos > input->size) return ERROR(GENERIC); |
| 3845 | |
| 3846 | return ZSTD_compressStream_generic(zcs, output, input, ZSTD_e_continue); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3847 | } |
| 3848 | |
Yann Collet | f35e2de | 2017-06-05 18:32:48 -0700 | [diff] [blame] | 3849 | /*! ZSTDMT_initCStream_internal() : |
| 3850 | * Private use only. Init streaming operation. |
| 3851 | * expects params to be valid. |
| 3852 | * must receive dict, or cdict, or none, but not both. |
| 3853 | * @return : 0, or an error code */ |
| 3854 | size_t ZSTDMT_initCStream_internal(ZSTDMT_CCtx* zcs, |
| 3855 | const void* dict, size_t dictSize, const ZSTD_CDict* cdict, |
| 3856 | ZSTD_parameters params, unsigned long long pledgedSrcSize); |
| 3857 | |
| 3858 | |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 3859 | size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, |
| 3860 | ZSTD_outBuffer* output, |
| 3861 | ZSTD_inBuffer* input, |
| 3862 | ZSTD_EndDirective endOp) |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3863 | { |
| 3864 | /* check conditions */ |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 3865 | if (output->pos > output->size) return ERROR(GENERIC); |
| 3866 | if (input->pos > input->size) return ERROR(GENERIC); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3867 | assert(cctx!=NULL); |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3868 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3869 | if (cctx->streamStage == zcss_init) { |
| 3870 | /* transparent reset */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 3871 | ZSTD_parameters params = cctx->requestedParams; |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3872 | if (cctx->compressionLevel != ZSTD_CLEVEL_CUSTOM) |
| 3873 | params.cParams = ZSTD_getCParams(cctx->compressionLevel, |
Yann Collet | aee916e | 2017-06-16 17:01:46 -0700 | [diff] [blame] | 3874 | cctx->pledgedSrcSizePlusOne-1, 0 /* dictSize */); |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 3875 | |
| 3876 | #ifdef ZSTD_MULTITHREAD |
Yann Collet | f35e2de | 2017-06-05 18:32:48 -0700 | [diff] [blame] | 3877 | if (cctx->nbThreads > 1) { |
Yann Collet | c08e649 | 2017-06-19 18:25:35 -0700 | [diff] [blame] | 3878 | DEBUGLOG(4, "call ZSTDMT_initCStream_internal"); |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 3879 | CHECK_F( ZSTDMT_initCStream_internal(cctx->mtctx, NULL, 0, cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); |
Yann Collet | 23aace9 | 2017-06-11 18:32:36 -0700 | [diff] [blame] | 3880 | cctx->streamStage = zcss_load; |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 3881 | } else |
| 3882 | #endif |
| 3883 | { |
Yann Collet | aee916e | 2017-06-16 17:01:46 -0700 | [diff] [blame] | 3884 | CHECK_F( ZSTD_resetCStream_internal(cctx, params, cctx->pledgedSrcSizePlusOne-1) ); |
Yann Collet | f35e2de | 2017-06-05 18:32:48 -0700 | [diff] [blame] | 3885 | } } |
| 3886 | |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 3887 | #ifdef ZSTD_MULTITHREAD |
Yann Collet | f35e2de | 2017-06-05 18:32:48 -0700 | [diff] [blame] | 3888 | if (cctx->nbThreads > 1) { |
Yann Collet | 23aace9 | 2017-06-11 18:32:36 -0700 | [diff] [blame] | 3889 | size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp); |
Yann Collet | c08e649 | 2017-06-19 18:25:35 -0700 | [diff] [blame] | 3890 | DEBUGLOG(4, "ZSTDMT_compressStream_generic : %u", (U32)flushMin); |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 3891 | if ( ZSTD_isError(flushMin) |
| 3892 | || (endOp == ZSTD_e_end && flushMin == 0) ) { /* compression completed */ |
| 3893 | ZSTD_startNewCompression(cctx); |
Yann Collet | 9e73f2f | 2017-06-16 12:24:01 -0700 | [diff] [blame] | 3894 | } |
Yann Collet | 23aace9 | 2017-06-11 18:32:36 -0700 | [diff] [blame] | 3895 | return flushMin; |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3896 | } |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 3897 | #endif |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3898 | |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 3899 | DEBUGLOG(5, "calling ZSTD_compressStream_generic(%i,...)", endOp); |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3900 | CHECK_F( ZSTD_compressStream_generic(cctx, output, input, endOp) ); |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 3901 | DEBUGLOG(5, "completed ZSTD_compress_generic"); |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 3902 | return cctx->outBuffContentSize - cctx->outBuffFlushedSize; /* remaining to flush */ |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3903 | } |
| 3904 | |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 3905 | size_t ZSTD_compress_generic_simpleArgs ( |
| 3906 | ZSTD_CCtx* cctx, |
| 3907 | void* dst, size_t dstCapacity, size_t* dstPos, |
| 3908 | const void* src, size_t srcSize, size_t* srcPos, |
| 3909 | ZSTD_EndDirective endOp) |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3910 | { |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 3911 | ZSTD_outBuffer output = { dst, dstCapacity, *dstPos }; |
| 3912 | ZSTD_inBuffer input = { src, srcSize, *srcPos }; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3913 | /* ZSTD_compress_generic() will check validity of dstPos and srcPos */ |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 3914 | size_t const cErr = ZSTD_compress_generic(cctx, &output, &input, endOp); |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 3915 | *dstPos = output.pos; |
| 3916 | *srcPos = input.pos; |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 3917 | return cErr; |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3918 | } |
| 3919 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3920 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 3921 | /*====== Finalize ======*/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3922 | |
| 3923 | /*! ZSTD_flushStream() : |
| 3924 | * @return : amount of data remaining to flush */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3925 | size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3926 | { |
Yann Collet | 18ab5af | 2017-05-31 09:59:22 -0700 | [diff] [blame] | 3927 | ZSTD_inBuffer input = { NULL, 0, 0 }; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3928 | if (output->pos > output->size) return ERROR(GENERIC); |
| 3929 | CHECK_F( ZSTD_compressStream_generic(zcs, output, &input, ZSTD_e_flush) ); |
| 3930 | return zcs->outBuffContentSize - zcs->outBuffFlushedSize; /* remaining to flush */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 3934 | size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3935 | { |
Yann Collet | 18ab5af | 2017-05-31 09:59:22 -0700 | [diff] [blame] | 3936 | ZSTD_inBuffer input = { NULL, 0, 0 }; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 3937 | if (output->pos > output->size) return ERROR(GENERIC); |
| 3938 | CHECK_F( ZSTD_compressStream_generic(zcs, output, &input, ZSTD_e_end) ); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3939 | |
Yann Collet | 48855fa | 2017-05-19 10:56:11 -0700 | [diff] [blame] | 3940 | DEBUGLOG(5, "ZSTD_endStream : remaining to flush : %u", |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3941 | (unsigned)(zcs->outBuffContentSize - zcs->outBuffFlushedSize)); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3942 | return zcs->outBuffContentSize - zcs->outBuffFlushedSize; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 3943 | } |
| 3944 | |
| 3945 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 3946 | /*-===== Pre-defined compression levels =====-*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3947 | |
inikep | 2c5eeea | 2016-04-15 13:44:46 +0200 | [diff] [blame] | 3948 | #define ZSTD_MAX_CLEVEL 22 |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 3949 | int ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 3950 | |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3951 | static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL+1] = { |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3952 | { /* "default" */ |
Yann Collet | 793c649 | 2016-04-09 20:32:00 +0200 | [diff] [blame] | 3953 | /* W, C, H, S, L, TL, strat */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3954 | { 18, 12, 12, 1, 7, 16, ZSTD_fast }, /* level 0 - never used */ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 3955 | { 19, 13, 14, 1, 7, 16, ZSTD_fast }, /* level 1 */ |
| 3956 | { 19, 15, 16, 1, 6, 16, ZSTD_fast }, /* level 2 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3957 | { 20, 16, 17, 1, 5, 16, ZSTD_dfast }, /* level 3.*/ |
| 3958 | { 20, 18, 18, 1, 5, 16, ZSTD_dfast }, /* level 4.*/ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 3959 | { 20, 15, 18, 3, 5, 16, ZSTD_greedy }, /* level 5 */ |
| 3960 | { 21, 16, 19, 2, 5, 16, ZSTD_lazy }, /* level 6 */ |
| 3961 | { 21, 17, 20, 3, 5, 16, ZSTD_lazy }, /* level 7 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3962 | { 21, 18, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 8 */ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 3963 | { 21, 20, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 9 */ |
| 3964 | { 21, 19, 21, 4, 5, 16, ZSTD_lazy2 }, /* level 10 */ |
| 3965 | { 22, 20, 22, 4, 5, 16, ZSTD_lazy2 }, /* level 11 */ |
| 3966 | { 22, 20, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 12 */ |
| 3967 | { 22, 21, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 13 */ |
| 3968 | { 22, 21, 22, 6, 5, 16, ZSTD_lazy2 }, /* level 14 */ |
| 3969 | { 22, 21, 21, 5, 5, 16, ZSTD_btlazy2 }, /* level 15 */ |
| 3970 | { 23, 22, 22, 5, 5, 16, ZSTD_btlazy2 }, /* level 16 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3971 | { 23, 21, 22, 4, 5, 24, ZSTD_btopt }, /* level 17 */ |
Yann Collet | 29297c6 | 2017-04-27 17:44:01 -0700 | [diff] [blame] | 3972 | { 23, 22, 22, 5, 4, 32, ZSTD_btopt }, /* level 18 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3973 | { 23, 23, 22, 6, 3, 48, ZSTD_btopt }, /* level 19 */ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 3974 | { 25, 25, 23, 7, 3, 64, ZSTD_btultra }, /* level 20 */ |
| 3975 | { 26, 26, 23, 7, 3,256, ZSTD_btultra }, /* level 21 */ |
| 3976 | { 27, 27, 25, 9, 3,512, ZSTD_btultra }, /* level 22 */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3977 | }, |
| 3978 | { /* for srcSize <= 256 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3979 | /* W, C, H, S, L, T, strat */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 3980 | { 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - not used */ |
Yann Collet | a2cdffe | 2016-08-24 19:42:15 +0200 | [diff] [blame] | 3981 | { 18, 13, 14, 1, 6, 8, ZSTD_fast }, /* level 1 */ |
Yann Collet | 24b68a5 | 2016-08-24 14:22:26 +0200 | [diff] [blame] | 3982 | { 18, 14, 13, 1, 5, 8, ZSTD_dfast }, /* level 2 */ |
| 3983 | { 18, 16, 15, 1, 5, 8, ZSTD_dfast }, /* level 3 */ |
| 3984 | { 18, 15, 17, 1, 5, 8, ZSTD_greedy }, /* level 4.*/ |
| 3985 | { 18, 16, 17, 4, 5, 8, ZSTD_greedy }, /* level 5.*/ |
| 3986 | { 18, 16, 17, 3, 5, 8, ZSTD_lazy }, /* level 6.*/ |
| 3987 | { 18, 17, 17, 4, 4, 8, ZSTD_lazy }, /* level 7 */ |
| 3988 | { 18, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ |
| 3989 | { 18, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ |
| 3990 | { 18, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ |
| 3991 | { 18, 18, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 11.*/ |
| 3992 | { 18, 18, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 12.*/ |
| 3993 | { 18, 19, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13 */ |
Yann Collet | 78267d1 | 2016-04-08 12:36:19 +0200 | [diff] [blame] | 3994 | { 18, 18, 18, 4, 4, 16, ZSTD_btopt }, /* level 14.*/ |
Yann Collet | 24b68a5 | 2016-08-24 14:22:26 +0200 | [diff] [blame] | 3995 | { 18, 18, 18, 4, 3, 16, ZSTD_btopt }, /* level 15.*/ |
| 3996 | { 18, 19, 18, 6, 3, 32, ZSTD_btopt }, /* level 16.*/ |
| 3997 | { 18, 19, 18, 8, 3, 64, ZSTD_btopt }, /* level 17.*/ |
Yann Collet | 78267d1 | 2016-04-08 12:36:19 +0200 | [diff] [blame] | 3998 | { 18, 19, 18, 9, 3,128, ZSTD_btopt }, /* level 18.*/ |
| 3999 | { 18, 19, 18, 10, 3,256, ZSTD_btopt }, /* level 19.*/ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 4000 | { 18, 19, 18, 11, 3,512, ZSTD_btultra }, /* level 20.*/ |
| 4001 | { 18, 19, 18, 12, 3,512, ZSTD_btultra }, /* level 21.*/ |
| 4002 | { 18, 19, 18, 13, 3,512, ZSTD_btultra }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 4003 | }, |
| 4004 | { /* for srcSize <= 128 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 4005 | /* W, C, H, S, L, T, strat */ |
Yann Collet | 5894ea8 | 2016-07-22 14:36:46 +0200 | [diff] [blame] | 4006 | { 17, 12, 12, 1, 7, 8, ZSTD_fast }, /* level 0 - not used */ |
| 4007 | { 17, 12, 13, 1, 6, 8, ZSTD_fast }, /* level 1 */ |
| 4008 | { 17, 13, 16, 1, 5, 8, ZSTD_fast }, /* level 2 */ |
| 4009 | { 17, 16, 16, 2, 5, 8, ZSTD_dfast }, /* level 3 */ |
| 4010 | { 17, 13, 15, 3, 4, 8, ZSTD_greedy }, /* level 4 */ |
| 4011 | { 17, 15, 17, 4, 4, 8, ZSTD_greedy }, /* level 5 */ |
| 4012 | { 17, 16, 17, 3, 4, 8, ZSTD_lazy }, /* level 6 */ |
| 4013 | { 17, 15, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 7 */ |
| 4014 | { 17, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ |
| 4015 | { 17, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ |
| 4016 | { 17, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ |
| 4017 | { 17, 17, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 11 */ |
| 4018 | { 17, 17, 17, 8, 4, 8, ZSTD_lazy2 }, /* level 12 */ |
| 4019 | { 17, 18, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13.*/ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 4020 | { 17, 17, 17, 7, 3, 8, ZSTD_btopt }, /* level 14.*/ |
| 4021 | { 17, 17, 17, 7, 3, 16, ZSTD_btopt }, /* level 15.*/ |
| 4022 | { 17, 18, 17, 7, 3, 32, ZSTD_btopt }, /* level 16.*/ |
| 4023 | { 17, 18, 17, 7, 3, 64, ZSTD_btopt }, /* level 17.*/ |
| 4024 | { 17, 18, 17, 7, 3,256, ZSTD_btopt }, /* level 18.*/ |
| 4025 | { 17, 18, 17, 8, 3,256, ZSTD_btopt }, /* level 19.*/ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 4026 | { 17, 18, 17, 9, 3,256, ZSTD_btultra }, /* level 20.*/ |
| 4027 | { 17, 18, 17, 10, 3,256, ZSTD_btultra }, /* level 21.*/ |
| 4028 | { 17, 18, 17, 11, 3,512, ZSTD_btultra }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 4029 | }, |
| 4030 | { /* for srcSize <= 16 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 4031 | /* W, C, H, S, L, T, strat */ |
Yann Collet | 2b1a363 | 2016-07-13 15:16:00 +0200 | [diff] [blame] | 4032 | { 14, 12, 12, 1, 7, 6, ZSTD_fast }, /* level 0 - not used */ |
Yann Collet | e557fd5 | 2016-07-17 16:21:37 +0200 | [diff] [blame] | 4033 | { 14, 14, 14, 1, 6, 6, ZSTD_fast }, /* level 1 */ |
Yann Collet | 2b1a363 | 2016-07-13 15:16:00 +0200 | [diff] [blame] | 4034 | { 14, 14, 14, 1, 4, 6, ZSTD_fast }, /* level 2 */ |
| 4035 | { 14, 14, 14, 1, 4, 6, ZSTD_dfast }, /* level 3.*/ |
| 4036 | { 14, 14, 14, 4, 4, 6, ZSTD_greedy }, /* level 4.*/ |
| 4037 | { 14, 14, 14, 3, 4, 6, ZSTD_lazy }, /* level 5.*/ |
| 4038 | { 14, 14, 14, 4, 4, 6, ZSTD_lazy2 }, /* level 6 */ |
| 4039 | { 14, 14, 14, 5, 4, 6, ZSTD_lazy2 }, /* level 7 */ |
| 4040 | { 14, 14, 14, 6, 4, 6, ZSTD_lazy2 }, /* level 8.*/ |
| 4041 | { 14, 15, 14, 6, 4, 6, ZSTD_btlazy2 }, /* level 9.*/ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 4042 | { 14, 15, 14, 3, 3, 6, ZSTD_btopt }, /* level 10.*/ |
| 4043 | { 14, 15, 14, 6, 3, 8, ZSTD_btopt }, /* level 11.*/ |
| 4044 | { 14, 15, 14, 6, 3, 16, ZSTD_btopt }, /* level 12.*/ |
| 4045 | { 14, 15, 14, 6, 3, 24, ZSTD_btopt }, /* level 13.*/ |
| 4046 | { 14, 15, 15, 6, 3, 48, ZSTD_btopt }, /* level 14.*/ |
| 4047 | { 14, 15, 15, 6, 3, 64, ZSTD_btopt }, /* level 15.*/ |
| 4048 | { 14, 15, 15, 6, 3, 96, ZSTD_btopt }, /* level 16.*/ |
| 4049 | { 14, 15, 15, 6, 3,128, ZSTD_btopt }, /* level 17.*/ |
| 4050 | { 14, 15, 15, 6, 3,256, ZSTD_btopt }, /* level 18.*/ |
| 4051 | { 14, 15, 15, 7, 3,256, ZSTD_btopt }, /* level 19.*/ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 4052 | { 14, 15, 15, 8, 3,256, ZSTD_btultra }, /* level 20.*/ |
| 4053 | { 14, 15, 15, 9, 3,256, ZSTD_btultra }, /* level 21.*/ |
| 4054 | { 14, 15, 15, 10, 3,256, ZSTD_btultra }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 4055 | }, |
| 4056 | }; |
| 4057 | |
Yann Collet | 236d94f | 2016-05-18 12:06:33 +0200 | [diff] [blame] | 4058 | /*! ZSTD_getCParams() : |
| 4059 | * @return ZSTD_compressionParameters structure for a selected compression level, `srcSize` and `dictSize`. |
| 4060 | * Size values are optional, provide 0 if not known or unused */ |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 4061 | 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] | 4062 | { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 4063 | size_t const addedSize = srcSizeHint ? 0 : 500; |
| 4064 | U64 const rSize = srcSizeHint+dictSize ? srcSizeHint+dictSize+addedSize : (U64)-1; |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 4065 | 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] | 4066 | 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] | 4067 | if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; |
Yann Collet | e51d51b | 2017-06-20 17:44:55 -0700 | [diff] [blame] | 4068 | { ZSTD_compressionParameters const cp = ZSTD_defaultCParameters[tableID][compressionLevel]; |
| 4069 | return ZSTD_adjustCParams_internal(cp, srcSizeHint, dictSize); } |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 4070 | } |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 4071 | |
| 4072 | /*! ZSTD_getParams() : |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 4073 | * 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] | 4074 | * All fields of `ZSTD_frameParameters` are set to default (0) */ |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 4075 | 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] | 4076 | ZSTD_parameters params; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 4077 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, srcSizeHint, dictSize); |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 4078 | memset(¶ms, 0, sizeof(params)); |
| 4079 | params.cParams = cParams; |
| 4080 | return params; |
| 4081 | } |