Coverage Report

Created: 2025-08-29 06:07

/src/woff2/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 "../common/platform.h"
15
#include <brotli/types.h>
16
17
#if defined(__cplusplus) || defined(c_plusplus)
18
extern "C" {
19
#endif
20
21
20.7M
#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
22
23
static const uint32_t kBitMask[33] = {  0x00000000,
24
    0x00000001, 0x00000003, 0x00000007, 0x0000000F,
25
    0x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,
26
    0x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,
27
    0x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,
28
    0x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,
29
    0x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,
30
    0x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,
31
    0x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF
32
};
33
34
2.05G
static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
35
2.05G
  if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
36
    /* Masking with this expression turns to a single
37
       "Unsigned Bit Field Extract" UBFX instruction on ARM. */
38
1.98G
    return ~((0xFFFFFFFFu) << n);
39
1.98G
  } else {
40
72.2M
    return kBitMask[n];
41
72.2M
  }
42
2.05G
}
decode.c:BitMask
Line
Count
Source
34
2.05G
static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
35
2.05G
  if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
36
    /* Masking with this expression turns to a single
37
       "Unsigned Bit Field Extract" UBFX instruction on ARM. */
38
1.98G
    return ~((0xFFFFFFFFu) << n);
39
1.98G
  } else {
40
72.2M
    return kBitMask[n];
41
72.2M
  }
42
2.05G
}
Unexecuted instantiation: state.c:BitMask
Unexecuted instantiation: bit_reader.c:BitMask
43
44
typedef struct {
45
  brotli_reg_t val_;       /* pre-fetched bits */
46
  uint32_t bit_pos_;       /* current bit-reading position in val_ */
47
  const uint8_t* next_in;  /* the byte we're reading from */
48
  size_t avail_in;
49
} BrotliBitReader;
50
51
typedef struct {
52
  brotli_reg_t val_;
53
  uint32_t bit_pos_;
54
  const uint8_t* next_in;
55
  size_t avail_in;
56
} BrotliBitReaderState;
57
58
/* Initializes the BrotliBitReader fields. */
59
BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br);
60
61
/* Ensures that accumulator is not empty.
62
   May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
63
   Returns BROTLI_FALSE if data is required but there is no input available.
64
   For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
65
   reading. */
66
BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br);
67
68
static BROTLI_INLINE void BrotliBitReaderSaveState(
69
201M
    BrotliBitReader* const from, BrotliBitReaderState* to) {
70
201M
  to->val_ = from->val_;
71
201M
  to->bit_pos_ = from->bit_pos_;
72
201M
  to->next_in = from->next_in;
73
201M
  to->avail_in = from->avail_in;
74
201M
}
decode.c:BrotliBitReaderSaveState
Line
Count
Source
69
201M
    BrotliBitReader* const from, BrotliBitReaderState* to) {
70
201M
  to->val_ = from->val_;
71
201M
  to->bit_pos_ = from->bit_pos_;
72
201M
  to->next_in = from->next_in;
73
201M
  to->avail_in = from->avail_in;
74
201M
}
Unexecuted instantiation: state.c:BrotliBitReaderSaveState
Unexecuted instantiation: bit_reader.c:BrotliBitReaderSaveState
75
76
static BROTLI_INLINE void BrotliBitReaderRestoreState(
77
1.18k
    BrotliBitReader* const to, BrotliBitReaderState* from) {
78
1.18k
  to->val_ = from->val_;
79
1.18k
  to->bit_pos_ = from->bit_pos_;
80
1.18k
  to->next_in = from->next_in;
81
1.18k
  to->avail_in = from->avail_in;
82
1.18k
}
decode.c:BrotliBitReaderRestoreState
Line
Count
Source
77
1.18k
    BrotliBitReader* const to, BrotliBitReaderState* from) {
78
1.18k
  to->val_ = from->val_;
79
1.18k
  to->bit_pos_ = from->bit_pos_;
80
1.18k
  to->next_in = from->next_in;
81
1.18k
  to->avail_in = from->avail_in;
82
1.18k
}
Unexecuted instantiation: state.c:BrotliBitReaderRestoreState
Unexecuted instantiation: bit_reader.c:BrotliBitReaderRestoreState
83
84
static BROTLI_INLINE uint32_t BrotliGetAvailableBits(
85
1.65G
    const BrotliBitReader* br) {
86
1.65G
  return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
87
1.65G
}
decode.c:BrotliGetAvailableBits
Line
Count
Source
85
1.65G
    const BrotliBitReader* br) {
86
1.65G
  return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
87
1.65G
}
Unexecuted instantiation: state.c:BrotliGetAvailableBits
bit_reader.c:BrotliGetAvailableBits
Line
Count
Source
85
91.3k
    const BrotliBitReader* br) {
86
91.3k
  return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
87
91.3k
}
88
89
/* Returns amount of unread bytes the bit reader still has buffered from the
90
   BrotliInput, including whole bytes in br->val_. */
