]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Use ZSTD Error codes, improve explanation of ZSTD_loadCEntropy() and ZSTD_loadDEntropy()
authorSen Huang <senhuang96@fb.com>
Thu, 7 Nov 2019 16:46:25 +0000 (11:46 -0500)
committerSen Huang <senhuang96@fb.com>
Fri, 8 Nov 2019 18:57:26 +0000 (13:57 -0500)
lib/compress/zstd_compress.c
lib/compress/zstd_compress_internal.h
lib/decompress/zstd_decompress_internal.h
lib/dictBuilder/zdict.c
lib/dictBuilder/zdict.h

index 3c04718e3e428d3ec6800675dd6e04625784132d..edc238b8b13ba5a30e77bd24b49d7df47526187a 100644 (file)
@@ -2772,7 +2772,7 @@ size_t ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace,
                          short* offcodeNCount, unsigned* offcodeMaxValue,
                          const void* const dict, size_t dictSize) 
 {
-    const BYTE* dictPtr = (const BYTE*)dict + 8;
+    const BYTE* dictPtr = (const BYTE*)dict + 8;    /* skip magic num and dict ID */
     const BYTE* const dictEnd = dictPtr + dictSize;
 
     {   unsigned maxSymbolValue = 255;
@@ -2869,7 +2869,7 @@ static size_t ZSTD_loadZstdDictionary(ZSTD_compressedBlockState_t* bs,
     dictID = params->fParams.noDictIDFlag ? 0 :  MEM_readLE32(dictPtr);
     dictPtr += 4;
 
-    dictPtr += eSize - 8;   /* size of header + magic number already accounted for */
+    dictPtr += eSize - 8;
 
     {   size_t const dictContentSize = (size_t)(dictEnd - dictPtr);
         U32 offcodeMax = MaxOff;
index 62ee3f9bc64d1a54046238aee7fe6e0261464cfe..0811ccf9a2829c17ffa036255e2973e640e990ec 100644 (file)
@@ -931,13 +931,13 @@ MEM_STATIC void ZSTD_debugTable(const U32* table, U32 max)
 }
 #endif
 /* ===============================================================
- * Public declarations
+ * Shared internal declarations
  * These prototypes may be called from sources not in lib/compress
  * =============================================================== */
 
 /* ZSTD_loadCEntropy() :
  * dict : must point at beginning of a valid zstd dictionary.
- * return : size of entropy tables read */
+ * return : size of dictionary header (size of magic number + dict ID + entropy tables) */
 size_t ZSTD_loadCEntropy(ZSTD_compressedBlockState_t* bs, void* workspace,
                          short* offcodeNCount, unsigned* offcodeMaxValue,
                          const void* const dict, size_t dictSize);
index ccbdfa090faa2fa133f7281e9817b291ab70c55f..99eab854ce1ccded64ee2fd4bcbf10d60c4ef9e5 100644 (file)
@@ -160,7 +160,7 @@ struct ZSTD_DCtx_s
 
 /*! ZSTD_loadDEntropy() :
  *  dict : must point at beginning of a valid zstd dictionary.
- * @return : size of entropy tables read */
+ * @return : size of dictionary header (size of magic number + dict ID + entropy tables) */
 size_t ZSTD_loadDEntropy(ZSTD_entropyDTables_t* entropy,
                    const void* const dict, size_t const dictSize);
 
index 6d76fb5218a5a8e28bf25bbf1aab04f89efa62c6..1c0915fe37a7fe11fd60f1d635151b6d6ba3cef4 100644 (file)
@@ -102,22 +102,22 @@ unsigned ZDICT_getDictID(const void* dictBuffer, size_t dictSize)
 
 size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize)
 {
-    if (dictSize <= 8 || MEM_readLE32(dictBuffer) != ZSTD_MAGIC_DICTIONARY) return 0;
+    if (dictSize <= 8 || MEM_readLE32(dictBuffer) != ZSTD_MAGIC_DICTIONARY) return ERROR(dictionary_corrupted);
 
     {   size_t headerSize;
         unsigned offcodeMaxValue = MaxOff;
         ZSTD_compressedBlockState_t* dummyBs = (ZSTD_compressedBlockState_t*)malloc(sizeof(ZSTD_compressedBlockState_t));
         U32* wksp = (U32*)malloc(HUF_WORKSPACE_SIZE);
         short* offcodeNCount = (short*)malloc((MaxOff+1)*sizeof(short));
-        if (!dummyBs || !wksp) {
-            return 0;
+        if (!dummyBs || !wksp || !offcodeNCount) {
+            return ERROR(memory_allocation);
         }
 
         headerSize = ZSTD_loadCEntropy(dummyBs, wksp, offcodeNCount, &offcodeMaxValue, dictBuffer, dictSize);
         free(dummyBs);
         free(wksp);
         free(offcodeNCount);
-        return headerSize;
+        return headerSize;  /* this may be an error value if ZSTD_loadCEntropy() encountered an error */
     }
 }
 
index bb89f1f9f9dbbdab6ee891d109d4537ddd83e387..1313bd214485a4adac2e9ed4acd8507cfda77d72 100644 (file)
@@ -64,7 +64,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 size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize);  /* returns dict header size; returns zero if error (not a valid dictionary or mem alloc failure) */
+ZDICTLIB_API size_t ZDICT_getDictHeaderSize(const void* dictBuffer, size_t dictSize);  /* returns dict header size; returns a ZSTD error code on failure */
 ZDICTLIB_API unsigned ZDICT_isError(size_t errorCode);
 ZDICTLIB_API const char* ZDICT_getErrorName(size_t errorCode);