]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
zlib_wrapper's uncompress() uses ZSTD_isFrame() for routing
authorYann Collet <cyan@fb.com>
Fri, 2 Jun 2017 21:24:58 +0000 (14:24 -0700)
committerYann Collet <cyan@fb.com>
Fri, 2 Jun 2017 21:27:11 +0000 (14:27 -0700)
more generic and safer than using own routing for magic number comparison

lib/common/zstd_common.c
lib/decompress/zstd_decompress.c
zlibWrapper/zstd_zlibwrapper.c

index 93187d5c0175ba09099d0e10fed35beb7b1cb5e7..f68167238158d25dc0710cadce7b7a1ee2ab7c2d 100644 (file)
@@ -22,7 +22,7 @@
 /*-****************************************
 *  Version
 ******************************************/
-unsigned ZSTD_versionNumber (void) { return ZSTD_VERSION_NUMBER; }
+unsigned ZSTD_versionNumber(void) { return ZSTD_VERSION_NUMBER; }
 
 const char* ZSTD_versionString(void) { return ZSTD_VERSION_STRING; }
 
index 7dfcddb1b407e44e073499bccb39c645cc748cfd..766ff0ff65ee854c22eea661c4e89ff226e05575 100644 (file)
@@ -204,16 +204,14 @@ static void ZSTD_initDCtx_internal(ZSTD_DCtx* dctx)
 
 ZSTD_DCtx* ZSTD_createDCtx_advanced(ZSTD_customMem customMem)
 {
-    ZSTD_DCtx* dctx;
-
-    if (!customMem.customAlloc ^ !customMem.customFree)
-        return NULL;
+    if (!customMem.customAlloc ^ !customMem.customFree) return NULL;
 
-    dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(ZSTD_DCtx), customMem);
-    if (!dctx) return NULL;
-    memcpy(&dctx->customMem, &customMem, sizeof(customMem));
-    ZSTD_initDCtx_internal(dctx);
-    return dctx;
+    {   ZSTD_DCtx* const dctx = (ZSTD_DCtx*)ZSTD_malloc(sizeof(*dctx), customMem);
+        if (!dctx) return NULL;
+        dctx->customMem = customMem;
+        ZSTD_initDCtx_internal(dctx);
+        return dctx;
+    }
 }
 
 ZSTD_DCtx* ZSTD_initStaticDCtx(void *workspace, size_t workspaceSize)
index 7a0b09db693948bcb13a15809787e767f845d32c..ed68a8a0715cfde62ad127659609a27684c942f7 100644 (file)
@@ -15,7 +15,7 @@
 #define ZLIB_CONST
 #include <zlib.h>                  /* without #define Z_PREFIX */
 #include "zstd_zlibwrapper.h"
-#define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_MAGICNUMBER */
+#define ZSTD_STATIC_LINKING_ONLY   /* ZSTD_isFrame, ZSTD_MAGICNUMBER */
 #include "zstd.h"
 #include "zstd_internal.h"         /* ZSTD_malloc, ZSTD_free */
 
@@ -1004,9 +1004,9 @@ ZEXTERN int ZEXPORT z_compress2 OF((Bytef *dest,   uLongf *destLen,
         return compress2(dest, destLen, source, sourceLen, level);
 
     { size_t dstCapacity = *destLen;
-      size_t const errorCode = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
-      if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
-      *destLen = errorCode;
+      size_t const cSize = ZSTD_compress(dest, dstCapacity, source, sourceLen, level);
+      if (ZSTD_isError(cSize)) return Z_STREAM_ERROR;
+      *destLen = cSize;
     }
     return Z_OK;
 }
@@ -1024,13 +1024,13 @@ ZEXTERN uLong ZEXPORT z_compressBound OF((uLong sourceLen))
 ZEXTERN int ZEXPORT z_uncompress OF((Bytef *dest,   uLongf *destLen,
                                    const Bytef *source, uLong sourceLen))
 {
-    if (sourceLen < 4 || MEM_readLE32(source) != ZSTD_MAGICNUMBER)
+    if (!ZSTD_isFrame(source, sourceLen))
         return uncompress(dest, destLen, source, sourceLen);
 
     { size_t dstCapacity = *destLen;
-      size_t const errorCode = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
-      if (ZSTD_isError(errorCode)) return Z_STREAM_ERROR;
-      *destLen = errorCode;
+      size_t const dSize = ZSTD_decompress(dest, dstCapacity, source, sourceLen);
+      if (ZSTD_isError(dSize)) return Z_STREAM_ERROR;
+      *destLen = dSize;
      }
     return Z_OK;
 }