zstd-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +19 -0
  3. data/.gitmodules +3 -0
  4. data/.rspec +2 -0
  5. data/.travis.yml +11 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE.txt +29 -0
  9. data/README.md +63 -0
  10. data/Rakefile +22 -0
  11. data/bin/console +14 -0
  12. data/bin/setup +8 -0
  13. data/ext/zstdruby/extconf.rb +23 -0
  14. data/ext/zstdruby/libzstd/.gitignore +2 -0
  15. data/ext/zstdruby/libzstd/Makefile +133 -0
  16. data/ext/zstdruby/libzstd/README.md +77 -0
  17. data/ext/zstdruby/libzstd/common/bitstream.h +414 -0
  18. data/ext/zstdruby/libzstd/common/entropy_common.c +227 -0
  19. data/ext/zstdruby/libzstd/common/error_private.c +43 -0
  20. data/ext/zstdruby/libzstd/common/error_private.h +76 -0
  21. data/ext/zstdruby/libzstd/common/fse.h +668 -0
  22. data/ext/zstdruby/libzstd/common/fse_decompress.c +329 -0
  23. data/ext/zstdruby/libzstd/common/huf.h +238 -0
  24. data/ext/zstdruby/libzstd/common/mem.h +372 -0
  25. data/ext/zstdruby/libzstd/common/xxhash.c +867 -0
  26. data/ext/zstdruby/libzstd/common/xxhash.h +309 -0
  27. data/ext/zstdruby/libzstd/common/zstd_common.c +77 -0
  28. data/ext/zstdruby/libzstd/common/zstd_errors.h +60 -0
  29. data/ext/zstdruby/libzstd/common/zstd_internal.h +270 -0
  30. data/ext/zstdruby/libzstd/compress/fse_compress.c +850 -0
  31. data/ext/zstdruby/libzstd/compress/huf_compress.c +609 -0
  32. data/ext/zstdruby/libzstd/compress/zstd_compress.c +3291 -0
  33. data/ext/zstdruby/libzstd/compress/zstd_opt.h +919 -0
  34. data/ext/zstdruby/libzstd/decompress/huf_decompress.c +885 -0
  35. data/ext/zstdruby/libzstd/decompress/zstd_decompress.c +2154 -0
  36. data/ext/zstdruby/libzstd/deprecated/zbuff.h +210 -0
  37. data/ext/zstdruby/libzstd/deprecated/zbuff_compress.c +145 -0
  38. data/ext/zstdruby/libzstd/deprecated/zbuff_decompress.c +74 -0
  39. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.c +1913 -0
  40. data/ext/zstdruby/libzstd/dictBuilder/divsufsort.h +67 -0
  41. data/ext/zstdruby/libzstd/dictBuilder/zdict.c +1012 -0
  42. data/ext/zstdruby/libzstd/dictBuilder/zdict.h +111 -0
  43. data/ext/zstdruby/libzstd/dll/example/Makefile +47 -0
  44. data/ext/zstdruby/libzstd/dll/example/README.md +69 -0
  45. data/ext/zstdruby/libzstd/dll/example/build_package.bat +17 -0
  46. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.sln +25 -0
  47. data/ext/zstdruby/libzstd/dll/example/fullbench-dll.vcxproj +179 -0
  48. data/ext/zstdruby/libzstd/dll/libzstd.def +86 -0
  49. data/ext/zstdruby/libzstd/legacy/zstd_legacy.h +259 -0
  50. data/ext/zstdruby/libzstd/legacy/zstd_v01.c +2095 -0
  51. data/ext/zstdruby/libzstd/legacy/zstd_v01.h +80 -0
  52. data/ext/zstdruby/libzstd/legacy/zstd_v02.c +3518 -0
  53. data/ext/zstdruby/libzstd/legacy/zstd_v02.h +79 -0
  54. data/ext/zstdruby/libzstd/legacy/zstd_v03.c +3159 -0
  55. data/ext/zstdruby/libzstd/legacy/zstd_v03.h +79 -0
  56. data/ext/zstdruby/libzstd/legacy/zstd_v04.c +3795 -0
  57. data/ext/zstdruby/libzstd/legacy/zstd_v04.h +128 -0
  58. data/ext/zstdruby/libzstd/legacy/zstd_v05.c +4056 -0
  59. data/ext/zstdruby/libzstd/legacy/zstd_v05.h +149 -0
  60. data/ext/zstdruby/libzstd/legacy/zstd_v06.c +4167 -0
  61. data/ext/zstdruby/libzstd/legacy/zstd_v06.h +159 -0
  62. data/ext/zstdruby/libzstd/legacy/zstd_v07.c +4540 -0
  63. data/ext/zstdruby/libzstd/legacy/zstd_v07.h +173 -0
  64. data/ext/zstdruby/libzstd/libzstd.pc.in +14 -0
  65. data/ext/zstdruby/libzstd/zstd.h +673 -0
  66. data/ext/zstdruby/zstdruby.c +117 -0
  67. data/ext/zstdruby/zstdruby.h +6 -0
  68. data/lib/zstd-ruby.rb +6 -0
  69. data/lib/zstd-ruby/version.rb +3 -0
  70. data/zstd-ruby.gemspec +37 -0
  71. metadata +170 -0
