zstd-ruby 1.3.7.0 → 1.3.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/ext/zstdruby/libzstd/BUCK +15 -2
  4. data/ext/zstdruby/libzstd/Makefile +37 -2
  5. data/ext/zstdruby/libzstd/README.md +67 -41
  6. data/ext/zstdruby/libzstd/common/bitstream.h +2 -2
  7. data/ext/zstdruby/libzstd/common/compiler.h +19 -12
  8. data/ext/zstdruby/libzstd/common/cpu.h +1 -1
  9. data/ext/zstdruby/libzstd/common/debug.h +22 -11
  10. data/ext/zstdruby/libzstd/common/error_private.c +6 -0
  11. data/ext/zstdruby/libzstd/common/fse.h +2 -2
  12. data/ext/zstdruby/libzstd/common/huf.h +25 -1
  13. data/ext/zstdruby/libzstd/common/pool.c +1 -1
  14. data/ext/zstdruby/libzstd/common/zstd_common.c +3 -1
  15. data/ext/zstdruby/libzstd/common/zstd_errors.h +1 -0
  16. data/ext/zstdruby/libzstd/common/zstd_internal.h +11 -2
  17. data/ext/zstdruby/libzstd/compress/fse_compress.c +3 -3
  18. data/ext/zstdruby/libzstd/compress/hist.c +19 -11
  19. data/ext/zstdruby/libzstd/compress/hist.h +11 -8
  20. data/ext/zstdruby/libzstd/compress/huf_compress.c +33 -31
  21. data/ext/zstdruby/libzstd/compress/zstd_compress.c +621 -371
  22. data/ext/zstdruby/libzstd/compress/zstd_compress_internal.h +90 -28
  23. data/ext/zstdruby/libzstd/compress/zstd_double_fast.c +4 -4
  24. data/ext/zstdruby/libzstd/compress/zstd_fast.c +15 -15
  25. data/ext/zstdruby/libzstd/compress/zstd_lazy.c +25 -18
  26. data/ext/zstdruby/libzstd/compress/zstd_ldm.c +18 -67
  27. data/ext/zstdruby/libzstd/compress/zstd_ldm.h +2 -6
  28. data/ext/zstdruby/libzstd/compress/zstd_opt.c +133 -48
  29. data/ext/zstdruby/libzstd/compress/zstd_opt.h +8 -0
  30. data/ext/zstdruby/libzstd/compress/zstdmt_compress.c +229 -73
  31. data/ext/zstdruby/libzstd/compress/zstdmt_compress.h +18 -10
  32. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +178 -42
  33. data/ext/zstdruby/libzstd/decompress/zstd_ddict.c +240 -0
  34. data/ext/zstdruby/libzstd/decompress/zstd_ddict.h +44 -0
  35. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +244 -1680
  36. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.c +1307 -0
  37. data/ext/zstdruby/libzstd/decompress/zstd_decompress_block.h +59 -0
  38. data/ext/zstdruby/libzstd/decompress/zstd_decompress_internal.h +168 -0
  39. data/ext/zstdruby/libzstd/dictBuilder/cover.c +13 -11
  40. data/ext/zstdruby/libzstd/dictBuilder/fastcover.c +15 -15
  41. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +28 -28
  42. data/ext/zstdruby/libzstd/dll/libzstd.def +0 -1
  43. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +0 -10
  44. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +15 -15
  45. data/ext/zstdruby/libzstd/zstd.h +1208 -968
  46. data/lib/zstd-ruby/version.rb +1 -1
  47. metadata +7 -2
@@ -78,7 +78,6 @@ EXPORTS
78
78
  ZSTD_nextSrcSizeToDecompress
79
79
  ZSTD_resetCStream
80
80
  ZSTD_resetDStream
81
- ZSTD_setDStreamParameter
82
81
  ZSTD_sizeof_CCtx
83
82
  ZSTD_sizeof_CDict
84
83
  ZSTD_sizeof_CStream
@@ -240,17 +240,7 @@ MEM_STATIC size_t MEM_readLEST(const void* memPtr)
240
240
  /* *************************************
241
241
  * Types
242
242
  ***************************************/
243
- #define ZSTD_WINDOWLOG_MAX 26
244
- #define ZSTD_WINDOWLOG_MIN 18
245
243
  #define ZSTD_WINDOWLOG_ABSOLUTEMIN 11
246
- #define ZSTD_CONTENTLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
247
- #define ZSTD_CONTENTLOG_MIN 4
248
- #define ZSTD_HASHLOG_MAX 28
249
- #define ZSTD_HASHLOG_MIN 4
250
- #define ZSTD_SEARCHLOG_MAX (ZSTD_CONTENTLOG_MAX-1)
251
- #define ZSTD_SEARCHLOG_MIN 1
252
- #define ZSTD_SEARCHLENGTH_MAX 7
253
- #define ZSTD_SEARCHLENGTH_MIN 4
254
244
 
255
245
  /** from faster to stronger */
256
246
  typedef enum { ZSTD_fast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2 } ZSTD_strategy;
@@ -836,7 +836,7 @@ MEM_STATIC void BITv05_skipBits(BITv05_DStream_t* bitD, U32 nbBits)
836
836
  bitD->bitsConsumed += nbBits;
837
837
  }
838
838
 
