Coverage Report

Created: 2025-08-26 06:35

/src/h2o/deps/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
#include "./port.h"
16
17
#if defined(__cplusplus) || defined(c_plusplus)
18
extern "C" {
19
#endif
20
21
0
#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(reg_t) >> 1)
22
23
static const uint32_t kBitMask[33] = { 0x0000,
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
0
static BROTLI_INLINE uint32_t BitMask(uint32_t n) {
35
0
  if (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
0
    return ~((0xffffffffU) << n);
39
0
  } else {
40
0
    return kBitMask[n];
41
0
  }
42
0
}
Unexecuted instantiation: bit_reader.c:BitMask
Unexecuted instantiation: decode.c:BitMask
Unexecuted instantiation: state.c:BitMask
43
44
typedef struct {
45
  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
  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. May consume one byte of input.
62
   Returns 0 if data is required but there is no input available.
63
   For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
64
   reading. */
65
BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br);
66
67
static BROTLI_INLINE void BrotliBitReaderSaveState(
68
0
    BrotliBitReader* const from, BrotliBitReaderState* to) {
69
0
  to->val_ = from->val_;
70
0
  to->bit_pos_ = from->bit_pos_;
71
0
  to->next_in = from->next_in;
72
0
  to->avail_in = from->avail_in;
73
0
}
Unexecuted instantiation: bit_reader.c:BrotliBitReaderSaveState
Unexecuted instantiation: decode.c:BrotliBitReaderSaveState
Unexecuted instantiation: state.c:BrotliBitReaderSaveState
74
75
static BROTLI_INLINE void BrotliBitReaderRestoreState(
76
0
    BrotliBitReader* const to, BrotliBitReaderState* from) {
77
0
  to->val_ = from->val_;
78
0
  to->bit_pos_ = from->bit_pos_;
79
0
  to->next_in = from->next_in;
80
0
  to->avail_in = from->avail_in;
81
0
}
Unexecuted instantiation: bit_reader.c:BrotliBitReaderRestoreState
Unexecuted instantiation: decode.c:BrotliBitReaderRestoreState
Unexecuted instantiation: state.c:BrotliBitReaderRestoreState
82
83
static BROTLI_INLINE uint32_t BrotliGetAvailableBits(
84
0
    const BrotliBitReader* br) {
85
0
  return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
86
0
}
Unexecuted instantiation: bit_reader.c:BrotliGetAvailableBits
Unexecuted instantiation: decode.c:BrotliGetAvailableBits
Unexecuted instantiation: state.c:BrotliGetAvailableBits
87
88
/* Returns amount of unread bytes the bit reader still has buffered from the
89
   BrotliInput, including whole bytes in br->val_. */
90
0
static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
91
0
  return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
92
0
}
Unexecuted instantiation: bit_reader.c:BrotliGetRemainingBytes
Unexecuted instantiation: decode.c:BrotliGetRemainingBytes
Unexecuted instantiation: state.c:BrotliGetRemainingBytes
93
94
/* Checks if there is at least |num| bytes left in the input ring-buffer
95
   (excluding the bits remaining in br->val_). */
96
static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(
97
0
    BrotliBitReader* const br, size_t num) {
98
0
  return TO_BROTLI_BOOL(br->avail_in >= num);
99
0
}
Unexecuted instantiation: bit_reader.c:BrotliCheckInputAmount
Unexecuted instantiation: decode.c:BrotliCheckInputAmount
Unexecuted instantiation: state.c:BrotliCheckInputAmount
100
101
0
static BROTLI_INLINE uint16_t BrotliLoad16LE(const uint8_t* in) {
102
0
  if (BROTLI_LITTLE_ENDIAN) {
103
0
    return *((const uint16_t*)in);
104
0
  } else if (BROTLI_BIG_ENDIAN) {
105
0
    uint16_t value = *((const uint16_t*)in);
106
0
    return (uint16_t)(((value & 0xFFU) << 8) | ((value & 0xFF00U) >> 8));
107
0
  } else {
108
0
    return (uint16_t)(in[0] | (in[1] << 8));
109
0
  }
110
0
}
Unexecuted instantiation: bit_reader.c:BrotliLoad16LE
Unexecuted instantiation: decode.c:BrotliLoad16LE
Unexecuted instantiation: state.c:BrotliLoad16LE
111
112
0
static BROTLI_INLINE uint32_t BrotliLoad32LE(const uint8_t* in) {
113
0
  if (BROTLI_LITTLE_ENDIAN) {
114
0
    return *((const uint32_t*)in);
115
0
  } else if (BROTLI_BIG_ENDIAN) {
116
0
    uint32_t value = *((const uint32_t*)in);
117
0
    return ((value & 0xFFU) << 24) | ((value & 0xFF00U) << 8) |
118
0
        ((value & 0xFF0000U) >> 8) | ((value & 0xFF000000U) >> 24);
119
0
  } else {
120
0
    uint32_t value = (uint32_t)(*(in++));
121
0
    value |= (uint32_t)(*(in++)) << 8;
122
0
    value |= (uint32_t)(*(in++)) << 16;
123
0
    value |= (uint32_t)(*(in++)) << 24;
124
0
    return value;
125
0
  }
126
0
}
Unexecuted instantiation: bit_reader.c:BrotliLoad32LE
Unexecuted instantiation: decode.c:BrotliLoad32LE
Unexecuted instantiation: state.c:BrotliLoad32LE
127
128
#if (BROTLI_64_BITS)
129
0
static BROTLI_INLINE uint64_t BrotliLoad64LE(const uint8_t* in) {
130
0
  if (BROTLI_LITTLE_ENDIAN) {
131
0
    return *((const uint64_t*)in);
132
0
  } else if (BROTLI_BIG_ENDIAN) {
133
0
    uint64_t value = *((const uint64_t*)in);
134
0
    return
135
0
        ((value & 0xFFU) << 56) |
136
0
        ((value & 0xFF00U) << 40) |
137
0
        ((value & 0xFF0000U) << 24) |
138
0
        ((value & 0xFF000000U) << 8) |
139
0
        ((value & 0xFF00000000U) >> 8) |
140
0
        ((value & 0xFF0000000000U) >> 24) |
141
0
        ((value & 0xFF000000000000U) >> 40) |
142
0
        ((value & 0xFF00000000000000U) >> 56);
143
0
  } else {
144
0
    uint64_t value = (uint64_t)(*(in++));
145
0
    value |= (uint64_t)(*(in++)) << 8;
146
0
    value |= (uint64_t)(*(in++)) << 16;
147
0
    value |= (uint64_t)(*(in++)) << 24;
148
0
    value |= (uint64_t)(*(in++)) << 32;
149
0
    value |= (uint64_t)(*(in++)) << 40;
150
0
    value |= (uint64_t)(*(in++)) << 48;
151
0
    value |= (uint64_t)(*(in++)) << 56;
152
0
    return value;
153
0
  }
154
0
}
Unexecuted instantiation: bit_reader.c:BrotliLoad64LE
Unexecuted instantiation: decode.c:BrotliLoad64LE
Unexecuted instantiation: state.c:BrotliLoad64LE
155
#endif
156
157
/* Guarantees that there are at least n_bits + 1 bits in accumulator.
158
   Precondition: accumulator contains at least 1 bit.
159
   n_bits should be in the range [1..24] for regular build. For portable
160
   non-64-bit little-endian build only 16 bits are safe to request. */
161
static BROTLI_INLINE void BrotliFillBitWindow(
162
0
    BrotliBitReader* const br, uint32_t n_bits) {
163
0
#if (BROTLI_64_BITS)
164
0
  if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) {
165
0
    if (br->bit_pos_ >= 56) {
166
0
      br->val_ >>= 56;
167
0
      br->bit_pos_ ^= 56;  /* here same as -= 56 because of the if condition */
168
0
      br->val_ |= BrotliLoad64LE(br->next_in) << 8;
169
0
      br->avail_in -= 7;
170
0
      br->next_in += 7;
171
0
    }
172
0
  } else if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 16)) {
173
0
    if (br->bit_pos_ >= 48) {
174
0
      br->val_ >>= 48;
175
0
      br->bit_pos_ ^= 48;  /* here same as -= 48 because of the if condition */
176
0
      br->val_ |= BrotliLoad64LE(br->next_in) << 16;
177
0
      br->avail_in -= 6;
178
0
      br->next_in += 6;
179
0
    }
180
0
  } else {
181
0
    if (br->bit_pos_ >= 32) {
182
0
      br->val_ >>= 32;
183
0
      br->bit_pos_ ^= 32;  /* here same as -= 32 because of the if condition */
184
0
      br->val_ |= ((uint64_t)BrotliLoad32LE(br->next_in)) << 32;
185
0
      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
186
0
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
187
0
    }
188
0
  }
