zstd/lib/deprecated/zbuff_compress.c

320 lines
13 KiB
C
Raw Normal View History

2016-08-30 10:04:33 -07: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 05:42:45 -08:00
/* *************************************
2016-02-02 17:46:46 -08:00
* Dependencies
2015-11-25 05:42:45 -08:00
***************************************/
#include <stdlib.h>
#include "error_private.h"
#include "zstd_internal.h" /* MIN, ZSTD_BLOCKHEADERSIZE, defaultCustomMem */
#define ZBUFF_STATIC_LINKING_ONLY
#include "zbuff.h"
2015-11-25 05:42:45 -08:00
2016-02-02 17:46:46 -08:00
/* *************************************
* Constants
***************************************/
static size_t const ZBUFF_endFrameSize = ZSTD_BLOCKHEADERSIZE;
2016-02-02 17:46:46 -08:00
2016-07-28 10:55:09 -07:00
/*-***********************************************************
2015-11-25 05:42:45 -08: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 05:42:45 -08: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 05:42:45 -08: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 05:42:45 -08: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 05:42:45 -08: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 10:55:09 -07:00
* ***********************************************************/
2015-11-25 05:42:45 -08:00
typedef enum { ZBUFFcs_init, ZBUFFcs_load, ZBUFFcs_flush, ZBUFFcs_final } ZBUFF_cStage;
2015-11-25 05:42:45 -08:00
/* *** Resources *** */
2015-11-25 05:42:45 -08:00
struct ZBUFF_CCtx_s {
ZSTD_CCtx* zc;
char* inBuff;
2015-11-25 05:42:45 -08:00
size_t inBuffSize;
size_t inToCompress;
size_t inBuffPos;
size_t inBuffTarget;
size_t blockSize;
char* outBuff;
2015-11-25 05:42:45 -08:00
size_t outBuffSize;
size_t outBuffContentSize;
size_t outBuffFlushedSize;
ZBUFF_cStage stage;
2016-07-27 15:55:43 -07:00
U32 checksum;
2016-07-28 10:55:09 -07:00
U32 frameEnded;
ZSTD_customMem customMem;
}; /* typedef'd tp ZBUFF_CCtx within "zbuff.h" */
2015-11-25 05:42:45 -08:00
ZBUFF_CCtx* ZBUFF_createCCtx(void)
{
return ZBUFF_createCCtx_advanced(defaultCustomMem);
}
ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
{
ZBUFF_CCtx* zbc;
if (!customMem.customAlloc && !customMem.customFree)
2016-06-03 04:23:04 -07:00
customMem = defaultCustomMem;
if (!customMem.customAlloc || !customMem.customFree)
return NULL;
zbc = (ZBUFF_CCtx*)customMem.customAlloc(customMem.opaque, sizeof(ZBUFF_CCtx));
2015-11-25 06:04:37 -08:00
if (zbc==NULL) return NULL;
memset(zbc, 0, sizeof(ZBUFF_CCtx));
memcpy(&zbc->customMem, &customMem, sizeof(ZSTD_customMem));
zbc->zc = ZSTD_createCCtx_advanced(customMem);
if (zbc->zc == NULL) { ZBUFF_freeCCtx(zbc); return NULL; }
2015-11-25 05:42:45 -08:00
return zbc;
}
size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
{
if (zbc==NULL) return 0; /* support free on NULL */
ZSTD_freeCCtx(zbc->zc);
if (zbc->inBuff) zbc->customMem.customFree(zbc->customMem.opaque, zbc->inBuff);
if (zbc->outBuff) zbc->customMem.customFree(zbc->customMem.opaque, zbc->outBuff);
zbc->customMem.customFree(zbc->customMem.opaque, zbc);
2015-11-25 05:42:45 -08:00
return 0;
}
2016-07-28 07:32:34 -07:00
/* ====== Initialization ====== */
2015-11-25 05:42:45 -08: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 05:42:45 -08:00
{
/* allocate buffers */
2016-04-01 06:48:48 -07:00
{ size_t const neededInBuffSize = (size_t)1 << params.cParams.windowLog;
if (zbc->inBuffSize < neededInBuffSize) {
zbc->inBuffSize = neededInBuffSize;
zbc->customMem.customFree(zbc->customMem.opaque, zbc->inBuff); /* should not be necessary */
zbc->inBuff = (char*)zbc->customMem.customAlloc(zbc->customMem.opaque, neededInBuffSize);
2016-04-01 06:48:48 -07:00
if (zbc->inBuff == NULL) return ERROR(memory_allocation);
}
2016-07-18 07:52:10 -07:00
zbc->blockSize = MIN(ZSTD_BLOCKSIZE_ABSOLUTEMAX, neededInBuffSize);
}
2016-01-28 08:56:33 -08:00
if (zbc->outBuffSize < ZSTD_compressBound(zbc->blockSize)+1) {
2015-11-25 05:42:45 -08:00
zbc->outBuffSize = ZSTD_compressBound(zbc->blockSize)+1;
zbc->customMem.customFree(zbc->customMem.opaque, zbc->outBuff); /* should not be necessary */
zbc->outBuff = (char*)zbc->customMem.customAlloc(zbc->customMem.opaque, zbc->outBuffSize);
2015-11-25 05:42:45 -08:00
if (zbc->outBuff == NULL) return ERROR(memory_allocation);
}
2016-04-01 06:48:48 -07:00
{ size_t const errorCode = ZSTD_compressBegin_advanced(zbc->zc, dict, dictSize, params, pledgedSrcSize);
if (ZSTD_isError(errorCode)) return errorCode; }
2015-11-25 05:42:45 -08:00
zbc->inToCompress = 0;
zbc->inBuffPos = 0;
zbc->inBuffTarget = zbc->blockSize;
zbc->outBuffContentSize = zbc->outBuffFlushedSize = 0;
2016-04-01 06:48:48 -07:00
zbc->stage = ZBUFFcs_load;
2016-07-27 15:55:43 -07:00
zbc->checksum = params.fParams.checksumFlag > 0;
2016-07-28 10:55:09 -07:00
zbc->frameEnded = 0;
2015-11-25 05:42:45 -08:00
return 0; /* ready to go */
}
2016-04-01 06:48:48 -07:00
size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
2015-11-25 05:42:45 -08:00
{
2016-06-27 06:28:45 -07:00
ZSTD_parameters const params = ZSTD_getParams(compressionLevel, 0, dictSize);
return ZBUFF_compressInit_advanced(zbc, dict, dictSize, params, 0);
2015-11-25 05:42:45 -08:00
}
size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
{
return ZBUFF_compressInitDictionary(zbc, NULL, 0, compressionLevel);
}
2015-11-25 05:42:45 -08:00
/* internal util function */
MEM_STATIC size_t ZBUFF_limitCopy(void* dst, size_t dstCapacity, const void* src, size_t srcSize)
{
size_t const length = MIN(dstCapacity, srcSize);
memcpy(dst, src, length);
return length;
}
2016-07-28 07:32:34 -07:00
/* ====== Compression ====== */
typedef enum { zbf_gather, zbf_flush, zbf_end } ZBUFF_flush_e;
2015-11-25 05:42:45 -08:00
static size_t ZBUFF_compressContinue_generic(ZBUFF_CCtx* zbc,
void* dst, size_t* dstCapacityPtr,
2015-11-25 05:42:45 -08:00
const void* src, size_t* srcSizePtr,
2016-07-28 10:55:09 -07:00
ZBUFF_flush_e const flush)
2015-11-25 05:42:45 -08:00
{
2016-07-28 07:32:34 -07:00
U32 someMoreWork = 1;
2015-11-25 05:42:45 -08:00
const char* const istart = (const char*)src;
const char* const iend = istart + *srcSizePtr;
2016-04-01 06:48:48 -07:00
const char* ip = istart;
2015-11-25 05:42:45 -08:00
char* const ostart = (char*)dst;
char* const oend = ostart + *dstCapacityPtr;
2016-04-01 06:48:48 -07:00
char* op = ostart;
2015-11-25 05:42:45 -08:00
2016-07-28 07:32:34 -07:00
while (someMoreWork) {
2015-11-25 05:42:45 -08:00
switch(zbc->stage)
{
case ZBUFFcs_init: return ERROR(init_missing); /* call ZBUFF_compressInit() first ! */
case ZBUFFcs_load:
/* complete inBuffer */
2016-04-01 06:48:48 -07:00
{ size_t const toLoad = zbc->inBuffTarget - zbc->inBuffPos;
size_t const loaded = ZBUFF_limitCopy(zbc->inBuff + zbc->inBuffPos, toLoad, ip, iend-ip);
2015-11-25 05:42:45 -08:00
zbc->inBuffPos += loaded;
ip += loaded;
2016-01-28 08:56:33 -08:00
if ( (zbc->inBuffPos==zbc->inToCompress) || (!flush && (toLoad != loaded)) ) {
2016-07-28 07:32:34 -07:00
someMoreWork = 0; break; /* not enough input to get a full block : stop there, wait for more */
2016-01-28 08:56:33 -08:00
} }
2015-11-25 05:42:45 -08:00
/* compress current block (note : this stage cannot be stopped in the middle) */
2016-04-01 06:48:48 -07:00
{ void* cDst;
2015-11-25 05:42:45 -08:00
size_t cSize;
2016-04-01 06:48:48 -07:00
size_t const iSize = zbc->inBuffPos - zbc->inToCompress;
size_t oSize = oend-op;
if (oSize >= ZSTD_compressBound(iSize))
2015-11-25 05:42:45 -08:00
cDst = op; /* compress directly into output buffer (avoid flush stage) */
else
cDst = zbc->outBuff, oSize = zbc->outBuffSize;
2016-07-28 10:55:09 -07:00
cSize = (flush == zbf_end) ?
ZSTD_compressEnd(zbc->zc, cDst, oSize, zbc->inBuff + zbc->inToCompress, iSize) :
ZSTD_compressContinue(zbc->zc, cDst, oSize, zbc->inBuff + zbc->inToCompress, iSize);
2015-11-25 05:42:45 -08:00
if (ZSTD_isError(cSize)) return cSize;
2016-07-28 10:55:09 -07:00
if (flush == zbf_end) zbc->frameEnded = 1;
2015-11-25 05:42:45 -08:00
/* prepare next block */
zbc->inBuffTarget = zbc->inBuffPos + zbc->blockSize;
if (zbc->inBuffTarget > zbc->inBuffSize)
2016-04-03 11:24:25 -07:00
zbc->inBuffPos = 0, zbc->inBuffTarget = zbc->blockSize; /* note : inBuffSize >= blockSize */
2015-11-25 05:42:45 -08:00
zbc->inToCompress = zbc->inBuffPos;
if (cDst == op) { op += cSize; break; } /* no need to flush */
zbc->outBuffContentSize = cSize;
zbc->outBuffFlushedSize = 0;
2016-04-01 06:48:48 -07:00
zbc->stage = ZBUFFcs_flush; /* continue to flush stage */
2015-11-25 05:42:45 -08:00
}
case ZBUFFcs_flush:
2016-04-01 06:48:48 -07:00
{ size_t const toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize;
size_t const flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush);
2015-11-25 05:42:45 -08:00
op += flushed;
zbc->outBuffFlushedSize += flushed;
2016-07-28 07:32:34 -07:00
if (toFlush!=flushed) { someMoreWork = 0; break; } /* dst too small to store flushed data : stop there */
zbc->outBuffContentSize = zbc->outBuffFlushedSize = 0;
zbc->stage = ZBUFFcs_load;
2015-11-25 05:42:45 -08:00
break;
}
case ZBUFFcs_final:
2016-07-28 07:32:34 -07:00
someMoreWork = 0; /* do nothing */
break;
default:
return ERROR(GENERIC); /* impossible */
2015-11-25 05:42:45 -08:00
}
}
*srcSizePtr = ip - istart;
*dstCapacityPtr = op - ostart;
2016-07-28 10:55:09 -07:00
if (zbc->frameEnded) return 0;
2016-04-01 06:48:48 -07:00
{ size_t hintInSize = zbc->inBuffTarget - zbc->inBuffPos;
2015-11-25 05:42:45 -08:00
if (hintInSize==0) hintInSize = zbc->blockSize;
return hintInSize;
}
}
size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
void* dst, size_t* dstCapacityPtr,
2015-11-25 05:42:45 -08:00
const void* src, size_t* srcSizePtr)
2016-01-28 08:56:33 -08:00
{
2016-07-28 07:32:34 -07:00
return ZBUFF_compressContinue_generic(zbc, dst, dstCapacityPtr, src, srcSizePtr, zbf_gather);
2016-01-28 08:56:33 -08:00
}
2015-11-25 05:42:45 -08:00
2016-07-28 07:32:34 -07:00
/* ====== Finalize ====== */
2015-11-25 05:42:45 -08:00
size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
2015-11-25 05:42:45 -08:00
{
size_t srcSize = 0;
2016-07-28 07:32:34 -07:00
ZBUFF_compressContinue_generic(zbc, dst, dstCapacityPtr, &srcSize, &srcSize, zbf_flush); /* use a valid src address instead of NULL */
2015-11-25 05:42:45 -08:00
return zbc->outBuffContentSize - zbc->outBuffFlushedSize;
}
size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
2015-11-25 05:42:45 -08:00
{
BYTE* const ostart = (BYTE*)dst;
BYTE* const oend = ostart + *dstCapacityPtr;
2016-04-01 06:48:48 -07:00
BYTE* op = ostart;
if (zbc->stage != ZBUFFcs_final) {
/* flush whatever remains */
size_t outSize = *dstCapacityPtr;
2016-07-28 07:32:34 -07:00
size_t srcSize = 0;
2016-07-28 10:55:09 -07:00
size_t const notEnded = ZBUFF_compressContinue_generic(zbc, dst, &outSize, &srcSize, &srcSize, zbf_end); /* use a valid address instead of NULL */
2016-07-28 07:32:34 -07:00
size_t const remainingToFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize;
2016-07-28 10:55:09 -07:00
op += outSize;
if (remainingToFlush) {
*dstCapacityPtr = op-ostart;
2016-07-27 15:55:43 -07:00
return remainingToFlush + ZBUFF_endFrameSize + (zbc->checksum * 4);
}
/* create epilogue */
zbc->stage = ZBUFFcs_final;
2016-07-28 10:55:09 -07:00
zbc->outBuffContentSize = !notEnded ? 0 :
ZSTD_compressEnd(zbc->zc, zbc->outBuff, zbc->outBuffSize, NULL, 0); /* write epilogue into outBuff */
}
/* flush epilogue */
{ size_t const toFlush = zbc->outBuffContentSize - zbc->outBuffFlushedSize;
size_t const flushed = ZBUFF_limitCopy(op, oend-op, zbc->outBuff + zbc->outBuffFlushedSize, toFlush);
op += flushed;
zbc->outBuffFlushedSize += flushed;
*dstCapacityPtr = op-ostart;
if (toFlush==flushed) zbc->stage = ZBUFFcs_init; /* end reached */
return toFlush - flushed;
}
2015-11-25 05:42:45 -08:00
}
/* *************************************
* Tool functions
***************************************/
2016-07-18 07:52:10 -07:00
size_t ZBUFF_recommendedCInSize(void) { return ZSTD_BLOCKSIZE_ABSOLUTEMAX; }
size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_compressBound(ZSTD_BLOCKSIZE_ABSOLUTEMAX) + ZSTD_blockHeaderSize + ZBUFF_endFrameSize; }