839
- MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, U32 nbBits)
839
+ MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, unsigned nbBits)
840
840
  {
841
841
  size_t value = BITv05_lookBits(bitD, nbBits);
842
842
  BITv05_skipBits(bitD, nbBits);
@@ -845,7 +845,7 @@ MEM_STATIC size_t BITv05_readBits(BITv05_DStream_t* bitD, U32 nbBits)
845
845
 
846
846
  /*!BITv05_readBitsFast :
847
847
  * unsafe version; only works only if nbBits >= 1 */
848
- MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, U32 nbBits)
848
+ MEM_STATIC size_t BITv05_readBitsFast(BITv05_DStream_t* bitD, unsigned nbBits)
849
849
  {
850
850
  size_t value = BITv05_lookBitsFast(bitD, nbBits);
851
851
  BITv05_skipBits(bitD, nbBits);
@@ -1162,7 +1162,7 @@ MEM_STATIC unsigned FSEv05_endOfDState(const FSEv05_DState_t* DStatePtr)
1162
1162
  /* **************************************************************
1163
1163
  * Complex types
1164
1164
  ****************************************************************/
1165
- typedef U32 DTable_max_t[FSEv05_DTABLE_SIZE_U32(FSEv05_MAX_TABLELOG)];
1165
+ typedef unsigned DTable_max_t[FSEv05_DTABLE_SIZE_U32(FSEv05_MAX_TABLELOG)];
1166
1166
 
1167
1167
 
1168
1168
  /* **************************************************************
@@ -2191,7 +2191,7 @@ static void HUFv05_fillDTableX4(HUFv05_DEltX4* DTable, const U32 targetLog,
2191
2191
  }
2192
2192
  }
2193
2193
 
2194
- size_t HUFv05_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
2194
+ size_t HUFv05_readDTableX4 (unsigned* DTable, const void* src, size_t srcSize)
2195
2195
  {
2196
2196
  BYTE weightList[HUFv05_MAX_SYMBOL_VALUE + 1];
2197
2197
  sortedSymbol_t sortedSymbol[HUFv05_MAX_SYMBOL_VALUE + 1];
@@ -2205,7 +2205,7 @@ size_t HUFv05_readDTableX4 (U32* DTable, const void* src, size_t srcSize)
2205
2205
  void* dtPtr = DTable;
2206
2206
  HUFv05_DEltX4* const dt = ((HUFv05_DEltX4*)dtPtr) + 1;
2207
2207
 
2208
- HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(U32)); /* if compilation fails here, assertion is false */
2208
+ HUFv05_STATIC_ASSERT(sizeof(HUFv05_DEltX4) == sizeof(unsigned)); /* if compilation fails here, assertion is false */
2209
2209
  if (memLog > HUFv05_ABSOLUTEMAX_TABLELOG) return ERROR(tableLog_tooLarge);
2210
2210
  //memset(weightList, 0, sizeof(weightList)); /* is not necessary, even though some analyzer complain ... */
2211
2211
 
@@ -2332,7 +2332,7 @@ static inline size_t HUFv05_decodeStreamX4(BYTE* p, BITv05_DStream_t* bitDPtr, B
2332
2332
  size_t HUFv05_decompress1X4_usingDTable(
2333
2333
  void* dst, size_t dstSize,
2334
2334
  const void* cSrc, size_t cSrcSize,
2335
- const U32* DTable)
2335
+ const unsigned* DTable)
2336
2336
  {
2337
2337
  const BYTE* const istart = (const BYTE*) cSrc;
2338
2338
  BYTE* const ostart = (BYTE*) dst;
@@ -2375,7 +2375,7 @@ size_t HUFv05_decompress1X4 (void* dst, size_t dstSize, const void* cSrc, size_t
2375
2375
  size_t HUFv05_decompress4X4_usingDTable(
2376
2376
  void* dst, size_t dstSize,
2377
2377
  const void* cSrc, size_t cSrcSize,
2378
- const U32* DTable)
2378
+ const unsigned* DTable)
2379
2379
  {
2380
2380
  if (cSrcSize < 10) return ERROR(corruption_detected); /* strict minimum : jump table + 1 byte per stream */
2381
2381
 
@@ -2999,7 +2999,7 @@ static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t
2999
2999
  const BYTE* ip = istart;
3000
3000
  const BYTE* const iend = istart + srcSize;
3001
3001
  U32 LLtype, Offtype, MLtype;
3002
- U32 LLlog, Offlog, MLlog;
3002
+ unsigned LLlog, Offlog, MLlog;
3003
3003
  size_t dumpsLength;
3004
3004
 
3005
3005
  /* check */
@@ -3057,7 +3057,7 @@ static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t
3057
3057
  break;
3058
3058
  case FSEv05_ENCODING_DYNAMIC :
3059
3059
  default : /* impossible */
3060
- { U32 max = MaxLL;
3060
+ { unsigned max = MaxLL;
3061
3061
  headerSize = FSEv05_readNCount(norm, &max, &LLlog, ip, iend-ip);
3062
3062
  if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3063
3063
  if (LLlog > LLFSEv05Log) return ERROR(corruption_detected);
@@ -3081,7 +3081,7 @@ static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t
3081
3081
  break;
3082
3082
  case FSEv05_ENCODING_DYNAMIC :
3083
3083
  default : /* impossible */
3084
- { U32 max = MaxOff;
3084
+ { unsigned max = MaxOff;
3085
3085
  headerSize = FSEv05_readNCount(norm, &max, &Offlog, ip, iend-ip);
3086
3086
  if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3087
3087
  if (Offlog > OffFSEv05Log) return ERROR(corruption_detected);
@@ -3105,7 +3105,7 @@ static size_t ZSTDv05_decodeSeqHeaders(int* nbSeq, const BYTE** dumpsPtr, size_t
3105
3105
  break;
3106
3106
  case FSEv05_ENCODING_DYNAMIC :
3107
3107
  default : /* impossible */
3108
- { U32 max = MaxML;
3108
+ { unsigned max = MaxML;
3109
3109
  headerSize = FSEv05_readNCount(norm, &max, &MLlog, ip, iend-ip);
3110
3110
  if (FSEv05_isError(headerSize)) return ERROR(GENERIC);
3111
3111
  if (MLlog > MLFSEv05Log) return ERROR(corruption_detected);
@@ -3305,9 +3305,9 @@ static size_t ZSTDv05_decompressSequences(
3305
3305
  const BYTE* const litEnd = litPtr + dctx->litSize;
3306
3306
  int nbSeq=0;
3307
3307
  const BYTE* dumps = NULL;
3308
- U32* DTableLL = dctx->LLTable;
3309
- U32* DTableML = dctx->MLTable;
3310
- U32* DTableOffb = dctx->OffTable;
3308
+ unsigned* DTableLL = dctx->LLTable;
3309
+ unsigned* DTableML = dctx->MLTable;
3310
+ unsigned* DTableOffb = dctx->OffTable;
3311
3311
  const BYTE* const base = (const BYTE*) (dctx->base);
3312
3312
  const BYTE* const vBase = (const BYTE*) (dctx->vBase);
3313
3313
  const BYTE* const dictEnd = (const BYTE*) (dctx->dictEnd);
@@ -3633,7 +3633,7 @@ static size_t ZSTDv05_loadEntropy(ZSTDv05_DCtx* dctx, const void* dict, size_t d
3633
3633
  {
3634
3634
  size_t hSize, offcodeHeaderSize, matchlengthHeaderSize, errorCode, litlengthHeaderSize;
3635
3635
  short offcodeNCount[MaxOff+1];
3636
- U32 offcodeMaxValue=MaxOff, offcodeLog;
3636
+ unsigned offcodeMaxValue=MaxOff, offcodeLog;
3637
3637
  short matchlengthNCount[MaxML+1];
3638
3638
  unsigned matchlengthMaxValue = MaxML, matchlengthLog;
3639
3639
  short litlengthNCount[MaxLL+1];
@@ -71,16 +71,16 @@ extern "C" {
71
71
  /*------ Version ------*/
72
72
  #define ZSTD_VERSION_MAJOR 1
73
73
  #define ZSTD_VERSION_MINOR 3
74
- #define ZSTD_VERSION_RELEASE 7
74
+ #define ZSTD_VERSION_RELEASE 8
75
75
 
76
76
  #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
77
- ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< useful to check dll version */
77
+ ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to check runtime library version */
78
78
 
79
79
  #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
80
80
  #define ZSTD_QUOTE(str) #str
81
81
  #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
82
82
  #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
83
- ZSTDLIB_API const char* ZSTD_versionString(void); /* v1.3.0+ */
83
+ ZSTDLIB_API const char* ZSTD_versionString(void); /* requires v1.3.0+ */
84
84
 
85
85
  /***************************************
86
86
  * Default constant
@@ -110,7 +110,7 @@ ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
110
110
  ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
111
111
  const void* src, size_t compressedSize);
112
112
 
113
- /*! ZSTD_getFrameContentSize() : added in v1.3.0
113
+ /*! ZSTD_getFrameContentSize() : requires v1.3.0+
114
114
  * `src` should point to the start of a ZSTD encoded frame.
115
115
  * `srcSize` must be at least as large as the frame header.
116
116
  * hint : any size >= `ZSTD_frameHeaderSize_max` is large enough.
@@ -167,8 +167,10 @@ ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
167
167
  ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
168
168
 
169
169
  /*! ZSTD_compressCCtx() :
170
- * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */
171
- ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx,
170
+ * Same as ZSTD_compress(), using an explicit ZSTD_CCtx
171
+ * The function will compress at requested compression level,
172
+ * ignoring any other parameter */
173
+ ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* cctx,
172
174
  void* dst, size_t dstCapacity,
173
175
  const void* src, size_t srcSize,
174
176
  int compressionLevel);
@@ -184,8 +186,11 @@ ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
184
186
  ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
185
187
 
186
188
  /*! ZSTD_decompressDCtx() :
187
- * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()) */
188
- ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx,
189
+ * Same as ZSTD_decompress(),
190
+ * requires an allocated ZSTD_DCtx.
191
+ * Compatible with sticky parameters.
192
+ */
193
+ ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx,
189
194
  void* dst, size_t dstCapacity,
190
195
  const void* src, size_t srcSize);
191
196
 
@@ -194,9 +199,12 @@ ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx,
194
199
  * Simple dictionary API
195
200
  ***************************/
196
201
  /*! ZSTD_compress_usingDict() :
197
- * Compression using a predefined Dictionary (see dictBuilder/zdict.h).
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).
198
205
  * Note : This function loads the dictionary, resulting in significant startup delay.
199
- * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
206
+ * It's intended for a dictionary used only once.
207
+ * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */
200
208
  ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
201
209
  void* dst, size_t dstCapacity,
202
210
  const void* src, size_t srcSize,
@@ -204,9 +212,10 @@ ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
204
212
  int compressionLevel);
205
213
 
206
214
  /*! ZSTD_decompress_usingDict() :
207
- * Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
215
+ * Decompression using a known Dictionary.
208
216
  * Dictionary must be identical to the one used during compression.
209
217
  * Note : This function loads the dictionary, resulting in significant startup delay.
218
+ * It's intended for a dictionary used only once.
210
219
  * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
211
220
  ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
212
221
  void* dst, size_t dstCapacity,
@@ -214,17 +223,18 @@ ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
214
223
  const void* dict,size_t dictSize);
215
224
 
216
225
 
217
- /**********************************
226
+ /***********************************
218
227
  * Bulk processing dictionary API
219
- *********************************/
228
+ **********************************/
220
229
  typedef struct ZSTD_CDict_s ZSTD_CDict;
221
230
 
222
231
  /*! ZSTD_createCDict() :
223
- * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
224
- * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
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.
225
234
  * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only.
226
- * `dictBuffer` can be released after ZSTD_CDict creation, since its content is copied within CDict
227
- * Note : A ZSTD_CDict can be created with an empty dictionary, but it is inefficient for small data. */
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. */
228
238
  ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize,
229
239
  int compressionLevel);
230
240
 
@@ -234,11 +244,9 @@ ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
234
244
 
235
245
  /*! ZSTD_compress_usingCDict() :
236
246
  * Compression using a digested Dictionary.
237
- * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
238
- * Note that compression level is decided during dictionary creation.
239
- * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no)
240
- * Note : ZSTD_compress_usingCDict() can be used with a ZSTD_CDict created from an empty dictionary.
241
- * But it is inefficient for small data, and it is recommended to use ZSTD_compressCCtx(). */
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) */
242
250
  ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
243
251
  void* dst, size_t dstCapacity,
244
252
  const void* src, size_t srcSize,
@@ -249,7 +257,7 @@ typedef struct ZSTD_DDict_s ZSTD_DDict;
249
257
 
250
258
  /*! ZSTD_createDDict() :
251
259
  * Create a digested dictionary, ready to start decompression operation without startup delay.
252
- * dictBuffer can be released after DDict creation, as its content is copied inside DDict */
260
+ * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */
253
261
  ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize);
254
262
 
255
263
  /*! ZSTD_freeDDict() :
@@ -258,7 +266,7 @@ ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
258
266
 
259
267
  /*! ZSTD_decompress_usingDDict() :
260
268
  * Decompression using a digested Dictionary.
261
- * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */
269
+ * Recommended when same dictionary is used multiple times. */
262
270
  ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
263
271
  void* dst, size_t dstCapacity,
264
272
  const void* src, size_t srcSize,
@@ -289,13 +297,17 @@ typedef struct ZSTD_outBuffer_s {
289
297
  * A ZSTD_CStream object is required to track streaming operation.
290
298
  * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
291
299
  * ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
292
- * It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
293
- * since it will play nicer with system's memory, by re-using already allocated memory.
294
- * Use one separate ZSTD_CStream per thread for parallel execution.
300
+ * It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory.
301
+ *
302
+ * For parallel execution, use one separate ZSTD_CStream per thread.
303
+ *
304
+ * note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing.
295
305
  *
296
- * Start a new compression by initializing ZSTD_CStream context.
297
- * Use ZSTD_initCStream() to start a new compression operation.
298
- * Use variants ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for streaming with dictionary (experimental section)
306
+ * Parameters are sticky : when starting a new compression on the same context,
307
+ * it will re-use the same sticky parameters as previous compression session.
308
+ * 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.
299
311
  *
300
312
  * Use ZSTD_compressStream() as many times as necessary to consume input stream.
301
313
  * The function will automatically update both `pos` fields within `input` and `output`.
@@ -304,12 +316,11 @@ typedef struct ZSTD_outBuffer_s {
304
316
  * in which case `input.pos < input.size`.
305
317
  * The caller must check if input has been entirely consumed.
306
318
  * If not, the caller must make some room to receive more compressed data,
307
- * typically by emptying output buffer, or allocating a new output buffer,
308
319
  * and then present again remaining input data.
309
- * @return : a size hint, preferred nb of bytes to use as input for next function call
310
- * or an error code, which can be tested using ZSTD_isError().
311
- * Note 1 : it's just a hint, to help latency a little, any other value will work fine.
312
- * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
320
+ * @return : a size hint, preferred nb of bytes to use as input for next function call
321
+ * 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()
313
324
  *
314
325
  * At any moment, it's possible to flush whatever data might remain stuck within internal buffer,
315
326
  * using ZSTD_flushStream(). `output->pos` will be updated.
@@ -353,25 +364,24 @@ ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output
353
364
  * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
354
365
  * ZSTD_DStream objects can be re-used multiple times.
355
366
  *
356
- * Use ZSTD_initDStream() to start a new decompression operation,
357
- * or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
358
- * @return : recommended first input size
367
+ * Use ZSTD_initDStream() to start a new decompression operation.
368
+ * @return : recommended first input size
369
+ * Alternatively, use advanced API to set specific properties.
359
370
  *
360
371
  * Use ZSTD_decompressStream() repetitively to consume your input.
361
372
  * The function will update both `pos` fields.
362
373
  * If `input.pos < input.size`, some input has not been consumed.
363
374
  * It's up to the caller to present again remaining data.
364
- * The function tries to flush all data decoded immediately, repecting buffer sizes.
375
+ * The function tries to flush all data decoded immediately, respecting output buffer size.
365
376
  * If `output.pos < output.size`, decoder has flushed everything it could.
366
- * But if `output.pos == output.size`, there is no such guarantee,
367
- * it's likely that some decoded data was not flushed and still remains within internal buffers.
377
+ * But if `output.pos == output.size`, there might be some data left within internal buffers.,
368
378
  * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer.
369
- * When no additional input is provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.
379
+ * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX.
370
380
  * @return : 0 when a frame is completely decoded and fully flushed,
371
381
  * or an error code, which can be tested using ZSTD_isError(),
372
382
  * or any other value > 0, which means there is still some decoding or flushing to do to complete current frame :
373
- * the return value is a suggested next input size (a hint for better latency)
374
- * that will never load more than the current frame.
383
+ * the return value is a suggested next input size (just a hint for better latency)
384
+ * that will never request more than the remaining frame size.
375
385
  * *******************************************************************************/
376
386
 
377
387
  typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */
@@ -392,148 +402,64 @@ ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output
392
402
 
393
403
 
394
404
 
395
- #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
396
- #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
397
-
398
405
  /****************************************************************************************
399
406
  * ADVANCED AND EXPERIMENTAL FUNCTIONS
400
407
  ****************************************************************************************
401
- * The definitions in this section are considered experimental.
402
- * They should never be used with a dynamic library, as prototypes may change in the future.
408
+ * The definitions in the following section are considered experimental.
403
409
  * They are provided for advanced scenarios.
410
+ * They should never be used with a dynamic library, as prototypes may change in the future.
404
411
  * Use them only in association with static linking.
405
412
  * ***************************************************************************************/
406
413
 
407
- ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
408
-
409
- /* --- Constants ---*/
410
- #define ZSTD_MAGICNUMBER 0xFD2FB528 /* v0.8+ */
411
- #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* v0.7+ */
412
- #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
413
-
414
- #define ZSTD_BLOCKSIZELOG_MAX 17
415
- #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) /* define, for static allocation */
416
-
417
- #define ZSTD_WINDOWLOG_MAX_32 30
418
- #define ZSTD_WINDOWLOG_MAX_64 31
419
- #define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
420
- #define ZSTD_WINDOWLOG_MIN 10
421
- #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30)
422
- #define ZSTD_HASHLOG_MIN 6
423
- #define ZSTD_CHAINLOG_MAX_32 29
424
- #define ZSTD_CHAINLOG_MAX_64 30
425
- #define ZSTD_CHAINLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64))
426
- #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
427
- #define ZSTD_HASHLOG3_MAX 17
428
- #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
429
- #define ZSTD_SEARCHLOG_MIN 1
430
- #define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
431
- #define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */
432
- #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX
433
- #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */
434
- #define ZSTD_LDM_MINMATCH_MAX 4096
435
- #define ZSTD_LDM_MINMATCH_MIN 4
436
- #define ZSTD_LDM_BUCKETSIZELOG_MAX 8
437
-
438
- #define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size to know frame header size */
439
- #define ZSTD_FRAMEHEADERSIZE_MIN 6
440
- #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
441
- static const size_t ZSTD_frameHeaderSize_prefix = ZSTD_FRAMEHEADERSIZE_PREFIX;
442
- static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN;
443
- static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
444
- static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */
445
-
414
+ #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
415
+ #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
446
416
 
447
417
 
448
- /* --- Advanced types --- */
449
- typedef enum { ZSTD_fast=1, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2,
450
- ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; /* from faster to stronger */
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
+ * ***************************************************************************************/
451
428
 
452
- typedef struct {
453
- unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
454
- unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
455
- unsigned hashLog; /**< dispatch table : larger == faster, more memory */
456
- unsigned searchLog; /**< nb of searches : larger == more compression, slower */
457
- unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */
458
- unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
459
- ZSTD_strategy strategy;
460
- } ZSTD_compressionParameters;
461
429
 
462
- typedef struct {
463
- unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */
464
- unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */
465
- unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */
466
- } ZSTD_frameParameters;
430
+ /* === Constants === */
467
431
 
468
- typedef struct {
469
- ZSTD_compressionParameters cParams;
470
- ZSTD_frameParameters fParams;
471
- } ZSTD_parameters;
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
472
437
 
473
- typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;
438
+ #define ZSTD_BLOCKSIZELOG_MAX 17
439
+ #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX)
474
440
 
475
- typedef enum {
476
- ZSTD_dct_auto=0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */
477
- ZSTD_dct_rawContent, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */
478
- ZSTD_dct_fullDict /* refuses to load a dictionary if it does not respect Zstandard's specification */
479
- } ZSTD_dictContentType_e;
480
441
 
481
- typedef enum {
482
- ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */
483
- ZSTD_dlm_byRef, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
484
- } ZSTD_dictLoadMethod_e;
442
+ /* === query limits === */
485
443
 
444
+ ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */
486
445
 
487
446
 
488
- /***************************************
489
- * Frame size functions
490
- ***************************************/
447
+ /* === frame size === */
491
448
 
492
449
  /*! ZSTD_findFrameCompressedSize() :
493
- * `src` should point to the start of a ZSTD encoded frame or skippable frame
494
- * `srcSize` must be >= first frame size
495
- * @return : the compressed size of the first frame starting at `src`,
496
- * suitable to pass to `ZSTD_decompress` or similar,
497
- * or an error code if input is invalid */
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 */
498
455
  ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize);
499
456
 
500
- /*! ZSTD_findDecompressedSize() :
501
- * `src` should point the start of a series of ZSTD encoded and/or skippable frames
502
- * `srcSize` must be the _exact_ size of this series
503
- * (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`)
504
- * @return : - decompressed size of all data in all successive frames
505
- * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
506
- * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
507
- *
508
- * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
509
- * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
510
- * In which case, it's necessary to use streaming mode to decompress data.
511
- * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
512
- * note 3 : decompressed size can be very large (64-bits value),
513
- * potentially larger than what local system can handle as a single memory segment.
514
- * In which case, it's necessary to use streaming mode to decompress data.
515
- * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
516
- * Always ensure result fits within application's authorized limits.
517
- * Each application can set its own limits.
518
- * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
519
- * read each contained frame header. This is fast as most of the data is skipped,
520
- * however it does mean that all frame data must be present and valid. */
521
- ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
522
-
523
- /*! ZSTD_frameHeaderSize() :
524
- * srcSize must be >= ZSTD_frameHeaderSize_prefix.
525
- * @return : size of the Frame Header,
526
- * or an error code (if srcSize is too small) */
527
- ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
528
-
529
457
 
530
- /***************************************
531
- * Memory management
532
- ***************************************/
458
+ /* === Memory management === */
533
459
 
534
460
  /*! ZSTD_sizeof_*() :
535
- * These functions give the current memory usage of selected object.
536
- * Object memory usage can evolve when re-used. */
461
+ * These functions give the _current_ memory usage of selected object.
462
+ * Note that object memory usage can evolve (increase or decrease) over time. */
537
463
  ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
538
464
  ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
539
465
  ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
@@ -541,945 +467,1259 @@ ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
541
467
  ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
542
468
  ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
543
469
 
544
- /*! ZSTD_estimate*() :
545
- * These functions make it possible to estimate memory usage
546
- * of a future {D,C}Ctx, before its creation.
547
- * ZSTD_estimateCCtxSize() will provide a budget large enough for any compression level up to selected one.
548
- * It will also consider src size to be arbitrarily "large", which is worst case.
549
- * If srcSize is known to always be small, ZSTD_estimateCCtxSize_usingCParams() can provide a tighter estimation.
550
- * ZSTD_estimateCCtxSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
551
- * 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_p_nbWorkers is >= 1.
552
- * Note : CCtx size estimation is only correct for single-threaded compression. */
553
- ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
554
- ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
555
- ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
556
- ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
557
470
 
558
- /*! ZSTD_estimateCStreamSize() :
559
- * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
560
- * It will also consider src size to be arbitrarily "large", which is worst case.
561
- * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
562
- * ZSTD_estimateCStreamSize_usingCParams() can be used in tandem with ZSTD_getCParams() to create cParams from compressionLevel.
563
- * 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_p_nbWorkers is >= 1.
564
- * Note : CStream size estimation is only correct for single-threaded compression.
565
- * ZSTD_DStream memory budget depends on window Size.
566
- * This information can be passed manually, using ZSTD_estimateDStreamSize,
567
- * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
568
- * Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
569
- * an internal ?Dict will be created, which additional size is not estimated here.
570
- * In this case, get total size by adding ZSTD_estimate?DictSize */
571
- ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel);
572
- ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams);
573
- ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params);
574
- ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize);
575
- ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize);
471
+ /***************************************
472
+ * Advanced compression API
473
+ ***************************************/
576
474
 
577
- /*! ZSTD_estimate?DictSize() :
578
- * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict().
579
- * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced().
580
- * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller.
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.
581
486
  */
582
- ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel);
583
- ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod);
584
- ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod);
585
487
 
586
- /*! ZSTD_initStatic*() :
587
- * Initialize an object using a pre-allocated fixed-size buffer.
588
- * workspace: The memory area to emplace the object into.
589
- * Provided pointer *must be 8-bytes aligned*.
590
- * Buffer must outlive object.
591
- * workspaceSize: Use ZSTD_estimate*Size() to determine
592
- * how large workspace must be to support target scenario.
593
- * @return : pointer to object (same address as workspace, just different type),
594
- * or NULL if error (size too small, incorrect alignment, etc.)
595
- * Note : zstd will never resize nor malloc() when using a static buffer.
596
- * If the object requires more memory than available,
597
- * zstd will just error out (typically ZSTD_error_memory_allocation).
598
- * Note 2 : there is no corresponding "free" function.
599
- * Since workspace is allocated externally, it must be freed externally too.
600
- * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level
601
- * into its associated cParams.
602
- * Limitation 1 : currently not compatible with internal dictionary creation, triggered by
603
- * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict().
604
- * Limitation 2 : static cctx currently not compatible with multi-threading.
605
- * Limitation 3 : static dctx is incompatible with legacy support.
606
- */
607
- ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);
608
- ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */
609
488
 
610
- ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);
611
- ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */
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;
612
502
 
