]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
Fix ZSTD_initCStream_usingCDict() to use dictionary
authorNick Terrell <terrelln@fb.com>
Tue, 4 Apr 2017 03:56:39 +0000 (20:56 -0700)
committerNick Terrell <terrelln@fb.com>
Tue, 4 Apr 2017 04:05:59 +0000 (21:05 -0700)
lib/compress/zstd_compress.c
tests/zstreamtest.c

index e1c734927563a6a5b4553a90aee7ff11b6a446ca..03b9758e818d8b2db18f36b60f0c7a1a3ce54326 100644 (file)
@@ -3068,7 +3068,8 @@ size_t ZSTD_initCStream_usingCDict(ZSTD_CStream* zcs, const ZSTD_CDict* cdict)
     size_t const initError =  ZSTD_initCStream_advanced(zcs, NULL, 0, params, 0);
     zcs->cdict = cdict;
     zcs->cctx->dictID = params.fParams.noDictIDFlag ? 0 : cdict->refContext->dictID;
-    return initError;
+    if (ZSTD_isError(initError)) return initError;
+    return ZSTD_resetCStream_internal(zcs, 0);
 }
 
 size_t ZSTD_initCStream_usingDict(ZSTD_CStream* zcs, const void* dict, size_t dictSize, int compressionLevel)
index 10bcbe0cdd75a14db31ac629feff43edce050dda..e1e68b01cf021d486e22c4f8492dc65b49903f47 100644 (file)
@@ -438,6 +438,34 @@ static int basicUnitTests(U32 seed, double compressibility, ZSTD_customMem custo
       if (!ZSTD_isError(r)) goto _output_error;  /* must fail : frame requires > 100 bytes */
       DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
 
+    DISPLAYLEVEL(3, "test%3i : check dictionary used : ", testNb++);
+    {   ZSTD_parameters params = ZSTD_getParams(1, CNBufferSize, dictionary.filled);
+        ZSTD_CDict* cdict;
+        params.fParams.noDictIDFlag = 1;
+        cdict = ZSTD_createCDict_advanced(dictionary.start, dictionary.filled, 1, params, customMem);
+        { size_t const initError = ZSTD_initCStream_usingCDict(zc, cdict);
+          if (ZSTD_isError(initError)) goto _output_error; }
+        cSize = 0;
+        outBuff.dst = compressedBuffer;
+        outBuff.size = compressedBufferSize;
+        outBuff.pos = 0;
+        inBuff.src = CNBuffer;
+        inBuff.size = CNBufferSize;
+        inBuff.pos = 0;
+        { size_t const r = ZSTD_compressStream(zc, &outBuff, &inBuff);
+          if (ZSTD_isError(r)) goto _output_error; }
+        if (inBuff.pos != inBuff.size) goto _output_error;   /* entire input should be consumed */
+        { size_t const r = ZSTD_endStream(zc, &outBuff);
+          if (r != 0) goto _output_error; }  /* error, or some data not flushed */
+        cSize = outBuff.pos;
+        ZSTD_freeCDict(cdict);
+        DISPLAYLEVEL(3, "OK (%u bytes : %.2f%%)\n", (U32)cSize, (double)cSize/CNBufferSize*100);
+
+        { size_t const r = ZSTD_decompress(decodedBuffer, CNBufferSize, compressedBuffer, cSize);
+          if (!ZSTD_isError(r)) goto _output_error; /* must fail : dictionary not used */
+          DISPLAYLEVEL(3, "OK (%s)\n", ZSTD_getErrorName(r)); }
+    }
+
     /* Unknown srcSize */
     DISPLAYLEVEL(3, "test%3i : pledgedSrcSize == 0 behaves properly : ", testNb++);
     {   ZSTD_parameters params = ZSTD_getParams(5, 0, 0);