189
#else
190
  if (!BROTLI_ALIGNED_READ && IS_CONSTANT(n_bits) && (n_bits <= 8)) {
191
    if (br->bit_pos_ >= 24) {
192
      br->val_ >>= 24;
193
      br->bit_pos_ ^= 24;  /* here same as -= 24 because of the if condition */
194
      br->val_ |= BrotliLoad32LE(br->next_in) << 8;
195
      br->avail_in -= 3;
196
      br->next_in += 3;
197
    }
198
  } else {
199
    if (br->bit_pos_ >= 16) {
200
      br->val_ >>= 16;
201
      br->bit_pos_ ^= 16;  /* here same as -= 16 because of the if condition */
202
      br->val_ |= ((uint32_t)BrotliLoad16LE(br->next_in)) << 16;
203
      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
204
      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
205
    }
206
  }
207
#endif
208
0
}
Unexecuted instantiation: bit_reader.c:BrotliFillBitWindow
Unexecuted instantiation: decode.c:BrotliFillBitWindow
Unexecuted instantiation: state.c:BrotliFillBitWindow
209
210
/* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
211
   more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
212
0
static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
213
0
  BrotliFillBitWindow(br, 17);
214
0
}
Unexecuted instantiation: bit_reader.c:BrotliFillBitWindow16
Unexecuted instantiation: decode.c:BrotliFillBitWindow16
Unexecuted instantiation: state.c:BrotliFillBitWindow16
215
216
/* Pulls one byte of input to accumulator. */
217
0
static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
218
0
  if (br->avail_in == 0) {
219
0
    return BROTLI_FALSE;
220
0
  }
