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 ===== */ |
| 22 | #if defined(__GNUC__) && (__GNUC__ >= 4) |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 23 | # define ZSTDLIB_VISIBILITY __attribute__ ((visibility ("default"))) |
Christophe Chevalier | c6e8453 | 2015-12-07 17:44:09 +0100 | [diff] [blame] | 24 | #else |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 25 | # define ZSTDLIB_VISIBILITY |
| 26 | #endif |
| 27 | #if defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1) |
| 28 | # define ZSTDLIB_API __declspec(dllexport) ZSTDLIB_VISIBILITY |
| 29 | #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1) |
| 30 | # 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.*/ |
| 31 | #else |
| 32 | # define ZSTDLIB_API ZSTDLIB_VISIBILITY |
Christophe Chevalier | c6e8453 | 2015-12-07 17:44:09 +0100 | [diff] [blame] | 33 | #endif |
| 34 | |
| 35 | |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 36 | /******************************************************************************************************* |
| 37 | Introduction |
| 38 | |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 39 | 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] | 40 | at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and |
| 41 | decompression functions. The library supports compression levels from 1 up to ZSTD_maxCLevel() which is 22. |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 42 | Levels >= 20, labelled `--ultra`, should be used with caution, as they require more memory. |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 43 | Compression can be done in: |
| 44 | - a single step (described as Simple API) |
| 45 | - a single step, reusing a context (described as Explicit memory management) |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 46 | - unbounded multiple steps (described as Streaming compression) |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 47 | The compression ratio achievable on small data can be highly improved using compression with a dictionary in: |
| 48 | - a single step (described as Simple dictionary API) |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 49 | - a single step, reusing a dictionary (described as Fast dictionary API) |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 50 | |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 51 | Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h. |
| 52 | These APIs shall never be used with a dynamic library. |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 53 | They are not "stable", their definition may change in the future. Only static linking is allowed. |
| 54 | *********************************************************************************************************/ |
| 55 | |
| 56 | /*------ Version ------*/ |
Yann Collet | 901e85f | 2016-08-31 07:51:25 -0700 | [diff] [blame] | 57 | #define ZSTD_VERSION_MAJOR 1 |
Yann Collet | 1eb2fdc | 2016-09-18 12:21:47 +0200 | [diff] [blame] | 58 | #define ZSTD_VERSION_MINOR 1 |
Yann Collet | d46ecb5 | 2016-12-17 16:28:12 +0100 | [diff] [blame] | 59 | #define ZSTD_VERSION_RELEASE 3 |
Yann Collet | e02808f | 2016-04-20 22:46:16 +0200 | [diff] [blame] | 60 | |
| 61 | #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE |
| 62 | #define ZSTD_QUOTE(str) #str |
| 63 | #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str) |
| 64 | #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION) |
| 65 | |
Yann Collet | 213089c | 2015-06-18 07:43:16 -0800 | [diff] [blame] | 66 | #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE) |
Yann Collet | 379908b | 2016-12-06 10:36:15 -0800 | [diff] [blame] | 67 | ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< library version number; to be used when checking dll version */ |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 68 | |
| 69 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 70 | /*************************************** |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 71 | * Simple API |
Yann Collet | 7010c27 | 2015-10-21 09:07:25 +0100 | [diff] [blame] | 72 | ***************************************/ |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 73 | /*! ZSTD_compress() : |
Yann Collet | ac175d4 | 2016-09-13 00:51:47 +0200 | [diff] [blame] | 74 | Compresses `src` content as a single zstd compressed frame into already allocated `dst`. |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 75 | Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. |
Yann Collet | 64deef3 | 2016-09-14 00:16:07 +0200 | [diff] [blame] | 76 | @return : compressed size written into `dst` (<= `dstCapacity), |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 77 | 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] | 78 | ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity, |
| 79 | const void* src, size_t srcSize, |
| 80 | int compressionLevel); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 81 | |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 82 | /*! ZSTD_decompress() : |
Yann Collet | ac175d4 | 2016-09-13 00:51:47 +0200 | [diff] [blame] | 83 | `compressedSize` : must be the _exact_ size of a single compressed frame. |
| 84 | `dstCapacity` is an upper bound of originalSize. |
| 85 | If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data. |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 86 | @return : the number of bytes decompressed into `dst` (<= `dstCapacity`), |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 87 | 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] | 88 | ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity, |
Christophe Chevalier | c6e8453 | 2015-12-07 17:44:09 +0100 | [diff] [blame] | 89 | const void* src, size_t compressedSize); |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 90 | |
Yann Collet | ac175d4 | 2016-09-13 00:51:47 +0200 | [diff] [blame] | 91 | /*! ZSTD_getDecompressedSize() : |
| 92 | * 'src' is the start of a zstd compressed frame. |
| 93 | * @return : content size to be decompressed, as a 64-bits value _if known_, 0 otherwise. |
| 94 | * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode. |
| 95 | * When `return==0`, data to decompress could be any size. |
| 96 | * In which case, it's necessary to use streaming mode to decompress data. |
| 97 | * Optionally, application can still use ZSTD_decompress() while relying on implied limits. |
| 98 | * (For example, data may be necessarily cut into blocks <= 16 KB). |
| 99 | * note 2 : decompressed size is always present when compression is done with ZSTD_compress() |
| 100 | * note 3 : decompressed size can be very large (64-bits value), |
| 101 | * potentially larger than what local system can handle as a single memory segment. |
| 102 | * In which case, it's necessary to use streaming mode to decompress data. |
| 103 | * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified. |
| 104 | * Always ensure result fits within application's authorized limits. |
| 105 | * Each application can set its own limits. |
| 106 | * note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameParams() to know more. */ |
| 107 | ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize); |
| 108 | |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 109 | |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 110 | /*====== Helper functions ======*/ |
Yann Collet | 4110534 | 2016-07-27 15:09:11 +0200 | [diff] [blame] | 111 | ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */ |
| 112 | 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] | 113 | 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] | 114 | 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] | 115 | |
| 116 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 117 | /*************************************** |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 118 | * Explicit memory management |
Yann Collet | 7010c27 | 2015-10-21 09:07:25 +0100 | [diff] [blame] | 119 | ***************************************/ |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 120 | /*= Compression context |
Yann Collet | 379908b | 2016-12-06 10:36:15 -0800 | [diff] [blame] | 121 | * When compressing many times, |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 122 | * it is recommended to allocate a context just once, and re-use it for each successive compression operation. |
Yann Collet | 379908b | 2016-12-06 10:36:15 -0800 | [diff] [blame] | 123 | * This will make workload friendlier for system's memory. |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 124 | * Use one context per thread for parallel execution in multi-threaded environments. */ |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 125 | typedef struct ZSTD_CCtx_s ZSTD_CCtx; |
Christophe Chevalier | c6e8453 | 2015-12-07 17:44:09 +0100 | [diff] [blame] | 126 | ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void); |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 127 | ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx); |
Yann Collet | 7010c27 | 2015-10-21 09:07:25 +0100 | [diff] [blame] | 128 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 129 | /*! ZSTD_compressCCtx() : |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 130 | Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 131 | 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] | 132 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 133 | /*= Decompression context */ |
Yann Collet | 87c18b2 | 2016-08-26 01:43:47 +0200 | [diff] [blame] | 134 | typedef struct ZSTD_DCtx_s ZSTD_DCtx; |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 135 | ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void); |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 136 | ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 137 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 138 | /*! ZSTD_decompressDCtx() : |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 139 | * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()). */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 140 | ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 141 | |
| 142 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 143 | /************************** |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 144 | * Simple dictionary API |
| 145 | ***************************/ |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 146 | /*! ZSTD_compress_usingDict() : |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 147 | * Compression using a predefined Dictionary (see dictBuilder/zdict.h). |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 148 | * Note : This function loads the dictionary, resulting in significant startup delay. |
| 149 | * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 150 | ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx, |
| 151 | void* dst, size_t dstCapacity, |
| 152 | const void* src, size_t srcSize, |
| 153 | const void* dict,size_t dictSize, |
| 154 | int compressionLevel); |
| 155 | |
Yann Collet | 953ce72 | 2016-02-04 15:28:14 +0100 | [diff] [blame] | 156 | /*! ZSTD_decompress_usingDict() : |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 157 | * Decompression using a predefined Dictionary (see dictBuilder/zdict.h). |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 158 | * Dictionary must be identical to the one used during compression. |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 159 | * Note : This function loads the dictionary, resulting in significant startup delay. |
| 160 | * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */ |
Yann Collet | 7d968c7 | 2016-02-03 02:11:32 +0100 | [diff] [blame] | 161 | ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx, |
| 162 | void* dst, size_t dstCapacity, |
| 163 | const void* src, size_t srcSize, |
| 164 | const void* dict,size_t dictSize); |
Yann Collet | ecd651b | 2016-01-07 15:35:18 +0100 | [diff] [blame] | 165 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 166 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 167 | /**************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 168 | * Fast dictionary API |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 169 | ****************************/ |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 170 | typedef struct ZSTD_CDict_s ZSTD_CDict; |
| 171 | |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 172 | /*! ZSTD_createCDict() : |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 173 | * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once. |
| 174 | * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay. |
| 175 | * 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] | 176 | * `dictBuffer` can be released after ZSTD_CDict creation, as its content is copied within CDict */ |
| 177 | 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] | 178 | |
| 179 | /*! ZSTD_freeCDict() : |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 180 | * Function frees memory allocated by ZSTD_createCDict(). */ |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 181 | ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict); |
| 182 | |
| 183 | /*! ZSTD_compress_usingCDict() : |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 184 | * Compression using a digested Dictionary. |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 185 | * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times. |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 186 | * Note that compression level is decided during dictionary creation. */ |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 187 | ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, |
| 188 | void* dst, size_t dstCapacity, |
| 189 | const void* src, size_t srcSize, |
| 190 | const ZSTD_CDict* cdict); |
| 191 | |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 192 | |
| 193 | typedef struct ZSTD_DDict_s ZSTD_DDict; |
| 194 | |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 195 | /*! ZSTD_createDDict() : |
| 196 | * Create a digested dictionary, ready to start decompression operation without startup delay. |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 197 | * dictBuffer can be released after DDict creation, as its content is copied inside DDict */ |
| 198 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dictBuffer, size_t dictSize); |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 199 | |
| 200 | /*! ZSTD_freeDDict() : |
| 201 | * Function frees memory allocated with ZSTD_createDDict() */ |
Yann Collet | 302fb53 | 2016-06-07 12:16:49 +0200 | [diff] [blame] | 202 | ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict); |
| 203 | |
| 204 | /*! ZSTD_decompress_usingDDict() : |
Nick Terrell | d82efd8 | 2016-11-02 16:47:53 -0700 | [diff] [blame] | 205 | * Decompression using a digested Dictionary. |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 206 | * 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] | 207 | ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx, |
| 208 | void* dst, size_t dstCapacity, |
| 209 | const void* src, size_t srcSize, |
| 210 | const ZSTD_DDict* ddict); |
| 211 | |
| 212 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 213 | /**************************** |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 214 | * Streaming |
| 215 | ****************************/ |
| 216 | |
| 217 | typedef struct ZSTD_inBuffer_s { |
| 218 | const void* src; /**< start of input buffer */ |
| 219 | size_t size; /**< size of input buffer */ |
| 220 | size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */ |
| 221 | } ZSTD_inBuffer; |
| 222 | |
| 223 | typedef struct ZSTD_outBuffer_s { |
| 224 | void* dst; /**< start of output buffer */ |
| 225 | size_t size; /**< size of output buffer */ |
| 226 | size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */ |
| 227 | } ZSTD_outBuffer; |
| 228 | |
| 229 | |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 230 | |
inikep | ba1db37 | 2016-10-06 14:22:48 +0200 | [diff] [blame] | 231 | /*-*********************************************************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 232 | * Streaming compression - HowTo |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 233 | * |
| 234 | * A ZSTD_CStream object is required to track streaming operation. |
| 235 | * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources. |
| 236 | * ZSTD_CStream objects can be reused multiple times on consecutive compression operations. |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 237 | * It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively, |
| 238 | * since it will play nicer with system's memory, by re-using already allocated memory. |
| 239 | * Use one separate ZSTD_CStream per thread for parallel execution. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 240 | * |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 241 | * Start a new compression by initializing ZSTD_CStream. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 242 | * Use ZSTD_initCStream() to start a new compression operation. |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 243 | * 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] | 244 | * |
| 245 | * Use ZSTD_compressStream() repetitively to consume input stream. |
Yann Collet | fa72f6b | 2016-09-05 17:39:56 +0200 | [diff] [blame] | 246 | * The function will automatically update both `pos` fields. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 247 | * Note that it may not consume the entire input, in which case `pos < size`, |
| 248 | * and it's up to the caller to present again remaining data. |
Yann Collet | 4bf317d | 2016-08-28 07:43:34 -0700 | [diff] [blame] | 249 | * @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] | 250 | * or an error code, which can be tested using ZSTD_isError(). |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 251 | * Note 1 : it's just a hint, to help latency a little, any other value will work fine. |
| 252 | * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize() |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 253 | * |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 254 | * 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] | 255 | * `output->pos` will be updated. |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 256 | * 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] | 257 | * @return : nb of bytes still present within internal buffer (0 if it's empty) |
| 258 | * or an error code, which can be tested using ZSTD_isError(). |
| 259 | * |
| 260 | * ZSTD_endStream() instructs to finish a frame. |
| 261 | * It will perform a flush and write frame epilogue. |
| 262 | * The epilogue is required for decoders to consider a frame completed. |
| 263 | * Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small. |
| 264 | * In which case, call again ZSTD_endStream() to complete the flush. |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 265 | * @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] | 266 | * or an error code, which can be tested using ZSTD_isError(). |
| 267 | * |
| 268 | * *******************************************************************/ |
| 269 | |
| 270 | typedef struct ZSTD_CStream_s ZSTD_CStream; |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 271 | ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void); |
| 272 | ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 273 | |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 274 | ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel); |
| 275 | ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input); |
| 276 | ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
| 277 | ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output); |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 278 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 279 | ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */ |
| 280 | 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] | 281 | |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 282 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 283 | |
inikep | ba1db37 | 2016-10-06 14:22:48 +0200 | [diff] [blame] | 284 | /*-*************************************************************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 285 | * Streaming decompression - HowTo |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 286 | * |
| 287 | * A ZSTD_DStream object is required to track streaming operations. |
| 288 | * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources. |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 289 | * ZSTD_DStream objects can be re-used multiple times. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 290 | * |
| 291 | * Use ZSTD_initDStream() to start a new decompression operation, |
| 292 | * or ZSTD_initDStream_usingDict() if decompression requires a dictionary. |
Yann Collet | 7c83dfd | 2016-09-05 19:47:43 +0200 | [diff] [blame] | 293 | * @return : recommended first input size |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 294 | * |
| 295 | * Use ZSTD_decompressStream() repetitively to consume your input. |
Yann Collet | fa72f6b | 2016-09-05 17:39:56 +0200 | [diff] [blame] | 296 | * The function will update both `pos` fields. |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 297 | * If `input.pos < input.size`, some input has not been consumed. |
Yann Collet | 1d4208c | 2016-09-06 05:16:40 +0200 | [diff] [blame] | 298 | * It's up to the caller to present again remaining data. |
Yann Collet | b3060f7 | 2016-09-09 16:44:16 +0200 | [diff] [blame] | 299 | * If `output.pos < output.size`, decoder has flushed everything it could. |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 300 | * @return : 0 when a frame is completely decoded and fully flushed, |
Yann Collet | fa72f6b | 2016-09-05 17:39:56 +0200 | [diff] [blame] | 301 | * an error code, which can be tested using ZSTD_isError(), |
Yann Collet | 9ffbeea | 2016-12-02 18:37:38 -0800 | [diff] [blame] | 302 | * any other value > 0, which means there is still some decoding to do to complete current frame. |
| 303 | * 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] | 304 | * *******************************************************************************/ |
| 305 | |
| 306 | typedef struct ZSTD_DStream_s ZSTD_DStream; |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 307 | ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void); |
| 308 | ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 309 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 310 | ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds); |
| 311 | 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] | 312 | |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 313 | ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */ |
Yann Collet | 01c1992 | 2016-09-08 19:29:04 +0200 | [diff] [blame] | 314 | 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] | 315 | |
Nick Terrell | 05c00f2 | 2016-11-29 11:46:37 -0800 | [diff] [blame] | 316 | #endif /* ZSTD_H_235446 */ |
Yann Collet | 7be46bf | 2016-08-19 18:39:36 +0200 | [diff] [blame] | 317 | |
| 318 | |
Nick Terrell | 05c00f2 | 2016-11-29 11:46:37 -0800 | [diff] [blame] | 319 | #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY) |
| 320 | #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 321 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 322 | /**************************************************************************************** |
inikep | 2d26133 | 2016-10-06 16:28:21 +0200 | [diff] [blame] | 323 | * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 324 | * The definitions in this section are considered experimental. |
Yann Collet | a49e066 | 2016-06-21 11:54:03 +0200 | [diff] [blame] | 325 | * 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] | 326 | * They are provided for advanced usages. |
| 327 | * Use them only in association with static linking. |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 328 | * ***************************************************************************************/ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 329 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 330 | /* --- Constants ---*/ |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 331 | #define ZSTD_MAGICNUMBER 0xFD2FB528 /* >= v0.8.0 */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 332 | #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U |
| 333 | |
Yann Collet | ed3845d | 2016-07-08 12:57:10 +0200 | [diff] [blame] | 334 | #define ZSTD_WINDOWLOG_MAX_32 25 |
| 335 | #define ZSTD_WINDOWLOG_MAX_64 27 |
| 336 | #define ZSTD_WINDOWLOG_MAX ((U32)(MEM_32bits() ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64)) |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 337 | #define ZSTD_WINDOWLOG_MIN 10 |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 338 | #define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX |
Yann Collet | cf409a7 | 2016-09-26 16:41:05 +0200 | [diff] [blame] | 339 | #define ZSTD_HASHLOG_MIN 6 |
| 340 | #define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1) |
| 341 | #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 342 | #define ZSTD_HASHLOG3_MAX 17 |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 343 | #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1) |
| 344 | #define ZSTD_SEARCHLOG_MIN 1 |
Yann Collet | 0e07bf3 | 2016-09-07 06:33:02 +0200 | [diff] [blame] | 345 | #define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */ |
| 346 | #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] | 347 | #define ZSTD_TARGETLENGTH_MIN 4 |
| 348 | #define ZSTD_TARGETLENGTH_MAX 999 |
| 349 | |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 350 | #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */ |
Yann Collet | ba75e9d | 2016-12-21 19:57:18 +0100 | [diff] [blame] | 351 | #define ZSTD_FRAMEHEADERSIZE_MIN 6 |
Yann Collet | 7c83dfd | 2016-09-05 19:47:43 +0200 | [diff] [blame] | 352 | static const size_t ZSTD_frameHeaderSize_prefix = 5; |
Yann Collet | ba75e9d | 2016-12-21 19:57:18 +0100 | [diff] [blame] | 353 | static const size_t ZSTD_frameHeaderSize_min = ZSTD_FRAMEHEADERSIZE_MIN; |
Yann Collet | 673f0d7 | 2016-06-06 00:26:38 +0200 | [diff] [blame] | 354 | static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX; |
| 355 | static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */ |
| 356 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 357 | |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 358 | /*--- Advanced types ---*/ |
Przemyslaw Skibinski | 5c5f01f | 2016-10-25 12:25:07 +0200 | [diff] [blame] | 359 | typedef enum { ZSTD_fast, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt, ZSTD_btopt2 } ZSTD_strategy; /* from faster to stronger */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 360 | |
| 361 | typedef struct { |
Yann Collet | 655393c | 2016-08-14 00:16:20 +0200 | [diff] [blame] | 362 | unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */ |
| 363 | unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */ |
| 364 | unsigned hashLog; /**< dispatch table : larger == faster, more memory */ |
| 365 | unsigned searchLog; /**< nb of searches : larger == more compression, slower */ |
| 366 | unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */ |
| 367 | 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] | 368 | ZSTD_strategy strategy; |
| 369 | } ZSTD_compressionParameters; |
| 370 | |
| 371 | typedef struct { |
Yann Collet | 655393c | 2016-08-14 00:16:20 +0200 | [diff] [blame] | 372 | unsigned contentSizeFlag; /**< 1: content size will be in frame header (if known). */ |
| 373 | unsigned checksumFlag; /**< 1: will generate a 22-bits checksum at end of frame, to be used for error detection by decompressor */ |
| 374 | unsigned noDictIDFlag; /**< 1: no dict ID will be saved into frame header (if dictionary compression) */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 375 | } ZSTD_frameParameters; |
| 376 | |
| 377 | typedef struct { |
| 378 | ZSTD_compressionParameters cParams; |
| 379 | ZSTD_frameParameters fParams; |
| 380 | } ZSTD_parameters; |
| 381 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 382 | /*= Custom memory allocation functions */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 383 | typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size); |
| 384 | typedef void (*ZSTD_freeFunction) (void* opaque, void* address); |
| 385 | typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem; |
| 386 | |
| 387 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 388 | /*************************************** |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 389 | * Advanced compression functions |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 390 | ***************************************/ |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 391 | /*! ZSTD_estimateCCtxSize() : |
| 392 | * Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters. |
| 393 | * `frameContentSize` is an optional parameter, provide `0` if unknown */ |
Yann Collet | 8847238 | 2016-07-14 17:05:38 +0200 | [diff] [blame] | 394 | ZSTDLIB_API size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams); |
Yann Collet | 3ae543c | 2016-07-11 03:12:17 +0200 | [diff] [blame] | 395 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 396 | /*! ZSTD_createCCtx_advanced() : |
| 397 | * Create a ZSTD compression context using external alloc and free functions */ |
| 398 | ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem); |
| 399 | |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 400 | /*! ZSTD_sizeofCCtx() : |
| 401 | * Gives the amount of memory used by a given ZSTD_CCtx */ |
| 402 | ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx); |
| 403 | |
Yann Collet | 1f57c2e | 2016-12-21 16:20:11 +0100 | [diff] [blame] | 404 | /*! ZSTD_createCDict_byReference() : |
| 405 | * Create a digested dictionary for compression |
| 406 | * Dictionary content is simply referenced, and therefore stays in dictBuffer. |
| 407 | * It is important that dictBuffer outlives CDict, it must remain read accessible throughout the lifetime of CDict */ |
| 408 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_byReference(const void* dictBuffer, size_t dictSize, int compressionLevel); |
| 409 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 410 | /*! ZSTD_createCDict_advanced() : |
| 411 | * 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] | 412 | ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, unsigned byReference, |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 413 | ZSTD_parameters params, ZSTD_customMem customMem); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 414 | |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 415 | /*! ZSTD_sizeof_CDict() : |
| 416 | * Gives the amount of memory used by a given ZSTD_sizeof_CDict */ |
| 417 | ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict); |
Yann Collet | 8e0ee68 | 2016-07-11 13:09:52 +0200 | [diff] [blame] | 418 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 419 | /*! ZSTD_getCParams() : |
Yann Collet | 2b36b23 | 2016-12-13 17:59:55 +0100 | [diff] [blame] | 420 | * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize. |
| 421 | * `estimatedSrcSize` value is optional, select 0 if not known */ |
| 422 | ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize); |
| 423 | |
| 424 | /*! ZSTD_getParams() : |
| 425 | * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`. |
| 426 | * All fields of `ZSTD_frameParameters` are set to default (0) */ |
| 427 | 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] | 428 | |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 429 | /*! ZSTD_checkCParams() : |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 430 | * Ensure param values remain within authorized range */ |
| 431 | ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params); |
| 432 | |
Yann Collet | 3d2cd7f | 2016-06-27 15:12:26 +0200 | [diff] [blame] | 433 | /*! ZSTD_adjustCParams() : |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 434 | * optimize params for a given `srcSize` and `dictSize`. |
| 435 | * both values are optional, select `0` if unknown. */ |
Yann Collet | 52c04fe | 2016-07-07 11:53:18 +0200 | [diff] [blame] | 436 | 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] | 437 | |
| 438 | /*! ZSTD_compress_advanced() : |
| 439 | * Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter */ |
| 440 | ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx, |
| 441 | void* dst, size_t dstCapacity, |
| 442 | const void* src, size_t srcSize, |
| 443 | const void* dict,size_t dictSize, |
| 444 | ZSTD_parameters params); |
| 445 | |
Yann Collet | 45c03c5 | 2016-06-14 13:46:11 +0200 | [diff] [blame] | 446 | |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 447 | /*--- Advanced decompression functions ---*/ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 448 | |
Yann Collet | 179b197 | 2016-11-02 17:30:49 -0700 | [diff] [blame] | 449 | /*! ZSTD_isFrame() : |
| 450 | * Tells if the content of `buffer` starts with a valid Frame Identifier. |
| 451 | * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0. |
| 452 | * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled. |
| 453 | * Note 3 : Skippable Frame Identifiers are considered valid. */ |
| 454 | ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size); |
| 455 | |
Yann Collet | d158c35 | 2016-07-11 13:46:25 +0200 | [diff] [blame] | 456 | /*! ZSTD_estimateDCtxSize() : |
| 457 | * Gives the potential amount of memory allocated to create a ZSTD_DCtx */ |
| 458 | ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void); |
| 459 | |
Yann Collet | 81e13ef | 2016-06-07 00:51:51 +0200 | [diff] [blame] | 460 | /*! ZSTD_createDCtx_advanced() : |
| 461 | * Create a ZSTD decompression context using external alloc and free functions */ |
| 462 | ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 463 | |
Yann Collet | d7c6589 | 2016-09-15 02:50:27 +0200 | [diff] [blame] | 464 | /*! ZSTD_sizeof_DCtx() : |
Yann Collet | d158c35 | 2016-07-11 13:46:25 +0200 | [diff] [blame] | 465 | * Gives the amount of memory used by a given ZSTD_DCtx */ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 466 | ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx); |
Yann Collet | 8e0ee68 | 2016-07-11 13:09:52 +0200 | [diff] [blame] | 467 | |
Yann Collet | 4e5eea6 | 2016-12-21 16:44:35 +0100 | [diff] [blame] | 468 | /*! ZSTD_createDDict_byReference() : |
| 469 | * Create a digested dictionary, ready to start decompression operation without startup delay. |
| 470 | * Dictionary content is simply referenced, and therefore stays in dictBuffer. |
| 471 | * It is important that dictBuffer outlives DDict, it must remain read accessible throughout the lifetime of DDict */ |
| 472 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_byReference(const void* dictBuffer, size_t dictSize); |
| 473 | |
| 474 | ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict_advanced(const void* dict, size_t dictSize, |
| 475 | unsigned byReference, ZSTD_customMem customMem); |
| 476 | |
Yann Collet | e91c4b4 | 2016-09-14 16:55:44 +0200 | [diff] [blame] | 477 | /*! ZSTD_sizeof_DDict() : |
| 478 | * Gives the amount of memory used by a given ZSTD_DDict */ |
| 479 | ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict); |
| 480 | |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 481 | /*! ZSTD_getDictID_fromDict() : |
| 482 | * Provides the dictID stored within dictionary. |
| 483 | * if @return == 0, the dictionary is not conformant with Zstandard specification. |
| 484 | * It can still be loaded, but as a content-only dictionary. */ |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 485 | ZSTDLIB_API unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize); |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 486 | |
| 487 | /*! ZSTD_getDictID_fromDDict() : |
| 488 | * Provides the dictID of the dictionary loaded into `ddict`. |
| 489 | * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty. |
| 490 | * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */ |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 491 | ZSTDLIB_API unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict); |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 492 | |
| 493 | /*! ZSTD_getDictID_fromFrame() : |
| 494 | * Provides the dictID required to decompressed the frame stored within `src`. |
| 495 | * If @return == 0, the dictID could not be decoded. |
| 496 | * This could for one of the following reasons : |
| 497 | * - The frame does not require a dictionary to be decoded (most common case). |
| 498 | * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information. |
| 499 | * Note : this use case also happens when using a non-conformant dictionary. |
| 500 | * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`). |
| 501 | * - This is not a Zstandard frame. |
| 502 | * When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code. */ |
Nick Terrell | 8de46ab | 2016-12-16 13:27:30 -0800 | [diff] [blame] | 503 | ZSTDLIB_API unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize); |
Yann Collet | e7a41a5 | 2016-12-05 16:21:06 -0800 | [diff] [blame] | 504 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 505 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 506 | /******************************************************************** |
Przemyslaw Skibinski | 984b66c | 2016-10-24 15:59:51 +0200 | [diff] [blame] | 507 | * Advanced streaming functions |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 508 | ********************************************************************/ |
| 509 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 510 | /*===== Advanced Streaming compression functions =====*/ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 511 | ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem); |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 512 | ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct */ |
Sean Purcell | 0b5370a | 2017-01-18 13:44:43 -0800 | [diff] [blame^] | 513 | 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 */ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 514 | ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize, |
Yann Collet | 4cb2129 | 2016-09-15 14:54:07 +0200 | [diff] [blame] | 515 | ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be zero == unknown */ |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 516 | 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 | ee5b725 | 2016-10-27 14:20:55 -0700 | [diff] [blame] | 517 | ZSTDLIB_API size_t ZSTD_resetCStream(ZSTD_CStream* zcs, unsigned long long pledgedSrcSize); /**< re-use compression parameters from previous init; skip dictionary loading stage; zcs must be init at least once before */ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 518 | ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs); |
Yann Collet | cb32763 | 2016-08-23 00:30:31 +0200 | [diff] [blame] | 519 | |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 520 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 521 | /*===== Advanced Streaming decompression functions =====*/ |
Yann Collet | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 522 | typedef enum { ZSTDdsp_maxWindowSize } ZSTD_DStreamParameter_e; |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 523 | ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem); |
Sean Purcell | 0b5370a | 2017-01-18 13:44:43 -0800 | [diff] [blame^] | 524 | 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 | 17e482e | 2016-08-23 16:58:10 +0200 | [diff] [blame] | 525 | ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue); |
Yann Collet | 9516234 | 2016-10-25 16:19:52 -0700 | [diff] [blame] | 526 | 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] | 527 | ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */ |
Yann Collet | 70e3b31 | 2016-08-23 01:18:06 +0200 | [diff] [blame] | 528 | ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds); |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 529 | |
| 530 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 531 | /********************************************************************* |
Yann Collet | 5a0c8e2 | 2016-08-12 01:20:36 +0200 | [diff] [blame] | 532 | * Buffer-less and synchronous inner streaming functions |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 533 | * |
| 534 | * 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] | 535 | * But it's also a complex one, with many restrictions (documented below). |
Yann Collet | 37d1300 | 2016-10-24 17:22:12 -0700 | [diff] [blame] | 536 | * Prefer using normal streaming API for an easier experience |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 537 | ********************************************************************* */ |
Yann Collet | 60ba31c | 2016-07-28 19:55:09 +0200 | [diff] [blame] | 538 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 539 | /** |
| 540 | Buffer-less streaming compression (synchronous mode) |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 541 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 542 | A ZSTD_CCtx object is required to track streaming operations. |
Yann Collet | 45c03c5 | 2016-06-14 13:46:11 +0200 | [diff] [blame] | 543 | Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 544 | ZSTD_CCtx object can be re-used multiple times within successive compression operations. |
| 545 | |
| 546 | Start by initializing a context. |
| 547 | Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression, |
| 548 | or ZSTD_compressBegin_advanced(), for finer parameter control. |
| 549 | It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx() |
| 550 | |
| 551 | Then, consume your input using ZSTD_compressContinue(). |
Yann Collet | a49e066 | 2016-06-21 11:54:03 +0200 | [diff] [blame] | 552 | There are some important considerations to keep in mind when using this advanced function : |
| 553 | - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only. |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 554 | - 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] | 555 | - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario. |
| 556 | Worst case evaluation is provided by ZSTD_compressBound(). |
| 557 | ZSTD_compressContinue() doesn't guarantee recover after a failed compression. |
| 558 | - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog). |
| 559 | It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks) |
| 560 | - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps. |
| 561 | In which case, it will "discard" the relevant memory section from its history. |
| 562 | |
Yann Collet | 62470b4 | 2016-07-28 15:29:08 +0200 | [diff] [blame] | 563 | Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum. |
| 564 | It's possible to use a NULL,0 src content, in which case, it will write a final empty block to end the frame, |
| 565 | Without last block mark, frames will be considered unfinished (broken) by decoders. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 566 | |
Yann Collet | a49e066 | 2016-06-21 11:54:03 +0200 | [diff] [blame] | 567 | You can then reuse `ZSTD_CCtx` (ZSTD_compressBegin()) to compress some new frame. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 568 | */ |
| 569 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 570 | /*===== Buffer-less streaming compression functions =====*/ |
| 571 | ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel); |
| 572 | ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel); |
| 573 | ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize); |
| 574 | ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize); |
| 575 | ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 576 | 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] | 577 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 578 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 579 | |
inikep | ba1db37 | 2016-10-06 14:22:48 +0200 | [diff] [blame] | 580 | /*- |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 581 | Buffer-less streaming decompression (synchronous mode) |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 582 | |
| 583 | A ZSTD_DCtx object is required to track streaming operations. |
| 584 | Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it. |
| 585 | A ZSTD_DCtx object can be re-used multiple times. |
| 586 | |
Yann Collet | 6b615d3 | 2016-07-29 19:40:37 +0200 | [diff] [blame] | 587 | First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams(). |
| 588 | It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame, |
| 589 | such as the minimum rolling buffer size to allocate to decompress data (`windowSize`), |
| 590 | and the dictionary ID used. |
| 591 | (Note : content size is optional, it may not be present. 0 means : content size unknown). |
| 592 | Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information. |
| 593 | As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation. |
| 594 | Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB. |
| 595 | Frame parameters are extracted from the beginning of the compressed frame. |
| 596 | Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes. |
| 597 | @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled. |
| 598 | >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] | 599 | errorCode, which can be tested using ZSTD_isError(). |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 600 | |
| 601 | Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict(). |
| 602 | Alternatively, you can copy a prepared context, using ZSTD_copyDCtx(). |
| 603 | |
| 604 | Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively. |
Yann Collet | 6b615d3 | 2016-07-29 19:40:37 +0200 | [diff] [blame] | 605 | ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue(). |
| 606 | ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 607 | |
Yann Collet | 49bb004 | 2016-06-04 20:17:38 +0200 | [diff] [blame] | 608 | @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] | 609 | It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item. |
| 610 | 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] | 611 | |
Yann Collet | 3b6ae77 | 2016-07-08 23:42:22 +0200 | [diff] [blame] | 612 | ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`. |
| 613 | They should preferably be located contiguously, prior to current block. |
| 614 | Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters. |
| 615 | ZSTD_decompressContinue() is very sensitive to contiguity, |
| 616 | 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] | 617 | 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] | 618 | |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 619 | A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero. |
| 620 | Context can then be reset to start a new decompression. |
| 621 | |
Yann Collet | 4c5bbf6 | 2016-07-28 20:30:25 +0200 | [diff] [blame] | 622 | 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] | 623 | This information is not required to properly decode a frame. |
Yann Collet | 3b6ae77 | 2016-07-08 23:42:22 +0200 | [diff] [blame] | 624 | |
Yann Collet | e795c8a | 2016-12-13 16:39:36 +0100 | [diff] [blame] | 625 | == Special case : skippable frames == |
Yann Collet | 3b6ae77 | 2016-07-08 23:42:22 +0200 | [diff] [blame] | 626 | |
Yann Collet | d469a98 | 2016-07-28 03:47:45 +0200 | [diff] [blame] | 627 | 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] | 628 | 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] | 629 | a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F |
| 630 | b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits |
| 631 | c) Frame Content - any content (User Data) of length equal to Frame Size |
| 632 | For skippable frames ZSTD_decompressContinue() always returns 0. |
| 633 | For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable. |
| 634 | It also returns Frame Size as fparamsPtr->frameContentSize. |
| 635 | */ |
| 636 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 637 | typedef struct { |
| 638 | unsigned long long frameContentSize; |
| 639 | unsigned windowSize; |
| 640 | unsigned dictID; |
| 641 | unsigned checksumFlag; |
| 642 | } ZSTD_frameParams; |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 643 | |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 644 | /*===== Buffer-less streaming decompression functions =====*/ |
| 645 | ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input, see details below */ |
| 646 | ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx); |
| 647 | ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize); |
| 648 | ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx); |
| 649 | ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx); |
| 650 | ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 651 | typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e; |
| 652 | ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx); |
| 653 | |
| 654 | /** |
| 655 | Block functions |
| 656 | |
| 657 | Block functions produce and decode raw zstd blocks, without frame metadata. |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 658 | 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] | 659 | User will have to take in charge required information to regenerate data, such as compressed and content sizes. |
| 660 | |
| 661 | A few rules to respect : |
Yann Collet | f246cf5 | 2016-07-06 20:30:52 +0200 | [diff] [blame] | 662 | - Compressing and decompressing require a context structure |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 663 | + Use ZSTD_createCCtx() and ZSTD_createDCtx() |
| 664 | - It is necessary to init context before starting |
| 665 | + compression : ZSTD_compressBegin() |
| 666 | + decompression : ZSTD_decompressBegin() |
| 667 | + variants _usingDict() are also allowed |
| 668 | + copyCCtx() and copyDCtx() work too |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 669 | - Block size is limited, it must be <= ZSTD_getBlockSizeMax() |
| 670 | + If you need to compress more, cut data into multiple blocks |
| 671 | + Consider using the regular ZSTD_compress() instead, as frame metadata costs become negligible when source size is large. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 672 | - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero. |
| 673 | In which case, nothing is produced into `dst`. |
| 674 | + User must test for such outcome and deal directly with uncompressed data |
Yann Collet | f246cf5 | 2016-07-06 20:30:52 +0200 | [diff] [blame] | 675 | + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!! |
| 676 | + In case of multiple successive blocks, decoder must be informed of uncompressed block existence to follow proper history. |
| 677 | Use ZSTD_insertBlock() in such a case. |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 678 | */ |
| 679 | |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 680 | #define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024) /* define, for static allocation */ |
inikep | 82057aa | 2016-10-06 13:23:52 +0200 | [diff] [blame] | 681 | /*===== Raw zstd block functions =====*/ |
Yann Collet | cf05b9d | 2016-07-18 16:52:10 +0200 | [diff] [blame] | 682 | ZSTDLIB_API size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx); |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 683 | ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize); |
| 684 | 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] | 685 | 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] | 686 | |
| 687 | |
Nick Terrell | 05c00f2 | 2016-11-29 11:46:37 -0800 | [diff] [blame] | 688 | #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */ |
Yann Collet | d3b7f8d | 2016-06-04 19:47:02 +0200 | [diff] [blame] | 689 | |
Yann Collet | 4856a00 | 2015-01-24 01:58:16 +0100 | [diff] [blame] | 690 | #if defined (__cplusplus) |
| 691 | } |
| 692 | #endif |