| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 1 | /* ****************************************************************** |
| 2 | bitstream |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 3 | Part of FSE library |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 4 | header file (to include) |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 5 | Copyright (C) 2013-2016, Yann Collet. |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 6 | |
| 7 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without |
| 10 | modification, are permitted provided that the following conditions are |
| 11 | met: |
| 12 | |
| 13 | * Redistributions of source code must retain the above copyright |
| 14 | notice, this list of conditions and the following disclaimer. |
| 15 | * Redistributions in binary form must reproduce the above |
| 16 | copyright notice, this list of conditions and the following disclaimer |
| 17 | in the documentation and/or other materials provided with the |
| 18 | distribution. |
| 19 | |
| 20 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | |
| 32 | You can contact the author at : |
| 33 | - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 34 | ****************************************************************** */ |
| 35 | #ifndef BITSTREAM_H_MODULE |
| 36 | #define BITSTREAM_H_MODULE |
| 37 | |
| 38 | #if defined (__cplusplus) |
| 39 | extern "C" { |
| 40 | #endif |
| 41 | |
| 42 | |
| 43 | /* |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 44 | * This API consists of small unitary functions, which must be inlined for best performance. |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 45 | * Since link-time-optimization is not available for all compilers, |
| 46 | * these functions are defined into a .h to be included. |
| 47 | */ |
| 48 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 49 | /*-**************************************** |
| 50 | * Dependencies |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 51 | ******************************************/ |
| Yann Collet | 977f1f3 | 2016-01-21 15:38:47 +0100 | [diff] [blame] | 52 | #include "mem.h" /* unaligned access routines */ |
| 53 | #include "error_private.h" /* error codes and messages */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 54 | |
| 55 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 56 | /*-****************************************** |
| 57 | * bitStream encoding API (write forward) |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 58 | ********************************************/ |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 59 | /* bitStream can mix input from multiple sources. |
| 60 | * A critical property of these streams is that they encode and decode in **reverse** direction. |
| 61 | * So the first bit sequence you add will be the last to be read, like a LIFO stack. |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 62 | */ |
| 63 | typedef struct |
| 64 | { |
| 65 | size_t bitContainer; |
| 66 | int bitPos; |
| 67 | char* startPtr; |
| 68 | char* ptr; |
| 69 | char* endPtr; |
| 70 | } BIT_CStream_t; |
| 71 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 72 | MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* dstBuffer, size_t dstCapacity); |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 73 | MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits); |
| 74 | MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC); |
| 75 | MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC); |
| 76 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 77 | /* Start with initCStream, providing the size of buffer to write into. |
| 78 | * bitStream will never write outside of this buffer. |
| 79 | * `dstCapacity` must be >= sizeof(size_t), otherwise @return will be an error code. |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 80 | * |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 81 | * bits are first added to a local register. |
| 82 | * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems. |
| 83 | * Writing data into memory is an explicit operation, performed by the flushBits function. |
| 84 | * Hence keep track how many bits are potentially stored into local register to avoid register overflow. |
| 85 | * After a flushBits, a maximum of 7 bits might still be stored into local register. |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 86 | * |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 87 | * Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers. |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 88 | * |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 89 | * Last operation is to close the bitStream. |
| 90 | * The function returns the final size of CStream in bytes. |
| 91 | * If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable) |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 92 | */ |
| 93 | |
| 94 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 95 | /*-******************************************** |
| 96 | * bitStream decoding API (read backward) |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 97 | **********************************************/ |
| 98 | typedef struct |
| 99 | { |
| 100 | size_t bitContainer; |
| 101 | unsigned bitsConsumed; |
| 102 | const char* ptr; |
| 103 | const char* start; |
| 104 | } BIT_DStream_t; |
| 105 | |
| 106 | typedef enum { BIT_DStream_unfinished = 0, |
| 107 | BIT_DStream_endOfBuffer = 1, |
| 108 | BIT_DStream_completed = 2, |
| 109 | BIT_DStream_overflow = 3 } BIT_DStream_status; /* result of BIT_reloadDStream() */ |
| 110 | /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ |
| 111 | |
| 112 | MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize); |
| 113 | MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, unsigned nbBits); |
| 114 | MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD); |
| 115 | MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* bitD); |
| 116 | |
| 117 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 118 | /* Start by invoking BIT_initDStream(). |
| 119 | * A chunk of the bitStream is then stored into a local register. |
| 120 | * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). |
| 121 | * You can then retrieve bitFields stored into the local register, **in reverse order**. |
| 122 | * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. |
| 123 | * A reload guarantee a minimum of ((8*sizeof(size_t))-7) bits when its result is BIT_DStream_unfinished. |
| 124 | * Otherwise, it can be less than that, so proceed accordingly. |
| 125 | * Checking if DStream has reached its end can be performed with BIT_endOfDStream() |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 126 | */ |
| 127 | |
| 128 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 129 | /*-**************************************** |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 130 | * unsafe API |
| 131 | ******************************************/ |
| 132 | MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits); |
| 133 | /* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */ |
| 134 | |
| 135 | MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC); |
| 136 | /* unsafe version; does not check buffer overflow */ |
| 137 | |
| 138 | MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, unsigned nbBits); |
| 139 | /* faster, but works only if nbBits >= 1 */ |
| 140 | |
| 141 | |
| 142 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 143 | /*-************************************************************** |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 144 | * Helper functions |
| 145 | ****************************************************************/ |
| 146 | MEM_STATIC unsigned BIT_highbit32 (register U32 val) |
| 147 | { |
| 148 | # if defined(_MSC_VER) /* Visual */ |
| Yann Collet | 4114f95 | 2015-10-30 06:40:22 +0100 | [diff] [blame] | 149 | unsigned long r=0; |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 150 | _BitScanReverse ( &r, val ); |
| 151 | return (unsigned) r; |
| 152 | # elif defined(__GNUC__) && (__GNUC__ >= 3) /* Use GCC Intrinsic */ |
| 153 | return 31 - __builtin_clz (val); |
| 154 | # else /* Software version */ |
| 155 | static const unsigned DeBruijnClz[32] = { 0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30, 8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31 }; |
| 156 | U32 v = val; |
| 157 | unsigned r; |
| 158 | v |= v >> 1; |
| 159 | v |= v >> 2; |
| 160 | v |= v >> 4; |
| 161 | v |= v >> 8; |
| 162 | v |= v >> 16; |
| 163 | r = DeBruijnClz[ (U32) (v * 0x07C4ACDDU) >> 27]; |
| 164 | return r; |
| 165 | # endif |
| 166 | } |
| 167 | |
| 168 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 169 | /*-************************************************************** |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 170 | * bitStream encoding |
| 171 | ****************************************************************/ |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 172 | /*! BIT_initCStream() : |
| 173 | * `dstCapacity` must be > sizeof(void*) |
| 174 | * @return : 0 if success, |
| 175 | otherwise an error code (can be tested using ERR_isError() ) */ |
| 176 | MEM_STATIC size_t BIT_initCStream(BIT_CStream_t* bitC, void* startPtr, size_t dstCapacity) |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 177 | { |
| 178 | bitC->bitContainer = 0; |
| 179 | bitC->bitPos = 0; |
| 180 | bitC->startPtr = (char*)startPtr; |
| 181 | bitC->ptr = bitC->startPtr; |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 182 | bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr); |
| 183 | if (dstCapacity <= sizeof(bitC->ptr)) return ERROR(dstSize_tooSmall); |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 187 | /*! BIT_addBits() : |
| 188 | can add up to 26 bits into `bitC`. |
| 189 | Does not check for register overflow ! */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 190 | MEM_STATIC void BIT_addBits(BIT_CStream_t* bitC, size_t value, unsigned nbBits) |
| 191 | { |
| Yann Collet | 95cd0c2 | 2016-03-08 18:24:21 +0100 | [diff] [blame] | 192 | static const unsigned mask[] = { 0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF }; /* up to 26 bits */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 193 | bitC->bitContainer |= (value & mask[nbBits]) << bitC->bitPos; |
| 194 | bitC->bitPos += nbBits; |
| 195 | } |
| 196 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 197 | /*! BIT_addBitsFast() : |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 198 | * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */ |
| 199 | MEM_STATIC void BIT_addBitsFast(BIT_CStream_t* bitC, size_t value, unsigned nbBits) |
| 200 | { |
| 201 | bitC->bitContainer |= value << bitC->bitPos; |
| 202 | bitC->bitPos += nbBits; |
| 203 | } |
| 204 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 205 | /*! BIT_flushBitsFast() : |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 206 | * unsafe version; does not check buffer overflow */ |
| 207 | MEM_STATIC void BIT_flushBitsFast(BIT_CStream_t* bitC) |
| 208 | { |
| Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 209 | size_t const nbBytes = bitC->bitPos >> 3; |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 210 | MEM_writeLEST(bitC->ptr, bitC->bitContainer); |
| 211 | bitC->ptr += nbBytes; |
| 212 | bitC->bitPos &= 7; |
| Yann Collet | 00fd7a2 | 2015-11-28 16:03:22 +0100 | [diff] [blame] | 213 | bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 216 | /*! BIT_flushBits() : |
| 217 | * safe version; check for buffer overflow, and prevents it. |
| 218 | * note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 219 | MEM_STATIC void BIT_flushBits(BIT_CStream_t* bitC) |
| 220 | { |
| Yann Collet | d64f435 | 2016-03-21 00:07:42 +0100 | [diff] [blame] | 221 | size_t const nbBytes = bitC->bitPos >> 3; |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 222 | MEM_writeLEST(bitC->ptr, bitC->bitContainer); |
| 223 | bitC->ptr += nbBytes; |
| 224 | if (bitC->ptr > bitC->endPtr) bitC->ptr = bitC->endPtr; |
| 225 | bitC->bitPos &= 7; |
| Yann Collet | 00fd7a2 | 2015-11-28 16:03:22 +0100 | [diff] [blame] | 226 | bitC->bitContainer >>= nbBytes*8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 227 | } |
| 228 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 229 | /*! BIT_closeCStream() : |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 230 | * @return : size of CStream, in bytes, |
| 231 | or 0 if it could not fit into dstBuffer */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 232 | MEM_STATIC size_t BIT_closeCStream(BIT_CStream_t* bitC) |
| 233 | { |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 234 | BIT_addBitsFast(bitC, 1, 1); /* endMark */ |
| 235 | BIT_flushBits(bitC); |
| 236 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 237 | if (bitC->ptr >= bitC->endPtr) return 0; /* doesn't fit within authorized budget : cancel */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 238 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 239 | return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0); |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 243 | /*-******************************************************** |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 244 | * bitStream decoding |
| 245 | **********************************************************/ |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 246 | /*! BIT_initDStream() : |
| 247 | * Initialize a BIT_DStream_t. |
| 248 | * `bitD` : a pointer to an already allocated BIT_DStream_t structure. |
| 249 | * `srcBuffer` must point at the beginning of a bitStream. |
| 250 | * `srcSize` must be the exact size of the bitStream, in bytes. |
| 251 | * @return : size of stream (== srcSize) or an errorCode if a problem is detected |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 252 | */ |
| 253 | MEM_STATIC size_t BIT_initDStream(BIT_DStream_t* bitD, const void* srcBuffer, size_t srcSize) |
| 254 | { |
| 255 | if (srcSize < 1) { memset(bitD, 0, sizeof(*bitD)); return ERROR(srcSize_wrong); } |
| 256 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 257 | if (srcSize >= sizeof(size_t)) { /* normal case */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 258 | U32 contain32; |
| 259 | bitD->start = (const char*)srcBuffer; |
| 260 | bitD->ptr = (const char*)srcBuffer + srcSize - sizeof(size_t); |
| 261 | bitD->bitContainer = MEM_readLEST(bitD->ptr); |
| 262 | contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; |
| 263 | if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ |
| 264 | bitD->bitsConsumed = 8 - BIT_highbit32(contain32); |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 265 | } else { |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 266 | U32 contain32; |
| 267 | bitD->start = (const char*)srcBuffer; |
| 268 | bitD->ptr = bitD->start; |
| 269 | bitD->bitContainer = *(const BYTE*)(bitD->start); |
| 270 | switch(srcSize) |
| 271 | { |
| 272 | case 7: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[6]) << (sizeof(size_t)*8 - 16); |
| 273 | case 6: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[5]) << (sizeof(size_t)*8 - 24); |
| 274 | case 5: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[4]) << (sizeof(size_t)*8 - 32); |
| 275 | case 4: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[3]) << 24; |
| 276 | case 3: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[2]) << 16; |
| 277 | case 2: bitD->bitContainer += (size_t)(((const BYTE*)(bitD->start))[1]) << 8; |
| 278 | default:; |
| 279 | } |
| 280 | contain32 = ((const BYTE*)srcBuffer)[srcSize-1]; |
| 281 | if (contain32 == 0) return ERROR(GENERIC); /* endMark not present */ |
| 282 | bitD->bitsConsumed = 8 - BIT_highbit32(contain32); |
| 283 | bitD->bitsConsumed += (U32)(sizeof(size_t) - srcSize)*8; |
| 284 | } |
| 285 | |
| 286 | return srcSize; |
| 287 | } |
| 288 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 289 | /*! BIT_lookBits() : |
| 290 | * Provides next n bits from local register. |
| 291 | * local register is not modified (bits are still present for next read/look). |
| 292 | * On 32-bits, maxNbBits==24. |
| 293 | * On 64-bits, maxNbBits==56. |
| 294 | * @return : value extracted |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 295 | */ |
| 296 | MEM_STATIC size_t BIT_lookBits(BIT_DStream_t* bitD, U32 nbBits) |
| 297 | { |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 298 | U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1; |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 299 | return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask-nbBits) & bitMask); |
| 300 | } |
| 301 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 302 | /*! BIT_lookBitsFast() : |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 303 | * unsafe version; only works only if nbBits >= 1 */ |
| 304 | MEM_STATIC size_t BIT_lookBitsFast(BIT_DStream_t* bitD, U32 nbBits) |
| 305 | { |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 306 | U32 const bitMask = sizeof(bitD->bitContainer)*8 - 1; |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 307 | return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask+1)-nbBits) & bitMask); |
| 308 | } |
| 309 | |
| 310 | MEM_STATIC void BIT_skipBits(BIT_DStream_t* bitD, U32 nbBits) |
| 311 | { |
| 312 | bitD->bitsConsumed += nbBits; |
| 313 | } |
| 314 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 315 | /*! BIT_readBits() : |
| 316 | * Read next n bits from local register. |
| 317 | * pay attention to not read more than nbBits contained into local register. |
| 318 | * @return : extracted value. |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 319 | */ |
| 320 | MEM_STATIC size_t BIT_readBits(BIT_DStream_t* bitD, U32 nbBits) |
| 321 | { |
| 322 | size_t value = BIT_lookBits(bitD, nbBits); |
| 323 | BIT_skipBits(bitD, nbBits); |
| 324 | return value; |
| 325 | } |
| 326 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 327 | /*! BIT_readBitsFast() : |
| 328 | * unsafe version; only works only if nbBits >= 1 */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 329 | MEM_STATIC size_t BIT_readBitsFast(BIT_DStream_t* bitD, U32 nbBits) |
| 330 | { |
| 331 | size_t value = BIT_lookBitsFast(bitD, nbBits); |
| 332 | BIT_skipBits(bitD, nbBits); |
| 333 | return value; |
| 334 | } |
| 335 | |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 336 | /*! BIT_reloadDStream() : |
| 337 | * Refill `BIT_DStream_t` from src buffer previously defined (see BIT_initDStream() ). |
| 338 | * This function is safe, it guarantees it will not read beyond src buffer. |
| 339 | * @return : status of `BIT_DStream_t` internal register. |
| 340 | if status == unfinished, internal register is filled with >= (sizeof(size_t)*8 - 7) bits */ |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 341 | MEM_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t* bitD) |
| 342 | { |
| 343 | if (bitD->bitsConsumed > (sizeof(bitD->bitContainer)*8)) /* should never happen */ |
| 344 | return BIT_DStream_overflow; |
| 345 | |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 346 | if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) { |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 347 | bitD->ptr -= bitD->bitsConsumed >> 3; |
| 348 | bitD->bitsConsumed &= 7; |
| 349 | bitD->bitContainer = MEM_readLEST(bitD->ptr); |
| 350 | return BIT_DStream_unfinished; |
| 351 | } |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 352 | if (bitD->ptr == bitD->start) { |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 353 | if (bitD->bitsConsumed < sizeof(bitD->bitContainer)*8) return BIT_DStream_endOfBuffer; |
| 354 | return BIT_DStream_completed; |
| 355 | } |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 356 | { U32 nbBytes = bitD->bitsConsumed >> 3; |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 357 | BIT_DStream_status result = BIT_DStream_unfinished; |
| Yann Collet | ae7aa06 | 2016-02-03 02:46:46 +0100 | [diff] [blame] | 358 | if (bitD->ptr - nbBytes < bitD->start) { |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 359 | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ |
| 360 | result = BIT_DStream_endOfBuffer; |
| 361 | } |
| 362 | bitD->ptr -= nbBytes; |
| 363 | bitD->bitsConsumed -= nbBytes*8; |
| 364 | bitD->bitContainer = MEM_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ |
| 365 | return result; |
| 366 | } |
| 367 | } |
| 368 | |
| Yann Collet | d1d210f | 2016-03-19 12:12:07 +0100 | [diff] [blame] | 369 | /*! BIT_endOfDStream() : |
| Yann Collet | 01e5b95 | 2016-03-19 14:14:31 +0100 | [diff] [blame] | 370 | * @return Tells if DStream has exactly reached its end (all bits consumed). |
| Yann Collet | b1f3f4b | 2015-10-18 22:18:32 +0100 | [diff] [blame] | 371 | */ |
| 372 | MEM_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t* DStream) |
| 373 | { |
| 374 | return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer)*8)); |
| 375 | } |
| 376 | |
| 377 | #if defined (__cplusplus) |
| 378 | } |
| 379 | #endif |
| 380 | |
| 381 | #endif /* BITSTREAM_H_MODULE */ |