Path: blob/master/thirdparty/brotli/dec/bit_reader.c
21318 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#include "bit_reader.h"910#include "../common/platform.h"1112#if defined(__cplusplus) || defined(c_plusplus)13extern "C" {14#endif1516const BROTLI_MODEL("small")17brotli_reg_t kBrotliBitMask[33] = { 0x00000000,180x00000001, 0x00000003, 0x00000007, 0x0000000F,190x0000001F, 0x0000003F, 0x0000007F, 0x000000FF,200x000001FF, 0x000003FF, 0x000007FF, 0x00000FFF,210x00001FFF, 0x00003FFF, 0x00007FFF, 0x0000FFFF,220x0001FFFF, 0x0003FFFF, 0x0007FFFF, 0x000FFFFF,230x001FFFFF, 0x003FFFFF, 0x007FFFFF, 0x00FFFFFF,240x01FFFFFF, 0x03FFFFFF, 0x07FFFFFF, 0x0FFFFFFF,250x1FFFFFFF, 0x3FFFFFFF, 0x7FFFFFFF, 0xFFFFFFFF26};2728void BrotliInitBitReader(BrotliBitReader* const br) {29br->val_ = 0;30br->bit_pos_ = 0;31}3233BROTLI_BOOL BrotliWarmupBitReader(BrotliBitReader* const br) {34size_t aligned_read_mask = (sizeof(br->val_) >> 1) - 1;35/* Fixing alignment after unaligned BrotliFillWindow would result accumulator36overflow. If unalignment is caused by BrotliSafeReadBits, then there is37enough space in accumulator to fix alignment. */38if (BROTLI_UNALIGNED_READ_FAST) {39aligned_read_mask = 0;40}41if (BrotliGetAvailableBits(br) == 0) {42br->val_ = 0;43if (!BrotliPullByte(br)) {44return BROTLI_FALSE;45}46}4748while ((((size_t)br->next_in) & aligned_read_mask) != 0) {49if (!BrotliPullByte(br)) {50/* If we consumed all the input, we don't care about the alignment. */51return BROTLI_TRUE;52}53}54return BROTLI_TRUE;55}5657BROTLI_BOOL BrotliSafeReadBits32Slow(BrotliBitReader* const br,58brotli_reg_t n_bits, brotli_reg_t* val) {59brotli_reg_t low_val;60brotli_reg_t high_val;61BrotliBitReaderState memento;62BROTLI_DCHECK(n_bits <= 32);63BROTLI_DCHECK(n_bits > 24);64BrotliBitReaderSaveState(br, &memento);65if (!BrotliSafeReadBits(br, 16, &low_val) ||66!BrotliSafeReadBits(br, n_bits - 16, &high_val)) {67BrotliBitReaderRestoreState(br, &memento);68return BROTLI_FALSE;69}70*val = low_val | (high_val << 16);71return BROTLI_TRUE;72}7374#if defined(__cplusplus) || defined(c_plusplus)75} /* extern "C" */76#endif777879