10c16b537SWarner Losh /* 25ff13fbcSAllan Jude * Copyright (c) 2016-2021, Przemyslaw Skibinski, Yann Collet, Facebook, Inc. 30c16b537SWarner Losh * All rights reserved. 40c16b537SWarner Losh * 50c16b537SWarner Losh * This source code is licensed under both the BSD-style license (found in the 60c16b537SWarner Losh * LICENSE file in the root directory of this source tree) and the GPLv2 (found 70c16b537SWarner Losh * in the COPYING file in the root directory of this source tree). 80c16b537SWarner Losh * You may select, at your option, one of the above-listed licenses. 90c16b537SWarner Losh */ 100c16b537SWarner Losh 110c16b537SWarner Losh #ifndef ZSTD_ZLIBWRAPPER_H 120c16b537SWarner Losh #define ZSTD_ZLIBWRAPPER_H 130c16b537SWarner Losh 140c16b537SWarner Losh #if defined (__cplusplus) 150c16b537SWarner Losh extern "C" { 160c16b537SWarner Losh #endif 170c16b537SWarner Losh 180c16b537SWarner Losh 190c16b537SWarner Losh #define ZLIB_CONST 200c16b537SWarner Losh #define Z_PREFIX 210c16b537SWarner Losh #define ZLIB_INTERNAL /* disables gz*64 functions but fixes zlib 1.2.4 with Z_PREFIX */ 220c16b537SWarner Losh #include <zlib.h> 230c16b537SWarner Losh 240c16b537SWarner Losh #if !defined(z_const) 250c16b537SWarner Losh #define z_const 260c16b537SWarner Losh #endif 270c16b537SWarner Losh 280c16b537SWarner Losh 290c16b537SWarner Losh /* returns a string with version of zstd library */ 300c16b537SWarner Losh const char * zstdVersion(void); 310c16b537SWarner Losh 320c16b537SWarner Losh 330c16b537SWarner Losh /*** COMPRESSION ***/ 340c16b537SWarner Losh /* ZWRAP_useZSTDcompression() enables/disables zstd compression during runtime. 350c16b537SWarner Losh By default zstd compression is disabled. To enable zstd compression please use one of the methods: 360c16b537SWarner Losh - compilation with the additional option -DZWRAP_USE_ZSTD=1 370c16b537SWarner Losh - using '#define ZWRAP_USE_ZSTD 1' in source code before '#include "zstd_zlibwrapper.h"' 380c16b537SWarner Losh - calling ZWRAP_useZSTDcompression(1) 390c16b537SWarner Losh All above-mentioned methods will enable zstd compression for all threads. 400c16b537SWarner Losh Be aware that ZWRAP_useZSTDcompression() is not thread-safe and may lead to a race condition. */ 410c16b537SWarner Losh void ZWRAP_useZSTDcompression(int turn_on); 420c16b537SWarner Losh 430c16b537SWarner Losh /* checks if zstd compression is turned on */ 440c16b537SWarner Losh int ZWRAP_isUsingZSTDcompression(void); 450c16b537SWarner Losh 460c16b537SWarner Losh /* Changes a pledged source size for a given compression stream. 470c16b537SWarner Losh It will change ZSTD compression parameters what may improve compression speed and/or ratio. 480c16b537SWarner Losh The function should be called just after deflateInit() or deflateReset() and before deflate() or deflateSetDictionary(). 490c16b537SWarner Losh It's only helpful when data is compressed in blocks. 500c16b537SWarner Losh There will be no change in case of deflateInit() or deflateReset() immediately followed by deflate(strm, Z_FINISH) 510c16b537SWarner Losh as this case is automatically detected. */ 520c16b537SWarner Losh int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize); 530c16b537SWarner Losh 540c16b537SWarner Losh /* Similar to deflateReset but preserves dictionary set using deflateSetDictionary. 550c16b537SWarner Losh It should improve compression speed because there will be less calls to deflateSetDictionary 560c16b537SWarner Losh When using zlib compression this method redirects to deflateReset. */ 570c16b537SWarner Losh int ZWRAP_deflateReset_keepDict(z_streamp strm); 580c16b537SWarner Losh 590c16b537SWarner Losh 600c16b537SWarner Losh 610c16b537SWarner Losh /*** DECOMPRESSION ***/ 620c16b537SWarner Losh typedef enum { ZWRAP_FORCE_ZLIB, ZWRAP_AUTO } ZWRAP_decompress_type; 630c16b537SWarner Losh 640c16b537SWarner Losh /* ZWRAP_setDecompressionType() enables/disables automatic recognition of zstd/zlib compressed data during runtime. 650c16b537SWarner Losh By default auto-detection of zstd and zlib streams in enabled (ZWRAP_AUTO). 660c16b537SWarner Losh Forcing zlib decompression with ZWRAP_setDecompressionType(ZWRAP_FORCE_ZLIB) slightly improves 670c16b537SWarner Losh decompression speed of zlib-encoded streams. 680c16b537SWarner Losh Be aware that ZWRAP_setDecompressionType() is not thread-safe and may lead to a race condition. */ 690c16b537SWarner Losh void ZWRAP_setDecompressionType(ZWRAP_decompress_type type); 700c16b537SWarner Losh 710c16b537SWarner Losh /* checks zstd decompression type */ 720c16b537SWarner Losh ZWRAP_decompress_type ZWRAP_getDecompressionType(void); 730c16b537SWarner Losh 740c16b537SWarner Losh /* Checks if zstd decompression is used for a given stream. 750c16b537SWarner Losh If will return 1 only when inflate() was called and zstd header was detected. */ 760c16b537SWarner Losh int ZWRAP_isUsingZSTDdecompression(z_streamp strm); 770c16b537SWarner Losh 780c16b537SWarner Losh /* Similar to inflateReset but preserves dictionary set using inflateSetDictionary. 790c16b537SWarner Losh inflate() will return Z_NEED_DICT only for the first time what will improve decompression speed. 800c16b537SWarner Losh For zlib streams this method redirects to inflateReset. */ 810c16b537SWarner Losh int ZWRAP_inflateReset_keepDict(z_streamp strm); 820c16b537SWarner Losh 830c16b537SWarner Losh 840c16b537SWarner Losh #if defined (__cplusplus) 850c16b537SWarner Losh } 860c16b537SWarner Losh #endif 870c16b537SWarner Losh 880c16b537SWarner Losh #endif /* ZSTD_ZLIBWRAPPER_H */ 89