Coverage Report

Created: 2025-08-18 06:39

/src/brotli/c/dec/bit_reader.h
Line
Count
Source (jump to first uncovered line)
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
140k
#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
20
21
/* 162 bits + 7 bytes */
22
6.97M
#define BROTLI_FAST_INPUT_SLACK 28
23
24
BROTLI_INTERNAL extern const brotli_reg_t kBrotliBitMask[33];
25
26
1.96G
static BROTLI_INLINE brotli_reg_t BitMask(brotli_reg_t n) {
27
1.96G
  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
1.70G
    return ~(~((brotli_reg_t)0) << n);
31
1.70G
  } else {
32
256M
    return kBrotliBitMask[n];
33
256M
  }
34
1.96G
}
decode.c:BitMask
Line
Count
Source
26
1.96G
static BROTLI_INLINE brotli_reg_t BitMask(brotli_reg_t n) {
27
1.96G
  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
1.70G
    return ~(~((brotli_reg_t)0) << n);
31
1.70G
  } else {
32
256M
    return kBrotliBitMask[n];
33
256M
  }
34
1.96G
}
Unexecuted instantiation: state.c:BitMask
Unexecuted instantiation: bit_reader.c:BitMask
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
647M
BrotliBitReaderGetAvailIn(BrotliBitReader* const br) {
69
647M
  return (size_t)(br->last_in - br->next_in);
70
647M
}
decode.c:BrotliBitReaderGetAvailIn
Line
Count
Source
68
647M
BrotliBitReaderGetAvailIn(BrotliBitReader* const br) {
69
647M
  return (size_t)(br->last_in - br->next_in);
70
647M
}
Unexecuted instantiation: state.c:BrotliBitReaderGetAvailIn
Unexecuted instantiation: bit_reader.c:BrotliBitReaderGetAvailIn
71
72
static BROTLI_INLINE void BrotliBitReaderSaveState(
73
641M
    BrotliBitReader* const from, BrotliBitReaderState* to) {
74
641M
  to->val_ = from->val_;
75
641M
  to->bit_pos_ = from->bit_pos_;
76
641M
  to->next_in = from->next_in;
77
641M
  to->avail_in = BrotliBitReaderGetAvailIn(from);
78
641M
}
decode.c:BrotliBitReaderSaveState
Line
Count
Source
73
641M
    BrotliBitReader* const from, BrotliBitReaderState* to) {
74
641M
  to->val_ = from->val_;
75
641M
  to->bit_pos_ = from->bit_pos_;
76
641M
  to->next_in = from->next_in;
77
641M
  to->avail_in = BrotliBitReaderGetAvailIn(from);
78
641M
}
Unexecuted instantiation: state.c:BrotliBitReaderSaveState
Unexecuted instantiation: bit_reader.c:BrotliBitReaderSaveState
79
80
static BROTLI_INLINE void BrotliBitReaderSetInput(
81
5.72M
    BrotliBitReader* const br, const uint8_t* next_in, size_t avail_in) {
82
5.72M
  br->next_in = next_in;
83
5.72M
  br->last_in = (avail_in == 0) ? next_in : (next_in + avail_in);
84
5.72M
  if (avail_in + 1 > BROTLI_FAST_INPUT_SLACK) {
85
1.24M
    br->guard_in = next_in + (avail_in + 1 - BROTLI_FAST_INPUT_SLACK);
86
4.48M
  } else {
87
4.48M
    br->guard_in = next_in;
88
4.48M
  }
89
5.72M
}
decode.c:BrotliBitReaderSetInput
Line
Count
Source
81
5.72M
    BrotliBitReader* const br, const uint8_t* next_in, size_t avail_in) {
82
5.72M
  br->next_in = next_in;
83
5.72M
  br->last_in = (avail_in == 0) ? next_in : (next_in + avail_in);
84
5.72M
  if (avail_in + 1 > BROTLI_FAST_INPUT_SLACK) {
85
1.24M
    br->guard_in = next_in + (avail_in + 1 - BROTLI_FAST_INPUT_SLACK);
86
4.48M
  } else {
87
4.48M
    br->guard_in = next_in;
88
4.48M
  }
89
5.72M
}
Unexecuted instantiation: state.c:BrotliBitReaderSetInput
Unexecuted instantiation: bit_reader.c:BrotliBitReaderSetInput
90
91
static BROTLI_INLINE void BrotliBitReaderRestoreState(
92
90.0k
    BrotliBitReader* const to, BrotliBitReaderState* from) {
93
90.0k
  to->val_ = from->val_;
94
90.0k
  to->bit_pos_ = from->bit_pos_;
95
90.0k
  to->next_in = from->next_in;
96
90.0k
  BrotliBitReaderSetInput(to, from->next_in, from->avail_in);
97
90.0k
}
decode.c:BrotliBitReaderRestoreState
Line
Count
Source
92
90.0k
    BrotliBitReader* const to, BrotliBitReaderState* from) {
93
90.0k
  to->val_ = from->val_;
94
90.0k
  to->bit_pos_ = from->bit_pos_;
95
90.0k
  to->next_in = from->next_in;
96
90.0k
  BrotliBitReaderSetInput(to, from->next_in, from->avail_in);
97
90.0k
}
Unexecuted instantiation: state.c:BrotliBitReaderRestoreState
Unexecuted instantiation: bit_reader.c:BrotliBitReaderRestoreState
98
99
static BROTLI_INLINE brotli_reg_t BrotliGetAvailableBits(
100
3.46G
    const BrotliBitReader* br) {
101
3.46G
  return br->bit_pos_;
102
3.46G
}
decode.c:BrotliGetAvailableBits
Line
Count
Source
100
3.46G
    const BrotliBitReader* br) {
101
3.46G
  return br->bit_pos_;
102
3.46G
}
Unexecuted instantiation: state.c:BrotliGetAvailableBits
bit_reader.c:BrotliGetAvailableBits
Line
Count
Source
100
216k
    const BrotliBitReader* br) {
101
216k
  return br->bit_pos_;
102
216k
}
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
94.4k
static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
108
94.4k
  static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;
