Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 5 | * This source code is licensed under both the BSD-style license (found in the |
| 6 | * LICENSE file in the root directory of this source tree) and the GPLv2 (found |
| 7 | * in the COPYING file in the root directory of this source tree). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 10 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 11 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 12 | /*-************************************* |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 13 | * Tuning parameters |
| 14 | ***************************************/ |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 15 | #ifndef ZSTD_CLEVEL_DEFAULT |
| 16 | # define ZSTD_CLEVEL_DEFAULT 3 |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 17 | #endif |
| 18 | |
Yann Collet | bf99150 | 2017-06-19 12:56:25 -0700 | [diff] [blame] | 19 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 20 | /*-************************************* |
Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 21 | * Dependencies |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 22 | ***************************************/ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 23 | #include <string.h> /* memset */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 24 | #include "mem.h" |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 25 | #define FSE_STATIC_LINKING_ONLY /* FSE_encodeSymbol */ |
Yann Collet | d0e2cd1 | 2016-06-05 00:58:01 +0200 | [diff] [blame] | 26 | #include "fse.h" |
Yann Collet | 130fe11 | 2016-06-05 00:42:28 +0200 | [diff] [blame] | 27 | #define HUF_STATIC_LINKING_ONLY |
| 28 | #include "huf.h" |
Nick Terrell | 721726d | 2017-09-01 18:28:35 -0700 | [diff] [blame] | 29 | #include "zstd_compress.h" |
| 30 | #include "zstd_fast.h" |
| 31 | #include "zstd_double_fast.h" |
| 32 | #include "zstd_lazy.h" |
| 33 | #include "zstd_opt.h" |
Stella Lau | 360428c | 2017-09-06 17:56:01 -0700 | [diff] [blame] | 34 | #include "zstd_ldm.h" |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 35 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 36 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 37 | /*-************************************* |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 38 | * Helper functions |
| 39 | ***************************************/ |
Yann Collet | 3f75d52 | 2017-03-31 17:11:38 -0700 | [diff] [blame] | 40 | size_t ZSTD_compressBound(size_t srcSize) { |
Yann Collet | 5b10345 | 2017-09-29 23:17:41 -0700 | [diff] [blame] | 41 | return ZSTD_COMPRESSBOUND(srcSize); |
Yann Collet | 3f75d52 | 2017-03-31 17:11:38 -0700 | [diff] [blame] | 42 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 43 | |
| 44 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 45 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 46 | * Sequence storage |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 47 | ***************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 48 | static void ZSTD_resetSeqStore(seqStore_t* ssPtr) |
| 49 | { |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 50 | ssPtr->lit = ssPtr->litStart; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 51 | ssPtr->sequences = ssPtr->sequencesStart; |
Yann Collet | 5d39357 | 2016-04-07 17:19:00 +0200 | [diff] [blame] | 52 | ssPtr->longLengthID = 0; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 56 | /*-************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 57 | * Context memory management |
| 58 | ***************************************/ |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 59 | struct ZSTD_CDict_s { |
| 60 | void* dictBuffer; |
| 61 | const void* dictContent; |
| 62 | size_t dictContentSize; |
| 63 | ZSTD_CCtx* refContext; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 64 | }; /* typedef'd to ZSTD_CDict within "zstd.h" */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 65 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 66 | ZSTD_CCtx* ZSTD_createCCtx(void) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 67 | { |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 68 | return ZSTD_createCCtx_advanced(ZSTD_defaultCMem); |
inikep | 50e82c0 | 2016-05-23 15:49:09 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem) |
| 72 | { |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 73 | ZSTD_CCtx* cctx; |
inikep | 50e82c0 | 2016-05-23 15:49:09 +0200 | [diff] [blame] | 74 | |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 75 | if (!customMem.customAlloc ^ !customMem.customFree) return NULL; |
inikep | 107e243 | 2016-05-23 16:24:52 +0200 | [diff] [blame] | 76 | |
Yann Collet | c4f46b9 | 2017-05-30 17:45:37 -0700 | [diff] [blame] | 77 | cctx = (ZSTD_CCtx*) ZSTD_calloc(sizeof(ZSTD_CCtx), customMem); |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 78 | if (!cctx) return NULL; |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 79 | cctx->customMem = customMem; |
Stella Lau | 97e27af | 2017-08-18 11:20:08 -0700 | [diff] [blame] | 80 | cctx->requestedParams.compressionLevel = ZSTD_CLEVEL_DEFAULT; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 81 | ZSTD_STATIC_ASSERT(zcss_init==0); |
Yann Collet | d3de3d5 | 2017-06-16 16:51:33 -0700 | [diff] [blame] | 82 | ZSTD_STATIC_ASSERT(ZSTD_CONTENTSIZE_UNKNOWN==(0ULL - 1)); |
Yann Collet | 69c2cdb | 2016-07-14 16:52:45 +0200 | [diff] [blame] | 83 | return cctx; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 86 | ZSTD_CCtx* ZSTD_initStaticCCtx(void *workspace, size_t workspaceSize) |
| 87 | { |
Yann Collet | 335780c | 2017-09-13 16:35:29 -0700 | [diff] [blame] | 88 | ZSTD_CCtx* const cctx = (ZSTD_CCtx*) workspace; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 89 | if (workspaceSize <= sizeof(ZSTD_CCtx)) return NULL; /* minimum size */ |
| 90 | if ((size_t)workspace & 7) return NULL; /* must be 8-aligned */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 91 | 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] | 92 | cctx->staticSize = workspaceSize; |
| 93 | cctx->workSpace = (void*)(cctx+1); |
| 94 | cctx->workSpaceSize = workspaceSize - sizeof(ZSTD_CCtx); |
| 95 | |
| 96 | /* entropy space (never moves) */ |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 97 | if (cctx->workSpaceSize < sizeof(ZSTD_entropyCTables_t)) return NULL; |
Yann Collet | 335780c | 2017-09-13 16:35:29 -0700 | [diff] [blame] | 98 | assert(((size_t)cctx->workSpace & (sizeof(void*)-1)) == 0); /* ensure correct alignment */ |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 99 | cctx->entropy = (ZSTD_entropyCTables_t*)cctx->workSpace; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 100 | |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 101 | return cctx; |
| 102 | } |
| 103 | |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 104 | size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 105 | { |
inikep | 3640396 | 2016-06-03 16:36:50 +0200 | [diff] [blame] | 106 | if (cctx==NULL) return 0; /* support free on NULL */ |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 107 | if (cctx->staticSize) return ERROR(memory_allocation); /* not compatible with static CCtx */ |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 108 | ZSTD_free(cctx->workSpace, cctx->customMem); |
Yann Collet | 7855366 | 2017-05-08 17:15:00 -0700 | [diff] [blame] | 109 | cctx->workSpace = NULL; |
| 110 | ZSTD_freeCDict(cctx->cdictLocal); |
| 111 | cctx->cdictLocal = NULL; |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 112 | #ifdef ZSTD_MULTITHREAD |
Yann Collet | c4a5a21 | 2017-06-01 17:56:14 -0700 | [diff] [blame] | 113 | ZSTDMT_freeCCtx(cctx->mtctx); |
| 114 | cctx->mtctx = NULL; |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 115 | #endif |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 116 | ZSTD_free(cctx, cctx->customMem); |
Yann Collet | 982ffc7 | 2016-02-05 02:33:10 +0100 | [diff] [blame] | 117 | return 0; /* reserved as a potential error code in the future */ |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 120 | |
| 121 | static size_t ZSTD_sizeof_mtctx(const ZSTD_CCtx* cctx) |
| 122 | { |
| 123 | #ifdef ZSTD_MULTITHREAD |
| 124 | return ZSTDMT_sizeof_CCtx(cctx->mtctx); |
| 125 | #else |
| 126 | (void) cctx; |
| 127 | return 0; |
| 128 | #endif |
| 129 | } |
| 130 | |
| 131 | |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 132 | size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx) |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 133 | { |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 134 | if (cctx==NULL) return 0; /* support sizeof on NULL */ |
Yann Collet | 7d1ff38 | 2017-09-18 14:47:34 -0700 | [diff] [blame] | 135 | DEBUGLOG(3, "sizeof(*cctx) : %u", (U32)sizeof(*cctx)); |
| 136 | DEBUGLOG(3, "workSpaceSize (including streaming buffers): %u", (U32)cctx->workSpaceSize); |
| 137 | DEBUGLOG(3, "inner cdict : %u", (U32)ZSTD_sizeof_CDict(cctx->cdictLocal)); |
| 138 | DEBUGLOG(3, "inner MTCTX : %u", (U32)ZSTD_sizeof_mtctx(cctx)); |
Yann Collet | 791d744 | 2017-05-08 16:17:30 -0700 | [diff] [blame] | 139 | return sizeof(*cctx) + cctx->workSpaceSize |
| 140 | + ZSTD_sizeof_CDict(cctx->cdictLocal) |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 141 | + ZSTD_sizeof_mtctx(cctx); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 142 | } |
| 143 | |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 144 | size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs) |
| 145 | { |
| 146 | return ZSTD_sizeof_CCtx(zcs); /* same object */ |
| 147 | } |
| 148 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 149 | /* private API call, for dictBuilder only */ |
| 150 | const seqStore_t* ZSTD_getSeqStore(const ZSTD_CCtx* ctx) { return &(ctx->seqStore); } |
| 151 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 152 | #define ZSTD_CLEVEL_CUSTOM 999 |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 153 | |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 154 | static ZSTD_compressionParameters ZSTD_getCParamsFromCCtxParams( |
| 155 | ZSTD_CCtx_params params, U64 srcSizeHint, size_t dictSize) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 156 | { |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 157 | return (params.compressionLevel == ZSTD_CLEVEL_CUSTOM ? |
| 158 | params.cParams : |
| 159 | ZSTD_getCParams(params.compressionLevel, srcSizeHint, dictSize)); |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Stella Lau | 1c81f72 | 2017-08-23 15:47:15 -0700 | [diff] [blame] | 162 | static void ZSTD_cLevelToCCtxParams_srcSize(ZSTD_CCtx_params* params, U64 srcSize) |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 163 | { |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 164 | params->cParams = ZSTD_getCParamsFromCCtxParams(*params, srcSize, 0); |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 165 | params->compressionLevel = ZSTD_CLEVEL_CUSTOM; |
| 166 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 167 | |
Yann Collet | add66f8 | 2017-05-12 15:59:48 -0700 | [diff] [blame] | 168 | static void ZSTD_cLevelToCParams(ZSTD_CCtx* cctx) |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 169 | { |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 170 | ZSTD_cLevelToCCtxParams_srcSize( |
| 171 | &cctx->requestedParams, cctx->pledgedSrcSizePlusOne-1); |
| 172 | } |
| 173 | |
| 174 | static void ZSTD_cLevelToCCtxParams(ZSTD_CCtx_params* params) |
| 175 | { |
| 176 | ZSTD_cLevelToCCtxParams_srcSize(params, 0); |
Yann Collet | 7d36028 | 2016-02-12 00:07:30 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Stella Lau | 91b30db | 2017-08-21 10:09:06 -0700 | [diff] [blame] | 179 | static ZSTD_CCtx_params ZSTD_makeCCtxParamsFromCParams( |
| 180 | ZSTD_compressionParameters cParams) |
| 181 | { |
| 182 | ZSTD_CCtx_params cctxParams; |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 183 | memset(&cctxParams, 0, sizeof(cctxParams)); |
Stella Lau | 91b30db | 2017-08-21 10:09:06 -0700 | [diff] [blame] | 184 | cctxParams.cParams = cParams; |
| 185 | cctxParams.compressionLevel = ZSTD_CLEVEL_CUSTOM; |
| 186 | return cctxParams; |
| 187 | } |
| 188 | |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 189 | static ZSTD_CCtx_params* ZSTD_createCCtxParams_advanced( |
| 190 | ZSTD_customMem customMem) |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 191 | { |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 192 | ZSTD_CCtx_params* params; |
| 193 | if (!customMem.customAlloc ^ !customMem.customFree) return NULL; |
| 194 | params = (ZSTD_CCtx_params*)ZSTD_calloc( |
| 195 | sizeof(ZSTD_CCtx_params), customMem); |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 196 | if (!params) { return NULL; } |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 197 | params->customMem = customMem; |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 198 | params->compressionLevel = ZSTD_CLEVEL_DEFAULT; |
| 199 | return params; |
| 200 | } |
| 201 | |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 202 | ZSTD_CCtx_params* ZSTD_createCCtxParams(void) |
| 203 | { |
| 204 | return ZSTD_createCCtxParams_advanced(ZSTD_defaultCMem); |
| 205 | } |
| 206 | |
| 207 | size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params) |
| 208 | { |
| 209 | if (params == NULL) { return 0; } |
| 210 | ZSTD_free(params, params->customMem); |
| 211 | return 0; |
| 212 | } |
| 213 | |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 214 | size_t ZSTD_resetCCtxParams(ZSTD_CCtx_params* params) |
| 215 | { |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 216 | return ZSTD_initCCtxParams(params, ZSTD_CLEVEL_DEFAULT); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Stella Lau | 0744592 | 2017-08-25 13:23:16 -0700 | [diff] [blame] | 219 | size_t ZSTD_initCCtxParams(ZSTD_CCtx_params* cctxParams, int compressionLevel) { |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 220 | if (!cctxParams) { return ERROR(GENERIC); } |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 221 | memset(cctxParams, 0, sizeof(*cctxParams)); |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 222 | cctxParams->compressionLevel = compressionLevel; |
| 223 | return 0; |
Stella Lau | 0744592 | 2017-08-25 13:23:16 -0700 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | size_t ZSTD_initCCtxParams_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params) |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 227 | { |
Stella Lau | 560b34f | 2017-08-21 11:44:58 -0700 | [diff] [blame] | 228 | if (!cctxParams) { return ERROR(GENERIC); } |
Stella Lau | fd8a257 | 2017-08-21 13:23:35 -0700 | [diff] [blame] | 229 | CHECK_F( ZSTD_checkCParams(params.cParams) ); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 230 | memset(cctxParams, 0, sizeof(*cctxParams)); |
Stella Lau | f181f33 | 2017-08-21 01:59:08 -0700 | [diff] [blame] | 231 | cctxParams->cParams = params.cParams; |
| 232 | cctxParams->fParams = params.fParams; |
| 233 | cctxParams->compressionLevel = ZSTD_CLEVEL_CUSTOM; |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
Stella Lau | 1130377 | 2017-08-22 14:53:13 -0700 | [diff] [blame] | 237 | static ZSTD_CCtx_params ZSTD_assignParamsToCCtxParams( |
| 238 | ZSTD_CCtx_params cctxParams, ZSTD_parameters params) |
| 239 | { |
| 240 | ZSTD_CCtx_params ret = cctxParams; |
| 241 | ret.cParams = params.cParams; |
| 242 | ret.fParams = params.fParams; |
Nick Terrell | 86e83e9 | 2017-10-02 13:43:30 -0700 | [diff] [blame] | 243 | ret.compressionLevel = ZSTD_CLEVEL_CUSTOM; |
Stella Lau | 1130377 | 2017-08-22 14:53:13 -0700 | [diff] [blame] | 244 | return ret; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 245 | } |
| 246 | |
Yann Collet | 2bd6440 | 2017-07-13 17:12:16 -0700 | [diff] [blame] | 247 | #define CLAMPCHECK(val,min,max) { \ |
| 248 | if (((val)<(min)) | ((val)>(max))) { \ |
| 249 | return ERROR(parameter_outOfBound); \ |
Yann Collet | c3bce24 | 2017-06-20 16:09:11 -0700 | [diff] [blame] | 250 | } } |
| 251 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 252 | size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value) |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 253 | { |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 254 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 255 | |
| 256 | switch(param) |
| 257 | { |
Yann Collet | 7c3dea4 | 2017-09-24 15:57:29 -0700 | [diff] [blame] | 258 | case ZSTD_p_format : |
| 259 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
| 260 | |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 261 | case ZSTD_p_compressionLevel: |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 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); |
Stella Lau | f181f33 | 2017-08-21 01:59:08 -0700 | [diff] [blame] | 264 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 265 | |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 266 | case ZSTD_p_windowLog: |
| 267 | case ZSTD_p_hashLog: |
| 268 | case ZSTD_p_chainLog: |
| 269 | case ZSTD_p_searchLog: |
| 270 | case ZSTD_p_minMatch: |
| 271 | case ZSTD_p_targetLength: |
| 272 | case ZSTD_p_compressionStrategy: |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 273 | 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] | 274 | if (cctx->cdict) return ERROR(stage_wrong); |
Stella Lau | 91b30db | 2017-08-21 10:09:06 -0700 | [diff] [blame] | 275 | ZSTD_cLevelToCParams(cctx); /* Can optimize if srcSize is known */ |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 276 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 277 | |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 278 | case ZSTD_p_contentSizeFlag: |
| 279 | case ZSTD_p_checksumFlag: |
| 280 | case ZSTD_p_dictIDFlag: |
| 281 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Yann Collet | fecc721 | 2017-06-27 11:46:39 -0700 | [diff] [blame] | 282 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 283 | case ZSTD_p_forceMaxWindow : /* Force back-references to remain < windowSize, |
| 284 | * even when referencing into Dictionary content |
| 285 | * default : 0 when using a CDict, 1 when using a Prefix */ |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 286 | cctx->loadedDictEnd = 0; |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 287 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 288 | |
| 289 | case ZSTD_p_nbThreads: |
| 290 | if (value==0) return 0; |
Yann Collet | 33873f0 | 2017-06-16 12:04:21 -0700 | [diff] [blame] | 291 | DEBUGLOG(5, " setting nbThreads : %u", value); |
Stella Lau | 4e83572 | 2017-08-29 16:18:21 -0700 | [diff] [blame] | 292 | if (value > 1 && cctx->staticSize) { |
| 293 | return ERROR(parameter_unsupported); /* MT not compatible with static alloc */ |
Yann Collet | 33873f0 | 2017-06-16 12:04:21 -0700 | [diff] [blame] | 294 | } |
Stella Lau | 4e83572 | 2017-08-29 16:18:21 -0700 | [diff] [blame] | 295 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 296 | |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 297 | case ZSTD_p_jobSize: |
Stella Lau | 9911153 | 2017-08-25 13:14:51 -0700 | [diff] [blame] | 298 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Yann Collet | c35e535 | 2017-06-01 18:44:06 -0700 | [diff] [blame] | 299 | |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 300 | case ZSTD_p_overlapSizeLog: |
Stella Lau | 81d89d8 | 2017-08-18 12:08:57 -0700 | [diff] [blame] | 301 | DEBUGLOG(5, " setting overlap with nbThreads == %u", cctx->requestedParams.nbThreads); |
Stella Lau | 9911153 | 2017-08-25 13:14:51 -0700 | [diff] [blame] | 302 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 303 | |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 304 | case ZSTD_p_enableLongDistanceMatching: |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 305 | if (cctx->cdict) return ERROR(stage_wrong); |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 306 | if (value != 0) { |
| 307 | ZSTD_cLevelToCParams(cctx); |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 308 | } |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 309 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 310 | |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 311 | case ZSTD_p_ldmHashLog: |
| 312 | case ZSTD_p_ldmMinMatch: |
| 313 | if (value == 0) return 0; /* special value : 0 means "don't change anything" */ |
| 314 | if (cctx->cdict) return ERROR(stage_wrong); |
| 315 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
| 316 | |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 317 | case ZSTD_p_ldmBucketSizeLog: |
Stella Lau | a1f04d5 | 2017-09-01 14:52:51 -0700 | [diff] [blame] | 318 | case ZSTD_p_ldmHashEveryLog: |
| 319 | if (cctx->cdict) return ERROR(stage_wrong); |
| 320 | return ZSTD_CCtxParam_setParameter(&cctx->requestedParams, param, value); |
| 321 | |
Yann Collet | 2bd6440 | 2017-07-13 17:12:16 -0700 | [diff] [blame] | 322 | default: return ERROR(parameter_unsupported); |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 323 | } |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 324 | } |
| 325 | |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 326 | size_t ZSTD_CCtxParam_setParameter( |
| 327 | ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value) |
| 328 | { |
| 329 | switch(param) |
| 330 | { |
Yann Collet | 7c3dea4 | 2017-09-24 15:57:29 -0700 | [diff] [blame] | 331 | case ZSTD_p_format : |
Yann Collet | c994932 | 2017-09-27 12:22:22 -0700 | [diff] [blame] | 332 | if (value > (unsigned)ZSTD_f_zstd1_magicless) |
Yann Collet | 7c3dea4 | 2017-09-24 15:57:29 -0700 | [diff] [blame] | 333 | return ERROR(parameter_unsupported); |
| 334 | params->format = (ZSTD_format_e)value; |
| 335 | return 0; |
| 336 | |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 337 | case ZSTD_p_compressionLevel : |
| 338 | if ((int)value > ZSTD_maxCLevel()) value = ZSTD_maxCLevel(); |
| 339 | if (value == 0) return 0; |
| 340 | params->compressionLevel = value; |
| 341 | return 0; |
| 342 | |
| 343 | case ZSTD_p_windowLog : |
| 344 | if (value == 0) return 0; |
| 345 | CLAMPCHECK(value, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
| 346 | ZSTD_cLevelToCCtxParams(params); |
| 347 | params->cParams.windowLog = value; |
| 348 | return 0; |
| 349 | |
| 350 | case ZSTD_p_hashLog : |
| 351 | if (value == 0) return 0; |
| 352 | CLAMPCHECK(value, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 353 | ZSTD_cLevelToCCtxParams(params); |
| 354 | params->cParams.hashLog = value; |
| 355 | return 0; |
| 356 | |
| 357 | case ZSTD_p_chainLog : |
| 358 | if (value == 0) return 0; |
| 359 | CLAMPCHECK(value, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); |
| 360 | ZSTD_cLevelToCCtxParams(params); |
| 361 | params->cParams.chainLog = value; |
| 362 | return 0; |
| 363 | |
| 364 | case ZSTD_p_searchLog : |
| 365 | if (value == 0) return 0; |
| 366 | CLAMPCHECK(value, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
| 367 | ZSTD_cLevelToCCtxParams(params); |
| 368 | params->cParams.searchLog = value; |
| 369 | return 0; |
| 370 | |
| 371 | case ZSTD_p_minMatch : |
| 372 | if (value == 0) return 0; |
| 373 | CLAMPCHECK(value, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
| 374 | ZSTD_cLevelToCCtxParams(params); |
| 375 | params->cParams.searchLength = value; |
| 376 | return 0; |
| 377 | |
| 378 | case ZSTD_p_targetLength : |
| 379 | if (value == 0) return 0; |
| 380 | CLAMPCHECK(value, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
| 381 | ZSTD_cLevelToCCtxParams(params); |
| 382 | params->cParams.targetLength = value; |
| 383 | return 0; |
| 384 | |
| 385 | case ZSTD_p_compressionStrategy : |
| 386 | if (value == 0) return 0; |
| 387 | CLAMPCHECK(value, (unsigned)ZSTD_fast, (unsigned)ZSTD_btultra); |
| 388 | ZSTD_cLevelToCCtxParams(params); |
| 389 | params->cParams.strategy = (ZSTD_strategy)value; |
| 390 | return 0; |
| 391 | |
| 392 | case ZSTD_p_contentSizeFlag : |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 393 | /* Content size written in frame header _when known_ (default:1) */ |
| 394 | DEBUGLOG(5, "set content size flag = %u", (value>0)); |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 395 | params->fParams.contentSizeFlag = value > 0; |
| 396 | return 0; |
| 397 | |
| 398 | case ZSTD_p_checksumFlag : |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 399 | /* A 32-bits content checksum will be calculated and written at end of frame (default:0) */ |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 400 | params->fParams.checksumFlag = value > 0; |
| 401 | return 0; |
| 402 | |
Stella Lau | f181f33 | 2017-08-21 01:59:08 -0700 | [diff] [blame] | 403 | case ZSTD_p_dictIDFlag : /* When applicable, dictionary's dictID is provided in frame header (default:1) */ |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 404 | DEBUGLOG(5, "set dictIDFlag = %u", (value>0)); |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 405 | params->fParams.noDictIDFlag = (value == 0); |
| 406 | return 0; |
| 407 | |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 408 | case ZSTD_p_forceMaxWindow : |
Stella Lau | b6cb2ed | 2017-08-18 11:43:31 -0700 | [diff] [blame] | 409 | params->forceWindow = value > 0; |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 410 | return 0; |
| 411 | |
| 412 | case ZSTD_p_nbThreads : |
Stella Lau | 4e83572 | 2017-08-29 16:18:21 -0700 | [diff] [blame] | 413 | if (value == 0) return 0; |
Stella Lau | 81d89d8 | 2017-08-18 12:08:57 -0700 | [diff] [blame] | 414 | #ifndef ZSTD_MULTITHREAD |
| 415 | if (value > 1) return ERROR(parameter_unsupported); |
Yann Collet | f325ee4 | 2017-09-11 14:37:03 -0700 | [diff] [blame] | 416 | return 0; |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 417 | #else |
Stella Lau | 9911153 | 2017-08-25 13:14:51 -0700 | [diff] [blame] | 418 | return ZSTDMT_initializeCCtxParameters(params, value); |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 419 | #endif |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 420 | |
| 421 | case ZSTD_p_jobSize : |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 422 | #ifndef ZSTD_MULTITHREAD |
| 423 | return ERROR(parameter_unsupported); |
| 424 | #else |
Stella Lau | 1c0dbe8 | 2017-08-21 13:18:00 -0700 | [diff] [blame] | 425 | if (params->nbThreads <= 1) return ERROR(parameter_unsupported); |
Stella Lau | 9911153 | 2017-08-25 13:14:51 -0700 | [diff] [blame] | 426 | return ZSTDMT_CCtxParam_setMTCtxParameter(params, ZSTDMT_p_sectionSize, value); |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 427 | #endif |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 428 | |
| 429 | case ZSTD_p_overlapSizeLog : |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 430 | #ifndef ZSTD_MULTITHREAD |
| 431 | return ERROR(parameter_unsupported); |
| 432 | #else |
Stella Lau | 1c0dbe8 | 2017-08-21 13:18:00 -0700 | [diff] [blame] | 433 | if (params->nbThreads <= 1) return ERROR(parameter_unsupported); |
Stella Lau | 9911153 | 2017-08-25 13:14:51 -0700 | [diff] [blame] | 434 | return ZSTDMT_CCtxParam_setMTCtxParameter(params, ZSTDMT_p_overlapSectionLog, value); |
Yann Collet | 0d6ecc7 | 2017-09-11 14:09:34 -0700 | [diff] [blame] | 435 | #endif |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 436 | |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 437 | case ZSTD_p_enableLongDistanceMatching : |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 438 | if (value != 0) { |
| 439 | ZSTD_cLevelToCCtxParams(params); |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 440 | params->cParams.windowLog = ZSTD_LDM_DEFAULT_WINDOW_LOG; |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 441 | } |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 442 | return ZSTD_ldm_initializeParameters(¶ms->ldmParams, value); |
| 443 | |
| 444 | case ZSTD_p_ldmHashLog : |
| 445 | if (value == 0) return 0; |
| 446 | CLAMPCHECK(value, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 447 | params->ldmParams.hashLog = value; |
| 448 | return 0; |
| 449 | |
| 450 | case ZSTD_p_ldmMinMatch : |
| 451 | if (value == 0) return 0; |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 452 | CLAMPCHECK(value, ZSTD_LDM_MINMATCH_MIN, ZSTD_LDM_MINMATCH_MAX); |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 453 | params->ldmParams.minMatchLength = value; |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 454 | return 0; |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 455 | |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 456 | case ZSTD_p_ldmBucketSizeLog : |
| 457 | if (value > ZSTD_LDM_BUCKETSIZELOG_MAX) { |
| 458 | return ERROR(parameter_outOfBound); |
| 459 | } |
| 460 | params->ldmParams.bucketSizeLog = value; |
| 461 | return 0; |
| 462 | |
Stella Lau | a1f04d5 | 2017-09-01 14:52:51 -0700 | [diff] [blame] | 463 | case ZSTD_p_ldmHashEveryLog : |
| 464 | if (value > ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN) { |
| 465 | return ERROR(parameter_outOfBound); |
| 466 | } |
| 467 | params->ldmParams.hashEveryLog = value; |
| 468 | return 0; |
| 469 | |
Stella Lau | c022112 | 2017-08-17 19:30:22 -0700 | [diff] [blame] | 470 | default: return ERROR(parameter_unsupported); |
| 471 | } |
| 472 | } |
| 473 | |
Stella Lau | f181f33 | 2017-08-21 01:59:08 -0700 | [diff] [blame] | 474 | /** |
| 475 | * This function should be updated whenever ZSTD_CCtx_params is updated. |
| 476 | * Parameters are copied manually before the dictionary is loaded. |
| 477 | * The multithreading parameters jobSize and overlapSizeLog are set only if |
Stella Lau | 024098a | 2017-08-25 17:58:28 -0700 | [diff] [blame] | 478 | * nbThreads > 1. |
Stella Lau | 91b30db | 2017-08-21 10:09:06 -0700 | [diff] [blame] | 479 | * |
| 480 | * Pledged srcSize is treated as unknown. |
Stella Lau | f181f33 | 2017-08-21 01:59:08 -0700 | [diff] [blame] | 481 | */ |
Stella Lau | 82d636b | 2017-08-29 18:03:06 -0700 | [diff] [blame] | 482 | size_t ZSTD_CCtx_setParametersUsingCCtxParams( |
| 483 | ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params) |
Stella Lau | 97e27af | 2017-08-18 11:20:08 -0700 | [diff] [blame] | 484 | { |
Stella Lau | 4e83572 | 2017-08-29 16:18:21 -0700 | [diff] [blame] | 485 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
| 486 | if (cctx->cdict) return ERROR(stage_wrong); |
Stella Lau | 399ae01 | 2017-08-18 13:01:55 -0700 | [diff] [blame] | 487 | |
| 488 | /* Assume the compression and frame parameters are validated */ |
| 489 | cctx->requestedParams.cParams = params->cParams; |
| 490 | cctx->requestedParams.fParams = params->fParams; |
| 491 | cctx->requestedParams.compressionLevel = params->compressionLevel; |
| 492 | |
Stella Lau | 399ae01 | 2017-08-18 13:01:55 -0700 | [diff] [blame] | 493 | /* Set force window explicitly since it sets cctx->loadedDictEnd */ |
| 494 | CHECK_F( ZSTD_CCtx_setParameter( |
| 495 | cctx, ZSTD_p_forceMaxWindow, params->forceWindow) ); |
| 496 | |
| 497 | /* Set multithreading parameters explicitly */ |
| 498 | CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_nbThreads, params->nbThreads) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 499 | if (params->nbThreads > 1) { |
| 500 | CHECK_F( ZSTD_CCtx_setParameter(cctx, ZSTD_p_jobSize, params->jobSize) ); |
| 501 | CHECK_F( ZSTD_CCtx_setParameter( |
Stella Lau | 399ae01 | 2017-08-18 13:01:55 -0700 | [diff] [blame] | 502 | cctx, ZSTD_p_overlapSizeLog, params->overlapSizeLog) ); |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 503 | } |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 504 | |
Stella Lau | a1f04d5 | 2017-09-01 14:52:51 -0700 | [diff] [blame] | 505 | /* Copy long distance matching parameters */ |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 506 | cctx->requestedParams.ldmParams = params->ldmParams; |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 507 | |
Stella Lau | 5bc2c1e | 2017-08-23 12:03:30 -0700 | [diff] [blame] | 508 | /* customMem is used only for create/free params and can be ignored */ |
Stella Lau | 97e27af | 2017-08-18 11:20:08 -0700 | [diff] [blame] | 509 | return 0; |
| 510 | } |
| 511 | |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 512 | ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 513 | { |
Yann Collet | fbd5ab7 | 2017-09-29 19:40:27 -0700 | [diff] [blame] | 514 | DEBUGLOG(4, " setting pledgedSrcSize to %u", (U32)pledgedSrcSize); |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 515 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 516 | cctx->pledgedSrcSizePlusOne = pledgedSrcSize+1; |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 517 | return 0; |
| 518 | } |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 519 | |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 520 | size_t ZSTD_CCtx_loadDictionary_advanced( |
| 521 | ZSTD_CCtx* cctx, const void* dict, size_t dictSize, |
| 522 | ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictMode_e dictMode) |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 523 | { |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 524 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 525 | if (cctx->staticSize) return ERROR(memory_allocation); /* no malloc for static CCtx */ |
Yann Collet | fbd5ab7 | 2017-09-29 19:40:27 -0700 | [diff] [blame] | 526 | DEBUGLOG(4, "load dictionary of size %u", (U32)dictSize); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 527 | ZSTD_freeCDict(cctx->cdictLocal); /* in case one already exists */ |
| 528 | if (dict==NULL || dictSize==0) { /* no dictionary mode */ |
| 529 | cctx->cdictLocal = NULL; |
| 530 | cctx->cdict = NULL; |
| 531 | } else { |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 532 | ZSTD_compressionParameters const cParams = |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 533 | ZSTD_getCParamsFromCCtxParams(cctx->requestedParams, 0, dictSize); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 534 | cctx->cdictLocal = ZSTD_createCDict_advanced( |
| 535 | dict, dictSize, |
Stella Lau | ee65701 | 2017-08-29 20:27:35 -0700 | [diff] [blame] | 536 | dictLoadMethod, dictMode, |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 537 | cParams, cctx->customMem); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 538 | cctx->cdict = cctx->cdictLocal; |
| 539 | if (cctx->cdictLocal == NULL) |
| 540 | return ERROR(memory_allocation); |
| 541 | } |
| 542 | return 0; |
| 543 | } |
| 544 | |
Stella Lau | eb7bbab | 2017-08-25 10:48:07 -0700 | [diff] [blame] | 545 | ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference( |
| 546 | ZSTD_CCtx* cctx, const void* dict, size_t dictSize) |
| 547 | { |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 548 | return ZSTD_CCtx_loadDictionary_advanced( |
| 549 | cctx, dict, dictSize, ZSTD_dlm_byRef, ZSTD_dm_auto); |
Stella Lau | eb7bbab | 2017-08-25 10:48:07 -0700 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize) |
| 553 | { |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 554 | return ZSTD_CCtx_loadDictionary_advanced( |
| 555 | cctx, dict, dictSize, ZSTD_dlm_byCopy, ZSTD_dm_auto); |
Stella Lau | eb7bbab | 2017-08-25 10:48:07 -0700 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
Yann Collet | bd18c88 | 2017-06-16 10:17:50 -0700 | [diff] [blame] | 559 | size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) |
Yann Collet | b0edb7f | 2017-05-12 15:31:53 -0700 | [diff] [blame] | 560 | { |
Yann Collet | 24de7b0 | 2017-05-22 13:05:45 -0700 | [diff] [blame] | 561 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
| 562 | cctx->cdict = cdict; |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 563 | memset(&cctx->prefixDict, 0, sizeof(cctx->prefixDict)); /* exclusive */ |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 564 | return 0; |
| 565 | } |
| 566 | |
| 567 | size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize) |
| 568 | { |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 569 | return ZSTD_CCtx_refPrefix_advanced(cctx, prefix, prefixSize, ZSTD_dm_rawContent); |
| 570 | } |
| 571 | |
| 572 | size_t ZSTD_CCtx_refPrefix_advanced( |
| 573 | ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictMode_e dictMode) |
| 574 | { |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 575 | if (cctx->streamStage != zcss_init) return ERROR(stage_wrong); |
| 576 | cctx->cdict = NULL; /* prefix discards any prior cdict */ |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 577 | cctx->prefixDict.dict = prefix; |
| 578 | cctx->prefixDict.dictSize = prefixSize; |
| 579 | cctx->prefixDict.dictMode = dictMode; |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 580 | return 0; |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 581 | } |
| 582 | |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 583 | static void ZSTD_startNewCompression(ZSTD_CCtx* cctx) |
Yann Collet | bd18c88 | 2017-06-16 10:17:50 -0700 | [diff] [blame] | 584 | { |
| 585 | cctx->streamStage = zcss_init; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 586 | cctx->pledgedSrcSizePlusOne = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /*! ZSTD_CCtx_reset() : |
| 590 | * Also dumps dictionary */ |
| 591 | void ZSTD_CCtx_reset(ZSTD_CCtx* cctx) |
| 592 | { |
| 593 | ZSTD_startNewCompression(cctx); |
Yann Collet | bd18c88 | 2017-06-16 10:17:50 -0700 | [diff] [blame] | 594 | cctx->cdict = NULL; |
| 595 | } |
| 596 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 597 | /** ZSTD_checkCParams() : |
| 598 | control CParam values remain within authorized range. |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 599 | @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] | 600 | size_t ZSTD_checkCParams(ZSTD_compressionParameters cParams) |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 601 | { |
Yann Collet | 1535414 | 2016-04-04 04:22:53 +0200 | [diff] [blame] | 602 | CLAMPCHECK(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 603 | CLAMPCHECK(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 604 | CLAMPCHECK(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 605 | CLAMPCHECK(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
Yann Collet | 2e2e78d | 2017-03-29 16:02:47 -0700 | [diff] [blame] | 606 | CLAMPCHECK(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 607 | CLAMPCHECK(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
Yann Collet | 2bd6440 | 2017-07-13 17:12:16 -0700 | [diff] [blame] | 608 | if ((U32)(cParams.strategy) > (U32)ZSTD_btultra) |
| 609 | return ERROR(parameter_unsupported); |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 610 | return 0; |
| 611 | } |
| 612 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 613 | /** ZSTD_clampCParams() : |
| 614 | * make CParam values within valid range. |
| 615 | * @return : valid CParams */ |
| 616 | static ZSTD_compressionParameters ZSTD_clampCParams(ZSTD_compressionParameters cParams) |
| 617 | { |
| 618 | # define CLAMP(val,min,max) { \ |
| 619 | if (val<min) val=min; \ |
| 620 | else if (val>max) val=max; \ |
| 621 | } |
| 622 | CLAMP(cParams.windowLog, ZSTD_WINDOWLOG_MIN, ZSTD_WINDOWLOG_MAX); |
| 623 | CLAMP(cParams.chainLog, ZSTD_CHAINLOG_MIN, ZSTD_CHAINLOG_MAX); |
| 624 | CLAMP(cParams.hashLog, ZSTD_HASHLOG_MIN, ZSTD_HASHLOG_MAX); |
| 625 | CLAMP(cParams.searchLog, ZSTD_SEARCHLOG_MIN, ZSTD_SEARCHLOG_MAX); |
| 626 | CLAMP(cParams.searchLength, ZSTD_SEARCHLENGTH_MIN, ZSTD_SEARCHLENGTH_MAX); |
| 627 | CLAMP(cParams.targetLength, ZSTD_TARGETLENGTH_MIN, ZSTD_TARGETLENGTH_MAX); |
| 628 | if ((U32)(cParams.strategy) > (U32)ZSTD_btultra) cParams.strategy = ZSTD_btultra; |
| 629 | return cParams; |
| 630 | } |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 631 | |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 632 | /** ZSTD_cycleLog() : |
| 633 | * condition for correct operation : hashLog > 1 */ |
| 634 | static U32 ZSTD_cycleLog(U32 hashLog, ZSTD_strategy strat) |
| 635 | { |
| 636 | U32 const btScale = ((U32)strat >= (U32)ZSTD_btlazy2); |
| 637 | return hashLog - btScale; |
| 638 | } |
| 639 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 640 | /** ZSTD_adjustCParams_internal() : |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 641 | optimize `cPar` for a given input (`srcSize` and `dictSize`). |
Yann Collet | 86b4fe5 | 2017-09-28 18:14:28 -0700 | [diff] [blame] | 642 | mostly downsizing to reduce memory consumption and initialization latency. |
| 643 | Both `srcSize` and `dictSize` are optional (use 0 if unknown). |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 644 | Note : cPar is considered validated at this stage. Use ZSTD_checkCParams() to ensure that condition. */ |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 645 | 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] | 646 | { |
Yann Collet | 86b4fe5 | 2017-09-28 18:14:28 -0700 | [diff] [blame] | 647 | static const U64 minSrcSize = 513; /* (1<<9) + 1 */ |
| 648 | static const U64 maxWindowResize = 1ULL << (ZSTD_WINDOWLOG_MAX-1); |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 649 | assert(ZSTD_checkCParams(cPar)==0); |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 650 | |
Yann Collet | 86b4fe5 | 2017-09-28 18:14:28 -0700 | [diff] [blame] | 651 | if (dictSize && (srcSize+1<2) /* srcSize unknown */ ) |
| 652 | srcSize = minSrcSize; /* presumed small when there is a dictionary */ |
Nick Terrell | c5d6dde | 2017-09-30 14:17:32 -0700 | [diff] [blame] | 653 | else if (srcSize == 0) |
Yann Collet | dc40411 | 2017-09-30 15:02:40 -0700 | [diff] [blame] | 654 | srcSize = ZSTD_CONTENTSIZE_UNKNOWN; /* 0 == unknown : presumed large */ |
Yann Collet | 86b4fe5 | 2017-09-28 18:14:28 -0700 | [diff] [blame] | 655 | |
| 656 | /* resize windowLog if input is small enough, to use less memory */ |
| 657 | if ( (srcSize < maxWindowResize) |
| 658 | && (dictSize < maxWindowResize) ) { |
| 659 | U32 const tSize = (U32)(srcSize + dictSize); |
| 660 | static U32 const hashSizeMin = 1 << ZSTD_HASHLOG_MIN; |
| 661 | U32 const srcLog = (tSize < hashSizeMin) ? ZSTD_HASHLOG_MIN : |
| 662 | ZSTD_highbit32(tSize-1) + 1; |
| 663 | if (cPar.windowLog > srcLog) cPar.windowLog = srcLog; |
| 664 | } |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 665 | if (cPar.hashLog > cPar.windowLog) cPar.hashLog = cPar.windowLog; |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 666 | { U32 const cycleLog = ZSTD_cycleLog(cPar.chainLog, cPar.strategy); |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 667 | if (cycleLog > cPar.windowLog) |
| 668 | cPar.chainLog -= (cycleLog - cPar.windowLog); |
Yann Collet | c3a5c4b | 2016-12-12 00:47:30 +0100 | [diff] [blame] | 669 | } |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 670 | |
Yann Collet | 9b5b47a | 2017-09-28 01:25:40 -0700 | [diff] [blame] | 671 | if (cPar.windowLog < ZSTD_WINDOWLOG_ABSOLUTEMIN) |
| 672 | cPar.windowLog = ZSTD_WINDOWLOG_ABSOLUTEMIN; /* required for frame header */ |
Yann Collet | 70d1301 | 2016-06-01 18:45:34 +0200 | [diff] [blame] | 673 | |
| 674 | return cPar; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 675 | } |
| 676 | |
Yann Collet | 381e66c | 2017-06-16 17:29:35 -0700 | [diff] [blame] | 677 | ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize) |
| 678 | { |
| 679 | cPar = ZSTD_clampCParams(cPar); |
| 680 | return ZSTD_adjustCParams_internal(cPar, srcSize, dictSize); |
| 681 | } |
| 682 | |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 683 | size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params) |
Yann Collet | e74215e | 2016-03-19 16:09:09 +0100 | [diff] [blame] | 684 | { |
Stella Lau | 15fdeb9 | 2017-08-24 16:28:49 -0700 | [diff] [blame] | 685 | /* Estimate CCtx size is supported for single-threaded compression only. */ |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 686 | if (params->nbThreads > 1) { return ERROR(GENERIC); } |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 687 | { ZSTD_compressionParameters const cParams = |
| 688 | ZSTD_getCParamsFromCCtxParams(*params, 0, 0); |
Stella Lau | 15fdeb9 | 2017-08-24 16:28:49 -0700 | [diff] [blame] | 689 | size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << cParams.windowLog); |
| 690 | U32 const divider = (cParams.searchLength==3) ? 3 : 4; |
| 691 | size_t const maxNbSeq = blockSize / divider; |
| 692 | size_t const tokenSpace = blockSize + 11*maxNbSeq; |
| 693 | size_t const chainSize = |
Nick Terrell | d6abb28 | 2017-09-21 16:18:34 -0700 | [diff] [blame] | 694 | (cParams.strategy == ZSTD_fast) ? 0 : ((size_t)1 << cParams.chainLog); |
Stella Lau | 15fdeb9 | 2017-08-24 16:28:49 -0700 | [diff] [blame] | 695 | size_t const hSize = ((size_t)1) << cParams.hashLog; |
| 696 | U32 const hashLog3 = (cParams.searchLength>3) ? |
| 697 | 0 : MIN(ZSTD_HASHLOG3_MAX, cParams.windowLog); |
| 698 | size_t const h3Size = ((size_t)1) << hashLog3; |
| 699 | size_t const entropySpace = sizeof(ZSTD_entropyCTables_t); |
| 700 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 701 | |
Stella Lau | 15fdeb9 | 2017-08-24 16:28:49 -0700 | [diff] [blame] | 702 | size_t const optBudget = |
| 703 | ((MaxML+1) + (MaxLL+1) + (MaxOff+1) + (1<<Litbits))*sizeof(U32) |
| 704 | + (ZSTD_OPT_NUM+1)*(sizeof(ZSTD_match_t) + sizeof(ZSTD_optimal_t)); |
| 705 | size_t const optSpace = ((cParams.strategy == ZSTD_btopt) || (cParams.strategy == ZSTD_btultra)) ? optBudget : 0; |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 706 | |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 707 | size_t const ldmSpace = params->ldmParams.enableLdm ? |
| 708 | ZSTD_ldm_getTableSize(params->ldmParams.hashLog, |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 709 | params->ldmParams.bucketSizeLog) : 0; |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 710 | |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 711 | size_t const neededSpace = entropySpace + tableSpace + tokenSpace + |
| 712 | optSpace + ldmSpace; |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 713 | |
Stella Lau | 15fdeb9 | 2017-08-24 16:28:49 -0700 | [diff] [blame] | 714 | DEBUGLOG(5, "sizeof(ZSTD_CCtx) : %u", (U32)sizeof(ZSTD_CCtx)); |
| 715 | DEBUGLOG(5, "estimate workSpace : %u", (U32)neededSpace); |
| 716 | return sizeof(ZSTD_CCtx) + neededSpace; |
| 717 | } |
Stella Lau | ade95b8 | 2017-08-17 18:13:08 -0700 | [diff] [blame] | 718 | } |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 719 | |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 720 | size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams) |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 721 | { |
Stella Lau | 939f954 | 2017-08-21 12:57:18 -0700 | [diff] [blame] | 722 | ZSTD_CCtx_params const params = ZSTD_makeCCtxParamsFromCParams(cParams); |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 723 | return ZSTD_estimateCCtxSize_usingCCtxParams(¶ms); |
Yann Collet | 2e91dde | 2016-03-08 12:22:11 +0100 | [diff] [blame] | 724 | } |
| 725 | |
Yann Collet | 31af829 | 2017-06-26 15:52:39 -0700 | [diff] [blame] | 726 | size_t ZSTD_estimateCCtxSize(int compressionLevel) |
| 727 | { |
| 728 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, 0, 0); |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 729 | return ZSTD_estimateCCtxSize_usingCParams(cParams); |
Yann Collet | 31af829 | 2017-06-26 15:52:39 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 732 | size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 733 | { |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 734 | if (params->nbThreads > 1) { return ERROR(GENERIC); } |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 735 | { size_t const CCtxSize = ZSTD_estimateCCtxSize_usingCCtxParams(params); |
Stella Lau | 15fdeb9 | 2017-08-24 16:28:49 -0700 | [diff] [blame] | 736 | size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, (size_t)1 << params->cParams.windowLog); |
| 737 | size_t const inBuffSize = ((size_t)1 << params->cParams.windowLog) + blockSize; |
| 738 | size_t const outBuffSize = ZSTD_compressBound(blockSize) + 1; |
| 739 | size_t const streamingSize = inBuffSize + outBuffSize; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 740 | |
Stella Lau | 15fdeb9 | 2017-08-24 16:28:49 -0700 | [diff] [blame] | 741 | return CCtxSize + streamingSize; |
| 742 | } |
Stella Lau | ade95b8 | 2017-08-17 18:13:08 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 745 | size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 746 | { |
Stella Lau | 939f954 | 2017-08-21 12:57:18 -0700 | [diff] [blame] | 747 | ZSTD_CCtx_params const params = ZSTD_makeCCtxParamsFromCParams(cParams); |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 748 | return ZSTD_estimateCStreamSize_usingCCtxParams(¶ms); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Yann Collet | 0c9a915 | 2017-06-26 16:02:25 -0700 | [diff] [blame] | 751 | size_t ZSTD_estimateCStreamSize(int compressionLevel) { |
| 752 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, 0, 0); |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 753 | return ZSTD_estimateCStreamSize_usingCParams(cParams); |
Yann Collet | 0c9a915 | 2017-06-26 16:02:25 -0700 | [diff] [blame] | 754 | } |
| 755 | |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 756 | static U32 ZSTD_equivalentCParams(ZSTD_compressionParameters cParams1, |
| 757 | ZSTD_compressionParameters cParams2) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 758 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 759 | U32 bslog1 = MIN(cParams1.windowLog, ZSTD_BLOCKSIZELOG_MAX); |
| 760 | U32 bslog2 = MIN(cParams2.windowLog, ZSTD_BLOCKSIZELOG_MAX); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 761 | return (bslog1 == bslog2) /* same block size */ |
| 762 | & (cParams1.hashLog == cParams2.hashLog) |
| 763 | & (cParams1.chainLog == cParams2.chainLog) |
| 764 | & (cParams1.strategy == cParams2.strategy) /* opt parser space */ |
| 765 | & ((cParams1.searchLength==3) == (cParams2.searchLength==3)); /* hashlog3 space */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 766 | } |
| 767 | |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 768 | /** The parameters are equivalent if ldm is not enabled in both sets or |
| 769 | * all the parameters are equivalent. */ |
| 770 | static U32 ZSTD_equivalentLdmParams(ldmParams_t ldmParams1, |
| 771 | ldmParams_t ldmParams2) |
| 772 | { |
| 773 | return (!ldmParams1.enableLdm && !ldmParams2.enableLdm) || |
| 774 | (ldmParams1.enableLdm == ldmParams2.enableLdm && |
| 775 | ldmParams1.hashLog == ldmParams2.hashLog && |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 776 | ldmParams1.bucketSizeLog == ldmParams2.bucketSizeLog && |
Stella Lau | a1f04d5 | 2017-09-01 14:52:51 -0700 | [diff] [blame] | 777 | ldmParams1.minMatchLength == ldmParams2.minMatchLength && |
| 778 | ldmParams1.hashEveryLog == ldmParams2.hashEveryLog); |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 779 | } |
| 780 | |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 781 | /** Equivalence for resetCCtx purposes */ |
| 782 | static U32 ZSTD_equivalentParams(ZSTD_CCtx_params params1, |
| 783 | ZSTD_CCtx_params params2) |
| 784 | { |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 785 | return ZSTD_equivalentCParams(params1.cParams, params2.cParams) && |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 786 | ZSTD_equivalentLdmParams(params1.ldmParams, params2.ldmParams); |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 789 | /*! ZSTD_continueCCtx() : |
Yann Collet | c08e649 | 2017-06-19 18:25:35 -0700 | [diff] [blame] | 790 | * reuse CCtx without reset (note : requires no dictionary) */ |
Stella Lau | 97e27af | 2017-08-18 11:20:08 -0700 | [diff] [blame] | 791 | static size_t ZSTD_continueCCtx(ZSTD_CCtx* cctx, ZSTD_CCtx_params params, U64 pledgedSrcSize) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 792 | { |
| 793 | U32 const end = (U32)(cctx->nextSrc - cctx->base); |
Yann Collet | fbd5ab7 | 2017-09-29 19:40:27 -0700 | [diff] [blame] | 794 | DEBUGLOG(4, "continue mode"); |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 795 | cctx->appliedParams = params; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 796 | cctx->pledgedSrcSizePlusOne = pledgedSrcSize+1; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 797 | cctx->consumedSrcSize = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 798 | if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 799 | cctx->appliedParams.fParams.contentSizeFlag = 0; |
Yann Collet | fbd5ab7 | 2017-09-29 19:40:27 -0700 | [diff] [blame] | 800 | DEBUGLOG(4, "pledged content size : %u ; flag : %u", |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 801 | (U32)pledgedSrcSize, cctx->appliedParams.fParams.contentSizeFlag); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 802 | cctx->lowLimit = end; |
| 803 | cctx->dictLimit = end; |
| 804 | cctx->nextToUpdate = end+1; |
| 805 | cctx->stage = ZSTDcs_init; |
| 806 | cctx->dictID = 0; |
| 807 | cctx->loadedDictEnd = 0; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 808 | { int i; for (i=0; i<ZSTD_REP_NUM; i++) cctx->seqStore.rep[i] = repStartValue[i]; } |
Nick Terrell | 7a28b9e | 2017-07-17 15:29:11 -0700 | [diff] [blame] | 809 | cctx->optState.litLengthSum = 0; /* force reset of btopt stats */ |
Yann Collet | b624922 | 2016-09-06 09:54:22 +0200 | [diff] [blame] | 810 | XXH64_reset(&cctx->xxhState, 0); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 811 | return 0; |
| 812 | } |
| 813 | |
Yann Collet | b0739bc | 2017-05-22 17:45:15 -0700 | [diff] [blame] | 814 | typedef enum { ZSTDcrp_continue, ZSTDcrp_noMemset } ZSTD_compResetPolicy_e; |
Yann Collet | 5a77361 | 2017-07-03 15:21:24 -0700 | [diff] [blame] | 815 | typedef enum { ZSTDb_not_buffered, ZSTDb_buffered } ZSTD_buffered_policy_e; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 816 | |
Yann Collet | 30fb499 | 2017-04-18 14:08:50 -0700 | [diff] [blame] | 817 | /*! ZSTD_resetCCtx_internal() : |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 818 | note : `params` are assumed fully validated at this stage */ |
| 819 | static size_t ZSTD_resetCCtx_internal(ZSTD_CCtx* zc, |
Stella Lau | 97e27af | 2017-08-18 11:20:08 -0700 | [diff] [blame] | 820 | ZSTD_CCtx_params params, U64 pledgedSrcSize, |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 821 | ZSTD_compResetPolicy_e const crp, |
| 822 | ZSTD_buffered_policy_e const zbuff) |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 823 | { |
Yann Collet | db1668a | 2017-09-29 18:05:18 -0700 | [diff] [blame] | 824 | DEBUGLOG(4, "ZSTD_resetCCtx_internal"); |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 825 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | db1668a | 2017-09-29 18:05:18 -0700 | [diff] [blame] | 826 | DEBUGLOG(4, "pledgedSrcSize: %u", (U32)pledgedSrcSize); |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 827 | |
Yann Collet | b0739bc | 2017-05-22 17:45:15 -0700 | [diff] [blame] | 828 | if (crp == ZSTDcrp_continue) { |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 829 | if (ZSTD_equivalentParams(params, zc->appliedParams)) { |
Yann Collet | db1668a | 2017-09-29 18:05:18 -0700 | [diff] [blame] | 830 | DEBUGLOG(4, "ZSTD_equivalentParams()==1"); |
Stella Lau | a1f04d5 | 2017-09-01 14:52:51 -0700 | [diff] [blame] | 831 | assert(!(params.ldmParams.enableLdm && |
Stella Lau | 360428c | 2017-09-06 17:56:01 -0700 | [diff] [blame] | 832 | params.ldmParams.hashEveryLog == ZSTD_LDM_HASHEVERYLOG_NOTSET)); |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 833 | zc->entropy->hufCTable_repeatMode = HUF_repeat_none; |
Nick Terrell | 830ef41 | 2017-07-13 12:45:39 -0700 | [diff] [blame] | 834 | zc->entropy->offcode_repeatMode = FSE_repeat_none; |
| 835 | zc->entropy->matchlength_repeatMode = FSE_repeat_none; |
| 836 | zc->entropy->litlength_repeatMode = FSE_repeat_none; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 837 | return ZSTD_continueCCtx(zc, params, pledgedSrcSize); |
Yann Collet | b0739bc | 2017-05-22 17:45:15 -0700 | [diff] [blame] | 838 | } } |
inikep | 87d4f3d | 2016-03-02 15:56:24 +0100 | [diff] [blame] | 839 | |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 840 | if (params.ldmParams.enableLdm) { |
Stella Lau | 67d4a61 | 2017-09-02 21:10:36 -0700 | [diff] [blame] | 841 | /* Adjust long distance matching parameters */ |
Stella Lau | 360428c | 2017-09-06 17:56:01 -0700 | [diff] [blame] | 842 | ZSTD_ldm_adjustParameters(¶ms.ldmParams, params.cParams.windowLog); |
| 843 | assert(params.ldmParams.hashLog >= params.ldmParams.bucketSizeLog); |
Stella Lau | f902bf9 | 2017-09-11 14:55:29 -0700 | [diff] [blame] | 844 | assert(params.ldmParams.hashEveryLog < 32); |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 845 | zc->ldmState.hashPower = |
| 846 | ZSTD_ldm_getHashPower(params.ldmParams.minMatchLength); |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 847 | } |
| 848 | |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 849 | { 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] | 850 | U32 const divider = (params.cParams.searchLength==3) ? 3 : 4; |
| 851 | size_t const maxNbSeq = blockSize / divider; |
| 852 | size_t const tokenSpace = blockSize + 11*maxNbSeq; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 853 | size_t const chainSize = (params.cParams.strategy == ZSTD_fast) ? |
Nick Terrell | d6abb28 | 2017-09-21 16:18:34 -0700 | [diff] [blame] | 854 | 0 : ((size_t)1 << params.cParams.chainLog); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 855 | size_t const hSize = ((size_t)1) << params.cParams.hashLog; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 856 | U32 const hashLog3 = (params.cParams.searchLength>3) ? |
| 857 | 0 : MIN(ZSTD_HASHLOG3_MAX, params.cParams.windowLog); |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 858 | size_t const h3Size = ((size_t)1) << hashLog3; |
| 859 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 860 | size_t const buffOutSize = (zbuff==ZSTDb_buffered) ? ZSTD_compressBound(blockSize)+1 : 0; |
| 861 | 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] | 862 | void* ptr; |
Yann Collet | e74215e | 2016-03-19 16:09:09 +0100 | [diff] [blame] | 863 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 864 | /* Check if workSpace is large enough, alloc a new one if needed */ |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 865 | { size_t const entropySpace = sizeof(ZSTD_entropyCTables_t); |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 866 | 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] | 867 | + (ZSTD_OPT_NUM+1) * (sizeof(ZSTD_match_t)+sizeof(ZSTD_optimal_t)); |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 868 | size_t const optSpace = ( (params.cParams.strategy == ZSTD_btopt) |
| 869 | || (params.cParams.strategy == ZSTD_btultra)) ? |
| 870 | optPotentialSpace : 0; |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 871 | size_t const bufferSpace = buffInSize + buffOutSize; |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 872 | size_t const ldmSpace = params.ldmParams.enableLdm |
| 873 | ? ZSTD_ldm_getTableSize(params.ldmParams.hashLog, params.ldmParams.bucketSizeLog) |
| 874 | : 0; |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 875 | size_t const neededSpace = entropySpace + optSpace + ldmSpace + |
| 876 | tableSpace + tokenSpace + bufferSpace; |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 877 | |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 878 | if (zc->workSpaceSize < neededSpace) { /* too small : resize */ |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 879 | DEBUGLOG(5, "Need to update workSpaceSize from %uK to %uK \n", |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 880 | (unsigned)zc->workSpaceSize>>10, |
| 881 | (unsigned)neededSpace>>10); |
| 882 | /* static cctx : no resize, error out */ |
| 883 | if (zc->staticSize) return ERROR(memory_allocation); |
| 884 | |
Yann Collet | 0181fef | 2017-04-06 01:25:26 -0700 | [diff] [blame] | 885 | zc->workSpaceSize = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 886 | ZSTD_free(zc->workSpace, zc->customMem); |
| 887 | zc->workSpace = ZSTD_malloc(neededSpace, zc->customMem); |
| 888 | if (zc->workSpace == NULL) return ERROR(memory_allocation); |
| 889 | zc->workSpaceSize = neededSpace; |
Yann Collet | 7bb60b1 | 2017-04-20 17:38:56 -0700 | [diff] [blame] | 890 | ptr = zc->workSpace; |
| 891 | |
| 892 | /* entropy space */ |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 893 | assert(((size_t)zc->workSpace & 3) == 0); /* ensure correct alignment */ |
| 894 | assert(zc->workSpaceSize >= sizeof(ZSTD_entropyCTables_t)); |
| 895 | zc->entropy = (ZSTD_entropyCTables_t*)zc->workSpace; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 896 | } } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 897 | |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 898 | /* init params */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 899 | zc->appliedParams = params; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 900 | zc->pledgedSrcSizePlusOne = pledgedSrcSize+1; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 901 | zc->consumedSrcSize = 0; |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 902 | if (pledgedSrcSize == ZSTD_CONTENTSIZE_UNKNOWN) |
Yann Collet | cc9f9b7 | 2017-06-15 18:17:10 -0700 | [diff] [blame] | 903 | zc->appliedParams.fParams.contentSizeFlag = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 904 | DEBUGLOG(5, "pledged content size : %u ; flag : %u", |
| 905 | (U32)pledgedSrcSize, zc->appliedParams.fParams.contentSizeFlag); |
Yann Collet | cc9f9b7 | 2017-06-15 18:17:10 -0700 | [diff] [blame] | 906 | zc->blockSize = blockSize; |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 907 | |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 908 | XXH64_reset(&zc->xxhState, 0); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 909 | zc->stage = ZSTDcs_init; |
| 910 | zc->dictID = 0; |
| 911 | zc->loadedDictEnd = 0; |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 912 | zc->entropy->hufCTable_repeatMode = HUF_repeat_none; |
Nick Terrell | 830ef41 | 2017-07-13 12:45:39 -0700 | [diff] [blame] | 913 | zc->entropy->offcode_repeatMode = FSE_repeat_none; |
| 914 | zc->entropy->matchlength_repeatMode = FSE_repeat_none; |
| 915 | zc->entropy->litlength_repeatMode = FSE_repeat_none; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 916 | zc->nextToUpdate = 1; |
| 917 | zc->nextSrc = NULL; |
| 918 | zc->base = NULL; |
| 919 | zc->dictBase = NULL; |
| 920 | zc->dictLimit = 0; |
| 921 | zc->lowLimit = 0; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 922 | { int i; for (i=0; i<ZSTD_REP_NUM; i++) zc->seqStore.rep[i] = repStartValue[i]; } |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 923 | zc->hashLog3 = hashLog3; |
Nick Terrell | 7a28b9e | 2017-07-17 15:29:11 -0700 | [diff] [blame] | 924 | zc->optState.litLengthSum = 0; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 925 | |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 926 | ptr = zc->entropy + 1; |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 927 | |
| 928 | /* opt parser space */ |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 929 | if ((params.cParams.strategy == ZSTD_btopt) || (params.cParams.strategy == ZSTD_btultra)) { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 930 | DEBUGLOG(5, "reserving optimal parser space"); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 931 | assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ |
Nick Terrell | 7a28b9e | 2017-07-17 15:29:11 -0700 | [diff] [blame] | 932 | zc->optState.litFreq = (U32*)ptr; |
| 933 | zc->optState.litLengthFreq = zc->optState.litFreq + (1<<Litbits); |
| 934 | zc->optState.matchLengthFreq = zc->optState.litLengthFreq + (MaxLL+1); |
| 935 | zc->optState.offCodeFreq = zc->optState.matchLengthFreq + (MaxML+1); |
| 936 | ptr = zc->optState.offCodeFreq + (MaxOff+1); |
| 937 | zc->optState.matchTable = (ZSTD_match_t*)ptr; |
| 938 | ptr = zc->optState.matchTable + ZSTD_OPT_NUM+1; |
| 939 | zc->optState.priceTable = (ZSTD_optimal_t*)ptr; |
| 940 | ptr = zc->optState.priceTable + ZSTD_OPT_NUM+1; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 941 | } |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 942 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 943 | /* ldm hash table */ |
| 944 | /* initialize bucketOffsets table later for pointer alignment */ |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 945 | if (params.ldmParams.enableLdm) { |
| 946 | size_t const ldmHSize = ((size_t)1) << params.ldmParams.hashLog; |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 947 | memset(ptr, 0, ldmHSize * sizeof(ldmEntry_t)); |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 948 | assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ |
| 949 | zc->ldmState.hashTable = (ldmEntry_t*)ptr; |
| 950 | ptr = zc->ldmState.hashTable + ldmHSize; |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 953 | /* table Space */ |
| 954 | if (crp!=ZSTDcrp_noMemset) memset(ptr, 0, tableSpace); /* reset tables only */ |
| 955 | assert(((size_t)ptr & 3) == 0); /* ensure ptr is properly aligned */ |
| 956 | zc->hashTable = (U32*)(ptr); |
| 957 | zc->chainTable = zc->hashTable + hSize; |
| 958 | zc->hashTable3 = zc->chainTable + chainSize; |
| 959 | ptr = zc->hashTable3 + h3Size; |
| 960 | |
| 961 | /* sequences storage */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 962 | zc->seqStore.sequencesStart = (seqDef*)ptr; |
| 963 | ptr = zc->seqStore.sequencesStart + maxNbSeq; |
| 964 | zc->seqStore.llCode = (BYTE*) ptr; |
| 965 | zc->seqStore.mlCode = zc->seqStore.llCode + maxNbSeq; |
| 966 | zc->seqStore.ofCode = zc->seqStore.mlCode + maxNbSeq; |
| 967 | zc->seqStore.litStart = zc->seqStore.ofCode + maxNbSeq; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 968 | ptr = zc->seqStore.litStart + blockSize; |
| 969 | |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 970 | /* ldm bucketOffsets table */ |
| 971 | if (params.ldmParams.enableLdm) { |
| 972 | size_t const ldmBucketSize = |
| 973 | ((size_t)1) << (params.ldmParams.hashLog - |
| 974 | params.ldmParams.bucketSizeLog); |
Stella Lau | 9e40602 | 2017-09-06 08:39:46 -0700 | [diff] [blame] | 975 | memset(ptr, 0, ldmBucketSize); |
| 976 | zc->ldmState.bucketOffsets = (BYTE*)ptr; |
| 977 | ptr = zc->ldmState.bucketOffsets + ldmBucketSize; |
| 978 | } |
| 979 | |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 980 | /* buffers */ |
| 981 | zc->inBuffSize = buffInSize; |
| 982 | zc->inBuff = (char*)ptr; |
| 983 | zc->outBuffSize = buffOutSize; |
| 984 | zc->outBuff = zc->inBuff + buffInSize; |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 985 | |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 986 | return 0; |
Yann Collet | 72d706a | 2016-03-23 20:44:12 +0100 | [diff] [blame] | 987 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 988 | } |
| 989 | |
Yann Collet | 32dfae6 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 990 | /* ZSTD_invalidateRepCodes() : |
| 991 | * ensures next compression will not use repcodes from previous block. |
| 992 | * Note : only works with regular variant; |
| 993 | * do not use with extDict variant ! */ |
| 994 | void ZSTD_invalidateRepCodes(ZSTD_CCtx* cctx) { |
| 995 | int i; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 996 | for (i=0; i<ZSTD_REP_NUM; i++) cctx->seqStore.rep[i] = 0; |
Yann Collet | 32dfae6 | 2017-01-19 10:32:55 -0800 | [diff] [blame] | 997 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 998 | |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 999 | |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 1000 | /*! ZSTD_copyCCtx_internal() : |
| 1001 | * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. |
Stella Lau | fd0071d | 2017-09-05 15:34:17 -0700 | [diff] [blame] | 1002 | * The "context", in this case, refers to the hash and chain tables, entropy |
| 1003 | * tables, and dictionary offsets. |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 1004 | * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). |
| 1005 | * pledgedSrcSize=0 means "empty" if fParams.contentSizeFlag=1 |
| 1006 | * @return : 0, or an error code */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1007 | static size_t ZSTD_copyCCtx_internal(ZSTD_CCtx* dstCCtx, |
| 1008 | const ZSTD_CCtx* srcCCtx, |
| 1009 | ZSTD_frameParameters fParams, |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 1010 | unsigned long long pledgedSrcSize, |
| 1011 | ZSTD_buffered_policy_e zbuff) |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1012 | { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 1013 | DEBUGLOG(5, "ZSTD_copyCCtx_internal"); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1014 | if (srcCCtx->stage!=ZSTDcs_init) return ERROR(stage_wrong); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 1015 | |
inikep | 2866951 | 2016-06-02 13:04:18 +0200 | [diff] [blame] | 1016 | memcpy(&dstCCtx->customMem, &srcCCtx->customMem, sizeof(ZSTD_customMem)); |
Stella Lau | fd0071d | 2017-09-05 15:34:17 -0700 | [diff] [blame] | 1017 | { ZSTD_CCtx_params params = dstCCtx->requestedParams; |
| 1018 | /* Copy only compression parameters related to tables. */ |
| 1019 | params.cParams = srcCCtx->appliedParams.cParams; |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 1020 | params.fParams = fParams; |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 1021 | ZSTD_resetCCtx_internal(dstCCtx, params, pledgedSrcSize, |
| 1022 | ZSTDcrp_noMemset, zbuff); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 1023 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1024 | |
| 1025 | /* copy tables */ |
Nick Terrell | d6abb28 | 2017-09-21 16:18:34 -0700 | [diff] [blame] | 1026 | { size_t const chainSize = (srcCCtx->appliedParams.cParams.strategy == ZSTD_fast) ? 0 : ((size_t)1 << srcCCtx->appliedParams.cParams.chainLog); |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1027 | size_t const hSize = (size_t)1 << srcCCtx->appliedParams.cParams.hashLog; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1028 | size_t const h3Size = (size_t)1 << srcCCtx->hashLog3; |
| 1029 | size_t const tableSpace = (chainSize + hSize + h3Size) * sizeof(U32); |
Yann Collet | e6fa70a | 2017-04-20 17:28:31 -0700 | [diff] [blame] | 1030 | assert((U32*)dstCCtx->chainTable == (U32*)dstCCtx->hashTable + hSize); /* chainTable must follow hashTable */ |
| 1031 | assert((U32*)dstCCtx->hashTable3 == (U32*)dstCCtx->chainTable + chainSize); |
| 1032 | memcpy(dstCCtx->hashTable, srcCCtx->hashTable, tableSpace); /* presumes all tables follow each other */ |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 1033 | } |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1034 | |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1035 | /* copy dictionary offsets */ |
Yann Collet | c6eea2b | 2016-03-19 17:18:00 +0100 | [diff] [blame] | 1036 | dstCCtx->nextToUpdate = srcCCtx->nextToUpdate; |
| 1037 | dstCCtx->nextToUpdate3= srcCCtx->nextToUpdate3; |
| 1038 | dstCCtx->nextSrc = srcCCtx->nextSrc; |
| 1039 | dstCCtx->base = srcCCtx->base; |
| 1040 | dstCCtx->dictBase = srcCCtx->dictBase; |
| 1041 | dstCCtx->dictLimit = srcCCtx->dictLimit; |
| 1042 | dstCCtx->lowLimit = srcCCtx->lowLimit; |
| 1043 | dstCCtx->loadedDictEnd= srcCCtx->loadedDictEnd; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1044 | dstCCtx->dictID = srcCCtx->dictID; |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1045 | |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1046 | /* copy entropy tables */ |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 1047 | memcpy(dstCCtx->entropy, srcCCtx->entropy, sizeof(ZSTD_entropyCTables_t)); |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1048 | |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 1052 | /*! ZSTD_copyCCtx() : |
| 1053 | * Duplicate an existing context `srcCCtx` into another one `dstCCtx`. |
| 1054 | * Only works during stage ZSTDcs_init (i.e. after creation, but before first call to ZSTD_compressContinue()). |
| 1055 | * pledgedSrcSize==0 means "unknown". |
| 1056 | * @return : 0, or an error code */ |
| 1057 | size_t ZSTD_copyCCtx(ZSTD_CCtx* dstCCtx, const ZSTD_CCtx* srcCCtx, unsigned long long pledgedSrcSize) |
| 1058 | { |
| 1059 | ZSTD_frameParameters fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 1060 | ZSTD_buffered_policy_e const zbuff = (ZSTD_buffered_policy_e)(srcCCtx->inBuffSize>0); |
| 1061 | ZSTD_STATIC_ASSERT((U32)ZSTDb_buffered==1); |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 1062 | fParams.contentSizeFlag = pledgedSrcSize>0; |
| 1063 | |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 1064 | return ZSTD_copyCCtx_internal(dstCCtx, srcCCtx, fParams, pledgedSrcSize, zbuff); |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 1065 | } |
| 1066 | |
Yann Collet | 7b51a29 | 2016-01-26 15:58:49 +0100 | [diff] [blame] | 1067 | |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1068 | /*! ZSTD_reduceTable() : |
Yann Collet | a4cab80 | 2017-04-18 14:54:54 -0700 | [diff] [blame] | 1069 | * reduce table indexes by `reducerValue` */ |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1070 | 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] | 1071 | { |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1072 | U32 u; |
| 1073 | for (u=0 ; u < size ; u++) { |
| 1074 | if (table[u] < reducerValue) table[u] = 0; |
| 1075 | else table[u] -= reducerValue; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1079 | /*! ZSTD_ldm_reduceTable() : |
| 1080 | * reduce table indexes by `reducerValue` */ |
| 1081 | static void ZSTD_ldm_reduceTable(ldmEntry_t* const table, U32 const size, |
Stella Lau | 8081bec | 2017-08-31 15:40:16 -0700 | [diff] [blame] | 1082 | U32 const reducerValue) |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1083 | { |
| 1084 | U32 u; |
| 1085 | for (u = 0; u < size; u++) { |
| 1086 | if (table[u].offset < reducerValue) table[u].offset = 0; |
| 1087 | else table[u].offset -= reducerValue; |
| 1088 | } |
| 1089 | } |
| 1090 | |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1091 | /*! ZSTD_reduceIndex() : |
| 1092 | * rescale all indexes to avoid future overflow (indexes are U32) */ |
| 1093 | static void ZSTD_reduceIndex (ZSTD_CCtx* zc, const U32 reducerValue) |
| 1094 | { |
Stella Lau | 0d1b54d | 2017-09-11 13:02:09 -0700 | [diff] [blame] | 1095 | { U32 const hSize = (U32)1 << zc->appliedParams.cParams.hashLog; |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1096 | ZSTD_reduceTable(zc->hashTable, hSize, reducerValue); } |
| 1097 | |
Stella Lau | 0d1b54d | 2017-09-11 13:02:09 -0700 | [diff] [blame] | 1098 | { U32 const chainSize = (zc->appliedParams.cParams.strategy == ZSTD_fast) ? 0 : ((U32)1 << zc->appliedParams.cParams.chainLog); |
Yann Collet | 8a57b92 | 2016-04-04 13:49:18 +0200 | [diff] [blame] | 1099 | ZSTD_reduceTable(zc->chainTable, chainSize, reducerValue); } |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1100 | |
Stella Lau | 0d1b54d | 2017-09-11 13:02:09 -0700 | [diff] [blame] | 1101 | { U32 const h3Size = (zc->hashLog3) ? (U32)1 << zc->hashLog3 : 0; |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1102 | ZSTD_reduceTable(zc->hashTable3, h3Size, reducerValue); } |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1103 | |
Stella Lau | 767a0b3 | 2017-09-01 12:24:59 -0700 | [diff] [blame] | 1104 | { if (zc->appliedParams.ldmParams.enableLdm) { |
Stella Lau | 0d1b54d | 2017-09-11 13:02:09 -0700 | [diff] [blame] | 1105 | U32 const ldmHSize = (U32)1 << zc->appliedParams.ldmParams.hashLog; |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1106 | ZSTD_ldm_reduceTable(zc->ldmState.hashTable, ldmHSize, reducerValue); |
| 1107 | } |
| 1108 | } |
Yann Collet | ecabfe3 | 2016-03-20 16:20:06 +0100 | [diff] [blame] | 1109 | } |
| 1110 | |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1111 | |
Yann Collet | 863ec40 | 2016-01-28 17:56:33 +0100 | [diff] [blame] | 1112 | /*-******************************************************* |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1113 | * Block entropic compression |
| 1114 | *********************************************************/ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1115 | |
Przemyslaw Skibinski | 3ee94a7 | 2016-10-24 15:58:07 +0200 | [diff] [blame] | 1116 | /* See doc/zstd_compression_format.md for detailed format description */ |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1117 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1118 | 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] | 1119 | { |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1120 | if (srcSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); |
Yann Collet | 6fa05a2 | 2016-07-20 14:58:49 +0200 | [diff] [blame] | 1121 | memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize); |
| 1122 | MEM_writeLE24(dst, (U32)(srcSize << 2) + (U32)bt_raw); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1123 | return ZSTD_blockHeaderSize+srcSize; |
| 1124 | } |
| 1125 | |
| 1126 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1127 | 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] | 1128 | { |
| 1129 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1130 | U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1131 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1132 | if (srcSize + flSize > dstCapacity) return ERROR(dstSize_tooSmall); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1133 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1134 | switch(flSize) |
| 1135 | { |
| 1136 | case 1: /* 2 - 1 - 5 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1137 | ostart[0] = (BYTE)((U32)set_basic + (srcSize<<3)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1138 | break; |
| 1139 | case 2: /* 2 - 2 - 12 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1140 | MEM_writeLE16(ostart, (U16)((U32)set_basic + (1<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1141 | break; |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1142 | case 3: /* 2 - 2 - 20 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1143 | MEM_writeLE32(ostart, (U32)((U32)set_basic + (3<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1144 | break; |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 1145 | default: /* not necessary : flSize is {1,2,3} */ |
| 1146 | assert(0); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1147 | } |
| 1148 | |
| 1149 | memcpy(ostart + flSize, src, srcSize); |
| 1150 | return srcSize + flSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1151 | } |
| 1152 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1153 | 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] | 1154 | { |
| 1155 | BYTE* const ostart = (BYTE* const)dst; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1156 | U32 const flSize = 1 + (srcSize>31) + (srcSize>4095); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1157 | |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 1158 | (void)dstCapacity; /* dstCapacity already guaranteed to be >=4, hence large enough */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1159 | |
| 1160 | switch(flSize) |
| 1161 | { |
| 1162 | case 1: /* 2 - 1 - 5 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1163 | ostart[0] = (BYTE)((U32)set_rle + (srcSize<<3)); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1164 | break; |
| 1165 | case 2: /* 2 - 2 - 12 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1166 | MEM_writeLE16(ostart, (U16)((U32)set_rle + (1<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1167 | break; |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1168 | case 3: /* 2 - 2 - 20 */ |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1169 | MEM_writeLE32(ostart, (U32)((U32)set_rle + (3<<2) + (srcSize<<4))); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1170 | break; |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 1171 | default: /* not necessary : flSize is {1,2,3} */ |
| 1172 | assert(0); |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | ostart[flSize] = *(const BYTE*)src; |
| 1176 | return flSize+1; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1177 | } |
| 1178 | |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1179 | |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 1180 | static size_t ZSTD_minGain(size_t srcSize) { return (srcSize >> 6) + 2; } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1181 | |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1182 | static size_t ZSTD_compressLiterals (ZSTD_entropyCTables_t * entropy, |
| 1183 | ZSTD_strategy strategy, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1184 | void* dst, size_t dstCapacity, |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1185 | const void* src, size_t srcSize) |
| 1186 | { |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1187 | size_t const minGain = ZSTD_minGain(srcSize); |
| 1188 | size_t const lhSize = 3 + (srcSize >= 1 KB) + (srcSize >= 16 KB); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1189 | BYTE* const ostart = (BYTE*)dst; |
Yann Collet | afe0709 | 2016-01-25 04:10:46 +0100 | [diff] [blame] | 1190 | U32 singleStream = srcSize < 256; |
Yann Collet | f8e7b53 | 2016-07-23 16:31:49 +0200 | [diff] [blame] | 1191 | symbolEncodingType_e hType = set_compressed; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1192 | size_t cLitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1193 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1194 | |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 1195 | /* small ? don't even attempt compression (speed opt) */ |
| 1196 | # define LITERAL_NOENTROPY 63 |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1197 | { size_t const minLitSize = entropy->hufCTable_repeatMode == HUF_repeat_valid ? 6 : LITERAL_NOENTROPY; |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 1198 | if (srcSize <= minLitSize) return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); |
| 1199 | } |
| 1200 | |
| 1201 | if (dstCapacity < lhSize+1) return ERROR(dstSize_tooSmall); /* not enough space for compression */ |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1202 | { HUF_repeat repeat = entropy->hufCTable_repeatMode; |
| 1203 | int const preferRepeat = strategy < ZSTD_lazy ? srcSize <= 1024 : 0; |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 1204 | if (repeat == HUF_repeat_valid && lhSize == 3) singleStream = 1; |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 1205 | cLitSize = singleStream ? HUF_compress1X_repeat(ostart+lhSize, dstCapacity-lhSize, src, srcSize, 255, 11, |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1206 | entropy->workspace, sizeof(entropy->workspace), (HUF_CElt*)entropy->hufCTable, &repeat, preferRepeat) |
Yann Collet | e348dad | 2017-04-20 11:14:13 -0700 | [diff] [blame] | 1207 | : HUF_compress4X_repeat(ostart+lhSize, dstCapacity-lhSize, src, srcSize, 255, 11, |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1208 | entropy->workspace, sizeof(entropy->workspace), (HUF_CElt*)entropy->hufCTable, &repeat, preferRepeat); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 1209 | if (repeat != HUF_repeat_none) { hType = set_repeat; } /* reused the existing table */ |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1210 | else { entropy->hufCTable_repeatMode = HUF_repeat_check; } /* now have a table to reuse */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1211 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1212 | |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1213 | if ((cLitSize==0) | (cLitSize >= srcSize - minGain) | ERR_isError(cLitSize)) { |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1214 | entropy->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1215 | return ZSTD_noCompressLiterals(dst, dstCapacity, src, srcSize); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 1216 | } |
| 1217 | if (cLitSize==1) { |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1218 | entropy->hufCTable_repeatMode = HUF_repeat_none; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1219 | return ZSTD_compressRleLiteralsBlock(dst, dstCapacity, src, srcSize); |
Nick Terrell | a419777 | 2017-03-01 17:51:56 -0800 | [diff] [blame] | 1220 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1221 | |
| 1222 | /* Build header */ |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1223 | switch(lhSize) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1224 | { |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1225 | case 3: /* 2 - 2 - 10 - 10 */ |
Yann Collet | c2e1a68 | 2016-07-22 17:30:52 +0200 | [diff] [blame] | 1226 | { U32 const lhc = hType + ((!singleStream) << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<14); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 1227 | MEM_writeLE24(ostart, lhc); |
| 1228 | break; |
| 1229 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1230 | case 4: /* 2 - 2 - 14 - 14 */ |
Yann Collet | 32faf6c | 2016-07-22 04:45:06 +0200 | [diff] [blame] | 1231 | { U32 const lhc = hType + (2 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<18); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 1232 | MEM_writeLE32(ostart, lhc); |
| 1233 | break; |
| 1234 | } |
Yann Collet | 59d1f79 | 2016-01-23 19:28:41 +0100 | [diff] [blame] | 1235 | case 5: /* 2 - 2 - 18 - 18 */ |
Yann Collet | 32faf6c | 2016-07-22 04:45:06 +0200 | [diff] [blame] | 1236 | { U32 const lhc = hType + (3 << 2) + ((U32)srcSize<<4) + ((U32)cLitSize<<22); |
Yann Collet | 198e6aa | 2016-07-20 20:12:24 +0200 | [diff] [blame] | 1237 | MEM_writeLE32(ostart, lhc); |
| 1238 | ostart[4] = (BYTE)(cLitSize >> 10); |
| 1239 | break; |
| 1240 | } |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1241 | default: /* not possible : lhSize is {3,4,5} */ |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 1242 | assert(0); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1243 | } |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1244 | return lhSize+cLitSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1245 | } |
| 1246 | |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1247 | |
| 1248 | void ZSTD_seqToCodes(const seqStore_t* seqStorePtr) |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 1249 | { |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1250 | BYTE const LL_deltaCode = 19; |
| 1251 | BYTE const ML_deltaCode = 36; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1252 | const seqDef* const sequences = seqStorePtr->sequencesStart; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1253 | BYTE* const llCodeTable = seqStorePtr->llCode; |
| 1254 | BYTE* const ofCodeTable = seqStorePtr->ofCode; |
| 1255 | BYTE* const mlCodeTable = seqStorePtr->mlCode; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1256 | U32 const nbSeq = (U32)(seqStorePtr->sequences - seqStorePtr->sequencesStart); |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1257 | U32 u; |
| 1258 | for (u=0; u<nbSeq; u++) { |
| 1259 | U32 const llv = sequences[u].litLength; |
| 1260 | U32 const mlv = sequences[u].matchLength; |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 1261 | 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] | 1262 | ofCodeTable[u] = (BYTE)ZSTD_highbit32(sequences[u].offset); |
Yann Collet | 3b2bd1d | 2016-07-30 13:21:41 +0200 | [diff] [blame] | 1263 | 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] | 1264 | } |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1265 | if (seqStorePtr->longLengthID==1) |
| 1266 | llCodeTable[seqStorePtr->longLengthPos] = MaxLL; |
| 1267 | if (seqStorePtr->longLengthID==2) |
| 1268 | mlCodeTable[seqStorePtr->longLengthPos] = MaxML; |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 1269 | } |
| 1270 | |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1271 | typedef enum { |
| 1272 | ZSTD_defaultDisallowed = 0, |
| 1273 | ZSTD_defaultAllowed = 1 |
| 1274 | } ZSTD_defaultPolicy_e; |
| 1275 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1276 | MEM_STATIC |
| 1277 | symbolEncodingType_e ZSTD_selectEncodingType( |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1278 | FSE_repeat* repeatMode, size_t const mostFrequent, size_t nbSeq, |
| 1279 | U32 defaultNormLog, ZSTD_defaultPolicy_e const isDefaultAllowed) |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1280 | { |
| 1281 | #define MIN_SEQ_FOR_DYNAMIC_FSE 64 |
| 1282 | #define MAX_SEQ_FOR_STATIC_FSE 1000 |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1283 | ZSTD_STATIC_ASSERT(ZSTD_defaultDisallowed == 0 && ZSTD_defaultAllowed != 0); |
| 1284 | if ((mostFrequent == nbSeq) && (!isDefaultAllowed || nbSeq > 2)) { |
Nick Terrell | ced6e61 | 2017-10-13 14:09:17 -0700 | [diff] [blame^] | 1285 | DEBUGLOG(5, "Selected set_rle"); |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1286 | /* Prefer set_basic over set_rle when there are 2 or less symbols, |
| 1287 | * since RLE uses 1 byte, but set_basic uses 5-6 bits per symbol. |
| 1288 | * If basic encoding isn't possible, always choose RLE. |
| 1289 | */ |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1290 | *repeatMode = FSE_repeat_check; |
| 1291 | return set_rle; |
| 1292 | } |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1293 | if ( isDefaultAllowed |
| 1294 | && (*repeatMode == FSE_repeat_valid) && (nbSeq < MAX_SEQ_FOR_STATIC_FSE)) { |
Nick Terrell | ced6e61 | 2017-10-13 14:09:17 -0700 | [diff] [blame^] | 1295 | DEBUGLOG(5, "Selected set_repeat"); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1296 | return set_repeat; |
| 1297 | } |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1298 | if ( isDefaultAllowed |
| 1299 | && ((nbSeq < MIN_SEQ_FOR_DYNAMIC_FSE) || (mostFrequent < (nbSeq >> (defaultNormLog-1)))) ) { |
Nick Terrell | ced6e61 | 2017-10-13 14:09:17 -0700 | [diff] [blame^] | 1300 | DEBUGLOG(5, "Selected set_basic"); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1301 | *repeatMode = FSE_repeat_valid; |
| 1302 | return set_basic; |
| 1303 | } |
Nick Terrell | ced6e61 | 2017-10-13 14:09:17 -0700 | [diff] [blame^] | 1304 | DEBUGLOG(5, "Selected set_compressed"); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1305 | *repeatMode = FSE_repeat_check; |
| 1306 | return set_compressed; |
| 1307 | } |
| 1308 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1309 | MEM_STATIC |
| 1310 | size_t ZSTD_buildCTable(void* dst, size_t dstCapacity, |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1311 | FSE_CTable* CTable, U32 FSELog, symbolEncodingType_e type, |
| 1312 | U32* count, U32 max, |
| 1313 | BYTE const* codeTable, size_t nbSeq, |
| 1314 | S16 const* defaultNorm, U32 defaultNormLog, U32 defaultMax, |
| 1315 | void* workspace, size_t workspaceSize) |
| 1316 | { |
| 1317 | BYTE* op = (BYTE*)dst; |
| 1318 | BYTE const* const oend = op + dstCapacity; |
| 1319 | |
| 1320 | switch (type) { |
| 1321 | case set_rle: |
| 1322 | *op = codeTable[0]; |
| 1323 | CHECK_F(FSE_buildCTable_rle(CTable, (BYTE)max)); |
| 1324 | return 1; |
| 1325 | case set_repeat: |
| 1326 | return 0; |
| 1327 | case set_basic: |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1328 | CHECK_F(FSE_buildCTable_wksp(CTable, defaultNorm, defaultMax, defaultNormLog, workspace, workspaceSize)); /* note : could be pre-calculated */ |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1329 | return 0; |
| 1330 | case set_compressed: { |
| 1331 | S16 norm[MaxSeq + 1]; |
| 1332 | size_t nbSeq_1 = nbSeq; |
| 1333 | const U32 tableLog = FSE_optimalTableLog(FSELog, nbSeq, max); |
| 1334 | if (count[codeTable[nbSeq-1]] > 1) { |
| 1335 | count[codeTable[nbSeq-1]]--; |
| 1336 | nbSeq_1--; |
| 1337 | } |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1338 | assert(nbSeq_1 > 1); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1339 | CHECK_F(FSE_normalizeCount(norm, tableLog, count, nbSeq_1, max)); |
| 1340 | { size_t const NCountSize = FSE_writeNCount(op, oend - op, norm, max, tableLog); /* overflow protected */ |
| 1341 | if (FSE_isError(NCountSize)) return NCountSize; |
| 1342 | CHECK_F(FSE_buildCTable_wksp(CTable, norm, max, tableLog, workspace, workspaceSize)); |
| 1343 | return NCountSize; |
| 1344 | } |
| 1345 | } |
| 1346 | default: return assert(0), ERROR(GENERIC); |
| 1347 | } |
| 1348 | } |
| 1349 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1350 | MEM_STATIC |
| 1351 | size_t ZSTD_encodeSequences( |
| 1352 | void* dst, size_t dstCapacity, |
| 1353 | FSE_CTable const* CTable_MatchLength, BYTE const* mlCodeTable, |
| 1354 | FSE_CTable const* CTable_OffsetBits, BYTE const* ofCodeTable, |
| 1355 | FSE_CTable const* CTable_LitLength, BYTE const* llCodeTable, |
| 1356 | seqDef const* sequences, size_t nbSeq, int longOffsets) |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1357 | { |
| 1358 | BIT_CStream_t blockStream; |
| 1359 | FSE_CState_t stateMatchLength; |
| 1360 | FSE_CState_t stateOffsetBits; |
| 1361 | FSE_CState_t stateLitLength; |
| 1362 | |
| 1363 | CHECK_E(BIT_initCStream(&blockStream, dst, dstCapacity), dstSize_tooSmall); /* not enough space remaining */ |
| 1364 | |
| 1365 | /* first symbols */ |
| 1366 | FSE_initCState2(&stateMatchLength, CTable_MatchLength, mlCodeTable[nbSeq-1]); |
| 1367 | FSE_initCState2(&stateOffsetBits, CTable_OffsetBits, ofCodeTable[nbSeq-1]); |
| 1368 | FSE_initCState2(&stateLitLength, CTable_LitLength, llCodeTable[nbSeq-1]); |
| 1369 | BIT_addBits(&blockStream, sequences[nbSeq-1].litLength, LL_bits[llCodeTable[nbSeq-1]]); |
| 1370 | if (MEM_32bits()) BIT_flushBits(&blockStream); |
| 1371 | BIT_addBits(&blockStream, sequences[nbSeq-1].matchLength, ML_bits[mlCodeTable[nbSeq-1]]); |
| 1372 | if (MEM_32bits()) BIT_flushBits(&blockStream); |
| 1373 | if (longOffsets) { |
| 1374 | U32 const ofBits = ofCodeTable[nbSeq-1]; |
| 1375 | int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); |
| 1376 | if (extraBits) { |
| 1377 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset, extraBits); |
| 1378 | BIT_flushBits(&blockStream); |
| 1379 | } |
| 1380 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset >> extraBits, |
| 1381 | ofBits - extraBits); |
| 1382 | } else { |
| 1383 | BIT_addBits(&blockStream, sequences[nbSeq-1].offset, ofCodeTable[nbSeq-1]); |
| 1384 | } |
| 1385 | BIT_flushBits(&blockStream); |
| 1386 | |
| 1387 | { size_t n; |
| 1388 | for (n=nbSeq-2 ; n<nbSeq ; n--) { /* intentional underflow */ |
| 1389 | BYTE const llCode = llCodeTable[n]; |
| 1390 | BYTE const ofCode = ofCodeTable[n]; |
| 1391 | BYTE const mlCode = mlCodeTable[n]; |
| 1392 | U32 const llBits = LL_bits[llCode]; |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1393 | U32 const ofBits = ofCode; |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1394 | U32 const mlBits = ML_bits[mlCode]; |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1395 | DEBUGLOG(6, "encoding: litlen:%u - matchlen:%u - offCode:%u", |
| 1396 | sequences[n].litLength, |
| 1397 | sequences[n].matchLength + MINMATCH, |
| 1398 | sequences[n].offset); /* 32b*/ /* 64b*/ |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1399 | /* (7)*/ /* (7)*/ |
| 1400 | FSE_encodeSymbol(&blockStream, &stateOffsetBits, ofCode); /* 15 */ /* 15 */ |
| 1401 | FSE_encodeSymbol(&blockStream, &stateMatchLength, mlCode); /* 24 */ /* 24 */ |
| 1402 | if (MEM_32bits()) BIT_flushBits(&blockStream); /* (7)*/ |
| 1403 | FSE_encodeSymbol(&blockStream, &stateLitLength, llCode); /* 16 */ /* 33 */ |
| 1404 | if (MEM_32bits() || (ofBits+mlBits+llBits >= 64-7-(LLFSELog+MLFSELog+OffFSELog))) |
| 1405 | BIT_flushBits(&blockStream); /* (7)*/ |
| 1406 | BIT_addBits(&blockStream, sequences[n].litLength, llBits); |
| 1407 | if (MEM_32bits() && ((llBits+mlBits)>24)) BIT_flushBits(&blockStream); |
| 1408 | BIT_addBits(&blockStream, sequences[n].matchLength, mlBits); |
Yann Collet | 6b2b6a9 | 2017-08-22 12:08:39 -0700 | [diff] [blame] | 1409 | if (MEM_32bits() || (ofBits+mlBits+llBits > 56)) BIT_flushBits(&blockStream); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1410 | if (longOffsets) { |
| 1411 | int const extraBits = ofBits - MIN(ofBits, STREAM_ACCUMULATOR_MIN-1); |
| 1412 | if (extraBits) { |
| 1413 | BIT_addBits(&blockStream, sequences[n].offset, extraBits); |
| 1414 | BIT_flushBits(&blockStream); /* (7)*/ |
| 1415 | } |
| 1416 | BIT_addBits(&blockStream, sequences[n].offset >> extraBits, |
| 1417 | ofBits - extraBits); /* 31 */ |
| 1418 | } else { |
| 1419 | BIT_addBits(&blockStream, sequences[n].offset, ofBits); /* 31 */ |
| 1420 | } |
| 1421 | BIT_flushBits(&blockStream); /* (7)*/ |
| 1422 | } } |
| 1423 | |
| 1424 | FSE_flushCState(&blockStream, &stateMatchLength); |
| 1425 | FSE_flushCState(&blockStream, &stateOffsetBits); |
| 1426 | FSE_flushCState(&blockStream, &stateLitLength); |
| 1427 | |
| 1428 | { size_t const streamSize = BIT_closeCStream(&blockStream); |
| 1429 | if (streamSize==0) return ERROR(dstSize_tooSmall); /* not enough space */ |
| 1430 | return streamSize; |
| 1431 | } |
| 1432 | } |
| 1433 | |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1434 | MEM_STATIC size_t ZSTD_compressSequences_internal(seqStore_t* seqStorePtr, |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1435 | ZSTD_entropyCTables_t* entropy, |
| 1436 | ZSTD_compressionParameters const* cParams, |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1437 | void* dst, size_t dstCapacity) |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1438 | { |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1439 | const int longOffsets = cParams->windowLog > STREAM_ACCUMULATOR_MIN; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1440 | U32 count[MaxSeq+1]; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1441 | FSE_CTable* CTable_LitLength = entropy->litlengthCTable; |
| 1442 | FSE_CTable* CTable_OffsetBits = entropy->offcodeCTable; |
| 1443 | FSE_CTable* CTable_MatchLength = entropy->matchlengthCTable; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1444 | U32 LLtype, Offtype, MLtype; /* compressed, raw or rle */ |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1445 | const seqDef* const sequences = seqStorePtr->sequencesStart; |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1446 | const BYTE* const ofCodeTable = seqStorePtr->ofCode; |
| 1447 | const BYTE* const llCodeTable = seqStorePtr->llCode; |
| 1448 | const BYTE* const mlCodeTable = seqStorePtr->mlCode; |
Yann Collet | 5054ee0 | 2015-11-23 13:34:21 +0100 | [diff] [blame] | 1449 | BYTE* const ostart = (BYTE*)dst; |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1450 | BYTE* const oend = ostart + dstCapacity; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1451 | BYTE* op = ostart; |
Yann Collet | c0ce4f1 | 2016-07-30 00:55:13 +0200 | [diff] [blame] | 1452 | size_t const nbSeq = seqStorePtr->sequences - seqStorePtr->sequencesStart; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1453 | BYTE* seqHead; |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1454 | |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1455 | ZSTD_STATIC_ASSERT(sizeof(entropy->workspace) >= (1<<MAX(MLFSELog,LLFSELog))); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1456 | |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1457 | /* Compress literals */ |
Yann Collet | a5c2c08 | 2016-03-20 01:09:18 +0100 | [diff] [blame] | 1458 | { const BYTE* const literals = seqStorePtr->litStart; |
Yann Collet | a910dc8 | 2016-03-18 12:37:45 +0100 | [diff] [blame] | 1459 | size_t const litSize = seqStorePtr->lit - literals; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1460 | size_t const cSize = ZSTD_compressLiterals( |
| 1461 | entropy, cParams->strategy, op, dstCapacity, literals, litSize); |
| 1462 | if (ZSTD_isError(cSize)) |
| 1463 | return cSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1464 | op += cSize; |
| 1465 | } |
| 1466 | |
| 1467 | /* Sequences Header */ |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 1468 | 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] | 1469 | if (nbSeq < 0x7F) *op++ = (BYTE)nbSeq; |
| 1470 | else if (nbSeq < LONGNBSEQ) op[0] = (BYTE)((nbSeq>>8) + 0x80), op[1] = (BYTE)nbSeq, op+=2; |
| 1471 | else op[0]=0xFF, MEM_writeLE16(op+1, (U16)(nbSeq - LONGNBSEQ)), op+=3; |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1472 | if (nbSeq==0) return op - ostart; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1473 | |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 1474 | /* seqHead : flags for FSE encoding type */ |
| 1475 | seqHead = op++; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1476 | |
Yann Collet | b44be74 | 2016-03-26 20:52:14 +0100 | [diff] [blame] | 1477 | /* convert length/distances into codes */ |
Yann Collet | ed57d85 | 2016-07-29 21:22:17 +0200 | [diff] [blame] | 1478 | ZSTD_seqToCodes(seqStorePtr); |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1479 | /* build CTable for Literal Lengths */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1480 | { U32 max = MaxLL; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1481 | size_t const mostFrequent = FSE_countFast_wksp(count, &max, llCodeTable, nbSeq, entropy->workspace); |
Nick Terrell | ced6e61 | 2017-10-13 14:09:17 -0700 | [diff] [blame^] | 1482 | DEBUGLOG(5, "Building LL table"); |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1483 | LLtype = ZSTD_selectEncodingType(&entropy->litlength_repeatMode, mostFrequent, nbSeq, LL_defaultNormLog, ZSTD_defaultAllowed); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1484 | { size_t const countSize = ZSTD_buildCTable(op, oend - op, CTable_LitLength, LLFSELog, (symbolEncodingType_e)LLtype, |
| 1485 | count, max, llCodeTable, nbSeq, LL_defaultNorm, LL_defaultNormLog, MaxLL, |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1486 | entropy->workspace, sizeof(entropy->workspace)); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1487 | if (ZSTD_isError(countSize)) return countSize; |
| 1488 | op += countSize; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1489 | } } |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1490 | /* build CTable for Offsets */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1491 | { U32 max = MaxOff; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1492 | size_t const mostFrequent = FSE_countFast_wksp(count, &max, ofCodeTable, nbSeq, entropy->workspace); |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1493 | /* We can only use the basic table if max <= DefaultMaxOff, otherwise the offsets are too large */ |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1494 | ZSTD_defaultPolicy_e const defaultPolicy = (max <= DefaultMaxOff) ? ZSTD_defaultAllowed : ZSTD_defaultDisallowed; |
Nick Terrell | ced6e61 | 2017-10-13 14:09:17 -0700 | [diff] [blame^] | 1495 | DEBUGLOG(5, "Building OF table"); |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1496 | Offtype = ZSTD_selectEncodingType(&entropy->offcode_repeatMode, mostFrequent, nbSeq, OF_defaultNormLog, defaultPolicy); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1497 | { size_t const countSize = ZSTD_buildCTable(op, oend - op, CTable_OffsetBits, OffFSELog, (symbolEncodingType_e)Offtype, |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1498 | count, max, ofCodeTable, nbSeq, OF_defaultNorm, OF_defaultNormLog, DefaultMaxOff, |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1499 | entropy->workspace, sizeof(entropy->workspace)); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1500 | if (ZSTD_isError(countSize)) return countSize; |
| 1501 | op += countSize; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1502 | } } |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1503 | /* build CTable for MatchLengths */ |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1504 | { U32 max = MaxML; |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1505 | size_t const mostFrequent = FSE_countFast_wksp(count, &max, mlCodeTable, nbSeq, entropy->workspace); |
Nick Terrell | ced6e61 | 2017-10-13 14:09:17 -0700 | [diff] [blame^] | 1506 | DEBUGLOG(5, "Building ML table"); |
Nick Terrell | bbe7721 | 2017-09-18 16:54:53 -0700 | [diff] [blame] | 1507 | MLtype = ZSTD_selectEncodingType(&entropy->matchlength_repeatMode, mostFrequent, nbSeq, ML_defaultNormLog, ZSTD_defaultAllowed); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1508 | { size_t const countSize = ZSTD_buildCTable(op, oend - op, CTable_MatchLength, MLFSELog, (symbolEncodingType_e)MLtype, |
| 1509 | count, max, mlCodeTable, nbSeq, ML_defaultNorm, ML_defaultNormLog, MaxML, |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1510 | entropy->workspace, sizeof(entropy->workspace)); |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1511 | if (ZSTD_isError(countSize)) return countSize; |
| 1512 | op += countSize; |
Yann Collet | fadda6c | 2016-03-22 12:14:26 +0100 | [diff] [blame] | 1513 | } } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1514 | |
Yann Collet | be39143 | 2016-03-22 23:19:28 +0100 | [diff] [blame] | 1515 | *seqHead = (BYTE)((LLtype<<6) + (Offtype<<4) + (MLtype<<2)); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1516 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1517 | { size_t const bitstreamSize = ZSTD_encodeSequences( |
| 1518 | op, oend - op, |
| 1519 | CTable_MatchLength, mlCodeTable, |
| 1520 | CTable_OffsetBits, ofCodeTable, |
| 1521 | CTable_LitLength, llCodeTable, |
| 1522 | sequences, nbSeq, |
| 1523 | longOffsets); |
| 1524 | if (ZSTD_isError(bitstreamSize)) return bitstreamSize; |
| 1525 | op += bitstreamSize; |
Nick Terrell | 634f012 | 2017-07-14 19:11:58 -0700 | [diff] [blame] | 1526 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1527 | |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1528 | return op - ostart; |
| 1529 | } |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1530 | |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1531 | MEM_STATIC size_t ZSTD_compressSequences(seqStore_t* seqStorePtr, |
| 1532 | ZSTD_entropyCTables_t* entropy, |
| 1533 | ZSTD_compressionParameters const* cParams, |
| 1534 | void* dst, size_t dstCapacity, |
| 1535 | size_t srcSize) |
| 1536 | { |
| 1537 | size_t const cSize = ZSTD_compressSequences_internal(seqStorePtr, entropy, cParams, |
| 1538 | dst, dstCapacity); |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1539 | /* If the srcSize <= dstCapacity, then there is enough space to write a |
| 1540 | * raw uncompressed block. Since we ran out of space, the block must not |
| 1541 | * be compressible, so fall back to a raw uncompressed block. |
| 1542 | */ |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1543 | int const uncompressibleError = (cSize == ERROR(dstSize_tooSmall)) && (srcSize <= dstCapacity); |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1544 | if (ZSTD_isError(cSize) && !uncompressibleError) |
| 1545 | return cSize; |
Nick Terrell | 24ac2db | 2017-10-13 12:47:00 -0700 | [diff] [blame] | 1546 | /* We check that dictionaries have offset codes available for the first |
| 1547 | * block. After the first block, the offcode table might not have large |
| 1548 | * enough codes to represent the offsets in the data. |
| 1549 | */ |
| 1550 | if (entropy->offcode_repeatMode == FSE_repeat_valid) |
| 1551 | entropy->offcode_repeatMode = FSE_repeat_check; |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1552 | |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1553 | /* Check compressibility */ |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1554 | { size_t const minGain = ZSTD_minGain(srcSize); /* note : fixed formula, maybe should depend on compression level, or strategy */ |
| 1555 | size_t const maxCSize = srcSize - minGain; |
| 1556 | if (cSize >= maxCSize || uncompressibleError) { |
| 1557 | entropy->hufCTable_repeatMode = HUF_repeat_none; |
| 1558 | entropy->offcode_repeatMode = FSE_repeat_none; |
| 1559 | entropy->matchlength_repeatMode = FSE_repeat_none; |
| 1560 | entropy->litlength_repeatMode = FSE_repeat_none; |
| 1561 | return 0; /* block not compressed */ |
| 1562 | } } |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1563 | assert(!ZSTD_isError(cSize)); |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1564 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1565 | /* block is compressed => confirm repcodes in history */ |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1566 | { int i; for (i=0; i<ZSTD_REP_NUM; i++) seqStorePtr->rep[i] = seqStorePtr->repToConfirm[i]; } |
Nick Terrell | 308047e | 2017-08-03 14:05:01 -0700 | [diff] [blame] | 1567 | return cSize; |
Yann Collet | 14983e7 | 2015-11-11 21:38:21 +0100 | [diff] [blame] | 1568 | } |
| 1569 | |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 1570 | /* ZSTD_selectBlockCompressor() : |
Stella Lau | 360428c | 2017-09-06 17:56:01 -0700 | [diff] [blame] | 1571 | * Not static, but internal use only (used by long distance matcher) |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 1572 | * assumption : strat is a valid strategy */ |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1573 | typedef size_t (*ZSTD_blockCompressor) (ZSTD_CCtx* ctx, const void* src, size_t srcSize); |
Stella Lau | 360428c | 2017-09-06 17:56:01 -0700 | [diff] [blame] | 1574 | ZSTD_blockCompressor ZSTD_selectBlockCompressor(ZSTD_strategy strat, int extDict) |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1575 | { |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 1576 | static const ZSTD_blockCompressor blockCompressor[2][(unsigned)ZSTD_btultra+1] = { |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 1577 | { ZSTD_compressBlock_fast /* default for 0 */, |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 1578 | ZSTD_compressBlock_fast, ZSTD_compressBlock_doubleFast, ZSTD_compressBlock_greedy, |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1579 | ZSTD_compressBlock_lazy, ZSTD_compressBlock_lazy2, ZSTD_compressBlock_btlazy2, |
Nick Terrell | 5f2c721 | 2017-05-10 16:49:58 -0700 | [diff] [blame] | 1580 | ZSTD_compressBlock_btopt, ZSTD_compressBlock_btultra }, |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 1581 | { ZSTD_compressBlock_fast_extDict /* default for 0 */, |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 1582 | ZSTD_compressBlock_fast_extDict, ZSTD_compressBlock_doubleFast_extDict, ZSTD_compressBlock_greedy_extDict, |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1583 | ZSTD_compressBlock_lazy_extDict,ZSTD_compressBlock_lazy2_extDict, ZSTD_compressBlock_btlazy2_extDict, |
Nick Terrell | 5f2c721 | 2017-05-10 16:49:58 -0700 | [diff] [blame] | 1584 | ZSTD_compressBlock_btopt_extDict, ZSTD_compressBlock_btultra_extDict } |
Yann Collet | 7fe531e | 2015-11-29 02:38:09 +0100 | [diff] [blame] | 1585 | }; |
Yann Collet | a5ffe3d | 2017-05-12 16:29:19 -0700 | [diff] [blame] | 1586 | ZSTD_STATIC_ASSERT((unsigned)ZSTD_fast == 1); |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1587 | |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 1588 | assert((U32)strat >= (U32)ZSTD_fast); |
| 1589 | assert((U32)strat <= (U32)ZSTD_btultra); |
Yann Collet | b44ab82 | 2017-06-20 14:11:49 -0700 | [diff] [blame] | 1590 | return blockCompressor[extDict!=0][(U32)strat]; |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1591 | } |
| 1592 | |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1593 | static void ZSTD_storeLastLiterals(seqStore_t* seqStorePtr, |
| 1594 | const BYTE* anchor, size_t lastLLSize) |
| 1595 | { |
| 1596 | memcpy(seqStorePtr->lit, anchor, lastLLSize); |
| 1597 | seqStorePtr->lit += lastLLSize; |
| 1598 | } |
Yann Collet | 59d7063 | 2015-11-04 12:05:27 +0100 | [diff] [blame] | 1599 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 1600 | 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] | 1601 | { |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1602 | if (srcSize < MIN_CBLOCK_SIZE+ZSTD_blockHeaderSize+1) |
| 1603 | return 0; /* don't even attempt compression below a certain srcSize */ |
Yann Collet | 19cab46 | 2016-06-17 12:54:52 +0200 | [diff] [blame] | 1604 | ZSTD_resetSeqStore(&(zc->seqStore)); |
Stella Lau | 6a546ef | 2017-07-28 15:51:33 -0700 | [diff] [blame] | 1605 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1606 | /* limited update after a very long match */ |
| 1607 | { const BYTE* const base = zc->base; |
| 1608 | const BYTE* const istart = (const BYTE*)src; |
| 1609 | const U32 current = (U32)(istart-base); |
| 1610 | if (current > zc->nextToUpdate + 384) |
| 1611 | zc->nextToUpdate = current - MIN(192, (U32)(current - zc->nextToUpdate - 384)); |
| 1612 | } |
| 1613 | /* find and store sequences */ |
| 1614 | { U32 const extDict = zc->lowLimit < zc->dictLimit; |
| 1615 | const ZSTD_blockCompressor blockCompressor = |
| 1616 | zc->appliedParams.ldmParams.enableLdm |
| 1617 | ? (extDict ? ZSTD_compressBlock_ldm_extDict : ZSTD_compressBlock_ldm) |
| 1618 | : ZSTD_selectBlockCompressor(zc->appliedParams.cParams.strategy, extDict); |
| 1619 | size_t const lastLLSize = blockCompressor(zc, src, srcSize); |
| 1620 | const BYTE* const anchor = (const BYTE*)src + srcSize - lastLLSize; |
| 1621 | ZSTD_storeLastLiterals(&zc->seqStore, anchor, lastLLSize); |
| 1622 | } |
| 1623 | /* encode */ |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1624 | return ZSTD_compressSequences(&zc->seqStore, zc->entropy, &zc->appliedParams.cParams, dst, dstCapacity, srcSize); |
Yann Collet | be2010e | 2015-10-31 12:57:14 +0100 | [diff] [blame] | 1625 | } |
| 1626 | |
| 1627 | |
Yann Collet | db8e21d | 2017-05-12 13:46:49 -0700 | [diff] [blame] | 1628 | /*! ZSTD_compress_frameChunk() : |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 1629 | * Compress a chunk of data into one or multiple blocks. |
| 1630 | * All blocks will be terminated, all input will be consumed. |
| 1631 | * Function will issue an error if there is not enough `dstCapacity` to hold the compressed content. |
| 1632 | * Frame is supposed already started (header already produced) |
| 1633 | * @return : compressed size, or an error code |
| 1634 | */ |
Yann Collet | db8e21d | 2017-05-12 13:46:49 -0700 | [diff] [blame] | 1635 | static size_t ZSTD_compress_frameChunk (ZSTD_CCtx* cctx, |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1636 | void* dst, size_t dstCapacity, |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 1637 | const void* src, size_t srcSize, |
| 1638 | U32 lastFrameChunk) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1639 | { |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1640 | size_t blockSize = cctx->blockSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1641 | size_t remaining = srcSize; |
| 1642 | const BYTE* ip = (const BYTE*)src; |
| 1643 | BYTE* const ostart = (BYTE*)dst; |
| 1644 | BYTE* op = ostart; |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1645 | U32 const maxDist = (U32)1 << cctx->appliedParams.cParams.windowLog; |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1646 | assert(cctx->appliedParams.cParams.windowLog <= 31); |
Yann Collet | 9b11b46 | 2015-11-01 12:40:22 +0100 | [diff] [blame] | 1647 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1648 | if (cctx->appliedParams.fParams.checksumFlag && srcSize) |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1649 | XXH64_update(&cctx->xxhState, src, srcSize); |
| 1650 | |
Yann Collet | 2ce4923 | 2016-02-02 14:36:49 +0100 | [diff] [blame] | 1651 | while (remaining) { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 1652 | U32 const lastBlock = lastFrameChunk & (blockSize >= remaining); |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 1653 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1654 | if (dstCapacity < ZSTD_blockHeaderSize + MIN_CBLOCK_SIZE) |
| 1655 | return ERROR(dstSize_tooSmall); /* not enough space to store compressed block */ |
Yann Collet | 3e35827 | 2015-11-04 18:19:39 +0100 | [diff] [blame] | 1656 | if (remaining < blockSize) blockSize = remaining; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1657 | |
Nick Terrell | d6abb28 | 2017-09-21 16:18:34 -0700 | [diff] [blame] | 1658 | /* preemptive overflow correction: |
| 1659 | * 1. correction is large enough: |
| 1660 | * lowLimit > (3<<29) ==> current > 3<<29 + 1<<windowLog - blockSize |
| 1661 | * 1<<windowLog <= newCurrent < 1<<chainLog + 1<<windowLog |
| 1662 | * |
| 1663 | * current - newCurrent |
| 1664 | * > (3<<29 + 1<<windowLog - blockSize) - (1<<windowLog + 1<<chainLog) |
| 1665 | * > (3<<29 - blockSize) - (1<<chainLog) |
| 1666 | * > (3<<29 - blockSize) - (1<<30) (NOTE: chainLog <= 30) |
| 1667 | * > 1<<29 - 1<<17 |
| 1668 | * |
| 1669 | * 2. (ip+blockSize - cctx->base) doesn't overflow: |
| 1670 | * In 32 bit mode we limit windowLog to 30 so we don't get |
| 1671 | * differences larger than 1<<31-1. |
| 1672 | * 3. cctx->lowLimit < 1<<32: |
| 1673 | * windowLog <= 31 ==> 3<<29 + 1<<windowLog < 7<<29 < 1<<32. |
| 1674 | */ |
Sean Purcell | 881abe4 | 2017-03-07 16:52:23 -0800 | [diff] [blame] | 1675 | if (cctx->lowLimit > (3U<<29)) { |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1676 | U32 const cycleMask = ((U32)1 << ZSTD_cycleLog(cctx->appliedParams.cParams.chainLog, cctx->appliedParams.cParams.strategy)) - 1; |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 1677 | U32 const current = (U32)(ip - cctx->base); |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1678 | U32 const newCurrent = (current & cycleMask) + ((U32)1 << cctx->appliedParams.cParams.windowLog); |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 1679 | U32 const correction = current - newCurrent; |
Nick Terrell | d6abb28 | 2017-09-21 16:18:34 -0700 | [diff] [blame] | 1680 | ZSTD_STATIC_ASSERT(ZSTD_CHAINLOG_MAX <= 30); |
| 1681 | ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX_32 <= 30); |
| 1682 | ZSTD_STATIC_ASSERT(ZSTD_WINDOWLOG_MAX <= 31); |
| 1683 | assert(current > newCurrent); |
| 1684 | assert(correction > 1<<28); /* Loose bound, should be about 1<<29 */ |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1685 | ZSTD_reduceIndex(cctx, correction); |
| 1686 | cctx->base += correction; |
| 1687 | cctx->dictBase += correction; |
Yann Collet | c261f71 | 2016-12-12 00:25:07 +0100 | [diff] [blame] | 1688 | cctx->lowLimit -= correction; |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1689 | cctx->dictLimit -= correction; |
| 1690 | if (cctx->nextToUpdate < correction) cctx->nextToUpdate = 0; |
| 1691 | else cctx->nextToUpdate -= correction; |
Nick Terrell | d6abb28 | 2017-09-21 16:18:34 -0700 | [diff] [blame] | 1692 | DEBUGLOG(4, "Correction of 0x%x bytes to lowLimit=0x%x\n", correction, cctx->lowLimit); |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1693 | } |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1694 | /* enforce maxDist */ |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 1695 | if ((U32)(ip+blockSize - cctx->base) > cctx->loadedDictEnd + maxDist) { |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1696 | U32 const newLowLimit = (U32)(ip+blockSize - cctx->base) - maxDist; |
| 1697 | if (cctx->lowLimit < newLowLimit) cctx->lowLimit = newLowLimit; |
| 1698 | if (cctx->dictLimit < cctx->lowLimit) cctx->dictLimit = cctx->lowLimit; |
Yann Collet | c365215 | 2015-11-24 14:06:07 +0100 | [diff] [blame] | 1699 | } |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1700 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1701 | { size_t cSize = ZSTD_compressBlock_internal(cctx, |
| 1702 | op+ZSTD_blockHeaderSize, dstCapacity-ZSTD_blockHeaderSize, |
| 1703 | ip, blockSize); |
| 1704 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1705 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1706 | if (cSize == 0) { /* block is not compressible */ |
| 1707 | U32 const cBlockHeader24 = lastBlock + (((U32)bt_raw)<<1) + (U32)(blockSize << 3); |
| 1708 | if (blockSize + ZSTD_blockHeaderSize > dstCapacity) return ERROR(dstSize_tooSmall); |
| 1709 | MEM_writeLE32(op, cBlockHeader24); /* 4th byte will be overwritten */ |
| 1710 | memcpy(op + ZSTD_blockHeaderSize, ip, blockSize); |
| 1711 | cSize = ZSTD_blockHeaderSize + blockSize; |
| 1712 | } else { |
| 1713 | U32 const cBlockHeader24 = lastBlock + (((U32)bt_compressed)<<1) + (U32)(cSize << 3); |
| 1714 | MEM_writeLE24(op, cBlockHeader24); |
| 1715 | cSize += ZSTD_blockHeaderSize; |
| 1716 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1717 | |
Yann Collet | a9e5705 | 2017-10-13 02:36:16 -0700 | [diff] [blame] | 1718 | ip += blockSize; |
| 1719 | assert(remaining >= blockSize); |
| 1720 | remaining -= blockSize; |
| 1721 | op += cSize; |
| 1722 | assert(dstCapacity >= cSize); |
| 1723 | dstCapacity -= cSize; |
| 1724 | } } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1725 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 1726 | if (lastFrameChunk && (op>ostart)) cctx->stage = ZSTDcs_ending; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1727 | return op-ostart; |
| 1728 | } |
| 1729 | |
| 1730 | |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1731 | static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity, |
Stella Lau | 97e27af | 2017-08-18 11:20:08 -0700 | [diff] [blame] | 1732 | ZSTD_CCtx_params params, U64 pledgedSrcSize, U32 dictID) |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1733 | { BYTE* const op = (BYTE*)dst; |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 1734 | U32 const dictIDSizeCodeLength = (dictID>0) + (dictID>=256) + (dictID>=65536); /* 0-3 */ |
| 1735 | U32 const dictIDSizeCode = params.fParams.noDictIDFlag ? 0 : dictIDSizeCodeLength; /* 0-3 */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1736 | U32 const checksumFlag = params.fParams.checksumFlag>0; |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1737 | U32 const windowSize = (U32)1 << params.cParams.windowLog; |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 1738 | U32 const singleSegment = params.fParams.contentSizeFlag && (windowSize >= pledgedSrcSize); |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1739 | BYTE const windowLogByte = (BYTE)((params.cParams.windowLog - ZSTD_WINDOWLOG_ABSOLUTEMIN) << 3); |
| 1740 | U32 const fcsCode = params.fParams.contentSizeFlag ? |
Nick Terrell | 55fc1f9 | 2017-05-24 13:50:10 -0700 | [diff] [blame] | 1741 | (pledgedSrcSize>=256) + (pledgedSrcSize>=65536+256) + (pledgedSrcSize>=0xFFFFFFFFU) : 0; /* 0-3 */ |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1742 | BYTE const frameHeaderDecriptionByte = (BYTE)(dictIDSizeCode + (checksumFlag<<2) + (singleSegment<<5) + (fcsCode<<6) ); |
Yann Collet | 62568c9 | 2017-09-25 14:26:26 -0700 | [diff] [blame] | 1743 | size_t pos=0; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1744 | |
| 1745 | if (dstCapacity < ZSTD_frameHeaderSize_max) return ERROR(dstSize_tooSmall); |
Yann Collet | 62568c9 | 2017-09-25 14:26:26 -0700 | [diff] [blame] | 1746 | DEBUGLOG(4, "ZSTD_writeFrameHeader : dictIDFlag : %u ; dictID : %u ; dictIDSizeCode : %u", |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 1747 | !params.fParams.noDictIDFlag, dictID, dictIDSizeCode); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1748 | |
Yann Collet | 62568c9 | 2017-09-25 14:26:26 -0700 | [diff] [blame] | 1749 | if (params.format == ZSTD_f_zstd1) { |
| 1750 | DEBUGLOG(4, "writing zstd magic number"); |
| 1751 | MEM_writeLE32(dst, ZSTD_MAGICNUMBER); |
| 1752 | pos = 4; |
| 1753 | } |
| 1754 | op[pos++] = frameHeaderDecriptionByte; |
Eric Biggers | e4d0265 | 2016-07-26 10:42:19 -0700 | [diff] [blame] | 1755 | if (!singleSegment) op[pos++] = windowLogByte; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1756 | switch(dictIDSizeCode) |
| 1757 | { |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 1758 | default: assert(0); /* impossible */ |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1759 | case 0 : break; |
| 1760 | case 1 : op[pos] = (BYTE)(dictID); pos++; break; |
Yann Collet | d4180ca | 2016-07-27 21:21:36 +0200 | [diff] [blame] | 1761 | case 2 : MEM_writeLE16(op+pos, (U16)dictID); pos+=2; break; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1762 | case 3 : MEM_writeLE32(op+pos, dictID); pos+=4; break; |
| 1763 | } |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 1764 | switch(fcsCode) |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1765 | { |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 1766 | default: assert(0); /* impossible */ |
Eric Biggers | e4d0265 | 2016-07-26 10:42:19 -0700 | [diff] [blame] | 1767 | case 0 : if (singleSegment) op[pos++] = (BYTE)(pledgedSrcSize); break; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 1768 | case 1 : MEM_writeLE16(op+pos, (U16)(pledgedSrcSize-256)); pos+=2; break; |
| 1769 | case 2 : MEM_writeLE32(op+pos, (U32)(pledgedSrcSize)); pos+=4; break; |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1770 | case 3 : MEM_writeLE64(op+pos, (U64)(pledgedSrcSize)); pos+=8; break; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1771 | } |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 1772 | return pos; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1776 | static size_t ZSTD_compressContinue_internal (ZSTD_CCtx* cctx, |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 1777 | void* dst, size_t dstCapacity, |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 1778 | const void* src, size_t srcSize, |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 1779 | U32 frame, U32 lastFrameChunk) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1780 | { |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 1781 | const BYTE* const ip = (const BYTE*) src; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1782 | size_t fhSize = 0; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1783 | |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 1784 | DEBUGLOG(5, "ZSTD_compressContinue_internal"); |
| 1785 | DEBUGLOG(5, "stage: %u", cctx->stage); |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1786 | 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] | 1787 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1788 | if (frame && (cctx->stage==ZSTDcs_init)) { |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 1789 | fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, |
Stella Lau | 6f1a21c | 2017-08-23 10:24:19 -0700 | [diff] [blame] | 1790 | cctx->pledgedSrcSizePlusOne-1, cctx->dictID); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1791 | if (ZSTD_isError(fhSize)) return fhSize; |
| 1792 | dstCapacity -= fhSize; |
| 1793 | dst = (char*)dst + fhSize; |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1794 | cctx->stage = ZSTDcs_ongoing; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1795 | } |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1796 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1797 | /* Check if blocks follow each other */ |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1798 | if (src != cctx->nextSrc) { |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1799 | /* not contiguous */ |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1800 | ptrdiff_t const delta = cctx->nextSrc - ip; |
| 1801 | cctx->lowLimit = cctx->dictLimit; |
| 1802 | cctx->dictLimit = (U32)(cctx->nextSrc - cctx->base); |
| 1803 | cctx->dictBase = cctx->base; |
| 1804 | cctx->base -= delta; |
| 1805 | cctx->nextToUpdate = cctx->dictLimit; |
| 1806 | 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] | 1807 | } |
| 1808 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1809 | /* if input and dictionary overlap : reduce dictionary (area presumed modified by input) */ |
| 1810 | if ((ip+srcSize > cctx->dictBase + cctx->lowLimit) & (ip < cctx->dictBase + cctx->dictLimit)) { |
| 1811 | ptrdiff_t const highInputIdx = (ip + srcSize) - cctx->dictBase; |
| 1812 | U32 const lowLimitMax = (highInputIdx > (ptrdiff_t)cctx->dictLimit) ? cctx->dictLimit : (U32)highInputIdx; |
| 1813 | cctx->lowLimit = lowLimitMax; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1816 | cctx->nextSrc = ip + srcSize; |
Yann Collet | 89db5e0 | 2015-11-13 11:27:46 +0100 | [diff] [blame] | 1817 | |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 1818 | if (srcSize) { |
| 1819 | size_t const cSize = frame ? |
Yann Collet | db8e21d | 2017-05-12 13:46:49 -0700 | [diff] [blame] | 1820 | ZSTD_compress_frameChunk (cctx, dst, dstCapacity, src, srcSize, lastFrameChunk) : |
Yann Collet | 346efcc | 2016-08-02 14:26:00 +0200 | [diff] [blame] | 1821 | ZSTD_compressBlock_internal (cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 1822 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 1823 | cctx->consumedSrcSize += srcSize; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 1824 | return cSize + fhSize; |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 1825 | } else |
| 1826 | return fhSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1827 | } |
| 1828 | |
Yann Collet | 5b56739 | 2016-07-28 01:17:22 +0200 | [diff] [blame] | 1829 | size_t ZSTD_compressContinue (ZSTD_CCtx* cctx, |
Yann Collet | 7cbe79a | 2016-03-23 22:31:57 +0100 | [diff] [blame] | 1830 | void* dst, size_t dstCapacity, |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 1831 | const void* src, size_t srcSize) |
| 1832 | { |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 1833 | 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] | 1834 | } |
| 1835 | |
| 1836 | |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 1837 | size_t ZSTD_getBlockSize(const ZSTD_CCtx* cctx) |
Yann Collet | bf42c8e | 2016-01-09 01:08:23 +0100 | [diff] [blame] | 1838 | { |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 1839 | ZSTD_compressionParameters const cParams = |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 1840 | ZSTD_getCParamsFromCCtxParams(cctx->appliedParams, 0, 0); |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1841 | return MIN (ZSTD_BLOCKSIZE_MAX, (U32)1 << cParams.windowLog); |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | size_t ZSTD_compressBlock(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize) |
| 1845 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 1846 | size_t const blockSizeMax = ZSTD_getBlockSize(cctx); |
Yann Collet | 961b6a0 | 2016-07-15 11:56:53 +0200 | [diff] [blame] | 1847 | if (srcSize > blockSizeMax) return ERROR(srcSize_wrong); |
Yann Collet | 20d5e03 | 2017-04-11 18:34:02 -0700 | [diff] [blame] | 1848 | 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] | 1849 | } |
| 1850 | |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 1851 | /*! ZSTD_loadDictionaryContent() : |
| 1852 | * @return : 0, or an error code |
| 1853 | */ |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1854 | 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] | 1855 | { |
| 1856 | const BYTE* const ip = (const BYTE*) src; |
| 1857 | const BYTE* const iend = ip + srcSize; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 1858 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1859 | /* input becomes current prefix */ |
| 1860 | zc->lowLimit = zc->dictLimit; |
| 1861 | zc->dictLimit = (U32)(zc->nextSrc - zc->base); |
| 1862 | zc->dictBase = zc->base; |
| 1863 | zc->base += ip - zc->nextSrc; |
| 1864 | zc->nextToUpdate = zc->dictLimit; |
Stella Lau | b6cb2ed | 2017-08-18 11:43:31 -0700 | [diff] [blame] | 1865 | zc->loadedDictEnd = zc->appliedParams.forceWindow ? 0 : (U32)(iend - zc->base); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1866 | |
| 1867 | zc->nextSrc = iend; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 1868 | if (srcSize <= HASH_READ_SIZE) return 0; |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1869 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1870 | switch(zc->appliedParams.cParams.strategy) |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1871 | { |
| 1872 | case ZSTD_fast: |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1873 | ZSTD_fillHashTable (zc, iend, zc->appliedParams.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1874 | break; |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1875 | case ZSTD_dfast: |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1876 | ZSTD_fillDoubleHashTable (zc, iend, zc->appliedParams.cParams.searchLength); |
Yann Collet | 45dc356 | 2016-07-12 09:47:31 +0200 | [diff] [blame] | 1877 | break; |
| 1878 | |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1879 | case ZSTD_greedy: |
| 1880 | case ZSTD_lazy: |
| 1881 | case ZSTD_lazy2: |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 1882 | if (srcSize >= HASH_READ_SIZE) |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1883 | ZSTD_insertAndFindFirstIndex(zc, iend-HASH_READ_SIZE, zc->appliedParams.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1884 | break; |
| 1885 | |
| 1886 | case ZSTD_btlazy2: |
Yann Collet | cefef8c | 2016-02-15 07:21:54 +0100 | [diff] [blame] | 1887 | case ZSTD_btopt: |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 1888 | case ZSTD_btultra: |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 1889 | if (srcSize >= HASH_READ_SIZE) |
Nick Terrell | c233bdb | 2017-09-22 14:04:39 -0700 | [diff] [blame] | 1890 | ZSTD_updateTree(zc, iend-HASH_READ_SIZE, iend, (U32)1 << zc->appliedParams.cParams.searchLog, zc->appliedParams.cParams.searchLength); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1891 | break; |
| 1892 | |
| 1893 | default: |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 1894 | assert(0); /* not possible : not a valid strategy id */ |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
Nick Terrell | ecf90ca | 2017-02-13 18:27:34 -0800 | [diff] [blame] | 1897 | zc->nextToUpdate = (U32)(iend - zc->base); |
Yann Collet | 417890c | 2015-12-04 17:16:37 +0100 | [diff] [blame] | 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1902 | /* Dictionaries that assign zero probability to symbols that show up causes problems |
| 1903 | when FSE encoding. Refuse dictionaries that assign zero probability to symbols |
| 1904 | that we may encounter during compression. |
| 1905 | NOTE: This behavior is not standard and could be improved in the future. */ |
| 1906 | static size_t ZSTD_checkDictNCount(short* normalizedCounter, unsigned dictMaxSymbolValue, unsigned maxSymbolValue) { |
| 1907 | U32 s; |
| 1908 | if (dictMaxSymbolValue < maxSymbolValue) return ERROR(dictionary_corrupted); |
| 1909 | for (s = 0; s <= maxSymbolValue; ++s) { |
| 1910 | if (normalizedCounter[s] == 0) return ERROR(dictionary_corrupted); |
| 1911 | } |
| 1912 | return 0; |
| 1913 | } |
| 1914 | |
| 1915 | |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 1916 | /* Dictionary format : |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 1917 | * See : |
| 1918 | * https://github.com/facebook/zstd/blob/master/doc/zstd_compression_format.md#dictionary-format |
| 1919 | */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 1920 | /*! ZSTD_loadZstdDictionary() : |
| 1921 | * @return : 0, or an error code |
| 1922 | * assumptions : magic number supposed already checked |
| 1923 | * dictSize supposed > 8 |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 1924 | */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 1925 | 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] | 1926 | { |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1927 | const BYTE* dictPtr = (const BYTE*)dict; |
| 1928 | const BYTE* const dictEnd = dictPtr + dictSize; |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1929 | short offcodeNCount[MaxOff+1]; |
| 1930 | unsigned offcodeMaxValue = MaxOff; |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 1931 | |
| 1932 | ZSTD_STATIC_ASSERT(sizeof(cctx->entropy->workspace) >= (1<<MAX(MLFSELog,LLFSELog))); |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1933 | |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 1934 | dictPtr += 4; /* skip magic number */ |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 1935 | cctx->dictID = cctx->appliedParams.fParams.noDictIDFlag ? 0 : MEM_readLE32(dictPtr); |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 1936 | dictPtr += 4; |
| 1937 | |
Nick Terrell | a86a709 | 2017-10-03 13:22:13 -0700 | [diff] [blame] | 1938 | { unsigned maxSymbolValue = 255; |
| 1939 | size_t const hufHeaderSize = HUF_readCTable((HUF_CElt*)cctx->entropy->hufCTable, &maxSymbolValue, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1940 | if (HUF_isError(hufHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | a86a709 | 2017-10-03 13:22:13 -0700 | [diff] [blame] | 1941 | if (maxSymbolValue < 255) return ERROR(dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1942 | dictPtr += hufHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1943 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1944 | |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1945 | { unsigned offcodeLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1946 | size_t const offcodeHeaderSize = FSE_readNCount(offcodeNCount, &offcodeMaxValue, &offcodeLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1947 | if (FSE_isError(offcodeHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 1948 | if (offcodeLog > OffFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1949 | /* Defer checking offcodeMaxValue because we need to know the size of the dictionary content */ |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 1950 | CHECK_E( FSE_buildCTable_wksp(cctx->entropy->offcodeCTable, offcodeNCount, offcodeMaxValue, offcodeLog, cctx->entropy->workspace, sizeof(cctx->entropy->workspace)), |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1951 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1952 | dictPtr += offcodeHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1953 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1954 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1955 | { short matchlengthNCount[MaxML+1]; |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 1956 | unsigned matchlengthMaxValue = MaxML, matchlengthLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1957 | size_t const matchlengthHeaderSize = FSE_readNCount(matchlengthNCount, &matchlengthMaxValue, &matchlengthLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1958 | if (FSE_isError(matchlengthHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 1959 | if (matchlengthLog > MLFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1960 | /* Every match length code must have non-zero probability */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1961 | CHECK_F( ZSTD_checkDictNCount(matchlengthNCount, matchlengthMaxValue, MaxML)); |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 1962 | CHECK_E( FSE_buildCTable_wksp(cctx->entropy->matchlengthCTable, matchlengthNCount, matchlengthMaxValue, matchlengthLog, cctx->entropy->workspace, sizeof(cctx->entropy->workspace)), |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1963 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1964 | dictPtr += matchlengthHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1965 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1966 | |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1967 | { short litlengthNCount[MaxLL+1]; |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 1968 | unsigned litlengthMaxValue = MaxLL, litlengthLog; |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1969 | size_t const litlengthHeaderSize = FSE_readNCount(litlengthNCount, &litlengthMaxValue, &litlengthLog, dictPtr, dictEnd-dictPtr); |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1970 | if (FSE_isError(litlengthHeaderSize)) return ERROR(dictionary_corrupted); |
Nick Terrell | bfd943a | 2016-10-17 16:55:52 -0700 | [diff] [blame] | 1971 | if (litlengthLog > LLFSELog) return ERROR(dictionary_corrupted); |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1972 | /* Every literal length code must have non-zero probability */ |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1973 | CHECK_F( ZSTD_checkDictNCount(litlengthNCount, litlengthMaxValue, MaxLL)); |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 1974 | CHECK_E( FSE_buildCTable_wksp(cctx->entropy->litlengthCTable, litlengthNCount, litlengthMaxValue, litlengthLog, cctx->entropy->workspace, sizeof(cctx->entropy->workspace)), |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 1975 | dictionary_corrupted); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1976 | dictPtr += litlengthHeaderSize; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 1977 | } |
Yann Collet | fb810d6 | 2016-01-28 00:18:06 +0100 | [diff] [blame] | 1978 | |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1979 | if (dictPtr+12 > dictEnd) return ERROR(dictionary_corrupted); |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1980 | cctx->seqStore.rep[0] = MEM_readLE32(dictPtr+0); |
| 1981 | cctx->seqStore.rep[1] = MEM_readLE32(dictPtr+4); |
| 1982 | cctx->seqStore.rep[2] = MEM_readLE32(dictPtr+8); |
Yann Collet | 52a0622 | 2016-06-15 13:53:34 +0200 | [diff] [blame] | 1983 | dictPtr += 12; |
| 1984 | |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 1985 | { size_t const dictContentSize = (size_t)(dictEnd - dictPtr); |
| 1986 | U32 offcodeMax = MaxOff; |
| 1987 | if (dictContentSize <= ((U32)-1) - 128 KB) { |
| 1988 | U32 const maxOffset = (U32)dictContentSize + 128 KB; /* The maximum offset that must be supported */ |
| 1989 | offcodeMax = ZSTD_highbit32(maxOffset); /* Calculate minimum offset code required to represent maxOffset */ |
Nick Terrell | b2c39a2 | 2016-10-24 14:11:27 -0700 | [diff] [blame] | 1990 | } |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 1991 | /* All offset values <= dictContentSize + 128 KB must be representable */ |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1992 | CHECK_F (ZSTD_checkDictNCount(offcodeNCount, offcodeMaxValue, MIN(offcodeMax, MaxOff))); |
Yann Collet | bea78e8 | 2017-03-22 18:09:11 -0700 | [diff] [blame] | 1993 | /* All repCodes must be <= dictContentSize and != 0*/ |
| 1994 | { U32 u; |
| 1995 | for (u=0; u<3; u++) { |
Nick Terrell | e198230 | 2017-07-17 12:27:24 -0700 | [diff] [blame] | 1996 | if (cctx->seqStore.rep[u] == 0) return ERROR(dictionary_corrupted); |
| 1997 | if (cctx->seqStore.rep[u] > dictContentSize) return ERROR(dictionary_corrupted); |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 1998 | } } |
Nick Terrell | f9c9af3 | 2016-10-19 17:22:08 -0700 | [diff] [blame] | 1999 | |
Nick Terrell | de0414b | 2017-07-12 19:08:24 -0700 | [diff] [blame] | 2000 | cctx->entropy->hufCTable_repeatMode = HUF_repeat_valid; |
Nick Terrell | 830ef41 | 2017-07-13 12:45:39 -0700 | [diff] [blame] | 2001 | cctx->entropy->offcode_repeatMode = FSE_repeat_valid; |
| 2002 | cctx->entropy->matchlength_repeatMode = FSE_repeat_valid; |
| 2003 | cctx->entropy->litlength_repeatMode = FSE_repeat_valid; |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2004 | return ZSTD_loadDictionaryContent(cctx, dictPtr, dictContentSize); |
| 2005 | } |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2008 | /** ZSTD_compress_insertDictionary() : |
| 2009 | * @return : 0, or an error code */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2010 | static size_t ZSTD_compress_insertDictionary(ZSTD_CCtx* cctx, |
| 2011 | const void* dict, size_t dictSize, |
| 2012 | ZSTD_dictMode_e dictMode) |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2013 | { |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2014 | DEBUGLOG(5, "ZSTD_compress_insertDictionary"); |
Yann Collet | c46fb92 | 2016-05-29 05:01:04 +0200 | [diff] [blame] | 2015 | if ((dict==NULL) || (dictSize<=8)) return 0; |
Yann Collet | b923f65 | 2016-01-26 03:14:20 +0100 | [diff] [blame] | 2016 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2017 | /* dict restricted modes */ |
| 2018 | if (dictMode==ZSTD_dm_rawContent) |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2019 | return ZSTD_loadDictionaryContent(cctx, dict, dictSize); |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2020 | |
Yann Collet | 7d38161 | 2017-06-27 13:50:34 -0700 | [diff] [blame] | 2021 | if (MEM_readLE32(dict) != ZSTD_MAGIC_DICTIONARY) { |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2022 | if (dictMode == ZSTD_dm_auto) { |
| 2023 | DEBUGLOG(5, "raw content dictionary detected"); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2024 | return ZSTD_loadDictionaryContent(cctx, dict, dictSize); |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2025 | } |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2026 | if (dictMode == ZSTD_dm_fullDict) |
| 2027 | return ERROR(dictionary_wrong); |
| 2028 | assert(0); /* impossible */ |
| 2029 | } |
| 2030 | |
| 2031 | /* dict as full zstd dictionary */ |
Yann Collet | 16a0b10 | 2017-03-24 12:46:46 -0700 | [diff] [blame] | 2032 | return ZSTD_loadZstdDictionary(cctx, dict, dictSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2035 | /*! ZSTD_compressBegin_internal() : |
Yann Collet | c3bce24 | 2017-06-20 16:09:11 -0700 | [diff] [blame] | 2036 | * @return : 0, or an error code */ |
Yann Collet | a7737f6 | 2016-09-06 09:44:59 +0200 | [diff] [blame] | 2037 | static size_t ZSTD_compressBegin_internal(ZSTD_CCtx* cctx, |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2038 | const void* dict, size_t dictSize, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2039 | ZSTD_dictMode_e dictMode, |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 2040 | const ZSTD_CDict* cdict, |
Stella Lau | 97e27af | 2017-08-18 11:20:08 -0700 | [diff] [blame] | 2041 | ZSTD_CCtx_params params, U64 pledgedSrcSize, |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 2042 | ZSTD_buffered_policy_e zbuff) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2043 | { |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2044 | DEBUGLOG(4, "ZSTD_compressBegin_internal"); |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 2045 | /* params are supposed to be fully validated at this point */ |
Yann Collet | ab9162e | 2017-04-11 10:46:20 -0700 | [diff] [blame] | 2046 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 2047 | assert(!((dict) && (cdict))); /* either dict or cdict, not both */ |
| 2048 | |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2049 | if (cdict && cdict->dictContentSize>0) { |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 2050 | return ZSTD_copyCCtx_internal(cctx, cdict->refContext, |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2051 | params.fParams, pledgedSrcSize, |
| 2052 | zbuff); |
| 2053 | } |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 2054 | |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2055 | CHECK_F( ZSTD_resetCCtx_internal(cctx, params, pledgedSrcSize, |
| 2056 | ZSTDcrp_continue, zbuff) ); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2057 | return ZSTD_compress_insertDictionary(cctx, dict, dictSize, dictMode); |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2058 | } |
| 2059 | |
Stella Lau | 23fc0e4 | 2017-08-22 14:24:47 -0700 | [diff] [blame] | 2060 | size_t ZSTD_compressBegin_advanced_internal( |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2061 | ZSTD_CCtx* cctx, |
Stella Lau | 63b8c98 | 2017-08-18 16:17:24 -0700 | [diff] [blame] | 2062 | const void* dict, size_t dictSize, |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2063 | ZSTD_dictMode_e dictMode, |
Stella Lau | 63b8c98 | 2017-08-18 16:17:24 -0700 | [diff] [blame] | 2064 | ZSTD_CCtx_params params, |
| 2065 | unsigned long long pledgedSrcSize) |
| 2066 | { |
| 2067 | /* compression parameters verification and optimization */ |
| 2068 | CHECK_F( ZSTD_checkCParams(params.cParams) ); |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2069 | return ZSTD_compressBegin_internal(cctx, dict, dictSize, dictMode, NULL, |
Stella Lau | 63b8c98 | 2017-08-18 16:17:24 -0700 | [diff] [blame] | 2070 | params, pledgedSrcSize, |
| 2071 | ZSTDb_not_buffered); |
| 2072 | } |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2073 | |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2074 | /*! ZSTD_compressBegin_advanced() : |
| 2075 | * @return : 0, or an error code */ |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2076 | size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2077 | const void* dict, size_t dictSize, |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 2078 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2079 | { |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2080 | ZSTD_CCtx_params const cctxParams = |
Stella Lau | 1130377 | 2017-08-22 14:53:13 -0700 | [diff] [blame] | 2081 | ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2082 | return ZSTD_compressBegin_advanced_internal(cctx, dict, dictSize, ZSTD_dm_auto, |
| 2083 | cctxParams, |
Stella Lau | 23fc0e4 | 2017-08-22 14:24:47 -0700 | [diff] [blame] | 2084 | pledgedSrcSize); |
Yann Collet | 27caf2a | 2016-04-01 15:48:48 +0200 | [diff] [blame] | 2085 | } |
| 2086 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2087 | 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] | 2088 | { |
Yann Collet | 6c6e175 | 2016-06-27 15:28:45 +0200 | [diff] [blame] | 2089 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2090 | ZSTD_CCtx_params const cctxParams = |
Stella Lau | 64ce494 | 2017-08-23 12:30:47 -0700 | [diff] [blame] | 2091 | ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2092 | return ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, |
Stella Lau | 64ce494 | 2017-08-23 12:30:47 -0700 | [diff] [blame] | 2093 | cctxParams, 0, ZSTDb_not_buffered); |
Yann Collet | 1c8e194 | 2016-01-26 16:31:22 +0100 | [diff] [blame] | 2094 | } |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2095 | |
Yann Collet | b05c482 | 2017-01-12 02:01:28 +0100 | [diff] [blame] | 2096 | size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel) |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2097 | { |
Yann Collet | b05c482 | 2017-01-12 02:01:28 +0100 | [diff] [blame] | 2098 | return ZSTD_compressBegin_usingDict(cctx, NULL, 0, compressionLevel); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2099 | } |
| 2100 | |
| 2101 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2102 | /*! ZSTD_writeEpilogue() : |
| 2103 | * Ends a frame. |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2104 | * @return : nb of bytes written into dst (or an error code) */ |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2105 | 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] | 2106 | { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2107 | BYTE* const ostart = (BYTE*)dst; |
| 2108 | BYTE* op = ostart; |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2109 | size_t fhSize = 0; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2110 | |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2111 | DEBUGLOG(5, "ZSTD_writeEpilogue"); |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 2112 | if (cctx->stage == ZSTDcs_created) return ERROR(stage_wrong); /* init missing */ |
Yann Collet | 887e7da | 2016-04-11 20:12:27 +0200 | [diff] [blame] | 2113 | |
| 2114 | /* special case : empty frame */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2115 | if (cctx->stage == ZSTDcs_init) { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2116 | fhSize = ZSTD_writeFrameHeader(dst, dstCapacity, cctx->appliedParams, 0, 0); |
Yann Collet | 6236eba | 2016-04-12 15:52:33 +0200 | [diff] [blame] | 2117 | if (ZSTD_isError(fhSize)) return fhSize; |
| 2118 | dstCapacity -= fhSize; |
| 2119 | op += fhSize; |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2120 | cctx->stage = ZSTDcs_ongoing; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 2121 | } |
| 2122 | |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2123 | if (cctx->stage != ZSTDcs_ending) { |
| 2124 | /* write one last empty block, make it the "last" block */ |
| 2125 | U32 const cBlockHeader24 = 1 /* last block */ + (((U32)bt_raw)<<1) + 0; |
| 2126 | if (dstCapacity<4) return ERROR(dstSize_tooSmall); |
| 2127 | MEM_writeLE32(op, cBlockHeader24); |
| 2128 | op += ZSTD_blockHeaderSize; |
| 2129 | dstCapacity -= ZSTD_blockHeaderSize; |
| 2130 | } |
| 2131 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2132 | if (cctx->appliedParams.fParams.checksumFlag) { |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2133 | U32 const checksum = (U32) XXH64_digest(&cctx->xxhState); |
| 2134 | if (dstCapacity<4) return ERROR(dstSize_tooSmall); |
| 2135 | MEM_writeLE32(op, checksum); |
| 2136 | op += 4; |
Yann Collet | f2a3b6e | 2016-05-31 18:13:56 +0200 | [diff] [blame] | 2137 | } |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2138 | |
Yann Collet | 731ef16 | 2016-07-27 21:05:12 +0200 | [diff] [blame] | 2139 | cctx->stage = ZSTDcs_created; /* return to "created but no init" status */ |
Yann Collet | c991cc1 | 2016-07-28 00:55:43 +0200 | [diff] [blame] | 2140 | return op-ostart; |
Yann Collet | 2acb5d3 | 2015-10-29 16:49:43 +0100 | [diff] [blame] | 2141 | } |
| 2142 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2143 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2144 | size_t ZSTD_compressEnd (ZSTD_CCtx* cctx, |
| 2145 | void* dst, size_t dstCapacity, |
| 2146 | const void* src, size_t srcSize) |
| 2147 | { |
| 2148 | size_t endResult; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2149 | size_t const cSize = ZSTD_compressContinue_internal(cctx, |
| 2150 | dst, dstCapacity, src, srcSize, |
| 2151 | 1 /* frame mode */, 1 /* last chunk */); |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2152 | if (ZSTD_isError(cSize)) return cSize; |
| 2153 | endResult = ZSTD_writeEpilogue(cctx, (char*)dst + cSize, dstCapacity-cSize); |
| 2154 | if (ZSTD_isError(endResult)) return endResult; |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2155 | if (cctx->appliedParams.fParams.contentSizeFlag) { /* control src size */ |
Yann Collet | fbd5ab7 | 2017-09-29 19:40:27 -0700 | [diff] [blame] | 2156 | DEBUGLOG(4, "end of frame : controlling src size"); |
Yann Collet | a0ba849 | 2017-06-16 13:29:17 -0700 | [diff] [blame] | 2157 | if (cctx->pledgedSrcSizePlusOne != cctx->consumedSrcSize+1) { |
Yann Collet | fbd5ab7 | 2017-09-29 19:40:27 -0700 | [diff] [blame] | 2158 | DEBUGLOG(4, "error : pledgedSrcSize = %u, while realSrcSize = %u", |
Yann Collet | aee916e | 2017-06-16 17:01:46 -0700 | [diff] [blame] | 2159 | (U32)cctx->pledgedSrcSizePlusOne-1, (U32)cctx->consumedSrcSize); |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 2160 | return ERROR(srcSize_wrong); |
Yann Collet | 9e73f2f | 2017-06-16 12:24:01 -0700 | [diff] [blame] | 2161 | } } |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 2162 | return cSize + endResult; |
| 2163 | } |
| 2164 | |
| 2165 | |
Yann Collet | 19c1002 | 2016-07-28 01:25:46 +0200 | [diff] [blame] | 2166 | static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2167 | void* dst, size_t dstCapacity, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2168 | const void* src, size_t srcSize, |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2169 | const void* dict,size_t dictSize, |
Yann Collet | 88fcd29 | 2015-11-25 14:42:45 +0100 | [diff] [blame] | 2170 | ZSTD_parameters params) |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2171 | { |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2172 | ZSTD_CCtx_params const cctxParams = |
Stella Lau | 1130377 | 2017-08-22 14:53:13 -0700 | [diff] [blame] | 2173 | ZSTD_assignParamsToCCtxParams(cctx->requestedParams, params); |
Stella Lau | 23fc0e4 | 2017-08-22 14:24:47 -0700 | [diff] [blame] | 2174 | return ZSTD_compress_advanced_internal(cctx, |
| 2175 | dst, dstCapacity, |
| 2176 | src, srcSize, |
| 2177 | dict, dictSize, |
| 2178 | cctxParams); |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2179 | } |
| 2180 | |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2181 | size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, |
| 2182 | void* dst, size_t dstCapacity, |
| 2183 | const void* src, size_t srcSize, |
| 2184 | const void* dict,size_t dictSize, |
| 2185 | ZSTD_parameters params) |
| 2186 | { |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 2187 | CHECK_F(ZSTD_checkCParams(params.cParams)); |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2188 | return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); |
| 2189 | } |
| 2190 | |
Stella Lau | 63b8c98 | 2017-08-18 16:17:24 -0700 | [diff] [blame] | 2191 | /* Internal */ |
Stella Lau | 23fc0e4 | 2017-08-22 14:24:47 -0700 | [diff] [blame] | 2192 | size_t ZSTD_compress_advanced_internal( |
Stella Lau | 023b24e | 2017-08-20 22:55:07 -0700 | [diff] [blame] | 2193 | ZSTD_CCtx* cctx, |
| 2194 | void* dst, size_t dstCapacity, |
| 2195 | const void* src, size_t srcSize, |
| 2196 | const void* dict,size_t dictSize, |
| 2197 | ZSTD_CCtx_params params) |
Stella Lau | 63b8c98 | 2017-08-18 16:17:24 -0700 | [diff] [blame] | 2198 | { |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2199 | CHECK_F( ZSTD_compressBegin_internal(cctx, dict, dictSize, ZSTD_dm_auto, NULL, |
Stella Lau | 63b8c98 | 2017-08-18 16:17:24 -0700 | [diff] [blame] | 2200 | params, srcSize, ZSTDb_not_buffered) ); |
| 2201 | return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); |
| 2202 | } |
| 2203 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2204 | size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, |
| 2205 | const void* dict, size_t dictSize, int compressionLevel) |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2206 | { |
Yann Collet | 407a11f | 2016-11-03 15:52:01 -0700 | [diff] [blame] | 2207 | ZSTD_parameters params = ZSTD_getParams(compressionLevel, srcSize, dict ? dictSize : 0); |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2208 | params.fParams.contentSizeFlag = 1; |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2209 | return ZSTD_compress_internal(ctx, dst, dstCapacity, src, srcSize, dict, dictSize, params); |
Yann Collet | 31683c0 | 2015-12-18 01:26:48 +0100 | [diff] [blame] | 2210 | } |
| 2211 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2212 | 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] | 2213 | { |
Yann Collet | 21588e3 | 2016-03-30 16:50:44 +0200 | [diff] [blame] | 2214 | return ZSTD_compress_usingDict(ctx, dst, dstCapacity, src, srcSize, NULL, 0, compressionLevel); |
Yann Collet | 083fcc8 | 2015-10-25 14:06:35 +0100 | [diff] [blame] | 2215 | } |
| 2216 | |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2217 | 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] | 2218 | { |
Yann Collet | 44fe991 | 2015-10-29 22:02:40 +0100 | [diff] [blame] | 2219 | size_t result; |
Yann Collet | 5be2dd2 | 2015-11-11 13:43:58 +0100 | [diff] [blame] | 2220 | ZSTD_CCtx ctxBody; |
Yann Collet | 712def9 | 2015-10-29 18:41:45 +0100 | [diff] [blame] | 2221 | memset(&ctxBody, 0, sizeof(ctxBody)); |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 2222 | ctxBody.customMem = ZSTD_defaultCMem; |
Yann Collet | d1b2684 | 2016-03-15 01:24:33 +0100 | [diff] [blame] | 2223 | result = ZSTD_compressCCtx(&ctxBody, dst, dstCapacity, src, srcSize, compressionLevel); |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 2224 | 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] | 2225 | return result; |
Yann Collet | f3eca25 | 2015-10-22 15:31:46 +0100 | [diff] [blame] | 2226 | } |
Yann Collet | fdcad6d | 2015-12-17 23:50:15 +0100 | [diff] [blame] | 2227 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2228 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2229 | /* ===== Dictionary API ===== */ |
| 2230 | |
Yann Collet | 09ae03a | 2017-06-26 16:47:32 -0700 | [diff] [blame] | 2231 | /*! ZSTD_estimateCDictSize_advanced() : |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 2232 | * Estimate amount of memory that will be needed to create a dictionary with following arguments */ |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2233 | size_t ZSTD_estimateCDictSize_advanced( |
| 2234 | size_t dictSize, ZSTD_compressionParameters cParams, |
| 2235 | ZSTD_dictLoadMethod_e dictLoadMethod) |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 2236 | { |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2237 | DEBUGLOG(5, "sizeof(ZSTD_CDict) : %u", (U32)sizeof(ZSTD_CDict)); |
Stella Lau | b5b9275 | 2017-08-29 10:49:29 -0700 | [diff] [blame] | 2238 | DEBUGLOG(5, "CCtx estimate : %u", |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 2239 | (U32)ZSTD_estimateCCtxSize_usingCParams(cParams)); |
| 2240 | return sizeof(ZSTD_CDict) + ZSTD_estimateCCtxSize_usingCParams(cParams) |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2241 | + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize); |
Yann Collet | a1d6704 | 2017-05-08 17:51:49 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
Yann Collet | 09ae03a | 2017-06-26 16:47:32 -0700 | [diff] [blame] | 2244 | size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel) |
| 2245 | { |
| 2246 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); |
Stella Lau | ee65701 | 2017-08-29 20:27:35 -0700 | [diff] [blame] | 2247 | return ZSTD_estimateCDictSize_advanced(dictSize, cParams, ZSTD_dlm_byCopy); |
Yann Collet | 09ae03a | 2017-06-26 16:47:32 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 2250 | size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict) |
| 2251 | { |
| 2252 | if (cdict==NULL) return 0; /* support sizeof on NULL */ |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2253 | DEBUGLOG(5, "sizeof(*cdict) : %u", (U32)sizeof(*cdict)); |
| 2254 | DEBUGLOG(5, "ZSTD_sizeof_CCtx : %u", (U32)ZSTD_sizeof_CCtx(cdict->refContext)); |
Yann Collet | aca113f | 2016-12-23 22:25:03 +0100 | [diff] [blame] | 2255 | 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] | 2256 | } |
| 2257 | |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2258 | static size_t ZSTD_initCDict_internal( |
| 2259 | ZSTD_CDict* cdict, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2260 | const void* dictBuffer, size_t dictSize, |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2261 | ZSTD_dictLoadMethod_e dictLoadMethod, |
| 2262 | ZSTD_dictMode_e dictMode, |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2263 | ZSTD_compressionParameters cParams) |
| 2264 | { |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2265 | DEBUGLOG(5, "ZSTD_initCDict_internal, mode %u", (U32)dictMode); |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2266 | if ((dictLoadMethod == ZSTD_dlm_byRef) || (!dictBuffer) || (!dictSize)) { |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2267 | cdict->dictBuffer = NULL; |
| 2268 | cdict->dictContent = dictBuffer; |
| 2269 | } else { |
| 2270 | void* const internalBuffer = ZSTD_malloc(dictSize, cdict->refContext->customMem); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2271 | cdict->dictBuffer = internalBuffer; |
| 2272 | cdict->dictContent = internalBuffer; |
Yann Collet | c3bce24 | 2017-06-20 16:09:11 -0700 | [diff] [blame] | 2273 | if (!internalBuffer) return ERROR(memory_allocation); |
| 2274 | memcpy(internalBuffer, dictBuffer, dictSize); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2275 | } |
| 2276 | cdict->dictContentSize = dictSize; |
| 2277 | |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2278 | { ZSTD_CCtx_params cctxParams = cdict->refContext->requestedParams; |
Stella Lau | 5b956f4 | 2017-08-21 14:49:16 -0700 | [diff] [blame] | 2279 | cctxParams.cParams = cParams; |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2280 | CHECK_F( ZSTD_compressBegin_internal(cdict->refContext, |
| 2281 | cdict->dictContent, dictSize, dictMode, |
| 2282 | NULL, |
Stella Lau | 5b956f4 | 2017-08-21 14:49:16 -0700 | [diff] [blame] | 2283 | cctxParams, ZSTD_CONTENTSIZE_UNKNOWN, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2284 | ZSTDb_not_buffered) ); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | return 0; |
| 2288 | } |
| 2289 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2290 | ZSTD_CDict* ZSTD_createCDict_advanced(const void* dictBuffer, size_t dictSize, |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2291 | ZSTD_dictLoadMethod_e dictLoadMethod, |
| 2292 | ZSTD_dictMode_e dictMode, |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 2293 | ZSTD_compressionParameters cParams, ZSTD_customMem customMem) |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2294 | { |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2295 | DEBUGLOG(5, "ZSTD_createCDict_advanced, mode %u", (U32)dictMode); |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 2296 | if (!customMem.customAlloc ^ !customMem.customFree) return NULL; |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2297 | |
Yann Collet | 466f92e | 2017-06-20 16:25:29 -0700 | [diff] [blame] | 2298 | { ZSTD_CDict* const cdict = (ZSTD_CDict*)ZSTD_malloc(sizeof(ZSTD_CDict), customMem); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2299 | ZSTD_CCtx* const cctx = ZSTD_createCCtx_advanced(customMem); |
| 2300 | |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2301 | if (!cdict || !cctx) { |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2302 | ZSTD_free(cdict, customMem); |
Przemyslaw Skibinski | d8114e5 | 2017-02-21 18:59:56 +0100 | [diff] [blame] | 2303 | ZSTD_freeCCtx(cctx); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2304 | return NULL; |
| 2305 | } |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2306 | cdict->refContext = cctx; |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2307 | if (ZSTD_isError( ZSTD_initCDict_internal(cdict, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2308 | dictBuffer, dictSize, |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2309 | dictLoadMethod, dictMode, |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2310 | cParams) )) { |
| 2311 | ZSTD_freeCDict(cdict); |
| 2312 | return NULL; |
Nick Terrell | 3b9cdf9 | 2016-10-12 20:54:42 -0700 | [diff] [blame] | 2313 | } |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2314 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2315 | return cdict; |
| 2316 | } |
| 2317 | } |
| 2318 | |
| 2319 | ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) |
| 2320 | { |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 2321 | ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2322 | return ZSTD_createCDict_advanced(dict, dictSize, |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2323 | ZSTD_dlm_byCopy, ZSTD_dm_auto, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2324 | cParams, ZSTD_defaultCMem); |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 2325 | } |
| 2326 | |
| 2327 | ZSTD_CDict* ZSTD_createCDict_byReference(const void* dict, size_t dictSize, int compressionLevel) |
| 2328 | { |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 2329 | ZSTD_compressionParameters cParams = ZSTD_getCParams(compressionLevel, 0, dictSize); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2330 | return ZSTD_createCDict_advanced(dict, dictSize, |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2331 | ZSTD_dlm_byRef, ZSTD_dm_auto, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2332 | cParams, ZSTD_defaultCMem); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | size_t ZSTD_freeCDict(ZSTD_CDict* cdict) |
| 2336 | { |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2337 | if (cdict==NULL) return 0; /* support free on NULL */ |
Yann Collet | 993060e | 2016-09-21 16:46:08 +0200 | [diff] [blame] | 2338 | { ZSTD_customMem const cMem = cdict->refContext->customMem; |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2339 | ZSTD_freeCCtx(cdict->refContext); |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 2340 | ZSTD_free(cdict->dictBuffer, cMem); |
Yann Collet | 23b6e05 | 2016-08-28 21:05:43 -0700 | [diff] [blame] | 2341 | ZSTD_free(cdict, cMem); |
| 2342 | return 0; |
| 2343 | } |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2344 | } |
| 2345 | |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2346 | /*! ZSTD_initStaticCDict_advanced() : |
| 2347 | * Generate a digested dictionary in provided memory area. |
| 2348 | * workspace: The memory area to emplace the dictionary into. |
| 2349 | * Provided pointer must 8-bytes aligned. |
| 2350 | * It must outlive dictionary usage. |
| 2351 | * workspaceSize: Use ZSTD_estimateCDictSize() |
| 2352 | * to determine how large workspace must be. |
| 2353 | * cParams : use ZSTD_getCParams() to transform a compression level |
| 2354 | * into its relevants cParams. |
| 2355 | * @return : pointer to ZSTD_CDict*, or NULL if error (size too small) |
| 2356 | * Note : there is no corresponding "free" function. |
| 2357 | * Since workspace was allocated externally, it must be freed externally. |
| 2358 | */ |
| 2359 | ZSTD_CDict* ZSTD_initStaticCDict(void* workspace, size_t workspaceSize, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2360 | const void* dict, size_t dictSize, |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2361 | ZSTD_dictLoadMethod_e dictLoadMethod, |
| 2362 | ZSTD_dictMode_e dictMode, |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2363 | ZSTD_compressionParameters cParams) |
| 2364 | { |
Yann Collet | 96f0cde | 2017-09-24 16:47:02 -0700 | [diff] [blame] | 2365 | size_t const cctxSize = ZSTD_estimateCCtxSize_usingCParams(cParams); |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2366 | size_t const neededSize = sizeof(ZSTD_CDict) + (dictLoadMethod == ZSTD_dlm_byRef ? 0 : dictSize) |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2367 | + cctxSize; |
| 2368 | ZSTD_CDict* const cdict = (ZSTD_CDict*) workspace; |
| 2369 | void* ptr; |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 2370 | DEBUGLOG(5, "(size_t)workspace & 7 : %u", (U32)(size_t)workspace & 7); |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2371 | if ((size_t)workspace & 7) return NULL; /* 8-aligned */ |
Yann Collet | 2cf7755 | 2017-06-16 12:34:41 -0700 | [diff] [blame] | 2372 | DEBUGLOG(5, "(workspaceSize < neededSize) : (%u < %u) => %u", |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2373 | (U32)workspaceSize, (U32)neededSize, (U32)(workspaceSize < neededSize)); |
| 2374 | if (workspaceSize < neededSize) return NULL; |
| 2375 | |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2376 | if (dictLoadMethod == ZSTD_dlm_byCopy) { |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2377 | memcpy(cdict+1, dict, dictSize); |
| 2378 | dict = cdict+1; |
| 2379 | ptr = (char*)workspace + sizeof(ZSTD_CDict) + dictSize; |
| 2380 | } else { |
| 2381 | ptr = cdict+1; |
| 2382 | } |
| 2383 | cdict->refContext = ZSTD_initStaticCCtx(ptr, cctxSize); |
| 2384 | |
| 2385 | if (ZSTD_isError( ZSTD_initCDict_internal(cdict, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2386 | dict, dictSize, |
Stella Lau | c88fb92 | 2017-08-29 11:55:02 -0700 | [diff] [blame] | 2387 | ZSTD_dlm_byRef, dictMode, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2388 | cParams) )) |
Yann Collet | cdf7e82 | 2017-05-25 18:05:49 -0700 | [diff] [blame] | 2389 | return NULL; |
| 2390 | |
| 2391 | return cdict; |
| 2392 | } |
| 2393 | |
Stella Lau | 024098a | 2017-08-25 17:58:28 -0700 | [diff] [blame] | 2394 | ZSTD_compressionParameters ZSTD_getCParamsFromCDict(const ZSTD_CDict* cdict) { |
| 2395 | return cdict->refContext->appliedParams.cParams; |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 2396 | } |
| 2397 | |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 2398 | /* ZSTD_compressBegin_usingCDict_advanced() : |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2399 | * cdict must be != NULL */ |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 2400 | size_t ZSTD_compressBegin_usingCDict_advanced( |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2401 | ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, |
| 2402 | ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize) |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 2403 | { |
Yann Collet | 5ac72b4 | 2017-05-23 11:18:24 -0700 | [diff] [blame] | 2404 | if (cdict==NULL) return ERROR(dictionary_wrong); |
Stella Lau | 024098a | 2017-08-25 17:58:28 -0700 | [diff] [blame] | 2405 | { ZSTD_CCtx_params params = cctx->requestedParams; |
| 2406 | params.cParams = ZSTD_getCParamsFromCDict(cdict); |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2407 | params.fParams = fParams; |
Yann Collet | 1880337 | 2017-05-22 18:21:51 -0700 | [diff] [blame] | 2408 | DEBUGLOG(5, "ZSTD_compressBegin_usingCDict_advanced"); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2409 | return ZSTD_compressBegin_internal(cctx, |
| 2410 | NULL, 0, ZSTD_dm_auto, |
| 2411 | cdict, |
| 2412 | params, pledgedSrcSize, |
| 2413 | ZSTDb_not_buffered); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 2414 | } |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 2415 | } |
| 2416 | |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2417 | /* ZSTD_compressBegin_usingCDict() : |
| 2418 | * pledgedSrcSize=0 means "unknown" |
| 2419 | * if pledgedSrcSize>0, it will enable contentSizeFlag */ |
Yann Collet | 768df12 | 2017-04-26 15:42:10 -0700 | [diff] [blame] | 2420 | size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict) |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2421 | { |
Yann Collet | 768df12 | 2017-04-26 15:42:10 -0700 | [diff] [blame] | 2422 | ZSTD_frameParameters const fParams = { 0 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2423 | DEBUGLOG(5, "ZSTD_compressBegin_usingCDict : dictIDFlag == %u", !fParams.noDictIDFlag); |
Yann Collet | 768df12 | 2017-04-26 15:42:10 -0700 | [diff] [blame] | 2424 | return ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, 0); |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2425 | } |
| 2426 | |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 2427 | size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, |
| 2428 | void* dst, size_t dstCapacity, |
| 2429 | const void* src, size_t srcSize, |
| 2430 | const ZSTD_CDict* cdict, ZSTD_frameParameters fParams) |
| 2431 | { |
| 2432 | CHECK_F (ZSTD_compressBegin_usingCDict_advanced(cctx, cdict, fParams, srcSize)); /* will check if cdict != NULL */ |
| 2433 | return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2434 | } |
| 2435 | |
Yann Collet | 0763905 | 2016-08-03 01:57:57 +0200 | [diff] [blame] | 2436 | /*! ZSTD_compress_usingCDict() : |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2437 | * Compression using a digested Dictionary. |
| 2438 | * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. |
| 2439 | * Note that compression parameters are decided at CDict creation time |
| 2440 | * while frame parameters are hardcoded */ |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 2441 | size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, |
| 2442 | void* dst, size_t dstCapacity, |
| 2443 | const void* src, size_t srcSize, |
| 2444 | const ZSTD_CDict* cdict) |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2445 | { |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 2446 | ZSTD_frameParameters const fParams = { 1 /*content*/, 0 /*checksum*/, 0 /*noDictID*/ }; |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 2447 | return ZSTD_compress_usingCDict_advanced(cctx, dst, dstCapacity, src, srcSize, cdict, fParams); |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 2448 | } |
| 2449 | |
| 2450 | |
| 2451 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 2452 | /* ****************************************************************** |
| 2453 | * Streaming |
| 2454 | ********************************************************************/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2455 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2456 | ZSTD_CStream* ZSTD_createCStream(void) |
| 2457 | { |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 2458 | return ZSTD_createCStream_advanced(ZSTD_defaultCMem); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2459 | } |
| 2460 | |
Yann Collet | dde10b2 | 2017-06-26 17:44:26 -0700 | [diff] [blame] | 2461 | ZSTD_CStream* ZSTD_initStaticCStream(void *workspace, size_t workspaceSize) |
| 2462 | { |
| 2463 | return ZSTD_initStaticCCtx(workspace, workspaceSize); |
| 2464 | } |
| 2465 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2466 | ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem) |
Yann Collet | ae728a4 | 2017-05-30 17:11:39 -0700 | [diff] [blame] | 2467 | { /* CStream and CCtx are now same object */ |
Yann Collet | 6fb2f24 | 2017-05-10 11:06:06 -0700 | [diff] [blame] | 2468 | return ZSTD_createCCtx_advanced(customMem); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | size_t ZSTD_freeCStream(ZSTD_CStream* zcs) |
| 2472 | { |
Yann Collet | 7855366 | 2017-05-08 17:15:00 -0700 | [diff] [blame] | 2473 | return ZSTD_freeCCtx(zcs); /* same object */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2474 | } |
| 2475 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2476 | |
| 2477 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 2478 | /*====== Initialization ======*/ |
| 2479 | |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 2480 | size_t ZSTD_CStreamInSize(void) { return ZSTD_BLOCKSIZE_MAX; } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2481 | |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2482 | size_t ZSTD_CStreamOutSize(void) |
| 2483 | { |
Yann Collet | fa3671e | 2017-05-19 10:51:30 -0700 | [diff] [blame] | 2484 | return ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + 4 /* 32-bits hash */ ; |
Yann Collet | c17e020 | 2017-04-20 12:50:02 -0700 | [diff] [blame] | 2485 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2486 | |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2487 | static size_t ZSTD_resetCStream_internal(ZSTD_CStream* zcs, |
Yann Collet | 2e42742 | 2017-06-27 17:09:12 -0700 | [diff] [blame] | 2488 | const void* dict, size_t dictSize, ZSTD_dictMode_e dictMode, |
| 2489 | const ZSTD_CDict* cdict, |
Stella Lau | ee65701 | 2017-08-29 20:27:35 -0700 | [diff] [blame] | 2490 | const ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 2491 | { |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2492 | DEBUGLOG(4, "ZSTD_resetCStream_internal"); |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 2493 | /* params are supposed to be fully validated at this point */ |
| 2494 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
| 2495 | assert(!((dict) && (cdict))); /* either dict or cdict, not both */ |
Yann Collet | ee5b725 | 2016-10-27 14:20:55 -0700 | [diff] [blame] | 2496 | |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2497 | CHECK_F( ZSTD_compressBegin_internal(zcs, |
Yann Collet | 2e42742 | 2017-06-27 17:09:12 -0700 | [diff] [blame] | 2498 | dict, dictSize, dictMode, |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 2499 | cdict, |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2500 | params, pledgedSrcSize, |
| 2501 | ZSTDb_buffered) ); |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 2502 | |
| 2503 | zcs->inToCompress = 0; |
| 2504 | zcs->inBuffPos = 0; |
| 2505 | zcs->inBuffTarget = zcs->blockSize; |
| 2506 | zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 2507 | zcs->streamStage = zcss_load; |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 2508 | zcs->frameEnded = 0; |
| 2509 | return 0; /* ready to go */ |
| 2510 | } |
| 2511 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 2512 | size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize) |
| 2513 | { |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 2514 | ZSTD_CCtx_params params = zcs->requestedParams; |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 2515 | params.fParams.contentSizeFlag = (pledgedSrcSize > 0); |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 2516 | params.cParams = ZSTD_getCParamsFromCCtxParams(params, pledgedSrcSize, 0); |
Yann Collet | db1668a | 2017-09-29 18:05:18 -0700 | [diff] [blame] | 2517 | DEBUGLOG(4, "ZSTD_resetCStream"); |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2518 | return ZSTD_resetCStream_internal(zcs, NULL, 0, ZSTD_dm_auto, zcs->cdict, params, pledgedSrcSize); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 2519 | } |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 2520 | |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2521 | /*! ZSTD_initCStream_internal() : |
Yann Collet | b737293 | 2017-06-27 15:49:12 -0700 | [diff] [blame] | 2522 | * Note : not static, but hidden (not exposed). Used by zstdmt_compress.c |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2523 | * Assumption 1 : params are valid |
| 2524 | * Assumption 2 : either dict, or cdict, is defined, not both */ |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2525 | size_t ZSTD_initCStream_internal(ZSTD_CStream* zcs, |
| 2526 | const void* dict, size_t dictSize, const ZSTD_CDict* cdict, |
Stella Lau | ee65701 | 2017-08-29 20:27:35 -0700 | [diff] [blame] | 2527 | ZSTD_CCtx_params params, unsigned long long pledgedSrcSize) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2528 | { |
Yann Collet | db1668a | 2017-09-29 18:05:18 -0700 | [diff] [blame] | 2529 | DEBUGLOG(4, "ZSTD_initCStream_internal"); |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 2530 | assert(!ZSTD_isError(ZSTD_checkCParams(params.cParams))); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2531 | assert(!((dict) && (cdict))); /* either dict or cdict, not both */ |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 2532 | |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 2533 | if (dict && dictSize >= 8) { |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2534 | DEBUGLOG(5, "loading dictionary of size %u", (U32)dictSize); |
Yann Collet | c7fe262 | 2017-05-23 13:16:00 -0700 | [diff] [blame] | 2535 | if (zcs->staticSize) { /* static CCtx : never uses malloc */ |
| 2536 | /* incompatible with internal cdict creation */ |
| 2537 | return ERROR(memory_allocation); |
Yann Collet | 02d37aa | 2017-04-05 14:53:51 -0700 | [diff] [blame] | 2538 | } |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 2539 | ZSTD_freeCDict(zcs->cdictLocal); |
Yann Collet | 7bd1a29 | 2017-06-21 11:50:33 -0700 | [diff] [blame] | 2540 | zcs->cdictLocal = ZSTD_createCDict_advanced(dict, dictSize, |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2541 | ZSTD_dlm_byCopy, ZSTD_dm_auto, |
Yann Collet | 204b6b7 | 2017-06-21 15:13:00 -0700 | [diff] [blame] | 2542 | params.cParams, zcs->customMem); |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 2543 | zcs->cdict = zcs->cdictLocal; |
Yann Collet | 466f92e | 2017-06-20 16:25:29 -0700 | [diff] [blame] | 2544 | if (zcs->cdictLocal == NULL) return ERROR(memory_allocation); |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2545 | } else { |
| 2546 | if (cdict) { |
Stella Lau | 024098a | 2017-08-25 17:58:28 -0700 | [diff] [blame] | 2547 | params.cParams = ZSTD_getCParamsFromCDict(cdict); /* cParams are enforced from cdict */ |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2548 | } |
Yann Collet | 466f92e | 2017-06-20 16:25:29 -0700 | [diff] [blame] | 2549 | ZSTD_freeCDict(zcs->cdictLocal); |
| 2550 | zcs->cdictLocal = NULL; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2551 | zcs->cdict = cdict; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2552 | } |
Sean Purcell | 57d423c | 2017-01-17 11:04:08 -0800 | [diff] [blame] | 2553 | |
Stella Lau | 64ce494 | 2017-08-23 12:30:47 -0700 | [diff] [blame] | 2554 | params.compressionLevel = ZSTD_CLEVEL_CUSTOM; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2555 | zcs->requestedParams = params; |
Stella Lau | b6cb2ed | 2017-08-18 11:43:31 -0700 | [diff] [blame] | 2556 | |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2557 | return ZSTD_resetCStream_internal(zcs, NULL, 0, ZSTD_dm_auto, zcs->cdict, params, pledgedSrcSize); |
Yann Collet | ee5b725 | 2016-10-27 14:20:55 -0700 | [diff] [blame] | 2558 | } |
| 2559 | |
| 2560 | /* ZSTD_initCStream_usingCDict_advanced() : |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2561 | * same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2562 | size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, |
| 2563 | const ZSTD_CDict* cdict, |
| 2564 | ZSTD_frameParameters fParams, |
| 2565 | unsigned long long pledgedSrcSize) |
| 2566 | { /* cannot handle NULL cdict (does not know what to do) */ |
| 2567 | if (!cdict) return ERROR(dictionary_wrong); |
Stella Lau | 024098a | 2017-08-25 17:58:28 -0700 | [diff] [blame] | 2568 | { ZSTD_CCtx_params params = zcs->requestedParams; |
| 2569 | params.cParams = ZSTD_getCParamsFromCDict(cdict); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2570 | params.fParams = fParams; |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2571 | return ZSTD_initCStream_internal(zcs, |
| 2572 | NULL, 0, cdict, |
| 2573 | params, pledgedSrcSize); |
Yann Collet | e88034f | 2017-04-10 22:24:02 -0700 | [diff] [blame] | 2574 | } |
| 2575 | } |
| 2576 | |
| 2577 | /* note : cdict must outlive compression session */ |
| 2578 | size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict) |
| 2579 | { |
Yann Collet | 8c910d2 | 2017-06-03 01:15:02 -0700 | [diff] [blame] | 2580 | ZSTD_frameParameters const fParams = { 0 /* contentSize */, 0 /* checksum */, 0 /* hideDictID */ }; |
| 2581 | return ZSTD_initCStream_usingCDict_advanced(zcs, cdict, fParams, 0); /* note : will check that cdict != NULL */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2582 | } |
| 2583 | |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 2584 | size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, |
| 2585 | const void* dict, size_t dictSize, |
| 2586 | ZSTD_parameters params, unsigned long long pledgedSrcSize) |
| 2587 | { |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2588 | ZSTD_CCtx_params const cctxParams = |
Stella Lau | 1130377 | 2017-08-22 14:53:13 -0700 | [diff] [blame] | 2589 | ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); |
Yann Collet | 4b987ad | 2017-04-10 17:50:44 -0700 | [diff] [blame] | 2590 | CHECK_F( ZSTD_checkCParams(params.cParams) ); |
Stella Lau | 8fd1636 | 2017-08-21 18:10:44 -0700 | [diff] [blame] | 2591 | return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, pledgedSrcSize); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 2592 | } |
| 2593 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2594 | size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel) |
| 2595 | { |
| 2596 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2597 | ZSTD_CCtx_params const cctxParams = |
Stella Lau | 1130377 | 2017-08-22 14:53:13 -0700 | [diff] [blame] | 2598 | ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); |
Stella Lau | 8fd1636 | 2017-08-21 18:10:44 -0700 | [diff] [blame] | 2599 | return ZSTD_initCStream_internal(zcs, dict, dictSize, NULL, cctxParams, 0); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2600 | } |
| 2601 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 2602 | size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize) |
| 2603 | { |
Stella Lau | 64ce494 | 2017-08-23 12:30:47 -0700 | [diff] [blame] | 2604 | ZSTD_CCtx_params cctxParams; |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2605 | ZSTD_parameters const params = ZSTD_getParams(compressionLevel, pledgedSrcSize, 0); |
Stella Lau | 64ce494 | 2017-08-23 12:30:47 -0700 | [diff] [blame] | 2606 | cctxParams = ZSTD_assignParamsToCCtxParams(zcs->requestedParams, params); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2607 | cctxParams.fParams.contentSizeFlag = (pledgedSrcSize>0); |
Stella Lau | 64ce494 | 2017-08-23 12:30:47 -0700 | [diff] [blame] | 2608 | return ZSTD_initCStream_internal(zcs, NULL, 0, NULL, cctxParams, pledgedSrcSize); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2611 | size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel) |
| 2612 | { |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 2613 | return ZSTD_initCStream_srcSize(zcs, compressionLevel, 0); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 2614 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2615 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 2616 | /*====== Compression ======*/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2617 | |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2618 | MEM_STATIC size_t ZSTD_limitCopy(void* dst, size_t dstCapacity, |
| 2619 | const void* src, size_t srcSize) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2620 | { |
| 2621 | size_t const length = MIN(dstCapacity, srcSize); |
Yann Collet | 18ab5af | 2017-05-31 09:59:22 -0700 | [diff] [blame] | 2622 | if (length) memcpy(dst, src, length); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2623 | return length; |
| 2624 | } |
| 2625 | |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2626 | /** ZSTD_compressStream_generic(): |
| 2627 | * internal function for all *compressStream*() variants and *compress_generic() |
Yann Collet | 6ee05a0 | 2017-09-25 15:41:48 -0700 | [diff] [blame] | 2628 | * non-static, because can be called from zstdmt.c |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2629 | * @return : hint size for next input */ |
Yann Collet | d5c046c | 2017-06-30 14:51:01 -0700 | [diff] [blame] | 2630 | size_t ZSTD_compressStream_generic(ZSTD_CStream* zcs, |
| 2631 | ZSTD_outBuffer* output, |
| 2632 | ZSTD_inBuffer* input, |
| 2633 | ZSTD_EndDirective const flushMode) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2634 | { |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2635 | const char* const istart = (const char*)input->src; |
| 2636 | const char* const iend = istart + input->size; |
| 2637 | const char* ip = istart + input->pos; |
| 2638 | char* const ostart = (char*)output->dst; |
| 2639 | char* const oend = ostart + output->size; |
| 2640 | char* op = ostart + output->pos; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2641 | U32 someMoreWork = 1; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2642 | |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 2643 | /* check expectations */ |
Yann Collet | 2cb9774 | 2017-07-04 12:39:26 -0700 | [diff] [blame] | 2644 | DEBUGLOG(5, "ZSTD_compressStream_generic, flush=%u", (U32)flushMode); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2645 | assert(zcs->inBuff != NULL); |
Yann Collet | ecb0f46 | 2017-06-21 17:25:01 -0700 | [diff] [blame] | 2646 | assert(zcs->inBuffSize>0); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2647 | assert(zcs->outBuff!= NULL); |
Yann Collet | ecb0f46 | 2017-06-21 17:25:01 -0700 | [diff] [blame] | 2648 | assert(zcs->outBuffSize>0); |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 2649 | assert(output->pos <= output->size); |
| 2650 | assert(input->pos <= input->size); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2651 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2652 | while (someMoreWork) { |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 2653 | switch(zcs->streamStage) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2654 | { |
Yann Collet | 1ad7c82 | 2017-05-22 17:06:04 -0700 | [diff] [blame] | 2655 | case zcss_init: |
| 2656 | /* call ZSTD_initCStream() first ! */ |
| 2657 | return ERROR(init_missing); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2658 | |
| 2659 | case zcss_load: |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2660 | if ( (flushMode == ZSTD_e_end) |
Yann Collet | 2084b04 | 2017-07-03 15:52:19 -0700 | [diff] [blame] | 2661 | && ((size_t)(oend-op) >= ZSTD_compressBound(iend-ip)) /* enough dstCapacity */ |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2662 | && (zcs->inBuffPos == 0) ) { |
| 2663 | /* shortcut to compression pass directly into output buffer */ |
| 2664 | size_t const cSize = ZSTD_compressEnd(zcs, |
| 2665 | op, oend-op, ip, iend-ip); |
| 2666 | DEBUGLOG(4, "ZSTD_compressEnd : %u", (U32)cSize); |
| 2667 | if (ZSTD_isError(cSize)) return cSize; |
| 2668 | ip = iend; |
| 2669 | op += cSize; |
| 2670 | zcs->frameEnded = 1; |
| 2671 | ZSTD_startNewCompression(zcs); |
| 2672 | someMoreWork = 0; break; |
Yann Collet | 2084b04 | 2017-07-03 15:52:19 -0700 | [diff] [blame] | 2673 | } |
| 2674 | /* complete loading into inBuffer */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2675 | { size_t const toLoad = zcs->inBuffTarget - zcs->inBuffPos; |
Yann Collet | 06589fe | 2017-05-31 10:03:20 -0700 | [diff] [blame] | 2676 | size_t const loaded = ZSTD_limitCopy( |
| 2677 | zcs->inBuff + zcs->inBuffPos, toLoad, |
| 2678 | ip, iend-ip); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2679 | zcs->inBuffPos += loaded; |
| 2680 | ip += loaded; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2681 | if ( (flushMode == ZSTD_e_continue) |
| 2682 | && (zcs->inBuffPos < zcs->inBuffTarget) ) { |
| 2683 | /* not enough input to fill full block : stop here */ |
| 2684 | someMoreWork = 0; break; |
| 2685 | } |
| 2686 | if ( (flushMode == ZSTD_e_flush) |
| 2687 | && (zcs->inBuffPos == zcs->inToCompress) ) { |
| 2688 | /* empty */ |
| 2689 | someMoreWork = 0; break; |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 2690 | } |
| 2691 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2692 | /* compress current block (note : this stage cannot be stopped in the middle) */ |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2693 | DEBUGLOG(5, "stream compression stage (flushMode==%u)", flushMode); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2694 | { void* cDst; |
| 2695 | size_t cSize; |
| 2696 | size_t const iSize = zcs->inBuffPos - zcs->inToCompress; |
| 2697 | size_t oSize = oend-op; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2698 | unsigned const lastBlock = (flushMode == ZSTD_e_end) && (ip==iend); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2699 | if (oSize >= ZSTD_compressBound(iSize)) |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 2700 | cDst = op; /* compress into output buffer, to skip flush stage */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2701 | else |
| 2702 | cDst = zcs->outBuff, oSize = zcs->outBuffSize; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2703 | cSize = lastBlock ? |
| 2704 | ZSTD_compressEnd(zcs, cDst, oSize, |
| 2705 | zcs->inBuff + zcs->inToCompress, iSize) : |
| 2706 | ZSTD_compressContinue(zcs, cDst, oSize, |
| 2707 | zcs->inBuff + zcs->inToCompress, iSize); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2708 | if (ZSTD_isError(cSize)) return cSize; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2709 | zcs->frameEnded = lastBlock; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2710 | /* prepare next block */ |
| 2711 | zcs->inBuffTarget = zcs->inBuffPos + zcs->blockSize; |
| 2712 | if (zcs->inBuffTarget > zcs->inBuffSize) |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2713 | zcs->inBuffPos = 0, zcs->inBuffTarget = zcs->blockSize; |
Yann Collet | 8b21ec4 | 2017-05-19 19:46:15 -0700 | [diff] [blame] | 2714 | DEBUGLOG(5, "inBuffTarget:%u / inBuffSize:%u", |
| 2715 | (U32)zcs->inBuffTarget, (U32)zcs->inBuffSize); |
| 2716 | if (!lastBlock) |
| 2717 | assert(zcs->inBuffTarget <= zcs->inBuffSize); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2718 | zcs->inToCompress = zcs->inBuffPos; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2719 | if (cDst == op) { /* no need to flush */ |
| 2720 | op += cSize; |
| 2721 | if (zcs->frameEnded) { |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 2722 | DEBUGLOG(5, "Frame completed directly in outBuffer"); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2723 | someMoreWork = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 2724 | ZSTD_startNewCompression(zcs); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2725 | } |
| 2726 | break; |
| 2727 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2728 | zcs->outBuffContentSize = cSize; |
| 2729 | zcs->outBuffFlushedSize = 0; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2730 | zcs->streamStage = zcss_flush; /* pass-through to flush stage */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2731 | } |
Jos Collin | 7cd7a75 | 2017-05-11 13:17:20 +0530 | [diff] [blame] | 2732 | /* fall-through */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2733 | case zcss_flush: |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2734 | DEBUGLOG(5, "flush stage"); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2735 | { size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2736 | size_t const flushed = ZSTD_limitCopy(op, oend-op, |
| 2737 | zcs->outBuff + zcs->outBuffFlushedSize, toFlush); |
Yann Collet | 2cb9774 | 2017-07-04 12:39:26 -0700 | [diff] [blame] | 2738 | DEBUGLOG(5, "toFlush: %u into %u ==> flushed: %u", |
| 2739 | (U32)toFlush, (U32)(oend-op), (U32)flushed); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2740 | op += flushed; |
| 2741 | zcs->outBuffFlushedSize += flushed; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2742 | if (toFlush!=flushed) { |
Yann Collet | 2cb9774 | 2017-07-04 12:39:26 -0700 | [diff] [blame] | 2743 | /* flush not fully completed, presumably because dst is too small */ |
| 2744 | assert(op==oend); |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2745 | someMoreWork = 0; |
| 2746 | break; |
| 2747 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2748 | zcs->outBuffContentSize = zcs->outBuffFlushedSize = 0; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2749 | if (zcs->frameEnded) { |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 2750 | DEBUGLOG(5, "Frame completed on flush"); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2751 | someMoreWork = 0; |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 2752 | ZSTD_startNewCompression(zcs); |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 2753 | break; |
| 2754 | } |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 2755 | zcs->streamStage = zcss_load; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2756 | break; |
| 2757 | } |
| 2758 | |
Yann Collet | cd2892f | 2017-06-01 09:44:54 -0700 | [diff] [blame] | 2759 | default: /* impossible */ |
| 2760 | assert(0); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2761 | } |
| 2762 | } |
| 2763 | |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2764 | input->pos = ip - istart; |
| 2765 | output->pos = op - ostart; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2766 | if (zcs->frameEnded) return 0; |
| 2767 | { size_t hintInSize = zcs->inBuffTarget - zcs->inBuffPos; |
| 2768 | if (hintInSize==0) hintInSize = zcs->blockSize; |
| 2769 | return hintInSize; |
| 2770 | } |
| 2771 | } |
| 2772 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 2773 | 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] | 2774 | { |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2775 | /* check conditions */ |
| 2776 | if (output->pos > output->size) return ERROR(GENERIC); |
| 2777 | if (input->pos > input->size) return ERROR(GENERIC); |
| 2778 | |
| 2779 | return ZSTD_compressStream_generic(zcs, output, input, ZSTD_e_continue); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2780 | } |
| 2781 | |
Yann Collet | f35e2de | 2017-06-05 18:32:48 -0700 | [diff] [blame] | 2782 | |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 2783 | size_t ZSTD_compress_generic (ZSTD_CCtx* cctx, |
| 2784 | ZSTD_outBuffer* output, |
| 2785 | ZSTD_inBuffer* input, |
| 2786 | ZSTD_EndDirective endOp) |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2787 | { |
Yann Collet | 8afb151 | 2017-09-29 22:14:37 -0700 | [diff] [blame] | 2788 | DEBUGLOG(5, "ZSTD_compress_generic"); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2789 | /* check conditions */ |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 2790 | if (output->pos > output->size) return ERROR(GENERIC); |
| 2791 | if (input->pos > input->size) return ERROR(GENERIC); |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2792 | assert(cctx!=NULL); |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2793 | |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2794 | /* transparent initialization stage */ |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2795 | if (cctx->streamStage == zcss_init) { |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2796 | ZSTD_prefixDict const prefixDict = cctx->prefixDict; |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 2797 | ZSTD_CCtx_params params = cctx->requestedParams; |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 2798 | params.cParams = ZSTD_getCParamsFromCCtxParams( |
| 2799 | cctx->requestedParams, cctx->pledgedSrcSizePlusOne-1, 0 /*dictSize*/); |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2800 | memset(&cctx->prefixDict, 0, sizeof(cctx->prefixDict)); /* single usage */ |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2801 | assert(prefixDict.dict==NULL || cctx->cdict==NULL); /* only one can be set */ |
Yann Collet | db1668a | 2017-09-29 18:05:18 -0700 | [diff] [blame] | 2802 | DEBUGLOG(4, "ZSTD_compress_generic : transparent init stage"); |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 2803 | |
| 2804 | #ifdef ZSTD_MULTITHREAD |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 2805 | if (params.nbThreads > 1) { |
Stella Lau | 4e83572 | 2017-08-29 16:18:21 -0700 | [diff] [blame] | 2806 | if (cctx->mtctx == NULL || cctx->appliedParams.nbThreads != params.nbThreads) { |
| 2807 | ZSTDMT_freeCCtx(cctx->mtctx); |
| 2808 | cctx->mtctx = ZSTDMT_createCCtx_advanced(params.nbThreads, cctx->customMem); |
| 2809 | if (cctx->mtctx == NULL) return ERROR(memory_allocation); |
| 2810 | } |
Stella Lau | d775519 | 2017-08-18 17:37:58 -0700 | [diff] [blame] | 2811 | DEBUGLOG(4, "call ZSTDMT_initCStream_internal as nbThreads=%u", params.nbThreads); |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2812 | CHECK_F( ZSTDMT_initCStream_internal( |
Stella Lau | 90a31bf | 2017-08-30 14:36:54 -0700 | [diff] [blame] | 2813 | cctx->mtctx, |
| 2814 | prefixDict.dict, prefixDict.dictSize, ZSTD_dm_rawContent, |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2815 | cctx->cdict, params, cctx->pledgedSrcSizePlusOne-1) ); |
Yann Collet | 23aace9 | 2017-06-11 18:32:36 -0700 | [diff] [blame] | 2816 | cctx->streamStage = zcss_load; |
Stella Lau | 4e83572 | 2017-08-29 16:18:21 -0700 | [diff] [blame] | 2817 | cctx->appliedParams.nbThreads = params.nbThreads; |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 2818 | } else |
| 2819 | #endif |
| 2820 | { |
Stella Lau | c7a18b7 | 2017-08-29 15:10:42 -0700 | [diff] [blame] | 2821 | CHECK_F( ZSTD_resetCStream_internal( |
| 2822 | cctx, prefixDict.dict, prefixDict.dictSize, |
| 2823 | prefixDict.dictMode, cctx->cdict, params, |
| 2824 | cctx->pledgedSrcSizePlusOne-1) ); |
Yann Collet | f35e2de | 2017-06-05 18:32:48 -0700 | [diff] [blame] | 2825 | } } |
| 2826 | |
Yann Collet | a3d9926 | 2017-06-29 14:44:49 -0700 | [diff] [blame] | 2827 | /* compression stage */ |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 2828 | #ifdef ZSTD_MULTITHREAD |
Stella Lau | 4e83572 | 2017-08-29 16:18:21 -0700 | [diff] [blame] | 2829 | if (cctx->appliedParams.nbThreads > 1) { |
Yann Collet | 23aace9 | 2017-06-11 18:32:36 -0700 | [diff] [blame] | 2830 | size_t const flushMin = ZSTDMT_compressStream_generic(cctx->mtctx, output, input, endOp); |
Yann Collet | 8afb151 | 2017-09-29 22:14:37 -0700 | [diff] [blame] | 2831 | DEBUGLOG(5, "ZSTDMT_compressStream_generic result : %u", (U32)flushMin); |
Yann Collet | b26728c | 2017-06-16 14:00:46 -0700 | [diff] [blame] | 2832 | if ( ZSTD_isError(flushMin) |
| 2833 | || (endOp == ZSTD_e_end && flushMin == 0) ) { /* compression completed */ |
| 2834 | ZSTD_startNewCompression(cctx); |
Yann Collet | 9e73f2f | 2017-06-16 12:24:01 -0700 | [diff] [blame] | 2835 | } |
Yann Collet | 23aace9 | 2017-06-11 18:32:36 -0700 | [diff] [blame] | 2836 | return flushMin; |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2837 | } |
Yann Collet | f129fd3 | 2017-06-11 18:46:09 -0700 | [diff] [blame] | 2838 | #endif |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2839 | CHECK_F( ZSTD_compressStream_generic(cctx, output, input, endOp) ); |
Yann Collet | 559ee82 | 2017-06-16 11:58:21 -0700 | [diff] [blame] | 2840 | DEBUGLOG(5, "completed ZSTD_compress_generic"); |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 2841 | return cctx->outBuffContentSize - cctx->outBuffFlushedSize; /* remaining to flush */ |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2842 | } |
| 2843 | |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 2844 | size_t ZSTD_compress_generic_simpleArgs ( |
| 2845 | ZSTD_CCtx* cctx, |
| 2846 | void* dst, size_t dstCapacity, size_t* dstPos, |
| 2847 | const void* src, size_t srcSize, size_t* srcPos, |
| 2848 | ZSTD_EndDirective endOp) |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 2849 | { |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 2850 | ZSTD_outBuffer output = { dst, dstCapacity, *dstPos }; |
| 2851 | ZSTD_inBuffer input = { src, srcSize, *srcPos }; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2852 | /* ZSTD_compress_generic() will check validity of dstPos and srcPos */ |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 2853 | size_t const cErr = ZSTD_compress_generic(cctx, &output, &input, endOp); |
Yann Collet | deee6e5 | 2017-05-30 17:42:00 -0700 | [diff] [blame] | 2854 | *dstPos = output.pos; |
| 2855 | *srcPos = input.pos; |
Yann Collet | 58e8d79 | 2017-06-02 18:20:48 -0700 | [diff] [blame] | 2856 | return cErr; |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2857 | } |
| 2858 | |
| 2859 | |
Yann Collet | 104e5b0 | 2016-08-12 13:04:27 +0200 | [diff] [blame] | 2860 | /*====== Finalize ======*/ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2861 | |
| 2862 | /*! ZSTD_flushStream() : |
| 2863 | * @return : amount of data remaining to flush */ |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 2864 | size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2865 | { |
Yann Collet | 18ab5af | 2017-05-31 09:59:22 -0700 | [diff] [blame] | 2866 | ZSTD_inBuffer input = { NULL, 0, 0 }; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2867 | if (output->pos > output->size) return ERROR(GENERIC); |
| 2868 | CHECK_F( ZSTD_compressStream_generic(zcs, output, &input, ZSTD_e_flush) ); |
| 2869 | return zcs->outBuffContentSize - zcs->outBuffFlushedSize; /* remaining to flush */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | |
Yann Collet | 53e17fb | 2016-08-17 01:39:22 +0200 | [diff] [blame] | 2873 | size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output) |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2874 | { |
Yann Collet | 18ab5af | 2017-05-31 09:59:22 -0700 | [diff] [blame] | 2875 | ZSTD_inBuffer input = { NULL, 0, 0 }; |
Yann Collet | 01b1549 | 2017-05-30 18:10:26 -0700 | [diff] [blame] | 2876 | if (output->pos > output->size) return ERROR(GENERIC); |
| 2877 | CHECK_F( ZSTD_compressStream_generic(zcs, output, &input, ZSTD_e_end) ); |
Yann Collet | 2cb9774 | 2017-07-04 12:39:26 -0700 | [diff] [blame] | 2878 | { size_t const lastBlockSize = zcs->frameEnded ? 0 : ZSTD_BLOCKHEADERSIZE; |
| 2879 | size_t const checksumSize = zcs->frameEnded ? 0 : zcs->appliedParams.fParams.checksumFlag * 4; |
| 2880 | size_t const toFlush = zcs->outBuffContentSize - zcs->outBuffFlushedSize + lastBlockSize + checksumSize; |
| 2881 | DEBUGLOG(5, "ZSTD_endStream : remaining to flush : %u", |
| 2882 | (unsigned)toFlush); |
| 2883 | return toFlush; |
| 2884 | } |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 2885 | } |
| 2886 | |
| 2887 | |
Yann Collet | 70e8c38 | 2016-02-10 13:37:52 +0100 | [diff] [blame] | 2888 | /*-===== Pre-defined compression levels =====-*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2889 | |
inikep | 2c5eeea | 2016-04-15 13:44:46 +0200 | [diff] [blame] | 2890 | #define ZSTD_MAX_CLEVEL 22 |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 2891 | int ZSTD_maxCLevel(void) { return ZSTD_MAX_CLEVEL; } |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 2892 | |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2893 | static const ZSTD_compressionParameters ZSTD_defaultCParameters[4][ZSTD_MAX_CLEVEL+1] = { |
Yann Collet | adbe74a | 2017-06-28 13:22:37 -0700 | [diff] [blame] | 2894 | { /* "default" - guarantees a monotonically increasing memory budget */ |
Yann Collet | 793c649 | 2016-04-09 20:32:00 +0200 | [diff] [blame] | 2895 | /* W, C, H, S, L, TL, strat */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 2896 | { 18, 12, 12, 1, 7, 16, ZSTD_fast }, /* level 0 - never used */ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 2897 | { 19, 13, 14, 1, 7, 16, ZSTD_fast }, /* level 1 */ |
| 2898 | { 19, 15, 16, 1, 6, 16, ZSTD_fast }, /* level 2 */ |
Yann Collet | adbe74a | 2017-06-28 13:22:37 -0700 | [diff] [blame] | 2899 | { 20, 16, 17, 1, 5, 16, ZSTD_dfast }, /* level 3 */ |
| 2900 | { 20, 17, 18, 1, 5, 16, ZSTD_dfast }, /* level 4 */ |
| 2901 | { 20, 17, 18, 2, 5, 16, ZSTD_greedy }, /* level 5 */ |
| 2902 | { 21, 17, 19, 2, 5, 16, ZSTD_lazy }, /* level 6 */ |
| 2903 | { 21, 18, 19, 3, 5, 16, ZSTD_lazy }, /* level 7 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 2904 | { 21, 18, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 8 */ |
Yann Collet | adbe74a | 2017-06-28 13:22:37 -0700 | [diff] [blame] | 2905 | { 21, 19, 20, 3, 5, 16, ZSTD_lazy2 }, /* level 9 */ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 2906 | { 21, 19, 21, 4, 5, 16, ZSTD_lazy2 }, /* level 10 */ |
| 2907 | { 22, 20, 22, 4, 5, 16, ZSTD_lazy2 }, /* level 11 */ |
| 2908 | { 22, 20, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 12 */ |
| 2909 | { 22, 21, 22, 5, 5, 16, ZSTD_lazy2 }, /* level 13 */ |
| 2910 | { 22, 21, 22, 6, 5, 16, ZSTD_lazy2 }, /* level 14 */ |
Yann Collet | adbe74a | 2017-06-28 13:22:37 -0700 | [diff] [blame] | 2911 | { 22, 21, 22, 5, 5, 16, ZSTD_btlazy2 }, /* level 15 */ |
Yann Collet | 3c242e7 | 2016-07-13 14:56:24 +0200 | [diff] [blame] | 2912 | { 23, 22, 22, 5, 5, 16, ZSTD_btlazy2 }, /* level 16 */ |
Yann Collet | adbe74a | 2017-06-28 13:22:37 -0700 | [diff] [blame] | 2913 | { 23, 22, 22, 4, 5, 24, ZSTD_btopt }, /* level 17 */ |
Yann Collet | 29297c6 | 2017-04-27 17:44:01 -0700 | [diff] [blame] | 2914 | { 23, 22, 22, 5, 4, 32, ZSTD_btopt }, /* level 18 */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 2915 | { 23, 23, 22, 6, 3, 48, ZSTD_btopt }, /* level 19 */ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 2916 | { 25, 25, 23, 7, 3, 64, ZSTD_btultra }, /* level 20 */ |
Yann Collet | adbe74a | 2017-06-28 13:22:37 -0700 | [diff] [blame] | 2917 | { 26, 26, 24, 7, 3,256, ZSTD_btultra }, /* level 21 */ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 2918 | { 27, 27, 25, 9, 3,512, ZSTD_btultra }, /* level 22 */ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2919 | }, |
| 2920 | { /* for srcSize <= 256 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2921 | /* W, C, H, S, L, T, strat */ |
Yann Collet | e19a9ef | 2016-08-26 20:02:49 +0200 | [diff] [blame] | 2922 | { 0, 0, 0, 0, 0, 0, ZSTD_fast }, /* level 0 - not used */ |
Yann Collet | a2cdffe | 2016-08-24 19:42:15 +0200 | [diff] [blame] | 2923 | { 18, 13, 14, 1, 6, 8, ZSTD_fast }, /* level 1 */ |
Yann Collet | 24b68a5 | 2016-08-24 14:22:26 +0200 | [diff] [blame] | 2924 | { 18, 14, 13, 1, 5, 8, ZSTD_dfast }, /* level 2 */ |
| 2925 | { 18, 16, 15, 1, 5, 8, ZSTD_dfast }, /* level 3 */ |
| 2926 | { 18, 15, 17, 1, 5, 8, ZSTD_greedy }, /* level 4.*/ |
| 2927 | { 18, 16, 17, 4, 5, 8, ZSTD_greedy }, /* level 5.*/ |
| 2928 | { 18, 16, 17, 3, 5, 8, ZSTD_lazy }, /* level 6.*/ |
| 2929 | { 18, 17, 17, 4, 4, 8, ZSTD_lazy }, /* level 7 */ |
| 2930 | { 18, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ |
| 2931 | { 18, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ |
| 2932 | { 18, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ |
| 2933 | { 18, 18, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 11.*/ |
| 2934 | { 18, 18, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 12.*/ |
| 2935 | { 18, 19, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13 */ |
Yann Collet | 78267d1 | 2016-04-08 12:36:19 +0200 | [diff] [blame] | 2936 | { 18, 18, 18, 4, 4, 16, ZSTD_btopt }, /* level 14.*/ |
Yann Collet | 24b68a5 | 2016-08-24 14:22:26 +0200 | [diff] [blame] | 2937 | { 18, 18, 18, 4, 3, 16, ZSTD_btopt }, /* level 15.*/ |
| 2938 | { 18, 19, 18, 6, 3, 32, ZSTD_btopt }, /* level 16.*/ |
| 2939 | { 18, 19, 18, 8, 3, 64, ZSTD_btopt }, /* level 17.*/ |
Yann Collet | 78267d1 | 2016-04-08 12:36:19 +0200 | [diff] [blame] | 2940 | { 18, 19, 18, 9, 3,128, ZSTD_btopt }, /* level 18.*/ |
| 2941 | { 18, 19, 18, 10, 3,256, ZSTD_btopt }, /* level 19.*/ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 2942 | { 18, 19, 18, 11, 3,512, ZSTD_btultra }, /* level 20.*/ |
| 2943 | { 18, 19, 18, 12, 3,512, ZSTD_btultra }, /* level 21.*/ |
| 2944 | { 18, 19, 18, 13, 3,512, ZSTD_btultra }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2945 | }, |
| 2946 | { /* for srcSize <= 128 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2947 | /* W, C, H, S, L, T, strat */ |
Yann Collet | 5894ea8 | 2016-07-22 14:36:46 +0200 | [diff] [blame] | 2948 | { 17, 12, 12, 1, 7, 8, ZSTD_fast }, /* level 0 - not used */ |
| 2949 | { 17, 12, 13, 1, 6, 8, ZSTD_fast }, /* level 1 */ |
| 2950 | { 17, 13, 16, 1, 5, 8, ZSTD_fast }, /* level 2 */ |
| 2951 | { 17, 16, 16, 2, 5, 8, ZSTD_dfast }, /* level 3 */ |
| 2952 | { 17, 13, 15, 3, 4, 8, ZSTD_greedy }, /* level 4 */ |
| 2953 | { 17, 15, 17, 4, 4, 8, ZSTD_greedy }, /* level 5 */ |
| 2954 | { 17, 16, 17, 3, 4, 8, ZSTD_lazy }, /* level 6 */ |
| 2955 | { 17, 15, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 7 */ |
| 2956 | { 17, 17, 17, 4, 4, 8, ZSTD_lazy2 }, /* level 8 */ |
| 2957 | { 17, 17, 17, 5, 4, 8, ZSTD_lazy2 }, /* level 9 */ |
| 2958 | { 17, 17, 17, 6, 4, 8, ZSTD_lazy2 }, /* level 10 */ |
| 2959 | { 17, 17, 17, 7, 4, 8, ZSTD_lazy2 }, /* level 11 */ |
| 2960 | { 17, 17, 17, 8, 4, 8, ZSTD_lazy2 }, /* level 12 */ |
| 2961 | { 17, 18, 17, 6, 4, 8, ZSTD_btlazy2 }, /* level 13.*/ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2962 | { 17, 17, 17, 7, 3, 8, ZSTD_btopt }, /* level 14.*/ |
| 2963 | { 17, 17, 17, 7, 3, 16, ZSTD_btopt }, /* level 15.*/ |
| 2964 | { 17, 18, 17, 7, 3, 32, ZSTD_btopt }, /* level 16.*/ |
| 2965 | { 17, 18, 17, 7, 3, 64, ZSTD_btopt }, /* level 17.*/ |
| 2966 | { 17, 18, 17, 7, 3,256, ZSTD_btopt }, /* level 18.*/ |
| 2967 | { 17, 18, 17, 8, 3,256, ZSTD_btopt }, /* level 19.*/ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 2968 | { 17, 18, 17, 9, 3,256, ZSTD_btultra }, /* level 20.*/ |
| 2969 | { 17, 18, 17, 10, 3,256, ZSTD_btultra }, /* level 21.*/ |
| 2970 | { 17, 18, 17, 11, 3,512, ZSTD_btultra }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2971 | }, |
| 2972 | { /* for srcSize <= 16 KB */ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2973 | /* W, C, H, S, L, T, strat */ |
Yann Collet | 2b1a363 | 2016-07-13 15:16:00 +0200 | [diff] [blame] | 2974 | { 14, 12, 12, 1, 7, 6, ZSTD_fast }, /* level 0 - not used */ |
Yann Collet | e557fd5 | 2016-07-17 16:21:37 +0200 | [diff] [blame] | 2975 | { 14, 14, 14, 1, 6, 6, ZSTD_fast }, /* level 1 */ |
Yann Collet | 2b1a363 | 2016-07-13 15:16:00 +0200 | [diff] [blame] | 2976 | { 14, 14, 14, 1, 4, 6, ZSTD_fast }, /* level 2 */ |
| 2977 | { 14, 14, 14, 1, 4, 6, ZSTD_dfast }, /* level 3.*/ |
| 2978 | { 14, 14, 14, 4, 4, 6, ZSTD_greedy }, /* level 4.*/ |
| 2979 | { 14, 14, 14, 3, 4, 6, ZSTD_lazy }, /* level 5.*/ |
| 2980 | { 14, 14, 14, 4, 4, 6, ZSTD_lazy2 }, /* level 6 */ |
| 2981 | { 14, 14, 14, 5, 4, 6, ZSTD_lazy2 }, /* level 7 */ |
| 2982 | { 14, 14, 14, 6, 4, 6, ZSTD_lazy2 }, /* level 8.*/ |
| 2983 | { 14, 15, 14, 6, 4, 6, ZSTD_btlazy2 }, /* level 9.*/ |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 2984 | { 14, 15, 14, 3, 3, 6, ZSTD_btopt }, /* level 10.*/ |
| 2985 | { 14, 15, 14, 6, 3, 8, ZSTD_btopt }, /* level 11.*/ |
| 2986 | { 14, 15, 14, 6, 3, 16, ZSTD_btopt }, /* level 12.*/ |
| 2987 | { 14, 15, 14, 6, 3, 24, ZSTD_btopt }, /* level 13.*/ |
| 2988 | { 14, 15, 15, 6, 3, 48, ZSTD_btopt }, /* level 14.*/ |
| 2989 | { 14, 15, 15, 6, 3, 64, ZSTD_btopt }, /* level 15.*/ |
| 2990 | { 14, 15, 15, 6, 3, 96, ZSTD_btopt }, /* level 16.*/ |
| 2991 | { 14, 15, 15, 6, 3,128, ZSTD_btopt }, /* level 17.*/ |
| 2992 | { 14, 15, 15, 6, 3,256, ZSTD_btopt }, /* level 18.*/ |
| 2993 | { 14, 15, 15, 7, 3,256, ZSTD_btopt }, /* level 19.*/ |
Nick Terrell | 374f868 | 2017-05-10 17:48:42 -0700 | [diff] [blame] | 2994 | { 14, 15, 15, 8, 3,256, ZSTD_btultra }, /* level 20.*/ |
| 2995 | { 14, 15, 15, 9, 3,256, ZSTD_btultra }, /* level 21.*/ |
| 2996 | { 14, 15, 15, 10, 3,256, ZSTD_btultra }, /* level 22.*/ |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 2997 | }, |
| 2998 | }; |
| 2999 | |
Yann Collet | 0374662 | 2017-06-28 20:17:22 -0700 | [diff] [blame] | 3000 | #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=1) |
Yann Collet | 8135351 | 2017-06-28 15:34:56 -0700 | [diff] [blame] | 3001 | /* This function just controls |
| 3002 | * the monotonic memory budget increase of ZSTD_defaultCParameters[0]. |
Yann Collet | 2cb9774 | 2017-07-04 12:39:26 -0700 | [diff] [blame] | 3003 | * Run once, on first ZSTD_getCParams() usage, if ZSTD_DEBUG is enabled |
Yann Collet | 8135351 | 2017-06-28 15:34:56 -0700 | [diff] [blame] | 3004 | */ |
| 3005 | MEM_STATIC void ZSTD_check_compressionLevel_monotonicIncrease_memoryBudget(void) |
| 3006 | { |
Yann Collet | 0374662 | 2017-06-28 20:17:22 -0700 | [diff] [blame] | 3007 | int level; |
| 3008 | for (level=1; level<ZSTD_maxCLevel(); level++) { |
| 3009 | ZSTD_compressionParameters const c1 = ZSTD_defaultCParameters[0][level]; |
| 3010 | ZSTD_compressionParameters const c2 = ZSTD_defaultCParameters[0][level+1]; |
Yann Collet | 0374662 | 2017-06-28 20:17:22 -0700 | [diff] [blame] | 3011 | assert(c1.windowLog <= c2.windowLog); |
| 3012 | # define ZSTD_TABLECOST(h,c) ((1<<(h)) + (1<<(c))) |
| 3013 | assert(ZSTD_TABLECOST(c1.hashLog, c1.chainLog) <= ZSTD_TABLECOST(c2.hashLog, c2.chainLog)); |
Yann Collet | 8135351 | 2017-06-28 15:34:56 -0700 | [diff] [blame] | 3014 | } |
Yann Collet | 8135351 | 2017-06-28 15:34:56 -0700 | [diff] [blame] | 3015 | } |
Yann Collet | 0374662 | 2017-06-28 20:17:22 -0700 | [diff] [blame] | 3016 | #endif |
Yann Collet | 8135351 | 2017-06-28 15:34:56 -0700 | [diff] [blame] | 3017 | |
Yann Collet | 236d94f | 2016-05-18 12:06:33 +0200 | [diff] [blame] | 3018 | /*! ZSTD_getCParams() : |
| 3019 | * @return ZSTD_compressionParameters structure for a selected compression level, `srcSize` and `dictSize`. |
| 3020 | * Size values are optional, provide 0 if not known or unused */ |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3021 | 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] | 3022 | { |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3023 | size_t const addedSize = srcSizeHint ? 0 : 500; |
| 3024 | U64 const rSize = srcSizeHint+dictSize ? srcSizeHint+dictSize+addedSize : (U64)-1; |
Yann Collet | 3b71925 | 2016-03-30 19:48:05 +0200 | [diff] [blame] | 3025 | U32 const tableID = (rSize <= 256 KB) + (rSize <= 128 KB) + (rSize <= 16 KB); /* intentional underflow for srcSizeHint == 0 */ |
Yann Collet | 8135351 | 2017-06-28 15:34:56 -0700 | [diff] [blame] | 3026 | |
| 3027 | #if defined(ZSTD_DEBUG) && (ZSTD_DEBUG>=1) |
| 3028 | static int g_monotonicTest = 1; |
| 3029 | if (g_monotonicTest) { |
| 3030 | ZSTD_check_compressionLevel_monotonicIncrease_memoryBudget(); |
| 3031 | g_monotonicTest=0; |
| 3032 | } |
| 3033 | #endif |
| 3034 | |
Yann Collet | 6d4fef3 | 2017-05-17 18:36:15 -0700 | [diff] [blame] | 3035 | 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] | 3036 | if (compressionLevel > ZSTD_MAX_CLEVEL) compressionLevel = ZSTD_MAX_CLEVEL; |
Yann Collet | e51d51b | 2017-06-20 17:44:55 -0700 | [diff] [blame] | 3037 | { ZSTD_compressionParameters const cp = ZSTD_defaultCParameters[tableID][compressionLevel]; |
| 3038 | return ZSTD_adjustCParams_internal(cp, srcSizeHint, dictSize); } |
Stella Lau | 0e56a84 | 2017-08-28 19:25:17 -0700 | [diff] [blame] | 3039 | |
Yann Collet | fd416f1 | 2016-01-30 03:14:15 +0100 | [diff] [blame] | 3040 | } |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 3041 | |
| 3042 | /*! ZSTD_getParams() : |
Yann Collet | a43a854 | 2016-07-12 13:42:10 +0200 | [diff] [blame] | 3043 | * 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] | 3044 | * All fields of `ZSTD_frameParameters` are set to default (0) */ |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3045 | 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] | 3046 | ZSTD_parameters params; |
Yann Collet | 009d604 | 2017-05-19 10:17:59 -0700 | [diff] [blame] | 3047 | ZSTD_compressionParameters const cParams = ZSTD_getCParams(compressionLevel, srcSizeHint, dictSize); |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 3048 | memset(¶ms, 0, sizeof(params)); |
| 3049 | params.cParams = cParams; |
| 3050 | return params; |
| 3051 | } |