From da3fbcb3023369690e31c9691f1580a491b704c3 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Fri, 19 Aug 2016 14:23:58 +0200 Subject: [PATCH] Added ZDICT_getDictID() --- lib/dictBuilder/zdict.c | 7 +++++++ lib/dictBuilder/zdict.h | 1 + tests/fuzzer.c | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/lib/dictBuilder/zdict.c b/lib/dictBuilder/zdict.c index 0e44c425e..b2fb7c08f 100644 --- a/lib/dictBuilder/zdict.c +++ b/lib/dictBuilder/zdict.c @@ -126,6 +126,13 @@ unsigned ZDICT_isError(size_t errorCode) { return ERR_isError(errorCode); } const char* ZDICT_getErrorName(size_t errorCode) { return ERR_getErrorName(errorCode); } +unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize) +{ + if (dictSize < 8) return 0; + if (MEM_readLE32(dictBuffer) != ZSTD_DICT_MAGIC) return 0; + return MEM_readLE32((const char*)dictBuffer + 4); +} + /*-******************************************************** * Dictionary training functions diff --git a/lib/dictBuilder/zdict.h b/lib/dictBuilder/zdict.h index 67dba70d7..68349da05 100644 --- a/lib/dictBuilder/zdict.h +++ b/lib/dictBuilder/zdict.h @@ -68,6 +68,7 @@ ZDICTLIB_API size_t ZDICT_trainFromBuffer(void* dictBuffer, size_t dictBufferCap /*====== Helper functions ======*/ +ZDICTLIB_API unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize); /**< extracts dictID; @return zero if error (not a valid dictionary) */ ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode); ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode); diff --git a/tests/fuzzer.c b/tests/fuzzer.c index be36d74bb..87647b9d1 100644 --- a/tests/fuzzer.c +++ b/tests/fuzzer.c @@ -272,6 +272,12 @@ static int basicUnitTests(U32 seed, double compressibility) if (ZDICT_isError(dictSize)) goto _output_error; DISPLAYLEVEL(4, "OK, created dictionary of size %u \n", (U32)dictSize); + DISPLAYLEVEL(4, "test%3i : check dictID : ", testNb++); + { U32 const dictID = ZDICT_getDictID(dictBuffer, dictSize); + if (dictID==0) goto _output_error; + DISPLAYLEVEL(4, "OK : %u \n", dictID); + } + DISPLAYLEVEL(4, "test%3i : compress with dictionary : ", testNb++); cSize = ZSTD_compress_usingDict(cctx, compressedBuffer, ZSTD_compressBound(CNBuffSize), CNBuffer, CNBuffSize,