613
- ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict(
614
- void* workspace, size_t workspaceSize,
615
- const void* dict, size_t dictSize,
616
- ZSTD_dictLoadMethod_e dictLoadMethod,
617
- ZSTD_dictContentType_e dictContentType,
618
- ZSTD_compressionParameters cParams);
619
503
 
620
- ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict(
621
- void* workspace, size_t workspaceSize,
622
- const void* dict, size_t dictSize,
623
- ZSTD_dictLoadMethod_e dictLoadMethod,
624
- ZSTD_dictContentType_e dictContentType);
504
+ typedef enum {
625
505
 
626
- /*! Custom memory allocation :
627
- * These prototypes make it possible to pass your own allocation/free functions.
628
- * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below.
629
- * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.
630
- */
631
- typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
632
- typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
633
- typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
634
- static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */
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". */
635
555
 
636
- ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
637
- ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
638
- ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
639
- ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
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". */
640
583
 
641
- ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
642
- ZSTD_dictLoadMethod_e dictLoadMethod,
643
- ZSTD_dictContentType_e dictContentType,
644
- ZSTD_compressionParameters cParams,
645
- ZSTD_customMem customMem);
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) */
646
591
 
647
- ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
648
- ZSTD_dictLoadMethod_e dictLoadMethod,
649
- ZSTD_dictContentType_e dictContentType,
650
- ZSTD_customMem customMem);
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 */
620
+
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;
651
637
 
652
638
 
639
+ typedef struct {
640
+ size_t error;
641
+ int lowerBound;
642
+ int upperBound;
643
+ } ZSTD_bounds;
644
+
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);
653
653
 
654
- /***************************************
655
- * Advanced compression functions
656
- ***************************************/
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);
657
666
 
658
- /*! ZSTD_createCDict_byReference() :
659
- * Create a digested dictionary for compression
660
- * Dictionary content is simply referenced, and therefore stays in dictBuffer.
661
- * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */
662
- ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
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);
663
683
 
664
- /*! ZSTD_getCParams() :
665
- * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
666
- * `estimatedSrcSize` value is optional, select 0 if not known */
667
- ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
684
+ /*! ZSTD_CCtx_loadDictionary() :
685
+ * Create an internal CDict from `dict` buffer.
686
+ * Decompression will have to use same dictionary.
687
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
688
+ * Special: Loading a NULL (or 0-size) dictionary invalidates previous dictionary,
689
+ * meaning "return to no-dictionary mode".
690
+ * Note 1 : Dictionary is sticky, it will be used for all future compressed frames.
691
+ * To return to "no-dictionary" situation, load a NULL dictionary (or reset parameters).
692
+ * Note 2 : Loading a dictionary involves building tables.
693
+ * It's also a CPU consuming operation, with non-negligible impact on latency.
694
+ * Tables are dependent on compression parameters, and for this reason,
695
+ * compression parameters can no longer be changed after loading a dictionary.
696
+ * Note 3 :`dict` content will be copied internally.
697
+ * Use experimental ZSTD_CCtx_loadDictionary_byReference() to reference content instead.
698
+ * In such a case, dictionary buffer must outlive its users.
699
+ * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced()
700
+ * to precisely select how dictionary content must be interpreted. */
701
+ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
668
702
 
669
- /*! ZSTD_getParams() :
670
- * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
671
- * All fields of `ZSTD_frameParameters` are set to default : contentSize=1, checksum=0, noDictID=0 */
672
- ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
703
+ /*! ZSTD_CCtx_refCDict() :
704
+ * Reference a prepared dictionary, to be used for all next compressed frames.
705
+ * Note that compression parameters are enforced from within CDict,
706
+ * and supercede any compression parameter previously set within CCtx.
707
+ * The dictionary will remain valid for future compressed frames using same CCtx.
708
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
709
+ * Special : Referencing a NULL CDict means "return to no-dictionary mode".
710
+ * Note 1 : Currently, only one dictionary can be managed.
711
+ * Referencing a new dictionary effectively "discards" any previous one.
712
+ * Note 2 : CDict is just referenced, its lifetime must outlive its usage within CCtx. */
713
+ ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
673
714
 
674
- /*! ZSTD_checkCParams() :
675
- * Ensure param values remain within authorized range */
676
- ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
715
+ /*! ZSTD_CCtx_refPrefix() :
716
+ * Reference a prefix (single-usage dictionary) for next compressed frame.
717
+ * A prefix is **only used once**. Tables are discarded at end of frame (ZSTD_e_end).
718
+ * Decompression will need same prefix to properly regenerate data.
719
+ * Compressing with a prefix is similar in outcome as performing a diff and compressing it,
720
+ * but performs much faster, especially during decompression (compression speed is tunable with compression level).
721
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
722
+ * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary
723
+ * Note 1 : Prefix buffer is referenced. It **must** outlive compression.
724
+ * Its content must remain unmodified during compression.
725
+ * Note 2 : If the intention is to diff some large src data blob with some prior version of itself,
726
+ * ensure that the window size is large enough to contain the entire source.
727
+ * See ZSTD_c_windowLog.
728
+ * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.
729
+ * It's a CPU consuming operation, with non-negligible impact on latency.
730
+ * 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).
732
+ * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */
733
+ ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
734
+ const void* prefix, size_t prefixSize);
677
735
 
678
- /*! ZSTD_adjustCParams() :
679
- * optimize params for a given `srcSize` and `dictSize`.
680
- * both values are optional, select `0` if unknown. */
681
- ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
682
736
 
683
- /*! ZSTD_compress_advanced() :
684
- * Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */
685
- ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx,
686
- void* dst, size_t dstCapacity,
687
- const void* src, size_t srcSize,
688
- const void* dict,size_t dictSize,
689
- ZSTD_parameters params);
737
+ typedef enum {
738
+ ZSTD_reset_session_only = 1,
739
+ ZSTD_reset_parameters = 2,
740
+ ZSTD_reset_session_and_parameters = 3
741
+ } ZSTD_ResetDirective;
690
742
 
691
- /*! ZSTD_compress_usingCDict_advanced() :
692
- * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
693
- ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
694
- void* dst, size_t dstCapacity,
695
- const void* src, size_t srcSize,
696
- const ZSTD_CDict* cdict, ZSTD_frameParameters fParams);
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);
697
758
 
698
759
 
699
- /*--- Advanced decompression functions ---*/
700
760
 
701
- /*! ZSTD_isFrame() :
702
- * Tells if the content of `buffer` starts with a valid Frame Identifier.
703
- * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
704
- * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
705
- * Note 3 : Skippable Frame Identifiers are considered valid. */
706
- ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
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);
707
774
 
708
- /*! ZSTD_createDDict_byReference() :
709
- * Create a digested dictionary, ready to start decompression operation without startup delay.
710
- * Dictionary content is referenced, and therefore stays in dictBuffer.
711
- * It is important that dictBuffer outlives DDict,
712
- * it must remain read accessible throughout the lifetime of DDict */
713
- ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
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;
714
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);
715
811
 
716
- /*! ZSTD_getDictID_fromDict() :
717
- * Provides the dictID stored within dictionary.
718
- * if @return == 0, the dictionary is not conformant with Zstandard specification.
719
- * It can still be loaded, but as a content-only dictionary. */
720
- ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
721
812
 
722
- /*! ZSTD_getDictID_fromDDict() :
723
- * Provides the dictID of the dictionary loaded into `ddict`.
724
- * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
725
- * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
726
- ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
727
813
 
728
- /*! ZSTD_getDictID_fromFrame() :
729
- * Provides the dictID required to decompressed the frame stored within `src`.
730
- * If @return == 0, the dictID could not be decoded.
731
- * This could for one of the following reasons :
732
- * - The frame does not require a dictionary to be decoded (most common case).
733
- * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
734
- * Note : this use case also happens when using a non-conformant dictionary.
735
- * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
736
- * - This is not a Zstandard frame.
737
- * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */
738
- ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
814
+ /* ============================== */
815
+ /* Advanced decompression API */
816
+ /* ============================== */
739
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
+ */
740
825
 
741
- /********************************************************************
742
- * Advanced streaming functions
743
- ********************************************************************/
744
826
 
745
- /*===== Advanced Streaming compression functions =====*/
746
- 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. */
747
- 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.*/
748
- ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
749
- 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. */
750
- 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 */
751
- 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. */
827
+ typedef enum {
752
828
 
753
- /*! ZSTD_resetCStream() :
754
- * start a new compression job, using same parameters from previous job.
755
- * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
756
- * Note that zcs must be init at least once before using ZSTD_resetCStream().
757
- * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
758
- * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
759
- * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs,
760
- * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.
761
- * @return : 0, or an error code (which can be tested using ZSTD_isError())
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
762
853
  */
763
- ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
854
+ ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam);
764
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);
765
864
 
766
- typedef struct {
767
- unsigned long long ingested; /* nb input bytes read and buffered */
768
- unsigned long long consumed; /* nb input bytes actually compressed */
769
- unsigned long long produced; /* nb of compressed bytes generated and buffered */
770
- unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */
771
- unsigned currentJobID; /* MT only : latest started job nb */
772
- unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */
773
- } ZSTD_frameProgression;
774
865
 
775
- /* ZSTD_getFrameProgression() :
776
- * tells how much data has been ingested (read from input)
777
- * consumed (input actually compressed) and produced (output) for current frame.
778
- * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed.
779
- * Aggregates progression inside active worker threads.
866
+ /*! ZSTD_DCtx_loadDictionary() :
867
+ * Create an internal DDict from dict buffer,
868
+ * to be used to decompress next frames.
869
+ * The dictionary remains valid for all future frames, until explicitly invalidated.
870
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
871
+ * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,
872
+ * meaning "return to no-dictionary mode".
873
+ * Note 1 : Loading a dictionary involves building tables,
874
+ * which has a non-negligible impact on CPU usage and latency.
875
+ * It's recommended to "load once, use many times", to amortize the cost
876
+ * Note 2 :`dict` content will be copied internally, so `dict` can be released after loading.
877
+ * Use ZSTD_DCtx_loadDictionary_byReference() to reference dictionary content instead.
878
+ * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to take control of
879
+ * how dictionary content is loaded and interpreted.
780
880
  */
781
- ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx);
881
+ ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
782
882
 
783
- /*! ZSTD_toFlushNow() :
784
- * Tell how many bytes are ready to be flushed immediately.
785
- * Useful for multithreading scenarios (nbWorkers >= 1).
786
- * Probe the oldest active job, defined as oldest job not yet entirely flushed,
787
- * and check its output buffer.
788
- * @return : amount of data stored in oldest job and ready to be flushed immediately.
789
- * if @return == 0, it means either :
790
- * + there is no active job (could be checked with ZSTD_frameProgression()), or
791
- * + oldest job is still actively compressing data,
792
- * but everything it has produced has also been flushed so far,
793
- * therefore flushing speed is currently limited by production speed of oldest job
794
- * irrespective of the speed of concurrent newer jobs.
883
+ /*! ZSTD_DCtx_refDDict() :
884
+ * Reference a prepared dictionary, to be used to decompress next frames.
885
+ * The dictionary remains active for decompression of future frames using same DCtx.
886
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
887
+ * Note 1 : Currently, only one dictionary can be managed.
888
+ * Referencing a new dictionary effectively "discards" any previous one.
889
+ * Special: referencing a NULL DDict means "return to no-dictionary mode".
890
+ * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx.
795
891
  */
796
- ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
892
+ ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
797
893
 
894
+ /*! ZSTD_DCtx_refPrefix() :
895
+ * Reference a prefix (single-usage dictionary) to decompress next frame.
896
+ * This is the reverse operation of ZSTD_CCtx_refPrefix(),
897
+ * and must use the same prefix as the one used during compression.
898
+ * Prefix is **only used once**. Reference is discarded at end of frame.
899
+ * End of frame is reached when ZSTD_decompressStream() returns 0.
900
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
901
+ * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary
902
+ * Note 2 : Prefix buffer is referenced. It **must** outlive decompression.
903
+ * Prefix buffer must remain unmodified up to the end of frame,
904
+ * reached when ZSTD_decompressStream() returns 0.
905
+ * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
906
+ * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode (Experimental section)
907
+ * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.
908
+ * A full dictionary is more costly, as it requires building tables.
909
+ */
910
+ ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
911
+ const void* prefix, size_t prefixSize);
798
912
 
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);
799
920
 
800
- /*===== Advanced Streaming decompression functions =====*/
801
- typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e;
802
- ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); /* obsolete : this API will be removed in a future version */
803
- 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 */
804
- ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict is referenced, it must outlive decompression session */
805
- ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */
806
921
 
807
922
 
808
- /*********************************************************************
809
- * Buffer-less and synchronous inner streaming functions
810
- *
811
- * This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
812
- * But it's also a complex one, with several restrictions, documented below.
813
- * Prefer normal streaming API for an easier experience.
814
- ********************************************************************* */
923
+ /****************************************************************************************
924
+ * experimental API (static linking only)
925
+ ****************************************************************************************
926
+ * The following symbols and constants
927
+ * are not planned to join "stable API" status in the near future.
928
+ * They can still change in future versions.
929
+ * Some of them are planned to remain in the static_only section indefinitely.
930
+ * Some of them might be removed in the future (especially when redundant with existing stable functions)
931
+ * ***************************************************************************************/
815
932
 