109
94.4k
  size_t avail_in = BrotliBitReaderGetAvailIn(br);
110
94.4k
  if (avail_in > kCap) return kCap;
111
94.4k
  return avail_in + (BrotliGetAvailableBits(br) >> 3);
112
94.4k
}
decode.c:BrotliGetRemainingBytes
Line
Count
Source
107
94.4k
static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
108
94.4k
  static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;
109
94.4k
  size_t avail_in = BrotliBitReaderGetAvailIn(br);
110
94.4k
  if (avail_in > kCap) return kCap;
111
94.4k
  return avail_in + (BrotliGetAvailableBits(br) >> 3);
112
94.4k
}
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
790M
    BrotliBitReader* const br) {
118
790M
  return TO_BROTLI_BOOL(br->next_in < br->guard_in);
119
790M
}
decode.c:BrotliCheckInputAmount
Line
Count
Source
117
790M
    BrotliBitReader* const br) {
118
790M
  return TO_BROTLI_BOOL(br->next_in < br->guard_in);
119
790M
}
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
2.89M
                                                          brotli_reg_t offset) {
126
2.89M
  BROTLI_DCHECK(
127
2.89M
      !((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count)));
128
2.89M
  (void)count;
129
2.89M
  return val | (new_bits << offset);
130
2.89M
}
decode.c:BrotliBitReaderLoadBits
Line
Count
Source
125
2.87M
                                                          brotli_reg_t offset) {
126
2.87M
  BROTLI_DCHECK(
127
2.87M
      !((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count)));
128
2.87M
  (void)count;
129
2.87M
  return val | (new_bits << offset);
130
2.87M
}
Unexecuted instantiation: state.c:BrotliBitReaderLoadBits
bit_reader.c:BrotliBitReaderLoadBits
Line
Count
Source
125
15.3k
                                                          brotli_reg_t offset) {
126
15.3k
  BROTLI_DCHECK(
127
15.3k
      !((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count)));
128
15.3k
  (void)count;
129
15.3k
  return val | (new_bits << offset);
130
15.3k
}
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
1.02G
    BrotliBitReader* const br, brotli_reg_t n_bits) {
138
1.02G
#if (BROTLI_64_BITS)
139
1.02G
  if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
140
1.02G
      (n_bits <= 8)) {
141
530M
    brotli_reg_t bit_pos = br->bit_pos_;
142
530M
    if (bit_pos <= 8) {
143
164k
      br->val_ = BrotliBitReaderLoadBits(br->val_,
144
164k
          BROTLI_UNALIGNED_LOAD64LE(br->next_in), 56, bit_pos);
145
164k
      br->bit_pos_ = bit_pos + 56;
146
164k
      br->next_in += 7;
147
164k
    }
148
530M
  } else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
