2017-08-18 23:52:05 +00:00
|
|
|
/*
|
2017-08-21 18:24:32 +00:00
|
|
|
* Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
|
2016-08-30 17:04:33 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-18 23:52:05 +00:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-09-08 07:09:23 +00:00
|
|
|
* You may select, at your option, one of the above-listed licenses.
|
2016-08-30 17:04:33 +00:00
|
|
|
*/
|
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
/* === Tuning parameters === */
|
|
|
|
#ifndef ZWRAP_USE_ZSTD
|
|
|
|
#define ZWRAP_USE_ZSTD 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/* === Dependencies === */
|
2017-05-31 00:11:39 +00:00
|
|
|
#include <stdlib.h>
|
2016-08-10 12:28:47 +00:00
|
|
|
#include <stdio.h> /* vsprintf */
|
|
|
|
#include <stdarg.h> /* va_list, for z_gzprintf */
|
2016-11-04 15:05:28 +00:00
|
|
|
#define NO_DUMMY_DECL
|
2016-11-04 16:49:17 +00:00
|
|
|
#define ZLIB_CONST
|
2016-11-04 15:05:28 +00:00
|
|
|
#include <zlib.h> /* without #define Z_PREFIX */
|
2016-05-12 15:15:41 +00:00
|
|
|
#include "zstd_zlibwrapper.h"
|
2017-06-02 21:24:58 +00:00
|
|
|
#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_isFrame, ZSTD_MAGICNUMBER */
|
2016-05-12 15:15:41 +00:00
|
|
|
#include "zstd.h"
|
2017-06-02 21:01:21 +00:00
|
|
|
#include "zstd_internal.h" /* ZSTD_malloc, ZSTD_free */
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
/* === Constants === */
|
2016-05-12 15:15:41 +00:00
|
|
|
#define Z_INFLATE_SYNC 8
|
2016-09-16 15:14:01 +00:00
|
|
|
#define ZLIB_HEADERSIZE 4
|
2018-11-14 00:56:32 +00:00
|
|
|
#define ZSTD_HEADERSIZE ZSTD_FRAMEHEADERSIZE_MIN
|
2016-09-26 18:49:18 +00:00
|
|
|
#define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
|
|
|
|
/* === Debug === */
|
|
|
|
#define LOG_WRAPPERC(...) /* fprintf(stderr, __VA_ARGS__) */
|
|
|
|
#define LOG_WRAPPERD(...) /* fprintf(stderr, __VA_ARGS__) */
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2016-09-23 18:03:17 +00:00
|
|
|
#define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }
|
|
|
|
#define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }
|
2016-09-22 09:52:00 +00:00
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
/* === Wrapper === */
|
|
|
|
static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2016-09-23 07:08:40 +00:00
|
|
|
void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2016-09-23 07:08:40 +00:00
|
|
|
int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-22 09:52:00 +00:00
|
|
|
static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;
|
|
|
|
|
2016-09-23 07:08:40 +00:00
|
|
|
void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; };
|
2016-09-22 09:52:00 +00:00
|
|
|
|
2016-09-23 07:08:40 +00:00
|
|
|
ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-23 18:03:17 +00:00
|
|
|
const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }
|
|
|
|
|
|
|
|
ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion(); }
|
|
|
|
|
|
|
|
|
2016-09-21 14:46:35 +00:00
|
|
|
|
2016-06-02 16:24:07 +00:00
|
|
|
static void* ZWRAP_allocFunction(void* opaque, size_t size)
|
2016-06-02 14:52:36 +00:00
|
|
|
{
|
|
|
|
z_streamp strm = (z_streamp) opaque;
|
2016-12-22 14:26:33 +00:00
|
|
|
void* address = strm->zalloc(strm->opaque, 1, (uInt)size);
|
2017-06-03 00:10:49 +00:00
|
|
|
/* LOG_WRAPPERC("ZWRAP alloc %p, %d \n", address, (int)size); */
|
2016-06-02 14:52:36 +00:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2016-06-02 16:24:07 +00:00
|
|
|
static void ZWRAP_freeFunction(void* opaque, void* address)
|
2016-06-02 14:52:36 +00:00
|
|
|
{
|
|
|
|
z_streamp strm = (z_streamp) opaque;
|
|
|
|
strm->zfree(strm->opaque, address);
|
2017-06-03 00:10:49 +00:00
|
|
|
/* if (address) LOG_WRAPPERC("ZWRAP free %p \n", address); */
|
2016-06-02 14:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
/* === Compression === */
|
2016-09-27 14:56:07 +00:00
|
|
|
typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2016-09-16 12:06:10 +00:00
|
|
|
ZSTD_CStream* zbc;
|
2016-05-12 15:15:41 +00:00
|
|
|
int compressionLevel;
|
2017-01-19 11:10:52 +00:00
|
|
|
int streamEnd; /* a flag to signal the end of a stream */
|
|
|
|
unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
|
2016-06-03 12:53:51 +00:00
|
|
|
ZSTD_customMem customMem;
|
2016-06-02 14:52:36 +00:00
|
|
|
z_stream allocFunc; /* copy of zalloc, zfree, opaque */
|
2016-09-16 12:06:10 +00:00
|
|
|
ZSTD_inBuffer inBuffer;
|
|
|
|
ZSTD_outBuffer outBuffer;
|
2016-09-27 14:56:07 +00:00
|
|
|
ZWRAP_state_t comprState;
|
2016-09-21 14:46:35 +00:00
|
|
|
unsigned long long pledgedSrcSize;
|
2016-11-04 15:05:28 +00:00
|
|
|
} ZWRAP_CCtx;
|
|
|
|
|
|
|
|
typedef ZWRAP_CCtx internal_state;
|
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)
|
2016-06-03 14:31:57 +00:00
|
|
|
{
|
|
|
|
if (zwc==NULL) return 0; /* support free on NULL */
|
2017-06-03 00:10:49 +00:00
|
|
|
ZSTD_freeCStream(zwc->zbc);
|
2017-06-02 21:01:21 +00:00
|
|
|
ZSTD_free(zwc, zwc->customMem);
|
2016-06-03 14:31:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)
|
2016-05-12 15:15:41 +00:00
|
|
|
{
|
2016-06-03 12:53:51 +00:00
|
|
|
ZWRAP_CCtx* zwc;
|
|
|
|
|
2016-06-02 14:52:36 +00:00
|
|
|
if (strm->zalloc && strm->zfree) {
|
2016-06-03 12:53:51 +00: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));
|
2017-06-03 00:10:49 +00:00
|
|
|
{ ZSTD_customMem const ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwc->allocFunc };
|
|
|
|
zwc->customMem = ZWRAP_customMem; }
|
2016-06-03 12:53:51 +00:00
|
|
|
} else {
|
2017-05-31 00:11:39 +00:00
|
|
|
zwc = (ZWRAP_CCtx*)calloc(1, sizeof(*zwc));
|
2016-06-03 12:53:51 +00:00
|
|
|
if (zwc==NULL) return NULL;
|
2016-06-02 14:52:36 +00:00
|
|
|
}
|
2016-06-03 12:53:51 +00:00
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
return zwc;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)
|
2016-09-21 17:30:29 +00:00
|
|
|
{
|
2016-09-23 16:59:53 +00:00
|
|
|
LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);
|
2016-09-26 18:49:18 +00:00
|
|
|
if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;
|
2016-12-21 18:57:18 +00:00
|
|
|
|
2016-09-26 18:49:18 +00:00
|
|
|
if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;
|
2017-06-03 00:10:49 +00:00
|
|
|
{ ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);
|
|
|
|
size_t initErr;
|
2018-11-20 23:13:27 +00:00
|
|
|
LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d minMatch=%d strategy=%d\n",
|
|
|
|
(int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.minMatch, params.cParams.strategy);
|
2017-06-03 00:10:49 +00:00
|
|
|
initErr = ZSTD_initCStream_advanced(zwc->zbc, dict, dictSize, params, pledgedSrcSize);
|
|
|
|
if (ZSTD_isError(initErr)) return Z_STREAM_ERROR;
|
|
|
|
}
|
2016-09-22 12:42:32 +00:00
|
|
|
|
2016-09-21 17:30:29 +00:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)
|
2016-09-19 12:54:13 +00:00
|
|
|
{
|
2016-09-21 17:39:25 +00:00
|
|
|
LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);
|
2016-09-19 12:54:13 +00:00
|
|
|
if (zwc) ZWRAP_freeCCtx(zwc);
|
|
|
|
if (strm) strm->state = NULL;
|
2016-09-20 10:50:59 +00:00
|
|
|
return (error) ? error : Z_STREAM_ERROR;
|
2016-09-19 12:54:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)
|
2016-09-21 12:05:01 +00:00
|
|
|
{
|
|
|
|
ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
|
|
|
|
strm->msg = message;
|
|
|
|
if (zwc == NULL) return Z_STREAM_ERROR;
|
2016-12-21 18:57:18 +00:00
|
|
|
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPC_finishWithError(zwc, strm, 0);
|
2016-09-21 12:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-23 07:08:40 +00:00
|
|
|
int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)
|
2016-09-21 14:46:35 +00:00
|
|
|
{
|
|
|
|
ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
|
|
|
|
if (zwc == NULL) return Z_STREAM_ERROR;
|
2016-12-21 18:57:18 +00:00
|
|
|
|
2016-09-21 14:46:35 +00:00
|
|
|
zwc->pledgedSrcSize = pledgedSrcSize;
|
2016-09-27 13:24:44 +00:00
|
|
|
zwc->comprState = ZWRAP_useInit;
|
2016-09-21 14:46:35 +00:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,
|
|
|
|
const char *version, int stream_size))
|
|
|
|
{
|
|
|
|
ZWRAP_CCtx* zwc;
|
2016-08-30 17:04:33 +00:00
|
|
|
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("- deflateInit level=%d\n", level);
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression) {
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateInit_((strm), (level), version, stream_size);
|
|
|
|
}
|
|
|
|
|
2016-06-02 14:52:36 +00:00
|
|
|
zwc = ZWRAP_createCCtx(strm);
|
2016-05-12 15:15:41 +00:00
|
|
|
if (zwc == NULL) return Z_MEM_ERROR;
|
|
|
|
|
2016-06-01 08:50:17 +00:00
|
|
|
if (level == Z_DEFAULT_COMPRESSION)
|
|
|
|
level = ZWRAP_DEFAULT_CLEVEL;
|
|
|
|
|
2016-11-24 17:26:30 +00:00
|
|
|
zwc->streamEnd = 0;
|
2017-01-19 11:10:52 +00:00
|
|
|
zwc->totalInBytes = 0;
|
2016-05-12 15:15:41 +00:00
|
|
|
zwc->compressionLevel = level;
|
2016-06-02 10:00:32 +00:00
|
|
|
strm->state = (struct internal_state*) zwc; /* use state which in not used by user */
|
2016-05-12 15:15:41 +00:00
|
|
|
strm->total_in = 0;
|
|
|
|
strm->total_out = 0;
|
2016-09-23 10:42:21 +00:00
|
|
|
strm->adler = 0;
|
2016-05-12 15:15:41 +00:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-01 08:50:17 +00:00
|
|
|
ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,
|
2016-05-12 15:15:41 +00:00
|
|
|
int windowBits, int memLevel,
|
|
|
|
int strategy, const char *version,
|
|
|
|
int stream_size))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);
|
|
|
|
|
|
|
|
return z_deflateInit_ (strm, level, version, stream_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-27 15:27:43 +00:00
|
|
|
int ZWRAP_deflateReset_keepDict(z_streamp strm)
|
2016-09-27 14:56:07 +00:00
|
|
|
{
|
2016-09-27 15:27:43 +00:00
|
|
|
LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");
|
2016-09-27 15:14:04 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
|
|
|
return deflateReset(strm);
|
|
|
|
|
2016-11-24 17:26:30 +00:00
|
|
|
{ ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
|
2017-05-31 00:11:39 +00:00
|
|
|
if (zwc) {
|
2017-01-19 11:10:52 +00:00
|
|
|
zwc->streamEnd = 0;
|
|
|
|
zwc->totalInBytes = 0;
|
|
|
|
}
|
2016-11-24 17:26:30 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 14:56:07 +00:00
|
|
|
strm->total_in = 0;
|
|
|
|
strm->total_out = 0;
|
|
|
|
strm->adler = 0;
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-19 12:27:29 +00:00
|
|
|
ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))
|
|
|
|
{
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("- deflateReset\n");
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-09-19 12:27:29 +00:00
|
|
|
return deflateReset(strm);
|
2016-12-21 18:57:18 +00:00
|
|
|
|
2016-09-27 15:27:43 +00:00
|
|
|
ZWRAP_deflateReset_keepDict(strm);
|
2016-09-27 14:56:07 +00:00
|
|
|
|
|
|
|
{ ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
|
2016-11-24 17:26:30 +00:00
|
|
|
if (zwc) zwc->comprState = ZWRAP_useInit;
|
2016-09-27 14:56:07 +00:00
|
|
|
}
|
2016-09-20 10:54:26 +00:00
|
|
|
return Z_OK;
|
2016-09-19 12:27:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,
|
|
|
|
const Bytef *dictionary,
|
|
|
|
uInt dictLength))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression) {
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("- deflateSetDictionary\n");
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateSetDictionary(strm, dictionary, dictLength);
|
2016-09-20 13:18:00 +00:00
|
|
|
}
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2016-06-02 10:00:32 +00:00
|
|
|
{ ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);
|
2016-09-21 17:30:29 +00:00
|
|
|
if (!zwc) return Z_STREAM_ERROR;
|
|
|
|
if (zwc->zbc == NULL) {
|
2016-09-26 18:49:18 +00:00
|
|
|
zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
|
2016-09-26 20:11:55 +00:00
|
|
|
if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
|
2016-09-21 17:30:29 +00:00
|
|
|
}
|
2017-10-18 18:18:27 +00:00
|
|
|
{ int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, ZSTD_CONTENTSIZE_UNKNOWN);
|
2016-09-26 20:11:55 +00:00
|
|
|
if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }
|
2016-09-27 13:24:44 +00:00
|
|
|
zwc->comprState = ZWRAP_useReset;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2016-08-30 17:04:33 +00:00
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-19 12:54:13 +00:00
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))
|
|
|
|
{
|
|
|
|
ZWRAP_CCtx* zwc;
|
|
|
|
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression) {
|
2017-06-03 00:10:49 +00: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);
|
|
|
|
return deflate(strm, flush);
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 10:00:32 +00:00
|
|
|
zwc = (ZWRAP_CCtx*) strm->state;
|
2016-09-23 16:59:53 +00:00
|
|
|
if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2016-09-21 17:30:29 +00:00
|
|
|
if (zwc->zbc == NULL) {
|
2016-09-26 18:49:18 +00:00
|
|
|
zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);
|
2016-12-21 18:57:18 +00:00
|
|
|
if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);
|
2017-10-18 18:18:27 +00:00
|
|
|
{ int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
|
2017-06-03 00:10:49 +00:00
|
|
|
if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }
|
2016-09-27 13:24:44 +00:00
|
|
|
if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
|
2016-09-23 08:30:26 +00:00
|
|
|
} else {
|
2017-01-19 11:10:52 +00:00
|
|
|
if (zwc->totalInBytes == 0) {
|
2016-09-27 13:24:44 +00:00
|
|
|
if (zwc->comprState == ZWRAP_useReset) {
|
2017-06-03 00:10:49 +00:00
|
|
|
size_t const resetErr = ZSTD_resetCStream(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);
|
|
|
|
if (ZSTD_isError(resetErr)) {
|
|
|
|
LOG_WRAPPERC("ERROR: ZSTD_resetCStream errorCode=%s\n",
|
|
|
|
ZSTD_getErrorName(resetErr));
|
|
|
|
return ZWRAPC_finishWithError(zwc, strm, 0);
|
|
|
|
}
|
2016-09-26 18:49:18 +00:00
|
|
|
} else {
|
2017-10-18 18:18:27 +00:00
|
|
|
int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);
|
2016-09-26 18:49:18 +00:00
|
|
|
if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);
|
2016-09-27 13:24:44 +00:00
|
|
|
if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;
|
2016-09-26 18:49:18 +00:00
|
|
|
}
|
2017-06-03 00:10:49 +00:00
|
|
|
} /* (zwc->totalInBytes == 0) */
|
|
|
|
} /* ! (zwc->zbc == NULL) */
|
2016-09-21 17:30:29 +00:00
|
|
|
|
2016-12-13 18:14:04 +00: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 15:15:41 +00:00
|
|
|
if (strm->avail_in > 0) {
|
2016-09-16 12:06:10 +00: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;
|
2017-06-03 00:10:49 +00:00
|
|
|
{ size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);
|
2017-06-03 00:10:49 +00:00
|
|
|
if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);
|
2016-09-16 12:06:10 +00: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;
|
2017-01-19 11:10:52 +00:00
|
|
|
zwc->totalInBytes += zwc->inBuffer.pos;
|
2016-09-16 12:06:10 +00:00
|
|
|
strm->next_in += zwc->inBuffer.pos;
|
|
|
|
strm->avail_in -= zwc->inBuffer.pos;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
2016-12-21 18:57:18 +00:00
|
|
|
if (flush == Z_FULL_FLUSH
|
2016-11-28 11:46:16 +00:00
|
|
|
#if ZLIB_VERNUM >= 0x1240
|
2016-12-21 18:57:18 +00:00
|
|
|
|| flush == Z_TREES
|
2016-11-28 11:46:16 +00:00
|
|
|
#endif
|
|
|
|
|| flush == Z_BLOCK)
|
|
|
|
return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2016-08-13 19:02:45 +00:00
|
|
|
if (flush == Z_FINISH) {
|
2016-05-12 15:15:41 +00:00
|
|
|
size_t bytesLeft;
|
2016-11-24 17:26:30 +00:00
|
|
|
if (zwc->streamEnd) return Z_STREAM_END;
|
2016-09-16 12:06:10 +00:00
|
|
|
zwc->outBuffer.dst = strm->next_out;
|
|
|
|
zwc->outBuffer.size = strm->avail_out;
|
|
|
|
zwc->outBuffer.pos = 0;
|
2016-09-19 11:24:07 +00:00
|
|
|
bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
|
2016-09-21 17:39:25 +00:00
|
|
|
if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
|
2016-09-16 12:06:10 +00:00
|
|
|
strm->next_out += zwc->outBuffer.pos;
|
|
|
|
strm->total_out += zwc->outBuffer.pos;
|
|
|
|
strm->avail_out -= zwc->outBuffer.pos;
|
2017-06-03 00:10:49 +00:00
|
|
|
if (bytesLeft == 0) {
|
|
|
|
zwc->streamEnd = 1;
|
|
|
|
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-09-19 11:24:07 +00:00
|
|
|
else
|
2016-09-19 12:27:29 +00:00
|
|
|
if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {
|
2016-08-13 19:02:45 +00:00
|
|
|
size_t bytesLeft;
|
2016-09-16 15:14:01 +00:00
|
|
|
zwc->outBuffer.dst = strm->next_out;
|
|
|
|
zwc->outBuffer.size = strm->avail_out;
|
|
|
|
zwc->outBuffer.pos = 0;
|
2016-09-16 12:06:10 +00:00
|
|
|
bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);
|
2016-09-21 17:39:25 +00:00
|
|
|
if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);
|
2016-09-16 12:06:10 +00:00
|
|
|
strm->next_out += zwc->outBuffer.pos;
|
|
|
|
strm->total_out += zwc->outBuffer.pos;
|
|
|
|
strm->avail_out -= zwc->outBuffer.pos;
|
2016-08-13 19:02:45 +00:00
|
|
|
}
|
2016-12-13 18:14:04 +00:00
|
|
|
LOG_WRAPPERC("- deflate3 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 15:15:41 +00:00
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-30 17:04:33 +00:00
|
|
|
ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))
|
2016-05-12 15:15:41 +00:00
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression) {
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("- deflateEnd\n");
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateEnd(strm);
|
|
|
|
}
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));
|
2016-09-21 11:51:57 +00:00
|
|
|
{ size_t errorCode;
|
|
|
|
ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;
|
|
|
|
if (zwc == NULL) return Z_OK; /* structures are already freed */
|
2016-09-19 12:54:13 +00:00
|
|
|
strm->state = NULL;
|
2016-09-21 11:51:57 +00:00
|
|
|
errorCode = ZWRAP_freeCCtx(zwc);
|
2016-09-20 10:50:59 +00:00
|
|
|
if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,
|
|
|
|
uLong sourceLen))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateBound(strm, sourceLen);
|
|
|
|
|
|
|
|
return ZSTD_compressBound(sourceLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,
|
|
|
|
int level,
|
|
|
|
int strategy))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression) {
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateParams(strm, level, strategy);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
/* === Decompression === */
|
|
|
|
|
2016-09-27 14:56:07 +00:00
|
|
|
typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2016-09-16 12:06:10 +00:00
|
|
|
ZSTD_DStream* zbd;
|
2017-06-03 00:10:49 +00:00
|
|
|
char headerBuf[16]; /* must be >= ZSTD_frameHeaderSize_min */
|
2016-05-12 15:15:41 +00:00
|
|
|
int errorCount;
|
2017-01-19 11:10:52 +00:00
|
|
|
unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */
|
2016-09-27 14:56:07 +00:00
|
|
|
ZWRAP_state_t decompState;
|
2016-09-20 14:22:28 +00:00
|
|
|
ZSTD_inBuffer inBuffer;
|
|
|
|
ZSTD_outBuffer outBuffer;
|
2016-08-30 17:04:33 +00:00
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
/* zlib params */
|
|
|
|
int stream_size;
|
|
|
|
char *version;
|
|
|
|
int windowBits;
|
2016-06-03 14:31:57 +00:00
|
|
|
ZSTD_customMem customMem;
|
2017-06-03 00:10:49 +00:00
|
|
|
z_stream allocFunc; /* just to copy zalloc, zfree, opaque */
|
2016-11-04 15:05:28 +00:00
|
|
|
} ZWRAP_DCtx;
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)
|
2016-09-20 14:22:28 +00:00
|
|
|
{
|
2016-09-27 14:56:07 +00:00
|
|
|
zwd->errorCount = 0;
|
2016-09-20 14:22:28 +00:00
|
|
|
zwd->outBuffer.pos = 0;
|
|
|
|
zwd->outBuffer.size = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)
|
2016-05-12 15:15:41 +00:00
|
|
|
{
|
2016-06-03 14:31:57 +00:00
|
|
|
ZWRAP_DCtx* zwd;
|
2017-06-03 00:10:49 +00:00
|
|
|
MEM_STATIC_ASSERT(sizeof(zwd->headerBuf) >= ZSTD_FRAMEHEADERSIZE_MIN); /* check static buffer size condition */
|
2016-06-03 14:31:57 +00:00
|
|
|
|
|
|
|
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));
|
2017-06-03 00:10:49 +00:00
|
|
|
zwd->allocFunc = *strm; /* just to copy zalloc, zfree & opaque */
|
|
|
|
{ ZSTD_customMem const ZWRAP_customMem = { ZWRAP_allocFunction, ZWRAP_freeFunction, &zwd->allocFunc };
|
|
|
|
zwd->customMem = ZWRAP_customMem; }
|
2016-06-03 14:31:57 +00:00
|
|
|
} else {
|
2017-05-31 00:11:39 +00:00
|
|
|
zwd = (ZWRAP_DCtx*)calloc(1, sizeof(*zwd));
|
2016-06-03 14:31:57 +00:00
|
|
|
if (zwd==NULL) return NULL;
|
|
|
|
}
|
|
|
|
|
2016-09-20 14:22:28 +00:00
|
|
|
ZWRAP_initDCtx(zwd);
|
2016-05-12 15:15:41 +00:00
|
|
|
return zwd;
|
|
|
|
}
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)
|
2016-05-12 15:15:41 +00:00
|
|
|
{
|
|
|
|
if (zwd==NULL) return 0; /* support free on null */
|
2017-06-03 00:10:49 +00:00
|
|
|
ZSTD_freeDStream(zwd->zbd);
|
2017-06-02 21:01:21 +00:00
|
|
|
ZSTD_free(zwd->version, zwd->customMem);
|
|
|
|
ZSTD_free(zwd, zwd->customMem);
|
2016-05-12 15:15:41 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
int ZWRAP_isUsingZSTDdecompression(z_streamp strm)
|
|
|
|
{
|
|
|
|
if (strm == NULL) return 0;
|
|
|
|
return (strm->reserved == ZWRAP_ZSTD_STREAM);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)
|
2016-09-19 12:31:16 +00:00
|
|
|
{
|
2016-09-21 17:39:25 +00:00
|
|
|
LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);
|
2017-06-03 00:10:49 +00:00
|
|
|
ZWRAP_freeDCtx(zwd);
|
|
|
|
strm->state = NULL;
|
2016-09-20 10:50:59 +00:00
|
|
|
return (error) ? error : Z_STREAM_ERROR;
|
2016-09-19 12:31:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)
|
2016-09-21 12:05:01 +00:00
|
|
|
{
|
2017-06-03 00:10:49 +00:00
|
|
|
ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
|
2016-09-21 12:05:01 +00:00
|
|
|
strm->msg = message;
|
|
|
|
if (zwd == NULL) return Z_STREAM_ERROR;
|
2016-12-21 18:57:18 +00:00
|
|
|
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithError(zwd, strm, 0);
|
2016-09-21 12:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,
|
|
|
|
const char *version, int stream_size))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
|
2017-06-03 00:10:49 +00:00
|
|
|
strm->reserved = ZWRAP_ZLIB_STREAM;
|
2016-09-22 09:52:00 +00:00
|
|
|
return inflateInit(strm);
|
|
|
|
}
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
{ ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);
|
2017-06-02 21:01:21 +00:00
|
|
|
LOG_WRAPPERD("- inflateInit\n");
|
|
|
|
if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2017-06-02 21:01:21 +00:00
|
|
|
zwd->version = ZSTD_malloc(strlen(version)+1, zwd->customMem);
|
|
|
|
if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);
|
|
|
|
strcpy(zwd->version, version);
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2017-06-02 21:01:21 +00:00
|
|
|
zwd->stream_size = stream_size;
|
|
|
|
zwd->totalInBytes = 0;
|
2017-06-03 00:10:49 +00:00
|
|
|
strm->state = (struct internal_state*) zwd;
|
2017-06-02 21:01:21 +00:00
|
|
|
strm->total_in = 0;
|
|
|
|
strm->total_out = 0;
|
2017-06-03 00:10:49 +00:00
|
|
|
strm->reserved = ZWRAP_UNKNOWN_STREAM;
|
2017-06-02 21:01:21 +00:00
|
|
|
strm->adler = 0;
|
2016-09-22 09:52:00 +00:00
|
|
|
}
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,
|
|
|
|
const char *version, int stream_size))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {
|
|
|
|
return inflateInit2_(strm, windowBits, version, stream_size);
|
|
|
|
}
|
2016-12-21 18:57:18 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
{ int const ret = z_inflateInit_ (strm, version, stream_size);
|
2017-06-02 21:01:21 +00:00
|
|
|
LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);
|
|
|
|
if (ret == Z_OK) {
|
|
|
|
ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
|
|
|
|
if (zwd == NULL) return Z_STREAM_ERROR;
|
|
|
|
zwd->windowBits = windowBits;
|
|
|
|
}
|
|
|
|
return ret;
|
2016-09-22 09:52:00 +00:00
|
|
|
}
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 15:27:43 +00:00
|
|
|
int ZWRAP_inflateReset_keepDict(z_streamp strm)
|
2016-09-20 10:50:59 +00:00
|
|
|
{
|
2016-09-27 15:27:43 +00:00
|
|
|
LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");
|
2016-09-27 15:14:04 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
|
|
|
return inflateReset(strm);
|
|
|
|
|
2017-06-02 21:01:21 +00:00
|
|
|
{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
|
2016-09-20 13:18:00 +00:00
|
|
|
if (zwd == NULL) return Z_STREAM_ERROR;
|
2016-09-20 14:22:28 +00:00
|
|
|
ZWRAP_initDCtx(zwd);
|
2016-09-27 14:56:07 +00:00
|
|
|
zwd->decompState = ZWRAP_useReset;
|
2017-01-19 11:10:52 +00:00
|
|
|
zwd->totalInBytes = 0;
|
2016-09-20 13:18:00 +00:00
|
|
|
}
|
2016-12-21 18:57:18 +00:00
|
|
|
|
2016-09-20 10:50:59 +00:00
|
|
|
strm->total_in = 0;
|
|
|
|
strm->total_out = 0;
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-27 14:56:07 +00:00
|
|
|
ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))
|
|
|
|
{
|
|
|
|
LOG_WRAPPERD("- inflateReset\n");
|
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
|
|
|
return inflateReset(strm);
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
{ int const ret = ZWRAP_inflateReset_keepDict(strm);
|
2016-09-27 14:56:07 +00:00
|
|
|
if (ret != Z_OK) return ret; }
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
|
2016-12-21 18:57:18 +00:00
|
|
|
if (zwd == NULL) return Z_STREAM_ERROR;
|
2016-09-27 14:56:07 +00:00
|
|
|
zwd->decompState = ZWRAP_useInit; }
|
|
|
|
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-20 14:40:50 +00:00
|
|
|
#if ZLIB_VERNUM >= 0x1240
|
|
|
|
ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,
|
|
|
|
int windowBits))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-09-20 14:40:50 +00:00
|
|
|
return inflateReset2(strm, windowBits);
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
{ int const ret = z_inflateReset (strm);
|
2016-09-20 14:40:50 +00:00
|
|
|
if (ret == Z_OK) {
|
2017-06-03 00:10:49 +00:00
|
|
|
ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;
|
2016-09-20 14:40:50 +00:00
|
|
|
if (zwd == NULL) return Z_STREAM_ERROR;
|
|
|
|
zwd->windowBits = windowBits;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,
|
|
|
|
const Bytef *dictionary,
|
|
|
|
uInt dictLength))
|
|
|
|
{
|
2016-09-20 13:18:00 +00:00
|
|
|
LOG_WRAPPERD("- inflateSetDictionary\n");
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-05-12 15:15:41 +00:00
|
|
|
return inflateSetDictionary(strm, dictionary, dictLength);
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
|
2016-09-22 15:59:10 +00:00
|
|
|
if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;
|
2017-06-03 00:10:49 +00:00
|
|
|
{ size_t const initErr = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);
|
|
|
|
if (ZSTD_isError(initErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }
|
2016-12-21 18:57:18 +00:00
|
|
|
zwd->decompState = ZWRAP_useReset;
|
2016-08-30 17:04:33 +00:00
|
|
|
|
2017-01-19 11:10:52 +00:00
|
|
|
if (zwd->totalInBytes == ZSTD_HEADERSIZE) {
|
2016-09-16 12:06:10 +00:00
|
|
|
zwd->inBuffer.src = zwd->headerBuf;
|
2017-01-19 11:10:52 +00:00
|
|
|
zwd->inBuffer.size = zwd->totalInBytes;
|
2016-09-16 12:06:10 +00:00
|
|
|
zwd->inBuffer.pos = 0;
|
|
|
|
zwd->outBuffer.dst = strm->next_out;
|
|
|
|
zwd->outBuffer.size = 0;
|
|
|
|
zwd->outBuffer.pos = 0;
|
2017-06-03 00:10:49 +00:00
|
|
|
{ size_t const errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
|
|
|
|
LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n",
|
|
|
|
(int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
|
|
|
|
if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {
|
|
|
|
LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n",
|
|
|
|
ZSTD_getErrorName(errorCode));
|
|
|
|
return ZWRAPD_finishWithError(zwd, strm, 0);
|
|
|
|
} } } }
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-30 17:04:33 +00:00
|
|
|
ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))
|
2016-05-12 15:15:41 +00:00
|
|
|
{
|
2016-09-23 12:59:46 +00:00
|
|
|
ZWRAP_DCtx* zwd;
|
2017-06-03 00:10:49 +00:00
|
|
|
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
|
2017-06-03 00:10:49 +00:00
|
|
|
int const result = inflate(strm, flush);
|
|
|
|
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, result);
|
|
|
|
return result;
|
2016-09-20 13:18:00 +00:00
|
|
|
}
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2016-09-23 14:20:13 +00:00
|
|
|
if (strm->avail_in <= 0) return Z_OK;
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
zwd = (ZWRAP_DCtx*) strm->state;
|
|
|
|
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 14:22:28 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
if (zwd == NULL) return Z_STREAM_ERROR;
|
|
|
|
if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;
|
|
|
|
|
|
|
|
if (zwd->totalInBytes < ZLIB_HEADERSIZE) {
|
|
|
|
if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {
|
|
|
|
if (MEM_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {
|
|
|
|
{ int const initErr = (zwd->windowBits) ?
|
|
|
|
inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
|
|
|
|
inflateInit_(strm, zwd->version, zwd->stream_size);
|
|
|
|
LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
|
|
|
|
if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
|
2016-09-23 12:59:46 +00:00
|
|
|
}
|
2016-09-16 15:14:01 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
strm->reserved = ZWRAP_ZLIB_STREAM;
|
|
|
|
{ size_t const freeErr = ZWRAP_freeDCtx(zwd);
|
|
|
|
if (ZSTD_isError(freeErr)) goto error; }
|
|
|
|
|
|
|
|
{ int const result = (flush == Z_INFLATE_SYNC) ?
|
|
|
|
inflateSync(strm) :
|
|
|
|
inflate(strm, flush);
|
|
|
|
LOG_WRAPPERD("- inflate3 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);
|
|
|
|
return result;
|
|
|
|
} }
|
|
|
|
} else { /* ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
|
|
|
|
size_t const srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);
|
|
|
|
memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
|
|
|
|
strm->total_in += srcSize;
|
|
|
|
zwd->totalInBytes += srcSize;
|
|
|
|
strm->next_in += srcSize;
|
|
|
|
strm->avail_in -= srcSize;
|
|
|
|
if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
{ int const initErr = (zwd->windowBits) ?
|
|
|
|
inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :
|
|
|
|
inflateInit_(strm, zwd->version, zwd->stream_size);
|
|
|
|
LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);
|
|
|
|
if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);
|
|
|
|
}
|
2016-09-23 12:59:46 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
/* inflate header */
|
|
|
|
strm->next_in = (unsigned char*)zwd->headerBuf;
|
|
|
|
strm->avail_in = ZLIB_HEADERSIZE;
|
|
|
|
strm->avail_out = 0;
|
|
|
|
{ int const dErr = inflate(strm, Z_NO_FLUSH);
|
|
|
|
LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n",
|
|
|
|
dErr, (int)strm->avail_in);
|
|
|
|
if (dErr != Z_OK)
|
|
|
|
return ZWRAPD_finishWithError(zwd, strm, dErr);
|
|
|
|
}
|
|
|
|
if (strm->avail_in > 0) goto error;
|
|
|
|
|
|
|
|
strm->next_in = strm2.next_in;
|
|
|
|
strm->avail_in = strm2.avail_in;
|
|
|
|
strm->next_out = strm2.next_out;
|
|
|
|
strm->avail_out = strm2.avail_out;
|
|
|
|
|
|
|
|
strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */
|
|
|
|
{ size_t const freeErr = ZWRAP_freeDCtx(zwd);
|
|
|
|
if (ZSTD_isError(freeErr)) goto error; }
|
|
|
|
|
|
|
|
{ int const result = (flush == Z_INFLATE_SYNC) ?
|
|
|
|
inflateSync(strm) :
|
|
|
|
inflate(strm, flush);
|
|
|
|
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);
|
|
|
|
return result;
|
|
|
|
} } } /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */
|
|
|
|
} /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */
|
|
|
|
|
|
|
|
strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */
|
|
|
|
|
|
|
|
if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }
|
|
|
|
|
|
|
|
if (!zwd->zbd) {
|
|
|
|
zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);
|
|
|
|
if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }
|
|
|
|
zwd->decompState = ZWRAP_useInit;
|
|
|
|
}
|
2016-09-22 15:59:10 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
if (zwd->totalInBytes < ZSTD_HEADERSIZE) {
|
|
|
|
if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {
|
|
|
|
if (zwd->decompState == ZWRAP_useInit) {
|
|
|
|
size_t const initErr = ZSTD_initDStream(zwd->zbd);
|
|
|
|
if (ZSTD_isError(initErr)) {
|
|
|
|
LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
|
|
|
|
ZSTD_getErrorName(initErr));
|
|
|
|
goto error;
|
2016-09-23 12:59:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
2017-06-03 00:10:49 +00:00
|
|
|
size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
|
|
|
|
if (ZSTD_isError(resetErr)) goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_t const srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);
|
|
|
|
memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);
|
|
|
|
strm->total_in += srcSize;
|
|
|
|
zwd->totalInBytes += srcSize;
|
|
|
|
strm->next_in += srcSize;
|
|
|
|
strm->avail_in -= srcSize;
|
|
|
|
if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;
|
|
|
|
|
|
|
|
if (zwd->decompState == ZWRAP_useInit) {
|
|
|
|
size_t const initErr = ZSTD_initDStream(zwd->zbd);
|
|
|
|
if (ZSTD_isError(initErr)) {
|
|
|
|
LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",
|
|
|
|
ZSTD_getErrorName(initErr));
|
2016-09-23 12:59:46 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2017-06-03 00:10:49 +00:00
|
|
|
} else {
|
|
|
|
size_t const resetErr = ZSTD_resetDStream(zwd->zbd);
|
|
|
|
if (ZSTD_isError(resetErr)) goto error;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2017-06-03 00:10:49 +00:00
|
|
|
|
|
|
|
zwd->inBuffer.src = zwd->headerBuf;
|
|
|
|
zwd->inBuffer.size = ZSTD_HEADERSIZE;
|
|
|
|
zwd->inBuffer.pos = 0;
|
|
|
|
zwd->outBuffer.dst = strm->next_out;
|
|
|
|
zwd->outBuffer.size = 0;
|
|
|
|
zwd->outBuffer.pos = 0;
|
|
|
|
{ size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
|
|
|
|
LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n",
|
|
|
|
(int)dErr, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);
|
|
|
|
if (ZSTD_isError(dErr)) {
|
|
|
|
LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(dErr));
|
|
|
|
goto error;
|
|
|
|
} }
|
|
|
|
if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2017-06-03 00:10:49 +00:00
|
|
|
} /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */
|
2016-05-12 15:15:41 +00:00
|
|
|
|
2017-06-03 00:10:49 +00: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;
|
|
|
|
{ size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);
|
|
|
|
LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n",
|
|
|
|
(int)dErr, (int)strm->avail_in, (int)strm->avail_out);
|
|
|
|
if (ZSTD_isError(dErr)) {
|
2016-05-12 15:15:41 +00:00
|
|
|
zwd->errorCount++;
|
2017-06-03 00:10:49 +00:00
|
|
|
LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",
|
|
|
|
ZSTD_getErrorName(dErr), zwd->errorCount);
|
2016-06-13 10:00:46 +00:00
|
|
|
if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2017-06-03 00:10:49 +00:00
|
|
|
LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n",
|
|
|
|
(int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);
|
2016-09-16 12:06:10 +00:00
|
|
|
strm->next_out += zwd->outBuffer.pos;
|
|
|
|
strm->total_out += zwd->outBuffer.pos;
|
|
|
|
strm->avail_out -= zwd->outBuffer.pos;
|
2016-09-22 15:59:10 +00:00
|
|
|
strm->total_in += zwd->inBuffer.pos;
|
2017-01-19 11:10:52 +00:00
|
|
|
zwd->totalInBytes += zwd->inBuffer.pos;
|
2016-09-22 15:59:10 +00:00
|
|
|
strm->next_in += zwd->inBuffer.pos;
|
|
|
|
strm->avail_in -= zwd->inBuffer.pos;
|
2017-06-03 00:10:49 +00:00
|
|
|
if (dErr == 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);
|
2016-12-21 18:57:18 +00:00
|
|
|
zwd->decompState = ZWRAP_streamEnd;
|
2016-09-20 14:22:28 +00:00
|
|
|
return Z_STREAM_END;
|
|
|
|
}
|
2017-06-03 00:10:49 +00:00
|
|
|
} /* dErr lifetime */
|
|
|
|
|
|
|
|
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 15:15:41 +00:00
|
|
|
return Z_OK;
|
2016-09-23 12:59:46 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
return ZWRAPD_finishWithError(zwd, strm, 0);
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-06-02 10:00:32 +00:00
|
|
|
return inflateEnd(strm);
|
2016-08-30 17:04:33 +00:00
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n",
|
|
|
|
(int)(strm->total_in), (int)(strm->total_out));
|
|
|
|
{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;
|
2016-09-21 11:51:57 +00:00
|
|
|
if (zwd == NULL) return Z_OK; /* structures are already freed */
|
2017-06-03 00:10:49 +00:00
|
|
|
{ size_t const freeErr = ZWRAP_freeDCtx(zwd);
|
|
|
|
if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }
|
2016-06-13 10:00:46 +00:00
|
|
|
strm->state = NULL;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2016-09-21 11:51:57 +00:00
|
|
|
return Z_OK;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {
|
2016-09-20 10:50:59 +00:00
|
|
|
return inflateSync(strm);
|
|
|
|
}
|
2016-09-19 12:27:29 +00:00
|
|
|
|
2016-05-12 15:15:41 +00:00
|
|
|
return z_inflate(strm, Z_INFLATE_SYNC);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Advanced compression functions */
|
|
|
|
ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,
|
|
|
|
z_streamp source))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateCopy(dest, source);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,
|
|
|
|
int good_length,
|
|
|
|
int max_lazy,
|
|
|
|
int nice_length,
|
|
|
|
int max_chain))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-02 08:19:35 +00:00
|
|
|
#if ZLIB_VERNUM >= 0x1260
|
2016-05-12 15:15:41 +00:00
|
|
|
ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,
|
|
|
|
unsigned *pending,
|
|
|
|
int *bits))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflatePending(strm, pending, bits);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2016-06-02 08:19:35 +00:00
|
|
|
#endif
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,
|
|
|
|
int bits,
|
|
|
|
int value))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflatePrime(strm, bits, value);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,
|
|
|
|
gz_headerp head))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return deflateSetHeader(strm, head);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-09-19 12:27:29 +00:00
|
|
|
/* Advanced decompression functions */
|
2016-06-02 08:19:35 +00:00
|
|
|
#if ZLIB_VERNUM >= 0x1280
|
2016-05-12 15:15:41 +00:00
|
|
|
ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,
|
|
|
|
Bytef *dictionary,
|
|
|
|
uInt *dictLength))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-05-12 15:15:41 +00:00
|
|
|
return inflateGetDictionary(strm, dictionary, dictLength);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2016-06-02 08:19:35 +00:00
|
|
|
#endif
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,
|
|
|
|
z_streamp source))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)
|
2016-05-12 15:15:41 +00:00
|
|
|
return inflateCopy(dest, source);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-02 08:19:35 +00:00
|
|
|
#if ZLIB_VERNUM >= 0x1240
|
|
|
|
ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))
|
2016-05-12 15:15:41 +00:00
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-06-02 08:19:35 +00:00
|
|
|
return inflateMark(strm);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
2016-06-02 08:19:35 +00:00
|
|
|
#endif
|
2016-05-12 15:15:41 +00:00
|
|
|
|
|
|
|
|
2016-06-02 08:19:35 +00:00
|
|
|
ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,
|
|
|
|
int bits,
|
|
|
|
int value))
|
2016-05-12 15:15:41 +00:00
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-06-02 08:19:35 +00:00
|
|
|
return inflatePrime(strm, bits, value);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,
|
|
|
|
gz_headerp head))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-05-12 15:15:41 +00:00
|
|
|
return inflateGetHeader(strm, head);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,
|
|
|
|
unsigned char FAR *window,
|
|
|
|
const char *version,
|
|
|
|
int stream_size))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-05-12 15:15:41 +00:00
|
|
|
return inflateBackInit_(strm, windowBits, window, version, stream_size);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,
|
|
|
|
in_func in, void FAR *in_desc,
|
|
|
|
out_func out, void FAR *out_desc))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-05-12 15:15:41 +00:00
|
|
|
return inflateBack(strm, in, in_desc, out, out_desc);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))
|
|
|
|
{
|
2016-09-22 09:52:00 +00:00
|
|
|
if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)
|
2016-05-12 15:15:41 +00:00
|
|
|
return inflateBackEnd(strm);
|
2016-09-21 17:39:25 +00:00
|
|
|
return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); };
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
/* === utility functions === */
|
2016-05-12 15:15:41 +00:00
|
|
|
#ifndef Z_SOLO
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,
|
|
|
|
const Bytef *source, uLong sourceLen))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return compress(dest, destLen, source, sourceLen);
|
|
|
|
|
2017-06-03 00:10:49 +00:00
|
|
|
{ size_t dstCapacity = *destLen;
|
|
|
|
size_t const cSize = ZSTD_compress(dest, dstCapacity,
|
|
|
|
source, sourceLen,
|
|
|
|
ZWRAP_DEFAULT_CLEVEL);
|
|
|
|
LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n",
|
|
|
|
(int)sourceLen, (int)dstCapacity);
|
|
|
|
if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
|
|
|
|
*destLen = cSize;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest, uLongf *destLen,
|
|
|
|
const Bytef *source, uLong sourceLen,
|
|
|
|
int level))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return compress2(dest, destLen, source, sourceLen, level);
|
2016-08-30 17:04:33 +00:00
|
|
|
|
|
|
|
{ size_t dstCapacity = *destLen;
|
2017-06-02 21:24:58 +00:00
|
|
|
size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
|
|
|
|
if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
|
|
|
|
*destLen = cSize;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
|
|
|
|
{
|
2016-09-23 07:08:40 +00:00
|
|
|
if (!g_ZWRAP_useZSTDcompression)
|
2016-05-12 15:15:41 +00:00
|
|
|
return compressBound(sourceLen);
|
|
|
|
|
|
|
|
return ZSTD_compressBound(sourceLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest, uLongf *destLen,
|
|
|
|
const Bytef *source, uLong sourceLen))
|
|
|
|
{
|
2017-06-02 21:24:58 +00:00
|
|
|
if (!ZSTD_isFrame(source, sourceLen))
|
2016-05-12 15:15:41 +00:00
|
|
|
return uncompress(dest, destLen, source, sourceLen);
|
|
|
|
|
2016-08-30 17:04:33 +00:00
|
|
|
{ size_t dstCapacity = *destLen;
|
2017-06-02 21:24:58 +00:00
|
|
|
size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
|
|
|
|
if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;
|
|
|
|
*destLen = dSize;
|
2016-05-12 15:15:41 +00:00
|
|
|
}
|
|
|
|
return Z_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
#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);
|
|
|
|
}
|
2017-01-17 12:02:06 +00:00
|
|
|
|
2017-01-18 11:51:44 +00:00
|
|
|
|
|
|
|
#if ZLIB_VERNUM >= 0x12B0
|
|
|
|
ZEXTERN uLong ZEXPORT z_adler32_z OF((uLong adler, const Bytef *buf, z_size_t len))
|
|
|
|
{
|
|
|
|
return adler32_z(adler, buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
ZEXTERN uLong ZEXPORT z_crc32_z OF((uLong crc, const Bytef *buf, z_size_t len))
|
|
|
|
{
|
|
|
|
return crc32_z(crc, buf, len);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-01-18 09:39:39 +00:00
|
|
|
#if ZLIB_VERNUM >= 0x1270
|
2017-01-17 12:02:06 +00:00
|
|
|
ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table OF((void))
|
|
|
|
{
|
|
|
|
return get_crc_table();
|
|
|
|
}
|
2017-01-18 09:39:39 +00:00
|
|
|
#endif
|