91
10.1k
static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
92
10.1k
  return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
93
10.1k
}
decode.c:BrotliGetRemainingBytes
Line
Count
Source
91
10.1k
static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
92
10.1k
  return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
93
10.1k
}
Unexecuted instantiation: state.c:BrotliGetRemainingBytes
Unexecuted instantiation: bit_reader.c:BrotliGetRemainingBytes
94
95
/* Checks if there is at least |num| bytes left in the input ring-buffer
96
   (excluding the bits remaining in br->val_). */
97
static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(
98
807M
    BrotliBitReader* const br, size_t num) {
99
807M
  return TO_BROTLI_BOOL(br->avail_in >= num);
100
807M
}
decode.c:BrotliCheckInputAmount
Line
Count
Source
98
807M
    BrotliBitReader* const br, size_t num) {
99
807M
  return TO_BROTLI_BOOL(br->avail_in >= num);
100
807M
}
Unexecuted instantiation: state.c:BrotliCheckInputAmount
Unexecuted instantiation: bit_reader.c:BrotliCheckInputAmount
101
102
/* Guarantees that there are at least |n_bits| + 1 bits in accumulator.
103
   Precondition: accumulator contains at least 1 bit.
104
   |n_bits| should be in the range [1..24] for regular build. For portable
105
   non-64-bit little-endian build only 16 bits are safe to request. */
