/src/brotli/c/dec/bit_reader.h
Line | Count | Source |
1 | | /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | | |
3 | | Distributed under MIT license. |
4 | | See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 | | */ |
6 | | |
7 | | /* Bit reading helpers */ |
8 | | |
9 | | #ifndef BROTLI_DEC_BIT_READER_H_ |
10 | | #define BROTLI_DEC_BIT_READER_H_ |
11 | | |
12 | | #include "../common/constants.h" |
13 | | #include "../common/platform.h" |
14 | | |
15 | | #if defined(__cplusplus) || defined(c_plusplus) |
16 | | extern "C" { |
17 | | #endif |
18 | | |
19 | 0 | #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1) |
20 | | |
21 | | /* 162 bits + 7 bytes */ |
22 | 0 | #define BROTLI_FAST_INPUT_SLACK 28 |
23 | | |
24 | | BROTLI_INTERNAL extern const brotli_reg_t kBrotliBitMask[33]; |
25 | | |
26 | 0 | static BROTLI_INLINE brotli_reg_t BitMask(brotli_reg_t n) { |
27 | 0 | if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) { |
28 | 0 | /* Masking with this expression turns to a single |
29 | 0 | "Unsigned Bit Field Extract" UBFX instruction on ARM. */ |
30 | 0 | return ~(~((brotli_reg_t)0) << n); |
31 | 0 | } else { |
32 | 0 | return kBrotliBitMask[n]; |
33 | 0 | } |
34 | 0 | } |
35 | | |
36 | | typedef struct { |
37 | | brotli_reg_t val_; /* pre-fetched bits */ |
38 | | brotli_reg_t bit_pos_; /* current bit-reading position in val_ */ |
39 | | const uint8_t* next_in; /* the byte we're reading from */ |
40 | | const uint8_t* guard_in; /* position from which "fast-path" is prohibited */ |
41 | | const uint8_t* last_in; /* == next_in + avail_in */ |
42 | | } BrotliBitReader; |
43 | | |
44 | | typedef struct { |
45 | | brotli_reg_t val_; |
46 | | brotli_reg_t bit_pos_; |
47 | | const uint8_t* next_in; |
48 | | size_t avail_in; |
49 | | } BrotliBitReaderState; |
50 | | |
51 | | /* Initializes the BrotliBitReader fields. */ |
52 | | BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* br); |
53 | | |
54 | | /* Ensures that accumulator is not empty. |
55 | | May consume up to sizeof(brotli_reg_t) - 1 bytes of input. |
56 | | Returns BROTLI_FALSE if data is required but there is no input available. |
57 | | For !BROTLI_UNALIGNED_READ_FAST this function also prepares bit reader for |
58 | | aligned reading. */ |
59 | | BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* br); |
60 | | |
61 | | /* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden |
62 | | the main code-path. Never called for RFC brotli streams, required only for |
63 | | "large-window" mode and other extensions. */ |
64 | | BROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow( |
65 | | BrotliBitReader* br, brotli_reg_t n_bits, brotli_reg_t* val); |
66 | | |
67 | | static BROTLI_INLINE size_t |
68 | 0 | BrotliBitReaderGetAvailIn(BrotliBitReader* const br) { |
69 | 0 | return (size_t)(br->last_in - br->next_in); |
70 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderGetAvailIn Unexecuted instantiation: state.c:BrotliBitReaderGetAvailIn Unexecuted instantiation: bit_reader.c:BrotliBitReaderGetAvailIn |
71 | | |
72 | | static BROTLI_INLINE void BrotliBitReaderSaveState( |
73 | 0 | BrotliBitReader* const from, BrotliBitReaderState* to) { |
74 | 0 | to->val_ = from->val_; |
75 | 0 | to->bit_pos_ = from->bit_pos_; |
76 | 0 | to->next_in = from->next_in; |
77 | 0 | to->avail_in = BrotliBitReaderGetAvailIn(from); |
78 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderSaveState Unexecuted instantiation: state.c:BrotliBitReaderSaveState Unexecuted instantiation: bit_reader.c:BrotliBitReaderSaveState |
79 | | |
80 | | static BROTLI_INLINE void BrotliBitReaderSetInput( |
81 | 0 | BrotliBitReader* const br, const uint8_t* next_in, size_t avail_in) { |
82 | 0 | br->next_in = next_in; |
83 | 0 | br->last_in = (avail_in == 0) ? next_in : (next_in + avail_in); |
84 | 0 | if (avail_in + 1 > BROTLI_FAST_INPUT_SLACK) { |
85 | 0 | br->guard_in = next_in + (avail_in + 1 - BROTLI_FAST_INPUT_SLACK); |
86 | 0 | } else { |
87 | 0 | br->guard_in = next_in; |
88 | 0 | } |
89 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderSetInput Unexecuted instantiation: state.c:BrotliBitReaderSetInput Unexecuted instantiation: bit_reader.c:BrotliBitReaderSetInput |
90 | | |
91 | | static BROTLI_INLINE void BrotliBitReaderRestoreState( |
92 | 0 | BrotliBitReader* const to, BrotliBitReaderState* from) { |
93 | 0 | to->val_ = from->val_; |
94 | 0 | to->bit_pos_ = from->bit_pos_; |
95 | 0 | to->next_in = from->next_in; |
96 | 0 | BrotliBitReaderSetInput(to, from->next_in, from->avail_in); |
97 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderRestoreState Unexecuted instantiation: state.c:BrotliBitReaderRestoreState Unexecuted instantiation: bit_reader.c:BrotliBitReaderRestoreState |
98 | | |
99 | | static BROTLI_INLINE brotli_reg_t BrotliGetAvailableBits( |
100 | 0 | const BrotliBitReader* br) { |
101 | 0 | return br->bit_pos_; |
102 | 0 | } Unexecuted instantiation: decode.c:BrotliGetAvailableBits Unexecuted instantiation: state.c:BrotliGetAvailableBits Unexecuted instantiation: bit_reader.c:BrotliGetAvailableBits |
103 | | |
104 | | /* Returns amount of unread bytes the bit reader still has buffered from the |
105 | | BrotliInput, including whole bytes in br->val_. Result is capped with |
106 | | maximal ring-buffer size (larger number won't be utilized anyway). */ |
107 | 0 | static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) { |
108 | 0 | static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS; |
109 | 0 | size_t avail_in = BrotliBitReaderGetAvailIn(br); |
110 | 0 | if (avail_in > kCap) return kCap; |
111 | 0 | return avail_in + (BrotliGetAvailableBits(br) >> 3); |
112 | 0 | } Unexecuted instantiation: decode.c:BrotliGetRemainingBytes Unexecuted instantiation: state.c:BrotliGetRemainingBytes Unexecuted instantiation: bit_reader.c:BrotliGetRemainingBytes |
113 | | |
114 | | /* Checks if there is at least |num| bytes left in the input ring-buffer |
115 | | (excluding the bits remaining in br->val_). */ |
116 | | static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount( |
117 | 0 | BrotliBitReader* const br) { |
118 | 0 | return TO_BROTLI_BOOL(br->next_in < br->guard_in); |
119 | 0 | } Unexecuted instantiation: decode.c:BrotliCheckInputAmount Unexecuted instantiation: state.c:BrotliCheckInputAmount Unexecuted instantiation: bit_reader.c:BrotliCheckInputAmount |
120 | | |
121 | | /* Load more bits into accumulator. */ |
122 | | static BROTLI_INLINE brotli_reg_t BrotliBitReaderLoadBits(brotli_reg_t val, |
123 | | brotli_reg_t new_bits, |
124 | | brotli_reg_t count, |
125 | 0 | brotli_reg_t offset) { |
126 | 0 | BROTLI_DCHECK( |
127 | 0 | !((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count))); |
128 | 0 | (void)count; |
129 | 0 | return val | (new_bits << offset); |
130 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderLoadBits Unexecuted instantiation: state.c:BrotliBitReaderLoadBits Unexecuted instantiation: bit_reader.c:BrotliBitReaderLoadBits |
131 | | |
132 | | /* Guarantees that there are at least |n_bits| + 1 bits in accumulator. |
133 | | Precondition: accumulator contains at least 1 bit. |
134 | | |n_bits| should be in the range [1..24] for regular build. For portable |
135 | | non-64-bit little-endian build only 16 bits are safe to request. */ |
136 | | static BROTLI_INLINE void BrotliFillBitWindow( |
137 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits) { |
138 | 0 | #if (BROTLI_64_BITS) |
139 | 0 | if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) && |
140 | 0 | (n_bits <= 8)) { |
141 | 0 | brotli_reg_t bit_pos = br->bit_pos_; |
142 | 0 | if (bit_pos <= 8) { |
143 | 0 | br->val_ = BrotliBitReaderLoadBits(br->val_, |
144 | 0 | BROTLI_UNALIGNED_LOAD64LE(br->next_in), 56, bit_pos); |
145 | 0 | br->bit_pos_ = bit_pos + 56; |
146 | 0 | br->next_in += 7; |
147 | 0 | } |
148 | 0 | } else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) && |
149 | 0 | (n_bits <= 16)) { |
150 | 0 | brotli_reg_t bit_pos = br->bit_pos_; |
151 | 0 | if (bit_pos <= 16) { |
152 | 0 | br->val_ = BrotliBitReaderLoadBits(br->val_, |
153 | 0 | BROTLI_UNALIGNED_LOAD64LE(br->next_in), 48, bit_pos); |
154 | 0 | br->bit_pos_ = bit_pos + 48; |
155 | 0 | br->next_in += 6; |
156 | 0 | } |
157 | 0 | } else { |
158 | 0 | brotli_reg_t bit_pos = br->bit_pos_; |
159 | 0 | if (bit_pos <= 32) { |
160 | 0 | br->val_ = BrotliBitReaderLoadBits(br->val_, |
161 | 0 | (uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in), 32, bit_pos); |
162 | 0 | br->bit_pos_ = bit_pos + 32; |
163 | 0 | br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ; |
164 | 0 | } |
165 | 0 | } |
166 | | #else |
167 | | if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) && |
168 | | (n_bits <= 8)) { |
169 | | brotli_reg_t bit_pos = br->bit_pos_; |
170 | | if (bit_pos <= 8) { |
171 | | br->val_ = BrotliBitReaderLoadBits(br->val_, |
172 | | BROTLI_UNALIGNED_LOAD32LE(br->next_in), 24, bit_pos); |
173 | | br->bit_pos_ = bit_pos + 24; |
174 | | br->next_in += 3; |
175 | | } |
176 | | } else { |
177 | | brotli_reg_t bit_pos = br->bit_pos_; |
178 | | if (bit_pos <= 16) { |
179 | | br->val_ = BrotliBitReaderLoadBits(br->val_, |
180 | | (uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in), 16, bit_pos); |
181 | | br->bit_pos_ = bit_pos + 16; |
182 | | br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ; |
183 | | } |
184 | | } |
185 | | #endif |
186 | 0 | } Unexecuted instantiation: decode.c:BrotliFillBitWindow Unexecuted instantiation: state.c:BrotliFillBitWindow Unexecuted instantiation: bit_reader.c:BrotliFillBitWindow |
187 | | |
188 | | /* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no |
189 | | more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */ |
190 | 0 | static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) { |
191 | 0 | BrotliFillBitWindow(br, 17); |
192 | 0 | } Unexecuted instantiation: decode.c:BrotliFillBitWindow16 Unexecuted instantiation: state.c:BrotliFillBitWindow16 Unexecuted instantiation: bit_reader.c:BrotliFillBitWindow16 |
193 | | |
194 | | /* Tries to pull one byte of input to accumulator. |
195 | | Returns BROTLI_FALSE if there is no input available. */ |
196 | 0 | static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) { |
197 | 0 | if (br->next_in == br->last_in) { |
198 | 0 | return BROTLI_FALSE; |
199 | 0 | } |
200 | 0 | br->val_ = BrotliBitReaderLoadBits(br->val_, |
201 | 0 | (brotli_reg_t)*br->next_in, 8, br->bit_pos_); |
202 | 0 | br->bit_pos_ += 8; |
203 | 0 | ++br->next_in; |
204 | 0 | return BROTLI_TRUE; |
205 | 0 | } Unexecuted instantiation: decode.c:BrotliPullByte Unexecuted instantiation: state.c:BrotliPullByte Unexecuted instantiation: bit_reader.c:BrotliPullByte |
206 | | |
207 | | /* Returns currently available bits. |
208 | | The number of valid bits could be calculated by BrotliGetAvailableBits. */ |
209 | | static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked( |
210 | 0 | BrotliBitReader* const br) { |
211 | 0 | return br->val_; |
212 | 0 | } Unexecuted instantiation: decode.c:BrotliGetBitsUnmasked Unexecuted instantiation: state.c:BrotliGetBitsUnmasked Unexecuted instantiation: bit_reader.c:BrotliGetBitsUnmasked |
213 | | |
214 | | /* Like BrotliGetBits, but does not mask the result. |
215 | | The result contains at least 16 valid bits. */ |
216 | | static BROTLI_INLINE brotli_reg_t BrotliGet16BitsUnmasked( |
217 | 0 | BrotliBitReader* const br) { |
218 | 0 | BrotliFillBitWindow(br, 16); |
219 | 0 | return (brotli_reg_t)BrotliGetBitsUnmasked(br); |
220 | 0 | } Unexecuted instantiation: decode.c:BrotliGet16BitsUnmasked Unexecuted instantiation: state.c:BrotliGet16BitsUnmasked Unexecuted instantiation: bit_reader.c:BrotliGet16BitsUnmasked |
221 | | |
222 | | /* Returns the specified number of bits from |br| without advancing bit |
223 | | position. */ |
224 | | static BROTLI_INLINE brotli_reg_t BrotliGetBits( |
225 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits) { |
226 | 0 | BrotliFillBitWindow(br, n_bits); |
227 | 0 | return BrotliGetBitsUnmasked(br) & BitMask(n_bits); |
228 | 0 | } Unexecuted instantiation: decode.c:BrotliGetBits Unexecuted instantiation: state.c:BrotliGetBits Unexecuted instantiation: bit_reader.c:BrotliGetBits |
229 | | |
230 | | /* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there |
231 | | is not enough input. */ |
232 | | static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits( |
233 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) { |
234 | 0 | while (BrotliGetAvailableBits(br) < n_bits) { |
235 | 0 | if (!BrotliPullByte(br)) { |
236 | 0 | return BROTLI_FALSE; |
237 | 0 | } |
238 | 0 | } |
239 | 0 | *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits); |
240 | 0 | return BROTLI_TRUE; |
241 | 0 | } Unexecuted instantiation: decode.c:BrotliSafeGetBits Unexecuted instantiation: state.c:BrotliSafeGetBits Unexecuted instantiation: bit_reader.c:BrotliSafeGetBits |
242 | | |
243 | | /* Advances the bit pos by |n_bits|. */ |
244 | | static BROTLI_INLINE void BrotliDropBits( |
245 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits) { |
246 | 0 | br->bit_pos_ -= n_bits; |
247 | 0 | br->val_ >>= n_bits; |
248 | 0 | } Unexecuted instantiation: decode.c:BrotliDropBits Unexecuted instantiation: state.c:BrotliDropBits Unexecuted instantiation: bit_reader.c:BrotliDropBits |
249 | | |
250 | | /* Make sure that there are no spectre bits in accumulator. |
251 | | This is important for the cases when some bytes are skipped |
252 | | (i.e. never placed into accumulator). */ |
253 | 0 | static BROTLI_INLINE void BrotliBitReaderNormalize(BrotliBitReader* br) { |
254 | | /* Actually, it is enough to normalize when br->bit_pos_ == 0 */ |
255 | 0 | if (br->bit_pos_ < (sizeof(brotli_reg_t) << 3u)) { |
256 | 0 | br->val_ &= (((brotli_reg_t)1) << br->bit_pos_) - 1; |
257 | 0 | } |
258 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderNormalize Unexecuted instantiation: state.c:BrotliBitReaderNormalize Unexecuted instantiation: bit_reader.c:BrotliBitReaderNormalize |
259 | | |
260 | 0 | static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) { |
261 | 0 | brotli_reg_t unused_bytes = BrotliGetAvailableBits(br) >> 3; |
262 | 0 | brotli_reg_t unused_bits = unused_bytes << 3; |
263 | 0 | br->next_in = |
264 | 0 | (unused_bytes == 0) ? br->next_in : (br->next_in - unused_bytes); |
265 | 0 | br->bit_pos_ -= unused_bits; |
266 | 0 | BrotliBitReaderNormalize(br); |
267 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderUnload Unexecuted instantiation: state.c:BrotliBitReaderUnload Unexecuted instantiation: bit_reader.c:BrotliBitReaderUnload |
268 | | |
269 | | /* Reads the specified number of bits from |br| and advances the bit pos. |
270 | | Precondition: accumulator MUST contain at least |n_bits|. */ |
271 | | static BROTLI_INLINE void BrotliTakeBits(BrotliBitReader* const br, |
272 | | brotli_reg_t n_bits, |
273 | 0 | brotli_reg_t* val) { |
274 | 0 | *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits); |
275 | 0 | BROTLI_LOG(("[BrotliTakeBits] %d %d %d val: %6x\n", |
276 | 0 | (int)BrotliBitReaderGetAvailIn(br), (int)br->bit_pos_, |
277 | 0 | (int)n_bits, (int)*val)); |
278 | 0 | BrotliDropBits(br, n_bits); |
279 | 0 | } Unexecuted instantiation: decode.c:BrotliTakeBits Unexecuted instantiation: state.c:BrotliTakeBits Unexecuted instantiation: bit_reader.c:BrotliTakeBits |
280 | | |
281 | | /* Reads the specified number of bits from |br| and advances the bit pos. |
282 | | Assumes that there is enough input to perform BrotliFillBitWindow. |
283 | | Up to 24 bits are allowed to be requested from this method. */ |
284 | | static BROTLI_INLINE brotli_reg_t BrotliReadBits24( |
285 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits) { |
286 | 0 | BROTLI_DCHECK(n_bits <= 24); |
287 | 0 | if (BROTLI_64_BITS || (n_bits <= 16)) { |
288 | 0 | brotli_reg_t val; |
289 | 0 | BrotliFillBitWindow(br, n_bits); |
290 | 0 | BrotliTakeBits(br, n_bits, &val); |
291 | 0 | return val; |
292 | 0 | } else { |
293 | 0 | brotli_reg_t low_val; |
294 | 0 | brotli_reg_t high_val; |
295 | 0 | BrotliFillBitWindow(br, 16); |
296 | 0 | BrotliTakeBits(br, 16, &low_val); |
297 | 0 | BrotliFillBitWindow(br, 8); |
298 | 0 | BrotliTakeBits(br, n_bits - 16, &high_val); |
299 | 0 | return low_val | (high_val << 16); |
300 | 0 | } |
301 | 0 | } Unexecuted instantiation: decode.c:BrotliReadBits24 Unexecuted instantiation: state.c:BrotliReadBits24 Unexecuted instantiation: bit_reader.c:BrotliReadBits24 |
302 | | |
303 | | /* Same as BrotliReadBits24, but allows reading up to 32 bits. */ |
304 | | static BROTLI_INLINE brotli_reg_t BrotliReadBits32( |
305 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits) { |
306 | 0 | BROTLI_DCHECK(n_bits <= 32); |
307 | 0 | if (BROTLI_64_BITS || (n_bits <= 16)) { |
308 | 0 | brotli_reg_t val; |
309 | 0 | BrotliFillBitWindow(br, n_bits); |
310 | 0 | BrotliTakeBits(br, n_bits, &val); |
311 | 0 | return val; |
312 | 0 | } else { |
313 | 0 | brotli_reg_t low_val; |
314 | 0 | brotli_reg_t high_val; |
315 | 0 | BrotliFillBitWindow(br, 16); |
316 | 0 | BrotliTakeBits(br, 16, &low_val); |
317 | 0 | BrotliFillBitWindow(br, 16); |
318 | 0 | BrotliTakeBits(br, n_bits - 16, &high_val); |
319 | 0 | return low_val | (high_val << 16); |
320 | 0 | } |
321 | 0 | } Unexecuted instantiation: decode.c:BrotliReadBits32 Unexecuted instantiation: state.c:BrotliReadBits32 Unexecuted instantiation: bit_reader.c:BrotliReadBits32 |
322 | | |
323 | | /* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there |
324 | | is not enough input. |n_bits| MUST be positive. |
325 | | Up to 24 bits are allowed to be requested from this method. */ |
326 | | static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits( |
327 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) { |
328 | 0 | BROTLI_DCHECK(n_bits <= 24); |
329 | 0 | while (BrotliGetAvailableBits(br) < n_bits) { |
330 | 0 | if (!BrotliPullByte(br)) { |
331 | 0 | return BROTLI_FALSE; |
332 | 0 | } |
333 | 0 | } |
334 | 0 | BrotliTakeBits(br, n_bits, val); |
335 | 0 | return BROTLI_TRUE; |
336 | 0 | } Unexecuted instantiation: decode.c:BrotliSafeReadBits Unexecuted instantiation: state.c:BrotliSafeReadBits Unexecuted instantiation: bit_reader.c:BrotliSafeReadBits |
337 | | |
338 | | /* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */ |
339 | | static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32( |
340 | 0 | BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) { |
341 | 0 | BROTLI_DCHECK(n_bits <= 32); |
342 | 0 | if (BROTLI_64_BITS || (n_bits <= 24)) { |
343 | 0 | while (BrotliGetAvailableBits(br) < n_bits) { |
344 | 0 | if (!BrotliPullByte(br)) { |
345 | 0 | return BROTLI_FALSE; |
346 | 0 | } |
347 | 0 | } |
348 | 0 | BrotliTakeBits(br, n_bits, val); |
349 | 0 | return BROTLI_TRUE; |
350 | 0 | } else { |
351 | 0 | return BrotliSafeReadBits32Slow(br, n_bits, val); |
352 | 0 | } |
353 | 0 | } Unexecuted instantiation: decode.c:BrotliSafeReadBits32 Unexecuted instantiation: state.c:BrotliSafeReadBits32 Unexecuted instantiation: bit_reader.c:BrotliSafeReadBits32 |
354 | | |
355 | | /* Advances the bit reader position to the next byte boundary and verifies |
356 | | that any skipped bits are set to zero. */ |
357 | 0 | static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) { |
358 | 0 | brotli_reg_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7; |
359 | 0 | brotli_reg_t pad_bits = 0; |
360 | 0 | if (pad_bits_count != 0) { |
361 | 0 | BrotliTakeBits(br, pad_bits_count, &pad_bits); |
362 | 0 | } |
363 | 0 | BrotliBitReaderNormalize(br); |
364 | 0 | return TO_BROTLI_BOOL(pad_bits == 0); |
365 | 0 | } Unexecuted instantiation: decode.c:BrotliJumpToByteBoundary Unexecuted instantiation: state.c:BrotliJumpToByteBoundary Unexecuted instantiation: bit_reader.c:BrotliJumpToByteBoundary |
366 | | |
367 | 0 | static BROTLI_INLINE void BrotliDropBytes(BrotliBitReader* br, size_t num) { |
368 | | /* Check detour is legal: accumulator must to be empty. */ |
369 | 0 | BROTLI_DCHECK(br->bit_pos_ == 0); |
370 | 0 | BROTLI_DCHECK(br->val_ == 0); |
371 | 0 | br->next_in += num; |
372 | 0 | } Unexecuted instantiation: decode.c:BrotliDropBytes Unexecuted instantiation: state.c:BrotliDropBytes Unexecuted instantiation: bit_reader.c:BrotliDropBytes |
373 | | |
374 | | /* Copies remaining input bytes stored in the bit reader to the output. Value |
375 | | |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be |
376 | | warmed up again after this. */ |
377 | | static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest, |
378 | 0 | BrotliBitReader* br, size_t num) { |
379 | 0 | while (BrotliGetAvailableBits(br) >= 8 && num > 0) { |
380 | 0 | *dest = (uint8_t)BrotliGetBitsUnmasked(br); |
381 | 0 | BrotliDropBits(br, 8); |
382 | 0 | ++dest; |
383 | 0 | --num; |
384 | 0 | } |
385 | 0 | BrotliBitReaderNormalize(br); |
386 | 0 | if (num > 0) { |
387 | 0 | memcpy(dest, br->next_in, num); |
388 | 0 | BrotliDropBytes(br, num); |
389 | 0 | } |
390 | 0 | } Unexecuted instantiation: decode.c:BrotliCopyBytes Unexecuted instantiation: state.c:BrotliCopyBytes Unexecuted instantiation: bit_reader.c:BrotliCopyBytes |
391 | | |
392 | 0 | BROTLI_UNUSED_FUNCTION void BrotliBitReaderSuppressUnusedFunctions(void) { |
393 | 0 | BROTLI_UNUSED(&BrotliBitReaderSuppressUnusedFunctions); |
394 | 0 |
|
395 | 0 | BROTLI_UNUSED(&BrotliBitReaderGetAvailIn); |
396 | 0 | BROTLI_UNUSED(&BrotliBitReaderLoadBits); |
397 | 0 | BROTLI_UNUSED(&BrotliBitReaderRestoreState); |
398 | 0 | BROTLI_UNUSED(&BrotliBitReaderSaveState); |
399 | 0 | BROTLI_UNUSED(&BrotliBitReaderSetInput); |
400 | 0 | BROTLI_UNUSED(&BrotliBitReaderUnload); |
401 | 0 | BROTLI_UNUSED(&BrotliCheckInputAmount); |
402 | 0 | BROTLI_UNUSED(&BrotliCopyBytes); |
403 | 0 | BROTLI_UNUSED(&BrotliFillBitWindow16); |
404 | 0 | BROTLI_UNUSED(&BrotliGet16BitsUnmasked); |
405 | 0 | BROTLI_UNUSED(&BrotliGetBits); |
406 | 0 | BROTLI_UNUSED(&BrotliGetRemainingBytes); |
407 | 0 | BROTLI_UNUSED(&BrotliJumpToByteBoundary); |
408 | 0 | BROTLI_UNUSED(&BrotliReadBits24); |
409 | 0 | BROTLI_UNUSED(&BrotliReadBits32); |
410 | 0 | BROTLI_UNUSED(&BrotliSafeGetBits); |
411 | 0 | BROTLI_UNUSED(&BrotliSafeReadBits); |
412 | 0 | BROTLI_UNUSED(&BrotliSafeReadBits32); |
413 | 0 | } Unexecuted instantiation: decode.c:BrotliBitReaderSuppressUnusedFunctions Unexecuted instantiation: state.c:BrotliBitReaderSuppressUnusedFunctions Unexecuted instantiation: bit_reader.c:BrotliBitReaderSuppressUnusedFunctions |
414 | | |
415 | | #if defined(__cplusplus) || defined(c_plusplus) |
416 | | } /* extern "C" */ |
417 | | #endif |
418 | | |
419 | | #endif /* BROTLI_DEC_BIT_READER_H_ */ |