Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 1 | /* |
Nick Terrell | ac58c8d | 2020-03-26 15:19:05 -0700 | [diff] [blame] | 2 | * Copyright (c) 2016-2020, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 3 | * All rights reserved. |
| 4 | * |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 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). |
Yann Collet | 3128e03 | 2017-09-08 00:09:23 -0700 | [diff] [blame] | 8 | * You may select, at your option, one of the above-listed licenses. |
Yann Collet | 4ded9e5 | 2016-08-30 10:04:33 -0700 | [diff] [blame] | 9 | */ |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 10 | |
| 11 | #ifndef ZSTD_ZLIBWRAPPER_H |
| 12 | #define ZSTD_ZLIBWRAPPER_H |
| 13 | |
| 14 | #if defined (__cplusplus) |
| 15 | extern "C" { |
| 16 | #endif |
| 17 | |
| 18 | |
Przemyslaw Skibinski | 6cecb35 | 2016-11-04 17:49:17 +0100 | [diff] [blame] | 19 | #define ZLIB_CONST |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 20 | #define Z_PREFIX |
Przemyslaw Skibinski | c77befe | 2016-11-28 14:09:26 +0100 | [diff] [blame] | 21 | #define ZLIB_INTERNAL /* disables gz*64 functions but fixes zlib 1.2.4 with Z_PREFIX */ |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 22 | #include <zlib.h> |
| 23 | |
inikep | d0c3862 | 2016-05-27 12:33:19 +0200 | [diff] [blame] | 24 | #if !defined(z_const) |
inikep | bf25d7a | 2016-06-02 10:19:35 +0200 | [diff] [blame] | 25 | #define z_const |
| 26 | #endif |
Przemyslaw Skibinski | 6cecb35 | 2016-11-04 17:49:17 +0100 | [diff] [blame] | 27 | |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 28 | |
inikep | 230a61f | 2016-09-21 16:46:35 +0200 | [diff] [blame] | 29 | /* returns a string with version of zstd library */ |
inikep | 614fdde | 2016-06-02 18:40:41 +0200 | [diff] [blame] | 30 | const char * zstdVersion(void); |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 31 | |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 32 | |
inikep | 706876f | 2016-09-27 16:56:07 +0200 | [diff] [blame] | 33 | /*** COMPRESSION ***/ |
Przemyslaw Skibinski | 5c42aad | 2017-02-23 18:28:48 +0100 | [diff] [blame] | 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: |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 36 | - compilation with the additional option -DZWRAP_USE_ZSTD=1 |
Przemyslaw Skibinski | 5c42aad | 2017-02-23 18:28:48 +0100 | [diff] [blame] | 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. */ |
inikep | 252c20d | 2016-09-23 09:08:40 +0200 | [diff] [blame] | 41 | void ZWRAP_useZSTDcompression(int turn_on); |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 42 | |
inikep | 706876f | 2016-09-27 16:56:07 +0200 | [diff] [blame] | 43 | /* checks if zstd compression is turned on */ |
inikep | 252c20d | 2016-09-23 09:08:40 +0200 | [diff] [blame] | 44 | int ZWRAP_isUsingZSTDcompression(void); |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 45 | |
inikep | 7e79257 | 2016-09-21 17:17:29 +0200 | [diff] [blame] | 46 | /* Changes a pledged source size for a given compression stream. |
inikep | dfef5dd | 2016-09-22 10:23:26 +0200 | [diff] [blame] | 47 | It will change ZSTD compression parameters what may improve compression speed and/or ratio. |
inikep | 572d428 | 2016-09-27 15:25:20 +0200 | [diff] [blame] | 48 | The function should be called just after deflateInit() or deflateReset() and before deflate() or deflateSetDictionary(). |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 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) |
inikep | 67a1f4d | 2016-09-26 20:49:18 +0200 | [diff] [blame] | 51 | as this case is automatically detected. */ |
inikep | 252c20d | 2016-09-23 09:08:40 +0200 | [diff] [blame] | 52 | int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize); |
inikep | 230a61f | 2016-09-21 16:46:35 +0200 | [diff] [blame] | 53 | |
inikep | 856f91e | 2016-09-27 17:14:04 +0200 | [diff] [blame] | 54 | /* Similar to deflateReset but preserves dictionary set using deflateSetDictionary. |
Yann Collet | 32fb407 | 2017-08-18 16:52:05 -0700 | [diff] [blame] | 55 | It should improve compression speed because there will be less calls to deflateSetDictionary |
inikep | 856f91e | 2016-09-27 17:14:04 +0200 | [diff] [blame] | 56 | When using zlib compression this method redirects to deflateReset. */ |
inikep | 20859af | 2016-09-27 17:27:43 +0200 | [diff] [blame] | 57 | int ZWRAP_deflateReset_keepDict(z_streamp strm); |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 58 | |
inikep | 706876f | 2016-09-27 16:56:07 +0200 | [diff] [blame] | 59 | |
inikep | 856f91e | 2016-09-27 17:14:04 +0200 | [diff] [blame] | 60 | |
inikep | 706876f | 2016-09-27 16:56:07 +0200 | [diff] [blame] | 61 | /*** DECOMPRESSION ***/ |
inikep | 252c20d | 2016-09-23 09:08:40 +0200 | [diff] [blame] | 62 | typedef enum { ZWRAP_FORCE_ZLIB, ZWRAP_AUTO } ZWRAP_decompress_type; |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 63 | |
Przemyslaw Skibinski | 5c42aad | 2017-02-23 18:28:48 +0100 | [diff] [blame] | 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. */ |
inikep | 252c20d | 2016-09-23 09:08:40 +0200 | [diff] [blame] | 69 | void ZWRAP_setDecompressionType(ZWRAP_decompress_type type); |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 70 | |
inikep | 706876f | 2016-09-27 16:56:07 +0200 | [diff] [blame] | 71 | /* checks zstd decompression type */ |
inikep | 252c20d | 2016-09-23 09:08:40 +0200 | [diff] [blame] | 72 | ZWRAP_decompress_type ZWRAP_getDecompressionType(void); |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 73 | |
inikep | 22e2730 | 2016-09-27 18:21:17 +0200 | [diff] [blame] | 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. */ |
inikep | 706876f | 2016-09-27 16:56:07 +0200 | [diff] [blame] | 76 | int ZWRAP_isUsingZSTDdecompression(z_streamp strm); |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 77 | |
inikep | 706876f | 2016-09-27 16:56:07 +0200 | [diff] [blame] | 78 | /* Similar to inflateReset but preserves dictionary set using inflateSetDictionary. |
inikep | 856f91e | 2016-09-27 17:14:04 +0200 | [diff] [blame] | 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. */ |
inikep | 20859af | 2016-09-27 17:27:43 +0200 | [diff] [blame] | 81 | int ZWRAP_inflateReset_keepDict(z_streamp strm); |
inikep | d755717 | 2016-09-22 11:52:00 +0200 | [diff] [blame] | 82 | |
| 83 | |
inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 84 | #if defined (__cplusplus) |
| 85 | } |
| 86 | #endif |
| 87 | |
| 88 | #endif /* ZSTD_ZLIBWRAPPER_H */ |