/src/barebox/lib/zstd/bitstream.h
Line | Count | Source |
1 | | /* |
2 | | * bitstream |
3 | | * Part of FSE library |
4 | | * header file (to include) |
5 | | * Copyright (C) 2013-2016, Yann Collet. |
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 | | * This program is free software; you can redistribute it and/or modify it under |
33 | | * the terms of the GNU General Public License version 2 as published by the |
34 | | * Free Software Foundation. This program is dual-licensed; you may select |
35 | | * either version 2 of the GNU General Public License ("GPL") or BSD license |
36 | | * ("BSD"). |
37 | | * |
38 | | * You can contact the author at : |
39 | | * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
40 | | */ |
41 | | #ifndef BITSTREAM_H_MODULE |
42 | | #define BITSTREAM_H_MODULE |
43 | | |
44 | | /* |
45 | | * This API consists of small unitary functions, which must be inlined for best performance. |
46 | | * Since link-time-optimization is not available for all compilers, |
47 | | * these functions are defined into a .h to be included. |
48 | | */ |
49 | | |
50 | | /*-**************************************** |
51 | | * Dependencies |
52 | | ******************************************/ |
53 | | #include "error_private.h" /* error codes and messages */ |
54 | | #include "mem.h" /* unaligned access routines */ |
55 | | |
56 | | /*========================================= |
57 | | * Target specific |
58 | | =========================================*/ |
59 | 0 | #define STREAM_ACCUMULATOR_MIN_32 25 |
60 | 6.32M | #define STREAM_ACCUMULATOR_MIN_64 57 |
61 | 6.32M | #define STREAM_ACCUMULATOR_MIN ((U32)(ZSTD_32bits() ? STREAM_ACCUMULATOR_MIN_32 : STREAM_ACCUMULATOR_MIN_64)) |
62 | | |
63 | | /*-****************************************** |
64 | | * bitStream encoding API (write forward) |
65 | | ********************************************/ |
66 | | /* bitStream can mix input from multiple sources. |
67 | | * A critical property of these streams is that they encode and decode in **reverse** direction. |
68 | | * So the first bit sequence you add will be the last to be read, like a LIFO stack. |
69 | | */ |
70 | | typedef struct { |
71 | | size_t bitContainer; |
72 | | int bitPos; |
73 | | char *startPtr; |
74 | | char *ptr; |
75 | | char *endPtr; |
76 | | } BIT_CStream_t; |
77 | | |
78 | | ZSTD_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *dstBuffer, size_t dstCapacity); |
79 | | ZSTD_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits); |
80 | | ZSTD_STATIC void BIT_flushBits(BIT_CStream_t *bitC); |
81 | | ZSTD_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC); |
82 | | |
83 | | /* Start with initCStream, providing the size of buffer to write into. |
84 | | * bitStream will never write outside of this buffer. |
85 | | * `dstCapacity` must be >= sizeof(bitD->bitContainer), otherwise @return will be an error code. |
86 | | * |
87 | | * bits are first added to a local register. |
88 | | * Local register is size_t, hence 64-bits on 64-bits systems, or 32-bits on 32-bits systems. |
89 | | * Writing data into memory is an explicit operation, performed by the flushBits function. |
90 | | * Hence keep track how many bits are potentially stored into local register to avoid register overflow. |
91 | | * After a flushBits, a maximum of 7 bits might still be stored into local register. |
92 | | * |
93 | | * Avoid storing elements of more than 24 bits if you want compatibility with 32-bits bitstream readers. |
94 | | * |
95 | | * Last operation is to close the bitStream. |
96 | | * The function returns the final size of CStream in bytes. |
97 | | * If data couldn't fit into `dstBuffer`, it will return a 0 ( == not storable) |
98 | | */ |
99 | | |
100 | | /*-******************************************** |
101 | | * bitStream decoding API (read backward) |
102 | | **********************************************/ |
103 | | typedef struct { |
104 | | size_t bitContainer; |
105 | | unsigned bitsConsumed; |
106 | | const char *ptr; |
107 | | const char *start; |
108 | | } BIT_DStream_t; |
109 | | |
110 | | typedef enum { |
111 | | BIT_DStream_unfinished = 0, |
112 | | BIT_DStream_endOfBuffer = 1, |
113 | | BIT_DStream_completed = 2, |
114 | | BIT_DStream_overflow = 3 |
115 | | } BIT_DStream_status; /* result of BIT_reloadDStream() */ |
116 | | /* 1,2,4,8 would be better for bitmap combinations, but slows down performance a bit ... :( */ |
117 | | |
118 | | ZSTD_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize); |
119 | | ZSTD_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, unsigned nbBits); |
120 | | ZSTD_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD); |
121 | | ZSTD_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t *bitD); |
122 | | |
123 | | /* Start by invoking BIT_initDStream(). |
124 | | * A chunk of the bitStream is then stored into a local register. |
125 | | * Local register size is 64-bits on 64-bits systems, 32-bits on 32-bits systems (size_t). |
126 | | * You can then retrieve bitFields stored into the local register, **in reverse order**. |
127 | | * Local register is explicitly reloaded from memory by the BIT_reloadDStream() method. |
128 | | * A reload guarantee a minimum of ((8*sizeof(bitD->bitContainer))-7) bits when its result is BIT_DStream_unfinished. |
129 | | * Otherwise, it can be less than that, so proceed accordingly. |
130 | | * Checking if DStream has reached its end can be performed with BIT_endOfDStream(). |
131 | | */ |
132 | | |
133 | | /*-**************************************** |
134 | | * unsafe API |
135 | | ******************************************/ |
136 | | ZSTD_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits); |
137 | | /* faster, but works only if value is "clean", meaning all high bits above nbBits are 0 */ |
138 | | |
139 | | ZSTD_STATIC void BIT_flushBitsFast(BIT_CStream_t *bitC); |
140 | | /* unsafe version; does not check buffer overflow */ |
141 | | |
142 | | ZSTD_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, unsigned nbBits); |
143 | | /* faster, but works only if nbBits >= 1 */ |
144 | | |
145 | | /*-************************************************************** |
146 | | * Internal functions |
147 | | ****************************************************************/ |
148 | 4.66M | ZSTD_STATIC unsigned BIT_highbit32(register U32 val) { return 31 - __builtin_clz(val); }huf_decompress.c:BIT_highbit32 Line | Count | Source | 148 | 20.9k | ZSTD_STATIC unsigned BIT_highbit32(register U32 val) { return 31 - __builtin_clz(val); } |
decompress.c:BIT_highbit32 Line | Count | Source | 148 | 31.8k | ZSTD_STATIC unsigned BIT_highbit32(register U32 val) { return 31 - __builtin_clz(val); } |
entropy_common.c:BIT_highbit32 Line | Count | Source | 148 | 37.6k | ZSTD_STATIC unsigned BIT_highbit32(register U32 val) { return 31 - __builtin_clz(val); } |
fse_decompress.c:BIT_highbit32 Line | Count | Source | 148 | 4.57M | ZSTD_STATIC unsigned BIT_highbit32(register U32 val) { return 31 - __builtin_clz(val); } |
|
149 | | |
150 | | /*===== Local Constants =====*/ |
151 | | static const unsigned BIT_mask[] = {0, 1, 3, 7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, |
152 | | 0x1FF, 0x3FF, 0x7FF, 0xFFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF, 0x1FFFF, |
153 | | 0x3FFFF, 0x7FFFF, 0xFFFFF, 0x1FFFFF, 0x3FFFFF, 0x7FFFFF, 0xFFFFFF, 0x1FFFFFF, 0x3FFFFFF}; /* up to 26 bits */ |
154 | | |
155 | | /*-************************************************************** |
156 | | * bitStream encoding |
157 | | ****************************************************************/ |
158 | | /*! BIT_initCStream() : |
159 | | * `dstCapacity` must be > sizeof(void*) |
160 | | * @return : 0 if success, |
161 | | otherwise an error code (can be tested using ERR_isError() ) */ |
162 | | ZSTD_STATIC size_t BIT_initCStream(BIT_CStream_t *bitC, void *startPtr, size_t dstCapacity) |
163 | 0 | { |
164 | 0 | bitC->bitContainer = 0; |
165 | 0 | bitC->bitPos = 0; |
166 | 0 | bitC->startPtr = (char *)startPtr; |
167 | 0 | bitC->ptr = bitC->startPtr; |
168 | 0 | bitC->endPtr = bitC->startPtr + dstCapacity - sizeof(bitC->ptr); |
169 | 0 | if (dstCapacity <= sizeof(bitC->ptr)) |
170 | 0 | return ERROR(dstSize_tooSmall); |
171 | 0 | return 0; |
172 | 0 | } Unexecuted instantiation: huf_decompress.c:BIT_initCStream Unexecuted instantiation: decompress.c:BIT_initCStream Unexecuted instantiation: entropy_common.c:BIT_initCStream Unexecuted instantiation: fse_decompress.c:BIT_initCStream |
173 | | |
174 | | /*! BIT_addBits() : |
175 | | can add up to 26 bits into `bitC`. |
176 | | Does not check for register overflow ! */ |
177 | | ZSTD_STATIC void BIT_addBits(BIT_CStream_t *bitC, size_t value, unsigned nbBits) |
178 | 0 | { |
179 | 0 | bitC->bitContainer |= (value & BIT_mask[nbBits]) << bitC->bitPos; |
180 | 0 | bitC->bitPos += nbBits; |
181 | 0 | } Unexecuted instantiation: huf_decompress.c:BIT_addBits Unexecuted instantiation: decompress.c:BIT_addBits Unexecuted instantiation: entropy_common.c:BIT_addBits Unexecuted instantiation: fse_decompress.c:BIT_addBits |
182 | | |
183 | | /*! BIT_addBitsFast() : |
184 | | * works only if `value` is _clean_, meaning all high bits above nbBits are 0 */ |
185 | | ZSTD_STATIC void BIT_addBitsFast(BIT_CStream_t *bitC, size_t value, unsigned nbBits) |
186 | 0 | { |
187 | 0 | bitC->bitContainer |= value << bitC->bitPos; |
188 | 0 | bitC->bitPos += nbBits; |
189 | 0 | } Unexecuted instantiation: huf_decompress.c:BIT_addBitsFast Unexecuted instantiation: decompress.c:BIT_addBitsFast Unexecuted instantiation: entropy_common.c:BIT_addBitsFast Unexecuted instantiation: fse_decompress.c:BIT_addBitsFast |
190 | | |
191 | | /*! BIT_flushBitsFast() : |
192 | | * unsafe version; does not check buffer overflow */ |
193 | | ZSTD_STATIC void BIT_flushBitsFast(BIT_CStream_t *bitC) |
194 | 0 | { |
195 | 0 | size_t const nbBytes = bitC->bitPos >> 3; |
196 | 0 | ZSTD_writeLEST(bitC->ptr, bitC->bitContainer); |
197 | 0 | bitC->ptr += nbBytes; |
198 | 0 | bitC->bitPos &= 7; |
199 | 0 | bitC->bitContainer >>= nbBytes * 8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ |
200 | 0 | } Unexecuted instantiation: huf_decompress.c:BIT_flushBitsFast Unexecuted instantiation: decompress.c:BIT_flushBitsFast Unexecuted instantiation: entropy_common.c:BIT_flushBitsFast Unexecuted instantiation: fse_decompress.c:BIT_flushBitsFast |
201 | | |
202 | | /*! BIT_flushBits() : |
203 | | * safe version; check for buffer overflow, and prevents it. |
204 | | * note : does not signal buffer overflow. This will be revealed later on using BIT_closeCStream() */ |
205 | | ZSTD_STATIC void BIT_flushBits(BIT_CStream_t *bitC) |
206 | 0 | { |
207 | 0 | size_t const nbBytes = bitC->bitPos >> 3; |
208 | 0 | ZSTD_writeLEST(bitC->ptr, bitC->bitContainer); |
209 | 0 | bitC->ptr += nbBytes; |
210 | 0 | if (bitC->ptr > bitC->endPtr) |
211 | 0 | bitC->ptr = bitC->endPtr; |
212 | 0 | bitC->bitPos &= 7; |
213 | 0 | bitC->bitContainer >>= nbBytes * 8; /* if bitPos >= sizeof(bitContainer)*8 --> undefined behavior */ |
214 | 0 | } Unexecuted instantiation: huf_decompress.c:BIT_flushBits Unexecuted instantiation: decompress.c:BIT_flushBits Unexecuted instantiation: entropy_common.c:BIT_flushBits Unexecuted instantiation: fse_decompress.c:BIT_flushBits |
215 | | |
216 | | /*! BIT_closeCStream() : |
217 | | * @return : size of CStream, in bytes, |
218 | | or 0 if it could not fit into dstBuffer */ |
219 | | ZSTD_STATIC size_t BIT_closeCStream(BIT_CStream_t *bitC) |
220 | 0 | { |
221 | 0 | BIT_addBitsFast(bitC, 1, 1); /* endMark */ |
222 | 0 | BIT_flushBits(bitC); |
223 | 0 |
|
224 | 0 | if (bitC->ptr >= bitC->endPtr) |
225 | 0 | return 0; /* doesn't fit within authorized budget : cancel */ |
226 | 0 |
|
227 | 0 | return (bitC->ptr - bitC->startPtr) + (bitC->bitPos > 0); |
228 | 0 | } Unexecuted instantiation: huf_decompress.c:BIT_closeCStream Unexecuted instantiation: decompress.c:BIT_closeCStream Unexecuted instantiation: entropy_common.c:BIT_closeCStream Unexecuted instantiation: fse_decompress.c:BIT_closeCStream |
229 | | |
230 | | /*-******************************************************** |
231 | | * bitStream decoding |
232 | | **********************************************************/ |
233 | | /*! BIT_initDStream() : |
234 | | * Initialize a BIT_DStream_t. |
235 | | * `bitD` : a pointer to an already allocated BIT_DStream_t structure. |
236 | | * `srcSize` must be the *exact* size of the bitStream, in bytes. |
237 | | * @return : size of stream (== srcSize) or an errorCode if a problem is detected |
238 | | */ |
239 | | ZSTD_STATIC size_t BIT_initDStream(BIT_DStream_t *bitD, const void *srcBuffer, size_t srcSize) |
240 | 71.3k | { |
241 | 71.3k | if (srcSize < 1) { |
242 | 1.22k | memset(bitD, 0, sizeof(*bitD)); |
243 | 1.22k | return ERROR(srcSize_wrong); |
244 | 1.22k | } |
245 | | |
246 | 70.1k | if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ |
247 | 41.7k | bitD->start = (const char *)srcBuffer; |
248 | 41.7k | bitD->ptr = (const char *)srcBuffer + srcSize - sizeof(bitD->bitContainer); |
249 | 41.7k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); |
250 | 41.7k | { |
251 | 41.7k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; |
252 | 41.7k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ |
253 | 41.7k | if (lastByte == 0) |
254 | 3.11k | return ERROR(GENERIC); /* endMark not present */ |
255 | 41.7k | } |
256 | 41.7k | } else { |
257 | 28.4k | bitD->start = (const char *)srcBuffer; |
258 | 28.4k | bitD->ptr = bitD->start; |
259 | 28.4k | bitD->bitContainer = *(const BYTE *)(bitD->start); |
260 | 28.4k | switch (srcSize) { |
261 | 1.89k | case 7: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[6]) << (sizeof(bitD->bitContainer) * 8 - 16); |
262 | 4.51k | case 6: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[5]) << (sizeof(bitD->bitContainer) * 8 - 24); |
263 | 8.34k | case 5: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[4]) << (sizeof(bitD->bitContainer) * 8 - 32); |
264 | 14.0k | case 4: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[3]) << 24; |
265 | 19.7k | case 3: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[2]) << 16; |
266 | 22.9k | case 2: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[1]) << 8; |
267 | 28.4k | default:; |
268 | 28.4k | } |
269 | 28.4k | { |
270 | 28.4k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; |
271 | 28.4k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; |
272 | 28.4k | if (lastByte == 0) |
273 | 1.80k | return ERROR(GENERIC); /* endMark not present */ |
274 | 28.4k | } |
275 | 26.6k | bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize) * 8; |
276 | 26.6k | } |
277 | | |
278 | 65.2k | return srcSize; |
279 | 70.1k | } huf_decompress.c:BIT_initDStream Line | Count | Source | 240 | 24.1k | { | 241 | 24.1k | if (srcSize < 1) { | 242 | 614 | memset(bitD, 0, sizeof(*bitD)); | 243 | 614 | return ERROR(srcSize_wrong); | 244 | 614 | } | 245 | | | 246 | 23.5k | if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ | 247 | 13.6k | bitD->start = (const char *)srcBuffer; | 248 | 13.6k | bitD->ptr = (const char *)srcBuffer + srcSize - sizeof(bitD->bitContainer); | 249 | 13.6k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); | 250 | 13.6k | { | 251 | 13.6k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; | 252 | 13.6k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ | 253 | 13.6k | if (lastByte == 0) | 254 | 1.89k | return ERROR(GENERIC); /* endMark not present */ | 255 | 13.6k | } | 256 | 13.6k | } else { | 257 | 9.84k | bitD->start = (const char *)srcBuffer; | 258 | 9.84k | bitD->ptr = bitD->start; | 259 | 9.84k | bitD->bitContainer = *(const BYTE *)(bitD->start); | 260 | 9.84k | switch (srcSize) { | 261 | 485 | case 7: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[6]) << (sizeof(bitD->bitContainer) * 8 - 16); | 262 | 1.29k | case 6: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[5]) << (sizeof(bitD->bitContainer) * 8 - 24); | 263 | 2.28k | case 5: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[4]) << (sizeof(bitD->bitContainer) * 8 - 32); | 264 | 3.42k | case 4: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[3]) << 24; | 265 | 5.51k | case 3: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[2]) << 16; | 266 | 6.96k | case 2: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[1]) << 8; | 267 | 9.84k | default:; | 268 | 9.84k | } | 269 | 9.84k | { | 270 | 9.84k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; | 271 | 9.84k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; | 272 | 9.84k | if (lastByte == 0) | 273 | 745 | return ERROR(GENERIC); /* endMark not present */ | 274 | 9.84k | } | 275 | 9.10k | bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize) * 8; | 276 | 9.10k | } | 277 | | | 278 | 20.9k | return srcSize; | 279 | 23.5k | } |
decompress.c:BIT_initDStream Line | Count | Source | 240 | 33.7k | { | 241 | 33.7k | if (srcSize < 1) { | 242 | 394 | memset(bitD, 0, sizeof(*bitD)); | 243 | 394 | return ERROR(srcSize_wrong); | 244 | 394 | } | 245 | | | 246 | 33.3k | if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ | 247 | 21.0k | bitD->start = (const char *)srcBuffer; | 248 | 21.0k | bitD->ptr = (const char *)srcBuffer + srcSize - sizeof(bitD->bitContainer); | 249 | 21.0k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); | 250 | 21.0k | { | 251 | 21.0k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; | 252 | 21.0k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ | 253 | 21.0k | if (lastByte == 0) | 254 | 690 | return ERROR(GENERIC); /* endMark not present */ | 255 | 21.0k | } | 256 | 21.0k | } else { | 257 | 12.2k | bitD->start = (const char *)srcBuffer; | 258 | 12.2k | bitD->ptr = bitD->start; | 259 | 12.2k | bitD->bitContainer = *(const BYTE *)(bitD->start); | 260 | 12.2k | switch (srcSize) { | 261 | 1.00k | case 7: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[6]) << (sizeof(bitD->bitContainer) * 8 - 16); | 262 | 2.53k | case 6: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[5]) << (sizeof(bitD->bitContainer) * 8 - 24); | 263 | 4.88k | case 5: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[4]) << (sizeof(bitD->bitContainer) * 8 - 32); | 264 | 7.10k | case 4: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[3]) << 24; | 265 | 10.0k | case 3: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[2]) << 16; | 266 | 11.3k | case 2: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[1]) << 8; | 267 | 12.2k | default:; | 268 | 12.2k | } | 269 | 12.2k | { | 270 | 12.2k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; | 271 | 12.2k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; | 272 | 12.2k | if (lastByte == 0) | 273 | 800 | return ERROR(GENERIC); /* endMark not present */ | 274 | 12.2k | } | 275 | 11.4k | bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize) * 8; | 276 | 11.4k | } | 277 | | | 278 | 31.8k | return srcSize; | 279 | 33.3k | } |
Unexecuted instantiation: entropy_common.c:BIT_initDStream fse_decompress.c:BIT_initDStream Line | Count | Source | 240 | 13.4k | { | 241 | 13.4k | if (srcSize < 1) { | 242 | 221 | memset(bitD, 0, sizeof(*bitD)); | 243 | 221 | return ERROR(srcSize_wrong); | 244 | 221 | } | 245 | | | 246 | 13.2k | if (srcSize >= sizeof(bitD->bitContainer)) { /* normal case */ | 247 | 6.92k | bitD->start = (const char *)srcBuffer; | 248 | 6.92k | bitD->ptr = (const char *)srcBuffer + srcSize - sizeof(bitD->bitContainer); | 249 | 6.92k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); | 250 | 6.92k | { | 251 | 6.92k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; | 252 | 6.92k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; /* ensures bitsConsumed is always set */ | 253 | 6.92k | if (lastByte == 0) | 254 | 534 | return ERROR(GENERIC); /* endMark not present */ | 255 | 6.92k | } | 256 | 6.92k | } else { | 257 | 6.32k | bitD->start = (const char *)srcBuffer; | 258 | 6.32k | bitD->ptr = bitD->start; | 259 | 6.32k | bitD->bitContainer = *(const BYTE *)(bitD->start); | 260 | 6.32k | switch (srcSize) { | 261 | 405 | case 7: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[6]) << (sizeof(bitD->bitContainer) * 8 - 16); | 262 | 682 | case 6: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[5]) << (sizeof(bitD->bitContainer) * 8 - 24); | 263 | 1.17k | case 5: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[4]) << (sizeof(bitD->bitContainer) * 8 - 32); | 264 | 3.54k | case 4: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[3]) << 24; | 265 | 4.10k | case 3: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[2]) << 16; | 266 | 4.68k | case 2: bitD->bitContainer += (size_t)(((const BYTE *)(srcBuffer))[1]) << 8; | 267 | 6.32k | default:; | 268 | 6.32k | } | 269 | 6.32k | { | 270 | 6.32k | BYTE const lastByte = ((const BYTE *)srcBuffer)[srcSize - 1]; | 271 | 6.32k | bitD->bitsConsumed = lastByte ? 8 - BIT_highbit32(lastByte) : 0; | 272 | 6.32k | if (lastByte == 0) | 273 | 258 | return ERROR(GENERIC); /* endMark not present */ | 274 | 6.32k | } | 275 | 6.07k | bitD->bitsConsumed += (U32)(sizeof(bitD->bitContainer) - srcSize) * 8; | 276 | 6.07k | } | 277 | | | 278 | 12.4k | return srcSize; | 279 | 13.2k | } |
|
280 | | |
281 | 0 | ZSTD_STATIC size_t BIT_getUpperBits(size_t bitContainer, U32 const start) { return bitContainer >> start; }Unexecuted instantiation: huf_decompress.c:BIT_getUpperBits Unexecuted instantiation: decompress.c:BIT_getUpperBits Unexecuted instantiation: entropy_common.c:BIT_getUpperBits Unexecuted instantiation: fse_decompress.c:BIT_getUpperBits |
282 | | |
283 | 0 | ZSTD_STATIC size_t BIT_getMiddleBits(size_t bitContainer, U32 const start, U32 const nbBits) { return (bitContainer >> start) & BIT_mask[nbBits]; }Unexecuted instantiation: huf_decompress.c:BIT_getMiddleBits Unexecuted instantiation: decompress.c:BIT_getMiddleBits Unexecuted instantiation: entropy_common.c:BIT_getMiddleBits Unexecuted instantiation: fse_decompress.c:BIT_getMiddleBits |
284 | | |
285 | 0 | ZSTD_STATIC size_t BIT_getLowerBits(size_t bitContainer, U32 const nbBits) { return bitContainer & BIT_mask[nbBits]; }Unexecuted instantiation: huf_decompress.c:BIT_getLowerBits Unexecuted instantiation: decompress.c:BIT_getLowerBits Unexecuted instantiation: entropy_common.c:BIT_getLowerBits Unexecuted instantiation: fse_decompress.c:BIT_getLowerBits |
286 | | |
287 | | /*! BIT_lookBits() : |
288 | | * Provides next n bits from local register. |
289 | | * local register is not modified. |
290 | | * On 32-bits, maxNbBits==24. |
291 | | * On 64-bits, maxNbBits==56. |
292 | | * @return : value extracted |
293 | | */ |
294 | | ZSTD_STATIC size_t BIT_lookBits(const BIT_DStream_t *bitD, U32 nbBits) |
295 | 20.8M | { |
296 | 20.8M | U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; |
297 | 20.8M | return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask - nbBits) & bitMask); |
298 | 20.8M | } Unexecuted instantiation: huf_decompress.c:BIT_lookBits decompress.c:BIT_lookBits Line | Count | Source | 295 | 20.0M | { | 296 | 20.0M | U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; | 297 | 20.0M | return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask - nbBits) & bitMask); | 298 | 20.0M | } |
Unexecuted instantiation: entropy_common.c:BIT_lookBits fse_decompress.c:BIT_lookBits Line | Count | Source | 295 | 774k | { | 296 | 774k | U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; | 297 | 774k | return ((bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> 1) >> ((bitMask - nbBits) & bitMask); | 298 | 774k | } |
|
299 | | |
300 | | /*! BIT_lookBitsFast() : |
301 | | * unsafe version; only works only if nbBits >= 1 */ |
302 | | ZSTD_STATIC size_t BIT_lookBitsFast(const BIT_DStream_t *bitD, U32 nbBits) |
303 | 27.8M | { |
304 | 27.8M | U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; |
305 | 27.8M | return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask + 1) - nbBits) & bitMask); |
306 | 27.8M | } huf_decompress.c:BIT_lookBitsFast Line | Count | Source | 303 | 27.2M | { | 304 | 27.2M | U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; | 305 | 27.2M | return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask + 1) - nbBits) & bitMask); | 306 | 27.2M | } |
decompress.c:BIT_lookBitsFast Line | Count | Source | 303 | 279k | { | 304 | 279k | U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; | 305 | 279k | return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask + 1) - nbBits) & bitMask); | 306 | 279k | } |
Unexecuted instantiation: entropy_common.c:BIT_lookBitsFast fse_decompress.c:BIT_lookBitsFast Line | Count | Source | 303 | 324k | { | 304 | 324k | U32 const bitMask = sizeof(bitD->bitContainer) * 8 - 1; | 305 | 324k | return (bitD->bitContainer << (bitD->bitsConsumed & bitMask)) >> (((bitMask + 1) - nbBits) & bitMask); | 306 | 324k | } |
|
307 | | |
308 | 48.6M | ZSTD_STATIC void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits) { bitD->bitsConsumed += nbBits; }huf_decompress.c:BIT_skipBits Line | Count | Source | 308 | 27.2M | ZSTD_STATIC void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits) { bitD->bitsConsumed += nbBits; } |
decompress.c:BIT_skipBits Line | Count | Source | 308 | 20.3M | ZSTD_STATIC void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits) { bitD->bitsConsumed += nbBits; } |
Unexecuted instantiation: entropy_common.c:BIT_skipBits fse_decompress.c:BIT_skipBits Line | Count | Source | 308 | 1.09M | ZSTD_STATIC void BIT_skipBits(BIT_DStream_t *bitD, U32 nbBits) { bitD->bitsConsumed += nbBits; } |
|
309 | | |
310 | | /*! BIT_readBits() : |
311 | | * Read (consume) next n bits from local register and update. |
312 | | * Pay attention to not read more than nbBits contained into local register. |
313 | | * @return : extracted value. |
314 | | */ |
315 | | ZSTD_STATIC size_t BIT_readBits(BIT_DStream_t *bitD, U32 nbBits) |
316 | 20.8M | { |
317 | 20.8M | size_t const value = BIT_lookBits(bitD, nbBits); |
318 | 20.8M | BIT_skipBits(bitD, nbBits); |
319 | 20.8M | return value; |
320 | 20.8M | } Unexecuted instantiation: huf_decompress.c:BIT_readBits decompress.c:BIT_readBits Line | Count | Source | 316 | 20.0M | { | 317 | 20.0M | size_t const value = BIT_lookBits(bitD, nbBits); | 318 | 20.0M | BIT_skipBits(bitD, nbBits); | 319 | 20.0M | return value; | 320 | 20.0M | } |
Unexecuted instantiation: entropy_common.c:BIT_readBits fse_decompress.c:BIT_readBits Line | Count | Source | 316 | 774k | { | 317 | 774k | size_t const value = BIT_lookBits(bitD, nbBits); | 318 | 774k | BIT_skipBits(bitD, nbBits); | 319 | 774k | return value; | 320 | 774k | } |
|
321 | | |
322 | | /*! BIT_readBitsFast() : |
323 | | * unsafe version; only works only if nbBits >= 1 */ |
324 | | ZSTD_STATIC size_t BIT_readBitsFast(BIT_DStream_t *bitD, U32 nbBits) |
325 | 604k | { |
326 | 604k | size_t const value = BIT_lookBitsFast(bitD, nbBits); |
327 | 604k | BIT_skipBits(bitD, nbBits); |
328 | 604k | return value; |
329 | 604k | } Unexecuted instantiation: huf_decompress.c:BIT_readBitsFast decompress.c:BIT_readBitsFast Line | Count | Source | 325 | 279k | { | 326 | 279k | size_t const value = BIT_lookBitsFast(bitD, nbBits); | 327 | 279k | BIT_skipBits(bitD, nbBits); | 328 | 279k | return value; | 329 | 279k | } |
Unexecuted instantiation: entropy_common.c:BIT_readBitsFast fse_decompress.c:BIT_readBitsFast Line | Count | Source | 325 | 324k | { | 326 | 324k | size_t const value = BIT_lookBitsFast(bitD, nbBits); | 327 | 324k | BIT_skipBits(bitD, nbBits); | 328 | 324k | return value; | 329 | 324k | } |
|
330 | | |
331 | | /*! BIT_reloadDStream() : |
332 | | * Refill `bitD` from buffer previously set in BIT_initDStream() . |
333 | | * This function is safe, it guarantees it will not read beyond src buffer. |
334 | | * @return : status of `BIT_DStream_t` internal register. |
335 | | if status == BIT_DStream_unfinished, internal register is filled with >= (sizeof(bitD->bitContainer)*8 - 7) bits */ |
336 | | ZSTD_STATIC BIT_DStream_status BIT_reloadDStream(BIT_DStream_t *bitD) |
337 | 8.15M | { |
338 | 8.15M | if (bitD->bitsConsumed > (sizeof(bitD->bitContainer) * 8)) /* should not happen => corruption detected */ |
339 | 27.6k | return BIT_DStream_overflow; |
340 | | |
341 | 8.12M | if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) { |
342 | 1.65M | bitD->ptr -= bitD->bitsConsumed >> 3; |
343 | 1.65M | bitD->bitsConsumed &= 7; |
344 | 1.65M | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); |
345 | 1.65M | return BIT_DStream_unfinished; |
346 | 1.65M | } |
347 | 6.47M | if (bitD->ptr == bitD->start) { |
348 | 1.48M | if (bitD->bitsConsumed < sizeof(bitD->bitContainer) * 8) |
349 | 1.46M | return BIT_DStream_endOfBuffer; |
350 | 20.3k | return BIT_DStream_completed; |
351 | 1.48M | } |
352 | 4.98M | { |
353 | 4.98M | U32 nbBytes = bitD->bitsConsumed >> 3; |
354 | 4.98M | BIT_DStream_status result = BIT_DStream_unfinished; |
355 | 4.98M | if (bitD->ptr - nbBytes < bitD->start) { |
356 | 9.19k | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ |
357 | 9.19k | result = BIT_DStream_endOfBuffer; |
358 | 9.19k | } |
359 | 4.98M | bitD->ptr -= nbBytes; |
360 | 4.98M | bitD->bitsConsumed -= nbBytes * 8; |
361 | 4.98M | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ |
362 | 4.98M | return result; |
363 | 6.47M | } |
364 | 6.47M | } huf_decompress.c:BIT_reloadDStream Line | Count | Source | 337 | 649k | { | 338 | 649k | if (bitD->bitsConsumed > (sizeof(bitD->bitContainer) * 8)) /* should not happen => corruption detected */ | 339 | 0 | return BIT_DStream_overflow; | 340 | | | 341 | 649k | if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) { | 342 | 576k | bitD->ptr -= bitD->bitsConsumed >> 3; | 343 | 576k | bitD->bitsConsumed &= 7; | 344 | 576k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); | 345 | 576k | return BIT_DStream_unfinished; | 346 | 576k | } | 347 | 72.8k | if (bitD->ptr == bitD->start) { | 348 | 31.3k | if (bitD->bitsConsumed < sizeof(bitD->bitContainer) * 8) | 349 | 30.5k | return BIT_DStream_endOfBuffer; | 350 | 812 | return BIT_DStream_completed; | 351 | 31.3k | } | 352 | 41.4k | { | 353 | 41.4k | U32 nbBytes = bitD->bitsConsumed >> 3; | 354 | 41.4k | BIT_DStream_status result = BIT_DStream_unfinished; | 355 | 41.4k | if (bitD->ptr - nbBytes < bitD->start) { | 356 | 4.35k | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ | 357 | 4.35k | result = BIT_DStream_endOfBuffer; | 358 | 4.35k | } | 359 | 41.4k | bitD->ptr -= nbBytes; | 360 | 41.4k | bitD->bitsConsumed -= nbBytes * 8; | 361 | 41.4k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ | 362 | 41.4k | return result; | 363 | 72.8k | } | 364 | 72.8k | } |
decompress.c:BIT_reloadDStream Line | Count | Source | 337 | 6.79M | { | 338 | 6.79M | if (bitD->bitsConsumed > (sizeof(bitD->bitContainer) * 8)) /* should not happen => corruption detected */ | 339 | 11.6k | return BIT_DStream_overflow; | 340 | | | 341 | 6.78M | if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) { | 342 | 990k | bitD->ptr -= bitD->bitsConsumed >> 3; | 343 | 990k | bitD->bitsConsumed &= 7; | 344 | 990k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); | 345 | 990k | return BIT_DStream_unfinished; | 346 | 990k | } | 347 | 5.79M | if (bitD->ptr == bitD->start) { | 348 | 908k | if (bitD->bitsConsumed < sizeof(bitD->bitContainer) * 8) | 349 | 907k | return BIT_DStream_endOfBuffer; | 350 | 1.93k | return BIT_DStream_completed; | 351 | 908k | } | 352 | 4.88M | { | 353 | 4.88M | U32 nbBytes = bitD->bitsConsumed >> 3; | 354 | 4.88M | BIT_DStream_status result = BIT_DStream_unfinished; | 355 | 4.88M | if (bitD->ptr - nbBytes < bitD->start) { | 356 | 3.72k | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ | 357 | 3.72k | result = BIT_DStream_endOfBuffer; | 358 | 3.72k | } | 359 | 4.88M | bitD->ptr -= nbBytes; | 360 | 4.88M | bitD->bitsConsumed -= nbBytes * 8; | 361 | 4.88M | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ | 362 | 4.88M | return result; | 363 | 5.79M | } | 364 | 5.79M | } |
Unexecuted instantiation: entropy_common.c:BIT_reloadDStream fse_decompress.c:BIT_reloadDStream Line | Count | Source | 337 | 704k | { | 338 | 704k | if (bitD->bitsConsumed > (sizeof(bitD->bitContainer) * 8)) /* should not happen => corruption detected */ | 339 | 16.0k | return BIT_DStream_overflow; | 340 | | | 341 | 688k | if (bitD->ptr >= bitD->start + sizeof(bitD->bitContainer)) { | 342 | 83.2k | bitD->ptr -= bitD->bitsConsumed >> 3; | 343 | 83.2k | bitD->bitsConsumed &= 7; | 344 | 83.2k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); | 345 | 83.2k | return BIT_DStream_unfinished; | 346 | 83.2k | } | 347 | 604k | if (bitD->ptr == bitD->start) { | 348 | 541k | if (bitD->bitsConsumed < sizeof(bitD->bitContainer) * 8) | 349 | 524k | return BIT_DStream_endOfBuffer; | 350 | 17.6k | return BIT_DStream_completed; | 351 | 541k | } | 352 | 63.0k | { | 353 | 63.0k | U32 nbBytes = bitD->bitsConsumed >> 3; | 354 | 63.0k | BIT_DStream_status result = BIT_DStream_unfinished; | 355 | 63.0k | if (bitD->ptr - nbBytes < bitD->start) { | 356 | 1.11k | nbBytes = (U32)(bitD->ptr - bitD->start); /* ptr > start */ | 357 | 1.11k | result = BIT_DStream_endOfBuffer; | 358 | 1.11k | } | 359 | 63.0k | bitD->ptr -= nbBytes; | 360 | 63.0k | bitD->bitsConsumed -= nbBytes * 8; | 361 | 63.0k | bitD->bitContainer = ZSTD_readLEST(bitD->ptr); /* reminder : srcSize > sizeof(bitD) */ | 362 | 63.0k | return result; | 363 | 604k | } | 364 | 604k | } |
|
365 | | |
366 | | /*! BIT_endOfDStream() : |
367 | | * @return Tells if DStream has exactly reached its end (all bits consumed). |
368 | | */ |
369 | | ZSTD_STATIC unsigned BIT_endOfDStream(const BIT_DStream_t *DStream) |
370 | 16.3k | { |
371 | 16.3k | return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer) * 8)); |
372 | 16.3k | } huf_decompress.c:BIT_endOfDStream Line | Count | Source | 370 | 16.3k | { | 371 | 16.3k | return ((DStream->ptr == DStream->start) && (DStream->bitsConsumed == sizeof(DStream->bitContainer) * 8)); | 372 | 16.3k | } |
Unexecuted instantiation: decompress.c:BIT_endOfDStream Unexecuted instantiation: entropy_common.c:BIT_endOfDStream Unexecuted instantiation: fse_decompress.c:BIT_endOfDStream |
373 | | |
374 | | #endif /* BITSTREAM_H_MODULE */ |