zstd-ruby 1.3.8.0 → 1.4.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +6 -5
  3. data/README.md +1 -1
  4. data/ext/zstdruby/libzstd/Makefile +133 -61
  5. data/ext/zstdruby/libzstd/README.md +51 -18
  6. data/ext/zstdruby/libzstd/common/bitstream.h +38 -39
  7. data/ext/zstdruby/libzstd/common/compiler.h +41 -6
  8. data/ext/zstdruby/libzstd/common/cpu.h +1 -1
  9. data/ext/zstdruby/libzstd/common/debug.c +11 -31
  10. data/ext/zstdruby/libzstd/common/debug.h +11 -31
  11. data/ext/zstdruby/libzstd/common/entropy_common.c +13 -33
  12. data/ext/zstdruby/libzstd/common/error_private.c +2 -1
  13. data/ext/zstdruby/libzstd/common/error_private.h +6 -2
  14. data/ext/zstdruby/libzstd/common/fse.h +13 -33
  15. data/ext/zstdruby/libzstd/common/fse_decompress.c +12 -35
  16. data/ext/zstdruby/libzstd/common/huf.h +15 -33
  17. data/ext/zstdruby/libzstd/common/mem.h +75 -2
  18. data/ext/zstdruby/libzstd/common/pool.c +8 -4
  19. data/ext/zstdruby/libzstd/common/pool.h +2 -2
  20. data/ext/zstdruby/libzstd/common/threading.c +52 -6
  21. data/ext/zstdruby/libzstd/common/threading.h +36 -4
  22. data/ext/zstdruby/libzstd/common/xxhash.c +25 -37
  23. data/ext/zstdruby/libzstd/common/xxhash.h +11 -31
  24. data/ext/zstdruby/libzstd/common/zstd_common.c +1 -1
  25. data/ext/zstdruby/libzstd/common/zstd_errors.h +2 -1
  26. data/ext/zstdruby/libzstd/common/zstd_internal.h +203 -22
  27. data/ext/zstdruby/libzstd/compress/fse_compress.c +19 -42
  28. data/ext/zstdruby/libzstd/compress/hist.c +15 -35
  29. data/ext/zstdruby/libzstd/compress/hist.h +12 -32
  30. data/ext/zstdruby/libzstd/compress/huf_compress.c +92 -92
  31. data/ext/zstdruby/libzstd/compress/zstd_compress.c +1460 -1472
  32. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +330 -65
  33. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.c +158 -0
  34. data/ext/zstdruby/libzstd/compress/zstd_compress_literals.h +29 -0
  35. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.c +419 -0
  36. data/ext/zstdruby/libzstd/compress/zstd_compress_sequences.h +54 -0
  37. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.c +845 -0
  38. data/ext/zstdruby/libzstd/compress/zstd_compress_superblock.h +32 -0
  39. data/ext/zstdruby/libzstd/compress/zstd_cwksp.h +525 -0
  40. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +65 -43
  41. data/ext/zstdruby/libzstd/compress/zstd_double_fast.h +2 -2
  42. data/ext/zstdruby/libzstd/compress/zstd_fast.c +264 -159
  43. data/ext/zstdruby/libzstd/compress/zstd_fast.h +2 -2
  44. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +74 -42
  45. data/ext/zstdruby/libzstd/compress/zstd_lazy.h +2 -2
  46. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +33 -11
  47. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +7 -2
  48. data/ext/zstdruby/libzstd/compress/zstd_opt.c +108 -125
  49. data/ext/zstdruby/libzstd/compress/zstd_opt.h +1 -1
  50. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +129 -93
  51. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +46 -28
  52. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +76 -60
  53. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +14 -10
  54. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +2 -2
  55. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +471 -258
  56. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +471 -346
  57. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +3 -3
  58. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +25 -4
  59. data/ext/zstdruby/libzstd/deprecated/zbuff.h +9 -8
  60. data/ext/zstdruby/libzstd/deprecated/zbuff_common.c +2 -2
  61. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +1 -1
  62. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +1 -1
  63. data/ext/zstdruby/libzstd/dictBuilder/cover.c +220 -65
  64. data/ext/zstdruby/libzstd/dictBuilder/cover.h +81 -7
  65. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +85 -56
  66. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +43 -19
  67. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +73 -35
  68. data/ext/zstdruby/libzstd/dll/example/Makefile +2 -1
  69. data/ext/zstdruby/libzstd/dll/example/build_package.bat +3 -2
  70. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +49 -15
  71. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +142 -117
  72. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +13 -8
  73. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +54 -25
  74. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +13 -8
  75. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +55 -25
  76. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +13 -8
  77. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +62 -29
  78. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +13 -8
  79. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +145 -109
  80. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +14 -9
  81. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +56 -26
  82. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +11 -6
  83. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +65 -28
  84. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +11 -6
  85. data/ext/zstdruby/libzstd/libzstd.pc.in +3 -2
  86. data/ext/zstdruby/libzstd/zstd.h +921 -597
  87. data/lib/zstd-ruby/version.rb +1 -1
  88. data/zstd-ruby.gemspec +2 -2
  89. metadata +19 -14
  90. data/ext/zstdruby/libzstd/dll/libzstd.def +0 -87
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -50,12 +50,17 @@ ZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity,
50
50
  const void* src, size_t compressedSize);
51
51
 
52
52
  /**
53
- ZSTDv07_getFrameSrcSize() : get the source length of a ZSTD frame
54
- compressedSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
55
- return : the number of bytes that would be read to decompress this frame
56
- or an errorCode if it fails (which can be tested using ZSTDv07_isError())
53
+ ZSTDv07_findFrameSizeInfoLegacy() : get the source length and decompressed bound of a ZSTD frame compliant with v0.7.x format
54
+ srcSize : The size of the 'src' buffer, at least as large as the frame pointed to by 'src'
55
+ cSize (output parameter) : the number of bytes that would be read to decompress this frame
56
+ or an error code if it fails (which can be tested using ZSTDv01_isError())
57
+ dBound (output parameter) : an upper-bound for the decompressed size of the data in the frame
58
+ or ZSTD_CONTENTSIZE_ERROR if an error occurs
59
+
60
+ note : assumes `cSize` and `dBound` are _not_ NULL.
57
61
  */
58
- size_t ZSTDv07_findFrameCompressedSize(const void* src, size_t compressedSize);
62
+ void ZSTDv07_findFrameSizeInfoLegacy(const void *src, size_t srcSize,
63
+ size_t* cSize, unsigned long long* dBound);
59
64
 
60
65
  /*====== Helper functions ======*/
61
66
  ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
@@ -3,8 +3,9 @@
3
3
  # BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
4
4
 
5
5
  prefix=@PREFIX@
6
- libdir=@LIBDIR@
7
- includedir=@INCLUDEDIR@
6
+ exec_prefix=${prefix}
7
+ includedir=${prefix}/@INCLUDEDIR@
8
+ libdir=${exec_prefix}/@LIBDIR@
8
9
 
9
10
  Name: zstd
10
11
  Description: fast lossless compression algorithm library
@@ -1,5 +1,5 @@
1
1
  /*
2
- * Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
2
+ * Copyright (c) 2016-2020, Yann Collet, Facebook, Inc.
3
3
  * All rights reserved.
4
4
  *
5
5
  * This source code is licensed under both the BSD-style license (found in the
@@ -15,6 +15,7 @@ extern "C" {
15
15
  #define ZSTD_H_235446
16
16
 
17
17
  /* ====== Dependency ======*/
18
+ #include <limits.h> /* INT_MAX */
18
19
  #include <stddef.h> /* size_t */
19
20
 
20
21
 
@@ -70,8 +71,8 @@ extern "C" {
70
71
 
71
72
  /*------ Version ------*/
72
73
  #define ZSTD_VERSION_MAJOR 1
73
- #define ZSTD_VERSION_MINOR 3
74
- #define ZSTD_VERSION_RELEASE 8
74
+ #define ZSTD_VERSION_MINOR 4
75
+ #define ZSTD_VERSION_RELEASE 5
75
76
 
76
77
  #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
77
78
  ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to check runtime library version */
@@ -82,13 +83,28 @@ ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to check runtime library v
82
83
  #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
83
84
  ZSTDLIB_API const char* ZSTD_versionString(void); /* requires v1.3.0+ */
84
85
 
85
- /***************************************
86
- * Default constant
87
- ***************************************/
86
+ /* *************************************
87
+ * Default constant
88
+ ***************************************/
88
89
  #ifndef ZSTD_CLEVEL_DEFAULT
89
90
  # define ZSTD_CLEVEL_DEFAULT 3
90
91
  #endif
91
92
 
93
+ /* *************************************
94
+ * Constants
95
+ ***************************************/
96
+
97
+ /* All magic numbers are supposed read/written to/from files/memory using little-endian convention */
98
+ #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */
99
+ #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */
100
+ #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */
101
+ #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0
102
+
103
+ #define ZSTD_BLOCKSIZELOG_MAX 17
104
+ #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX)
105
+
106
+
107
+
92
108
  /***************************************
93
109
  * Simple API
94
110
  ***************************************/
@@ -145,12 +161,21 @@ ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t
145
161
  * @return : decompressed size of `src` frame content _if known and not empty_, 0 otherwise. */
146
162
  ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
147
163
 
164
+ /*! ZSTD_findFrameCompressedSize() :
165
+ * `src` should point to the start of a ZSTD frame or skippable frame.
166
+ * `srcSize` must be >= first frame size
167
+ * @return : the compressed size of the first frame starting at `src`,
168
+ * suitable to pass as `srcSize` to `ZSTD_decompress` or similar,
169
+ * or an error code if input is invalid */
170
+ ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
171
+
148
172
 
149
173
  /*====== Helper functions ======*/
150
174
  #define ZSTD_COMPRESSBOUND(srcSize) ((srcSize) + ((srcSize)>>8) + (((srcSize) < (128<<10)) ? (((128<<10) - (srcSize)) >> 11) /* margin, from 64 to 0 */ : 0)) /* this formula ensures that bound(A) + bound(B) <= bound(A+B) as long as A and B >= 128 KB */
151
175
  ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case single-pass scenario */
152
176
  ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
153
177
  ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
178
+ ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
154
179
  ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
155
180
 
156
181
 
@@ -159,17 +184,26 @@ ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compres
159
184
  ***************************************/
160
185
  /*= Compression context
161
186
  * When compressing many times,
162
- * it is recommended to allocate a context just once, and re-use it for each successive compression operation.
187
+ * it is recommended to allocate a context just once,
188
+ * and re-use it for each successive compression operation.
163
189
  * This will make workload friendlier for system's memory.
164
- * Use one context per thread for parallel execution in multi-threaded environments. */
190
+ * Note : re-using context is just a speed / resource optimization.
191
+ * It doesn't change the compression ratio, which remains identical.
192
+ * Note 2 : In multi-threaded environments,
193
+ * use one different context per thread for parallel execution.
194
+ */
165
195
  typedef struct ZSTD_CCtx_s ZSTD_CCtx;
166
196
  ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
167
197
  ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
168
198
 
169
199
  /*! ZSTD_compressCCtx() :
170
- * Same as ZSTD_compress(), using an explicit ZSTD_CCtx
171
- * The function will compress at requested compression level,
172
- * ignoring any other parameter */
200
+ * Same as ZSTD_compress(), using an explicit ZSTD_CCtx.
201
+ * Important : in order to behave similarly to `ZSTD_compress()`,
202
+ * this function compresses at requested compression level,
203
+ * __ignoring any other parameter__ .
204
+ * If any advanced parameter was set using the advanced API,
205
+ * they will all be reset. Only `compressionLevel` remains.
206
+ */
173
207
  ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,
174
208
  void* dst, size_t dstCapacity,
175
209
  const void* src, size_t srcSize,
@@ -195,97 +229,349 @@ ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
195
229
  const void* src, size_t srcSize);
196
230
 
197
231
 