106
static BROTLI_INLINE void BrotliFillBitWindow(
107
909M
    BrotliBitReader* const br, uint32_t n_bits) {
108
909M
#if (BROTLI_64_BITS)
109
909M
  if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
110
697M
    if (br->bit_pos_ >= 56) {
111
28.8k
      br->val_ >>= 56;
112
28.8k
      br->bit_pos_ ^= 56;  /* here same as -= 56 because of the if condition */
113
28.8k
      br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8;
114
28.8k
      br->avail_in -= 7;
115
28.8k
      br->next_in += 7;
116
28.8k
    }
117
697M
  } else if (
118
212M
      !BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 16)) {
119
139M
    if (br->bit_pos_ >= 48) {
120
1.33M
      br->val_ >>= 48;
121
1.33M
      br->bit_pos_ ^= 48;  /* here same as -= 48 because of the if condition */
122
1.33M
      br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16;
123
1.33M
      br->avail_in -= 6;
124
1.33M
      br->next_in += 6;
125
1.33M
    }
126
139M
  } else {
127
72.7M
    if (br->bit_pos_ >= 32) {
128
8.72M
      br->val_ >>= 32;
129
8.72M
      br->bit_pos_ ^= 32;  /* here same as -= 32 because of the if condition */
130
8.72M
      br->val_ |= ((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32;
131
8.72M
      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
132
8.72M
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
133
8.72M
    }
134
72.7M
  }
135
#else
136
  if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
137
    if (br->bit_pos_ >= 24) {
138
      br->val_ >>= 24;
139
      br->bit_pos_ ^= 24;  /* here same as -= 24 because of the if condition */
140
      br->val_ |= BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8;
141
      br->avail_in -= 3;
142
      br->next_in += 3;
143
    }
144
  } else {
145
    if (br->bit_pos_ >= 16) {
146
      br->val_ >>= 16;
147
      br->bit_pos_ ^= 16;  /* here same as -= 16 because of the if condition */
148
      br->val_ |= ((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16;
149
      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
150
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
151
    }
152
  }
153
#endif
154
909M
}
decode.c:BrotliFillBitWindow
Line
Count
Source
107
909M
    BrotliBitReader* const br, uint32_t n_bits) {
108
909M
#if (BROTLI_64_BITS)
109
909M
  if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
110
697M
    if (br->bit_pos_ >= 56) {
111
28.8k
      br->val_ >>= 56;
112
28.8k
      br->bit_pos_ ^= 56;  /* here same as -= 56 because of the if condition */
113
28.8k
      br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8;
114
28.8k
      br->avail_in -= 7;
115
28.8k
      br->next_in += 7;
116
28.8k
    }
117
697M
  } else if (
118
212M
      !BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 16)) {
119
139M
    if (br->bit_pos_ >= 48) {
120
1.33M
      br->val_ >>= 48;
121
1.33M
      br->bit_pos_ ^= 48;  /* here same as -= 48 because of the if condition */
122
1.33M
      br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16;
123
1.33M
      br->avail_in -= 6;
124
1.33M
      br->next_in += 6;
125
1.33M
    }
126
139M
  } else {
127
72.7M
    if (br->bit_pos_ >= 32) {
128
8.72M
      br->val_ >>= 32;
129
8.72M
      br->bit_pos_ ^= 32;  /* here same as -= 32 because of the if condition */
130
8.72M
      br->val_ |= ((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32;
131
8.72M
      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
132
8.72M
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
133
8.72M
    }
134
72.7M
  }
135
#else
136
  if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
137
    if (br->bit_pos_ >= 24) {
138
      br->val_ >>= 24;
139
      br->bit_pos_ ^= 24;  /* here same as -= 24 because of the if condition */
140
      br->val_ |= BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8;
141
      br->avail_in -= 3;
142
      br->next_in += 3;
143
    }
144
  } else {
145
    if (br->bit_pos_ >= 16) {
146
      br->val_ >>= 16;
147
      br->bit_pos_ ^= 16;  /* here same as -= 16 because of the if condition */
148
      br->val_ |= ((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16;
149
      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
150
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
151
    }
152
  }
153
#endif
154
909M
}
Unexecuted instantiation: state.c:BrotliFillBitWindow
Unexecuted instantiation: bit_reader.c:BrotliFillBitWindow
155
156
/* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
157
   more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
158
3.26M
static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
159
3.26M
  BrotliFillBitWindow(br, 17);
160
3.26M
}
decode.c:BrotliFillBitWindow16
Line
Count
Source
158
3.26M
static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
159
3.26M
  BrotliFillBitWindow(br, 17);
160
3.26M
}
Unexecuted instantiation: state.c:BrotliFillBitWindow16
Unexecuted instantiation: bit_reader.c:BrotliFillBitWindow16
161
162
/* Tries to pull one byte of input to accumulator.
163
   Returns BROTLI_FALSE if there is no input available. */
164
185M
static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
165
185M
  if (br->avail_in == 0) {
166
182M
    return BROTLI_FALSE;
167
182M
  }
168
3.25M
  br->val_ >>= 8;
169
3.25M
#if (BROTLI_64_BITS)
170
3.25M
  br->val_ |= ((uint64_t)*br->next_in) << 56;
171
#else
172
  br->val_ |= ((uint32_t)*br->next_in) << 24;
173
#endif
174
3.25M
  br->bit_pos_ -= 8;
175
3.25M
  --br->avail_in;
176
3.25M
  ++br->next_in;
177
3.25M
  return BROTLI_TRUE;
178
185M
}
decode.c:BrotliPullByte
Line
Count
Source
164
185M
static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
165
185M
  if (br->avail_in == 0) {
166
182M
    return BROTLI_FALSE;
167
182M
  }
168
3.22M
  br->val_ >>= 8;
169
3.22M
#if (BROTLI_64_BITS)
170
3.22M
  br->val_ |= ((uint64_t)*br->next_in) << 56;
171
#else
172
  br->val_ |= ((uint32_t)*br->next_in) << 24;
173
#endif
174
3.22M
  br->bit_pos_ -= 8;
175
3.22M
  --br->avail_in;
176
3.22M
  ++br->next_in;
177
3.22M
  return BROTLI_TRUE;
178
185M
}
Unexecuted instantiation: state.c:BrotliPullByte
bit_reader.c:BrotliPullByte
Line
Count
Source
164
24.3k
static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
165
24.3k
  if (br->avail_in == 0) {
166
2.44k
    return BROTLI_FALSE;
167
2.44k
  }
