10c16b537SWarner Losh /* 2*5ff13fbcSAllan Jude * Copyright (c) Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh 110c16b537SWarner Losh /* *************************************************************** 120c16b537SWarner Losh * NOTES/WARNINGS 130c16b537SWarner Losh ******************************************************************/ 140c16b537SWarner Losh /* The streaming API defined here is deprecated. 150c16b537SWarner Losh * Consider migrating towards ZSTD_compressStream() API in `zstd.h` 160c16b537SWarner Losh * See 'lib/README.md'. 170c16b537SWarner Losh *****************************************************************/ 180c16b537SWarner Losh 190c16b537SWarner Losh 200c16b537SWarner Losh #if defined (__cplusplus) 210c16b537SWarner Losh extern "C" { 220c16b537SWarner Losh #endif 230c16b537SWarner Losh 240c16b537SWarner Losh #ifndef ZSTD_BUFFERED_H_23987 250c16b537SWarner Losh #define ZSTD_BUFFERED_H_23987 260c16b537SWarner Losh 270c16b537SWarner Losh /* ************************************* 280c16b537SWarner Losh * Dependencies 290c16b537SWarner Losh ***************************************/ 300c16b537SWarner Losh #include <stddef.h> /* size_t */ 3137f1f268SConrad Meyer #include "../zstd.h" /* ZSTD_CStream, ZSTD_DStream, ZSTDLIB_API */ 320c16b537SWarner Losh 330c16b537SWarner Losh 340c16b537SWarner Losh /* *************************************************************** 350c16b537SWarner Losh * Compiler specifics 360c16b537SWarner Losh *****************************************************************/ 370c16b537SWarner Losh /* Deprecation warnings */ 380c16b537SWarner Losh /* Should these warnings be a problem, 399cbefe25SConrad Meyer * it is generally possible to disable them, 409cbefe25SConrad Meyer * typically with -Wno-deprecated-declarations for gcc 419cbefe25SConrad Meyer * or _CRT_SECURE_NO_WARNINGS in Visual. 429cbefe25SConrad Meyer * Otherwise, it's also possible to define ZBUFF_DISABLE_DEPRECATE_WARNINGS 439cbefe25SConrad Meyer */ 440c16b537SWarner Losh #ifdef ZBUFF_DISABLE_DEPRECATE_WARNINGS 450c16b537SWarner Losh # define ZBUFF_DEPRECATED(message) ZSTDLIB_API /* disable deprecation warnings */ 460c16b537SWarner Losh #else 470c16b537SWarner Losh # if defined (__cplusplus) && (__cplusplus >= 201402) /* C++14 or greater */ 480c16b537SWarner Losh # define ZBUFF_DEPRECATED(message) [[deprecated(message)]] ZSTDLIB_API 499cbefe25SConrad Meyer # elif (defined(GNUC) && (GNUC > 4 || (GNUC == 4 && GNUC_MINOR >= 5))) || defined(__clang__) 500c16b537SWarner Losh # define ZBUFF_DEPRECATED(message) ZSTDLIB_API __attribute__((deprecated(message))) 510c16b537SWarner Losh # elif defined(__GNUC__) && (__GNUC__ >= 3) 520c16b537SWarner Losh # define ZBUFF_DEPRECATED(message) ZSTDLIB_API __attribute__((deprecated)) 530c16b537SWarner Losh # elif defined(_MSC_VER) 540c16b537SWarner Losh # define ZBUFF_DEPRECATED(message) ZSTDLIB_API __declspec(deprecated(message)) 550c16b537SWarner Losh # else 560c16b537SWarner Losh # pragma message("WARNING: You need to implement ZBUFF_DEPRECATED for this compiler") 570c16b537SWarner Losh # define ZBUFF_DEPRECATED(message) ZSTDLIB_API 580c16b537SWarner Losh # endif 590c16b537SWarner Losh #endif /* ZBUFF_DISABLE_DEPRECATE_WARNINGS */ 600c16b537SWarner Losh 610c16b537SWarner Losh 620c16b537SWarner Losh /* ************************************* 630c16b537SWarner Losh * Streaming functions 640c16b537SWarner Losh ***************************************/ 650c16b537SWarner Losh /* This is the easier "buffered" streaming API, 660c16b537SWarner Losh * using an internal buffer to lift all restrictions on user-provided buffers 670c16b537SWarner Losh * which can be any size, any place, for both input and output. 680c16b537SWarner Losh * ZBUFF and ZSTD are 100% interoperable, 690c16b537SWarner Losh * frames created by one can be decoded by the other one */ 700c16b537SWarner Losh 710c16b537SWarner Losh typedef ZSTD_CStream ZBUFF_CCtx; 720c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_createCStream") ZBUFF_CCtx* ZBUFF_createCCtx(void); 730c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_freeCStream") size_t ZBUFF_freeCCtx(ZBUFF_CCtx* cctx); 740c16b537SWarner Losh 750c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_initCStream") size_t ZBUFF_compressInit(ZBUFF_CCtx* cctx, int compressionLevel); 760c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_initCStream_usingDict") size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); 770c16b537SWarner Losh 780c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_compressStream") size_t ZBUFF_compressContinue(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr, const void* src, size_t* srcSizePtr); 790c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_flushStream") size_t ZBUFF_compressFlush(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr); 800c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_endStream") size_t ZBUFF_compressEnd(ZBUFF_CCtx* cctx, void* dst, size_t* dstCapacityPtr); 810c16b537SWarner Losh 820c16b537SWarner Losh /*-************************************************* 830c16b537SWarner Losh * Streaming compression - howto 840c16b537SWarner Losh * 850c16b537SWarner Losh * A ZBUFF_CCtx object is required to track streaming operation. 860c16b537SWarner Losh * Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources. 870c16b537SWarner Losh * ZBUFF_CCtx objects can be reused multiple times. 880c16b537SWarner Losh * 890c16b537SWarner Losh * Start by initializing ZBUF_CCtx. 900c16b537SWarner Losh * Use ZBUFF_compressInit() to start a new compression operation. 910c16b537SWarner Losh * Use ZBUFF_compressInitDictionary() for a compression which requires a dictionary. 920c16b537SWarner Losh * 930c16b537SWarner Losh * Use ZBUFF_compressContinue() repetitively to consume input stream. 940c16b537SWarner Losh * *srcSizePtr and *dstCapacityPtr can be any size. 950c16b537SWarner Losh * The function will report how many bytes were read or written within *srcSizePtr and *dstCapacityPtr. 960c16b537SWarner Losh * Note that it may not consume the entire input, in which case it's up to the caller to present again remaining data. 970c16b537SWarner Losh * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each call, so save its content if it matters or change @dst . 980c16b537SWarner Losh * @return : a hint to preferred nb of bytes to use as input for next function call (it's just a hint, to improve latency) 990c16b537SWarner Losh * or an error code, which can be tested using ZBUFF_isError(). 1000c16b537SWarner Losh * 1010c16b537SWarner Losh * At any moment, it's possible to flush whatever data remains within buffer, using ZBUFF_compressFlush(). 1020c16b537SWarner Losh * The nb of bytes written into `dst` will be reported into *dstCapacityPtr. 1030c16b537SWarner Losh * Note that the function cannot output more than *dstCapacityPtr, 1040c16b537SWarner Losh * therefore, some content might still be left into internal buffer if *dstCapacityPtr is too small. 1050c16b537SWarner Losh * @return : nb of bytes still present into internal buffer (0 if it's empty) 1060c16b537SWarner Losh * or an error code, which can be tested using ZBUFF_isError(). 1070c16b537SWarner Losh * 1080c16b537SWarner Losh * ZBUFF_compressEnd() instructs to finish a frame. 1090c16b537SWarner Losh * It will perform a flush and write frame epilogue. 1100c16b537SWarner Losh * The epilogue is required for decoders to consider a frame completed. 1110c16b537SWarner Losh * Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small. 1120c16b537SWarner Losh * In which case, call again ZBUFF_compressFlush() to complete the flush. 1130c16b537SWarner Losh * @return : nb of bytes still present into internal buffer (0 if it's empty) 1140c16b537SWarner Losh * or an error code, which can be tested using ZBUFF_isError(). 1150c16b537SWarner Losh * 1160c16b537SWarner Losh * Hint : _recommended buffer_ sizes (not compulsory) : ZBUFF_recommendedCInSize() / ZBUFF_recommendedCOutSize() 1170c16b537SWarner Losh * input : ZBUFF_recommendedCInSize==128 KB block size is the internal unit, use this value to reduce intermediate stages (better latency) 1180c16b537SWarner Losh * output : ZBUFF_recommendedCOutSize==ZSTD_compressBound(128 KB) + 3 + 3 : ensures it's always possible to write/flush/end a full block. Skip some buffering. 1190c16b537SWarner Losh * By using both, it ensures that input will be entirely consumed, and output will always contain the result, reducing intermediate buffering. 1200c16b537SWarner Losh * **************************************************/ 1210c16b537SWarner Losh 1220c16b537SWarner Losh 1230c16b537SWarner Losh typedef ZSTD_DStream ZBUFF_DCtx; 1240c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_createDStream") ZBUFF_DCtx* ZBUFF_createDCtx(void); 1250c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_freeDStream") size_t ZBUFF_freeDCtx(ZBUFF_DCtx* dctx); 1260c16b537SWarner Losh 1270c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_initDStream") size_t ZBUFF_decompressInit(ZBUFF_DCtx* dctx); 1280c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_initDStream_usingDict") size_t ZBUFF_decompressInitDictionary(ZBUFF_DCtx* dctx, const void* dict, size_t dictSize); 1290c16b537SWarner Losh 1300c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_decompressStream") size_t ZBUFF_decompressContinue(ZBUFF_DCtx* dctx, 1310c16b537SWarner Losh void* dst, size_t* dstCapacityPtr, 1320c16b537SWarner Losh const void* src, size_t* srcSizePtr); 1330c16b537SWarner Losh 1340c16b537SWarner Losh /*-*************************************************************************** 1350c16b537SWarner Losh * Streaming decompression howto 1360c16b537SWarner Losh * 1370c16b537SWarner Losh * A ZBUFF_DCtx object is required to track streaming operations. 1380c16b537SWarner Losh * Use ZBUFF_createDCtx() and ZBUFF_freeDCtx() to create/release resources. 1390c16b537SWarner Losh * Use ZBUFF_decompressInit() to start a new decompression operation, 1400c16b537SWarner Losh * or ZBUFF_decompressInitDictionary() if decompression requires a dictionary. 1410c16b537SWarner Losh * Note that ZBUFF_DCtx objects can be re-init multiple times. 1420c16b537SWarner Losh * 1430c16b537SWarner Losh * Use ZBUFF_decompressContinue() repetitively to consume your input. 1440c16b537SWarner Losh * *srcSizePtr and *dstCapacityPtr can be any size. 1450c16b537SWarner Losh * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr. 1460c16b537SWarner Losh * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again. 1470c16b537SWarner Losh * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`. 1480c16b537SWarner Losh * @return : 0 when a frame is completely decoded and fully flushed, 1490c16b537SWarner Losh * 1 when there is still some data left within internal buffer to flush, 1500c16b537SWarner Losh * >1 when more data is expected, with value being a suggested next input size (it's just a hint, which helps latency), 1510c16b537SWarner Losh * or an error code, which can be tested using ZBUFF_isError(). 1520c16b537SWarner Losh * 1530c16b537SWarner Losh * Hint : recommended buffer sizes (not compulsory) : ZBUFF_recommendedDInSize() and ZBUFF_recommendedDOutSize() 1540c16b537SWarner Losh * output : ZBUFF_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded. 1550c16b537SWarner Losh * input : ZBUFF_recommendedDInSize == 128KB + 3; 1560c16b537SWarner Losh * just follow indications from ZBUFF_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 . 1570c16b537SWarner Losh * *******************************************************************************/ 1580c16b537SWarner Losh 1590c16b537SWarner Losh 1600c16b537SWarner Losh /* ************************************* 1610c16b537SWarner Losh * Tool functions 1620c16b537SWarner Losh ***************************************/ 1630c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_isError") unsigned ZBUFF_isError(size_t errorCode); 1640c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_getErrorName") const char* ZBUFF_getErrorName(size_t errorCode); 1650c16b537SWarner Losh 1660c16b537SWarner Losh /** Functions below provide recommended buffer sizes for Compression or Decompression operations. 1670c16b537SWarner Losh * These sizes are just hints, they tend to offer better latency */ 1680c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_CStreamInSize") size_t ZBUFF_recommendedCInSize(void); 1690c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_CStreamOutSize") size_t ZBUFF_recommendedCOutSize(void); 1700c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_DStreamInSize") size_t ZBUFF_recommendedDInSize(void); 1710c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_DStreamOutSize") size_t ZBUFF_recommendedDOutSize(void); 1720c16b537SWarner Losh 1730c16b537SWarner Losh #endif /* ZSTD_BUFFERED_H_23987 */ 1740c16b537SWarner Losh 1750c16b537SWarner Losh 1760c16b537SWarner Losh #ifdef ZBUFF_STATIC_LINKING_ONLY 1770c16b537SWarner Losh #ifndef ZBUFF_STATIC_H_30298098432 1780c16b537SWarner Losh #define ZBUFF_STATIC_H_30298098432 1790c16b537SWarner Losh 1800c16b537SWarner Losh /* ==================================================================================== 1810c16b537SWarner Losh * The definitions in this section are considered experimental. 1820c16b537SWarner Losh * They should never be used in association with a dynamic library, as they may change in the future. 1830c16b537SWarner Losh * They are provided for advanced usages. 1840c16b537SWarner Losh * Use them only in association with static linking. 1850c16b537SWarner Losh * ==================================================================================== */ 1860c16b537SWarner Losh 1870c16b537SWarner Losh /*--- Dependency ---*/ 1880c16b537SWarner Losh #define ZSTD_STATIC_LINKING_ONLY /* ZSTD_parameters, ZSTD_customMem */ 18937f1f268SConrad Meyer #include "../zstd.h" 1900c16b537SWarner Losh 1910c16b537SWarner Losh 1920c16b537SWarner Losh /*--- Custom memory allocator ---*/ 1930c16b537SWarner Losh /*! ZBUFF_createCCtx_advanced() : 1940c16b537SWarner Losh * Create a ZBUFF compression context using external alloc and free functions */ 1950c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_createCStream_advanced") ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem); 1960c16b537SWarner Losh 1970c16b537SWarner Losh /*! ZBUFF_createDCtx_advanced() : 1980c16b537SWarner Losh * Create a ZBUFF decompression context using external alloc and free functions */ 1990c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_createDStream_advanced") ZBUFF_DCtx* ZBUFF_createDCtx_advanced(ZSTD_customMem customMem); 2000c16b537SWarner Losh 2010c16b537SWarner Losh 2020c16b537SWarner Losh /*--- Advanced Streaming Initialization ---*/ 2030c16b537SWarner Losh ZBUFF_DEPRECATED("use ZSTD_initDStream_usingDict") size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc, 2040c16b537SWarner Losh const void* dict, size_t dictSize, 2050c16b537SWarner Losh ZSTD_parameters params, unsigned long long pledgedSrcSize); 2060c16b537SWarner Losh 2070c16b537SWarner Losh 2080c16b537SWarner Losh #endif /* ZBUFF_STATIC_H_30298098432 */ 2090c16b537SWarner Losh #endif /* ZBUFF_STATIC_LINKING_ONLY */ 2100c16b537SWarner Losh 2110c16b537SWarner Losh 2120c16b537SWarner Losh #if defined (__cplusplus) 2130c16b537SWarner Losh } 2140c16b537SWarner Losh #endif 215