198
- /**************************
199
- * Simple dictionary API
200
- ***************************/
201
- /*! ZSTD_compress_usingDict() :
202
- * Compression at an explicit compression level using a Dictionary.
203
- * A dictionary can be any arbitrary data segment (also called a prefix),
204
- * or a buffer with specified information (see dictBuilder/zdict.h).
205
- * Note : This function loads the dictionary, resulting in significant startup delay.
206
- * It's intended for a dictionary used only once.
207
- * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */
208
- ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
209
- void* dst, size_t dstCapacity,
210
- const void* src, size_t srcSize,
211
- const void* dict,size_t dictSize,
212
- int compressionLevel);
213
-
214
- /*! ZSTD_decompress_usingDict() :
215
- * Decompression using a known Dictionary.
216
- * Dictionary must be identical to the one used during compression.
217
- * Note : This function loads the dictionary, resulting in significant startup delay.
218
- * It's intended for a dictionary used only once.
219
- * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
220
- ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
221
- void* dst, size_t dstCapacity,
222
- const void* src, size_t srcSize,
223
- const void* dict,size_t dictSize);
224
-
232
+ /***************************************
233
+ * Advanced compression API
234
+ ***************************************/
225
235
 
226
- /***********************************
227
- * Bulk processing dictionary API
228
- **********************************/
229
- typedef struct ZSTD_CDict_s ZSTD_CDict;
236
+ /* API design :
237
+ * Parameters are pushed one by one into an existing context,
238
+ * using ZSTD_CCtx_set*() functions.
239
+ * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame.
240
+ * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` !
241
+ * __They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()__ .
242
+ *
243
+ * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset().
244
+ *
245
+ * This API supercedes all other "advanced" API entry points in the experimental section.
246
+ * In the future, we expect to remove from experimental API entry points which are redundant with this API.
247
+ */
230
248
 
231
- /*! ZSTD_createCDict() :
232
- * When compressing multiple messages / blocks using the same dictionary, it's recommended to load it only once.
233
- * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup cost.
234
- * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
235
- * `dictBuffer` can be released after ZSTD_CDict creation, because its content is copied within CDict.
236
- * Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate `dictBuffer` content.
237
- * Note : A ZSTD_CDict can be created from an empty dictBuffer, but it is inefficient when used to compress small data. */
238
- ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
239
- int compressionLevel);
240
249
 
241
- /*! ZSTD_freeCDict() :
242
- * Function frees memory allocated by ZSTD_createCDict(). */
243
- ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
250
+ /* Compression strategies, listed from fastest to strongest */
251
+ typedef enum { ZSTD_fast=1,
252
+ ZSTD_dfast=2,
253
+ ZSTD_greedy=3,
254
+ ZSTD_lazy=4,
255
+ ZSTD_lazy2=5,
256
+ ZSTD_btlazy2=6,
257
+ ZSTD_btopt=7,
258
+ ZSTD_btultra=8,
259
+ ZSTD_btultra2=9
260
+ /* note : new strategies _might_ be added in the future.
261
+ Only the order (from fast to strong) is guaranteed */
262
+ } ZSTD_strategy;
244
263
 
245
- /*! ZSTD_compress_usingCDict() :
246
- * Compression using a digested Dictionary.
247
- * Recommended when same dictionary is used multiple times.
248
- * Note : compression level is _decided at dictionary creation time_,
249
- * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
250
- ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
251
- void* dst, size_t dstCapacity,
252
- const void* src, size_t srcSize,
253
- const ZSTD_CDict* cdict);
254
264
 
265
+ typedef enum {
255
266
 
256
- typedef struct ZSTD_DDict_s ZSTD_DDict;
267
+ /* compression parameters
268
+ * Note: When compressing with a ZSTD_CDict these parameters are superseded
269
+ * by the parameters used to construct the ZSTD_CDict.
270
+ * See ZSTD_CCtx_refCDict() for more info (superseded-by-cdict). */
271
+ ZSTD_c_compressionLevel=100, /* Set compression parameters according to pre-defined cLevel table.
272
+ * Note that exact compression parameters are dynamically determined,
273
+ * depending on both compression level and srcSize (when known).
274
+ * Default level is ZSTD_CLEVEL_DEFAULT==3.
275
+ * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
276
+ * Note 1 : it's possible to pass a negative compression level.
277
+ * Note 2 : setting a level does not automatically set all other compression parameters
278
+ * to default. Setting this will however eventually dynamically impact the compression
279
+ * parameters which have not been manually set. The manually set
280
+ * ones will 'stick'. */
281
+ /* Advanced compression parameters :
282
+ * It's possible to pin down compression parameters to some specific values.
283
+ * In which case, these values are no longer dynamically selected by the compressor */
284
+ ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2.
285
+ * This will set a memory budget for streaming decompression,
286
+ * with larger values requiring more memory
287
+ * and typically compressing more.
288
+ * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
289
+ * Special: value 0 means "use default windowLog".
290
+ * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT
291
+ * requires explicitly allowing such size at streaming decompression stage. */
292
+ ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2.
293
+ * Resulting memory usage is (1 << (hashLog+2)).
294
+ * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
295
+ * Larger tables improve compression ratio of strategies <= dFast,
296
+ * and improve speed of strategies > dFast.
297
+ * Special: value 0 means "use default hashLog". */
298
+ ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2.
299
+ * Resulting memory usage is (1 << (chainLog+2)).
300
+ * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.
301
+ * Larger tables result in better and slower compression.
302
+ * This parameter is useless for "fast" strategy.
303
+ * It's still useful when using "dfast" strategy,
304
+ * in which case it defines a secondary probe table.
305
+ * Special: value 0 means "use default chainLog". */
306
+ ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2.
307
+ * More attempts result in better and slower compression.
308
+ * This parameter is useless for "fast" and "dFast" strategies.
309
+ * Special: value 0 means "use default searchLog". */
310
+ ZSTD_c_minMatch=105, /* Minimum size of searched matches.
311
+ * Note that Zstandard can still find matches of smaller size,
312
+ * it just tweaks its search algorithm to look for this size and larger.
313
+ * Larger values increase compression and decompression speed, but decrease ratio.
314
+ * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX.
315
+ * Note that currently, for all strategies < btopt, effective minimum is 4.
316
+ * , for all strategies > fast, effective maximum is 6.
317
+ * Special: value 0 means "use default minMatchLength". */
318
+ ZSTD_c_targetLength=106, /* Impact of this field depends on strategy.
319
+ * For strategies btopt, btultra & btultra2:
320
+ * Length of Match considered "good enough" to stop search.
321
+ * Larger values make compression stronger, and slower.
322
+ * For strategy fast:
323
+ * Distance between match sampling.
324
+ * Larger values make compression faster, and weaker.
325
+ * Special: value 0 means "use default targetLength". */
326
+ ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition.
327
+ * The higher the value of selected strategy, the more complex it is,
328
+ * resulting in stronger and slower compression.
329
+ * Special: value 0 means "use default strategy". */
257
330
 
258
- /*! ZSTD_createDDict() :
259
- * Create a digested dictionary, ready to start decompression operation without startup delay.
260
- * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */
261
- ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
331
+ /* LDM mode parameters */
332
+ ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching.
333
+ * This parameter is designed to improve compression ratio
334
+ * for large inputs, by finding large matches at long distance.
335
+ * It increases memory usage and window size.
336
+ * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB
337
+ * except when expressly set to a different value. */
338
+ ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2.
339
+ * Larger values increase memory usage and compression ratio,
340
+ * but decrease compression speed.
341
+ * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX
342
+ * default: windowlog - 7.
343
+ * Special: value 0 means "automatically determine hashlog". */
344
+ ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher.
345
+ * Larger/too small values usually decrease compression ratio.
346
+ * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.
347
+ * Special: value 0 means "use default value" (default: 64). */
348
+ ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution.
349
+ * Larger values improve collision resolution but decrease compression speed.
350
+ * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX.
351
+ * Special: value 0 means "use default value" (default: 3). */
352
+ ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table.
353
+ * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).
354
+ * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.
355
+ * Larger values improve compression speed.
356
+ * Deviating far from default value will likely result in a compression ratio decrease.
357
+ * Special: value 0 means "automatically determine hashRateLog". */
262
358
 
263
- /*! ZSTD_freeDDict() :
264
- * Function frees memory allocated with ZSTD_createDDict() */
265
- ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
359
+ /* frame parameters */
360
+ ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)
361
+ * Content size must be known at the beginning of compression.
362
+ * This is automatically the case when using ZSTD_compress2(),
363
+ * For streaming scenarios, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */
364
+ ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */
365
+ ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */
266
366
 
267
- /*! ZSTD_decompress_usingDDict() :
268
- * Decompression using a digested Dictionary.
269
- * Recommended when same dictionary is used multiple times. */
270
- ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
271
- void* dst, size_t dstCapacity,
272
- const void* src, size_t srcSize,
273
- const ZSTD_DDict* ddict);
367
+ /* multi-threading parameters */
368
+ /* These parameters are only useful if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD).
369
+ * They return an error otherwise. */
370
+ ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel.
371
+ * When nbWorkers >= 1, triggers asynchronous mode when used with ZSTD_compressStream*() :
372
+ * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller,
373
+ * while compression work is performed in parallel, within worker threads.
374
+ * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end :
375
+ * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call).
376
+ * More workers improve speed, but also increase memory usage.
377
+ * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */
378
+ ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1.
379
+ * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads.
380
+ * 0 means default, which is dynamically determined based on compression parameters.
381
+ * Job size must be a minimum of overlap size, or 1 MB, whichever is largest.
382
+ * The minimum size is automatically and transparently enforced. */
383
+ ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size.
384
+ * The overlap size is an amount of data reloaded from previous job at the beginning of a new job.
385
+ * It helps preserve compression ratio, while each job is compressed in parallel.
386
+ * This value is enforced only when nbWorkers >= 1.
387
+ * Larger values increase compression ratio, but decrease speed.
388
+ * Possible values range from 0 to 9 :
389
+ * - 0 means "default" : value will be determined by the library, depending on strategy
390
+ * - 1 means "no overlap"
391
+ * - 9 means "full overlap", using a full window size.
392
+ * Each intermediate rank increases/decreases load size by a factor 2 :
393
+ * 9: full window; 8: w/2; 7: w/4; 6: w/8; 5:w/16; 4: w/32; 3:w/64; 2:w/128; 1:no overlap; 0:default
394
+ * default value varies between 6 and 9, depending on strategy */
274
395
 
396
+ /* note : additional experimental parameters are also available
397
+ * within the experimental section of the API.
398
+ * At the time of this writing, they include :
399
+ * ZSTD_c_rsyncable
400
+ * ZSTD_c_format
401
+ * ZSTD_c_forceMaxWindow
402
+ * ZSTD_c_forceAttachDict
403
+ * ZSTD_c_literalCompressionMode
404
+ * ZSTD_c_targetCBlockSize
405
+ * ZSTD_c_srcSizeHint
406
+ * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
407
+ * note : never ever use experimentalParam? names directly;
408
+ * also, the enums values themselves are unstable and can still change.
409
+ */
410
+ ZSTD_c_experimentalParam1=500,
411
+ ZSTD_c_experimentalParam2=10,
412
+ ZSTD_c_experimentalParam3=1000,
413
+ ZSTD_c_experimentalParam4=1001,
414
+ ZSTD_c_experimentalParam5=1002,
415
+ ZSTD_c_experimentalParam6=1003,
416
+ ZSTD_c_experimentalParam7=1004
417
+ } ZSTD_cParameter;
275
418
 
276
- /****************************
277
- * Streaming
278
- ****************************/
419
+ typedef struct {
420
+ size_t error;
421
+ int lowerBound;
422
+ int upperBound;
423
+ } ZSTD_bounds;
279
424
 