168
21.9k
  br->val_ >>= 8;
169
21.9k
#if (BROTLI_64_BITS)
170
21.9k
  br->val_ |= ((uint64_t)*br->next_in) << 56;
171
#else
172
  br->val_ |= ((uint32_t)*br->next_in) << 24;
173
#endif
174
21.9k
  br->bit_pos_ -= 8;
175
21.9k
  --br->avail_in;
176
21.9k
  ++br->next_in;
177
21.9k
  return BROTLI_TRUE;
178
24.3k
}
179
180
/* Returns currently available bits.
181
   The number of valid bits could be calculated by BrotliGetAvailableBits. */
182
static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(
183
2.36G
    BrotliBitReader* const br) {
184
2.36G
  return br->val_ >> br->bit_pos_;
185
2.36G
}
decode.c:BrotliGetBitsUnmasked
Line
Count
Source
183
2.36G
    BrotliBitReader* const br) {
184
2.36G
  return br->val_ >> br->bit_pos_;
185
2.36G
}
Unexecuted instantiation: state.c:BrotliGetBitsUnmasked
Unexecuted instantiation: bit_reader.c:BrotliGetBitsUnmasked
186
187
/* Like BrotliGetBits, but does not mask the result.
188
   The result contains at least 16 valid bits. */
189
static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked(
190
139M
    BrotliBitReader* const br) {
191
139M
  BrotliFillBitWindow(br, 16);
192
139M
  return (uint32_t)BrotliGetBitsUnmasked(br);
193
139M
}
decode.c:BrotliGet16BitsUnmasked
Line
Count
Source
190
139M
    BrotliBitReader* const br) {
191
139M
  BrotliFillBitWindow(br, 16);
192
139M
  return (uint32_t)BrotliGetBitsUnmasked(br);
193
139M
}
Unexecuted instantiation: state.c:BrotliGet16BitsUnmasked
Unexecuted instantiation: bit_reader.c:BrotliGet16BitsUnmasked
194
195
/* Returns the specified number of bits from |br| without advancing bit
196
   position. */
197
static BROTLI_INLINE uint32_t BrotliGetBits(
198
697M
    BrotliBitReader* const br, uint32_t n_bits) {
199
697M
  BrotliFillBitWindow(br, n_bits);
200
697M
  return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
201
697M
}
decode.c:BrotliGetBits
Line
Count
Source
198
697M
    BrotliBitReader* const br, uint32_t n_bits) {
199
697M
  BrotliFillBitWindow(br, n_bits);
200
697M
  return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
201
697M
}
Unexecuted instantiation: state.c:BrotliGetBits
Unexecuted instantiation: bit_reader.c:BrotliGetBits
202
203
/* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there
204
   is not enough input. */
205
static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
206
1.46G
    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
207
1.46G
  while (BrotliGetAvailableBits(br) < n_bits) {
208
182M
    if (!BrotliPullByte(br)) {
209
182M
      return BROTLI_FALSE;
210
182M
    }
211
182M
  }
212
1.27G
  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
213
1.27G
  return BROTLI_TRUE;
214
1.46G
}
decode.c:BrotliSafeGetBits
Line
Count
Source
206
1.46G
    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
207
1.46G
  while (BrotliGetAvailableBits(br) < n_bits) {
208
182M
    if (!BrotliPullByte(br)) {
209
182M
      return BROTLI_FALSE;
210
182M
    }
211
182M
  }
212
1.27G
  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
213
1.27G
  return BROTLI_TRUE;
214
1.46G
}
Unexecuted instantiation: state.c:BrotliSafeGetBits
Unexecuted instantiation: bit_reader.c:BrotliSafeGetBits
215
216
/* Advances the bit pos by |n_bits|. */
217
static BROTLI_INLINE void BrotliDropBits(
218
2.35G
    BrotliBitReader* const br, uint32_t n_bits) {
219
2.35G
  br->bit_pos_ += n_bits;
220
2.35G
}
decode.c:BrotliDropBits
Line
Count
Source
218
2.35G
    BrotliBitReader* const br, uint32_t n_bits) {
219
2.35G
  br->bit_pos_ += n_bits;
220
2.35G
}
Unexecuted instantiation: state.c:BrotliDropBits
Unexecuted instantiation: bit_reader.c:BrotliDropBits
221
222
4.85k
static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
223
4.85k
  uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