816
- /**
817
- Buffer-less streaming compression (synchronous mode)
933
+ #define ZSTD_FRAMEHEADERSIZE_PREFIX 5 /* minimum input size required to query frame header size */
934
+ #define ZSTD_FRAMEHEADERSIZE_MIN 6
935
+ #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* can be useful for static allocation */
936
+ #define ZSTD_SKIPPABLEHEADERSIZE 8
937
+
938
+ /* compression parameter bounds */
939
+ #define ZSTD_WINDOWLOG_MAX_32 30
940
+ #define ZSTD_WINDOWLOG_MAX_64 31
941
+ #define ZSTD_WINDOWLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
942
+ #define ZSTD_WINDOWLOG_MIN 10
943
+ #define ZSTD_HASHLOG_MAX ((ZSTD_WINDOWLOG_MAX < 30) ? ZSTD_WINDOWLOG_MAX : 30)
944
+ #define ZSTD_HASHLOG_MIN 6
945
+ #define ZSTD_CHAINLOG_MAX_32 29
946
+ #define ZSTD_CHAINLOG_MAX_64 30
947
+ #define ZSTD_CHAINLOG_MAX ((int)(sizeof(size_t) == 4 ? ZSTD_CHAINLOG_MAX_32 : ZSTD_CHAINLOG_MAX_64))
948
+ #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
949
+ #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
950
+ #define ZSTD_SEARCHLOG_MIN 1
951
+ #define ZSTD_MINMATCH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
952
+ #define ZSTD_MINMATCH_MIN 3 /* only for ZSTD_btopt+, faster strategies are limited to 4 */
953
+ #define ZSTD_TARGETLENGTH_MAX ZSTD_BLOCKSIZE_MAX
954
+ #define ZSTD_TARGETLENGTH_MIN 0 /* note : comparing this constant to an unsigned results in a tautological test */
955
+ #define ZSTD_STRATEGY_MIN ZSTD_fast
956
+ #define ZSTD_STRATEGY_MAX ZSTD_btultra2
957
+
958
+
959
+ #define ZSTD_OVERLAPLOG_MIN 0
960
+ #define ZSTD_OVERLAPLOG_MAX 9
961
+
962
+ #define ZSTD_WINDOWLOG_LIMIT_DEFAULT 27 /* by default, the streaming decoder will refuse any frame
963
+ * requiring larger than (1<<ZSTD_WINDOWLOG_LIMIT_DEFAULT) window size,
964
+ * to preserve host's memory from unreasonable requirements.
965
+ * This limit can be overriden using ZSTD_DCtx_setParameter(,ZSTD_d_windowLogMax,).
966
+ * The limit does not apply for one-pass decoders (such as ZSTD_decompress()), since no additional memory is allocated */
967
+
968
+
969
+ /* LDM parameter bounds */
970
+ #define ZSTD_LDM_HASHLOG_MIN ZSTD_HASHLOG_MIN
971
+ #define ZSTD_LDM_HASHLOG_MAX ZSTD_HASHLOG_MAX
972
+ #define ZSTD_LDM_MINMATCH_MIN 4
973
+ #define ZSTD_LDM_MINMATCH_MAX 4096
974
+ #define ZSTD_LDM_BUCKETSIZELOG_MIN 1
975
+ #define ZSTD_LDM_BUCKETSIZELOG_MAX 8
976
+ #define ZSTD_LDM_HASHRATELOG_MIN 0
977
+ #define ZSTD_LDM_HASHRATELOG_MAX (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN)
978
+
979
+ /* internal */
980
+ #define ZSTD_HASHLOG3_MAX 17
818
981
 
819
- A ZSTD_CCtx object is required to track streaming operations.
820
- Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
821
- ZSTD_CCtx object can be re-used multiple times within successive compression operations.
822
982
 
823
- Start by initializing a context.
824
- Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
825
- or ZSTD_compressBegin_advanced(), for finer parameter control.
826
- It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
983
+ /* --- Advanced types --- */
827
984
 
828
- Then, consume your input using ZSTD_compressContinue().
829
- There are some important considerations to keep in mind when using this advanced function :
830
- - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only.
831
- - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks.
832
- - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
833
- Worst case evaluation is provided by ZSTD_compressBound().
834
- ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
835
- - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
836
- It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
837
- - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
838
- In which case, it will "discard" the relevant memory section from its history.
985
+ typedef struct ZSTD_CCtx_params_s ZSTD_CCtx_params;
839
986
 
840
- Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
841
- It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
842
- Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders.
987
+ typedef struct {
988
+ unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
989
+ unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
990
+ unsigned hashLog; /**< dispatch table : larger == faster, more memory */
991
+ unsigned searchLog; /**< nb of searches : larger == more compression, slower */
992
+ unsigned minMatch; /**< match length searched : larger == faster decompression, sometimes less compression */
993
+ unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
994
+ ZSTD_strategy strategy; /**< see ZSTD_strategy definition above */
995
+ } ZSTD_compressionParameters;
843
996
 
844
- `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again.
845
- */
997
+ typedef struct {
998
+ int contentSizeFlag; /**< 1: content size will be in frame header (when known) */
999
+ int checksumFlag; /**< 1: generate a 32-bits checksum using XXH64 algorithm at end of frame, for error detection */
1000
+ int noDictIDFlag; /**< 1: no dictID will be saved into frame header (dictID is only useful for dictionary compression) */
1001
+ } ZSTD_frameParameters;
846
1002
 
847
- /*===== Buffer-less streaming compression functions =====*/
848
- ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
849
- ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
850
- ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */
851
- ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */
852
- ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */
853
- ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */
1003
+ typedef struct {
1004
+ ZSTD_compressionParameters cParams;
1005
+ ZSTD_frameParameters fParams;
1006
+ } ZSTD_parameters;
854
1007
 
855
- ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
856
- ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1008
+ typedef enum {
1009
+ ZSTD_dct_auto = 0, /* dictionary is "full" when starting with ZSTD_MAGIC_DICTIONARY, otherwise it is "rawContent" */
1010
+ ZSTD_dct_rawContent = 1, /* ensures dictionary is always loaded as rawContent, even if it starts with ZSTD_MAGIC_DICTIONARY */
1011
+ ZSTD_dct_fullDict = 2 /* refuses to load a dictionary if it does not respect Zstandard's specification, starting with ZSTD_MAGIC_DICTIONARY */
1012
+ } ZSTD_dictContentType_e;
857
1013
 
1014
+ typedef enum {
1015
+ ZSTD_dlm_byCopy = 0, /**< Copy dictionary content internally */
1016
+ ZSTD_dlm_byRef = 1, /**< Reference dictionary content -- the dictionary buffer must outlive its users. */
1017
+ } ZSTD_dictLoadMethod_e;
858
1018
 
859
- /*-
860
- Buffer-less streaming decompression (synchronous mode)
1019
+ 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
+ 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.
1031
+ * Useful to save 4 bytes per generated frame.
1032
+ * Decoder cannot recognise automatically this format, requiring this instruction. */
1033
+ } ZSTD_format_e;
861
1034
 
862
- A ZSTD_DCtx object is required to track streaming operations.
863
- Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
864
- A ZSTD_DCtx object can be re-used multiple times.
1035
+ typedef enum {
1036
+ /* Note: this enum and the behavior it controls are effectively internal
1037
+ * implementation details of the compressor. They are expected to continue
1038
+ * to evolve and should be considered only in the context of extremely
1039
+ * advanced performance tuning.
1040
+ *
1041
+ * Zstd currently supports the use of a CDict in two ways:
1042
+ *
1043
+ * - The contents of the CDict can be copied into the working context. This
1044
+ * means that the compression can search both the dictionary and input
1045
+ * while operating on a single set of internal tables. This makes
1046
+ * the compression faster per-byte of input. However, the initial copy of
1047
+ * the CDict's tables incurs a fixed cost at the beginning of the
1048
+ * compression. For small compressions (< 8 KB), that copy can dominate
1049
+ * the cost of the compression.
1050
+ *
1051
+ * - The CDict's tables can be used in-place. In this model, compression is
1052
+ * slower per input byte, because the compressor has to search two sets of
1053
+ * tables. However, this model incurs no start-up cost (as long as the
1054
+ * working context's tables can be reused). For small inputs, this can be
1055
+ * faster than copying the CDict's tables.
1056
+ *
1057
+ * Zstd has a simple internal heuristic that selects which strategy to use
1058
+ * at the beginning of a compression. However, if experimentation shows that
1059
+ * Zstd is making poor choices, it is possible to override that choice with
1060
+ * this enum.
1061
+ */
1062
+ ZSTD_dictDefaultAttach = 0, /* Use the default heuristic. */
1063
+ ZSTD_dictForceAttach = 1, /* Never copy the dictionary. */
1064
+ ZSTD_dictForceCopy = 2, /* Always copy the dictionary. */
1065
+ } ZSTD_dictAttachPref_e;
865
1066
 
866
- First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().
867
- Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough.
868
- Data fragment must be large enough to ensure successful decoding.
869
- `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough.
870
- @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled.
871
- >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
872
- errorCode, which can be tested using ZSTD_isError().
873
1067
 
874
- It fills a ZSTD_frameHeader structure with important information to correctly decode the frame,
875
- such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`).
876
- Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information.
877
- As a consequence, check that values remain within valid application range.
878
- For example, do not allocate memory blindly, check that `windowSize` is within expectation.
879
- Each application can set its own limits, depending on local restrictions.
880
- For extended interoperability, it is recommended to support `windowSize` of at least 8 MB.
1068
+ /***************************************
1069
+ * Frame size functions
1070
+ ***************************************/
881
1071
 
882
- ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes.
883
- ZSTD_decompressContinue() is very sensitive to contiguity,
884
- if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
885
- or that previous contiguous segment is large enough to properly handle maximum back-reference distance.
886
- There are multiple ways to guarantee this condition.
1072
+ /*! ZSTD_findDecompressedSize() :
1073
+ * `src` should point the start of a series of ZSTD encoded and/or skippable frames
1074
+ * `srcSize` must be the _exact_ size of this series
1075
+ * (i.e. there should be a frame boundary exactly at `srcSize` bytes after `src`)
1076
+ * @return : - decompressed size of all data in all successive frames
1077
+ * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN
1078
+ * - if an error occurred: ZSTD_CONTENTSIZE_ERROR
1079
+ *
1080
+ * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
1081
+ * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size.
1082
+ * In which case, it's necessary to use streaming mode to decompress data.
1083
+ * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
1084
+ * note 3 : decompressed size can be very large (64-bits value),
1085
+ * potentially larger than what local system can handle as a single memory segment.
1086
+ * In which case, it's necessary to use streaming mode to decompress data.
1087
+ * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
1088
+ * Always ensure result fits within application's authorized limits.
1089
+ * Each application can set its own limits.
1090
+ * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to
1091
+ * read each contained frame header. This is fast as most of the data is skipped,
1092
+ * however it does mean that all frame data must be present and valid. */
1093
+ ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize);
887
1094
 
888
- The most memory efficient way is to use a round buffer of sufficient size.
889
- Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(),
890
- which can @return an error code if required value is too large for current system (in 32-bits mode).
891
- In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one,
892
- up to the moment there is not enough room left in the buffer to guarantee decoding another full block,
893
- which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`.
894
- At which point, decoding can resume from the beginning of the buffer.
895
- Note that already decoded data stored in the buffer should be flushed before being overwritten.
1095
+ /*! ZSTD_frameHeaderSize() :
1096
+ * srcSize must be >= ZSTD_FRAMEHEADERSIZE_PREFIX.
1097
+ * @return : size of the Frame Header,
1098
+ * or an error code (if srcSize is too small) */
1099
+ ZSTDLIB_API size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize);
896
1100
 
897
- There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory.
898
1101
 
899
- Finally, if you control the compression process, you can also ignore all buffer size rules,
900
- as long as the encoder and decoder progress in "lock-step",
901
- aka use exactly the same buffer sizes, break contiguity at the same place, etc.
1102
+ /***************************************
1103
+ * Memory management
1104
+ ***************************************/
902
1105
 
903
- Once buffers are setup, start decompression, with ZSTD_decompressBegin().
904
- If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().
1106
+ /*! ZSTD_estimate*() :
1107
+ * These functions make it possible to estimate memory usage
1108
+ * 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. */
1115
+ ZSTDLIB_API size_t ZSTD_estimateCCtxSize(int compressionLevel);
1116
+ ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCParams(ZSTD_compressionParameters cParams);
1117
+ ZSTDLIB_API size_t ZSTD_estimateCCtxSize_usingCCtxParams(const ZSTD_CCtx_params* params);
1118
+ ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
905
1119
 
906
- Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
907
- ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
908
- ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
1120
+ /*! ZSTD_estimateCStreamSize() :
1121
+ * ZSTD_estimateCStreamSize() will provide a budget large enough for any compression level up to selected one.
1122
+ * It will also consider src size to be arbitrarily "large", which is worst case.
1123
+ * If srcSize is known to always be small, ZSTD_estimateCStreamSize_usingCParams() can provide a tighter estimation.
1124
+ * 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.
1126
+ * Note : CStream size estimation is only correct for single-threaded compression.
1127
+ * ZSTD_DStream memory budget depends on window Size.
1128
+ * This information can be passed manually, using ZSTD_estimateDStreamSize,
1129
+ * or deducted from a valid frame Header, using ZSTD_estimateDStreamSize_fromFrame();
1130
+ * Note : if streaming is init with function ZSTD_init?Stream_usingDict(),
1131
+ * an internal ?Dict will be created, which additional size is not estimated here.
1132
+ * In this case, get total size by adding ZSTD_estimate?DictSize */
1133
+ ZSTDLIB_API size_t ZSTD_estimateCStreamSize(int compressionLevel);
1134
+ ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCParams(ZSTD_compressionParameters cParams);
1135
+ ZSTDLIB_API size_t ZSTD_estimateCStreamSize_usingCCtxParams(const ZSTD_CCtx_params* params);
1136
+ ZSTDLIB_API size_t ZSTD_estimateDStreamSize(size_t windowSize);
1137
+ ZSTDLIB_API size_t ZSTD_estimateDStreamSize_fromFrame(const void* src, size_t srcSize);
909
1138
 
910
- @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
911
- It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item.
912
- It can also be an error code, which can be tested with ZSTD_isError().
1139
+ /*! ZSTD_estimate?DictSize() :
1140
+ * ZSTD_estimateCDictSize() will bet that src size is relatively "small", and content is copied, like ZSTD_createCDict().
1141
+ * ZSTD_estimateCDictSize_advanced() makes it possible to control compression parameters precisely, like ZSTD_createCDict_advanced().
1142
+ * Note : dictionaries created by reference (`ZSTD_dlm_byRef`) are logically smaller.
1143
+ */
1144
+ ZSTDLIB_API size_t ZSTD_estimateCDictSize(size_t dictSize, int compressionLevel);
1145
+ ZSTDLIB_API size_t ZSTD_estimateCDictSize_advanced(size_t dictSize, ZSTD_compressionParameters cParams, ZSTD_dictLoadMethod_e dictLoadMethod);
1146
+ ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod);
913
1147
 
