zstd/lib/deprecated/zbuff_compress.c

146 lines
5.0 KiB
C
Raw Normal View History

2016-08-30 17:04:33 +00:00
/**
* Copyright (c) 2016-present, Yann Collet, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
2015-11-25 13:42:45 +00:00
/* *************************************
2016-02-03 01:46:46 +00:00
* Dependencies
2015-11-25 13:42:45 +00:00
***************************************/
#define ZBUFF_STATIC_LINKING_ONLY
#include "zbuff.h"
2015-11-25 13:42:45 +00:00
2016-07-28 17:55:09 +00:00
/*-***********************************************************
2015-11-25 13:42:45 +00:00
* Streaming compression
*
* A ZBUFF_CCtx object is required to track streaming operation.
* Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
* Use ZBUFF_compressInit() to start a new compression operation.
* ZBUFF_CCtx objects can be reused multiple times.
*
* Use ZBUFF_compressContinue() repetitively to consume your input.
* *srcSizePtr and *dstCapacityPtr can be any size.
* The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
2015-11-25 13:42:45 +00:00
* 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.
* The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
2015-11-25 13:42:45 +00:00
* @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
* or an error code, which can be tested using ZBUFF_isError().
*
* ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
* Note that it will not output more than *dstCapacityPtr.
2015-11-25 13:42:45 +00:00
* Therefore, some content might still be left into its internal buffer if dst buffer is too small.
* @return : nb of bytes still present into internal buffer (0 if it's empty)
* or an error code, which can be tested using ZBUFF_isError().
*
* ZBUFF_compressEnd() instructs to finish a frame.
* It will perform a flush and write frame epilogue.
* Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
2015-11-25 13:42:45 +00:00
* @return : nb of bytes still present into internal buffer (0 if it's empty)
* or an error code, which can be tested using ZBUFF_isError().
*
* Hint : recommended buffer sizes (not compulsory)
* input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
* 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.
2016-07-28 17:55:09 +00:00
* ***********************************************************/
2015-11-25 13:42:45 +00:00
ZBUFF_CCtx* ZBUFF_createCCtx(void)
{
return ZSTD_createCStream();
}
ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
{
return ZSTD_createCStream_advanced(customMem);
2015-11-25 13:42:45 +00:00
}
size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
{
return ZSTD_freeCStream(zbc);
2015-11-25 13:42:45 +00:00
}
2016-07-28 14:32:34 +00:00
/* ====== Initialization ====== */
2015-11-25 13:42:45 +00:00
size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
const void* dict, size_t dictSize,
ZSTD_parameters params, unsigned long long pledgedSrcSize)
2015-11-25 13:42:45 +00:00
{
return ZSTD_initCStream_advanced(zbc, dict, dictSize, params, pledgedSrcSize);
2015-11-25 13:42:45 +00:00
}
2016-04-01 13:48:48 +00:00
size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
2015-11-25 13:42:45 +00:00
{
return ZSTD_initCStream_usingDict(zbc, dict, dictSize, compressionLevel);
2015-11-25 13:42:45 +00:00
}
size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
{
return ZSTD_initCStream(zbc, compressionLevel);
}
2016-07-28 14:32:34 +00:00
/* ====== Compression ====== */
2015-11-25 13:42:45 +00:00
size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
void* dst, size_t* dstCapacityPtr,
2015-11-25 13:42:45 +00:00
const void* src, size_t* srcSizePtr)
2016-01-28 16:56:33 +00:00
{
size_t result;
ZSTD_outBuffer outBuff;
ZSTD_inBuffer inBuff;
outBuff.dst = dst;
outBuff.pos = 0;
outBuff.size = *dstCapacityPtr;
inBuff.src = src;
inBuff.pos = 0;
inBuff.size = *srcSizePtr;
result = ZSTD_compressStream(zbc, &outBuff, &inBuff);
*dstCapacityPtr = outBuff.pos;
*srcSizePtr = inBuff.pos;
return result;
2016-01-28 16:56:33 +00:00
}
2015-11-25 13:42:45 +00:00
2016-07-28 14:32:34 +00:00
/* ====== Finalize ====== */
2015-11-25 13:42:45 +00:00
size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
2015-11-25 13:42:45 +00:00
{
size_t result;
ZSTD_outBuffer outBuff;
outBuff.dst = dst;
outBuff.pos = 0;
outBuff.size = *dstCapacityPtr;
result = ZSTD_flushStream(zbc, &outBuff);
*dstCapacityPtr = outBuff.pos;
return result;
2015-11-25 13:42:45 +00:00
}
size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
2015-11-25 13:42:45 +00:00
{
size_t result;
ZSTD_outBuffer outBuff;
outBuff.dst = dst;
outBuff.pos = 0;
outBuff.size = *dstCapacityPtr;
result = ZSTD_endStream(zbc, &outBuff);
*dstCapacityPtr = outBuff.pos;
return result;
2015-11-25 13:42:45 +00:00
}
/* *************************************
* Tool functions
***************************************/
size_t ZBUFF_recommendedCInSize(void) { return ZSTD_CStreamInSize(); }
size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }