11cb0ef41Sopenharmony_ci/* Copyright 2013 Google Inc. All Rights Reserved.
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci   Distributed under MIT license.
41cb0ef41Sopenharmony_ci   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
51cb0ef41Sopenharmony_ci*/
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci/* Bit reading helpers */
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci#ifndef BROTLI_DEC_BIT_READER_H_
101cb0ef41Sopenharmony_ci#define BROTLI_DEC_BIT_READER_H_
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_ci#include <string.h>  /* memcpy */
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ci#include "../common/constants.h"
151cb0ef41Sopenharmony_ci#include "../common/platform.h"
161cb0ef41Sopenharmony_ci#include <brotli/types.h>
171cb0ef41Sopenharmony_ci
181cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
191cb0ef41Sopenharmony_ciextern "C" {
201cb0ef41Sopenharmony_ci#endif
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ciBROTLI_INTERNAL extern const uint32_t kBrotliBitMask[33];
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BitMask(uint32_t n) {
271cb0ef41Sopenharmony_ci  if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {
281cb0ef41Sopenharmony_ci    /* Masking with this expression turns to a single
291cb0ef41Sopenharmony_ci       "Unsigned Bit Field Extract" UBFX instruction on ARM. */
301cb0ef41Sopenharmony_ci    return ~((0xFFFFFFFFu) << n);
311cb0ef41Sopenharmony_ci  } else {
321cb0ef41Sopenharmony_ci    return kBrotliBitMask[n];
331cb0ef41Sopenharmony_ci  }
341cb0ef41Sopenharmony_ci}
351cb0ef41Sopenharmony_ci
361cb0ef41Sopenharmony_citypedef struct {
371cb0ef41Sopenharmony_ci  brotli_reg_t val_;       /* pre-fetched bits */
381cb0ef41Sopenharmony_ci  uint32_t bit_pos_;       /* current bit-reading position in val_ */
391cb0ef41Sopenharmony_ci  const uint8_t* next_in;  /* the byte we're reading from */
401cb0ef41Sopenharmony_ci  size_t avail_in;
411cb0ef41Sopenharmony_ci} BrotliBitReader;
421cb0ef41Sopenharmony_ci
431cb0ef41Sopenharmony_citypedef struct {
441cb0ef41Sopenharmony_ci  brotli_reg_t val_;
451cb0ef41Sopenharmony_ci  uint32_t bit_pos_;
461cb0ef41Sopenharmony_ci  const uint8_t* next_in;
471cb0ef41Sopenharmony_ci  size_t avail_in;
481cb0ef41Sopenharmony_ci} BrotliBitReaderState;
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci/* Initializes the BrotliBitReader fields. */
511cb0ef41Sopenharmony_ciBROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* const br);
521cb0ef41Sopenharmony_ci
531cb0ef41Sopenharmony_ci/* Ensures that accumulator is not empty.
541cb0ef41Sopenharmony_ci   May consume up to sizeof(brotli_reg_t) - 1 bytes of input.
551cb0ef41Sopenharmony_ci   Returns BROTLI_FALSE if data is required but there is no input available.
561cb0ef41Sopenharmony_ci   For BROTLI_ALIGNED_READ this function also prepares bit reader for aligned
571cb0ef41Sopenharmony_ci   reading. */
581cb0ef41Sopenharmony_ciBROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br);
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci/* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden
611cb0ef41Sopenharmony_ci   the main code-path. Never called for RFC brotli streams, required only for
621cb0ef41Sopenharmony_ci   "large-window" mode and other extensions. */
631cb0ef41Sopenharmony_ciBROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow(
641cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val);
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliBitReaderSaveState(
671cb0ef41Sopenharmony_ci    BrotliBitReader* const from, BrotliBitReaderState* to) {
681cb0ef41Sopenharmony_ci  to->val_ = from->val_;
691cb0ef41Sopenharmony_ci  to->bit_pos_ = from->bit_pos_;
701cb0ef41Sopenharmony_ci  to->next_in = from->next_in;
711cb0ef41Sopenharmony_ci  to->avail_in = from->avail_in;
721cb0ef41Sopenharmony_ci}
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliBitReaderRestoreState(
751cb0ef41Sopenharmony_ci    BrotliBitReader* const to, BrotliBitReaderState* from) {
761cb0ef41Sopenharmony_ci  to->val_ = from->val_;
771cb0ef41Sopenharmony_ci  to->bit_pos_ = from->bit_pos_;
781cb0ef41Sopenharmony_ci  to->next_in = from->next_in;
791cb0ef41Sopenharmony_ci  to->avail_in = from->avail_in;
801cb0ef41Sopenharmony_ci}
811cb0ef41Sopenharmony_ci
821cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliGetAvailableBits(
831cb0ef41Sopenharmony_ci    const BrotliBitReader* br) {
841cb0ef41Sopenharmony_ci  return (BROTLI_64_BITS ? 64 : 32) - br->bit_pos_;
851cb0ef41Sopenharmony_ci}
861cb0ef41Sopenharmony_ci
871cb0ef41Sopenharmony_ci/* Returns amount of unread bytes the bit reader still has buffered from the
881cb0ef41Sopenharmony_ci   BrotliInput, including whole bytes in br->val_. Result is capped with
891cb0ef41Sopenharmony_ci   maximal ring-buffer size (larger number won't be utilized anyway). */
901cb0ef41Sopenharmony_cistatic BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {
911cb0ef41Sopenharmony_ci  static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;
921cb0ef41Sopenharmony_ci  if (br->avail_in > kCap) return kCap;
931cb0ef41Sopenharmony_ci  return br->avail_in + (BrotliGetAvailableBits(br) >> 3);
941cb0ef41Sopenharmony_ci}
951cb0ef41Sopenharmony_ci
961cb0ef41Sopenharmony_ci/* Checks if there is at least |num| bytes left in the input ring-buffer
971cb0ef41Sopenharmony_ci   (excluding the bits remaining in br->val_). */
981cb0ef41Sopenharmony_cistatic BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(
991cb0ef41Sopenharmony_ci    BrotliBitReader* const br, size_t num) {
1001cb0ef41Sopenharmony_ci  return TO_BROTLI_BOOL(br->avail_in >= num);
1011cb0ef41Sopenharmony_ci}
1021cb0ef41Sopenharmony_ci
1031cb0ef41Sopenharmony_ci/* Guarantees that there are at least |n_bits| + 1 bits in accumulator.
1041cb0ef41Sopenharmony_ci   Precondition: accumulator contains at least 1 bit.
1051cb0ef41Sopenharmony_ci   |n_bits| should be in the range [1..24] for regular build. For portable
1061cb0ef41Sopenharmony_ci   non-64-bit little-endian build only 16 bits are safe to request. */
1071cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliFillBitWindow(
1081cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits) {
1091cb0ef41Sopenharmony_ci#if (BROTLI_64_BITS)
1101cb0ef41Sopenharmony_ci  if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
1111cb0ef41Sopenharmony_ci    if (br->bit_pos_ >= 56) {
1121cb0ef41Sopenharmony_ci      br->val_ >>= 56;
1131cb0ef41Sopenharmony_ci      br->bit_pos_ ^= 56;  /* here same as -= 56 because of the if condition */
1141cb0ef41Sopenharmony_ci      br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 8;
1151cb0ef41Sopenharmony_ci      br->avail_in -= 7;
1161cb0ef41Sopenharmony_ci      br->next_in += 7;
1171cb0ef41Sopenharmony_ci    }
1181cb0ef41Sopenharmony_ci  } else if (
1191cb0ef41Sopenharmony_ci      !BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 16)) {
1201cb0ef41Sopenharmony_ci    if (br->bit_pos_ >= 48) {
1211cb0ef41Sopenharmony_ci      br->val_ >>= 48;
1221cb0ef41Sopenharmony_ci      br->bit_pos_ ^= 48;  /* here same as -= 48 because of the if condition */
1231cb0ef41Sopenharmony_ci      br->val_ |= BROTLI_UNALIGNED_LOAD64LE(br->next_in) << 16;
1241cb0ef41Sopenharmony_ci      br->avail_in -= 6;
1251cb0ef41Sopenharmony_ci      br->next_in += 6;
1261cb0ef41Sopenharmony_ci    }
1271cb0ef41Sopenharmony_ci  } else {
1281cb0ef41Sopenharmony_ci    if (br->bit_pos_ >= 32) {
1291cb0ef41Sopenharmony_ci      br->val_ >>= 32;
1301cb0ef41Sopenharmony_ci      br->bit_pos_ ^= 32;  /* here same as -= 32 because of the if condition */
1311cb0ef41Sopenharmony_ci      br->val_ |= ((uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in)) << 32;
1321cb0ef41Sopenharmony_ci      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
1331cb0ef41Sopenharmony_ci      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
1341cb0ef41Sopenharmony_ci    }
1351cb0ef41Sopenharmony_ci  }
1361cb0ef41Sopenharmony_ci#else
1371cb0ef41Sopenharmony_ci  if (!BROTLI_ALIGNED_READ && BROTLI_IS_CONSTANT(n_bits) && (n_bits <= 8)) {
1381cb0ef41Sopenharmony_ci    if (br->bit_pos_ >= 24) {
1391cb0ef41Sopenharmony_ci      br->val_ >>= 24;
1401cb0ef41Sopenharmony_ci      br->bit_pos_ ^= 24;  /* here same as -= 24 because of the if condition */
1411cb0ef41Sopenharmony_ci      br->val_ |= BROTLI_UNALIGNED_LOAD32LE(br->next_in) << 8;
1421cb0ef41Sopenharmony_ci      br->avail_in -= 3;
1431cb0ef41Sopenharmony_ci      br->next_in += 3;
1441cb0ef41Sopenharmony_ci    }
1451cb0ef41Sopenharmony_ci  } else {
1461cb0ef41Sopenharmony_ci    if (br->bit_pos_ >= 16) {
1471cb0ef41Sopenharmony_ci      br->val_ >>= 16;
1481cb0ef41Sopenharmony_ci      br->bit_pos_ ^= 16;  /* here same as -= 16 because of the if condition */
1491cb0ef41Sopenharmony_ci      br->val_ |= ((uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in)) << 16;
1501cb0ef41Sopenharmony_ci      br->avail_in -= BROTLI_SHORT_FILL_BIT_WINDOW_READ;
1511cb0ef41Sopenharmony_ci      br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;
1521cb0ef41Sopenharmony_ci    }
1531cb0ef41Sopenharmony_ci  }
1541cb0ef41Sopenharmony_ci#endif
1551cb0ef41Sopenharmony_ci}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci/* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no
1581cb0ef41Sopenharmony_ci   more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */
1591cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {
1601cb0ef41Sopenharmony_ci  BrotliFillBitWindow(br, 17);
1611cb0ef41Sopenharmony_ci}
1621cb0ef41Sopenharmony_ci
1631cb0ef41Sopenharmony_ci/* Tries to pull one byte of input to accumulator.
1641cb0ef41Sopenharmony_ci   Returns BROTLI_FALSE if there is no input available. */
1651cb0ef41Sopenharmony_cistatic BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {
1661cb0ef41Sopenharmony_ci  if (br->avail_in == 0) {
1671cb0ef41Sopenharmony_ci    return BROTLI_FALSE;
1681cb0ef41Sopenharmony_ci  }
1691cb0ef41Sopenharmony_ci  br->val_ >>= 8;
1701cb0ef41Sopenharmony_ci#if (BROTLI_64_BITS)
1711cb0ef41Sopenharmony_ci  br->val_ |= ((uint64_t)*br->next_in) << 56;
1721cb0ef41Sopenharmony_ci#else
1731cb0ef41Sopenharmony_ci  br->val_ |= ((uint32_t)*br->next_in) << 24;
1741cb0ef41Sopenharmony_ci#endif
1751cb0ef41Sopenharmony_ci  br->bit_pos_ -= 8;
1761cb0ef41Sopenharmony_ci  --br->avail_in;
1771cb0ef41Sopenharmony_ci  ++br->next_in;
1781cb0ef41Sopenharmony_ci  return BROTLI_TRUE;
1791cb0ef41Sopenharmony_ci}
1801cb0ef41Sopenharmony_ci
1811cb0ef41Sopenharmony_ci/* Returns currently available bits.
1821cb0ef41Sopenharmony_ci   The number of valid bits could be calculated by BrotliGetAvailableBits. */
1831cb0ef41Sopenharmony_cistatic BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(
1841cb0ef41Sopenharmony_ci    BrotliBitReader* const br) {
1851cb0ef41Sopenharmony_ci  return br->val_ >> br->bit_pos_;
1861cb0ef41Sopenharmony_ci}
1871cb0ef41Sopenharmony_ci
1881cb0ef41Sopenharmony_ci/* Like BrotliGetBits, but does not mask the result.
1891cb0ef41Sopenharmony_ci   The result contains at least 16 valid bits. */
1901cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliGet16BitsUnmasked(
1911cb0ef41Sopenharmony_ci    BrotliBitReader* const br) {
1921cb0ef41Sopenharmony_ci  BrotliFillBitWindow(br, 16);
1931cb0ef41Sopenharmony_ci  return (uint32_t)BrotliGetBitsUnmasked(br);
1941cb0ef41Sopenharmony_ci}
1951cb0ef41Sopenharmony_ci
1961cb0ef41Sopenharmony_ci/* Returns the specified number of bits from |br| without advancing bit
1971cb0ef41Sopenharmony_ci   position. */
1981cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliGetBits(
1991cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits) {
2001cb0ef41Sopenharmony_ci  BrotliFillBitWindow(br, n_bits);
2011cb0ef41Sopenharmony_ci  return (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
2021cb0ef41Sopenharmony_ci}
2031cb0ef41Sopenharmony_ci
2041cb0ef41Sopenharmony_ci/* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there
2051cb0ef41Sopenharmony_ci   is not enough input. */
2061cb0ef41Sopenharmony_cistatic BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(
2071cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
2081cb0ef41Sopenharmony_ci  while (BrotliGetAvailableBits(br) < n_bits) {
2091cb0ef41Sopenharmony_ci    if (!BrotliPullByte(br)) {
2101cb0ef41Sopenharmony_ci      return BROTLI_FALSE;
2111cb0ef41Sopenharmony_ci    }
2121cb0ef41Sopenharmony_ci  }
2131cb0ef41Sopenharmony_ci  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
2141cb0ef41Sopenharmony_ci  return BROTLI_TRUE;
2151cb0ef41Sopenharmony_ci}
2161cb0ef41Sopenharmony_ci
2171cb0ef41Sopenharmony_ci/* Advances the bit pos by |n_bits|. */
2181cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliDropBits(
2191cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits) {
2201cb0ef41Sopenharmony_ci  br->bit_pos_ += n_bits;
2211cb0ef41Sopenharmony_ci}
2221cb0ef41Sopenharmony_ci
2231cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {
2241cb0ef41Sopenharmony_ci  uint32_t unused_bytes = BrotliGetAvailableBits(br) >> 3;
2251cb0ef41Sopenharmony_ci  uint32_t unused_bits = unused_bytes << 3;
2261cb0ef41Sopenharmony_ci  br->avail_in += unused_bytes;
2271cb0ef41Sopenharmony_ci  br->next_in -= unused_bytes;
2281cb0ef41Sopenharmony_ci  if (unused_bits == sizeof(br->val_) << 3) {
2291cb0ef41Sopenharmony_ci    br->val_ = 0;
2301cb0ef41Sopenharmony_ci  } else {
2311cb0ef41Sopenharmony_ci    br->val_ <<= unused_bits;
2321cb0ef41Sopenharmony_ci  }
2331cb0ef41Sopenharmony_ci  br->bit_pos_ += unused_bits;
2341cb0ef41Sopenharmony_ci}
2351cb0ef41Sopenharmony_ci
2361cb0ef41Sopenharmony_ci/* Reads the specified number of bits from |br| and advances the bit pos.
2371cb0ef41Sopenharmony_ci   Precondition: accumulator MUST contain at least |n_bits|. */
2381cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliTakeBits(
2391cb0ef41Sopenharmony_ci  BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
2401cb0ef41Sopenharmony_ci  *val = (uint32_t)BrotliGetBitsUnmasked(br) & BitMask(n_bits);
2411cb0ef41Sopenharmony_ci  BROTLI_LOG(("[BrotliTakeBits]  %d %d %d val: %6x\n",
2421cb0ef41Sopenharmony_ci      (int)br->avail_in, (int)br->bit_pos_, (int)n_bits, (int)*val));
2431cb0ef41Sopenharmony_ci  BrotliDropBits(br, n_bits);
2441cb0ef41Sopenharmony_ci}
2451cb0ef41Sopenharmony_ci
2461cb0ef41Sopenharmony_ci/* Reads the specified number of bits from |br| and advances the bit pos.
2471cb0ef41Sopenharmony_ci   Assumes that there is enough input to perform BrotliFillBitWindow.
2481cb0ef41Sopenharmony_ci   Up to 24 bits are allowed to be requested from this method. */
2491cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliReadBits24(
2501cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits) {
2511cb0ef41Sopenharmony_ci  BROTLI_DCHECK(n_bits <= 24);
2521cb0ef41Sopenharmony_ci  if (BROTLI_64_BITS || (n_bits <= 16)) {
2531cb0ef41Sopenharmony_ci    uint32_t val;
2541cb0ef41Sopenharmony_ci    BrotliFillBitWindow(br, n_bits);
2551cb0ef41Sopenharmony_ci    BrotliTakeBits(br, n_bits, &val);
2561cb0ef41Sopenharmony_ci    return val;
2571cb0ef41Sopenharmony_ci  } else {
2581cb0ef41Sopenharmony_ci    uint32_t low_val;
2591cb0ef41Sopenharmony_ci    uint32_t high_val;
2601cb0ef41Sopenharmony_ci    BrotliFillBitWindow(br, 16);
2611cb0ef41Sopenharmony_ci    BrotliTakeBits(br, 16, &low_val);
2621cb0ef41Sopenharmony_ci    BrotliFillBitWindow(br, 8);
2631cb0ef41Sopenharmony_ci    BrotliTakeBits(br, n_bits - 16, &high_val);
2641cb0ef41Sopenharmony_ci    return low_val | (high_val << 16);
2651cb0ef41Sopenharmony_ci  }
2661cb0ef41Sopenharmony_ci}
2671cb0ef41Sopenharmony_ci
2681cb0ef41Sopenharmony_ci/* Same as BrotliReadBits24, but allows reading up to 32 bits. */
2691cb0ef41Sopenharmony_cistatic BROTLI_INLINE uint32_t BrotliReadBits32(
2701cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits) {
2711cb0ef41Sopenharmony_ci  BROTLI_DCHECK(n_bits <= 32);
2721cb0ef41Sopenharmony_ci  if (BROTLI_64_BITS || (n_bits <= 16)) {
2731cb0ef41Sopenharmony_ci    uint32_t val;
2741cb0ef41Sopenharmony_ci    BrotliFillBitWindow(br, n_bits);
2751cb0ef41Sopenharmony_ci    BrotliTakeBits(br, n_bits, &val);
2761cb0ef41Sopenharmony_ci    return val;
2771cb0ef41Sopenharmony_ci  } else {
2781cb0ef41Sopenharmony_ci    uint32_t low_val;
2791cb0ef41Sopenharmony_ci    uint32_t high_val;
2801cb0ef41Sopenharmony_ci    BrotliFillBitWindow(br, 16);
2811cb0ef41Sopenharmony_ci    BrotliTakeBits(br, 16, &low_val);
2821cb0ef41Sopenharmony_ci    BrotliFillBitWindow(br, 16);
2831cb0ef41Sopenharmony_ci    BrotliTakeBits(br, n_bits - 16, &high_val);
2841cb0ef41Sopenharmony_ci    return low_val | (high_val << 16);
2851cb0ef41Sopenharmony_ci  }
2861cb0ef41Sopenharmony_ci}
2871cb0ef41Sopenharmony_ci
2881cb0ef41Sopenharmony_ci/* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there
2891cb0ef41Sopenharmony_ci   is not enough input. |n_bits| MUST be positive.
2901cb0ef41Sopenharmony_ci   Up to 24 bits are allowed to be requested from this method. */
2911cb0ef41Sopenharmony_cistatic BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(
2921cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
2931cb0ef41Sopenharmony_ci  BROTLI_DCHECK(n_bits <= 24);
2941cb0ef41Sopenharmony_ci  while (BrotliGetAvailableBits(br) < n_bits) {
2951cb0ef41Sopenharmony_ci    if (!BrotliPullByte(br)) {
2961cb0ef41Sopenharmony_ci      return BROTLI_FALSE;
2971cb0ef41Sopenharmony_ci    }
2981cb0ef41Sopenharmony_ci  }
2991cb0ef41Sopenharmony_ci  BrotliTakeBits(br, n_bits, val);
3001cb0ef41Sopenharmony_ci  return BROTLI_TRUE;
3011cb0ef41Sopenharmony_ci}
3021cb0ef41Sopenharmony_ci
3031cb0ef41Sopenharmony_ci/* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */
3041cb0ef41Sopenharmony_cistatic BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(
3051cb0ef41Sopenharmony_ci    BrotliBitReader* const br, uint32_t n_bits, uint32_t* val) {
3061cb0ef41Sopenharmony_ci  BROTLI_DCHECK(n_bits <= 32);
3071cb0ef41Sopenharmony_ci  if (BROTLI_64_BITS || (n_bits <= 24)) {
3081cb0ef41Sopenharmony_ci    while (BrotliGetAvailableBits(br) < n_bits) {
3091cb0ef41Sopenharmony_ci      if (!BrotliPullByte(br)) {
3101cb0ef41Sopenharmony_ci        return BROTLI_FALSE;
3111cb0ef41Sopenharmony_ci      }
3121cb0ef41Sopenharmony_ci    }
3131cb0ef41Sopenharmony_ci    BrotliTakeBits(br, n_bits, val);
3141cb0ef41Sopenharmony_ci    return BROTLI_TRUE;
3151cb0ef41Sopenharmony_ci  } else {
3161cb0ef41Sopenharmony_ci    return BrotliSafeReadBits32Slow(br, n_bits, val);
3171cb0ef41Sopenharmony_ci  }
3181cb0ef41Sopenharmony_ci}
3191cb0ef41Sopenharmony_ci
3201cb0ef41Sopenharmony_ci/* Advances the bit reader position to the next byte boundary and verifies
3211cb0ef41Sopenharmony_ci   that any skipped bits are set to zero. */
3221cb0ef41Sopenharmony_cistatic BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {
3231cb0ef41Sopenharmony_ci  uint32_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;
3241cb0ef41Sopenharmony_ci  uint32_t pad_bits = 0;
3251cb0ef41Sopenharmony_ci  if (pad_bits_count != 0) {
3261cb0ef41Sopenharmony_ci    BrotliTakeBits(br, pad_bits_count, &pad_bits);
3271cb0ef41Sopenharmony_ci  }
3281cb0ef41Sopenharmony_ci  return TO_BROTLI_BOOL(pad_bits == 0);
3291cb0ef41Sopenharmony_ci}
3301cb0ef41Sopenharmony_ci
3311cb0ef41Sopenharmony_ci/* Copies remaining input bytes stored in the bit reader to the output. Value
3321cb0ef41Sopenharmony_ci   |num| may not be larger than BrotliGetRemainingBytes. The bit reader must be
3331cb0ef41Sopenharmony_ci   warmed up again after this. */
3341cb0ef41Sopenharmony_cistatic BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,
3351cb0ef41Sopenharmony_ci                                          BrotliBitReader* br, size_t num) {
3361cb0ef41Sopenharmony_ci  while (BrotliGetAvailableBits(br) >= 8 && num > 0) {
3371cb0ef41Sopenharmony_ci    *dest = (uint8_t)BrotliGetBitsUnmasked(br);
3381cb0ef41Sopenharmony_ci    BrotliDropBits(br, 8);
3391cb0ef41Sopenharmony_ci    ++dest;
3401cb0ef41Sopenharmony_ci    --num;
3411cb0ef41Sopenharmony_ci  }
3421cb0ef41Sopenharmony_ci  memcpy(dest, br->next_in, num);
3431cb0ef41Sopenharmony_ci  br->avail_in -= num;
3441cb0ef41Sopenharmony_ci  br->next_in += num;
3451cb0ef41Sopenharmony_ci}
3461cb0ef41Sopenharmony_ci
3471cb0ef41Sopenharmony_ci#if defined(__cplusplus) || defined(c_plusplus)
3481cb0ef41Sopenharmony_ci}  /* extern "C" */
3491cb0ef41Sopenharmony_ci#endif
3501cb0ef41Sopenharmony_ci
3511cb0ef41Sopenharmony_ci#endif  /* BROTLI_DEC_BIT_READER_H_ */
352