914
- A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
915
- Context can then be reset to start a new decompression.
1148
+ /*! ZSTD_initStatic*() :
1149
+ * Initialize an object using a pre-allocated fixed-size buffer.
1150
+ * workspace: The memory area to emplace the object into.
1151
+ * Provided pointer *must be 8-bytes aligned*.
1152
+ * Buffer must outlive object.
1153
+ * workspaceSize: Use ZSTD_estimate*Size() to determine
1154
+ * how large workspace must be to support target scenario.
1155
+ * @return : pointer to object (same address as workspace, just different type),
1156
+ * or NULL if error (size too small, incorrect alignment, etc.)
1157
+ * Note : zstd will never resize nor malloc() when using a static buffer.
1158
+ * If the object requires more memory than available,
1159
+ * zstd will just error out (typically ZSTD_error_memory_allocation).
1160
+ * Note 2 : there is no corresponding "free" function.
1161
+ * Since workspace is allocated externally, it must be freed externally too.
1162
+ * Note 3 : cParams : use ZSTD_getCParams() to convert a compression level
1163
+ * into its associated cParams.
1164
+ * Limitation 1 : currently not compatible with internal dictionary creation, triggered by
1165
+ * ZSTD_CCtx_loadDictionary(), ZSTD_initCStream_usingDict() or ZSTD_initDStream_usingDict().
1166
+ * Limitation 2 : static cctx currently not compatible with multi-threading.
1167
+ * Limitation 3 : static dctx is incompatible with legacy support.
1168
+ */
1169
+ ZSTDLIB_API ZSTD_CCtx* ZSTD_initStaticCCtx(void* workspace, size_t workspaceSize);
1170
+ ZSTDLIB_API ZSTD_CStream* ZSTD_initStaticCStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticCCtx() */
916
1171
 
917
- Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
918
- This information is not required to properly decode a frame.
1172
+ ZSTDLIB_API ZSTD_DCtx* ZSTD_initStaticDCtx(void* workspace, size_t workspaceSize);
1173
+ ZSTDLIB_API ZSTD_DStream* ZSTD_initStaticDStream(void* workspace, size_t workspaceSize); /**< same as ZSTD_initStaticDCtx() */
919
1174
 
920
- == Special case : skippable frames ==
1175
+ ZSTDLIB_API const ZSTD_CDict* ZSTD_initStaticCDict(
1176
+ void* workspace, size_t workspaceSize,
1177
+ const void* dict, size_t dictSize,
1178
+ ZSTD_dictLoadMethod_e dictLoadMethod,
1179
+ ZSTD_dictContentType_e dictContentType,
1180
+ ZSTD_compressionParameters cParams);
921
1181
 
922
- Skippable frames allow integration of user-defined data into a flow of concatenated frames.
923
- Skippable frames will be ignored (skipped) by decompressor.
924
- The format of skippable frames is as follows :
925
- a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
926
- b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
927
- c) Frame Content - any content (User Data) of length equal to Frame Size
928
- For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame.
929
- For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content.
930
- */
1182
+ ZSTDLIB_API const ZSTD_DDict* ZSTD_initStaticDDict(
1183
+ void* workspace, size_t workspaceSize,
1184
+ const void* dict, size_t dictSize,
1185
+ ZSTD_dictLoadMethod_e dictLoadMethod,
1186
+ ZSTD_dictContentType_e dictContentType);
931
1187
 
932
- /*===== Buffer-less streaming decompression functions =====*/
933
- typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e;
934
- typedef struct {
935
- unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */
936
- unsigned long long windowSize; /* can be very large, up to <= frameContentSize */
937
- unsigned blockSizeMax;
938
- ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */
939
- unsigned headerSize;
940
- unsigned dictID;
941
- unsigned checksumFlag;
942
- } ZSTD_frameHeader;
943
- /** ZSTD_getFrameHeader() :
944
- * decode Frame Header, or requires larger `srcSize`.
945
- * @return : 0, `zfhPtr` is correctly filled,
946
- * >0, `srcSize` is too small, value is wanted `srcSize` amount,
947
- * or an error code, which can be tested using ZSTD_isError() */
948
- ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */
949
- ZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); /**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */
950
1188
 
951
- ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
952
- ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
953
- ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
1189
+ /*! Custom memory allocation :
1190
+ * These prototypes make it possible to pass your own allocation/free functions.
1191
+ * ZSTD_customMem is provided at creation time, using ZSTD_create*_advanced() variants listed below.
1192
+ * All allocation/free operations will be completed using these custom variants instead of regular <stdlib.h> ones.
1193
+ */
1194
+ typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
1195
+ typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
1196
+ typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
1197
+ static ZSTD_customMem const ZSTD_defaultCMem = { NULL, NULL, NULL }; /**< this constant defers to stdlib's functions */
1198
+
1199
+ ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
1200
+ ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
1201
+ ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
1202
+ ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
1203
+
1204
+ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
1205
+ ZSTD_dictLoadMethod_e dictLoadMethod,
1206
+ ZSTD_dictContentType_e dictContentType,
1207
+ ZSTD_compressionParameters cParams,
1208
+ ZSTD_customMem customMem);
1209
+
1210
+ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize,
1211
+ ZSTD_dictLoadMethod_e dictLoadMethod,
1212
+ ZSTD_dictContentType_e dictContentType,
1213
+ ZSTD_customMem customMem);
1214
+
1215
+
1216
+
1217
+ /***************************************
1218
+ * Advanced compression functions
1219
+ ***************************************/
1220
+
1221
+ /*! ZSTD_createCDict_byReference() :
1222
+ * Create a digested dictionary for compression
1223
+ * Dictionary content is just referenced, not duplicated.
1224
+ * As a consequence, `dictBuffer` **must** outlive CDict,
1225
+ * and its content must remain unmodified throughout the lifetime of CDict. */
1226
+ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel);
1227
+
1228
+ /*! 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 */
1231
+ ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
1232
+
1233
+ /*! 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 */
1236
+ ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
1237
+
1238
+ /*! ZSTD_checkCParams() :
1239
+ * Ensure param values remain within authorized range */
1240
+ ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
1241
+
1242
+ /*! ZSTD_adjustCParams() :
1243
+ * optimize params for a given `srcSize` and `dictSize`.
1244
+ * both values are optional, select `0` if unknown. */
1245
+ ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
1246
+
1247
+ /*! ZSTD_compress_advanced() :
1248
+ * Same as ZSTD_compress_usingDict(), with fine-tune control over compression parameters (by structure) */
1249
+ ZSTDLIB_API size_t ZSTD_compress_advanced(ZSTD_CCtx* cctx,
1250
+ void* dst, size_t dstCapacity,
1251
+ const void* src, size_t srcSize,
1252
+ const void* dict,size_t dictSize,
1253
+ ZSTD_parameters params);
1254
+
1255
+ /*! ZSTD_compress_usingCDict_advanced() :
1256
+ * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */
1257
+ ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx,
1258
+ void* dst, size_t dstCapacity,
1259
+ const void* src, size_t srcSize,
1260
+ const ZSTD_CDict* cdict,
1261
+ ZSTD_frameParameters fParams);
1262
+
1263
+
1264
+ /*! ZSTD_CCtx_loadDictionary_byReference() :
1265
+ * Same as ZSTD_CCtx_loadDictionary(), but dictionary content is referenced, instead of being copied into CCtx.
1266
+ * It saves some memory, but also requires that `dict` outlives its usage within `cctx` */
1267
+ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
1268
+
1269
+ /*! ZSTD_CCtx_loadDictionary_advanced() :
1270
+ * Same as ZSTD_CCtx_loadDictionary(), but gives finer control over
1271
+ * how to load the dictionary (by copy ? by reference ?)
1272
+ * and how to interpret it (automatic ? force raw mode ? full mode only ?) */
1273
+ ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
1274
+
1275
+ /*! ZSTD_CCtx_refPrefix_advanced() :
1276
+ * Same as ZSTD_CCtx_refPrefix(), but gives finer control over
1277
+ * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */
1278
+ ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);
1279
+
1280
+ /* === experimental parameters === */
1281
+ /* these parameters can be used with ZSTD_setParameter()
1282
+ * they are not guaranteed to remain supported in the future */
1283
+
1284
+ /* Enables rsyncable mode,
1285
+ * which makes compressed files more rsync friendly
1286
+ * by adding periodic synchronization points to the compressed data.
1287
+ * The target average block size is ZSTD_c_jobSize / 2.
1288
+ * It's possible to modify the job size to increase or decrease
1289
+ * the granularity of the synchronization point.
1290
+ * Once the jobSize is smaller than the window size,
1291
+ * it will result in compression ratio degradation.
1292
+ * NOTE 1: rsyncable mode only works when multithreading is enabled.
1293
+ * NOTE 2: rsyncable performs poorly in combination with long range mode,
1294
+ * since it will decrease the effectiveness of synchronization points,
1295
+ * though mileage may vary.
1296
+ * NOTE 3: Rsyncable mode limits maximum compression speed to ~400 MB/s.
1297
+ * If the selected compression level is already running significantly slower,
1298
+ * the overall speed won't be significantly impacted.
1299
+ */
1300
+ #define ZSTD_c_rsyncable ZSTD_c_experimentalParam1
1301
+
1302
+ /* Select a compression format.
1303
+ * The value must be of type ZSTD_format_e.
1304
+ * See ZSTD_format_e enum definition for details */
1305
+ #define ZSTD_c_format ZSTD_c_experimentalParam2
1306
+
1307
+ /* Force back-reference distances to remain < windowSize,
1308
+ * even when referencing into Dictionary content (default:0) */
1309
+ #define ZSTD_c_forceMaxWindow ZSTD_c_experimentalParam3
1310
+
1311
+ /* Controls whether the contents of a CDict
1312
+ * are used in place, or copied into the working context.
1313
+ * Accepts values from the ZSTD_dictAttachPref_e enum.
1314
+ * See the comments on that enum for an explanation of the feature. */
1315
+ #define ZSTD_c_forceAttachDict ZSTD_c_experimentalParam4
1316
+
1317
+ /*! ZSTD_CCtx_getParameter() :
1318
+ * Get the requested compression parameter value, selected by enum ZSTD_cParameter,
1319
+ * and store it into int* value.
1320
+ * @return : 0, or an error code (which can be tested with ZSTD_isError()).
1321
+ */
1322
+ ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, int* value);
1323
+
1324
+
1325
+ /*! ZSTD_CCtx_params :
1326
+ * Quick howto :
1327
+ * - 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().
1332
+ * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
1333
+ * an existing CCtx.
1334
+ * These parameters will be applied to
1335
+ * all subsequent frames.
1336
+ * - ZSTD_compressStream2() : Do compression using the CCtx.
1337
+ * - ZSTD_freeCCtxParams() : Free the memory.
1338
+ *
1339
+ * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams()
1340
+ * for static allocation of CCtx for single-threaded compression.
1341
+ */
1342
+ ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);
1343
+ ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params);
1344
+
1345
+ /*! ZSTD_CCtxParams_reset() :
1346
+ * Reset params to default values.
1347
+ */
1348
+ ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params);
1349
+
1350
+ /*! ZSTD_CCtxParams_init() :
1351
+ * Initializes the compression parameters of cctxParams according to
1352
+ * compression level. All other parameters are reset to their default values.
1353
+ */
1354
+ ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel);
1355
+
1356
+ /*! ZSTD_CCtxParams_init_advanced() :
1357
+ * Initializes the compression and frame parameters of cctxParams according to
1358
+ * params. All other parameters are reset to their default values.
1359
+ */
1360
+ ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
1361
+
1362
+ /*! ZSTD_CCtxParam_setParameter() :
1363
+ * Similar to ZSTD_CCtx_setParameter.
1364
+ * Set one compression parameter, selected by enum ZSTD_cParameter.
1365
+ * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
1366
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1367
+ */
1368
+ ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int value);
954
1369
 
955
- ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
956
- ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1370
+ /*! ZSTD_CCtxParam_getParameter() :
1371
+ * Similar to ZSTD_CCtx_getParameter.
1372
+ * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
1373
+ * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1374
+ */
1375
+ ZSTDLIB_API size_t ZSTD_CCtxParam_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, int* value);
957
1376
 
958
- /* misc */
959
- ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
960
- typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
961
- ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
1377
+ /*! ZSTD_CCtx_setParametersUsingCCtxParams() :
1378
+ * Apply a set of ZSTD_CCtx_params to the compression context.
1379
+ * This can be done even after compression is started,
1380
+ * if nbWorkers==0, this will have no impact until a new compression is started.
1381
+ * if nbWorkers>=1, new parameters will be picked up at next job,
1382
+ * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated).
1383
+ */
1384
+ ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams(
1385
+ ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params);
962
1386
 
1387
+ /*! ZSTD_compressStream2_simpleArgs() :
1388
+ * Same as ZSTD_compressStream2(),
1389
+ * but using only integral types as arguments.
1390
+ * This variant might be helpful for binders from dynamic languages
1391
+ * which have troubles handling structures containing memory pointers.
1392
+ */
1393
+ ZSTDLIB_API size_t ZSTD_compressStream2_simpleArgs (
1394
+ ZSTD_CCtx* cctx,
1395
+ void* dst, size_t dstCapacity, size_t* dstPos,
1396
+ const void* src, size_t srcSize, size_t* srcPos,
1397
+ ZSTD_EndDirective endOp);
963
1398
 
964
1399
 
965
- /* ============================================ */
966
- /** New advanced API (experimental) */
967
- /* ============================================ */
1400
+ /***************************************
1401
+ * Advanced decompression functions
1402
+ ***************************************/
968
1403
 
969
- /* API design :
970
- * In this advanced API, parameters are pushed one by one into an existing context,
971
- * using ZSTD_CCtx_set*() functions.
972
- * Pushed parameters are sticky : they are applied to next job, and any subsequent job.
973
- * It's possible to reset parameters to "default" using ZSTD_CCtx_reset().
974
- * Important : "sticky" parameters only work with `ZSTD_compress_generic()` !
975
- * For any other entry point, "sticky" parameters are ignored !
976
- *
977
- * This API is intended to replace all others advanced / experimental API entry points.
978
- */
1404
+ /*! ZSTD_isFrame() :
1405
+ * Tells if the content of `buffer` starts with a valid Frame Identifier.
1406
+ * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
1407
+ * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
1408
+ * Note 3 : Skippable Frame Identifiers are considered valid. */
1409
+ ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
979
1410
 
980
- /* note on enum design :
981
- * All enum will be pinned to explicit values before reaching "stable API" status */
1411
+ /*! ZSTD_createDDict_byReference() :
1412
+ * Create a digested dictionary, ready to start decompression operation without startup delay.
1413
+ * Dictionary content is referenced, and therefore stays in dictBuffer.
1414
+ * It is important that dictBuffer outlives DDict,
1415
+ * it must remain read accessible throughout the lifetime of DDict */
1416
+ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize);
982
1417
 