280
- typedef struct ZSTD_inBuffer_s {
281
- const void* src; /**< start of input buffer */
282
- size_t size; /**< size of input buffer */
283
- size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
284
- } ZSTD_inBuffer;
425
+ /*! ZSTD_cParam_getBounds() :
426
+ * All parameters must belong to an interval with lower and upper bounds,
427
+ * otherwise they will either trigger an error or be automatically clamped.
428
+ * @return : a structure, ZSTD_bounds, which contains
429
+ * - an error status field, which must be tested using ZSTD_isError()
430
+ * - lower and upper bounds, both inclusive
431
+ */
432
+ ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam);
285
433
 
286
- typedef struct ZSTD_outBuffer_s {
287
- void* dst; /**< start of output buffer */
288
- size_t size; /**< size of output buffer */
434
+ /*! ZSTD_CCtx_setParameter() :
435
+ * Set one compression parameter, selected by enum ZSTD_cParameter.
436
+ * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds().
437
+ * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
438
+ * Setting a parameter is generally only possible during frame initialization (before starting compression).
439
+ * Exception : when using multi-threading mode (nbWorkers >= 1),
440
+ * the following parameters can be updated _during_ compression (within same frame):
441
+ * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.
442
+ * new parameters will be active for next job only (after a flush()).
443
+ * @return : an error code (which can be tested using ZSTD_isError()).
444
+ */
445
+ ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value);
446
+
447
+ /*! ZSTD_CCtx_setPledgedSrcSize() :
448
+ * Total input data size to be compressed as a single frame.
449
+ * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag.
450
+ * This value will also be controlled at end of frame, and trigger an error if not respected.
451
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
452
+ * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame.
453
+ * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
454
+ * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame.
455
+ * Note 2 : pledgedSrcSize is only valid once, for the next frame.
456
+ * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN.
457
+ * Note 3 : Whenever all input data is provided and consumed in a single round,
458
+ * for example with ZSTD_compress2(),
459
+ * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end),
460
+ * this value is automatically overridden by srcSize instead.
461
+ */
462
+ ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
463
+
464
+ typedef enum {
465
+ ZSTD_reset_session_only = 1,
466
+ ZSTD_reset_parameters = 2,
467
+ ZSTD_reset_session_and_parameters = 3
468
+ } ZSTD_ResetDirective;
469
+
470
+ /*! ZSTD_CCtx_reset() :
471
+ * There are 2 different things that can be reset, independently or jointly :
472
+ * - The session : will stop compressing current frame, and make CCtx ready to start a new one.
473
+ * Useful after an error, or to interrupt any ongoing compression.
474
+ * Any internal data not yet flushed is cancelled.
475
+ * Compression parameters and dictionary remain unchanged.
476
+ * They will be used to compress next frame.
477
+ * Resetting session never fails.
478
+ * - The parameters : changes all parameters back to "default".
479
+ * This removes any reference to any dictionary too.
480
+ * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing)
481
+ * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())
482
+ * - Both : similar to resetting the session, followed by resetting parameters.
483
+ */
484
+ ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
485
+
486
+ /*! ZSTD_compress2() :
487
+ * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
488
+ * ZSTD_compress2() always starts a new frame.
489
+ * Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
490
+ * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
491
+ * - The function is always blocking, returns when compression is completed.
492
+ * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
493
+ * @return : compressed size written into `dst` (<= `dstCapacity),
494
+ * or an error code if it fails (which can be tested using ZSTD_isError()).
495
+ */
496
+ ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx,
497
+ void* dst, size_t dstCapacity,
498
+ const void* src, size_t srcSize);
499
+
500
+
501
+ /***************************************
502
+ * Advanced decompression API
503
+ ***************************************/
504
+
505
+ /* The advanced API pushes parameters one by one into an existing DCtx context.
506
+ * Parameters are sticky, and remain valid for all following frames
507
+ * using the same DCtx context.
508
+ * It's possible to reset parameters to default values using ZSTD_DCtx_reset().
509
+ * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream().
510
+ * Therefore, no new decompression function is necessary.
511
+ */
512
+
513
+ typedef enum {
514
+
515
+ ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which
516
+ * the streaming API will refuse to allocate memory buffer
517
+ * in order to protect the host from unreasonable memory requirements.
518
+ * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
519
+ * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT).
520
+ * Special: value 0 means "use default maximum windowLog". */
521
+
522
+ /* note : additional experimental parameters are also available
523
+ * within the experimental section of the API.
524
+ * At the time of this writing, they include :
525
+ * ZSTD_d_format
526
+ * ZSTD_d_stableOutBuffer
527
+ * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
528
+ * note : never ever use experimentalParam? names directly
529
+ */
530
+ ZSTD_d_experimentalParam1=1000,
531
+ ZSTD_d_experimentalParam2=1001
532
+
533
+ } ZSTD_dParameter;
534
+
535
+ /*! ZSTD_dParam_getBounds() :
536
+ * All parameters must belong to an interval with lower and upper bounds,
537
+ * otherwise they will either trigger an error or be automatically clamped.
538
+ * @return : a structure, ZSTD_bounds, which contains
539
+ * - an error status field, which must be tested using ZSTD_isError()
540
+ * - both lower and upper bounds, inclusive
541
+ */
542
+ ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);
543
+
544
+ /*! ZSTD_DCtx_setParameter() :
545
+ * Set one compression parameter, selected by enum ZSTD_dParameter.
546
+ * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds().
547
+ * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
548
+ * Setting a parameter is only possible during frame initialization (before starting decompression).
549
+ * @return : 0, or an error code (which can be tested using ZSTD_isError()).
550
+ */
551
+ ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value);
552
+
553
+ /*! ZSTD_DCtx_reset() :
554
+ * Return a DCtx to clean state.
555
+ * Session and parameters can be reset jointly or separately.
556
+ * Parameters can only be reset when no active frame is being decompressed.
557
+ * @return : 0, or an error code, which can be tested with ZSTD_isError()
558
+ */
559
+ ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
560
+
561
+
562
+ /****************************
563
+ * Streaming
564
+ ****************************/
565
+
566
+ typedef struct ZSTD_inBuffer_s {
567
+ const void* src; /**< start of input buffer */
568
+ size_t size; /**< size of input buffer */
569
+ size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
570
+ } ZSTD_inBuffer;
571
+
572
+ typedef struct ZSTD_outBuffer_s {
573
+ void* dst; /**< start of output buffer */
574
+ size_t size; /**< size of output buffer */
289
575
  size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
290
576
  } ZSTD_outBuffer;
291
577
 
@@ -306,34 +592,46 @@ typedef struct ZSTD_outBuffer_s {
306
592
  * Parameters are sticky : when starting a new compression on the same context,
307
593
  * it will re-use the same sticky parameters as previous compression session.
308
594
  * When in doubt, it's recommended to fully initialize the context before usage.
309
- * Use ZSTD_initCStream() to set the parameter to a selected compression level.
310
- * Use advanced API (ZSTD_CCtx_setParameter(), etc.) to set more specific parameters.
595
+ * Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(),
596
+ * ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to
597
+ * set more specific parameters, the pledged source size, or load a dictionary.
311
598
  *
312
- * Use ZSTD_compressStream() as many times as necessary to consume input stream.
313
- * The function will automatically update both `pos` fields within `input` and `output`.
314
- * Note that the function may not consume the entire input,
315
- * for example, because the output buffer is already full,
316
- * in which case `input.pos < input.size`.
599
+ * Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to
600
+ * consume input stream. The function will automatically update both `pos`
601
+ * fields within `input` and `output`.
602
+ * Note that the function may not consume the entire input, for example, because
603
+ * the output buffer is already full, in which case `input.pos < input.size`.
317
604
  * The caller must check if input has been entirely consumed.
318
605
  * If not, the caller must make some room to receive more compressed data,
319
606
  * and then present again remaining input data.
320
- * @return : a size hint, preferred nb of bytes to use as input for next function call
607
+ * note: ZSTD_e_continue is guaranteed to make some forward progress when called,
608
+ * but doesn't guarantee maximal forward progress. This is especially relevant
609
+ * when compressing with multiple threads. The call won't block if it can
610
+ * consume some input, but if it can't it will wait for some, but not all,
611
+ * output to be flushed.
612
+ * @return : provides a minimum amount of data remaining to be flushed from internal buffers
321
613
  * or an error code, which can be tested using ZSTD_isError().
322
- * Note 1 : it's just a hint, to help latency a little, any value will work fine.
323
- * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
324
614
  *
325
615
  * At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
326
- * using ZSTD_flushStream(). `output->pos` will be updated.
327
- * Note that, if `output->size` is too small, a single invocation of ZSTD_flushStream() might not be enough (return code > 0).
328
- * In which case, make some room to receive more compressed data, and call again ZSTD_flushStream().
616
+ * using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated.
617
+ * Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0).
618
+ * In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush.
619
+ * You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the
620
+ * operation.
621
+ * note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will
622
+ * block until the flush is complete or the output buffer is full.
329
623
  * @return : 0 if internal buffers are entirely flushed,
330
624
  * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
331
625
  * or an error code, which can be tested using ZSTD_isError().
332
626
  *
333
- * ZSTD_endStream() instructs to finish a frame.
627
+ * Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame.
334
628
  * It will perform a flush and write frame epilogue.
335
629
  * The epilogue is required for decoders to consider a frame completed.
336
- * flush() operation is the same, and follows same rules as ZSTD_flushStream().
630
+ * flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush.
631
+ * You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to
632
+ * start a new frame.
633
+ * note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will
634
+ * block until the flush is complete or the output buffer is full.
337
635
  * @return : 0 if frame fully completed and fully flushed,
338
636
  * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size),
339
637
  * or an error code, which can be tested using ZSTD_isError().
@@ -347,15 +645,91 @@ ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
347
645
  ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
348
646
 
349
647
  /*===== Streaming compression functions =====*/
648
+ typedef enum {
649
+ ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
650
+ ZSTD_e_flush=1, /* flush any data provided so far,
651
+ * it creates (at least) one new block, that can be decoded immediately on reception;
652
+ * frame will continue: any future data can still reference previously compressed data, improving compression.
653
+ * note : multithreaded compression will block to flush as much output as possible. */
654
+ ZSTD_e_end=2 /* flush any remaining data _and_ close current frame.
655
+ * note that frame is only closed after compressed data is fully flushed (return value == 0).
656
+ * After that point, any additional data starts a new frame.
657
+ * note : each frame is independent (does not reference any content from previous frame).
658
+ : note : multithreaded compression will block to flush as much output as possible. */
659
+ } ZSTD_EndDirective;
660
+
661
+ /*! ZSTD_compressStream2() :
662
+ * Behaves about the same as ZSTD_compressStream, with additional control on end directive.
663
+ * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
664
+ * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode)
665
+ * - output->pos must be <= dstCapacity, input->pos must be <= srcSize
666
+ * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
667
+ * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller.
668
+ * - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available,
669
+ * and then immediately returns, just indicating that there is some data remaining to be flushed.
670
+ * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
671
+ * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking.
672
+ * - @return provides a minimum amount of data remaining to be flushed from internal buffers
673
+ * or an error code, which can be tested using ZSTD_isError().
674
+ * if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
675
+ * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
676
+ * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
677
+ * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
678
+ * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
679
+ * Before starting a new compression job, or changing compression parameters,
680
+ * it is required to fully flush internal buffers.
681
+ */
682
+ ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
683
+ ZSTD_outBuffer* output,
684
+ ZSTD_inBuffer* input,
685
+ ZSTD_EndDirective endOp);
686
+
687
+
688
+ /* These buffer sizes are softly recommended.
689
+ * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output.
690
+ * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(),
691
+ * reducing the amount of memory shuffling and buffering, resulting in minor performance savings.
692
+ *
693
+ * However, note that these recommendations are from the perspective of a C caller program.
694
+ * If the streaming interface is invoked from some other language,
695
+ * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo,
696
+ * a major performance rule is to reduce crossing such interface to an absolute minimum.
697
+ * It's not rare that performance ends being spent more into the interface, rather than compression itself.
698
+ * In which cases, prefer using large buffers, as large as practical,
699
+ * for both input and output, to reduce the nb of roundtrips.
700
+ */
701
+ ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
702
+ ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */
703
+
704
+
705
+ /* *****************************************************************************
706
+ * This following is a legacy streaming API.
707
+ * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2().
708
+ * It is redundant, but remains fully supported.
709
+ * Advanced parameters and dictionary compression can only be used through the
710
+ * new API.
711
+ ******************************************************************************/
712
+
713
+ /*!
714
+ * Equivalent to:
715
+ *
716
+ * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
717
+ * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
718
+ * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
719
+ */
350
720
  ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
