Path: blob/master/thirdparty/brotli/dec/bit_reader.h
10278 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 <string.h> /* memcpy */1213#include <brotli/types.h>1415#include "../common/constants.h"16#include "../common/platform.h"1718#if defined(__cplusplus) || defined(c_plusplus)19extern "C" {20#endif2122#define BROTLI_SHORT_FILL_BIT_WINDOW_READ (sizeof(brotli_reg_t) >> 1)2324/* 162 bits + 7 bytes */25#define BROTLI_FAST_INPUT_SLACK 282627BROTLI_INTERNAL extern const brotli_reg_t kBrotliBitMask[33];2829static BROTLI_INLINE brotli_reg_t BitMask(brotli_reg_t n) {30if (BROTLI_IS_CONSTANT(n) || BROTLI_HAS_UBFX) {31/* Masking with this expression turns to a single32"Unsigned Bit Field Extract" UBFX instruction on ARM. */33return ~(~((brotli_reg_t)0) << n);34} else {35return kBrotliBitMask[n];36}37}3839typedef struct {40brotli_reg_t val_; /* pre-fetched bits */41brotli_reg_t bit_pos_; /* current bit-reading position in val_ */42const uint8_t* next_in; /* the byte we're reading from */43const uint8_t* guard_in; /* position from which "fast-path" is prohibited */44const uint8_t* last_in; /* == next_in + avail_in */45} BrotliBitReader;4647typedef struct {48brotli_reg_t val_;49brotli_reg_t bit_pos_;50const uint8_t* next_in;51size_t avail_in;52} BrotliBitReaderState;5354/* Initializes the BrotliBitReader fields. */55BROTLI_INTERNAL void BrotliInitBitReader(BrotliBitReader* br);5657/* Ensures that accumulator is not empty.58May consume up to sizeof(brotli_reg_t) - 1 bytes of input.59Returns BROTLI_FALSE if data is required but there is no input available.60For !BROTLI_UNALIGNED_READ_FAST this function also prepares bit reader for61aligned reading. */62BROTLI_INTERNAL BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* br);6364/* Fallback for BrotliSafeReadBits32. Extracted as noninlined method to unburden65the main code-path. Never called for RFC brotli streams, required only for66"large-window" mode and other extensions. */67BROTLI_INTERNAL BROTLI_NOINLINE BROTLI_BOOL BrotliSafeReadBits32Slow(68BrotliBitReader* br, brotli_reg_t n_bits, brotli_reg_t* val);6970static BROTLI_INLINE size_t71BrotliBitReaderGetAvailIn(BrotliBitReader* const br) {72return (size_t)(br->last_in - br->next_in);73}7475static BROTLI_INLINE void BrotliBitReaderSaveState(76BrotliBitReader* const from, BrotliBitReaderState* to) {77to->val_ = from->val_;78to->bit_pos_ = from->bit_pos_;79to->next_in = from->next_in;80to->avail_in = BrotliBitReaderGetAvailIn(from);81}8283static BROTLI_INLINE void BrotliBitReaderSetInput(84BrotliBitReader* const br, const uint8_t* next_in, size_t avail_in) {85br->next_in = next_in;86br->last_in = (avail_in == 0) ? next_in : (next_in + avail_in);87if (avail_in + 1 > BROTLI_FAST_INPUT_SLACK) {88br->guard_in = next_in + (avail_in + 1 - BROTLI_FAST_INPUT_SLACK);89} else {90br->guard_in = next_in;91}92}9394static BROTLI_INLINE void BrotliBitReaderRestoreState(95BrotliBitReader* const to, BrotliBitReaderState* from) {96to->val_ = from->val_;97to->bit_pos_ = from->bit_pos_;98to->next_in = from->next_in;99BrotliBitReaderSetInput(to, from->next_in, from->avail_in);100}101102static BROTLI_INLINE brotli_reg_t BrotliGetAvailableBits(103const BrotliBitReader* br) {104return br->bit_pos_;105}106107/* Returns amount of unread bytes the bit reader still has buffered from the108BrotliInput, including whole bytes in br->val_. Result is capped with109maximal ring-buffer size (larger number won't be utilized anyway). */110static BROTLI_INLINE size_t BrotliGetRemainingBytes(BrotliBitReader* br) {111static const size_t kCap = (size_t)1 << BROTLI_LARGE_MAX_WBITS;112size_t avail_in = BrotliBitReaderGetAvailIn(br);113if (avail_in > kCap) return kCap;114return avail_in + (BrotliGetAvailableBits(br) >> 3);115}116117/* Checks if there is at least |num| bytes left in the input ring-buffer118(excluding the bits remaining in br->val_). */119static BROTLI_INLINE BROTLI_BOOL BrotliCheckInputAmount(120BrotliBitReader* const br) {121return TO_BROTLI_BOOL(br->next_in < br->guard_in);122}123124/* Load more bits into accumulator. */125static BROTLI_INLINE brotli_reg_t BrotliBitReaderLoadBits(brotli_reg_t val,126brotli_reg_t new_bits,127brotli_reg_t count,128brotli_reg_t offset) {129BROTLI_DCHECK(130!((val >> offset) & ~new_bits & ~(~((brotli_reg_t)0) << count)));131(void)count;132return val | (new_bits << offset);133}134135/* Guarantees that there are at least |n_bits| + 1 bits in accumulator.136Precondition: accumulator contains at least 1 bit.137|n_bits| should be in the range [1..24] for regular build. For portable138non-64-bit little-endian build only 16 bits are safe to request. */139static BROTLI_INLINE void BrotliFillBitWindow(140BrotliBitReader* const br, brotli_reg_t n_bits) {141#if (BROTLI_64_BITS)142if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&143(n_bits <= 8)) {144brotli_reg_t bit_pos = br->bit_pos_;145if (bit_pos <= 8) {146br->val_ = BrotliBitReaderLoadBits(br->val_,147BROTLI_UNALIGNED_LOAD64LE(br->next_in), 56, bit_pos);148br->bit_pos_ = bit_pos + 56;149br->next_in += 7;150}151} else if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&152(n_bits <= 16)) {153brotli_reg_t bit_pos = br->bit_pos_;154if (bit_pos <= 16) {155br->val_ = BrotliBitReaderLoadBits(br->val_,156BROTLI_UNALIGNED_LOAD64LE(br->next_in), 48, bit_pos);157br->bit_pos_ = bit_pos + 48;158br->next_in += 6;159}160} else {161brotli_reg_t bit_pos = br->bit_pos_;162if (bit_pos <= 32) {163br->val_ = BrotliBitReaderLoadBits(br->val_,164(uint64_t)BROTLI_UNALIGNED_LOAD32LE(br->next_in), 32, bit_pos);165br->bit_pos_ = bit_pos + 32;166br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;167}168}169#else170if (BROTLI_UNALIGNED_READ_FAST && BROTLI_IS_CONSTANT(n_bits) &&171(n_bits <= 8)) {172brotli_reg_t bit_pos = br->bit_pos_;173if (bit_pos <= 8) {174br->val_ = BrotliBitReaderLoadBits(br->val_,175BROTLI_UNALIGNED_LOAD32LE(br->next_in), 24, bit_pos);176br->bit_pos_ = bit_pos + 24;177br->next_in += 3;178}179} else {180brotli_reg_t bit_pos = br->bit_pos_;181if (bit_pos <= 16) {182br->val_ = BrotliBitReaderLoadBits(br->val_,183(uint32_t)BROTLI_UNALIGNED_LOAD16LE(br->next_in), 16, bit_pos);184br->bit_pos_ = bit_pos + 16;185br->next_in += BROTLI_SHORT_FILL_BIT_WINDOW_READ;186}187}188#endif189}190191/* Mostly like BrotliFillBitWindow, but guarantees only 16 bits and reads no192more than BROTLI_SHORT_FILL_BIT_WINDOW_READ bytes of input. */193static BROTLI_INLINE void BrotliFillBitWindow16(BrotliBitReader* const br) {194BrotliFillBitWindow(br, 17);195}196197/* Tries to pull one byte of input to accumulator.198Returns BROTLI_FALSE if there is no input available. */199static BROTLI_INLINE BROTLI_BOOL BrotliPullByte(BrotliBitReader* const br) {200if (br->next_in == br->last_in) {201return BROTLI_FALSE;202}203br->val_ = BrotliBitReaderLoadBits(br->val_,204(brotli_reg_t)*br->next_in, 8, br->bit_pos_);205br->bit_pos_ += 8;206++br->next_in;207return BROTLI_TRUE;208}209210/* Returns currently available bits.211The number of valid bits could be calculated by BrotliGetAvailableBits. */212static BROTLI_INLINE brotli_reg_t BrotliGetBitsUnmasked(213BrotliBitReader* const br) {214return br->val_;215}216217/* Like BrotliGetBits, but does not mask the result.218The result contains at least 16 valid bits. */219static BROTLI_INLINE brotli_reg_t BrotliGet16BitsUnmasked(220BrotliBitReader* const br) {221BrotliFillBitWindow(br, 16);222return (brotli_reg_t)BrotliGetBitsUnmasked(br);223}224225/* Returns the specified number of bits from |br| without advancing bit226position. */227static BROTLI_INLINE brotli_reg_t BrotliGetBits(228BrotliBitReader* const br, brotli_reg_t n_bits) {229BrotliFillBitWindow(br, n_bits);230return BrotliGetBitsUnmasked(br) & BitMask(n_bits);231}232233/* Tries to peek the specified amount of bits. Returns BROTLI_FALSE, if there234is not enough input. */235static BROTLI_INLINE BROTLI_BOOL BrotliSafeGetBits(236BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {237while (BrotliGetAvailableBits(br) < n_bits) {238if (!BrotliPullByte(br)) {239return BROTLI_FALSE;240}241}242*val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);243return BROTLI_TRUE;244}245246/* Advances the bit pos by |n_bits|. */247static BROTLI_INLINE void BrotliDropBits(248BrotliBitReader* const br, brotli_reg_t n_bits) {249br->bit_pos_ -= n_bits;250br->val_ >>= n_bits;251}252253/* Make sure that there are no spectre bits in accumulator.254This is important for the cases when some bytes are skipped255(i.e. never placed into accumulator). */256static BROTLI_INLINE void BrotliBitReaderNormalize(BrotliBitReader* br) {257/* Actually, it is enough to normalize when br->bit_pos_ == 0 */258if (br->bit_pos_ < (sizeof(brotli_reg_t) << 3u)) {259br->val_ &= (((brotli_reg_t)1) << br->bit_pos_) - 1;260}261}262263static BROTLI_INLINE void BrotliBitReaderUnload(BrotliBitReader* br) {264brotli_reg_t unused_bytes = BrotliGetAvailableBits(br) >> 3;265brotli_reg_t unused_bits = unused_bytes << 3;266br->next_in =267(unused_bytes == 0) ? br->next_in : (br->next_in - unused_bytes);268br->bit_pos_ -= unused_bits;269BrotliBitReaderNormalize(br);270}271272/* Reads the specified number of bits from |br| and advances the bit pos.273Precondition: accumulator MUST contain at least |n_bits|. */274static BROTLI_INLINE void BrotliTakeBits(BrotliBitReader* const br,275brotli_reg_t n_bits,276brotli_reg_t* val) {277*val = BrotliGetBitsUnmasked(br) & BitMask(n_bits);278BROTLI_LOG(("[BrotliTakeBits] %d %d %d val: %6x\n",279(int)BrotliBitReaderGetAvailIn(br), (int)br->bit_pos_,280(int)n_bits, (int)*val));281BrotliDropBits(br, n_bits);282}283284/* Reads the specified number of bits from |br| and advances the bit pos.285Assumes that there is enough input to perform BrotliFillBitWindow.286Up to 24 bits are allowed to be requested from this method. */287static BROTLI_INLINE brotli_reg_t BrotliReadBits24(288BrotliBitReader* const br, brotli_reg_t n_bits) {289BROTLI_DCHECK(n_bits <= 24);290if (BROTLI_64_BITS || (n_bits <= 16)) {291brotli_reg_t val;292BrotliFillBitWindow(br, n_bits);293BrotliTakeBits(br, n_bits, &val);294return val;295} else {296brotli_reg_t low_val;297brotli_reg_t high_val;298BrotliFillBitWindow(br, 16);299BrotliTakeBits(br, 16, &low_val);300BrotliFillBitWindow(br, 8);301BrotliTakeBits(br, n_bits - 16, &high_val);302return low_val | (high_val << 16);303}304}305306/* Same as BrotliReadBits24, but allows reading up to 32 bits. */307static BROTLI_INLINE brotli_reg_t BrotliReadBits32(308BrotliBitReader* const br, brotli_reg_t n_bits) {309BROTLI_DCHECK(n_bits <= 32);310if (BROTLI_64_BITS || (n_bits <= 16)) {311brotli_reg_t val;312BrotliFillBitWindow(br, n_bits);313BrotliTakeBits(br, n_bits, &val);314return val;315} else {316brotli_reg_t low_val;317brotli_reg_t high_val;318BrotliFillBitWindow(br, 16);319BrotliTakeBits(br, 16, &low_val);320BrotliFillBitWindow(br, 16);321BrotliTakeBits(br, n_bits - 16, &high_val);322return low_val | (high_val << 16);323}324}325326/* Tries to read the specified amount of bits. Returns BROTLI_FALSE, if there327is not enough input. |n_bits| MUST be positive.328Up to 24 bits are allowed to be requested from this method. */329static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits(330BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {331BROTLI_DCHECK(n_bits <= 24);332while (BrotliGetAvailableBits(br) < n_bits) {333if (!BrotliPullByte(br)) {334return BROTLI_FALSE;335}336}337BrotliTakeBits(br, n_bits, val);338return BROTLI_TRUE;339}340341/* Same as BrotliSafeReadBits, but allows reading up to 32 bits. */342static BROTLI_INLINE BROTLI_BOOL BrotliSafeReadBits32(343BrotliBitReader* const br, brotli_reg_t n_bits, brotli_reg_t* val) {344BROTLI_DCHECK(n_bits <= 32);345if (BROTLI_64_BITS || (n_bits <= 24)) {346while (BrotliGetAvailableBits(br) < n_bits) {347if (!BrotliPullByte(br)) {348return BROTLI_FALSE;349}350}351BrotliTakeBits(br, n_bits, val);352return BROTLI_TRUE;353} else {354return BrotliSafeReadBits32Slow(br, n_bits, val);355}356}357358/* Advances the bit reader position to the next byte boundary and verifies359that any skipped bits are set to zero. */360static BROTLI_INLINE BROTLI_BOOL BrotliJumpToByteBoundary(BrotliBitReader* br) {361brotli_reg_t pad_bits_count = BrotliGetAvailableBits(br) & 0x7;362brotli_reg_t pad_bits = 0;363if (pad_bits_count != 0) {364BrotliTakeBits(br, pad_bits_count, &pad_bits);365}366BrotliBitReaderNormalize(br);367return TO_BROTLI_BOOL(pad_bits == 0);368}369370static BROTLI_INLINE void BrotliDropBytes(BrotliBitReader* br, size_t num) {371/* Check detour is legal: accumulator must to be empty. */372BROTLI_DCHECK(br->bit_pos_ == 0);373BROTLI_DCHECK(br->val_ == 0);374br->next_in += num;375}376377/* Copies remaining input bytes stored in the bit reader to the output. Value378|num| may not be larger than BrotliGetRemainingBytes. The bit reader must be379warmed up again after this. */380static BROTLI_INLINE void BrotliCopyBytes(uint8_t* dest,381BrotliBitReader* br, size_t num) {382while (BrotliGetAvailableBits(br) >= 8 && num > 0) {383*dest = (uint8_t)BrotliGetBitsUnmasked(br);384BrotliDropBits(br, 8);385++dest;386--num;387}388BrotliBitReaderNormalize(br);389if (num > 0) {390memcpy(dest, br->next_in, num);391BrotliDropBytes(br, num);392}393}394395BROTLI_UNUSED_FUNCTION void BrotliBitReaderSuppressUnusedFunctions(void) {396BROTLI_UNUSED(&BrotliBitReaderSuppressUnusedFunctions);397398BROTLI_UNUSED(&BrotliBitReaderGetAvailIn);399BROTLI_UNUSED(&BrotliBitReaderLoadBits);400BROTLI_UNUSED(&BrotliBitReaderRestoreState);401BROTLI_UNUSED(&BrotliBitReaderSaveState);402BROTLI_UNUSED(&BrotliBitReaderSetInput);403BROTLI_UNUSED(&BrotliBitReaderUnload);404BROTLI_UNUSED(&BrotliCheckInputAmount);405BROTLI_UNUSED(&BrotliCopyBytes);406BROTLI_UNUSED(&BrotliFillBitWindow16);407BROTLI_UNUSED(&BrotliGet16BitsUnmasked);408BROTLI_UNUSED(&BrotliGetBits);409BROTLI_UNUSED(&BrotliGetRemainingBytes);410BROTLI_UNUSED(&BrotliJumpToByteBoundary);411BROTLI_UNUSED(&BrotliReadBits24);412BROTLI_UNUSED(&BrotliReadBits32);413BROTLI_UNUSED(&BrotliSafeGetBits);414BROTLI_UNUSED(&BrotliSafeReadBits);415BROTLI_UNUSED(&BrotliSafeReadBits32);416}417418#if defined(__cplusplus) || defined(c_plusplus)419} /* extern "C" */420#endif421422#endif /* BROTLI_DEC_BIT_READER_H_ */423424425