2016-08-30 10:04:33 -07:00
/**
* Copyright ( c ) 2016 - present , Przemyslaw Skibinski , Facebook , Inc .
* All rights reserved .
*
* This source code is licensed under the BSD - style license found in the
* LICENSE file in the root directory of this source tree . An additional grant
* of patent rights can be found in the PATENTS file in the same directory .
*/
2016-05-12 08:15:41 -07:00
2016-08-10 05:28:47 -07:00
# include <stdio.h> /* vsprintf */
# include <stdarg.h> /* va_list, for z_gzprintf */
2016-05-12 08:15:41 -07:00
# include <zlib.h>
# include "zstd_zlibwrapper.h"
2016-06-04 10:52:06 -07:00
# define ZSTD_STATIC_LINKING_ONLY /* ZSTD_MAGICNUMBER */
2016-05-12 08:15:41 -07:00
# include "zstd.h"
2016-08-10 05:28:47 -07:00
# include "zstd_internal.h" /* defaultCustomMem */
2016-05-12 08:15:41 -07:00
# define Z_INFLATE_SYNC 8
2016-09-16 08:14:01 -07:00
# define ZLIB_HEADERSIZE 4
# define ZSTD_HEADERSIZE ZSTD_frameHeaderSize_min
2016-06-01 01:50:17 -07:00
# define ZWRAP_DEFAULT_CLEVEL 5 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
2016-05-12 08:15:41 -07:00
2016-09-20 06:18:00 -07:00
# define LOG_WRAPPERC(...) /*printf(__VA_ARGS__)*/
2016-09-20 07:22:28 -07:00
# define LOG_WRAPPERD(...) /*printf(__VA_ARGS__)*/
2016-05-12 08:15:41 -07:00
2016-06-03 08:39:31 -07:00
# define FINISH_WITH_GZ_ERR(msg) { \
( void ) msg ; \
2016-09-20 03:50:59 -07:00
return Z_STREAM_ERROR ; \
2016-06-03 08:39:31 -07:00
}
2016-05-12 08:15:41 -07:00
# define FINISH_WITH_NULL_ERR(msg) { \
2016-06-03 08:39:31 -07:00
( void ) msg ; \
2016-05-12 08:15:41 -07:00
return NULL ; \
}
# ifndef ZWRAP_USE_ZSTD
# define ZWRAP_USE_ZSTD 0
# endif
static int g_useZSTD = ZWRAP_USE_ZSTD ; /* 0 = don't use ZSTD */
void useZSTD ( int turn_on ) { g_useZSTD = turn_on ; }
2016-06-02 09:40:41 -07:00
int isUsingZSTD ( void ) { return g_useZSTD ; }
2016-05-12 08:15:41 -07:00
2016-06-02 09:40:41 -07:00
const char * zstdVersion ( void ) { return ZSTD_VERSION_STRING ; }
2016-05-12 08:15:41 -07:00
ZEXTERN const char * ZEXPORT z_zlibVersion OF ( ( void ) ) { return zlibVersion ( ) ; }
2016-09-21 07:46:35 -07:00
2016-06-02 09:24:07 -07:00
static void * ZWRAP_allocFunction ( void * opaque , size_t size )
2016-06-02 07:52:36 -07:00
{
z_streamp strm = ( z_streamp ) opaque ;
void * address = strm - > zalloc ( strm - > opaque , 1 , size ) ;
/* printf("ZWRAP alloc %p, %d \n", address, (int)size); */
return address ;
}
2016-06-02 09:24:07 -07:00
static void ZWRAP_freeFunction ( void * opaque , void * address )
2016-06-02 07:52:36 -07:00
{
z_streamp strm = ( z_streamp ) opaque ;
strm - > zfree ( strm - > opaque , address ) ;
/* if (address) printf("ZWRAP free %p \n", address); */
}
2016-05-12 08:15:41 -07:00
/* *** Compression *** */
typedef struct {
2016-09-16 05:06:10 -07:00
ZSTD_CStream * zbc ;
2016-05-12 08:15:41 -07:00
int compressionLevel ;
2016-06-03 05:53:51 -07:00
ZSTD_customMem customMem ;
2016-06-02 07:52:36 -07:00
z_stream allocFunc ; /* copy of zalloc, zfree, opaque */
2016-09-16 05:06:10 -07:00
ZSTD_inBuffer inBuffer ;
ZSTD_outBuffer outBuffer ;
2016-09-21 07:46:35 -07:00
unsigned long long pledgedSrcSize ;
2016-05-12 08:15:41 -07:00
} ZWRAP_CCtx ;
2016-06-03 07:31:57 -07:00
size_t ZWRAP_freeCCtx ( ZWRAP_CCtx * zwc )
{
if ( zwc = = NULL ) return 0 ; /* support free on NULL */
2016-09-21 10:30:29 -07:00
if ( zwc - > zbc ) ZSTD_freeCStream ( zwc - > zbc ) ;
2016-06-03 07:31:57 -07:00
zwc - > customMem . customFree ( zwc - > customMem . opaque , zwc ) ;
return 0 ;
}
2016-06-02 07:52:36 -07:00
ZWRAP_CCtx * ZWRAP_createCCtx ( z_streamp strm )
2016-05-12 08:15:41 -07:00
{
2016-06-03 05:53:51 -07:00
ZWRAP_CCtx * zwc ;
2016-06-02 07:52:36 -07:00
if ( strm - > zalloc & & strm - > zfree ) {
2016-06-03 05:53:51 -07:00
zwc = ( ZWRAP_CCtx * ) strm - > zalloc ( strm - > opaque , 1 , sizeof ( ZWRAP_CCtx ) ) ;
if ( zwc = = NULL ) return NULL ;
memset ( zwc , 0 , sizeof ( ZWRAP_CCtx ) ) ;
memcpy ( & zwc - > allocFunc , strm , sizeof ( z_stream ) ) ;
{ ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction , ZWRAP_freeFunction , & zwc - > allocFunc } ;
memcpy ( & zwc - > customMem , & ZWRAP_customMem , sizeof ( ZSTD_customMem ) ) ;
}
} else {
zwc = ( ZWRAP_CCtx * ) defaultCustomMem . customAlloc ( defaultCustomMem . opaque , sizeof ( ZWRAP_CCtx ) ) ;
if ( zwc = = NULL ) return NULL ;
memset ( zwc , 0 , sizeof ( ZWRAP_CCtx ) ) ;
memcpy ( & zwc - > customMem , & defaultCustomMem , sizeof ( ZSTD_customMem ) ) ;
2016-06-02 07:52:36 -07:00
}
2016-06-03 05:53:51 -07:00
2016-05-12 08:15:41 -07:00
return zwc ;
}
2016-09-21 10:30:29 -07:00
int ZWRAP_initializeCStream ( ZWRAP_CCtx * zwc )
{
if ( zwc = = NULL ) return Z_STREAM_ERROR ;
if ( zwc - > zbc = = NULL ) {
zwc - > zbc = ZSTD_createCStream_advanced ( zwc - > customMem ) ;
if ( zwc - > zbc = = NULL ) return Z_STREAM_ERROR ;
{ ZSTD_parameters const params = ZSTD_getParams ( zwc - > compressionLevel , zwc - > pledgedSrcSize , 0 ) ;
size_t errorCode ;
LOG_WRAPPERC ( " windowLog=%d chainLog=%d hashLog=%d searchLog=%d searchLength=%d strategy=%d \n " , params . cParams . windowLog , params . cParams . chainLog , params . cParams . hashLog , params . cParams . searchLog , params . cParams . searchLength , params . cParams . strategy ) ;
errorCode = ZSTD_initCStream_advanced ( zwc - > zbc , NULL , 0 , params , zwc - > pledgedSrcSize ) ;
if ( ZSTD_isError ( errorCode ) ) return Z_STREAM_ERROR ; }
}
return Z_OK ;
}
2016-09-19 05:54:13 -07:00
int ZWRAPC_finish_with_error ( ZWRAP_CCtx * zwc , z_streamp strm , int error )
{
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - ZWRAPC_finish_with_error=%d \n " , error ) ;
2016-09-19 05:54:13 -07:00
if ( zwc ) ZWRAP_freeCCtx ( zwc ) ;
if ( strm ) strm - > state = NULL ;
2016-09-20 03:50:59 -07:00
return ( error ) ? error : Z_STREAM_ERROR ;
2016-09-19 05:54:13 -07:00
}
2016-09-21 05:05:01 -07:00
int ZWRAPC_finish_with_error_message ( z_streamp strm , char * message )
{
ZWRAP_CCtx * zwc = ( ZWRAP_CCtx * ) strm - > state ;
strm - > msg = message ;
if ( zwc = = NULL ) return Z_STREAM_ERROR ;
return ZWRAPC_finish_with_error ( zwc , strm , 0 ) ;
}
2016-09-21 07:46:35 -07:00
int ZSTD_setPledgedSrcSize ( z_streamp strm , unsigned long long pledgedSrcSize )
{
ZWRAP_CCtx * zwc = ( ZWRAP_CCtx * ) strm - > state ;
if ( zwc = = NULL ) return Z_STREAM_ERROR ;
zwc - > pledgedSrcSize = pledgedSrcSize ;
return Z_OK ;
}
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_deflateInit_ OF ( ( z_streamp strm , int level ,
const char * version , int stream_size ) )
{
ZWRAP_CCtx * zwc ;
2016-08-30 10:04:33 -07:00
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflateInit level=%d \n " , level ) ;
2016-05-12 08:15:41 -07:00
if ( ! g_useZSTD ) {
return deflateInit_ ( ( strm ) , ( level ) , version , stream_size ) ;
}
2016-06-02 07:52:36 -07:00
zwc = ZWRAP_createCCtx ( strm ) ;
2016-05-12 08:15:41 -07:00
if ( zwc = = NULL ) return Z_MEM_ERROR ;
2016-06-01 01:50:17 -07:00
if ( level = = Z_DEFAULT_COMPRESSION )
level = ZWRAP_DEFAULT_CLEVEL ;
2016-05-12 08:15:41 -07:00
zwc - > compressionLevel = level ;
2016-06-02 03:00:32 -07:00
strm - > state = ( struct internal_state * ) zwc ; /* use state which in not used by user */
2016-05-12 08:15:41 -07:00
strm - > total_in = 0 ;
strm - > total_out = 0 ;
return Z_OK ;
}
2016-06-01 01:50:17 -07:00
ZEXTERN int ZEXPORT z_deflateInit2_ OF ( ( z_streamp strm , int level , int method ,
2016-05-12 08:15:41 -07:00
int windowBits , int memLevel ,
int strategy , const char * version ,
int stream_size ) )
{
if ( ! g_useZSTD )
return deflateInit2_ ( strm , level , method , windowBits , memLevel , strategy , version , stream_size ) ;
return z_deflateInit_ ( strm , level , version , stream_size ) ;
}
2016-09-19 05:27:29 -07:00
ZEXTERN int ZEXPORT z_deflateReset OF ( ( z_streamp strm ) )
{
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflateReset \n " ) ;
2016-09-19 05:27:29 -07:00
if ( ! g_useZSTD )
return deflateReset ( strm ) ;
2016-09-20 03:50:59 -07:00
2016-09-20 03:54:26 -07:00
{ ZWRAP_CCtx * zwc = ( ZWRAP_CCtx * ) strm - > state ;
2016-09-21 10:30:29 -07:00
if ( ! zwc ) return Z_STREAM_ERROR ;
if ( zwc - > zbc = = NULL ) {
int res = ZWRAP_initializeCStream ( zwc ) ;
if ( res ! = Z_OK ) return ZWRAPC_finish_with_error ( zwc , strm , res ) ;
}
2016-09-21 07:46:35 -07:00
{ size_t const errorCode = ZSTD_resetCStream ( zwc - > zbc , zwc - > pledgedSrcSize ) ;
2016-09-20 03:54:26 -07:00
if ( ZSTD_isError ( errorCode ) ) return ZWRAPC_finish_with_error ( zwc , strm , 0 ) ; }
}
strm - > total_in = 0 ;
strm - > total_out = 0 ;
return Z_OK ;
2016-09-19 05:27:29 -07:00
}
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_deflateSetDictionary OF ( ( z_streamp strm ,
const Bytef * dictionary ,
uInt dictLength ) )
{
2016-09-20 06:18:00 -07:00
if ( ! g_useZSTD ) {
LOG_WRAPPERC ( " - deflateSetDictionary \n " ) ;
2016-05-12 08:15:41 -07:00
return deflateSetDictionary ( strm , dictionary , dictLength ) ;
2016-09-20 06:18:00 -07:00
}
2016-05-12 08:15:41 -07:00
2016-06-02 03:00:32 -07:00
{ ZWRAP_CCtx * zwc = ( ZWRAP_CCtx * ) strm - > state ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflateSetDictionary level=%d \n " , ( int ) zwc - > compressionLevel ) ;
2016-09-21 10:30:29 -07:00
if ( ! zwc ) return Z_STREAM_ERROR ;
if ( zwc - > zbc = = NULL ) {
int res = ZWRAP_initializeCStream ( zwc ) ;
if ( res ! = Z_OK ) return ZWRAPC_finish_with_error ( zwc , strm , res ) ;
}
2016-09-16 05:06:10 -07:00
{ size_t const errorCode = ZSTD_initCStream_usingDict ( zwc - > zbc , dictionary , dictLength , zwc - > compressionLevel ) ;
2016-09-19 05:54:13 -07:00
if ( ZSTD_isError ( errorCode ) ) return ZWRAPC_finish_with_error ( zwc , strm , 0 ) ; }
2016-05-12 08:15:41 -07:00
}
2016-08-30 10:04:33 -07:00
2016-05-12 08:15:41 -07:00
return Z_OK ;
}
2016-09-19 05:54:13 -07:00
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_deflate OF ( ( z_streamp strm , int flush ) )
{
ZWRAP_CCtx * zwc ;
if ( ! g_useZSTD ) {
2016-09-20 06:18:00 -07:00
int res ;
LOG_WRAPPERC ( " - deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out ) ;
res = deflate ( strm , flush ) ;
LOG_WRAPPERC ( " - deflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out ) ;
2016-05-12 08:15:41 -07:00
return res ;
}
2016-06-02 03:00:32 -07:00
zwc = ( ZWRAP_CCtx * ) strm - > state ;
2016-09-20 03:50:59 -07:00
if ( zwc = = NULL ) return Z_STREAM_ERROR ;
2016-05-12 08:15:41 -07:00
2016-09-21 10:30:29 -07:00
if ( zwc - > zbc = = NULL ) {
int res = ZWRAP_initializeCStream ( zwc ) ;
if ( res ! = Z_OK ) return ZWRAPC_finish_with_error ( zwc , strm , res ) ;
}
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out ) ;
2016-05-12 08:15:41 -07:00
if ( strm - > avail_in > 0 ) {
2016-09-16 05:06:10 -07:00
zwc - > inBuffer . src = strm - > next_in ;
zwc - > inBuffer . size = strm - > avail_in ;
zwc - > inBuffer . pos = 0 ;
zwc - > outBuffer . dst = strm - > next_out ;
zwc - > outBuffer . size = strm - > avail_out ;
zwc - > outBuffer . pos = 0 ;
{ size_t const errorCode = ZSTD_compressStream ( zwc - > zbc , & zwc - > outBuffer , & zwc - > inBuffer ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " deflate ZSTD_compressStream srcSize=%d dstCapacity=%d \n " , ( int ) zwc - > inBuffer . size , ( int ) zwc - > outBuffer . size ) ;
2016-09-19 05:54:13 -07:00
if ( ZSTD_isError ( errorCode ) ) return ZWRAPC_finish_with_error ( zwc , strm , 0 ) ;
2016-09-16 05:06:10 -07:00
}
strm - > next_out + = zwc - > outBuffer . pos ;
strm - > total_out + = zwc - > outBuffer . pos ;
strm - > avail_out - = zwc - > outBuffer . pos ;
strm - > total_in + = zwc - > inBuffer . pos ;
strm - > next_in + = zwc - > inBuffer . pos ;
strm - > avail_in - = zwc - > inBuffer . pos ;
2016-05-12 08:15:41 -07:00
}
2016-09-21 05:05:01 -07:00
if ( flush = = Z_FULL_FLUSH | | flush = = Z_BLOCK | | flush = = Z_TREES ) return ZWRAPC_finish_with_error_message ( strm , " Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported! " ) ;
2016-05-12 08:15:41 -07:00
2016-08-13 12:02:45 -07:00
if ( flush = = Z_FINISH ) {
2016-05-12 08:15:41 -07:00
size_t bytesLeft ;
2016-09-16 05:06:10 -07:00
zwc - > outBuffer . dst = strm - > next_out ;
zwc - > outBuffer . size = strm - > avail_out ;
zwc - > outBuffer . pos = 0 ;
2016-09-19 04:24:07 -07:00
bytesLeft = ZSTD_endStream ( zwc - > zbc , & zwc - > outBuffer ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d \n " , ( int ) strm - > avail_out , ( int ) bytesLeft ) ;
2016-09-19 05:54:13 -07:00
if ( ZSTD_isError ( bytesLeft ) ) return ZWRAPC_finish_with_error ( zwc , strm , 0 ) ;
2016-09-16 05:06:10 -07:00
strm - > next_out + = zwc - > outBuffer . pos ;
strm - > total_out + = zwc - > outBuffer . pos ;
strm - > avail_out - = zwc - > outBuffer . pos ;
2016-09-20 06:18:00 -07:00
if ( bytesLeft = = 0 ) { LOG_WRAPPERC ( " Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d \n " , ( int ) strm - > total_in , ( int ) strm - > avail_out , ( int ) strm - > total_out ) ; return Z_STREAM_END ; }
2016-05-12 08:15:41 -07:00
}
2016-09-19 04:24:07 -07:00
else
2016-09-19 05:27:29 -07:00
if ( flush = = Z_SYNC_FLUSH | | flush = = Z_PARTIAL_FLUSH ) {
2016-08-13 12:02:45 -07:00
size_t bytesLeft ;
2016-09-16 08:14:01 -07:00
zwc - > outBuffer . dst = strm - > next_out ;
zwc - > outBuffer . size = strm - > avail_out ;
zwc - > outBuffer . pos = 0 ;
2016-09-16 05:06:10 -07:00
bytesLeft = ZSTD_flushStream ( zwc - > zbc , & zwc - > outBuffer ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d \n " , ( int ) strm - > avail_out , ( int ) bytesLeft ) ;
2016-09-19 05:54:13 -07:00
if ( ZSTD_isError ( bytesLeft ) ) return ZWRAPC_finish_with_error ( zwc , strm , 0 ) ;
2016-09-16 05:06:10 -07:00
strm - > next_out + = zwc - > outBuffer . pos ;
strm - > total_out + = zwc - > outBuffer . pos ;
strm - > avail_out - = zwc - > outBuffer . pos ;
2016-08-13 12:02:45 -07:00
}
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out ) ;
2016-05-12 08:15:41 -07:00
return Z_OK ;
}
2016-08-30 10:04:33 -07:00
ZEXTERN int ZEXPORT z_deflateEnd OF ( ( z_streamp strm ) )
2016-05-12 08:15:41 -07:00
{
if ( ! g_useZSTD ) {
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflateEnd \n " ) ;
2016-05-12 08:15:41 -07:00
return deflateEnd ( strm ) ;
}
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflateEnd total_in=%d total_out=%d \n " , ( int ) ( strm - > total_in ) , ( int ) ( strm - > total_out ) ) ;
2016-09-21 04:51:57 -07:00
{ size_t errorCode ;
ZWRAP_CCtx * zwc = ( ZWRAP_CCtx * ) strm - > state ;
if ( zwc = = NULL ) return Z_OK ; /* structures are already freed */
2016-09-19 05:54:13 -07:00
strm - > state = NULL ;
2016-09-21 04:51:57 -07:00
errorCode = ZWRAP_freeCCtx ( zwc ) ;
2016-09-20 03:50:59 -07:00
if ( ZSTD_isError ( errorCode ) ) return Z_STREAM_ERROR ;
2016-05-12 08:15:41 -07:00
}
return Z_OK ;
}
ZEXTERN uLong ZEXPORT z_deflateBound OF ( ( z_streamp strm ,
uLong sourceLen ) )
{
if ( ! g_useZSTD )
return deflateBound ( strm , sourceLen ) ;
return ZSTD_compressBound ( sourceLen ) ;
}
ZEXTERN int ZEXPORT z_deflateParams OF ( ( z_streamp strm ,
int level ,
int strategy ) )
{
if ( ! g_useZSTD ) {
2016-09-20 06:18:00 -07:00
LOG_WRAPPERC ( " - deflateParams level=%d strategy=%d \n " , level , strategy ) ;
2016-05-12 08:15:41 -07:00
return deflateParams ( strm , level , strategy ) ;
}
return Z_OK ;
}
/* *** Decompression *** */
typedef struct {
2016-09-16 05:06:10 -07:00
ZSTD_DStream * zbd ;
2016-09-16 08:14:01 -07:00
char headerBuf [ 16 ] ; /* should be equal or bigger than ZSTD_frameHeaderSize_min */
2016-05-12 08:15:41 -07:00
int errorCount ;
2016-09-20 07:22:28 -07:00
int decompState ;
ZSTD_inBuffer inBuffer ;
ZSTD_outBuffer outBuffer ;
2016-08-30 10:04:33 -07:00
2016-05-12 08:15:41 -07:00
/* zlib params */
int stream_size ;
char * version ;
int windowBits ;
2016-06-03 07:31:57 -07:00
ZSTD_customMem customMem ;
2016-06-02 07:52:36 -07:00
z_stream allocFunc ; /* copy of zalloc, zfree, opaque */
2016-05-12 08:15:41 -07:00
} ZWRAP_DCtx ;
2016-09-20 07:22:28 -07:00
void ZWRAP_initDCtx ( ZWRAP_DCtx * zwd )
{
zwd - > errorCount = zwd - > decompState = 0 ;
zwd - > outBuffer . pos = 0 ;
zwd - > outBuffer . size = 0 ;
}
2016-06-02 07:52:36 -07:00
ZWRAP_DCtx * ZWRAP_createDCtx ( z_streamp strm )
2016-05-12 08:15:41 -07:00
{
2016-06-03 07:31:57 -07:00
ZWRAP_DCtx * zwd ;
if ( strm - > zalloc & & strm - > zfree ) {
zwd = ( ZWRAP_DCtx * ) strm - > zalloc ( strm - > opaque , 1 , sizeof ( ZWRAP_DCtx ) ) ;
if ( zwd = = NULL ) return NULL ;
memset ( zwd , 0 , sizeof ( ZWRAP_DCtx ) ) ;
memcpy ( & zwd - > allocFunc , strm , sizeof ( z_stream ) ) ;
{ ZSTD_customMem ZWRAP_customMem = { ZWRAP_allocFunction , ZWRAP_freeFunction , & zwd - > allocFunc } ;
memcpy ( & zwd - > customMem , & ZWRAP_customMem , sizeof ( ZSTD_customMem ) ) ;
}
} else {
zwd = ( ZWRAP_DCtx * ) defaultCustomMem . customAlloc ( defaultCustomMem . opaque , sizeof ( ZWRAP_DCtx ) ) ;
if ( zwd = = NULL ) return NULL ;
memset ( zwd , 0 , sizeof ( ZWRAP_DCtx ) ) ;
memcpy ( & zwd - > customMem , & defaultCustomMem , sizeof ( ZSTD_customMem ) ) ;
}
2016-09-20 07:22:28 -07:00
ZWRAP_initDCtx ( zwd ) ;
2016-05-12 08:15:41 -07:00
return zwd ;
}
size_t ZWRAP_freeDCtx ( ZWRAP_DCtx * zwd )
{
if ( zwd = = NULL ) return 0 ; /* support free on null */
2016-09-16 05:06:10 -07:00
ZSTD_freeDStream ( zwd - > zbd ) ;
2016-06-03 07:31:57 -07:00
if ( zwd - > version ) zwd - > customMem . customFree ( zwd - > customMem . opaque , zwd - > version ) ;
zwd - > customMem . customFree ( zwd - > customMem . opaque , zwd ) ;
2016-05-12 08:15:41 -07:00
return 0 ;
}
2016-09-19 05:54:13 -07:00
int ZWRAPD_finish_with_error ( ZWRAP_DCtx * zwd , z_streamp strm , int error )
2016-09-19 05:31:16 -07:00
{
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " - ZWRAPD_finish_with_error=%d \n " , error ) ;
2016-09-19 05:31:16 -07:00
if ( zwd ) ZWRAP_freeDCtx ( zwd ) ;
2016-09-19 05:54:13 -07:00
if ( strm ) strm - > state = NULL ;
2016-09-20 03:50:59 -07:00
return ( error ) ? error : Z_STREAM_ERROR ;
2016-09-19 05:31:16 -07:00
}
2016-09-21 05:05:01 -07:00
int ZWRAPD_finish_with_error_message ( z_streamp strm , char * message )
{
ZWRAP_DCtx * zwd = ( ZWRAP_DCtx * ) strm - > state ;
strm - > msg = message ;
if ( zwd = = NULL ) return Z_STREAM_ERROR ;
return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ;
}
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_inflateInit_ OF ( ( z_streamp strm ,
const char * version , int stream_size ) )
{
2016-06-02 07:52:36 -07:00
ZWRAP_DCtx * zwd = ZWRAP_createDCtx ( strm ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " - inflateInit \n " ) ;
2016-09-19 05:54:13 -07:00
if ( zwd = = NULL ) return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ;
2016-05-12 08:15:41 -07:00
2016-06-03 07:31:57 -07:00
zwd - > version = zwd - > customMem . customAlloc ( zwd - > customMem . opaque , strlen ( version ) + 1 ) ;
2016-09-19 05:54:13 -07:00
if ( zwd - > version = = NULL ) return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ;
2016-06-03 07:31:57 -07:00
strcpy ( zwd - > version , version ) ;
2016-05-12 08:15:41 -07:00
2016-06-03 07:31:57 -07:00
zwd - > stream_size = stream_size ;
2016-06-02 03:00:32 -07:00
strm - > state = ( struct internal_state * ) zwd ; /* use state which in not used by user */
2016-05-12 08:15:41 -07:00
strm - > total_in = 0 ;
strm - > total_out = 0 ;
strm - > reserved = 1 ; /* mark as unknown steam */
return Z_OK ;
}
ZEXTERN int ZEXPORT z_inflateInit2_ OF ( ( z_streamp strm , int windowBits ,
const char * version , int stream_size ) )
{
int ret = z_inflateInit_ ( strm , version , stream_size ) ;
if ( ret = = Z_OK ) {
ZWRAP_DCtx * zwd = ( ZWRAP_DCtx * ) strm - > state ;
2016-09-20 07:40:50 -07:00
if ( zwd = = NULL ) return Z_STREAM_ERROR ;
2016-05-12 08:15:41 -07:00
zwd - > windowBits = windowBits ;
}
return ret ;
}
2016-09-20 03:50:59 -07:00
ZEXTERN int ZEXPORT z_inflateReset OF ( ( z_streamp strm ) )
{
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " - inflateReset \n " ) ;
2016-09-20 03:50:59 -07:00
if ( ! strm - > reserved )
return inflateReset ( strm ) ;
2016-09-20 06:18:00 -07:00
{ ZWRAP_DCtx * zwd = ( ZWRAP_DCtx * ) strm - > state ;
if ( zwd = = NULL ) return Z_STREAM_ERROR ;
{ size_t const errorCode = ZSTD_resetDStream ( zwd - > zbd ) ;
if ( ZSTD_isError ( errorCode ) ) return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ; }
2016-09-20 07:22:28 -07:00
ZWRAP_initDCtx ( zwd ) ;
2016-09-20 06:18:00 -07:00
}
2016-09-20 03:50:59 -07:00
strm - > total_in = 0 ;
strm - > total_out = 0 ;
return Z_OK ;
}
2016-09-20 07:40:50 -07:00
# if ZLIB_VERNUM >= 0x1240
ZEXTERN int ZEXPORT z_inflateReset2 OF ( ( z_streamp strm ,
int windowBits ) )
{
if ( ! strm - > reserved )
return inflateReset2 ( strm , windowBits ) ;
{ int ret = z_inflateReset ( strm ) ;
if ( ret = = Z_OK ) {
ZWRAP_DCtx * zwd = ( ZWRAP_DCtx * ) strm - > state ;
if ( zwd = = NULL ) return Z_STREAM_ERROR ;
zwd - > windowBits = windowBits ;
}
return ret ;
}
}
# endif
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_inflateSetDictionary OF ( ( z_streamp strm ,
const Bytef * dictionary ,
uInt dictLength ) )
{
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " - inflateSetDictionary \n " ) ;
2016-05-12 08:15:41 -07:00
if ( ! strm - > reserved )
return inflateSetDictionary ( strm , dictionary , dictLength ) ;
2016-06-13 03:00:46 -07:00
{ size_t errorCode ;
ZWRAP_DCtx * zwd = ( ZWRAP_DCtx * ) strm - > state ;
2016-09-21 04:51:57 -07:00
if ( zwd = = NULL ) return Z_STREAM_ERROR ;
2016-09-16 05:06:10 -07:00
errorCode = ZSTD_initDStream_usingDict ( zwd - > zbd , dictionary , dictLength ) ;
2016-09-19 05:54:13 -07:00
if ( ZSTD_isError ( errorCode ) ) return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ;
2016-08-30 10:04:33 -07:00
2016-09-16 08:14:01 -07:00
if ( strm - > total_in = = ZSTD_HEADERSIZE ) {
2016-09-16 05:06:10 -07:00
zwd - > inBuffer . src = zwd - > headerBuf ;
zwd - > inBuffer . size = strm - > total_in ;
zwd - > inBuffer . pos = 0 ;
zwd - > outBuffer . dst = strm - > next_out ;
zwd - > outBuffer . size = 0 ;
zwd - > outBuffer . pos = 0 ;
errorCode = ZSTD_decompressStream ( zwd - > zbd , & zwd - > outBuffer , & zwd - > inBuffer ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d \n " , ( int ) errorCode , ( int ) zwd - > inBuffer . size , ( int ) zwd - > outBuffer . size ) ;
2016-09-16 08:14:01 -07:00
if ( zwd - > inBuffer . pos < zwd - > outBuffer . size | | ZSTD_isError ( errorCode ) ) {
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " ERROR: ZSTD_decompressStream %s \n " , ZSTD_getErrorName ( errorCode ) ) ;
2016-09-19 05:54:13 -07:00
return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ;
2016-05-12 08:15:41 -07:00
}
}
}
return Z_OK ;
}
2016-08-30 10:04:33 -07:00
ZEXTERN int ZEXPORT z_inflate OF ( ( z_streamp strm , int flush ) )
2016-05-12 08:15:41 -07:00
{
2016-09-20 06:18:00 -07:00
int res ;
if ( ! strm - > reserved ) {
LOG_WRAPPERD ( " - inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out ) ;
res = inflate ( strm , flush ) ;
2016-09-20 07:22:28 -07:00
LOG_WRAPPERD ( " - inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out , res ) ;
2016-09-20 06:18:00 -07:00
return res ;
}
2016-05-12 08:15:41 -07:00
if ( strm - > avail_in > 0 ) {
2016-09-16 08:14:01 -07:00
size_t errorCode , srcSize , inPos ;
2016-05-12 08:15:41 -07:00
ZWRAP_DCtx * zwd = ( ZWRAP_DCtx * ) strm - > state ;
2016-09-21 04:51:57 -07:00
if ( zwd = = NULL ) return Z_STREAM_ERROR ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " - inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out ) ;
2016-09-20 07:22:28 -07:00
if ( zwd - > decompState = = Z_STREAM_END ) return Z_STREAM_END ;
2016-09-16 08:14:01 -07:00
// if (((strm->avail_in < ZSTD_HEADERSIZE) || (strm->total_in > 0)) && (strm->total_in < ZLIB_HEADERSIZE))
if ( strm - > total_in < ZLIB_HEADERSIZE )
2016-05-12 08:15:41 -07:00
{
2016-09-16 08:14:01 -07:00
// printf(".");
srcSize = MIN ( strm - > avail_in , ZLIB_HEADERSIZE - strm - > total_in ) ;
2016-05-12 08:15:41 -07:00
memcpy ( zwd - > headerBuf + strm - > total_in , strm - > next_in , srcSize ) ;
strm - > total_in + = srcSize ;
strm - > next_in + = srcSize ;
strm - > avail_in - = srcSize ;
2016-09-16 08:14:01 -07:00
if ( strm - > total_in < ZLIB_HEADERSIZE ) return Z_OK ;
2016-05-12 08:15:41 -07:00
if ( MEM_readLE32 ( zwd - > headerBuf ) ! = ZSTD_MAGICNUMBER ) {
z_stream strm2 ;
strm2 . next_in = strm - > next_in ;
strm2 . avail_in = strm - > avail_in ;
strm2 . next_out = strm - > next_out ;
strm2 . avail_out = strm - > avail_out ;
if ( zwd - > windowBits )
errorCode = inflateInit2_ ( strm , zwd - > windowBits , zwd - > version , zwd - > stream_size ) ;
else
errorCode = inflateInit_ ( strm , zwd - > version , zwd - > stream_size ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " ZLIB inflateInit errorCode=%d \n " , ( int ) errorCode ) ;
2016-09-19 05:54:13 -07:00
if ( errorCode ! = Z_OK ) return ZWRAPD_finish_with_error ( zwd , strm , ( int ) errorCode ) ;
2016-05-12 08:15:41 -07:00
/* inflate header */
strm - > next_in = ( unsigned char * ) zwd - > headerBuf ;
2016-09-16 08:14:01 -07:00
strm - > avail_in = ZLIB_HEADERSIZE ;
2016-05-12 08:15:41 -07:00
strm - > avail_out = 0 ;
errorCode = inflate ( strm , Z_NO_FLUSH ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " ZLIB inflate errorCode=%d strm->avail_in=%d \n " , ( int ) errorCode , ( int ) strm - > avail_in ) ;
2016-09-19 05:54:13 -07:00
if ( errorCode ! = Z_OK ) return ZWRAPD_finish_with_error ( zwd , strm , ( int ) errorCode ) ;
2016-06-13 03:00:46 -07:00
if ( strm - > avail_in > 0 ) goto error ;
2016-08-30 10:04:33 -07:00
2016-05-12 08:15:41 -07:00
strm - > next_in = strm2 . next_in ;
strm - > avail_in = strm2 . avail_in ;
strm - > next_out = strm2 . next_out ;
strm - > avail_out = strm2 . avail_out ;
2016-06-02 03:00:32 -07:00
2016-05-12 08:15:41 -07:00
strm - > reserved = 0 ; /* mark as zlib stream */
2016-06-02 09:40:41 -07:00
errorCode = ZWRAP_freeDCtx ( zwd ) ;
2016-06-13 03:00:46 -07:00
if ( ZSTD_isError ( errorCode ) ) goto error ;
2016-06-02 03:00:32 -07:00
2016-09-20 06:18:00 -07:00
if ( flush = = Z_INFLATE_SYNC ) res = inflateSync ( strm ) ;
else res = inflate ( strm , flush ) ;
2016-09-20 07:22:28 -07:00
LOG_WRAPPERD ( " - inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out , res ) ;
2016-09-20 06:18:00 -07:00
return res ;
2016-05-12 08:15:41 -07:00
}
2016-09-16 08:14:01 -07:00
}
// if (((strm->avail_in < ZSTD_HEADERSIZE) || (strm->total_in > 0)) && (strm->total_in < ZSTD_HEADERSIZE))
if ( strm - > total_in < ZSTD_HEADERSIZE )
{
// printf("+");
srcSize = MIN ( strm - > avail_in , ZSTD_HEADERSIZE - strm - > total_in ) ;
memcpy ( zwd - > headerBuf + strm - > total_in , strm - > next_in , srcSize ) ;
strm - > total_in + = srcSize ;
strm - > next_in + = srcSize ;
strm - > avail_in - = srcSize ;
if ( strm - > total_in < ZSTD_HEADERSIZE ) return Z_OK ;
2016-05-12 08:15:41 -07:00
2016-09-16 05:06:10 -07:00
zwd - > zbd = ZSTD_createDStream_advanced ( zwd - > customMem ) ;
2016-06-13 03:00:46 -07:00
if ( zwd - > zbd = = NULL ) goto error ;
2016-06-02 07:52:36 -07:00
2016-09-16 05:06:10 -07:00
errorCode = ZSTD_initDStream ( zwd - > zbd ) ;
2016-06-13 03:00:46 -07:00
if ( ZSTD_isError ( errorCode ) ) goto error ;
2016-05-12 08:15:41 -07:00
2016-09-19 05:27:29 -07:00
if ( flush = = Z_INFLATE_SYNC ) { strm - > msg = " inflateSync is not supported! " ; goto error ; }
2016-09-16 08:14:01 -07:00
inPos = zwd - > inBuffer . pos ;
2016-09-16 05:06:10 -07:00
zwd - > inBuffer . src = zwd - > headerBuf ;
2016-09-16 08:14:01 -07:00
zwd - > inBuffer . size = ZSTD_HEADERSIZE ;
2016-09-16 05:06:10 -07:00
zwd - > inBuffer . pos = 0 ;
zwd - > outBuffer . dst = strm - > next_out ;
zwd - > outBuffer . size = 0 ;
zwd - > outBuffer . pos = 0 ;
errorCode = ZSTD_decompressStream ( zwd - > zbd , & zwd - > outBuffer , & zwd - > inBuffer ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d \n " , ( int ) errorCode , ( int ) zwd - > inBuffer . size , ( int ) zwd - > outBuffer . size ) ;
2016-05-12 08:15:41 -07:00
if ( ZSTD_isError ( errorCode ) ) {
2016-09-20 07:22:28 -07:00
LOG_WRAPPERD ( " ERROR: ZSTD_decompressStream1 %s \n " , ZSTD_getErrorName ( errorCode ) ) ;
2016-06-13 03:00:46 -07:00
goto error ;
2016-05-12 08:15:41 -07:00
}
2016-09-20 06:18:00 -07:00
// LOG_WRAPPERD("1srcSize=%d inpos=%d inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d\n", (int)srcSize, (int)inPos, (int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos);
if ( zwd - > inBuffer . pos ! = zwd - > inBuffer . size ) return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ; /* not consumed */
2016-05-12 08:15:41 -07:00
}
2016-09-16 08:14:01 -07:00
inPos = 0 ; //zwd->inBuffer.pos;
2016-09-16 05:06:10 -07:00
zwd - > inBuffer . src = strm - > next_in ;
zwd - > inBuffer . size = strm - > avail_in ;
zwd - > inBuffer . pos = 0 ;
zwd - > outBuffer . dst = strm - > next_out ;
zwd - > outBuffer . size = strm - > avail_out ;
zwd - > outBuffer . pos = 0 ;
errorCode = ZSTD_decompressStream ( zwd - > zbd , & zwd - > outBuffer , & zwd - > inBuffer ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d \n " , ( int ) errorCode , ( int ) strm - > avail_in , ( int ) strm - > avail_out ) ;
2016-05-12 08:15:41 -07:00
if ( ZSTD_isError ( errorCode ) ) {
zwd - > errorCount + + ;
2016-09-20 07:22:28 -07:00
LOG_WRAPPERD ( " ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d \n " , ZSTD_getErrorName ( errorCode ) , zwd - > errorCount ) ;
2016-06-13 03:00:46 -07:00
if ( zwd - > errorCount < = 1 ) return Z_NEED_DICT ; else goto error ;
2016-05-12 08:15:41 -07:00
}
2016-09-20 07:22:28 -07:00
LOG_WRAPPERD ( " inflate inpos=%d inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o \n " , ( int ) inPos , ( int ) zwd - > inBuffer . pos , ( int ) zwd - > inBuffer . size , ( int ) zwd - > outBuffer . pos , ( int ) zwd - > outBuffer . size ) ;
2016-09-16 05:06:10 -07:00
strm - > next_out + = zwd - > outBuffer . pos ;
strm - > total_out + = zwd - > outBuffer . pos ;
strm - > avail_out - = zwd - > outBuffer . pos ;
2016-09-16 08:14:01 -07:00
strm - > total_in + = zwd - > inBuffer . pos - inPos ;
strm - > next_in + = zwd - > inBuffer . pos - inPos ;
strm - > avail_in - = zwd - > inBuffer . pos - inPos ;
2016-09-20 07:22:28 -07:00
if ( errorCode = = 0 ) {
LOG_WRAPPERD ( " inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d \n " , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out ) ;
zwd - > decompState = Z_STREAM_END ;
return Z_STREAM_END ;
}
2016-09-20 06:18:00 -07:00
goto finish ;
2016-06-13 03:00:46 -07:00
error :
2016-09-19 05:54:13 -07:00
return ZWRAPD_finish_with_error ( zwd , strm , 0 ) ;
2016-05-12 08:15:41 -07:00
}
2016-09-20 06:18:00 -07:00
finish :
2016-09-20 07:22:28 -07:00
LOG_WRAPPERD ( " - inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d \n " , ( int ) flush , ( int ) strm - > avail_in , ( int ) strm - > avail_out , ( int ) strm - > total_in , ( int ) strm - > total_out , Z_OK ) ;
2016-05-12 08:15:41 -07:00
return Z_OK ;
}
ZEXTERN int ZEXPORT z_inflateEnd OF ( ( z_streamp strm ) )
{
if ( ! strm - > reserved )
2016-06-02 03:00:32 -07:00
return inflateEnd ( strm ) ;
2016-08-30 10:04:33 -07:00
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " - inflateEnd total_in=%d total_out=%d \n " , ( int ) ( strm - > total_in ) , ( int ) ( strm - > total_out ) ) ;
2016-09-21 04:51:57 -07:00
{ size_t errorCode ;
ZWRAP_DCtx * zwd = ( ZWRAP_DCtx * ) strm - > state ;
if ( zwd = = NULL ) return Z_OK ; /* structures are already freed */
2016-06-13 03:00:46 -07:00
strm - > state = NULL ;
2016-09-21 04:51:57 -07:00
errorCode = ZWRAP_freeDCtx ( zwd ) ;
if ( ZSTD_isError ( errorCode ) ) return Z_STREAM_ERROR ;
2016-05-12 08:15:41 -07:00
}
2016-09-21 04:51:57 -07:00
return Z_OK ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_inflateSync OF ( ( z_streamp strm ) )
{
2016-09-20 03:50:59 -07:00
if ( ! strm - > reserved ) {
return inflateSync ( strm ) ;
}
2016-09-19 05:27:29 -07:00
2016-05-12 08:15:41 -07:00
return z_inflate ( strm , Z_INFLATE_SYNC ) ;
}
/* Advanced compression functions */
ZEXTERN int ZEXPORT z_deflateCopy OF ( ( z_streamp dest ,
z_streamp source ) )
{
if ( ! g_useZSTD )
return deflateCopy ( dest , source ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPC_finish_with_error_message ( source , " deflateCopy is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_deflateTune OF ( ( z_streamp strm ,
int good_length ,
int max_lazy ,
int nice_length ,
int max_chain ) )
{
if ( ! g_useZSTD )
return deflateTune ( strm , good_length , max_lazy , nice_length , max_chain ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPC_finish_with_error_message ( strm , " deflateTune is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
# if ZLIB_VERNUM >= 0x1260
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_deflatePending OF ( ( z_streamp strm ,
unsigned * pending ,
int * bits ) )
{
if ( ! g_useZSTD )
return deflatePending ( strm , pending , bits ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPC_finish_with_error_message ( strm , " deflatePending is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
# endif
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_deflatePrime OF ( ( z_streamp strm ,
int bits ,
int value ) )
{
if ( ! g_useZSTD )
return deflatePrime ( strm , bits , value ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPC_finish_with_error_message ( strm , " deflatePrime is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_deflateSetHeader OF ( ( z_streamp strm ,
gz_headerp head ) )
{
if ( ! g_useZSTD )
return deflateSetHeader ( strm , head ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPC_finish_with_error_message ( strm , " deflateSetHeader is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-09-19 05:27:29 -07:00
/* Advanced decompression functions */
2016-06-02 01:19:35 -07:00
# if ZLIB_VERNUM >= 0x1280
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_inflateGetDictionary OF ( ( z_streamp strm ,
Bytef * dictionary ,
uInt * dictLength ) )
{
if ( ! strm - > reserved )
return inflateGetDictionary ( strm , dictionary , dictLength ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( strm , " inflateGetDictionary is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
# endif
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_inflateCopy OF ( ( z_streamp dest ,
z_streamp source ) )
{
if ( ! g_useZSTD )
return inflateCopy ( dest , source ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( source , " inflateCopy is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
# if ZLIB_VERNUM >= 0x1240
ZEXTERN long ZEXPORT z_inflateMark OF ( ( z_streamp strm ) )
2016-05-12 08:15:41 -07:00
{
if ( ! strm - > reserved )
2016-06-02 01:19:35 -07:00
return inflateMark ( strm ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( strm , " inflateMark is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
# endif
2016-05-12 08:15:41 -07:00
2016-06-02 01:19:35 -07:00
ZEXTERN int ZEXPORT z_inflatePrime OF ( ( z_streamp strm ,
int bits ,
int value ) )
2016-05-12 08:15:41 -07:00
{
if ( ! strm - > reserved )
2016-06-02 01:19:35 -07:00
return inflatePrime ( strm , bits , value ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( strm , " inflatePrime is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_inflateGetHeader OF ( ( z_streamp strm ,
gz_headerp head ) )
{
if ( ! strm - > reserved )
return inflateGetHeader ( strm , head ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( strm , " inflateGetHeader is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_inflateBackInit_ OF ( ( z_streamp strm , int windowBits ,
unsigned char FAR * window ,
const char * version ,
int stream_size ) )
{
if ( ! strm - > reserved )
return inflateBackInit_ ( strm , windowBits , window , version , stream_size ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( strm , " inflateBackInit is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_inflateBack OF ( ( z_streamp strm ,
in_func in , void FAR * in_desc ,
out_func out , void FAR * out_desc ) )
{
if ( ! strm - > reserved )
return inflateBack ( strm , in , in_desc , out , out_desc ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( strm , " inflateBack is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_inflateBackEnd OF ( ( z_streamp strm ) )
{
if ( ! strm - > reserved )
return inflateBackEnd ( strm ) ;
2016-09-21 05:05:01 -07:00
return ZWRAPD_finish_with_error_message ( strm , " inflateBackEnd is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF ( ( void ) ) { return zlibCompileFlags ( ) ; } ;
/* utility functions */
# ifndef Z_SOLO
ZEXTERN int ZEXPORT z_compress OF ( ( Bytef * dest , uLongf * destLen ,
const Bytef * source , uLong sourceLen ) )
{
if ( ! g_useZSTD )
return compress ( dest , destLen , source , sourceLen ) ;
2016-08-30 10:04:33 -07:00
{ size_t dstCapacity = * destLen ;
2016-06-03 10:44:03 -07:00
size_t const errorCode = ZSTD_compress ( dest , dstCapacity , source , sourceLen , ZWRAP_DEFAULT_CLEVEL ) ;
2016-09-20 06:18:00 -07:00
LOG_WRAPPERD ( " z_compress sourceLen=%d dstCapacity=%d \n " , ( int ) sourceLen , ( int ) dstCapacity ) ;
2016-09-20 03:50:59 -07:00
if ( ZSTD_isError ( errorCode ) ) return Z_STREAM_ERROR ;
2016-05-12 08:15:41 -07:00
* destLen = errorCode ;
}
return Z_OK ;
}
ZEXTERN int ZEXPORT z_compress2 OF ( ( Bytef * dest , uLongf * destLen ,
const Bytef * source , uLong sourceLen ,
int level ) )
{
if ( ! g_useZSTD )
return compress2 ( dest , destLen , source , sourceLen , level ) ;
2016-08-30 10:04:33 -07:00
{ size_t dstCapacity = * destLen ;
2016-06-02 13:15:09 -07:00
size_t const errorCode = ZSTD_compress ( dest , dstCapacity , source , sourceLen , level ) ;
2016-09-20 03:50:59 -07:00
if ( ZSTD_isError ( errorCode ) ) return Z_STREAM_ERROR ;
2016-05-12 08:15:41 -07:00
* destLen = errorCode ;
}
return Z_OK ;
}
ZEXTERN uLong ZEXPORT z_compressBound OF ( ( uLong sourceLen ) )
{
if ( ! g_useZSTD )
return compressBound ( sourceLen ) ;
return ZSTD_compressBound ( sourceLen ) ;
}
ZEXTERN int ZEXPORT z_uncompress OF ( ( Bytef * dest , uLongf * destLen ,
const Bytef * source , uLong sourceLen ) )
{
if ( sourceLen < 4 | | MEM_readLE32 ( source ) ! = ZSTD_MAGICNUMBER )
return uncompress ( dest , destLen , source , sourceLen ) ;
2016-08-30 10:04:33 -07:00
{ size_t dstCapacity = * destLen ;
2016-06-02 13:15:09 -07:00
size_t const errorCode = ZSTD_decompress ( dest , dstCapacity , source , sourceLen ) ;
2016-09-20 03:50:59 -07:00
if ( ZSTD_isError ( errorCode ) ) return Z_STREAM_ERROR ;
2016-05-12 08:15:41 -07:00
* destLen = errorCode ;
}
return Z_OK ;
}
/* gzip file access functions */
ZEXTERN gzFile ZEXPORT z_gzopen OF ( ( const char * path , const char * mode ) )
{
if ( ! g_useZSTD )
return gzopen ( path , mode ) ;
FINISH_WITH_NULL_ERR ( " gzopen is not supported! " ) ;
}
ZEXTERN gzFile ZEXPORT z_gzdopen OF ( ( int fd , const char * mode ) )
{
if ( ! g_useZSTD )
return gzdopen ( fd , mode ) ;
FINISH_WITH_NULL_ERR ( " gzdopen is not supported! " ) ;
}
2016-06-02 01:19:35 -07:00
# if ZLIB_VERNUM >= 0x1240
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_gzbuffer OF ( ( gzFile file , unsigned size ) )
{
if ( ! g_useZSTD )
return gzbuffer ( file , size ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzbuffer is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
ZEXTERN z_off_t ZEXPORT z_gzoffset OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gzoffset ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzoffset is not supported! " ) ;
2016-06-02 01:19:35 -07:00
}
ZEXTERN int ZEXPORT z_gzclose_r OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gzclose_r ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzclose_r is not supported! " ) ;
2016-06-02 01:19:35 -07:00
}
ZEXTERN int ZEXPORT z_gzclose_w OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gzclose_w ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzclose_w is not supported! " ) ;
2016-06-02 01:19:35 -07:00
}
# endif
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_gzsetparams OF ( ( gzFile file , int level , int strategy ) )
{
if ( ! g_useZSTD )
return gzsetparams ( file , level , strategy ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzsetparams is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzread OF ( ( gzFile file , voidp buf , unsigned len ) )
{
if ( ! g_useZSTD )
return gzread ( file , buf , len ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzread is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzwrite OF ( ( gzFile file ,
voidpc buf , unsigned len ) )
{
if ( ! g_useZSTD )
return gzwrite ( file , buf , len ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzwrite is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
# if ZLIB_VERNUM >= 0x1260
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORTVA z_gzprintf Z_ARG ( ( gzFile file , const char * format , . . . ) )
2016-06-02 01:19:35 -07:00
# else
ZEXTERN int ZEXPORTVA z_gzprintf OF ( ( gzFile file , const char * format , . . . ) )
# endif
2016-05-12 08:15:41 -07:00
{
if ( ! g_useZSTD ) {
int ret ;
char buf [ 1024 ] ;
va_list args ;
va_start ( args , format ) ;
ret = vsprintf ( buf , format , args ) ;
va_end ( args ) ;
ret = gzprintf ( file , buf ) ;
return ret ;
}
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzprintf is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzputs OF ( ( gzFile file , const char * s ) )
{
if ( ! g_useZSTD )
return gzputs ( file , s ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzputs is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN char * ZEXPORT z_gzgets OF ( ( gzFile file , char * buf , int len ) )
{
if ( ! g_useZSTD )
return gzgets ( file , buf , len ) ;
FINISH_WITH_NULL_ERR ( " gzgets is not supported! " ) ;
}
ZEXTERN int ZEXPORT z_gzputc OF ( ( gzFile file , int c ) )
{
if ( ! g_useZSTD )
return gzputc ( file , c ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzputc is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
2016-06-02 01:19:35 -07:00
# if ZLIB_VERNUM == 0x1260
ZEXTERN int ZEXPORT z_gzgetc_ OF ( ( gzFile file ) )
# else
2016-05-12 08:15:41 -07:00
ZEXTERN int ZEXPORT z_gzgetc OF ( ( gzFile file ) )
2016-06-02 01:19:35 -07:00
# endif
2016-05-12 08:15:41 -07:00
{
if ( ! g_useZSTD )
return gzgetc ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzgetc is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzungetc OF ( ( int c , gzFile file ) )
{
if ( ! g_useZSTD )
return gzungetc ( c , file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzungetc is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzflush OF ( ( gzFile file , int flush ) )
{
if ( ! g_useZSTD )
return gzflush ( file , flush ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzflush is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN z_off_t ZEXPORT z_gzseek OF ( ( gzFile file , z_off_t offset , int whence ) )
{
if ( ! g_useZSTD )
return gzseek ( file , offset , whence ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzseek is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzrewind OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gzrewind ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzrewind is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN z_off_t ZEXPORT z_gztell OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gztell ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gztell is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzeof OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gzeof ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzeof is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzdirect OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gzdirect ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzdirect is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN int ZEXPORT z_gzclose OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
return gzclose ( file ) ;
2016-06-03 08:39:31 -07:00
FINISH_WITH_GZ_ERR ( " gzclose is not supported! " ) ;
2016-05-12 08:15:41 -07:00
}
ZEXTERN const char * ZEXPORT z_gzerror OF ( ( gzFile file , int * errnum ) )
{
if ( ! g_useZSTD )
return gzerror ( file , errnum ) ;
FINISH_WITH_NULL_ERR ( " gzerror is not supported! " ) ;
}
ZEXTERN void ZEXPORT z_gzclearerr OF ( ( gzFile file ) )
{
if ( ! g_useZSTD )
gzclearerr ( file ) ;
}
# endif /* !Z_SOLO */
/* checksum functions */
ZEXTERN uLong ZEXPORT z_adler32 OF ( ( uLong adler , const Bytef * buf , uInt len ) )
{
return adler32 ( adler , buf , len ) ;
}
ZEXTERN uLong ZEXPORT z_crc32 OF ( ( uLong crc , const Bytef * buf , uInt len ) )
{
return crc32 ( crc , buf , len ) ;
}