721
+ /*!
722
+ * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue).
723
+ * NOTE: The return value is different. ZSTD_compressStream() returns a hint for
724
+ * the next read size (if non-zero and not an error). ZSTD_compressStream2()
725
+ * returns the minimum nb of bytes left to flush (if non-zero and not an error).
726
+ */
351
727
  ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
728
+ /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */
352
729
  ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
730
+ /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */
353
731
  ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
354
732
 
355
- ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
356
- ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */
357
-
358
-
359
733
 
360
734
  /*-***************************************************************************
361
735
  * Streaming decompression - HowTo
@@ -391,295 +765,141 @@ ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
391
765
  ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
392
766
 
393
767
  /*===== Streaming decompression functions =====*/
768
+
769
+ /* This function is redundant with the advanced API and equivalent to:
770
+ *
771
+ * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
772
+ * ZSTD_DCtx_refDDict(zds, NULL);
773
+ */
394
774
  ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
775
+
395
776
  ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
396
777
 
397
778
  ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
398
779
  ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
399
780
 
400
- #endif /* ZSTD_H_235446 */
401
-
402
-
403
-
404
-
405
- /****************************************************************************************
406
- * ADVANCED AND EXPERIMENTAL FUNCTIONS
407
- ****************************************************************************************
408
- * The definitions in the following section are considered experimental.
409
- * They are provided for advanced scenarios.
410
- * They should never be used with a dynamic library, as prototypes may change in the future.
411
- * Use them only in association with static linking.
412
- * ***************************************************************************************/
413
-
414
- #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
415
- #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
416
-
417
-
418
- /****************************************************************************************
419
- * Candidate API for promotion to stable status
420
- ****************************************************************************************
421
- * The following symbols and constants form the "staging area" :
422
- * they are considered to join "stable API" by v1.4.0.
423
- * The proposal is written so that it can be made stable "as is",
424
- * though it's still possible to suggest improvements.
425
- * Staging is in fact last chance for changes,
426
- * the API is locked once reaching "stable" status.
427
- * ***************************************************************************************/
428
-
429
-
430
- /* === Constants === */
431
-
432
- /* all magic numbers are supposed read/written to/from files/memory using little-endian convention */
433
- #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */
434
- #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */
435
- #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */
436
- #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0
437
-
438
- #define ZSTD_BLOCKSIZELOG_MAX 17
439
- #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX)
440
-
441
-
442
- /* === query limits === */
443
-
444
- ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
445
-
446
-
447
- /* === frame size === */
448
-
449
- /*! ZSTD_findFrameCompressedSize() :
450
- * `src` should point to the start of a ZSTD frame or skippable frame.
451
- * `srcSize` must be >= first frame size
452
- * @return : the compressed size of the first frame starting at `src`,
453
- * suitable to pass as `srcSize` to `ZSTD_decompress` or similar,
454
- * or an error code if input is invalid */
455
- ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
456
-
457
-
458
- /* === Memory management === */
459
781
 
460
- /*! ZSTD_sizeof_*() :
461
- * These functions give the _current_ memory usage of selected object.
462
- * Note that object memory usage can evolve (increase or decrease) over time. */
463
- ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
464
- ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
465
- ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
466
- ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
467
- ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
468
- ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
782
+ /**************************
783
+ * Simple dictionary API
784
+ ***************************/
785
+ /*! ZSTD_compress_usingDict() :
786
+ * Compression at an explicit compression level using a Dictionary.
787
+ * A dictionary can be any arbitrary data segment (also called a prefix),
788
+ * or a buffer with specified information (see dictBuilder/zdict.h).
789
+ * Note : This function loads the dictionary, resulting in significant startup delay.
790
+ * It's intended for a dictionary used only once.
791
+ * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */
792
+ ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
793
+ void* dst, size_t dstCapacity,
794
+ const void* src, size_t srcSize,
795
+ const void* dict,size_t dictSize,
796
+ int compressionLevel);
469
797
 
798
+ /*! ZSTD_decompress_usingDict() :
799
+ * Decompression using a known Dictionary.
800
+ * Dictionary must be identical to the one used during compression.
801
+ * Note : This function loads the dictionary, resulting in significant startup delay.
802
+ * It's intended for a dictionary used only once.
803
+ * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
804
+ ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
805
+ void* dst, size_t dstCapacity,
806
+ const void* src, size_t srcSize,
807
+ const void* dict,size_t dictSize);
470
808
 
471
- /***************************************
472
- * Advanced compression API
473
- ***************************************/
474
809
 
475
- /* API design :
476
- * Parameters are pushed one by one into an existing context,
477
- * using ZSTD_CCtx_set*() functions.
478
- * Pushed parameters are sticky : they are valid for next compressed frame, and any subsequent frame.
479
- * "sticky" parameters are applicable to `ZSTD_compress2()` and `ZSTD_compressStream*()` !
480
- * They do not apply to "simple" one-shot variants such as ZSTD_compressCCtx()
481
- *
482
- * It's possible to reset all parameters to "default" using ZSTD_CCtx_reset().
483
- *
484
- * This API supercedes all other "advanced" API entry points in the experimental section.
485
- * In the future, we expect to remove from experimental API entry points which are redundant with this API.
486
- */
810
+ /***********************************
811
+ * Bulk processing dictionary API
812
+ **********************************/
813
+ typedef struct ZSTD_CDict_s ZSTD_CDict;
487
814
 
815
+ /*! ZSTD_createCDict() :
816
+ * When compressing multiple messages or blocks using the same dictionary,
817
+ * it's recommended to digest the dictionary only once, since it's a costly operation.
818
+ * ZSTD_createCDict() will create a state from digesting a dictionary.
819
+ * The resulting state can be used for future compression operations with very limited startup cost.
820
+ * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
821
+ * @dictBuffer can be released after ZSTD_CDict creation, because its content is copied within CDict.
822
+ * Note 1 : Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate @dictBuffer content.
823
+ * Note 2 : A ZSTD_CDict can be created from an empty @dictBuffer,
824
+ * in which case the only thing that it transports is the @compressionLevel.
825
+ * This can be useful in a pipeline featuring ZSTD_compress_usingCDict() exclusively,
826
+ * expecting a ZSTD_CDict parameter with any data, including those without a known dictionary. */
827
+ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
828
+ int compressionLevel);
488
829
 
489
- /* Compression strategies, listed from fastest to strongest */
490
- typedef enum { ZSTD_fast=1,
491
- ZSTD_dfast=2,
492
- ZSTD_greedy=3,
493
- ZSTD_lazy=4,
494
- ZSTD_lazy2=5,
495
- ZSTD_btlazy2=6,
496
- ZSTD_btopt=7,
497
- ZSTD_btultra=8,
498
- ZSTD_btultra2=9
499
- /* note : new strategies _might_ be added in the future.
500
- Only the order (from fast to strong) is guaranteed */
501
- } ZSTD_strategy;
830
+ /*! ZSTD_freeCDict() :
831
+ * Function frees memory allocated by ZSTD_createCDict(). */
832
+ ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
502
833
 
834
+ /*! ZSTD_compress_usingCDict() :
835
+ * Compression using a digested Dictionary.
836
+ * Recommended when same dictionary is used multiple times.
837
+ * Note : compression level is _decided at dictionary creation time_,
838
+ * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */
839
+ ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
840
+ void* dst, size_t dstCapacity,
841
+ const void* src, size_t srcSize,
842
+ const ZSTD_CDict* cdict);
503
843
 
504
- typedef enum {
505
844
 
506
- /* compression parameters */
507
- ZSTD_c_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
508
- * Default level is ZSTD_CLEVEL_DEFAULT==3.
509
- * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
510
- * Note 1 : it's possible to pass a negative compression level.
511
- * Note 2 : setting a level sets all default values of other compression parameters */
512
- ZSTD_c_windowLog=101, /* Maximum allowed back-reference distance, expressed as power of 2.
513
- * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
514
- * Special: value 0 means "use default windowLog".
515
- * Note: Using a windowLog greater than ZSTD_WINDOWLOG_LIMIT_DEFAULT
516
- * requires explicitly allowing such window size at decompression stage if using streaming. */
517
- ZSTD_c_hashLog=102, /* Size of the initial probe table, as a power of 2.
518
- * Resulting memory usage is (1 << (hashLog+2)).
519
- * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
520
- * Larger tables improve compression ratio of strategies <= dFast,
521
- * and improve speed of strategies > dFast.
522
- * Special: value 0 means "use default hashLog". */
523
- ZSTD_c_chainLog=103, /* Size of the multi-probe search table, as a power of 2.
524
- * Resulting memory usage is (1 << (chainLog+2)).
525
- * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.
526
- * Larger tables result in better and slower compression.
527
- * This parameter is useless when using "fast" strategy.
528
- * It's still useful when using "dfast" strategy,
529
- * in which case it defines a secondary probe table.
530
- * Special: value 0 means "use default chainLog". */
531
- ZSTD_c_searchLog=104, /* Number of search attempts, as a power of 2.
532
- * More attempts result in better and slower compression.
533
- * This parameter is useless when using "fast" and "dFast" strategies.
534
- * Special: value 0 means "use default searchLog". */
535
- ZSTD_c_minMatch=105, /* Minimum size of searched matches.
536
- * Note that Zstandard can still find matches of smaller size,
537
- * it just tweaks its search algorithm to look for this size and larger.
538
- * Larger values increase compression and decompression speed, but decrease ratio.
539
- * Must be clamped between ZSTD_MINMATCH_MIN and ZSTD_MINMATCH_MAX.
540
- * Note that currently, for all strategies < btopt, effective minimum is 4.
541
- * , for all strategies > fast, effective maximum is 6.
542
- * Special: value 0 means "use default minMatchLength". */
543
- ZSTD_c_targetLength=106, /* Impact of this field depends on strategy.
544
- * For strategies btopt, btultra & btultra2:
545
- * Length of Match considered "good enough" to stop search.
546
- * Larger values make compression stronger, and slower.
547
- * For strategy fast:
548
- * Distance between match sampling.
549
- * Larger values make compression faster, and weaker.
550
- * Special: value 0 means "use default targetLength". */
551
- ZSTD_c_strategy=107, /* See ZSTD_strategy enum definition.
552
- * The higher the value of selected strategy, the more complex it is,
553
- * resulting in stronger and slower compression.
554
- * Special: value 0 means "use default strategy". */
845
+ typedef struct ZSTD_DDict_s ZSTD_DDict;
555
846
 
556
- /* LDM mode parameters */
557
- ZSTD_c_enableLongDistanceMatching=160, /* Enable long distance matching.
558
- * This parameter is designed to improve compression ratio
559
- * for large inputs, by finding large matches at long distance.
560
- * It increases memory usage and window size.
561
- * Note: enabling this parameter increases default ZSTD_c_windowLog to 128 MB
562
- * except when expressly set to a different value. */
563
- ZSTD_c_ldmHashLog=161, /* Size of the table for long distance matching, as a power of 2.
564
- * Larger values increase memory usage and compression ratio,
565
- * but decrease compression speed.
566
- * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX
567
- * default: windowlog - 7.
568
- * Special: value 0 means "automatically determine hashlog". */
569
- ZSTD_c_ldmMinMatch=162, /* Minimum match size for long distance matcher.
570
- * Larger/too small values usually decrease compression ratio.
571
- * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.
572
- * Special: value 0 means "use default value" (default: 64). */
573
- ZSTD_c_ldmBucketSizeLog=163, /* Log size of each bucket in the LDM hash table for collision resolution.
574
- * Larger values improve collision resolution but decrease compression speed.
575
- * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX.
576
- * Special: value 0 means "use default value" (default: 3). */
577
- ZSTD_c_ldmHashRateLog=164, /* Frequency of inserting/looking up entries into the LDM hash table.
578
- * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).
579
- * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.
580
- * Larger values improve compression speed.
581
- * Deviating far from default value will likely result in a compression ratio decrease.
582
- * Special: value 0 means "automatically determine hashRateLog". */
847
+ /*! ZSTD_createDDict() :
848
+ * Create a digested dictionary, ready to start decompression operation without startup delay.
849
+ * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */
850
+ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
583
851
 
584
- /* frame parameters */
585
- ZSTD_c_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)
586
- * Content size must be known at the beginning of compression.
587
- * This is automatically the case when using ZSTD_compress2(),
588
- * For streaming variants, content size must be provided with ZSTD_CCtx_setPledgedSrcSize() */
589
- ZSTD_c_checksumFlag=201, /* A 32-bits checksum of content is written at end of frame (default:0) */
590
- ZSTD_c_dictIDFlag=202, /* When applicable, dictionary's ID is written into frame header (default:1) */
852
+ /*! ZSTD_freeDDict() :
853
+ * Function frees memory allocated with ZSTD_createDDict() */
854
+ ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
591
855
 
