ZSTD_compress_usingCDict() correctly provides original size by default in frame header

Fixed dictionary examples
dev
Yann Collet 2016-08-03 01:57:57 +02:00
parent 6a21971f4a
commit 0763905f44
4 changed files with 20 additions and 22 deletions

View File

@ -56,4 +56,8 @@ test: all
./simple_compression tmp ./simple_compression tmp
@echo starting simple_decompression @echo starting simple_decompression
./simple_decompression tmp.zst ./simple_decompression tmp.zst
@echo dictionary compression
./dictionary_compression tmp README.md
@echo dictionary decompression
./dictionary_decompression tmp.zst README.md
@echo tests completed @echo tests completed

View File

@ -165,4 +165,5 @@ int main(int argc, const char** argv)
ZSTD_freeCDict(dictPtr); ZSTD_freeCDict(dictPtr);
printf("All %u files compressed. \n", argc-2); printf("All %u files compressed. \n", argc-2);
return 0;
} }

View File

@ -135,4 +135,5 @@ int main(int argc, const char** argv)
ZSTD_freeDDict(dictPtr); ZSTD_freeDDict(dictPtr);
printf("All %u files correctly decoded (in memory) \n", argc-2); printf("All %u files correctly decoded (in memory) \n", argc-2);
return 0;
} }

View File

@ -2621,22 +2621,6 @@ size_t ZSTD_compressEnd (ZSTD_CCtx* cctx,
} }
/*! ZSTD_compress_usingPreparedCCtx() :
* Same as ZSTD_compress_usingDict, but using a reference context `preparedCCtx`, where dictionary has been loaded.
* It avoids reloading the dictionary each time.
* `preparedCCtx` must have been properly initialized using ZSTD_compressBegin_usingDict() or ZSTD_compressBegin_advanced().
* Requires 2 contexts : 1 for reference (preparedCCtx) which will not be modified, and 1 to run the compression operation (cctx) */
static size_t ZSTD_compress_usingPreparedCCtx(ZSTD_CCtx* cctx, const ZSTD_CCtx* preparedCCtx,
void* dst, size_t dstCapacity,
const void* src, size_t srcSize)
{
size_t const errorCode = ZSTD_copyCCtx(cctx, preparedCCtx);
if (ZSTD_isError(errorCode)) return errorCode;
return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
}
static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx, static size_t ZSTD_compress_internal (ZSTD_CCtx* cctx,
void* dst, size_t dstCapacity, void* dst, size_t dstCapacity,
const void* src, size_t srcSize, const void* src, size_t srcSize,
@ -2732,9 +2716,7 @@ ZSTD_CDict* ZSTD_createCDict_advanced(const void* dict, size_t dictSize, ZSTD_pa
ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel) ZSTD_CDict* ZSTD_createCDict(const void* dict, size_t dictSize, int compressionLevel)
{ {
ZSTD_customMem const allocator = { NULL, NULL, NULL }; ZSTD_customMem const allocator = { NULL, NULL, NULL };
ZSTD_parameters params; ZSTD_parameters params = ZSTD_getParams(compressionLevel, 0, dictSize);
memset(&params, 0, sizeof(params));
params.cParams = ZSTD_getCParams(compressionLevel, 0, dictSize);
params.fParams.contentSizeFlag = 1; params.fParams.contentSizeFlag = 1;
return ZSTD_createCDict_advanced(dict, dictSize, params, allocator); return ZSTD_createCDict_advanced(dict, dictSize, params, allocator);
} }
@ -2749,14 +2731,24 @@ size_t ZSTD_freeCDict(ZSTD_CDict* cdict)
return 0; return 0;
} }
/*! ZSTD_compress_usingCDict() :
* Compression using a digested Dictionary.
* Faster startup than ZSTD_compress_usingDict(), recommended when same dictionary is used multiple times.
* Note that compression level is decided during dictionary creation */
ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx, ZSTDLIB_API size_t ZSTD_compress_usingCDict(ZSTD_CCtx* cctx,
void* dst, size_t dstCapacity, void* dst, size_t dstCapacity,
const void* src, size_t srcSize, const void* src, size_t srcSize,
const ZSTD_CDict* cdict) const ZSTD_CDict* cdict)
{ {
return ZSTD_compress_usingPreparedCCtx(cctx, cdict->refContext, size_t const errorCode = ZSTD_copyCCtx(cctx, cdict->refContext);
dst, dstCapacity, if (ZSTD_isError(errorCode)) return errorCode;
src, srcSize);
if (cdict->refContext->params.fParams.contentSizeFlag==1) {
cctx->params.fParams.contentSizeFlag = 1;
cctx->frameContentSize = srcSize;
}
return ZSTD_compressEnd(cctx, dst, dstCapacity, src, srcSize);
} }