191 * Compatible with sticky parameters. |
220 * Compatible with sticky parameters. |
192 */ |
221 */ |
193 ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, |
222 ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* dctx, |
194 void* dst, size_t dstCapacity, |
223 void* dst, size_t dstCapacity, |
195 const void* src, size_t srcSize); |
224 const void* src, size_t srcSize); |
196 |
|
197 |
|
198 /************************** |
|
199 * Simple dictionary API |
|
200 ***************************/ |
|
201 /*! ZSTD_compress_usingDict() : |
|
202 * Compression at an explicit compression level using a Dictionary. |
|
203 * A dictionary can be any arbitrary data segment (also called a prefix), |
|
204 * or a buffer with specified information (see dictBuilder/zdict.h). |
|
205 * Note : This function loads the dictionary, resulting in significant startup delay. |
|
206 * It's intended for a dictionary used only once. |
|
207 * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
|
208 ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, |
|
209 void* dst, size_t dstCapacity, |
|
210 const void* src, size_t srcSize, |
|
211 const void* dict,size_t dictSize, |
|
212 int compressionLevel); |
|
213 |
|
214 /*! ZSTD_decompress_usingDict() : |
|
215 * Decompression using a known Dictionary. |
|
216 * Dictionary must be identical to the one used during compression. |
|
217 * Note : This function loads the dictionary, resulting in significant startup delay. |
|
218 * It's intended for a dictionary used only once. |
|
219 * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
|
220 ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
|
221 void* dst, size_t dstCapacity, |
|
222 const void* src, size_t srcSize, |
|
223 const void* dict,size_t dictSize); |
|
224 |
|
225 |
|
226 /*********************************** |
|
227 * Bulk processing dictionary API |
|
228 **********************************/ |
|
229 typedef struct ZSTD_CDict_s ZSTD_CDict; |
|
230 |
|
231 /*! ZSTD_createCDict() : |
|
232 * When compressing multiple messages / blocks using the same dictionary, it's recommended to load it only once. |
|
233 * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup cost. |
|
234 * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. |
|
235 * `dictBuffer` can be released after ZSTD_CDict creation, because its content is copied within CDict. |
|
236 * Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate `dictBuffer` content. |
|
237 * Note : A ZSTD_CDict can be created from an empty dictBuffer, but it is inefficient when used to compress small data. */ |
|
238 ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, |
|
239 int compressionLevel); |
|
240 |
|
241 /*! ZSTD_freeCDict() : |
|
242 * Function frees memory allocated by ZSTD_createCDict(). */ |
|
243 ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); |
|
244 |
|
245 /*! ZSTD_compress_usingCDict() : |
|
246 * Compression using a digested Dictionary. |
|
247 * Recommended when same dictionary is used multiple times. |
|
248 * Note : compression level is _decided at dictionary creation time_, |
|
249 * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ |
|
250 ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, |
|
251 void* dst, size_t dstCapacity, |
|
252 const void* src, size_t srcSize, |
|
253 const ZSTD_CDict* cdict); |
|
254 |
|
255 |
|
256 typedef struct ZSTD_DDict_s ZSTD_DDict; |
|
257 |
|
258 /*! ZSTD_createDDict() : |
|
259 * Create a digested dictionary, ready to start decompression operation without startup delay. |
|
260 * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */ |
|
261 ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); |
|
262 |
|
263 /*! ZSTD_freeDDict() : |
|
264 * Function frees memory allocated with ZSTD_createDDict() */ |
|
265 ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); |
|
266 |
|
267 /*! ZSTD_decompress_usingDDict() : |
|
268 * Decompression using a digested Dictionary. |
|
269 * Recommended when same dictionary is used multiple times. */ |
|
270 ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, |
|
271 void* dst, size_t dstCapacity, |
|
272 const void* src, size_t srcSize, |
|
273 const ZSTD_DDict* ddict); |
|
274 |
|
275 |
|
276 /**************************** |
|
277 * Streaming |
|
278 ****************************/ |
|
279 |
|
280 typedef struct ZSTD_inBuffer_s { |
|
281 const void* src; /**< start of input buffer */ |
|
282 size_t size; /**< size of input buffer */ |
|
283 size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ |
|
284 } ZSTD_inBuffer; |
|
285 |
|
286 typedef struct ZSTD_outBuffer_s { |
|
287 void* dst; /**< start of output buffer */ |
|
288 size_t size; /**< size of output buffer */ |
|
289 size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ |
|
290 } ZSTD_outBuffer; |
|
291 |
|
292 |
|
293 |
|
294 /*-*********************************************************************** |
|
295 * Streaming compression - HowTo |
|
296 * |
|
297 * A ZSTD_CStream object is required to track streaming operation. |
|
298 * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. |
|
299 * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. |
|
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. |
|
305 * |
|
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. |
|
311 * |
|
312 * Use ZSTD_compressStream() as many times as necessary to consume input stream. |
|
313 * The function will automatically update both `pos` fields within `input` and `output`. |
|
314 * Note that the function may not consume the entire input, |
|
315 * for example, because the output buffer is already full, |
|
316 * in which case `input.pos < input.size`. |
|
317 * The caller must check if input has been entirely consumed. |
|
318 * If not, the caller must make some room to receive more compressed data, |
|
319 * and then present again remaining input data. |
|
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() |
|
324 * |
|
325 * At any moment, it's possible to flush whatever data might remain stuck within internal buffer, |
|
326 * using ZSTD_flushStream(). `output->pos` will be updated. |
|
327 * Note that, if `output->size` is too small, a single invocation of ZSTD_flushStream() might not be enough (return code > 0). |
|
328 * In which case, make some room to receive more compressed data, and call again ZSTD_flushStream(). |
|
329 * @return : 0 if internal buffers are entirely flushed, |
|
330 * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), |
|
331 * or an error code, which can be tested using ZSTD_isError(). |
|
332 * |
|
333 * ZSTD_endStream() instructs to finish a frame. |
|
334 * It will perform a flush and write frame epilogue. |
|
335 * The epilogue is required for decoders to consider a frame completed. |
|
336 * flush() operation is the same, and follows same rules as ZSTD_flushStream(). |
|
337 * @return : 0 if frame fully completed and fully flushed, |
|
338 * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), |
|
339 * or an error code, which can be tested using ZSTD_isError(). |
|
340 * |
|
341 * *******************************************************************/ |
|
342 |
|
343 typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */ |
|
344 /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */ |
|
345 /*===== ZSTD_CStream management functions =====*/ |
|
346 ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); |
|
347 ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); |
|
348 |
|
349 /*===== Streaming compression functions =====*/ |
|
350 ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); |
|
351 ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
|
352 ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
|
353 ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
|
354 |
|
355 ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ |
|
356 ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block in all circumstances. */ |
|
357 |
|
358 |
|
359 |
|
360 /*-*************************************************************************** |
|
361 * Streaming decompression - HowTo |
|
362 * |
|
363 * A ZSTD_DStream object is required to track streaming operations. |
|
364 * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. |
|
365 * ZSTD_DStream objects can be re-used multiple times. |
|
366 * |
|
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. |
|
370 * |
|
371 * Use ZSTD_decompressStream() repetitively to consume your input. |
|
372 * The function will update both `pos` fields. |
|
373 * If `input.pos < input.size`, some input has not been consumed. |
|
374 * It's up to the caller to present again remaining data. |
|
375 * The function tries to flush all data decoded immediately, respecting output buffer size. |
|
376 * If `output.pos < output.size`, decoder has flushed everything it could. |
|
377 * But if `output.pos == output.size`, there might be some data left within internal buffers., |
|
378 * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer. |
|
379 * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX. |
|
380 * @return : 0 when a frame is completely decoded and fully flushed, |
|
381 * or an error code, which can be tested using ZSTD_isError(), |
|
382 * or any other value > 0, which means there is still some decoding or flushing to do to complete 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. |
|
385 * *******************************************************************************/ |
|
386 |
|
387 typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */ |
|
388 /* For compatibility with versions <= v1.2.0, prefer differentiating them. */ |
|
389 /*===== ZSTD_DStream management functions =====*/ |
|
390 ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); |
|
391 ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); |
|
392 |
|
393 /*===== Streaming decompression functions =====*/ |
|
394 ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); |
|
395 ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
|
396 |
|
397 ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ |
|
398 ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ |
|
399 |
|
400 #endif /* ZSTD_H_235446 */ |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 /**************************************************************************************** |
|
406 * ADVANCED AND EXPERIMENTAL FUNCTIONS |
|
407 **************************************************************************************** |
|
408 * The definitions in the following section are considered experimental. |
|
409 * They are provided for advanced scenarios. |
|
410 * They should never be used with a dynamic library, as prototypes may change in the future. |
|
411 * Use them only in association with static linking. |
|
412 * ***************************************************************************************/ |
|
413 |
|
414 #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) |
|
415 #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY |
|
416 |
|
417 |
|
418 /**************************************************************************************** |
|
419 * Candidate API for promotion to stable status |
|
420 **************************************************************************************** |
|
421 * The following symbols and constants form the "staging area" : |
|
422 * they are considered to join "stable API" by v1.4.0. |
|
423 * The proposal is written so that it can be made stable "as is", |
|
424 * though it's still possible to suggest improvements. |
|
425 * Staging is in fact last chance for changes, |
|
426 * the API is locked once reaching "stable" status. |
|
427 * ***************************************************************************************/ |
|
428 |
|
429 |
|
430 /* === Constants === */ |
|
431 |
|
432 /* all magic numbers are supposed read/written to/from files/memory using little-endian convention */ |
|
433 #define ZSTD_MAGICNUMBER 0xFD2FB528 /* valid since v0.8.0 */ |
|
434 #define ZSTD_MAGIC_DICTIONARY 0xEC30A437 /* valid since v0.7.0 */ |
|
435 #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50 /* all 16 values, from 0x184D2A50 to 0x184D2A5F, signal the beginning of a skippable frame */ |
|
436 #define ZSTD_MAGIC_SKIPPABLE_MASK 0xFFFFFFF0 |
|
437 |
|
438 #define ZSTD_BLOCKSIZELOG_MAX 17 |
|
439 #define ZSTD_BLOCKSIZE_MAX (1<<ZSTD_BLOCKSIZELOG_MAX) |
|
440 |
|
441 |
|
442 /* === query limits === */ |
|
443 |
|
444 ZSTDLIB_API int ZSTD_minCLevel(void); /*!< minimum negative compression level allowed */ |
|
445 |
|
446 |
|
447 /* === frame size === */ |
|
448 |
|
449 /*! ZSTD_findFrameCompressedSize() : |
|
450 * `src` should point to the start of a ZSTD frame or skippable frame. |
|
451 * `srcSize` must be >= first frame size |
|
452 * @return : the compressed size of the first frame starting at `src`, |
|
453 * suitable to pass as `srcSize` to `ZSTD_decompress` or similar, |
|
454 * or an error code if input is invalid */ |
|
455 ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); |
|
456 |
|
457 |
|
458 /* === Memory management === */ |
|
459 |
|
460 /*! ZSTD_sizeof_*() : |
|
461 * These functions give the _current_ memory usage of selected object. |
|
462 * Note that object memory usage can evolve (increase or decrease) over time. */ |
|
463 ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); |
|
464 ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); |
|
465 ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); |
|
466 ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); |
|
467 ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); |
|
468 ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); |
|
469 |
225 |
470 |
226 |
471 /*************************************** |
227 /*************************************** |
472 * Advanced compression API |
228 * Advanced compression API |
473 ***************************************/ |
229 ***************************************/ |
675 * Note 2 : pledgedSrcSize is only valid once, for the next frame. |
437 * 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. |
438 * 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, |
439 * Note 3 : Whenever all input data is provided and consumed in a single round, |
678 * for example with ZSTD_compress2(), |
440 * for example with ZSTD_compress2(), |
679 * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end), |
441 * or invoking immediately ZSTD_compressStream2(,,,ZSTD_e_end), |
680 * this value is automatically overriden by srcSize instead. |
442 * this value is automatically overridden by srcSize instead. |
681 */ |
443 */ |
682 ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); |
444 ZSTDLIB_API size_t ZSTD_CCtx_setPledgedSrcSize(ZSTD_CCtx* cctx, unsigned long long pledgedSrcSize); |
|
445 |
|
446 typedef enum { |
|
447 ZSTD_reset_session_only = 1, |
|
448 ZSTD_reset_parameters = 2, |
|
449 ZSTD_reset_session_and_parameters = 3 |
|
450 } ZSTD_ResetDirective; |
|
451 |
|
452 /*! ZSTD_CCtx_reset() : |
|
453 * There are 2 different things that can be reset, independently or jointly : |
|
454 * - The session : will stop compressing current frame, and make CCtx ready to start a new one. |
|
455 * Useful after an error, or to interrupt any ongoing compression. |
|
456 * Any internal data not yet flushed is cancelled. |
|
457 * Compression parameters and dictionary remain unchanged. |
|
458 * They will be used to compress next frame. |
|
459 * Resetting session never fails. |
|
460 * - The parameters : changes all parameters back to "default". |
|
461 * This removes any reference to any dictionary too. |
|
462 * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing) |
|
463 * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError()) |
|
464 * - Both : similar to resetting the session, followed by resetting parameters. |
|
465 */ |
|
466 ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset); |
|
467 |
|
468 /*! ZSTD_compress2() : |
|
469 * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API. |
|
470 * ZSTD_compress2() always starts a new frame. |
|
471 * Should cctx hold data from a previously unfinished frame, everything about it is forgotten. |
|
472 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() |
|
473 * - The function is always blocking, returns when compression is completed. |
|
474 * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. |
|
475 * @return : compressed size written into `dst` (<= `dstCapacity), |
|
476 * or an error code if it fails (which can be tested using ZSTD_isError()). |
|
477 */ |
|
478 ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx, |
|
479 void* dst, size_t dstCapacity, |
|
480 const void* src, size_t srcSize); |
|
481 |
|
482 |
|
483 /*************************************** |
|
484 * Advanced decompression API |
|
485 ***************************************/ |
|
486 |
|
487 /* The advanced API pushes parameters one by one into an existing DCtx context. |
|
488 * Parameters are sticky, and remain valid for all following frames |
|
489 * using the same DCtx context. |
|
490 * It's possible to reset parameters to default values using ZSTD_DCtx_reset(). |
|
491 * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream(). |
|
492 * Therefore, no new decompression function is necessary. |
|
493 */ |
|
494 |
|
495 typedef enum { |
|
496 |
|
497 ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which |
|
498 * the streaming API will refuse to allocate memory buffer |
|
499 * in order to protect the host from unreasonable memory requirements. |
|
500 * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. |
|
501 * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT). |
|
502 * Special: value 0 means "use default maximum windowLog". */ |
|
503 |
|
504 /* note : additional experimental parameters are also available |
|
505 * within the experimental section of the API. |
|
506 * At the time of this writing, they include : |
|
507 * ZSTD_c_format |
|
508 * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. |
|
509 * note : never ever use experimentalParam? names directly |
|
510 */ |
|
511 ZSTD_d_experimentalParam1=1000 |
|
512 |
|
513 } ZSTD_dParameter; |
|
514 |
|
515 /*! ZSTD_dParam_getBounds() : |
|
516 * All parameters must belong to an interval with lower and upper bounds, |
|
517 * otherwise they will either trigger an error or be automatically clamped. |
|
518 * @return : a structure, ZSTD_bounds, which contains |
|
519 * - an error status field, which must be tested using ZSTD_isError() |
|
520 * - both lower and upper bounds, inclusive |
|
521 */ |
|
522 ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam); |
|
523 |
|
524 /*! ZSTD_DCtx_setParameter() : |
|
525 * Set one compression parameter, selected by enum ZSTD_dParameter. |
|
526 * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds(). |
|
527 * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). |
|
528 * Setting a parameter is only possible during frame initialization (before starting decompression). |
|
529 * @return : 0, or an error code (which can be tested using ZSTD_isError()). |
|
530 */ |
|
531 ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value); |
|
532 |
|
533 /*! ZSTD_DCtx_reset() : |
|
534 * Return a DCtx to clean state. |
|
535 * Session and parameters can be reset jointly or separately. |
|
536 * Parameters can only be reset when no active frame is being decompressed. |
|
537 * @return : 0, or an error code, which can be tested with ZSTD_isError() |
|
538 */ |
|
539 ZSTDLIB_API size_t ZSTD_DCtx_reset(ZSTD_DCtx* dctx, ZSTD_ResetDirective reset); |
|
540 |
|
541 |
|
542 /**************************** |
|
543 * Streaming |
|
544 ****************************/ |
|
545 |
|
546 typedef struct ZSTD_inBuffer_s { |
|
547 const void* src; /**< start of input buffer */ |
|
548 size_t size; /**< size of input buffer */ |
|
549 size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ |
|
550 } ZSTD_inBuffer; |
|
551 |
|
552 typedef struct ZSTD_outBuffer_s { |
|
553 void* dst; /**< start of output buffer */ |
|
554 size_t size; /**< size of output buffer */ |
|
555 size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ |
|
556 } ZSTD_outBuffer; |
|
557 |
|
558 |
|
559 |
|
560 /*-*********************************************************************** |
|
561 * Streaming compression - HowTo |
|
562 * |
|
563 * A ZSTD_CStream object is required to track streaming operation. |
|
564 * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. |
|
565 * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. |
|
566 * It is recommended to re-use ZSTD_CStream since it will play nicer with system's memory, by re-using already allocated memory. |
|
567 * |
|
568 * For parallel execution, use one separate ZSTD_CStream per thread. |
|
569 * |
|
570 * note : since v1.3.0, ZSTD_CStream and ZSTD_CCtx are the same thing. |
|
571 * |
|
572 * Parameters are sticky : when starting a new compression on the same context, |
|
573 * it will re-use the same sticky parameters as previous compression session. |
|
574 * When in doubt, it's recommended to fully initialize the context before usage. |
|
575 * Use ZSTD_CCtx_reset() to reset the context and ZSTD_CCtx_setParameter(), |
|
576 * ZSTD_CCtx_setPledgedSrcSize(), or ZSTD_CCtx_loadDictionary() and friends to |
|
577 * set more specific parameters, the pledged source size, or load a dictionary. |
|
578 * |
|
579 * Use ZSTD_compressStream2() with ZSTD_e_continue as many times as necessary to |
|
580 * consume input stream. The function will automatically update both `pos` |
|
581 * fields within `input` and `output`. |
|
582 * Note that the function may not consume the entire input, for example, because |
|
583 * the output buffer is already full, in which case `input.pos < input.size`. |
|
584 * The caller must check if input has been entirely consumed. |
|
585 * If not, the caller must make some room to receive more compressed data, |
|
586 * and then present again remaining input data. |
|
587 * note: ZSTD_e_continue is guaranteed to make some forward progress when called, |
|
588 * but doesn't guarantee maximal forward progress. This is especially relevant |
|
589 * when compressing with multiple threads. The call won't block if it can |
|
590 * consume some input, but if it can't it will wait for some, but not all, |
|
591 * output to be flushed. |
|
592 * @return : provides a minimum amount of data remaining to be flushed from internal buffers |
|
593 * or an error code, which can be tested using ZSTD_isError(). |
|
594 * |
|
595 * At any moment, it's possible to flush whatever data might remain stuck within internal buffer, |
|
596 * using ZSTD_compressStream2() with ZSTD_e_flush. `output->pos` will be updated. |
|
597 * Note that, if `output->size` is too small, a single invocation with ZSTD_e_flush might not be enough (return code > 0). |
|
598 * In which case, make some room to receive more compressed data, and call again ZSTD_compressStream2() with ZSTD_e_flush. |
|
599 * You must continue calling ZSTD_compressStream2() with ZSTD_e_flush until it returns 0, at which point you can change the |
|
600 * operation. |
|
601 * note: ZSTD_e_flush will flush as much output as possible, meaning when compressing with multiple threads, it will |
|
602 * block until the flush is complete or the output buffer is full. |
|
603 * @return : 0 if internal buffers are entirely flushed, |
|
604 * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), |
|
605 * or an error code, which can be tested using ZSTD_isError(). |
|
606 * |
|
607 * Calling ZSTD_compressStream2() with ZSTD_e_end instructs to finish a frame. |
|
608 * It will perform a flush and write frame epilogue. |
|
609 * The epilogue is required for decoders to consider a frame completed. |
|
610 * flush operation is the same, and follows same rules as calling ZSTD_compressStream2() with ZSTD_e_flush. |
|
611 * You must continue calling ZSTD_compressStream2() with ZSTD_e_end until it returns 0, at which point you are free to |
|
612 * start a new frame. |
|
613 * note: ZSTD_e_end will flush as much output as possible, meaning when compressing with multiple threads, it will |
|
614 * block until the flush is complete or the output buffer is full. |
|
615 * @return : 0 if frame fully completed and fully flushed, |
|
616 * >0 if some data still present within internal buffer (the value is minimal estimation of remaining size), |
|
617 * or an error code, which can be tested using ZSTD_isError(). |
|
618 * |
|
619 * *******************************************************************/ |
|
620 |
|
621 typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are now effectively same object (>= v1.3.0) */ |
|
622 /* Continue to distinguish them for compatibility with older versions <= v1.2.0 */ |
|
623 /*===== ZSTD_CStream management functions =====*/ |
|
624 ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); |
|
625 ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); |
|
626 |
|
627 /*===== Streaming compression functions =====*/ |
|
628 typedef enum { |
|
629 ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */ |
|
630 ZSTD_e_flush=1, /* flush any data provided so far, |
|
631 * it creates (at least) one new block, that can be decoded immediately on reception; |
|
632 * frame will continue: any future data can still reference previously compressed data, improving compression. |
|
633 * note : multithreaded compression will block to flush as much output as possible. */ |
|
634 ZSTD_e_end=2 /* flush any remaining data _and_ close current frame. |
|
635 * note that frame is only closed after compressed data is fully flushed (return value == 0). |
|
636 * After that point, any additional data starts a new frame. |
|
637 * note : each frame is independent (does not reference any content from previous frame). |
|
638 : note : multithreaded compression will block to flush as much output as possible. */ |
|
639 } ZSTD_EndDirective; |
|
640 |
|
641 /*! ZSTD_compressStream2() : |
|
642 * Behaves about the same as ZSTD_compressStream, with additional control on end directive. |
|
643 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() |
|
644 * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode) |
|
645 * - output->pos must be <= dstCapacity, input->pos must be <= srcSize |
|
646 * - output->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. |
|
647 * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller. |
|
648 * - 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, |
|
649 * and then immediately returns, just indicating that there is some data remaining to be flushed. |
|
650 * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. |
|
651 * - 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. |
|
652 * - @return provides a minimum amount of data remaining to be flushed from internal buffers |
|
653 * or an error code, which can be tested using ZSTD_isError(). |
|
654 * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. |
|
655 * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. |
|
656 * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. |
|
657 * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), |
|
658 * only ZSTD_e_end or ZSTD_e_flush operations are allowed. |
|
659 * Before starting a new compression job, or changing compression parameters, |
|
660 * it is required to fully flush internal buffers. |
|
661 */ |
|
662 ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx, |
|
663 ZSTD_outBuffer* output, |
|
664 ZSTD_inBuffer* input, |
|
665 ZSTD_EndDirective endOp); |
|
666 |
|
667 |
|
668 /* These buffer sizes are softly recommended. |
|
669 * They are not required : ZSTD_compressStream*() happily accepts any buffer size, for both input and output. |
|
670 * Respecting the recommended size just makes it a bit easier for ZSTD_compressStream*(), |
|
671 * reducing the amount of memory shuffling and buffering, resulting in minor performance savings. |
|
672 * |
|
673 * However, note that these recommendations are from the perspective of a C caller program. |
|
674 * If the streaming interface is invoked from some other language, |
|
675 * especially managed ones such as Java or Go, through a foreign function interface such as jni or cgo, |
|
676 * a major performance rule is to reduce crossing such interface to an absolute minimum. |
|
677 * It's not rare that performance ends being spent more into the interface, rather than compression itself. |
|
678 * In which cases, prefer using large buffers, as large as practical, |
|
679 * for both input and output, to reduce the nb of roundtrips. |
|
680 */ |
|
681 ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ |
|
682 ZSTDLIB_API size_t ZSTD_CStreamOutSize(void); /**< recommended size for output buffer. Guarantee to successfully flush at least one complete compressed block. */ |
|
683 |
|
684 |
|
685 /* ***************************************************************************** |
|
686 * This following is a legacy streaming API. |
|
687 * It can be replaced by ZSTD_CCtx_reset() and ZSTD_compressStream2(). |
|
688 * It is redundant, but remains fully supported. |
|
689 * Advanced parameters and dictionary compression can only be used through the |
|
690 * new API. |
|
691 ******************************************************************************/ |
|
692 |
|
693 /*! |
|
694 * Equivalent to: |
|
695 * |
|
696 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
|
697 * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) |
|
698 * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); |
|
699 */ |
|
700 ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); |
|
701 /*! |
|
702 * Alternative for ZSTD_compressStream2(zcs, output, input, ZSTD_e_continue). |
|
703 * NOTE: The return value is different. ZSTD_compressStream() returns a hint for |
|
704 * the next read size (if non-zero and not an error). ZSTD_compressStream2() |
|
705 * returns the minimum nb of bytes left to flush (if non-zero and not an error). |
|
706 */ |
|
707 ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
|
708 /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_flush). */ |
|
709 ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
|
710 /*! Equivalent to ZSTD_compressStream2(zcs, output, &emptyInput, ZSTD_e_end). */ |
|
711 ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
|
712 |
|
713 |
|
714 /*-*************************************************************************** |
|
715 * Streaming decompression - HowTo |
|
716 * |
|
717 * A ZSTD_DStream object is required to track streaming operations. |
|
718 * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. |
|
719 * ZSTD_DStream objects can be re-used multiple times. |
|
720 * |
|
721 * Use ZSTD_initDStream() to start a new decompression operation. |
|
722 * @return : recommended first input size |
|
723 * Alternatively, use advanced API to set specific properties. |
|
724 * |
|
725 * Use ZSTD_decompressStream() repetitively to consume your input. |
|
726 * The function will update both `pos` fields. |
|
727 * If `input.pos < input.size`, some input has not been consumed. |
|
728 * It's up to the caller to present again remaining data. |
|
729 * The function tries to flush all data decoded immediately, respecting output buffer size. |
|
730 * If `output.pos < output.size`, decoder has flushed everything it could. |
|
731 * But if `output.pos == output.size`, there might be some data left within internal buffers., |
|
732 * In which case, call ZSTD_decompressStream() again to flush whatever remains in the buffer. |
|
733 * Note : with no additional input provided, amount of data flushed is necessarily <= ZSTD_BLOCKSIZE_MAX. |
|
734 * @return : 0 when a frame is completely decoded and fully flushed, |
|
735 * or an error code, which can be tested using ZSTD_isError(), |
|
736 * or any other value > 0, which means there is still some decoding or flushing to do to complete current frame : |
|
737 * the return value is a suggested next input size (just a hint for better latency) |
|
738 * that will never request more than the remaining frame size. |
|
739 * *******************************************************************************/ |
|
740 |
|
741 typedef ZSTD_DCtx ZSTD_DStream; /**< DCtx and DStream are now effectively same object (>= v1.3.0) */ |
|
742 /* For compatibility with versions <= v1.2.0, prefer differentiating them. */ |
|
743 /*===== ZSTD_DStream management functions =====*/ |
|
744 ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); |
|
745 ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); |
|
746 |
|
747 /*===== Streaming decompression functions =====*/ |
|
748 |
|
749 /* This function is redundant with the advanced API and equivalent to: |
|
750 * |
|
751 * ZSTD_DCtx_reset(zds); |
|
752 * ZSTD_DCtx_refDDict(zds, NULL); |
|
753 */ |
|
754 ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); |
|
755 |
|
756 ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
|
757 |
|
758 ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ |
|
759 ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ |
|
760 |
|
761 |
|
762 /************************** |
|
763 * Simple dictionary API |
|
764 ***************************/ |
|
765 /*! ZSTD_compress_usingDict() : |
|
766 * Compression at an explicit compression level using a Dictionary. |
|
767 * A dictionary can be any arbitrary data segment (also called a prefix), |
|
768 * or a buffer with specified information (see dictBuilder/zdict.h). |
|
769 * Note : This function loads the dictionary, resulting in significant startup delay. |
|
770 * It's intended for a dictionary used only once. |
|
771 * Note 2 : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
|
772 ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, |
|
773 void* dst, size_t dstCapacity, |
|
774 const void* src, size_t srcSize, |
|
775 const void* dict,size_t dictSize, |
|
776 int compressionLevel); |
|
777 |
|
778 /*! ZSTD_decompress_usingDict() : |
|
779 * Decompression using a known Dictionary. |
|
780 * Dictionary must be identical to the one used during compression. |
|
781 * Note : This function loads the dictionary, resulting in significant startup delay. |
|
782 * It's intended for a dictionary used only once. |
|
783 * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
|
784 ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
|
785 void* dst, size_t dstCapacity, |
|
786 const void* src, size_t srcSize, |
|
787 const void* dict,size_t dictSize); |
|
788 |
|
789 |
|
790 /*********************************** |
|
791 * Bulk processing dictionary API |
|
792 **********************************/ |
|
793 typedef struct ZSTD_CDict_s ZSTD_CDict; |
|
794 |
|
795 /*! ZSTD_createCDict() : |
|
796 * When compressing multiple messages / blocks using the same dictionary, it's recommended to load it only once. |
|
797 * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup cost. |
|
798 * ZSTD_CDict can be created once and shared by multiple threads concurrently, since its usage is read-only. |
|
799 * `dictBuffer` can be released after ZSTD_CDict creation, because its content is copied within CDict. |
|
800 * Consider experimental function `ZSTD_createCDict_byReference()` if you prefer to not duplicate `dictBuffer` content. |
|
801 * Note : A ZSTD_CDict can be created from an empty dictBuffer, but it is inefficient when used to compress small data. */ |
|
802 ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, |
|
803 int compressionLevel); |
|
804 |
|
805 /*! ZSTD_freeCDict() : |
|
806 * Function frees memory allocated by ZSTD_createCDict(). */ |
|
807 ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); |
|
808 |
|
809 /*! ZSTD_compress_usingCDict() : |
|
810 * Compression using a digested Dictionary. |
|
811 * Recommended when same dictionary is used multiple times. |
|
812 * Note : compression level is _decided at dictionary creation time_, |
|
813 * and frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ |
|
814 ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, |
|
815 void* dst, size_t dstCapacity, |
|
816 const void* src, size_t srcSize, |
|
817 const ZSTD_CDict* cdict); |
|
818 |
|
819 |
|
820 typedef struct ZSTD_DDict_s ZSTD_DDict; |
|
821 |
|
822 /*! ZSTD_createDDict() : |
|
823 * Create a digested dictionary, ready to start decompression operation without startup delay. |
|
824 * dictBuffer can be released after DDict creation, as its content is copied inside DDict. */ |
|
825 ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); |
|
826 |
|
827 /*! ZSTD_freeDDict() : |
|
828 * Function frees memory allocated with ZSTD_createDDict() */ |
|
829 ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); |
|
830 |
|
831 /*! ZSTD_decompress_usingDDict() : |
|
832 * Decompression using a digested Dictionary. |
|
833 * Recommended when same dictionary is used multiple times. */ |
|
834 ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, |
|
835 void* dst, size_t dstCapacity, |
|
836 const void* src, size_t srcSize, |
|
837 const ZSTD_DDict* ddict); |
|
838 |
|
839 |
|
840 /******************************** |
|
841 * Dictionary helper functions |
|
842 *******************************/ |
|
843 |
|
844 /*! ZSTD_getDictID_fromDict() : |
|
845 * Provides the dictID stored within dictionary. |
|
846 * if @return == 0, the dictionary is not conformant with Zstandard specification. |
|
847 * It can still be loaded, but as a content-only dictionary. */ |
|
848 ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); |
|
849 |
|
850 /*! ZSTD_getDictID_fromDDict() : |
|
851 * Provides the dictID of the dictionary loaded into `ddict`. |
|
852 * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. |
|
853 * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ |
|
854 ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); |
|
855 |
|
856 /*! ZSTD_getDictID_fromFrame() : |
|
857 * Provides the dictID required to decompressed the frame stored within `src`. |
|
858 * If @return == 0, the dictID could not be decoded. |
|
859 * This could for one of the following reasons : |
|
860 * - The frame does not require a dictionary to be decoded (most common case). |
|
861 * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. |
|
862 * Note : this use case also happens when using a non-conformant dictionary. |
|
863 * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). |
|
864 * - This is not a Zstandard frame. |
|
865 * When identifying the exact failure cause, it's possible to use ZSTD_getFrameHeader(), which will provide a more precise error code. */ |
|
866 ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); |
|
867 |
|
868 |
|
869 /******************************************************************************* |
|
870 * Advanced dictionary and prefix API |
|
871 * |
|
872 * This API allows dictionaries to be used with ZSTD_compress2(), |
|
873 * ZSTD_compressStream2(), and ZSTD_decompress(). Dictionaries are sticky, and |
|
874 * only reset with the context is reset with ZSTD_reset_parameters or |
|
875 * ZSTD_reset_session_and_parameters. Prefixes are single-use. |
|
876 ******************************************************************************/ |
|
877 |
683 |
878 |
684 /*! ZSTD_CCtx_loadDictionary() : |
879 /*! ZSTD_CCtx_loadDictionary() : |
685 * Create an internal CDict from `dict` buffer. |
880 * Create an internal CDict from `dict` buffer. |
686 * Decompression will have to use same dictionary. |
881 * Decompression will have to use same dictionary. |
687 * @result : 0, or an error code (which can be tested with ZSTD_isError()). |
882 * @result : 0, or an error code (which can be tested with ZSTD_isError()). |
731 * Note 4 : By default, the prefix is interpreted as raw content (ZSTD_dm_rawContent). |
928 * 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. */ |
929 * Use experimental ZSTD_CCtx_refPrefix_advanced() to alter dictionary interpretation. */ |
733 ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, |
930 ZSTDLIB_API size_t ZSTD_CCtx_refPrefix(ZSTD_CCtx* cctx, |
734 const void* prefix, size_t prefixSize); |
931 const void* prefix, size_t prefixSize); |
735 |
932 |
736 |
|
737 typedef enum { |
|
738 ZSTD_reset_session_only = 1, |
|
739 ZSTD_reset_parameters = 2, |
|
740 ZSTD_reset_session_and_parameters = 3 |
|
741 } ZSTD_ResetDirective; |
|
742 |
|
743 /*! ZSTD_CCtx_reset() : |
|
744 * There are 2 different things that can be reset, independently or jointly : |
|
745 * - The session : will stop compressing current frame, and make CCtx ready to start a new one. |
|
746 * Useful after an error, or to interrupt any ongoing compression. |
|
747 * Any internal data not yet flushed is cancelled. |
|
748 * Compression parameters and dictionary remain unchanged. |
|
749 * They will be used to compress next frame. |
|
750 * Resetting session never fails. |
|
751 * - The parameters : changes all parameters back to "default". |
|
752 * This removes any reference to any dictionary too. |
|
753 * Parameters can only be changed between 2 sessions (i.e. no compression is currently ongoing) |
|
754 * otherwise the reset fails, and function returns an error value (which can be tested using ZSTD_isError()) |
|
755 * - Both : similar to resetting the session, followed by resetting parameters. |
|
756 */ |
|
757 ZSTDLIB_API size_t ZSTD_CCtx_reset(ZSTD_CCtx* cctx, ZSTD_ResetDirective reset); |
|
758 |
|
759 |
|
760 |
|
761 /*! ZSTD_compress2() : |
|
762 * Behave the same as ZSTD_compressCCtx(), but compression parameters are set using the advanced API. |
|
763 * ZSTD_compress2() always starts a new frame. |
|
764 * Should cctx hold data from a previously unfinished frame, everything about it is forgotten. |
|
765 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() |
|
766 * - The function is always blocking, returns when compression is completed. |
|
767 * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. |
|
768 * @return : compressed size written into `dst` (<= `dstCapacity), |
|
769 * or an error code if it fails (which can be tested using ZSTD_isError()). |
|
770 */ |
|
771 ZSTDLIB_API size_t ZSTD_compress2( ZSTD_CCtx* cctx, |
|
772 void* dst, size_t dstCapacity, |
|
773 const void* src, size_t srcSize); |
|
774 |
|
775 typedef enum { |
|
776 ZSTD_e_continue=0, /* collect more data, encoder decides when to output compressed result, for optimal compression ratio */ |
|
777 ZSTD_e_flush=1, /* flush any data provided so far, |
|
778 * it creates (at least) one new block, that can be decoded immediately on reception; |
|
779 * frame will continue: any future data can still reference previously compressed data, improving compression. */ |
|
780 ZSTD_e_end=2 /* flush any remaining data _and_ close current frame. |
|
781 * note that frame is only closed after compressed data is fully flushed (return value == 0). |
|
782 * After that point, any additional data starts a new frame. |
|
783 * note : each frame is independent (does not reference any content from previous frame). */ |
|
784 } ZSTD_EndDirective; |
|
785 |
|
786 /*! ZSTD_compressStream2() : |
|
787 * Behaves about the same as ZSTD_compressStream, with additional control on end directive. |
|
788 * - Compression parameters are pushed into CCtx before starting compression, using ZSTD_CCtx_set*() |
|
789 * - Compression parameters cannot be changed once compression is started (save a list of exceptions in multi-threading mode) |
|
790 * - outpot->pos must be <= dstCapacity, input->pos must be <= srcSize |
|
791 * - outpot->pos and input->pos will be updated. They are guaranteed to remain below their respective limit. |
|
792 * - When nbWorkers==0 (default), function is blocking : it completes its job before returning to caller. |
|
793 * - When nbWorkers>=1, function is non-blocking : it just acquires a copy of input, and distributes jobs to internal worker threads, flush whatever is available, |
|
794 * and then immediately returns, just indicating that there is some data remaining to be flushed. |
|
795 * The function nonetheless guarantees forward progress : it will return only after it reads or write at least 1+ byte. |
|
796 * - Exception : if the first call requests a ZSTD_e_end directive and provides enough dstCapacity, the function delegates to ZSTD_compress2() which is always blocking. |
|
797 * - @return provides a minimum amount of data remaining to be flushed from internal buffers |
|
798 * or an error code, which can be tested using ZSTD_isError(). |
|
799 * if @return != 0, flush is not fully completed, there is still some data left within internal buffers. |
|
800 * This is useful for ZSTD_e_flush, since in this case more flushes are necessary to empty all buffers. |
|
801 * For ZSTD_e_end, @return == 0 when internal buffers are fully flushed and frame is completed. |
|
802 * - after a ZSTD_e_end directive, if internal buffer is not fully flushed (@return != 0), |
|
803 * only ZSTD_e_end or ZSTD_e_flush operations are allowed. |
|
804 * Before starting a new compression job, or changing compression parameters, |
|
805 * it is required to fully flush internal buffers. |
|
806 */ |
|
807 ZSTDLIB_API size_t ZSTD_compressStream2( ZSTD_CCtx* cctx, |
|
808 ZSTD_outBuffer* output, |
|
809 ZSTD_inBuffer* input, |
|
810 ZSTD_EndDirective endOp); |
|
811 |
|
812 |
|
813 |
|
814 /* ============================== */ |
|
815 /* Advanced decompression API */ |
|
816 /* ============================== */ |
|
817 |
|
818 /* The advanced API pushes parameters one by one into an existing DCtx context. |
|
819 * Parameters are sticky, and remain valid for all following frames |
|
820 * using the same DCtx context. |
|
821 * It's possible to reset parameters to default values using ZSTD_DCtx_reset(). |
|
822 * Note : This API is compatible with existing ZSTD_decompressDCtx() and ZSTD_decompressStream(). |
|
823 * Therefore, no new decompression function is necessary. |
|
824 */ |
|
825 |
|
826 |
|
827 typedef enum { |
|
828 |
|
829 ZSTD_d_windowLogMax=100, /* Select a size limit (in power of 2) beyond which |
|
830 * the streaming API will refuse to allocate memory buffer |
|
831 * in order to protect the host from unreasonable memory requirements. |
|
832 * This parameter is only useful in streaming mode, since no internal buffer is allocated in single-pass mode. |
|
833 * By default, a decompression context accepts window sizes <= (1 << ZSTD_WINDOWLOG_LIMIT_DEFAULT) */ |
|
834 |
|
835 /* note : additional experimental parameters are also available |
|
836 * within the experimental section of the API. |
|
837 * At the time of this writing, they include : |
|
838 * ZSTD_c_format |
|
839 * Because they are not stable, it's necessary to define ZSTD_STATIC_LINKING_ONLY to access them. |
|
840 * note : never ever use experimentalParam? names directly |
|
841 */ |
|
842 ZSTD_d_experimentalParam1=1000 |
|
843 |
|
844 } ZSTD_dParameter; |
|
845 |
|
846 |
|
847 /*! ZSTD_dParam_getBounds() : |
|
848 * All parameters must belong to an interval with lower and upper bounds, |
|
849 * otherwise they will either trigger an error or be automatically clamped. |
|
850 * @return : a structure, ZSTD_bounds, which contains |
|
851 * - an error status field, which must be tested using ZSTD_isError() |
|
852 * - both lower and upper bounds, inclusive |
|
853 */ |
|
854 ZSTDLIB_API ZSTD_bounds ZSTD_dParam_getBounds(ZSTD_dParameter dParam); |
|
855 |
|
856 /*! ZSTD_DCtx_setParameter() : |
|
857 * Set one compression parameter, selected by enum ZSTD_dParameter. |
|
858 * All parameters have valid bounds. Bounds can be queried using ZSTD_dParam_getBounds(). |
|
859 * Providing a value beyond bound will either clamp it, or trigger an error (depending on parameter). |
|
860 * Setting a parameter is only possible during frame initialization (before starting decompression). |
|
861 * @return : 0, or an error code (which can be tested using ZSTD_isError()). |
|
862 */ |
|
863 ZSTDLIB_API size_t ZSTD_DCtx_setParameter(ZSTD_DCtx* dctx, ZSTD_dParameter param, int value); |
|
864 |
|
865 |
|
866 /*! ZSTD_DCtx_loadDictionary() : |
933 /*! ZSTD_DCtx_loadDictionary() : |
867 * Create an internal DDict from dict buffer, |
934 * Create an internal DDict from dict buffer, |
868 * to be used to decompress next frames. |
935 * to be used to decompress next frames. |
869 * The dictionary remains valid for all future frames, until explicitly invalidated. |
936 * 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()). |
937 * @result : 0, or an error code (which can be tested with ZSTD_isError()). |
1499 * Once Advanced API reaches "stable" status, |
1601 * Once Advanced API reaches "stable" status, |
1500 * redundant functions will be deprecated, and then at some point removed. |
1602 * redundant functions will be deprecated, and then at some point removed. |
1501 ********************************************************************/ |
1603 ********************************************************************/ |
1502 |
1604 |
1503 /*===== Advanced Streaming compression functions =====*/ |
1605 /*===== 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. */ |
1606 /**! ZSTD_initCStream_srcSize() : |
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.*/ |
1607 * This function is deprecated, and equivalent to: |
|
1608 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
|
1609 * ZSTD_CCtx_refCDict(zcs, NULL); // clear the dictionary (if any) |
|
1610 * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); |
|
1611 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); |
|
1612 * |
|
1613 * pledgedSrcSize must be correct. If it is not known at init time, use |
|
1614 * ZSTD_CONTENTSIZE_UNKNOWN. Note that, for compatibility with older programs, |
|
1615 * "0" also disables frame content size field. It may be enabled in the future. |
|
1616 */ |
|
1617 ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); |
|
1618 /**! ZSTD_initCStream_usingDict() : |
|
1619 * This function is deprecated, and is equivalent to: |
|
1620 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
|
1621 * ZSTD_CCtx_setParameter(zcs, ZSTD_c_compressionLevel, compressionLevel); |
|
1622 * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); |
|
1623 * |
|
1624 * Creates of an internal CDict (incompatible with static CCtx), except if |
|
1625 * dict == NULL or dictSize < 8, in which case no dict is used. |
|
1626 * Note: dict is loaded with ZSTD_dm_auto (treated as a full zstd dictionary if |
|
1627 * it begins with ZSTD_MAGIC_DICTIONARY, else as raw content) and ZSTD_dlm_byCopy. |
|
1628 */ |
|
1629 ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); |
|
1630 /**! ZSTD_initCStream_advanced() : |
|
1631 * This function is deprecated, and is approximately equivalent to: |
|
1632 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
|
1633 * ZSTD_CCtx_setZstdParams(zcs, params); // Set the zstd params and leave the rest as-is |
|
1634 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); |
|
1635 * ZSTD_CCtx_loadDictionary(zcs, dict, dictSize); |
|
1636 * |
|
1637 * pledgedSrcSize must be correct. If srcSize is not known at init time, use |
|
1638 * value ZSTD_CONTENTSIZE_UNKNOWN. dict is loaded with ZSTD_dm_auto and ZSTD_dlm_byCopy. |
|
1639 */ |
1506 ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, |
1640 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. */ |
1641 ZSTD_parameters params, unsigned long long pledgedSrcSize); |
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 */ |
1642 /**! ZSTD_initCStream_usingCDict() : |
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. */ |
1643 * This function is deprecated, and equivalent to: |
|
1644 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
|
1645 * ZSTD_CCtx_refCDict(zcs, cdict); |
|
1646 * |
|
1647 * note : cdict will just be referenced, and must outlive compression session |
|
1648 */ |
|
1649 ZSTDLIB_API size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict); |
|
1650 /**! ZSTD_initCStream_usingCDict_advanced() : |
|
1651 * This function is deprecated, and is approximately equivalent to: |
|
1652 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
|
1653 * ZSTD_CCtx_setZstdFrameParams(zcs, fParams); // Set the zstd frame params and leave the rest as-is |
|
1654 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); |
|
1655 * ZSTD_CCtx_refCDict(zcs, cdict); |
|
1656 * |
|
1657 * same as ZSTD_initCStream_usingCDict(), with control over frame parameters. |
|
1658 * pledgedSrcSize must be correct. If srcSize is not known at init time, use |
|
1659 * value ZSTD_CONTENTSIZE_UNKNOWN. |
|
1660 */ |
|
1661 ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, ZSTD_frameParameters fParams, unsigned long long pledgedSrcSize); |
1510 |
1662 |
1511 /*! ZSTD_resetCStream() : |
1663 /*! ZSTD_resetCStream() : |
|
1664 * This function is deprecated, and is equivalent to: |
|
1665 * ZSTD_CCtx_reset(zcs, ZSTD_reset_session_only); |
|
1666 * ZSTD_CCtx_setPledgedSrcSize(zcs, pledgedSrcSize); |
|
1667 * |
1512 * start a new frame, using same parameters from previous frame. |
1668 * 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. |
1669 * 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(). |
1670 * 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. |
1671 * 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. |
1672 * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. |