#![allow(non_snake_case)]
#![allow(unused_parens)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#![allow(unused_macros)]
use core;
use super::alloc;
pub use alloc::{AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator};
use core::mem;
use super::bit_reader;
use super::huffman;
use super::state;
use super::prefix;
use super::transform::{TransformDictionaryWord, copy_bytes, kNumTransforms};
use state::{BlockTypeAndLengthState, BrotliRunningContextMapState, BrotliRunningDecodeUint8State,
BrotliRunningHuffmanState, BrotliRunningMetablockHeaderState,
BrotliRunningReadBlockLengthState, BrotliRunningState, BrotliRunningTreeGroupState,
BrotliRunningUncompressedState, kLiteralContextBits,
BrotliDecoderErrorCode, COMPOUND_DICTIONARY_BLOCK_MAP_UNINITIALIZED,
};
use context::{kContextLookup};
use ::dictionary::{kBrotliDictionary, kBrotliDictionaryOffsetsByLength,
kBrotliDictionarySizeBitsByLength, kBrotliMaxDictionaryWordLength,
kBrotliMinDictionaryWordLength};
use ::shared_dictionary::{SHARED_BROTLI_MIN_DICTIONARY_WORD_LENGTH,
SHARED_BROTLI_MAX_DICTIONARY_WORD_LENGTH,
SHARED_BROTLI_MAX_TRANSFORM_AFFIX_LENGTH,
DictionaryLookupError};
pub use huffman::{HuffmanCode, HuffmanTreeGroup};
use huffman::{HuffmanTable, LITERAL_TABLE_WINDOW, INSERT_COPY_TABLE_WINDOW, DISTANCE_ROOT_WINDOW};
#[repr(C)]
#[derive(Debug)]
pub enum BrotliResult {
ResultSuccess = 1,
NeedsMoreInput = 2,
NeedsMoreOutput = 3,
ResultFailure = 0,
}
const kBrotliWindowGap: u32 = 16;
const kBrotliLargeMinWbits: u32 = 10;
const kBrotliLargeMaxWbits: u32 = 30;
const kBrotliMaxPostfix: usize = 3;
const kBrotliMaxAllowedDistance: u32 = 0x7FFFFFFC;
const kDefaultCodeLength: u32 = 8;
const kCodeLengthRepeatCode: u32 = 16;
pub const kNumLiteralCodes: u16 = 256;
pub const kNumInsertAndCopyCodes: u16 = 704;
pub const kNumBlockLengthCodes: u32 = 26;
const kDistanceContextBits: i32 = 2;
const HUFFMAN_TABLE_BITS: u32 = 8;
const HUFFMAN_TABLE_MASK: u32 = 0xff;
const CODE_LENGTH_CODES: usize = 18;
const kCodeLengthCodeOrder: [u8; CODE_LENGTH_CODES] = [1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10,
11, 12, 13, 14, 15];
const kCodeLengthPrefixLength: [u8; 16] = [2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4];
const kCodeLengthPrefixValue: [u8; 16] = [0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5];
macro_rules! BROTLI_LOG_UINT (
($num : expr) => {
xprintln!("{:?} = {:?}", stringify!($num), $num)
};
);
macro_rules! BROTLI_LOG (
($str : expr, $num : expr) => {xprintln!("{:?} {:?}", $str, $num);};
($str : expr, $num0 : expr, $num1 : expr) => {xprintln!("{:?} {:?} {:?}", $str, $num0, $num1);};
($str : expr, $num0 : expr, $num1 : expr, $num2 : expr) => {
xprintln!("{:?} {:?} {:?} {:?}", $str, $num0, $num1, $num2);
};
($str : expr, $num0 : expr, $num1 : expr, $num2 : expr, $num3 : expr) => {
xprintln!("{:?} {:?} {:?} {:?} {:?}", $str, $num0, $num1, $num2, $num3);
};
);
fn is_fatal(e: BrotliDecoderErrorCode) -> bool {
(e as i64) < 0
}
fn assign_error_code(output: &mut BrotliDecoderErrorCode, input: BrotliDecoderErrorCode) -> BrotliDecoderErrorCode {
*output = input;
input
}
#[allow(non_snake_case)]
macro_rules! SaveErrorCode {
($state: expr, $e:expr) => {
match assign_error_code(&mut $state.error_code, $e) {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS =>
BrotliResult::ResultSuccess,
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT =>
BrotliResult::NeedsMoreInput,
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT =>
BrotliResult::NeedsMoreOutput,
_ =>
BrotliResult::ResultFailure,
}
}
}
macro_rules! SaveResult {
($state: expr, $e:expr) => {
match ($state.error_code = match $e {
BrotliResult::ResultSuccess => BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS,
BrotliResult::NeedsMoreInput => BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT,
BrotliResult::NeedsMoreOutput => BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT,
BrotliResult::ResultFailure => BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE,
}) {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS =>
BrotliResult::ResultSuccess,
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT =>
BrotliResult::NeedsMoreInput,
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT =>
BrotliResult::NeedsMoreOutput,
_ =>
BrotliResult::ResultFailure,
}
}
}
macro_rules! BROTLI_LOG_ARRAY_INDEX (
($array : expr, $index : expr) => {
xprintln!("{:?}[{:?}] = {:?}", stringify!($array), $index, $array[$index as usize])
};
);
const NUM_DISTANCE_SHORT_CODES: u32 = 16;
pub const BROTLI_MAX_DISTANCE_BITS:u32 = 24;
pub const BROTLI_LARGE_MAX_DISTANCE_BITS: u32 = 62;
pub fn BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX: u32, NDIRECT:u32, MAXNBITS: u32) -> u32 {
NUM_DISTANCE_SHORT_CODES + (NDIRECT) +
((MAXNBITS) << ((NPOSTFIX) + 1))
}
pub use state::BrotliState;
fn DecodeWindowBits(s_large_window: &mut bool,
s_window_bits:&mut u32,
br: &mut bit_reader::BrotliBitReader) -> BrotliDecoderErrorCode {
let mut n: u32 = 0;
let large_window = *s_large_window;
*s_large_window = false;
bit_reader::BrotliTakeBits(br, 1, &mut n);
if (n == 0) {
*s_window_bits = 16;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
bit_reader::BrotliTakeBits(br, 3, &mut n);
if (n != 0) {
*s_window_bits = 17 + n;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
bit_reader::BrotliTakeBits(br, 3, &mut n);
if (n == 1) {
if (large_window) {
bit_reader::BrotliTakeBits(br, 1, &mut n);
if (n == 1) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
}
*s_large_window = true;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
} else {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
}
}
if (n != 0) {
*s_window_bits = 8 + n;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
*s_window_bits = 17;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
#[cold]
fn mark_unlikely() {}
fn DecodeVarLenUint8(substate_decode_uint8: &mut state::BrotliRunningDecodeUint8State,
mut br: &mut bit_reader::BrotliBitReader,
value: &mut u32,
input: &[u8])
-> BrotliDecoderErrorCode {
let mut bits: u32 = 0;
loop {
match *substate_decode_uint8 {
BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_NONE => {
if !bit_reader::BrotliSafeReadBits(&mut br, 1, &mut bits, input) {
mark_unlikely();
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (bits == 0) {
*value = 0;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
*substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_SHORT;
}
BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_SHORT => {
if !bit_reader::BrotliSafeReadBits(&mut br, 3, &mut bits, input) {
mark_unlikely();
*substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_SHORT;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (bits == 0) {
*value = 1;
*substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
*value = bits;
*substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_LONG;
}
BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_LONG => {
if !bit_reader::BrotliSafeReadBits(&mut br, *value, &mut bits, input) {
mark_unlikely();
*substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_LONG;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
*value = (1u32 << *value) + bits;
*substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
}
}
}
fn DecodeMetaBlockLength<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
let mut bits: u32 = 0;
loop {
match s.substate_metablock_header {
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE => {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
s.is_last_metablock = bits as u8;
s.meta_block_remaining_len = 0;
s.is_uncompressed = 0;
s.is_metadata = 0;
if (s.is_last_metablock == 0) {
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
continue;
}
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_EMPTY;
}
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_EMPTY => {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if bits != 0 {
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
}
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES => {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
s.size_nibbles = (bits + 4) as u8;
s.loop_counter = 0;
if (bits == 3) {
s.is_metadata = 1;
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_RESERVED;
continue;
}
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_SIZE;
}
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_SIZE => {
let mut i = s.loop_counter;
while i < s.size_nibbles as i32 {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 4, &mut bits, input) {
s.loop_counter = i;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (i + 1 == s.size_nibbles as i32 && s.size_nibbles > 4 && bits == 0) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE;
}
s.meta_block_remaining_len |= (bits << (i * 4)) as i32;
i += 1;
}
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
}
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED => {
if (s.is_last_metablock == 0 && s.is_metadata == 0) {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
s.is_uncompressed = bits as u8;
}
s.meta_block_remaining_len += 1;
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_RESERVED => {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (bits != 0) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_RESERVED;
}
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_BYTES;
}
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_BYTES => {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (bits == 0) {
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
s.size_nibbles = bits as u8;
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_METADATA;
}
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_METADATA => {
let mut i = s.loop_counter;
while i < s.size_nibbles as i32 {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 8, &mut bits, input) {
s.loop_counter = i;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (i + 1 == s.size_nibbles as i32 && s.size_nibbles > 1 && bits == 0) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE;
}
s.meta_block_remaining_len |= (bits << (i * 8)) as i32;
i += 1;
}
s.substate_metablock_header =
BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
continue;
}
}
}
}
#[inline(always)]
fn DecodeSymbol<T: HuffmanTable + ?Sized>(bits: u32, table: &T, br: &mut bit_reader::BrotliBitReader) -> u32 {
let mut table_index = bits & HUFFMAN_TABLE_MASK;
let mut table_element = table.entry(table_index as usize);
if table_element.bits > HUFFMAN_TABLE_BITS as u8 {
mark_unlikely();
let nbits = table_element.bits - HUFFMAN_TABLE_BITS as u8;
bit_reader::BrotliDropBits(br, HUFFMAN_TABLE_BITS);
table_index += table_element.value as u32;
let ext_idx = (table_index
+ ((bits >> HUFFMAN_TABLE_BITS)
& bit_reader::BitMask(nbits as u32))) as usize;
table_element = table.entry(ext_idx);
}
bit_reader::BrotliDropBits(br, table_element.bits as u32);
table_element.value as u32
}
#[inline(always)]
fn ReadSymbol<T: HuffmanTable + ?Sized>(table: &T, br: &mut bit_reader::BrotliBitReader, input: &[u8]) -> u32 {
DecodeSymbol(bit_reader::BrotliGet16BitsUnmasked(br, input), table, br)
}
#[cold]
fn SafeDecodeSymbol<T: HuffmanTable + ?Sized>(table: &T,
mut br: &mut bit_reader::BrotliBitReader,
result: &mut u32)
-> bool {
let mut available_bits = bit_reader::BrotliGetAvailableBits(br);
if (available_bits == 0) {
let root0 = table.entry(0);
if (root0.bits == 0) {
*result = root0.value as u32;
return true;
}
return false;
}
let mut val = bit_reader::BrotliGetBitsUnmasked(br) as u32;
let table_index = (val & HUFFMAN_TABLE_MASK) as usize;
let table_element = table.entry(table_index);
if (table_element.bits <= HUFFMAN_TABLE_BITS as u8) {
if (table_element.bits as u32 <= available_bits) {
bit_reader::BrotliDropBits(&mut br, table_element.bits as u32);
*result = table_element.value as u32;
return true;
} else {
return false;
}
}
if (available_bits <= HUFFMAN_TABLE_BITS) {
return false;
}
val = (val & bit_reader::BitMask(table_element.bits as u32)) >> HUFFMAN_TABLE_BITS;
available_bits -= HUFFMAN_TABLE_BITS;
let table_sub_element =
table.entry(table_index + table_element.value as usize + val as usize);
if (available_bits < table_sub_element.bits as u32) {
return false;
}
bit_reader::BrotliDropBits(&mut br, HUFFMAN_TABLE_BITS + table_sub_element.bits as u32);
*result = table_sub_element.value as u32;
true
}
#[inline(always)]
fn SafeReadSymbol<T: HuffmanTable + ?Sized>(table: &T,
br: &mut bit_reader::BrotliBitReader,
result: &mut u32,
input: &[u8])
-> bool {
let mut val: u32 = 0;
if (bit_reader::BrotliSafeGetBits(br, 15, &mut val, input)) {
*result = DecodeSymbol(val, table, br);
return true;
} else {
mark_unlikely();
}
SafeDecodeSymbol(table, br, result)
}
static EMPTY_LITERAL_TABLE: LiteralTable =
[HuffmanCode { value: 0, bits: 0 }; LITERAL_TABLE_WINDOW];
static EMPTY_CONTEXT_TREES: [&'static LiteralTable; 64] = [&EMPTY_LITERAL_TABLE; 64];
static EMPTY_DISTANCE_ROOT_TABLE: DistanceRootTable =
[HuffmanCode { value: 0, bits: 0 }; DISTANCE_ROOT_WINDOW];
static EMPTY_DISTANCE_ROOTS: [&'static DistanceRootTable; 4] = [&EMPTY_DISTANCE_ROOT_TABLE; 4];
#[inline(always)]
fn decode_symbol_bits<T: HuffmanTable + ?Sized>(bits: u32, table: &T) -> (u32, u32) {
let table_index = (bits & HUFFMAN_TABLE_MASK) as usize;
let table_element = table.entry(table_index);
if table_element.bits > HUFFMAN_TABLE_BITS as u8 {
mark_unlikely();
let sub_idx = table_index
+ table_element.value as usize
+ (((bits & bit_reader::BitMask(table_element.bits as u32)) >> HUFFMAN_TABLE_BITS) as usize);
let sub = table.entry(sub_idx);
return (sub.value as u32, HUFFMAN_TABLE_BITS + (sub.bits as u32 & 7));
}
(table_element.value as u32, table_element.bits as u32)
}
#[inline(always)]
fn decode_symbol_no_fill<T: HuffmanTable + ?Sized>(
fbr: &mut bit_reader::BitReaderSlice,
table: &T,
) -> u32 {
let (sym, consumed) = decode_symbol_bits(fbr.val as u32, table);
fbr.avail_bits = fbr.avail_bits.wrapping_sub(consumed);
fbr.val >>= consumed & bit_reader::SLICE_SHIFT_MASK;
sym
}
#[inline(always)]
fn read_symbol_slice<T: HuffmanTable + ?Sized>(
fbr: &mut bit_reader::BitReaderSlice,
table: &T,
) -> u32 {
fbr.fill16();
decode_symbol_no_fill(fbr, table)
}
fn Log2Floor(mut x: u32) -> u32 {
let mut result: u32 = 0;
while x != 0 {
x >>= 1;
result += 1;
}
result
}
fn ReadSimpleHuffmanSymbols<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(alphabet_size: u32, max_symbol: u32,
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
let max_bits = Log2Floor(alphabet_size - 1);
let mut i = s.sub_loop_counter;
let num_symbols = s.symbol;
for symbols_lists_item in fast_mut!((s.symbols_lists_array)[s.sub_loop_counter as usize;
num_symbols as usize + 1])
.iter_mut() {
let mut v: u32 = 0;
if !bit_reader::BrotliSafeReadBits(&mut s.br, max_bits, &mut v, input) {
mark_unlikely();
s.sub_loop_counter = i;
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (v >= max_symbol) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET;
}
*symbols_lists_item = v as u16;
BROTLI_LOG_UINT!(v);
i += 1;
}
i = 0;
for symbols_list_item in fast!((s.symbols_lists_array)[0; num_symbols as usize]).iter() {
for other_item in fast!((s.symbols_lists_array)[i as usize + 1 ; num_symbols as usize+ 1])
.iter() {
if (*symbols_list_item == *other_item) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME;
}
}
i += 1;
}
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
}
const SYMBOL_LISTS_OFFSET: usize = huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1;
#[inline(always)]
fn ProcessSingleCodeLength(code_len: u32,
symbol: &mut u32,
repeat: &mut u32,
space: &mut u32,
prev_code_len: &mut u32,
symbol_lists: &mut [u16; huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 +
huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE],
code_length_histo: &mut [u16; 16],
next_symbol: &mut [i32; 32]) {
*repeat = 0;
if (code_len != 0) {
let cl = (code_len as usize) & 15;
let idx = (SYMBOL_LISTS_OFFSET as isize).wrapping_add(next_symbol[cl] as isize) as usize;
debug_assert!(idx < symbol_lists.len());
if let Some(slot) = symbol_lists.get_mut(idx) {
*slot = (*symbol) as u16;
}
next_symbol[cl] = (*symbol) as i32;
*prev_code_len = code_len;
*space = space.wrapping_sub(32768 >> code_len);
code_length_histo[cl] += 1;
BROTLI_LOG!("[ReadHuffmanCode] code_length[{:}]={:} histo[]={:}\n",
*symbol, code_len, code_length_histo[cl]);
}
(*symbol) += 1;
}
#[inline(always)]
fn ProcessRepeatedCodeLength(code_len: u32,
mut repeat_delta: u32,
alphabet_size: u32,
symbol: &mut u32,
repeat: &mut u32,
space: &mut u32,
prev_code_len: &mut u32,
repeat_code_len: &mut u32,
symbol_lists: &mut [u16; huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH + 1 +
huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTHS_SIZE],
code_length_histo: &mut [u16; 16],
next_symbol: &mut [i32; 32]) {
let old_repeat: u32;
let extra_bits: u32;
let new_len: u32;
if (code_len == kCodeLengthRepeatCode) {
extra_bits = 2;
new_len = *prev_code_len
} else {
extra_bits = 3;
new_len = 0
}
if (*repeat_code_len != new_len) {
*repeat = 0;
*repeat_code_len = new_len;
}
old_repeat = *repeat;
if (*repeat > 0) {
*repeat -= 2;
*repeat <<= extra_bits;
}
*repeat += repeat_delta + 3;
repeat_delta = *repeat - old_repeat;
if (*symbol + repeat_delta > alphabet_size) {
*symbol = alphabet_size;
*space = 0xFFFFF;
return;
}
BROTLI_LOG!("[ReadHuffmanCode] code_length[{:}..{:}] = {:}\n",
*symbol, *symbol + repeat_delta - 1, *repeat_code_len);
if (*repeat_code_len != 0) {
let last: u32 = *symbol + repeat_delta;
let rcl = (*repeat_code_len as usize) & 15;
let mut next: i32 = next_symbol[rcl];
loop {
let idx = (SYMBOL_LISTS_OFFSET as isize).wrapping_add(next as isize) as usize;
debug_assert!(idx < symbol_lists.len());
if let Some(slot) = symbol_lists.get_mut(idx) {
*slot = (*symbol) as u16;
}
next = (*symbol) as i32;
(*symbol) += 1;
if *symbol == last {
break;
}
}
next_symbol[rcl] = next;
*space = space.wrapping_sub(repeat_delta << (15 - *repeat_code_len));
code_length_histo[rcl] = (code_length_histo[rcl] as u32 + repeat_delta) as u16;
} else {
*symbol += repeat_delta;
}
}
fn ReadSymbolCodeLengths<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(alphabet_size: u32,
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
let mut symbol = s.symbol;
let mut repeat = s.repeat;
let mut space = s.space;
let mut prev_code_len: u32 = s.prev_code_len;
let mut repeat_code_len: u32 = s.repeat_code_len;
if (!bit_reader::BrotliWarmupBitReader(&mut s.br, input)) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
let mut br = match bit_reader::BitReaderSlice::new(&s.br, input) {
Some(r) => r,
None => return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE,
};
while (symbol < alphabet_size && space > 0) {
if br.avail_in() < bit_reader::BROTLI_SHORT_FILL_BIT_WINDOW_READ {
br.sync_to(&mut s.br);
s.symbol = symbol;
s.repeat = repeat;
s.prev_code_len = prev_code_len;
s.repeat_code_len = repeat_code_len;
s.space = space;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
br.fill_short();
let p_index = (br.val as usize)
& ((1usize << huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH) - 1);
#[cfg(target_pointer_width = "64")]
let p = s.table[p_index];
#[cfg(not(target_pointer_width = "64"))]
let p = s.table[..].entry(p_index);
br.drop_bits(p.bits as u32);
let code_len: u32 = p.value as u32;
if (code_len < kCodeLengthRepeatCode) {
ProcessSingleCodeLength(code_len,
&mut symbol,
&mut repeat,
&mut space,
&mut prev_code_len,
&mut s.symbols_lists_array,
&mut s.code_length_histo,
&mut s.next_symbol);
} else {
let extra_bits: u32 = if code_len == kCodeLengthRepeatCode {
2
} else {
3
};
let repeat_delta: u32 = br.take_bits(extra_bits);
ProcessRepeatedCodeLength(code_len,
repeat_delta,
alphabet_size,
&mut symbol,
&mut repeat,
&mut space,
&mut prev_code_len,
&mut repeat_code_len,
&mut s.symbols_lists_array,
&mut s.code_length_histo,
&mut s.next_symbol);
}
}
br.sync_to(&mut s.br);
s.space = space;
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
}
fn SafeReadSymbolCodeLengths<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(alphabet_size: u32,
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
while (s.symbol < alphabet_size && s.space > 0) {
let mut p_index = 0;
let code_len: u32;
let mut bits: u32 = 0;
let available_bits: u32 = bit_reader::BrotliGetAvailableBits(&s.br);
if (available_bits != 0) {
bits = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32;
}
p_index += bits &
bit_reader::BitMask(huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as u32);
#[cfg(target_pointer_width = "64")]
let p = fast!((s.table)[p_index as usize]);
#[cfg(not(target_pointer_width = "64"))]
let p = s.table[..].entry(p_index as usize);
if (p.bits as u32 > available_bits) {
if (!bit_reader::BrotliPullByte(&mut s.br, input)) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
continue;
}
code_len = p.value as u32;
if (code_len < kCodeLengthRepeatCode) {
bit_reader::BrotliDropBits(&mut s.br, p.bits as u32);
ProcessSingleCodeLength(code_len,
&mut s.symbol,
&mut s.repeat,
&mut s.space,
&mut s.prev_code_len,
&mut s.symbols_lists_array,
&mut s.code_length_histo,
&mut s.next_symbol);
} else {
let extra_bits: u32 = code_len - 14;
let repeat_delta: u32 = (bits >> p.bits) & bit_reader::BitMask(extra_bits);
if (available_bits < p.bits as u32 + extra_bits) {
if (!bit_reader::BrotliPullByte(&mut s.br, input)) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
continue;
}
bit_reader::BrotliDropBits(&mut s.br, p.bits as u32 + extra_bits);
ProcessRepeatedCodeLength(code_len,
repeat_delta,
alphabet_size,
&mut s.symbol,
&mut s.repeat,
&mut s.space,
&mut s.prev_code_len,
&mut s.repeat_code_len,
&mut s.symbols_lists_array,
&mut s.code_length_histo,
&mut s.next_symbol);
}
}
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
}
fn ReadCodeLengthCodeLengths<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
let mut num_codes: u32 = s.repeat;
let mut space: u32 = s.space;
let mut i = s.sub_loop_counter;
for code_length_code_order in
fast!((kCodeLengthCodeOrder)[s.sub_loop_counter as usize; CODE_LENGTH_CODES]).iter() {
let code_len_idx = *code_length_code_order;
let mut ix: u32 = 0;
if !bit_reader::BrotliSafeGetBits(&mut s.br, 4, &mut ix, input) {
mark_unlikely();
let available_bits: u32 = bit_reader::BrotliGetAvailableBits(&s.br);
if (available_bits != 0) {
ix = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32 & 0xF;
} else {
ix = 0;
}
if (fast!((kCodeLengthPrefixLength)[ix as usize]) as u32 > available_bits) {
s.sub_loop_counter = i;
s.repeat = num_codes;
s.space = space;
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
}
BROTLI_LOG_UINT!(ix);
let v: u32 = fast!((kCodeLengthPrefixValue)[ix as usize]) as u32;
bit_reader::BrotliDropBits(&mut s.br,
fast!((kCodeLengthPrefixLength)[ix as usize]) as u32);
fast_mut!((s.code_length_code_lengths)[code_len_idx as usize]) = v as u8;
BROTLI_LOG_ARRAY_INDEX!(s.code_length_code_lengths, code_len_idx);
if v != 0 {
space = space.wrapping_sub(32 >> v);
num_codes += 1;
fast_mut!((s.code_length_histo)[v as usize]) += 1;
if space.wrapping_sub(1) >= 32 {
break;
}
}
i += 1;
}
if (!(num_codes == 1 || space == 0)) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CL_SPACE;
}
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
}
fn ReadHuffmanCode<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(mut alphabet_size: u32,
max_symbol: u32,
table: &mut [HuffmanCode],
offset: usize,
opt_table_size: Option<&mut u32>,
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
if offset > table.len() {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE;
}
alphabet_size &= 0x7ff;
loop {
match s.substate_huffman {
BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE => {
if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut s.sub_loop_counter, input) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
BROTLI_LOG_UINT!(s.sub_loop_counter);
if (s.sub_loop_counter != 1) {
s.space = 32;
s.repeat = 0;
let max_code_len_len = huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as usize + 1;
for code_length_histo in fast_mut!((s.code_length_histo)[0;max_code_len_len]).iter_mut() {
*code_length_histo = 0; }
for code_length_code_length in s.code_length_code_lengths[..].iter_mut() {
*code_length_code_length = 0;
}
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX;
continue;
}
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
}
BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE => {
if (!bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut s.symbol, input)) {
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
s.sub_loop_counter = 0;
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ;
}
BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ => {
let result = ReadSimpleHuffmanSymbols(alphabet_size, max_symbol, s, input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => return result,
}
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
}
BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD => {
let table_size: u32;
if (s.symbol == 3) {
let mut bits: u32 = 0;
if (!bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input)) {
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
s.symbol += bits;
}
BROTLI_LOG_UINT!(s.symbol);
table_size = huffman::BrotliBuildSimpleHuffmanTable(&mut table[offset..],
HUFFMAN_TABLE_BITS as i32,
&s.symbols_lists_array[..],
s.symbol);
if table_size == 0 {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE;
}
if let Some(opt_table_size_ref) = opt_table_size {
*opt_table_size_ref = table_size
}
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX => {
let result = ReadCodeLengthCodeLengths(s, input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => return result,
}
if !huffman::BrotliBuildCodeLengthsHuffmanTable(
&mut s.table,
&s.code_length_code_lengths,
&s.code_length_histo,
) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE;
}
for code_length_histo in s.code_length_histo.iter_mut() {
*code_length_histo = 0;
}
let max_code_length = huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH as usize + 1;
for (i, next_symbol_mut) in s.next_symbol[..max_code_length]
.iter_mut()
.enumerate() {
*next_symbol_mut = i as i32 - (max_code_length as i32);
}
for symbol_list in s.symbols_lists_array[..max_code_length].iter_mut() {
*symbol_list = 0xFFFF;
}
s.symbol = 0;
s.prev_code_len = kDefaultCodeLength;
s.repeat = 0;
s.repeat_code_len = 0;
s.space = 32768;
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
}
BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS => {
let table_size: u32;
let mut result = ReadSymbolCodeLengths(max_symbol, s, input);
if let BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT = result {
result = SafeReadSymbolCodeLengths(max_symbol, s, input)
}
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => return result,
}
if (s.space != 0) {
BROTLI_LOG!("[ReadHuffmanCode] space = %d\n", s.space);
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE;
}
table_size = huffman::BrotliBuildHuffmanTable(fast_mut!((table)[offset;]),
HUFFMAN_TABLE_BITS as i32,
&s.symbols_lists_array[..],
s.symbol_lists_index,
&mut s.code_length_histo);
if table_size == 0 {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE;
}
if let Some(opt_table_size_ref) = opt_table_size {
*opt_table_size_ref = table_size
}
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
}
}
}
#[inline(always)]
fn ReadBlockLength(table: &[HuffmanCode],
br: &mut bit_reader::BrotliBitReader,
input: &[u8])
-> u32 {
let code = ReadSymbol(table, br, input);
let p = &prefix::kBlockLengthPrefixCode[code as usize & 31];
let nbits = p.nbits as u32;
p.offset as u32 + bit_reader::BrotliReadBits(br, nbits, input)
}
fn SafeReadBlockLengthIndex(substate_read_block_length: &state::BrotliRunningReadBlockLengthState,
block_length_index: u32,
table: &[HuffmanCode],
mut br: &mut bit_reader::BrotliBitReader,
input: &[u8])
-> (bool, u32) {
match *substate_read_block_length {
state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE => {
let mut index: u32 = 0;
if (!SafeReadSymbol(table, &mut br, &mut index, input)) {
return (false, 0);
}
(true, index)
}
_ => (true, block_length_index),
}
}
fn SafeReadBlockLengthFromIndex<
AllocHC : alloc::Allocator<HuffmanCode> >(s : &mut BlockTypeAndLengthState<AllocHC>,
br : &mut bit_reader::BrotliBitReader,
result : &mut u32,
res_index : (bool, u32),
input : &[u8]) -> bool{
let (res, index) = res_index;
if !res {
return false;
}
let mut bits: u32 = 0;
let p = &prefix::kBlockLengthPrefixCode[index as usize & 31];
let nbits = p.nbits;
if (!bit_reader::BrotliSafeReadBits(br, nbits as u32, &mut bits, input)) {
s.block_length_index = index;
s.substate_read_block_length =
state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
return false;
}
*result = p.offset as u32 + bits;
s.substate_read_block_length =
state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
true
}
macro_rules! SafeReadBlockLength (
($state : expr, $result : expr , $table : expr) => {
SafeReadBlockLengthFromIndex(&mut $state, &mut $result,
SafeReadBlockLengthIndex($state.substate_read_block_length,
$state.block_length_index,
$table,
&mut $state.br))
};
);
fn InverseMoveToFrontTransform(v: &mut [u8],
v_len: u32,
mtf: &mut [u8;256],
mtf_upper_bound: &mut u32) {
let upper_bound = (*mtf_upper_bound as usize).min(255);
for (i, item) in mtf[..=upper_bound].iter_mut().enumerate() {
*item = i as u8;
}
let mut new_upper_bound: u32 = 0;
for v_i in fast_mut!((v)[0usize ; (v_len as usize)]).iter_mut() {
let mut index = (*v_i) as usize;
let value = mtf[index];
new_upper_bound |= index as u32;
*v_i = value;
while index > 0 {
mtf[index & 0xff] = mtf[(index - 1) & 0xff];
index -= 1;
}
mtf[0] = value;
}
*mtf_upper_bound = new_upper_bound;
}
fn HuffmanTreeGroupDecode<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(group_index: i32,
mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
let mut hcodes: AllocHC::AllocatedMemory;
let mut htrees: AllocU32::AllocatedMemory;
let alphabet_size: u16;
let group_num_htrees: u16;
let group_max_symbol;
if group_index == 0 {
hcodes = mem::replace(&mut s.literal_hgroup.codes,
AllocHC::AllocatedMemory::default());
htrees = mem::replace(&mut s.literal_hgroup.htrees,
AllocU32::AllocatedMemory::default());
group_num_htrees = s.literal_hgroup.num_htrees;
alphabet_size = s.literal_hgroup.alphabet_size;
group_max_symbol = s.literal_hgroup.max_symbol;
} else if group_index == 1 {
hcodes = mem::replace(&mut s.insert_copy_hgroup.codes,
AllocHC::AllocatedMemory::default());
htrees = mem::replace(&mut s.insert_copy_hgroup.htrees,
AllocU32::AllocatedMemory::default());
group_num_htrees = s.insert_copy_hgroup.num_htrees;
alphabet_size = s.insert_copy_hgroup.alphabet_size;
group_max_symbol = s.insert_copy_hgroup.max_symbol;
} else {
if group_index != 2 {
let ret = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
SaveErrorCode!(s, ret);
return ret;
}
hcodes = mem::replace(&mut s.distance_hgroup.codes,
AllocHC::AllocatedMemory::default());
htrees = mem::replace(&mut s.distance_hgroup.htrees,
AllocU32::AllocatedMemory::default());
group_num_htrees = s.distance_hgroup.num_htrees;
alphabet_size = s.distance_hgroup.alphabet_size;
group_max_symbol = s.distance_hgroup.max_symbol;
}
match s.substate_tree_group {
BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_NONE => {
s.htree_next_offset = 0;
s.htree_index = 0;
s.substate_tree_group = BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_LOOP;
}
BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_LOOP => {}
}
let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
let max_table_size = huffman::BrotliHuffmanMaxTableSize(alphabet_size.max(group_max_symbol));
for htree_iter in
fast_mut!((htrees.slice_mut())[s.htree_index as usize ; (group_num_htrees as usize)])
.iter_mut() {
let room_needed = s.htree_next_offset as usize + max_table_size;
if hcodes.slice().len() < room_needed && !EnsureBlockTrees(&mut s) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES;
break;
}
if !huffman::ensure_codes_room(&mut hcodes, &mut s.alloc_hc, s.htree_next_offset as usize,
max_table_size) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS;
break;
}
let mut table_size: u32 = 0;
result = ReadHuffmanCode(u32::from(alphabet_size), u32::from(group_max_symbol),
hcodes.slice_mut(),
s.htree_next_offset as usize,
Some(&mut table_size),
&mut s,
input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break, }
*htree_iter = s.htree_next_offset;
s.htree_next_offset += table_size;
s.htree_index += 1;
}
if group_index == 0 {
let _ = mem::replace(&mut s.literal_hgroup.codes,
mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
let _ = mem::replace(&mut s.literal_hgroup.htrees,
mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
} else if group_index == 1 {
let _ = mem::replace(&mut s.insert_copy_hgroup.codes,
mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
let _ = mem::replace(&mut s.insert_copy_hgroup.htrees,
mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
} else {
let _ = mem::replace(&mut s.distance_hgroup.codes,
mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
let _ = mem::replace(&mut s.distance_hgroup.htrees,
mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
}
if let BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS = result {
s.substate_tree_group = BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_NONE
}
result
}
#[allow(dead_code)]
pub fn lg_window_size(first_byte: u8, second_byte: u8) -> Result<(u8, u8), ()> {
if first_byte & 1 == 0 {
return Ok((16, 1));
}
match first_byte & 15 {
0x3 => return Ok((18, 4)),
0x5 => return Ok((19, 4)),
0x7 => return Ok((20, 4)),
0x9 => return Ok((21, 4)),
0xb => return Ok((22, 4)),
0xd => return Ok((23, 4)),
0xf => return Ok((24, 4)),
_ => match first_byte & 127 {
0x71 => return Ok((15, 7)),
0x61 => return Ok((14, 7)),
0x51 => return Ok((13, 7)),
0x41 => return Ok((12, 7)),
0x31 => return Ok((11, 7)),
0x21 => return Ok((10, 7)),
0x1 => return Ok((17, 7)),
_ => {},
}
}
if (first_byte & 0x80) != 0 {
return Err(());
}
let ret = second_byte & 0x3f;
if ret < 10 || ret > 30 {
return Err(());
}
Ok((ret, 14))
}
fn bzero(data: &mut [u8]) {
for iter in data.iter_mut() {
*iter = 0;
}
}
fn DecodeContextMapInner<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(context_map_size: u32,
num_htrees: &mut u32,
context_map_arg: &mut AllocU8::AllocatedMemory,
mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
let mut result;
loop {
match s.substate_context_map {
BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_NONE => {
result = DecodeVarLenUint8(&mut s.substate_decode_uint8, &mut s.br, num_htrees, input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => return result,
}
(*num_htrees) += 1;
s.context_index = 0;
BROTLI_LOG_UINT!(context_map_size);
BROTLI_LOG_UINT!(*num_htrees);
if context_map_arg.slice().len() < context_map_size as usize {
s.alloc_u8.free_cell(core::mem::take(context_map_arg));
*context_map_arg = s.alloc_u8.alloc_cell(context_map_size as usize);
if (context_map_arg.slice().len() < context_map_size as usize) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP;
}
}
if (*num_htrees <= 1) {
bzero(&mut context_map_arg.slice_mut()[..context_map_size as usize]);
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
}
BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_READ_PREFIX => {
let mut bits: u32 = 0;
if (!bit_reader::BrotliSafeGetBits(&mut s.br, 5, &mut bits, input)) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if ((bits & 1) != 0) {
s.max_run_length_prefix = (bits >> 1) + 1;
bit_reader::BrotliDropBits(&mut s.br, 5);
} else {
s.max_run_length_prefix = 0;
bit_reader::BrotliDropBits(&mut s.br, 1);
}
BROTLI_LOG_UINT!(s.max_run_length_prefix);
s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
}
BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_HUFFMAN => {
if !EnsureContextMapTable(s) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP;
}
let mut local_context_map_table = mem::replace(&mut s.context_map_table,
AllocHC::AllocatedMemory::default());
let alphabet_size = *num_htrees + s.max_run_length_prefix;
result = ReadHuffmanCode(alphabet_size, alphabet_size,
&mut local_context_map_table.slice_mut(),
0,
None,
&mut s,
input);
let _ = mem::replace(&mut s.context_map_table,
mem::replace(&mut local_context_map_table,
AllocHC::AllocatedMemory::default()));
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => return result,
}
s.code = 0xFFFF;
s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_DECODE;
}
BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_DECODE => {
let mut context_index: u32 = s.context_index;
let max_run_length_prefix: u32 = s.max_run_length_prefix;
let context_map = &mut context_map_arg.slice_mut()[..context_map_size as usize];
let context_map_table = s.context_map_table.slice();
let mut code: u32 = s.code;
let mut rleCodeGoto = (code != 0xFFFF);
while (rleCodeGoto || context_index < context_map_size) {
if !rleCodeGoto {
if (!SafeReadSymbol(context_map_table, &mut s.br, &mut code, input)) {
s.code = 0xFFFF;
s.context_index = context_index;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
BROTLI_LOG_UINT!(code);
if code == 0 {
fast_mut!((context_map)[context_index as usize]) = 0;
BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
context_index += 1;
continue;
}
if code > max_run_length_prefix {
fast_mut!((context_map)[context_index as usize]) =
(code - max_run_length_prefix) as u8;
BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
context_index += 1;
continue;
}
}
rleCodeGoto = false; {
let mut reps: u32 = 0;
if (!bit_reader::BrotliSafeReadBits(&mut s.br, code, &mut reps, input)) {
s.code = code;
s.context_index = context_index;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
reps += 1u32 << code;
BROTLI_LOG_UINT!(reps);
if (context_index + reps > context_map_size) {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT;
}
for slot in context_map[context_index as usize..(context_index + reps) as usize].iter_mut() {
*slot = 0;
}
context_index += reps;
}
}
s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
}
BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM => {
let mut bits: u32 = 0;
if (!bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input)) {
s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if (bits != 0) {
if let Ok(ref mut mtf) = s.mtf_or_error_string {
InverseMoveToFrontTransform(context_map_arg.slice_mut(),
context_map_size,
mtf,
&mut s.mtf_upper_bound);
} else {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
}
}
s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_NONE;
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
}
}
}
fn DecodeContextMap<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(context_map_size: usize,
is_dist_context_map: bool,
mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
match s.state {
BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1 if !is_dist_context_map => {},
BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2 if is_dist_context_map => {},
_ => return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE,
}
let (mut num_htrees, mut context_map_arg) = if is_dist_context_map {
(s.num_dist_htrees, mem::replace(&mut s.dist_context_map, AllocU8::AllocatedMemory::default()))
} else {
(s.num_literal_htrees, mem::replace(&mut s.context_map, AllocU8::AllocatedMemory::default()))
};
let retval = DecodeContextMapInner(context_map_size as u32,
&mut num_htrees,
&mut context_map_arg,
&mut s,
input);
if is_dist_context_map {
s.num_dist_htrees = num_htrees;
let _ = mem::replace(&mut s.dist_context_map,
mem::replace(&mut context_map_arg, AllocU8::AllocatedMemory::default()));
} else {
s.num_literal_htrees = num_htrees;
let _ = mem::replace(&mut s.context_map,
mem::replace(&mut context_map_arg, AllocU8::AllocatedMemory::default()));
}
retval
}
#[inline(always)]
fn DecodeBlockTypeAndLength<
AllocHC : alloc::Allocator<HuffmanCode>> (safe : bool,
s : &mut BlockTypeAndLengthState<AllocHC>,
br : &mut bit_reader::BrotliBitReader,
tree_type : i32,
input : &[u8]) -> BrotliDecoderErrorCode {
let max_block_type = fast!((s.num_block_types)[tree_type as usize]);
let type_tree_offset = tree_type as usize * huffman::BROTLI_HUFFMAN_MAX_SIZE_258 as usize;
let len_tree_offset = tree_type as usize * huffman::BROTLI_HUFFMAN_MAX_SIZE_26 as usize;
let mut block_type: u32 = 0;
if max_block_type <= 1 {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH;
}
if (!safe) {
block_type = ReadSymbol(fast_slice!((s.block_type_trees)[type_tree_offset;]), br, input);
fast_mut!((s.block_length)[tree_type as usize]) =
ReadBlockLength(fast_slice!((s.block_len_trees)[len_tree_offset;]), br, input);
} else {
let memento = bit_reader::BrotliBitReaderSaveState(br);
if (!SafeReadSymbol(fast_slice!((s.block_type_trees)[type_tree_offset;]),
br,
&mut block_type,
input)) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
let mut block_length_out: u32 = 0;
let index_ret = SafeReadBlockLengthIndex(&s.substate_read_block_length,
s.block_length_index,
fast_slice!((s.block_len_trees)[len_tree_offset;]),
br,
input);
if !SafeReadBlockLengthFromIndex(s, br, &mut block_length_out, index_ret, input) {
s.substate_read_block_length =
BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
bit_reader::BrotliBitReaderRestoreState(br, &memento);
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
fast_mut!((s.block_length)[tree_type as usize]) = block_length_out;
}
let rb_idx = (tree_type as usize & 3) * 2;
let rb0 = s.block_type_rb[rb_idx];
let rb1 = s.block_type_rb[rb_idx + 1];
if (block_type == 1) {
block_type = rb1 + 1;
} else if (block_type == 0) {
block_type = rb0;
} else {
block_type -= 2;
}
if (block_type >= max_block_type) {
block_type -= max_block_type;
}
s.block_type_rb[rb_idx] = rb1;
s.block_type_rb[rb_idx + 1] = block_type;
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
}
fn DetectTrivialLiteralBlockTypes<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
for current in s.trivial_literal_contexts.iter_mut() {
*current = 0;
}
let num_block_types = fast!((s.block_type_length_state.num_block_types)[0]) as usize;
let context_map = s.context_map.slice();
for i in 0..num_block_types {
let offset = i << kLiteralContextBits;
if let Some(chunk) = context_map.get(offset..offset + (1usize << kLiteralContextBits)) {
let sample = chunk[0];
if chunk.iter().all(|&b| b == sample) {
s.trivial_literal_contexts[i >> 5] |= 1u32 << (i & 31);
}
}
}
}
#[inline(always)]
fn update_literal_block_state(
block_type_rb: &[u32; 6],
trivial_literal_contexts: &[u32; 8],
context_map: &[u8],
context_modes: &[u8],
context_map_slice_index: &mut usize,
trivial_literal_context: &mut i32,
literal_htree_index: &mut u8,
context_lookup: &mut &'static [u8; 512],
) {
let block_type = fast!((block_type_rb)[1]) as usize;
let context_offset = block_type << kLiteralContextBits;
*context_map_slice_index = context_offset;
let trivial = fast!((trivial_literal_contexts)[(block_type >> 5) & 7]);
*trivial_literal_context = ((trivial >> (block_type & 31)) & 1) as i32;
*literal_htree_index = fast!((context_map)[context_offset]);
let context_mode_index = fast!((context_modes)[block_type]) & 3;
*context_lookup = &kContextLookup[context_mode_index as usize];
}
#[inline(always)]
fn PrepareLiteralDecoding<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
update_literal_block_state(
&s.block_type_length_state.block_type_rb,
&s.trivial_literal_contexts,
s.context_map.slice(),
s.context_modes.slice(),
&mut s.context_map_slice_index,
&mut s.trivial_literal_context,
&mut s.literal_htree_index,
&mut s.context_lookup,
);
}
fn UnwrittenBytes<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>> (
s: &BrotliState<AllocU8, AllocU32, AllocHC>,
wrap: bool,
) -> u64 {
debug_assert!(s.pos >= 0 && s.ringbuffer_size > 0);
let ringbuffer_size = s.ringbuffer_size as u64;
let pos = if wrap && s.pos > s.ringbuffer_size {
ringbuffer_size
} else {
s.pos as u64
};
let partial_pos_rb = s.rb_roundtrips * ringbuffer_size + pos;
debug_assert!(partial_pos_rb >= s.partial_pos_out);
partial_pos_rb - s.partial_pos_out
}
fn CheckRingBufferConsistency<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
s: &BrotliState<AllocU8, AllocU32, AllocHC>,
) -> bool {
s.pos >= 0 && s.ringbuffer_size > 0 && s.ringbuffer_mask >= 0 &&
s.ringbuffer_mask as i64 + 1 == s.ringbuffer_size as i64 &&
s.ringbuffer_size as usize <= s.ringbuffer.slice().len()
}
fn WriteRingBuffer<'a,
AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
available_out: &mut usize,
opt_output: Option<&mut [u8]>,
output_offset: &mut usize,
total_out: &mut usize,
force: bool,
s: &'a mut BrotliState<AllocU8, AllocU32, AllocHC>,
) -> (BrotliDecoderErrorCode, &'a [u8]) {
if s.meta_block_remaining_len < 0 {
return (BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1, &[]);
}
if !CheckRingBufferConsistency(s) {
return (BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE, &[]);
}
debug_assert!(s.window_bits < 64);
let window_size = 1i64 << (s.window_bits & 63);
let to_write = UnwrittenBytes(s, true);
let num_written = core::cmp::min(*available_out as u64, to_write) as usize;
let start_index = (s.partial_pos_out & s.ringbuffer_mask as u64) as usize;
let start = match s.ringbuffer.slice().get(start_index..start_index + num_written) {
Some(start) => start,
None => return (BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE, &[]),
};
let output_end = match (*output_offset).checked_add(num_written) {
Some(output_end) => output_end,
None => return (BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS, &[]),
};
if let Some(output) = opt_output {
match output.get_mut(*output_offset..output_end) {
Some(output_slice) => output_slice.clone_from_slice(start),
None => return (BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS, &[]),
}
}
*output_offset = output_end;
*available_out -= num_written;
BROTLI_LOG_UINT!(to_write);
BROTLI_LOG_UINT!(num_written);
s.partial_pos_out += num_written as u64;
*total_out = s.partial_pos_out as usize;
if (num_written as u64) < to_write {
if s.ringbuffer_size as i64 == window_size || force {
return (BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT, start);
} else {
return (BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS, start);
}
}
if (s.ringbuffer_size as i64 == window_size &&
s.pos >= s.ringbuffer_size) {
s.pos -= s.ringbuffer_size;
s.rb_roundtrips += 1;
s.should_wrap_ringbuffer = s.pos != 0;
}
(BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS, start)
}
fn WrapRingBuffer<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
) -> bool {
if s.should_wrap_ringbuffer {
if s.ringbuffer_size < 0 || s.pos < 0 {
return false;
}
let ringbuffer_size = s.ringbuffer_size as usize;
let pos = s.pos as usize;
let ringbuffer_len = s.ringbuffer.slice().len();
if ringbuffer_size > ringbuffer_len ||
pos > ringbuffer_size ||
pos > ringbuffer_len - ringbuffer_size {
return false;
}
let (ring_buffer_start, ring_buffer_end) =
s.ringbuffer.slice_mut().split_at_mut(ringbuffer_size);
ring_buffer_start[..pos].clone_from_slice(&ring_buffer_end[..pos]);
s.should_wrap_ringbuffer = false;
}
true
}
fn CopyUncompressedBlockToOutput<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(mut available_out: &mut usize,
mut output: &mut [u8],
mut output_offset: &mut usize,
mut total_out: &mut usize,
mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
match BrotliEnsureRingBuffer(s,
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1) {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
error => return error,
}
loop {
match s.substate_uncompressed {
BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_NONE => {
let remaining = bit_reader::BrotliGetRemainingBytes(&s.br);
let mut nbytes = core::cmp::min(remaining, s.meta_block_remaining_len as u32);
nbytes = core::cmp::min(nbytes, (s.ringbuffer_size - s.pos) as u32);
bit_reader::BrotliCopyBytes(fast_mut!((s.ringbuffer.slice_mut())[s.pos as usize;]),
&mut s.br,
nbytes,
input);
s.pos += nbytes as i32;
s.meta_block_remaining_len -= nbytes as i32;
if s.pos < (1 << s.window_bits) {
if (s.meta_block_remaining_len == 0) {
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
s.substate_uncompressed = BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_WRITE;
}
BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_WRITE => {
let (result, _) = WriteRingBuffer(&mut available_out,
Some(&mut output),
&mut output_offset,
&mut total_out,
false,
&mut s);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => return result,
}
if s.ringbuffer_size == 1 << s.window_bits {
s.max_distance = s.max_backward_distance;
}
s.substate_uncompressed = BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_NONE;
}
}
}
}
const kRingBufferWriteAheadSlack: i32 =
SHARED_BROTLI_MAX_TRANSFORM_AFFIX_LENGTH as i32 + (SHARED_BROTLI_MAX_DICTIONARY_WORD_LENGTH as i32 + 1) + SHARED_BROTLI_MAX_TRANSFORM_AFFIX_LENGTH as i32; const _RING_BUFFER_SLACK_MATCHES_C: [(); 542] = [(); kRingBufferWriteAheadSlack as usize];
const kRingBufferAllocationSlack: usize =
kRingBufferWriteAheadSlack as usize + kBrotliMaxDictionaryWordLength as usize;
const kRingBufferMinSize: i64 = 1024;
const kSmallRingBufferMinSize: i64 = 32;
fn BrotliCalculateRingBufferSize<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8]) {
let window_size = 1i64 << (s.window_bits & 63);
let mut new_ringbuffer_size = window_size;
let fixed_pool = s.u8_pool_size != 0;
let mut is_last = s.is_last_metablock != 0;
if s.is_uncompressed != 0 && s.ringbuffer.slice().len() == 0 {
let next_block_header =
bit_reader::BrotliPeekByte(&mut s.br, s.meta_block_remaining_len as u32, input);
if next_block_header != -1 && (next_block_header & 3) == 3 {
is_last = true;
}
}
let mut min_size = if s.ringbuffer_size != 0 {
s.ringbuffer_size as i64
} else if is_last {
kSmallRingBufferMinSize
} else if s.initial_ringbuffer_size != 0 {
core::cmp::max(s.initial_ringbuffer_size as i64, kSmallRingBufferMinSize)
} else if fixed_pool {
kSmallRingBufferMinSize
} else {
kRingBufferMinSize
};
if s.ringbuffer_size as i64 == window_size {
return;
}
if s.is_metadata != 0 {
return;
}
let mut output_size = if s.ringbuffer.slice().len() == 0 {
0
} else {
s.pos as i64
};
output_size += s.meta_block_remaining_len as i64;
if min_size < output_size {
min_size = output_size;
}
let pool_holds_window = fixed_pool &&
s.u8_pool_size as u64 >= window_size as u64 + kRingBufferAllocationSlack as u64;
if s.canny_ringbuffer_allocation && !(pool_holds_window && !is_last) {
let min_size = core::cmp::max(min_size, 1);
while (new_ringbuffer_size >> 1) >= min_size {
new_ringbuffer_size >>= 1;
}
}
s.new_ringbuffer_size = if new_ringbuffer_size > 0 &&
new_ringbuffer_size <= i32::MAX as i64 {
new_ringbuffer_size as i32
} else {
0
};
}
fn BrotliEnsureRingBuffer<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
alloc_error: BrotliDecoderErrorCode)
-> BrotliDecoderErrorCode {
let new_size = s.new_ringbuffer_size;
if s.ringbuffer_size == new_size && s.ringbuffer.slice().len() != 0 {
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
let pos = s.pos;
if new_size < 2 || (new_size & (new_size - 1)) != 0 || pos < 0 || pos > new_size ||
pos as usize > s.ringbuffer.slice().len() || s.rb_roundtrips != 0 {
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
}
let size = new_size as usize;
let decoded = pos as usize;
let alloc_size = size + kRingBufferAllocationSlack;
let mut new_ringbuffer = s.alloc_u8.alloc_cell(alloc_size);
if new_ringbuffer.slice().len() < alloc_size {
s.alloc_u8.free_cell(new_ringbuffer);
return alloc_error;
}
{
let new_slice = new_ringbuffer.slice_mut();
new_slice[size - 2] = 0;
new_slice[size - 1] = 0;
new_slice[..decoded].clone_from_slice(&s.ringbuffer.slice()[..decoded]);
}
let old_ringbuffer = mem::replace(&mut s.ringbuffer, new_ringbuffer);
s.alloc_u8.free_cell(old_ringbuffer);
s.ringbuffer_size = new_size;
s.ringbuffer_mask = new_size - 1;
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
}
fn EnsureCompoundDictionaryInitialized<AllocU8: alloc::Allocator<u8>>(
addon: &mut state::BrotliDecoderCompoundDictionary<AllocU8>,
) {
let mut block_bits: u32 = 8;
let maximal_address = addon.total_size as u32 - 1;
if addon.block_bits != COMPOUND_DICTIONARY_BLOCK_MAP_UNINITIALIZED {
return;
}
while (maximal_address >> block_bits) != 0 {
block_bits += 1;
}
block_bits -= 8;
addon.block_bits = block_bits;
let mut cursor: u32 = 0;
let mut index: usize = 0;
while cursor <= maximal_address {
while fast!((addon.chunk_offsets)[index + 1]) < cursor {
index += 1;
}
fast_mut!((addon.block_map)[(cursor >> block_bits) as usize]) = index as u8;
match cursor.checked_add(1 << block_bits) {
Some(next) => cursor = next,
None => break,
}
}
}
#[cold]
#[inline(never)]
fn InitializeCompoundDictionaryCopy<AllocU8: alloc::Allocator<u8>>(
addon: &mut state::BrotliDecoderCompoundDictionary<AllocU8>,
address: usize,
length: usize,
) -> bool {
EnsureCompoundDictionaryInitialized(addon);
if length > addon.total_size - address {
return false;
}
let mut index = fast!((addon.block_map)[address >> addon.block_bits]) as usize;
while address as u32 >= fast!((addon.chunk_offsets)[index + 1]) {
index += 1;
}
addon.br_index = index;
addon.br_offset = address - fast!((addon.chunk_offsets)[index]) as usize;
addon.br_length = length;
addon.br_copied = 0;
true
}
#[cold]
#[inline(never)]
fn CopyFromCompoundDictionary<AllocU8: alloc::Allocator<u8>>(
addon: &mut state::BrotliDecoderCompoundDictionary<AllocU8>,
ringbuffer: &mut [u8],
ringbuffer_size: i32,
pos: i32,
external: &[u8],
) -> i32 {
let ringbuffer_size = ringbuffer_size as usize;
let mut pos = pos as usize;
let orig_pos = pos;
while addon.br_length != addon.br_copied {
let chunk = match addon.chunks[addon.br_index] {
state::MaybeOwnedSlice::External(_) => external,
ref chunk => chunk.slice(),
};
let space = ringbuffer_size - pos;
let rem_chunk_length = chunk.len() - addon.br_offset;
let mut length = addon.br_length - addon.br_copied;
if length > rem_chunk_length {
length = rem_chunk_length;
}
if length > space {
length = space;
}
fast_mut!((ringbuffer)[pos ; pos + length])
.clone_from_slice(fast!((chunk)[addon.br_offset ; addon.br_offset + length]));
pos += length;
addon.br_offset += length;
addon.br_copied += length;
if length == rem_chunk_length {
addon.br_index += 1;
addon.br_offset = 0;
}
if pos == ringbuffer_size {
break;
}
}
(pos - orig_pos) as i32
}
#[cfg(all(test, feature="std"))]
mod tests {
use super::*;
struct CountingAlloc {
fail_at: usize,
short: bool,
requests: std::vec::Vec<usize>,
allocated: usize,
freed: usize,
}
impl Default for CountingAlloc {
fn default() -> Self {
CountingAlloc {
fail_at: usize::MAX,
short: false,
requests: std::vec::Vec::new(),
allocated: 0,
freed: 0,
}
}
}
impl Allocator<u8> for CountingAlloc {
type AllocatedMemory = <::StandardAlloc as Allocator<u8>>::AllocatedMemory;
fn alloc_cell(&mut self, len: usize) -> Self::AllocatedMemory {
self.requests.push(len);
if len >= self.fail_at {
return Self::AllocatedMemory::default();
}
let len = if self.short && len != 0 { len - 1 } else { len };
if len != 0 {
self.allocated += 1;
}
::StandardAlloc::default().alloc_cell(len)
}
fn free_cell(&mut self, data: Self::AllocatedMemory) {
if data.slice().len() != 0 {
self.freed += 1;
}
}
}
type CountingState = BrotliState<CountingAlloc, ::StandardAlloc, ::StandardAlloc>;
fn counting_state(window_bits: u32) -> CountingState {
let mut s = BrotliState::new(CountingAlloc::default(),
::StandardAlloc::default(),
::StandardAlloc::default());
s.window_bits = window_bits;
s
}
fn start_metablock(s: &mut CountingState, mlen: i32) -> BrotliDecoderErrorCode {
s.meta_block_remaining_len = mlen;
BrotliCalculateRingBufferSize(s, &[]);
BrotliEnsureRingBuffer(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2)
}
fn finish_metablock(s: &mut CountingState) {
let (pos, mlen) = (s.pos as usize, s.meta_block_remaining_len as usize);
for (i, byte) in s.ringbuffer.slice_mut()[pos..pos + mlen].iter_mut().enumerate() {
*byte = ((pos + i) % 251) as u8 + 1;
}
s.pos += s.meta_block_remaining_len;
s.meta_block_remaining_len = 0;
}
fn assert_holds_decoded_bytes(s: &CountingState) {
let ringbuffer = s.ringbuffer.slice();
for i in 0..s.pos as usize {
assert_eq!(ringbuffer[i], (i % 251) as u8 + 1, "byte {}", i);
}
}
fn assert_success(result: BrotliDecoderErrorCode) {
assert_eq!(result as i32, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS as i32);
}
fn assert_ring_buffer(s: &CountingState, size: i32) {
assert_eq!(s.ringbuffer_size, size);
assert_eq!(s.ringbuffer_mask, size - 1);
assert_eq!(s.new_ringbuffer_size, size);
assert_eq!(s.ringbuffer.slice().len(), size as usize + kRingBufferAllocationSlack);
}
#[test]
fn ring_buffer_starts_small_and_grows_on_demand() {
let mut s = counting_state(22);
assert_success(start_metablock(&mut s, 10));
assert_ring_buffer(&s, 1024);
assert_eq!(&s.ringbuffer.slice()[1022..1024], &[0, 0]);
finish_metablock(&mut s);
assert_success(start_metablock(&mut s, 1014));
assert_ring_buffer(&s, 1024);
finish_metablock(&mut s);
assert_eq!(s.alloc_u8.allocated, 1);
assert_success(start_metablock(&mut s, 1));
assert_ring_buffer(&s, 2048);
assert_holds_decoded_bytes(&s);
assert_eq!(&s.ringbuffer.slice()[2046..2048], &[0, 0]);
finish_metablock(&mut s);
assert_success(start_metablock(&mut s, 5000));
assert_ring_buffer(&s, 8192);
assert_holds_decoded_bytes(&s);
finish_metablock(&mut s);
assert_success(start_metablock(&mut s, 3 << 20));
assert_ring_buffer(&s, 1 << 22);
assert_holds_decoded_bytes(&s);
finish_metablock(&mut s);
assert_success(start_metablock(&mut s, 3 << 20));
assert_ring_buffer(&s, 1 << 22);
let slack = kRingBufferAllocationSlack;
assert_eq!(s.alloc_u8.requests,
[1024 + slack, 2048 + slack, 8192 + slack, (1 << 22) + slack]);
assert_eq!(s.alloc_u8.allocated, 4);
assert_eq!(s.alloc_u8.freed, 3);
}
#[test]
fn ring_buffer_sizing_matches_the_c_decoder() {
let first_size = |window_bits: u32, mlen: i32, canny: bool, is_last: bool| {
let mut s = counting_state(window_bits);
s.canny_ringbuffer_allocation = canny;
s.is_last_metablock = is_last as u8;
assert_success(start_metablock(&mut s, mlen));
s.ringbuffer_size
};
assert_eq!(first_size(16, 1, true, false), 1024);
assert_eq!(first_size(24, 1024, true, false), 1024);
assert_eq!(first_size(30, 1, true, false), 1024);
assert_eq!(first_size(24, 1025, true, false), 2048);
assert_eq!(first_size(16, 1 << 24, true, false), 1 << 16);
assert_eq!(first_size(10, 1, true, false), 1024);
assert_eq!(first_size(16, 1, false, false), 1 << 16);
assert_eq!(first_size(24, 1, false, false), 1 << 24);
assert_eq!(first_size(24, 1, false, true), 1 << 24);
assert_eq!(first_size(22, 1, true, true), 32);
assert_eq!(first_size(10, 17, true, true), 32);
assert_eq!(first_size(22, 40, true, true), 64);
assert_eq!(first_size(22, 513, true, true), 1024);
assert_eq!(first_size(22, 1025, true, true), 2048);
let mut s = counting_state(22);
s.is_metadata = 1;
s.meta_block_remaining_len = 100;
BrotliCalculateRingBufferSize(&mut s, &[]);
assert_eq!(s.new_ringbuffer_size, 0);
assert!(s.alloc_u8.requests.is_empty());
}
#[test]
fn fixed_pool_ring_buffer_needs_no_more_than_before_growth() {
let slack = kRingBufferAllocationSlack;
let window = 1usize << 16;
let mut s = counting_state(16);
s.u8_pool_size = window + slack;
assert_success(start_metablock(&mut s, 10));
assert_ring_buffer(&s, window as i32);
assert_eq!(&s.ringbuffer.slice()[window - 2..window], &[0, 0]);
finish_metablock(&mut s);
assert_success(start_metablock(&mut s, 50000));
assert_ring_buffer(&s, window as i32);
assert_eq!(s.alloc_u8.requests, [window + slack]);
let mut s = counting_state(16);
s.u8_pool_size = window + slack;
s.is_last_metablock = 1;
assert_success(start_metablock(&mut s, 40));
assert_ring_buffer(&s, 64);
let mut s = counting_state(16);
s.u8_pool_size = window + slack - 1;
assert_success(start_metablock(&mut s, 1));
assert_ring_buffer(&s, 32);
finish_metablock(&mut s);
assert_success(start_metablock(&mut s, 100));
assert_ring_buffer(&s, 128);
assert_holds_decoded_bytes(&s);
finish_metablock(&mut s);
assert_success(start_metablock(&mut s, 40000));
assert_ring_buffer(&s, window as i32);
assert_holds_decoded_bytes(&s);
assert_eq!(s.alloc_u8.requests, [32 + slack, 128 + slack, window + slack]);
assert_eq!((s.alloc_u8.allocated, s.alloc_u8.freed), (3, 2));
let mut s = counting_state(16);
s.u8_pool_size = 1;
s.canny_ringbuffer_allocation = false;
s.is_last_metablock = 1;
assert_success(start_metablock(&mut s, 1));
assert_ring_buffer(&s, window as i32);
}
#[test]
fn failed_ring_buffer_growth_keeps_the_current_one() {
let mut s = counting_state(24);
s.alloc_u8.fail_at = 1 << 20;
assert_success(start_metablock(&mut s, 700));
finish_metablock(&mut s);
assert_eq!(start_metablock(&mut s, 3 << 20) as i32,
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2 as i32);
assert_eq!(s.ringbuffer_size, 1024);
assert_eq!(s.ringbuffer_mask, 1023);
assert_eq!(s.ringbuffer.slice().len(), 1024 + kRingBufferAllocationSlack);
assert_eq!(s.pos, 700);
assert_holds_decoded_bytes(&s);
assert_eq!((s.alloc_u8.allocated, s.alloc_u8.freed), (1, 0));
s.alloc_u8.fail_at = usize::MAX;
assert_success(BrotliEnsureRingBuffer(
&mut s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2));
assert_ring_buffer(&s, 4 << 20);
assert_holds_decoded_bytes(&s);
assert_eq!((s.alloc_u8.allocated, s.alloc_u8.freed), (2, 1));
}
#[test]
fn short_ring_buffer_allocation_is_returned_and_reported() {
let mut s = counting_state(22);
s.alloc_u8.short = true;
assert_eq!(start_metablock(&mut s, 10) as i32,
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2 as i32);
assert_eq!(s.ringbuffer.slice().len(), 0);
assert_eq!(s.ringbuffer_size, 0);
assert_eq!((s.alloc_u8.allocated, s.alloc_u8.freed), (1, 1));
}
#[test]
fn ensure_ring_buffer_rejects_states_the_decoder_cannot_produce() {
let unreachable = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE as i32;
let ensure = |s: &mut CountingState| {
BrotliEnsureRingBuffer(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1)
as i32
};
let mut s = counting_state(16);
assert_eq!(ensure(&mut s), unreachable);
s.new_ringbuffer_size = 1000;
assert_eq!(ensure(&mut s), unreachable);
s.new_ringbuffer_size = 1024;
s.pos = 1025;
assert_eq!(ensure(&mut s), unreachable);
s.pos = 1;
assert_eq!(ensure(&mut s), unreachable);
s.pos = -1;
assert_eq!(ensure(&mut s), unreachable);
s.pos = 0;
s.rb_roundtrips = 1;
assert_eq!(ensure(&mut s), unreachable);
assert!(s.alloc_u8.requests.is_empty());
s.rb_roundtrips = 0;
assert_eq!(ensure(&mut s), BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS as i32);
}
struct BitWriter {
bytes: std::vec::Vec<u8>,
bits: usize,
}
impl BitWriter {
fn put(&mut self, count: usize, value: u32) {
for i in 0..count {
if self.bits % 8 == 0 {
self.bytes.push(0);
}
let last = self.bytes.len() - 1;
self.bytes[last] |= (((value >> i) & 1) as u8) << (self.bits % 8);
self.bits += 1;
}
}
fn raw(&mut self, data: &[u8]) {
self.bytes.extend_from_slice(data);
self.bits = self.bytes.len() * 8;
}
}
fn uncompressed_stream(lengths: &[usize]) -> (std::vec::Vec<u8>, std::vec::Vec<u8>) {
let mut writer = BitWriter { bytes: std::vec::Vec::new(), bits: 0 };
writer.put(1, 0); let mut data = std::vec::Vec::new();
let mut seed = 1u32;
for &length in lengths {
let block: std::vec::Vec<u8> = (0..length).map(|_| {
seed = seed.wrapping_mul(1103515245).wrapping_add(12345);
(seed >> 16) as u8
}).collect();
writer.put(1, 0);
writer.put(2, 0);
writer.put(16, length as u32 - 1);
writer.put(1, 1);
writer.raw(&block);
data.extend_from_slice(&block);
}
writer.put(2, 3); (writer.bytes, data)
}
fn decode_in_chunks(s: &mut CountingState, input: &[u8], in_chunk: usize, out_chunk: usize)
-> std::vec::Vec<u8> {
let mut output = std::vec::Vec::new();
let mut buffer = vec![0u8; core::cmp::min(out_chunk, 1 << 20)];
let mut input_offset = 0;
loop {
let mut available_in = core::cmp::min(in_chunk, input.len() - input_offset);
let mut available_out = buffer.len();
let mut output_offset = 0;
let mut total_out = 0;
let result = BrotliDecompressStream(&mut available_in, &mut input_offset, input,
&mut available_out, &mut output_offset, &mut buffer,
&mut total_out, s);
output.extend_from_slice(&buffer[..output_offset]);
match result {
BrotliResult::ResultSuccess => return output,
BrotliResult::NeedsMoreInput => assert!(input_offset < input.len(), "truncated"),
BrotliResult::NeedsMoreOutput => {}
BrotliResult::ResultFailure => panic!("decoding failed: {:?}", s.error_code),
}
}
}
#[test]
fn ring_buffer_grows_across_metablocks_then_wraps() {
let (input, expected) = uncompressed_stream(&[10, 1000, 1500, 3000, 30000, 40000, 5000]);
let slack = kRingBufferAllocationSlack;
for &(in_chunk, out_chunk) in &[(usize::MAX, usize::MAX), (1, 1), (1, 4099), (4099, 1),
(333, 777)] {
let mut s = counting_state(0);
let output = decode_in_chunks(&mut s, &input, in_chunk, out_chunk);
assert!(output == expected, "chunks of {} and {}", in_chunk, out_chunk);
assert_eq!(s.alloc_u8.requests, [1024 + slack, 4096 + slack, 8192 + slack, 65536 + slack]);
assert_eq!((s.alloc_u8.allocated, s.alloc_u8.freed), (4, 3));
}
}
#[test]
fn set_parameter_disables_growth_only_before_decoding() {
use state::BrotliDecoderParameter::{BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION,
BROTLI_DECODER_PARAM_LARGE_WINDOW};
let (input, expected) = uncompressed_stream(&[10, 1000, 1500, 3000, 30000, 40000, 5000]);
let mut s = counting_state(0);
assert!(s.set_parameter(BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION, 1));
assert!(decode_in_chunks(&mut s, &input, usize::MAX, usize::MAX) == expected);
assert_eq!(s.alloc_u8.requests, [65536 + kRingBufferAllocationSlack]);
let large_window = s.large_window;
for &value in &[0, 1] {
assert!(!s.set_parameter(BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION, value));
assert!(!s.set_parameter(BROTLI_DECODER_PARAM_LARGE_WINDOW, value));
}
assert!(!s.canny_ringbuffer_allocation);
assert_eq!(s.large_window, large_window);
}
#[test]
fn initial_ring_buffer_size_sets_the_first_ring_buffer() {
let first_size = |initial: u32, mlen: i32, is_last: bool| {
let mut s = counting_state(22);
assert!(s.set_initial_ring_buffer_size(initial));
s.is_last_metablock = is_last as u8;
assert_success(start_metablock(&mut s, mlen));
s.ringbuffer_size
};
assert_eq!(first_size(0, 10, false), 1024);
assert_eq!(first_size(4096, 10, false), 4096);
assert_eq!(first_size(5000, 10, false), 8192);
assert_eq!(first_size(1, 10, false), 32);
assert_eq!(first_size(4096, 5000, false), 8192);
assert_eq!(first_size(u32::MAX, 10, false), 1 << 22);
assert_eq!(first_size(u32::MAX, 10, true), 32);
let mut s = counting_state(22);
assert!(s.set_initial_ring_buffer_size(4096));
s.canny_ringbuffer_allocation = false;
assert_success(start_metablock(&mut s, 10));
assert_eq!(s.ringbuffer_size, 1 << 22);
}
#[test]
fn initial_ring_buffer_size_is_only_set_before_decoding() {
let (input, expected) = uncompressed_stream(&[10, 1000, 1500, 3000, 30000, 40000, 5000]);
let mut s = counting_state(0);
assert!(s.set_initial_ring_buffer_size(4096));
assert!(s.set_initial_ring_buffer_size(u32::MAX));
let (mut available_in, mut input_offset) = (1, 0);
let (mut available_out, mut output_offset, mut total_out) = (0, 0, 0);
match BrotliDecompressStream(&mut available_in, &mut input_offset, &input,
&mut available_out, &mut output_offset, &mut [],
&mut total_out, &mut s) {
BrotliResult::NeedsMoreInput => {}
_ => panic!("expected NeedsMoreInput"),
}
assert!(s.alloc_u8.requests.is_empty());
assert!(!s.set_initial_ring_buffer_size(0));
assert!(decode_in_chunks(&mut s, &input[1..], 333, 777) == expected);
assert_eq!(s.alloc_u8.requests, [65536 + kRingBufferAllocationSlack]);
}
#[test]
fn uncompressed_metablock_before_an_empty_last_one_is_sized_to_fit() {
let (input, expected) = uncompressed_stream(&[10]);
let requests = |initial: u32, canny: bool, in_chunk: usize| {
let mut s = counting_state(0);
assert!(s.set_initial_ring_buffer_size(initial));
s.canny_ringbuffer_allocation = canny;
assert!(decode_in_chunks(&mut s, &input, in_chunk, usize::MAX) == expected);
s.alloc_u8.requests.clone()
};
let slack = kRingBufferAllocationSlack;
assert_eq!(requests(0, true, usize::MAX), [32 + slack]);
assert_eq!(requests(u32::MAX, true, usize::MAX), [32 + slack]);
assert_eq!(requests(0, false, usize::MAX), [65536 + slack]);
assert_eq!(requests(0, true, 1), [1024 + slack]);
assert_eq!(requests(u32::MAX, true, 1), [65536 + slack]);
}
#[test]
fn take_output_returns_every_consumed_byte() {
let input = [0x1b, 0x13, 0x00, 0x00, 0xa4, 0xb0, 0xb2, 0xea, 0x81, 0x47, 0x02, 0x8a];
let expected = b"XXXXXXXXXXYYYYYYYYYY";
let mut state = BrotliState::new(::StandardAlloc::default(),
::StandardAlloc::default(),
::StandardAlloc::default());
let mut available_in = input.len();
let mut input_offset = 0usize;
let mut available_out = 0usize;
let mut output_offset = 0usize;
let mut total_out = 0usize;
let result = BrotliDecompressStream(&mut available_in, &mut input_offset, &input,
&mut available_out, &mut output_offset, &mut [],
&mut total_out, &mut state);
assert_eq!(result as i32, BrotliResult::NeedsMoreOutput as i32);
assert_eq!(total_out, 0);
let mut consumed = 0usize;
for &limit in &[1usize, 3, 7, 0] {
assert!(BrotliDecoderHasMoreOutput(&state));
let mut size = limit;
{
let output = BrotliDecoderTakeOutput(&mut state, &mut size);
assert_eq!(size, if limit == 0 { expected.len() - consumed } else { limit });
assert_eq!(output.len(), size);
assert_eq!(output, &expected[consumed..consumed + size]);
}
consumed += size;
assert_eq!(state.partial_pos_out, consumed as u64);
}
assert_eq!(consumed, expected.len());
assert!(!BrotliDecoderHasMoreOutput(&state));
for &limit in &[1usize, 0] {
let mut size = limit;
assert!(BrotliDecoderTakeOutput(&mut state, &mut size).is_empty());
assert_eq!(size, 0);
}
}
fn assert_large_remaining_copy_is_clamped(meta_block_remaining_len: i32,
pos: i32,
expected_result: BrotliDecoderErrorCode) {
let mut s = BrotliState::new(::StandardAlloc::default(),
::StandardAlloc::default(),
::StandardAlloc::default());
s.ringbuffer_size = 16;
s.new_ringbuffer_size = 16;
s.window_bits = 5;
s.ringbuffer = s.alloc_u8.alloc_cell(s.ringbuffer_size as usize);
s.meta_block_remaining_len = meta_block_remaining_len;
s.pos = pos;
s.br.avail_in = 1u32 << 31;
let input = [0x5au8];
let mut available_out = 0usize;
let mut output = [0u8; 0];
let mut output_offset = 0usize;
let mut total_out = 0usize;
let result = CopyUncompressedBlockToOutput(&mut available_out,
&mut output,
&mut output_offset,
&mut total_out,
&mut s,
&input);
assert_eq!(result as i32, expected_result as i32);
assert_eq!(s.br.avail_in, (1u32 << 31) - 1);
assert_eq!(s.meta_block_remaining_len, meta_block_remaining_len - 1);
assert_eq!(s.pos, pos + 1);
assert_eq!(s.ringbuffer.slice()[pos as usize], input[0]);
}
#[test]
fn uncompressed_copy_clamps_unsigned_remaining_bytes() {
assert_large_remaining_copy_is_clamped(
1, 0, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS);
assert_large_remaining_copy_is_clamped(
16, 15, BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT);
}
#[test]
fn stream_rejects_wrapping_output_range() {
let mut state = BrotliState::new(::StandardAlloc::default(),
::StandardAlloc::default(),
::StandardAlloc::default());
let input = [0x06u8];
let mut output = [0u8; 1];
let mut available_in = input.len();
let mut input_offset = 0usize;
let mut available_out = usize::MAX;
let mut output_offset = 1usize;
let mut total_out = 0usize;
let result = BrotliDecompressStream(&mut available_in,
&mut input_offset,
&input,
&mut available_out,
&mut output_offset,
&mut output,
&mut total_out,
&mut state);
assert_eq!(result as i32, BrotliResult::ResultFailure as i32);
assert_eq!(state.error_code as i32,
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS as i32);
}
#[test]
fn stream_rejects_wrapping_input_range() {
let mut state = BrotliState::new(::StandardAlloc::default(),
::StandardAlloc::default(),
::StandardAlloc::default());
let input = [0x06u8];
let mut output = [0u8; 1];
let mut available_in = u32::MAX as usize;
let mut input_offset = 1usize;
let mut available_out = output.len();
let mut output_offset = 0usize;
let mut total_out = 0usize;
let result = BrotliDecompressStream(&mut available_in,
&mut input_offset,
&input,
&mut available_out,
&mut output_offset,
&mut output,
&mut total_out,
&mut state);
assert_eq!(result as i32, BrotliResult::ResultFailure as i32);
assert_eq!(state.error_code as i32,
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS as i32);
}
#[test]
fn format_distance_error_restores_huffman_tree_groups() {
let mut state = BrotliState::new(::StandardAlloc::default(),
::StandardAlloc::default(),
::StandardAlloc::default());
state.BrotliHuffmanTreeGroupInit(state::WhichTreeGroup::LITERAL,
kNumLiteralCodes, kNumLiteralCodes, 1);
state.BrotliHuffmanTreeGroupInit(state::WhichTreeGroup::INSERT_COPY,
kNumInsertAndCopyCodes, kNumInsertAndCopyCodes, 2);
state.BrotliHuffmanTreeGroupInit(state::WhichTreeGroup::DISTANCE, 64, 64, 3);
let expected_code_lengths = [
state.literal_hgroup.codes.slice().len(),
state.insert_copy_hgroup.codes.slice().len(),
state.distance_hgroup.codes.slice().len(),
];
state.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
state.distance_code = 1;
state.dist_rb_idx = 1;
state.dist_rb[0] = i32::MAX;
state.max_distance = 0;
state.max_backward_distance = 0;
let result = SafeProcessCommands(&mut state, &[], &[]);
assert_eq!(result as i32,
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE as i32);
assert_eq!(state.literal_hgroup.codes.slice().len(), expected_code_lengths[0]);
assert_eq!(state.insert_copy_hgroup.codes.slice().len(), expected_code_lengths[1]);
assert_eq!(state.distance_hgroup.codes.slice().len(), expected_code_lengths[2]);
}
}
fn EnsureContextMapTable<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>)
-> bool {
if s.context_map_table.slice().len() == 0 {
s.context_map_table = s.alloc_hc.alloc_cell(huffman::BROTLI_HUFFMAN_MAX_SIZE_272 as usize);
}
s.context_map_table.slice().len() != 0
}
fn EnsureBlockTrees<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>)
-> bool {
if !EnsureContextMapTable(s) {
return false;
}
if s.block_type_length_state.block_type_trees.slice().len() == 0 {
s.block_type_length_state.block_type_trees =
s.alloc_hc.alloc_cell(3 * huffman::BROTLI_HUFFMAN_MAX_SIZE_258 as usize);
if s.block_type_length_state.block_type_trees.slice().len() == 0 {
return false;
}
}
if s.block_type_length_state.block_len_trees.slice().len() == 0 {
s.block_type_length_state.block_len_trees =
s.alloc_hc.alloc_cell(3 * huffman::BROTLI_HUFFMAN_MAX_SIZE_26 as usize);
if s.block_type_length_state.block_len_trees.slice().len() == 0 {
return false;
}
}
true
}
pub fn ReadContextModes<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8])
-> BrotliDecoderErrorCode {
let mut i: i32 = s.loop_counter;
for context_mode_iter in fast_mut!((s.context_modes.slice_mut())[i as usize ;
(s.block_type_length_state.num_block_types[0]
as usize)])
.iter_mut() {
let mut bits: u32 = 0;
if (!bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input)) {
mark_unlikely();
s.loop_counter = i;
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
*context_mode_iter = bits as u8;
BROTLI_LOG_UINT!(i);
i += 1;
}
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
}
#[inline(always)]
fn TakeDistanceFromRingBuffer(
distance_code: &mut i32,
dist_rb_idx: &mut i32,
dist_rb: &[i32; 4],
distance_context: &mut i32,
) {
if *distance_code == 0 {
*dist_rb_idx -= 1;
*distance_code = fast!((dist_rb)[(*dist_rb_idx & 3) as usize]);
*distance_context = 1;
} else {
let dcode = *distance_code << 1;
const kDistanceShortCodeIndexOffset: u32 = 0xaaafff1b;
const kDistanceShortCodeValueOffset: u32 = 0xfa5fa500;
let mut v = (*dist_rb_idx + (kDistanceShortCodeIndexOffset as i32 >> dcode)) & 0x3;
*distance_code = fast!((dist_rb)[v as usize]);
v = (kDistanceShortCodeValueOffset >> dcode) as i32 & 0x3;
if (dcode & 0x3) != 0 {
*distance_code += v;
} else {
*distance_code -= v;
if *distance_code <= 0 {
*distance_code = 0x7fffffff;
}
}
}
}
pub fn SafeReadBits(br: &mut bit_reader::BrotliBitReader,
n_bits: u32,
val: &mut u32,
input: &[u8])
-> bool {
if (n_bits != 0) {
bit_reader::BrotliSafeReadBits(br, n_bits, val, input)
} else {
*val = 0;
true
}
}
trait CommandLoopMode {
const SAFE: bool;
}
struct FastCommandLoop;
struct SafeCommandLoop;
impl CommandLoopMode for FastCommandLoop { const SAFE: bool = false; }
impl CommandLoopMode for SafeCommandLoop { const SAFE: bool = true; }
#[cold]
#[inline(never)]
fn decode_distance_subtable<
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>,
>(
bits: u32,
table_index: usize,
root: HuffmanCode,
distance_hgroup: &HuffmanTreeGroup<AllocU32, AllocHC>,
dist_context_map: &[u8],
dist_context_map_slice_index: usize,
distance_context: i32,
) -> (usize, u32) {
let tree_idx = dist_context_map
.get(dist_context_map_slice_index + (distance_context as usize & 3))
.copied()
.unwrap_or(0) as usize;
let distance_tree = huffman_tree(distance_hgroup, tree_idx);
let sub_idx = table_index
+ root.value as usize
+ (((bits & bit_reader::BitMask(root.bits as u32)) >> HUFFMAN_TABLE_BITS) as usize);
let sub = distance_tree.entry(sub_idx);
(sub.value as usize, HUFFMAN_TABLE_BITS + (sub.bits as u32 & 7))
}
#[inline(always)]
fn read_distance<M: CommandLoopMode>(
code: usize,
memento: &bit_reader::BrotliBitReaderState,
fbr: &mut bit_reader::BitReaderSlice,
br: &mut bit_reader::BrotliBitReader,
input: &[u8],
distance_code: &mut i32,
distance_context: &mut i32,
dist_rb_idx: &mut i32,
dist_rb: &[i32; 4],
block_len_2: &mut u32,
distance_postfix_bits: u32,
distance_postfix_mask: i32,
num_direct_distance_codes: u32,
) -> bool {
if (code & !0xfusize) == 0 {
*distance_code = code as i32;
*distance_context = 0;
TakeDistanceFromRingBuffer(distance_code, dist_rb_idx, dist_rb, distance_context);
*block_len_2 -= 1;
return true;
}
if !M::SAFE {
if distance_postfix_bits == 0 {
let ndirect = num_direct_distance_codes;
let code = code as u32;
*distance_code = match code.checked_sub(ndirect) {
Some(distval) => {
let nbits = (distval >> 1) + 1;
let bits = fbr.read_bits(nbits);
((2 + (distval & 1)).wrapping_shl(nbits))
.wrapping_add(ndirect)
.wrapping_sub(4 + NUM_DISTANCE_SHORT_CODES as u32 - 1)
.wrapping_add(bits) as i32
}
None => code.wrapping_sub(NUM_DISTANCE_SHORT_CODES as u32 - 1) as i32,
};
} else {
let num_direct = num_direct_distance_codes as i32;
let distval = code as i32 - num_direct;
let mask = !(distval >> 31) as u32;
let d = (distval as u32) & mask;
let postfix_bits = distance_postfix_bits & 3;
let postfix = d & (distance_postfix_mask as u32);
let hcode = d >> postfix_bits;
let nbits = ((hcode >> 1) + 1) & mask;
let bits = fbr.read_bits(nbits);
let offset = (((2u32 + (hcode & 1)).wrapping_shl(nbits)).wrapping_sub(4)) & mask;
*distance_code = ((offset.wrapping_add(bits).wrapping_shl(postfix_bits))
.wrapping_add(postfix) & mask)
.wrapping_add((code as u32) & !mask)
.wrapping_add((num_direct as u32) & mask)
.wrapping_sub(NUM_DISTANCE_SHORT_CODES as u32 - 1) as i32;
}
*distance_context = 0;
*block_len_2 -= 1;
return true;
}
let num_direct = num_direct_distance_codes as usize;
if code < num_direct {
*distance_code = (code - (NUM_DISTANCE_SHORT_CODES as usize - 1)) as i32;
*distance_context = 0;
*block_len_2 -= 1;
return true;
}
let distval = (code - num_direct) as u32;
let postfix_bits = distance_postfix_bits & 3;
let postfix = distval & (distance_postfix_mask as u32);
let hcode = distval >> postfix_bits;
let nbits = (hcode >> 1) + 1;
if nbits > 29 {
*distance_code = 0x7fffffff;
*distance_context = 0;
*block_len_2 -= 1;
return true;
}
let mut bits: u32 = 0;
if !SafeReadBits(br, nbits, &mut bits, input) {
bit_reader::BrotliBitReaderRestoreState(br, memento);
return false;
}
#[cfg(target_pointer_width = "64")]
{
let offset = ((2u64 + (hcode & 1) as u64) << (nbits & 63)).wrapping_sub(4);
*distance_code = ((offset.wrapping_add(bits as u64) << postfix_bits)
.wrapping_add(postfix as u64)
.wrapping_add(num_direct as u64) as i32)
.wrapping_sub(NUM_DISTANCE_SHORT_CODES as i32)
.wrapping_add(1);
}
#[cfg(not(target_pointer_width = "64"))]
{
let offset = (2u32 + (hcode & 1)).wrapping_shl(nbits).wrapping_sub(4);
*distance_code = (offset.wrapping_add(bits).wrapping_shl(postfix_bits)
.wrapping_add(postfix)
.wrapping_add(num_direct as u32) as i32)
.wrapping_sub(NUM_DISTANCE_SHORT_CODES as i32)
.wrapping_add(1);
}
*distance_context = 0;
*block_len_2 -= 1;
true
}
#[inline(always)]
fn read_command<M: CommandLoopMode>(
fbr: &mut bit_reader::BitReaderSlice,
br: &mut bit_reader::BrotliBitReader,
input: &[u8],
distance_code: &mut i32,
distance_context: &mut i32,
copy_len_out: &mut i32,
block_len_1: &mut u32,
insert_length: &mut i32,
insert_copy_tree: &InsertCopyTable,
) -> bool {
let memento = if M::SAFE {
bit_reader::BrotliBitReaderSaveState(br)
} else {
bit_reader::BrotliBitReaderState::default()
};
let cmd_code = if !M::SAFE {
*block_len_1 -= 1;
read_symbol_slice(fbr, insert_copy_tree) as usize
} else {
let mut sym: u32 = 0;
if !SafeReadSymbol(insert_copy_tree, br, &mut sym, input) {
return false;
}
sym as usize
};
let v = &prefix::kCmdLut[cmd_code & 1023];
*distance_code = v.distance_code as i32;
*distance_context = v.context as i32;
if !M::SAFE {
let mut insert_len = v.insert_len_offset as i32;
if v.insert_len_extra_bits != 0 {
mark_unlikely();
insert_len += fbr.read_bits(v.insert_len_extra_bits as u32) as i32;
}
let copy_offset = v.copy_len_offset as i32;
let copy_extra = fbr.read_bits(v.copy_len_extra_bits as u32) as i32;
*insert_length = insert_len;
*copy_len_out = copy_offset + copy_extra;
} else {
let mut insert_len = v.insert_len_offset as i32;
let insert_extra_bits = v.insert_len_extra_bits as u32;
if insert_extra_bits != 0 {
let mut bits = 0u32;
if !bit_reader::BrotliSafeReadBits(br, insert_extra_bits, &mut bits, input) {
bit_reader::BrotliBitReaderRestoreState(br, &memento);
return false;
}
insert_len += bits as i32;
}
let copy_extra_bits = v.copy_len_extra_bits as u32;
let mut copy_bits = 0u32;
if !SafeReadBits(br, copy_extra_bits, &mut copy_bits, input) {
bit_reader::BrotliBitReaderRestoreState(br, &memento);
return false;
}
*insert_length = insert_len;
*copy_len_out = v.copy_len_offset as i32 + copy_bits as i32;
*block_len_1 -= 1;
}
true
}
#[inline(always)]
fn WarmupBitReader(safe: bool, br: &mut bit_reader::BrotliBitReader, input: &[u8]) -> bool {
safe || bit_reader::BrotliWarmupBitReader(br, input)
}
#[inline(always)]
fn CheckInputAmount(safe: bool, br: &bit_reader::BrotliBitReader, num: u32) -> bool {
safe || bit_reader::BrotliCheckInputAmount(br, num)
}
#[inline(always)]
fn memmove16_from_ringbuffer(data: &mut [u8], u32off_dst: u32, u32off_src: u32) -> bool {
let off_dst = u32off_dst as usize;
let off_src = u32off_src as usize;
let len = data.len();
if len < 32 || core::cmp::max(off_dst, off_src) > len - 32 {
return false;
}
#[cfg(target_pointer_width = "64")]
{
let dst_end = off_dst + 16;
let src_end = off_src + 16;
let mut local_array: [u8; 16] = fast_uninitialized!(16);
local_array.clone_from_slice(&data[off_src..src_end]);
data[off_dst..dst_end].clone_from_slice(&local_array);
true
}
#[cfg(not(target_pointer_width = "64"))]
{
let local_array: [u8; 16] = match data.get(off_src..) {
Some(&[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15, ..]) => {
[a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15]
}
_ => return false,
};
match data.get_mut(off_dst..) {
Some([d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15, ..]) => {
*d0 = local_array[0];
*d1 = local_array[1];
*d2 = local_array[2];
*d3 = local_array[3];
*d4 = local_array[4];
*d5 = local_array[5];
*d6 = local_array[6];
*d7 = local_array[7];
*d8 = local_array[8];
*d9 = local_array[9];
*d10 = local_array[10];
*d11 = local_array[11];
*d12 = local_array[12];
*d13 = local_array[13];
*d14 = local_array[14];
*d15 = local_array[15];
true
}
_ => false,
}
}
}
#[inline(always)]
fn memmove16_offset16(data: &mut [u8], u32off_dst: u32, u32off_src: u32) -> bool {
let off_dst = u32off_dst as usize;
let off_src = u32off_src as usize;
let len = data.len();
if len < 32 || core::cmp::max(off_dst, off_src) > len - 32 {
return false;
}
#[cfg(target_pointer_width = "64")]
{
let mut local_array: [u8; 16] = fast_uninitialized!(16);
local_array.clone_from_slice(&data[off_src + 16..off_src + 32]);
data[off_dst + 16..off_dst + 32].clone_from_slice(&local_array);
true
}
#[cfg(not(target_pointer_width = "64"))]
{
let local_array: [u8; 16] = match data.get(off_src..) {
Some(
&[
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15,
..,
],
) => [a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15],
_ => return false,
};
match data.get_mut(off_dst..) {
Some(
[
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
d0, d1, d2, d3, d4, d5, d6, d7, d8, d9, d10, d11, d12, d13, d14, d15,
..,
],
) => {
*d0 = local_array[0];
*d1 = local_array[1];
*d2 = local_array[2];
*d3 = local_array[3];
*d4 = local_array[4];
*d5 = local_array[5];
*d6 = local_array[6];
*d7 = local_array[7];
*d8 = local_array[8];
*d9 = local_array[9];
*d10 = local_array[10];
*d11 = local_array[11];
*d12 = local_array[12];
*d13 = local_array[13];
*d14 = local_array[14];
*d15 = local_array[15];
true
}
_ => false,
}
}
}
#[inline(always)]
fn copy_overlapping_backreference(data: &mut [u8], dst: usize, distance: usize,
size: usize) -> bool {
if distance == 0 || distance > dst || dst > data.len() || size > data.len() - dst {
return false;
}
let seed = core::cmp::min(distance, size);
data.copy_within(dst - distance..dst - distance + seed, dst);
let mut copied = seed;
while copied < size {
let step = core::cmp::min(copied, size - copied);
data.copy_within(dst..dst + step, dst + copied);
copied += step;
}
true
}
#[cfg_attr(target_pointer_width = "64", inline(always))]
#[cfg_attr(not(target_pointer_width = "64"), inline(never))]
fn memcpy_within_slice(data: &mut [u8], off_dst: usize, off_src: usize, size: usize) -> bool {
let len = data.len();
let apart = if off_dst > off_src { off_dst - off_src } else { off_src - off_dst };
if size > len || off_dst > len - size || off_src > len - size || apart < size {
return false;
}
#[cfg(not(feature="unsafe"))]
{
if off_dst > off_src {
let (src, dst) = data.split_at_mut(off_dst);
let src_slice = fast!((src)[off_src ; off_src + size]);
fast_mut!((dst)[0;size]).clone_from_slice(src_slice);
} else {
let (dst, src) = data.split_at_mut(off_src);
let src_slice = fast!((src)[0;size]);
fast_mut!((dst)[off_dst;off_dst + size]).clone_from_slice(src_slice);
}
}
#[cfg(feature="unsafe")]
unsafe {
let ptr = data.as_mut_ptr();
let dst = ptr.add(off_dst);
let src = ptr.add(off_src);
core::ptr::copy_nonoverlapping(src, dst, size);
}
true
}
pub fn BrotliDecoderHasMoreOutput<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> bool {
if is_fatal(s.error_code) {
return false;
}
s.ringbuffer.len() != 0 && CheckRingBufferConsistency(s) && UnwrittenBytes(s, false) != 0
}
pub fn BrotliDecoderTakeOutput<'a,
AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
s: &'a mut BrotliState<AllocU8, AllocU32, AllocHC>,
size: &mut usize,
) -> &'a [u8] {
let one:usize = 1;
let mut available_out = if *size != 0 { *size } else { one << 24 };
let requested_out = available_out;
if (s.ringbuffer.len() == 0) || is_fatal(s.error_code) {
*size = 0;
return &[];
}
if !WrapRingBuffer(s) {
s.error_code = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
*size = 0;
return &[];
}
let mut ign = 0usize;
let mut ign2 = 0usize;
let (status, result) = WriteRingBuffer(&mut available_out, None, &mut ign,&mut ign2, true, s);
match status {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS | BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT => {
*size = requested_out - available_out;
},
_ => {
if is_fatal(status) {
}
*size = 0;
return &[];
}
}
return result;
}
#[cfg(feature="ffi-api")]
pub fn BrotliDecoderIsUsed<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> bool {
!matches!(s.state, BrotliRunningState::BROTLI_STATE_UNINITED)
|| bit_reader::BrotliGetAvailableBits(&s.br) != 0
}
pub fn BrotliDecoderIsFinished<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> bool {
if let BrotliRunningState::BROTLI_STATE_DONE = s.state {
!BrotliDecoderHasMoreOutput(s)
} else {
false
}
}
pub fn BrotliDecoderGetErrorCode<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> BrotliDecoderErrorCode {
s.error_code
}
#[inline(always)]
fn huffman_tree<AllocU32: alloc::Allocator<u32>, AllocHC: alloc::Allocator<HuffmanCode>>(
group: &HuffmanTreeGroup<AllocU32, AllocHC>,
index: usize,
) -> &[HuffmanCode] {
match group.htrees.slice().get(index) {
Some(offset) => group.codes.slice().get(*offset as usize..).unwrap_or(&[]),
None => &[],
}
}
#[cfg_attr(target_pointer_width = "64", inline(always))]
#[cfg_attr(not(target_pointer_width = "64"), inline(never))]
fn huffman_window<AllocU32: alloc::Allocator<u32>, AllocHC: alloc::Allocator<HuffmanCode>,
T: huffman::TableWindow>(
group: &HuffmanTreeGroup<AllocU32, AllocHC>,
index: usize,
) -> &T {
let offset = group.htrees.slice().get(index).map(|o| *o as usize);
huffman::table_window(group.codes.slice(), offset)
}
type LiteralTable = [HuffmanCode; LITERAL_TABLE_WINDOW];
type InsertCopyTable = [HuffmanCode; INSERT_COPY_TABLE_WINDOW];
type DistanceRootTable = [HuffmanCode; DISTANCE_ROOT_WINDOW];
#[cfg_attr(target_pointer_width = "64", inline(always))]
#[cfg_attr(not(target_pointer_width = "64"), inline(never))]
fn load_distance_roots<'a, AllocU32: alloc::Allocator<u32>, AllocHC: alloc::Allocator<HuffmanCode>>(
group: &'a HuffmanTreeGroup<AllocU32, AllocHC>,
map: &[u8],
slice_index: usize,
) -> [&'a DistanceRootTable; 4] {
let idx: [u8; 4] = match map.get(slice_index..).and_then(|m| m.get(..4)) {
Some(m) => [m[0], m[1], m[2], m[3]],
None => return EMPTY_DISTANCE_ROOTS,
};
let htrees = group.htrees.slice();
let codes = group.codes.slice();
let t0: &DistanceRootTable =
huffman::table_window(codes, htrees.get(idx[0] as usize).map(|&o| o as usize));
let t1 = if idx[1] == idx[0] {
t0
} else {
huffman::table_window(codes, htrees.get(idx[1] as usize).map(|&o| o as usize))
};
let t2 = if idx[2] == idx[1] {
t1
} else if idx[2] == idx[0] {
t0
} else {
huffman::table_window(codes, htrees.get(idx[2] as usize).map(|&o| o as usize))
};
let t3 = if idx[3] == idx[2] {
t2
} else if idx[3] == idx[1] {
t1
} else if idx[3] == idx[0] {
t0
} else {
huffman::table_window(codes, htrees.get(idx[3] as usize).map(|&o| o as usize))
};
[t0, t1, t2, t3]
}
#[inline(never)]
fn fill_context_trees<'a, AllocU32: alloc::Allocator<u32>, AllocHC: alloc::Allocator<HuffmanCode>>(
trees: &mut [&'a LiteralTable; 64],
group: &'a HuffmanTreeGroup<AllocU32, AllocHC>,
context_map: &[u8],
slice_index: usize,
) {
match context_map.get(slice_index..).and_then(|map| map.get(..64)) {
Some(map) => {
let htrees = group.htrees.slice();
let codes = group.codes.slice();
let mut prev_index = usize::MAX;
let mut prev_tree: &'a LiteralTable = &EMPTY_LITERAL_TABLE;
for (tree, &index) in trees.iter_mut().zip(map.iter()) {
let idx = index as usize;
if idx != prev_index {
prev_index = idx;
prev_tree = huffman::table_window(codes, htrees.get(idx).map(|&o| o as usize));
}
*tree = prev_tree;
}
}
None => *trees = EMPTY_CONTEXT_TREES,
}
}
#[cfg_attr(target_pointer_width = "64", inline(always))]
#[cfg_attr(not(target_pointer_width = "64"), inline(never))]
fn decode_block_type_and_length_slice<'a, AllocHC: alloc::Allocator<HuffmanCode>>(
btl: &mut BlockTypeAndLengthState<AllocHC>,
mut fbr: bit_reader::BitReaderSlice<'a>,
tree_type: i32,
) -> (bit_reader::BitReaderSlice<'a>, BrotliDecoderErrorCode) {
let mut temp_br = bit_reader::BrotliBitReader {
val_: fbr.val,
bit_pos_: fbr.avail_bits,
next_in: 0,
avail_in: fbr.rem.len() as u32,
};
let status = DecodeBlockTypeAndLength(false, btl, &mut temp_br, tree_type, fbr.rem);
fbr.val = temp_br.val_;
fbr.avail_bits = temp_br.bit_pos_;
fbr.rem = &fbr.rem[temp_br.next_in as usize..];
(fbr, status)
}
#[derive(Clone, Copy)]
enum LiteralBatchStatus {
Decoded,
NotHandled,
CorruptInput,
}
#[inline(always)]
fn decode_literal_batch<'b>(
rb: &mut [u8],
pos: usize,
count: usize,
trivial_literal_context: i32,
literal_htree: &LiteralTable,
context_trees: &[&LiteralTable; 64],
context_lookup: &[u8; 512],
mut fbr: bit_reader::BitReaderSlice<'b>,
) -> (bit_reader::BitReaderSlice<'b>, LiteralBatchStatus) {
if trivial_literal_context != 0 {
let ring_window = match rb.get_mut(pos..).and_then(|w| w.get_mut(..count)) {
Some(w) => w,
None => return (fbr, LiteralBatchStatus::NotHandled),
};
for dst in ring_window.iter_mut() {
if !fbr.fill16_checked() {
mark_unlikely();
return (fbr, LiteralBatchStatus::CorruptInput);
}
*dst = decode_symbol_no_fill(&mut fbr, literal_htree) as u8;
}
(fbr, LiteralBatchStatus::Decoded)
} else {
let start = pos.wrapping_sub(2);
let len = count + 2;
let (p2_init, p1_init, ring_window) =
match rb.get_mut(start..).and_then(|w| w.get_mut(..len)) {
Some([p2_init, p1_init, ring_window @ ..]) => (p2_init, p1_init, ring_window),
_ => return (fbr, LiteralBatchStatus::NotHandled),
};
let mut p1: u8 = *p1_init;
let mut p2: u8 = *p2_init;
for dst in ring_window.iter_mut() {
if !fbr.fill16_checked() {
mark_unlikely();
return (fbr, LiteralBatchStatus::CorruptInput);
}
let context = (context_lookup[p1 as usize] | context_lookup[(p2 as usize) | 256]) as usize;
let hc = context_trees[context & 63];
p2 = p1;
p1 = decode_symbol_no_fill(&mut fbr, hc) as u8;
*dst = p1;
}
(fbr, LiteralBatchStatus::Decoded)
}
}
#[cold]
#[inline(never)]
fn execute_custom_shared_dictionary_copy<AllocU8: alloc::Allocator<u8>, AllocU32: alloc::Allocator<u32>>(
shared_dict: &::shared_dictionary::BrotliSharedDictionary<AllocU8, AllocU32>,
context_lookup: &[u8; 512],
dist_rb_idx: &mut i32,
meta_block_remaining_len: &mut i32,
mut pos: i32,
rb: &mut [u8],
rb_size: i32,
rb_mask: i32,
distance_code: i32,
max_distance: i32,
distance_context: i32,
copy_len: i32,
compound_dictionary_size: u32,
) -> Result<(i32, bool), BrotliDecoderErrorCode> {
let dict_id = if shared_dict.context_based {
let p1 = rb[(pos.wrapping_sub(1) as usize) & (rb_mask as usize)];
let p2 = rb[(pos.wrapping_sub(2) as usize) & (rb_mask as usize)];
let context = (context_lookup[p1 as usize] | context_lookup[p2 as usize | 256]) as usize;
shared_dict.context_map[context]
} else {
0
};
let address = distance_code - max_distance - 1 - compound_dictionary_size as i32;
*dist_rb_idx += distance_context;
let lookup = shared_dict.lookup(dict_id, copy_len, address).map_err(|why| match why {
DictionaryLookupError::LengthNotEncodable =>
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DICTIONARY,
DictionaryLookupError::AddressOutOfRange =>
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_TRANSFORM,
})?;
let mut len = copy_len;
let word = lookup.words.word(len, lookup.word_idx);
if lookup.transform_idx == lookup.transforms.cutoff_identity() {
rb[pos as usize..(pos + len) as usize].clone_from_slice(word);
} else {
len = lookup.transforms.apply(&mut rb[pos as usize..], word, len, lookup.transform_idx);
if len == 0 && distance_code <= 120 {
return Err(BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_TRANSFORM);
}
}
pos += len;
*meta_block_remaining_len -= len;
Ok((pos, pos >= rb_size))
}
#[inline(always)]
fn execute_dictionary_copy<AllocU8: alloc::Allocator<u8>, AllocU32: alloc::Allocator<u32>>(
compound_dictionary: &mut state::BrotliDecoderCompoundDictionary<AllocU8>,
shared_dict: &::shared_dictionary::BrotliSharedDictionary<AllocU8, AllocU32>,
context_lookup: &[u8; 512],
dist_rb: &mut [i32; 4],
dist_rb_idx: &mut i32,
meta_block_remaining_len: &mut i32,
mut pos: i32,
rb: &mut [u8],
rb_size: i32,
rb_mask: i32,
distance_code: i32,
max_distance: i32,
distance_context: i32,
copy_len: i32,
external_dictionary: &[u8],
) -> Result<(i32, bool), BrotliDecoderErrorCode> {
if distance_code > kBrotliMaxAllowedDistance as i32 {
return Err(BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE);
}
let compound_dictionary_size = compound_dictionary.total_size as u32;
if (distance_code - max_distance) as u32 <= compound_dictionary_size {
let address = compound_dictionary_size - (distance_code - max_distance) as u32;
let address = address as usize;
let length = copy_len as usize;
if compound_dictionary.num_chunks == 1 && ((rb_size - pos) as usize) > length {
if length > compound_dictionary.total_size - address {
return Err(BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY);
}
let chunk = match compound_dictionary.chunks[0] {
state::MaybeOwnedSlice::External(_) => external_dictionary,
ref chunk => chunk.slice(),
};
let (dst, src) = match (rb.get_mut(pos as usize..pos as usize + length),
chunk.get(address..address + length)) {
(Some(dst), Some(src)) => (dst, src),
_ => return Err(BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY),
};
dist_rb[(*dist_rb_idx & 3) as usize] = distance_code;
*dist_rb_idx += 1;
*meta_block_remaining_len -= copy_len;
copy_bytes(dst, src);
return Ok((pos + copy_len, false));
}
if !InitializeCompoundDictionaryCopy(compound_dictionary, address, length) {
return Err(BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY);
}
dist_rb[(*dist_rb_idx & 3) as usize] = distance_code;
*dist_rb_idx += 1;
*meta_block_remaining_len -= copy_len;
pos += CopyFromCompoundDictionary(compound_dictionary, rb, rb_size, pos, external_dictionary);
return Ok((pos, pos >= rb_size));
}
if shared_dict.is_custom()
&& copy_len >= SHARED_BROTLI_MIN_DICTIONARY_WORD_LENGTH as i32
&& copy_len <= SHARED_BROTLI_MAX_DICTIONARY_WORD_LENGTH as i32
{
return execute_custom_shared_dictionary_copy(
shared_dict,
context_lookup,
dist_rb_idx,
meta_block_remaining_len,
pos,
rb,
rb_size,
rb_mask,
distance_code,
max_distance,
distance_context,
copy_len,
compound_dictionary_size,
);
}
if copy_len >= kBrotliMinDictionaryWordLength as i32
&& copy_len <= kBrotliMaxDictionaryWordLength as i32
{
let mut offset = fast!((kBrotliDictionaryOffsetsByLength)[copy_len as usize]) as i32;
let word_id = distance_code - max_distance - 1 - compound_dictionary_size as i32;
let shift = fast!((kBrotliDictionarySizeBitsByLength)[copy_len as usize]);
let mask = bit_reader::BitMask(shift as u32) as i32;
let word_idx = word_id & mask;
let transform_idx = word_id >> shift;
*dist_rb_idx += distance_context;
offset += word_idx * copy_len;
if transform_idx >= kNumTransforms {
return Err(BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_TRANSFORM);
}
let mut len = copy_len;
let word = fast!((kBrotliDictionary)[offset as usize ; (offset + len) as usize]);
if transform_idx == 0 {
copy_bytes(&mut rb[pos as usize..(pos + len) as usize], word);
} else {
len = TransformDictionaryWord(&mut rb[pos as usize..], word, len, transform_idx);
}
pos += len;
*meta_block_remaining_len -= len;
return Ok((pos, pos >= rb_size));
}
Err(BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DICTIONARY)
}
#[cfg_attr(target_pointer_width = "64", inline(always))]
#[cfg_attr(not(target_pointer_width = "64"), inline(never))]
fn copy_wrap_or_overlapping(
rb: &mut [u8],
rb_size: i32,
rb_mask: i32,
mut pos: i32,
mut rem: i32,
distance_code: i32,
) -> (i32, i32, bool) {
let rb_mask_usize = (rb_mask as usize) & 0x7fff_ffff;
if rb.len() <= rb_mask_usize {
return (pos, rem, false);
}
let mut wrap_guard = rb_size - pos;
if rem >= 16 && pos >= 0 && wrap_guard > 0 && distance_code > 0 && distance_code <= pos {
let count = core::cmp::min(rem, wrap_guard);
if copy_overlapping_backreference(rb, pos as usize, distance_code as usize, count as usize) {
pos += count;
rem -= count;
wrap_guard -= count;
if wrap_guard == 0 {
return (pos, rem, true);
}
}
}
while rem > 0 {
rem -= 1;
rb[(pos as usize) & rb_mask_usize] =
rb[(pos.wrapping_sub(distance_code) as usize) & rb_mask_usize];
pos += 1;
wrap_guard -= 1;
if wrap_guard == 0 {
return (pos, rem, true);
}
}
(pos, rem, false)
}
#[inline(always)]
fn save_mid_command_state<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>(
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
i: i32,
distance_code: i32,
distance_context: i32,
copy_length: i32,
) {
s.loop_counter = i;
s.distance_code = distance_code;
s.distance_context = distance_context;
s.copy_length = copy_length;
if let Some(&idx) = s
.dist_context_map
.slice()
.get(s.dist_context_map_slice_index + (distance_context as usize & 3))
{
s.dist_htree_index = idx;
}
}
#[inline(always)]
fn ProcessCommandsInternal<
M: CommandLoopMode,
AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>,
>(
single_command: bool,
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8],
dictionary: &[u8],
) -> BrotliDecoderErrorCode {
if M::SAFE {
if (!CheckInputAmount(true, &s.br, 28)) || (!WarmupBitReader(true, &mut s.br, input)) {
mark_unlikely();
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if !matches!(
s.state,
BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN
| BrotliRunningState::BROTLI_STATE_COMMAND_INNER
| BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS
| BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY
) {
mark_unlikely();
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
}
}
let rb_size = s.ringbuffer_size;
let rb_mask = s.ringbuffer_mask;
let mut pos = s.pos;
if !M::SAFE && (rb_size < 2 || (rb_size & (rb_size - 1)) != 0 || rb_mask != rb_size - 1 || pos < 0) {
mark_unlikely();
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
}
let rb_mask_usize = (rb_mask as usize) & 0x7fff_ffff;
let min_rb_len = rb_mask_usize + kRingBufferAllocationSlack;
if !M::SAFE && (pos >= rb_size || s.ringbuffer.slice().len() < min_rb_len) {
mark_unlikely();
return SafeProcessCommands(s, input, dictionary);
}
let mut fbr = if !M::SAFE {
match bit_reader::BitReaderSlice::new(&s.br, input) {
Some(r) => r,
None => {
mark_unlikely();
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
}
}
} else {
bit_reader::BitReaderSlice::empty()
};
let rb: &mut [u8] = if !M::SAFE {
match s.ringbuffer.slice_mut().get_mut(..min_rb_len) {
Some(r) => r,
None => {
mark_unlikely();
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
}
}
} else {
s.ringbuffer.slice_mut()
};
let mut insert_copy_tree: &InsertCopyTable = huffman_window(&s.insert_copy_hgroup, s.htree_command_index as usize);
let mut literal_htree: &LiteralTable = huffman_window(&s.literal_hgroup, s.literal_htree_index as usize);
let mut context_trees: [&LiteralTable; 64] = EMPTY_CONTEXT_TREES;
let mut context_trees_slice: Option<usize> = None;
let mut distance_tree_index = s.dist_htree_index;
let mut distance_tree: &[HuffmanCode] = if M::SAFE {
huffman_tree(&s.distance_hgroup, distance_tree_index as usize)
} else {
&[]
};
let mut distance_roots: [&DistanceRootTable; 4] = if !M::SAFE {
load_distance_roots(&s.distance_hgroup, s.dist_context_map.slice(), s.dist_context_map_slice_index)
} else {
EMPTY_DISTANCE_ROOTS
};
if !M::SAFE && s.trivial_literal_context == 0 {
fill_context_trees(
&mut context_trees,
&s.literal_hgroup,
s.context_map.slice(),
s.context_map_slice_index,
);
context_trees_slice = Some(s.context_map_slice_index);
}
let mut state = if M::SAFE {
core::mem::replace(&mut s.state, BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN)
} else {
BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN
};
if M::SAFE
&& !matches!(
state,
BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN
| BrotliRunningState::BROTLI_STATE_COMMAND_INNER
| BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS
| BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY
)
{
s.state = state;
return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
}
let mut i: i32 = if M::SAFE { s.loop_counter } else { 0 };
let mut distance_code: i32 = if M::SAFE { s.distance_code } else { 0 };
let mut distance_context: i32 = if M::SAFE { s.distance_context } else { 0 };
let mut copy_length: i32 = if M::SAFE { s.copy_length } else { 0 };
let mut exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
let mut mid_cmd: Option<(i32, i32, i32, i32)> = None;
'cmd: loop {
if !M::SAFE {
i = 0;
distance_code = 0;
distance_context = 0;
copy_length = 0;
}
if !M::SAFE || matches!(state, BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN) {
if !M::SAFE && !fbr.has_input() {
mark_unlikely();
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break 'cmd;
}
if s.block_type_length_state.block_length[1] == 0 {
mark_unlikely();
let status = if !M::SAFE {
let (next_fbr, st) =
decode_block_type_and_length_slice(&mut s.block_type_length_state, fbr, 1);
fbr = next_fbr;
if matches!(st, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS) {
s.htree_command_index = s.block_type_length_state.block_type_rb[3] as u16;
insert_copy_tree = huffman_window(&s.insert_copy_hgroup, s.htree_command_index as usize);
}
st
} else {
let st = DecodeBlockTypeAndLength(
true,
&mut s.block_type_length_state,
&mut s.br,
1,
input,
);
if matches!(st, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS) {
s.htree_command_index = s.block_type_length_state.block_type_rb[3] as u16;
insert_copy_tree = huffman_window(&s.insert_copy_hgroup, s.htree_command_index as usize);
}
st
};
if !matches!(status, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS) {
result = status;
break 'cmd;
}
continue 'cmd;
}
if !read_command::<M>(
&mut fbr,
&mut s.br,
input,
&mut distance_code,
&mut distance_context,
&mut copy_length,
&mut s.block_type_length_state.block_length[1],
&mut i,
insert_copy_tree,
) {
mark_unlikely();
result = if M::SAFE {
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT
} else {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE
};
break 'cmd;
}
if M::SAFE {
if i == 0 {
state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
} else {
s.meta_block_remaining_len -= i;
state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
}
} else if i != 0 {
s.meta_block_remaining_len -= i;
}
}
if (!M::SAFE && i != 0) || (M::SAFE && matches!(state, BrotliRunningState::BROTLI_STATE_COMMAND_INNER)) {
'inner: loop {
if !M::SAFE {
let i_u32 = i as u32;
let block_len = s.block_type_length_state.block_length[0];
let mut handled = false;
if i > 0 && i_u32 <= block_len && pos < rb_size - i && i_u32 * 2 + 34 <= fbr.avail_in() {
let (next_fbr, status) = decode_literal_batch(
rb,
pos as usize,
i as usize,
s.trivial_literal_context,
literal_htree,
&context_trees,
s.context_lookup,
fbr,
);
fbr = next_fbr;
match status {
LiteralBatchStatus::Decoded => {
pos += i;
s.block_type_length_state.block_length[0] -= i_u32;
handled = true;
}
LiteralBatchStatus::NotHandled => {}
LiteralBatchStatus::CorruptInput => {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
break 'cmd;
}
}
}
if handled {
break 'inner;
}
mark_unlikely();
}
let mut p1: u8 = if !M::SAFE {
rb[(pos.wrapping_sub(1) as usize) & rb_mask_usize]
} else if rb.is_empty() {
0
} else {
fast!((rb)[((pos - 1) & rb_mask) as usize])
};
let mut p2: u8 = if !M::SAFE {
rb[(pos.wrapping_sub(2) as usize) & rb_mask_usize]
} else if rb.is_empty() {
0
} else {
fast!((rb)[((pos - 2) & rb_mask) as usize])
};
loop {
if !M::SAFE && !fbr.has_input() {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break 'cmd;
}
if s.block_type_length_state.block_length[0] == 0 {
if !M::SAFE {
mark_unlikely();
let status = if !fbr.has_input() {
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT
} else {
let (next_fbr, st) =
decode_block_type_and_length_slice(&mut s.block_type_length_state, fbr, 0);
fbr = next_fbr;
st
};
if !matches!(status, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS) {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
result = status;
break 'cmd;
}
update_literal_block_state(
&s.block_type_length_state.block_type_rb,
&s.trivial_literal_contexts,
s.context_map.slice(),
s.context_modes.slice(),
&mut s.context_map_slice_index,
&mut s.trivial_literal_context,
&mut s.literal_htree_index,
&mut s.context_lookup,
);
literal_htree = huffman_window(&s.literal_hgroup, s.literal_htree_index as usize);
if s.trivial_literal_context == 0
&& context_trees_slice != Some(s.context_map_slice_index)
{
fill_context_trees(
&mut context_trees,
&s.literal_hgroup,
s.context_map.slice(),
s.context_map_slice_index,
);
context_trees_slice = Some(s.context_map_slice_index);
}
continue 'inner;
} else {
mark_unlikely();
let status = DecodeBlockTypeAndLength(
true,
&mut s.block_type_length_state,
&mut s.br,
0,
input,
);
if !matches!(status, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS) {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
result = status;
break 'cmd;
}
update_literal_block_state(
&s.block_type_length_state.block_type_rb,
&s.trivial_literal_contexts,
s.context_map.slice(),
s.context_modes.slice(),
&mut s.context_map_slice_index,
&mut s.trivial_literal_context,
&mut s.literal_htree_index,
&mut s.context_lookup,
);
literal_htree = huffman_window(&s.literal_hgroup, s.literal_htree_index as usize);
if s.block_type_length_state.block_length[0] == 0 {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
break 'cmd;
}
}
}
let hc = if s.trivial_literal_context != 0 {
literal_htree
} else if !M::SAFE {
let context = (s.context_lookup[p1 as usize] | s.context_lookup[p2 as usize | 256]) as usize;
context_trees[context & 63]
} else {
let context = (s.context_lookup[p1 as usize] | s.context_lookup[p2 as usize | 256]) as usize;
let idx = fast_slice!((s.context_map)[s.context_map_slice_index + (context & 63)]);
huffman_window(&s.literal_hgroup, idx as usize)
};
let lit = if !M::SAFE {
read_symbol_slice(&mut fbr, hc) as u8
} else {
let mut sym = 0u32;
if !SafeReadSymbol(hc, &mut s.br, &mut sym, input) {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break 'cmd;
}
sym as u8
};
p2 = p1;
p1 = lit;
if !M::SAFE {
rb[(pos as usize) & rb_mask_usize] = lit;
} else {
fast_mut!((rb)[pos as usize]) = lit;
}
s.block_type_length_state.block_length[0] -= 1;
pos += 1;
if pos == rb_size {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE;
mid_cmd = Some((i - 1, distance_code, distance_context, copy_length));
break 'cmd;
}
i -= 1;
if i == 0 {
break 'inner;
}
}
}
if s.meta_block_remaining_len <= 0 {
mark_unlikely();
exit_state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
break 'cmd;
}
if M::SAFE {
state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
}
}
if !M::SAFE || matches!(state, BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS) {
if distance_code >= 0 {
distance_context = if distance_code != 0 { 0 } else { 1 };
s.dist_rb_idx -= 1;
distance_code = s.dist_rb[(s.dist_rb_idx & 3) as usize];
} else {
if s.block_type_length_state.block_length[2] == 0 {
mark_unlikely();
let status = if !M::SAFE {
if !fbr.has_input() {
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT
} else {
let (next_fbr, st) =
decode_block_type_and_length_slice(&mut s.block_type_length_state, fbr, 2);
fbr = next_fbr;
st
}
} else {
DecodeBlockTypeAndLength(
true,
&mut s.block_type_length_state,
&mut s.br,
2,
input,
)
};
if !matches!(status, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS) {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
mid_cmd = Some((0, distance_code, distance_context, copy_length));
result = status;
break 'cmd;
}
s.dist_context_map_slice_index =
(s.block_type_length_state.block_type_rb[5] << kDistanceContextBits) as usize;
if !M::SAFE {
distance_roots = load_distance_roots(
&s.distance_hgroup,
s.dist_context_map.slice(),
s.dist_context_map_slice_index,
);
}
}
let memento = if M::SAFE {
bit_reader::BrotliBitReaderSaveState(&s.br)
} else {
bit_reader::BrotliBitReaderState::default()
};
let dist_sym = if !M::SAFE {
let distance_root = distance_roots[(distance_context as usize) & 3];
fbr.fill16();
let bits = fbr.val as u32;
let table_index = (bits & HUFFMAN_TABLE_MASK) as usize;
let root = distance_root.entry(table_index);
let (sym, consumed) = if root.bits > HUFFMAN_TABLE_BITS as u8 {
decode_distance_subtable(
bits,
table_index,
root,
&s.distance_hgroup,
s.dist_context_map.slice(),
s.dist_context_map_slice_index,
distance_context,
)
} else {
(root.value as usize, root.bits as u32)
};
fbr.avail_bits = fbr.avail_bits.wrapping_sub(consumed);
fbr.val >>= consumed & bit_reader::SLICE_SHIFT_MASK;
sym
} else {
let idx = fast_slice!((s.dist_context_map)[s.dist_context_map_slice_index + (distance_context as usize & 3)]);
s.dist_htree_index = idx;
if idx != distance_tree_index {
distance_tree_index = idx;
distance_tree = huffman_tree(&s.distance_hgroup, idx as usize);
}
let mut sym: u32 = 0;
if !SafeReadSymbol(distance_tree, &mut s.br, &mut sym, input) {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
mid_cmd = Some((0, distance_code, distance_context, copy_length));
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break 'cmd;
}
sym as usize
};
if !read_distance::<M>(
dist_sym,
&memento,
&mut fbr,
&mut s.br,
input,
&mut distance_code,
&mut distance_context,
&mut s.dist_rb_idx,
&s.dist_rb,
&mut s.block_type_length_state.block_length[2],
s.distance_postfix_bits,
s.distance_postfix_mask,
s.num_direct_distance_codes,
) {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
mid_cmd = Some((0, distance_code, distance_context, copy_length));
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break 'cmd;
}
}
let max_backward_distance = s.max_backward_distance;
let mut max_distance = s.max_distance;
if max_distance != max_backward_distance {
max_distance = if pos < max_backward_distance {
pos
} else {
max_backward_distance
};
s.max_distance = max_distance;
}
i = copy_length;
if distance_code > max_distance {
mark_unlikely();
match execute_dictionary_copy(
&mut s.compound_dictionary,
&s.dictionary,
s.context_lookup,
&mut s.dist_rb,
&mut s.dist_rb_idx,
&mut s.meta_block_remaining_len,
pos,
rb,
rb_size,
rb_mask,
distance_code,
max_distance,
distance_context,
i,
dictionary,
) {
Ok((next_pos, true)) => {
pos = next_pos;
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
break 'cmd;
}
Ok((next_pos, false)) => {
pos = next_pos;
if s.meta_block_remaining_len <= 0 {
exit_state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
break 'cmd;
}
if M::SAFE {
state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
if single_command {
break 'cmd;
}
}
continue 'cmd;
}
Err(err) => {
result = err;
break 'cmd;
}
}
}
s.dist_rb[(s.dist_rb_idx & 3) as usize] = distance_code;
s.dist_rb_idx += 1;
s.meta_block_remaining_len -= i;
let src_start = (pos.wrapping_sub(distance_code) as usize) & rb_mask_usize;
let dst_start = (pos as usize) & rb_mask_usize;
let src_start_u32 = src_start as u32;
let dst_start_u32 = pos as u32;
let copy_len_u32 = i as u32;
let lo = core::cmp::min(dst_start_u32, src_start_u32);
let hi = core::cmp::max(dst_start_u32, src_start_u32);
if hi - lo < copy_len_u32 || hi.wrapping_add(copy_len_u32) >= rb_size as u32 {
mark_unlikely();
if M::SAFE {
if rb.len() < 32 {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE;
break 'cmd;
}
state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
} else {
let (next_pos, next_i, wrapped) =
copy_wrap_or_overlapping(rb, rb_size, rb_mask, pos, i, distance_code);
pos = next_pos;
i = next_i;
if wrapped {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
break 'cmd;
}
}
} else {
if !memmove16_from_ringbuffer(rb, dst_start_u32, src_start_u32) {
mark_unlikely();
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE;
break 'cmd;
}
pos += i;
if i > 16 {
let copy_ok = if i > 32 {
memcpy_within_slice(rb, dst_start + 16, src_start + 16, (i - 16) as usize)
} else {
memmove16_offset16(rb, dst_start_u32, src_start_u32)
};
if !copy_ok {
mark_unlikely();
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE;
break 'cmd;
}
}
}
}
if M::SAFE && matches!(state, BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY) {
let (next_pos, next_i, wrapped) =
copy_wrap_or_overlapping(rb, rb_size, rb_mask, pos, i, distance_code);
pos = next_pos;
i = next_i;
if wrapped {
exit_state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2;
mid_cmd = Some((i, distance_code, distance_context, copy_length));
break 'cmd;
}
}
if s.meta_block_remaining_len <= 0 {
mark_unlikely();
exit_state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
break 'cmd;
}
if M::SAFE {
state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
if single_command {
break 'cmd;
}
}
}
if !M::SAFE {
fbr.sync_to(&mut s.br);
}
s.state = exit_state;
s.pos = pos;
if let Some((rem_i, d_code, d_ctx, c_len)) = mid_cmd {
save_mid_command_state(s, rem_i, d_code, d_ctx, c_len);
}
result
}
#[inline(never)]
fn ProcessCommands<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8],
dictionary: &[u8])
-> BrotliDecoderErrorCode {
if (!CheckInputAmount(false, &s.br, 28)) || (!WarmupBitReader(false, &mut s.br, input)) {
mark_unlikely();
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
if !matches!(s.state, BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN) {
mark_unlikely();
let status = SafeProcessCommandsEntry(true, s, input, dictionary);
if !matches!(status, BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS)
|| !matches!(s.state, BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN)
{
return status;
}
if (!CheckInputAmount(false, &s.br, 28)) || (!WarmupBitReader(false, &mut s.br, input)) {
return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
}
}
ProcessCommandsInternal::<FastCommandLoop, _, _, _>(false, s, input, dictionary)
}
#[inline(never)]
fn SafeProcessCommandsEntry<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(single_command: bool,
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8],
dictionary: &[u8])
-> BrotliDecoderErrorCode {
ProcessCommandsInternal::<SafeCommandLoop, _, _, _>(single_command, s, input, dictionary)
}
#[inline(always)]
fn SafeProcessCommands<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
input: &[u8],
dictionary: &[u8])
-> BrotliDecoderErrorCode {
SafeProcessCommandsEntry(false, s, input, dictionary)
}
pub fn BrotliMaxDistanceSymbol(ndirect: u32, npostfix: u32) -> u32{
let bound:[u32;kBrotliMaxPostfix + 1] = [0, 4, 12, 28];
let diff:[u32;kBrotliMaxPostfix + 1] = [73, 126, 228, 424];
let postfix = 1 << npostfix;
if (ndirect < bound[npostfix as usize ]) {
return ndirect + diff[npostfix as usize] + postfix;
} else if (ndirect > bound[npostfix as usize] + postfix) {
return ndirect + diff[npostfix as usize];
} else {
return bound[npostfix as usize] + diff[npostfix as usize] + postfix;
}
}
pub fn BrotliDecompressStream<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(available_in: &mut usize,
input_offset: &mut usize,
xinput: &[u8],
available_out: &mut usize,
output_offset: &mut usize,
output: &mut [u8],
total_out: &mut usize,
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>)
-> BrotliResult {
BrotliDecompressStreamWithDictionaryAndMetadata(
available_in, input_offset, xinput,
available_out, output_offset, output,
total_out, &[], None, s)
}
pub fn BrotliDecompressStreamWithDictionary<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(available_in: &mut usize,
input_offset: &mut usize,
xinput: &[u8],
available_out: &mut usize,
output_offset: &mut usize,
output: &mut [u8],
total_out: &mut usize,
dictionary: &[u8],
s: &mut BrotliState<AllocU8, AllocU32, AllocHC>)
-> BrotliResult {
BrotliDecompressStreamWithDictionaryAndMetadata(
available_in, input_offset, xinput,
available_out, output_offset, output,
total_out, dictionary, None, s)
}
pub fn BrotliDecompressStreamWithDictionaryAndMetadata<AllocU8: alloc::Allocator<u8>,
AllocU32: alloc::Allocator<u32>,
AllocHC: alloc::Allocator<HuffmanCode>>
(available_in: &mut usize,
input_offset: &mut usize,
xinput: &[u8],
mut available_out: &mut usize,
mut output_offset: &mut usize,
mut output: &mut [u8],
mut total_out: &mut usize,
dictionary: &[u8],
mut metadata_cb: Option<&mut dyn state::BrotliMetadataCallback>,
mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>)
-> BrotliResult {
let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
let mut saved_buffer: [u8; 8] = s.buffer;
let mut local_input: &[u8];
if is_fatal(s.error_code) {
return BrotliResult::ResultFailure;
}
if !bit_reader::is_valid_input_range(*input_offset, *available_in, xinput.len()) {
return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
}
if dictionary.len() != s.external_dictionary_len() {
return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
}
match output_offset.checked_add(*available_out) {
Some(end) if end <= output.len() => {}
_ => return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS),
}
if s.buffer_length as usize > s.buffer.len() {
return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE);
}
if s.buffer_length == 0 {
local_input = xinput;
s.br.avail_in = *available_in as u32;
s.br.next_in = *input_offset as u32;
bit_reader::BrotliBitReaderClearUnclaimed(&mut s.br);
} else {
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
let copy_len = core::cmp::min(saved_buffer.len() - s.buffer_length as usize, *available_in);
if copy_len > 0 {
fast_mut!((saved_buffer)[s.buffer_length as usize ; (s.buffer_length as usize + copy_len)])
.clone_from_slice(fast!((xinput)[*input_offset ; copy_len + *input_offset]));
fast_mut!((s.buffer)[s.buffer_length as usize ; (s.buffer_length as usize + copy_len)])
.clone_from_slice(fast!((xinput)[*input_offset ; copy_len + *input_offset]));
}
local_input = &saved_buffer[..];
s.br.next_in = 0;
bit_reader::BrotliBitReaderClearUnclaimed(&mut s.br);
}
loop {
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => {
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT => {
if s.ringbuffer.slice().len() != 0 {
let (intermediate_result, _) = WriteRingBuffer(available_out,
Some(&mut output),
&mut output_offset,
&mut total_out,
true,
&mut s);
if is_fatal(intermediate_result) {
result = intermediate_result;
break;
}
}
if s.buffer_length != 0 {
if s.br.avail_in == 0 {
s.buffer_length = 0;
result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
local_input = xinput;
s.br.avail_in = *available_in as u32;
s.br.next_in = *input_offset as u32;
bit_reader::BrotliBitReaderClearUnclaimed(&mut s.br);
continue;
} else if *available_in != 0 {
result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
let new_byte = fast!((xinput)[*input_offset]);
let buffer_length = s.buffer_length as usize;
if saved_buffer.get(buffer_length) != Some(&new_byte) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
break;
}
s.buffer[buffer_length] = new_byte;
s.buffer_length += 1;
s.br.avail_in = s.buffer_length;
(*input_offset) += 1;
(*available_in) -= 1;
continue;
}
break;
} else {
*input_offset = s.br.next_in as usize;
*available_in = s.br.avail_in as usize;
let buffer_length = s.buffer_length as usize;
let input_end = *input_offset as u64 + *available_in as u64;
let buffer_end = buffer_length as u64 + *available_in as u64;
if input_end > xinput.len() as u64 || buffer_end > s.buffer.len() as u64 {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
break;
}
let (input_end, buffer_end) = (input_end as usize, buffer_end as usize);
s.buffer[buffer_length..buffer_end]
.clone_from_slice(&xinput[*input_offset..input_end]);
s.buffer_length = buffer_end as u32;
*input_offset = input_end;
*available_in = 0;
break;
}
}
_ => {
if s.buffer_length != 0 {
s.buffer_length = 0;
} else {
bit_reader::BrotliBitReaderUnload(&mut s.br);
*available_in = s.br.avail_in as usize;
*input_offset = s.br.next_in as usize;
}
}
}
break;
}
}
if !bit_reader::is_valid_bit_reader(&s.br, local_input) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
continue;
}
loop {
match s.state {
BrotliRunningState::BROTLI_STATE_UNINITED => {
if (!bit_reader::BrotliWarmupBitReader(&mut s.br, local_input)) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break;
}
result = DecodeWindowBits(&mut s.large_window, &mut s.window_bits, &mut s.br);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
if s.large_window {
s.state = BrotliRunningState::BROTLI_STATE_LARGE_WINDOW_BITS;
} else {
s.state = BrotliRunningState::BROTLI_STATE_INITIALIZE;
}
}
BrotliRunningState::BROTLI_STATE_LARGE_WINDOW_BITS => {
if (!bit_reader::BrotliSafeReadBits(&mut s.br, 6, &mut s.window_bits, local_input)) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break;
}
if (s.window_bits < kBrotliLargeMinWbits ||
s.window_bits > kBrotliLargeMaxWbits) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
break;
}
s.state = BrotliRunningState::BROTLI_STATE_INITIALIZE;
}
BrotliRunningState::BROTLI_STATE_INITIALIZE => {
s.max_backward_distance = (1 << s.window_bits) - kBrotliWindowGap as i32;
if s.custom_dict.slice().len() != 0 {
let dict = mem::replace(&mut s.custom_dict, AllocU8::AllocatedMemory::default());
if !s.attach_compound_dictionary_chunk(state::MaybeOwnedSlice::Owned(dict)) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_COMPOUND_DICTIONARY;
break;
}
}
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN;
}
BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN => {
s.BrotliStateMetablockBegin();
BROTLI_LOG_UINT!(s.pos);
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER;
}
BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER => {
result = DecodeMetaBlockLength(&mut s, local_input); match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
debug_assert!(s.meta_block_remaining_len <= 1 << 24);
BROTLI_LOG_UINT!(s.is_last_metablock);
BROTLI_LOG_UINT!(s.meta_block_remaining_len);
BROTLI_LOG_UINT!(s.is_metadata);
BROTLI_LOG_UINT!(s.is_uncompressed);
if (s.is_metadata != 0 || s.is_uncompressed != 0) &&
!bit_reader::BrotliJumpToByteBoundary(&mut s.br) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_1;
break;
}
if s.is_metadata != 0 {
s.state = BrotliRunningState::BROTLI_STATE_METADATA;
if let Some(ref mut callback) = metadata_cb {
callback.start(s.meta_block_remaining_len as usize);
}
break;
}
if s.meta_block_remaining_len == 0 {
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
break;
}
BrotliCalculateRingBufferSize(s, local_input);
if s.is_uncompressed != 0 {
s.state = BrotliRunningState::BROTLI_STATE_UNCOMPRESSED;
break;
}
s.loop_counter = 0;
s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0;
break;
}
BrotliRunningState::BROTLI_STATE_UNCOMPRESSED => {
let mut _bytes_copied = s.meta_block_remaining_len;
result = CopyUncompressedBlockToOutput(&mut available_out,
&mut output,
&mut output_offset,
&mut total_out,
&mut s,
local_input);
_bytes_copied -= s.meta_block_remaining_len;
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
break;
}
BrotliRunningState::BROTLI_STATE_METADATA => {
result = read_metadata_block(
&mut s.br,
&mut s.meta_block_remaining_len,
local_input,
&mut metadata_cb);
if let BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS = result {
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE
}
break;
}
BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0 => {
if s.loop_counter >= 3 {
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER_2;
break;
}
{
let index = s.loop_counter as usize;
result =
DecodeVarLenUint8(&mut s.substate_decode_uint8,
&mut s.br,
&mut fast_mut!((s.block_type_length_state.num_block_types)[index]),
local_input);
}
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
fast_mut!((s.block_type_length_state.num_block_types)[s.loop_counter as usize]) += 1;
BROTLI_LOG_UINT!(s.block_type_length_state.num_block_types[s.loop_counter as usize]);
if fast!((s.block_type_length_state.num_block_types)[s.loop_counter as usize]) < 2 {
s.loop_counter += 1;
break;
}
s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_1;
}
BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_1 => {
if !EnsureBlockTrees(&mut s) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES;
break;
}
let tree_offset = s.loop_counter as u32 * huffman::BROTLI_HUFFMAN_MAX_SIZE_258 as u32;
let mut new_huffman_table = mem::replace(&mut s.block_type_length_state.block_type_trees,
AllocHC::AllocatedMemory::default());
let loop_counter = s.loop_counter as usize;
let alphabet_size = fast!((s.block_type_length_state.num_block_types)[loop_counter]) + 2;
result =
ReadHuffmanCode(alphabet_size, alphabet_size,
new_huffman_table.slice_mut(),
tree_offset as usize,
None,
&mut s,
local_input);
let _ = mem::replace(&mut s.block_type_length_state.block_type_trees,
new_huffman_table);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_2;
}
BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_2 => {
let tree_offset = s.loop_counter * huffman::BROTLI_HUFFMAN_MAX_SIZE_26 as i32;
let mut new_huffman_table = mem::replace(&mut s.block_type_length_state.block_len_trees,
AllocHC::AllocatedMemory::default());
result = ReadHuffmanCode(kNumBlockLengthCodes, kNumBlockLengthCodes,
new_huffman_table.slice_mut(),
tree_offset as usize,
None,
&mut s,
local_input);
let _ = mem::replace(&mut s.block_type_length_state.block_len_trees,
new_huffman_table);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_3;
}
BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_3 => {
let tree_offset = s.loop_counter * huffman::BROTLI_HUFFMAN_MAX_SIZE_26 as i32;
let mut block_length_out: u32 = 0;
let ind_ret: (bool, u32);
ind_ret = SafeReadBlockLengthIndex(&s.block_type_length_state.substate_read_block_length,
s.block_type_length_state.block_length_index,
fast_slice!((s.block_type_length_state.block_len_trees)
[tree_offset as usize;]),
&mut s.br, local_input);
if !SafeReadBlockLengthFromIndex(&mut s.block_type_length_state,
&mut s.br,
&mut block_length_out,
ind_ret,
local_input) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break;
}
fast_mut!((s.block_type_length_state.block_length)[s.loop_counter as usize]) =
block_length_out;
BROTLI_LOG_UINT!(s.block_type_length_state.block_length[s.loop_counter as usize]);
s.loop_counter += 1;
s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0;
break;
}
BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER_2 => {
let mut bits: u32 = 0;
if (!bit_reader::BrotliSafeReadBits(&mut s.br, 6, &mut bits, local_input)) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
break;
}
s.distance_postfix_bits = bits & bit_reader::BitMask(2);
bits >>= 2;
s.num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
(bits << s.distance_postfix_bits);
BROTLI_LOG_UINT!(s.num_direct_distance_codes);
BROTLI_LOG_UINT!(s.distance_postfix_bits);
s.distance_postfix_mask = bit_reader::BitMask(s.distance_postfix_bits) as i32;
let num_literal_block_types =
fast!((s.block_type_length_state.num_block_types)[0]) as usize;
if s.context_modes.slice().len() < num_literal_block_types {
s.alloc_u8.free_cell(core::mem::take(&mut s.context_modes));
s.context_modes = s.alloc_u8.alloc_cell(num_literal_block_types);
if s.context_modes.slice().len() < num_literal_block_types {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES;
break;
}
}
s.loop_counter = 0;
s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MODES;
}
BrotliRunningState::BROTLI_STATE_CONTEXT_MODES => {
result = ReadContextModes(&mut s, local_input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1;
}
BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1 => {
result =
DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[0]) as usize) <<
kLiteralContextBits as usize,
false,
&mut s,
local_input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
DetectTrivialLiteralBlockTypes(s);
s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2;
}
BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2 => {
let num_direct_codes =
s.num_direct_distance_codes - NUM_DISTANCE_SHORT_CODES;
let num_distance_codes = BROTLI_DISTANCE_ALPHABET_SIZE(
s.distance_postfix_bits, num_direct_codes,
(if s.large_window { BROTLI_LARGE_MAX_DISTANCE_BITS } else {
BROTLI_MAX_DISTANCE_BITS}));
let max_distance_symbol = if s.large_window {
BrotliMaxDistanceSymbol(
num_direct_codes, s.distance_postfix_bits)
} else {
num_distance_codes
};
result =
DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[2]) as usize) <<
kDistanceContextBits as usize,
true,
s,
local_input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
let num_command_htrees =
fast!((s.block_type_length_state.num_block_types)[1]) as u16;
if (s.literal_hgroup.needs_codes_growth(
kNumLiteralCodes, kNumLiteralCodes, s.num_literal_htrees as u16) ||
s.insert_copy_hgroup.needs_codes_growth(
kNumInsertAndCopyCodes, kNumInsertAndCopyCodes, num_command_htrees) ||
s.distance_hgroup.needs_codes_growth(
num_distance_codes as u16, max_distance_symbol as u16, s.num_dist_htrees as u16)) &&
!EnsureBlockTrees(s) {
return SaveErrorCode!(
s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES);
}
s.literal_hgroup.init(&mut s.alloc_u32,
&mut s.alloc_hc,
kNumLiteralCodes,
kNumLiteralCodes,
s.num_literal_htrees as u16);
s.insert_copy_hgroup.init(&mut s.alloc_u32,
&mut s.alloc_hc,
kNumInsertAndCopyCodes,
kNumInsertAndCopyCodes,
num_command_htrees);
s.distance_hgroup.init(&mut s.alloc_u32,
&mut s.alloc_hc,
num_distance_codes as u16,
max_distance_symbol as u16,
s.num_dist_htrees as u16);
if (s.literal_hgroup.htrees.slice().len() == 0 ||
s.literal_hgroup.codes.slice().len() == 0 ||
s.insert_copy_hgroup.htrees.slice().len() == 0 ||
s.insert_copy_hgroup.codes.slice().len() == 0 ||
s.distance_hgroup.htrees.slice().len() == 0 ||
s.distance_hgroup.codes.slice().len() == 0) {
return SaveErrorCode!(
s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS);
}
s.loop_counter = 0;
s.state = BrotliRunningState::BROTLI_STATE_TREE_GROUP;
}
BrotliRunningState::BROTLI_STATE_TREE_GROUP => {
result = HuffmanTreeGroupDecode(s.loop_counter, &mut s, local_input);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
s.loop_counter += 1;
if (s.loop_counter >= 3) {
PrepareLiteralDecoding(s);
s.dist_context_map_slice_index = 0;
s.htree_command_index = 0;
result = BrotliEnsureRingBuffer(
s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
}
break;
}
BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN |
BrotliRunningState::BROTLI_STATE_COMMAND_INNER |
BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS |
BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY => {
result = ProcessCommands(s, local_input, dictionary);
if let BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT = result {
result = SafeProcessCommands(s, local_input, dictionary)
}
break;
}
BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE |
BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1 |
BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2 => {
let (xresult, _) = WriteRingBuffer(&mut available_out,
Some(&mut output),
&mut output_offset,
&mut total_out,
false,
&mut s);
result = xresult;
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
if !WrapRingBuffer(s) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
break;
}
if s.ringbuffer_size == 1 << s.window_bits {
s.max_distance = s.max_backward_distance;
}
match s.state {
BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1 => {
if s.compound_dictionary.br_length != s.compound_dictionary.br_copied {
let pos = s.pos;
let rb_size = s.ringbuffer_size;
let copied = CopyFromCompoundDictionary(
&mut s.compound_dictionary,
s.ringbuffer.slice_mut(),
rb_size,
pos,
dictionary,
);
s.pos += copied;
if s.pos >= s.ringbuffer_size {
continue;
}
}
if (s.meta_block_remaining_len <= 0) {
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
} else {
s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
}
break;
}
BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2 => {
s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
}
_ => {
if (s.loop_counter == 0) {
if (s.meta_block_remaining_len <= 0) {
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
} else {
s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
}
break;
}
s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
}
}
break;
}
BrotliRunningState::BROTLI_STATE_METABLOCK_DONE => {
if (s.meta_block_remaining_len < 0) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2;
break;
}
if (s.is_last_metablock == 0) {
s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN;
break;
}
s.BrotliStateCleanupAfterMetablock();
if (!bit_reader::BrotliJumpToByteBoundary(&mut s.br)) {
result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_2;
break;
}
if (s.buffer_length == 0) {
bit_reader::BrotliBitReaderUnload(&mut s.br);
*available_in = s.br.avail_in as usize;
*input_offset = s.br.next_in as usize;
}
s.state = BrotliRunningState::BROTLI_STATE_DONE;
}
BrotliRunningState::BROTLI_STATE_DONE => {
if (s.ringbuffer.slice().len() != 0) {
let (xresult, _) = WriteRingBuffer(&mut available_out,
Some(&mut output),
&mut output_offset,
&mut total_out,
true,
&mut s);
result = xresult;
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
_ => break,
}
}
return SaveErrorCode!(s, result);
}
}
}
}
SaveErrorCode!(s, result)
}
fn read_metadata_block(
br: &mut bit_reader::BrotliBitReader,
meta_block_remaining_len: &mut i32,
local_input: &[u8],
metadata_cb: &mut Option<&mut dyn state::BrotliMetadataCallback>,
) -> BrotliDecoderErrorCode {
if *meta_block_remaining_len <= 0 {
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
let bit_reader_bits_avail = bit_reader::BrotliGetAvailableBits(br);
debug_assert_eq!(bit_reader_bits_avail & 7, 0);
let amt_in_bit_reader = core::cmp::min(
(bit_reader_bits_avail / 8) as usize,
*meta_block_remaining_len as usize);
if amt_in_bit_reader > 0 {
let mut scratch = [0u8; core::mem::size_of::<bit_reader::BrotliBitReader>()];
bit_reader::BrotliCopyBytes(&mut scratch[..], br, amt_in_bit_reader as u32, local_input);
*meta_block_remaining_len -= amt_in_bit_reader as i32;
if let Some(callback) = metadata_cb {
callback.chunk(&scratch[..amt_in_bit_reader]);
}
}
let remaining_bytes = core::cmp::min(*meta_block_remaining_len as usize,
br.avail_in as usize);
if remaining_bytes != 0 {
if let Some(callback) = metadata_cb {
let all_remaining_input = local_input.split_at(br.next_in as usize).1;
callback.chunk(&all_remaining_input.split_at(remaining_bytes).0);
}
br.next_in += remaining_bytes as u32;
bit_reader::BrotliBitReaderClearUnclaimed(br);
br.avail_in -= remaining_bytes as u32;
*meta_block_remaining_len -= remaining_bytes as i32;
}
if *meta_block_remaining_len == 0 {
return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
}
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT
}
#[cfg(test)]
mod safeguard_tests {
use super::{copy_overlapping_backreference, memcpy_within_slice, memmove16_from_ringbuffer,
memmove16_offset16};
#[test]
fn context_lookup_values_fit_the_context_map() {
for table in super::kContextLookup.iter() {
for value in table.iter() {
assert!(*value < 64);
}
}
}
#[test]
fn overlapping_backreferences_match_sequential_copy() {
for distance in 1..65 {
for size in 0..257 {
let mut data = [0u8; 384];
for (index, byte) in data.iter_mut().enumerate() {
*byte = (index.wrapping_mul(73) ^ (index >> 3)) as u8;
}
let mut expected = data;
for index in 64..64 + size {
expected[index] = expected[index - distance];
}
assert!(copy_overlapping_backreference(&mut data, 64, distance, size));
assert_eq!(&data[..], &expected[..]);
}
}
}
#[test]
fn overlapping_backreferences_reject_invalid_ranges() {
let original = [37u8; 32];
for &(dst, distance, size) in &[
(8, 0, 4), (8, 9, 4), (33, 1, 0), (31, 1, 2),
(usize::MAX, 1, 0), (8, usize::MAX, 1), (8, 1, usize::MAX)] {
let mut data = original;
assert!(!copy_overlapping_backreference(&mut data, dst, distance, size));
assert_eq!(data, original);
}
}
#[test]
fn memmove16_helpers_reject_out_of_bounds_ranges() {
let mut short = [0u8; 31];
assert!(!memmove16_from_ringbuffer(&mut short, 0, 0));
assert!(!memmove16_offset16(&mut short, 0, 0));
let mut data = [7u8; 64];
for &(dst, src) in &[(33u32, 0u32), (0, 33), (u32::MAX, 0), (0, u32::MAX)] {
assert!(!memmove16_from_ringbuffer(&mut data, dst, src));
assert!(!memmove16_offset16(&mut data, dst, src));
}
assert_eq!(data, [7u8; 64]);
assert!(memmove16_from_ringbuffer(&mut data, 32, 0));
assert!(memmove16_offset16(&mut data, 0, 32));
}
#[test]
fn memmove16_helpers_copy_with_memmove_semantics() {
let mut pattern = [0u8; 80];
for (i, b) in pattern.iter_mut().enumerate() {
*b = i as u8;
}
for &(dst, src) in &[(40u32, 0u32), (0, 40), (20, 16), (16, 20), (17, 16), (5, 5)] {
let mut data = pattern;
let mut expected = pattern;
expected.copy_within(src as usize..src as usize + 16, dst as usize);
assert!(memmove16_from_ringbuffer(&mut data, dst, src));
assert_eq!(&data[..], &expected[..], "memmove16_from_ringbuffer({}, {})", dst, src);
let mut data = pattern;
let mut expected = pattern;
expected.copy_within(src as usize + 16..src as usize + 32, dst as usize + 16);
assert!(memmove16_offset16(&mut data, dst, src));
assert_eq!(&data[..], &expected[..], "memmove16_offset16({}, {})", dst, src);
}
}
#[test]
fn memcpy_within_slice_validates_nonoverlapping_ranges() {
let mut data = [0u8, 1, 2, 3, 4, 5, 6, 7];
assert!(memcpy_within_slice(&mut data, 4, 0, 2));
assert_eq!(data, [0, 1, 2, 3, 0, 1, 6, 7]);
let unchanged = data;
assert!(!memcpy_within_slice(&mut data, 1, 0, 4));
assert!(!memcpy_within_slice(&mut data, 7, 0, 2));
assert!(!memcpy_within_slice(&mut data, usize::MAX, 0, 2));
assert!(!memcpy_within_slice(&mut data, 0, usize::MAX, 2));
assert_eq!(data, unchanged);
}
}
#[cfg(all(test, feature="std"))]
mod state_guard_tests {
use super::{CheckRingBufferConsistency, DecodeContextMap, ReadHuffmanCode, UnwrittenBytes,
WrapRingBuffer, WriteRingBuffer};
use super::{decode_block_type_and_length_slice, DecodeBlockTypeAndLength};
use bit_reader::BitReaderSlice;
use super::{BrotliDecoderErrorCode, BrotliRunningState, BrotliState, HuffmanCode};
use ::state::BrotliRunningHuffmanState;
use ::alloc::Allocator;
use ::StandardAlloc;
type TestState = BrotliState<StandardAlloc, StandardAlloc, StandardAlloc>;
fn state() -> TestState {
BrotliState::new(StandardAlloc::default(), StandardAlloc::default(), StandardAlloc::default())
}
fn state_with_ringbuffer(size: i32, window_bits: u32) -> TestState {
let mut s = state();
s.ringbuffer = s.alloc_u8.alloc_cell(size as usize);
s.ringbuffer_size = size;
s.ringbuffer_mask = size - 1;
s.new_ringbuffer_size = size;
s.window_bits = window_bits;
s
}
#[test]
fn ring_buffer_consistency_rejects_inconsistent_state() {
let mut s = state_with_ringbuffer(16, 4);
assert!(CheckRingBufferConsistency(&s));
s.pos = -1;
assert!(!CheckRingBufferConsistency(&s));
s.pos = 0;
s.ringbuffer_size = 0;
assert!(!CheckRingBufferConsistency(&s));
s.ringbuffer_size = 16;
s.ringbuffer_mask = -1;
assert!(!CheckRingBufferConsistency(&s));
s.ringbuffer_mask = 7;
assert!(!CheckRingBufferConsistency(&s));
s.ringbuffer_mask = 15;
s.ringbuffer_size = 32;
s.ringbuffer_mask = 31;
assert!(!CheckRingBufferConsistency(&s));
}
#[test]
fn unwritten_bytes_is_exact_past_a_32_bit_wrap() {
let mut s = state_with_ringbuffer(1 << 16, 16);
s.rb_roundtrips = 1 << 17; s.pos = 100;
s.partial_pos_out = (1u64 << 33) + 40;
assert_eq!(UnwrittenBytes(&s, false), 60);
}
#[test]
fn write_ring_buffer_rejects_inconsistent_ring_buffer() {
let mut s = state_with_ringbuffer(16, 4);
s.ringbuffer_mask = 7; let mut available_out = 8usize;
let mut output = [0u8; 8];
let mut output_offset = 0usize;
let mut total_out = 0usize;
let (code, out) = WriteRingBuffer(&mut available_out, Some(&mut output[..]),
&mut output_offset, &mut total_out, false, &mut s);
match code {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE => {}
other => panic!("expected UNREACHABLE, got {:?}", other),
}
assert!(out.is_empty());
}
#[test]
fn write_ring_buffer_rejects_negative_block_length() {
let mut s = state_with_ringbuffer(16, 4);
s.meta_block_remaining_len = -1;
let mut available_out = 0usize;
let mut output_offset = 0usize;
let mut total_out = 0usize;
let (code, _) = WriteRingBuffer(&mut available_out, None, &mut output_offset,
&mut total_out, false, &mut s);
match code {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1 => {}
other => panic!("expected BLOCK_LENGTH_1, got {:?}", other),
}
}
#[test]
fn write_ring_buffer_rejects_output_slice_that_cannot_hold_the_write() {
let mut s = state_with_ringbuffer(16, 4);
s.pos = 8; let mut available_out = 8usize;
let mut output = [0u8; 8];
let mut output_offset = 4usize;
let mut total_out = 0usize;
let (code, _) = WriteRingBuffer(&mut available_out, Some(&mut output[..]),
&mut output_offset, &mut total_out, false, &mut s);
match code {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS => {}
other => panic!("expected INVALID_ARGUMENTS, got {:?}", other),
}
}
#[test]
fn wrap_ring_buffer_rejects_state_it_cannot_split() {
let mut s = state_with_ringbuffer(16, 4);
s.should_wrap_ringbuffer = true;
s.pos = -1;
assert!(!WrapRingBuffer(&mut s));
s.pos = 0;
s.ringbuffer_size = -1;
assert!(!WrapRingBuffer(&mut s));
s.ringbuffer_size = 64;
s.pos = 1;
assert!(!WrapRingBuffer(&mut s));
s.ringbuffer_size = 16;
s.pos = 8;
assert!(!WrapRingBuffer(&mut s));
let mut ok = state_with_ringbuffer(16, 4);
ok.ringbuffer_size = 8;
ok.ringbuffer_mask = 7;
ok.pos = 4;
ok.should_wrap_ringbuffer = true;
assert!(WrapRingBuffer(&mut ok));
assert!(!ok.should_wrap_ringbuffer);
}
#[test]
fn read_huffman_code_rejects_offset_past_table() {
let mut s = state();
let mut table = [HuffmanCode::default(); 4];
let code = ReadHuffmanCode(256, 256, &mut table, 5, None, &mut s, &[]);
match code {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE => {}
other => panic!("expected HUFFMAN_SPACE, got {:?}", other),
}
}
#[test]
fn read_huffman_code_reports_a_simple_table_that_will_not_fit() {
let mut s = state();
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
s.symbol = 0;
let mut table = [HuffmanCode::default(); 8];
match ReadHuffmanCode(256, 256, &mut table, 0, None, &mut s, &[]) {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE => {}
other => panic!("expected HUFFMAN_SPACE, got {:?}", other),
}
}
#[test]
fn read_huffman_code_reports_a_complex_table_that_will_not_fit() {
let mut s = state();
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
s.symbol = 256;
s.space = 0;
s.code_length_histo[1] = 2;
let mut table = [HuffmanCode::default(); 8];
match ReadHuffmanCode(256, 256, &mut table, 0, None, &mut s, &[]) {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE => {}
other => panic!("expected HUFFMAN_SPACE, got {:?}", other),
}
}
#[test]
fn read_huffman_code_reports_a_code_length_histogram_mismatch() {
let mut s = state();
s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX;
s.sub_loop_counter = super::CODE_LENGTH_CODES as u32;
s.repeat = 1;
s.space = 32;
s.code_length_histo[2] = 9;
let mut table = [HuffmanCode::default(); 1080];
match ReadHuffmanCode(256, 256, &mut table, 0, None, &mut s, &[]) {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE => {}
other => panic!("expected HUFFMAN_SPACE, got {:?}", other),
}
}
#[test]
fn write_ring_buffer_rejects_a_pending_run_longer_than_the_ring() {
let mut s = state_with_ringbuffer(16, 4);
s.rb_roundtrips = 1;
s.pos = 16;
s.partial_pos_out = 4;
let mut available_out = 64usize;
let mut output = [0u8; 64];
let mut output_offset = 0usize;
let mut total_out = 0usize;
let (code, _) = WriteRingBuffer(&mut available_out, Some(&mut output[..]),
&mut output_offset, &mut total_out, false, &mut s);
match code {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE => {}
other => panic!("expected UNREACHABLE, got {:?}", other),
}
}
#[test]
fn write_ring_buffer_rejects_output_offset_that_would_overflow() {
let mut s = state_with_ringbuffer(16, 4);
s.pos = 8;
let mut available_out = 8usize;
let mut output_offset = usize::MAX - 1;
let mut total_out = 0usize;
let (code, _) = WriteRingBuffer(&mut available_out, None, &mut output_offset,
&mut total_out, false, &mut s);
match code {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS => {}
other => panic!("expected INVALID_ARGUMENTS, got {:?}", other),
}
}
#[test]
fn decode_context_map_rejects_mismatched_state() {
let mut s = state();
s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1;
match DecodeContextMap(4, true, &mut s, &[]) {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE => {}
other => panic!("expected UNREACHABLE, got {:?}", other),
}
s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2;
match DecodeContextMap(4, false, &mut s, &[]) {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE => {}
other => panic!("expected UNREACHABLE, got {:?}", other),
}
s.state = BrotliRunningState::BROTLI_STATE_UNINITED;
match DecodeContextMap(4, false, &mut s, &[]) {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE => {}
other => panic!("expected UNREACHABLE, got {:?}", other),
}
}
#[test]
fn block_switch_with_one_block_type_is_a_format_error() {
let mut s = state();
s.block_type_length_state.num_block_types = [1, 1, 1];
s.block_type_length_state.block_length = [0, 0, 0];
for &safe in [false, true].iter() {
for category in 0..3 {
let result = DecodeBlockTypeAndLength(
safe,
&mut s.block_type_length_state,
&mut s.br,
category,
&[],
);
match result {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH => {}
other => panic!("category {} safe {}: expected FORMAT_BLOCK_SWITCH, got {:?}",
category, safe, other),
}
}
}
for category in 0..3 {
let fbr = BitReaderSlice::new(&s.br, &[]).unwrap();
let (fbr, status) =
decode_block_type_and_length_slice(&mut s.block_type_length_state, fbr, category);
fbr.sync_to(&mut s.br);
match status {
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_SWITCH => {}
other => panic!("category {} slice: expected FORMAT_BLOCK_SWITCH, got {:?}",
category, other),
}
}
assert_eq!(s.br.avail_in, 0);
assert_eq!(s.buffer_length, 0);
}
}
#[cfg(all(test, feature="std"))]
mod metadata_block_tests {
use super::{read_metadata_block, BrotliDecoderErrorCode};
use ::bit_reader::{self, BrotliBitReader};
use ::state::BrotliMetadataCallback;
use std::vec::Vec;
#[derive(Default)]
struct Chunks(Vec<Vec<u8>>);
impl BrotliMetadataCallback for Chunks {
fn start(&mut self, _size: usize) {
panic!("read_metadata_block must not start a block");
}
fn chunk(&mut self, data: &[u8]) {
assert!(!data.is_empty());
self.0.push(data.to_vec());
}
}
const ACCUMULATOR_BYTES: u32 = ::core::mem::size_of::<bit_reader::reg_t>() as u32;
fn input() -> Vec<u8> {
(0..64u32).map(|i| (i * 37 + 11) as u8).collect()
}
fn reader(input: &[u8], buffered: u32) -> BrotliBitReader {
let mut br = BrotliBitReader::default();
bit_reader::BrotliInitBitReader(&mut br);
br.avail_in = input.len() as u32;
for _ in 0..buffered {
assert!(bit_reader::BrotliPullByte(&mut br, input));
}
br
}
fn copy(br: &BrotliBitReader) -> BrotliBitReader {
let mut copy = BrotliBitReader::default();
bit_reader::BrotliBitReaderRestoreState(&mut copy, &bit_reader::BrotliBitReaderSaveState(br));
copy
}
fn fields(br: &BrotliBitReader) -> (u64, u32, u32, u32) {
(br.val_ as u64, br.bit_pos_, br.next_in, br.avail_in)
}
fn run(br: &mut BrotliBitReader, remaining: &mut i32, input: &[u8], with_callback: bool)
-> (bool, Vec<Vec<u8>>) {
let mut chunks = Chunks::default();
let result = {
let mut cb: Option<&mut dyn BrotliMetadataCallback> =
if with_callback { Some(&mut chunks) } else { None };
read_metadata_block(br, remaining, input, &mut cb)
};
let done = match result {
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => true,
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT => false,
other => panic!("unexpected {:?}", other),
};
(done, chunks.0)
}
#[test]
fn zero_length_block_touches_nothing() {
let input = input();
for buffered in 0..ACCUMULATOR_BYTES + 1 {
let mut br = reader(&input, buffered);
let before = fields(&br);
let mut remaining = 0;
let (done, chunks) = run(&mut br, &mut remaining, &input, true);
assert!(done);
assert!(chunks.is_empty());
assert_eq!(fields(&br), before);
}
}
#[test]
fn block_inside_the_accumulator_leaves_the_rest_buffered() {
let input = input();
for buffered in 1..ACCUMULATOR_BYTES + 1 {
for len in 1..buffered + 1 {
let mut br = reader(&input, buffered);
let mut remaining = len as i32;
let (done, chunks) = run(&mut br, &mut remaining, &input, true);
assert!(done);
assert_eq!(remaining, 0);
assert_eq!(chunks, vec![input[..len as usize].to_vec()]);
assert_eq!((br.next_in, br.avail_in), (buffered, input.len() as u32 - buffered));
assert_eq!(bit_reader::BrotliGetAvailableBits(&br), (buffered - len) * 8);
for expected in &input[len as usize..buffered as usize + 4] {
let mut byte = 0u32;
assert!(bit_reader::BrotliSafeReadBits(&mut br, 8, &mut byte, &input));
assert_eq!(byte, *expected as u32);
}
}
}
}
#[test]
fn longer_block_drains_the_accumulator_then_reads_input_directly() {
let input = input();
for buffered in 0..ACCUMULATOR_BYTES + 1 {
for len in buffered + 1..48 {
let mut br = reader(&input, buffered);
let mut remaining = len as i32;
let (done, chunks) = run(&mut br, &mut remaining, &input, true);
assert!(done);
assert_eq!(remaining, 0);
let mut expected = Vec::new();
if buffered != 0 {
expected.push(input[..buffered as usize].to_vec());
}
expected.push(input[buffered as usize..len as usize].to_vec());
assert_eq!(chunks, expected);
assert_eq!((br.next_in, br.avail_in), (len, input.len() as u32 - len));
assert_eq!(bit_reader::BrotliGetAvailableBits(&br), 0);
let mut safe = copy(&br);
let mut byte = 0u32;
assert!(bit_reader::BrotliSafeReadBits(&mut safe, 8, &mut byte, &input));
assert_eq!(byte, input[len as usize] as u32);
let mut fast = copy(&br);
assert!(bit_reader::BrotliWarmupBitReader(&mut fast, &input));
for expected in &input[len as usize..len as usize + 8] {
bit_reader::BrotliFillBitWindow(&mut fast, 8, &input);
assert_eq!(bit_reader::BrotliReadBits(&mut fast, 8, &input), *expected as u32);
}
}
}
}
#[test]
fn block_longer_than_the_input_resumes_on_the_next_call() {
let input = input();
for buffered in 0..ACCUMULATOR_BYTES + 1 {
for available in buffered..buffered + 6 {
let len = available + 5;
let first = &input[..available as usize];
let mut br = reader(first, buffered);
let mut remaining = len as i32;
let (done, mut chunks) = run(&mut br, &mut remaining, first, true);
assert!(!done);
assert_eq!(remaining, 5);
assert_eq!(br.avail_in, 0);
assert_eq!(bit_reader::BrotliGetAvailableBits(&br), 0);
assert!(chunks.iter().all(|c| !c.is_empty()));
br.avail_in = (input.len() - available as usize) as u32;
let (done, more) = run(&mut br, &mut remaining, &input, true);
assert!(done);
assert_eq!(more, vec![input[available as usize..len as usize].to_vec()]);
chunks.extend(more);
assert_eq!(chunks.concat(), &input[..len as usize]);
assert_eq!(br.next_in, len);
}
}
}
#[test]
fn no_callback_has_the_same_effect_on_the_reader() {
let input = input();
for buffered in 0..ACCUMULATOR_BYTES + 1 {
for len in 0..20i32 {
for &available in [input.len() as u32, buffered + 2].iter() {
let limited = &input[..available as usize];
let mut with = reader(limited, buffered);
let mut without = copy(&with);
let (mut with_remaining, mut without_remaining) = (len, len);
let (with_done, _) = run(&mut with, &mut with_remaining, limited, true);
let (without_done, chunks) = run(&mut without, &mut without_remaining, limited, false);
assert!(chunks.is_empty());
assert_eq!(with_done, without_done);
assert_eq!(with_remaining, without_remaining);
assert_eq!(fields(&with), fields(&without));
}
}
}
}
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "assertion")]
fn unaligned_accumulator_is_a_precondition_violation() {
let input = input();
let mut br = reader(&input, 2);
let mut bits = 0u32;
assert!(bit_reader::BrotliSafeReadBits(&mut br, 3, &mut bits, &input));
let mut remaining = 4;
run(&mut br, &mut remaining, &input, true);
}
}