592
- /* multi-threading parameters */
593
- /* These parameters are only useful if multi-threading is enabled (compiled with build macro ZSTD_MULTITHREAD).
594
- * They return an error otherwise. */
595
- ZSTD_c_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel.
596
- * When nbWorkers >= 1, triggers asynchronous mode when used with ZSTD_compressStream*() :
597
- * ZSTD_compressStream*() consumes input and flush output if possible, but immediately gives back control to caller,
598
- * while compression work is performed in parallel, within worker threads.
599
- * (note : a strong exception to this rule is when first invocation of ZSTD_compressStream2() sets ZSTD_e_end :
600
- * in which case, ZSTD_compressStream2() delegates to ZSTD_compress2(), which is always a blocking call).
601
- * More workers improve speed, but also increase memory usage.
602
- * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */
603
- ZSTD_c_jobSize=401, /* Size of a compression job. This value is enforced only when nbWorkers >= 1.
604
- * Each compression job is completed in parallel, so this value can indirectly impact the nb of active threads.
605
- * 0 means default, which is dynamically determined based on compression parameters.
606
- * Job size must be a minimum of overlap size, or 1 MB, whichever is largest.
607
- * The minimum size is automatically and transparently enforced */
608
- ZSTD_c_overlapLog=402, /* Control the overlap size, as a fraction of window size.
609
- * The overlap size is an amount of data reloaded from previous job at the beginning of a new job.
610
- * It helps preserve compression ratio, while each job is compressed in parallel.
611
- * This value is enforced only when nbWorkers >= 1.
612
- * Larger values increase compression ratio, but decrease speed.
613
- * Possible values range from 0 to 9 :
614
- * - 0 means "default" : value will be determined by the library, depending on strategy
615
- * - 1 means "no overlap"
616
- * - 9 means "full overlap", using a full window size.
617
- * Each intermediate rank increases/decreases load size by a factor 2 :
618
- * 9: full window; 8: w/2; 7: w/4; 6: w/8; 5:w/16; 4: w/32; 3:w/64; 2:w/128; 1:no overlap; 0:default
619
- * default value varies between 6 and 9, depending on strategy */
856
+ /*! ZSTD_decompress_usingDDict() :
857
+ * Decompression using a digested Dictionary.
858
+ * Recommended when same dictionary is used multiple times. */
859
+ ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
860
+ void* dst, size_t dstCapacity,
861
+ const void* src, size_t srcSize,
862
+ const ZSTD_DDict* ddict);
620
863
 
621
- /* note : additional experimental parameters are also available
622
- * within the experimental section of the API.
623
- * At the time of this writing, they include :
624
- * ZSTD_c_rsyncable
625
- * ZSTD_c_format
626
- * ZSTD_c_forceMaxWindow
627
- * ZSTD_c_forceAttachDict
628
- * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
629
- * note : never ever use experimentalParam? names directly;
630
- * also, the enums values themselves are unstable and can still change.
631
- */
632
- ZSTD_c_experimentalParam1=500,
633
- ZSTD_c_experimentalParam2=10,
634
- ZSTD_c_experimentalParam3=1000,
635
- ZSTD_c_experimentalParam4=1001
636
- } ZSTD_cParameter;
637
864
 
865
+ /********************************
866
+ * Dictionary helper functions
867
+ *******************************/
638
868
 
639
- typedef struct {
640
- size_t error;
641
- int lowerBound;
642
- int upperBound;
643
- } ZSTD_bounds;
869
+ /*! ZSTD_getDictID_fromDict() :
870
+ * Provides the dictID stored within dictionary.
871
+ * if @return == 0, the dictionary is not conformant with Zstandard specification.
872
+ * It can still be loaded, but as a content-only dictionary. */
873
+ ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
644
874
 
645
- /*! ZSTD_cParam_getBounds() :
646
- * All parameters must belong to an interval with lower and upper bounds,
647
- * otherwise they will either trigger an error or be automatically clamped.
648
- * @return : a structure, ZSTD_bounds, which contains
649
- * - an error status field, which must be tested using ZSTD_isError()
650
- * - lower and upper bounds, both inclusive
651
- */
652
- ZSTDLIB_API ZSTD_bounds ZSTD_cParam_getBounds(ZSTD_cParameter cParam);
875
+ /*! ZSTD_getDictID_fromDDict() :
876
+ * Provides the dictID of the dictionary loaded into `ddict`.
877
+ * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
878
+ * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
879
+ ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
653
880
 
654
- /*! ZSTD_CCtx_setParameter() :
655
- * Set one compression parameter, selected by enum ZSTD_cParameter.
656
- * All parameters have valid bounds. Bounds can be queried using ZSTD_cParam_getBounds().
657
- * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
658
- * Setting a parameter is generally only possible during frame initialization (before starting compression).
659
- * Exception : when using multi-threading mode (nbWorkers >= 1),
660
- * the following parameters can be updated _during_ compression (within same frame):
661
- * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.
662
- * new parameters will be active for next job only (after a flush()).
663
- * @return : an error code (which can be tested using ZSTD_isError()).
664
- */
665
- ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int value);
881
+ /*! ZSTD_getDictID_fromFrame() :
882
+ * Provides the dictID required to decompressed the frame stored within `src`.
883
+ * If @return == 0, the dictID could not be decoded.
884
+ * This could for one of the following reasons :
885
+ * - The frame does not require a dictionary to be decoded (most common case).
886
+ * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
887
+ * Note : this use case also happens when using a non-conformant dictionary.
888
+ * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
889
+ * - This is not a Zstandard frame.
890
+ * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
891
+ ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
892
+
893
+
894
+ /*******************************************************************************
895
+ * Advanced dictionary and prefix API
896
+ *
897
+ * This API allows dictionaries to be used with ZSTD_compress2(),
898
+ * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and
899
+ * only reset with the context is reset with ZSTD_reset_parameters or
900
+ * ZSTD_reset_session_and_parameters. Prefixes are single-use.
901
+ ******************************************************************************/
666
902
 
667
- /*! ZSTD_CCtx_setPledgedSrcSize() :
668
- * Total input data size to be compressed as a single frame.
669
- * Value will be written in frame header, unless if explicitly forbidden using ZSTD_c_contentSizeFlag.
670
- * This value will also be controlled at end of frame, and trigger an error if not respected.
671
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
672
- * Note 1 : pledgedSrcSize==0 actually means zero, aka an empty frame.
673
- * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
674
- * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new frame.
675
- * Note 2 : pledgedSrcSize is only valid once, for the next frame.
676
- * It's discarded at the end of the frame, and replaced by ZSTD_CONTENTSIZE_UNKNOWN.
677
- * Note 3 : Whenever all input data is provided and consumed in a single round,
678
- * for example with ZSTD_compress2(),
679
- * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end),
680
- * this value is automatically overriden by srcSize instead.
681
- */
682
- ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
683
903
 
684
904
  /*! ZSTD_CCtx_loadDictionary() :
685
905
  * Create an internal CDict from `dict` buffer.
@@ -703,7 +923,9 @@ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, s
703
923
  /*! ZSTD_CCtx_refCDict() :
704
924
  * Reference a prepared dictionary, to be used for all next compressed frames.
705
925
  * Note that compression parameters are enforced from within CDict,
706
- * and supercede any compression parameter previously set within CCtx.
926
+ * and supersede any compression parameter previously set within CCtx.
927
+ * The parameters ignored are labled as "superseded-by-cdict" in the ZSTD_cParameter enum docs.
928
+ * The ignored parameters will be used again if the CCtx is returned to no-dictionary mode.
707
929
  * The dictionary will remain valid for future compressed frames using same CCtx.
708
930
  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
709
931
  * Special : Referencing a NULL CDict means "return to no-dictionary mode".
@@ -728,141 +950,11 @@ ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
728
950
  * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.
729
951
  * It's a CPU consuming operation, with non-negligible impact on latency.
730
952
  * If there is a need to use the same prefix multiple times, consider loadDictionary instead.
731
- * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dm_rawContent).
953
+ * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dct_rawContent).
732
954
  * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */
733
955
  ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
734
956
  const void* prefix, size_t prefixSize);
735
957
 
