From 202082f285ed5a9d971b29a42f52ae5bf9995584 Mon Sep 17 00:00:00 2001
From: Yann Collet
Date: Fri, 28 Apr 2017 16:56:39 -0700
Subject: [PATCH] sync bitstream from FSE project
add assert into unsafe *_fast() variants
---
doc/zstd_manual.html | 2 +-
lib/common/bitstream.h | 17 +++++++++++++++--
2 files changed, 16 insertions(+), 3 deletions(-)
diff --git a/doc/zstd_manual.html b/doc/zstd_manual.html
index 4755396e3..2e77e7742 100644
--- a/doc/zstd_manual.html
+++ b/doc/zstd_manual.html
@@ -499,7 +499,7 @@ typedef struct { ZSTD_allocFunction customAlloc; ZSTD_freeFunction customFree; v
Note : this use case also happens when using a non-conformant dictionary.
- `srcSize` is too small, and as a result, the frame header could not be decoded (only possible if `srcSize < ZSTD_FRAMEHEADERSIZE_MAX`).
- This is not a Zstandard frame.
- When identifying the exact failure cause, it's possible to used ZSTD_getFrameParams(), which will provide a more precise error code.
+ When identifying the exact failure cause, it's possible to use ZSTD_getFrameParams(), which will provide a more precise error code.
Advanced streaming functions
diff --git a/lib/common/bitstream.h b/lib/common/bitstream.h
index 0e3d2fc55..c7a764465 100644
--- a/lib/common/bitstream.h
+++ b/lib/common/bitstream.h
@@ -2,7 +2,7 @@
bitstream
Part of FSE library
header file (to include)
- Copyright (C) 2013-2016, Yann Collet.
+ Copyright (C) 2013-2017, Yann Collet.
BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
@@ -53,6 +53,16 @@ extern "C" {
#include "error_private.h" /* error codes and messages */
+/*-*************************************
+* Debug
+***************************************/
+#if defined(BIT_DEBUG) && (BIT_DEBUG>=1)
+# include
+#else
+# define assert(condition) ((void)0)
+#endif
+
+
/*=========================================
* Target specific
=========================================*/
@@ -209,6 +219,7 @@ MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
* works only if `value` is _clean_, meaning all high bits above nbBits are 0 */
MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits)
{
+ assert((value>>nbBits) == 0);
bitC->bitContainer |= value << bitC->bitPos;
bitC->bitPos += nbBits;
}
@@ -336,10 +347,11 @@ MEM_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits)
}
/*! BIT_lookBitsFast() :
-* unsafe version; only works only if nbBits >= 1 */
+ * unsafe version; only works if nbBits >= 1 */
MEM_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t* bitD, U32 nbBits)
{
U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1;
+ assert(nbBits >= 1);
return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask);
}
@@ -365,6 +377,7 @@ MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits)
MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits)
{
size_t const value = BIT_lookBitsFast(bitD, nbBits);
+ assert(nbBits >= 1);
BIT_skipBits(bitD, nbBits);
return value;
}