983
- typedef enum {
984
- /* Opened question : should we have a format ZSTD_f_auto ?
985
- * Today, it would mean exactly the same as ZSTD_f_zstd1.
986
- * But, in the future, should several formats become supported,
987
- * on the compression side, it would mean "default format".
988
- * On the decompression side, it would mean "automatic format detection",
989
- * so that ZSTD_f_zstd1 would mean "accept *only* zstd frames".
990
- * Since meaning is a little different, another option could be to define different enums for compression and decompression.
991
- * This question could be kept for later, when there are actually multiple formats to support,
992
- * but there is also the question of pinning enum values, and pinning value `0` is especially important */
993
- ZSTD_f_zstd1 = 0, /* zstd frame format, specified in zstd_compression_format.md (default) */
994
- ZSTD_f_zstd1_magicless, /* Variant of zstd frame format, without initial 4-bytes magic number.
995
- * Useful to save 4 bytes per generated frame.
996
- * Decoder cannot recognise automatically this format, requiring instructions. */
997
- } ZSTD_format_e;
998
1418
 
999
- typedef enum {
1000
- /* compression format */
1001
- ZSTD_p_format = 10, /* See ZSTD_format_e enum definition.
1002
- * Cast selected format as unsigned for ZSTD_CCtx_setParameter() compatibility. */
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);
1003
1424
 
1004
- /* compression parameters */
1005
- ZSTD_p_compressionLevel=100, /* Update all compression parameters according to pre-defined cLevel table
1006
- * Default level is ZSTD_CLEVEL_DEFAULT==3.
1007
- * Special: value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT.
1008
- * Note 1 : it's possible to pass a negative compression level by casting it to unsigned type.
1009
- * Note 2 : setting a level sets all default values of other compression parameters.
1010
- * Note 3 : setting compressionLevel automatically updates ZSTD_p_compressLiterals. */
1011
- ZSTD_p_windowLog, /* Maximum allowed back-reference distance, expressed as power of 2.
1012
- * Must be clamped between ZSTD_WINDOWLOG_MIN and ZSTD_WINDOWLOG_MAX.
1013
- * Special: value 0 means "use default windowLog".
1014
- * Note: Using a window size greater than ZSTD_MAXWINDOWSIZE_DEFAULT (default: 2^27)
1015
- * requires explicitly allowing such window size during decompression stage. */
1016
- ZSTD_p_hashLog, /* Size of the initial probe table, as a power of 2.
1017
- * Resulting table size is (1 << (hashLog+2)).
1018
- * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX.
1019
- * Larger tables improve compression ratio of strategies <= dFast,
1020
- * and improve speed of strategies > dFast.
1021
- * Special: value 0 means "use default hashLog". */
1022
- ZSTD_p_chainLog, /* Size of the multi-probe search table, as a power of 2.
1023
- * Resulting table size is (1 << (chainLog+2)).
1024
- * Must be clamped between ZSTD_CHAINLOG_MIN and ZSTD_CHAINLOG_MAX.
1025
- * Larger tables result in better and slower compression.
1026
- * This parameter is useless when using "fast" strategy.
1027
- * Note it's still useful when using "dfast" strategy,
1028
- * in which case it defines a secondary probe table.
1029
- * Special: value 0 means "use default chainLog". */
1030
- ZSTD_p_searchLog, /* Number of search attempts, as a power of 2.
1031
- * More attempts result in better and slower compression.
1032
- * This parameter is useless when using "fast" and "dFast" strategies.
1033
- * Special: value 0 means "use default searchLog". */
1034
- ZSTD_p_minMatch, /* Minimum size of searched matches (note : repCode matches can be smaller).
1035
- * Larger values make faster compression and decompression, but decrease ratio.
1036
- * Must be clamped between ZSTD_SEARCHLENGTH_MIN and ZSTD_SEARCHLENGTH_MAX.
1037
- * Note that currently, for all strategies < btopt, effective minimum is 4.
1038
- * , for all strategies > fast, effective maximum is 6.
1039
- * Special: value 0 means "use default minMatchLength". */
1040
- ZSTD_p_targetLength, /* Impact of this field depends on strategy.
1041
- * For strategies btopt & btultra:
1042
- * Length of Match considered "good enough" to stop search.
1043
- * Larger values make compression stronger, and slower.
1044
- * For strategy fast:
1045
- * Distance between match sampling.
1046
- * Larger values make compression faster, and weaker.
1047
- * Special: value 0 means "use default targetLength". */
1048
- ZSTD_p_compressionStrategy, /* See ZSTD_strategy enum definition.
1049
- * Cast selected strategy as unsigned for ZSTD_CCtx_setParameter() compatibility.
1050
- * The higher the value of selected strategy, the more complex it is,
1051
- * resulting in stronger and slower compression.
1052
- * Special: value 0 means "use default strategy". */
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);
1053
1430
 
1054
- ZSTD_p_enableLongDistanceMatching=160, /* Enable long distance matching.
1055
- * This parameter is designed to improve compression ratio
1056
- * for large inputs, by finding large matches at long distance.
1057
- * It increases memory usage and window size.
1058
- * Note: enabling this parameter increases ZSTD_p_windowLog to 128 MB
1059
- * except when expressly set to a different value. */
1060
- ZSTD_p_ldmHashLog, /* Size of the table for long distance matching, as a power of 2.
1061
- * Larger values increase memory usage and compression ratio,
1062
- * but decrease compression speed.
1063
- * Must be clamped between ZSTD_HASHLOG_MIN and ZSTD_HASHLOG_MAX
1064
- * default: windowlog - 7.
1065
- * Special: value 0 means "automatically determine hashlog". */
1066
- ZSTD_p_ldmMinMatch, /* Minimum match size for long distance matcher.
1067
- * Larger/too small values usually decrease compression ratio.
1068
- * Must be clamped between ZSTD_LDM_MINMATCH_MIN and ZSTD_LDM_MINMATCH_MAX.
1069
- * Special: value 0 means "use default value" (default: 64). */
1070
- ZSTD_p_ldmBucketSizeLog, /* Log size of each bucket in the LDM hash table for collision resolution.
1071
- * Larger values improve collision resolution but decrease compression speed.
1072
- * The maximum value is ZSTD_LDM_BUCKETSIZELOG_MAX .
1073
- * Special: value 0 means "use default value" (default: 3). */
1074
- ZSTD_p_ldmHashEveryLog, /* Frequency of inserting/looking up entries in the LDM hash table.
1075
- * Must be clamped between 0 and (ZSTD_WINDOWLOG_MAX - ZSTD_HASHLOG_MIN).
1076
- * Default is MAX(0, (windowLog - ldmHashLog)), optimizing hash table usage.
1077
- * Larger values improve compression speed.
1078
- * Deviating far from default value will likely result in a compression ratio decrease.
1079
- * Special: value 0 means "automatically determine hashEveryLog". */
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);
1080
1442
 
1081
- /* frame parameters */
1082
- ZSTD_p_contentSizeFlag=200, /* Content size will be written into frame header _whenever known_ (default:1)
1083
- * Content size must be known at the beginning of compression,
1084
- * it is provided using ZSTD_CCtx_setPledgedSrcSize() */
1085
- ZSTD_p_checksumFlag, /* A 32-bits checksum of content is written at end of frame (default:0) */
1086
- ZSTD_p_dictIDFlag, /* When applicable, dictionary's ID is written into frame header (default:1) */
1443
+ /*! ZSTD_DCtx_loadDictionary_byReference() :
1444
+ * Same as ZSTD_DCtx_loadDictionary(),
1445
+ * but references `dict` content instead of copying it into `dctx`.
1446
+ * This saves memory if `dict` remains around.,
1447
+ * However, it's imperative that `dict` remains accessible (and unmodified) while being used, so it must outlive decompression. */
1448
+ ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
1087
1449
 
1088
- /* multi-threading parameters */
1089
- /* These parameters are only useful if multi-threading is enabled (ZSTD_MULTITHREAD).
1090
- * They return an error otherwise. */
1091
- ZSTD_p_nbWorkers=400, /* Select how many threads will be spawned to compress in parallel.
1092
- * When nbWorkers >= 1, triggers asynchronous mode :
1093
- * ZSTD_compress_generic() consumes some input, flush some output if possible, and immediately gives back control to caller,
1094
- * while compression work is performed in parallel, within worker threads.
1095
- * (note : a strong exception to this rule is when first invocation sets ZSTD_e_end : it becomes a blocking call).
1096
- * More workers improve speed, but also increase memory usage.
1097
- * Default value is `0`, aka "single-threaded mode" : no worker is spawned, compression is performed inside Caller's thread, all invocations are blocking */
1098
- ZSTD_p_jobSize, /* Size of a compression job. This value is enforced only in non-blocking mode.
1099
- * Each compression job is completed in parallel, so this value indirectly controls the nb of active threads.
1100
- * 0 means default, which is dynamically determined based on compression parameters.
1101
- * Job size must be a minimum of overlapSize, or 1 MB, whichever is largest.
1102
- * The minimum size is automatically and transparently enforced */
1103
- ZSTD_p_overlapSizeLog, /* Size of previous input reloaded at the beginning of each job.
1104
- * 0 => no overlap, 6(default) => use 1/8th of windowSize, >=9 => use full windowSize */
1105
-
1106
- /* =================================================================== */
1107
- /* experimental parameters - no stability guaranteed */
1108
- /* =================================================================== */
1109
-
1110
- ZSTD_p_forceMaxWindow=1100, /* Force back-reference distances to remain < windowSize,
1111
- * even when referencing into Dictionary content (default:0) */
1112
- ZSTD_p_forceAttachDict, /* ZSTD supports usage of a CDict in-place
1113
- * (avoiding having to copy the compression tables
1114
- * from the CDict into the working context). Using
1115
- * a CDict in this way saves an initial setup step,
1116
- * but comes at the cost of more work per byte of
1117
- * input. ZSTD has a simple internal heuristic that
1118
- * guesses which strategy will be faster. You can
1119
- * use this flag to override that guess.
1120
- *
1121
- * Note that the by-reference, in-place strategy is
1122
- * only used when reusing a compression context
1123
- * with compatible compression parameters. (If
1124
- * incompatible / uninitialized, the working
1125
- * context needs to be cleared anyways, which is
1126
- * about as expensive as overwriting it with the
1127
- * dictionary context, so there's no savings in
1128
- * using the CDict by-ref.)
1129
- *
1130
- * Values greater than 0 force attaching the dict.
1131
- * Values less than 0 force copying the dict.
1132
- * 0 selects the default heuristic-guided behavior.
1133
- */
1450
+ /*! ZSTD_DCtx_loadDictionary_advanced() :
1451
+ * Same as ZSTD_DCtx_loadDictionary(),
1452
+ * but gives direct control over
1453
+ * how to load the dictionary (by copy ? by reference ?)
1454
+ * and how to interpret it (automatic ? force raw mode ? full mode only ?). */
1455
+ ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
1134
1456
 
1135
- } ZSTD_cParameter;
1457
+ /*! ZSTD_DCtx_refPrefix_advanced() :
1458
+ * Same as ZSTD_DCtx_refPrefix(), but gives finer control over
1459
+ * how to interpret prefix content (automatic ? force raw mode (default) ? full mode only ?) */
1460
+ ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx, const void* prefix, size_t prefixSize, ZSTD_dictContentType_e dictContentType);
1136
1461
 
1462
+ /*! ZSTD_DCtx_setMaxWindowSize() :
1463
+ * Refuses allocating internal buffers for frames requiring a window size larger than provided limit.
1464
+ * This protects a decoder context from reserving too much memory for itself (potential attack scenario).
1465
+ * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode.
1466
+ * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT)
1467
+ * @return : 0, or an error code (which can be tested using ZSTD_isError()).
1468
+ */
1469
+ ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize);
1137
1470
 
1138
- /*! ZSTD_CCtx_setParameter() :
1139
- * Set one compression parameter, selected by enum ZSTD_cParameter.
1140
- * Setting a parameter is generally only possible during frame initialization (before starting compression).
1141
- * Exception : when using multi-threading mode (nbThreads >= 1),
1142
- * following parameters can be updated _during_ compression (within same frame):
1143
- * => compressionLevel, hashLog, chainLog, searchLog, minMatch, targetLength and strategy.
1144
- * new parameters will be active on next job, or after a flush().
1145
- * Note : when `value` type is not unsigned (int, or enum), cast it to unsigned for proper type checking.
1146
- * @result : informational value (typically, value being set, correctly clamped),
1147
- * or an error code (which can be tested with ZSTD_isError()). */
1148
- ZSTDLIB_API size_t ZSTD_CCtx_setParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned value);
1471
+ /* ZSTD_d_format
1472
+ * experimental parameter,
1473
+ * allowing selection between ZSTD_format_e input compression formats
1474
+ */
1475
+ #define ZSTD_d_format ZSTD_d_experimentalParam1
1149
1476
 
1150
- /*! ZSTD_CCtx_getParameter() :
1151
- * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
1152
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1477
+ /*! ZSTD_DCtx_setFormat() :
1478
+ * Instruct the decoder context about what kind of data to decode next.
1479
+ * This instruction is mandatory to decode data without a fully-formed header,
1480
+ * such ZSTD_f_zstd1_magicless for example.
1481
+ * @return : 0, or an error code (which can be tested using ZSTD_isError()). */
1482
+ ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format);
1483
+
1484
+ /*! ZSTD_decompressStream_simpleArgs() :
1485
+ * Same as ZSTD_decompressStream(),
1486
+ * but using only integral types as arguments.
1487
+ * This can be helpful for binders from dynamic languages
1488
+ * which have troubles handling structures containing memory pointers.
1153
1489
  */
1154
- ZSTDLIB_API size_t ZSTD_CCtx_getParameter(ZSTD_CCtx* cctx, ZSTD_cParameter param, unsigned* value);
1490
+ ZSTDLIB_API size_t ZSTD_decompressStream_simpleArgs (
1491
+ ZSTD_DCtx* dctx,
1492
+ void* dst, size_t dstCapacity, size_t* dstPos,
1493
+ const void* src, size_t srcSize, size_t* srcPos);
1155
1494
 
1156
- /*! ZSTD_CCtx_setPledgedSrcSize() :
1157
- * Total input data size to be compressed as a single frame.
1158
- * This value will be controlled at the end, and result in error if not respected.
1159
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1160
- * Note 1 : 0 means zero, empty.
1161
- * In order to mean "unknown content size", pass constant ZSTD_CONTENTSIZE_UNKNOWN.
1162
- * ZSTD_CONTENTSIZE_UNKNOWN is default value for any new compression job.
1163
- * Note 2 : If all data is provided and consumed in a single round,
1164
- * this value is overriden by srcSize instead. */
1165
- ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize);
1166
1495
 
