Coverage Report

Created: 2025-07-12 06:50

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