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