149
492M
             (n_bits <= 16)) {
150
296M
    brotli_reg_t bit_pos = br->bit_pos_;
151
296M
    if (bit_pos <= 16) {
152
184k
      br->val_ = BrotliBitReaderLoadBits(br->val_,
153
184k
          BROTLI_UNALIGNED_LOAD64LE(br->next_in), 48, bit_pos);
154
184k
      br->bit_pos_ = bit_pos + 48;
155
184k
      br->next_in += 6;
156
184k
    }
157
296M
  } else {
158
195M
    brotli_reg_t bit_pos = br->bit_pos_;
159
195M
    if (bit_pos <= 32) {
160
140k
      br->val_ = BrotliBitReaderLoadBits(br->val_,
161
140k
          (uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in), 32, bit_pos);
162
140k
      br->bit_pos_ = bit_pos + 32;
163
140k
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
164
140k
    }
165
195M
  }
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
1.02G
}
decode.c:BrotliFillBitWindow
Line
Count
Source
137
1.02G
    BrotliBitReader* const br, brotli_reg_t n_bits) {
138
1.02G
#if (BROTLI_64_BITS)
139
1.02G
  if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
140
1.02G
      (n_bits <= 8)) {
141
530M
    brotli_reg_t bit_pos = br->bit_pos_;
142
530M
    if (bit_pos <= 8) {
143
164k
      br->val_ = BrotliBitReaderLoadBits(br->val_,
144
164k
          BROTLI_UNALIGNED_LOAD64LE(br->next_in), 56, bit_pos);
145
164k
      br->bit_pos_ = bit_pos + 56;
146
164k
      br->next_in += 7;
147
164k
    }
148
530M
  } else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&
149
492M
             (n_bits <= 16)) {
150
296M
    brotli_reg_t bit_pos = br->bit_pos_;
151
296M
    if (bit_pos <= 16) {
152
184k
      br->val_ = BrotliBitReaderLoadBits(br->val_,
153
184k
          BROTLI_UNALIGNED_LOAD64LE(br->next_in), 48, bit_pos);
154
184k
      br->bit_pos_ = bit_pos + 48;
155
184k
      br->next_in += 6;
156
184k
    }
157
296M
  } else {
158
195M
    brotli_reg_t bit_pos = br->bit_pos_;
159
195M
    if (bit_pos <= 32) {
160
140k
      br->val_ = BrotliBitReaderLoadBits(br->val_,
161
140k
          (uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in), 32, bit_pos);
162
140k
      br->bit_pos_ = bit_pos + 32;
163
140k
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
164
140k
    }
165
195M
  }
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
1.02G
}
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
129k
static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
191
129k
  BrotliFillBitWindow(br, 17);
