xref: /freebsd/sys/contrib/zstd/zlibWrapper/zstd_zlibwrapper.h (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
1 /*
2  * Copyright (c) 2016-2021, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
3  * All rights reserved.
4  *
5  * This source code is licensed under both the BSD-style license (found in the
6  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
7  * in the COPYING file in the root directory of this source tree).
8  * You may select, at your option, one of the above-listed licenses.
9  */
10 
11 #ifndef ZSTD_ZLIBWRAPPER_H
12 #define ZSTD_ZLIBWRAPPER_H
13 
14 #if defined (__cplusplus)
15 extern "C" {
16 #endif
17 
18 
19 #define ZLIB_CONST
20 #define Z_PREFIX
21 #define ZLIB_INTERNAL   /* disables gz*64 functions but fixes zlib 1.2.4 with Z_PREFIX */
22 #include <zlib.h>
23 
24 #if !defined(z_const)
25     #define z_const
26 #endif
27 
28 
29 /* returns a string with version of zstd library */
30 const char * zstdVersion(void);
31 
32 
33 /*** COMPRESSION ***/
34 /* ZWRAP_useZSTDcompression() enables/disables zstd compression during runtime.
35    By default zstd compression is disabled. To enable zstd compression please use one of the methods:
36    - compilation with the additional option -DZWRAP_USE_ZSTD=1
37    - using '#define ZWRAP_USE_ZSTD 1' in source code before '#include "zstd_zlibwrapper.h"'
38    - calling ZWRAP_useZSTDcompression(1)
39    All above-mentioned methods will enable zstd compression for all threads.
40    Be aware that ZWRAP_useZSTDcompression() is not thread-safe and may lead to a race condition. */
41 void ZWRAP_useZSTDcompression(int turn_on);
42 
43 /* checks if zstd compression is turned on */
44 int ZWRAP_isUsingZSTDcompression(void);
45 
46 /* Changes a pledged source size for a given compression stream.
47    It will change ZSTD compression parameters what may improve compression speed and/or ratio.
48    The function should be called just after deflateInit() or deflateReset() and before deflate() or deflateSetDictionary().
49    It's only helpful when data is compressed in blocks.
50    There will be no change in case of deflateInit() or deflateReset() immediately followed by deflate(strm, Z_FINISH)
51    as this case is automatically detected.  */
52 int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize);
53 
54 /* Similar to deflateReset but preserves dictionary set using deflateSetDictionary.
55    It should improve compression speed because there will be less calls to deflateSetDictionary
56    When using zlib compression this method redirects to deflateReset. */
57 int ZWRAP_deflateReset_keepDict(z_streamp strm);
58 
59 
60 
61 /*** DECOMPRESSION ***/
62 typedef enum { ZWRAP_FORCE_ZLIB, ZWRAP_AUTO } ZWRAP_decompress_type;
63 
64 /* ZWRAP_setDecompressionType() enables/disables automatic recognition of zstd/zlib compressed data during runtime.
65    By default auto-detection of zstd and zlib streams in enabled (ZWRAP_AUTO).
66    Forcing zlib decompression with ZWRAP_setDecompressionType(ZWRAP_FORCE_ZLIB) slightly improves
67    decompression speed of zlib-encoded streams.
68    Be aware that ZWRAP_setDecompressionType() is not thread-safe and may lead to a race condition. */
69 void ZWRAP_setDecompressionType(ZWRAP_decompress_type type);
70 
71 /* checks zstd decompression type */
72 ZWRAP_decompress_type ZWRAP_getDecompressionType(void);
73 
74 /* Checks if zstd decompression is used for a given stream.
75    If will return 1 only when inflate() was called and zstd header was detected. */
76 int ZWRAP_isUsingZSTDdecompression(z_streamp strm);
77 
78 /* Similar to inflateReset but preserves dictionary set using inflateSetDictionary.
79    inflate() will return Z_NEED_DICT only for the first time what will improve decompression speed.
80    For zlib streams this method redirects to inflateReset. */
81 int ZWRAP_inflateReset_keepDict(z_streamp strm);
82 
83 
84 #if defined (__cplusplus)
85 }
86 #endif
87 
88 #endif /* ZSTD_ZLIBWRAPPER_H */
89