221
0
  br->val_ >>= 8;
222
0
#if (BROTLI_64_BITS)
223
0
  br->val_ |= ((uint64_t)*br->next_in) << 56;
224
#else
225
  br->val_ |= ((uint32_t)*br->next_in) << 24;
226
#endif
227
0
  br->bit_pos_ -= 8;
228
0
  --br->avail_in;
229
0
  ++br->next_in;
230
0
  return BROTLI_TRUE;
231
0
}
Unexecuted instantiation: bit_reader.c:BrotliPullByte
Unexecuted instantiation: decode.c:BrotliPullByte
Unexecuted instantiation: state.c:BrotliPullByte
232
233
/* Returns currently available bits.
234
   The number of valid bits could be calculated by BrotliGetAvailableBits. */
235
0
static BROTLI_INLINE reg_t BrotliGetBitsUnmasked(BrotliBitReader* const br) {
236
0
  return br->val_ >> br->bit_pos_;
237
0
}
Unexecuted instantiation: bit_reader.c:BrotliGetBitsUnmasked
Unexecuted instantiation: decode.c:BrotliGetBitsUnmasked
Unexecuted instantiation: state.c:BrotliGetBitsUnmasked
238
239
/* Like BrotliGetBits, but does not mask the result.
240
   The result contains at least 16 valid bits. */
241
static BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked(
242
0
    BrotliBitReader* const br) {
243
0
  BrotliFillBitWindow(br, 16);
244
0
  return (uint32_t)BrotliGetBitsUnmasked(br);
245
0
}
Unexecuted instantiation: bit_reader.c:BrotliGet16BitsUnmasked
Unexecuted instantiation: decode.c:BrotliGet16BitsUnmasked
Unexecuted instantiation: state.c:BrotliGet16BitsUnmasked
246
247
/* Returns the specified number of bits from |br| without advancing bit pos. */
248
static BROTLI_INLINE uint32_t BrotliGetBits(
249
0
    BrotliBitReader* const br, uint32_t n_bits) {
250
0
  BrotliFillBitWindow(br, n_bits);
251
0
  return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
252
0
}
Unexecuted instantiation: bit_reader.c:BrotliGetBits
Unexecuted instantiation: decode.c:BrotliGetBits
Unexecuted instantiation: state.c:BrotliGetBits
253
254
/* Tries to peek the specified amount of bits. Returns 0, if there is not
255
   enough input. */
256
static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
257
0
    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
258
0
  while (BrotliGetAvailableBits(br) < n_bits) {
259
0
    if (!BrotliPullByte(br)) {
260
0
      return BROTLI_FALSE;
261
0
    }
262
0
  }
263
0
  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
264
0
  return BROTLI_TRUE;
265
0
}
Unexecuted instantiation: bit_reader.c:BrotliSafeGetBits
Unexecuted instantiation: decode.c:BrotliSafeGetBits
Unexecuted instantiation: state.c:BrotliSafeGetBits
266
267
/* Advances the bit pos by n_bits. */
268
static BROTLI_INLINE void BrotliDropBits(
269
0
    BrotliBitReader* const br, uint32_t n_bits) {
270
0
  br->bit_pos_ += n_bits;
271
0
}
Unexecuted instantiation: bit_reader.c:BrotliDropBits
Unexecuted instantiation: decode.c:BrotliDropBits
Unexecuted instantiation: state.c:BrotliDropBits
272
273
0
static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
274
0
  uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
275
0
  uint32_t unused_bits = unused_bytes << 3;
276
0
  br->avail_in += unused_bytes;
277
0
  br->next_in -= unused_bytes;