192
129k
}
decode.c:BrotliFillBitWindow16
Line
Count
Source
190
129k
static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
191
129k
  BrotliFillBitWindow(br, 17);
192
129k
}
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
1.14G
static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
197
1.14G
  if (br->next_in == br->last_in) {
198
1.13G
    return BROTLI_FALSE;
199
1.13G
  }
200
2.40M
  br->val_ = BrotliBitReaderLoadBits(br->val_,
201
2.40M
      (brotli_reg_t)*br->next_in, 8, br->bit_pos_);
202
2.40M
  br->bit_pos_ += 8;
203
2.40M
  ++br->next_in;
204
2.40M
  return BROTLI_TRUE;
205
1.14G
}
decode.c:BrotliPullByte
Line
Count
Source
196
1.14G
static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
197
1.14G
  if (br->next_in == br->last_in) {
198
1.13G
    return BROTLI_FALSE;
199
1.13G
  }
200
2.38M
  br->val_ = BrotliBitReaderLoadBits(br->val_,
201
2.38M
      (brotli_reg_t)*br->next_in, 8, br->bit_pos_);
202
2.38M
  br->bit_pos_ += 8;
203
2.38M
  ++br->next_in;
204
2.38M
  return BROTLI_TRUE;
205
1.14G
}
Unexecuted instantiation: state.c:BrotliPullByte
bit_reader.c:BrotliPullByte
Line
Count
Source
196
16.1k
static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
197
16.1k
  if (br->next_in == br->last_in) {
198
830
    return BROTLI_FALSE;
199
830
  }
200
15.3k
  br->val_ = BrotliBitReaderLoadBits(br->val_,
201
15.3k
      (brotli_reg_t)*br->next_in, 8, br->bit_pos_);
202
15.3k
  br->bit_pos_ += 8;
203
15.3k
  ++br->next_in;
204
15.3k
  return BROTLI_TRUE;
205
16.1k
}
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
3.21G
    BrotliBitReader* const br) {
211
3.21G
  return br->val_;
212
3.21G
}
decode.c:BrotliGetBitsUnmasked
Line
Count
Source
210
3.21G
    BrotliBitReader* const br) {
211
3.21G
  return br->val_;
212
3.21G
}
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
296M
    BrotliBitReader* const br) {
218
296M
  BrotliFillBitWindow(br, 16);
219
296M
  return (brotli_reg_t)BrotliGetBitsUnmasked(br);
220
296M
}
decode.c:BrotliGet16BitsUnmasked
Line
Count
Source
217
296M
    BrotliBitReader* const br) {
218
296M
  BrotliFillBitWindow(br, 16);
219
296M
  return (brotli_reg_t)BrotliGetBitsUnmasked(br);
220
296M
}
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
530M
    BrotliBitReader* const br, brotli_reg_t n_bits) {
226
530M
  BrotliFillBitWindow(br, n_bits);
227
530M
  return BrotliGetBitsUnmasked(br) & BitMask(n_bits);
228
530M
}
decode.c:BrotliGetBits
Line
Count
Source
225
530M
    BrotliBitReader* const br, brotli_reg_t n_bits) {
226
530M
  BrotliFillBitWindow(br, n_bits);
227
530M
  return BrotliGetBitsUnmasked(br) & BitMask(n_bits);
228
530M
}
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
2.31G
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
234
2.32G
  while (BrotliGetAvailableBits(br) < n_bits) {
235
1.14G
    if (!BrotliPullByte(br)) {
236
1.13G
      return BROTLI_FALSE;
237
1.13G
    }
238
1.14G
  }
239
1.17G
  *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
240
1.17G
  return BROTLI_TRUE;
241
2.31G
}
decode.c:BrotliSafeGetBits
Line
Count
Source
233
2.31G
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
234
2.32G
  while (BrotliGetAvailableBits(br) < n_bits) {
235
1.14G
    if (!BrotliPullByte(br)) {
236
1.13G
      return BROTLI_FALSE;
237
1.13G
    }
238
1.14G
  }
