xref: /freebsd-src/sys/contrib/zstd/lib/deprecated/zbuff_compress.c (revision 5ff13fbc199bdf5f0572845351c68ee5ca828e71)
10c16b537SWarner Losh /*
2*5ff13fbcSAllan Jude  * Copyright (c) Yann Collet, Facebook, Inc.
30c16b537SWarner Losh  * All rights reserved.
40c16b537SWarner Losh  *
50c16b537SWarner Losh  * This source code is licensed under both the BSD-style license (found in the
60c16b537SWarner Losh  * LICENSE file in the root directory of this source tree) and the GPLv2 (found
70c16b537SWarner Losh  * in the COPYING file in the root directory of this source tree).
80c16b537SWarner Losh  * You may select, at your option, one of the above-listed licenses.
90c16b537SWarner Losh  */
100c16b537SWarner Losh 
110c16b537SWarner Losh 
120c16b537SWarner Losh 
130c16b537SWarner Losh /* *************************************
140c16b537SWarner Losh *  Dependencies
150c16b537SWarner Losh ***************************************/
160c16b537SWarner Losh #define ZBUFF_STATIC_LINKING_ONLY
170c16b537SWarner Losh #include "zbuff.h"
18*5ff13fbcSAllan Jude #include "../common/error_private.h"
190c16b537SWarner Losh 
200c16b537SWarner Losh 
210c16b537SWarner Losh /*-***********************************************************
220c16b537SWarner Losh *  Streaming compression
230c16b537SWarner Losh *
240c16b537SWarner Losh *  A ZBUFF_CCtx object is required to track streaming operation.
250c16b537SWarner Losh *  Use ZBUFF_createCCtx() and ZBUFF_freeCCtx() to create/release resources.
260c16b537SWarner Losh *  Use ZBUFF_compressInit() to start a new compression operation.
270c16b537SWarner Losh *  ZBUFF_CCtx objects can be reused multiple times.
280c16b537SWarner Losh *
290c16b537SWarner Losh *  Use ZBUFF_compressContinue() repetitively to consume your input.
300c16b537SWarner Losh *  *srcSizePtr and *dstCapacityPtr can be any size.
310c16b537SWarner Losh *  The function will report how many bytes were read or written by modifying *srcSizePtr and *dstCapacityPtr.
320c16b537SWarner Losh *  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.
330c16b537SWarner Losh *  The content of dst will be overwritten (up to *dstCapacityPtr) at each function call, so save its content if it matters or change dst .
340c16b537SWarner Losh *  @return : a hint to preferred nb of bytes to use as input for next function call (it's only a hint, to improve latency)
350c16b537SWarner Losh *            or an error code, which can be tested using ZBUFF_isError().
360c16b537SWarner Losh *
370c16b537SWarner Losh *  ZBUFF_compressFlush() can be used to instruct ZBUFF to compress and output whatever remains within its buffer.
380c16b537SWarner Losh *  Note that it will not output more than *dstCapacityPtr.
390c16b537SWarner Losh *  Therefore, some content might still be left into its internal buffer if dst buffer is too small.
400c16b537SWarner Losh *  @return : nb of bytes still present into internal buffer (0 if it's empty)
410c16b537SWarner Losh *            or an error code, which can be tested using ZBUFF_isError().
420c16b537SWarner Losh *
430c16b537SWarner Losh *  ZBUFF_compressEnd() instructs to finish a frame.
440c16b537SWarner Losh *  It will perform a flush and write frame epilogue.
450c16b537SWarner Losh *  Similar to ZBUFF_compressFlush(), it may not be able to output the entire internal buffer content if *dstCapacityPtr is too small.
460c16b537SWarner Losh *  @return : nb of bytes still present into internal buffer (0 if it's empty)
470c16b537SWarner Losh *            or an error code, which can be tested using ZBUFF_isError().
480c16b537SWarner Losh *
490c16b537SWarner Losh *  Hint : recommended buffer sizes (not compulsory)
500c16b537SWarner Losh *  input : ZSTD_BLOCKSIZE_MAX (128 KB), internal unit size, it improves latency to use this value.
510c16b537SWarner Losh *  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.
520c16b537SWarner Losh * ***********************************************************/
530c16b537SWarner Losh 
ZBUFF_createCCtx(void)540c16b537SWarner Losh ZBUFF_CCtx* ZBUFF_createCCtx(void)
550c16b537SWarner Losh {
560c16b537SWarner Losh     return ZSTD_createCStream();
570c16b537SWarner Losh }
580c16b537SWarner Losh 
ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)590c16b537SWarner Losh ZBUFF_CCtx* ZBUFF_createCCtx_advanced(ZSTD_customMem customMem)
600c16b537SWarner Losh {
610c16b537SWarner Losh     return ZSTD_createCStream_advanced(customMem);
620c16b537SWarner Losh }
630c16b537SWarner Losh 
ZBUFF_freeCCtx(ZBUFF_CCtx * zbc)640c16b537SWarner Losh size_t ZBUFF_freeCCtx(ZBUFF_CCtx* zbc)
650c16b537SWarner Losh {
660c16b537SWarner Losh     return ZSTD_freeCStream(zbc);
670c16b537SWarner Losh }
680c16b537SWarner Losh 
690c16b537SWarner Losh 
700c16b537SWarner Losh /* ======   Initialization   ====== */
710c16b537SWarner Losh 
ZBUFF_compressInit_advanced(ZBUFF_CCtx * zbc,const void * dict,size_t dictSize,ZSTD_parameters params,unsigned long long pledgedSrcSize)720c16b537SWarner Losh size_t ZBUFF_compressInit_advanced(ZBUFF_CCtx* zbc,
730c16b537SWarner Losh                                    const void* dict, size_t dictSize,
740c16b537SWarner Losh                                    ZSTD_parameters params, unsigned long long pledgedSrcSize)
750c16b537SWarner Losh {
76052d3c12SConrad Meyer     if (pledgedSrcSize==0) pledgedSrcSize = ZSTD_CONTENTSIZE_UNKNOWN;  /* preserve "0 == unknown" behavior */
77*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");
78*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setPledgedSrcSize(zbc, pledgedSrcSize), "");
790c16b537SWarner Losh 
80*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_checkCParams(params.cParams), "");
81*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_windowLog, params.cParams.windowLog), "");
82*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_hashLog, params.cParams.hashLog), "");
83*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_chainLog, params.cParams.chainLog), "");
84*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_searchLog, params.cParams.searchLog), "");
85*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_minMatch, params.cParams.minMatch), "");
86*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_targetLength, params.cParams.targetLength), "");
87*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_strategy, params.cParams.strategy), "");
88*5ff13fbcSAllan Jude 
89*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_contentSizeFlag, params.fParams.contentSizeFlag), "");
90*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_checksumFlag, params.fParams.checksumFlag), "");
91*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_dictIDFlag, params.fParams.noDictIDFlag), "");
92*5ff13fbcSAllan Jude 
93*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");
94*5ff13fbcSAllan Jude     return 0;
95*5ff13fbcSAllan Jude }
960c16b537SWarner Losh 
ZBUFF_compressInitDictionary(ZBUFF_CCtx * zbc,const void * dict,size_t dictSize,int compressionLevel)970c16b537SWarner Losh size_t ZBUFF_compressInitDictionary(ZBUFF_CCtx* zbc, const void* dict, size_t dictSize, int compressionLevel)
980c16b537SWarner Losh {
99*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_reset(zbc, ZSTD_reset_session_only), "");
100*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_setParameter(zbc, ZSTD_c_compressionLevel, compressionLevel), "");
101*5ff13fbcSAllan Jude     FORWARD_IF_ERROR(ZSTD_CCtx_loadDictionary(zbc, dict, dictSize), "");
102*5ff13fbcSAllan Jude     return 0;
1030c16b537SWarner Losh }
1040c16b537SWarner Losh 
ZBUFF_compressInit(ZBUFF_CCtx * zbc,int compressionLevel)1050c16b537SWarner Losh size_t ZBUFF_compressInit(ZBUFF_CCtx* zbc, int compressionLevel)
1060c16b537SWarner Losh {
1070c16b537SWarner Losh     return ZSTD_initCStream(zbc, compressionLevel);
1080c16b537SWarner Losh }
1090c16b537SWarner Losh 
1100c16b537SWarner Losh /* ======   Compression   ====== */
1110c16b537SWarner Losh 
1120c16b537SWarner Losh 
ZBUFF_compressContinue(ZBUFF_CCtx * zbc,void * dst,size_t * dstCapacityPtr,const void * src,size_t * srcSizePtr)1130c16b537SWarner Losh size_t ZBUFF_compressContinue(ZBUFF_CCtx* zbc,
1140c16b537SWarner Losh                               void* dst, size_t* dstCapacityPtr,
1150c16b537SWarner Losh                         const void* src, size_t* srcSizePtr)
1160c16b537SWarner Losh {
1170c16b537SWarner Losh     size_t result;
1180c16b537SWarner Losh     ZSTD_outBuffer outBuff;
1190c16b537SWarner Losh     ZSTD_inBuffer inBuff;
1200c16b537SWarner Losh     outBuff.dst = dst;
1210c16b537SWarner Losh     outBuff.pos = 0;
1220c16b537SWarner Losh     outBuff.size = *dstCapacityPtr;
1230c16b537SWarner Losh     inBuff.src = src;
1240c16b537SWarner Losh     inBuff.pos = 0;
1250c16b537SWarner Losh     inBuff.size = *srcSizePtr;
1260c16b537SWarner Losh     result = ZSTD_compressStream(zbc, &outBuff, &inBuff);
1270c16b537SWarner Losh     *dstCapacityPtr = outBuff.pos;
1280c16b537SWarner Losh     *srcSizePtr = inBuff.pos;
1290c16b537SWarner Losh     return result;
1300c16b537SWarner Losh }
1310c16b537SWarner Losh 
1320c16b537SWarner Losh 
1330c16b537SWarner Losh 
1340c16b537SWarner Losh /* ======   Finalize   ====== */
1350c16b537SWarner Losh 
ZBUFF_compressFlush(ZBUFF_CCtx * zbc,void * dst,size_t * dstCapacityPtr)1360c16b537SWarner Losh size_t ZBUFF_compressFlush(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
1370c16b537SWarner Losh {
1380c16b537SWarner Losh     size_t result;
1390c16b537SWarner Losh     ZSTD_outBuffer outBuff;
1400c16b537SWarner Losh     outBuff.dst = dst;
1410c16b537SWarner Losh     outBuff.pos = 0;
1420c16b537SWarner Losh     outBuff.size = *dstCapacityPtr;
1430c16b537SWarner Losh     result = ZSTD_flushStream(zbc, &outBuff);
1440c16b537SWarner Losh     *dstCapacityPtr = outBuff.pos;
1450c16b537SWarner Losh     return result;
1460c16b537SWarner Losh }
1470c16b537SWarner Losh 
1480c16b537SWarner Losh 
ZBUFF_compressEnd(ZBUFF_CCtx * zbc,void * dst,size_t * dstCapacityPtr)1490c16b537SWarner Losh size_t ZBUFF_compressEnd(ZBUFF_CCtx* zbc, void* dst, size_t* dstCapacityPtr)
1500c16b537SWarner Losh {
1510c16b537SWarner Losh     size_t result;
1520c16b537SWarner Losh     ZSTD_outBuffer outBuff;
1530c16b537SWarner Losh     outBuff.dst = dst;
1540c16b537SWarner Losh     outBuff.pos = 0;
1550c16b537SWarner Losh     outBuff.size = *dstCapacityPtr;
1560c16b537SWarner Losh     result = ZSTD_endStream(zbc, &outBuff);
1570c16b537SWarner Losh     *dstCapacityPtr = outBuff.pos;
1580c16b537SWarner Losh     return result;
1590c16b537SWarner Losh }
1600c16b537SWarner Losh 
1610c16b537SWarner Losh 
1620c16b537SWarner Losh 
1630c16b537SWarner Losh /* *************************************
1640c16b537SWarner Losh *  Tool functions
1650c16b537SWarner Losh ***************************************/
ZBUFF_recommendedCInSize(void)1660c16b537SWarner Losh size_t ZBUFF_recommendedCInSize(void)  { return ZSTD_CStreamInSize(); }
ZBUFF_recommendedCOutSize(void)1670c16b537SWarner Losh size_t ZBUFF_recommendedCOutSize(void) { return ZSTD_CStreamOutSize(); }
168