1167
- /*! ZSTD_CCtx_loadDictionary() :
1168
- * Create an internal CDict from `dict` buffer.
1169
- * Decompression will have to use same dictionary.
1170
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1171
- * Special: Adding a NULL (or 0-size) dictionary invalidates previous dictionary,
1172
- * meaning "return to no-dictionary mode".
1173
- * Note 1 : Dictionary will be used for all future compression jobs.
1174
- * To return to "no-dictionary" situation, load a NULL dictionary
1175
- * Note 2 : Loading a dictionary involves building tables, which are dependent on compression parameters.
1176
- * For this reason, compression parameters cannot be changed anymore after loading a dictionary.
1177
- * It's also a CPU consuming operation, with non-negligible impact on latency.
1178
- * Note 3 :`dict` content will be copied internally.
1179
- * Use ZSTD_CCtx_loadDictionary_byReference() to reference dictionary content instead.
1180
- * In such a case, dictionary buffer must outlive its users.
1181
- * Note 4 : Use ZSTD_CCtx_loadDictionary_advanced()
1182
- * to precisely select how dictionary content must be interpreted. */
1183
- ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
1184
- ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_byReference(ZSTD_CCtx* cctx, const void* dict, size_t dictSize);
1185
- ZSTDLIB_API size_t ZSTD_CCtx_loadDictionary_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
1496
+ /********************************************************************
1497
+ * Advanced streaming functions
1498
+ * Warning : most of these functions are now redundant with the Advanced API.
1499
+ * Once Advanced API reaches "stable" status,
1500
+ * redundant functions will be deprecated, and then at some point removed.
1501
+ ********************************************************************/
1502
+
1503
+ /*===== 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. */
1186
1510
 
1511
+ /*! ZSTD_resetCStream() :
1512
+ * start a new frame, using same parameters from previous frame.
1513
+ * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.
1514
+ * Note that zcs must be init at least once before using ZSTD_resetCStream().
1515
+ * If pledgedSrcSize is not known at reset time, use macro ZSTD_CONTENTSIZE_UNKNOWN.
1516
+ * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end.
1517
+ * For the time being, pledgedSrcSize==0 is interpreted as "srcSize unknown" for compatibility with older programs,
1518
+ * but it will change to mean "empty" in future version, so use macro ZSTD_CONTENTSIZE_UNKNOWN instead.
1519
+ * @return : 0, or an error code (which can be tested using ZSTD_isError())
1520
+ */
1521
+ ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize);
1187
1522
 
1188
- /*! ZSTD_CCtx_refCDict() :
1189
- * Reference a prepared dictionary, to be used for all next compression jobs.
1190
- * Note that compression parameters are enforced from within CDict,
1191
- * and supercede any compression parameter previously set within CCtx.
1192
- * The dictionary will remain valid for future compression jobs using same CCtx.
1193
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1194
- * Special : adding a NULL CDict means "return to no-dictionary mode".
1195
- * Note 1 : Currently, only one dictionary can be managed.
1196
- * Adding a new dictionary effectively "discards" any previous one.
1197
- * Note 2 : CDict is just referenced, its lifetime must outlive CCtx. */
1198
- ZSTDLIB_API size_t ZSTD_CCtx_refCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict);
1199
1523
 
1200
- /*! ZSTD_CCtx_refPrefix() :
1201
- * Reference a prefix (single-usage dictionary) for next compression job.
1202
- * Decompression will need same prefix to properly regenerate data.
1203
- * Compressing with a prefix is similar in outcome as performing a diff and compressing it,
1204
- * but performs much faster, especially during decompression (compression speed is tunable with compression level).
1205
- * Note that prefix is **only used once**. Tables are discarded at end of compression job (ZSTD_e_end).
1206
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1207
- * Special: Adding any prefix (including NULL) invalidates any previous prefix or dictionary
1208
- * Note 1 : Prefix buffer is referenced. It **must** outlive compression job.
1209
- * Its contain must remain unmodified up to end of compression (ZSTD_e_end).
1210
- * Note 2 : If the intention is to diff some large src data blob with some prior version of itself,
1211
- * ensure that the window size is large enough to contain the entire source.
1212
- * See ZSTD_p_windowLog.
1213
- * Note 3 : Referencing a prefix involves building tables, which are dependent on compression parameters.
1214
- * It's a CPU consuming operation, with non-negligible impact on latency.
1215
- * If there is a need to use same prefix multiple times, consider loadDictionary instead.
1216
- * Note 4 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
1217
- * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode. */
1218
- ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx,
1219
- const void* prefix, size_t prefixSize);
1220
- ZSTDLIB_API size_t ZSTD_CCtx_refPrefix_advanced(ZSTD_CCtx* cctx,
1221
- const void* prefix, size_t prefixSize,
1222
- ZSTD_dictContentType_e dictContentType);
1524
+ typedef struct {
1525
+ unsigned long long ingested; /* nb input bytes read and buffered */
1526
+ unsigned long long consumed; /* nb input bytes actually compressed */
1527
+ unsigned long long produced; /* nb of compressed bytes generated and buffered */
1528
+ unsigned long long flushed; /* nb of compressed bytes flushed : not provided; can be tracked from caller side */
1529
+ unsigned currentJobID; /* MT only : latest started job nb */
1530
+ unsigned nbActiveWorkers; /* MT only : nb of workers actively compressing at probe time */
1531
+ } ZSTD_frameProgression;
1223
1532
 
1224
- /*! ZSTD_CCtx_reset() :
1225
- * Return a CCtx to clean state.
1226
- * Useful after an error, or to interrupt an ongoing compression job and start a new one.
1227
- * Any internal data not yet flushed is cancelled.
1228
- * The parameters and dictionary are kept unchanged, to reset them use ZSTD_CCtx_resetParameters().
1533
+ /* ZSTD_getFrameProgression() :
1534
+ * tells how much data has been ingested (read from input)
1535
+ * consumed (input actually compressed) and produced (output) for current frame.
1536
+ * Note : (ingested - consumed) is amount of input data buffered internally, not yet compressed.
1537
+ * Aggregates progression inside active worker threads.
1229
1538
  */
1230
- ZSTDLIB_API void ZSTD_CCtx_reset(ZSTD_CCtx* cctx);
1231
-
1232
- /*! ZSTD_CCtx_resetParameters() :
1233
- * All parameters are back to default values (compression level is ZSTD_CLEVEL_DEFAULT).
1234
- * Dictionary (if any) is dropped.
1235
- * Resetting parameters is only possible during frame initialization (before starting compression).
1236
- * To reset the context use ZSTD_CCtx_reset().
1237
- * @return 0 or an error code (which can be checked with ZSTD_isError()).
1539
+ ZSTDLIB_API ZSTD_frameProgression ZSTD_getFrameProgression(const ZSTD_CCtx* cctx);
1540
+
1541
+ /*! ZSTD_toFlushNow() :
1542
+ * Tell how many bytes are ready to be flushed immediately.
1543
+ * Useful for multithreading scenarios (nbWorkers >= 1).
1544
+ * Probe the oldest active job, defined as oldest job not yet entirely flushed,
1545
+ * and check its output buffer.
1546
+ * @return : amount of data stored in oldest job and ready to be flushed immediately.
1547
+ * if @return == 0, it means either :
1548
+ * + there is no active job (could be checked with ZSTD_frameProgression()), or
1549
+ * + oldest job is still actively compressing data,
1550
+ * but everything it has produced has also been flushed so far,
1551
+ * therefore flush speed is limited by production speed of oldest job
1552
+ * irrespective of the speed of concurrent (and newer) jobs.
1238
1553
  */
1239
- ZSTDLIB_API size_t ZSTD_CCtx_resetParameters(ZSTD_CCtx* cctx);
1554
+ ZSTDLIB_API size_t ZSTD_toFlushNow(ZSTD_CCtx* cctx);
1240
1555
 
1241
1556
 
1557
+ /*===== 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 */
1242
1561
 
1243
- typedef enum {
1244
- ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */
1245
- ZSTD_e_flush, /* flush any data provided so far,
1246
- * it creates (at least) one new block, that can be decoded immediately on reception;
1247
- * frame will continue: any future data can still reference previously compressed data, improving compression. */
1248
- ZSTD_e_end /* flush any remaining data and close current frame.
1249
- * any additional data starts a new frame.
1250
- * each frame is independent (does not reference any content from previous frame). */
1251
- } ZSTD_EndDirective;
1252
1562
 
1253
- /*! ZSTD_compress_generic() :
1254
- * Behave about the same as ZSTD_compressStream. To note :
1255
- * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_setParameter()
1256
- * - Compression parameters cannot be changed once compression is started.
1257
- * - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize
1258
- * - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit.
1259
- * - In single-thread mode (default), function is blocking : it completed its job before returning to caller.
1260
- * - In multi-thread mode, function is non-blocking : it just acquires a copy of input, and distribute job to internal worker threads,
1261
- * and then immediately returns, just indicating that there is some data remaining to be flushed.
1262
- * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte.
1263
- * - Exception : in multi-threading mode, if the first call requests a ZSTD_e_end directive, it is blocking : it will complete compression before giving back control to caller.
1264
- * - @return provides a minimum amount of data remaining to be flushed from internal buffers
1265
- * or an error code, which can be tested using ZSTD_isError().
1266
- * if @return != 0, flush is not fully completed, there is still some data left within internal buffers.
1267
- * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers.
1268
- * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed.
1269
- * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0),
1270
- * only ZSTD_e_end or ZSTD_e_flush operations are allowed.
1271
- * Before starting a new compression job, or changing compression parameters,
1272
- * it is required to fully flush internal buffers.
1273
- */
1274
- ZSTDLIB_API size_t ZSTD_compress_generic (ZSTD_CCtx* cctx,
1275
- ZSTD_outBuffer* output,
1276
- ZSTD_inBuffer* input,
1277
- ZSTD_EndDirective endOp);
1563
+ /*********************************************************************
1564
+ * Buffer-less and synchronous inner streaming functions
1565
+ *
1566
+ * This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
1567
+ * But it's also a complex one, with several restrictions, documented below.
1568
+ * Prefer normal streaming API for an easier experience.
1569
+ ********************************************************************* */
1278
1570
 
1571
+ /**
1572
+ Buffer-less streaming compression (synchronous mode)
1279
1573
 
1280
- /*! ZSTD_compress_generic_simpleArgs() :
1281
- * Same as ZSTD_compress_generic(),
1282
- * but using only integral types as arguments.
1283
- * Argument list is larger than ZSTD_{in,out}Buffer,
1284
- * but can be helpful for binders from dynamic languages
1285
- * which have troubles handling structures containing memory pointers.
1286
- */
1287
- ZSTDLIB_API size_t ZSTD_compress_generic_simpleArgs (
1288
- ZSTD_CCtx* cctx,
1289
- void* dst, size_t dstCapacity, size_t* dstPos,
1290
- const void* src, size_t srcSize, size_t* srcPos,
1291
- ZSTD_EndDirective endOp);
1574
+ A ZSTD_CCtx object is required to track streaming operations.
1575
+ Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
1576
+ ZSTD_CCtx object can be re-used multiple times within successive compression operations.
1292
1577
 
1578
+ Start by initializing a context.
1579
+ Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
1580
+ or ZSTD_compressBegin_advanced(), for finer parameter control.
1581
+ It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
1293
1582
 
1294
- /*! ZSTD_CCtx_params :
1295
- * Quick howto :
1296
- * - ZSTD_createCCtxParams() : Create a ZSTD_CCtx_params structure
1297
- * - ZSTD_CCtxParam_setParameter() : Push parameters one by one into
1298
- * an existing ZSTD_CCtx_params structure.
1299
- * This is similar to
1300
- * ZSTD_CCtx_setParameter().
1301
- * - ZSTD_CCtx_setParametersUsingCCtxParams() : Apply parameters to
1302
- * an existing CCtx.
1303
- * These parameters will be applied to
1304
- * all subsequent compression jobs.
1305
- * - ZSTD_compress_generic() : Do compression using the CCtx.
1306
- * - ZSTD_freeCCtxParams() : Free the memory.
1307
- *
1308
- * This can be used with ZSTD_estimateCCtxSize_advanced_usingCCtxParams()
1309
- * for static allocation for single-threaded compression.
1310
- */
1311
- ZSTDLIB_API ZSTD_CCtx_params* ZSTD_createCCtxParams(void);
1312
- ZSTDLIB_API size_t ZSTD_freeCCtxParams(ZSTD_CCtx_params* params);
1583
+ Then, consume your input using ZSTD_compressContinue().
1584
+ There are some important considerations to keep in mind when using this advanced function :
1585
+ - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffers only.
1586
+ - Interface is synchronous : input is consumed entirely and produces 1+ compressed blocks.
1587
+ - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
1588
+ Worst case evaluation is provided by ZSTD_compressBound().
1589
+ ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
1590
+ - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
1591
+ It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
1592
+ - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
1593
+ In which case, it will "discard" the relevant memory section from its history.
1313
1594
 
1595
+ Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
1596
+ It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame.
1597
+ Without last block mark, frames are considered unfinished (hence corrupted) by compliant decoders.
1314
1598
 