736
-
737
- typedef enum {
738
- ZSTD_reset_session_only = 1,
739
- ZSTD_reset_parameters = 2,
740
- ZSTD_reset_session_and_parameters = 3
741
- } ZSTD_ResetDirective;
742
-
743
- /*! ZSTD_CCtx_reset() :
744
- * There are 2 different things that can be reset, independently or jointly :
745
- * - The session : will stop compressing current frame, and make CCtx ready to start a new one.
746
- * Useful after an error, or to interrupt any ongoing compression.
747
- * Any internal data not yet flushed is cancelled.
748
- * Compression parameters and dictionary remain unchanged.
749
- * They will be used to compress next frame.
750
- * Resetting session never fails.
751
- * - The parameters : changes all parameters back to "default".
752
- * This removes any reference to any dictionary too.
753
- * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing)
754
- * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError())
755
- * - Both : similar to resetting the session, followed by resetting parameters.
756
- */
757
- ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset);
758
-
759
-
760
-
761
- /*! ZSTD_compress2() :
762
- * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API.
763
- * ZSTD_compress2() always starts a new frame.
764
- * Should cctx hold data from a previously unfinished frame, everything about it is forgotten.
765
- * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
766
- * - The function is always blocking, returns when compression is completed.
767
- * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
768
- * @return : compressed size written into `dst` (<= `dstCapacity),
769
- * or an error code if it fails (which can be tested using ZSTD_isError()).
770
- */
771
- ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx,
772
- void* dst, size_t dstCapacity,
773
- const void* src, size_t srcSize);
774
-
775
- typedef enum {
776
- ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
777
- ZSTD_e_flush=1, /* flush any data provided so far,
778
- * it creates (at least) one new block, that can be decoded immediately on reception;
779
- * frame will continue: any future data can still reference previously compressed data, improving compression. */
780
- ZSTD_e_end=2 /* flush any remaining data _and_ close current frame.
781
- * note that frame is only closed after compressed data is fully flushed (return value == 0).
782
- * After that point, any additional data starts a new frame.
783
- * note : each frame is independent (does not reference any content from previous frame). */
784
- } ZSTD_EndDirective;
785
-
786
- /*! ZSTD_compressStream2() :
787
- * Behaves about the same as ZSTD_compressStream, with additional control on end directive.
788
- * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*()
789
- * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode)
790
- * - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize
791
- * - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
792
- * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller.
793
- * - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available,
794
- * and then immediately returns, just indicating that there is some data remaining to be flushed.
795
- * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
796
- * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking.
797
- * - @return provides a minimum amount of data remaining to be flushed from internal buffers
798
- * or an error code, which can be tested using ZSTD_isError().
799
- * if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
800
- * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
801
- * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
802
- * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
803
- * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
804
- * Before starting a new compression job, or changing compression parameters,
805
- * it is required to fully flush internal buffers.
806
- */
807
- ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
808
- ZSTD_outBuffer* output,
809
- ZSTD_inBuffer* input,
810
- ZSTD_EndDirective endOp);
811
-
812
-
813
-
814
- /* ============================== */
815
- /* Advanced decompression API */
816
- /* ============================== */
817
-
818
- /* The advanced API pushes parameters one by one into an existing DCtx context.
819
- * Parameters are sticky, and remain valid for all following frames
820
- * using the same DCtx context.
821
- * It's possible to reset parameters to default values using ZSTD_DCtx_reset().
822
- * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream().
823
- * Therefore, no new decompression function is necessary.
824
- */
825
-
826
-
827
- typedef enum {
828
-
829
- ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which
830
- * the streaming API will refuse to allocate memory buffer
831
- * in order to protect the host from unreasonable memory requirements.
832
- * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
833
- * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) */
834
-
835
- /* note : additional experimental parameters are also available
836
- * within the experimental section of the API.
837
- * At the time of this writing, they include :
838
- * ZSTD_c_format
839
- * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them.
840
- * note : never ever use experimentalParam? names directly
841
- */
842
- ZSTD_d_experimentalParam1=1000
843
-
844
- } ZSTD_dParameter;
845
-
846
-
847
- /*! ZSTD_dParam_getBounds() :
848
- * All parameters must belong to an interval with lower and upper bounds,
849
- * otherwise they will either trigger an error or be automatically clamped.
850
- * @return : a structure, ZSTD_bounds, which contains
851
- * - an error status field, which must be tested using ZSTD_isError()
852
- * - both lower and upper bounds, inclusive
853
- */
854
- ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);
855
-
856
- /*! ZSTD_DCtx_setParameter() :
857
- * Set one compression parameter, selected by enum ZSTD_dParameter.
858
- * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds().
859
- * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter).
860
- * Setting a parameter is only possible during frame initialization (before starting decompression).
861
- * @return : 0, or an error code (which can be tested using ZSTD_isError()).
862
- */
863
- ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value);
864
-
865
-
866
958
  /*! ZSTD_DCtx_loadDictionary() :
867
959
  * Create an internal DDict from dict buffer,
868
960
  * to be used to decompress next frames.
@@ -902,7 +994,7 @@ ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
902
994
  * Note 2 : Prefix buffer is referenced. It **must** outlive decompression.
903
995
  * Prefix buffer must remain unmodified up to the end of frame,
904
996
  * reached when ZSTD_decompressStream() returns 0.
905
- * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
997
+ * Note 3 : By default, the prefix is treated as raw content (ZSTD_dct_rawContent).
906
998
  * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section)
907
999
  * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.
908
1000
  * A full dictionary is more costly, as it requires building tables.
@@ -910,15 +1002,32 @@ ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
910
1002
  ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
911
1003
  const void* prefix, size_t prefixSize);
912
1004
 
913
- /*! ZSTD_DCtx_reset() :
914
- * Return a DCtx to clean state.
915
- * Session and parameters can be reset jointly or separately.
916
- * Parameters can only be reset when no active frame is being decompressed.
917
- * @return : 0, or an error code, which can be tested with ZSTD_isError()
918
- */
919
- ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
1005
+ /* === Memory management === */
1006
+
1007
+ /*! ZSTD_sizeof_*() :
1008
+ * These functions give the _current_ memory usage of selected object.
1009
+ * Note that object memory usage can evolve (increase or decrease) over time. */
1010
+ ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
1011
+ ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
1012
+ ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
1013
+ ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
1014
+ ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
1015
+ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
1016
+
1017
+ #endif /* ZSTD_H_235446 */
920
1018
 
921
1019
 
1020
+ /* **************************************************************************************
1021
+ * ADVANCED AND EXPERIMENTAL FUNCTIONS
1022
+ ****************************************************************************************
1023
+ * The definitions in the following section are considered experimental.
1024
+ * They are provided for advanced scenarios.
1025
+ * They should never be used with a dynamic library, as prototypes may change in the future.
1026
+ * Use them only in association with static linking.
1027
+ * ***************************************************************************************/
1028
+
1029
+ #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
1030
+ #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
922
1031
 
923
1032
  /****************************************************************************************
924
1033
  * experimental API (static linking only)
@@ -930,8 +1039,8 @@ ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
930
1039
  * Some of them might be removed in the future (especially when redundant with existing stable functions)
931
1040
  * ***************************************************************************************/
932
1041
 
933
- #define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size required to query frame header size */
934
- #define ZSTD_FRAMEHEADERSIZE_MIN 6
1042
+ #define ZSTD_FRAMEHEADERSIZE_PREFIX(format) ((format) == ZSTD_f_zstd1 ? 5 : 1) /* minimum input size required to query frame header size */
1043
+ #define ZSTD_FRAMEHEADERSIZE_MIN(format) ((format) == ZSTD_f_zstd1 ? 6 : 2)
935
1044
  #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */
936
1045
  #define ZSTD_SKIPPABLEHEADERSIZE 8
937
1046
 
@@ -962,7 +1071,7 @@ ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
962
1071
  #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame
963
1072
  * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size,
964
1073
  * to preserve host's memory from unreasonable requirements.
965
- * This limit can be overriden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).
1074
+ * This limit can be overridden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).
966
1075
  * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */
967
1076
 
968
1077
 
@@ -976,6 +1085,12 @@ ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
976
1085
  #define ZSTD_LDM_HASHRATELOG_MIN 0
977
1086
  #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)
978
1087
 
1088
+ /* Advanced parameter bounds */
1089
+ #define ZSTD_TARGETCBLOCKSIZE_MIN 64
1090
+ #define ZSTD_TARGETCBLOCKSIZE_MAX ZSTD_BLOCKSIZE_MAX
1091
+ #define ZSTD_SRCSIZEHINT_MIN 0
1092
+ #define ZSTD_SRCSIZEHINT_MAX INT_MAX
1093
+
979
1094
  /* internal */
980
1095
  #define ZSTD_HASHLOG3_MAX 17
981
1096
 
@@ -984,6 +1099,24 @@ ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset);
984
1099
 
985
1100
  typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;
986
1101
 