278
0
  if (unused_bits == sizeof(br->val_) << 3) {
279
0
    br->val_ = 0;
280
0
  } else {
281
0
    br->val_ <<= unused_bits;
282
0
  }
283
0
  br->bit_pos_ += unused_bits;
284
0
}
Unexecuted instantiation: bit_reader.c:BrotliBitReaderUnload
Unexecuted instantiation: decode.c:BrotliBitReaderUnload
Unexecuted instantiation: state.c:BrotliBitReaderUnload
285
286
/* Reads the specified number of bits from |br| and advances the bit pos.
287
   Precondition: accumulator MUST contain at least n_bits. */
288
static BROTLI_INLINE void BrotliTakeBits(
289
0
  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
290
0
  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
291
0
  BROTLI_LOG(("[BrotliReadBits]  %d %d %d val: %6x\n",
292
0
      (int)br->avail_in, (int)br->bit_pos_, n_bits, (int)*val));
293
0
  BrotliDropBits(br, n_bits);
294
0
}
Unexecuted instantiation: bit_reader.c:BrotliTakeBits
Unexecuted instantiation: decode.c:BrotliTakeBits
Unexecuted instantiation: state.c:BrotliTakeBits
295
296
/* Reads the specified number of bits from |br| and advances the bit pos.
297
   Assumes that there is enough input to perform BrotliFillBitWindow. */
298
static BROTLI_INLINE uint32_t BrotliReadBits(
299
0
    BrotliBitReader* const br, uint32_t n_bits) {
300
0
  if (BROTLI_64_BITS || (n_bits <= 16)) {
301
0
    uint32_t val;
302
0
    BrotliFillBitWindow(br, n_bits);
303
0
    BrotliTakeBits(br, n_bits, &val);
304
0
    return val;
305
0
  } else {
306
0
    uint32_t low_val;
307
0
    uint32_t high_val;
308
0
    BrotliFillBitWindow(br, 16);
309
0
    BrotliTakeBits(br, 16, &low_val);
310
0
    BrotliFillBitWindow(br, 8);
311
0
    BrotliTakeBits(br, n_bits - 16, &high_val);
312
0
    return low_val | (high_val << 16);
313
0
  }
314
0
}
Unexecuted instantiation: bit_reader.c:BrotliReadBits
Unexecuted instantiation: decode.c:BrotliReadBits
Unexecuted instantiation: state.c:BrotliReadBits
315
316
/* Tries to read the specified amount of bits. Returns 0, if there is not
317
   enough input. n_bits MUST be positive. */
318
static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
319
0
    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
320
0
  while (BrotliGetAvailableBits(br) < n_bits) {
321
0
    if (!BrotliPullByte(br)) {
322
0
      return BROTLI_FALSE;
323
0
    }
324
0
  }
325
0
  BrotliTakeBits(br, n_bits, val);
326
0
  return BROTLI_TRUE;
327
0
}
Unexecuted instantiation: bit_reader.c:BrotliSafeReadBits
Unexecuted instantiation: decode.c:BrotliSafeReadBits
Unexecuted instantiation: state.c:BrotliSafeReadBits
328
329
/* Advances the bit reader position to the next byte boundary and verifies
330
   that any skipped bits are set to zero. */
331
0
static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
332
0
  uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
333
0
  uint32_t pad_bits = 0;
334
0
  if (pad_bits_count != 0) {
335
0
    BrotliTakeBits(br, pad_bits_count, &pad_bits);
336
0
  }
337
0
  return TO_BROTLI_BOOL(pad_bits == 0);
338
0
}
Unexecuted instantiation: bit_reader.c:BrotliJumpToByteBoundary
Unexecuted instantiation: decode.c:BrotliJumpToByteBoundary
Unexecuted instantiation: state.c:BrotliJumpToByteBoundary
339
340
/* Copies remaining input bytes stored in the bit reader to the output. Value
341
   num may not be larger than BrotliGetRemainingBytes. The bit reader must be
342
   warmed up again after this. */
343
static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
344
0
                                          BrotliBitReader* br, size_t num) {
345
0
  while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
346
0
    *dest = (uint8_t)BrotliGetBitsUnmasked(br);
347
0
    BrotliDropBits(br, 8);
348
0
    ++dest;
349
0
    --num;
350
0
  }
351
0
  memcpy(dest, br->next_in, num);
352
0
  br->avail_in -= num;
353
0
  br->next_in += num;
354
0
}
Unexecuted instantiation: bit_reader.c:BrotliCopyBytes
Unexecuted instantiation: decode.c:BrotliCopyBytes
Unexecuted instantiation: state.c:BrotliCopyBytes
355
356
#if defined(__cplusplus) || defined(c_plusplus)
357
}  /* extern "C" */
358
#endif
359
360
#endif  /* BROTLI_DEC_BIT_READER_H_ */