]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Added ZDICT_getDictID()
authorYann Collet <yann.collet.73@gmail.com>
Fri, 19 Aug 2016 12:23:58 +0000 (14:23 +0200)
committerYann Collet <yann.collet.73@gmail.com>
Fri, 19 Aug 2016 12:23:58 +0000 (14:23 +0200)
lib/dictBuilder/zdict.c
lib/dictBuilder/zdict.h
tests/fuzzer.c

index 0e44c425e4b40bf5cb75c7522314464ce8a63bd9..b2fb7c08f75397e0161fb35bbacb4510682eec11 100644 (file)
@@ -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
index 67dba70d71dbca6c3b5af84ec2bd205ea25a8855..68349da05dc84a339ff5df54ee5921c0086c56c8 100644 (file)
@@ -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);
 
index be36d74bbbd45ded8435f4d8baaec265c8b55154..87647b9d1e72faaf7bc5ec0dd2f8b0419107cb8c 100644 (file)
@@ -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,