]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Fixed : dictBuilder fails if first sample is too small
authorYann Collet <yann.collet.73@gmail.com>
Wed, 22 Jun 2016 09:05:34 +0000 (11:05 +0200)
committerYann Collet <yann.collet.73@gmail.com>
Wed, 22 Jun 2016 09:05:34 +0000 (11:05 +0200)
lib/dictBuilder/zdict.c

index 0814581fe63fc23623ed45aba9726471a376688b..031509453b66d2162a14dd237771cc4688a4f970 100644 (file)
@@ -584,48 +584,51 @@ static void ZDICT_countEStats(EStats_ress_t esr,
                             U32* countLit, U32* offsetcodeCount, U32* matchlengthCount, U32* litlengthCount, U32* repOffsets,
                             const void* src, size_t srcSize)
 {
-    const seqStore_t* seqStorePtr;
+    size_t cSize;
 
     if (srcSize > ZSTD_BLOCKSIZE_MAX) srcSize = ZSTD_BLOCKSIZE_MAX;   /* protection vs large samples */
     ZSTD_copyCCtx(esr.zc, esr.ref);
-    ZSTD_compressBlock(esr.zc, esr.workPlace, ZSTD_BLOCKSIZE_MAX, src, srcSize);
-    seqStorePtr = ZSTD_getSeqStore(esr.zc);
+    cSize = ZSTD_compressBlock(esr.zc, esr.workPlace, ZSTD_BLOCKSIZE_MAX, src, srcSize);
+    if (ZSTD_isError(cSize)) { DISPLAYLEVEL(1, "warning : could not compress sample size %u \n", (U32)srcSize); return; }
 
-    /* literals stats */
-    {   const BYTE* bytePtr;
-        for(bytePtr = seqStorePtr->litStart; bytePtr < seqStorePtr->lit; bytePtr++)
-            countLit[*bytePtr]++;
-    }
-
-    /* seqStats */
-    {   size_t const nbSeq = (size_t)(seqStorePtr->offset - seqStorePtr->offsetStart);
-        ZSTD_seqToCodes(seqStorePtr, nbSeq);
+    if (cSize) {  /* if == 0; block is not compressible */
+        const seqStore_t* seqStorePtr = ZSTD_getSeqStore(esr.zc);
 
-        {   const BYTE* codePtr = seqStorePtr->offCodeStart;
-            size_t u;
-            for (u=0; u<nbSeq; u++) offsetcodeCount[codePtr[u]]++;
+        /* literals stats */
+        {   const BYTE* bytePtr;
+            for(bytePtr = seqStorePtr->litStart; bytePtr < seqStorePtr->lit; bytePtr++)
+                countLit[*bytePtr]++;
         }
 
-        {   const BYTE* codePtr = seqStorePtr->mlCodeStart;
-            size_t u;
-            for (u=0; u<nbSeq; u++) matchlengthCount[codePtr[u]]++;
-        }
+        /* seqStats */
+        {   size_t const nbSeq = (size_t)(seqStorePtr->offset - seqStorePtr->offsetStart);
+            ZSTD_seqToCodes(seqStorePtr, nbSeq);
 
-        {   const BYTE* codePtr = seqStorePtr->llCodeStart;
-            size_t u;
-            for (u=0; u<nbSeq; u++) litlengthCount[codePtr[u]]++;
-    }   }
+            {   const BYTE* codePtr = seqStorePtr->offCodeStart;
+                size_t u;
+                for (u=0; u<nbSeq; u++) offsetcodeCount[codePtr[u]]++;
+            }
 
-    /* rep offsets */
-    {   const U32* const offsetPtr = seqStorePtr->offsetStart;
-        U32 offset1 = offsetPtr[0] - 3;
-        U32 offset2 = offsetPtr[1] - 3;
-        if (offset1 >= MAXREPOFFSET) offset1 = 0;
-        if (offset2 >= MAXREPOFFSET) offset2 = 0;
-        repOffsets[offset1] += 3;
-        repOffsets[offset2] += 1;
-    }
+            {   const BYTE* codePtr = seqStorePtr->mlCodeStart;
+                size_t u;
+                for (u=0; u<nbSeq; u++) matchlengthCount[codePtr[u]]++;
+            }
+
+            {   const BYTE* codePtr = seqStorePtr->llCodeStart;
+                size_t u;
+                for (u=0; u<nbSeq; u++) litlengthCount[codePtr[u]]++;
+        }   }
 
+        /* rep offsets */
+        {   const U32* const offsetPtr = seqStorePtr->offsetStart;
+            U32 offset1 = offsetPtr[0] - 3;
+            U32 offset2 = offsetPtr[1] - 3;
+            if (offset1 >= MAXREPOFFSET) offset1 = 0;
+            if (offset2 >= MAXREPOFFSET) offset2 = 0;
+            repOffsets[offset1] += 3;
+            repOffsets[offset2] += 1;
+        }
+    }
 }
 
 /*