224
4.85k
  uint32_t unused_bits = unused_bytes << 3;
225
4.85k
  br->avail_in += unused_bytes;
226
4.85k
  br->next_in -= unused_bytes;
227
4.85k
  if (unused_bits == sizeof(br->val_) << 3) {
228
18
    br->val_ = 0;
229
4.83k
  } else {
230
4.83k
    br->val_ <<= unused_bits;
231
4.83k
  }
232
4.85k
  br->bit_pos_ += unused_bits;
233
4.85k
}
decode.c:BrotliBitReaderUnload
Line
Count
Source
222
4.85k
static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
223
4.85k
  uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
224
4.85k
  uint32_t unused_bits = unused_bytes << 3;
225
4.85k
  br->avail_in += unused_bytes;
226
4.85k
  br->next_in -= unused_bytes;
227
4.85k
  if (unused_bits == sizeof(br->val_) << 3) {
228
18
    br->val_ = 0;
229
4.83k
  } else {
230
4.83k
    br->val_ <<= unused_bits;
231
4.83k
  }
232
4.85k
  br->bit_pos_ += unused_bits;
233
4.85k
}
Unexecuted instantiation: state.c:BrotliBitReaderUnload
Unexecuted instantiation: bit_reader.c:BrotliBitReaderUnload
234
235
/* Reads the specified number of bits from |br| and advances the bit pos.
236
   Precondition: accumulator MUST contain at least |n_bits|. */
237
static BROTLI_INLINE void BrotliTakeBits(
238
74.2M
  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
239
74.2M
  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
240
74.2M
  BROTLI_LOG(("[BrotliReadBits]  %d %d %d val: %6x\n",
241
74.2M
      (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val));
242
74.2M
  BrotliDropBits(br, n_bits);
243
74.2M
}
decode.c:BrotliTakeBits
Line
Count
Source
238
74.2M
  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
239
74.2M
  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
240
74.2M
  BROTLI_LOG(("[BrotliReadBits]  %d %d %d val: %6x\n",
241
74.2M
      (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val));
242
74.2M
  BrotliDropBits(br, n_bits);
243
74.2M
}
Unexecuted instantiation: state.c:BrotliTakeBits
Unexecuted instantiation: bit_reader.c:BrotliTakeBits
244
245
/* Reads the specified number of bits from |br| and advances the bit pos.
246
   Assumes that there is enough input to perform BrotliFillBitWindow. */
247
static BROTLI_INLINE uint32_t BrotliReadBits(
248
69.5M
    BrotliBitReader* const br, uint32_t n_bits) {
249
69.5M
  if (BROTLI_64_BITS || (n_bits <= 16)) {
250
69.5M
    uint32_t val;
251
69.5M
    BrotliFillBitWindow(br, n_bits);
252
69.5M
    BrotliTakeBits(br, n_bits, &val);
253
69.5M
    return val;
254
69.5M
  } else {
255
0
    uint32_t low_val;
256
0
    uint32_t high_val;
257
0
    BrotliFillBitWindow(br, 16);
258
0
    BrotliTakeBits(br, 16, &low_val);
259
0
    BrotliFillBitWindow(br, 8);
260
0
    BrotliTakeBits(br, n_bits - 16, &high_val);
261
0
    return low_val | (high_val << 16);
262
0
  }
263
69.5M
}
decode.c:BrotliReadBits
Line
Count
Source
248
69.5M
    BrotliBitReader* const br, uint32_t n_bits) {
249
69.5M
  if (BROTLI_64_BITS || (n_bits <= 16)) {
250
69.5M
    uint32_t val;
251
69.5M
    BrotliFillBitWindow(br, n_bits);
252
69.5M
    BrotliTakeBits(br, n_bits, &val);
253
69.5M
    return val;
254
69.5M
  } else {
255
0
    uint32_t low_val;
256
0
    uint32_t high_val;
257
0
    BrotliFillBitWindow(br, 16);
258
0
    BrotliTakeBits(br, 16, &low_val);
259
0
    BrotliFillBitWindow(br, 8);
260
0
    BrotliTakeBits(br, n_bits - 16, &high_val);
261
0
    return low_val | (high_val << 16);
262
0
  }
263
69.5M
}
Unexecuted instantiation: state.c:BrotliReadBits
Unexecuted instantiation: bit_reader.c:BrotliReadBits
264
265
/* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
266
   is not enough input. |n_bits| MUST be positive. */
