]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Add test case to roundtrip using ZSTD_getSequences() and ZSTD_compressSequences()
authorsenhuang42 <senhuang96@fb.com>
Tue, 3 Nov 2020 23:53:44 +0000 (18:53 -0500)
committersenhuang42 <senhuang96@fb.com>
Mon, 16 Nov 2020 15:49:16 +0000 (10:49 -0500)
lib/compress/zstd_compress.c
tests/fuzzer.c

index 107b7ea33b6498458fe1424d66b71ebd2b947ab2..80dd3c478b209a8078f18f0e05c931dbd7b0d040 100644 (file)
@@ -3407,7 +3407,6 @@ static size_t ZSTD_writeEpilogue(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity)
     }
 
     if (cctx->stage != ZSTDcs_ending) {
-        DEBUGLOG(4, "did this\n");
         /* write one last empty block, make it the "last" block */
         U32 const cBlockHeader24 = 1 /* last block */ + (((U32)bt_raw)<<1) + 0;
         RETURN_ERROR_IF(dstCapacity<4, dstSize_tooSmall, "no room for epilogue");
@@ -4389,7 +4388,7 @@ size_t ZSTD_compressStream2( ZSTD_CCtx* cctx,
 
     /* transparent initialization stage */
     if (cctx->streamStage == zcss_init) {
-        ZSTD_CCtx_init_compressStream2(cctx, endOp, input->size);
+        FORWARD_IF_ERROR(ZSTD_CCtx_init_compressStream2(cctx, endOp, input->size), "CompressStream2 initialization failed");
         ZSTD_setBufferExpectations(cctx, output, input);    /* Set initial buffer expectations now that we've initialized */
     }
     /* end of transparent initialization stage */
@@ -4661,11 +4660,11 @@ static size_t ZSTD_copySequencesToSeqStore(seqStore_t* seqStore, const ZSTD_sequ
  *
  * Returns the cumulative size of all compressed blocks (including their headers), otherwise a ZSTD error
  */
-size_t ZSTD_compressSequences_internal(void* dst, size_t dstCapacity,
-                                       ZSTD_CCtx* cctx,
-                                       const ZSTD_Sequence* inSeqs, size_t inSeqsSize,
-                                       const void* src, size_t srcSize,
-                                       ZSTD_sequenceFormat_e format) {
+static size_t ZSTD_compressSequences_internal(void* dst, size_t dstCapacity,
+                                              ZSTD_CCtx* cctx,
+                                              const ZSTD_Sequence* inSeqs, size_t inSeqsSize,
+                                              const void* src, size_t srcSize,
+                                              ZSTD_sequenceFormat_e format) {
     U32 cSize = 0;
     U32 lastBlock;
     U32 blockSize;
@@ -4675,7 +4674,6 @@ size_t ZSTD_compressSequences_internal(void* dst, size_t dstCapacity,
     seqStore_t blockSeqStore; 
     blockSeqStore.longLengthID = 0;
     blockSeqStore.longLengthPos = 0;
-    size_t origDstCapacity = dstCapacity;
     
     DEBUGLOG(4, "ZSTD_compressSequences_internal srcSize: %zu, inSeqsSize: %zu", srcSize, inSeqsSize);
     BYTE const* ip = (BYTE const*)src;
index 2afd10935da5ac6fc6db1b6dab49838551db89e7..31e9d4bb90e0c6994e0ccd0b1527f7cf2757a2f0 100644 (file)
@@ -2739,6 +2739,61 @@ static int basicUnitTests(U32 const seed, double compressibility)
         free(seqs);
     }
     DISPLAYLEVEL(3, "OK \n");
+    
+    DISPLAYLEVEL(3, "test%3i : ZSTD_getSequences followed by ZSTD_compressSequences : ", testNb++);
+    {
+        size_t srcSize = 150 KB;
+        BYTE* src = (BYTE*)CNBuffer;
+        BYTE* dst = (BYTE*)compressedBuffer;
+        size_t dstSize = ZSTD_compressBound(srcSize);
+        size_t decompressSize = srcSize;
+        char* decompressBuffer = (char*)malloc(decompressSize);
+        size_t compressedSize;
+        size_t dSize;
+
+        ZSTD_CCtx* cctx = ZSTD_createCCtx();
+        ZSTD_Sequence* seqs = (ZSTD_Sequence*)malloc(srcSize * sizeof(ZSTD_Sequence));
+        size_t seqsSize;
+
+        if (seqs == NULL) goto _output_error;
+        assert(cctx != NULL);
+
+        /* Populate src with random data */
+        RDG_genBuffer(CNBuffer, srcSize, compressibility, 0., seed);
+
+        /* Test with block delimiters roundtrip */
+        seqsSize = ZSTD_getSequences(cctx, seqs, srcSize, src, srcSize, ZSTD_sf_explicitBlockDelimiters);
+        compressedSize = ZSTD_compressSequences(dst, dstSize, seqs, seqsSize, src, srcSize, 3 /* clevel */, ZSTD_sf_explicitBlockDelimiters);
+        if (ZSTD_isError(compressedSize)) {
+            DISPLAY("Error in sequence compression with block delims\n");
+            goto _output_error;
+        }
+        dSize = ZSTD_decompress(decompressBuffer, decompressSize, dst, compressedSize);
+        if (ZSTD_isError(dSize)) {
+            DISPLAY("Error in sequence compression roundtrip with block delims\n");
+            goto _output_error;
+        }
+        assert(!memcmp(decompressBuffer, src, srcSize));
+
+        /* Test with no block delimiters roundtrip */
+        seqsSize = ZSTD_getSequences(cctx, seqs, srcSize, src, srcSize, ZSTD_sf_noBlockDelimiters);
+        compressedSize = ZSTD_compressSequences(dst, dstSize, seqs, seqsSize, src, srcSize, 3 /* clevel */, ZSTD_sf_noBlockDelimiters);
+        if (ZSTD_isError(compressedSize)) {
+            DISPLAY("Error in sequence compression with no block delims\n");
+            goto _output_error;
+        }
+        dSize = ZSTD_decompress(decompressBuffer, decompressSize, dst, compressedSize);
+        if (ZSTD_isError(dSize)) {
+            DISPLAY("Error in sequence compression roundtrip with no block delims\n");
+            goto _output_error;
+        }
+        assert(!memcmp(decompressBuffer, src, srcSize));
+
+        ZSTD_freeCCtx(cctx);
+        free(decompressBuffer);
+        free(seqs);
+    }
+    DISPLAYLEVEL(3, "OK \n");
 
     /* Multiple blocks of zeros test */
     #define LONGZEROSLENGTH 1000000 /* 1MB of zeros */