239
1.17G
  *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
240
1.17G
  return BROTLI_TRUE;
241
2.31G
}
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
3.14G
    BrotliBitReader* const br, brotli_reg_t n_bits) {
246
3.14G
  br->bit_pos_ -= n_bits;
247
3.14G
  br->val_ >>= n_bits;
248
3.14G
}
decode.c:BrotliDropBits
Line
Count
Source
245
3.14G
    BrotliBitReader* const br, brotli_reg_t n_bits) {
246
3.14G
  br->bit_pos_ -= n_bits;
247
3.14G
  br->val_ >>= n_bits;
248
3.14G
}
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
3.51M
static BROTLI_INLINE void BrotliBitReaderNormalize(BrotliBitReader* br) {
254
  /* Actually, it is enough to normalize when br->bit_pos_ == 0 */
255
3.51M
  if (br->bit_pos_ < (sizeof(brotli_reg_t) << 3u)) {
256
3.51M
    br->val_ &= (((brotli_reg_t)1) << br->bit_pos_) - 1;
257
3.51M
  }
258
3.51M
}
decode.c:BrotliBitReaderNormalize
Line
Count
Source
253
3.51M
static BROTLI_INLINE void BrotliBitReaderNormalize(BrotliBitReader* br) {
254
  /* Actually, it is enough to normalize when br->bit_pos_ == 0 */
255
3.51M
  if (br->bit_pos_ < (sizeof(brotli_reg_t) << 3u)) {
256
3.51M
    br->val_ &= (((brotli_reg_t)1) << br->bit_pos_) - 1;
257
3.51M
  }
258
3.51M
}
Unexecuted instantiation: state.c:BrotliBitReaderNormalize
Unexecuted instantiation: bit_reader.c:BrotliBitReaderNormalize
259
260
3.42M
static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
261
3.42M
  brotli_reg_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
262
3.42M
  brotli_reg_t unused_bits = unused_bytes << 3;
263
3.42M
  br->next_in =
264
3.42M
      (unused_bytes == 0) ? br->next_in : (br->next_in - unused_bytes);
265
3.42M
  br->bit_pos_ -= unused_bits;
266
3.42M
  BrotliBitReaderNormalize(br);
267
3.42M
}
decode.c:BrotliBitReaderUnload
Line
Count
Source
260
3.42M
static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
261
3.42M
  brotli_reg_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
262
3.42M
  brotli_reg_t unused_bits = unused_bytes << 3;
263
3.42M
  br->next_in =
264
3.42M
      (unused_bytes == 0) ? br->next_in : (br->next_in - unused_bytes);
265
3.42M
  br->bit_pos_ -= unused_bits;
266
3.42M
  BrotliBitReaderNormalize(br);
267
3.42M
}
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
196M
                                         brotli_reg_t* val) {
274
196M
  *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
275
196M
  BROTLI_LOG(("[BrotliTakeBits]  %d %d %d val: %6x\n",
276
196M
              (int)BrotliBitReaderGetAvailIn(br), (int)br->bit_pos_,
277
196M
              (int)n_bits, (int)*val));
278
196M
  BrotliDropBits(br, n_bits);
279
196M
}
decode.c:BrotliTakeBits
Line
Count
Source
273
196M
                                         brotli_reg_t* val) {
274
196M
  *val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);
275
196M
  BROTLI_LOG(("[BrotliTakeBits]  %d %d %d val: %6x\n",
276
196M
              (int)BrotliBitReaderGetAvailIn(br), (int)br->bit_pos_,
277
196M
              (int)n_bits, (int)*val));
278
196M
  BrotliDropBits(br, n_bits);
