Path: blob/master/Utilities/cmzstd/lib/deprecated/zbuff_compress.c
3156 views
/*1* Copyright (c) Meta Platforms, Inc. and affiliates.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*/9101112/* *************************************13* Dependencies14***************************************/15#define ZBUFF_STATIC_LINKING_ONLY16#include "zbuff.h"17#include "../common/error_private.h"181920/*-***********************************************************21* Streaming compression22*23* A ZBUFF_CCtx object is required to track streaming operation.24* Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.25* Use ZBUFF_compressInit() to start a new compression operation.26* ZBUFF_CCtx objects can be reused multiple times.27*28* Use ZBUFF_compressContinue() repetitively to consume your input.29* *srcSizePtr and *dstCapacityPtr can be any size.30* The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.31* Note that it may not consume the entire input, in which case it's up to the caller to call again the function with remaining input.32* The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .33* @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)34* or an error code, which can be tested using ZBUFF_isError().35*36* ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.37* Note that it will not output more than *dstCapacityPtr.38* Therefore, some content might still be left into its internal buffer if dst buffer is too small.39* @return : nb of bytes still present into internal buffer (0 if it's empty)40* or an error code, which can be tested using ZBUFF_isError().41*42* ZBUFF_compressEnd() instructs to finish a frame.43* It will perform a flush and write frame epilogue.44* Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.45* @return : nb of bytes still present into internal buffer (0 if it's empty)46* or an error code, which can be tested using ZBUFF_isError().47*48* Hint : recommended buffer sizes (not compulsory)49* input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.50* output : ZSTD_compressBound(ZSTD_BLOCKSIZE_MAX) + ZSTD_blockHeaderSize + ZBUFF_endFrameSize : ensures it's always possible to write/flush/end a full block at best speed.51* ***********************************************************/5253ZBUFF_CCtx* ZBUFF_createCCtx(void)54{55return ZSTD_createCStream();56}5758ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)59{60return ZSTD_createCStream_advanced(customMem);61}6263size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)64{65return ZSTD_freeCStream(zbc);66}676869/* ====== Initialization ====== */7071size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,72const void* dict, size_t dictSize,73ZSTD_parameters params, unsigned long long pledgedSrcSize)74{75if (pledgedSrcSize==0) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN; /* preserve "0 == unknown" behavior */76FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");77FORWARD_IF_ERROR(ZSTD_CCtx_setPledgedSrcSize(zbc, pledgedSrcSize), "");7879FORWARD_IF_ERROR(ZSTD_checkCParams(params.cParams), "");80FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_windowLog, params.cParams.windowLog), "");81FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_hashLog, params.cParams.hashLog), "");82FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_chainLog, params.cParams.chainLog), "");83FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_searchLog, params.cParams.searchLog), "");84FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_minMatch, params.cParams.minMatch), "");85FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_targetLength, params.cParams.targetLength), "");86FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_strategy, params.cParams.strategy), "");8788FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_contentSizeFlag, params.fParams.contentSizeFlag), "");89FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_checksumFlag, params.fParams.checksumFlag), "");90FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_dictIDFlag, params.fParams.noDictIDFlag), "");9192FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");93return 0;94}9596size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)97{98FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");99FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_compressionLevel, compressionLevel), "");100FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");101return 0;102}103104size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)105{106return ZSTD_initCStream(zbc, compressionLevel);107}108109/* ====== Compression ====== */110111112size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,113void* dst, size_t* dstCapacityPtr,114const void* src, size_t* srcSizePtr)115{116size_t result;117ZSTD_outBuffer outBuff;118ZSTD_inBuffer inBuff;119outBuff.dst = dst;120outBuff.pos = 0;121outBuff.size = *dstCapacityPtr;122inBuff.src = src;123inBuff.pos = 0;124inBuff.size = *srcSizePtr;125result = ZSTD_compressStream(zbc, &outBuff, &inBuff);126*dstCapacityPtr = outBuff.pos;127*srcSizePtr = inBuff.pos;128return result;129}130131132133/* ====== Finalize ====== */134135size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)136{137size_t result;138ZSTD_outBuffer outBuff;139outBuff.dst = dst;140outBuff.pos = 0;141outBuff.size = *dstCapacityPtr;142result = ZSTD_flushStream(zbc, &outBuff);143*dstCapacityPtr = outBuff.pos;144return result;145}146147148size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)149{150size_t result;151ZSTD_outBuffer outBuff;152outBuff.dst = dst;153outBuff.pos = 0;154outBuff.size = *dstCapacityPtr;155result = ZSTD_endStream(zbc, &outBuff);156*dstCapacityPtr = outBuff.pos;157return result;158}159160161162/* *************************************163* Tool functions164***************************************/165size_t ZBUFF_recommendedCInSize(void) { return ZSTD_CStreamInSize(); }166size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }167168169