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