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