1102
+ typedef struct {
1103
+ unsigned int matchPos; /* Match pos in dst */
1104
+ /* If seqDef.offset > 3, then this is seqDef.offset - 3
1105
+ * If seqDef.offset < 3, then this is the corresponding repeat offset
1106
+ * But if seqDef.offset < 3 and litLength == 0, this is the
1107
+ * repeat offset before the corresponding repeat offset
1108
+ * And if seqDef.offset == 3 and litLength == 0, this is the
1109
+ * most recent repeat offset - 1
1110
+ */
1111
+ unsigned int offset;
1112
+ unsigned int litLength; /* Literal length */
1113
+ unsigned int matchLength; /* Match length */
1114
+ /* 0 when seq not rep and seqDef.offset otherwise
1115
+ * when litLength == 0 this will be <= 4, otherwise <= 3 like normal
1116
+ */
1117
+ unsigned int rep;
1118
+ } ZSTD_Sequence;
1119
+
987
1120
  typedef struct {
988
1121
  unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
989
1122
  unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
@@ -1013,21 +1146,12 @@ typedef enum {
1013
1146
 
1014
1147
  typedef enum {
1015
1148
  ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */
1016
- ZSTD_dlm_byRef = 1, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
1149
+ ZSTD_dlm_byRef = 1 /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
1017
1150
  } ZSTD_dictLoadMethod_e;
1018
1151
 
1019
1152
  typedef enum {
1020
- /* Opened question : should we have a format ZSTD_f_auto ?
1021
- * Today, it would mean exactly the same as ZSTD_f_zstd1.
1022
- * But, in the future, should several formats become supported,
1023
- * on the compression side, it would mean "default format".
1024
- * On the decompression side, it would mean "automatic format detection",
1025
- * so that ZSTD_f_zstd1 would mean "accept *only* zstd frames".
1026
- * Since meaning is a little different, another option could be to define different enums for compression and decompression.
1027
- * This question could be kept for later, when there are actually multiple formats to support,
1028
- * but there is also the question of pinning enum values, and pinning value `0` is especially important */
1029
1153
  ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */
1030
- ZSTD_f_zstd1_magicless = 1, /* Variant of zstd frame format, without initial 4-bytes magic number.
1154
+ ZSTD_f_zstd1_magicless = 1 /* Variant of zstd frame format, without initial 4-bytes magic number.
1031
1155
  * Useful to save 4 bytes per generated frame.
1032
1156
  * Decoder cannot recognise automatically this format, requiring this instruction. */
1033
1157
  } ZSTD_format_e;
@@ -1038,7 +1162,7 @@ typedef enum {
1038
1162
  * to evolve and should be considered only in the context of extremely
1039
1163
  * advanced performance tuning.
1040
1164
  *
1041
- * Zstd currently supports the use of a CDict in two ways:
1165
+ * Zstd currently supports the use of a CDict in three ways:
1042
1166
  *
1043
1167
  * - The contents of the CDict can be copied into the working context. This
1044
1168
  * means that the compression can search both the dictionary and input
@@ -1054,6 +1178,12 @@ typedef enum {
1054
1178
  * working context's tables can be reused). For small inputs, this can be
1055
1179
  * faster than copying the CDict's tables.
1056
1180
  *
1181
+ * - The CDict's tables are not used at all, and instead we use the working
1182
+ * context alone to reload the dictionary and use params based on the source
1183
+ * size. See ZSTD_compress_insertDictionary() and ZSTD_compress_usingDict().
1184
+ * This method is effective when the dictionary sizes are very small relative
1185
+ * to the input size, and the input size is fairly large to begin with.
1186
+ *
1057
1187
  * Zstd has a simple internal heuristic that selects which strategy to use
1058
1188
  * at the beginning of a compression. However, if experimentation shows that
1059
1189
  * Zstd is making poor choices, it is possible to override that choice with
@@ -1062,17 +1192,27 @@ typedef enum {
1062
1192
  ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */
1063
1193
  ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */
1064
1194
  ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */
1195
+ ZSTD_dictForceLoad = 3 /* Always reload the dictionary */
1065
1196
  } ZSTD_dictAttachPref_e;
1066
1197
 
1198
+ typedef enum {
1199
+ ZSTD_lcm_auto = 0, /**< Automatically determine the compression mode based on the compression level.
1200
+ * Negative compression levels will be uncompressed, and positive compression
1201
+ * levels will be compressed. */
1202
+ ZSTD_lcm_huffman = 1, /**< Always attempt Huffman compression. Uncompressed literals will still be
1203
+ * emitted if Huffman compression is not profitable. */
1204
+ ZSTD_lcm_uncompressed = 2 /**< Always emit uncompressed literals. */
1205
+ } ZSTD_literalCompressionMode_e;
1206
+
1067
1207
 
1068
1208
  /***************************************
1069
1209
  * Frame size functions
1070
1210
  ***************************************/
1071
1211
 
1072
1212
  /*! ZSTD_findDecompressedSize() :
1073
- * `src` should point the start of a series of ZSTD encoded and/or skippable frames
1213
+ * `src` should point to the start of a series of ZSTD encoded and/or skippable frames
1074
1214
  * `srcSize` must be the _exact_ size of this series
1075
- * (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`)
1215
+ * (i.e. there should be a frame boundary at `src + srcSize`)
1076
1216
  * @return : - decompressed size of all data in all successive frames
1077
1217
  * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
1078
1218
  * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
@@ -1092,12 +1232,36 @@ typedef enum {
1092
1232
  * however it does mean that all frame data must be present and valid. */
1093
1233
  ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
1094
1234
 
1235
+ /*! ZSTD_decompressBound() :
1236
+ * `src` should point to the start of a series of ZSTD encoded and/or skippable frames
1237
+ * `srcSize` must be the _exact_ size of this series
1238
+ * (i.e. there should be a frame boundary at `src + srcSize`)
1239
+ * @return : - upper-bound for the decompressed size of all data in all successive frames
1240
+ * - if an error occured: ZSTD_CONTENTSIZE_ERROR
1241
+ *
1242
+ * note 1 : an error can occur if `src` contains an invalid or incorrectly formatted frame.
1243
+ * note 2 : the upper-bound is exact when the decompressed size field is available in every ZSTD encoded frame of `src`.
1244
+ * in this case, `ZSTD_findDecompressedSize` and `ZSTD_decompressBound` return the same value.
1245
+ * note 3 : when the decompressed size field isn't available, the upper-bound for that frame is calculated by:
1246
+ * upper-bound = # blocks * min(128 KB, Window_Size)
1247
+ */
1248
+ ZSTDLIB_API unsigned long long ZSTD_decompressBound(const void* src, size_t srcSize);
1249
+
1095
1250
  /*! ZSTD_frameHeaderSize() :
1096
1251
  * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.
1097
1252
  * @return : size of the Frame Header,
1098
1253
  * or an error code (if srcSize is too small) */
1099
1254
  ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
1100
1255
 
1256
+ /*! ZSTD_getSequences() :
1257
+ * Extract sequences from the sequence store
1258
+ * zc can be used to insert custom compression params.
1259
+ * This function invokes ZSTD_compress2
1260
+ * @return : number of sequences extracted
1261
+ */
1262
+ ZSTDLIB_API size_t ZSTD_getSequences(ZSTD_CCtx* zc, ZSTD_Sequence* outSeqs,
1263
+ size_t outSeqsSize, const void* src, size_t srcSize);
1264
+
1101
1265
 
1102
1266
  /***************************************
1103
1267
  * Memory management
@@ -1106,12 +1270,26 @@ ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
1106
1270
  /*! ZSTD_estimate*() :
1107
1271
  * These functions make it possible to estimate memory usage
1108
1272
  * of a future {D,C}Ctx, before its creation.
1109
- * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one.
1110
- * It will also consider src size to be arbitrarily "large", which is worst case.
1111
- * If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation.
1112
- * ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
1113
- * ZSTD_estimateCCtxSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
1114
- * Note : CCtx size estimation is only correct for single-threaded compression. */
1273
+ *
1274
+ * ZSTD_estimateCCtxSize() will provide a memory budget large enough
1275
+ * for any compression level up to selected one.
1276
+ * Note : Unlike ZSTD_estimateCStreamSize*(), this estimate
1277
+ * does not include space for a window buffer.
1278
+ * Therefore, the estimation is only guaranteed for single-shot compressions, not streaming.
1279
+ * The estimate will assume the input may be arbitrarily large,
1280
+ * which is the worst case.
1281
+ *
1282
+ * When srcSize can be bound by a known and rather "small" value,
1283
+ * this fact can be used to provide a tighter estimation
1284
+ * because the CCtx compression context will need less memory.
1285
+ * This tighter estimation can be provided by more advanced functions
1286
+ * ZSTD_estimateCCtxSize_usingCParams(), which can be used in tandem with ZSTD_getCParams(),
1287
+ * and ZSTD_estimateCCtxSize_usingCCtxParams(), which can be used in tandem with ZSTD_CCtxParams_setParameter().
1288
+ * Both can be used to estimate memory using custom compression parameters and arbitrary srcSize limits.
1289
+ *
1290
+ * Note 2 : only single-threaded compression is supported.
1291
+ * ZSTD_estimateCCtxSize_usingCCtxParams() will return an error code if ZSTD_c_nbWorkers is >= 1.
1292
+ */
1115
1293
  ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
1116
1294
  ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
1117
1295
  ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
@@ -1122,7 +1300,7 @@ ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
1122
1300
  * It will also consider src size to be arbitrarily "large", which is worst case.
1123
1301
  * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
1124
1302
  * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
1125
- * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParam_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
1303
+ * ZSTD_estimateCStreamSize_usingCCtxParams() can be used in tandem with ZSTD_CCtxParams_setParameter(). Only single-threaded compression is supported. This function will return an error code if ZSTD_c_nbWorkers is >= 1.
1126
1304
  * Note : CStream size estimation is only correct for single-threaded compression.
1127
1305
  * ZSTD_DStream memory budget depends on window Size.
1128
1306
  * This information can be passed manually, using ZSTD_estimateDStreamSize,
@@ -1222,30 +1400,37 @@ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictS
1222
1400
  * Create a digested dictionary for compression
1223
1401
  * Dictionary content is just referenced, not duplicated.
1224
1402
  * As a consequence, `dictBuffer` **must** outlive CDict,
1225
- * and its content must remain unmodified throughout the lifetime of CDict. */
1403
+ * and its content must remain unmodified throughout the lifetime of CDict.
1404
+ * note: equivalent to ZSTD_createCDict_advanced(), with dictLoadMethod==ZSTD_dlm_byRef */
1226
1405
  ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
1227
1406
 
1228
1407
  /*! ZSTD_getCParams() :
1229
- * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
1230
- * `estimatedSrcSize` value is optional, select 0 if not known */
1408
+ * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
1409
+ * `estimatedSrcSize` value is optional, select 0 if not known */
1231
1410
  ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
1232
1411
 
1233
1412
  /*! ZSTD_getParams() :
1234
- * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
1235
- * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
1413
+ * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
1414
+ * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
1236
1415
  ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
1237
1416
 
1238
1417
  /*! ZSTD_checkCParams() :
1239
- * Ensure param values remain within authorized range */
1418
+ * Ensure param values remain within authorized range.
1419
+ * @return 0 on success, or an error code (can be checked with ZSTD_isError()) */
1240
1420
  ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
1241
1421
 
1242
1422
  /*! ZSTD_adjustCParams() :
1243
1423
  * optimize params for a given `srcSize` and `dictSize`.
1244
- * both values are optional, select `0` if unknown. */
1424
+ * `srcSize` can be unknown, in which case use ZSTD_CONTENTSIZE_UNKNOWN.
1425
+ * `dictSize` must be `0` when there is no dictionary.
1426
+ * cPar can be invalid : all parameters will be clamped within valid range in the @return struct.
1427
+ * This function never fails (wide contract) */
1245
1428
  ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
1246
1429
 
1247
1430
  /*! ZSTD_compress_advanced() :
1248
- * Same as ZSTD_compress_usingDict(), with fine-tune control over compression parameters (by structure) */
1431
+ * Note : this function is now DEPRECATED.
1432
+ * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_setParameter() and other parameter setters.
1433
+ * This prototype will be marked as deprecated and generate compilation warning on reaching v1.5.x */
1249
1434
  ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,
1250
1435
  void* dst, size_t dstCapacity,
1251
1436
  const void* src, size_t srcSize,
@@ -1253,7 +1438,9 @@ ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,
1253
1438
  ZSTD_parameters params);
1254
1439
 
1255
1440
  /*! ZSTD_compress_usingCDict_advanced() :
1256
- * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
1441
+ * Note : this function is now REDUNDANT.
1442
+ * It can be replaced by ZSTD_compress2(), in combination with ZSTD_CCtx_loadDictionary() and other parameter setters.
1443
+ * This prototype will be marked as deprecated and generate compilation warning in some future version */
1257
1444
  ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
1258
1445
  void* dst, size_t dstCapacity,
1259
1446
  const void* src, size_t srcSize,
@@ -1314,6 +1501,23 @@ ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* pre
1314
1501
  * See the comments on that enum for an explanation of the feature. */
1315
1502
  #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4
1316
1503
 
1504
+ /* Controls how the literals are compressed (default is auto).
1505
+ * The value must be of type ZSTD_literalCompressionMode_e.
1506
+ * See ZSTD_literalCompressionMode_t enum definition for details.
1507
+ */
1508
+ #define ZSTD_c_literalCompressionMode ZSTD_c_experimentalParam5
1509
+
1510
+ /* Tries to fit compressed block size to be around targetCBlockSize.
1511
+ * No target when targetCBlockSize == 0.
1512
+ * There is no guarantee on compressed block size (default:0) */
1513
+ #define ZSTD_c_targetCBlockSize ZSTD_c_experimentalParam6
1514
+
1515
+ /* User's best guess of source size.
1516
+ * Hint is not valid when srcSizeHint == 0.
1517
+ * There is no guarantee that hint is close to actual source size,
1518
+ * but compression ratio may regress significantly if guess considerably underestimates */
1519
+ #define ZSTD_c_srcSizeHint ZSTD_c_experimentalParam7
1520
+
1317
1521
  /*! ZSTD_CCtx_getParameter() :
1318
1522
  * Get the requested compression parameter value, selected by enum ZSTD_cParameter,
1319
1523
  * and store it into int* value.
@@ -1325,10 +1529,10 @@ ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param
1325
1529
  /*! ZSTD_CCtx_params :
1326
1530
  * Quick howto :
1327
1531
  * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
1328
- * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into
1329
- * an existing ZSTD_CCtx_params structure.
1330
- * This is similar to
1331
- * ZSTD_CCtx_setParameter().
1532
+ * - ZSTD_CCtxParams_setParameter() : Push parameters one by one into
1533
+ * an existing ZSTD_CCtx_params structure.
1534
+ * This is similar to
1535
+ * ZSTD_CCtx_setParameter().
1332
1536
  * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
1333
1537
  * an existing CCtx.
1334
1538
  * These parameters will be applied to
@@ -1359,20 +1563,20 @@ ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compre
1359
1563
  */
1360
1564
  ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
1361
1565
 
1362
- /*! ZSTD_CCtxParam_setParameter() :
1566
+ /*! ZSTD_CCtxParams_setParameter() :
1363
1567
  * Similar to ZSTD_CCtx_setParameter.
1364
1568
  * Set one compression parameter, selected by enum ZSTD_cParameter.
1365
1569
  * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
1366
1570
  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1367
1571
  */
1368
- ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);
1572
+ ZSTDLIB_API size_t ZSTD_CCtxParams_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);
1369
1573
 
1370
- /*! ZSTD_CCtxParam_getParameter() :
1574
+ /*! ZSTD_CCtxParams_getParameter() :
1371
1575
  * Similar to ZSTD_CCtx_getParameter.
1372
1576
  * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
1373
1577
  * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1374
1578
  */
1375
- ZSTDLIB_API size_t ZSTD_CCtxParam_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);
1579
+ ZSTDLIB_API size_t ZSTD_CCtxParams_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);
1376
1580
 
1377
1581
  /*! ZSTD_CCtx_setParametersUsingCCtxParams() :
1378
1582
  * Apply a set of ZSTD_CCtx_params to the compression context.
@@ -1415,31 +1619,6 @@ ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
1415
1619
  * it must remain read accessible throughout the lifetime of DDict */
1416
1620
  ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
1417
1621
 
1418
-
1419
- /*! ZSTD_getDictID_fromDict() :
1420
- * Provides the dictID stored within dictionary.
1421
- * if @return == 0, the dictionary is not conformant with Zstandard specification.
1422
- * It can still be loaded, but as a content-only dictionary. */
1423
- ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
1424
-
1425
- /*! ZSTD_getDictID_fromDDict() :
1426
- * Provides the dictID of the dictionary loaded into `ddict`.
1427
- * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
1428
- * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
1429
- ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
1430
-
1431
- /*! ZSTD_getDictID_fromFrame() :
1432
- * Provides the dictID required to decompressed the frame stored within `src`.
1433
- * If @return == 0, the dictID could not be decoded.
1434
- * This could for one of the following reasons :
1435
- * - The frame does not require a dictionary to be decoded (most common case).
1436
- * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
1437
- * Note : this use case also happens when using a non-conformant dictionary.
1438
- * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
1439
- * - This is not a Zstandard frame.
1440
- * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
1441
- ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
1442
-
1443
1622
  /*! ZSTD_DCtx_loadDictionary_byReference() :
1444
1623
  * Same as ZSTD_DCtx_loadDictionary(),
1445
1624
  * but references `dict` content instead of copying it into `dctx`.
@@ -1473,6 +1652,37 @@ ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowS
1473
1652
  * allowing selection between ZSTD_format_e input compression formats
1474
1653
  */
