inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 1 | /* |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This source code is licensed under the BSD-style license found in the |
| 6 | * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 9 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 10 | #if defined (__cplusplus) |
| 11 | extern "C" { |
| 12 | #endif |
| 13 | |
Nick Terrell | 05c00f2 | 2016-11-29 11:46:37 -0800 | [diff] [blame] | 14 | #ifndef ZSTD_H_235446 |
| 15 | #define ZSTD_H_235446 |
| 16 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 17 | /* ====== Dependency ======*/ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 18 | #include <stddef.h> /* size_t */ |
| 19 | |
| 20 | |
Yann Collet | 426a9d4 | 2016-12-07 16:39:34 -0800 | [diff] [blame] | 21 | /* ===== ZSTDLIB_API : control library symbols visibility ===== */ |
Nick Terrell | a128040 | 2017-05-19 18:01:59 -0700 | [diff] [blame] | 22 | #ifndef ZSTDLIB_VISIBILITY |
| 23 | # if defined(__GNUC__) && (__GNUC__ >= 4) |
| 24 | # define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default"))) |
| 25 | # else |
| 26 | # define ZSTDLIB_VISIBILITY |
| 27 | # endif |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 28 | #endif |
| 29 | #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) |
| 30 | # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY |
| 31 | #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) |
| 32 | # define ZSTDLIB_API __declspec(dllimport) ZSTDLIB_VISIBILITY /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/ |
| 33 | #else |
| 34 | # define ZSTDLIB_API ZSTDLIB_VISIBILITY |
Christophe Chevalier | c6e8453 | 2015-12-07 17:44:09 +0100 | [diff] [blame] | 35 | #endif |
| 36 | |
| 37 | |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 38 | /******************************************************************************************************* |
| 39 | Introduction |
| 40 | |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 41 | zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 42 | at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and |
| 43 | decompression functions. The library supports compression levels from 1 up to ZSTD_maxCLevel() which is 22. |
Anders Oleson | 517577b | 2017-02-20 12:08:59 -0800 | [diff] [blame] | 44 | Levels >= 20, labeled `--ultra`, should be used with caution, as they require more memory. |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 45 | Compression can be done in: |
| 46 | - a single step (described as Simple API) |
| 47 | - a single step, reusing a context (described as Explicit memory management) |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 48 | - unbounded multiple steps (described as Streaming compression) |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 49 | The compression ratio achievable on small data can be highly improved using compression with a dictionary in: |
| 50 | - a single step (described as Simple dictionary API) |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 51 | - a single step, reusing a dictionary (described as Fast dictionary API) |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 52 | |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 53 | Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h. |
| 54 | These APIs shall never be used with a dynamic library. |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 55 | They are not "stable", their definition may change in the future. Only static linking is allowed. |
| 56 | *********************************************************************************************************/ |
| 57 | |
| 58 | /*------ Version ------*/ |
Yann Collet | 901e85f | 2016-08-31 07:51:25 -0700 | [diff] [blame] | 59 | #define ZSTD_VERSION_MAJOR 1 |
Yann Collet | 5165252 | 2017-05-08 17:52:46 -0700 | [diff] [blame] | 60 | #define ZSTD_VERSION_MINOR 3 |
Yann Collet | 8b66953 | 2017-04-27 12:50:20 -0700 | [diff] [blame] | 61 | #define ZSTD_VERSION_RELEASE 0 |
Yann Collet | e02808f | 2016-04-20 22:46:16 +0200 | [diff] [blame] | 62 | |
Yann Collet | bfff899 | 2017-05-16 16:12:23 -0700 | [diff] [blame] | 63 | #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) |
| 64 | ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< to be used when checking dll version */ |
| 65 | |
Yann Collet | e02808f | 2016-04-20 22:46:16 +0200 | [diff] [blame] | 66 | #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE |
| 67 | #define ZSTD_QUOTE(str) #str |
| 68 | #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) |
| 69 | #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) |
Yann Collet | bfff899 | 2017-05-16 16:12:23 -0700 | [diff] [blame] | 70 | ZSTDLIB_API const char* ZSTD_versionString(void); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 71 | |
| 72 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 73 | /*************************************** |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 74 | * Simple API |
Yann Collet | 7010c27 | 2015-10-21 09:07:25 +0100 | [diff] [blame] | 75 | ***************************************/ |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 76 | /*! ZSTD_compress() : |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 77 | * Compresses `src` content as a single zstd compressed frame into already allocated `dst`. |
| 78 | * Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. |
| 79 | * @return : compressed size written into `dst` (<= `dstCapacity), |
| 80 | * or an error code if it fails (which can be tested using ZSTD_isError()). */ |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 81 | ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, |
| 82 | const void* src, size_t srcSize, |
| 83 | int compressionLevel); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 84 | |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 85 | /*! ZSTD_decompress() : |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 86 | * `compressedSize` : must be the _exact_ size of some number of compressed and/or skippable frames. |
| 87 | * `dstCapacity` is an upper bound of originalSize. |
| 88 | * If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. |
| 89 | * @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), |
| 90 | * or an errorCode if it fails (which can be tested using ZSTD_isError()). */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 91 | ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, |
Christophe Chevalier | c6e8453 | 2015-12-07 17:44:09 +0100 | [diff] [blame] | 92 | const void* src, size_t compressedSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 93 | |
Yann Collet | ac175d4 | 2016-09-13 00:51:47 +0200 | [diff] [blame] | 94 | /*! ZSTD_getDecompressedSize() : |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 95 | * NOTE: This function is planned to be obsolete, in favour of ZSTD_getFrameContentSize. |
| 96 | * ZSTD_getFrameContentSize functions the same way, returning the decompressed size of a single |
| 97 | * frame, but distinguishes empty frames from frames with an unknown size, or errors. |
| 98 | * |
| 99 | * Additionally, ZSTD_findDecompressedSize can be used instead. It can handle multiple |
| 100 | * concatenated frames in one buffer, and so is more general. |
| 101 | * As a result however, it requires more computation and entire frames to be passed to it, |
| 102 | * as opposed to ZSTD_getFrameContentSize which requires only a single frame's header. |
| 103 | * |
| 104 | * 'src' is the start of a zstd compressed frame. |
| 105 | * @return : content size to be decompressed, as a 64-bits value _if known_, 0 otherwise. |
| 106 | * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. |
| 107 | * When `return==0`, data to decompress could be any size. |
| 108 | * In which case, it's necessary to use streaming mode to decompress data. |
| 109 | * Optionally, application can still use ZSTD_decompress() while relying on implied limits. |
| 110 | * (For example, data may be necessarily cut into blocks <= 16 KB). |
| 111 | * note 2 : decompressed size is always present when compression is done with ZSTD_compress() |
| 112 | * note 3 : decompressed size can be very large (64-bits value), |
| 113 | * potentially larger than what local system can handle as a single memory segment. |
| 114 | * In which case, it's necessary to use streaming mode to decompress data. |
| 115 | * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. |
| 116 | * Always ensure result fits within application's authorized limits. |
| 117 | * Each application can set its own limits. |
| 118 | * note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameParams() to know more. */ |
Yann Collet | ac175d4 | 2016-09-13 00:51:47 +0200 | [diff] [blame] | 119 | ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); |
| 120 | |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 121 | |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 122 | /*====== Helper functions ======*/ |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 123 | ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ |
| 124 | ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case scenario */ |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 125 | ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */ |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 126 | ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */ |
Yann Collet | 7010c27 | 2015-10-21 09:07:25 +0100 | [diff] [blame] | 127 | |
| 128 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 129 | /*************************************** |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 130 | * Explicit memory management |
Yann Collet | 7010c27 | 2015-10-21 09:07:25 +0100 | [diff] [blame] | 131 | ***************************************/ |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 132 | /*= Compression context |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 133 | * When compressing many times, |
| 134 | * it is recommended to allocate a context just once, and re-use it for each successive compression operation. |
| 135 | * This will make workload friendlier for system's memory. |
| 136 | * Use one context per thread for parallel execution in multi-threaded environments. */ |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 137 | typedef struct ZSTD_CCtx_s ZSTD_CCtx; |
Christophe Chevalier | c6e8453 | 2015-12-07 17:44:09 +0100 | [diff] [blame] | 138 | ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 139 | ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); |
Yann Collet | 7010c27 | 2015-10-21 09:07:25 +0100 | [diff] [blame] | 140 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 141 | /*! ZSTD_compressCCtx() : |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 142 | * Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 143 | ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 144 | |
Sean Purcell | dec2b96 | 2017-03-14 11:24:09 -0700 | [diff] [blame] | 145 | /*= Decompression context |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 146 | * When decompressing many times, |
| 147 | * it is recommended to allocate a context just once, and re-use it for each successive compression operation. |
| 148 | * This will make workload friendlier for system's memory. |
| 149 | * Use one context per thread for parallel execution in multi-threaded environments. */ |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 150 | typedef struct ZSTD_DCtx_s ZSTD_DCtx; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 151 | ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 152 | ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 153 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 154 | /*! ZSTD_decompressDCtx() : |
Yann Collet | 71ddeb6 | 2017-04-20 22:54:54 -0700 | [diff] [blame] | 155 | * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()). */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 156 | ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 157 | |
| 158 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 159 | /************************** |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 160 | * Simple dictionary API |
| 161 | ***************************/ |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 162 | /*! ZSTD_compress_usingDict() : |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 163 | * Compression using a predefined Dictionary (see dictBuilder/zdict.h). |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 164 | * Note : This function loads the dictionary, resulting in significant startup delay. |
| 165 | * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 166 | ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, |
| 167 | void* dst, size_t dstCapacity, |
| 168 | const void* src, size_t srcSize, |
| 169 | const void* dict,size_t dictSize, |
| 170 | int compressionLevel); |
| 171 | |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 172 | /*! ZSTD_decompress_usingDict() : |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 173 | * Decompression using a predefined Dictionary (see dictBuilder/zdict.h). |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 174 | * Dictionary must be identical to the one used during compression. |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 175 | * Note : This function loads the dictionary, resulting in significant startup delay. |
| 176 | * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 177 | ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
| 178 | void* dst, size_t dstCapacity, |
| 179 | const void* src, size_t srcSize, |
| 180 | const void* dict,size_t dictSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 181 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 182 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 183 | /**************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 184 | * Fast dictionary API |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 185 | ****************************/ |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 186 | typedef struct ZSTD_CDict_s ZSTD_CDict; |
| 187 | |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 188 | /*! ZSTD_createCDict() : |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 189 | * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. |
| 190 | * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. |
| 191 | * ZSTD_CDict can be created once and used by multiple threads concurrently, as its usage is read-only. |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 192 | * `dictBuffer` can be released after ZSTD_CDict creation, as its content is copied within CDict */ |
| 193 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dictBuffer, size_t dictSize, int compressionLevel); |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 194 | |
| 195 | /*! ZSTD_freeCDict() : |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 196 | * Function frees memory allocated by ZSTD_createCDict(). */ |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 197 | ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); |
| 198 | |
| 199 | /*! ZSTD_compress_usingCDict() : |
Yann Collet | 4f81818 | 2017-04-17 17:57:35 -0700 | [diff] [blame] | 200 | * Compression using a digested Dictionary. |
| 201 | * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. |
| 202 | * Note that compression level is decided during dictionary creation. |
| 203 | * Frame parameters are hardcoded (dictID=yes, contentSize=yes, checksum=no) */ |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 204 | ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, |
| 205 | void* dst, size_t dstCapacity, |
| 206 | const void* src, size_t srcSize, |
| 207 | const ZSTD_CDict* cdict); |
| 208 | |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 209 | |
| 210 | typedef struct ZSTD_DDict_s ZSTD_DDict; |
| 211 | |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 212 | /*! ZSTD_createDDict() : |
| 213 | * Create a digested dictionary, ready to start decompression operation without startup delay. |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 214 | * dictBuffer can be released after DDict creation, as its content is copied inside DDict */ |
| 215 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 216 | |
| 217 | /*! ZSTD_freeDDict() : |
| 218 | * Function frees memory allocated with ZSTD_createDDict() */ |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 219 | ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); |
| 220 | |
| 221 | /*! ZSTD_decompress_usingDDict() : |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 222 | * Decompression using a digested Dictionary. |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 223 | * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */ |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 224 | ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, |
| 225 | void* dst, size_t dstCapacity, |
| 226 | const void* src, size_t srcSize, |
| 227 | const ZSTD_DDict* ddict); |
| 228 | |
| 229 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 230 | /**************************** |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 231 | * Streaming |
| 232 | ****************************/ |
| 233 | |
| 234 | typedef struct ZSTD_inBuffer_s { |
| 235 | const void* src; /**< start of input buffer */ |
| 236 | size_t size; /**< size of input buffer */ |
| 237 | size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ |
| 238 | } ZSTD_inBuffer; |
| 239 | |
| 240 | typedef struct ZSTD_outBuffer_s { |
| 241 | void* dst; /**< start of output buffer */ |
| 242 | size_t size; /**< size of output buffer */ |
| 243 | size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ |
| 244 | } ZSTD_outBuffer; |
| 245 | |
| 246 | |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 247 | |
inikep | ba1db37 | 2016-10-06 14:22:48 +0200 | [diff] [blame] | 248 | /*-*********************************************************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 249 | * Streaming compression - HowTo |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 250 | * |
| 251 | * A ZSTD_CStream object is required to track streaming operation. |
| 252 | * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. |
| 253 | * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 254 | * It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively, |
| 255 | * since it will play nicer with system's memory, by re-using already allocated memory. |
| 256 | * Use one separate ZSTD_CStream per thread for parallel execution. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 257 | * |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 258 | * Start a new compression by initializing ZSTD_CStream. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 259 | * Use ZSTD_initCStream() to start a new compression operation. |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 260 | * Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section) |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 261 | * |
| 262 | * Use ZSTD_compressStream() repetitively to consume input stream. |
Yann Collet | fa72f6b | 2016-09-05 17:39:56 +0200 | [diff] [blame] | 263 | * The function will automatically update both `pos` fields. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 264 | * Note that it may not consume the entire input, in which case `pos < size`, |
| 265 | * and it's up to the caller to present again remaining data. |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 266 | * @return : a size hint, preferred nb of bytes to use as input for next function call |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 267 | * or an error code, which can be tested using ZSTD_isError(). |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 268 | * Note 1 : it's just a hint, to help latency a little, any other value will work fine. |
| 269 | * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize() |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 270 | * |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 271 | * At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream(). |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 272 | * `output->pos` will be updated. |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 273 | * Note that some content might still be left within internal buffer if `output->size` is too small. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 274 | * @return : nb of bytes still present within internal buffer (0 if it's empty) |
| 275 | * or an error code, which can be tested using ZSTD_isError(). |
| 276 | * |
| 277 | * ZSTD_endStream() instructs to finish a frame. |
| 278 | * It will perform a flush and write frame epilogue. |
| 279 | * The epilogue is required for decoders to consider a frame completed. |
| 280 | * Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small. |
| 281 | * In which case, call again ZSTD_endStream() to complete the flush. |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 282 | * @return : nb of bytes still present within internal buffer (0 if it's empty, hence compression completed) |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 283 | * or an error code, which can be tested using ZSTD_isError(). |
| 284 | * |
| 285 | * *******************************************************************/ |
| 286 | |
Yann Collet | 0be6fd3 | 2017-05-08 16:08:01 -0700 | [diff] [blame] | 287 | typedef ZSTD_CCtx ZSTD_CStream; /**< CCtx and CStream are effectively same object */ |
| 288 | /* Continue due distinghish them for compatibility with versions <= v1.2.0 */ |
Sean Purcell | dec2b96 | 2017-03-14 11:24:09 -0700 | [diff] [blame] | 289 | /*===== ZSTD_CStream management functions =====*/ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 290 | ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); |
| 291 | ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 292 | |
Sean Purcell | dec2b96 | 2017-03-14 11:24:09 -0700 | [diff] [blame] | 293 | /*===== Streaming compression functions =====*/ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 294 | ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); |
| 295 | ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
| 296 | ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
| 297 | ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 298 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 299 | ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ |
| 300 | 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. */ |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 301 | |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 302 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 303 | |
inikep | ba1db37 | 2016-10-06 14:22:48 +0200 | [diff] [blame] | 304 | /*-*************************************************************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 305 | * Streaming decompression - HowTo |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 306 | * |
| 307 | * A ZSTD_DStream object is required to track streaming operations. |
| 308 | * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 309 | * ZSTD_DStream objects can be re-used multiple times. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 310 | * |
| 311 | * Use ZSTD_initDStream() to start a new decompression operation, |
| 312 | * or ZSTD_initDStream_usingDict() if decompression requires a dictionary. |
Yann Collet | 7c83dfd | 2016-09-05 19:47:43 +0200 | [diff] [blame] | 313 | * @return : recommended first input size |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 314 | * |
| 315 | * Use ZSTD_decompressStream() repetitively to consume your input. |
Yann Collet | fa72f6b | 2016-09-05 17:39:56 +0200 | [diff] [blame] | 316 | * The function will update both `pos` fields. |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 317 | * If `input.pos < input.size`, some input has not been consumed. |
Yann Collet | 1d4208c | 2016-09-06 05:16:40 +0200 | [diff] [blame] | 318 | * It's up to the caller to present again remaining data. |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 319 | * If `output.pos < output.size`, decoder has flushed everything it could. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 320 | * @return : 0 when a frame is completely decoded and fully flushed, |
Yann Collet | fa72f6b | 2016-09-05 17:39:56 +0200 | [diff] [blame] | 321 | * an error code, which can be tested using ZSTD_isError(), |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 322 | * any other value > 0, which means there is still some decoding to do to complete current frame. |
| 323 | * The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 324 | * *******************************************************************************/ |
| 325 | |
| 326 | typedef struct ZSTD_DStream_s ZSTD_DStream; |
Sean Purcell | dec2b96 | 2017-03-14 11:24:09 -0700 | [diff] [blame] | 327 | /*===== ZSTD_DStream management functions =====*/ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 328 | ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); |
| 329 | ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 330 | |
Sean Purcell | dec2b96 | 2017-03-14 11:24:09 -0700 | [diff] [blame] | 331 | /*===== Streaming decompression functions =====*/ |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 332 | ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); |
| 333 | ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 334 | |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 335 | ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ |
Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 336 | ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */ |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 337 | |
Nick Terrell | 05c00f2 | 2016-11-29 11:46:37 -0800 | [diff] [blame] | 338 | #endif /* ZSTD_H_235446 */ |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 339 | |
| 340 | |
Nick Terrell | 05c00f2 | 2016-11-29 11:46:37 -0800 | [diff] [blame] | 341 | #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) |
| 342 | #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 343 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 344 | /**************************************************************************************** |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 345 | * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 346 | * The definitions in this section are considered experimental. |
Yann Collet | a49e066 | 2016-06-21 11:54:03 +0200 | [diff] [blame] | 347 | * They should never be used with a dynamic library, as they may change in the future. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 348 | * They are provided for advanced usages. |
| 349 | * Use them only in association with static linking. |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 350 | * ***************************************************************************************/ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 351 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 352 | /* --- Constants ---*/ |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 353 | #define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 354 | #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U |
| 355 | |
Sean Purcell | d44703d | 2017-03-01 14:36:25 -0800 | [diff] [blame] | 356 | #define ZSTD_WINDOWLOG_MAX_32 27 |
Yann Collet | ed3845d | 2016-07-08 12:57:10 +0200 | [diff] [blame] | 357 | #define ZSTD_WINDOWLOG_MAX_64 27 |
Nick Terrell | e65aab8 | 2017-03-08 15:40:13 -0800 | [diff] [blame] | 358 | #define ZSTD_WINDOWLOG_MAX ((unsigned)(sizeof(size_t) == 4 ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 359 | #define ZSTD_WINDOWLOG_MIN 10 |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 360 | #define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 361 | #define ZSTD_HASHLOG_MIN 6 |
| 362 | #define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1) |
| 363 | #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 364 | #define ZSTD_HASHLOG3_MAX 17 |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 365 | #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) |
| 366 | #define ZSTD_SEARCHLOG_MIN 1 |
Yann Collet | 0e07bf3 | 2016-09-07 06:33:02 +0200 | [diff] [blame] | 367 | #define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ |
| 368 | #define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 369 | #define ZSTD_TARGETLENGTH_MIN 4 |
| 370 | #define ZSTD_TARGETLENGTH_MAX 999 |
| 371 | |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 372 | #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */ |
Yann Collet | ba75e9d | 2016-12-21 19:57:18 +0100 | [diff] [blame] | 373 | #define ZSTD_FRAMEHEADERSIZE_MIN 6 |
Yann Collet | 7c83dfd | 2016-09-05 19:47:43 +0200 | [diff] [blame] | 374 | static const size_t ZSTD_frameHeaderSize_prefix = 5; |
Yann Collet | ba75e9d | 2016-12-21 19:57:18 +0100 | [diff] [blame] | 375 | static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 376 | static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX; |
| 377 | static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */ |
| 378 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 379 | |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 380 | /*--- Advanced types ---*/ |
Nick Terrell | eeb31ee | 2017-03-09 11:44:25 -0800 | [diff] [blame] | 381 | typedef enum { ZSTD_fast, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt, ZSTD_btultra } ZSTD_strategy; /* from faster to stronger */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 382 | |
| 383 | typedef struct { |
Yann Collet | 655393c | 2016-08-14 00:16:20 +0200 | [diff] [blame] | 384 | unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ |
| 385 | unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ |
| 386 | unsigned hashLog; /**< dispatch table : larger == faster, more memory */ |
| 387 | unsigned searchLog; /**< nb of searches : larger == more compression, slower */ |
| 388 | unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */ |
| 389 | unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 390 | ZSTD_strategy strategy; |
| 391 | } ZSTD_compressionParameters; |
| 392 | |
| 393 | typedef struct { |
Yann Collet | 0ec6a95 | 2017-01-02 00:49:42 +0100 | [diff] [blame] | 394 | unsigned contentSizeFlag; /**< 1: content size will be in frame header (when known) */ |
| 395 | unsigned checksumFlag; /**< 1: generate a 32-bits checksum at end of frame, for error detection */ |
| 396 | unsigned noDictIDFlag; /**< 1: no dictID will be saved into frame header (if dictionary compression) */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 397 | } ZSTD_frameParameters; |
| 398 | |
| 399 | typedef struct { |
| 400 | ZSTD_compressionParameters cParams; |
| 401 | ZSTD_frameParameters fParams; |
| 402 | } ZSTD_parameters; |
| 403 | |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 404 | typedef struct { |
| 405 | unsigned long long frameContentSize; |
| 406 | unsigned windowSize; |
| 407 | unsigned dictID; |
| 408 | unsigned checksumFlag; |
| 409 | } ZSTD_frameHeader; |
| 410 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 411 | /*= Custom memory allocation functions */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 412 | typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); |
| 413 | typedef void (*ZSTD_freeFunction) (void* opaque, void* address); |
| 414 | typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; |
| 415 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 416 | /*************************************** |
Yann Collet | 5a36c06 | 2017-05-09 15:11:30 -0700 | [diff] [blame] | 417 | * Frame size functions |
Sean Purcell | d7bfcac | 2017-02-10 11:38:57 -0800 | [diff] [blame] | 418 | ***************************************/ |
| 419 | |
Sean Purcell | 9050e19 | 2017-02-22 12:12:32 -0800 | [diff] [blame] | 420 | /*! ZSTD_findFrameCompressedSize() : |
Sean Purcell | 9757cc8 | 2017-02-22 12:27:15 -0800 | [diff] [blame] | 421 | * `src` should point to the start of a ZSTD encoded frame or skippable frame |
Sean Purcell | d7bfcac | 2017-02-10 11:38:57 -0800 | [diff] [blame] | 422 | * `srcSize` must be at least as large as the frame |
Yann Collet | 5a36c06 | 2017-05-09 15:11:30 -0700 | [diff] [blame] | 423 | * @return : the compressed size of the frame pointed to by `src`, |
| 424 | * suitable to pass to `ZSTD_decompress` or similar, |
| 425 | * or an error code if given invalid input. */ |
Sean Purcell | 9050e19 | 2017-02-22 12:12:32 -0800 | [diff] [blame] | 426 | ZSTDLIB_API size_t ZSTD_findFrameCompressedSize(const void* src, size_t srcSize); |
Sean Purcell | d7bfcac | 2017-02-10 11:38:57 -0800 | [diff] [blame] | 427 | |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 428 | /*! ZSTD_getFrameContentSize() : |
Yann Collet | 5a36c06 | 2017-05-09 15:11:30 -0700 | [diff] [blame] | 429 | * `src` should point to the start of a ZSTD encoded frame. |
| 430 | * `srcSize` must be at least as large as the frame header. |
| 431 | * A value >= `ZSTD_frameHeaderSize_max` is guaranteed to be large enough. |
| 432 | * @return : - decompressed size of the frame pointed to be `src` if known |
| 433 | * - ZSTD_CONTENTSIZE_UNKNOWN if the size cannot be determined |
| 434 | * - ZSTD_CONTENTSIZE_ERROR if an error occurred (e.g. invalid magic number, srcSize too small) */ |
| 435 | #define ZSTD_CONTENTSIZE_UNKNOWN (0ULL - 1) |
| 436 | #define ZSTD_CONTENTSIZE_ERROR (0ULL - 2) |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 437 | ZSTDLIB_API unsigned long long ZSTD_getFrameContentSize(const void *src, size_t srcSize); |
| 438 | |
| 439 | /*! ZSTD_findDecompressedSize() : |
Yann Collet | 5a36c06 | 2017-05-09 15:11:30 -0700 | [diff] [blame] | 440 | * `src` should point the start of a series of ZSTD encoded and/or skippable frames |
| 441 | * `srcSize` must be the _exact_ size of this series |
| 442 | * (i.e. there should be a frame boundary exactly `srcSize` bytes after `src`) |
| 443 | * @return : - decompressed size of all data in all successive frames |
| 444 | * - if the decompressed size cannot be determined: ZSTD_CONTENTSIZE_UNKNOWN |
| 445 | * - if an error occurred: ZSTD_CONTENTSIZE_ERROR |
| 446 | * |
| 447 | * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. |
| 448 | * When `return==ZSTD_CONTENTSIZE_UNKNOWN`, data to decompress could be any size. |
| 449 | * In which case, it's necessary to use streaming mode to decompress data. |
| 450 | * Optionally, application can still use ZSTD_decompress() while relying on implied limits. |
| 451 | * (For example, data may be necessarily cut into blocks <= 16 KB). |
| 452 | * note 2 : decompressed size is always present when compression is done with ZSTD_compress() |
| 453 | * note 3 : decompressed size can be very large (64-bits value), |
| 454 | * potentially larger than what local system can handle as a single memory segment. |
| 455 | * In which case, it's necessary to use streaming mode to decompress data. |
| 456 | * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. |
| 457 | * Always ensure result fits within application's authorized limits. |
| 458 | * Each application can set its own limits. |
| 459 | * note 5 : ZSTD_findDecompressedSize handles multiple frames, and so it must traverse the input to |
| 460 | * read each contained frame header. This is efficient as most of the data is skipped, |
| 461 | * however it does mean that all frame data must be present and valid. */ |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 462 | ZSTDLIB_API unsigned long long ZSTD_findDecompressedSize(const void* src, size_t srcSize); |
| 463 | |
Paul Cruz | a9b77c8 | 2017-06-15 14:13:28 -0700 | [diff] [blame] | 464 | /*! ZSTD_frameHeaderSize() : |
| 465 | * `src` should point to the start of a ZSTD frame |
| 466 | * `srcSize` must be >= ZSTD_frameHeaderSize_prefix. |
| 467 | * @return : size of the Frame Header */ |
| 468 | size_t ZSTD_frameHeaderSize(const void* src, size_t srcSize); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 469 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 470 | /*************************************** |
Yann Collet | 5a36c06 | 2017-05-09 15:11:30 -0700 | [diff] [blame] | 471 | * Context memory usage |
| 472 | ***************************************/ |
| 473 | |
| 474 | /*! ZSTD_sizeof_*() : |
| 475 | * These functions give the current memory usage of selected object. |
| 476 | * Object memory usage can evolve if it's re-used multiple times. */ |
| 477 | ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); |
| 478 | ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); |
| 479 | ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); |
| 480 | ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); |
| 481 | ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); |
| 482 | ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); |
| 483 | |
| 484 | /*! ZSTD_estimate*() : |
| 485 | * These functions make it possible to estimate memory usage |
| 486 | * of a future target object, before its allocation, |
| 487 | * given a set of parameters, which vary depending on target object. |
| 488 | * The objective is to guide decision before allocation. */ |
| 489 | ZSTDLIB_API size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams); |
| 490 | ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); |
| 491 | |
| 492 | /*! ZSTD_estimate?StreamSize() : |
| 493 | * Note : if streaming is init with function ZSTD_init?Stream_usingDict(), |
| 494 | * an internal ?Dict will be created, which size is not estimated. |
| 495 | * In this case, get additional size by using ZSTD_estimate?DictSize */ |
| 496 | ZSTDLIB_API size_t ZSTD_estimateCStreamSize(ZSTD_compressionParameters cParams); |
Yann Collet | f16f449 | 2017-05-09 16:18:17 -0700 | [diff] [blame] | 497 | ZSTDLIB_API size_t ZSTD_estimateDStreamSize(ZSTD_frameHeader fHeader); |
Yann Collet | 5a36c06 | 2017-05-09 15:11:30 -0700 | [diff] [blame] | 498 | |
| 499 | /*! ZSTD_estimate?DictSize() : |
| 500 | * Note : if dictionary is created "byReference", reduce estimation by dictSize */ |
| 501 | ZSTDLIB_API size_t ZSTD_estimateCDictSize(ZSTD_compressionParameters cParams, size_t dictSize); |
| 502 | ZSTDLIB_API size_t ZSTD_estimateDDictSize(size_t dictSize); |
| 503 | |
| 504 | |
| 505 | /*************************************** |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 506 | * Advanced compression functions |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 507 | ***************************************/ |
| 508 | /*! ZSTD_createCCtx_advanced() : |
| 509 | * Create a ZSTD compression context using external alloc and free functions */ |
| 510 | ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); |
| 511 | |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 512 | typedef enum { |
Yann Collet | 14312d8 | 2017-02-23 23:42:12 -0800 | [diff] [blame] | 513 | ZSTD_p_forceWindow, /* Force back-references to remain < windowSize, even when referencing Dictionary content (default:0) */ |
| 514 | ZSTD_p_forceRawDict /* Force loading dictionary in "content-only" mode (no header analysis) */ |
Yann Collet | 06e7697 | 2017-01-25 16:39:03 -0800 | [diff] [blame] | 515 | } ZSTD_CCtxParameter; |
Yann Collet | 4a62f79 | 2017-01-26 09:16:56 -0800 | [diff] [blame] | 516 | /*! ZSTD_setCCtxParameter() : |
| 517 | * Set advanced parameters, selected through enum ZSTD_CCtxParameter |
| 518 | * @result : 0, or an error code (which can be tested with ZSTD_isError()) */ |
Yann Collet | ef33d00 | 2017-01-26 12:24:21 -0800 | [diff] [blame] | 519 | ZSTDLIB_API size_t ZSTD_setCCtxParameter(ZSTD_CCtx* cctx, ZSTD_CCtxParameter param, unsigned value); |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 520 | |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 521 | /*! ZSTD_createCDict_byReference() : |
| 522 | * Create a digested dictionary for compression |
| 523 | * Dictionary content is simply referenced, and therefore stays in dictBuffer. |
| 524 | * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */ |
| 525 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); |
| 526 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 527 | /*! ZSTD_createCDict_advanced() : |
| 528 | * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */ |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 529 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, unsigned byReference, |
Yann Collet | 31533ba | 2017-04-27 00:29:04 -0700 | [diff] [blame] | 530 | ZSTD_compressionParameters cParams, ZSTD_customMem customMem); |
Yann Collet | 8e0ee68 | 2016-07-11 13:09:52 +0200 | [diff] [blame] | 531 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 532 | /*! ZSTD_getCParams() : |
Yann Collet | 2b36b23 | 2016-12-13 17:59:55 +0100 | [diff] [blame] | 533 | * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. |
| 534 | * `estimatedSrcSize` value is optional, select 0 if not known */ |
| 535 | ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); |
| 536 | |
| 537 | /*! ZSTD_getParams() : |
| 538 | * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. |
| 539 | * All fields of `ZSTD_frameParameters` are set to default (0) */ |
| 540 | ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 541 | |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 542 | /*! ZSTD_checkCParams() : |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 543 | * Ensure param values remain within authorized range */ |
| 544 | ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); |
| 545 | |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 546 | /*! ZSTD_adjustCParams() : |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 547 | * optimize params for a given `srcSize` and `dictSize`. |
| 548 | * both values are optional, select `0` if unknown. */ |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 549 | ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 550 | |
| 551 | /*! ZSTD_compress_advanced() : |
Yann Collet | f4bd857 | 2017-04-27 11:31:55 -0700 | [diff] [blame] | 552 | * Same as ZSTD_compress_usingDict(), with fine-tune control over each compression parameter */ |
| 553 | ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* cctx, |
| 554 | void* dst, size_t dstCapacity, |
| 555 | const void* src, size_t srcSize, |
| 556 | const void* dict,size_t dictSize, |
| 557 | ZSTD_parameters params); |
| 558 | |
| 559 | /*! ZSTD_compress_usingCDict_advanced() : |
| 560 | * Same as ZSTD_compress_usingCDict(), with fine-tune control over frame parameters */ |
| 561 | ZSTDLIB_API size_t ZSTD_compress_usingCDict_advanced(ZSTD_CCtx* cctx, |
| 562 | void* dst, size_t dstCapacity, |
| 563 | const void* src, size_t srcSize, |
| 564 | const ZSTD_CDict* cdict, ZSTD_frameParameters fParams); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 565 | |
Yann Collet | 45c03c5 | 2016-06-14 13:46:11 +0200 | [diff] [blame] | 566 | |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 567 | /*--- Advanced decompression functions ---*/ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 568 | |
Yann Collet | 179b197 | 2016-11-02 17:30:49 -0700 | [diff] [blame] | 569 | /*! ZSTD_isFrame() : |
| 570 | * Tells if the content of `buffer` starts with a valid Frame Identifier. |
| 571 | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. |
| 572 | * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. |
| 573 | * Note 3 : Skippable Frame Identifiers are considered valid. */ |
| 574 | ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); |
| 575 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 576 | /*! ZSTD_createDCtx_advanced() : |
| 577 | * Create a ZSTD decompression context using external alloc and free functions */ |
| 578 | ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 579 | |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 580 | /*! ZSTD_createDDict_byReference() : |
| 581 | * Create a digested dictionary, ready to start decompression operation without startup delay. |
| 582 | * Dictionary content is simply referenced, and therefore stays in dictBuffer. |
| 583 | * It is important that dictBuffer outlives DDict, it must remain read accessible throughout the lifetime of DDict */ |
| 584 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); |
| 585 | |
Sean Purcell | dec2b96 | 2017-03-14 11:24:09 -0700 | [diff] [blame] | 586 | /*! ZSTD_createDDict_advanced() : |
| 587 | * Create a ZSTD_DDict using external alloc and free, optionally by reference */ |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 588 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, |
| 589 | unsigned byReference, ZSTD_customMem customMem); |
| 590 | |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 591 | /*! ZSTD_getDictID_fromDict() : |
| 592 | * Provides the dictID stored within dictionary. |
| 593 | * if @return == 0, the dictionary is not conformant with Zstandard specification. |
| 594 | * It can still be loaded, but as a content-only dictionary. */ |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 595 | ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 596 | |
| 597 | /*! ZSTD_getDictID_fromDDict() : |
| 598 | * Provides the dictID of the dictionary loaded into `ddict`. |
| 599 | * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. |
| 600 | * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 601 | ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 602 | |
| 603 | /*! ZSTD_getDictID_fromFrame() : |
| 604 | * Provides the dictID required to decompressed the frame stored within `src`. |
| 605 | * If @return == 0, the dictID could not be decoded. |
| 606 | * This could for one of the following reasons : |
| 607 | * - The frame does not require a dictionary to be decoded (most common case). |
| 608 | * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. |
| 609 | * Note : this use case also happens when using a non-conformant dictionary. |
| 610 | * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). |
| 611 | * - This is not a Zstandard frame. |
Yann Collet | a92cbb7 | 2017-04-27 15:08:56 -0700 | [diff] [blame] | 612 | * When identifying the exact failure cause, it's possible to use ZSTD_getFrameParams(), which will provide a more precise error code. */ |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 613 | ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 614 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 615 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 616 | /******************************************************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 617 | * Advanced streaming functions |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 618 | ********************************************************************/ |
| 619 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 620 | /*===== Advanced Streaming compression functions =====*/ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 621 | ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 622 | ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct, a size of 0 means unknown. for a frame size of 0 use initCStream_advanced */ |
Yann Collet | fc51459 | 2017-05-08 17:07:59 -0700 | [diff] [blame] | 623 | ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel); /**< note: a dict will not be used if dict == NULL or dictSize < 8. This result in the creation of an internal CDict */ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 624 | ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, |
Sean Purcell | 0f5c95a | 2017-02-07 16:33:48 -0800 | [diff] [blame] | 625 | ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0 */ |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 626 | 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 */ |
Yann Collet | 77bf59e | 2017-04-27 11:43:04 -0700 | [diff] [blame] | 627 | ZSTDLIB_API size_t ZSTD_initCStream_usingCDict_advanced(ZSTD_CStream* zcs, const ZSTD_CDict* cdict, unsigned long long pledgedSrcSize, ZSTD_frameParameters fParams); /**< same as ZSTD_initCStream_usingCDict(), with control over frame parameters */ |
Yann Collet | 36c2a03 | 2017-04-05 22:06:21 -0700 | [diff] [blame] | 628 | |
| 629 | /*! ZSTD_resetCStream() : |
| 630 | * start a new compression job, using same parameters from previous job. |
| 631 | * This is typically useful to skip dictionary loading stage, since it will re-use it in-place.. |
| 632 | * Note that zcs must be init at least once before using ZSTD_resetCStream(). |
| 633 | * pledgedSrcSize==0 means "srcSize unknown". |
| 634 | * If pledgedSrcSize > 0, its value must be correct, as it will be written in header, and controlled at the end. |
| 635 | * @return : 0, or an error code (which can be tested using ZSTD_isError()) */ |
| 636 | ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 637 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 638 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 639 | /*===== Advanced Streaming decompression functions =====*/ |
Yann Collet | bb00274 | 2017-01-25 16:25:38 -0800 | [diff] [blame] | 640 | typedef enum { DStream_p_maxWindowSize } ZSTD_DStreamParameter_e; |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 641 | ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 642 | ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); |
Yann Collet | 5a36c06 | 2017-05-09 15:11:30 -0700 | [diff] [blame] | 643 | ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize); /**< note: a dict will not be used if dict == NULL or dictSize < 8 */ |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 644 | ZSTDLIB_API size_t ZSTD_initDStream_usingDDict(ZSTD_DStream* zds, const ZSTD_DDict* ddict); /**< note : ddict will just be referenced, and must outlive decompression session */ |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 645 | ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */ |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 646 | |
| 647 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 648 | /********************************************************************* |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 649 | * Buffer-less and synchronous inner streaming functions |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 650 | * |
| 651 | * This is an advanced API, giving full control over buffer management, for users which need direct control over memory. |
Yann Collet | 655393c | 2016-08-14 00:16:20 +0200 | [diff] [blame] | 652 | * But it's also a complex one, with many restrictions (documented below). |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 653 | * Prefer using normal streaming API for an easier experience |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 654 | ********************************************************************* */ |
Yann Collet | 60ba31c | 2016-07-28 19:55:09 +0200 | [diff] [blame] | 655 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 656 | /** |
| 657 | Buffer-less streaming compression (synchronous mode) |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 658 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 659 | A ZSTD_CCtx object is required to track streaming operations. |
Yann Collet | 45c03c5 | 2016-06-14 13:46:11 +0200 | [diff] [blame] | 660 | Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 661 | ZSTD_CCtx object can be re-used multiple times within successive compression operations. |
| 662 | |
| 663 | Start by initializing a context. |
| 664 | Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, |
| 665 | or ZSTD_compressBegin_advanced(), for finer parameter control. |
| 666 | It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() |
| 667 | |
| 668 | Then, consume your input using ZSTD_compressContinue(). |
Yann Collet | a49e066 | 2016-06-21 11:54:03 +0200 | [diff] [blame] | 669 | There are some important considerations to keep in mind when using this advanced function : |
| 670 | - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only. |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 671 | - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks. |
Yann Collet | a49e066 | 2016-06-21 11:54:03 +0200 | [diff] [blame] | 672 | - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. |
| 673 | Worst case evaluation is provided by ZSTD_compressBound(). |
| 674 | ZSTD_compressContinue() doesn't guarantee recover after a failed compression. |
| 675 | - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). |
| 676 | It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) |
| 677 | - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. |
| 678 | In which case, it will "discard" the relevant memory section from its history. |
| 679 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 680 | Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 681 | It's possible to use srcSize==0, in which case, it will write a final empty block to end the frame. |
| 682 | Without last block mark, frames will be considered unfinished (corrupted) by decoders. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 683 | |
Yann Collet | 5eb749e | 2017-01-11 18:21:25 +0100 | [diff] [blame] | 684 | `ZSTD_CCtx` object can be re-used (ZSTD_compressBegin()) to compress some new frame. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 685 | */ |
| 686 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 687 | /*===== Buffer-less streaming compression functions =====*/ |
| 688 | ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); |
| 689 | ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); |
Sean Purcell | 0f5c95a | 2017-02-07 16:33:48 -0800 | [diff] [blame] | 690 | ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be 0 (meaning unknown). note: if the contentSizeFlag is set, pledgedSrcSize == 0 means the source size is actually 0 */ |
Yann Collet | 768df12 | 2017-04-26 15:42:10 -0700 | [diff] [blame] | 691 | ZSTDLIB_API size_t ZSTD_compressBegin_usingCDict(ZSTD_CCtx* cctx, const ZSTD_CDict* cdict); /**< note: fails if cdict==NULL */ |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 692 | 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=0 means null-size */ |
Sean Purcell | 2db7249 | 2017-02-09 10:50:43 -0800 | [diff] [blame] | 693 | ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); /**< note: if pledgedSrcSize can be 0, indicating unknown size. if it is non-zero, it must be accurate. for 0 size frames, use compressBegin_advanced */ |
Yann Collet | 715b9aa | 2017-04-18 13:55:53 -0700 | [diff] [blame] | 694 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 695 | ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 696 | ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 697 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 698 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 699 | |
inikep | ba1db37 | 2016-10-06 14:22:48 +0200 | [diff] [blame] | 700 | /*- |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 701 | Buffer-less streaming decompression (synchronous mode) |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 702 | |
| 703 | A ZSTD_DCtx object is required to track streaming operations. |
| 704 | Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. |
| 705 | A ZSTD_DCtx object can be re-used multiple times. |
| 706 | |
Yann Collet | 6b615d3 | 2016-07-29 19:40:37 +0200 | [diff] [blame] | 707 | First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams(). |
| 708 | It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame, |
| 709 | such as the minimum rolling buffer size to allocate to decompress data (`windowSize`), |
| 710 | and the dictionary ID used. |
| 711 | (Note : content size is optional, it may not be present. 0 means : content size unknown). |
| 712 | Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information. |
| 713 | As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation. |
| 714 | Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB. |
| 715 | Frame parameters are extracted from the beginning of the compressed frame. |
| 716 | Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes. |
| 717 | @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled. |
| 718 | >0 : `srcSize` is too small, please provide at least @result bytes on next attempt. |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 719 | errorCode, which can be tested using ZSTD_isError(). |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 720 | |
Yann Collet | 4eff813 | 2017-05-16 16:05:27 -0700 | [diff] [blame] | 721 | Start decompression, with ZSTD_decompressBegin(). |
| 722 | If decompression requires a dictionary, use ZSTD_decompressBegin_usingDict() or ZSTD_decompressBegin_usingDDict(). |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 723 | Alternatively, you can copy a prepared context, using ZSTD_copyDCtx(). |
| 724 | |
| 725 | Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. |
Yann Collet | 6b615d3 | 2016-07-29 19:40:37 +0200 | [diff] [blame] | 726 | ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). |
| 727 | ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 728 | |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 729 | @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity). |
Yann Collet | 6b615d3 | 2016-07-29 19:40:37 +0200 | [diff] [blame] | 730 | It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item. |
| 731 | It can also be an error code, which can be tested with ZSTD_isError(). |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 732 | |
Yann Collet | 3b6ae77 | 2016-07-08 23:42:22 +0200 | [diff] [blame] | 733 | ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`. |
| 734 | They should preferably be located contiguously, prior to current block. |
| 735 | Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters. |
| 736 | ZSTD_decompressContinue() is very sensitive to contiguity, |
| 737 | if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place, |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 738 | or that previous contiguous segment is large enough to properly handle maximum back-reference. |
Yann Collet | 3b6ae77 | 2016-07-08 23:42:22 +0200 | [diff] [blame] | 739 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 740 | A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. |
| 741 | Context can then be reset to start a new decompression. |
| 742 | |
Yann Collet | 4c5bbf6 | 2016-07-28 20:30:25 +0200 | [diff] [blame] | 743 | Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType(). |
Yann Collet | 6b615d3 | 2016-07-29 19:40:37 +0200 | [diff] [blame] | 744 | This information is not required to properly decode a frame. |
Yann Collet | 3b6ae77 | 2016-07-08 23:42:22 +0200 | [diff] [blame] | 745 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 746 | == Special case : skippable frames == |
Yann Collet | 3b6ae77 | 2016-07-08 23:42:22 +0200 | [diff] [blame] | 747 | |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 748 | Skippable frames allow integration of user-defined data into a flow of concatenated frames. |
Yann Collet | 5b56739 | 2016-07-28 01:17:22 +0200 | [diff] [blame] | 749 | Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows : |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 750 | a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F |
| 751 | b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits |
| 752 | c) Frame Content - any content (User Data) of length equal to Frame Size |
| 753 | For skippable frames ZSTD_decompressContinue() always returns 0. |
| 754 | For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable. |
Sean Purcell | 64417cd | 2017-02-22 13:29:01 -0800 | [diff] [blame] | 755 | Note : If fparamsPtr->frameContentSize==0, it is ambiguous: the frame might actually be a Zstd encoded frame with no content. |
| 756 | For purposes of decompression, it is valid in both cases to skip the frame using |
| 757 | ZSTD_findFrameCompressedSize to find its size in bytes. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 758 | It also returns Frame Size as fparamsPtr->frameContentSize. |
| 759 | */ |
| 760 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 761 | /*===== Buffer-less streaming decompression functions =====*/ |
Yann Collet | cef02d9 | 2017-05-10 11:14:08 -0700 | [diff] [blame] | 762 | ZSTDLIB_API size_t ZSTD_getFrameHeader(ZSTD_frameHeader* zfhPtr, const void* src, size_t srcSize); /**< doesn't consume input, see details below */ |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 763 | ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); |
| 764 | ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); |
Yann Collet | 4eff813 | 2017-05-16 16:05:27 -0700 | [diff] [blame] | 765 | ZSTDLIB_API size_t ZSTD_decompressBegin_usingDDict(ZSTD_DCtx* dctx, const ZSTD_DDict* ddict); |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 766 | ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); |
Yann Collet | 4eff813 | 2017-05-16 16:05:27 -0700 | [diff] [blame] | 767 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 768 | ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); |
| 769 | ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 770 | typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; |
| 771 | ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); |
| 772 | |
| 773 | /** |
| 774 | Block functions |
| 775 | |
| 776 | Block functions produce and decode raw zstd blocks, without frame metadata. |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 777 | Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes). |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 778 | User will have to take in charge required information to regenerate data, such as compressed and content sizes. |
| 779 | |
| 780 | A few rules to respect : |
Yann Collet | f246cf5 | 2016-07-06 20:30:52 +0200 | [diff] [blame] | 781 | - Compressing and decompressing require a context structure |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 782 | + Use ZSTD_createCCtx() and ZSTD_createDCtx() |
| 783 | - It is necessary to init context before starting |
Yann Collet | af4f45b | 2017-04-18 03:17:44 -0700 | [diff] [blame] | 784 | + compression : any ZSTD_compressBegin*() variant, including with dictionary |
| 785 | + decompression : any ZSTD_decompressBegin*() variant, including with dictionary |
| 786 | + copyCCtx() and copyDCtx() can be used too |
| 787 | - Block size is limited, it must be <= ZSTD_getBlockSizeMax() <= ZSTD_BLOCKSIZE_ABSOLUTEMAX |
| 788 | + If input is larger than a block size, it's necessary to split input data into multiple blocks |
| 789 | + For inputs larger than a single block size, consider using the regular ZSTD_compress() instead. |
| 790 | Frame metadata is not that costly, and quickly becomes negligible as source size grows larger. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 791 | - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero. |
| 792 | In which case, nothing is produced into `dst`. |
| 793 | + User must test for such outcome and deal directly with uncompressed data |
Yann Collet | f246cf5 | 2016-07-06 20:30:52 +0200 | [diff] [blame] | 794 | + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!! |
Yann Collet | af4f45b | 2017-04-18 03:17:44 -0700 | [diff] [blame] | 795 | + In case of multiple successive blocks, should some of them be uncompressed, |
| 796 | decoder must be informed of their existence in order to follow proper history. |
| 797 | Use ZSTD_insertBlock() for such a case. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 798 | */ |
| 799 | |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 800 | #define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024) /* define, for static allocation */ |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 801 | /*===== Raw zstd block functions =====*/ |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 802 | ZSTDLIB_API size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 803 | ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 804 | ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
Yann Collet | d5c5a77 | 2016-07-19 15:06:55 +0200 | [diff] [blame] | 805 | ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert block into `dctx` history. Useful for uncompressed blocks */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 806 | |
| 807 | |
Nick Terrell | 05c00f2 | 2016-11-29 11:46:37 -0800 | [diff] [blame] | 808 | #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 809 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 810 | #if defined (__cplusplus) |
| 811 | } |
| 812 | #endif |