279
196M
}
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
154M
    BrotliBitReader* const br, brotli_reg_t n_bits) {
286
154M
  BROTLI_DCHECK(n_bits <= 24);
287
154M
  if (BROTLI_64_BITS || (n_bits <= 16)) {
288
154M
    brotli_reg_t val;
289
154M
    BrotliFillBitWindow(br, n_bits);
290
154M
    BrotliTakeBits(br, n_bits, &val);
291
154M
    return val;
292
154M
  } 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
154M
}
decode.c:BrotliReadBits24
Line
Count
Source
285
154M
    BrotliBitReader* const br, brotli_reg_t n_bits) {
286
154M
  BROTLI_DCHECK(n_bits <= 24);
287
154M
  if (BROTLI_64_BITS || (n_bits <= 16)) {
288
154M
    brotli_reg_t val;
289
154M
    BrotliFillBitWindow(br, n_bits);
290
154M
    BrotliTakeBits(br, n_bits, &val);
291
154M
    return val;
292
154M
  } 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
154M
}
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
41.6M
    BrotliBitReader* const br, brotli_reg_t n_bits) {
306
41.6M
  BROTLI_DCHECK(n_bits <= 32);
307
41.6M
  if (BROTLI_64_BITS || (n_bits <= 16)) {
308
41.6M
    brotli_reg_t val;
309
41.6M
    BrotliFillBitWindow(br, n_bits);
310
41.6M
    BrotliTakeBits(br, n_bits, &val);
311
41.6M
    return val;
312
41.6M
  } 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
41.6M
}
decode.c:BrotliReadBits32
Line
Count
Source
305
41.6M
    BrotliBitReader* const br, brotli_reg_t n_bits) {
306
41.6M
  BROTLI_DCHECK(n_bits <= 32);
307
41.6M
  if (BROTLI_64_BITS || (n_bits <= 16)) {
308
41.6M
    brotli_reg_t val;
309
41.6M
    BrotliFillBitWindow(br, n_bits);
310
41.6M
    BrotliTakeBits(br, n_bits, &val);
311
41.6M
    return val;
312
41.6M
  } 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
41.6M
}
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
846k
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
328
846k
  BROTLI_DCHECK(n_bits <= 24);
329
1.00M
  while (BrotliGetAvailableBits(br) < n_bits) {
330
298k
    if (!BrotliPullByte(br)) {
331
144k
      return BROTLI_FALSE;
332
144k
    }
333
298k
  }
334
701k
  BrotliTakeBits(br, n_bits, val);
335
701k
  return BROTLI_TRUE;
336
846k
}
decode.c:BrotliSafeReadBits
Line
Count
Source
327
846k
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
328
846k
  BROTLI_DCHECK(n_bits <= 24);
329
1.00M
  while (BrotliGetAvailableBits(br) < n_bits) {
330
298k
    if (!BrotliPullByte(br)) {
331
144k
      return BROTLI_FALSE;
332
144k
    }
333
298k
  }
334
701k
  BrotliTakeBits(br, n_bits, val);
335
701k
  return BROTLI_TRUE;
336
846k
}
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
27.6k
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
341
27.6k
  BROTLI_DCHECK(n_bits <= 32);
342
27.6k
  if (BROTLI_64_BITS || (n_bits <= 24)) {
343
28.4k
    while (BrotliGetAvailableBits(br) < n_bits) {
344
8.13k
      if (!BrotliPullByte(br)) {
345
7.33k
        return BROTLI_FALSE;
346
7.33k
      }
347
8.13k
    }
348
20.2k
    BrotliTakeBits(br, n_bits, val);
349
20.2k
    return BROTLI_TRUE;
350
27.6k
  } else {
351
0
    return BrotliSafeReadBits32Slow(br, n_bits, val);
352
0
  }
