OLD | NEW |
1 /* Copyright 2013 Google Inc. All Rights Reserved. | 1 /* Copyright 2013 Google Inc. All Rights Reserved. |
2 | 2 |
3 Distributed under MIT license. | 3 Distributed under MIT license. |
4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT | 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT |
5 */ | 5 */ |
6 | 6 |
7 /* Bit reading helpers */ | 7 /* Bit reading helpers */ |
8 | 8 |
9 #ifndef BROTLI_DEC_BIT_READER_H_ | 9 #ifndef BROTLI_DEC_BIT_READER_H_ |
10 #define BROTLI_DEC_BIT_READER_H_ | 10 #define BROTLI_DEC_BIT_READER_H_ |
11 | 11 |
12 #include <string.h> /* memcpy */ | 12 #include <string.h> /* memcpy */ |
13 | 13 |
| 14 #include <brotli/types.h> |
14 #include "./port.h" | 15 #include "./port.h" |
15 #include "./types.h" | |
16 | 16 |
17 #if defined(__cplusplus) || defined(c_plusplus) | 17 #if defined(__cplusplus) || defined(c_plusplus) |
18 extern "C" { | 18 extern "C" { |
19 #endif | 19 #endif |
20 | 20 |
21 #if (BROTLI_64_BITS) | 21 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(reg_t) >> 1) |
22 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 4 | |
23 typedef uint64_t reg_t; | |
24 #else | |
25 #define BROTLI_SHORT_FILL_BIT_WINDOW_READ 2 | |
26 typedef uint32_t reg_t; | |
27 #endif | |
28 | 22 |
29 static const uint32_t kBitMask[33] = { 0x0000, | 23 static const uint32_t kBitMask[33] = { 0x0000, |
30 0x00000001, 0x00000003, 0x00000007, 0x0000000F, | 24 0x00000001, 0x00000003, 0x00000007, 0x0000000F, |
31 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF, | 25 0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF, |
32 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF, | 26 0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF, |
33 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, | 27 0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF, |
34 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF, | 28 0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF, |
35 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF, | 29 0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF, |
36 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, | 30 0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF, |
37 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF | 31 0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF |
(...skipping 16 matching lines...) Expand all Loading... |
54 size_t avail_in; | 48 size_t avail_in; |
55 } BrotliBitReader; | 49 } BrotliBitReader; |
56 | 50 |
57 typedef struct { | 51 typedef struct { |
58 reg_t val_; | 52 reg_t val_; |
59 uint32_t bit_pos_; | 53 uint32_t bit_pos_; |
60 const uint8_t* next_in; | 54 const uint8_t* next_in; |
61 size_t avail_in; | 55 size_t avail_in; |
62 } BrotliBitReaderState; | 56 } BrotliBitReaderState; |
63 | 57 |
64 /* Initializes the bitreader fields. */ | 58 /* Initializes the BrotliBitReader fields. */ |
65 BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br); | 59 BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br); |
66 | 60 |
67 /* Ensures that accumulator is not empty. May consume one byte of input. | 61 /* Ensures that accumulator is not empty. May consume one byte of input. |
68 Returns 0 if data is required but there is no input available. | 62 Returns 0 if data is required but there is no input available. |
69 For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned | 63 For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned |
70 reading. */ | 64 reading. */ |
71 BROTLI_INTERNAL int BrotliWarmupBitReader(BrotliBitReader* const br); | 65 BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br); |
72 | 66 |
73 static BROTLI_INLINE void BrotliBitReaderSaveState( | 67 static BROTLI_INLINE void BrotliBitReaderSaveState( |
74 BrotliBitReader* const from, BrotliBitReaderState* to) { | 68 BrotliBitReader* const from, BrotliBitReaderState* to) { |
75 to->val_ = from->val_; | 69 to->val_ = from->val_; |
76 to->bit_pos_ = from->bit_pos_; | 70 to->bit_pos_ = from->bit_pos_; |
77 to->next_in = from->next_in; | 71 to->next_in = from->next_in; |
78 to->avail_in = from->avail_in; | 72 to->avail_in = from->avail_in; |
79 } | 73 } |
80 | 74 |
81 static BROTLI_INLINE void BrotliBitReaderRestoreState( | 75 static BROTLI_INLINE void BrotliBitReaderRestoreState( |
82 BrotliBitReader* const to, BrotliBitReaderState* from) { | 76 BrotliBitReader* const to, BrotliBitReaderState* from) { |
83 to->val_ = from->val_; | 77 to->val_ = from->val_; |
84 to->bit_pos_ = from->bit_pos_; | 78 to->bit_pos_ = from->bit_pos_; |
85 to->next_in = from->next_in; | 79 to->next_in = from->next_in; |
86 to->avail_in = from->avail_in; | 80 to->avail_in = from->avail_in; |
87 } | 81 } |
88 | 82 |
89 static BROTLI_INLINE uint32_t BrotliGetAvailableBits( | 83 static BROTLI_INLINE uint32_t BrotliGetAvailableBits( |
90 const BrotliBitReader* br) { | 84 const BrotliBitReader* br) { |
91 return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_; | 85 return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_; |
92 } | 86 } |
93 | 87 |
94 /* Returns amount of unread bytes the bit reader still has buffered from the | 88 /* Returns amount of unread bytes the bit reader still has buffered from the |
95 BrotliInput, including whole bytes in br->val_. */ | 89 BrotliInput, including whole bytes in br->val_. */ |
96 static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) { | 90 static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) { |
97 return br->avail_in + (BrotliGetAvailableBits(br) >> 3); | 91 return br->avail_in + (BrotliGetAvailableBits(br) >> 3); |
98 } | 92 } |
99 | 93 |
100 /* Checks if there is at least num bytes left in the input ringbuffer (excluding | 94 /* Checks if there is at least |num| bytes left in the input ring-buffer |
101 the bits remaining in br->val_). */ | 95 (excluding the bits remaining in br->val_). */ |
102 static BROTLI_INLINE int BrotliCheckInputAmount( | 96 static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount( |
103 BrotliBitReader* const br, size_t num) { | 97 BrotliBitReader* const br, size_t num) { |
104 return br->avail_in >= num; | 98 return TO_BROTLI_BOOL(br->avail_in >= num); |
105 } | 99 } |
106 | 100 |
107 static BROTLI_INLINE uint16_t BrotliLoad16LE(const uint8_t* in) { | 101 static BROTLI_INLINE uint16_t BrotliLoad16LE(const uint8_t* in) { |
108 if (BROTLI_LITTLE_ENDIAN) { | 102 if (BROTLI_LITTLE_ENDIAN) { |
109 return *((const uint16_t*)in); | 103 return *((const uint16_t*)in); |
110 } else if (BROTLI_BIG_ENDIAN) { | 104 } else if (BROTLI_BIG_ENDIAN) { |
111 uint16_t value = *((const uint16_t*)in); | 105 uint16_t value = *((const uint16_t*)in); |
112 return (uint16_t)(((value & 0xFFU) << 8) | ((value & 0xFF00U) >> 8)); | 106 return (uint16_t)(((value & 0xFFU) << 8) | ((value & 0xFF00U) >> 8)); |
113 } else { | 107 } else { |
114 return (uint16_t)(in[0] | (in[1] << 8)); | 108 return (uint16_t)(in[0] | (in[1] << 8)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 value |= (uint64_t)(*(in++)) << 48; | 150 value |= (uint64_t)(*(in++)) << 48; |
157 value |= (uint64_t)(*(in++)) << 56; | 151 value |= (uint64_t)(*(in++)) << 56; |
158 return value; | 152 return value; |
159 } | 153 } |
160 } | 154 } |
161 #endif | 155 #endif |
162 | 156 |
163 /* Guarantees that there are at least n_bits + 1 bits in accumulator. | 157 /* Guarantees that there are at least n_bits + 1 bits in accumulator. |
164 Precondition: accumulator contains at least 1 bit. | 158 Precondition: accumulator contains at least 1 bit. |
165 n_bits should be in the range [1..24] for regular build. For portable | 159 n_bits should be in the range [1..24] for regular build. For portable |
166 non-64-bit little endian build only 16 bits are safe to request. */ | 160 non-64-bit little-endian build only 16 bits are safe to request. */ |
167 static BROTLI_INLINE void BrotliFillBitWindow( | 161 static BROTLI_INLINE void BrotliFillBitWindow( |
168 BrotliBitReader* const br, uint32_t n_bits) { | 162 BrotliBitReader* const br, uint32_t n_bits) { |
169 #if (BROTLI_64_BITS) | 163 #if (BROTLI_64_BITS) |
170 if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) { | 164 if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) { |
171 if (br->bit_pos_ >= 56) { | 165 if (br->bit_pos_ >= 56) { |
172 br->val_ >>= 56; | 166 br->val_ >>= 56; |
173 br->bit_pos_ ^= 56; /* here same as -= 56 because of the if condition */ | 167 br->bit_pos_ ^= 56; /* here same as -= 56 because of the if condition */ |
174 br->val_ |= BrotliLoad64LE(br->next_in) << 8; | 168 br->val_ |= BrotliLoad64LE(br->next_in) << 8; |
175 br->avail_in -= 7; | 169 br->avail_in -= 7; |
176 br->next_in += 7; | 170 br->next_in += 7; |
(...skipping 29 matching lines...) Expand all Loading... |
206 br->val_ >>= 16; | 200 br->val_ >>= 16; |
207 br->bit_pos_ ^= 16; /* here same as -= 16 because of the if condition */ | 201 br->bit_pos_ ^= 16; /* here same as -= 16 because of the if condition */ |
208 br->val_ |= ((uint32_t)BrotliLoad16LE(br->next_in)) << 16; | 202 br->val_ |= ((uint32_t)BrotliLoad16LE(br->next_in)) << 16; |
209 br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ; | 203 br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ; |
210 br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ; | 204 br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ; |
211 } | 205 } |
212 } | 206 } |
213 #endif | 207 #endif |
214 } | 208 } |
215 | 209 |
216 /* Mosltly like BrotliFillBitWindow, but guarantees only 16 bits and reads no | 210 /* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no |
217 more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */ | 211 more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */ |
218 static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) { | 212 static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) { |
219 BrotliFillBitWindow(br, 17); | 213 BrotliFillBitWindow(br, 17); |
220 } | 214 } |
221 | 215 |
222 /* Pulls one byte of input to accumulator. */ | 216 /* Pulls one byte of input to accumulator. */ |
223 static BROTLI_INLINE int BrotliPullByte(BrotliBitReader* const br) { | 217 static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) { |
224 if (br->avail_in == 0) { | 218 if (br->avail_in == 0) { |
225 return 0; | 219 return BROTLI_FALSE; |
226 } | 220 } |
227 br->val_ >>= 8; | 221 br->val_ >>= 8; |
228 #if (BROTLI_64_BITS) | 222 #if (BROTLI_64_BITS) |
229 br->val_ |= ((uint64_t)*br->next_in) << 56; | 223 br->val_ |= ((uint64_t)*br->next_in) << 56; |
230 #else | 224 #else |
231 br->val_ |= ((uint32_t)*br->next_in) << 24; | 225 br->val_ |= ((uint32_t)*br->next_in) << 24; |
232 #endif | 226 #endif |
233 br->bit_pos_ -= 8; | 227 br->bit_pos_ -= 8; |
234 --br->avail_in; | 228 --br->avail_in; |
235 ++br->next_in; | 229 ++br->next_in; |
236 return 1; | 230 return BROTLI_TRUE; |
237 } | 231 } |
238 | 232 |
239 /* Returns currently available bits. | 233 /* Returns currently available bits. |
240 The number of valid bits could be calclulated by BrotliGetAvailableBits. */ | 234 The number of valid bits could be calculated by BrotliGetAvailableBits. */ |
241 static BROTLI_INLINE reg_t BrotliGetBitsUnmasked(BrotliBitReader* const br) { | 235 static BROTLI_INLINE reg_t BrotliGetBitsUnmasked(BrotliBitReader* const br) { |
242 return br->val_ >> br->bit_pos_; | 236 return br->val_ >> br->bit_pos_; |
243 } | 237 } |
244 | 238 |
245 /* Like BrotliGetBits, but does not mask the result. | 239 /* Like BrotliGetBits, but does not mask the result. |
246 The result contains at least 16 valid bits. */ | 240 The result contains at least 16 valid bits. */ |
247 static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked( | 241 static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked( |
248 BrotliBitReader* const br) { | 242 BrotliBitReader* const br) { |
249 BrotliFillBitWindow(br, 16); | 243 BrotliFillBitWindow(br, 16); |
250 return (uint32_t)BrotliGetBitsUnmasked(br); | 244 return (uint32_t)BrotliGetBitsUnmasked(br); |
251 } | 245 } |
252 | 246 |
253 /* Returns the specified number of bits from br without advancing bit pos. */ | 247 /* Returns the specified number of bits from |br| without advancing bit pos. */ |
254 static BROTLI_INLINE uint32_t BrotliGetBits( | 248 static BROTLI_INLINE uint32_t BrotliGetBits( |
255 BrotliBitReader* const br, uint32_t n_bits) { | 249 BrotliBitReader* const br, uint32_t n_bits) { |
256 BrotliFillBitWindow(br, n_bits); | 250 BrotliFillBitWindow(br, n_bits); |
257 return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); | 251 return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); |
258 } | 252 } |
259 | 253 |
260 /* Tries to peek the specified amount of bits. Returns 0, if there is not | 254 /* Tries to peek the specified amount of bits. Returns 0, if there is not |
261 enough input. */ | 255 enough input. */ |
262 static BROTLI_INLINE int BrotliSafeGetBits( | 256 static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits( |
263 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { | 257 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { |
264 while (BrotliGetAvailableBits(br) < n_bits) { | 258 while (BrotliGetAvailableBits(br) < n_bits) { |
265 if (!BrotliPullByte(br)) { | 259 if (!BrotliPullByte(br)) { |
266 return 0; | 260 return BROTLI_FALSE; |
267 } | 261 } |
268 } | 262 } |
269 *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); | 263 *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); |
270 return 1; | 264 return BROTLI_TRUE; |
271 } | 265 } |
272 | 266 |
273 /* Advances the bit pos by n_bits. */ | 267 /* Advances the bit pos by n_bits. */ |
274 static BROTLI_INLINE void BrotliDropBits( | 268 static BROTLI_INLINE void BrotliDropBits( |
275 BrotliBitReader* const br, uint32_t n_bits) { | 269 BrotliBitReader* const br, uint32_t n_bits) { |
276 br->bit_pos_ += n_bits; | 270 br->bit_pos_ += n_bits; |
277 } | 271 } |
278 | 272 |
279 static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) { | 273 static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) { |
280 uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3; | 274 uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3; |
281 uint32_t unused_bits = unused_bytes << 3; | 275 uint32_t unused_bits = unused_bytes << 3; |
282 br->avail_in += unused_bytes; | 276 br->avail_in += unused_bytes; |
283 br->next_in -= unused_bytes; | 277 br->next_in -= unused_bytes; |
284 if (unused_bits == sizeof(br->val_) << 3) { | 278 if (unused_bits == sizeof(br->val_) << 3) { |
285 br->val_ = 0; | 279 br->val_ = 0; |
286 } else { | 280 } else { |
287 br->val_ <<= unused_bits; | 281 br->val_ <<= unused_bits; |
288 } | 282 } |
289 br->bit_pos_ += unused_bits; | 283 br->bit_pos_ += unused_bits; |
290 } | 284 } |
291 | 285 |
292 /* Reads the specified number of bits from br and advances the bit pos. | 286 /* Reads the specified number of bits from |br| and advances the bit pos. |
293 Precondition: accumulator MUST contain at least n_bits. */ | 287 Precondition: accumulator MUST contain at least n_bits. */ |
294 static BROTLI_INLINE void BrotliTakeBits( | 288 static BROTLI_INLINE void BrotliTakeBits( |
295 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { | 289 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { |
296 *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); | 290 *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits); |
297 BROTLI_LOG(("[BrotliReadBits] %d %d %d val: %6x\n", | 291 BROTLI_LOG(("[BrotliReadBits] %d %d %d val: %6x\n", |
298 (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val)); | 292 (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val)); |
299 BrotliDropBits(br, n_bits); | 293 BrotliDropBits(br, n_bits); |
300 } | 294 } |
301 | 295 |
302 /* Reads the specified number of bits from br and advances the bit pos. | 296 /* Reads the specified number of bits from |br| and advances the bit pos. |
303 Assumes that there is enough input to perform BrotliFillBitWindow. */ | 297 Assumes that there is enough input to perform BrotliFillBitWindow. */ |
304 static BROTLI_INLINE uint32_t BrotliReadBits( | 298 static BROTLI_INLINE uint32_t BrotliReadBits( |
305 BrotliBitReader* const br, uint32_t n_bits) { | 299 BrotliBitReader* const br, uint32_t n_bits) { |
306 if (BROTLI_64_BITS || (n_bits <= 16)) { | 300 if (BROTLI_64_BITS || (n_bits <= 16)) { |
307 uint32_t val; | 301 uint32_t val; |
308 BrotliFillBitWindow(br, n_bits); | 302 BrotliFillBitWindow(br, n_bits); |
309 BrotliTakeBits(br, n_bits, &val); | 303 BrotliTakeBits(br, n_bits, &val); |
310 return val; | 304 return val; |
311 } else { | 305 } else { |
312 uint32_t low_val; | 306 uint32_t low_val; |
313 uint32_t high_val; | 307 uint32_t high_val; |
314 BrotliFillBitWindow(br, 16); | 308 BrotliFillBitWindow(br, 16); |
315 BrotliTakeBits(br, 16, &low_val); | 309 BrotliTakeBits(br, 16, &low_val); |
316 BrotliFillBitWindow(br, 8); | 310 BrotliFillBitWindow(br, 8); |
317 BrotliTakeBits(br, n_bits - 16, &high_val); | 311 BrotliTakeBits(br, n_bits - 16, &high_val); |
318 return low_val | (high_val << 16); | 312 return low_val | (high_val << 16); |
319 } | 313 } |
320 } | 314 } |
321 | 315 |
322 /* Tries to read the specified amount of bits. Returns 0, if there is not | 316 /* Tries to read the specified amount of bits. Returns 0, if there is not |
323 enough input. n_bits MUST be positive. */ | 317 enough input. n_bits MUST be positive. */ |
324 static BROTLI_INLINE int BrotliSafeReadBits( | 318 static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits( |
325 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { | 319 BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) { |
326 while (BrotliGetAvailableBits(br) < n_bits) { | 320 while (BrotliGetAvailableBits(br) < n_bits) { |
327 if (!BrotliPullByte(br)) { | 321 if (!BrotliPullByte(br)) { |
328 return 0; | 322 return BROTLI_FALSE; |
329 } | 323 } |
330 } | 324 } |
331 BrotliTakeBits(br, n_bits, val); | 325 BrotliTakeBits(br, n_bits, val); |
332 return 1; | 326 return BROTLI_TRUE; |
333 } | 327 } |
334 | 328 |
335 /* Advances the bit reader position to the next byte boundary and verifies | 329 /* Advances the bit reader position to the next byte boundary and verifies |
336 that any skipped bits are set to zero. */ | 330 that any skipped bits are set to zero. */ |
337 static BROTLI_INLINE int BrotliJumpToByteBoundary(BrotliBitReader* br) { | 331 static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) { |
338 uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7; | 332 uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7; |
339 uint32_t pad_bits = 0; | 333 uint32_t pad_bits = 0; |
340 if (pad_bits_count != 0) { | 334 if (pad_bits_count != 0) { |
341 BrotliTakeBits(br, pad_bits_count, &pad_bits); | 335 BrotliTakeBits(br, pad_bits_count, &pad_bits); |
342 } | 336 } |
343 return pad_bits == 0; | 337 return TO_BROTLI_BOOL(pad_bits == 0); |
344 } | |
345 | |
346 /* Peeks a byte at specified offset. | |
347 Precondition: bit reader is parked to a byte boundary. | |
348 Returns -1 if operation is not feasible. */ | |
349 static BROTLI_INLINE int BrotliPeekByte(BrotliBitReader* br, size_t offset) { | |
350 uint32_t available_bits = BrotliGetAvailableBits(br); | |
351 size_t bytes_left = available_bits >> 3; | |
352 BROTLI_DCHECK((available_bits & 7) == 0); | |
353 if (offset < bytes_left) { | |
354 return (BrotliGetBitsUnmasked(br) >> (unsigned)(offset << 3)) & 0xFF; | |
355 } | |
356 offset -= bytes_left; | |
357 if (offset < br->avail_in) { | |
358 return br->next_in[offset]; | |
359 } | |
360 return -1; | |
361 } | 338 } |
362 | 339 |
363 /* Copies remaining input bytes stored in the bit reader to the output. Value | 340 /* Copies remaining input bytes stored in the bit reader to the output. Value |
364 num may not be larger than BrotliGetRemainingBytes. The bit reader must be | 341 num may not be larger than BrotliGetRemainingBytes. The bit reader must be |
365 warmed up again after this. */ | 342 warmed up again after this. */ |
366 static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest, | 343 static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest, |
367 BrotliBitReader* br, size_t num) { | 344 BrotliBitReader* br, size_t num) { |
368 while (BrotliGetAvailableBits(br) >= 8 && num > 0) { | 345 while (BrotliGetAvailableBits(br) >= 8 && num > 0) { |
369 *dest = (uint8_t)BrotliGetBitsUnmasked(br); | 346 *dest = (uint8_t)BrotliGetBitsUnmasked(br); |
370 BrotliDropBits(br, 8); | 347 BrotliDropBits(br, 8); |
371 ++dest; | 348 ++dest; |
372 --num; | 349 --num; |
373 } | 350 } |
374 memcpy(dest, br->next_in, num); | 351 memcpy(dest, br->next_in, num); |
375 br->avail_in -= num; | 352 br->avail_in -= num; |
376 br->next_in += num; | 353 br->next_in += num; |
377 } | 354 } |
378 | 355 |
379 #if defined(__cplusplus) || defined(c_plusplus) | 356 #if defined(__cplusplus) || defined(c_plusplus) |
380 } /* extern "C" */ | 357 } /* extern "C" */ |
381 #endif | 358 #endif |
382 | 359 |
383 #endif /* BROTLI_DEC_BIT_READER_H_ */ | 360 #endif /* BROTLI_DEC_BIT_READER_H_ */ |
OLD | NEW |