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