353
27.6k
}
decode.c:BrotliSafeReadBits32
Line
Count
Source
340
27.6k
    BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {
341
27.6k
  BROTLI_DCHECK(n_bits <= 32);
342
27.6k
  if (BROTLI_64_BITS || (n_bits <= 24)) {
343
28.4k
    while (BrotliGetAvailableBits(br) < n_bits) {
344
8.13k
      if (!BrotliPullByte(br)) {
345
7.33k
        return BROTLI_FALSE;
346
7.33k
      }
347
8.13k
    }
348
20.2k
    BrotliTakeBits(br, n_bits, val);
349
20.2k
    return BROTLI_TRUE;
350
27.6k
  } else {
351
0
    return BrotliSafeReadBits32Slow(br, n_bits, val);
352
0
  }
353
27.6k
}
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
6.94k
static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
358
6.94k
  brotli_reg_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
359
6.94k
  brotli_reg_t pad_bits = 0;
360
6.94k
  if (pad_bits_count != 0) {
361
6.11k
    BrotliTakeBits(br, pad_bits_count, &pad_bits);
362
6.11k
  }
363
6.94k
  BrotliBitReaderNormalize(br);
364
6.94k
  return TO_BROTLI_BOOL(pad_bits == 0);
365
6.94k
}
decode.c:BrotliJumpToByteBoundary
Line
Count
Source
357
6.94k
static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
358
6.94k
  brotli_reg_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
359
6.94k
  brotli_reg_t pad_bits = 0;
360
6.94k
  if (pad_bits_count != 0) {
361
6.11k
    BrotliTakeBits(br, pad_bits_count, &pad_bits);
362
6.11k
  }
363
6.94k
  BrotliBitReaderNormalize(br);
364
6.94k
  return TO_BROTLI_BOOL(pad_bits == 0);
365
6.94k
}
Unexecuted instantiation: state.c:BrotliJumpToByteBoundary
Unexecuted instantiation: bit_reader.c:BrotliJumpToByteBoundary
366
367
92.3k
static BROTLI_INLINE void BrotliDropBytes(BrotliBitReader* br, size_t num) {
368
  /* Check detour is legal: accumulator must to be empty. */
369
92.3k
  BROTLI_DCHECK(br->bit_pos_ == 0);
370
92.3k
  BROTLI_DCHECK(br->val_ == 0);
371
92.3k
  br->next_in += num;
372
92.3k
}
decode.c:BrotliDropBytes
Line
Count
Source
367
92.3k
static BROTLI_INLINE void BrotliDropBytes(BrotliBitReader* br, size_t num) {
368
  /* Check detour is legal: accumulator must to be empty. */
369
92.3k
  BROTLI_DCHECK(br->bit_pos_ == 0);
370
92.3k
  BROTLI_DCHECK(br->val_ == 0);
371
92.3k
  br->next_in += num;
372
92.3k
}
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
90.4k
                                          BrotliBitReader* br, size_t num) {
379
94.9k
  while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
380
4.51k
    *dest = (uint8_t)BrotliGetBitsUnmasked(br);
381
4.51k
    BrotliDropBits(br, 8);
382
4.51k
    ++dest;
383
4.51k
    --num;
384
4.51k
  }
385
90.4k
  BrotliBitReaderNormalize(br);
386
90.4k
  if (num > 0) {
387
87.9k
    memcpy(dest, br->next_in, num);
388
87.9k
    BrotliDropBytes(br, num);
389
87.9k
  }
390
90.4k
}
decode.c:BrotliCopyBytes
Line
Count
Source
378
90.4k
                                          BrotliBitReader* br, size_t num) {
379
94.9k
  while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
380
4.51k
    *dest = (uint8_t)BrotliGetBitsUnmasked(br);
381
4.51k
    BrotliDropBits(br, 8);
382
4.51k
    ++dest;
383
4.51k
    --num;
384
4.51k
  }
385
90.4k
  BrotliBitReaderNormalize(br);
386
90.4k
  if (num > 0) {
387
87.9k
    memcpy(dest, br->next_in, num);
388
87.9k
    BrotliDropBytes(br, num);
389
87.9k
  }
390
90.4k
}
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_ */