267
static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
268
4.50M
    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
269
7.34M
  while (BrotliGetAvailableBits(br) < n_bits) {
270
2.84M
    if (!BrotliPullByte(br)) {
271
2.12k
      return BROTLI_FALSE;
272
2.12k
    }
273
2.84M
  }
274
4.50M
  BrotliTakeBits(br, n_bits, val);
275
4.50M
  return BROTLI_TRUE;
276
4.50M
}
decode.c:BrotliSafeReadBits
Line
Count
Source
268
4.50M
    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
269
7.34M
  while (BrotliGetAvailableBits(br) < n_bits) {
270
2.84M
    if (!BrotliPullByte(br)) {
271
2.12k
      return BROTLI_FALSE;
272
2.12k
    }
273
2.84M
  }
274
4.50M
  BrotliTakeBits(br, n_bits, val);
275
4.50M
  return BROTLI_TRUE;
276
4.50M
}
Unexecuted instantiation: state.c:BrotliSafeReadBits
Unexecuted instantiation: bit_reader.c:BrotliSafeReadBits
277
278
/* Advances the bit reader position to the next byte boundary and verifies
279
   that any skipped bits are set to zero. */
280
227k
static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
281
227k
  uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
282
227k
  uint32_t pad_bits = 0;
283
227k
  if (pad_bits_count != 0) {
284
227k
    BrotliTakeBits(br, pad_bits_count, &pad_bits);
285
227k
  }
286
227k
  return TO_BROTLI_BOOL(pad_bits == 0);
287
227k
}
decode.c:BrotliJumpToByteBoundary
Line
Count
Source
280
227k
static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
281
227k
  uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
282
227k
  uint32_t pad_bits = 0;
283
227k
  if (pad_bits_count != 0) {
284
227k
    BrotliTakeBits(br, pad_bits_count, &pad_bits);
285
227k
  }
286
227k
  return TO_BROTLI_BOOL(pad_bits == 0);
287
227k
}
Unexecuted instantiation: state.c:BrotliJumpToByteBoundary
Unexecuted instantiation: bit_reader.c:BrotliJumpToByteBoundary
288
289
/* Copies remaining input bytes stored in the bit reader to the output. Value
290
   |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
291
   warmed up again after this. */
292
static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
293
10.1k
                                          BrotliBitReader* br, size_t num) {
294
17.9k
  while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
295
7.79k
    *dest = (uint8_t)BrotliGetBitsUnmasked(br);
296
7.79k
    BrotliDropBits(br, 8);
297
7.79k
    ++dest;
298
7.79k
    --num;
299
7.79k
  }
300
10.1k
  memcpy(dest, br->next_in, num);
301
10.1k
  br->avail_in -= num;
302
10.1k
  br->next_in += num;
303
10.1k
}
decode.c:BrotliCopyBytes
Line
Count
Source
293
10.1k
                                          BrotliBitReader* br, size_t num) {
294
17.9k
  while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
295
7.79k
    *dest = (uint8_t)BrotliGetBitsUnmasked(br);
296
7.79k
    BrotliDropBits(br, 8);
297
7.79k
    ++dest;
298
7.79k
    --num;
299
7.79k
  }
300
10.1k
  memcpy(dest, br->next_in, num);
301
10.1k
  br->avail_in -= num;
302
10.1k
  br->next_in += num;
303
10.1k
}
Unexecuted instantiation: state.c:BrotliCopyBytes
Unexecuted instantiation: bit_reader.c:BrotliCopyBytes
304
305
#if defined(__cplusplus) || defined(c_plusplus)
306
}  /* extern "C" */
307
#endif
308
309
#endif  /* BROTLI_DEC_BIT_READER_H_ */