]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
added minimum for decoder buffer
authorYann Collet <cyan@fb.com>
Tue, 26 Oct 2021 15:21:31 +0000 (08:21 -0700)
committerYann Collet <cyan@fb.com>
Tue, 26 Oct 2021 15:21:31 +0000 (08:21 -0700)
also : introduced macro BOUNDED()

lib/common/zstd_internal.h
lib/compress/zstd_compress.c
lib/compress/zstd_lazy.c
lib/decompress/zstd_decompress.c
lib/decompress/zstd_decompress_internal.h

index eaa0afc90336980e29aa7c45ee32ceb01e854ed5..aeafac2fa8e495e9a2238d6c74ece3824f16a3ef 100644 (file)
@@ -57,6 +57,7 @@ extern "C" {
 #undef MAX
 #define MIN(a,b) ((a)<(b) ? (a) : (b))
 #define MAX(a,b) ((a)>(b) ? (a) : (b))
+#define BOUNDED(min,val,max) (MAX(min,MIN(val,max)))
 
 
 /*-*************************************
index edfd7eb07356fae068ef00b044216db20354845f..656d3fdfa4aba26afcce1fa7e04421eac2b492da 100644 (file)
@@ -1465,7 +1465,7 @@ static size_t ZSTD_estimateCCtxSize_usingCCtxParams_internal(
         const size_t buffOutSize,
         const U64 pledgedSrcSize)
 {
-    size_t const windowSize = MAX(1, (size_t)MIN(((U64)1 << cParams->windowLog), pledgedSrcSize));
+    size_t const windowSize = BOUNDED(1, 1 << cParams->windowLog, pledgedSrcSize);
     size_t const blockSize = MIN(ZSTD_BLOCKSIZE_MAX, windowSize);
     U32    const divider = (cParams->minMatch==3) ? 3 : 4;
     size_t const maxNbSeq = blockSize / divider;
@@ -1783,7 +1783,7 @@ ZSTD_reset_matchState(ZSTD_matchState_t* ms,
             if (ms->tagTable) ZSTD_memset(ms->tagTable, 0, tagTableSize);
         }
         {   /* Switch to 32-entry rows if searchLog is 5 (or more) */
-            U32 const rowLog = MAX(MIN(cParams->searchLog, 6), 4);
+            U32 const rowLog = BOUNDED(4, cParams->searchLog, 6);
             assert(cParams->hashLog >= rowLog);
             ms->rowHashLog = cParams->hashLog - rowLog;
         }
index 78cc5b09bd370d21275482aca0ed8a9282e76d7d..566da2d4515865b1eead6b8452d616089c441dd5 100644 (file)
@@ -1086,7 +1086,7 @@ FORCE_INLINE_TEMPLATE void ZSTD_row_update_internal(ZSTD_matchState_t* ms, const
  * processing.
  */
 void ZSTD_row_update(ZSTD_matchState_t* const ms, const BYTE* ip) {
-    const U32 rowLog = MAX(MIN(ms->cParams.searchLog, 6), 4);
+    const U32 rowLog = BOUNDED(4, ms->cParams.searchLog, 6);
     const U32 rowMask = (1u << rowLog) - 1;
     const U32 mls = MIN(ms->cParams.minMatch, 6 /* mls caps out at 6 */);
 
index b5ccac53c5dc14595a7a83b8546f9dd876f0a75a..47ca874bf6194a8cf9e1542355dbc5ed78eb7ec5 100644 (file)
@@ -1123,7 +1123,7 @@ static size_t ZSTD_nextSrcSizeToDecompressWithInputSize(ZSTD_DCtx* dctx, size_t
         return dctx->expected;
     if (dctx->bType != bt_raw)
         return dctx->expected;
-    return MIN(MAX(inputSize, 1), dctx->expected);
+    return BOUNDED(1, inputSize, dctx->expected);
 }
 
 ZSTD_nextInputType_e ZSTD_nextInputType(ZSTD_DCtx* dctx) {
index 7f555911d6ee7ad55638b881902b2a367fd2856e..51dc347294d279369da21d0a294b13722d2e4f59 100644 (file)
@@ -110,10 +110,11 @@ typedef struct {
 #  define ZSTD_DECODER_INTERNAL_BUFFER  (1 << 16)
 #endif
 
+#define ZSTD_LBMIN 64
 #define ZSTD_LBMAX (128 << 10)
 
-/* extra buffer, compensates amount of dst required to store litBuffer */
-#define ZSTD_LITBUFFEREXTRASIZE  MIN(ZSTD_DECODER_INTERNAL_BUFFER, ZSTD_LBMAX)
+/* extra buffer, compensates when dst is not large enough to store litBuffer */
+#define ZSTD_LITBUFFEREXTRASIZE  BOUNDED(ZSTD_LBMIN, ZSTD_DECODER_INTERNAL_BUFFER, ZSTD_LBMAX)
 
 typedef enum {
     ZSTD_not_in_dst = 0,  /* Stored entirely within litExtraBuffer */