@@ -0,0 +1,173 @@
1
+ /**
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
+ */
9
+
10
+ #ifndef ZSTDv07_H_235446
11
+ #define ZSTDv07_H_235446
12
+
13
+ #if defined (__cplusplus)
14
+ extern "C" {
15
+ #endif
16
+
17
+ /*====== Dependency ======*/
18
+ #include <stddef.h> /* size_t */
19
+
20
+
21
+ /*====== Export for Windows ======*/
22
+ /*!
23
+ * ZSTDv07_DLL_EXPORT :
24
+ * Enable exporting of functions when building a Windows DLL
25
+ */
26
+ #if defined(_WIN32) && defined(ZSTDv07_DLL_EXPORT) && (ZSTDv07_DLL_EXPORT==1)
27
+ # define ZSTDLIBv07_API __declspec(dllexport)
28
+ #else
29
+ # define ZSTDLIBv07_API
30
+ #endif
31
+
32
+
33
+ /* *************************************
34
+ * Simple API
35
+ ***************************************/
36
+ /*! ZSTDv07_getDecompressedSize() :
37
+ * @return : decompressed size if known, 0 otherwise.
38
+ note 1 : if `0`, follow up with ZSTDv07_getFrameParams() to know precise failure cause.
39
+ note 2 : decompressed size could be wrong or intentionally modified !
40
+ always ensure results fit within application's authorized limits */
41
+ unsigned long long ZSTDv07_getDecompressedSize(const void* src, size_t srcSize);
42
+
43
+ /*! ZSTDv07_decompress() :
44
+ `compressedSize` : must be _exact_ size of compressed input, otherwise decompression will fail.
45
+ `dstCapacity` must be equal or larger than originalSize.
46
+ @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
47
+ or an errorCode if it fails (which can be tested using ZSTDv07_isError()) */
48
+ ZSTDLIBv07_API size_t ZSTDv07_decompress( void* dst, size_t dstCapacity,
49
+ const void* src, size_t compressedSize);
50
+
51
+ /*====== Helper functions ======*/
52
+ ZSTDLIBv07_API unsigned ZSTDv07_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
53
+ ZSTDLIBv07_API const char* ZSTDv07_getErrorName(size_t code); /*!< provides readable string from an error code */
54
+
55
+
56
+ /*-*************************************
57
+ * Explicit memory management
58
+ ***************************************/
59
+ /** Decompression context */
60
+ typedef struct ZSTDv07_DCtx_s ZSTDv07_DCtx;
61
+ ZSTDLIBv07_API ZSTDv07_DCtx* ZSTDv07_createDCtx(void);
62
+ ZSTDLIBv07_API size_t ZSTDv07_freeDCtx(ZSTDv07_DCtx* dctx); /*!< @return : errorCode */
63
+
64
+ /** ZSTDv07_decompressDCtx() :
65
+ * Same as ZSTDv07_decompress(), requires an allocated ZSTDv07_DCtx (see ZSTDv07_createDCtx()) */
66
+ ZSTDLIBv07_API size_t ZSTDv07_decompressDCtx(ZSTDv07_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
67
+
68
+
69
+ /*-************************
70
+ * Simple dictionary API
71
+ ***************************/
72
+ /*! ZSTDv07_decompress_usingDict() :
73
+ * Decompression using a pre-defined Dictionary content (see dictBuilder).
74
+ * Dictionary must be identical to the one used during compression.
75
+ * Note : This function load the dictionary, resulting in a significant startup time */
76
+ ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDict(ZSTDv07_DCtx* dctx,
77
+ void* dst, size_t dstCapacity,
78
+ const void* src, size_t srcSize,
79
+ const void* dict,size_t dictSize);
80
+
81
+
82
+ /*-**************************
83
+ * Advanced Dictionary API
84
+ ****************************/
85
+ /*! ZSTDv07_createDDict() :
86
+ * Create a digested dictionary, ready to start decompression operation without startup delay.
87
+ * `dict` can be released after creation */
88
+ typedef struct ZSTDv07_DDict_s ZSTDv07_DDict;
89
+ ZSTDLIBv07_API ZSTDv07_DDict* ZSTDv07_createDDict(const void* dict, size_t dictSize);
90
+ ZSTDLIBv07_API size_t ZSTDv07_freeDDict(ZSTDv07_DDict* ddict);
91
+
92
+ /*! ZSTDv07_decompress_usingDDict() :
93
+ * Decompression using a pre-digested Dictionary
94
+ * Faster startup than ZSTDv07_decompress_usingDict(), recommended when same dictionary is used multiple times. */
95
+ ZSTDLIBv07_API size_t ZSTDv07_decompress_usingDDict(ZSTDv07_DCtx* dctx,
96
+ void* dst, size_t dstCapacity,
97
+ const void* src, size_t srcSize,
98
+ const ZSTDv07_DDict* ddict);
99
+
100
+ typedef struct {
101
+ unsigned long long frameContentSize;
102
+ unsigned windowSize;
103
+ unsigned dictID;
104
+ unsigned checksumFlag;
105
+ } ZSTDv07_frameParams;
106
+
107
+ ZSTDLIBv07_API size_t ZSTDv07_getFrameParams(ZSTDv07_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input */
108
+
109
+
110
+
111
+
112
+ /* *************************************
113
+ * Streaming functions
114
+ ***************************************/
115
+ typedef struct ZBUFFv07_DCtx_s ZBUFFv07_DCtx;
116
+ ZSTDLIBv07_API ZBUFFv07_DCtx* ZBUFFv07_createDCtx(void);
117
+ ZSTDLIBv07_API size_t ZBUFFv07_freeDCtx(ZBUFFv07_DCtx* dctx);
118
+
119
+ ZSTDLIBv07_API size_t ZBUFFv07_decompressInit(ZBUFFv07_DCtx* dctx);
120
+ ZSTDLIBv07_API size_t ZBUFFv07_decompressInitDictionary(ZBUFFv07_DCtx* dctx, const void* dict, size_t dictSize);
121
+
122
+ ZSTDLIBv07_API size_t ZBUFFv07_decompressContinue(ZBUFFv07_DCtx* dctx,
123
+ void* dst, size_t* dstCapacityPtr,
124
+ const void* src, size_t* srcSizePtr);
125
+
126
+ /*-***************************************************************************
127
+ * Streaming decompression howto
128
+ *
129
+ * A ZBUFFv07_DCtx object is required to track streaming operations.
130
+ * Use ZBUFFv07_createDCtx() and ZBUFFv07_freeDCtx() to create/release resources.
131
+ * Use ZBUFFv07_decompressInit() to start a new decompression operation,
132
+ * or ZBUFFv07_decompressInitDictionary() if decompression requires a dictionary.
133
+ * Note that ZBUFFv07_DCtx objects can be re-init multiple times.
134
+ *
135
+ * Use ZBUFFv07_decompressContinue() repetitively to consume your input.
136
+ * *srcSizePtr and *dstCapacityPtr can be any size.
137
+ * The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
138
+ * Note that it may not consume the entire input, in which case it's up to the caller to present remaining input again.
139
+ * The content of `dst` will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters, or change `dst`.
140
+ * @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to help latency),
141
+ * or 0 when a frame is completely decoded,
142
+ * or an error code, which can be tested using ZBUFFv07_isError().
143
+ *
144
+ * Hint : recommended buffer sizes (not compulsory) : ZBUFFv07_recommendedDInSize() and ZBUFFv07_recommendedDOutSize()
145
+ * output : ZBUFFv07_recommendedDOutSize== 128 KB block size is the internal unit, it ensures it's always possible to write a full block when decoded.
146
+ * input : ZBUFFv07_recommendedDInSize == 128KB + 3;
147
+ * just follow indications from ZBUFFv07_decompressContinue() to minimize latency. It should always be <= 128 KB + 3 .
148
+ * *******************************************************************************/
149
+
150
+
151
+ /* *************************************
152
+ * Tool functions
153
+ ***************************************/
154
+ ZSTDLIBv07_API unsigned ZBUFFv07_isError(size_t errorCode);
155
+ ZSTDLIBv07_API const char* ZBUFFv07_getErrorName(size_t errorCode);
156
+
157
+ /** Functions below provide recommended buffer sizes for Compression or Decompression operations.
158
+ * These sizes are just hints, they tend to offer better latency */
159
+ ZSTDLIBv07_API size_t ZBUFFv07_recommendedDInSize(void);
160
+ ZSTDLIBv07_API size_t ZBUFFv07_recommendedDOutSize(void);
161
+
162
+
163
+ /*-*************************************
164
+ * Constants
165
+ ***************************************/
166
+ #define ZSTDv07_MAGICNUMBER 0xFD2FB527 /* v0.7 */
167
+
168
+
169
+ #if defined (__cplusplus)
170
+ }
171
+ #endif
172
+
173
+ #endif /* ZSTDv07_H_235446 */
@@ -0,0 +1,14 @@
1
+ # ZSTD - standard compression algorithm
2
+ # Copyright (C) 2014-2016, Yann Collet, Facebook
3
+ # BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
4
+
5
+ prefix=@PREFIX@
6
+ libdir=@LIBDIR@
7
+ includedir=@INCLUDEDIR@
8
+
9
+ Name: zstd
10
+ Description: fast lossless compression algorithm library
11
+ URL: http://www.zstd.net/
12
+ Version: @VERSION@
13
+ Libs: -L${libdir} -lzstd
14
+ Cflags: -I${includedir}
@@ -0,0 +1,673 @@
1
+ /*
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
+ */
9
+
10
+ #if defined (__cplusplus)
11
+ extern "C" {
12
+ #endif
13
+
14
+ #ifndef ZSTD_H_235446
15
+ #define ZSTD_H_235446
16
+
17
+ /* ====== Dependency ======*/
18
+ #include <stddef.h> /* size_t */
19
+
20
+
21
+ /* ===== ZSTDLIB_API : control library symbols visibility ===== */
22
+ #if defined(__GNUC__) && (__GNUC__ >= 4)
23
+ # define ZSTDLIB_API __attribute__ ((visibility ("default")))
24
+ #elif defined(ZSTD_DLL_EXPORT) && (ZSTD_DLL_EXPORT==1)
25
+ # define ZSTDLIB_API __declspec(dllexport)
26
+ #elif defined(ZSTD_DLL_IMPORT) && (ZSTD_DLL_IMPORT==1)
27
+ # define ZSTDLIB_API __declspec(dllimport) /* It isn't required but allows to generate better code, saving a function pointer load from the IAT and an indirect jump.*/
28
+ #else
29
+ # define ZSTDLIB_API
30
+ #endif
31
+
32
+
33
+ /*******************************************************************************************************
34
+ Introduction
35
+
36
+ zstd, short for Zstandard, is a fast lossless compression algorithm, targeting real-time compression scenarios
37
+ at zlib-level and better compression ratios. The zstd compression library provides in-memory compression and
38
+ decompression functions. The library supports compression levels from 1 up to ZSTD_maxCLevel() which is 22.
39
+ Levels >= 20, labelled `--ultra`, should be used with caution, as they require more memory.
40
+ Compression can be done in:
41
+ - a single step (described as Simple API)
42
+ - a single step, reusing a context (described as Explicit memory management)
43
+ - unbounded multiple steps (described as Streaming compression)
44
+ The compression ratio achievable on small data can be highly improved using compression with a dictionary in:
45
+ - a single step (described as Simple dictionary API)
46
+ - a single step, reusing a dictionary (described as Fast dictionary API)
47
+
48
+ Advanced experimental functions can be accessed using #define ZSTD_STATIC_LINKING_ONLY before including zstd.h.
49
+ These APIs shall never be used with a dynamic library.
50
+ They are not "stable", their definition may change in the future. Only static linking is allowed.
51
+ *********************************************************************************************************/
52
+
53
+ /*------ Version ------*/
54
+ #define ZSTD_VERSION_MAJOR 1
55
+ #define ZSTD_VERSION_MINOR 1
56
+ #define ZSTD_VERSION_RELEASE 2
57
+
58
+ #define ZSTD_LIB_VERSION ZSTD_VERSION_MAJOR.ZSTD_VERSION_MINOR.ZSTD_VERSION_RELEASE
59
+ #define ZSTD_QUOTE(str) #str
60
+ #define ZSTD_EXPAND_AND_QUOTE(str) ZSTD_QUOTE(str)
61
+ #define ZSTD_VERSION_STRING ZSTD_EXPAND_AND_QUOTE(ZSTD_LIB_VERSION)
62
+
63
+ #define ZSTD_VERSION_NUMBER (ZSTD_VERSION_MAJOR *100*100 + ZSTD_VERSION_MINOR *100 + ZSTD_VERSION_RELEASE)
64
+ ZSTDLIB_API unsigned ZSTD_versionNumber(void); /**< library version number; to be used when checking dll version */
65
+
66
+
67
+ /***************************************
68
+ * Simple API
69
+ ***************************************/
70
+ /*! ZSTD_compress() :
71
+ Compresses `src` content as a single zstd compressed frame into already allocated `dst`.
72
+ Hint : compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`.
73
+ @return : compressed size written into `dst` (<= `dstCapacity),
74
+ or an error code if it fails (which can be tested using ZSTD_isError()). */
75
+ ZSTDLIB_API size_t ZSTD_compress( void* dst, size_t dstCapacity,
76
+ const void* src, size_t srcSize,
77
+ int compressionLevel);
78
+
79
+ /*! ZSTD_decompress() :
80
+ `compressedSize` : must be the _exact_ size of a single compressed frame.
81
+ `dstCapacity` is an upper bound of originalSize.
82
+ If user cannot imply a maximum upper bound, it's better to use streaming mode to decompress data.
83
+ @return : the number of bytes decompressed into `dst` (<= `dstCapacity`),
84
+ or an errorCode if it fails (which can be tested using ZSTD_isError()). */
85
+ ZSTDLIB_API size_t ZSTD_decompress( void* dst, size_t dstCapacity,
86
+ const void* src, size_t compressedSize);
87
+
88
+ /*! ZSTD_getDecompressedSize() :
89
+ * 'src' is the start of a zstd compressed frame.
90
+ * @return : content size to be decompressed, as a 64-bits value _if known_, 0 otherwise.
91
+ * note 1 : decompressed size is an optional field, that may not be present, especially in streaming mode.
92
+ * When `return==0`, data to decompress could be any size.
93
+ * In which case, it's necessary to use streaming mode to decompress data.
94
+ * Optionally, application can still use ZSTD_decompress() while relying on implied limits.
95
+ * (For example, data may be necessarily cut into blocks <= 16 KB).
96
+ * note 2 : decompressed size is always present when compression is done with ZSTD_compress()
97
+ * note 3 : decompressed size can be very large (64-bits value),
98
+ * potentially larger than what local system can handle as a single memory segment.
99
+ * In which case, it's necessary to use streaming mode to decompress data.
100
+ * note 4 : If source is untrusted, decompressed size could be wrong or intentionally modified.
101
+ * Always ensure result fits within application's authorized limits.
102
+ * Each application can set its own limits.
103
+ * note 5 : when `return==0`, if precise failure cause is needed, use ZSTD_getFrameParams() to know more. */
104
+ ZSTDLIB_API unsigned long long ZSTD_getDecompressedSize(const void* src, size_t srcSize);
105
+
106
+
107
+ /*====== Helper functions ======*/
108
+ ZSTDLIB_API int ZSTD_maxCLevel(void); /*!< maximum compression level available */
109
+ ZSTDLIB_API size_t ZSTD_compressBound(size_t srcSize); /*!< maximum compressed size in worst case scenario */
110
+ ZSTDLIB_API unsigned ZSTD_isError(size_t code); /*!< tells if a `size_t` function result is an error code */
111
+ ZSTDLIB_API const char* ZSTD_getErrorName(size_t code); /*!< provides readable string from an error code */
112
+
113
+
114
+ /***************************************
115
+ * Explicit memory management
116
+ ***************************************/
117
+ /*= Compression context
118
+ * When compressing many times,
119
+ * it is recommended to allocate a context just once, and re-use it for each successive compression operation.
120
+ * This will make workload friendlier for system's memory.
121
+ * Use one context per thread for parallel execution in multi-threaded environments. */
122
+ typedef struct ZSTD_CCtx_s ZSTD_CCtx;
123
+ ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx(void);
124
+ ZSTDLIB_API size_t ZSTD_freeCCtx(ZSTD_CCtx* cctx);
125
+
126
+ /*! ZSTD_compressCCtx() :
127
+ Same as ZSTD_compress(), requires an allocated ZSTD_CCtx (see ZSTD_createCCtx()). */
128
+ ZSTDLIB_API size_t ZSTD_compressCCtx(ZSTD_CCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize, int compressionLevel);
129
+
130
+ /*= Decompression context */
131
+ typedef struct ZSTD_DCtx_s ZSTD_DCtx;
132
+ ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx(void);
133
+ ZSTDLIB_API size_t ZSTD_freeDCtx(ZSTD_DCtx* dctx);
134
+
135
+ /*! ZSTD_decompressDCtx() :
136
+ * Same as ZSTD_decompress(), requires an allocated ZSTD_DCtx (see ZSTD_createDCtx()). */
137
+ ZSTDLIB_API size_t ZSTD_decompressDCtx(ZSTD_DCtx* ctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
138
+
139
+
140
+ /**************************
141
+ * Simple dictionary API
142
+ ***************************/
143
+ /*! ZSTD_compress_usingDict() :
144
+ * Compression using a predefined Dictionary (see dictBuilder/zdict.h).
145
+ * Note : This function loads the dictionary, resulting in significant startup delay.
146
+ * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
147
+ ZSTDLIB_API size_t ZSTD_compress_usingDict(ZSTD_CCtx* ctx,
148
+ void* dst, size_t dstCapacity,
149
+ const void* src, size_t srcSize,
150
+ const void* dict,size_t dictSize,
151
+ int compressionLevel);
152
+
153
+ /*! ZSTD_decompress_usingDict() :
154
+ * Decompression using a predefined Dictionary (see dictBuilder/zdict.h).
155
+ * Dictionary must be identical to the one used during compression.
156
+ * Note : This function loads the dictionary, resulting in significant startup delay.
157
+ * Note : When `dict == NULL || dictSize < 8` no dictionary is used. */
158
+ ZSTDLIB_API size_t ZSTD_decompress_usingDict(ZSTD_DCtx* dctx,
159
+ void* dst, size_t dstCapacity,
160
+ const void* src, size_t srcSize,
161
+ const void* dict,size_t dictSize);
162
+
163
+
164
+ /****************************
165
+ * Fast dictionary API
166
+ ****************************/
167
+ typedef struct ZSTD_CDict_s ZSTD_CDict;
168
+
169
+ /*! ZSTD_createCDict() :
170
+ * When compressing multiple messages / blocks with the same dictionary, it's recommended to load it just once.
171
+ * ZSTD_createCDict() will create a digested dictionary, ready to start future compression operations without startup delay.
172
+ * ZSTD_CDict can be created once and used by multiple threads concurrently, as its usage is read-only.
173
+ * `dict` can be released after ZSTD_CDict creation. */
174
+ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel);
175
+
176
+ /*! ZSTD_freeCDict() :
177
+ * Function frees memory allocated by ZSTD_createCDict(). */
178
+ ZSTDLIB_API size_t ZSTD_freeCDict(ZSTD_CDict* CDict);
179
+
180
+ /*! ZSTD_compress_usingCDict() :
181
+ * Compression using a digested Dictionary.
182
+ * Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
183
+ * Note that compression level is decided during dictionary creation. */
184
+ ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
185
+ void* dst, size_t dstCapacity,
186
+ const void* src, size_t srcSize,
187
+ const ZSTD_CDict* cdict);
188
+
189
+
190
+ typedef struct ZSTD_DDict_s ZSTD_DDict;
191
+
192
+ /*! ZSTD_createDDict() :
193
+ * Create a digested dictionary, ready to start decompression operation without startup delay.
194
+ * `dict` can be released after creation. */
195
+ ZSTDLIB_API ZSTD_DDict* ZSTD_createDDict(const void* dict, size_t dictSize);
196
+
197
+ /*! ZSTD_freeDDict() :
198
+ * Function frees memory allocated with ZSTD_createDDict() */
199
+ ZSTDLIB_API size_t ZSTD_freeDDict(ZSTD_DDict* ddict);
200
+
201
+ /*! ZSTD_decompress_usingDDict() :
202
+ * Decompression using a digested Dictionary.
203
+ * Faster startup than ZSTD_decompress_usingDict(), recommended when same dictionary is used multiple times. */
204
+ ZSTDLIB_API size_t ZSTD_decompress_usingDDict(ZSTD_DCtx* dctx,
205
+ void* dst, size_t dstCapacity,
206
+ const void* src, size_t srcSize,
207
+ const ZSTD_DDict* ddict);
208
+
209
+
210
+ /****************************
211
+ * Streaming
212
+ ****************************/
213
+
214
+ typedef struct ZSTD_inBuffer_s {
215
+ const void* src; /**< start of input buffer */
216
+ size_t size; /**< size of input buffer */
217
+ size_t pos; /**< position where reading stopped. Will be updated. Necessarily 0 <= pos <= size */
218
+ } ZSTD_inBuffer;
219
+
220
+ typedef struct ZSTD_outBuffer_s {
221
+ void* dst; /**< start of output buffer */
222
+ size_t size; /**< size of output buffer */
223
+ size_t pos; /**< position where writing stopped. Will be updated. Necessarily 0 <= pos <= size */
224
+ } ZSTD_outBuffer;
225
+
226
+
227
+
228
+ /*-***********************************************************************
229
+ * Streaming compression - HowTo
230
+ *
231
+ * A ZSTD_CStream object is required to track streaming operation.
232
+ * Use ZSTD_createCStream() and ZSTD_freeCStream() to create/release resources.
233
+ * ZSTD_CStream objects can be reused multiple times on consecutive compression operations.
234
+ * It is recommended to re-use ZSTD_CStream in situations where many streaming operations will be achieved consecutively,
235
+ * since it will play nicer with system's memory, by re-using already allocated memory.
236
+ * Use one separate ZSTD_CStream per thread for parallel execution.
237
+ *
238
+ * Start a new compression by initializing ZSTD_CStream.
239
+ * Use ZSTD_initCStream() to start a new compression operation.
240
+ * Use ZSTD_initCStream_usingDict() or ZSTD_initCStream_usingCDict() for a compression which requires a dictionary (experimental section)
241
+ *
242
+ * Use ZSTD_compressStream() repetitively to consume input stream.
243
+ * The function will automatically update both `pos` fields.
244
+ * Note that it may not consume the entire input, in which case `pos < size`,
245
+ * and it's up to the caller to present again remaining data.
246
+ * @return : a size hint, preferred nb of bytes to use as input for next function call
247
+ * or an error code, which can be tested using ZSTD_isError().
248
+ * Note 1 : it's just a hint, to help latency a little, any other value will work fine.
249
+ * Note 2 : size hint is guaranteed to be <= ZSTD_CStreamInSize()
250
+ *
251
+ * At any moment, it's possible to flush whatever data remains within internal buffer, using ZSTD_flushStream().
252
+ * `output->pos` will be updated.
253
+ * Note that some content might still be left within internal buffer if `output->size` is too small.
254
+ * @return : nb of bytes still present within internal buffer (0 if it's empty)
255
+ * or an error code, which can be tested using ZSTD_isError().
256
+ *
257
+ * ZSTD_endStream() instructs to finish a frame.
258
+ * It will perform a flush and write frame epilogue.
259
+ * The epilogue is required for decoders to consider a frame completed.
260
+ * Similar to ZSTD_flushStream(), it may not be able to flush the full content if `output->size` is too small.
261
+ * In which case, call again ZSTD_endStream() to complete the flush.
262
+ * @return : nb of bytes still present within internal buffer (0 if it's empty, hence compression completed)
263
+ * or an error code, which can be tested using ZSTD_isError().
264
+ *
265
+ * *******************************************************************/
266
+
267
+ typedef struct ZSTD_CStream_s ZSTD_CStream;
268
+ ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream(void);
269
+ ZSTDLIB_API size_t ZSTD_freeCStream(ZSTD_CStream* zcs);
270
+
271
+ ZSTDLIB_API size_t ZSTD_initCStream(ZSTD_CStream* zcs, int compressionLevel);
272
+ ZSTDLIB_API size_t ZSTD_compressStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
273
+ ZSTDLIB_API size_t ZSTD_flushStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
274
+ ZSTDLIB_API size_t ZSTD_endStream(ZSTD_CStream* zcs, ZSTD_outBuffer* output);
275
+
276
+ ZSTDLIB_API size_t ZSTD_CStreamInSize(void); /**< recommended size for input buffer */
277
+ 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. */
278
+
279
+
280
+
281
+ /*-***************************************************************************
282
+ * Streaming decompression - HowTo
283
+ *
284
+ * A ZSTD_DStream object is required to track streaming operations.
285
+ * Use ZSTD_createDStream() and ZSTD_freeDStream() to create/release resources.
286
+ * ZSTD_DStream objects can be re-used multiple times.
287
+ *
288
+ * Use ZSTD_initDStream() to start a new decompression operation,
289
+ * or ZSTD_initDStream_usingDict() if decompression requires a dictionary.
290
+ * @return : recommended first input size
291
+ *
292
+ * Use ZSTD_decompressStream() repetitively to consume your input.
293
+ * The function will update both `pos` fields.
294
+ * If `input.pos < input.size`, some input has not been consumed.
295
+ * It's up to the caller to present again remaining data.
296
+ * If `output.pos < output.size`, decoder has flushed everything it could.
297
+ * @return : 0 when a frame is completely decoded and fully flushed,
298
+ * an error code, which can be tested using ZSTD_isError(),
299
+ * any other value > 0, which means there is still some decoding to do to complete current frame.
300
+ * The return value is a suggested next input size (a hint to improve latency) that will never load more than the current frame.
301
+ * *******************************************************************************/
302
+
303
+ typedef struct ZSTD_DStream_s ZSTD_DStream;
304
+ ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream(void);
305
+ ZSTDLIB_API size_t ZSTD_freeDStream(ZSTD_DStream* zds);
306
+
307
+ ZSTDLIB_API size_t ZSTD_initDStream(ZSTD_DStream* zds);
308
+ ZSTDLIB_API size_t ZSTD_decompressStream(ZSTD_DStream* zds, ZSTD_outBuffer* output, ZSTD_inBuffer* input);
309
+
310
+ ZSTDLIB_API size_t ZSTD_DStreamInSize(void); /*!< recommended size for input buffer */
311
+ ZSTDLIB_API size_t ZSTD_DStreamOutSize(void); /*!< recommended size for output buffer. Guarantee to successfully flush at least one complete block in all circumstances. */
312
+
313
+ #endif /* ZSTD_H_235446 */
314
+
315
+
316
+ #if defined(ZSTD_STATIC_LINKING_ONLY) && !defined(ZSTD_H_ZSTD_STATIC_LINKING_ONLY)
317
+ #define ZSTD_H_ZSTD_STATIC_LINKING_ONLY
318
+
319
+ /****************************************************************************************
320
+ * START OF ADVANCED AND EXPERIMENTAL FUNCTIONS
321
+ * The definitions in this section are considered experimental.
322
+ * They should never be used with a dynamic library, as they may change in the future.
323
+ * They are provided for advanced usages.
324
+ * Use them only in association with static linking.
325
+ * ***************************************************************************************/
326
+
327
+ /* --- Constants ---*/
328
+ #define ZSTD_MAGICNUMBER 0xFD2FB528 /* v0.8 */
329
+ #define ZSTD_MAGIC_SKIPPABLE_START 0x184D2A50U
330
+
331
+ #define ZSTD_WINDOWLOG_MAX_32 25
332
+ #define ZSTD_WINDOWLOG_MAX_64 27
333
+ #define ZSTD_WINDOWLOG_MAX ((U32)(MEM_32bits() ? ZSTD_WINDOWLOG_MAX_32 : ZSTD_WINDOWLOG_MAX_64))
334
+ #define ZSTD_WINDOWLOG_MIN 10
335
+ #define ZSTD_HASHLOG_MAX ZSTD_WINDOWLOG_MAX
336
+ #define ZSTD_HASHLOG_MIN 6
337
+ #define ZSTD_CHAINLOG_MAX (ZSTD_WINDOWLOG_MAX+1)
338
+ #define ZSTD_CHAINLOG_MIN ZSTD_HASHLOG_MIN
339
+ #define ZSTD_HASHLOG3_MAX 17
340
+ #define ZSTD_SEARCHLOG_MAX (ZSTD_WINDOWLOG_MAX-1)
341
+ #define ZSTD_SEARCHLOG_MIN 1
342
+ #define ZSTD_SEARCHLENGTH_MAX 7 /* only for ZSTD_fast, other strategies are limited to 6 */
343
+ #define ZSTD_SEARCHLENGTH_MIN 3 /* only for ZSTD_btopt, other strategies are limited to 4 */
344
+ #define ZSTD_TARGETLENGTH_MIN 4
345
+ #define ZSTD_TARGETLENGTH_MAX 999
346
+
347
+ #define ZSTD_FRAMEHEADERSIZE_MAX 18 /* for static allocation */
348
+ static const size_t ZSTD_frameHeaderSize_prefix = 5;
349
+ static const size_t ZSTD_frameHeaderSize_min = 6;
350
+ static const size_t ZSTD_frameHeaderSize_max = ZSTD_FRAMEHEADERSIZE_MAX;
351
+ static const size_t ZSTD_skippableHeaderSize = 8; /* magic number + skippable frame length */
352
+
353
+
354
+ /*--- Advanced types ---*/
355
+ typedef enum { ZSTD_fast, ZSTD_dfast, ZSTD_greedy, ZSTD_lazy, ZSTD_lazy2, ZSTD_btlazy2, ZSTD_btopt, ZSTD_btopt2 } ZSTD_strategy; /* from faster to stronger */
356
+
357
+ typedef struct {
358
+ unsigned windowLog; /**< largest match distance : larger == more compression, more memory needed during decompression */
359
+ unsigned chainLog; /**< fully searched segment : larger == more compression, slower, more memory (useless for fast) */
360
+ unsigned hashLog; /**< dispatch table : larger == faster, more memory */
361
+ unsigned searchLog; /**< nb of searches : larger == more compression, slower */
362
+ unsigned searchLength; /**< match length searched : larger == faster decompression, sometimes less compression */
363
+ unsigned targetLength; /**< acceptable match size for optimal parser (only) : larger == more compression, slower */
364
+ ZSTD_strategy strategy;
365
+ } ZSTD_compressionParameters;
366
+
367
+ typedef struct {
368
+ unsigned contentSizeFlag; /**< 1: content size will be in frame header (if known). */
369
+ unsigned checksumFlag; /**< 1: will generate a 22-bits checksum at end of frame, to be used for error detection by decompressor */
370
+ unsigned noDictIDFlag; /**< 1: no dict ID will be saved into frame header (if dictionary compression) */
371
+ } ZSTD_frameParameters;
372
+
373
+ typedef struct {
374
+ ZSTD_compressionParameters cParams;
375
+ ZSTD_frameParameters fParams;
376
+ } ZSTD_parameters;
377
+
378
+ /*= Custom memory allocation functions */
379
+ typedef void* (*ZSTD_allocFunction) (void* opaque, size_t size);
380
+ typedef void (*ZSTD_freeFunction) (void* opaque, void* address);
381
+ typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; void* opaque; } ZSTD_customMem;
382
+
383
+
384
+ /***************************************
385
+ * Advanced compression functions
386
+ ***************************************/
387
+ /*! ZSTD_estimateCCtxSize() :
388
+ * Gives the amount of memory allocated for a ZSTD_CCtx given a set of compression parameters.
389
+ * `frameContentSize` is an optional parameter, provide `0` if unknown */
390
+ ZSTDLIB_API size_t ZSTD_estimateCCtxSize(ZSTD_compressionParameters cParams);
391
+
392
+ /*! ZSTD_createCCtx_advanced() :
393
+ * Create a ZSTD compression context using external alloc and free functions */
394
+ ZSTDLIB_API ZSTD_CCtx* ZSTD_createCCtx_advanced(ZSTD_customMem customMem);
395
+
396
+ /*! ZSTD_sizeofCCtx() :
397
+ * Gives the amount of memory used by a given ZSTD_CCtx */
398
+ ZSTDLIB_API size_t ZSTD_sizeof_CCtx(const ZSTD_CCtx* cctx);
399
+
400
+ /*! ZSTD_createCDict_advanced() :
401
+ * Create a ZSTD_CDict using external alloc and free, and customized compression parameters */
402
+ ZSTDLIB_API ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize,
403
+ ZSTD_parameters params, ZSTD_customMem customMem);
404
+
405
+ /*! ZSTD_sizeof_CDict() :
406
+ * Gives the amount of memory used by a given ZSTD_sizeof_CDict */
407
+ ZSTDLIB_API size_t ZSTD_sizeof_CDict(const ZSTD_CDict* cdict);
408
+
409
+ /*! ZSTD_getCParams() :
410
+ * @return ZSTD_compressionParameters structure for a selected compression level and estimated srcSize.
411
+ * `estimatedSrcSize` value is optional, select 0 if not known */
412
+ ZSTDLIB_API ZSTD_compressionParameters ZSTD_getCParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
413
+
414
+ /*! ZSTD_getParams() :
415
+ * same as ZSTD_getCParams(), but @return a full `ZSTD_parameters` object instead of sub-component `ZSTD_compressionParameters`.
416
+ * All fields of `ZSTD_frameParameters` are set to default (0) */
417
+ ZSTDLIB_API ZSTD_parameters ZSTD_getParams(int compressionLevel, unsigned long long estimatedSrcSize, size_t dictSize);
418
+
419
+ /*! ZSTD_checkCParams() :
420
+ * Ensure param values remain within authorized range */
421
+ ZSTDLIB_API size_t ZSTD_checkCParams(ZSTD_compressionParameters params);
422
+
423
+ /*! ZSTD_adjustCParams() :
424
+ * optimize params for a given `srcSize` and `dictSize`.
425
+ * both values are optional, select `0` if unknown. */
426
+ ZSTDLIB_API ZSTD_compressionParameters ZSTD_adjustCParams(ZSTD_compressionParameters cPar, unsigned long long srcSize, size_t dictSize);
427
+
428
+ /*! ZSTD_compress_advanced() :
429
+ * Same as ZSTD_compress_usingDict(), with fine-tune control of each compression parameter */
430
+ ZSTDLIB_API size_t ZSTD_compress_advanced (ZSTD_CCtx* ctx,
431
+ void* dst, size_t dstCapacity,
432
+ const void* src, size_t srcSize,
433
+ const void* dict,size_t dictSize,
434
+ ZSTD_parameters params);
435
+
436
+
437
+ /*--- Advanced decompression functions ---*/
438
+
439
+ /*! ZSTD_isFrame() :
440
+ * Tells if the content of `buffer` starts with a valid Frame Identifier.
441
+ * Note : Frame Identifier is 4 bytes. If `size < 4`, @return will always be 0.
442
+ * Note 2 : Legacy Frame Identifiers are considered valid only if Legacy Support is enabled.
443
+ * Note 3 : Skippable Frame Identifiers are considered valid. */
444
+ ZSTDLIB_API unsigned ZSTD_isFrame(const void* buffer, size_t size);
445
+
446
+ /*! ZSTD_estimateDCtxSize() :
447
+ * Gives the potential amount of memory allocated to create a ZSTD_DCtx */
448
+ ZSTDLIB_API size_t ZSTD_estimateDCtxSize(void);
449
+
450
+ /*! ZSTD_createDCtx_advanced() :
451
+ * Create a ZSTD decompression context using external alloc and free functions */
452
+ ZSTDLIB_API ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem);
453
+
454
+ /*! ZSTD_sizeof_DCtx() :
455
+ * Gives the amount of memory used by a given ZSTD_DCtx */
456
+ ZSTDLIB_API size_t ZSTD_sizeof_DCtx(const ZSTD_DCtx* dctx);
457
+
458
+ /*! ZSTD_sizeof_DDict() :
459
+ * Gives the amount of memory used by a given ZSTD_DDict */
460
+ ZSTDLIB_API size_t ZSTD_sizeof_DDict(const ZSTD_DDict* ddict);
461
+
462
+ /*! ZSTD_getDictID_fromDict() :
463
+ * Provides the dictID stored within dictionary.
464
+ * if @return == 0, the dictionary is not conformant with Zstandard specification.
465
+ * It can still be loaded, but as a content-only dictionary. */
466
+ unsigned ZSTD_getDictID_fromDict(const void* dict, size_t dictSize);
467
+
468
+ /*! ZSTD_getDictID_fromDDict() :
469
+ * Provides the dictID of the dictionary loaded into `ddict`.
470
+ * If @return == 0, the dictionary is not conformant to Zstandard specification, or empty.
471
+ * Non-conformant dictionaries can still be loaded, but as content-only dictionaries. */
472
+ unsigned ZSTD_getDictID_fromDDict(const ZSTD_DDict* ddict);
473
+
474
+ /*! ZSTD_getDictID_fromFrame() :
475
+ * Provides the dictID required to decompressed the frame stored within `src`.
476
+ * If @return == 0, the dictID could not be decoded.
477
+ * This could for one of the following reasons :
478
+ * - The frame does not require a dictionary to be decoded (most common case).
479
+ * - The frame was built with dictID intentionally removed. Whatever dictionary is necessary is a hidden information.
480
+ * Note : this use case also happens when using a non-conformant dictionary.
481
+ * - `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
482
+ * - This is not a Zstandard frame.
483
+ * When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code. */
484
+ unsigned ZSTD_getDictID_fromFrame(const void* src, size_t srcSize);
485
+
486
+
487
+ /********************************************************************
488
+ * Advanced streaming functions
489
+ ********************************************************************/
490
+
491
+ /*===== Advanced Streaming compression functions =====*/
492
+ ZSTDLIB_API ZSTD_CStream* ZSTD_createCStream_advanced(ZSTD_customMem customMem);
493
+ ZSTDLIB_API size_t ZSTD_initCStream_srcSize(ZSTD_CStream* zcs, int compressionLevel, unsigned long long pledgedSrcSize); /**< pledgedSrcSize must be correct */
494
+ ZSTDLIB_API size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel);
495
+ ZSTDLIB_API size_t ZSTD_initCStream_advanced(ZSTD_CStream* zcs, const void* dict, size_t dictSize,
496
+ ZSTD_parameters params, unsigned long long pledgedSrcSize); /**< pledgedSrcSize is optional and can be zero == unknown */
497
+ 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 */
498
+ 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 */
499
+ ZSTDLIB_API size_t ZSTD_sizeof_CStream(const ZSTD_CStream* zcs);
500
+
501
+
502
+ /*===== Advanced Streaming decompression functions =====*/
503
+ typedef enum { ZSTDdsp_maxWindowSize } ZSTD_DStreamParameter_e;
504
+ ZSTDLIB_API ZSTD_DStream* ZSTD_createDStream_advanced(ZSTD_customMem customMem);
505
+ ZSTDLIB_API size_t ZSTD_initDStream_usingDict(ZSTD_DStream* zds, const void* dict, size_t dictSize);
506
+ ZSTDLIB_API size_t ZSTD_setDStreamParameter(ZSTD_DStream* zds, ZSTD_DStreamParameter_e paramType, unsigned paramValue);
507
+ 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 */
508
+ ZSTDLIB_API size_t ZSTD_resetDStream(ZSTD_DStream* zds); /**< re-use decompression parameters from previous init; saves dictionary loading */
509
+ ZSTDLIB_API size_t ZSTD_sizeof_DStream(const ZSTD_DStream* zds);
510
+
511
+
512
+ /*********************************************************************
513
+ * Buffer-less and synchronous inner streaming functions
514
+ *
515
+ * This is an advanced API, giving full control over buffer management, for users which need direct control over memory.
516
+ * But it's also a complex one, with many restrictions (documented below).
517
+ * Prefer using normal streaming API for an easier experience
518
+ ********************************************************************* */
519
+
520
+ /**
521
+ Buffer-less streaming compression (synchronous mode)
522
+
523
+ A ZSTD_CCtx object is required to track streaming operations.
524
+ Use ZSTD_createCCtx() / ZSTD_freeCCtx() to manage resource.
525
+ ZSTD_CCtx object can be re-used multiple times within successive compression operations.
526
+
527
+ Start by initializing a context.
528
+ Use ZSTD_compressBegin(), or ZSTD_compressBegin_usingDict() for dictionary compression,
529
+ or ZSTD_compressBegin_advanced(), for finer parameter control.
530
+ It's also possible to duplicate a reference context which has already been initialized, using ZSTD_copyCCtx()
531
+
532
+ Then, consume your input using ZSTD_compressContinue().
533
+ There are some important considerations to keep in mind when using this advanced function :
534
+ - ZSTD_compressContinue() has no internal buffer. It uses externally provided buffer only.
535
+ - Interface is synchronous : input is consumed entirely and produce 1+ (or more) compressed blocks.
536
+ - Caller must ensure there is enough space in `dst` to store compressed data under worst case scenario.
537
+ Worst case evaluation is provided by ZSTD_compressBound().
538
+ ZSTD_compressContinue() doesn't guarantee recover after a failed compression.
539
+ - ZSTD_compressContinue() presumes prior input ***is still accessible and unmodified*** (up to maximum distance size, see WindowLog).
540
+ It remembers all previous contiguous blocks, plus one separated memory segment (which can itself consists of multiple contiguous blocks)
541
+ - ZSTD_compressContinue() detects that prior input has been overwritten when `src` buffer overlaps.
542
+ In which case, it will "discard" the relevant memory section from its history.
543
+
544
+ Finish a frame with ZSTD_compressEnd(), which will write the last block(s) and optional checksum.
545
+ It's possible to use a NULL,0 src content, in which case, it will write a final empty block to end the frame,
546
+ Without last block mark, frames will be considered unfinished (broken) by decoders.
547
+
548
+ You can then reuse `ZSTD_CCtx` (ZSTD_compressBegin()) to compress some new frame.
549
+ */
550
+
551
+ /*===== Buffer-less streaming compression functions =====*/
552
+ ZSTDLIB_API size_t ZSTD_compressBegin(ZSTD_CCtx* cctx, int compressionLevel);
553
+ ZSTDLIB_API size_t ZSTD_compressBegin_usingDict(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, int compressionLevel);
554
+ ZSTDLIB_API size_t ZSTD_compressBegin_advanced(ZSTD_CCtx* cctx, const void* dict, size_t dictSize, ZSTD_parameters params, unsigned long long pledgedSrcSize);
555
+ ZSTDLIB_API size_t ZSTD_copyCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx, unsigned long long pledgedSrcSize);
556
+ ZSTDLIB_API size_t ZSTD_compressContinue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
557
+ ZSTDLIB_API size_t ZSTD_compressEnd(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
558
+
559
+
560
+
561
+ /*-
562
+ Buffer-less streaming decompression (synchronous mode)
563
+
564
+ A ZSTD_DCtx object is required to track streaming operations.
565
+ Use ZSTD_createDCtx() / ZSTD_freeDCtx() to manage it.
566
+ A ZSTD_DCtx object can be re-used multiple times.
567
+
568
+ First typical operation is to retrieve frame parameters, using ZSTD_getFrameParams().
569
+ It fills a ZSTD_frameParams structure which provide important information to correctly decode the frame,
570
+ such as the minimum rolling buffer size to allocate to decompress data (`windowSize`),
571
+ and the dictionary ID used.
572
+ (Note : content size is optional, it may not be present. 0 means : content size unknown).
573
+ Note that these values could be wrong, either because of data malformation, or because an attacker is spoofing deliberate false information.
574
+ As a consequence, check that values remain within valid application range, especially `windowSize`, before allocation.
575
+ Each application can set its own limit, depending on local restrictions. For extended interoperability, it is recommended to support at least 8 MB.
576
+ Frame parameters are extracted from the beginning of the compressed frame.
577
+ Data fragment must be large enough to ensure successful decoding, typically `ZSTD_frameHeaderSize_max` bytes.
578
+ @result : 0 : successful decoding, the `ZSTD_frameParams` structure is correctly filled.
579
+ >0 : `srcSize` is too small, please provide at least @result bytes on next attempt.
580
+ errorCode, which can be tested using ZSTD_isError().
581
+
582
+ Start decompression, with ZSTD_decompressBegin() or ZSTD_decompressBegin_usingDict().
583
+ Alternatively, you can copy a prepared context, using ZSTD_copyDCtx().
584
+
585
+ Then use ZSTD_nextSrcSizeToDecompress() and ZSTD_decompressContinue() alternatively.
586
+ ZSTD_nextSrcSizeToDecompress() tells how many bytes to provide as 'srcSize' to ZSTD_decompressContinue().
587
+ ZSTD_decompressContinue() requires this _exact_ amount of bytes, or it will fail.
588
+
589
+ @result of ZSTD_decompressContinue() is the number of bytes regenerated within 'dst' (necessarily <= dstCapacity).
590
+ It can be zero, which is not an error; it just means ZSTD_decompressContinue() has decoded some metadata item.
591
+ It can also be an error code, which can be tested with ZSTD_isError().
592
+
593
+ ZSTD_decompressContinue() needs previous data blocks during decompression, up to `windowSize`.
594
+ They should preferably be located contiguously, prior to current block.
595
+ Alternatively, a round buffer of sufficient size is also possible. Sufficient size is determined by frame parameters.
596
+ ZSTD_decompressContinue() is very sensitive to contiguity,
597
+ if 2 blocks don't follow each other, make sure that either the compressor breaks contiguity at the same place,
598
+ or that previous contiguous segment is large enough to properly handle maximum back-reference.
599
+
600
+ A frame is fully decoded when ZSTD_nextSrcSizeToDecompress() returns zero.
601
+ Context can then be reset to start a new decompression.
602
+
603
+ Note : it's possible to know if next input to present is a header or a block, using ZSTD_nextInputType().
604
+ This information is not required to properly decode a frame.
605
+
606
+ == Special case : skippable frames ==
607
+
608
+ Skippable frames allow integration of user-defined data into a flow of concatenated frames.
609
+ Skippable frames will be ignored (skipped) by a decompressor. The format of skippable frames is as follows :
610
+ a) Skippable frame ID - 4 Bytes, Little endian format, any value from 0x184D2A50 to 0x184D2A5F
611
+ b) Frame Size - 4 Bytes, Little endian format, unsigned 32-bits
612
+ c) Frame Content - any content (User Data) of length equal to Frame Size
613
+ For skippable frames ZSTD_decompressContinue() always returns 0.
614
+ For skippable frames ZSTD_getFrameParams() returns fparamsPtr->windowLog==0 what means that a frame is skippable.
615
+ It also returns Frame Size as fparamsPtr->frameContentSize.
616
+ */
617
+
618
+ typedef struct {
619
+ unsigned long long frameContentSize;
620
+ unsigned windowSize;
621
+ unsigned dictID;
622
+ unsigned checksumFlag;
623
+ } ZSTD_frameParams;
624
+
625
+ /*===== Buffer-less streaming decompression functions =====*/
626
+ ZSTDLIB_API size_t ZSTD_getFrameParams(ZSTD_frameParams* fparamsPtr, const void* src, size_t srcSize); /**< doesn't consume input, see details below */
627
+ ZSTDLIB_API size_t ZSTD_decompressBegin(ZSTD_DCtx* dctx);
628
+ ZSTDLIB_API size_t ZSTD_decompressBegin_usingDict(ZSTD_DCtx* dctx, const void* dict, size_t dictSize);
629
+ ZSTDLIB_API void ZSTD_copyDCtx(ZSTD_DCtx* dctx, const ZSTD_DCtx* preparedDCtx);
630
+ ZSTDLIB_API size_t ZSTD_nextSrcSizeToDecompress(ZSTD_DCtx* dctx);
631
+ ZSTDLIB_API size_t ZSTD_decompressContinue(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
632
+ typedef enum { ZSTDnit_frameHeader, ZSTDnit_blockHeader, ZSTDnit_block, ZSTDnit_lastBlock, ZSTDnit_checksum, ZSTDnit_skippableFrame } ZSTD_nextInputType_e;
633
+ ZSTDLIB_API ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx);
634
+
635
+ /**
636
+ Block functions
637
+
638
+ Block functions produce and decode raw zstd blocks, without frame metadata.
639
+ Frame metadata cost is typically ~18 bytes, which can be non-negligible for very small blocks (< 100 bytes).
640
+ User will have to take in charge required information to regenerate data, such as compressed and content sizes.
641
+
642
+ A few rules to respect :
643
+ - Compressing and decompressing require a context structure
644
+ + Use ZSTD_createCCtx() and ZSTD_createDCtx()
645
+ - It is necessary to init context before starting
646
+ + compression : ZSTD_compressBegin()
647
+ + decompression : ZSTD_decompressBegin()
648
+ + variants _usingDict() are also allowed
649
+ + copyCCtx() and copyDCtx() work too
650
+ - Block size is limited, it must be <= ZSTD_getBlockSizeMax()
651
+ + If you need to compress more, cut data into multiple blocks
652
+ + Consider using the regular ZSTD_compress() instead, as frame metadata costs become negligible when source size is large.
653
+ - When a block is considered not compressible enough, ZSTD_compressBlock() result will be zero.
654
+ In which case, nothing is produced into `dst`.
655
+ + User must test for such outcome and deal directly with uncompressed data
656
+ + ZSTD_decompressBlock() doesn't accept uncompressed data as input !!!
657
+ + In case of multiple successive blocks, decoder must be informed of uncompressed block existence to follow proper history.
658
+ Use ZSTD_insertBlock() in such a case.
659
+ */
660
+
661
+ #define ZSTD_BLOCKSIZE_ABSOLUTEMAX (128 * 1024) /* define, for static allocation */
662
+ /*===== Raw zstd block functions =====*/
663
+ ZSTDLIB_API size_t ZSTD_getBlockSizeMax(ZSTD_CCtx* cctx);
664
+ ZSTDLIB_API size_t ZSTD_compressBlock (ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
665
+ ZSTDLIB_API size_t ZSTD_decompressBlock(ZSTD_DCtx* dctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
666
+ ZSTDLIB_API size_t ZSTD_insertBlock(ZSTD_DCtx* dctx, const void* blockStart, size_t blockSize); /**< insert block into `dctx` history. Useful for uncompressed blocks */
667
+
668
+
669
+ #endif /* ZSTD_H_ZSTD_STATIC_LINKING_ONLY */
670
+
671
+ #if defined (__cplusplus)
672
+ }
673
+ #endif