Path: blob/main/sys/contrib/zstd/zlibWrapper/zstd_zlibwrapper.c
48254 views
/*1* Copyright (c) 2016-2021, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under both the BSD-style license (found in the5* LICENSE file in the root directory of this source tree) and the GPLv2 (found6* in the COPYING file in the root directory of this source tree).7* You may select, at your option, one of the above-listed licenses.8*/91011/* === Tuning parameters === */12#ifndef ZWRAP_USE_ZSTD13#define ZWRAP_USE_ZSTD 014#endif151617/* === Dependencies === */18#include <stdlib.h>19#include <stdio.h> /* vsprintf */20#include <stdarg.h> /* va_list, for z_gzprintf */21#include <string.h>22#define NO_DUMMY_DECL23#define ZLIB_CONST24#include <zlib.h> /* without #define Z_PREFIX */25#include "zstd_zlibwrapper.h"26#define ZSTD_STATIC_LINKING_ONLY /* ZSTD_isFrame, ZSTD_MAGICNUMBER, ZSTD_customMem */27#include "zstd.h"282930/* === Constants === */31#define Z_INFLATE_SYNC 832#define ZLIB_HEADERSIZE 433#define ZSTD_HEADERSIZE ZSTD_FRAMEHEADERSIZE_MIN(ZSTD_f_zstd1)34#define ZWRAP_DEFAULT_CLEVEL 3 /* Z_DEFAULT_COMPRESSION is translated to ZWRAP_DEFAULT_CLEVEL for zstd */353637/* === Debug === */38#define LOG_WRAPPERC(...) /* fprintf(stderr, __VA_ARGS__) */39#define LOG_WRAPPERD(...) /* fprintf(stderr, __VA_ARGS__) */4041#define FINISH_WITH_GZ_ERR(msg) { (void)msg; return Z_STREAM_ERROR; }42#define FINISH_WITH_NULL_ERR(msg) { (void)msg; return NULL; }4344/* === Utility === */4546#define MIN(x,y) ((x) < (y) ? (x) : (y))4748static unsigned ZWRAP_isLittleEndian(void)49{50const union { unsigned u; char c[4]; } one = { 1 }; /* don't use static : performance detrimental */51return one.c[0];52}5354#ifndef __has_builtin55# define __has_builtin(x) 056#endif5758static unsigned ZWRAP_swap32(unsigned in)59{60#if defined(_MSC_VER) /* Visual Studio */61return _byteswap_ulong(in);62#elif (defined (__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 403)) \63|| (defined(__clang__) && __has_builtin(__builtin_bswap32))64return __builtin_bswap32(in);65#else66return ((in << 24) & 0xff000000 ) |67((in << 8) & 0x00ff0000 ) |68((in >> 8) & 0x0000ff00 ) |69((in >> 24) & 0x000000ff );70#endif71}7273static unsigned ZWRAP_readLE32(const void* ptr)74{75unsigned value;76memcpy(&value, ptr, sizeof(value));77if (ZWRAP_isLittleEndian())78return value;79else80return ZWRAP_swap32(value);81}828384/* === Wrapper === */85static int g_ZWRAP_useZSTDcompression = ZWRAP_USE_ZSTD; /* 0 = don't use ZSTD */8687void ZWRAP_useZSTDcompression(int turn_on) { g_ZWRAP_useZSTDcompression = turn_on; }8889int ZWRAP_isUsingZSTDcompression(void) { return g_ZWRAP_useZSTDcompression; }90919293static ZWRAP_decompress_type g_ZWRAPdecompressionType = ZWRAP_AUTO;9495void ZWRAP_setDecompressionType(ZWRAP_decompress_type type) { g_ZWRAPdecompressionType = type; }9697ZWRAP_decompress_type ZWRAP_getDecompressionType(void) { return g_ZWRAPdecompressionType; }9899100101const char * zstdVersion(void) { return ZSTD_VERSION_STRING; }102103ZEXTERN const char * ZEXPORT z_zlibVersion OF((void)) { return zlibVersion(); }104105static void* ZWRAP_allocFunction(void* opaque, size_t size)106{107z_streamp strm = (z_streamp) opaque;108void* address = strm->zalloc(strm->opaque, 1, (uInt)size);109/* LOG_WRAPPERC("ZWRAP alloc %p, %d \n", address, (int)size); */110return address;111}112113static void ZWRAP_freeFunction(void* opaque, void* address)114{115z_streamp strm = (z_streamp) opaque;116strm->zfree(strm->opaque, address);117/* if (address) LOG_WRAPPERC("ZWRAP free %p \n", address); */118}119120static void* ZWRAP_customMalloc(size_t size, ZSTD_customMem customMem)121{122if (customMem.customAlloc)123return customMem.customAlloc(customMem.opaque, size);124return malloc(size);125}126127static void* ZWRAP_customCalloc(size_t size, ZSTD_customMem customMem)128{129if (customMem.customAlloc) {130/* calloc implemented as malloc+memset;131* not as efficient as calloc, but next best guess for custom malloc */132void* const ptr = customMem.customAlloc(customMem.opaque, size);133memset(ptr, 0, size);134return ptr;135}136return calloc(1, size);137}138139static void ZWRAP_customFree(void* ptr, ZSTD_customMem customMem)140{141if (ptr!=NULL) {142if (customMem.customFree)143customMem.customFree(customMem.opaque, ptr);144else145free(ptr);146}147}148149150151/* === Compression === */152typedef enum { ZWRAP_useInit, ZWRAP_useReset, ZWRAP_streamEnd } ZWRAP_state_t;153154typedef struct {155ZSTD_CStream* zbc;156int compressionLevel;157int streamEnd; /* a flag to signal the end of a stream */158unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */159ZSTD_customMem customMem;160z_stream allocFunc; /* copy of zalloc, zfree, opaque */161ZSTD_inBuffer inBuffer;162ZSTD_outBuffer outBuffer;163ZWRAP_state_t comprState;164unsigned long long pledgedSrcSize;165} ZWRAP_CCtx;166167/* typedef ZWRAP_CCtx internal_state; */168169170171static size_t ZWRAP_freeCCtx(ZWRAP_CCtx* zwc)172{173if (zwc==NULL) return 0; /* support free on NULL */174ZSTD_freeCStream(zwc->zbc);175ZWRAP_customFree(zwc, zwc->customMem);176return 0;177}178179180static ZWRAP_CCtx* ZWRAP_createCCtx(z_streamp strm)181{182ZWRAP_CCtx* zwc;183ZSTD_customMem customMem = { NULL, NULL, NULL };184185if (strm->zalloc && strm->zfree) {186customMem.customAlloc = ZWRAP_allocFunction;187customMem.customFree = ZWRAP_freeFunction;188}189customMem.opaque = strm;190191zwc = (ZWRAP_CCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_CCtx), customMem);192if (zwc == NULL) return NULL;193zwc->allocFunc = *strm;194customMem.opaque = &zwc->allocFunc;195zwc->customMem = customMem;196197return zwc;198}199200201static int ZWRAP_initializeCStream(ZWRAP_CCtx* zwc, const void* dict, size_t dictSize, unsigned long long pledgedSrcSize)202{203LOG_WRAPPERC("- ZWRAP_initializeCStream=%p\n", zwc);204if (zwc == NULL || zwc->zbc == NULL) return Z_STREAM_ERROR;205206if (!pledgedSrcSize) pledgedSrcSize = zwc->pledgedSrcSize;207{ unsigned initErr = 0;208ZSTD_parameters const params = ZSTD_getParams(zwc->compressionLevel, pledgedSrcSize, dictSize);209ZSTD_CCtx_params* cctxParams = ZSTD_createCCtxParams();210if (!cctxParams) return Z_STREAM_ERROR;211LOG_WRAPPERC("pledgedSrcSize=%d windowLog=%d chainLog=%d hashLog=%d searchLog=%d minMatch=%d strategy=%d\n",212(int)pledgedSrcSize, params.cParams.windowLog, params.cParams.chainLog, params.cParams.hashLog, params.cParams.searchLog, params.cParams.minMatch, params.cParams.strategy);213214initErr |= ZSTD_isError(ZSTD_CCtx_reset(zwc->zbc, ZSTD_reset_session_only));215initErr |= ZSTD_isError(ZSTD_CCtxParams_init_advanced(cctxParams, params));216initErr |= ZSTD_isError(ZSTD_CCtx_setParametersUsingCCtxParams(zwc->zbc, cctxParams));217initErr |= ZSTD_isError(ZSTD_CCtx_setPledgedSrcSize(zwc->zbc, pledgedSrcSize));218initErr |= ZSTD_isError(ZSTD_CCtx_loadDictionary(zwc->zbc, dict, dictSize));219220ZSTD_freeCCtxParams(cctxParams);221if (initErr) return Z_STREAM_ERROR;222}223224return Z_OK;225}226227228static int ZWRAPC_finishWithError(ZWRAP_CCtx* zwc, z_streamp strm, int error)229{230LOG_WRAPPERC("- ZWRAPC_finishWithError=%d\n", error);231if (zwc) ZWRAP_freeCCtx(zwc);232if (strm) strm->state = NULL;233return (error) ? error : Z_STREAM_ERROR;234}235236237static int ZWRAPC_finishWithErrorMsg(z_streamp strm, char* message)238{239ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;240strm->msg = message;241if (zwc == NULL) return Z_STREAM_ERROR;242243return ZWRAPC_finishWithError(zwc, strm, 0);244}245246247int ZWRAP_setPledgedSrcSize(z_streamp strm, unsigned long long pledgedSrcSize)248{249ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;250if (zwc == NULL) return Z_STREAM_ERROR;251252zwc->pledgedSrcSize = pledgedSrcSize;253zwc->comprState = ZWRAP_useInit;254return Z_OK;255}256257static struct internal_state* convert_into_sis(void* ptr)258{259return (struct internal_state*) ptr;260}261262ZEXTERN int ZEXPORT z_deflateInit_ OF((z_streamp strm, int level,263const char *version, int stream_size))264{265ZWRAP_CCtx* zwc;266267LOG_WRAPPERC("- deflateInit level=%d\n", level);268if (!g_ZWRAP_useZSTDcompression) {269return deflateInit_((strm), (level), version, stream_size);270}271272zwc = ZWRAP_createCCtx(strm);273if (zwc == NULL) return Z_MEM_ERROR;274275if (level == Z_DEFAULT_COMPRESSION)276level = ZWRAP_DEFAULT_CLEVEL;277278zwc->streamEnd = 0;279zwc->totalInBytes = 0;280zwc->compressionLevel = level;281strm->state = convert_into_sis(zwc); /* use state which in not used by user */282strm->total_in = 0;283strm->total_out = 0;284strm->adler = 0;285return Z_OK;286}287288289ZEXTERN int ZEXPORT z_deflateInit2_ OF((z_streamp strm, int level, int method,290int windowBits, int memLevel,291int strategy, const char *version,292int stream_size))293{294if (!g_ZWRAP_useZSTDcompression)295return deflateInit2_(strm, level, method, windowBits, memLevel, strategy, version, stream_size);296297return z_deflateInit_ (strm, level, version, stream_size);298}299300301int ZWRAP_deflateReset_keepDict(z_streamp strm)302{303LOG_WRAPPERC("- ZWRAP_deflateReset_keepDict\n");304if (!g_ZWRAP_useZSTDcompression)305return deflateReset(strm);306307{ ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;308if (zwc) {309zwc->streamEnd = 0;310zwc->totalInBytes = 0;311}312}313314strm->total_in = 0;315strm->total_out = 0;316strm->adler = 0;317return Z_OK;318}319320321ZEXTERN int ZEXPORT z_deflateReset OF((z_streamp strm))322{323LOG_WRAPPERC("- deflateReset\n");324if (!g_ZWRAP_useZSTDcompression)325return deflateReset(strm);326327ZWRAP_deflateReset_keepDict(strm);328329{ ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;330if (zwc) zwc->comprState = ZWRAP_useInit;331}332return Z_OK;333}334335336ZEXTERN int ZEXPORT z_deflateSetDictionary OF((z_streamp strm,337const Bytef *dictionary,338uInt dictLength))339{340if (!g_ZWRAP_useZSTDcompression) {341LOG_WRAPPERC("- deflateSetDictionary\n");342return deflateSetDictionary(strm, dictionary, dictLength);343}344345{ ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;346LOG_WRAPPERC("- deflateSetDictionary level=%d\n", (int)zwc->compressionLevel);347if (!zwc) return Z_STREAM_ERROR;348if (zwc->zbc == NULL) {349zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);350if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);351}352{ int res = ZWRAP_initializeCStream(zwc, dictionary, dictLength, ZSTD_CONTENTSIZE_UNKNOWN);353if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res); }354zwc->comprState = ZWRAP_useReset;355}356357return Z_OK;358}359360361ZEXTERN int ZEXPORT z_deflate OF((z_streamp strm, int flush))362{363ZWRAP_CCtx* zwc;364365if (!g_ZWRAP_useZSTDcompression) {366LOG_WRAPPERC("- deflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",367(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);368return deflate(strm, flush);369}370371zwc = (ZWRAP_CCtx*) strm->state;372if (zwc == NULL) { LOG_WRAPPERC("zwc == NULL\n"); return Z_STREAM_ERROR; }373374if (zwc->zbc == NULL) {375zwc->zbc = ZSTD_createCStream_advanced(zwc->customMem);376if (zwc->zbc == NULL) return ZWRAPC_finishWithError(zwc, strm, 0);377{ int const initErr = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);378if (initErr != Z_OK) return ZWRAPC_finishWithError(zwc, strm, initErr); }379if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;380} else {381if (zwc->totalInBytes == 0) {382if (zwc->comprState == ZWRAP_useReset) {383size_t resetErr = ZSTD_CCtx_reset(zwc->zbc, ZSTD_reset_session_only);384if (ZSTD_isError(resetErr)) {385LOG_WRAPPERC("ERROR: ZSTD_CCtx_reset errorCode=%s\n",386ZSTD_getErrorName(resetErr));387return ZWRAPC_finishWithError(zwc, strm, 0);388}389resetErr = ZSTD_CCtx_setPledgedSrcSize(zwc->zbc, (flush == Z_FINISH) ? strm->avail_in : zwc->pledgedSrcSize);390if (ZSTD_isError(resetErr)) {391LOG_WRAPPERC("ERROR: ZSTD_CCtx_setPledgedSrcSize errorCode=%s\n",392ZSTD_getErrorName(resetErr));393return ZWRAPC_finishWithError(zwc, strm, 0);394}395} else {396int const res = ZWRAP_initializeCStream(zwc, NULL, 0, (flush == Z_FINISH) ? strm->avail_in : ZSTD_CONTENTSIZE_UNKNOWN);397if (res != Z_OK) return ZWRAPC_finishWithError(zwc, strm, res);398if (flush != Z_FINISH) zwc->comprState = ZWRAP_useReset;399}400} /* (zwc->totalInBytes == 0) */401} /* ! (zwc->zbc == NULL) */402403LOG_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);404if (strm->avail_in > 0) {405zwc->inBuffer.src = strm->next_in;406zwc->inBuffer.size = strm->avail_in;407zwc->inBuffer.pos = 0;408zwc->outBuffer.dst = strm->next_out;409zwc->outBuffer.size = strm->avail_out;410zwc->outBuffer.pos = 0;411{ size_t const cErr = ZSTD_compressStream(zwc->zbc, &zwc->outBuffer, &zwc->inBuffer);412LOG_WRAPPERC("deflate ZSTD_compressStream srcSize=%d dstCapacity=%d\n", (int)zwc->inBuffer.size, (int)zwc->outBuffer.size);413if (ZSTD_isError(cErr)) return ZWRAPC_finishWithError(zwc, strm, 0);414}415strm->next_out += zwc->outBuffer.pos;416strm->total_out += zwc->outBuffer.pos;417strm->avail_out -= zwc->outBuffer.pos;418strm->total_in += zwc->inBuffer.pos;419zwc->totalInBytes += zwc->inBuffer.pos;420strm->next_in += zwc->inBuffer.pos;421strm->avail_in -= zwc->inBuffer.pos;422}423424if (flush == Z_FULL_FLUSH425#if ZLIB_VERNUM >= 0x1240426|| flush == Z_TREES427#endif428|| flush == Z_BLOCK)429return ZWRAPC_finishWithErrorMsg(strm, "Z_FULL_FLUSH, Z_BLOCK and Z_TREES are not supported!");430431if (flush == Z_FINISH) {432size_t bytesLeft;433if (zwc->streamEnd) return Z_STREAM_END;434zwc->outBuffer.dst = strm->next_out;435zwc->outBuffer.size = strm->avail_out;436zwc->outBuffer.pos = 0;437bytesLeft = ZSTD_endStream(zwc->zbc, &zwc->outBuffer);438LOG_WRAPPERC("deflate ZSTD_endStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);439if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);440strm->next_out += zwc->outBuffer.pos;441strm->total_out += zwc->outBuffer.pos;442strm->avail_out -= zwc->outBuffer.pos;443if (bytesLeft == 0) {444zwc->streamEnd = 1;445LOG_WRAPPERC("Z_STREAM_END2 strm->total_in=%d strm->avail_out=%d strm->total_out=%d\n",446(int)strm->total_in, (int)strm->avail_out, (int)strm->total_out);447return Z_STREAM_END;448} }449else450if (flush == Z_SYNC_FLUSH || flush == Z_PARTIAL_FLUSH) {451size_t bytesLeft;452zwc->outBuffer.dst = strm->next_out;453zwc->outBuffer.size = strm->avail_out;454zwc->outBuffer.pos = 0;455bytesLeft = ZSTD_flushStream(zwc->zbc, &zwc->outBuffer);456LOG_WRAPPERC("deflate ZSTD_flushStream dstCapacity=%d bytesLeft=%d\n", (int)strm->avail_out, (int)bytesLeft);457if (ZSTD_isError(bytesLeft)) return ZWRAPC_finishWithError(zwc, strm, 0);458strm->next_out += zwc->outBuffer.pos;459strm->total_out += zwc->outBuffer.pos;460strm->avail_out -= zwc->outBuffer.pos;461}462LOG_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);463return Z_OK;464}465466467ZEXTERN int ZEXPORT z_deflateEnd OF((z_streamp strm))468{469if (!g_ZWRAP_useZSTDcompression) {470LOG_WRAPPERC("- deflateEnd\n");471return deflateEnd(strm);472}473LOG_WRAPPERC("- deflateEnd total_in=%d total_out=%d\n", (int)(strm->total_in), (int)(strm->total_out));474{ size_t errorCode;475ZWRAP_CCtx* zwc = (ZWRAP_CCtx*) strm->state;476if (zwc == NULL) return Z_OK; /* structures are already freed */477strm->state = NULL;478errorCode = ZWRAP_freeCCtx(zwc);479if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;480}481return Z_OK;482}483484485ZEXTERN uLong ZEXPORT z_deflateBound OF((z_streamp strm,486uLong sourceLen))487{488if (!g_ZWRAP_useZSTDcompression)489return deflateBound(strm, sourceLen);490491return ZSTD_compressBound(sourceLen);492}493494495ZEXTERN int ZEXPORT z_deflateParams OF((z_streamp strm,496int level,497int strategy))498{499if (!g_ZWRAP_useZSTDcompression) {500LOG_WRAPPERC("- deflateParams level=%d strategy=%d\n", level, strategy);501return deflateParams(strm, level, strategy);502}503504return Z_OK;505}506507508509510511/* === Decompression === */512513typedef enum { ZWRAP_ZLIB_STREAM, ZWRAP_ZSTD_STREAM, ZWRAP_UNKNOWN_STREAM } ZWRAP_stream_type;514515typedef struct {516ZSTD_DStream* zbd;517char headerBuf[16]; /* must be >= ZSTD_frameHeaderSize_min */518int errorCount;519unsigned long long totalInBytes; /* we need it as strm->total_in can be reset by user */520ZWRAP_state_t decompState;521ZSTD_inBuffer inBuffer;522ZSTD_outBuffer outBuffer;523524/* zlib params */525int stream_size;526char *version;527int windowBits;528ZSTD_customMem customMem;529z_stream allocFunc; /* just to copy zalloc, zfree, opaque */530} ZWRAP_DCtx;531532533static void ZWRAP_initDCtx(ZWRAP_DCtx* zwd)534{535zwd->errorCount = 0;536zwd->outBuffer.pos = 0;537zwd->outBuffer.size = 0;538}539540static ZWRAP_DCtx* ZWRAP_createDCtx(z_streamp strm)541{542ZWRAP_DCtx* zwd;543ZSTD_customMem customMem = { NULL, NULL, NULL };544545if (strm->zalloc && strm->zfree) {546customMem.customAlloc = ZWRAP_allocFunction;547customMem.customFree = ZWRAP_freeFunction;548}549customMem.opaque = strm;550551zwd = (ZWRAP_DCtx*)ZWRAP_customCalloc(sizeof(ZWRAP_DCtx), customMem);552if (zwd == NULL) return NULL;553zwd->allocFunc = *strm;554customMem.opaque = &zwd->allocFunc;555zwd->customMem = customMem;556557ZWRAP_initDCtx(zwd);558return zwd;559}560561static size_t ZWRAP_freeDCtx(ZWRAP_DCtx* zwd)562{563if (zwd==NULL) return 0; /* support free on null */564ZSTD_freeDStream(zwd->zbd);565ZWRAP_customFree(zwd->version, zwd->customMem);566ZWRAP_customFree(zwd, zwd->customMem);567return 0;568}569570571int ZWRAP_isUsingZSTDdecompression(z_streamp strm)572{573if (strm == NULL) return 0;574return (strm->reserved == ZWRAP_ZSTD_STREAM);575}576577578static int ZWRAPD_finishWithError(ZWRAP_DCtx* zwd, z_streamp strm, int error)579{580LOG_WRAPPERD("- ZWRAPD_finishWithError=%d\n", error);581ZWRAP_freeDCtx(zwd);582strm->state = NULL;583return (error) ? error : Z_STREAM_ERROR;584}585586static int ZWRAPD_finishWithErrorMsg(z_streamp strm, char* message)587{588ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;589strm->msg = message;590if (zwd == NULL) return Z_STREAM_ERROR;591592return ZWRAPD_finishWithError(zwd, strm, 0);593}594595596ZEXTERN int ZEXPORT z_inflateInit_ OF((z_streamp strm,597const char* version, int stream_size))598{599if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {600strm->reserved = ZWRAP_ZLIB_STREAM;601return inflateInit(strm);602}603604{ ZWRAP_DCtx* const zwd = ZWRAP_createDCtx(strm);605LOG_WRAPPERD("- inflateInit\n");606if (zwd == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);607608zwd->version = (char*)ZWRAP_customMalloc(strlen(version)+1, zwd->customMem);609if (zwd->version == NULL) return ZWRAPD_finishWithError(zwd, strm, 0);610strcpy(zwd->version, version);611612zwd->stream_size = stream_size;613zwd->totalInBytes = 0;614strm->state = convert_into_sis(zwd);615strm->total_in = 0;616strm->total_out = 0;617strm->reserved = ZWRAP_UNKNOWN_STREAM;618strm->adler = 0;619}620621return Z_OK;622}623624625ZEXTERN int ZEXPORT z_inflateInit2_ OF((z_streamp strm, int windowBits,626const char *version, int stream_size))627{628if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB) {629return inflateInit2_(strm, windowBits, version, stream_size);630}631632{ int const ret = z_inflateInit_ (strm, version, stream_size);633LOG_WRAPPERD("- inflateInit2 windowBits=%d\n", windowBits);634if (ret == Z_OK) {635ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;636if (zwd == NULL) return Z_STREAM_ERROR;637zwd->windowBits = windowBits;638}639return ret;640}641}642643int ZWRAP_inflateReset_keepDict(z_streamp strm)644{645LOG_WRAPPERD("- ZWRAP_inflateReset_keepDict\n");646if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)647return inflateReset(strm);648649{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;650if (zwd == NULL) return Z_STREAM_ERROR;651ZWRAP_initDCtx(zwd);652zwd->decompState = ZWRAP_useReset;653zwd->totalInBytes = 0;654}655656strm->total_in = 0;657strm->total_out = 0;658return Z_OK;659}660661662ZEXTERN int ZEXPORT z_inflateReset OF((z_streamp strm))663{664LOG_WRAPPERD("- inflateReset\n");665if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)666return inflateReset(strm);667668{ int const ret = ZWRAP_inflateReset_keepDict(strm);669if (ret != Z_OK) return ret; }670671{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;672if (zwd == NULL) return Z_STREAM_ERROR;673zwd->decompState = ZWRAP_useInit; }674675return Z_OK;676}677678679#if ZLIB_VERNUM >= 0x1240680ZEXTERN int ZEXPORT z_inflateReset2 OF((z_streamp strm,681int windowBits))682{683if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)684return inflateReset2(strm, windowBits);685686{ int const ret = z_inflateReset (strm);687if (ret == Z_OK) {688ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*)strm->state;689if (zwd == NULL) return Z_STREAM_ERROR;690zwd->windowBits = windowBits;691}692return ret;693}694}695#endif696697698ZEXTERN int ZEXPORT z_inflateSetDictionary OF((z_streamp strm,699const Bytef *dictionary,700uInt dictLength))701{702LOG_WRAPPERD("- inflateSetDictionary\n");703if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)704return inflateSetDictionary(strm, dictionary, dictLength);705706{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;707if (zwd == NULL || zwd->zbd == NULL) return Z_STREAM_ERROR;708{ size_t const initErr = ZSTD_initDStream_usingDict(zwd->zbd, dictionary, dictLength);709if (ZSTD_isError(initErr)) return ZWRAPD_finishWithError(zwd, strm, 0); }710zwd->decompState = ZWRAP_useReset;711712if (zwd->totalInBytes == ZSTD_HEADERSIZE) {713zwd->inBuffer.src = zwd->headerBuf;714zwd->inBuffer.size = zwd->totalInBytes;715zwd->inBuffer.pos = 0;716zwd->outBuffer.dst = strm->next_out;717zwd->outBuffer.size = 0;718zwd->outBuffer.pos = 0;719{ size_t const errorCode = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);720LOG_WRAPPERD("inflateSetDictionary ZSTD_decompressStream errorCode=%d srcSize=%d dstCapacity=%d\n",721(int)errorCode, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);722if (zwd->inBuffer.pos < zwd->outBuffer.size || ZSTD_isError(errorCode)) {723LOG_WRAPPERD("ERROR: ZSTD_decompressStream %s\n",724ZSTD_getErrorName(errorCode));725return ZWRAPD_finishWithError(zwd, strm, 0);726} } } }727728return Z_OK;729}730731732ZEXTERN int ZEXPORT z_inflate OF((z_streamp strm, int flush))733{734ZWRAP_DCtx* zwd;735736if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {737int const result = inflate(strm, flush);738LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",739(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, result);740return result;741}742743if (strm->avail_in <= 0) return Z_OK;744745zwd = (ZWRAP_DCtx*) strm->state;746LOG_WRAPPERD("- inflate1 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d\n",747(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);748749if (zwd == NULL) return Z_STREAM_ERROR;750if (zwd->decompState == ZWRAP_streamEnd) return Z_STREAM_END;751752if (zwd->totalInBytes < ZLIB_HEADERSIZE) {753if (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) {754if (ZWRAP_readLE32(strm->next_in) != ZSTD_MAGICNUMBER) {755{ int const initErr = (zwd->windowBits) ?756inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :757inflateInit_(strm, zwd->version, zwd->stream_size);758LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);759if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);760}761762strm->reserved = ZWRAP_ZLIB_STREAM;763{ size_t const freeErr = ZWRAP_freeDCtx(zwd);764if (ZSTD_isError(freeErr)) goto error; }765766{ int const result = (flush == Z_INFLATE_SYNC) ?767inflateSync(strm) :768inflate(strm, flush);769LOG_WRAPPERD("- inflate3 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",770(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);771return result;772} }773} else { /* ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */774size_t const srcSize = MIN(strm->avail_in, ZLIB_HEADERSIZE - zwd->totalInBytes);775memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);776strm->total_in += srcSize;777zwd->totalInBytes += srcSize;778strm->next_in += srcSize;779strm->avail_in -= srcSize;780if (zwd->totalInBytes < ZLIB_HEADERSIZE) return Z_OK;781782if (ZWRAP_readLE32(zwd->headerBuf) != ZSTD_MAGICNUMBER) {783z_stream strm2;784strm2.next_in = strm->next_in;785strm2.avail_in = strm->avail_in;786strm2.next_out = strm->next_out;787strm2.avail_out = strm->avail_out;788789{ int const initErr = (zwd->windowBits) ?790inflateInit2_(strm, zwd->windowBits, zwd->version, zwd->stream_size) :791inflateInit_(strm, zwd->version, zwd->stream_size);792LOG_WRAPPERD("ZLIB inflateInit errorCode=%d\n", initErr);793if (initErr != Z_OK) return ZWRAPD_finishWithError(zwd, strm, initErr);794}795796/* inflate header */797strm->next_in = (unsigned char*)zwd->headerBuf;798strm->avail_in = ZLIB_HEADERSIZE;799strm->avail_out = 0;800{ int const dErr = inflate(strm, Z_NO_FLUSH);801LOG_WRAPPERD("ZLIB inflate errorCode=%d strm->avail_in=%d\n",802dErr, (int)strm->avail_in);803if (dErr != Z_OK)804return ZWRAPD_finishWithError(zwd, strm, dErr);805}806if (strm->avail_in > 0) goto error;807808strm->next_in = strm2.next_in;809strm->avail_in = strm2.avail_in;810strm->next_out = strm2.next_out;811strm->avail_out = strm2.avail_out;812813strm->reserved = ZWRAP_ZLIB_STREAM; /* mark as zlib stream */814{ size_t const freeErr = ZWRAP_freeDCtx(zwd);815if (ZSTD_isError(freeErr)) goto error; }816817{ int const result = (flush == Z_INFLATE_SYNC) ?818inflateSync(strm) :819inflate(strm, flush);820LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",821(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, res);822return result;823} } } /* if ! (zwd->totalInBytes == 0 && strm->avail_in >= ZLIB_HEADERSIZE) */824} /* (zwd->totalInBytes < ZLIB_HEADERSIZE) */825826strm->reserved = ZWRAP_ZSTD_STREAM; /* mark as zstd steam */827828if (flush == Z_INFLATE_SYNC) { strm->msg = "inflateSync is not supported!"; goto error; }829830if (!zwd->zbd) {831zwd->zbd = ZSTD_createDStream_advanced(zwd->customMem);832if (zwd->zbd == NULL) { LOG_WRAPPERD("ERROR: ZSTD_createDStream_advanced\n"); goto error; }833zwd->decompState = ZWRAP_useInit;834}835836if (zwd->totalInBytes < ZSTD_HEADERSIZE) {837if (zwd->totalInBytes == 0 && strm->avail_in >= ZSTD_HEADERSIZE) {838if (zwd->decompState == ZWRAP_useInit) {839size_t const initErr = ZSTD_initDStream(zwd->zbd);840if (ZSTD_isError(initErr)) {841LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",842ZSTD_getErrorName(initErr));843goto error;844}845} else {846size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);847if (ZSTD_isError(resetErr)) goto error;848}849} else {850size_t const srcSize = MIN(strm->avail_in, ZSTD_HEADERSIZE - zwd->totalInBytes);851memcpy(zwd->headerBuf+zwd->totalInBytes, strm->next_in, srcSize);852strm->total_in += srcSize;853zwd->totalInBytes += srcSize;854strm->next_in += srcSize;855strm->avail_in -= srcSize;856if (zwd->totalInBytes < ZSTD_HEADERSIZE) return Z_OK;857858if (zwd->decompState == ZWRAP_useInit) {859size_t const initErr = ZSTD_initDStream(zwd->zbd);860if (ZSTD_isError(initErr)) {861LOG_WRAPPERD("ERROR: ZSTD_initDStream errorCode=%s\n",862ZSTD_getErrorName(initErr));863goto error;864}865} else {866size_t const resetErr = ZSTD_DCtx_reset(zwd->zbd, ZSTD_reset_session_only);867if (ZSTD_isError(resetErr)) goto error;868}869870zwd->inBuffer.src = zwd->headerBuf;871zwd->inBuffer.size = ZSTD_HEADERSIZE;872zwd->inBuffer.pos = 0;873zwd->outBuffer.dst = strm->next_out;874zwd->outBuffer.size = 0;875zwd->outBuffer.pos = 0;876{ size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);877LOG_WRAPPERD("inflate ZSTD_decompressStream1 errorCode=%d srcSize=%d dstCapacity=%d\n",878(int)dErr, (int)zwd->inBuffer.size, (int)zwd->outBuffer.size);879if (ZSTD_isError(dErr)) {880LOG_WRAPPERD("ERROR: ZSTD_decompressStream1 %s\n", ZSTD_getErrorName(dErr));881goto error;882} }883if (zwd->inBuffer.pos != zwd->inBuffer.size) goto error; /* not consumed */884}885} /* (zwd->totalInBytes < ZSTD_HEADERSIZE) */886887zwd->inBuffer.src = strm->next_in;888zwd->inBuffer.size = strm->avail_in;889zwd->inBuffer.pos = 0;890zwd->outBuffer.dst = strm->next_out;891zwd->outBuffer.size = strm->avail_out;892zwd->outBuffer.pos = 0;893{ size_t const dErr = ZSTD_decompressStream(zwd->zbd, &zwd->outBuffer, &zwd->inBuffer);894LOG_WRAPPERD("inflate ZSTD_decompressStream2 errorCode=%d srcSize=%d dstCapacity=%d\n",895(int)dErr, (int)strm->avail_in, (int)strm->avail_out);896if (ZSTD_isError(dErr)) {897zwd->errorCount++;898LOG_WRAPPERD("ERROR: ZSTD_decompressStream2 %s zwd->errorCount=%d\n",899ZSTD_getErrorName(dErr), zwd->errorCount);900if (zwd->errorCount<=1) return Z_NEED_DICT; else goto error;901}902LOG_WRAPPERD("inflate inBuffer.pos=%d inBuffer.size=%d outBuffer.pos=%d outBuffer.size=%d o\n",903(int)zwd->inBuffer.pos, (int)zwd->inBuffer.size, (int)zwd->outBuffer.pos, (int)zwd->outBuffer.size);904strm->next_out += zwd->outBuffer.pos;905strm->total_out += zwd->outBuffer.pos;906strm->avail_out -= zwd->outBuffer.pos;907strm->total_in += zwd->inBuffer.pos;908zwd->totalInBytes += zwd->inBuffer.pos;909strm->next_in += zwd->inBuffer.pos;910strm->avail_in -= zwd->inBuffer.pos;911if (dErr == 0) {912LOG_WRAPPERD("inflate Z_STREAM_END1 avail_in=%d avail_out=%d total_in=%d total_out=%d\n",913(int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out);914zwd->decompState = ZWRAP_streamEnd;915return Z_STREAM_END;916}917} /* dErr lifetime */918919LOG_WRAPPERD("- inflate2 flush=%d avail_in=%d avail_out=%d total_in=%d total_out=%d res=%d\n",920(int)flush, (int)strm->avail_in, (int)strm->avail_out, (int)strm->total_in, (int)strm->total_out, Z_OK);921return Z_OK;922923error:924return ZWRAPD_finishWithError(zwd, strm, 0);925}926927928ZEXTERN int ZEXPORT z_inflateEnd OF((z_streamp strm))929{930if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)931return inflateEnd(strm);932933LOG_WRAPPERD("- inflateEnd total_in=%d total_out=%d\n",934(int)(strm->total_in), (int)(strm->total_out));935{ ZWRAP_DCtx* const zwd = (ZWRAP_DCtx*) strm->state;936if (zwd == NULL) return Z_OK; /* structures are already freed */937{ size_t const freeErr = ZWRAP_freeDCtx(zwd);938if (ZSTD_isError(freeErr)) return Z_STREAM_ERROR; }939strm->state = NULL;940}941return Z_OK;942}943944945ZEXTERN int ZEXPORT z_inflateSync OF((z_streamp strm))946{947if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved) {948return inflateSync(strm);949}950951return z_inflate(strm, Z_INFLATE_SYNC);952}953954955956/* Advanced compression functions */957ZEXTERN int ZEXPORT z_deflateCopy OF((z_streamp dest,958z_streamp source))959{960if (!g_ZWRAP_useZSTDcompression)961return deflateCopy(dest, source);962return ZWRAPC_finishWithErrorMsg(source, "deflateCopy is not supported!");963}964965966ZEXTERN int ZEXPORT z_deflateTune OF((z_streamp strm,967int good_length,968int max_lazy,969int nice_length,970int max_chain))971{972if (!g_ZWRAP_useZSTDcompression)973return deflateTune(strm, good_length, max_lazy, nice_length, max_chain);974return ZWRAPC_finishWithErrorMsg(strm, "deflateTune is not supported!");975}976977978#if ZLIB_VERNUM >= 0x1260979ZEXTERN int ZEXPORT z_deflatePending OF((z_streamp strm,980unsigned *pending,981int *bits))982{983if (!g_ZWRAP_useZSTDcompression)984return deflatePending(strm, pending, bits);985return ZWRAPC_finishWithErrorMsg(strm, "deflatePending is not supported!");986}987#endif988989990ZEXTERN int ZEXPORT z_deflatePrime OF((z_streamp strm,991int bits,992int value))993{994if (!g_ZWRAP_useZSTDcompression)995return deflatePrime(strm, bits, value);996return ZWRAPC_finishWithErrorMsg(strm, "deflatePrime is not supported!");997}9989991000ZEXTERN int ZEXPORT z_deflateSetHeader OF((z_streamp strm,1001gz_headerp head))1002{1003if (!g_ZWRAP_useZSTDcompression)1004return deflateSetHeader(strm, head);1005return ZWRAPC_finishWithErrorMsg(strm, "deflateSetHeader is not supported!");1006}10071008100910101011/* Advanced decompression functions */1012#if ZLIB_VERNUM >= 0x12801013ZEXTERN int ZEXPORT z_inflateGetDictionary OF((z_streamp strm,1014Bytef *dictionary,1015uInt *dictLength))1016{1017if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)1018return inflateGetDictionary(strm, dictionary, dictLength);1019return ZWRAPD_finishWithErrorMsg(strm, "inflateGetDictionary is not supported!");1020}1021#endif102210231024ZEXTERN int ZEXPORT z_inflateCopy OF((z_streamp dest,1025z_streamp source))1026{1027if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !source->reserved)1028return inflateCopy(dest, source);1029return ZWRAPD_finishWithErrorMsg(source, "inflateCopy is not supported!");1030}103110321033#if ZLIB_VERNUM >= 0x12401034ZEXTERN long ZEXPORT z_inflateMark OF((z_streamp strm))1035{1036if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)1037return inflateMark(strm);1038return ZWRAPD_finishWithErrorMsg(strm, "inflateMark is not supported!");1039}1040#endif104110421043ZEXTERN int ZEXPORT z_inflatePrime OF((z_streamp strm,1044int bits,1045int value))1046{1047if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)1048return inflatePrime(strm, bits, value);1049return ZWRAPD_finishWithErrorMsg(strm, "inflatePrime is not supported!");1050}105110521053ZEXTERN int ZEXPORT z_inflateGetHeader OF((z_streamp strm,1054gz_headerp head))1055{1056if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)1057return inflateGetHeader(strm, head);1058return ZWRAPD_finishWithErrorMsg(strm, "inflateGetHeader is not supported!");1059}106010611062ZEXTERN int ZEXPORT z_inflateBackInit_ OF((z_streamp strm, int windowBits,1063unsigned char FAR *window,1064const char *version,1065int stream_size))1066{1067if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)1068return inflateBackInit_(strm, windowBits, window, version, stream_size);1069return ZWRAPD_finishWithErrorMsg(strm, "inflateBackInit is not supported!");1070}107110721073ZEXTERN int ZEXPORT z_inflateBack OF((z_streamp strm,1074in_func in, void FAR *in_desc,1075out_func out, void FAR *out_desc))1076{1077if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)1078return inflateBack(strm, in, in_desc, out, out_desc);1079return ZWRAPD_finishWithErrorMsg(strm, "inflateBack is not supported!");1080}108110821083ZEXTERN int ZEXPORT z_inflateBackEnd OF((z_streamp strm))1084{1085if (g_ZWRAPdecompressionType == ZWRAP_FORCE_ZLIB || !strm->reserved)1086return inflateBackEnd(strm);1087return ZWRAPD_finishWithErrorMsg(strm, "inflateBackEnd is not supported!");1088}108910901091ZEXTERN uLong ZEXPORT z_zlibCompileFlags OF((void)) { return zlibCompileFlags(); }1092109310941095/* === utility functions === */1096#ifndef Z_SOLO10971098ZEXTERN int ZEXPORT z_compress OF((Bytef *dest, uLongf *destLen,1099const Bytef *source, uLong sourceLen))1100{1101if (!g_ZWRAP_useZSTDcompression)1102return compress(dest, destLen, source, sourceLen);11031104{ size_t dstCapacity = *destLen;1105size_t const cSize = ZSTD_compress(dest, dstCapacity,1106source, sourceLen,1107ZWRAP_DEFAULT_CLEVEL);1108LOG_WRAPPERD("z_compress sourceLen=%d dstCapacity=%d\n",1109(int)sourceLen, (int)dstCapacity);1110if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;1111*destLen = cSize;1112}1113return Z_OK;1114}111511161117ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest, uLongf *destLen,1118const Bytef *source, uLong sourceLen,1119int level))1120{1121if (!g_ZWRAP_useZSTDcompression)1122return compress2(dest, destLen, source, sourceLen, level);11231124{ size_t dstCapacity = *destLen;1125size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);1126if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;1127*destLen = cSize;1128}1129return Z_OK;1130}113111321133ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))1134{1135if (!g_ZWRAP_useZSTDcompression)1136return compressBound(sourceLen);11371138return ZSTD_compressBound(sourceLen);1139}114011411142ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest, uLongf *destLen,1143const Bytef *source, uLong sourceLen))1144{1145if (!ZSTD_isFrame(source, sourceLen))1146return uncompress(dest, destLen, source, sourceLen);11471148{ size_t dstCapacity = *destLen;1149size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);1150if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;1151*destLen = dSize;1152}1153return Z_OK;1154}11551156#endif /* !Z_SOLO */115711581159/* checksum functions */11601161ZEXTERN uLong ZEXPORT z_adler32 OF((uLong adler, const Bytef *buf, uInt len))1162{1163return adler32(adler, buf, len);1164}11651166ZEXTERN uLong ZEXPORT z_crc32 OF((uLong crc, const Bytef *buf, uInt len))1167{1168return crc32(crc, buf, len);1169}117011711172#if ZLIB_VERNUM >= 0x12B01173ZEXTERN uLong ZEXPORT z_adler32_z OF((uLong adler, const Bytef *buf, z_size_t len))1174{1175return adler32_z(adler, buf, len);1176}11771178ZEXTERN uLong ZEXPORT z_crc32_z OF((uLong crc, const Bytef *buf, z_size_t len))1179{1180return crc32_z(crc, buf, len);1181}1182#endif118311841185#if ZLIB_VERNUM >= 0x12701186ZEXTERN const z_crc_t FAR * ZEXPORT z_get_crc_table OF((void))1187{1188return get_crc_table();1189}1190#endif11911192/* Error function */1193ZEXTERN const char * ZEXPORT z_zError OF((int err))1194{1195/* Just use zlib Error function */1196return zError(err);1197}119811991200