1475
1654
  #define ZSTD_d_format ZSTD_d_experimentalParam1
1655
+ /* ZSTD_d_stableOutBuffer
1656
+ * Experimental parameter.
1657
+ * Default is 0 == disabled. Set to 1 to enable.
1658
+ *
1659
+ * Tells the decompressor that the ZSTD_outBuffer will ALWAYS be the same
1660
+ * between calls, except for the modifications that zstd makes to pos (the
1661
+ * caller must not modify pos). This is checked by the decompressor, and
1662
+ * decompression will fail if it ever changes. Therefore the ZSTD_outBuffer
1663
+ * MUST be large enough to fit the entire decompressed frame. This will be
1664
+ * checked when the frame content size is known. The data in the ZSTD_outBuffer
1665
+ * in the range [dst, dst + pos) MUST not be modified during decompression
1666
+ * or you will get data corruption.
1667
+ *
1668
+ * When this flags is enabled zstd won't allocate an output buffer, because
1669
+ * it can write directly to the ZSTD_outBuffer, but it will still allocate
1670
+ * an input buffer large enough to fit any compressed block. This will also
1671
+ * avoid the memcpy() from the internal output buffer to the ZSTD_outBuffer.
1672
+ * If you need to avoid the input buffer allocation use the buffer-less
1673
+ * streaming API.
1674
+ *
1675
+ * NOTE: So long as the ZSTD_outBuffer always points to valid memory, using
1676
+ * this flag is ALWAYS memory safe, and will never access out-of-bounds
1677
+ * memory. However, decompression WILL fail if you violate the preconditions.
1678
+ *
1679
+ * WARNING: The data in the ZSTD_outBuffer in the range [dst, dst + pos) MUST
1680
+ * not be modified during decompression or you will get data corruption. This
1681
+ * is because zstd needs to reference data in the ZSTD_outBuffer to regenerate
1682
+ * matches. Normally zstd maintains its own buffer for this purpose, but passing
1683
+ * this flag tells zstd to use the user provided buffer.
1684
+ */
1685
+ #define ZSTD_d_stableOutBuffer ZSTD_d_experimentalParam2
1476
1686
 
1477
1687
  /*! ZSTD_DCtx_setFormat() :
1478
1688
  * Instruct the decoder context about what kind of data to decode next.
@@ -1501,14 +1711,97 @@ ZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs (
1501
1711
  ********************************************************************/
1502
1712
 
1503
1713
  /*===== Advanced Streaming compression functions =====*/
1504
- ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If it is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, "0" also disables frame content size field. It may be enabled in the future. */
1505
- ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< creates of an internal CDict (incompatible with static CCtx), except if dict == NULL or dictSize < 8, in which case no dict is used. Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.*/
1506
- ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
1507
- ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. */
1508
- ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); /**< note : cdict will just be referenced, and must outlive compression session */
1509
- ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters. pledgedSrcSize must be correct. If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN. */
1714
+ /**! ZSTD_initCStream_srcSize() :
1715
+ * This function is deprecated, and equivalent to:
1716
+ * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1717
+ * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any)
1718
+ * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
1719
+ * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1720
+ *
1721
+ * pledgedSrcSize must be correct. If it is not known at init time, use
1722
+ * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs,
1723
+ * "0" also disables frame content size field. It may be enabled in the future.
1724
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1725
+ */
1726
+ ZSTDLIB_API size_t
1727
+ ZSTD_initCStream_srcSize(ZSTD_CStream* zcs,
1728
+ int compressionLevel,
1729
+ unsigned long long pledgedSrcSize);
1730
+
1731
+ /**! ZSTD_initCStream_usingDict() :
1732
+ * This function is deprecated, and is equivalent to:
1733
+ * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1734
+ * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel);
1735
+ * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
1736
+ *
1737
+ * Creates of an internal CDict (incompatible with static CCtx), except if
1738
+ * dict == NULL or dictSize < 8, in which case no dict is used.
1739
+ * Note: dict is loaded with ZSTD_dct_auto (treated as a full zstd dictionary if
1740
+ * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy.
1741
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1742
+ */
1743
+ ZSTDLIB_API size_t
1744
+ ZSTD_initCStream_usingDict(ZSTD_CStream* zcs,
1745
+ const void* dict, size_t dictSize,
1746
+ int compressionLevel);
1747
+
1748
+ /**! ZSTD_initCStream_advanced() :
1749
+ * This function is deprecated, and is approximately equivalent to:
1750
+ * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1751
+ * // Pseudocode: Set each zstd parameter and leave the rest as-is.
1752
+ * for ((param, value) : params) {
1753
+ * ZSTD_CCtx_setParameter(zcs, param, value);
1754
+ * }
1755
+ * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1756
+ * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize);
1757
+ *
1758
+ * dict is loaded with ZSTD_dct_auto and ZSTD_dlm_byCopy.
1759
+ * pledgedSrcSize must be correct.
1760
+ * If srcSize is not known at init time, use value ZSTD_CONTENTSIZE_UNKNOWN.
1761
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1762
+ */
1763
+ ZSTDLIB_API size_t
1764
+ ZSTD_initCStream_advanced(ZSTD_CStream* zcs,
1765
+ const void* dict, size_t dictSize,
1766
+ ZSTD_parameters params,
1767
+ unsigned long long pledgedSrcSize);
1768
+
1769
+ /**! ZSTD_initCStream_usingCDict() :
1770
+ * This function is deprecated, and equivalent to:
1771
+ * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1772
+ * ZSTD_CCtx_refCDict(zcs, cdict);
1773
+ *
1774
+ * note : cdict will just be referenced, and must outlive compression session
1775
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1776
+ */
1777
+ ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict);
1778
+
1779
+ /**! ZSTD_initCStream_usingCDict_advanced() :
1780
+ * This function is DEPRECATED, and is approximately equivalent to:
1781
+ * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1782
+ * // Pseudocode: Set each zstd frame parameter and leave the rest as-is.
1783
+ * for ((fParam, value) : fParams) {
1784
+ * ZSTD_CCtx_setParameter(zcs, fParam, value);
1785
+ * }
1786
+ * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1787
+ * ZSTD_CCtx_refCDict(zcs, cdict);
1788
+ *
1789
+ * same as ZSTD_initCStream_usingCDict(), with control over frame parameters.
1790
+ * pledgedSrcSize must be correct. If srcSize is not known at init time, use
1791
+ * value ZSTD_CONTENTSIZE_UNKNOWN.
1792
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1793
+ */
1794
+ ZSTDLIB_API size_t
1795
+ ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs,
1796
+ const ZSTD_CDict* cdict,
1797
+ ZSTD_frameParameters fParams,
1798
+ unsigned long long pledgedSrcSize);
1510
1799
 
1511
1800
  /*! ZSTD_resetCStream() :
1801
+ * This function is deprecated, and is equivalent to:
1802
+ * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only);
1803
+ * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize);
1804
+ *
1512
1805
  * start a new frame, using same parameters from previous frame.
1513
1806
  * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
1514
1807
  * Note that zcs must be init at least once before using ZSTD_resetCStream().
@@ -1517,6 +1810,7 @@ ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const
1517
1810
  * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs,
1518
1811
  * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.
1519
1812
  * @return : 0, or an error code (which can be tested using ZSTD_isError())
1813
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1520
1814
  */
1521
1815
  ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
1522
1816
 
@@ -1555,9 +1849,37 @@ ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
1555
1849
 
1556
1850
 
1557
1851
  /*===== Advanced Streaming decompression functions =====*/
1558
- ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: no dictionary will be used if dict == NULL or dictSize < 8 */
1559
- ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict is referenced, it must outlive decompression session */
1560
- ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */
1852
+ /**
1853
+ * This function is deprecated, and is equivalent to:
1854
+ *
1855
+ * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
1856
+ * ZSTD_DCtx_loadDictionary(zds, dict, dictSize);
1857
+ *
1858
+ * note: no dictionary will be used if dict == NULL or dictSize < 8
1859
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1860
+ */
1861
+ ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
1862
+
1863
+ /**
1864
+ * This function is deprecated, and is equivalent to:
1865
+ *
1866
+ * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
1867
+ * ZSTD_DCtx_refDDict(zds, ddict);
1868
+ *
1869
+ * note : ddict is referenced, it must outlive decompression session
1870
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1871
+ */
1872
+ ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict);
1873
+
1874
+ /**
1875
+ * This function is deprecated, and is equivalent to:
1876
+ *
1877
+ * ZSTD_DCtx_reset(zds, ZSTD_reset_session_only);
1878
+ *
1879
+ * re-use decompression parameters from previous init; saves dictionary loading
1880
+ * Note : this prototype will be marked as deprecated and generate compilation warnings on reaching v1.5.x
1881
+ */
1882
+ ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds);
1561
1883
 
1562
1884
 
1563
1885
  /*********************************************************************
@@ -1696,7 +2018,7 @@ typedef struct {
1696
2018
  unsigned checksumFlag;
1697
2019
  } ZSTD_frameHeader;
1698
2020
 
1699
- /** ZSTD_getFrameHeader() :
2021
+ /*! ZSTD_getFrameHeader() :
1700
2022
  * decode Frame Header, or requires larger `srcSize`.
1701
2023
  * @return : 0, `zfhPtr` is correctly filled,
1702
2024
  * >0, `srcSize` is too small, value is wanted `srcSize` amount,
@@ -1729,8 +2051,8 @@ ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
1729
2051
 
1730
2052
  /*!
1731
2053
  Block functions produce and decode raw zstd blocks, without frame metadata.
1732
- Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
1733
- User will have to take in charge required information to regenerate data, such as compressed and content sizes.
2054
+ Frame metadata cost is typically ~12 bytes, which can be non-negligible for very small blocks (< 100 bytes).
2055
+ But users will have to take in charge needed metadata to regenerate data, such as compressed and content sizes.
1734
2056
 
1735
2057
  A few rules to respect :
1736
2058
  - Compressing and decompressing require a context structure
@@ -1741,12 +2063,14 @@ ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
1741
2063
  + copyCCtx() and copyDCtx() can be used too
1742
2064
  - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
1743
2065
  + If input is larger than a block size, it's necessary to split input data into multiple blocks
1744
- + For inputs larger than a single block, really consider using regular ZSTD_compress() instead.
1745
- Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
1746
- - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
1747
- In which case, nothing is produced into `dst` !
1748
- + User must test for such outcome and deal directly with uncompressed data
1749
- + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
2066
+ + For inputs larger than a single block, consider using regular ZSTD_compress() instead.
2067
+ Frame metadata is not that costly, and quickly becomes negligible as source size grows larger than a block.
2068
+ - When a block is considered not compressible enough, ZSTD_compressBlock() result will be 0 (zero) !
2069
+ ===> In which case, nothing is produced into `dst` !
2070
+ + User __must__ test for such outcome and deal directly with uncompressed data
2071
+ + A block cannot be declared incompressible if ZSTD_compressBlock() return value was != 0.
2072
+ Doing so would mess up with statistics history, leading to potential data corruption.
2073
+ + ZSTD_decompressBlock() _doesn't accept uncompressed data as input_ !!
1750
2074
  + In case of multiple successive blocks, should some of them be uncompressed,
1751
2075
  decoder must be informed of their existence in order to follow proper history.
1752
2076
  Use ZSTD_insertBlock() for such a case.