1315
- /*! ZSTD_CCtxParams_reset() :
1316
- * Reset params to default values.
1317
- */
1318
- ZSTDLIB_API size_t ZSTD_CCtxParams_reset(ZSTD_CCtx_params* params);
1599
+ `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress again.
1600
+ */
1319
1601
 
1320
- /*! ZSTD_CCtxParams_init() :
1321
- * Initializes the compression parameters of cctxParams according to
1322
- * compression level. All other parameters are reset to their default values.
1323
- */
1324
- ZSTDLIB_API size_t ZSTD_CCtxParams_init(ZSTD_CCtx_params* cctxParams, int compressionLevel);
1602
+ /*===== Buffer-less streaming compression functions =====*/
1603
+ ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
1604
+ ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
1605
+ ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize : If srcSize is not known at init time, use ZSTD_CONTENTSIZE_UNKNOWN */
1606
+ ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */
1607
+ ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict_advanced(ZSTD_CCtx* const cctx, const ZSTD_CDict* const cdict, ZSTD_frameParameters const fParams, unsigned long long const pledgedSrcSize); /* compression parameters are already set within cdict. pledgedSrcSize must be correct. If srcSize is not known, use macro ZSTD_CONTENTSIZE_UNKNOWN */
1608
+ ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize is not known, use ZSTD_CONTENTSIZE_UNKNOWN */
1325
1609
 
1326
- /*! ZSTD_CCtxParams_init_advanced() :
1327
- * Initializes the compression and frame parameters of cctxParams according to
1328
- * params. All other parameters are reset to their default values.
1329
- */
1330
- ZSTDLIB_API size_t ZSTD_CCtxParams_init_advanced(ZSTD_CCtx_params* cctxParams, ZSTD_parameters params);
1610
+ ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1611
+ ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1331
1612
 
1332
1613
 
1333
- /*! ZSTD_CCtxParam_setParameter() :
1334
- * Similar to ZSTD_CCtx_setParameter.
1335
- * Set one compression parameter, selected by enum ZSTD_cParameter.
1336
- * Parameters must be applied to a ZSTD_CCtx using ZSTD_CCtx_setParametersUsingCCtxParams().
1337
- * Note : when `value` is an enum, cast it to unsigned for proper type checking.
1338
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1339
- */
1340
- ZSTDLIB_API size_t ZSTD_CCtxParam_setParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned value);
1614
+ /*-
1615
+ Buffer-less streaming decompression (synchronous mode)
1341
1616
 
1342
- /*! ZSTD_CCtxParam_getParameter() :
1343
- * Similar to ZSTD_CCtx_getParameter.
1344
- * Get the requested value of one compression parameter, selected by enum ZSTD_cParameter.
1345
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1346
- */
1347
- ZSTDLIB_API size_t ZSTD_CCtxParam_getParameter(ZSTD_CCtx_params* params, ZSTD_cParameter param, unsigned* value);
1617
+ A ZSTD_DCtx object is required to track streaming operations.
1618
+ Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
1619
+ A ZSTD_DCtx object can be re-used multiple times.
1348
1620
 
1349
- /*! ZSTD_CCtx_setParametersUsingCCtxParams() :
1350
- * Apply a set of ZSTD_CCtx_params to the compression context.
1351
- * This can be done even after compression is started,
1352
- * if nbWorkers==0, this will have no impact until a new compression is started.
1353
- * if nbWorkers>=1, new parameters will be picked up at next job,
1354
- * with a few restrictions (windowLog, pledgedSrcSize, nbWorkers, jobSize, and overlapLog are not updated).
1355
- */
1356
- ZSTDLIB_API size_t ZSTD_CCtx_setParametersUsingCCtxParams(
1357
- ZSTD_CCtx* cctx, const ZSTD_CCtx_params* params);
1621
+ First typical operation is to retrieve frame parameters, using ZSTD_getFrameHeader().
1622
+ Frame header is extracted from the beginning of compressed frame, so providing only the frame's beginning is enough.
1623
+ Data fragment must be large enough to ensure successful decoding.
1624
+ `ZSTD_frameHeaderSize_max` bytes is guaranteed to always be large enough.
1625
+ @result : 0 : successful decoding, the `ZSTD_frameHeader` structure is correctly filled.
1626
+ >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
1627
+ errorCode, which can be tested using ZSTD_isError().
1358
1628
 
1629
+ It fills a ZSTD_frameHeader structure with important information to correctly decode the frame,
1630
+ such as the dictionary ID, content size, or maximum back-reference distance (`windowSize`).
1631
+ Note that these values could be wrong, either because of data corruption, or because a 3rd party deliberately spoofs false information.
1632
+ As a consequence, check that values remain within valid application range.
1633
+ For example, do not allocate memory blindly, check that `windowSize` is within expectation.
1634
+ Each application can set its own limits, depending on local restrictions.
1635
+ For extended interoperability, it is recommended to support `windowSize` of at least 8 MB.
1359
1636
 
1360
- /* ==================================== */
1361
- /*=== Advanced decompression API ===*/
1362
- /* ==================================== */
1637
+ ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize` bytes.
1638
+ ZSTD_decompressContinue() is very sensitive to contiguity,
1639
+ if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
1640
+ or that previous contiguous segment is large enough to properly handle maximum back-reference distance.
1641
+ There are multiple ways to guarantee this condition.
1363
1642
 
1364
- /* The following API works the same way as the advanced compression API :
1365
- * a context is created, parameters are pushed into it one by one,
1366
- * then the context can be used to decompress data using an interface similar to the straming API.
1367
- */
1643
+ The most memory efficient way is to use a round buffer of sufficient size.
1644
+ Sufficient size is determined by invoking ZSTD_decodingBufferSize_min(),
1645
+ which can @return an error code if required value is too large for current system (in 32-bits mode).
1646
+ In a round buffer methodology, ZSTD_decompressContinue() decompresses each block next to previous one,
1647
+ up to the moment there is not enough room left in the buffer to guarantee decoding another full block,
1648
+ which maximum size is provided in `ZSTD_frameHeader` structure, field `blockSizeMax`.
1649
+ At which point, decoding can resume from the beginning of the buffer.
1650
+ Note that already decoded data stored in the buffer should be flushed before being overwritten.
1368
1651
 
1369
- /*! ZSTD_DCtx_loadDictionary() :
1370
- * Create an internal DDict from dict buffer,
1371
- * to be used to decompress next frames.
1372
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1373
- * Special : Adding a NULL (or 0-size) dictionary invalidates any previous dictionary,
1374
- * meaning "return to no-dictionary mode".
1375
- * Note 1 : `dict` content will be copied internally.
1376
- * Use ZSTD_DCtx_loadDictionary_byReference()
1377
- * to reference dictionary content instead.
1378
- * In which case, the dictionary buffer must outlive its users.
1379
- * Note 2 : Loading a dictionary involves building tables,
1380
- * which has a non-negligible impact on CPU usage and latency.
1381
- * Note 3 : Use ZSTD_DCtx_loadDictionary_advanced() to select
1382
- * how dictionary content will be interpreted and loaded.
1383
- */
1384
- ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
1385
- ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_byReference(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
1386
- ZSTDLIB_API size_t ZSTD_DCtx_loadDictionary_advanced(ZSTD_DCtx* dctx, const void* dict, size_t dictSize, ZSTD_dictLoadMethod_e dictLoadMethod, ZSTD_dictContentType_e dictContentType);
1652
+ There are alternatives possible, for example using two or more buffers of size `windowSize` each, though they consume more memory.
1387
1653
 
1654
+ Finally, if you control the compression process, you can also ignore all buffer size rules,
1655
+ as long as the encoder and decoder progress in "lock-step",
1656
+ aka use exactly the same buffer sizes, break contiguity at the same place, etc.
1388
1657
 
1389
- /*! ZSTD_DCtx_refDDict() :
1390
- * Reference a prepared dictionary, to be used to decompress next frames.
1391
- * The dictionary remains active for decompression of future frames using same DCtx.
1392
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1393
- * Note 1 : Currently, only one dictionary can be managed.
1394
- * Referencing a new dictionary effectively "discards" any previous one.
1395
- * Special : adding a NULL DDict means "return to no-dictionary mode".
1396
- * Note 2 : DDict is just referenced, its lifetime must outlive its usage from DCtx.
1397
- */
1398
- ZSTDLIB_API size_t ZSTD_DCtx_refDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
1658
+ Once buffers are setup, start decompression, with ZSTD_decompressBegin().
1659
+ If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict().
1399
1660
 
1661
+ Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
1662
+ ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
1663
+ ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
1400
1664
 
1401
- /*! ZSTD_DCtx_refPrefix() :
1402
- * Reference a prefix (single-usage dictionary) for next compression job.
1403
- * This is the reverse operation of ZSTD_CCtx_refPrefix(),
1404
- * and must use the same prefix as the one used during compression.
1405
- * Prefix is **only used once**. Reference is discarded at end of frame.
1406
- * End of frame is reached when ZSTD_DCtx_decompress_generic() returns 0.
1407
- * @result : 0, or an error code (which can be tested with ZSTD_isError()).
1408
- * Note 1 : Adding any prefix (including NULL) invalidates any previously set prefix or dictionary
1409
- * Note 2 : Prefix buffer is referenced. It **must** outlive decompression job.
1410
- * Prefix buffer must remain unmodified up to the end of frame,
1411
- * reached when ZSTD_DCtx_decompress_generic() returns 0.
1412
- * Note 3 : By default, the prefix is treated as raw content (ZSTD_dm_rawContent).
1413
- * Use ZSTD_CCtx_refPrefix_advanced() to alter dictMode.
1414
- * Note 4 : Referencing a raw content prefix has almost no cpu nor memory cost.
1415
- * A fulldict prefix is more costly though.
1416
- */
1417
- ZSTDLIB_API size_t ZSTD_DCtx_refPrefix(ZSTD_DCtx* dctx,
1418
- const void* prefix, size_t prefixSize);
1419
- ZSTDLIB_API size_t ZSTD_DCtx_refPrefix_advanced(ZSTD_DCtx* dctx,
1420
- const void* prefix, size_t prefixSize,
1421
- ZSTD_dictContentType_e dictContentType);
1665
+ @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
1666
+ It can be zero : it just means ZSTD_decompressContinue() has decoded some metadata item.
1667
+ It can also be an error code, which can be tested with ZSTD_isError().
1422
1668
 
1669
+ A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
1670
+ Context can then be reset to start a new decompression.
1423
1671
 
1424
- /*! ZSTD_DCtx_setMaxWindowSize() :
1425
- * Refuses allocating internal buffers for frames requiring a window size larger than provided limit.
1426
- * This is useful to prevent a decoder context from reserving too much memory for itself (potential attack scenario).
1427
- * This parameter is only useful in streaming mode, since no internal buffer is allocated in direct mode.
1428
- * By default, a decompression context accepts all window sizes <= (1 << ZSTD_WINDOWLOG_MAX)
1429
- * @return : 0, or an error code (which can be tested using ZSTD_isError()).
1430
- */
1431
- ZSTDLIB_API size_t ZSTD_DCtx_setMaxWindowSize(ZSTD_DCtx* dctx, size_t maxWindowSize);
1672
+ Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
1673
+ This information is not required to properly decode a frame.
1432
1674
 
1675
+ == Special case : skippable frames ==
1433
1676
 
1434
- /*! ZSTD_DCtx_setFormat() :
1435
- * Instruct the decoder context about what kind of data to decode next.
1436
- * This instruction is mandatory to decode data without a fully-formed header,
1437
- * such ZSTD_f_zstd1_magicless for example.
1438
- * @return : 0, or an error code (which can be tested using ZSTD_isError()).
1439
- */
1440
- ZSTDLIB_API size_t ZSTD_DCtx_setFormat(ZSTD_DCtx* dctx, ZSTD_format_e format);
1677
+ Skippable frames allow integration of user-defined data into a flow of concatenated frames.
1678
+ Skippable frames will be ignored (skipped) by decompressor.
1679
+ The format of skippable frames is as follows :
1680
+ a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
1681
+ b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
1682
+ c) Frame Content - any content (User Data) of length equal to Frame Size
1683
+ For skippable frames ZSTD_getFrameHeader() returns zfhPtr->frameType==ZSTD_skippableFrame.
1684
+ For skippable frames ZSTD_decompressContinue() always returns 0 : it only skips the content.
1685
+ */
1441
1686
 
1687
+ /*===== Buffer-less streaming decompression functions =====*/
1688
+ typedef enum { ZSTD_frame, ZSTD_skippableFrame } ZSTD_frameType_e;
1689
+ typedef struct {
1690
+ unsigned long long frameContentSize; /* if == ZSTD_CONTENTSIZE_UNKNOWN, it means this field is not available. 0 means "empty" */
1691
+ unsigned long long windowSize; /* can be very large, up to <= frameContentSize */
1692
+ unsigned blockSizeMax;
1693
+ ZSTD_frameType_e frameType; /* if == ZSTD_skippableFrame, frameContentSize is the size of skippable content */
1694
+ unsigned headerSize;
1695
+ unsigned dictID;
1696
+ unsigned checksumFlag;
1697
+ } ZSTD_frameHeader;
1442
1698
 
1699
+ /** ZSTD_getFrameHeader() :
1700
+ * decode Frame Header, or requires larger `srcSize`.
1701
+ * @return : 0, `zfhPtr` is correctly filled,
1702
+ * >0, `srcSize` is too small, value is wanted `srcSize` amount,
1703
+ * or an error code, which can be tested using ZSTD_isError() */
1704
+ ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input */
1443
1705
  /*! ZSTD_getFrameHeader_advanced() :
1444
1706
  * same as ZSTD_getFrameHeader(),
1445
1707
  * with added capability to select a format (like ZSTD_f_zstd1_magicless) */
1446
- ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr,
1447
- const void* src, size_t srcSize, ZSTD_format_e format);
1448
-
1449
-
1450
- /*! ZSTD_decompress_generic() :
1451
- * Behave the same as ZSTD_decompressStream.
1452
- * Decompression parameters cannot be changed once decompression is started.
1453
- * @return : an error code, which can be tested using ZSTD_isError()
1454
- * if >0, a hint, nb of expected input bytes for next invocation.
1455
- * `0` means : a frame has just been fully decoded and flushed.
1456
- */
1457
- ZSTDLIB_API size_t ZSTD_decompress_generic(ZSTD_DCtx* dctx,
1458
- ZSTD_outBuffer* output,
1459
- ZSTD_inBuffer* input);
1708
+ ZSTDLIB_API size_t ZSTD_getFrameHeader_advanced(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize, ZSTD_format_e format);
1709
+ ZSTDLIB_API size_t ZSTD_decodingBufferSize_min(unsigned long long windowSize, unsigned long long frameContentSize); /**< when frame content size is not known, pass in frameContentSize == ZSTD_CONTENTSIZE_UNKNOWN */
1460
1710
 
1711
+ ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
1712
+ ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
1713
+ ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict);
1461
1714
 
1462
- /*! ZSTD_decompress_generic_simpleArgs() :
1463
- * Same as ZSTD_decompress_generic(),
1464
- * but using only integral types as arguments.
1465
- * Argument list is larger than ZSTD_{in,out}Buffer,
1466
- * but can be helpful for binders from dynamic languages
1467
- * which have troubles handling structures containing memory pointers.
1468
- */
1469
- ZSTDLIB_API size_t ZSTD_decompress_generic_simpleArgs (
1470
- ZSTD_DCtx* dctx,
1471
- void* dst, size_t dstCapacity, size_t* dstPos,
1472
- const void* src, size_t srcSize, size_t* srcPos);
1715
+ ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
1716
+ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
1473
1717
 
1718
+ /* misc */
1719
+ ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
1720
+ typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
1721
+ ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
1474
1722
 
1475
- /*! ZSTD_DCtx_reset() :
1476
- * Return a DCtx to clean state.
1477
- * If a decompression was ongoing, any internal data not yet flushed is cancelled.
1478
- * All parameters are back to default values, including sticky ones.
1479
- * Dictionary (if any) is dropped.
1480
- * Parameters can be modified again after a reset.
1481
- */
1482
- ZSTDLIB_API void ZSTD_DCtx_reset(ZSTD_DCtx* dctx);
1483
1723
 
1484
1724
 
1485
1725
 
@@ -1501,10 +1741,10 @@ ZSTDLIB_API void ZSTD_DCtx_reset(ZSTD_DCtx* dctx);
1501
1741
  + copyCCtx() and copyDCtx() can be used too
1502
1742
  - Block size is limited, it must be <= ZSTD_getBlockSize() <= ZSTD_BLOCKSIZE_MAX == 128 KB
1503
1743
  + If input is larger than a block size, it's necessary to split input data into multiple blocks
1504
- + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead.
1744
+ + For inputs larger than a single block, really consider using regular ZSTD_compress() instead.
1505
1745
  Frame metadata is not that costly, and quickly becomes negligible as source size grows larger.
1506
1746
  - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
1507
- In which case, nothing is produced into `dst`.
1747
+ In which case, nothing is produced into `dst` !
1508
1748
  + User must test for such outcome and deal directly with uncompressed data
1509
1749
  + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
1510
1750
  + In case of multiple successive blocks, should some of them be uncompressed,