]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Add support for uncompressible blocks
authorsenhuang42 <senhuang96@fb.com>
Thu, 29 Oct 2020 14:24:45 +0000 (10:24 -0400)
committersenhuang42 <senhuang96@fb.com>
Mon, 16 Nov 2020 15:49:16 +0000 (10:49 -0500)
lib/compress/zstd_compress.c
lib/compress/zstd_compress_internal.h

index c552a854a6024aef31539fe1e7a435886e0534fe..29b64b30ca3930074718eafd1b64677c8d49cd18 100644 (file)
@@ -2927,6 +2927,7 @@ static size_t ZSTD_writeFrameHeader(void* dst, size_t dstCapacity,
         case 3 : MEM_writeLE64(op+pos, (U64)(pledgedSrcSize)); pos+=8; break;
     }
     printBits(pos, op);
+    DEBUGLOG(4, "frame header size: %u", pos);
     return pos;
 }
 
@@ -4540,7 +4541,7 @@ static size_t ZSTD_copySequencesToSeqStore(ZSTD_CCtx* zc, const ZSTD_sequenceRan
         U32 litLength = inSeqs[idx].litLength;
         U32 matchLength = inSeqs[idx].matchLength;
         U32 offCode = inSeqs[idx].offset + ZSTD_REP_MOVE;
-        //DEBUGLOG(4, "Seqstore idx: %zu, seq: (ll: %u, ml: %u, of: %u)", idx, litLength, matchLength, offCode);
+        DEBUGLOG(4, "Seqstore idx: %zu, seq: (ll: %u, ml: %u, of: %u)", idx, litLength, matchLength, offCode);
 
         /* Adjust litLength and matchLength for the sequence at startIdx */
         if (idx == seqRange->startIdx) {
@@ -4609,8 +4610,15 @@ size_t ZSTD_compressSequences_ext_internal(void* dst, size_t dstCapacity,
                                            const ZSTD_Sequence* inSeqs, size_t inSeqsSize,
                                            const void* src, size_t srcSize) {
     U32 cSize;
-    U32 lastBlock = 1;
+    U32 lastBlock;
     ZSTD_sequenceRange seqRange = {0, 0, 0, 0};
+    /* Derive the appropriate block size */
+    size_t const windowSize = MAX(1, (size_t)MIN(((U64)1 << cctx->appliedParams.cParams.windowLog), srcSize));
+    size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, windowSize);
+
+    BYTE const* ip = (BYTE const*)src;
+    BYTE* op = (BYTE*)dst;
+
     ZSTD_updateSequenceRange(&seqRange, srcSize, inSeqs, inSeqsSize);
 
     ZSTD_copySequencesToSeqStore(cctx, &seqRange, inSeqs, inSeqsSize, src, srcSize);
@@ -4622,7 +4630,13 @@ size_t ZSTD_compressSequences_ext_internal(void* dst, size_t dstCapacity,
             srcSize,
             cctx->entropyWorkspace, ENTROPY_WORKSPACE_SIZE /* statically allocated in resetCCtx */,
             cctx->bmi2);
-
+    if (ZSTD_isError(cSize)) {
+        printf("ERROR\n");
+    } else if (cSize == 0) {
+        DEBUGLOG(4, "NO compress BLOCK");
+        cSize = ZSTD_noCompressBlock(dst, dstCapacity, src, srcSize, lastBlock);
+        return cSize;
+    }
     DEBUGLOG(4, "Compressed sequences size : %u", cSize);
     /* Error checking */
     if (!ZSTD_isError(cSize) && cSize > 1) {
@@ -4698,6 +4712,7 @@ size_t ZSTD_compressSequences_ext(void* dst, size_t dstCapacity,
     cSize += ZSTD_compressSequences_ext_internal(op, dstCapacity,
                                                 cctx, inSeqs, inSeqsSize,
                                                 src, srcSize);
+    DEBUGLOG(4, "Final compressed size: %u\n", cSize);                            
     dstCapacity -= cSize;
 
     if (cctx->appliedParams.fParams.checksumFlag) {
index 3ff318d5348ef8f9e6512aa26418f26b61ba9bd2..0a1da925fca8ef0468420935128c2ac5412d5e17 100644 (file)
@@ -432,6 +432,7 @@ MEM_STATIC size_t ZSTD_noCompressBlock (void* dst, size_t dstCapacity, const voi
                     dstSize_tooSmall, "dst buf too small for uncompressed block");
     MEM_writeLE24(dst, cBlockHeader24);
     ZSTD_memcpy((BYTE*)dst + ZSTD_blockHeaderSize, src, srcSize);
+    DEBUGLOG(4, "nocompress: %u", ZSTD_blockHeaderSize + srcSize);
     return ZSTD_blockHeaderSize + srcSize;
 }