Path: blob/master/thirdparty/brotli/dec/bit_reader.h
21345 views
/* Copyright 2013 Google Inc. All Rights Reserved.12Distributed under MIT license.3See file LICENSE for detail or copy at https://opensource.org/licenses/MIT4*/56/* Bit reading helpers */78#ifndef BROTLI_DEC_BIT_READER_H_9#define BROTLI_DEC_BIT_READER_H_1011#include "../common/constants.h"12#include "../common/platform.h"1314#if defined(__cplusplus) || defined(c_plusplus)15extern "C" {16#endif1718#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)1920/* 162 bits + 7 bytes */21#define BROTLI_FAST_INPUT_SLACK 282223BROTLI_INTERNAL extern const brotli_reg_t kBrotliBitMask[33];2425static BROTLI_INLINE brotli_reg_t BitMask(brotli_reg_t n) {26if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {27/* Masking with this expression turns to a single28"Unsigned Bit Field Extract" UBFX instruction on ARM. */29return ~(~((brotli_reg_t)0) << n);30} else {31return kBrotliBitMask[n];32}33}3435typedef struct {36brotli_reg_t val_; /* pre-fetched bits */37brotli_reg_t bit_pos_; /* current bit-reading position in val_ */38const uint8_t* next_in; /* the byte we're reading from */39const uint8_t* guard_in; /* position from which "fast-path" is prohibited */40const uint8_t* last_in; /* == next_in + avail_in */41} BrotliBitReader;4243typedef struct {44brotli_reg_t val_;45brotli_reg_t bit_pos_;46const uint8_t* next_in;47size_t avail_in;48} BrotliBitReaderState;4950/* Initializes the BrotliBitReader fields. */51BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* br);5253/* Ensures that accumulator is not empty.54May consume up to sizeof(brotli_reg_t) - 1 bytes of input.55Returns BROTLI_FALSE if data is required but there is no input available.56For !BROTLI_UNALIGNED_READ_FAST this function also prepares bit reader for57aligned reading. */58BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* br);5960/* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden61the main code-path. Never called for RFC brotli streams, required only for62"large-window" mode and other extensions. */63BROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow(64BrotliBitReader* br, brotli_reg_t n_bits, brotli_reg_t* val);6566static BROTLI_INLINE size_t67BrotliBitReaderGetAvailIn(BrotliBitReader* const br) {68return (size_t)(br->last_in - br->next_in);69}7071static BROTLI_INLINE void BrotliBitReaderSaveState(72BrotliBitReader* const from, BrotliBitReaderState* to) {73to->val_ = from->val_;74to->bit_pos_ = from->bit_pos_;75to->next_in = from->next_in;76to->avail_in = BrotliBitReaderGetAvailIn(from);77}7879static BROTLI_INLINE void BrotliBitReaderSetInput(80BrotliBitReader* const br, const uint8_t* next_in, size_t avail_in) {81br->next_in = next_in;82br->last_in = (avail_in == 0) ? next_in : (next_in + avail_in);83if (avail_in + 1 > BROTLI_FAST_INPUT_SLACK) {84br->guard_in = next_in + (avail_in + 1 - BROTLI_FAST_INPUT_SLACK);85} else {86br->guard_in = next_in;87}88}8990static BROTLI_INLINE void BrotliBitReaderRestoreState(91BrotliBitReader* const to, BrotliBitReaderState* from) {92to->val_ = from->val_;93to->bit_pos_ = from->bit_pos_;94to->next_in = from->next_in;95BrotliBitReaderSetInput(to, from->next_in, from->avail_in);96}9798static BROTLI_INLINE brotli_reg_t BrotliGetAvailableBits(99const BrotliBitReader* br) {100return br->bit_pos_;101}102103/* Returns amount of unread bytes the bit reader still has buffered from the104BrotliInput, including whole bytes in br->val_. Result is capped with105maximal ring-buffer size (larger number won't be utilized anyway). */106static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {107static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;108size_t avail_in = BrotliBitReaderGetAvailIn(br);109if (avail_in > kCap) return kCap;110return avail_in + (BrotliGetAvailableBits(br) >> 3);111}112113/* Checks if there is at least |num| bytes left in the input ring-buffer114(excluding the bits remaining in br->val_). */115static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(116BrotliBitReader* const br) {117return TO_BROTLI_BOOL(br->next_in < br->guard_in);118}119120/* Load more bits into accumulator. */121static BROTLI_INLINE brotli_reg_t BrotliBitReaderLoadBits(brotli_reg_t val,122brotli_reg_t new_bits,123brotli_reg_t count,124brotli_reg_t offset) {125BROTLI_DCHECK(126!((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count)));127(void)count;128return val | (new_bits << offset);129}130131/* Guarantees that there are at least |n_bits| + 1 bits in accumulator.132Precondition: accumulator contains at least 1 bit.133|n_bits| should be in the range [1..24] for regular build. For portable134non-64-bit little-endian build only 16 bits are safe to request. */135static BROTLI_INLINE void BrotliFillBitWindow(136BrotliBitReader* const br, brotli_reg_t n_bits) {137#if (BROTLI_64_BITS)138if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&139(n_bits <= 8)) {140brotli_reg_t bit_pos = br->bit_pos_;141if (bit_pos <= 8) {142br->val_ = BrotliBitReaderLoadBits(br->val_,143BROTLI_UNALIGNED_LOAD64LE(br->next_in), 56, bit_pos);144br->bit_pos_ = bit_pos + 56;145br->next_in += 7;146}147} else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&148(n_bits <= 16)) {149brotli_reg_t bit_pos = br->bit_pos_;150if (bit_pos <= 16) {151br->val_ = BrotliBitReaderLoadBits(br->val_,152BROTLI_UNALIGNED_LOAD64LE(br->next_in), 48, bit_pos);153br->bit_pos_ = bit_pos + 48;154br->next_in += 6;155}156} else {157brotli_reg_t bit_pos = br->bit_pos_;158if (bit_pos <= 32) {159br->val_ = BrotliBitReaderLoadBits(br->val_,160(uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in), 32, bit_pos);161br->bit_pos_ = bit_pos + 32;162br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;163}164}165#else166if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&167(n_bits <= 8)) {168brotli_reg_t bit_pos = br->bit_pos_;169if (bit_pos <= 8) {170br->val_ = BrotliBitReaderLoadBits(br->val_,171BROTLI_UNALIGNED_LOAD32LE(br->next_in), 24, bit_pos);172br->bit_pos_ = bit_pos + 24;173br->next_in += 3;174}175} else {176brotli_reg_t bit_pos = br->bit_pos_;177if (bit_pos <= 16) {178br->val_ = BrotliBitReaderLoadBits(br->val_,179(uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in), 16, bit_pos);180br->bit_pos_ = bit_pos + 16;181br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;182}183}184#endif185}186187/* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no188more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */189static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {190BrotliFillBitWindow(br, 17);191}192193/* Tries to pull one byte of input to accumulator.194Returns BROTLI_FALSE if there is no input available. */195static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {196if (br->next_in == br->last_in) {197return BROTLI_FALSE;198}199br->val_ = BrotliBitReaderLoadBits(br->val_,200(brotli_reg_t)*br->next_in, 8, br->bit_pos_);201br->bit_pos_ += 8;202++br->next_in;203return BROTLI_TRUE;204}205206/* Returns currently available bits.207The number of valid bits could be calculated by BrotliGetAvailableBits. */208static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(209BrotliBitReader* const br) {210return br->val_;211}212213/* Like BrotliGetBits, but does not mask the result.214The result contains at least 16 valid bits. */215static BROTLI_INLINE brotli_reg_t BrotliGet16BitsUnmasked(216BrotliBitReader* const br) {217BrotliFillBitWindow(br, 16);218return (brotli_reg_t)BrotliGetBitsUnmasked(br);219}220221/* Returns the specified number of bits from |br| without advancing bit222position. */223static BROTLI_INLINE brotli_reg_t BrotliGetBits(224BrotliBitReader* const br, brotli_reg_t n_bits) {225BrotliFillBitWindow(br, n_bits);226return BrotliGetBitsUnmasked(br) & BitMask(n_bits);227}228229/* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there230is not enough input. */231static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(232BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {233while (BrotliGetAvailableBits(br) < n_bits) {234if (!BrotliPullByte(br)) {235return BROTLI_FALSE;236}237}238*val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);239return BROTLI_TRUE;240}241242/* Advances the bit pos by |n_bits|. */243static BROTLI_INLINE void BrotliDropBits(244BrotliBitReader* const br, brotli_reg_t n_bits) {245br->bit_pos_ -= n_bits;246br->val_ >>= n_bits;247}248249/* Make sure that there are no spectre bits in accumulator.250This is important for the cases when some bytes are skipped251(i.e. never placed into accumulator). */252static BROTLI_INLINE void BrotliBitReaderNormalize(BrotliBitReader* br) {253/* Actually, it is enough to normalize when br->bit_pos_ == 0 */254if (br->bit_pos_ < (sizeof(brotli_reg_t) << 3u)) {255br->val_ &= (((brotli_reg_t)1) << br->bit_pos_) - 1;256}257}258259static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {260brotli_reg_t unused_bytes = BrotliGetAvailableBits(br) >> 3;261brotli_reg_t unused_bits = unused_bytes << 3;262br->next_in =263(unused_bytes == 0) ? br->next_in : (br->next_in - unused_bytes);264br->bit_pos_ -= unused_bits;265BrotliBitReaderNormalize(br);266}267268/* Reads the specified number of bits from |br| and advances the bit pos.269Precondition: accumulator MUST contain at least |n_bits|. */270static BROTLI_INLINE void BrotliTakeBits(BrotliBitReader* const br,271brotli_reg_t n_bits,272brotli_reg_t* val) {273*val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);274BROTLI_LOG(("[BrotliTakeBits] %d %d %d val: %6x\n",275(int)BrotliBitReaderGetAvailIn(br), (int)br->bit_pos_,276(int)n_bits, (int)*val));277BrotliDropBits(br, n_bits);278}279280/* Reads the specified number of bits from |br| and advances the bit pos.281Assumes that there is enough input to perform BrotliFillBitWindow.282Up to 24 bits are allowed to be requested from this method. */283static BROTLI_INLINE brotli_reg_t BrotliReadBits24(284BrotliBitReader* const br, brotli_reg_t n_bits) {285BROTLI_DCHECK(n_bits <= 24);286if (BROTLI_64_BITS || (n_bits <= 16)) {287brotli_reg_t val;288BrotliFillBitWindow(br, n_bits);289BrotliTakeBits(br, n_bits, &val);290return val;291} else {292brotli_reg_t low_val;293brotli_reg_t high_val;294BrotliFillBitWindow(br, 16);295BrotliTakeBits(br, 16, &low_val);296BrotliFillBitWindow(br, 8);297BrotliTakeBits(br, n_bits - 16, &high_val);298return low_val | (high_val << 16);299}300}301302/* Same as BrotliReadBits24, but allows reading up to 32 bits. */303static BROTLI_INLINE brotli_reg_t BrotliReadBits32(304BrotliBitReader* const br, brotli_reg_t n_bits) {305BROTLI_DCHECK(n_bits <= 32);306if (BROTLI_64_BITS || (n_bits <= 16)) {307brotli_reg_t val;308BrotliFillBitWindow(br, n_bits);309BrotliTakeBits(br, n_bits, &val);310return val;311} else {312brotli_reg_t low_val;313brotli_reg_t high_val;314BrotliFillBitWindow(br, 16);315BrotliTakeBits(br, 16, &low_val);316BrotliFillBitWindow(br, 16);317BrotliTakeBits(br, n_bits - 16, &high_val);318return low_val | (high_val << 16);319}320}321322/* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there323is not enough input. |n_bits| MUST be positive.324Up to 24 bits are allowed to be requested from this method. */325static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(326BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {327BROTLI_DCHECK(n_bits <= 24);328while (BrotliGetAvailableBits(br) < n_bits) {329if (!BrotliPullByte(br)) {330return BROTLI_FALSE;331}332}333BrotliTakeBits(br, n_bits, val);334return BROTLI_TRUE;335}336337/* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */338static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(339BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {340BROTLI_DCHECK(n_bits <= 32);341if (BROTLI_64_BITS || (n_bits <= 24)) {342while (BrotliGetAvailableBits(br) < n_bits) {343if (!BrotliPullByte(br)) {344return BROTLI_FALSE;345}346}347BrotliTakeBits(br, n_bits, val);348return BROTLI_TRUE;349} else {350return BrotliSafeReadBits32Slow(br, n_bits, val);351}352}353354/* Advances the bit reader position to the next byte boundary and verifies355that any skipped bits are set to zero. */356static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {357brotli_reg_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;358brotli_reg_t pad_bits = 0;359if (pad_bits_count != 0) {360BrotliTakeBits(br, pad_bits_count, &pad_bits);361}362BrotliBitReaderNormalize(br);363return TO_BROTLI_BOOL(pad_bits == 0);364}365366static BROTLI_INLINE void BrotliDropBytes(BrotliBitReader* br, size_t num) {367/* Check detour is legal: accumulator must to be empty. */368BROTLI_DCHECK(br->bit_pos_ == 0);369BROTLI_DCHECK(br->val_ == 0);370br->next_in += num;371}372373/* Copies remaining input bytes stored in the bit reader to the output. Value374|num| may not be larger than BrotliGetRemainingBytes. The bit reader must be375warmed up again after this. */376static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,377BrotliBitReader* br, size_t num) {378while (BrotliGetAvailableBits(br) >= 8 && num > 0) {379*dest = (uint8_t)BrotliGetBitsUnmasked(br);380BrotliDropBits(br, 8);381++dest;382--num;383}384BrotliBitReaderNormalize(br);385if (num > 0) {386memcpy(dest, br->next_in, num);387BrotliDropBytes(br, num);388}389}390391BROTLI_UNUSED_FUNCTION void BrotliBitReaderSuppressUnusedFunctions(void) {392BROTLI_UNUSED(&BrotliBitReaderSuppressUnusedFunctions);393394BROTLI_UNUSED(&BrotliBitReaderGetAvailIn);395BROTLI_UNUSED(&BrotliBitReaderLoadBits);396BROTLI_UNUSED(&BrotliBitReaderRestoreState);397BROTLI_UNUSED(&BrotliBitReaderSaveState);398BROTLI_UNUSED(&BrotliBitReaderSetInput);399BROTLI_UNUSED(&BrotliBitReaderUnload);400BROTLI_UNUSED(&BrotliCheckInputAmount);401BROTLI_UNUSED(&BrotliCopyBytes);402BROTLI_UNUSED(&BrotliFillBitWindow16);403BROTLI_UNUSED(&BrotliGet16BitsUnmasked);404BROTLI_UNUSED(&BrotliGetBits);405BROTLI_UNUSED(&BrotliGetRemainingBytes);406BROTLI_UNUSED(&BrotliJumpToByteBoundary);407BROTLI_UNUSED(&BrotliReadBits24);408BROTLI_UNUSED(&BrotliReadBits32);409BROTLI_UNUSED(&BrotliSafeGetBits);410BROTLI_UNUSED(&BrotliSafeReadBits);411BROTLI_UNUSED(&BrotliSafeReadBits32);412}413414#if defined(__cplusplus) || defined(c_plusplus)415} /* extern "C" */416#endif417418#endif /* BROTLI_DEC_BIT_READER_H_ */419420421