Coverage Report

Created: 2025-12-11 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/brotli-decompressor-5.0.0/src/decode.rs
Line
Count
Source
1
#![allow(non_snake_case)]
2
#![allow(unused_parens)]
3
#![allow(non_camel_case_types)]
4
#![allow(non_snake_case)]
5
#![allow(non_upper_case_globals)]
6
#![allow(unused_macros)]
7
8
// #[macro_use] //<- for debugging, remove xprintln from bit_reader and replace with println
9
// extern crate std;
10
use core;
11
use super::alloc;
12
pub use alloc::{AllocatedStackMemory, Allocator, SliceWrapper, SliceWrapperMut, StackAllocator};
13
14
use core::mem;
15
16
use super::bit_reader;
17
use super::huffman;
18
use super::state;
19
use super::prefix;
20
21
use super::transform::{TransformDictionaryWord, kNumTransforms};
22
use state::{BlockTypeAndLengthState, BrotliRunningContextMapState, BrotliRunningDecodeUint8State,
23
            BrotliRunningHuffmanState, BrotliRunningMetablockHeaderState,
24
            BrotliRunningReadBlockLengthState, BrotliRunningState, BrotliRunningTreeGroupState,
25
            BrotliRunningUncompressedState, kLiteralContextBits,
26
            BrotliDecoderErrorCode,
27
};
28
use context::{kContextLookup};
29
use ::dictionary::{kBrotliDictionary, kBrotliDictionaryOffsetsByLength,
30
                   kBrotliDictionarySizeBitsByLength, kBrotliMaxDictionaryWordLength,
31
                   kBrotliMinDictionaryWordLength};
32
pub use huffman::{HuffmanCode, HuffmanTreeGroup};
33
#[repr(C)]
34
#[derive(Debug)]
35
pub enum BrotliResult {
36
  ResultSuccess = 1,
37
  NeedsMoreInput = 2,
38
  NeedsMoreOutput = 3,
39
  ResultFailure = 0,
40
}
41
const kBrotliWindowGap: u32 = 16;
42
const kBrotliLargeMinWbits: u32 = 10;
43
const kBrotliLargeMaxWbits: u32 = 30;
44
const kBrotliMaxPostfix: usize = 3;
45
const kBrotliMaxAllowedDistance: u32 = 0x7FFFFFFC;
46
const kDefaultCodeLength: u32 = 8;
47
const kCodeLengthRepeatCode: u32 = 16;
48
pub const kNumLiteralCodes: u16 = 256;
49
pub const kNumInsertAndCopyCodes: u16 = 704;
50
pub const kNumBlockLengthCodes: u32 = 26;
51
const kDistanceContextBits: i32 = 2;
52
const HUFFMAN_TABLE_BITS: u32 = 8;
53
const HUFFMAN_TABLE_MASK: u32 = 0xff;
54
const CODE_LENGTH_CODES: usize = 18;
55
const kCodeLengthCodeOrder: [u8; CODE_LENGTH_CODES] = [1, 2, 3, 4, 0, 5, 17, 6, 16, 7, 8, 9, 10,
56
                                                       11, 12, 13, 14, 15];
57
58
// Static prefix code for the complex code length code lengths.
59
const kCodeLengthPrefixLength: [u8; 16] = [2, 2, 2, 3, 2, 2, 2, 4, 2, 2, 2, 3, 2, 2, 2, 4];
60
61
const kCodeLengthPrefixValue: [u8; 16] = [0, 4, 3, 2, 0, 4, 3, 1, 0, 4, 3, 2, 0, 4, 3, 5];
62
63
64
macro_rules! BROTLI_LOG_UINT (
65
    ($num : expr) => {
66
       xprintln!("{:?} = {:?}", stringify!($num),  $num)
67
    };
68
);
69
70
macro_rules! BROTLI_LOG (
71
    ($str : expr, $num : expr) => {xprintln!("{:?} {:?}", $str, $num);};
72
    ($str : expr, $num0 : expr, $num1 : expr) => {xprintln!("{:?} {:?} {:?}", $str, $num0, $num1);};
73
    ($str : expr, $num0 : expr, $num1 : expr, $num2 : expr) => {
74
        xprintln!("{:?} {:?} {:?} {:?}", $str, $num0, $num1, $num2);
75
    };
76
    ($str : expr, $num0 : expr, $num1 : expr, $num2 : expr, $num3 : expr) => {
77
        xprintln!("{:?} {:?} {:?} {:?} {:?}", $str, $num0, $num1, $num2, $num3);
78
    };
79
);
80
175M
fn is_fatal(e: BrotliDecoderErrorCode) -> bool {
81
175M
  (e as i64) < 0
82
175M
}
83
171M
fn assign_error_code(output: &mut BrotliDecoderErrorCode, input: BrotliDecoderErrorCode) -> BrotliDecoderErrorCode {
84
171M
  *output = input;
85
171M
  input
86
171M
}
87
88
#[allow(non_snake_case)]
89
macro_rules! SaveErrorCode {
90
  ($state: expr, $e:expr) => {
91
    match assign_error_code(&mut $state.error_code, $e) {
92
      BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS =>
93
        BrotliResult::ResultSuccess,
94
      BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT =>
95
        BrotliResult::NeedsMoreInput,
96
      BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT =>
97
        BrotliResult::NeedsMoreOutput,
98
      _ =>
99
        BrotliResult::ResultFailure,
100
    }
101
  }
102
}
103
macro_rules! SaveResult {
104
  ($state: expr, $e:expr) => {
105
    match ($state.error_code = match $e  {
106
      BrotliResult::ResultSuccess => BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS,
107
      BrotliResult::NeedsMoreInput => BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT,
108
      BrotliResult::NeedsMoreOutput => BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT,
109
      BrotliResult::ResultFailure => BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE,
110
    }) {
111
      BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS =>
112
        BrotliResult::ResultSuccess,
113
      BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT =>
114
        BrotliResult::NeedsMoreInput,
115
      BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT =>
116
        BrotliResult::NeedsMoreOutput,
117
      _ =>
118
        BrotliResult::ResultFailure,
119
    }
120
  }
121
}
122
macro_rules! BROTLI_LOG_ARRAY_INDEX (
123
    ($array : expr, $index : expr) => {
124
       xprintln!("{:?}[{:?}] = {:?}", stringify!($array), $index,  $array[$index as usize])
125
    };
126
);
127
128
129
const NUM_DISTANCE_SHORT_CODES: u32 = 16;
130
pub const BROTLI_MAX_DISTANCE_BITS:u32 = 24;
131
132
pub const BROTLI_LARGE_MAX_DISTANCE_BITS: u32 = 62;
133
134
51.8k
pub fn BROTLI_DISTANCE_ALPHABET_SIZE(NPOSTFIX: u32, NDIRECT:u32, MAXNBITS: u32) -> u32 {
135
51.8k
    NUM_DISTANCE_SHORT_CODES + (NDIRECT) +
136
51.8k
        ((MAXNBITS) << ((NPOSTFIX) + 1))
137
51.8k
}
138
139
// pub struct BrotliState {
140
// total_written : usize,
141
// }
142
//
143
pub use state::BrotliState;
144
// impl BrotliState {
145
// pub fn new() -> BrotliState {
146
// return BrotliState {total_written: 0 };
147
// }
148
// }
149
150
/* Decodes WBITS by reading 1 - 7 bits, or 0x11 for "Large Window Brotli".
151
   Precondition: bit-reader accumulator has at least 8 bits. */
152
17.4k
fn DecodeWindowBits(s_large_window: &mut bool,
153
17.4k
                    s_window_bits:&mut u32,
154
17.4k
                    br: &mut bit_reader::BrotliBitReader) -> BrotliDecoderErrorCode {
155
17.4k
  let mut n: u32 = 0;
156
17.4k
  let large_window = *s_large_window;
157
17.4k
  *s_large_window = false;
158
17.4k
  bit_reader::BrotliTakeBits(br, 1, &mut n);
159
17.4k
  if (n == 0) {
160
3.47k
    *s_window_bits = 16;
161
3.47k
    return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
162
14.0k
  }
163
14.0k
  bit_reader::BrotliTakeBits(br, 3, &mut n);
164
14.0k
  if (n != 0) {
165
8.86k
    *s_window_bits = 17 + n;
166
8.86k
    return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
167
5.15k
  }
168
5.15k
  bit_reader::BrotliTakeBits(br, 3, &mut n);
169
5.15k
  if (n == 1) {
170
202
    if (large_window) {
171
202
      bit_reader::BrotliTakeBits(br, 1, &mut n);
172
202
      if (n == 1) {
173
1
        return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
174
201
      }
175
201
      *s_large_window = true;
176
201
      return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
177
    } else {
178
0
      return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
179
    }
180
4.95k
  }
181
4.95k
  if (n != 0) {
182
4.88k
    *s_window_bits = 8 + n;
183
4.88k
    return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
184
74
  }
185
74
  *s_window_bits = 17;
186
74
  return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
187
17.4k
}
188
189
190
#[cold]
191
542M
fn mark_unlikely() {}
192
193
259k
fn DecodeVarLenUint8(substate_decode_uint8: &mut state::BrotliRunningDecodeUint8State,
194
259k
                     mut br: &mut bit_reader::BrotliBitReader,
195
259k
                     value: &mut u32,
196
259k
                     input: &[u8])
197
259k
                     -> BrotliDecoderErrorCode {
198
259k
  let mut bits: u32 = 0;
199
  loop {
200
292k
    match *substate_decode_uint8 {
201
      BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_NONE => {
202
259k
        if !bit_reader::BrotliSafeReadBits(&mut br, 1, &mut bits, input) {
203
2.56k
          mark_unlikely();
204
2.56k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
205
256k
        }
206
256k
        if (bits == 0) {
207
236k
          *value = 0;
208
236k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
209
19.7k
        }
210
19.7k
        *substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_SHORT;
211
        // No break, transit to the next state.
212
      }
213
      BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_SHORT => {
214
19.9k
        if !bit_reader::BrotliSafeReadBits(&mut br, 3, &mut bits, input) {
215
268
          mark_unlikely();
216
268
          *substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_SHORT;
217
268
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
218
19.6k
        }
219
19.6k
        if (bits == 0) {
220
6.32k
          *value = 1;
221
6.32k
          *substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_NONE;
222
6.32k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
223
13.3k
        }
224
        // Use output value as a temporary storage. It MUST be persisted.
225
13.3k
        *value = bits;
226
        // No break, transit to the next state.
227
13.3k
        *substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_LONG;
228
      }
229
      BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_LONG => {
230
13.4k
        if !bit_reader::BrotliSafeReadBits(&mut br, *value, &mut bits, input) {
231
157
          mark_unlikely();
232
157
          *substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_LONG;
233
157
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
234
13.3k
        }
235
13.3k
        *value = (1u32 << *value) + bits;
236
13.3k
        *substate_decode_uint8 = BrotliRunningDecodeUint8State::BROTLI_STATE_DECODE_UINT8_NONE;
237
13.3k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
238
      }
239
    }
240
  }
241
259k
}
242
243
84.4k
fn DecodeMetaBlockLength<AllocU8: alloc::Allocator<u8>,
244
84.4k
                         AllocU32: alloc::Allocator<u32>,
245
84.4k
                         AllocHC: alloc::Allocator<HuffmanCode>>
246
84.4k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
247
84.4k
   input: &[u8])
248
84.4k
   -> BrotliDecoderErrorCode {
249
84.4k
  let mut bits: u32 = 0;
250
  loop {
251
298k
    match s.substate_metablock_header {
252
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE => {
253
71.8k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
254
1.71k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
255
70.1k
        }
256
70.1k
        s.is_last_metablock = bits as u8;
257
70.1k
        s.meta_block_remaining_len = 0;
258
70.1k
        s.is_uncompressed = 0;
259
70.1k
        s.is_metadata = 0;
260
70.1k
        if (s.is_last_metablock == 0) {
261
56.1k
          s.substate_metablock_header =
262
56.1k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
263
56.1k
          continue;
264
13.9k
        }
265
13.9k
        s.substate_metablock_header =
266
13.9k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_EMPTY;
267
        // No break, transit to the next state.
268
      }
269
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_EMPTY => {
270
14.1k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
271
201
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
272
13.9k
        }
273
13.9k
        if bits != 0 {
274
4.00k
          s.substate_metablock_header =
275
4.00k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
276
4.00k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
277
9.96k
        }
278
9.96k
        s.substate_metablock_header =
279
9.96k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
280
        // No break, transit to the next state.
281
      }
282
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES => {
283
67.6k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input) {
284
1.58k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
285
66.1k
        }
286
66.1k
        s.size_nibbles = (bits + 4) as u8;
287
66.1k
        s.loop_counter = 0;
288
66.1k
        if (bits == 3) {
289
1.74k
          s.is_metadata = 1;
290
1.74k
          s.substate_metablock_header =
291
1.74k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_RESERVED;
292
1.74k
          continue;
293
64.3k
        }
294
64.3k
        s.substate_metablock_header =
295
64.3k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_SIZE;
296
        // No break, transit to the next state.
297
298
      }
299
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_SIZE => {
300
74.7k
        let mut i = s.loop_counter;
301
335k
        while i < s.size_nibbles as i32 {
302
271k
          if !bit_reader::BrotliSafeReadBits(&mut s.br, 4, &mut bits, input) {
303
10.4k
            s.loop_counter = i;
304
10.4k
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
305
261k
          }
306
261k
          if (i + 1 == s.size_nibbles as i32 && s.size_nibbles > 4 && bits == 0) {
307
11
            return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE;
308
261k
          }
309
261k
          s.meta_block_remaining_len |= (bits << (i * 4)) as i32;
310
261k
          i += 1;
311
        }
312
64.2k
        s.substate_metablock_header =
313
64.2k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
314
        // No break, transit to the next state.
315
      }
316
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED => {
317
65.3k
        if (s.is_last_metablock == 0 && s.is_metadata == 0) {
318
54.7k
          if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
319
405
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
320
54.3k
          }
321
54.3k
          s.is_uncompressed = bits as u8;
322
10.5k
        }
323
64.9k
        s.meta_block_remaining_len += 1;
324
64.9k
        s.substate_metablock_header =
325
64.9k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
326
64.9k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
327
      }
328
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_RESERVED => {
329
1.75k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
330
16
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
331
1.74k
        }
332
1.74k
        if (bits != 0) {
333
10
          return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_RESERVED;
334
1.73k
        }
335
1.73k
        s.substate_metablock_header =
336
1.73k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_BYTES;
337
        // No break, transit to the next state.
338
      }
339
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_BYTES => {
340
1.73k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input) {
341
8
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
342
1.72k
        }
343
1.72k
        if (bits == 0) {
344
1.04k
          s.substate_metablock_header =
345
1.04k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
346
1.04k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
347
684
        }
348
684
        s.size_nibbles = bits as u8;
349
684
        s.substate_metablock_header =
350
684
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_METADATA;
351
        // No break, transit to the next state.
352
      }
353
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_METADATA => {
354
704
        let mut i = s.loop_counter;
355
1.52k
        while i < s.size_nibbles as i32 {
356
858
          if !bit_reader::BrotliSafeReadBits(&mut s.br, 8, &mut bits, input) {
357
40
            s.loop_counter = i;
358
40
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
359
818
          }
360
818
          if (i + 1 == s.size_nibbles as i32 && s.size_nibbles > 1 && bits == 0) {
361
2
            return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE;
362
816
          }
363
816
          s.meta_block_remaining_len |= (bits << (i * 8)) as i32;
364
816
          i += 1;
365
        }
366
662
        s.substate_metablock_header =
367
662
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
368
662
        continue;
369
      }
370
    }
371
  }
372
84.4k
}
Unexecuted instantiation: brotli_decompressor::decode::DecodeMetaBlockLength::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DecodeMetaBlockLength::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
243
84.4k
fn DecodeMetaBlockLength<AllocU8: alloc::Allocator<u8>,
244
84.4k
                         AllocU32: alloc::Allocator<u32>,
245
84.4k
                         AllocHC: alloc::Allocator<HuffmanCode>>
246
84.4k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
247
84.4k
   input: &[u8])
248
84.4k
   -> BrotliDecoderErrorCode {
249
84.4k
  let mut bits: u32 = 0;
250
  loop {
251
298k
    match s.substate_metablock_header {
252
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE => {
253
71.8k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
254
1.71k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
255
70.1k
        }
256
70.1k
        s.is_last_metablock = bits as u8;
257
70.1k
        s.meta_block_remaining_len = 0;
258
70.1k
        s.is_uncompressed = 0;
259
70.1k
        s.is_metadata = 0;
260
70.1k
        if (s.is_last_metablock == 0) {
261
56.1k
          s.substate_metablock_header =
262
56.1k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
263
56.1k
          continue;
264
13.9k
        }
265
13.9k
        s.substate_metablock_header =
266
13.9k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_EMPTY;
267
        // No break, transit to the next state.
268
      }
269
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_EMPTY => {
270
14.1k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
271
201
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
272
13.9k
        }
273
13.9k
        if bits != 0 {
274
4.00k
          s.substate_metablock_header =
275
4.00k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
276
4.00k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
277
9.96k
        }
278
9.96k
        s.substate_metablock_header =
279
9.96k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES;
280
        // No break, transit to the next state.
281
      }
282
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NIBBLES => {
283
67.6k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input) {
284
1.58k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
285
66.1k
        }
286
66.1k
        s.size_nibbles = (bits + 4) as u8;
287
66.1k
        s.loop_counter = 0;
288
66.1k
        if (bits == 3) {
289
1.74k
          s.is_metadata = 1;
290
1.74k
          s.substate_metablock_header =
291
1.74k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_RESERVED;
292
1.74k
          continue;
293
64.3k
        }
294
64.3k
        s.substate_metablock_header =
295
64.3k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_SIZE;
296
        // No break, transit to the next state.
297
298
      }
299
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_SIZE => {
300
74.7k
        let mut i = s.loop_counter;
301
335k
        while i < s.size_nibbles as i32 {
302
271k
          if !bit_reader::BrotliSafeReadBits(&mut s.br, 4, &mut bits, input) {
303
10.4k
            s.loop_counter = i;
304
10.4k
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
305
261k
          }
306
261k
          if (i + 1 == s.size_nibbles as i32 && s.size_nibbles > 4 && bits == 0) {
307
11
            return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE;
308
261k
          }
309
261k
          s.meta_block_remaining_len |= (bits << (i * 4)) as i32;
310
261k
          i += 1;
311
        }
312
64.2k
        s.substate_metablock_header =
313
64.2k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
314
        // No break, transit to the next state.
315
      }
316
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED => {
317
65.3k
        if (s.is_last_metablock == 0 && s.is_metadata == 0) {
318
54.7k
          if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
319
405
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
320
54.3k
          }
321
54.3k
          s.is_uncompressed = bits as u8;
322
10.5k
        }
323
64.9k
        s.meta_block_remaining_len += 1;
324
64.9k
        s.substate_metablock_header =
325
64.9k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
326
64.9k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
327
      }
328
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_RESERVED => {
329
1.75k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input) {
330
16
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
331
1.74k
        }
332
1.74k
        if (bits != 0) {
333
10
          return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_RESERVED;
334
1.73k
        }
335
1.73k
        s.substate_metablock_header =
336
1.73k
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_BYTES;
337
        // No break, transit to the next state.
338
      }
339
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_BYTES => {
340
1.73k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input) {
341
8
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
342
1.72k
        }
343
1.72k
        if (bits == 0) {
344
1.04k
          s.substate_metablock_header =
345
1.04k
            BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_NONE;
346
1.04k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
347
684
        }
348
684
        s.size_nibbles = bits as u8;
349
684
        s.substate_metablock_header =
350
684
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_METADATA;
351
        // No break, transit to the next state.
352
      }
353
      BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_METADATA => {
354
704
        let mut i = s.loop_counter;
355
1.52k
        while i < s.size_nibbles as i32 {
356
858
          if !bit_reader::BrotliSafeReadBits(&mut s.br, 8, &mut bits, input) {
357
40
            s.loop_counter = i;
358
40
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
359
818
          }
360
818
          if (i + 1 == s.size_nibbles as i32 && s.size_nibbles > 1 && bits == 0) {
361
2
            return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE;
362
816
          }
363
816
          s.meta_block_remaining_len |= (bits << (i * 8)) as i32;
364
816
          i += 1;
365
        }
366
662
        s.substate_metablock_header =
367
662
          BrotliRunningMetablockHeaderState::BROTLI_STATE_METABLOCK_HEADER_UNCOMPRESSED;
368
662
        continue;
369
      }
370
    }
371
  }
372
84.4k
}
373
// Decodes the Huffman code.
374
// This method doesn't read data from the bit reader, BUT drops the amount of
375
// bits that correspond to the decoded symbol.
376
// bits MUST contain at least 15 (BROTLI_HUFFMAN_MAX_CODE_LENGTH) valid bits.
377
#[inline(always)]
378
1.48G
fn DecodeSymbol(bits: u32, table: &[HuffmanCode], br: &mut bit_reader::BrotliBitReader) -> u32 {
379
1.48G
  let mut table_index = bits & HUFFMAN_TABLE_MASK;
380
1.48G
  let mut table_element = fast!((table)[table_index as usize]);
381
1.48G
  if table_element.bits > HUFFMAN_TABLE_BITS as u8 {
382
9.77M
    let nbits = table_element.bits - HUFFMAN_TABLE_BITS as u8;
383
9.77M
    bit_reader::BrotliDropBits(br, HUFFMAN_TABLE_BITS);
384
9.77M
    table_index += table_element.value as u32;
385
9.77M
    table_element = fast!((table)[(table_index
386
9.77M
                           + ((bits >> HUFFMAN_TABLE_BITS)
387
9.77M
                              & bit_reader::BitMask(nbits as u32))) as usize]);
388
1.47G
  }
389
1.48G
  bit_reader::BrotliDropBits(br, table_element.bits as u32);
390
1.48G
  table_element.value as u32
391
1.48G
}
392
393
// Reads and decodes the next Huffman code from bit-stream.
394
// This method peeks 16 bits of input and drops 0 - 15 of them.
395
#[inline(always)]
396
387M
fn ReadSymbol(table: &[HuffmanCode], br: &mut bit_reader::BrotliBitReader, input: &[u8]) -> u32 {
397
387M
  DecodeSymbol(bit_reader::BrotliGet16BitsUnmasked(br, input), table, br)
398
387M
}
399
400
// Same as DecodeSymbol, but it is known that there is less than 15 bits of
401
// input are currently available.
402
524M
fn SafeDecodeSymbol(table: &[HuffmanCode],
403
524M
                    mut br: &mut bit_reader::BrotliBitReader,
404
524M
                    result: &mut u32)
405
524M
                    -> bool {
406
524M
  let mut available_bits = bit_reader::BrotliGetAvailableBits(br);
407
524M
  if (available_bits == 0) {
408
44.7M
    if (fast!((table)[0]).bits == 0) {
409
44.1M
      *result = fast!((table)[0]).value as u32;
410
44.1M
      return true;
411
576k
    }
412
576k
    return false; /* No valid bits at all. */
413
479M
  }
414
479M
  let mut val = bit_reader::BrotliGetBitsUnmasked(br) as u32;
415
479M
  let table_index = (val & HUFFMAN_TABLE_MASK) as usize;
416
479M
  let table_element = fast!((table)[table_index]);
417
479M
  if (table_element.bits <= HUFFMAN_TABLE_BITS as u8) {
418
479M
    if (table_element.bits as u32 <= available_bits) {
419
476M
      bit_reader::BrotliDropBits(&mut br, table_element.bits as u32);
420
476M
      *result = table_element.value as u32;
421
476M
      return true;
422
    } else {
423
2.32M
      return false; /* Not enough bits for the first level. */
424
    }
425
349k
  }
426
349k
  if (available_bits <= HUFFMAN_TABLE_BITS) {
427
210k
    return false; /* Not enough bits to move to the second level. */
428
138k
  }
429
430
  // Speculatively drop HUFFMAN_TABLE_BITS.
431
138k
  val = (val & bit_reader::BitMask(table_element.bits as u32)) >> HUFFMAN_TABLE_BITS;
432
138k
  available_bits -= HUFFMAN_TABLE_BITS;
433
138k
  let table_sub_element = fast!((table)[table_index + table_element.value as usize + val as usize]);
434
138k
  if (available_bits < table_sub_element.bits as u32) {
435
11.5k
    return false; /* Not enough bits for the second level. */
436
127k
  }
437
438
127k
  bit_reader::BrotliDropBits(&mut br, HUFFMAN_TABLE_BITS + table_sub_element.bits as u32);
439
127k
  *result = table_sub_element.value as u32;
440
127k
  true
441
524M
}
442
443
1.61G
fn SafeReadSymbol(table: &[HuffmanCode],
444
1.61G
                  br: &mut bit_reader::BrotliBitReader,
445
1.61G
                  result: &mut u32,
446
1.61G
                  input: &[u8])
447
1.61G
                  -> bool {
448
1.61G
  let mut val: u32 = 0;
449
1.61G
  if (bit_reader::BrotliSafeGetBits(br, 15, &mut val, input)) {
450
1.09G
    *result = DecodeSymbol(val, table, br);
451
1.09G
    return true;
452
524M
  } else {
453
524M
    mark_unlikely();
454
524M
  }
455
524M
  SafeDecodeSymbol(table, br, result)
456
1.61G
}
457
458
// Makes a look-up in first level Huffman table. Peeks 8 bits.
459
540M
fn PreloadSymbol(safe: bool,
460
540M
                 table: &[HuffmanCode],
461
540M
                 br: &mut bit_reader::BrotliBitReader,
462
540M
                 bits: &mut u32,
463
540M
                 value: &mut u32,
464
540M
                 input: &[u8]) {
465
540M
  if (safe) {
466
58.5M
    return;
467
482M
  }
468
482M
  let table_element =
469
482M
    fast!((table)[bit_reader::BrotliGetBits(br, HUFFMAN_TABLE_BITS, input) as usize]);
470
482M
  *bits = table_element.bits as u32;
471
482M
  *value = table_element.value as u32;
472
540M
}
473
474
// Decodes the next Huffman code using data prepared by PreloadSymbol.
475
// Reads 0 - 15 bits. Also peeks 8 following bits.
476
438M
fn ReadPreloadedSymbol(table: &[HuffmanCode],
477
438M
                       br: &mut bit_reader::BrotliBitReader,
478
438M
                       bits: &mut u32,
479
438M
                       value: &mut u32,
480
438M
                       input: &[u8])
481
438M
                       -> u32 {
482
438M
  let result = if *bits > HUFFMAN_TABLE_BITS {
483
4.09M
    mark_unlikely();
484
4.09M
    let val = bit_reader::BrotliGet16BitsUnmasked(br, input);
485
4.09M
    let mut ext_index = (val & HUFFMAN_TABLE_MASK) + *value;
486
4.09M
    let mask = bit_reader::BitMask((*bits - HUFFMAN_TABLE_BITS));
487
4.09M
    bit_reader::BrotliDropBits(br, HUFFMAN_TABLE_BITS);
488
4.09M
    ext_index += (val >> HUFFMAN_TABLE_BITS) & mask;
489
4.09M
    let ext = fast!((table)[ext_index as usize]);
490
4.09M
    bit_reader::BrotliDropBits(br, ext.bits as u32);
491
4.09M
    ext.value as u32
492
  } else {
493
434M
    bit_reader::BrotliDropBits(br, *bits);
494
434M
    *value
495
  };
496
438M
  PreloadSymbol(false, table, br, bits, value, input);
497
438M
  result
498
438M
}
499
500
157k
fn Log2Floor(mut x: u32) -> u32 {
501
157k
  let mut result: u32 = 0;
502
1.34M
  while x != 0 {
503
1.18M
    x >>= 1;
504
1.18M
    result += 1;
505
1.18M
  }
506
157k
  result
507
157k
}
508
509
510
// Reads (s->symbol + 1) symbols.
511
// Totally 1..4 symbols are read, 1..11 bits each.
512
// The list of symbols MUST NOT contain duplicates.
513
//
514
157k
fn ReadSimpleHuffmanSymbols<AllocU8: alloc::Allocator<u8>,
515
157k
                            AllocU32: alloc::Allocator<u32>,
516
157k
                            AllocHC: alloc::Allocator<HuffmanCode>>
517
157k
  (alphabet_size: u32, max_symbol: u32,
518
157k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
519
157k
   input: &[u8])
520
157k
   -> BrotliDecoderErrorCode {
521
522
  // max_bits == 1..11; symbol == 0..3; 1..44 bits will be read.
523
157k
  let max_bits = Log2Floor(alphabet_size - 1);
524
157k
  let mut i = s.sub_loop_counter;
525
157k
  let num_symbols = s.symbol;
526
329k
  for symbols_lists_item in fast_mut!((s.symbols_lists_array)[s.sub_loop_counter as usize;
527
157k
                                                  num_symbols as usize + 1])
528
157k
    .iter_mut() {
529
329k
    let mut v: u32 = 0;
530
329k
    if !bit_reader::BrotliSafeReadBits(&mut s.br, max_bits, &mut v, input) {
531
10.3k
      mark_unlikely();
532
10.3k
      s.sub_loop_counter = i;
533
10.3k
      s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ;
534
10.3k
      return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
535
318k
    }
536
318k
    if (v >= max_symbol) {
537
29
      return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET;
538
318k
    }
539
318k
    *symbols_lists_item = v as u16;
540
    BROTLI_LOG_UINT!(v);
541
318k
    i += 1;
542
  }
543
146k
  i = 0;
544
172k
  for symbols_list_item in fast!((s.symbols_lists_array)[0; num_symbols as usize]).iter() {
545
282k
    for other_item in fast!((s.symbols_lists_array)[i as usize + 1 ; num_symbols as usize+ 1])
546
172k
      .iter() {
547
282k
      if (*symbols_list_item == *other_item) {
548
12
        return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME;
549
282k
      }
550
    }
551
172k
    i += 1;
552
  }
553
146k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
554
157k
}
Unexecuted instantiation: brotli_decompressor::decode::ReadSimpleHuffmanSymbols::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ReadSimpleHuffmanSymbols::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
514
157k
fn ReadSimpleHuffmanSymbols<AllocU8: alloc::Allocator<u8>,
515
157k
                            AllocU32: alloc::Allocator<u32>,
516
157k
                            AllocHC: alloc::Allocator<HuffmanCode>>
517
157k
  (alphabet_size: u32, max_symbol: u32,
518
157k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
519
157k
   input: &[u8])
520
157k
   -> BrotliDecoderErrorCode {
521
522
  // max_bits == 1..11; symbol == 0..3; 1..44 bits will be read.
523
157k
  let max_bits = Log2Floor(alphabet_size - 1);
524
157k
  let mut i = s.sub_loop_counter;
525
157k
  let num_symbols = s.symbol;
526
329k
  for symbols_lists_item in fast_mut!((s.symbols_lists_array)[s.sub_loop_counter as usize;
527
157k
                                                  num_symbols as usize + 1])
528
157k
    .iter_mut() {
529
329k
    let mut v: u32 = 0;
530
329k
    if !bit_reader::BrotliSafeReadBits(&mut s.br, max_bits, &mut v, input) {
531
10.3k
      mark_unlikely();
532
10.3k
      s.sub_loop_counter = i;
533
10.3k
      s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ;
534
10.3k
      return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
535
318k
    }
536
318k
    if (v >= max_symbol) {
537
29
      return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET;
538
318k
    }
539
318k
    *symbols_lists_item = v as u16;
540
    BROTLI_LOG_UINT!(v);
541
318k
    i += 1;
542
  }
543
146k
  i = 0;
544
172k
  for symbols_list_item in fast!((s.symbols_lists_array)[0; num_symbols as usize]).iter() {
545
282k
    for other_item in fast!((s.symbols_lists_array)[i as usize + 1 ; num_symbols as usize+ 1])
546
172k
      .iter() {
547
282k
      if (*symbols_list_item == *other_item) {
548
12
        return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME;
549
282k
      }
550
    }
551
172k
    i += 1;
552
  }
553
146k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
554
157k
}
555
556
// Process single decoded symbol code length:
557
// A) reset the repeat variable
558
// B) remember code length (if it is not 0)
559
// C) extend corredponding index-chain
560
// D) reduce the huffman space
561
// E) update the histogram
562
//
563
5.78M
fn ProcessSingleCodeLength(code_len: u32,
564
5.78M
                           symbol: &mut u32,
565
5.78M
                           repeat: &mut u32,
566
5.78M
                           space: &mut u32,
567
5.78M
                           prev_code_len: &mut u32,
568
5.78M
                           symbol_lists: &mut [u16],
569
5.78M
                           symbol_list_index_offset: usize,
570
5.78M
                           code_length_histo: &mut [u16],
571
5.78M
                           next_symbol: &mut [i32]) {
572
5.78M
  *repeat = 0;
573
5.78M
  if (code_len != 0) {
574
5.00M
    // code_len == 1..15
575
5.00M
    // next_symbol may be negative, hence we have to supply offset to function
576
5.00M
    fast_mut!((symbol_lists)[(symbol_list_index_offset as i32 +
577
5.00M
                             fast_inner!((next_symbol)[code_len as usize])) as usize]) =
578
5.00M
      (*symbol) as u16;
579
5.00M
    fast_mut!((next_symbol)[code_len as usize]) = (*symbol) as i32;
580
5.00M
    *prev_code_len = code_len;
581
5.00M
    *space = space.wrapping_sub(32768 >> code_len);
582
5.00M
    fast_mut!((code_length_histo)[code_len as usize]) += 1;
583
5.00M
    BROTLI_LOG!("[ReadHuffmanCode] code_length[{:}]={:} histo[]={:}\n",
584
5.00M
                *symbol, code_len, code_length_histo[code_len as usize]);
585
5.00M
  }
586
5.78M
  (*symbol) += 1;
587
5.78M
}
588
589
// Process repeated symbol code length.
590
// A) Check if it is the extension of previous repeat sequence; if the decoded
591
// value is not kCodeLengthRepeatCode, then it is a new symbol-skip
592
// B) Update repeat variable
593
// C) Check if operation is feasible (fits alphapet)
594
// D) For each symbol do the same operations as in ProcessSingleCodeLength
595
//
596
// PRECONDITION: code_len == kCodeLengthRepeatCode or kCodeLengthRepeatCode + 1
597
//
598
2.90M
fn ProcessRepeatedCodeLength(code_len: u32,
599
2.90M
                             mut repeat_delta: u32,
600
2.90M
                             alphabet_size: u32,
601
2.90M
                             symbol: &mut u32,
602
2.90M
                             repeat: &mut u32,
603
2.90M
                             space: &mut u32,
604
2.90M
                             prev_code_len: &mut u32,
605
2.90M
                             repeat_code_len: &mut u32,
606
2.90M
                             symbol_lists: &mut [u16],
607
2.90M
                             symbol_lists_index: usize,
608
2.90M
                             code_length_histo: &mut [u16],
609
2.90M
                             next_symbol: &mut [i32]) {
610
  let old_repeat: u32;
611
  let extra_bits: u32;
612
  let new_len: u32;
613
2.90M
  if (code_len == kCodeLengthRepeatCode) {
614
721k
    extra_bits = 2;
615
721k
    new_len = *prev_code_len
616
  } else {
617
2.18M
    extra_bits = 3;
618
2.18M
    new_len = 0
619
  }
620
2.90M
  if (*repeat_code_len != new_len) {
621
602k
    *repeat = 0;
622
602k
    *repeat_code_len = new_len;
623
2.30M
  }
624
2.90M
  old_repeat = *repeat;
625
2.90M
  if (*repeat > 0) {
626
846k
    *repeat -= 2;
627
846k
    *repeat <<= extra_bits;
628
2.05M
  }
629
2.90M
  *repeat += repeat_delta + 3;
630
2.90M
  repeat_delta = *repeat - old_repeat;
631
2.90M
  if (*symbol + repeat_delta > alphabet_size) {
632
75
    *symbol = alphabet_size;
633
75
    *space = 0xFFFFF;
634
75
    return;
635
2.90M
  }
636
  BROTLI_LOG!("[ReadHuffmanCode] code_length[{:}..{:}] = {:}\n",
637
              *symbol, *symbol + repeat_delta - 1, *repeat_code_len);
638
2.90M
  if (*repeat_code_len != 0) {
639
721k
    let last: u32 = *symbol + repeat_delta;
640
721k
    let mut next: i32 = fast!((next_symbol)[*repeat_code_len as usize]);
641
    loop {
642
5.46M
      fast_mut!((symbol_lists)[(symbol_lists_index as i32 + next) as usize]) = (*symbol) as u16;
643
5.46M
      next = (*symbol) as i32;
644
5.46M
      (*symbol) += 1;
645
5.46M
      if *symbol == last {
646
721k
        break;
647
4.74M
      }
648
    }
649
721k
    fast_mut!((next_symbol)[*repeat_code_len as usize]) = next;
650
721k
    *space = space.wrapping_sub(repeat_delta << (15 - *repeat_code_len));
651
721k
    fast_mut!((code_length_histo)[*repeat_code_len as usize]) =
652
721k
      (fast!((code_length_histo)[*repeat_code_len as usize]) as u32 + repeat_delta) as u16;
653
2.18M
  } else {
654
2.18M
    *symbol += repeat_delta;
655
2.18M
  }
656
2.90M
}
657
658
// Reads and decodes symbol codelengths.
659
526k
fn ReadSymbolCodeLengths<AllocU8: alloc::Allocator<u8>,
660
526k
                         AllocU32: alloc::Allocator<u32>,
661
526k
                         AllocHC: alloc::Allocator<HuffmanCode>>
662
526k
  (alphabet_size: u32,
663
526k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
664
526k
   input: &[u8])
665
526k
   -> BrotliDecoderErrorCode {
666
667
526k
  let mut symbol = s.symbol;
668
526k
  let mut repeat = s.repeat;
669
526k
  let mut space = s.space;
670
526k
  let mut prev_code_len: u32 = s.prev_code_len;
671
526k
  let mut repeat_code_len: u32 = s.repeat_code_len;
672
526k
  if (!bit_reader::BrotliWarmupBitReader(&mut s.br, input)) {
673
2.12k
    return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
674
524k
  }
675
8.23M
  while (symbol < alphabet_size && space > 0) {
676
8.06M
    let mut p_index = 0;
677
    let code_len: u32;
678
8.06M
    if (!bit_reader::BrotliCheckInputAmount(&s.br, bit_reader::BROTLI_SHORT_FILL_BIT_WINDOW_READ)) {
679
359k
      s.symbol = symbol;
680
359k
      s.repeat = repeat;
681
359k
      s.prev_code_len = prev_code_len;
682
359k
      s.repeat_code_len = repeat_code_len;
683
359k
      s.space = space;
684
359k
      return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
685
7.70M
    }
686
7.70M
    bit_reader::BrotliFillBitWindow16(&mut s.br, input);
687
7.70M
    p_index +=
688
7.70M
      bit_reader::BrotliGetBitsUnmasked(&s.br) &
689
7.70M
      bit_reader::BitMask(huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as u32) as u64;
690
7.70M
    let p = fast!((s.table)[p_index as usize]);
691
7.70M
    bit_reader::BrotliDropBits(&mut s.br, p.bits as u32); /* Use 1..5 bits */
692
7.70M
    code_len = p.value as u32; /* code_len == 0..17 */
693
7.70M
    if (code_len < kCodeLengthRepeatCode) {
694
5.12M
      ProcessSingleCodeLength(code_len,
695
5.12M
                              &mut symbol,
696
5.12M
                              &mut repeat,
697
5.12M
                              &mut space,
698
5.12M
                              &mut prev_code_len,
699
5.12M
                              &mut s.symbols_lists_array,
700
5.12M
                              s.symbol_lists_index as usize,
701
5.12M
                              &mut s.code_length_histo[..],
702
5.12M
                              &mut s.next_symbol[..]);
703
5.12M
    } else {
704
      // code_len == 16..17, extra_bits == 2..3
705
2.58M
      let extra_bits: u32 = if code_len == kCodeLengthRepeatCode {
706
660k
        2
707
      } else {
708
1.92M
        3
709
      };
710
2.58M
      let repeat_delta: u32 = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32 &
711
2.58M
                              bit_reader::BitMask(extra_bits);
712
2.58M
      bit_reader::BrotliDropBits(&mut s.br, extra_bits);
713
2.58M
      ProcessRepeatedCodeLength(code_len,
714
2.58M
                                repeat_delta,
715
2.58M
                                alphabet_size,
716
2.58M
                                &mut symbol,
717
2.58M
                                &mut repeat,
718
2.58M
                                &mut space,
719
2.58M
                                &mut prev_code_len,
720
2.58M
                                &mut repeat_code_len,
721
2.58M
                                &mut s.symbols_lists_array,
722
2.58M
                                s.symbol_lists_index as usize,
723
2.58M
                                &mut s.code_length_histo[..],
724
2.58M
                                &mut s.next_symbol[..]);
725
    }
726
  }
727
164k
  s.space = space;
728
164k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
729
526k
}
Unexecuted instantiation: brotli_decompressor::decode::ReadSymbolCodeLengths::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ReadSymbolCodeLengths::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
659
526k
fn ReadSymbolCodeLengths<AllocU8: alloc::Allocator<u8>,
660
526k
                         AllocU32: alloc::Allocator<u32>,
661
526k
                         AllocHC: alloc::Allocator<HuffmanCode>>
662
526k
  (alphabet_size: u32,
663
526k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
664
526k
   input: &[u8])
665
526k
   -> BrotliDecoderErrorCode {
666
667
526k
  let mut symbol = s.symbol;
668
526k
  let mut repeat = s.repeat;
669
526k
  let mut space = s.space;
670
526k
  let mut prev_code_len: u32 = s.prev_code_len;
671
526k
  let mut repeat_code_len: u32 = s.repeat_code_len;
672
526k
  if (!bit_reader::BrotliWarmupBitReader(&mut s.br, input)) {
673
2.12k
    return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
674
524k
  }
675
8.23M
  while (symbol < alphabet_size && space > 0) {
676
8.06M
    let mut p_index = 0;
677
    let code_len: u32;
678
8.06M
    if (!bit_reader::BrotliCheckInputAmount(&s.br, bit_reader::BROTLI_SHORT_FILL_BIT_WINDOW_READ)) {
679
359k
      s.symbol = symbol;
680
359k
      s.repeat = repeat;
681
359k
      s.prev_code_len = prev_code_len;
682
359k
      s.repeat_code_len = repeat_code_len;
683
359k
      s.space = space;
684
359k
      return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
685
7.70M
    }
686
7.70M
    bit_reader::BrotliFillBitWindow16(&mut s.br, input);
687
7.70M
    p_index +=
688
7.70M
      bit_reader::BrotliGetBitsUnmasked(&s.br) &
689
7.70M
      bit_reader::BitMask(huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as u32) as u64;
690
7.70M
    let p = fast!((s.table)[p_index as usize]);
691
7.70M
    bit_reader::BrotliDropBits(&mut s.br, p.bits as u32); /* Use 1..5 bits */
692
7.70M
    code_len = p.value as u32; /* code_len == 0..17 */
693
7.70M
    if (code_len < kCodeLengthRepeatCode) {
694
5.12M
      ProcessSingleCodeLength(code_len,
695
5.12M
                              &mut symbol,
696
5.12M
                              &mut repeat,
697
5.12M
                              &mut space,
698
5.12M
                              &mut prev_code_len,
699
5.12M
                              &mut s.symbols_lists_array,
700
5.12M
                              s.symbol_lists_index as usize,
701
5.12M
                              &mut s.code_length_histo[..],
702
5.12M
                              &mut s.next_symbol[..]);
703
5.12M
    } else {
704
      // code_len == 16..17, extra_bits == 2..3
705
2.58M
      let extra_bits: u32 = if code_len == kCodeLengthRepeatCode {
706
660k
        2
707
      } else {
708
1.92M
        3
709
      };
710
2.58M
      let repeat_delta: u32 = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32 &
711
2.58M
                              bit_reader::BitMask(extra_bits);
712
2.58M
      bit_reader::BrotliDropBits(&mut s.br, extra_bits);
713
2.58M
      ProcessRepeatedCodeLength(code_len,
714
2.58M
                                repeat_delta,
715
2.58M
                                alphabet_size,
716
2.58M
                                &mut symbol,
717
2.58M
                                &mut repeat,
718
2.58M
                                &mut space,
719
2.58M
                                &mut prev_code_len,
720
2.58M
                                &mut repeat_code_len,
721
2.58M
                                &mut s.symbols_lists_array,
722
2.58M
                                s.symbol_lists_index as usize,
723
2.58M
                                &mut s.code_length_histo[..],
724
2.58M
                                &mut s.next_symbol[..]);
725
    }
726
  }
727
164k
  s.space = space;
728
164k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
729
526k
}
730
731
361k
fn SafeReadSymbolCodeLengths<AllocU8: alloc::Allocator<u8>,
732
361k
                             AllocU32: alloc::Allocator<u32>,
733
361k
                             AllocHC: alloc::Allocator<HuffmanCode>>
734
361k
  (alphabet_size: u32,
735
361k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
736
361k
   input: &[u8])
737
361k
   -> BrotliDecoderErrorCode {
738
1.62M
  while (s.symbol < alphabet_size && s.space > 0) {
739
1.60M
    let mut p_index = 0;
740
    let code_len: u32;
741
1.60M
    let mut bits: u32 = 0;
742
1.60M
    let available_bits: u32 = bit_reader::BrotliGetAvailableBits(&s.br);
743
1.60M
    if (available_bits != 0) {
744
1.51M
      bits = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32;
745
1.51M
    }
746
1.60M
    p_index += bits &
747
1.60M
               bit_reader::BitMask(huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as u32);
748
1.60M
    let p = fast!((s.table)[p_index as usize]);
749
1.60M
    if (p.bits as u32 > available_bits) {
750
      // pullMoreInput;
751
452k
      if (!bit_reader::BrotliPullByte(&mut s.br, input)) {
752
256k
        return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
753
195k
      }
754
195k
      continue;
755
1.15M
    }
756
1.15M
    code_len = p.value as u32; /* code_len == 0..17 */
757
1.15M
    if (code_len < kCodeLengthRepeatCode) {
758
662k
      bit_reader::BrotliDropBits(&mut s.br, p.bits as u32);
759
662k
      ProcessSingleCodeLength(code_len,
760
662k
                              &mut s.symbol,
761
662k
                              &mut s.repeat,
762
662k
                              &mut s.space,
763
662k
                              &mut s.prev_code_len,
764
662k
                              &mut s.symbols_lists_array,
765
662k
                              s.symbol_lists_index as usize,
766
662k
                              &mut s.code_length_histo[..],
767
662k
                              &mut s.next_symbol[..]);
768
662k
    } else {
769
      // code_len == 16..17, extra_bits == 2..3
770
494k
      let extra_bits: u32 = code_len - 14;
771
494k
      let repeat_delta: u32 = (bits >> p.bits) & bit_reader::BitMask(extra_bits);
772
494k
      if (available_bits < p.bits as u32 + extra_bits) {
773
        // pullMoreInput;
774
175k
        if (!bit_reader::BrotliPullByte(&mut s.br, input)) {
775
85.1k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
776
90.5k
        }
777
90.5k
        continue;
778
319k
      }
779
319k
      bit_reader::BrotliDropBits(&mut s.br, p.bits as u32 + extra_bits);
780
319k
      ProcessRepeatedCodeLength(code_len,
781
319k
                                repeat_delta,
782
319k
                                alphabet_size,
783
319k
                                &mut s.symbol,
784
319k
                                &mut s.repeat,
785
319k
                                &mut s.space,
786
319k
                                &mut s.prev_code_len,
787
319k
                                &mut s.repeat_code_len,
788
319k
                                &mut s.symbols_lists_array,
789
319k
                                s.symbol_lists_index as usize,
790
319k
                                &mut s.code_length_histo[..],
791
319k
                                &mut s.next_symbol[..]);
792
    }
793
  }
794
19.5k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
795
361k
}
Unexecuted instantiation: brotli_decompressor::decode::SafeReadSymbolCodeLengths::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::SafeReadSymbolCodeLengths::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
731
361k
fn SafeReadSymbolCodeLengths<AllocU8: alloc::Allocator<u8>,
732
361k
                             AllocU32: alloc::Allocator<u32>,
733
361k
                             AllocHC: alloc::Allocator<HuffmanCode>>
734
361k
  (alphabet_size: u32,
735
361k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
736
361k
   input: &[u8])
737
361k
   -> BrotliDecoderErrorCode {
738
1.62M
  while (s.symbol < alphabet_size && s.space > 0) {
739
1.60M
    let mut p_index = 0;
740
    let code_len: u32;
741
1.60M
    let mut bits: u32 = 0;
742
1.60M
    let available_bits: u32 = bit_reader::BrotliGetAvailableBits(&s.br);
743
1.60M
    if (available_bits != 0) {
744
1.51M
      bits = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32;
745
1.51M
    }
746
1.60M
    p_index += bits &
747
1.60M
               bit_reader::BitMask(huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as u32);
748
1.60M
    let p = fast!((s.table)[p_index as usize]);
749
1.60M
    if (p.bits as u32 > available_bits) {
750
      // pullMoreInput;
751
452k
      if (!bit_reader::BrotliPullByte(&mut s.br, input)) {
752
256k
        return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
753
195k
      }
754
195k
      continue;
755
1.15M
    }
756
1.15M
    code_len = p.value as u32; /* code_len == 0..17 */
757
1.15M
    if (code_len < kCodeLengthRepeatCode) {
758
662k
      bit_reader::BrotliDropBits(&mut s.br, p.bits as u32);
759
662k
      ProcessSingleCodeLength(code_len,
760
662k
                              &mut s.symbol,
761
662k
                              &mut s.repeat,
762
662k
                              &mut s.space,
763
662k
                              &mut s.prev_code_len,
764
662k
                              &mut s.symbols_lists_array,
765
662k
                              s.symbol_lists_index as usize,
766
662k
                              &mut s.code_length_histo[..],
767
662k
                              &mut s.next_symbol[..]);
768
662k
    } else {
769
      // code_len == 16..17, extra_bits == 2..3
770
494k
      let extra_bits: u32 = code_len - 14;
771
494k
      let repeat_delta: u32 = (bits >> p.bits) & bit_reader::BitMask(extra_bits);
772
494k
      if (available_bits < p.bits as u32 + extra_bits) {
773
        // pullMoreInput;
774
175k
        if (!bit_reader::BrotliPullByte(&mut s.br, input)) {
775
85.1k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
776
90.5k
        }
777
90.5k
        continue;
778
319k
      }
779
319k
      bit_reader::BrotliDropBits(&mut s.br, p.bits as u32 + extra_bits);
780
319k
      ProcessRepeatedCodeLength(code_len,
781
319k
                                repeat_delta,
782
319k
                                alphabet_size,
783
319k
                                &mut s.symbol,
784
319k
                                &mut s.repeat,
785
319k
                                &mut s.space,
786
319k
                                &mut s.prev_code_len,
787
319k
                                &mut s.repeat_code_len,
788
319k
                                &mut s.symbols_lists_array,
789
319k
                                s.symbol_lists_index as usize,
790
319k
                                &mut s.code_length_histo[..],
791
319k
                                &mut s.next_symbol[..]);
792
    }
793
  }
794
19.5k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
795
361k
}
796
797
// Reads and decodes 15..18 codes using static prefix code.
798
// Each code is 2..4 bits long. In total 30..72 bits are used.
799
224k
fn ReadCodeLengthCodeLengths<AllocU8: alloc::Allocator<u8>,
800
224k
                             AllocU32: alloc::Allocator<u32>,
801
224k
                             AllocHC: alloc::Allocator<HuffmanCode>>
802
224k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
803
224k
   input: &[u8])
804
224k
   -> BrotliDecoderErrorCode {
805
806
224k
  let mut num_codes: u32 = s.repeat;
807
224k
  let mut space: u32 = s.space;
808
224k
  let mut i = s.sub_loop_counter;
809
1.62M
  for code_length_code_order in
810
224k
      fast!((kCodeLengthCodeOrder)[s.sub_loop_counter as usize; CODE_LENGTH_CODES]).iter() {
811
1.62M
    let code_len_idx = *code_length_code_order;
812
1.62M
    let mut ix: u32 = 0;
813
814
1.62M
    if !bit_reader::BrotliSafeGetBits(&mut s.br, 4, &mut ix, input) {
815
64.1k
      mark_unlikely();
816
64.1k
      let available_bits: u32 = bit_reader::BrotliGetAvailableBits(&s.br);
817
64.1k
      if (available_bits != 0) {
818
48.1k
        ix = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32 & 0xF;
819
48.1k
      } else {
820
15.9k
        ix = 0;
821
15.9k
      }
822
64.1k
      if (fast!((kCodeLengthPrefixLength)[ix as usize]) as u32 > available_bits) {
823
39.4k
        s.sub_loop_counter = i;
824
39.4k
        s.repeat = num_codes;
825
39.4k
        s.space = space;
826
39.4k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX;
827
39.4k
        return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
828
24.6k
      }
829
1.55M
    }
830
    BROTLI_LOG_UINT!(ix);
831
1.58M
    let v: u32 = fast!((kCodeLengthPrefixValue)[ix as usize]) as u32;
832
1.58M
    bit_reader::BrotliDropBits(&mut s.br,
833
1.58M
                               fast!((kCodeLengthPrefixLength)[ix as usize]) as u32);
834
1.58M
    fast_mut!((s.code_length_code_lengths)[code_len_idx as usize]) = v as u8;
835
    BROTLI_LOG_ARRAY_INDEX!(s.code_length_code_lengths, code_len_idx);
836
1.58M
    if v != 0 {
837
1.26M
      space = space.wrapping_sub(32 >> v);
838
1.26M
      num_codes += 1;
839
1.26M
      fast_mut!((s.code_length_histo)[v as usize]) += 1;
840
1.26M
      if space.wrapping_sub(1) >= 32 {
841
        // space is 0 or wrapped around
842
182k
        break;
843
1.08M
      }
844
317k
    }
845
1.39M
    i += 1;
846
  }
847
184k
  if (!(num_codes == 1 || space == 0)) {
848
138
    return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CL_SPACE;
849
184k
  }
850
184k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
851
224k
}
Unexecuted instantiation: brotli_decompressor::decode::ReadCodeLengthCodeLengths::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ReadCodeLengthCodeLengths::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
799
224k
fn ReadCodeLengthCodeLengths<AllocU8: alloc::Allocator<u8>,
800
224k
                             AllocU32: alloc::Allocator<u32>,
801
224k
                             AllocHC: alloc::Allocator<HuffmanCode>>
802
224k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
803
224k
   input: &[u8])
804
224k
   -> BrotliDecoderErrorCode {
805
806
224k
  let mut num_codes: u32 = s.repeat;
807
224k
  let mut space: u32 = s.space;
808
224k
  let mut i = s.sub_loop_counter;
809
1.62M
  for code_length_code_order in
810
224k
      fast!((kCodeLengthCodeOrder)[s.sub_loop_counter as usize; CODE_LENGTH_CODES]).iter() {
811
1.62M
    let code_len_idx = *code_length_code_order;
812
1.62M
    let mut ix: u32 = 0;
813
814
1.62M
    if !bit_reader::BrotliSafeGetBits(&mut s.br, 4, &mut ix, input) {
815
64.1k
      mark_unlikely();
816
64.1k
      let available_bits: u32 = bit_reader::BrotliGetAvailableBits(&s.br);
817
64.1k
      if (available_bits != 0) {
818
48.1k
        ix = bit_reader::BrotliGetBitsUnmasked(&s.br) as u32 & 0xF;
819
48.1k
      } else {
820
15.9k
        ix = 0;
821
15.9k
      }
822
64.1k
      if (fast!((kCodeLengthPrefixLength)[ix as usize]) as u32 > available_bits) {
823
39.4k
        s.sub_loop_counter = i;
824
39.4k
        s.repeat = num_codes;
825
39.4k
        s.space = space;
826
39.4k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX;
827
39.4k
        return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
828
24.6k
      }
829
1.55M
    }
830
    BROTLI_LOG_UINT!(ix);
831
1.58M
    let v: u32 = fast!((kCodeLengthPrefixValue)[ix as usize]) as u32;
832
1.58M
    bit_reader::BrotliDropBits(&mut s.br,
833
1.58M
                               fast!((kCodeLengthPrefixLength)[ix as usize]) as u32);
834
1.58M
    fast_mut!((s.code_length_code_lengths)[code_len_idx as usize]) = v as u8;
835
    BROTLI_LOG_ARRAY_INDEX!(s.code_length_code_lengths, code_len_idx);
836
1.58M
    if v != 0 {
837
1.26M
      space = space.wrapping_sub(32 >> v);
838
1.26M
      num_codes += 1;
839
1.26M
      fast_mut!((s.code_length_histo)[v as usize]) += 1;
840
1.26M
      if space.wrapping_sub(1) >= 32 {
841
        // space is 0 or wrapped around
842
182k
        break;
843
1.08M
      }
844
317k
    }
845
1.39M
    i += 1;
846
  }
847
184k
  if (!(num_codes == 1 || space == 0)) {
848
138
    return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CL_SPACE;
849
184k
  }
850
184k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
851
224k
}
852
853
854
// Decodes the Huffman tables.
855
// There are 2 scenarios:
856
// A) Huffman code contains only few symbols (1..4). Those symbols are read
857
// directly; their code lengths are defined by the number of symbols.
858
// For this scenario 4 - 49 bits will be read.
859
//
860
// B) 2-phase decoding:
861
// B.1) Small Huffman table is decoded; it is specified with code lengths
862
// encoded with predefined entropy code. 32 - 74 bits are used.
863
// B.2) Decoded table is used to decode code lengths of symbols in resulting
864
// Huffman table. In worst case 3520 bits are read.
865
//
866
728k
fn ReadHuffmanCode<AllocU8: alloc::Allocator<u8>,
867
728k
                   AllocU32: alloc::Allocator<u32>,
868
728k
                   AllocHC: alloc::Allocator<HuffmanCode>>
869
728k
  (mut alphabet_size: u32,
870
728k
   max_symbol: u32,
871
728k
   table: &mut [HuffmanCode],
872
728k
   offset: usize,
873
728k
   opt_table_size: Option<&mut u32>,
874
728k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
875
728k
   input: &[u8])
876
728k
   -> BrotliDecoderErrorCode {
877
  // Unnecessary masking, but might be good for safety.
878
728k
  alphabet_size &= 0x7ff;
879
  // State machine
880
  loop {
881
1.53M
    match s.substate_huffman {
882
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE => {
883
336k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut s.sub_loop_counter, input) {
884
4.65k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
885
332k
        }
886
887
        BROTLI_LOG_UINT!(s.sub_loop_counter);
888
        // The value is used as follows:
889
        // 1 for simple code;
890
        // 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths
891
332k
        if (s.sub_loop_counter != 1) {
892
185k
          s.space = 32;
893
185k
          s.repeat = 0; /* num_codes */
894
185k
          let max_code_len_len = huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as usize + 1;
895
1.11M
          for code_length_histo in fast_mut!((s.code_length_histo)[0;max_code_len_len]).iter_mut() {
896
1.11M
            *code_length_histo = 0; // memset
897
1.11M
          }
898
3.33M
          for code_length_code_length in s.code_length_code_lengths[..].iter_mut() {
899
3.33M
            *code_length_code_length = 0;
900
3.33M
          }
901
185k
          s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX;
902
          // goto Complex;
903
185k
          continue;
904
146k
        }
905
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
906
        // No break, transit to the next state.
907
      }
908
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE => {
909
        // Read symbols, codes & code lengths directly.
910
147k
        if (!bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut s.symbol, input)) {
911
          // num_symbols
912
1.07k
          s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
913
1.07k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
914
146k
        }
915
146k
        s.sub_loop_counter = 0;
916
        // No break, transit to the next state.
917
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ;
918
      }
919
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ => {
920
157k
        let result = ReadSimpleHuffmanSymbols(alphabet_size, max_symbol, s, input);
921
157k
        match result {
922
146k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
923
10.4k
          _ => return result,
924
        }
925
        // No break, transit to the next state.
926
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
927
      }
928
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD => {
929
        let table_size: u32;
930
146k
        if (s.symbol == 3) {
931
28.6k
          let mut bits: u32 = 0;
932
28.6k
          if (!bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input)) {
933
160
            s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
934
160
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
935
28.5k
          }
936
28.5k
          s.symbol += bits;
937
118k
        }
938
        BROTLI_LOG_UINT!(s.symbol);
939
146k
        table_size = huffman::BrotliBuildSimpleHuffmanTable(&mut table[offset..],
940
146k
                                                            HUFFMAN_TABLE_BITS as i32,
941
146k
                                                            &s.symbols_lists_array[..],
942
146k
                                                            s.symbol);
943
146k
        if let Some(opt_table_size_ref) = opt_table_size {
944
128k
          *opt_table_size_ref = table_size
945
18.6k
        }
946
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE;
947
146k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
948
      }
949
950
      // Decode Huffman-coded code lengths.
951
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX => {
952
953
224k
        let result = ReadCodeLengthCodeLengths(s, input);
954
224k
        match result {
955
184k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
956
39.5k
          _ => return result,
957
        }
958
184k
        huffman::BrotliBuildCodeLengthsHuffmanTable(&mut s.table,
959
184k
                                                    &s.code_length_code_lengths,
960
184k
                                                    &s.code_length_histo);
961
2.95M
        for code_length_histo in s.code_length_histo[..].iter_mut() {
962
2.95M
          *code_length_histo = 0; // memset
963
2.95M
        }
964
965
184k
        let max_code_length = huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH as usize + 1;
966
2.95M
        for (i, next_symbol_mut) in fast_mut!((s.next_symbol)[0; max_code_length])
967
184k
          .iter_mut()
968
2.95M
          .enumerate() {
969
2.95M
          *next_symbol_mut = i as i32 - (max_code_length as i32);
970
2.95M
          fast_mut!((s.symbols_lists_array)[(s.symbol_lists_index as i32
971
2.95M
                                 + i as i32
972
2.95M
                                 - (max_code_length as i32)) as usize]) = 0xFFFF;
973
2.95M
        }
974
975
184k
        s.symbol = 0;
976
184k
        s.prev_code_len = kDefaultCodeLength;
977
184k
        s.repeat = 0;
978
184k
        s.repeat_code_len = 0;
979
184k
        s.space = 32768;
980
        // No break, transit to the next state.
981
184k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
982
      }
983
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS => {
984
        let table_size: u32;
985
526k
        let mut result = ReadSymbolCodeLengths(max_symbol, s, input);
986
526k
        if let BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT = result {
987
361k
          result = SafeReadSymbolCodeLengths(max_symbol, s, input)
988
164k
        }
989
526k
        match result {
990
184k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
991
341k
          _ => return result,
992
        }
993
994
184k
        if (s.space != 0) {
995
          BROTLI_LOG!("[ReadHuffmanCode] space = %d\n", s.space);
996
171
          return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE;
997
184k
        }
998
184k
        table_size = huffman::BrotliBuildHuffmanTable(fast_mut!((table)[offset;]),
999
184k
                                                      HUFFMAN_TABLE_BITS as i32,
1000
184k
                                                      &s.symbols_lists_array[..],
1001
184k
                                                      s.symbol_lists_index,
1002
184k
                                                      &mut s.code_length_histo);
1003
184k
        if let Some(opt_table_size_ref) = opt_table_size {
1004
175k
          *opt_table_size_ref = table_size
1005
9.17k
        }
1006
184k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE;
1007
184k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1008
      }
1009
    }
1010
  }
1011
728k
}
Unexecuted instantiation: brotli_decompressor::decode::ReadHuffmanCode::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ReadHuffmanCode::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
866
728k
fn ReadHuffmanCode<AllocU8: alloc::Allocator<u8>,
867
728k
                   AllocU32: alloc::Allocator<u32>,
868
728k
                   AllocHC: alloc::Allocator<HuffmanCode>>
869
728k
  (mut alphabet_size: u32,
870
728k
   max_symbol: u32,
871
728k
   table: &mut [HuffmanCode],
872
728k
   offset: usize,
873
728k
   opt_table_size: Option<&mut u32>,
874
728k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
875
728k
   input: &[u8])
876
728k
   -> BrotliDecoderErrorCode {
877
  // Unnecessary masking, but might be good for safety.
878
728k
  alphabet_size &= 0x7ff;
879
  // State machine
880
  loop {
881
1.53M
    match s.substate_huffman {
882
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE => {
883
336k
        if !bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut s.sub_loop_counter, input) {
884
4.65k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
885
332k
        }
886
887
        BROTLI_LOG_UINT!(s.sub_loop_counter);
888
        // The value is used as follows:
889
        // 1 for simple code;
890
        // 0 for no skipping, 2 skips 2 code lengths, 3 skips 3 code lengths
891
332k
        if (s.sub_loop_counter != 1) {
892
185k
          s.space = 32;
893
185k
          s.repeat = 0; /* num_codes */
894
185k
          let max_code_len_len = huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH_CODE_LENGTH as usize + 1;
895
1.11M
          for code_length_histo in fast_mut!((s.code_length_histo)[0;max_code_len_len]).iter_mut() {
896
1.11M
            *code_length_histo = 0; // memset
897
1.11M
          }
898
3.33M
          for code_length_code_length in s.code_length_code_lengths[..].iter_mut() {
899
3.33M
            *code_length_code_length = 0;
900
3.33M
          }
901
185k
          s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX;
902
          // goto Complex;
903
185k
          continue;
904
146k
        }
905
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
906
        // No break, transit to the next state.
907
      }
908
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE => {
909
        // Read symbols, codes & code lengths directly.
910
147k
        if (!bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut s.symbol, input)) {
911
          // num_symbols
912
1.07k
          s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_SIZE;
913
1.07k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
914
146k
        }
915
146k
        s.sub_loop_counter = 0;
916
        // No break, transit to the next state.
917
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ;
918
      }
919
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_READ => {
920
157k
        let result = ReadSimpleHuffmanSymbols(alphabet_size, max_symbol, s, input);
921
157k
        match result {
922
146k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
923
10.4k
          _ => return result,
924
        }
925
        // No break, transit to the next state.
926
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
927
      }
928
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD => {
929
        let table_size: u32;
930
146k
        if (s.symbol == 3) {
931
28.6k
          let mut bits: u32 = 0;
932
28.6k
          if (!bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input)) {
933
160
            s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_SIMPLE_BUILD;
934
160
            return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
935
28.5k
          }
936
28.5k
          s.symbol += bits;
937
118k
        }
938
        BROTLI_LOG_UINT!(s.symbol);
939
146k
        table_size = huffman::BrotliBuildSimpleHuffmanTable(&mut table[offset..],
940
146k
                                                            HUFFMAN_TABLE_BITS as i32,
941
146k
                                                            &s.symbols_lists_array[..],
942
146k
                                                            s.symbol);
943
146k
        if let Some(opt_table_size_ref) = opt_table_size {
944
128k
          *opt_table_size_ref = table_size
945
18.6k
        }
946
146k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE;
947
146k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
948
      }
949
950
      // Decode Huffman-coded code lengths.
951
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_COMPLEX => {
952
953
224k
        let result = ReadCodeLengthCodeLengths(s, input);
954
224k
        match result {
955
184k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
956
39.5k
          _ => return result,
957
        }
958
184k
        huffman::BrotliBuildCodeLengthsHuffmanTable(&mut s.table,
959
184k
                                                    &s.code_length_code_lengths,
960
184k
                                                    &s.code_length_histo);
961
2.95M
        for code_length_histo in s.code_length_histo[..].iter_mut() {
962
2.95M
          *code_length_histo = 0; // memset
963
2.95M
        }
964
965
184k
        let max_code_length = huffman::BROTLI_HUFFMAN_MAX_CODE_LENGTH as usize + 1;
966
2.95M
        for (i, next_symbol_mut) in fast_mut!((s.next_symbol)[0; max_code_length])
967
184k
          .iter_mut()
968
2.95M
          .enumerate() {
969
2.95M
          *next_symbol_mut = i as i32 - (max_code_length as i32);
970
2.95M
          fast_mut!((s.symbols_lists_array)[(s.symbol_lists_index as i32
971
2.95M
                                 + i as i32
972
2.95M
                                 - (max_code_length as i32)) as usize]) = 0xFFFF;
973
2.95M
        }
974
975
184k
        s.symbol = 0;
976
184k
        s.prev_code_len = kDefaultCodeLength;
977
184k
        s.repeat = 0;
978
184k
        s.repeat_code_len = 0;
979
184k
        s.space = 32768;
980
        // No break, transit to the next state.
981
184k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS;
982
      }
983
      BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_LENGTH_SYMBOLS => {
984
        let table_size: u32;
985
526k
        let mut result = ReadSymbolCodeLengths(max_symbol, s, input);
986
526k
        if let BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT = result {
987
361k
          result = SafeReadSymbolCodeLengths(max_symbol, s, input)
988
164k
        }
989
526k
        match result {
990
184k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
991
341k
          _ => return result,
992
        }
993
994
184k
        if (s.space != 0) {
995
          BROTLI_LOG!("[ReadHuffmanCode] space = %d\n", s.space);
996
171
          return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE;
997
184k
        }
998
184k
        table_size = huffman::BrotliBuildHuffmanTable(fast_mut!((table)[offset;]),
999
184k
                                                      HUFFMAN_TABLE_BITS as i32,
1000
184k
                                                      &s.symbols_lists_array[..],
1001
184k
                                                      s.symbol_lists_index,
1002
184k
                                                      &mut s.code_length_histo);
1003
184k
        if let Some(opt_table_size_ref) = opt_table_size {
1004
175k
          *opt_table_size_ref = table_size
1005
9.17k
        }
1006
184k
        s.substate_huffman = BrotliRunningHuffmanState::BROTLI_STATE_HUFFMAN_NONE;
1007
184k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1008
      }
1009
    }
1010
  }
1011
728k
}
1012
1013
// Decodes a block length by reading 3..39 bits.
1014
3.92M
fn ReadBlockLength(table: &[HuffmanCode],
1015
3.92M
                   br: &mut bit_reader::BrotliBitReader,
1016
3.92M
                   input: &[u8])
1017
3.92M
                   -> u32 {
1018
  let code: u32;
1019
  let nbits: u32;
1020
3.92M
  code = ReadSymbol(table, br, input);
1021
3.92M
  nbits = fast_ref!((prefix::kBlockLengthPrefixCode)[code as usize]).nbits as u32; /*nbits==2..24*/
1022
3.92M
  fast_ref!((prefix::kBlockLengthPrefixCode)[code as usize]).offset as u32 +
1023
3.92M
  bit_reader::BrotliReadBits(br, nbits, input)
1024
3.92M
}
1025
1026
1027
// WARNING: if state is not BROTLI_STATE_READ_BLOCK_LENGTH_NONE, then
1028
// reading can't be continued with ReadBlockLength.
1029
33.7k
fn SafeReadBlockLengthIndex(substate_read_block_length: &state::BrotliRunningReadBlockLengthState,
1030
33.7k
                            block_length_index: u32,
1031
33.7k
                            table: &[HuffmanCode],
1032
33.7k
                            mut br: &mut bit_reader::BrotliBitReader,
1033
33.7k
                            input: &[u8])
1034
33.7k
                            -> (bool, u32) {
1035
33.7k
  match *substate_read_block_length {
1036
    state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE => {
1037
33.6k
      let mut index: u32 = 0;
1038
33.6k
      if (!SafeReadSymbol(table, &mut br, &mut index, input)) {
1039
3.03k
        return (false, 0);
1040
30.5k
      }
1041
30.5k
      (true, index)
1042
    }
1043
126
    _ => (true, block_length_index),
1044
  }
1045
33.7k
}
1046
33.7k
fn SafeReadBlockLengthFromIndex<
1047
33.7k
    AllocHC : alloc::Allocator<HuffmanCode> >(s : &mut BlockTypeAndLengthState<AllocHC>,
1048
33.7k
                                              br : &mut bit_reader::BrotliBitReader,
1049
33.7k
                                              result : &mut u32,
1050
33.7k
                                              res_index : (bool, u32),
1051
33.7k
                                              input : &[u8]) -> bool{
1052
33.7k
  let (res, index) = res_index;
1053
33.7k
  if !res {
1054
3.03k
    return false;
1055
30.7k
  }
1056
30.7k
  let mut bits: u32 = 0;
1057
30.7k
  let nbits = fast_ref!((prefix::kBlockLengthPrefixCode)[index as usize]).nbits; /* nbits==2..24 */
1058
30.7k
  if (!bit_reader::BrotliSafeReadBits(br, nbits as u32, &mut bits, input)) {
1059
4.42k
    s.block_length_index = index;
1060
4.42k
    s.substate_read_block_length =
1061
4.42k
      state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
1062
4.42k
    return false;
1063
26.2k
  }
1064
26.2k
  *result = fast_ref!((prefix::kBlockLengthPrefixCode)[index as usize]).offset as u32 + bits;
1065
26.2k
  s.substate_read_block_length =
1066
26.2k
    state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
1067
26.2k
  true
1068
33.7k
}
Unexecuted instantiation: brotli_decompressor::decode::SafeReadBlockLengthFromIndex::<alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::SafeReadBlockLengthFromIndex::<alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1046
33.7k
fn SafeReadBlockLengthFromIndex<
1047
33.7k
    AllocHC : alloc::Allocator<HuffmanCode> >(s : &mut BlockTypeAndLengthState<AllocHC>,
1048
33.7k
                                              br : &mut bit_reader::BrotliBitReader,
1049
33.7k
                                              result : &mut u32,
1050
33.7k
                                              res_index : (bool, u32),
1051
33.7k
                                              input : &[u8]) -> bool{
1052
33.7k
  let (res, index) = res_index;
1053
33.7k
  if !res {
1054
3.03k
    return false;
1055
30.7k
  }
1056
30.7k
  let mut bits: u32 = 0;
1057
30.7k
  let nbits = fast_ref!((prefix::kBlockLengthPrefixCode)[index as usize]).nbits; /* nbits==2..24 */
1058
30.7k
  if (!bit_reader::BrotliSafeReadBits(br, nbits as u32, &mut bits, input)) {
1059
4.42k
    s.block_length_index = index;
1060
4.42k
    s.substate_read_block_length =
1061
4.42k
      state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_SUFFIX;
1062
4.42k
    return false;
1063
26.2k
  }
1064
26.2k
  *result = fast_ref!((prefix::kBlockLengthPrefixCode)[index as usize]).offset as u32 + bits;
1065
26.2k
  s.substate_read_block_length =
1066
26.2k
    state::BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
1067
26.2k
  true
1068
33.7k
}
1069
macro_rules! SafeReadBlockLength (
1070
   ($state : expr, $result : expr , $table : expr) => {
1071
       SafeReadBlockLengthFromIndex(&mut $state, &mut $result,
1072
                                    SafeReadBlockLengthIndex($state.substate_read_block_length,
1073
                                                             $state.block_length_index,
1074
                                                             $table,
1075
                                                             &mut $state.br))
1076
   };
1077
);
1078
1079
// Transform:
1080
// 1) initialize list L with values 0, 1,... 255
1081
// 2) For each input element X:
1082
// 2.1) let Y = L[X]
1083
// 2.2) remove X-th element from L
1084
// 2.3) prepend Y to L
1085
// 2.4) append Y to output
1086
//
1087
// In most cases max(Y) <= 7, so most of L remains intact.
1088
// To reduce the cost of initialization, we reuse L, remember the upper bound
1089
// of Y values, and reinitialize only first elements in L.
1090
//
1091
// Most of input values are 0 and 1. To reduce number of branches, we replace
1092
// inner for loop with do-while.
1093
//
1094
8.83k
fn InverseMoveToFrontTransform(v: &mut [u8],
1095
8.83k
                               v_len: u32,
1096
8.83k
                               mtf: &mut [u8;256],
1097
8.83k
                               mtf_upper_bound: &mut u32) {
1098
  // Reinitialize elements that could have been changed.
1099
8.83k
  let mut upper_bound: u32 = *mtf_upper_bound;
1100
1.50M
  for (i, item) in fast_mut!((mtf)[0;(upper_bound as usize + 1usize)]).iter_mut().enumerate() {
1101
1.50M
    *item = i as u8;
1102
1.50M
  }
1103
1104
  // Transform the input.
1105
8.83k
  upper_bound = 0;
1106
2.50M
  for v_i in fast_mut!((v)[0usize ; (v_len as usize)]).iter_mut() {
1107
2.50M
    let mut index = (*v_i) as i32;
1108
2.50M
    let value = fast!((mtf)[index as usize]);
1109
2.50M
    upper_bound |= (*v_i) as u32;
1110
2.50M
    *v_i = value;
1111
2.50M
    if index <= 0 {
1112
1.07M
      fast_mut!((mtf)[0]) = 0;
1113
1.07M
    } else {
1114
      loop {
1115
15.0M
        index -= 1;
1116
15.0M
        fast_mut!((mtf)[(index + 1) as usize]) = fast!((mtf)[index as usize]);
1117
15.0M
        if index <= 0 {
1118
1.42M
          break;
1119
13.6M
        }
1120
      }
1121
    }
1122
2.50M
    fast_mut!((mtf)[0]) = value;
1123
  }
1124
  // Remember amount of elements to be reinitialized.
1125
8.83k
  *mtf_upper_bound = upper_bound;
1126
8.83k
}
1127
// Decodes a series of Huffman table using ReadHuffmanCode function.
1128
544k
fn HuffmanTreeGroupDecode<AllocU8: alloc::Allocator<u8>,
1129
544k
                          AllocU32: alloc::Allocator<u32>,
1130
544k
                          AllocHC: alloc::Allocator<HuffmanCode>>
1131
544k
  (group_index: i32,
1132
544k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1133
544k
   input: &[u8])
1134
544k
   -> BrotliDecoderErrorCode {
1135
  let mut hcodes: AllocHC::AllocatedMemory;
1136
  let mut htrees: AllocU32::AllocatedMemory;
1137
  let alphabet_size: u16;
1138
  let group_num_htrees: u16;
1139
  let group_max_symbol;
1140
544k
  if group_index == 0 {
1141
294k
    hcodes = mem::replace(&mut s.literal_hgroup.codes,
1142
294k
                          AllocHC::AllocatedMemory::default());
1143
294k
    htrees = mem::replace(&mut s.literal_hgroup.htrees,
1144
294k
                          AllocU32::AllocatedMemory::default());
1145
294k
    group_num_htrees = s.literal_hgroup.num_htrees;
1146
294k
    alphabet_size = s.literal_hgroup.alphabet_size;
1147
294k
    group_max_symbol = s.literal_hgroup.max_symbol;
1148
294k
  } else if group_index == 1 {
1149
166k
    hcodes = mem::replace(&mut s.insert_copy_hgroup.codes,
1150
166k
                          AllocHC::AllocatedMemory::default());
1151
166k
    htrees = mem::replace(&mut s.insert_copy_hgroup.htrees,
1152
166k
                          AllocU32::AllocatedMemory::default());
1153
166k
    group_num_htrees = s.insert_copy_hgroup.num_htrees;
1154
166k
    alphabet_size = s.insert_copy_hgroup.alphabet_size;
1155
166k
    group_max_symbol = s.insert_copy_hgroup.max_symbol;
1156
166k
  } else {
1157
83.5k
    if group_index != 2 {
1158
0
      let ret = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
1159
0
      SaveErrorCode!(s, ret);
1160
0
      return ret;
1161
83.5k
    }
1162
83.5k
    hcodes = mem::replace(&mut s.distance_hgroup.codes,
1163
83.5k
                          AllocHC::AllocatedMemory::default());
1164
83.5k
    htrees = mem::replace(&mut s.distance_hgroup.htrees,
1165
83.5k
                          AllocU32::AllocatedMemory::default());
1166
83.5k
    group_num_htrees = s.distance_hgroup.num_htrees;
1167
83.5k
    alphabet_size = s.distance_hgroup.alphabet_size;
1168
83.5k
    group_max_symbol = s.distance_hgroup.max_symbol;
1169
  }
1170
544k
  match s.substate_tree_group {
1171
151k
    BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_NONE => {
1172
151k
      s.htree_next_offset = 0;
1173
151k
      s.htree_index = 0;
1174
151k
      s.substate_tree_group = BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_LOOP;
1175
151k
    }
1176
393k
    BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_LOOP => {}
1177
  }
1178
544k
  let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1179
697k
  for htree_iter in
1180
544k
      fast_mut!((htrees.slice_mut())[s.htree_index as usize ; (group_num_htrees as usize)])
1181
544k
    .iter_mut() {
1182
697k
    let mut table_size: u32 = 0;
1183
697k
    result = ReadHuffmanCode(u32::from(alphabet_size), u32::from(group_max_symbol),
1184
697k
                             hcodes.slice_mut(),
1185
697k
                             s.htree_next_offset as usize,
1186
697k
                             Some(&mut table_size),
1187
697k
                             &mut s,
1188
697k
                             input);
1189
697k
    match result {
1190
303k
      BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1191
394k
      _ => break, // break and return the result code
1192
    }
1193
303k
    *htree_iter = s.htree_next_offset;
1194
303k
    s.htree_next_offset += table_size;
1195
303k
    s.htree_index += 1;
1196
  }
1197
544k
  if group_index == 0 {
1198
294k
    let _ = mem::replace(&mut s.literal_hgroup.codes,
1199
294k
                 mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
1200
294k
    let _ = mem::replace(&mut s.literal_hgroup.htrees,
1201
294k
                 mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
1202
294k
  } else if group_index == 1 {
1203
166k
    let _ = mem::replace(&mut s.insert_copy_hgroup.codes,
1204
166k
                 mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
1205
166k
    let _ = mem::replace(&mut s.insert_copy_hgroup.htrees,
1206
166k
                 mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
1207
166k
  } else {
1208
83.5k
    let _ = mem::replace(&mut s.distance_hgroup.codes,
1209
83.5k
                 mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
1210
83.5k
    let _ = mem::replace(&mut s.distance_hgroup.htrees,
1211
83.5k
                 mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
1212
83.5k
  }
1213
544k
  if let BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS = result {
1214
150k
    s.substate_tree_group = BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_NONE
1215
394k
  }
1216
544k
  result
1217
544k
}
Unexecuted instantiation: brotli_decompressor::decode::HuffmanTreeGroupDecode::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::HuffmanTreeGroupDecode::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1128
544k
fn HuffmanTreeGroupDecode<AllocU8: alloc::Allocator<u8>,
1129
544k
                          AllocU32: alloc::Allocator<u32>,
1130
544k
                          AllocHC: alloc::Allocator<HuffmanCode>>
1131
544k
  (group_index: i32,
1132
544k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1133
544k
   input: &[u8])
1134
544k
   -> BrotliDecoderErrorCode {
1135
  let mut hcodes: AllocHC::AllocatedMemory;
1136
  let mut htrees: AllocU32::AllocatedMemory;
1137
  let alphabet_size: u16;
1138
  let group_num_htrees: u16;
1139
  let group_max_symbol;
1140
544k
  if group_index == 0 {
1141
294k
    hcodes = mem::replace(&mut s.literal_hgroup.codes,
1142
294k
                          AllocHC::AllocatedMemory::default());
1143
294k
    htrees = mem::replace(&mut s.literal_hgroup.htrees,
1144
294k
                          AllocU32::AllocatedMemory::default());
1145
294k
    group_num_htrees = s.literal_hgroup.num_htrees;
1146
294k
    alphabet_size = s.literal_hgroup.alphabet_size;
1147
294k
    group_max_symbol = s.literal_hgroup.max_symbol;
1148
294k
  } else if group_index == 1 {
1149
166k
    hcodes = mem::replace(&mut s.insert_copy_hgroup.codes,
1150
166k
                          AllocHC::AllocatedMemory::default());
1151
166k
    htrees = mem::replace(&mut s.insert_copy_hgroup.htrees,
1152
166k
                          AllocU32::AllocatedMemory::default());
1153
166k
    group_num_htrees = s.insert_copy_hgroup.num_htrees;
1154
166k
    alphabet_size = s.insert_copy_hgroup.alphabet_size;
1155
166k
    group_max_symbol = s.insert_copy_hgroup.max_symbol;
1156
166k
  } else {
1157
83.5k
    if group_index != 2 {
1158
0
      let ret = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
1159
0
      SaveErrorCode!(s, ret);
1160
0
      return ret;
1161
83.5k
    }
1162
83.5k
    hcodes = mem::replace(&mut s.distance_hgroup.codes,
1163
83.5k
                          AllocHC::AllocatedMemory::default());
1164
83.5k
    htrees = mem::replace(&mut s.distance_hgroup.htrees,
1165
83.5k
                          AllocU32::AllocatedMemory::default());
1166
83.5k
    group_num_htrees = s.distance_hgroup.num_htrees;
1167
83.5k
    alphabet_size = s.distance_hgroup.alphabet_size;
1168
83.5k
    group_max_symbol = s.distance_hgroup.max_symbol;
1169
  }
1170
544k
  match s.substate_tree_group {
1171
151k
    BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_NONE => {
1172
151k
      s.htree_next_offset = 0;
1173
151k
      s.htree_index = 0;
1174
151k
      s.substate_tree_group = BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_LOOP;
1175
151k
    }
1176
393k
    BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_LOOP => {}
1177
  }
1178
544k
  let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1179
697k
  for htree_iter in
1180
544k
      fast_mut!((htrees.slice_mut())[s.htree_index as usize ; (group_num_htrees as usize)])
1181
544k
    .iter_mut() {
1182
697k
    let mut table_size: u32 = 0;
1183
697k
    result = ReadHuffmanCode(u32::from(alphabet_size), u32::from(group_max_symbol),
1184
697k
                             hcodes.slice_mut(),
1185
697k
                             s.htree_next_offset as usize,
1186
697k
                             Some(&mut table_size),
1187
697k
                             &mut s,
1188
697k
                             input);
1189
697k
    match result {
1190
303k
      BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1191
394k
      _ => break, // break and return the result code
1192
    }
1193
303k
    *htree_iter = s.htree_next_offset;
1194
303k
    s.htree_next_offset += table_size;
1195
303k
    s.htree_index += 1;
1196
  }
1197
544k
  if group_index == 0 {
1198
294k
    let _ = mem::replace(&mut s.literal_hgroup.codes,
1199
294k
                 mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
1200
294k
    let _ = mem::replace(&mut s.literal_hgroup.htrees,
1201
294k
                 mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
1202
294k
  } else if group_index == 1 {
1203
166k
    let _ = mem::replace(&mut s.insert_copy_hgroup.codes,
1204
166k
                 mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
1205
166k
    let _ = mem::replace(&mut s.insert_copy_hgroup.htrees,
1206
166k
                 mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
1207
166k
  } else {
1208
83.5k
    let _ = mem::replace(&mut s.distance_hgroup.codes,
1209
83.5k
                 mem::replace(&mut hcodes, AllocHC::AllocatedMemory::default()));
1210
83.5k
    let _ = mem::replace(&mut s.distance_hgroup.htrees,
1211
83.5k
                 mem::replace(&mut htrees, AllocU32::AllocatedMemory::default()));
1212
83.5k
  }
1213
544k
  if let BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS = result {
1214
150k
    s.substate_tree_group = BrotliRunningTreeGroupState::BROTLI_STATE_TREE_GROUP_NONE
1215
394k
  }
1216
544k
  result
1217
544k
}
1218
#[allow(dead_code)]
1219
0
pub fn lg_window_size(first_byte: u8, second_byte: u8) -> Result<(u8, u8), ()> {
1220
0
  if first_byte & 1 == 0 {
1221
0
    return Ok((16, 1));
1222
0
  }
1223
0
  match first_byte & 15 {
1224
0
    0x3 => return Ok((18, 4)),
1225
0
    0x5 => return Ok((19, 4)),
1226
0
    0x7 => return Ok((20, 4)),
1227
0
    0x9 => return Ok((21, 4)),
1228
0
    0xb => return Ok((22, 4)),
1229
0
    0xd => return Ok((23, 4)),
1230
0
    0xf => return Ok((24, 4)),
1231
0
    _ => match first_byte & 127 {
1232
0
      0x71 => return Ok((15, 7)),
1233
0
      0x61 => return Ok((14, 7)),
1234
0
      0x51 => return Ok((13, 7)),
1235
0
      0x41 => return Ok((12, 7)),
1236
0
      0x31 => return Ok((11, 7)),
1237
0
      0x21 => return Ok((10, 7)),
1238
0
      0x1 => return Ok((17, 7)),
1239
0
      _ => {},
1240
    }
1241
  }
1242
0
  if (first_byte & 0x80) != 0 {
1243
0
    return Err(());
1244
0
  }
1245
0
  let ret  = second_byte & 0x3f;
1246
0
  if ret < 10 || ret > 30 {
1247
0
    return Err(());
1248
0
  }
1249
0
  Ok((ret, 14))
1250
1251
0
}
1252
1253
1254
91.5k
fn bzero(data: &mut [u8]) {
1255
4.96M
  for iter in data.iter_mut() {
1256
4.96M
    *iter = 0;
1257
4.96M
  }
1258
91.5k
}
1259
1260
1261
// Decodes a context map.
1262
// Decoding is done in 4 phases:
1263
// 1) Read auxiliary information (6..16 bits) and allocate memory.
1264
// In case of trivial context map, decoding is finished at this phase.
1265
// 2) Decode Huffman table using ReadHuffmanCode function.
1266
// This table will be used for reading context map items.
1267
// 3) Read context map items; "0" values could be run-length encoded.
1268
// 4) Optionally, apply InverseMoveToFront transform to the resulting map.
1269
//
1270
119k
fn DecodeContextMapInner<AllocU8: alloc::Allocator<u8>,
1271
119k
                         AllocU32: alloc::Allocator<u32>,
1272
119k
                         AllocHC: alloc::Allocator<HuffmanCode>>
1273
119k
  (context_map_size: u32,
1274
119k
   num_htrees: &mut u32,
1275
119k
   context_map_arg: &mut AllocU8::AllocatedMemory,
1276
119k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1277
119k
   input: &[u8])
1278
119k
   -> BrotliDecoderErrorCode {
1279
1280
  let mut result;
1281
  loop {
1282
160k
    match s.substate_context_map {
1283
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_NONE => {
1284
103k
        result = DecodeVarLenUint8(&mut s.substate_decode_uint8, &mut s.br, num_htrees, input);
1285
103k
        match result {
1286
102k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1287
1.21k
          _ => return result,
1288
        }
1289
102k
        (*num_htrees) += 1;
1290
102k
        s.context_index = 0;
1291
        BROTLI_LOG_UINT!(context_map_size);
1292
        BROTLI_LOG_UINT!(*num_htrees);
1293
102k
        *context_map_arg = s.alloc_u8.alloc_cell(context_map_size as usize);
1294
102k
        if (context_map_arg.slice().len() < context_map_size as usize) {
1295
0
          return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP;
1296
102k
        }
1297
102k
        if (*num_htrees <= 1) {
1298
          // This happens automatically but we do it to retain C++ similarity:
1299
91.5k
          bzero(context_map_arg.slice_mut()); // necessary if we compiler with unsafe feature flag
1300
91.5k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1301
10.5k
        }
1302
10.5k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
1303
        // No break, continue to next state.
1304
      }
1305
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_READ_PREFIX => {
1306
10.7k
        let mut bits: u32 = 0;
1307
        // In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
1308
        // to peek 4 bits ahead.
1309
10.7k
        if (!bit_reader::BrotliSafeGetBits(&mut s.br, 5, &mut bits, input)) {
1310
233
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1311
10.4k
        }
1312
10.4k
        if ((bits & 1) != 0) {
1313
7.09k
          // Use RLE for zeroes.
1314
7.09k
          s.max_run_length_prefix = (bits >> 1) + 1;
1315
7.09k
          bit_reader::BrotliDropBits(&mut s.br, 5);
1316
7.09k
        } else {
1317
3.40k
          s.max_run_length_prefix = 0;
1318
3.40k
          bit_reader::BrotliDropBits(&mut s.br, 1);
1319
3.40k
        }
1320
        BROTLI_LOG_UINT!(s.max_run_length_prefix);
1321
10.4k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
1322
        // No break, continue to next state.
1323
      }
1324
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_HUFFMAN => {
1325
1326
12.4k
        let mut local_context_map_table = mem::replace(&mut s.context_map_table,
1327
12.4k
                                                       AllocHC::AllocatedMemory::default());
1328
12.4k
        let alphabet_size = *num_htrees + s.max_run_length_prefix;
1329
12.4k
        result = ReadHuffmanCode(alphabet_size, alphabet_size,
1330
12.4k
                                 &mut local_context_map_table.slice_mut(),
1331
12.4k
                                 0,
1332
12.4k
                                 None,
1333
12.4k
                                 &mut s,
1334
12.4k
                                 input);
1335
12.4k
        let _ = mem::replace(&mut s.context_map_table,
1336
12.4k
                     mem::replace(&mut local_context_map_table,
1337
12.4k
                                  AllocHC::AllocatedMemory::default()));
1338
12.4k
        match result {
1339
10.4k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1340
2.03k
          _ => return result,
1341
        }
1342
10.4k
        s.code = 0xFFFF;
1343
10.4k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_DECODE;
1344
        // No break, continue to next state.
1345
      }
1346
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_DECODE => {
1347
24.0k
        let mut context_index: u32 = s.context_index;
1348
24.0k
        let max_run_length_prefix: u32 = s.max_run_length_prefix;
1349
24.0k
        let context_map = &mut context_map_arg.slice_mut();
1350
24.0k
        let mut code: u32 = s.code;
1351
24.0k
        let mut rleCodeGoto = (code != 0xFFFF);
1352
1.69M
        while (rleCodeGoto || context_index < context_map_size) {
1353
1.68M
          if !rleCodeGoto {
1354
1.68M
            if (!SafeReadSymbol(s.context_map_table.slice(), &mut s.br, &mut code, input)) {
1355
12.6k
              s.code = 0xFFFF;
1356
12.6k
              s.context_index = context_index;
1357
12.6k
              return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1358
1.67M
            }
1359
            BROTLI_LOG_UINT!(code);
1360
1361
1.67M
            if code == 0 {
1362
112k
              fast_mut!((context_map)[context_index as usize]) = 0;
1363
              BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
1364
112k
              context_index += 1;
1365
112k
              continue;
1366
1.55M
            }
1367
1.55M
            if code > max_run_length_prefix {
1368
1.46M
              fast_mut!((context_map)[context_index as usize]) =
1369
1.46M
                (code - max_run_length_prefix) as u8;
1370
              BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
1371
1.46M
              context_index += 1;
1372
1.46M
              continue;
1373
96.4k
            }
1374
957
          }
1375
97.4k
          rleCodeGoto = false; // <- this was a goto beforehand... now we have reached label
1376
          // we are treated like everyday citizens from this point forth
1377
          {
1378
97.4k
            let mut reps: u32 = 0;
1379
97.4k
            if (!bit_reader::BrotliSafeReadBits(&mut s.br, code, &mut reps, input)) {
1380
985
              s.code = code;
1381
985
              s.context_index = context_index;
1382
985
              return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1383
96.4k
            }
1384
96.4k
            reps += 1u32 << code;
1385
            BROTLI_LOG_UINT!(reps);
1386
96.4k
            if (context_index + reps > context_map_size) {
1387
22
              return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT;
1388
96.4k
            }
1389
            loop {
1390
1.01M
              fast_mut!((context_map)[context_index as usize]) = 0;
1391
              BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
1392
1.01M
              context_index += 1;
1393
1.01M
              reps -= 1;
1394
1.01M
              if reps == 0 {
1395
96.4k
                break;
1396
918k
              }
1397
            }
1398
          }
1399
        }
1400
        // No break, continue to next state.
1401
10.3k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1402
      }
1403
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM => {
1404
10.4k
        let mut bits: u32 = 0;
1405
10.4k
        if (!bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input)) {
1406
63
          s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1407
63
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1408
10.3k
        }
1409
10.3k
        if (bits != 0) {
1410
8.83k
          if let Ok(ref mut mtf) = s.mtf_or_error_string {
1411
8.83k
            InverseMoveToFrontTransform(context_map_arg.slice_mut(),
1412
8.83k
                                        context_map_size,
1413
8.83k
                                        mtf,
1414
8.83k
                                        &mut s.mtf_upper_bound);
1415
8.83k
          } else {
1416
            // the error state is stored here--we can't make it this deep with an active error
1417
0
            return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
1418
          }
1419
1.50k
        }
1420
10.3k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_NONE;
1421
10.3k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1422
      }
1423
    }
1424
  }
1425
  // unreachable!(); (compiler will error if it's reachable due to the unit return type)
1426
119k
}
Unexecuted instantiation: brotli_decompressor::decode::DecodeContextMapInner::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DecodeContextMapInner::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1270
119k
fn DecodeContextMapInner<AllocU8: alloc::Allocator<u8>,
1271
119k
                         AllocU32: alloc::Allocator<u32>,
1272
119k
                         AllocHC: alloc::Allocator<HuffmanCode>>
1273
119k
  (context_map_size: u32,
1274
119k
   num_htrees: &mut u32,
1275
119k
   context_map_arg: &mut AllocU8::AllocatedMemory,
1276
119k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1277
119k
   input: &[u8])
1278
119k
   -> BrotliDecoderErrorCode {
1279
1280
  let mut result;
1281
  loop {
1282
160k
    match s.substate_context_map {
1283
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_NONE => {
1284
103k
        result = DecodeVarLenUint8(&mut s.substate_decode_uint8, &mut s.br, num_htrees, input);
1285
103k
        match result {
1286
102k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1287
1.21k
          _ => return result,
1288
        }
1289
102k
        (*num_htrees) += 1;
1290
102k
        s.context_index = 0;
1291
        BROTLI_LOG_UINT!(context_map_size);
1292
        BROTLI_LOG_UINT!(*num_htrees);
1293
102k
        *context_map_arg = s.alloc_u8.alloc_cell(context_map_size as usize);
1294
102k
        if (context_map_arg.slice().len() < context_map_size as usize) {
1295
0
          return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP;
1296
102k
        }
1297
102k
        if (*num_htrees <= 1) {
1298
          // This happens automatically but we do it to retain C++ similarity:
1299
91.5k
          bzero(context_map_arg.slice_mut()); // necessary if we compiler with unsafe feature flag
1300
91.5k
          return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1301
10.5k
        }
1302
10.5k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_READ_PREFIX;
1303
        // No break, continue to next state.
1304
      }
1305
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_READ_PREFIX => {
1306
10.7k
        let mut bits: u32 = 0;
1307
        // In next stage ReadHuffmanCode uses at least 4 bits, so it is safe
1308
        // to peek 4 bits ahead.
1309
10.7k
        if (!bit_reader::BrotliSafeGetBits(&mut s.br, 5, &mut bits, input)) {
1310
233
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1311
10.4k
        }
1312
10.4k
        if ((bits & 1) != 0) {
1313
7.09k
          // Use RLE for zeroes.
1314
7.09k
          s.max_run_length_prefix = (bits >> 1) + 1;
1315
7.09k
          bit_reader::BrotliDropBits(&mut s.br, 5);
1316
7.09k
        } else {
1317
3.40k
          s.max_run_length_prefix = 0;
1318
3.40k
          bit_reader::BrotliDropBits(&mut s.br, 1);
1319
3.40k
        }
1320
        BROTLI_LOG_UINT!(s.max_run_length_prefix);
1321
10.4k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_HUFFMAN;
1322
        // No break, continue to next state.
1323
      }
1324
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_HUFFMAN => {
1325
1326
12.4k
        let mut local_context_map_table = mem::replace(&mut s.context_map_table,
1327
12.4k
                                                       AllocHC::AllocatedMemory::default());
1328
12.4k
        let alphabet_size = *num_htrees + s.max_run_length_prefix;
1329
12.4k
        result = ReadHuffmanCode(alphabet_size, alphabet_size,
1330
12.4k
                                 &mut local_context_map_table.slice_mut(),
1331
12.4k
                                 0,
1332
12.4k
                                 None,
1333
12.4k
                                 &mut s,
1334
12.4k
                                 input);
1335
12.4k
        let _ = mem::replace(&mut s.context_map_table,
1336
12.4k
                     mem::replace(&mut local_context_map_table,
1337
12.4k
                                  AllocHC::AllocatedMemory::default()));
1338
12.4k
        match result {
1339
10.4k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1340
2.03k
          _ => return result,
1341
        }
1342
10.4k
        s.code = 0xFFFF;
1343
10.4k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_DECODE;
1344
        // No break, continue to next state.
1345
      }
1346
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_DECODE => {
1347
24.0k
        let mut context_index: u32 = s.context_index;
1348
24.0k
        let max_run_length_prefix: u32 = s.max_run_length_prefix;
1349
24.0k
        let context_map = &mut context_map_arg.slice_mut();
1350
24.0k
        let mut code: u32 = s.code;
1351
24.0k
        let mut rleCodeGoto = (code != 0xFFFF);
1352
1.69M
        while (rleCodeGoto || context_index < context_map_size) {
1353
1.68M
          if !rleCodeGoto {
1354
1.68M
            if (!SafeReadSymbol(s.context_map_table.slice(), &mut s.br, &mut code, input)) {
1355
12.6k
              s.code = 0xFFFF;
1356
12.6k
              s.context_index = context_index;
1357
12.6k
              return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1358
1.67M
            }
1359
            BROTLI_LOG_UINT!(code);
1360
1361
1.67M
            if code == 0 {
1362
112k
              fast_mut!((context_map)[context_index as usize]) = 0;
1363
              BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
1364
112k
              context_index += 1;
1365
112k
              continue;
1366
1.55M
            }
1367
1.55M
            if code > max_run_length_prefix {
1368
1.46M
              fast_mut!((context_map)[context_index as usize]) =
1369
1.46M
                (code - max_run_length_prefix) as u8;
1370
              BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
1371
1.46M
              context_index += 1;
1372
1.46M
              continue;
1373
96.4k
            }
1374
957
          }
1375
97.4k
          rleCodeGoto = false; // <- this was a goto beforehand... now we have reached label
1376
          // we are treated like everyday citizens from this point forth
1377
          {
1378
97.4k
            let mut reps: u32 = 0;
1379
97.4k
            if (!bit_reader::BrotliSafeReadBits(&mut s.br, code, &mut reps, input)) {
1380
985
              s.code = code;
1381
985
              s.context_index = context_index;
1382
985
              return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1383
96.4k
            }
1384
96.4k
            reps += 1u32 << code;
1385
            BROTLI_LOG_UINT!(reps);
1386
96.4k
            if (context_index + reps > context_map_size) {
1387
22
              return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT;
1388
96.4k
            }
1389
            loop {
1390
1.01M
              fast_mut!((context_map)[context_index as usize]) = 0;
1391
              BROTLI_LOG_ARRAY_INDEX!(context_map, context_index as usize);
1392
1.01M
              context_index += 1;
1393
1.01M
              reps -= 1;
1394
1.01M
              if reps == 0 {
1395
96.4k
                break;
1396
918k
              }
1397
            }
1398
          }
1399
        }
1400
        // No break, continue to next state.
1401
10.3k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1402
      }
1403
      BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM => {
1404
10.4k
        let mut bits: u32 = 0;
1405
10.4k
        if (!bit_reader::BrotliSafeReadBits(&mut s.br, 1, &mut bits, input)) {
1406
63
          s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_TRANSFORM;
1407
63
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1408
10.3k
        }
1409
10.3k
        if (bits != 0) {
1410
8.83k
          if let Ok(ref mut mtf) = s.mtf_or_error_string {
1411
8.83k
            InverseMoveToFrontTransform(context_map_arg.slice_mut(),
1412
8.83k
                                        context_map_size,
1413
8.83k
                                        mtf,
1414
8.83k
                                        &mut s.mtf_upper_bound);
1415
8.83k
          } else {
1416
            // the error state is stored here--we can't make it this deep with an active error
1417
0
            return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
1418
          }
1419
1.50k
        }
1420
10.3k
        s.substate_context_map = BrotliRunningContextMapState::BROTLI_STATE_CONTEXT_MAP_NONE;
1421
10.3k
        return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1422
      }
1423
    }
1424
  }
1425
  // unreachable!(); (compiler will error if it's reachable due to the unit return type)
1426
119k
}
1427
1428
119k
fn DecodeContextMap<AllocU8: alloc::Allocator<u8>,
1429
119k
                    AllocU32: alloc::Allocator<u32>,
1430
119k
                    AllocHC: alloc::Allocator<HuffmanCode>>
1431
119k
  (context_map_size: usize,
1432
119k
   is_dist_context_map: bool,
1433
119k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1434
119k
   input: &[u8])
1435
119k
   -> BrotliDecoderErrorCode {
1436
1437
119k
  match s.state {
1438
67.1k
    BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1 => assert_eq!(is_dist_context_map, false),
1439
51.8k
    BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2 => assert_eq!(is_dist_context_map, true),
1440
0
    _ => unreachable!(),
1441
  }
1442
119k
  let (mut num_htrees, mut context_map_arg) = if is_dist_context_map {
1443
51.8k
    (s.num_dist_htrees, mem::replace(&mut s.dist_context_map, AllocU8::AllocatedMemory::default()))
1444
  } else {
1445
67.1k
    (s.num_literal_htrees, mem::replace(&mut s.context_map, AllocU8::AllocatedMemory::default()))
1446
  };
1447
1448
119k
  let retval = DecodeContextMapInner(context_map_size as u32,
1449
119k
                                     &mut num_htrees,
1450
119k
                                     &mut context_map_arg,
1451
119k
                                     &mut s,
1452
119k
                                     input);
1453
119k
  if is_dist_context_map {
1454
51.8k
    s.num_dist_htrees = num_htrees;
1455
51.8k
    let _ = mem::replace(&mut s.dist_context_map,
1456
51.8k
                 mem::replace(&mut context_map_arg, AllocU8::AllocatedMemory::default()));
1457
67.1k
  } else {
1458
67.1k
    s.num_literal_htrees = num_htrees;
1459
67.1k
    let _ = mem::replace(&mut s.context_map,
1460
67.1k
                 mem::replace(&mut context_map_arg, AllocU8::AllocatedMemory::default()));
1461
67.1k
  }
1462
119k
  retval
1463
119k
}
Unexecuted instantiation: brotli_decompressor::decode::DecodeContextMap::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DecodeContextMap::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1428
119k
fn DecodeContextMap<AllocU8: alloc::Allocator<u8>,
1429
119k
                    AllocU32: alloc::Allocator<u32>,
1430
119k
                    AllocHC: alloc::Allocator<HuffmanCode>>
1431
119k
  (context_map_size: usize,
1432
119k
   is_dist_context_map: bool,
1433
119k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1434
119k
   input: &[u8])
1435
119k
   -> BrotliDecoderErrorCode {
1436
1437
119k
  match s.state {
1438
67.1k
    BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1 => assert_eq!(is_dist_context_map, false),
1439
51.8k
    BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2 => assert_eq!(is_dist_context_map, true),
1440
0
    _ => unreachable!(),
1441
  }
1442
119k
  let (mut num_htrees, mut context_map_arg) = if is_dist_context_map {
1443
51.8k
    (s.num_dist_htrees, mem::replace(&mut s.dist_context_map, AllocU8::AllocatedMemory::default()))
1444
  } else {
1445
67.1k
    (s.num_literal_htrees, mem::replace(&mut s.context_map, AllocU8::AllocatedMemory::default()))
1446
  };
1447
1448
119k
  let retval = DecodeContextMapInner(context_map_size as u32,
1449
119k
                                     &mut num_htrees,
1450
119k
                                     &mut context_map_arg,
1451
119k
                                     &mut s,
1452
119k
                                     input);
1453
119k
  if is_dist_context_map {
1454
51.8k
    s.num_dist_htrees = num_htrees;
1455
51.8k
    let _ = mem::replace(&mut s.dist_context_map,
1456
51.8k
                 mem::replace(&mut context_map_arg, AllocU8::AllocatedMemory::default()));
1457
67.1k
  } else {
1458
67.1k
    s.num_literal_htrees = num_htrees;
1459
67.1k
    let _ = mem::replace(&mut s.context_map,
1460
67.1k
                 mem::replace(&mut context_map_arg, AllocU8::AllocatedMemory::default()));
1461
67.1k
  }
1462
119k
  retval
1463
119k
}
1464
1465
// Decodes a command or literal and updates block type ringbuffer.
1466
// Reads 3..54 bits.
1467
3.95M
fn DecodeBlockTypeAndLength<
1468
3.95M
  AllocHC : alloc::Allocator<HuffmanCode>> (safe : bool,
1469
3.95M
                                            s : &mut BlockTypeAndLengthState<AllocHC>,
1470
3.95M
                                            br : &mut bit_reader::BrotliBitReader,
1471
3.95M
                                            tree_type : i32,
1472
3.95M
                                            input : &[u8]) -> bool {
1473
3.95M
  let max_block_type = fast!((s.num_block_types)[tree_type as usize]);
1474
3.95M
  let tree_offset = tree_type as usize * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as usize;
1475
1476
3.95M
  let mut block_type: u32 = 0;
1477
3.95M
  if max_block_type <= 1 {
1478
1
    return false;
1479
3.95M
  }
1480
  // Read 0..15 + 3..39 bits
1481
3.95M
  if (!safe) {
1482
3.92M
    block_type = ReadSymbol(fast_slice!((s.block_type_trees)[tree_offset;]), br, input);
1483
3.92M
    fast_mut!((s.block_length)[tree_type as usize]) =
1484
3.92M
      ReadBlockLength(fast_slice!((s.block_len_trees)[tree_offset;]), br, input);
1485
3.92M
  } else {
1486
27.1k
    let memento = bit_reader::BrotliBitReaderSaveState(br);
1487
27.1k
    if (!SafeReadSymbol(fast_slice!((s.block_type_trees)[tree_offset;]),
1488
27.1k
                        br,
1489
27.1k
                        &mut block_type,
1490
27.1k
                        input)) {
1491
2.17k
      return false;
1492
24.9k
    }
1493
24.9k
    let mut block_length_out: u32 = 0;
1494
1495
24.9k
    let index_ret = SafeReadBlockLengthIndex(&s.substate_read_block_length,
1496
24.9k
                                             s.block_length_index,
1497
24.9k
                                             fast_slice!((s.block_len_trees)[tree_offset;]),
1498
24.9k
                                             br,
1499
24.9k
                                             input);
1500
24.9k
    if !SafeReadBlockLengthFromIndex(s, br, &mut block_length_out, index_ret, input) {
1501
7.23k
      s.substate_read_block_length =
1502
7.23k
        BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
1503
7.23k
      bit_reader::BrotliBitReaderRestoreState(br, &memento);
1504
7.23k
      return false;
1505
17.7k
    }
1506
17.7k
    fast_mut!((s.block_length)[tree_type as usize]) = block_length_out;
1507
  }
1508
3.94M
  let ringbuffer: &mut [u32] = &mut fast_mut!((s.block_type_rb)[tree_type as usize * 2;]);
1509
3.94M
  if (block_type == 1) {
1510
2.36M
    block_type = fast!((ringbuffer)[1]) + 1;
1511
2.36M
  } else if (block_type == 0) {
1512
508k
    block_type = fast!((ringbuffer)[0]);
1513
1.06M
  } else {
1514
1.06M
    block_type -= 2;
1515
1.06M
  }
1516
3.94M
  if (block_type >= max_block_type) {
1517
411k
    block_type -= max_block_type;
1518
3.53M
  }
1519
3.94M
  fast_mut!((ringbuffer)[0]) = fast!((ringbuffer)[1]);
1520
3.94M
  fast_mut!((ringbuffer)[1]) = block_type;
1521
3.94M
  true
1522
3.95M
}
Unexecuted instantiation: brotli_decompressor::decode::DecodeBlockTypeAndLength::<alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DecodeBlockTypeAndLength::<alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1467
3.95M
fn DecodeBlockTypeAndLength<
1468
3.95M
  AllocHC : alloc::Allocator<HuffmanCode>> (safe : bool,
1469
3.95M
                                            s : &mut BlockTypeAndLengthState<AllocHC>,
1470
3.95M
                                            br : &mut bit_reader::BrotliBitReader,
1471
3.95M
                                            tree_type : i32,
1472
3.95M
                                            input : &[u8]) -> bool {
1473
3.95M
  let max_block_type = fast!((s.num_block_types)[tree_type as usize]);
1474
3.95M
  let tree_offset = tree_type as usize * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as usize;
1475
1476
3.95M
  let mut block_type: u32 = 0;
1477
3.95M
  if max_block_type <= 1 {
1478
1
    return false;
1479
3.95M
  }
1480
  // Read 0..15 + 3..39 bits
1481
3.95M
  if (!safe) {
1482
3.92M
    block_type = ReadSymbol(fast_slice!((s.block_type_trees)[tree_offset;]), br, input);
1483
3.92M
    fast_mut!((s.block_length)[tree_type as usize]) =
1484
3.92M
      ReadBlockLength(fast_slice!((s.block_len_trees)[tree_offset;]), br, input);
1485
3.92M
  } else {
1486
27.1k
    let memento = bit_reader::BrotliBitReaderSaveState(br);
1487
27.1k
    if (!SafeReadSymbol(fast_slice!((s.block_type_trees)[tree_offset;]),
1488
27.1k
                        br,
1489
27.1k
                        &mut block_type,
1490
27.1k
                        input)) {
1491
2.17k
      return false;
1492
24.9k
    }
1493
24.9k
    let mut block_length_out: u32 = 0;
1494
1495
24.9k
    let index_ret = SafeReadBlockLengthIndex(&s.substate_read_block_length,
1496
24.9k
                                             s.block_length_index,
1497
24.9k
                                             fast_slice!((s.block_len_trees)[tree_offset;]),
1498
24.9k
                                             br,
1499
24.9k
                                             input);
1500
24.9k
    if !SafeReadBlockLengthFromIndex(s, br, &mut block_length_out, index_ret, input) {
1501
7.23k
      s.substate_read_block_length =
1502
7.23k
        BrotliRunningReadBlockLengthState::BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
1503
7.23k
      bit_reader::BrotliBitReaderRestoreState(br, &memento);
1504
7.23k
      return false;
1505
17.7k
    }
1506
17.7k
    fast_mut!((s.block_length)[tree_type as usize]) = block_length_out;
1507
  }
1508
3.94M
  let ringbuffer: &mut [u32] = &mut fast_mut!((s.block_type_rb)[tree_type as usize * 2;]);
1509
3.94M
  if (block_type == 1) {
1510
2.36M
    block_type = fast!((ringbuffer)[1]) + 1;
1511
2.36M
  } else if (block_type == 0) {
1512
508k
    block_type = fast!((ringbuffer)[0]);
1513
1.06M
  } else {
1514
1.06M
    block_type -= 2;
1515
1.06M
  }
1516
3.94M
  if (block_type >= max_block_type) {
1517
411k
    block_type -= max_block_type;
1518
3.53M
  }
1519
3.94M
  fast_mut!((ringbuffer)[0]) = fast!((ringbuffer)[1]);
1520
3.94M
  fast_mut!((ringbuffer)[1]) = block_type;
1521
3.94M
  true
1522
3.95M
}
1523
51.0k
fn DetectTrivialLiteralBlockTypes<AllocU8: alloc::Allocator<u8>,
1524
51.0k
                                  AllocU32: alloc::Allocator<u32>,
1525
51.0k
                                  AllocHC: alloc::Allocator<HuffmanCode>>
1526
51.0k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
1527
408k
  for iter in s.trivial_literal_contexts.iter_mut() {
1528
408k
    *iter = 0;
1529
408k
  }
1530
51.0k
  let mut i: usize = 0;
1531
164k
  while i < fast!((s.block_type_length_state.num_block_types)[0]) as usize {
1532
113k
    let offset = (i as usize) << kLiteralContextBits;
1533
113k
    let mut error = 0usize;
1534
113k
    let sample: usize = fast_slice!((s.context_map)[offset]) as usize;
1535
113k
    let mut j = 0usize;
1536
1.93M
    while j < ((1 as usize) << kLiteralContextBits) {
1537
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1538
1.82M
      j += 1;
1539
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1540
1.82M
      j += 1;
1541
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1542
1.82M
      j += 1;
1543
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1544
1.82M
      j += 1
1545
    }
1546
113k
    if error == 0 {
1547
78.2k
      s.trivial_literal_contexts[i >> 5] |= ((1 as u32) << (i & 31));
1548
78.2k
    }
1549
113k
    i += 1
1550
  }
1551
51.0k
}
Unexecuted instantiation: brotli_decompressor::decode::DetectTrivialLiteralBlockTypes::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DetectTrivialLiteralBlockTypes::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1523
51.0k
fn DetectTrivialLiteralBlockTypes<AllocU8: alloc::Allocator<u8>,
1524
51.0k
                                  AllocU32: alloc::Allocator<u32>,
1525
51.0k
                                  AllocHC: alloc::Allocator<HuffmanCode>>
1526
51.0k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
1527
408k
  for iter in s.trivial_literal_contexts.iter_mut() {
1528
408k
    *iter = 0;
1529
408k
  }
1530
51.0k
  let mut i: usize = 0;
1531
164k
  while i < fast!((s.block_type_length_state.num_block_types)[0]) as usize {
1532
113k
    let offset = (i as usize) << kLiteralContextBits;
1533
113k
    let mut error = 0usize;
1534
113k
    let sample: usize = fast_slice!((s.context_map)[offset]) as usize;
1535
113k
    let mut j = 0usize;
1536
1.93M
    while j < ((1 as usize) << kLiteralContextBits) {
1537
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1538
1.82M
      j += 1;
1539
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1540
1.82M
      j += 1;
1541
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1542
1.82M
      j += 1;
1543
1.82M
      error |= fast_slice!((s.context_map)[offset + j]) as usize ^ sample;
1544
1.82M
      j += 1
1545
    }
1546
113k
    if error == 0 {
1547
78.2k
      s.trivial_literal_contexts[i >> 5] |= ((1 as u32) << (i & 31));
1548
78.2k
    }
1549
113k
    i += 1
1550
  }
1551
51.0k
}
1552
3.12M
fn PrepareLiteralDecoding<AllocU8: alloc::Allocator<u8>,
1553
3.12M
                          AllocU32: alloc::Allocator<u32>,
1554
3.12M
                          AllocHC: alloc::Allocator<HuffmanCode>>
1555
3.12M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
1556
1557
  let context_offset: u32;
1558
3.12M
  let block_type = fast!((s.block_type_length_state.block_type_rb)[1]) as usize;
1559
3.12M
  context_offset = (block_type << kLiteralContextBits) as u32;
1560
3.12M
  s.context_map_slice_index = context_offset as usize;
1561
3.12M
  let trivial = fast!((s.trivial_literal_contexts)[block_type >> 5]);
1562
3.12M
  s.trivial_literal_context = ((trivial >> (block_type & 31)) & 1) as i32;
1563
1564
3.12M
  s.literal_htree_index = fast_slice!((s.context_map)[s.context_map_slice_index]);
1565
  // s.literal_htree = fast!((s.literal_hgroup.htrees)[s.literal_htree_index]); // redundant
1566
3.12M
  let context_mode_index = fast!((s.context_modes.slice())[block_type]) & 3;
1567
3.12M
  s.context_lookup = &kContextLookup[context_mode_index as usize];
1568
3.12M
}
Unexecuted instantiation: brotli_decompressor::decode::PrepareLiteralDecoding::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::PrepareLiteralDecoding::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1552
3.12M
fn PrepareLiteralDecoding<AllocU8: alloc::Allocator<u8>,
1553
3.12M
                          AllocU32: alloc::Allocator<u32>,
1554
3.12M
                          AllocHC: alloc::Allocator<HuffmanCode>>
1555
3.12M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
1556
1557
  let context_offset: u32;
1558
3.12M
  let block_type = fast!((s.block_type_length_state.block_type_rb)[1]) as usize;
1559
3.12M
  context_offset = (block_type << kLiteralContextBits) as u32;
1560
3.12M
  s.context_map_slice_index = context_offset as usize;
1561
3.12M
  let trivial = fast!((s.trivial_literal_contexts)[block_type >> 5]);
1562
3.12M
  s.trivial_literal_context = ((trivial >> (block_type & 31)) & 1) as i32;
1563
1564
3.12M
  s.literal_htree_index = fast_slice!((s.context_map)[s.context_map_slice_index]);
1565
  // s.literal_htree = fast!((s.literal_hgroup.htrees)[s.literal_htree_index]); // redundant
1566
3.12M
  let context_mode_index = fast!((s.context_modes.slice())[block_type]) & 3;
1567
3.12M
  s.context_lookup = &kContextLookup[context_mode_index as usize];
1568
3.12M
}
1569
1570
// Decodes the block ty
1571
// pe and updates the state for literal context.
1572
// Reads 3..54 bits.
1573
3.08M
fn DecodeLiteralBlockSwitchInternal<AllocU8: alloc::Allocator<u8>,
1574
3.08M
                                    AllocU32: alloc::Allocator<u32>,
1575
3.08M
                                    AllocHC: alloc::Allocator<HuffmanCode>>
1576
3.08M
  (safe: bool,
1577
3.08M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1578
3.08M
   input: &[u8])
1579
3.08M
   -> bool {
1580
1581
3.08M
  if !DecodeBlockTypeAndLength(safe, &mut s.block_type_length_state, &mut s.br, 0, input) {
1582
7.51k
    return false;
1583
3.07M
  }
1584
3.07M
  PrepareLiteralDecoding(s);
1585
3.07M
  true
1586
3.08M
}
Unexecuted instantiation: brotli_decompressor::decode::DecodeLiteralBlockSwitchInternal::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DecodeLiteralBlockSwitchInternal::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1573
3.08M
fn DecodeLiteralBlockSwitchInternal<AllocU8: alloc::Allocator<u8>,
1574
3.08M
                                    AllocU32: alloc::Allocator<u32>,
1575
3.08M
                                    AllocHC: alloc::Allocator<HuffmanCode>>
1576
3.08M
  (safe: bool,
1577
3.08M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1578
3.08M
   input: &[u8])
1579
3.08M
   -> bool {
1580
1581
3.08M
  if !DecodeBlockTypeAndLength(safe, &mut s.block_type_length_state, &mut s.br, 0, input) {
1582
7.51k
    return false;
1583
3.07M
  }
1584
3.07M
  PrepareLiteralDecoding(s);
1585
3.07M
  true
1586
3.08M
}
1587
// fn DecodeLiteralBlockSwitch<
1588
// 'a,
1589
// AllocU8 : alloc::Allocator<u8>,
1590
// AllocU32 : alloc::Allocator<u32>,
1591
// AllocHC : alloc::Allocator<HuffmanCode>> (safe : bool,
1592
// mut s : &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
1593
// DecodeLiteralBlockSwitchInternal(false, s);
1594
// }
1595
//
1596
// fn SafeDecodeLiteralBlockSwitch<
1597
// 'a,
1598
// AllocU8 : alloc::Allocator<u8>,
1599
// AllocU32 : alloc::Allocator<u32>,
1600
// AllocHC : alloc::Allocator<HuffmanCode>> (safe : bool,
1601
// mut s : &mut BrotliState<AllocU8, AllocU32, AllocHC>) -> bool {
1602
// return DecodeLiteralBlockSwitchInternal(true, s);
1603
// }
1604
//
1605
// Block switch for insert/copy length.
1606
// Reads 3..54 bits.
1607
119k
fn DecodeCommandBlockSwitchInternal<AllocU8: alloc::Allocator<u8>,
1608
119k
                                    AllocU32: alloc::Allocator<u32>,
1609
119k
                                    AllocHC: alloc::Allocator<HuffmanCode>>
1610
119k
  (safe: bool,
1611
119k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1612
119k
   input: &[u8])
1613
119k
   -> bool {
1614
119k
  if (!DecodeBlockTypeAndLength(safe, &mut s.block_type_length_state, &mut s.br, 1, input)) {
1615
1.29k
    return false;
1616
117k
  }
1617
117k
  s.htree_command_index = fast!((s.block_type_length_state.block_type_rb)[3]) as u16;
1618
117k
  true
1619
119k
}
Unexecuted instantiation: brotli_decompressor::decode::DecodeCommandBlockSwitchInternal::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DecodeCommandBlockSwitchInternal::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1607
119k
fn DecodeCommandBlockSwitchInternal<AllocU8: alloc::Allocator<u8>,
1608
119k
                                    AllocU32: alloc::Allocator<u32>,
1609
119k
                                    AllocHC: alloc::Allocator<HuffmanCode>>
1610
119k
  (safe: bool,
1611
119k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1612
119k
   input: &[u8])
1613
119k
   -> bool {
1614
119k
  if (!DecodeBlockTypeAndLength(safe, &mut s.block_type_length_state, &mut s.br, 1, input)) {
1615
1.29k
    return false;
1616
117k
  }
1617
117k
  s.htree_command_index = fast!((s.block_type_length_state.block_type_rb)[3]) as u16;
1618
117k
  true
1619
119k
}
1620
1621
#[allow(dead_code)]
1622
0
fn DecodeCommandBlockSwitch<AllocU8: alloc::Allocator<u8>,
1623
0
                            AllocU32: alloc::Allocator<u32>,
1624
0
                            AllocHC: alloc::Allocator<HuffmanCode>>
1625
0
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1626
0
   input: &[u8]) {
1627
0
  DecodeCommandBlockSwitchInternal(false, s, input);
1628
0
}
1629
#[allow(dead_code)]
1630
0
fn SafeDecodeCommandBlockSwitch<AllocU8: alloc::Allocator<u8>,
1631
0
                                AllocU32: alloc::Allocator<u32>,
1632
0
                                AllocHC: alloc::Allocator<HuffmanCode>>
1633
0
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1634
0
   input: &[u8])
1635
0
   -> bool {
1636
0
  DecodeCommandBlockSwitchInternal(true, s, input)
1637
0
}
1638
1639
// Block switch for distance codes.
1640
// Reads 3..54 bits.
1641
749k
fn DecodeDistanceBlockSwitchInternal<AllocU8: alloc::Allocator<u8>,
1642
749k
                                     AllocU32: alloc::Allocator<u32>,
1643
749k
                                     AllocHC: alloc::Allocator<HuffmanCode>>
1644
749k
  (safe: bool,
1645
749k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1646
749k
   input: &[u8])
1647
749k
   -> bool {
1648
749k
  if (!DecodeBlockTypeAndLength(safe, &mut s.block_type_length_state, &mut s.br, 2, input)) {
1649
611
    return false;
1650
748k
  }
1651
748k
  s.dist_context_map_slice_index =
1652
748k
    (fast!((s.block_type_length_state.block_type_rb)[5]) << kDistanceContextBits) as usize;
1653
748k
  s.dist_htree_index = fast_slice!((s.dist_context_map)[s.dist_context_map_slice_index
1654
748k
                                                  + s.distance_context as usize]);
1655
748k
  true
1656
749k
}
Unexecuted instantiation: brotli_decompressor::decode::DecodeDistanceBlockSwitchInternal::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::DecodeDistanceBlockSwitchInternal::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1641
749k
fn DecodeDistanceBlockSwitchInternal<AllocU8: alloc::Allocator<u8>,
1642
749k
                                     AllocU32: alloc::Allocator<u32>,
1643
749k
                                     AllocHC: alloc::Allocator<HuffmanCode>>
1644
749k
  (safe: bool,
1645
749k
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1646
749k
   input: &[u8])
1647
749k
   -> bool {
1648
749k
  if (!DecodeBlockTypeAndLength(safe, &mut s.block_type_length_state, &mut s.br, 2, input)) {
1649
611
    return false;
1650
748k
  }
1651
748k
  s.dist_context_map_slice_index =
1652
748k
    (fast!((s.block_type_length_state.block_type_rb)[5]) << kDistanceContextBits) as usize;
1653
748k
  s.dist_htree_index = fast_slice!((s.dist_context_map)[s.dist_context_map_slice_index
1654
748k
                                                  + s.distance_context as usize]);
1655
748k
  true
1656
749k
}
1657
1658
#[allow(dead_code)]
1659
0
fn DecodeDistanceBlockSwitch<AllocU8: alloc::Allocator<u8>,
1660
0
                             AllocU32: alloc::Allocator<u32>,
1661
0
                             AllocHC: alloc::Allocator<HuffmanCode>>
1662
0
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1663
0
   input: &[u8]) {
1664
0
  DecodeDistanceBlockSwitchInternal(false, s, input);
1665
0
}
1666
1667
#[allow(dead_code)]
1668
0
fn SafeDecodeDistanceBlockSwitch<AllocU8: alloc::Allocator<u8>,
1669
0
                                 AllocU32: alloc::Allocator<u32>,
1670
0
                                 AllocHC: alloc::Allocator<HuffmanCode>>
1671
0
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1672
0
   input: &[u8])
1673
0
   -> bool {
1674
0
  DecodeDistanceBlockSwitchInternal(true, s, input)
1675
0
}
1676
1677
171M
fn UnwrittenBytes<AllocU8: alloc::Allocator<u8>,
1678
171M
                  AllocU32: alloc::Allocator<u32>,
1679
171M
                  AllocHC: alloc::Allocator<HuffmanCode>> (
1680
171M
  s: &BrotliState<AllocU8, AllocU32, AllocHC>,
1681
171M
  wrap: bool,
1682
171M
)  -> usize {
1683
171M
  let pos = if wrap && s.pos > s.ringbuffer_size {
1684
1.77M
    s.ringbuffer_size as usize
1685
  } else {
1686
170M
    s.pos as usize
1687
  };
1688
171M
  let partial_pos_rb = (s.rb_roundtrips as usize * s.ringbuffer_size as usize) + pos as usize;
1689
171M
  (partial_pos_rb - s.partial_pos_out) as usize
1690
171M
}
Unexecuted instantiation: brotli_decompressor::decode::UnwrittenBytes::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::UnwrittenBytes::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1677
171M
fn UnwrittenBytes<AllocU8: alloc::Allocator<u8>,
1678
171M
                  AllocU32: alloc::Allocator<u32>,
1679
171M
                  AllocHC: alloc::Allocator<HuffmanCode>> (
1680
171M
  s: &BrotliState<AllocU8, AllocU32, AllocHC>,
1681
171M
  wrap: bool,
1682
171M
)  -> usize {
1683
171M
  let pos = if wrap && s.pos > s.ringbuffer_size {
1684
1.77M
    s.ringbuffer_size as usize
1685
  } else {
1686
170M
    s.pos as usize
1687
  };
1688
171M
  let partial_pos_rb = (s.rb_roundtrips as usize * s.ringbuffer_size as usize) + pos as usize;
1689
171M
  (partial_pos_rb - s.partial_pos_out) as usize
1690
171M
}
1691
171M
fn WriteRingBuffer<'a,
1692
171M
                   AllocU8: alloc::Allocator<u8>,
1693
171M
                   AllocU32: alloc::Allocator<u32>,
1694
171M
                   AllocHC: alloc::Allocator<HuffmanCode>>(
1695
171M
  available_out: &mut usize,
1696
171M
  opt_output: Option<&mut [u8]>,
1697
171M
  output_offset: &mut usize,
1698
171M
  total_out: &mut usize,
1699
171M
  force: bool,
1700
171M
  s: &'a mut BrotliState<AllocU8, AllocU32, AllocHC>,
1701
171M
) -> (BrotliDecoderErrorCode, &'a [u8]) {
1702
171M
  let to_write = UnwrittenBytes(s, true);
1703
171M
  let mut num_written = *available_out as usize;
1704
171M
  if (num_written > to_write) {
1705
4.85M
    num_written = to_write;
1706
167M
  }
1707
171M
  if (s.meta_block_remaining_len < 0) {
1708
381
    return (BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1, &[]);
1709
171M
  }
1710
171M
  let start_index = (s.partial_pos_out & s.ringbuffer_mask as usize) as usize;
1711
171M
  let start = fast_slice!((s.ringbuffer)[start_index ; start_index + num_written as usize]);
1712
171M
  if let Some(output) = opt_output {
1713
171M
    fast_mut!((output)[*output_offset ; *output_offset + num_written as usize])
1714
171M
      .clone_from_slice(start);
1715
171M
  }
1716
171M
  *output_offset += num_written;
1717
171M
  *available_out -= num_written;
1718
  BROTLI_LOG_UINT!(to_write);
1719
  BROTLI_LOG_UINT!(num_written);
1720
171M
  s.partial_pos_out += num_written as usize;
1721
171M
  *total_out = s.partial_pos_out;
1722
171M
  if (num_written < to_write) {
1723
166M
    if s.ringbuffer_size == (1 << s.window_bits) || force {
1724
166M
      return (BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT, &[]);
1725
    } else {
1726
0
      return (BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS, start);
1727
    }
1728
5.04M
  }
1729
5.04M
  if (s.ringbuffer_size == (1 << s.window_bits) &&
1730
4.41M
      s.pos >= s.ringbuffer_size) {
1731
536k
    s.pos -= s.ringbuffer_size;
1732
536k
    s.rb_roundtrips += 1;
1733
536k
    s.should_wrap_ringbuffer = s.pos != 0;
1734
4.51M
  }
1735
5.04M
  (BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS, start)
1736
171M
 }
Unexecuted instantiation: brotli_decompressor::decode::WriteRingBuffer::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::WriteRingBuffer::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1691
171M
fn WriteRingBuffer<'a,
1692
171M
                   AllocU8: alloc::Allocator<u8>,
1693
171M
                   AllocU32: alloc::Allocator<u32>,
1694
171M
                   AllocHC: alloc::Allocator<HuffmanCode>>(
1695
171M
  available_out: &mut usize,
1696
171M
  opt_output: Option<&mut [u8]>,
1697
171M
  output_offset: &mut usize,
1698
171M
  total_out: &mut usize,
1699
171M
  force: bool,
1700
171M
  s: &'a mut BrotliState<AllocU8, AllocU32, AllocHC>,
1701
171M
) -> (BrotliDecoderErrorCode, &'a [u8]) {
1702
171M
  let to_write = UnwrittenBytes(s, true);
1703
171M
  let mut num_written = *available_out as usize;
1704
171M
  if (num_written > to_write) {
1705
4.85M
    num_written = to_write;
1706
167M
  }
1707
171M
  if (s.meta_block_remaining_len < 0) {
1708
381
    return (BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1, &[]);
1709
171M
  }
1710
171M
  let start_index = (s.partial_pos_out & s.ringbuffer_mask as usize) as usize;
1711
171M
  let start = fast_slice!((s.ringbuffer)[start_index ; start_index + num_written as usize]);
1712
171M
  if let Some(output) = opt_output {
1713
171M
    fast_mut!((output)[*output_offset ; *output_offset + num_written as usize])
1714
171M
      .clone_from_slice(start);
1715
171M
  }
1716
171M
  *output_offset += num_written;
1717
171M
  *available_out -= num_written;
1718
  BROTLI_LOG_UINT!(to_write);
1719
  BROTLI_LOG_UINT!(num_written);
1720
171M
  s.partial_pos_out += num_written as usize;
1721
171M
  *total_out = s.partial_pos_out;
1722
171M
  if (num_written < to_write) {
1723
166M
    if s.ringbuffer_size == (1 << s.window_bits) || force {
1724
166M
      return (BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT, &[]);
1725
    } else {
1726
0
      return (BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS, start);
1727
    }
1728
5.04M
  }
1729
5.04M
  if (s.ringbuffer_size == (1 << s.window_bits) &&
1730
4.41M
      s.pos >= s.ringbuffer_size) {
1731
536k
    s.pos -= s.ringbuffer_size;
1732
536k
    s.rb_roundtrips += 1;
1733
536k
    s.should_wrap_ringbuffer = s.pos != 0;
1734
4.51M
  }
1735
5.04M
  (BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS, start)
1736
171M
 }
1737
1738
527k
fn WrapRingBuffer<AllocU8: alloc::Allocator<u8>,
1739
527k
                   AllocU32: alloc::Allocator<u32>,
1740
527k
                   AllocHC: alloc::Allocator<HuffmanCode>>(
1741
527k
  s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1742
527k
) {
1743
527k
  if s.should_wrap_ringbuffer {
1744
7.28k
    let (ring_buffer_start, ring_buffer_end) = s.ringbuffer.slice_mut().split_at_mut(s.ringbuffer_size as usize);
1745
7.28k
    let pos = s.pos as usize;
1746
7.28k
    ring_buffer_start.split_at_mut(pos).0.clone_from_slice(ring_buffer_end.split_at(pos).0);
1747
7.28k
    s.should_wrap_ringbuffer = false;
1748
520k
  }
1749
1750
527k
}
Unexecuted instantiation: brotli_decompressor::decode::WrapRingBuffer::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::WrapRingBuffer::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1738
527k
fn WrapRingBuffer<AllocU8: alloc::Allocator<u8>,
1739
527k
                   AllocU32: alloc::Allocator<u32>,
1740
527k
                   AllocHC: alloc::Allocator<HuffmanCode>>(
1741
527k
  s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1742
527k
) {
1743
527k
  if s.should_wrap_ringbuffer {
1744
7.28k
    let (ring_buffer_start, ring_buffer_end) = s.ringbuffer.slice_mut().split_at_mut(s.ringbuffer_size as usize);
1745
7.28k
    let pos = s.pos as usize;
1746
7.28k
    ring_buffer_start.split_at_mut(pos).0.clone_from_slice(ring_buffer_end.split_at(pos).0);
1747
7.28k
    s.should_wrap_ringbuffer = false;
1748
520k
  }
1749
1750
527k
}
1751
1752
699k
fn CopyUncompressedBlockToOutput<AllocU8: alloc::Allocator<u8>,
1753
699k
                                 AllocU32: alloc::Allocator<u32>,
1754
699k
                                 AllocHC: alloc::Allocator<HuffmanCode>>
1755
699k
  (mut available_out: &mut usize,
1756
699k
   mut output: &mut [u8],
1757
699k
   mut output_offset: &mut usize,
1758
699k
   mut total_out: &mut usize,
1759
699k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1760
699k
   input: &[u8])
1761
699k
   -> BrotliDecoderErrorCode {
1762
  // State machine
1763
  loop {
1764
717k
    match s.substate_uncompressed {
1765
      BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_NONE => {
1766
674k
        let mut nbytes = bit_reader::BrotliGetRemainingBytes(&s.br) as i32;
1767
674k
        if (nbytes > s.meta_block_remaining_len) {
1768
13.9k
          nbytes = s.meta_block_remaining_len;
1769
660k
        }
1770
674k
        if (s.pos + nbytes > s.ringbuffer_size) {
1771
8.61k
          nbytes = s.ringbuffer_size - s.pos;
1772
666k
        }
1773
        // Copy remaining bytes from s.br.buf_ to ringbuffer.
1774
674k
        bit_reader::BrotliCopyBytes(fast_mut!((s.ringbuffer.slice_mut())[s.pos as usize;]),
1775
674k
                                    &mut s.br,
1776
674k
                                    nbytes as u32,
1777
674k
                                    input);
1778
674k
        s.pos += nbytes;
1779
674k
        s.meta_block_remaining_len -= nbytes;
1780
674k
        if s.pos < (1 << s.window_bits) {
1781
665k
          if (s.meta_block_remaining_len == 0) {
1782
11.9k
            return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1783
653k
          }
1784
653k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1785
8.75k
        }
1786
8.75k
        s.substate_uncompressed = BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_WRITE;
1787
        // s.partial_pos_rb += (size_t)s.ringbuffer_size;
1788
        // No break, continue to next state by going aroudn the loop
1789
      }
1790
      BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_WRITE => {
1791
42.8k
        let (result, _) = WriteRingBuffer(&mut available_out,
1792
42.8k
                                          Some(&mut output),
1793
42.8k
                                          &mut output_offset,
1794
42.8k
                                          &mut total_out,
1795
42.8k
                                          false,
1796
42.8k
                                          &mut s);
1797
42.8k
        match result {
1798
8.75k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1799
34.0k
          _ => return result,
1800
        }
1801
8.75k
        if s.ringbuffer_size == 1 << s.window_bits {
1802
8.75k
          s.max_distance = s.max_backward_distance;
1803
8.75k
        }
1804
8.75k
        s.substate_uncompressed = BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_NONE;
1805
      }
1806
    }
1807
  }
1808
699k
}
Unexecuted instantiation: brotli_decompressor::decode::CopyUncompressedBlockToOutput::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::CopyUncompressedBlockToOutput::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1752
699k
fn CopyUncompressedBlockToOutput<AllocU8: alloc::Allocator<u8>,
1753
699k
                                 AllocU32: alloc::Allocator<u32>,
1754
699k
                                 AllocHC: alloc::Allocator<HuffmanCode>>
1755
699k
  (mut available_out: &mut usize,
1756
699k
   mut output: &mut [u8],
1757
699k
   mut output_offset: &mut usize,
1758
699k
   mut total_out: &mut usize,
1759
699k
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1760
699k
   input: &[u8])
1761
699k
   -> BrotliDecoderErrorCode {
1762
  // State machine
1763
  loop {
1764
717k
    match s.substate_uncompressed {
1765
      BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_NONE => {
1766
674k
        let mut nbytes = bit_reader::BrotliGetRemainingBytes(&s.br) as i32;
1767
674k
        if (nbytes > s.meta_block_remaining_len) {
1768
13.9k
          nbytes = s.meta_block_remaining_len;
1769
660k
        }
1770
674k
        if (s.pos + nbytes > s.ringbuffer_size) {
1771
8.61k
          nbytes = s.ringbuffer_size - s.pos;
1772
666k
        }
1773
        // Copy remaining bytes from s.br.buf_ to ringbuffer.
1774
674k
        bit_reader::BrotliCopyBytes(fast_mut!((s.ringbuffer.slice_mut())[s.pos as usize;]),
1775
674k
                                    &mut s.br,
1776
674k
                                    nbytes as u32,
1777
674k
                                    input);
1778
674k
        s.pos += nbytes;
1779
674k
        s.meta_block_remaining_len -= nbytes;
1780
674k
        if s.pos < (1 << s.window_bits) {
1781
665k
          if (s.meta_block_remaining_len == 0) {
1782
11.9k
            return BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
1783
653k
          }
1784
653k
          return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1785
8.75k
        }
1786
8.75k
        s.substate_uncompressed = BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_WRITE;
1787
        // s.partial_pos_rb += (size_t)s.ringbuffer_size;
1788
        // No break, continue to next state by going aroudn the loop
1789
      }
1790
      BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_WRITE => {
1791
42.8k
        let (result, _) = WriteRingBuffer(&mut available_out,
1792
42.8k
                                          Some(&mut output),
1793
42.8k
                                          &mut output_offset,
1794
42.8k
                                          &mut total_out,
1795
42.8k
                                          false,
1796
42.8k
                                          &mut s);
1797
42.8k
        match result {
1798
8.75k
          BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
1799
34.0k
          _ => return result,
1800
        }
1801
8.75k
        if s.ringbuffer_size == 1 << s.window_bits {
1802
8.75k
          s.max_distance = s.max_backward_distance;
1803
8.75k
        }
1804
8.75k
        s.substate_uncompressed = BrotliRunningUncompressedState::BROTLI_STATE_UNCOMPRESSED_NONE;
1805
      }
1806
    }
1807
  }
1808
699k
}
1809
1810
17.2k
fn BrotliAllocateRingBuffer<AllocU8: alloc::Allocator<u8>,
1811
17.2k
                            AllocU32: alloc::Allocator<u32>,
1812
17.2k
                            AllocHC: alloc::Allocator<HuffmanCode>>
1813
17.2k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1814
17.2k
   input: &[u8])
1815
17.2k
   -> bool {
1816
  // We need the slack region for the following reasons:
1817
  // - doing up to two 16-byte copies for fast backward copying
1818
  // - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix)
1819
  const kRingBufferWriteAheadSlack: i32 = 42;
1820
17.2k
  let mut is_last = s.is_last_metablock;
1821
17.2k
  s.ringbuffer_size = 1 << s.window_bits;
1822
1823
17.2k
  if (s.is_uncompressed != 0) {
1824
3.06k
    let next_block_header =
1825
3.06k
      bit_reader::BrotliPeekByte(&mut s.br, s.meta_block_remaining_len as u32, input);
1826
3.06k
    if (next_block_header != -1) &&
1827
        // Peek succeeded
1828
2.76k
        ((next_block_header & 3) == 3) {
1829
2.59k
      // ISLAST and ISEMPTY
1830
2.59k
      is_last = 1;
1831
2.59k
    }
1832
14.1k
  }
1833
17.2k
  let max_dict_size = s.ringbuffer_size as usize - 16;
1834
  {
1835
17.2k
    let custom_dict = if s.custom_dict_size as usize > max_dict_size {
1836
0
      let cd = fast_slice!((s.custom_dict)[(s.custom_dict_size as usize - max_dict_size); s.custom_dict_size as usize]);
1837
0
      s.custom_dict_size = max_dict_size as isize;
1838
0
      cd
1839
    } else {
1840
17.2k
      fast_slice!((s.custom_dict)[0; s.custom_dict_size as usize])
1841
    };
1842
1843
    // We need at least 2 bytes of ring buffer size to get the last two
1844
    // bytes for context from there
1845
17.2k
    if (is_last != 0) {
1846
124k
      while (s.ringbuffer_size as isize >= (s.custom_dict_size + s.meta_block_remaining_len as isize + 16) * 2 && s.ringbuffer_size as isize > 32) {
1847
113k
        s.ringbuffer_size >>= 1;
1848
113k
      }
1849
5.39k
    }
1850
17.2k
    if s.ringbuffer_size > (1 << s.window_bits) {
1851
0
      s.ringbuffer_size = (1 << s.window_bits);
1852
17.2k
    }
1853
1854
17.2k
    s.ringbuffer_mask = s.ringbuffer_size - 1;
1855
17.2k
    s.ringbuffer = s.alloc_u8
1856
17.2k
      .alloc_cell((s.ringbuffer_size as usize + kRingBufferWriteAheadSlack as usize +
1857
17.2k
                   kBrotliMaxDictionaryWordLength as usize));
1858
17.2k
    if (s.ringbuffer.slice().len() == 0) {
1859
0
      return false;
1860
17.2k
    }
1861
17.2k
    fast_mut!((s.ringbuffer.slice_mut())[s.ringbuffer_size as usize - 1]) = 0;
1862
17.2k
    fast_mut!((s.ringbuffer.slice_mut())[s.ringbuffer_size as usize - 2]) = 0;
1863
17.2k
    if custom_dict.len() != 0 {
1864
0
      let offset = ((-s.custom_dict_size) & s.ringbuffer_mask as isize) as usize;
1865
0
      fast_mut!((s.ringbuffer.slice_mut())[offset ; offset + s.custom_dict_size as usize]).clone_from_slice(custom_dict);
1866
17.2k
    }
1867
  }
1868
17.2k
  if s.custom_dict.slice().len() != 0 {
1869
0
    s.alloc_u8.free_cell(core::mem::replace(&mut s.custom_dict,
1870
0
                         AllocU8::AllocatedMemory::default()));
1871
17.2k
  }
1872
17.2k
  true
1873
17.2k
}
Unexecuted instantiation: brotli_decompressor::decode::BrotliAllocateRingBuffer::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::BrotliAllocateRingBuffer::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1810
17.2k
fn BrotliAllocateRingBuffer<AllocU8: alloc::Allocator<u8>,
1811
17.2k
                            AllocU32: alloc::Allocator<u32>,
1812
17.2k
                            AllocHC: alloc::Allocator<HuffmanCode>>
1813
17.2k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1814
17.2k
   input: &[u8])
1815
17.2k
   -> bool {
1816
  // We need the slack region for the following reasons:
1817
  // - doing up to two 16-byte copies for fast backward copying
1818
  // - inserting transformed dictionary word (5 prefix + 24 base + 8 suffix)
1819
  const kRingBufferWriteAheadSlack: i32 = 42;
1820
17.2k
  let mut is_last = s.is_last_metablock;
1821
17.2k
  s.ringbuffer_size = 1 << s.window_bits;
1822
1823
17.2k
  if (s.is_uncompressed != 0) {
1824
3.06k
    let next_block_header =
1825
3.06k
      bit_reader::BrotliPeekByte(&mut s.br, s.meta_block_remaining_len as u32, input);
1826
3.06k
    if (next_block_header != -1) &&
1827
        // Peek succeeded
1828
2.76k
        ((next_block_header & 3) == 3) {
1829
2.59k
      // ISLAST and ISEMPTY
1830
2.59k
      is_last = 1;
1831
2.59k
    }
1832
14.1k
  }
1833
17.2k
  let max_dict_size = s.ringbuffer_size as usize - 16;
1834
  {
1835
17.2k
    let custom_dict = if s.custom_dict_size as usize > max_dict_size {
1836
0
      let cd = fast_slice!((s.custom_dict)[(s.custom_dict_size as usize - max_dict_size); s.custom_dict_size as usize]);
1837
0
      s.custom_dict_size = max_dict_size as isize;
1838
0
      cd
1839
    } else {
1840
17.2k
      fast_slice!((s.custom_dict)[0; s.custom_dict_size as usize])
1841
    };
1842
1843
    // We need at least 2 bytes of ring buffer size to get the last two
1844
    // bytes for context from there
1845
17.2k
    if (is_last != 0) {
1846
124k
      while (s.ringbuffer_size as isize >= (s.custom_dict_size + s.meta_block_remaining_len as isize + 16) * 2 && s.ringbuffer_size as isize > 32) {
1847
113k
        s.ringbuffer_size >>= 1;
1848
113k
      }
1849
5.39k
    }
1850
17.2k
    if s.ringbuffer_size > (1 << s.window_bits) {
1851
0
      s.ringbuffer_size = (1 << s.window_bits);
1852
17.2k
    }
1853
1854
17.2k
    s.ringbuffer_mask = s.ringbuffer_size - 1;
1855
17.2k
    s.ringbuffer = s.alloc_u8
1856
17.2k
      .alloc_cell((s.ringbuffer_size as usize + kRingBufferWriteAheadSlack as usize +
1857
17.2k
                   kBrotliMaxDictionaryWordLength as usize));
1858
17.2k
    if (s.ringbuffer.slice().len() == 0) {
1859
0
      return false;
1860
17.2k
    }
1861
17.2k
    fast_mut!((s.ringbuffer.slice_mut())[s.ringbuffer_size as usize - 1]) = 0;
1862
17.2k
    fast_mut!((s.ringbuffer.slice_mut())[s.ringbuffer_size as usize - 2]) = 0;
1863
17.2k
    if custom_dict.len() != 0 {
1864
0
      let offset = ((-s.custom_dict_size) & s.ringbuffer_mask as isize) as usize;
1865
0
      fast_mut!((s.ringbuffer.slice_mut())[offset ; offset + s.custom_dict_size as usize]).clone_from_slice(custom_dict);
1866
17.2k
    }
1867
  }
1868
17.2k
  if s.custom_dict.slice().len() != 0 {
1869
0
    s.alloc_u8.free_cell(core::mem::replace(&mut s.custom_dict,
1870
0
                         AllocU8::AllocatedMemory::default()));
1871
17.2k
  }
1872
17.2k
  true
1873
17.2k
}
1874
1875
// Reads 1..256 2-bit context modes.
1876
52.1k
pub fn ReadContextModes<AllocU8: alloc::Allocator<u8>,
1877
52.1k
                        AllocU32: alloc::Allocator<u32>,
1878
52.1k
                        AllocHC: alloc::Allocator<HuffmanCode>>
1879
52.1k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1880
52.1k
   input: &[u8])
1881
52.1k
   -> BrotliDecoderErrorCode {
1882
1883
52.1k
  let mut i: i32 = s.loop_counter;
1884
1885
116k
  for context_mode_iter in fast_mut!((s.context_modes.slice_mut())[i as usize ;
1886
52.1k
                                                       (s.block_type_length_state.num_block_types[0]
1887
52.1k
                                                        as usize)])
1888
52.1k
    .iter_mut() {
1889
116k
    let mut bits: u32 = 0;
1890
116k
    if (!bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input)) {
1891
1.01k
      mark_unlikely();
1892
1.01k
      s.loop_counter = i;
1893
1.01k
      return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1894
115k
    }
1895
115k
    *context_mode_iter = bits as u8;
1896
    BROTLI_LOG_UINT!(i);
1897
115k
    i += 1;
1898
  }
1899
51.1k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
1900
52.1k
}
Unexecuted instantiation: brotli_decompressor::decode::ReadContextModes::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ReadContextModes::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1876
52.1k
pub fn ReadContextModes<AllocU8: alloc::Allocator<u8>,
1877
52.1k
                        AllocU32: alloc::Allocator<u32>,
1878
52.1k
                        AllocHC: alloc::Allocator<HuffmanCode>>
1879
52.1k
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1880
52.1k
   input: &[u8])
1881
52.1k
   -> BrotliDecoderErrorCode {
1882
1883
52.1k
  let mut i: i32 = s.loop_counter;
1884
1885
116k
  for context_mode_iter in fast_mut!((s.context_modes.slice_mut())[i as usize ;
1886
52.1k
                                                       (s.block_type_length_state.num_block_types[0]
1887
52.1k
                                                        as usize)])
1888
52.1k
    .iter_mut() {
1889
116k
    let mut bits: u32 = 0;
1890
116k
    if (!bit_reader::BrotliSafeReadBits(&mut s.br, 2, &mut bits, input)) {
1891
1.01k
      mark_unlikely();
1892
1.01k
      s.loop_counter = i;
1893
1.01k
      return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
1894
115k
    }
1895
115k
    *context_mode_iter = bits as u8;
1896
    BROTLI_LOG_UINT!(i);
1897
115k
    i += 1;
1898
  }
1899
51.1k
  BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS
1900
52.1k
}
1901
1902
59.4M
pub fn TakeDistanceFromRingBuffer<AllocU8: alloc::Allocator<u8>,
1903
59.4M
                                  AllocU32: alloc::Allocator<u32>,
1904
59.4M
                                  AllocHC: alloc::Allocator<HuffmanCode>>
1905
59.4M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
1906
59.4M
  if (s.distance_code == 0) {
1907
18.3M
    s.dist_rb_idx -= 1;
1908
18.3M
    s.distance_code = fast!((s.dist_rb)[(s.dist_rb_idx & 3) as usize]);
1909
18.3M
    s.distance_context = 1;
1910
18.3M
  } else {
1911
41.0M
    let distance_code = s.distance_code << 1;
1912
    // kDistanceShortCodeIndexOffset has 2-bit values from LSB:
1913
    // 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2
1914
    const kDistanceShortCodeIndexOffset: u32 = 0xaaafff1b;
1915
    // kDistanceShortCodeValueOffset has 2-bit values from LSB:
1916
    // -0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3
1917
    const kDistanceShortCodeValueOffset: u32 = 0xfa5fa500;
1918
41.0M
    let mut v = (s.dist_rb_idx as i32 +
1919
41.0M
                 (kDistanceShortCodeIndexOffset as i32 >>
1920
41.0M
                  distance_code as i32)) as i32 & 0x3;
1921
41.0M
    s.distance_code = fast!((s.dist_rb)[v as usize]);
1922
41.0M
    v = (kDistanceShortCodeValueOffset >> distance_code) as i32 & 0x3;
1923
41.0M
    if ((distance_code & 0x3) != 0) {
1924
34.3M
      s.distance_code += v;
1925
34.3M
    } else {
1926
6.75M
      s.distance_code -= v;
1927
6.75M
      if (s.distance_code <= 0) {
1928
46
        // A huge distance will cause a BROTLI_FAILURE() soon.
1929
46
        // This is a little faster than failing here.
1930
46
        s.distance_code = 0x7fffffff;
1931
6.75M
      }
1932
    }
1933
  }
1934
59.4M
}
Unexecuted instantiation: brotli_decompressor::decode::TakeDistanceFromRingBuffer::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::TakeDistanceFromRingBuffer::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1902
59.4M
pub fn TakeDistanceFromRingBuffer<AllocU8: alloc::Allocator<u8>,
1903
59.4M
                                  AllocU32: alloc::Allocator<u32>,
1904
59.4M
                                  AllocHC: alloc::Allocator<HuffmanCode>>
1905
59.4M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>) {
1906
59.4M
  if (s.distance_code == 0) {
1907
18.3M
    s.dist_rb_idx -= 1;
1908
18.3M
    s.distance_code = fast!((s.dist_rb)[(s.dist_rb_idx & 3) as usize]);
1909
18.3M
    s.distance_context = 1;
1910
18.3M
  } else {
1911
41.0M
    let distance_code = s.distance_code << 1;
1912
    // kDistanceShortCodeIndexOffset has 2-bit values from LSB:
1913
    // 3, 2, 1, 0, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2
1914
    const kDistanceShortCodeIndexOffset: u32 = 0xaaafff1b;
1915
    // kDistanceShortCodeValueOffset has 2-bit values from LSB:
1916
    // -0, 0,-0, 0,-1, 1,-2, 2,-3, 3,-1, 1,-2, 2,-3, 3
1917
    const kDistanceShortCodeValueOffset: u32 = 0xfa5fa500;
1918
41.0M
    let mut v = (s.dist_rb_idx as i32 +
1919
41.0M
                 (kDistanceShortCodeIndexOffset as i32 >>
1920
41.0M
                  distance_code as i32)) as i32 & 0x3;
1921
41.0M
    s.distance_code = fast!((s.dist_rb)[v as usize]);
1922
41.0M
    v = (kDistanceShortCodeValueOffset >> distance_code) as i32 & 0x3;
1923
41.0M
    if ((distance_code & 0x3) != 0) {
1924
34.3M
      s.distance_code += v;
1925
34.3M
    } else {
1926
6.75M
      s.distance_code -= v;
1927
6.75M
      if (s.distance_code <= 0) {
1928
46
        // A huge distance will cause a BROTLI_FAILURE() soon.
1929
46
        // This is a little faster than failing here.
1930
46
        s.distance_code = 0x7fffffff;
1931
6.75M
      }
1932
    }
1933
  }
1934
59.4M
}
1935
1936
296M
pub fn SafeReadBits(br: &mut bit_reader::BrotliBitReader,
1937
296M
                    n_bits: u32,
1938
296M
                    val: &mut u32,
1939
296M
                    input: &[u8])
1940
296M
                    -> bool {
1941
296M
  if (n_bits != 0) {
1942
984k
    bit_reader::BrotliSafeReadBits(br, n_bits, val, input)
1943
  } else {
1944
295M
    *val = 0;
1945
295M
    true
1946
  }
1947
296M
}
1948
1949
// Precondition: s.distance_code < 0
1950
85.6M
pub fn ReadDistanceInternal<AllocU8: alloc::Allocator<u8>,
1951
85.6M
                            AllocU32: alloc::Allocator<u32>,
1952
85.6M
                            AllocHC: alloc::Allocator<HuffmanCode>>
1953
85.6M
  (safe: bool,
1954
85.6M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1955
85.6M
   input: &[u8],
1956
85.6M
   distance_hgroup: &[&[HuffmanCode]; 256])
1957
85.6M
   -> bool {
1958
  let mut distval: i32;
1959
85.6M
  let mut memento = bit_reader::BrotliBitReaderState::default();
1960
85.6M
  if (!safe) {
1961
34.7M
    s.distance_code = ReadSymbol(fast!((distance_hgroup)[s.dist_htree_index as usize]),
1962
34.7M
                                 &mut s.br,
1963
34.7M
                                 input) as i32;
1964
34.7M
  } else {
1965
50.8M
    let mut code: u32 = 0;
1966
50.8M
    memento = bit_reader::BrotliBitReaderSaveState(&s.br);
1967
50.8M
    if !SafeReadSymbol(fast!((distance_hgroup)[s.dist_htree_index as usize]),
1968
50.8M
                       &mut s.br,
1969
50.8M
                       &mut code,
1970
50.8M
                       input) {
1971
221k
      return false;
1972
50.6M
    }
1973
50.6M
    s.distance_code = code as i32;
1974
  }
1975
  // Convert the distance code to the actual distance by possibly
1976
  // looking up past distances from the s.ringbuffer.
1977
85.4M
  s.distance_context = 0;
1978
85.4M
  if ((s.distance_code as u64 & 0xfffffffffffffff0) == 0) {
1979
59.4M
    TakeDistanceFromRingBuffer(s);
1980
59.4M
    fast_mut!((s.block_type_length_state.block_length)[2]) -= 1;
1981
59.4M
    return true;
1982
25.9M
  }
1983
25.9M
  distval = s.distance_code - s.num_direct_distance_codes as i32;
1984
25.9M
  if (distval >= 0) {
1985
    let nbits: u32;
1986
    let postfix: i32;
1987
    let offset: i32;
1988
6.59M
    if (!safe && (s.distance_postfix_bits == 0)) {
1989
3.57M
      nbits = (distval as u32 >> 1) + 1;
1990
3.57M
      offset = ((2 + (distval & 1)) << nbits) - 4;
1991
3.57M
      s.distance_code = (s.num_direct_distance_codes as i64 + offset as i64 +
1992
3.57M
                        bit_reader::BrotliReadBits(&mut s.br, nbits, input) as i64) as i32;
1993
3.57M
    } else {
1994
      // This branch also works well when s.distance_postfix_bits == 0
1995
3.01M
      let mut bits: u32 = 0;
1996
3.01M
      postfix = distval & s.distance_postfix_mask;
1997
3.01M
      distval >>= s.distance_postfix_bits;
1998
3.01M
      nbits = (distval as u32 >> 1) + 1;
1999
3.01M
      if (safe) {
2000
580k
        if (!SafeReadBits(&mut s.br, nbits, &mut bits, input)) {
2001
149k
          s.distance_code = -1; /* Restore precondition. */
2002
149k
          bit_reader::BrotliBitReaderRestoreState(&mut s.br, &memento);
2003
149k
          return false;
2004
431k
        }
2005
2.43M
      } else {
2006
2.43M
        bits = bit_reader::BrotliReadBits(&mut s.br, nbits, input);
2007
2.43M
      }
2008
2.86M
      offset = (((distval & 1).wrapping_add(2)) << nbits).wrapping_sub(4);
2009
2.86M
      s.distance_code = ((i64::from(offset) + i64::from(bits)) << s.distance_postfix_bits).wrapping_add(i64::from(postfix)).wrapping_add(i64::from(s.num_direct_distance_codes)) as i32;
2010
    }
2011
19.3M
  }
2012
25.8M
  s.distance_code = s.distance_code.wrapping_sub(NUM_DISTANCE_SHORT_CODES as i32).wrapping_add(1);
2013
25.8M
  fast_mut!((s.block_type_length_state.block_length)[2]) -= 1;
2014
25.8M
  true
2015
85.6M
}
Unexecuted instantiation: brotli_decompressor::decode::ReadDistanceInternal::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ReadDistanceInternal::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
1950
85.6M
pub fn ReadDistanceInternal<AllocU8: alloc::Allocator<u8>,
1951
85.6M
                            AllocU32: alloc::Allocator<u32>,
1952
85.6M
                            AllocHC: alloc::Allocator<HuffmanCode>>
1953
85.6M
  (safe: bool,
1954
85.6M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
1955
85.6M
   input: &[u8],
1956
85.6M
   distance_hgroup: &[&[HuffmanCode]; 256])
1957
85.6M
   -> bool {
1958
  let mut distval: i32;
1959
85.6M
  let mut memento = bit_reader::BrotliBitReaderState::default();
1960
85.6M
  if (!safe) {
1961
34.7M
    s.distance_code = ReadSymbol(fast!((distance_hgroup)[s.dist_htree_index as usize]),
1962
34.7M
                                 &mut s.br,
1963
34.7M
                                 input) as i32;
1964
34.7M
  } else {
1965
50.8M
    let mut code: u32 = 0;
1966
50.8M
    memento = bit_reader::BrotliBitReaderSaveState(&s.br);
1967
50.8M
    if !SafeReadSymbol(fast!((distance_hgroup)[s.dist_htree_index as usize]),
1968
50.8M
                       &mut s.br,
1969
50.8M
                       &mut code,
1970
50.8M
                       input) {
1971
221k
      return false;
1972
50.6M
    }
1973
50.6M
    s.distance_code = code as i32;
1974
  }
1975
  // Convert the distance code to the actual distance by possibly
1976
  // looking up past distances from the s.ringbuffer.
1977
85.4M
  s.distance_context = 0;
1978
85.4M
  if ((s.distance_code as u64 & 0xfffffffffffffff0) == 0) {
1979
59.4M
    TakeDistanceFromRingBuffer(s);
1980
59.4M
    fast_mut!((s.block_type_length_state.block_length)[2]) -= 1;
1981
59.4M
    return true;
1982
25.9M
  }
1983
25.9M
  distval = s.distance_code - s.num_direct_distance_codes as i32;
1984
25.9M
  if (distval >= 0) {
1985
    let nbits: u32;
1986
    let postfix: i32;
1987
    let offset: i32;
1988
6.59M
    if (!safe && (s.distance_postfix_bits == 0)) {
1989
3.57M
      nbits = (distval as u32 >> 1) + 1;
1990
3.57M
      offset = ((2 + (distval & 1)) << nbits) - 4;
1991
3.57M
      s.distance_code = (s.num_direct_distance_codes as i64 + offset as i64 +
1992
3.57M
                        bit_reader::BrotliReadBits(&mut s.br, nbits, input) as i64) as i32;
1993
3.57M
    } else {
1994
      // This branch also works well when s.distance_postfix_bits == 0
1995
3.01M
      let mut bits: u32 = 0;
1996
3.01M
      postfix = distval & s.distance_postfix_mask;
1997
3.01M
      distval >>= s.distance_postfix_bits;
1998
3.01M
      nbits = (distval as u32 >> 1) + 1;
1999
3.01M
      if (safe) {
2000
580k
        if (!SafeReadBits(&mut s.br, nbits, &mut bits, input)) {
2001
149k
          s.distance_code = -1; /* Restore precondition. */
2002
149k
          bit_reader::BrotliBitReaderRestoreState(&mut s.br, &memento);
2003
149k
          return false;
2004
431k
        }
2005
2.43M
      } else {
2006
2.43M
        bits = bit_reader::BrotliReadBits(&mut s.br, nbits, input);
2007
2.43M
      }
2008
2.86M
      offset = (((distval & 1).wrapping_add(2)) << nbits).wrapping_sub(4);
2009
2.86M
      s.distance_code = ((i64::from(offset) + i64::from(bits)) << s.distance_postfix_bits).wrapping_add(i64::from(postfix)).wrapping_add(i64::from(s.num_direct_distance_codes)) as i32;
2010
    }
2011
19.3M
  }
2012
25.8M
  s.distance_code = s.distance_code.wrapping_sub(NUM_DISTANCE_SHORT_CODES as i32).wrapping_add(1);
2013
25.8M
  fast_mut!((s.block_type_length_state.block_length)[2]) -= 1;
2014
25.8M
  true
2015
85.6M
}
2016
2017
2018
236M
pub fn ReadCommandInternal<AllocU8: alloc::Allocator<u8>,
2019
236M
                           AllocU32: alloc::Allocator<u32>,
2020
236M
                           AllocHC: alloc::Allocator<HuffmanCode>>
2021
236M
  (safe: bool,
2022
236M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2023
236M
   insert_length: &mut i32,
2024
236M
   input: &[u8],
2025
236M
   insert_copy_hgroup: &[&[HuffmanCode]; 256])
2026
236M
   -> bool {
2027
236M
  let mut cmd_code: u32 = 0;
2028
236M
  let mut insert_len_extra: u32 = 0;
2029
236M
  let mut copy_length: u32 = 0;
2030
  let v: prefix::CmdLutElement;
2031
236M
  let mut memento = bit_reader::BrotliBitReaderState::default();
2032
236M
  if (!safe) {
2033
87.9M
    cmd_code = ReadSymbol(fast!((insert_copy_hgroup)[s.htree_command_index as usize]),
2034
87.9M
                          &mut s.br,
2035
87.9M
                          input);
2036
87.9M
  } else {
2037
148M
    memento = bit_reader::BrotliBitReaderSaveState(&s.br);
2038
148M
    if (!SafeReadSymbol(fast!((insert_copy_hgroup)[s.htree_command_index as usize]),
2039
148M
                        &mut s.br,
2040
148M
                        &mut cmd_code,
2041
148M
                        input)) {
2042
465k
      return false;
2043
147M
    }
2044
  }
2045
235M
  v = fast!((prefix::kCmdLut)[cmd_code as usize]);
2046
235M
  s.distance_code = v.distance_code as i32;
2047
235M
  s.distance_context = v.context as i32;
2048
235M
  s.dist_htree_index = fast_slice!((s.dist_context_map)[s.dist_context_map_slice_index
2049
235M
                                                  + s.distance_context as usize]);
2050
235M
  *insert_length = v.insert_len_offset as i32;
2051
235M
  if (!safe) {
2052
87.9M
    if v.insert_len_extra_bits != 0 {
2053
5.87M
      mark_unlikely();
2054
5.87M
      insert_len_extra =
2055
5.87M
        bit_reader::BrotliReadBits(&mut s.br, v.insert_len_extra_bits as u32, input);
2056
82.0M
    }
2057
87.9M
    copy_length = bit_reader::BrotliReadBits(&mut s.br, v.copy_len_extra_bits as u32, input);
2058
147M
  } else if (!SafeReadBits(&mut s.br,
2059
147M
                    v.insert_len_extra_bits as u32,
2060
147M
                    &mut insert_len_extra,
2061
147M
                    input)) ||
2062
147M
     (!SafeReadBits(&mut s.br,
2063
147M
                    v.copy_len_extra_bits as u32,
2064
147M
                    &mut copy_length,
2065
147M
                    input)) {
2066
83.2k
    bit_reader::BrotliBitReaderRestoreState(&mut s.br, &memento);
2067
83.2k
    return false;
2068
147M
  }
2069
235M
  s.copy_length = copy_length as i32 + v.copy_len_offset as i32;
2070
235M
  fast_mut!((s.block_type_length_state.block_length)[1]) -= 1;
2071
235M
  *insert_length += insert_len_extra as i32;
2072
235M
  true
2073
236M
}
Unexecuted instantiation: brotli_decompressor::decode::ReadCommandInternal::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ReadCommandInternal::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
2018
236M
pub fn ReadCommandInternal<AllocU8: alloc::Allocator<u8>,
2019
236M
                           AllocU32: alloc::Allocator<u32>,
2020
236M
                           AllocHC: alloc::Allocator<HuffmanCode>>
2021
236M
  (safe: bool,
2022
236M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2023
236M
   insert_length: &mut i32,
2024
236M
   input: &[u8],
2025
236M
   insert_copy_hgroup: &[&[HuffmanCode]; 256])
2026
236M
   -> bool {
2027
236M
  let mut cmd_code: u32 = 0;
2028
236M
  let mut insert_len_extra: u32 = 0;
2029
236M
  let mut copy_length: u32 = 0;
2030
  let v: prefix::CmdLutElement;
2031
236M
  let mut memento = bit_reader::BrotliBitReaderState::default();
2032
236M
  if (!safe) {
2033
87.9M
    cmd_code = ReadSymbol(fast!((insert_copy_hgroup)[s.htree_command_index as usize]),
2034
87.9M
                          &mut s.br,
2035
87.9M
                          input);
2036
87.9M
  } else {
2037
148M
    memento = bit_reader::BrotliBitReaderSaveState(&s.br);
2038
148M
    if (!SafeReadSymbol(fast!((insert_copy_hgroup)[s.htree_command_index as usize]),
2039
148M
                        &mut s.br,
2040
148M
                        &mut cmd_code,
2041
148M
                        input)) {
2042
465k
      return false;
2043
147M
    }
2044
  }
2045
235M
  v = fast!((prefix::kCmdLut)[cmd_code as usize]);
2046
235M
  s.distance_code = v.distance_code as i32;
2047
235M
  s.distance_context = v.context as i32;
2048
235M
  s.dist_htree_index = fast_slice!((s.dist_context_map)[s.dist_context_map_slice_index
2049
235M
                                                  + s.distance_context as usize]);
2050
235M
  *insert_length = v.insert_len_offset as i32;
2051
235M
  if (!safe) {
2052
87.9M
    if v.insert_len_extra_bits != 0 {
2053
5.87M
      mark_unlikely();
2054
5.87M
      insert_len_extra =
2055
5.87M
        bit_reader::BrotliReadBits(&mut s.br, v.insert_len_extra_bits as u32, input);
2056
82.0M
    }
2057
87.9M
    copy_length = bit_reader::BrotliReadBits(&mut s.br, v.copy_len_extra_bits as u32, input);
2058
147M
  } else if (!SafeReadBits(&mut s.br,
2059
147M
                    v.insert_len_extra_bits as u32,
2060
147M
                    &mut insert_len_extra,
2061
147M
                    input)) ||
2062
147M
     (!SafeReadBits(&mut s.br,
2063
147M
                    v.copy_len_extra_bits as u32,
2064
147M
                    &mut copy_length,
2065
147M
                    input)) {
2066
83.2k
    bit_reader::BrotliBitReaderRestoreState(&mut s.br, &memento);
2067
83.2k
    return false;
2068
147M
  }
2069
235M
  s.copy_length = copy_length as i32 + v.copy_len_offset as i32;
2070
235M
  fast_mut!((s.block_type_length_state.block_length)[1]) -= 1;
2071
235M
  *insert_length += insert_len_extra as i32;
2072
235M
  true
2073
236M
}
2074
2075
2076
3.98M
fn WarmupBitReader(safe: bool, br: &mut bit_reader::BrotliBitReader, input: &[u8]) -> bool {
2077
3.98M
  safe || bit_reader::BrotliWarmupBitReader(br, input)
2078
3.98M
}
2079
2080
2.35G
fn CheckInputAmount(safe: bool, br: &bit_reader::BrotliBitReader, num: u32) -> bool {
2081
2.35G
  safe || bit_reader::BrotliCheckInputAmount(br, num)
2082
2.35G
}
2083
2084
#[inline(always)]
2085
215M
fn memmove16(data: &mut [u8], u32off_dst: u32, u32off_src: u32) {
2086
215M
  let off_dst = u32off_dst as usize;
2087
215M
  let off_src = u32off_src as usize;
2088
  // data[off_dst + 15] = data[off_src + 15];
2089
  // data[off_dst + 14] = data[off_src + 14];
2090
  // data[off_dst + 13] = data[off_src + 13];
2091
  // data[off_dst + 12] = data[off_src + 12];
2092
  //
2093
  // data[off_dst + 11] = data[off_src + 11];
2094
  // data[off_dst + 10] = data[off_src + 10];
2095
  // data[off_dst + 9] = data[off_src + 9];
2096
  // data[off_dst + 8] = data[off_src + 8];
2097
  //
2098
  // data[off_dst + 7] = data[off_src + 7];
2099
  // data[off_dst + 6] = data[off_src + 6];
2100
  // data[off_dst + 5] = data[off_src + 5];
2101
  // data[off_dst + 4] = data[off_src + 4];
2102
  //
2103
  // data[off_dst + 3] = data[off_src + 3];
2104
  // data[off_dst + 2] = data[off_src + 2];
2105
  // data[off_dst + 1] = data[off_src + 1];
2106
  //
2107
215M
  let mut local_array: [u8; 16] = fast_uninitialized!(16);
2108
215M
  local_array.clone_from_slice(fast!((data)[off_src as usize ; off_src as usize + 16]));
2109
215M
  fast_mut!((data)[off_dst as usize ; off_dst as usize + 16]).clone_from_slice(&local_array);
2110
2111
2112
215M
}
2113
2114
2115
#[cfg(not(feature="unsafe"))]
2116
524k
fn memcpy_within_slice(data: &mut [u8], off_dst: usize, off_src: usize, size: usize) {
2117
524k
  if off_dst > off_src {
2118
458k
    let (src, dst) = data.split_at_mut(off_dst);
2119
458k
    let src_slice = fast!((src)[off_src ; off_src + size]);
2120
458k
    fast_mut!((dst)[0;size]).clone_from_slice(src_slice);
2121
458k
  } else {
2122
66.3k
    let (dst, src) = data.split_at_mut(off_src);
2123
66.3k
    let src_slice = fast!((src)[0;size]);
2124
66.3k
    fast_mut!((dst)[off_dst;off_dst + size]).clone_from_slice(src_slice);
2125
66.3k
  }
2126
524k
}
2127
2128
#[cfg(feature="unsafe")]
2129
fn memcpy_within_slice(data: &mut [u8], off_dst: usize, off_src: usize, size: usize) {
2130
  let ptr = data.as_mut_ptr();
2131
  unsafe {
2132
    let dst = ptr.offset(off_dst as isize);
2133
    let src = ptr.offset(off_src as isize);
2134
    core::ptr::copy_nonoverlapping(src, dst, size);
2135
  }
2136
}
2137
2138
0
pub fn BrotliDecoderHasMoreOutput<AllocU8: alloc::Allocator<u8>,
2139
0
                           AllocU32: alloc::Allocator<u32>,
2140
0
                           AllocHC: alloc::Allocator<HuffmanCode>>
2141
0
  (s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> bool {
2142
  /* After unrecoverable error remaining output is considered nonsensical. */
2143
0
  if is_fatal(s.error_code) {
2144
0
    return false;
2145
0
  }
2146
0
  s.ringbuffer.len() != 0 && UnwrittenBytes(s, false) != 0
2147
0
}
2148
0
pub fn BrotliDecoderTakeOutput<'a,
2149
0
                               AllocU8: alloc::Allocator<u8>,
2150
0
                               AllocU32: alloc::Allocator<u32>,
2151
0
                               AllocHC: alloc::Allocator<HuffmanCode>>(
2152
0
  s: &'a mut BrotliState<AllocU8, AllocU32, AllocHC>,
2153
0
  size: &mut usize,
2154
0
) -> &'a [u8] {
2155
0
  let one:usize = 1;
2156
0
  let mut available_out = if *size != 0 { *size } else { one << 24 };
2157
0
  let requested_out = available_out;
2158
0
  if (s.ringbuffer.len() == 0) || is_fatal(s.error_code) {
2159
0
    *size = 0;
2160
0
    return &[];
2161
0
  }
2162
0
  WrapRingBuffer(s);
2163
0
  let mut ign = 0usize;
2164
0
  let mut ign2 = 0usize;
2165
0
  let (status, result) = WriteRingBuffer(&mut available_out, None, &mut ign,&mut ign2, true, s);
2166
  // Either WriteRingBuffer returns those "success" codes...
2167
0
  match status {
2168
0
    BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS |  BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT => {
2169
0
      *size = requested_out - available_out;
2170
0
    },
2171
    _ => {
2172
      // ... or stream is broken. Normally this should be caught by
2173
      //   BrotliDecoderDecompressStream, this is just a safeguard.
2174
0
      if is_fatal(status) {
2175
0
        // SaveErrorCode!(s, status); //borrow checker doesn't like this
2176
0
        // but since it's a safeguard--ignore
2177
0
      }
2178
0
      *size = 0;
2179
0
      return &[];
2180
    }
2181
  }
2182
0
  return result;
2183
0
}
2184
2185
#[cfg(feature="ffi-api")]
2186
pub fn BrotliDecoderIsUsed<AllocU8: alloc::Allocator<u8>,
2187
                           AllocU32: alloc::Allocator<u32>,
2188
                           AllocHC: alloc::Allocator<HuffmanCode>>(
2189
  s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> bool {
2190
  if let BrotliRunningState::BROTLI_STATE_UNINITED = s.state {
2191
    false
2192
  } else {
2193
    bit_reader::BrotliGetAvailableBits(&s.br) != 0
2194
  }
2195
}
2196
2197
0
pub fn BrotliDecoderIsFinished<AllocU8: alloc::Allocator<u8>,
2198
0
                               AllocU32: alloc::Allocator<u32>,
2199
0
                               AllocHC: alloc::Allocator<HuffmanCode>>(
2200
0
  s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> bool {
2201
0
  if let BrotliRunningState::BROTLI_STATE_DONE = s.state {
2202
0
    !BrotliDecoderHasMoreOutput(s)
2203
  } else {
2204
0
    false
2205
  }
2206
0
}
2207
2208
0
pub fn BrotliDecoderGetErrorCode<AllocU8: alloc::Allocator<u8>,
2209
0
                               AllocU32: alloc::Allocator<u32>,
2210
0
                               AllocHC: alloc::Allocator<HuffmanCode>>(
2211
0
  s: &BrotliState<AllocU8, AllocU32, AllocHC>) -> BrotliDecoderErrorCode {
2212
0
  s.error_code
2213
0
}
Unexecuted instantiation: brotli_decompressor::decode::BrotliDecoderGetErrorCode::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
Unexecuted instantiation: brotli_decompressor::decode::BrotliDecoderGetErrorCode::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
2214
2215
7.37M
fn ProcessCommandsInternal<AllocU8: alloc::Allocator<u8>,
2216
7.37M
                           AllocU32: alloc::Allocator<u32>,
2217
7.37M
                           AllocHC: alloc::Allocator<HuffmanCode>>
2218
7.37M
  (safe: bool,
2219
7.37M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2220
7.37M
   input: &[u8])
2221
7.37M
   -> BrotliDecoderErrorCode {
2222
7.37M
  if (!CheckInputAmount(safe, &s.br, 28)) || (!WarmupBitReader(safe, &mut s.br, input)) {
2223
3.38M
    mark_unlikely();
2224
3.38M
    return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2225
3.98M
  }
2226
3.98M
  let mut pos = s.pos;
2227
3.98M
  let mut i: i32 = s.loop_counter; // important that this is signed
2228
3.98M
  let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2229
3.98M
  let mut saved_literal_hgroup =
2230
3.98M
    core::mem::replace(&mut s.literal_hgroup,
2231
3.98M
                       HuffmanTreeGroup::<AllocU32, AllocHC>::default());
2232
3.98M
  let mut saved_distance_hgroup =
2233
3.98M
    core::mem::replace(&mut s.distance_hgroup,
2234
3.98M
                       HuffmanTreeGroup::<AllocU32, AllocHC>::default());
2235
3.98M
  let mut saved_insert_copy_hgroup =
2236
3.98M
    core::mem::replace(&mut s.insert_copy_hgroup,
2237
3.98M
                       HuffmanTreeGroup::<AllocU32, AllocHC>::default());
2238
  {
2239
2240
3.98M
    let literal_hgroup = saved_literal_hgroup.build_hgroup_cache();
2241
3.98M
    let distance_hgroup = saved_distance_hgroup.build_hgroup_cache();
2242
3.98M
    let insert_copy_hgroup = saved_insert_copy_hgroup.build_hgroup_cache();
2243
2244
    loop {
2245
722M
      match s.state {
2246
        BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN => {
2247
236M
          if (!CheckInputAmount(safe, &s.br, 28)) {
2248
            // 156 bits + 7 bytes
2249
15.6k
            mark_unlikely();
2250
15.6k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2251
15.6k
            break; // return
2252
236M
          }
2253
236M
          if (fast_mut!((s.block_type_length_state.block_length)[1]) == 0) {
2254
119k
            mark_unlikely();
2255
119k
            if !DecodeCommandBlockSwitchInternal(safe, s, input) {
2256
1.29k
              result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2257
1.29k
              break; // return
2258
117k
            }
2259
117k
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
2260
117k
            continue; // goto CommandBegin;
2261
236M
          }
2262
          // Read the insert/copy length in the command
2263
236M
          if (!ReadCommandInternal(safe, s, &mut i, input, &insert_copy_hgroup)) && safe {
2264
548k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2265
548k
            break; // return
2266
235M
          }
2267
          BROTLI_LOG!("[ProcessCommandsInternal] pos = %d insert = %d copy = %d distance = %d\n",
2268
              pos, i, s.copy_length, s.distance_code);
2269
235M
          if (i == 0) {
2270
86.3M
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
2271
86.3M
            continue; // goto CommandPostDecodeLiterals;
2272
149M
          }
2273
149M
          s.meta_block_remaining_len -= i;
2274
149M
          s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2275
        }
2276
        BrotliRunningState::BROTLI_STATE_COMMAND_INNER => {
2277
          // Read the literals in the command
2278
152M
          if (s.trivial_literal_context != 0) {
2279
101M
            let mut bits: u32 = 0;
2280
101M
            let mut value: u32 = 0;
2281
101M
            let mut literal_htree = &fast!((literal_hgroup)[s.literal_htree_index as usize]);
2282
101M
            PreloadSymbol(safe, literal_htree, &mut s.br, &mut bits, &mut value, input);
2283
101M
            let mut inner_return: bool = false;
2284
101M
            let mut inner_continue: bool = false;
2285
            loop {
2286
1.74G
              if (!CheckInputAmount(safe, &s.br, 28)) {
2287
                // 162 bits + 7 bytes
2288
28.6k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2289
28.6k
                inner_return = true;
2290
28.6k
                break;
2291
1.74G
              }
2292
1.74G
              if (fast!((s.block_type_length_state.block_length)[0]) == 0) {
2293
1.26M
                mark_unlikely();
2294
1.26M
                if (!DecodeLiteralBlockSwitchInternal(safe, s, input)) && safe {
2295
468
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2296
468
                  inner_return = true;
2297
468
                  break;
2298
1.26M
                }
2299
1.26M
                literal_htree = fast_ref!((literal_hgroup)[s.literal_htree_index as usize]);
2300
1.26M
                PreloadSymbol(safe, literal_htree, &mut s.br, &mut bits, &mut value, input);
2301
1.26M
                if (s.trivial_literal_context == 0) {
2302
4.55k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2303
4.55k
                  inner_continue = true;
2304
4.55k
                  break; // goto StateCommandInner
2305
1.26M
                }
2306
1.74G
              }
2307
1.74G
              if (!safe) {
2308
438M
                fast_mut!((s.ringbuffer.slice_mut())[pos as usize]) =
2309
438M
                  ReadPreloadedSymbol(literal_htree, &mut s.br, &mut bits, &mut value, input) as u8;
2310
438M
              } else {
2311
1.30G
                let mut literal: u32 = 0;
2312
1.30G
                if (!SafeReadSymbol(literal_htree, &mut s.br, &mut literal, input)) {
2313
1.35M
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2314
1.35M
                  inner_return = true;
2315
1.35M
                  break;
2316
1.30G
                }
2317
1.30G
                fast_mut!((s.ringbuffer.slice_mut())[pos as usize]) = literal as u8;
2318
              }
2319
1.74G
              if (s.block_type_length_state.block_length)[0] == 0 {
2320
0
                  mark_unlikely();
2321
0
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
2322
0
                  inner_return = true;
2323
0
                  break;
2324
1.74G
              }
2325
1.74G
              fast_mut!((s.block_type_length_state.block_length)[0]) -= 1;
2326
              BROTLI_LOG_UINT!(s.literal_htree_index);
2327
              BROTLI_LOG_ARRAY_INDEX!(s.ringbuffer.slice(), pos);
2328
1.74G
              pos += 1;
2329
1.74G
              if (pos == s.ringbuffer_size) {
2330
154k
                mark_unlikely();
2331
154k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE;
2332
154k
                i -= 1;
2333
154k
                inner_return = true;
2334
154k
                break;
2335
1.74G
              }
2336
1.74G
              i -= 1;
2337
1.74G
              if i == 0 {
2338
99.5M
                break;
2339
1.64G
              }
2340
            }
2341
101M
            if inner_return {
2342
1.54M
              break; // return
2343
99.5M
            }
2344
99.5M
            if inner_continue {
2345
4.55k
              mark_unlikely();
2346
4.55k
              continue;
2347
99.5M
            }
2348
          } else {
2349
51.0M
            let mut p1 = fast_slice!((s.ringbuffer)[((pos - 1) & s.ringbuffer_mask) as usize]);
2350
51.0M
            let mut p2 = fast_slice!((s.ringbuffer)[((pos - 2) & s.ringbuffer_mask) as usize]);
2351
51.0M
            if s.custom_dict_avoid_context_seed && pos < 2 {
2352
0
                mark_unlikely();
2353
0
                p2 = 0;
2354
0
                p1 = 0;
2355
51.0M
            }
2356
51.0M
            if pos > 1
2357
51.0M
            {
2358
51.0M
                // have already set both seed bytes and can now move on to using
2359
51.0M
                // the ringbuffer.
2360
51.0M
                s.custom_dict_avoid_context_seed = false;
2361
51.0M
            }
2362
51.0M
            let mut inner_return: bool = false;
2363
51.0M
            let mut inner_continue: bool = false;
2364
            loop {
2365
369M
              if (!CheckInputAmount(safe, &s.br, 28)) {
2366
                // 162 bits + 7 bytes
2367
17.4k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2368
17.4k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2369
17.4k
                inner_return = true;
2370
17.4k
                break;
2371
369M
              }
2372
369M
              if (fast!((s.block_type_length_state.block_length)[0]) == 0) {
2373
1.81M
                mark_unlikely();
2374
1.81M
                if (!DecodeLiteralBlockSwitchInternal(safe, s, input)) && safe {
2375
7.04k
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2376
7.04k
                  inner_return = true;
2377
7.04k
                  break;
2378
1.80M
                }
2379
1.80M
                if s.trivial_literal_context != 0 {
2380
4.54k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2381
4.54k
                  inner_continue = true;
2382
4.54k
                  break;
2383
1.80M
                }
2384
367M
              }
2385
369M
              let context = s.context_lookup[p1 as usize] | s.context_lookup[p2 as usize |256];
2386
              BROTLI_LOG_UINT!(p1);
2387
              BROTLI_LOG_UINT!(p2);
2388
              BROTLI_LOG_UINT!(context);
2389
              let hc: &[HuffmanCode];
2390
369M
              {
2391
369M
                let i = fast_slice!((s.context_map)[s.context_map_slice_index + context as usize]);
2392
369M
                hc = fast!((literal_hgroup)[i as usize]);
2393
369M
              }
2394
369M
              p2 = p1;
2395
369M
              if (!safe) {
2396
256M
                p1 = ReadSymbol(hc, &mut s.br, input) as u8;
2397
256M
              } else {
2398
113M
                let mut literal: u32 = 0;
2399
113M
                if (!SafeReadSymbol(hc, &mut s.br, &mut literal, input)) {
2400
1.05M
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2401
1.05M
                  inner_return = true;
2402
1.05M
                  break;
2403
111M
                }
2404
111M
                p1 = literal as u8;
2405
              }
2406
368M
              fast_slice_mut!((s.ringbuffer)[pos as usize]) = p1;
2407
368M
              if (s.block_type_length_state.block_length)[0] == 0 {
2408
0
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
2409
0
                  inner_return = true;
2410
0
                  break;
2411
368M
              }
2412
368M
              fast_mut!((s.block_type_length_state.block_length)[0]) -= 1;
2413
              BROTLI_LOG_UINT!(s.context_map.slice()[s.context_map_slice_index as usize +
2414
                                                     context as usize]);
2415
              BROTLI_LOG_ARRAY_INDEX!(s.ringbuffer.slice(), pos & s.ringbuffer_mask);
2416
368M
              pos += 1;
2417
368M
              if (pos == s.ringbuffer_size) {
2418
26.6k
                mark_unlikely();
2419
26.6k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE;
2420
26.6k
                i -= 1;
2421
26.6k
                inner_return = true;
2422
26.6k
                break;
2423
368M
              }
2424
368M
              i -= 1;
2425
368M
              if i == 0 {
2426
49.9M
                break;
2427
318M
              }
2428
            }
2429
51.0M
            if inner_return {
2430
1.11M
              break; // return
2431
49.9M
            }
2432
49.9M
            if inner_continue {
2433
4.54k
              mark_unlikely();
2434
4.54k
              continue;
2435
49.9M
            }
2436
          }
2437
149M
          if (s.meta_block_remaining_len <= 0) {
2438
19.9k
            mark_unlikely();
2439
19.9k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2440
19.9k
            break; // return
2441
149M
          }
2442
149M
          s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
2443
        }
2444
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS => {
2445
236M
          if s.distance_code >= 0 {
2446
150M
            let not_distance_code = if s.distance_code != 0 { 0 } else { 1 };
2447
150M
            s.distance_context = not_distance_code;
2448
150M
            s.dist_rb_idx -= 1;
2449
150M
            s.distance_code = fast!((s.dist_rb)[(s.dist_rb_idx & 3) as usize]);
2450
            // goto postReadDistance
2451
          } else {
2452
85.6M
            if fast!((s.block_type_length_state.block_length)[2]) == 0 {
2453
749k
              mark_unlikely();
2454
749k
              if (!DecodeDistanceBlockSwitchInternal(safe, s, input)) && safe {
2455
611
                result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2456
611
                break; // return
2457
748k
              }
2458
84.9M
            }
2459
85.6M
            if (!ReadDistanceInternal(safe, s, input, &distance_hgroup)) && safe {
2460
371k
              result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2461
371k
              break; // return
2462
85.3M
            }
2463
          }
2464
          // postReadDistance:
2465
          BROTLI_LOG!("[ProcessCommandsInternal] pos = %d distance = %d\n",
2466
                      pos, s.distance_code);
2467
2468
235M
          if (s.max_distance != s.max_backward_distance) {
2469
78.2M
            if (pos < s.max_backward_distance_minus_custom_dict_size) {
2470
78.2M
              s.max_distance = pos + s.custom_dict_size as i32;
2471
78.2M
            } else {
2472
1.20k
              s.max_distance = s.max_backward_distance;
2473
1.20k
            }
2474
157M
          }
2475
235M
          i = s.copy_length;
2476
          // Apply copy of LZ77 back-reference, or static dictionary reference if
2477
          // the distance is larger than the max LZ77 distance
2478
235M
          if (s.distance_code > s.max_distance) {
2479
21.1M
            if s.distance_code > kBrotliMaxAllowedDistance as i32 {
2480
46
              return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE;
2481
21.1M
            }
2482
21.1M
            if (i >= kBrotliMinDictionaryWordLength as i32 &&
2483
21.1M
                i <= kBrotliMaxDictionaryWordLength as i32) {
2484
21.1M
              let mut offset = fast!((kBrotliDictionaryOffsetsByLength)[i as usize]) as i32;
2485
21.1M
              let word_id = s.distance_code - s.max_distance - 1;
2486
21.1M
              let shift = fast!((kBrotliDictionarySizeBitsByLength)[i as usize]);
2487
21.1M
              let mask = bit_reader::BitMask(shift as u32) as i32;
2488
21.1M
              let word_idx = word_id & mask;
2489
21.1M
              let transform_idx = word_id >> shift;
2490
21.1M
              s.dist_rb_idx += s.distance_context;
2491
21.1M
              offset += word_idx * i;
2492
21.1M
              if (transform_idx < kNumTransforms) {
2493
21.1M
                let mut len = i;
2494
21.1M
                let word = fast!((kBrotliDictionary)[offset as usize ; (offset + len) as usize]);
2495
21.1M
                if (transform_idx == 0) {
2496
20.8M
                  fast_slice_mut!((s.ringbuffer)[pos as usize ; ((pos + len) as usize)])
2497
20.8M
                    .clone_from_slice(word);
2498
20.8M
                } else {
2499
284k
                  len = TransformDictionaryWord(fast_slice_mut!((s.ringbuffer)[pos as usize;]),
2500
284k
                                                word,
2501
284k
                                                len,
2502
284k
                                                transform_idx);
2503
284k
                }
2504
21.1M
                pos += len;
2505
21.1M
                s.meta_block_remaining_len -= len;
2506
21.1M
                if (pos >= s.ringbuffer_size) {
2507
                  // s.partial_pos_rb += (size_t)s.ringbuffer_size;
2508
9.38k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1;
2509
9.38k
                  break; // return return
2510
21.1M
                }
2511
              } else {
2512
                BROTLI_LOG!(
2513
                  "Invalid backward reference. pos: %d distance: %d len: %d bytes left: %d\n",
2514
                  pos, s.distance_code, i,
2515
                  s.meta_block_remaining_len);
2516
121
                result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_TRANSFORM;
2517
121
                break; // return
2518
              }
2519
            } else {
2520
              BROTLI_LOG!(
2521
                "Invalid backward reference. pos:%d distance:%d len:%d bytes left:%d\n",
2522
                pos, s.distance_code, i, s.meta_block_remaining_len);
2523
111
              result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DICTIONARY;
2524
111
              break; // return
2525
            }
2526
          } else {
2527
            // update the recent distances cache
2528
214M
            fast_mut!((s.dist_rb)[(s.dist_rb_idx & 3) as usize]) = s.distance_code;
2529
214M
            s.dist_rb_idx += 1;
2530
214M
            s.meta_block_remaining_len -= i;
2531
            // There is 128+ bytes of slack in the ringbuffer allocation.
2532
            // Also, we have 16 short codes, that make these 16 bytes irrelevant
2533
            // in the ringbuffer. Let's copy over them as a first guess.
2534
            //
2535
214M
            let src_start = ((pos - s.distance_code) & s.ringbuffer_mask) as u32;
2536
214M
            let dst_start = pos as u32;
2537
214M
            let dst_end = pos as u32 + i as u32;
2538
214M
            let src_end = src_start + i as u32;
2539
214M
            memmove16(&mut s.ringbuffer.slice_mut(), dst_start, src_start);
2540
            // Now check if the copy extends over the ringbuffer end,
2541
            // or if the copy overlaps with itself, if yes, do wrap-copy.
2542
214M
            if (src_end > pos as u32 && dst_end > src_start) {
2543
97.3M
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
2544
97.3M
              continue; //goto CommandPostWrapCopy;
2545
117M
            }
2546
117M
            if (dst_end >= s.ringbuffer_size as u32 || src_end >= s.ringbuffer_size as u32) {
2547
286k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
2548
286k
              continue; //goto CommandPostWrapCopy;
2549
116M
            }
2550
116M
            pos += i;
2551
116M
            if (i > 16) {
2552
1.85M
              if (i > 32) {
2553
524k
                memcpy_within_slice(s.ringbuffer.slice_mut(),
2554
524k
                                    dst_start as usize + 16,
2555
524k
                                    src_start as usize + 16,
2556
524k
                                    (i - 16) as usize);
2557
1.33M
              } else {
2558
1.33M
                // This branch covers about 45% cases.
2559
1.33M
                // Fixed size short copy allows more compiler optimizations.
2560
1.33M
                memmove16(&mut s.ringbuffer.slice_mut(),
2561
1.33M
                          dst_start + 16,
2562
1.33M
                          src_start + 16);
2563
1.33M
              }
2564
115M
            }
2565
          }
2566
138M
          if (s.meta_block_remaining_len <= 0) {
2567
            // Next metablock, if any
2568
12.3k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2569
12.3k
            break; // return
2570
          } else {
2571
138M
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
2572
138M
            continue; // goto CommandBegin
2573
          }
2574
        }
2575
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY => {
2576
97.9M
          let mut wrap_guard = s.ringbuffer_size - pos;
2577
97.9M
          let mut inner_return: bool = false;
2578
2.26G
          while i > 0 {
2579
2.16G
            i -= 1;
2580
2.16G
            fast_slice_mut!((s.ringbuffer)[pos as usize]) =
2581
2.16G
              fast_slice!((s.ringbuffer)[((pos - s.distance_code) & s.ringbuffer_mask) as usize]);
2582
2.16G
            pos += 1;
2583
2.16G
            wrap_guard -= 1;
2584
2.16G
            if (wrap_guard == 0) {
2585
337k
              mark_unlikely();
2586
              // s.partial_pos_rb += (size_t)s.ringbuffer_size;
2587
337k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2;
2588
337k
              inner_return = true;
2589
337k
              break; //return
2590
2.16G
            }
2591
          }
2592
97.9M
          if inner_return {
2593
337k
            mark_unlikely();
2594
337k
            break;
2595
97.6M
          }
2596
97.6M
          i -= 1;
2597
97.6M
          if (s.meta_block_remaining_len <= 0) {
2598
            // Next metablock, if any
2599
14.8k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2600
14.8k
            break; // return
2601
          } else {
2602
97.6M
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
2603
97.6M
            continue;
2604
          }
2605
        }
2606
        _ => {
2607
0
          result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
2608
0
          break; // return
2609
        }
2610
      }
2611
    }
2612
  }
2613
3.98M
  s.pos = pos;
2614
3.98M
  s.loop_counter = i;
2615
2616
3.98M
  let _ = core::mem::replace(&mut s.literal_hgroup,
2617
3.98M
                     core::mem::replace(&mut saved_literal_hgroup,
2618
3.98M
                                        HuffmanTreeGroup::<AllocU32, AllocHC>::default()));
2619
2620
3.98M
  let _ = core::mem::replace(&mut s.distance_hgroup,
2621
3.98M
                     core::mem::replace(&mut saved_distance_hgroup,
2622
3.98M
                                        HuffmanTreeGroup::<AllocU32, AllocHC>::default()));
2623
2624
3.98M
  let _ = core::mem::replace(&mut s.insert_copy_hgroup,
2625
3.98M
                     core::mem::replace(&mut saved_insert_copy_hgroup,
2626
3.98M
                                        HuffmanTreeGroup::<AllocU32, AllocHC>::default()));
2627
2628
3.98M
  result
2629
7.37M
}
Unexecuted instantiation: brotli_decompressor::decode::ProcessCommandsInternal::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ProcessCommandsInternal::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
2215
7.37M
fn ProcessCommandsInternal<AllocU8: alloc::Allocator<u8>,
2216
7.37M
                           AllocU32: alloc::Allocator<u32>,
2217
7.37M
                           AllocHC: alloc::Allocator<HuffmanCode>>
2218
7.37M
  (safe: bool,
2219
7.37M
   s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2220
7.37M
   input: &[u8])
2221
7.37M
   -> BrotliDecoderErrorCode {
2222
7.37M
  if (!CheckInputAmount(safe, &s.br, 28)) || (!WarmupBitReader(safe, &mut s.br, input)) {
2223
3.38M
    mark_unlikely();
2224
3.38M
    return BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2225
3.98M
  }
2226
3.98M
  let mut pos = s.pos;
2227
3.98M
  let mut i: i32 = s.loop_counter; // important that this is signed
2228
3.98M
  let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2229
3.98M
  let mut saved_literal_hgroup =
2230
3.98M
    core::mem::replace(&mut s.literal_hgroup,
2231
3.98M
                       HuffmanTreeGroup::<AllocU32, AllocHC>::default());
2232
3.98M
  let mut saved_distance_hgroup =
2233
3.98M
    core::mem::replace(&mut s.distance_hgroup,
2234
3.98M
                       HuffmanTreeGroup::<AllocU32, AllocHC>::default());
2235
3.98M
  let mut saved_insert_copy_hgroup =
2236
3.98M
    core::mem::replace(&mut s.insert_copy_hgroup,
2237
3.98M
                       HuffmanTreeGroup::<AllocU32, AllocHC>::default());
2238
  {
2239
2240
3.98M
    let literal_hgroup = saved_literal_hgroup.build_hgroup_cache();
2241
3.98M
    let distance_hgroup = saved_distance_hgroup.build_hgroup_cache();
2242
3.98M
    let insert_copy_hgroup = saved_insert_copy_hgroup.build_hgroup_cache();
2243
2244
    loop {
2245
722M
      match s.state {
2246
        BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN => {
2247
236M
          if (!CheckInputAmount(safe, &s.br, 28)) {
2248
            // 156 bits + 7 bytes
2249
15.6k
            mark_unlikely();
2250
15.6k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2251
15.6k
            break; // return
2252
236M
          }
2253
236M
          if (fast_mut!((s.block_type_length_state.block_length)[1]) == 0) {
2254
119k
            mark_unlikely();
2255
119k
            if !DecodeCommandBlockSwitchInternal(safe, s, input) {
2256
1.29k
              result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2257
1.29k
              break; // return
2258
117k
            }
2259
117k
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
2260
117k
            continue; // goto CommandBegin;
2261
236M
          }
2262
          // Read the insert/copy length in the command
2263
236M
          if (!ReadCommandInternal(safe, s, &mut i, input, &insert_copy_hgroup)) && safe {
2264
548k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2265
548k
            break; // return
2266
235M
          }
2267
          BROTLI_LOG!("[ProcessCommandsInternal] pos = %d insert = %d copy = %d distance = %d\n",
2268
              pos, i, s.copy_length, s.distance_code);
2269
235M
          if (i == 0) {
2270
86.3M
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
2271
86.3M
            continue; // goto CommandPostDecodeLiterals;
2272
149M
          }
2273
149M
          s.meta_block_remaining_len -= i;
2274
149M
          s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2275
        }
2276
        BrotliRunningState::BROTLI_STATE_COMMAND_INNER => {
2277
          // Read the literals in the command
2278
152M
          if (s.trivial_literal_context != 0) {
2279
101M
            let mut bits: u32 = 0;
2280
101M
            let mut value: u32 = 0;
2281
101M
            let mut literal_htree = &fast!((literal_hgroup)[s.literal_htree_index as usize]);
2282
101M
            PreloadSymbol(safe, literal_htree, &mut s.br, &mut bits, &mut value, input);
2283
101M
            let mut inner_return: bool = false;
2284
101M
            let mut inner_continue: bool = false;
2285
            loop {
2286
1.74G
              if (!CheckInputAmount(safe, &s.br, 28)) {
2287
                // 162 bits + 7 bytes
2288
28.6k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2289
28.6k
                inner_return = true;
2290
28.6k
                break;
2291
1.74G
              }
2292
1.74G
              if (fast!((s.block_type_length_state.block_length)[0]) == 0) {
2293
1.26M
                mark_unlikely();
2294
1.26M
                if (!DecodeLiteralBlockSwitchInternal(safe, s, input)) && safe {
2295
468
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2296
468
                  inner_return = true;
2297
468
                  break;
2298
1.26M
                }
2299
1.26M
                literal_htree = fast_ref!((literal_hgroup)[s.literal_htree_index as usize]);
2300
1.26M
                PreloadSymbol(safe, literal_htree, &mut s.br, &mut bits, &mut value, input);
2301
1.26M
                if (s.trivial_literal_context == 0) {
2302
4.55k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2303
4.55k
                  inner_continue = true;
2304
4.55k
                  break; // goto StateCommandInner
2305
1.26M
                }
2306
1.74G
              }
2307
1.74G
              if (!safe) {
2308
438M
                fast_mut!((s.ringbuffer.slice_mut())[pos as usize]) =
2309
438M
                  ReadPreloadedSymbol(literal_htree, &mut s.br, &mut bits, &mut value, input) as u8;
2310
438M
              } else {
2311
1.30G
                let mut literal: u32 = 0;
2312
1.30G
                if (!SafeReadSymbol(literal_htree, &mut s.br, &mut literal, input)) {
2313
1.35M
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2314
1.35M
                  inner_return = true;
2315
1.35M
                  break;
2316
1.30G
                }
2317
1.30G
                fast_mut!((s.ringbuffer.slice_mut())[pos as usize]) = literal as u8;
2318
              }
2319
1.74G
              if (s.block_type_length_state.block_length)[0] == 0 {
2320
0
                  mark_unlikely();
2321
0
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
2322
0
                  inner_return = true;
2323
0
                  break;
2324
1.74G
              }
2325
1.74G
              fast_mut!((s.block_type_length_state.block_length)[0]) -= 1;
2326
              BROTLI_LOG_UINT!(s.literal_htree_index);
2327
              BROTLI_LOG_ARRAY_INDEX!(s.ringbuffer.slice(), pos);
2328
1.74G
              pos += 1;
2329
1.74G
              if (pos == s.ringbuffer_size) {
2330
154k
                mark_unlikely();
2331
154k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE;
2332
154k
                i -= 1;
2333
154k
                inner_return = true;
2334
154k
                break;
2335
1.74G
              }
2336
1.74G
              i -= 1;
2337
1.74G
              if i == 0 {
2338
99.5M
                break;
2339
1.64G
              }
2340
            }
2341
101M
            if inner_return {
2342
1.54M
              break; // return
2343
99.5M
            }
2344
99.5M
            if inner_continue {
2345
4.55k
              mark_unlikely();
2346
4.55k
              continue;
2347
99.5M
            }
2348
          } else {
2349
51.0M
            let mut p1 = fast_slice!((s.ringbuffer)[((pos - 1) & s.ringbuffer_mask) as usize]);
2350
51.0M
            let mut p2 = fast_slice!((s.ringbuffer)[((pos - 2) & s.ringbuffer_mask) as usize]);
2351
51.0M
            if s.custom_dict_avoid_context_seed && pos < 2 {
2352
0
                mark_unlikely();
2353
0
                p2 = 0;
2354
0
                p1 = 0;
2355
51.0M
            }
2356
51.0M
            if pos > 1
2357
51.0M
            {
2358
51.0M
                // have already set both seed bytes and can now move on to using
2359
51.0M
                // the ringbuffer.
2360
51.0M
                s.custom_dict_avoid_context_seed = false;
2361
51.0M
            }
2362
51.0M
            let mut inner_return: bool = false;
2363
51.0M
            let mut inner_continue: bool = false;
2364
            loop {
2365
369M
              if (!CheckInputAmount(safe, &s.br, 28)) {
2366
                // 162 bits + 7 bytes
2367
17.4k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2368
17.4k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2369
17.4k
                inner_return = true;
2370
17.4k
                break;
2371
369M
              }
2372
369M
              if (fast!((s.block_type_length_state.block_length)[0]) == 0) {
2373
1.81M
                mark_unlikely();
2374
1.81M
                if (!DecodeLiteralBlockSwitchInternal(safe, s, input)) && safe {
2375
7.04k
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2376
7.04k
                  inner_return = true;
2377
7.04k
                  break;
2378
1.80M
                }
2379
1.80M
                if s.trivial_literal_context != 0 {
2380
4.54k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
2381
4.54k
                  inner_continue = true;
2382
4.54k
                  break;
2383
1.80M
                }
2384
367M
              }
2385
369M
              let context = s.context_lookup[p1 as usize] | s.context_lookup[p2 as usize |256];
2386
              BROTLI_LOG_UINT!(p1);
2387
              BROTLI_LOG_UINT!(p2);
2388
              BROTLI_LOG_UINT!(context);
2389
              let hc: &[HuffmanCode];
2390
369M
              {
2391
369M
                let i = fast_slice!((s.context_map)[s.context_map_slice_index + context as usize]);
2392
369M
                hc = fast!((literal_hgroup)[i as usize]);
2393
369M
              }
2394
369M
              p2 = p1;
2395
369M
              if (!safe) {
2396
256M
                p1 = ReadSymbol(hc, &mut s.br, input) as u8;
2397
256M
              } else {
2398
113M
                let mut literal: u32 = 0;
2399
113M
                if (!SafeReadSymbol(hc, &mut s.br, &mut literal, input)) {
2400
1.05M
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2401
1.05M
                  inner_return = true;
2402
1.05M
                  break;
2403
111M
                }
2404
111M
                p1 = literal as u8;
2405
              }
2406
368M
              fast_slice_mut!((s.ringbuffer)[pos as usize]) = p1;
2407
368M
              if (s.block_type_length_state.block_length)[0] == 0 {
2408
0
                  result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
2409
0
                  inner_return = true;
2410
0
                  break;
2411
368M
              }
2412
368M
              fast_mut!((s.block_type_length_state.block_length)[0]) -= 1;
2413
              BROTLI_LOG_UINT!(s.context_map.slice()[s.context_map_slice_index as usize +
2414
                                                     context as usize]);
2415
              BROTLI_LOG_ARRAY_INDEX!(s.ringbuffer.slice(), pos & s.ringbuffer_mask);
2416
368M
              pos += 1;
2417
368M
              if (pos == s.ringbuffer_size) {
2418
26.6k
                mark_unlikely();
2419
26.6k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE;
2420
26.6k
                i -= 1;
2421
26.6k
                inner_return = true;
2422
26.6k
                break;
2423
368M
              }
2424
368M
              i -= 1;
2425
368M
              if i == 0 {
2426
49.9M
                break;
2427
318M
              }
2428
            }
2429
51.0M
            if inner_return {
2430
1.11M
              break; // return
2431
49.9M
            }
2432
49.9M
            if inner_continue {
2433
4.54k
              mark_unlikely();
2434
4.54k
              continue;
2435
49.9M
            }
2436
          }
2437
149M
          if (s.meta_block_remaining_len <= 0) {
2438
19.9k
            mark_unlikely();
2439
19.9k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2440
19.9k
            break; // return
2441
149M
          }
2442
149M
          s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
2443
        }
2444
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS => {
2445
236M
          if s.distance_code >= 0 {
2446
150M
            let not_distance_code = if s.distance_code != 0 { 0 } else { 1 };
2447
150M
            s.distance_context = not_distance_code;
2448
150M
            s.dist_rb_idx -= 1;
2449
150M
            s.distance_code = fast!((s.dist_rb)[(s.dist_rb_idx & 3) as usize]);
2450
            // goto postReadDistance
2451
          } else {
2452
85.6M
            if fast!((s.block_type_length_state.block_length)[2]) == 0 {
2453
749k
              mark_unlikely();
2454
749k
              if (!DecodeDistanceBlockSwitchInternal(safe, s, input)) && safe {
2455
611
                result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2456
611
                break; // return
2457
748k
              }
2458
84.9M
            }
2459
85.6M
            if (!ReadDistanceInternal(safe, s, input, &distance_hgroup)) && safe {
2460
371k
              result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2461
371k
              break; // return
2462
85.3M
            }
2463
          }
2464
          // postReadDistance:
2465
          BROTLI_LOG!("[ProcessCommandsInternal] pos = %d distance = %d\n",
2466
                      pos, s.distance_code);
2467
2468
235M
          if (s.max_distance != s.max_backward_distance) {
2469
78.2M
            if (pos < s.max_backward_distance_minus_custom_dict_size) {
2470
78.2M
              s.max_distance = pos + s.custom_dict_size as i32;
2471
78.2M
            } else {
2472
1.20k
              s.max_distance = s.max_backward_distance;
2473
1.20k
            }
2474
157M
          }
2475
235M
          i = s.copy_length;
2476
          // Apply copy of LZ77 back-reference, or static dictionary reference if
2477
          // the distance is larger than the max LZ77 distance
2478
235M
          if (s.distance_code > s.max_distance) {
2479
21.1M
            if s.distance_code > kBrotliMaxAllowedDistance as i32 {
2480
46
              return BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE;
2481
21.1M
            }
2482
21.1M
            if (i >= kBrotliMinDictionaryWordLength as i32 &&
2483
21.1M
                i <= kBrotliMaxDictionaryWordLength as i32) {
2484
21.1M
              let mut offset = fast!((kBrotliDictionaryOffsetsByLength)[i as usize]) as i32;
2485
21.1M
              let word_id = s.distance_code - s.max_distance - 1;
2486
21.1M
              let shift = fast!((kBrotliDictionarySizeBitsByLength)[i as usize]);
2487
21.1M
              let mask = bit_reader::BitMask(shift as u32) as i32;
2488
21.1M
              let word_idx = word_id & mask;
2489
21.1M
              let transform_idx = word_id >> shift;
2490
21.1M
              s.dist_rb_idx += s.distance_context;
2491
21.1M
              offset += word_idx * i;
2492
21.1M
              if (transform_idx < kNumTransforms) {
2493
21.1M
                let mut len = i;
2494
21.1M
                let word = fast!((kBrotliDictionary)[offset as usize ; (offset + len) as usize]);
2495
21.1M
                if (transform_idx == 0) {
2496
20.8M
                  fast_slice_mut!((s.ringbuffer)[pos as usize ; ((pos + len) as usize)])
2497
20.8M
                    .clone_from_slice(word);
2498
20.8M
                } else {
2499
284k
                  len = TransformDictionaryWord(fast_slice_mut!((s.ringbuffer)[pos as usize;]),
2500
284k
                                                word,
2501
284k
                                                len,
2502
284k
                                                transform_idx);
2503
284k
                }
2504
21.1M
                pos += len;
2505
21.1M
                s.meta_block_remaining_len -= len;
2506
21.1M
                if (pos >= s.ringbuffer_size) {
2507
                  // s.partial_pos_rb += (size_t)s.ringbuffer_size;
2508
9.38k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1;
2509
9.38k
                  break; // return return
2510
21.1M
                }
2511
              } else {
2512
                BROTLI_LOG!(
2513
                  "Invalid backward reference. pos: %d distance: %d len: %d bytes left: %d\n",
2514
                  pos, s.distance_code, i,
2515
                  s.meta_block_remaining_len);
2516
121
                result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_TRANSFORM;
2517
121
                break; // return
2518
              }
2519
            } else {
2520
              BROTLI_LOG!(
2521
                "Invalid backward reference. pos:%d distance:%d len:%d bytes left:%d\n",
2522
                pos, s.distance_code, i, s.meta_block_remaining_len);
2523
111
              result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DICTIONARY;
2524
111
              break; // return
2525
            }
2526
          } else {
2527
            // update the recent distances cache
2528
214M
            fast_mut!((s.dist_rb)[(s.dist_rb_idx & 3) as usize]) = s.distance_code;
2529
214M
            s.dist_rb_idx += 1;
2530
214M
            s.meta_block_remaining_len -= i;
2531
            // There is 128+ bytes of slack in the ringbuffer allocation.
2532
            // Also, we have 16 short codes, that make these 16 bytes irrelevant
2533
            // in the ringbuffer. Let's copy over them as a first guess.
2534
            //
2535
214M
            let src_start = ((pos - s.distance_code) & s.ringbuffer_mask) as u32;
2536
214M
            let dst_start = pos as u32;
2537
214M
            let dst_end = pos as u32 + i as u32;
2538
214M
            let src_end = src_start + i as u32;
2539
214M
            memmove16(&mut s.ringbuffer.slice_mut(), dst_start, src_start);
2540
            // Now check if the copy extends over the ringbuffer end,
2541
            // or if the copy overlaps with itself, if yes, do wrap-copy.
2542
214M
            if (src_end > pos as u32 && dst_end > src_start) {
2543
97.3M
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
2544
97.3M
              continue; //goto CommandPostWrapCopy;
2545
117M
            }
2546
117M
            if (dst_end >= s.ringbuffer_size as u32 || src_end >= s.ringbuffer_size as u32) {
2547
286k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
2548
286k
              continue; //goto CommandPostWrapCopy;
2549
116M
            }
2550
116M
            pos += i;
2551
116M
            if (i > 16) {
2552
1.85M
              if (i > 32) {
2553
524k
                memcpy_within_slice(s.ringbuffer.slice_mut(),
2554
524k
                                    dst_start as usize + 16,
2555
524k
                                    src_start as usize + 16,
2556
524k
                                    (i - 16) as usize);
2557
1.33M
              } else {
2558
1.33M
                // This branch covers about 45% cases.
2559
1.33M
                // Fixed size short copy allows more compiler optimizations.
2560
1.33M
                memmove16(&mut s.ringbuffer.slice_mut(),
2561
1.33M
                          dst_start + 16,
2562
1.33M
                          src_start + 16);
2563
1.33M
              }
2564
115M
            }
2565
          }
2566
138M
          if (s.meta_block_remaining_len <= 0) {
2567
            // Next metablock, if any
2568
12.3k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2569
12.3k
            break; // return
2570
          } else {
2571
138M
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
2572
138M
            continue; // goto CommandBegin
2573
          }
2574
        }
2575
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY => {
2576
97.9M
          let mut wrap_guard = s.ringbuffer_size - pos;
2577
97.9M
          let mut inner_return: bool = false;
2578
2.26G
          while i > 0 {
2579
2.16G
            i -= 1;
2580
2.16G
            fast_slice_mut!((s.ringbuffer)[pos as usize]) =
2581
2.16G
              fast_slice!((s.ringbuffer)[((pos - s.distance_code) & s.ringbuffer_mask) as usize]);
2582
2.16G
            pos += 1;
2583
2.16G
            wrap_guard -= 1;
2584
2.16G
            if (wrap_guard == 0) {
2585
337k
              mark_unlikely();
2586
              // s.partial_pos_rb += (size_t)s.ringbuffer_size;
2587
337k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2;
2588
337k
              inner_return = true;
2589
337k
              break; //return
2590
2.16G
            }
2591
          }
2592
97.9M
          if inner_return {
2593
337k
            mark_unlikely();
2594
337k
            break;
2595
97.6M
          }
2596
97.6M
          i -= 1;
2597
97.6M
          if (s.meta_block_remaining_len <= 0) {
2598
            // Next metablock, if any
2599
14.8k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2600
14.8k
            break; // return
2601
          } else {
2602
97.6M
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
2603
97.6M
            continue;
2604
          }
2605
        }
2606
        _ => {
2607
0
          result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
2608
0
          break; // return
2609
        }
2610
      }
2611
    }
2612
  }
2613
3.98M
  s.pos = pos;
2614
3.98M
  s.loop_counter = i;
2615
2616
3.98M
  let _ = core::mem::replace(&mut s.literal_hgroup,
2617
3.98M
                     core::mem::replace(&mut saved_literal_hgroup,
2618
3.98M
                                        HuffmanTreeGroup::<AllocU32, AllocHC>::default()));
2619
2620
3.98M
  let _ = core::mem::replace(&mut s.distance_hgroup,
2621
3.98M
                     core::mem::replace(&mut saved_distance_hgroup,
2622
3.98M
                                        HuffmanTreeGroup::<AllocU32, AllocHC>::default()));
2623
2624
3.98M
  let _ = core::mem::replace(&mut s.insert_copy_hgroup,
2625
3.98M
                     core::mem::replace(&mut saved_insert_copy_hgroup,
2626
3.98M
                                        HuffmanTreeGroup::<AllocU32, AllocHC>::default()));
2627
2628
3.98M
  result
2629
7.37M
}
2630
2631
3.92M
fn ProcessCommands<AllocU8: alloc::Allocator<u8>,
2632
3.92M
                   AllocU32: alloc::Allocator<u32>,
2633
3.92M
                   AllocHC: alloc::Allocator<HuffmanCode>>
2634
3.92M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2635
3.92M
   input: &[u8])
2636
3.92M
   -> BrotliDecoderErrorCode {
2637
3.92M
  ProcessCommandsInternal(false, s, input)
2638
3.92M
}
Unexecuted instantiation: brotli_decompressor::decode::ProcessCommands::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::ProcessCommands::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
2631
3.92M
fn ProcessCommands<AllocU8: alloc::Allocator<u8>,
2632
3.92M
                   AllocU32: alloc::Allocator<u32>,
2633
3.92M
                   AllocHC: alloc::Allocator<HuffmanCode>>
2634
3.92M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2635
3.92M
   input: &[u8])
2636
3.92M
   -> BrotliDecoderErrorCode {
2637
3.92M
  ProcessCommandsInternal(false, s, input)
2638
3.92M
}
2639
2640
3.44M
fn SafeProcessCommands<AllocU8: alloc::Allocator<u8>,
2641
3.44M
                       AllocU32: alloc::Allocator<u32>,
2642
3.44M
                       AllocHC: alloc::Allocator<HuffmanCode>>
2643
3.44M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2644
3.44M
   input: &[u8])
2645
3.44M
   -> BrotliDecoderErrorCode {
2646
3.44M
  ProcessCommandsInternal(true, s, input)
2647
3.44M
}
Unexecuted instantiation: brotli_decompressor::decode::SafeProcessCommands::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::SafeProcessCommands::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
2640
3.44M
fn SafeProcessCommands<AllocU8: alloc::Allocator<u8>,
2641
3.44M
                       AllocU32: alloc::Allocator<u32>,
2642
3.44M
                       AllocHC: alloc::Allocator<HuffmanCode>>
2643
3.44M
  (s: &mut BrotliState<AllocU8, AllocU32, AllocHC>,
2644
3.44M
   input: &[u8])
2645
3.44M
   -> BrotliDecoderErrorCode {
2646
3.44M
  ProcessCommandsInternal(true, s, input)
2647
3.44M
}
2648
2649
/* Returns the maximum number of distance symbols which can only represent
2650
   distances not exceeding BROTLI_MAX_ALLOWED_DISTANCE. */
2651
686
pub fn BrotliMaxDistanceSymbol(ndirect: u32, npostfix: u32) -> u32{
2652
686
  let bound:[u32;kBrotliMaxPostfix + 1] = [0, 4, 12, 28];
2653
686
  let diff:[u32;kBrotliMaxPostfix + 1] = [73, 126, 228, 424];
2654
686
  let postfix = 1 << npostfix;
2655
686
  if (ndirect < bound[npostfix as usize ]) {
2656
370
    return ndirect + diff[npostfix as usize] + postfix;
2657
316
  } else if (ndirect > bound[npostfix as usize] + postfix) {
2658
286
    return ndirect + diff[npostfix as usize];
2659
  } else {
2660
30
    return bound[npostfix as usize] + diff[npostfix as usize] + postfix;
2661
  }
2662
686
}
2663
2664
171M
pub fn BrotliDecompressStream<AllocU8: alloc::Allocator<u8>,
2665
171M
                              AllocU32: alloc::Allocator<u32>,
2666
171M
                              AllocHC: alloc::Allocator<HuffmanCode>>
2667
171M
  (available_in: &mut usize,
2668
171M
   input_offset: &mut usize,
2669
171M
   xinput: &[u8],
2670
171M
   mut available_out: &mut usize,
2671
171M
   mut output_offset: &mut usize,
2672
171M
   mut output: &mut [u8],
2673
171M
   mut total_out: &mut usize,
2674
171M
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>)
2675
171M
   -> BrotliResult {
2676
2677
171M
  let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2678
2679
171M
  let mut saved_buffer: [u8; 8] = s.buffer;
2680
  let mut local_input: &[u8];
2681
171M
  if is_fatal(s.error_code) {
2682
1.10k
    return BrotliResult::ResultFailure;
2683
171M
  }
2684
171M
  if *available_in as u64 >= (1u64 << 32) {
2685
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2686
171M
  }
2687
171M
  if *input_offset as u64 >= (1u64 << 32) {
2688
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2689
171M
  }
2690
171M
  if *input_offset + *available_in > xinput.len() {
2691
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2692
171M
  }
2693
171M
  if *output_offset + *available_out > output.len() {
2694
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2695
171M
  }
2696
171M
  if s.buffer_length == 0 {
2697
171M
    local_input = xinput;
2698
171M
    s.br.avail_in = *available_in as u32;
2699
171M
    s.br.next_in = *input_offset as u32;
2700
171M
  } else {
2701
57.8k
    result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2702
57.8k
    let copy_len = core::cmp::min(saved_buffer.len() - s.buffer_length as usize, *available_in);
2703
57.8k
    if copy_len > 0 {
2704
55.2k
      fast_mut!((saved_buffer)[s.buffer_length as usize ; (s.buffer_length as usize + copy_len)])
2705
55.2k
        .clone_from_slice(fast!((xinput)[*input_offset ; copy_len + *input_offset]));
2706
55.2k
      fast_mut!((s.buffer)[s.buffer_length as usize ; (s.buffer_length as usize + copy_len)])
2707
55.2k
        .clone_from_slice(fast!((xinput)[*input_offset ; copy_len + *input_offset]));
2708
55.2k
    }
2709
57.8k
    local_input = &saved_buffer[..];
2710
57.8k
    s.br.next_in = 0;
2711
  }
2712
  loop {
2713
344M
    match result {
2714
172M
      BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2715
      _ => {
2716
171M
        match result {
2717
          BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT => {
2718
4.50M
            if s.ringbuffer.slice().len() != 0 {
2719
4.49M
              let (intermediate_result, _) = WriteRingBuffer(available_out,
2720
4.49M
                                                             Some(&mut output),
2721
4.49M
                                                             &mut output_offset,
2722
4.49M
                                                             &mut total_out,
2723
4.49M
                                                             true,
2724
4.49M
                                                             &mut s);
2725
4.49M
              if is_fatal(intermediate_result) {
2726
92
                result = intermediate_result;
2727
92
                break;
2728
4.49M
              }
2729
14.1k
            }
2730
4.50M
            if s.buffer_length != 0 {
2731
              // Used with internal buffer.
2732
113k
              if s.br.avail_in == 0 {
2733
                // Successfully finished read transaction.
2734
                // Accamulator contains less than 8 bits, because internal buffer
2735
                // is expanded byte-by-byte until it is enough to complete read.
2736
53.4k
                s.buffer_length = 0;
2737
                // Switch to input stream and restart.
2738
53.4k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2739
53.4k
                local_input = xinput;
2740
53.4k
                s.br.avail_in = *available_in as u32;
2741
53.4k
                s.br.next_in = *input_offset as u32;
2742
53.4k
                continue;
2743
59.9k
              } else if *available_in != 0 {
2744
                // Not enough data in buffer, but can take one more byte from
2745
                // input stream.
2746
55.5k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2747
55.5k
                let new_byte = fast!((xinput)[*input_offset]);
2748
55.5k
                fast_mut!((s.buffer)[s.buffer_length as usize]) = new_byte;
2749
                // we did the following copy upfront, so we wouldn't have to do it here
2750
                // since saved_buffer[s.buffer_length as usize] = new_byte violates borrow rules
2751
55.5k
                assert_eq!(fast!((saved_buffer)[s.buffer_length as usize]), new_byte);
2752
55.5k
                s.buffer_length += 1;
2753
55.5k
                s.br.avail_in = s.buffer_length;
2754
55.5k
                (*input_offset) += 1;
2755
55.5k
                (*available_in) -= 1;
2756
                // Retry with more data in buffer.
2757
                // we can't re-borrow the saved buffer...so we have to do this recursively
2758
55.5k
                continue;
2759
4.37k
              }
2760
              // Can't finish reading and no more input.
2761
2762
              // FIXME :: NOT SURE WHAT THIS MEANT
2763
              // saved_buffer = core::mem::replace(
2764
              //  &mut s.br.input_,
2765
              //  &mut[]); // clear input
2766
4.37k
              break;
2767
            } else {
2768
              // Input stream doesn't contain enough input.
2769
              // Copy tail to internal buffer and return.
2770
4.39M
              *input_offset = s.br.next_in as usize;
2771
4.39M
              *available_in = s.br.avail_in as usize;
2772
4.44M
              while *available_in != 0 {
2773
54.3k
                fast_mut!((s.buffer)[s.buffer_length as usize]) = fast!((xinput)[*input_offset]);
2774
54.3k
                s.buffer_length += 1;
2775
54.3k
                (*input_offset) += 1;
2776
54.3k
                (*available_in) -= 1;
2777
54.3k
              }
2778
4.39M
              break;
2779
            }
2780
            // unreachable!(); <- dead code
2781
          }
2782
          _ => {
2783
            // Fail or needs more output.
2784
166M
            if s.buffer_length != 0 {
2785
2
              // Just consumed the buffered input and produced some output. Otherwise
2786
2
              // it would result in "needs more input". Reset internal buffer.
2787
2
              s.buffer_length = 0;
2788
166M
            } else {
2789
166M
              // Using input stream in last iteration. When decoder switches to input
2790
166M
              // stream it has less than 8 bits in accamulator, so it is safe to
2791
166M
              // return unused accamulator bits there.
2792
166M
              bit_reader::BrotliBitReaderUnload(&mut s.br);
2793
166M
              *available_in = s.br.avail_in as usize;
2794
166M
              *input_offset = s.br.next_in as usize;
2795
166M
            }
2796
          }
2797
        }
2798
166M
        break;
2799
      }
2800
    }
2801
    loop {
2802
      // this emulates fallthrough behavior
2803
173M
      match s.state {
2804
        BrotliRunningState::BROTLI_STATE_UNINITED => {
2805
          // Prepare to the first read.
2806
30.1k
          if (!bit_reader::BrotliWarmupBitReader(&mut s.br, local_input)) {
2807
12.6k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2808
12.6k
            break;
2809
17.4k
          }
2810
          // Decode window size.
2811
          /* Reads 1..8 bits. */
2812
17.4k
          result = DecodeWindowBits(&mut s.large_window, &mut s.window_bits, &mut s.br);
2813
17.4k
          match result {
2814
17.4k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2815
1
            _ => break,
2816
          }
2817
17.4k
          if s.large_window {
2818
201
              s.state = BrotliRunningState::BROTLI_STATE_LARGE_WINDOW_BITS;
2819
17.2k
          } else {
2820
17.2k
              s.state = BrotliRunningState::BROTLI_STATE_INITIALIZE;
2821
17.2k
          }
2822
        }
2823
        BrotliRunningState::BROTLI_STATE_LARGE_WINDOW_BITS => {
2824
202
          if (!bit_reader::BrotliSafeReadBits(&mut s.br, 6, &mut s.window_bits, local_input)) {
2825
2
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2826
2
            break;
2827
200
          }
2828
200
          if (s.window_bits < kBrotliLargeMinWbits ||
2829
195
              s.window_bits > kBrotliLargeMaxWbits) {
2830
9
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
2831
9
            break;
2832
191
          }
2833
191
          s.state = BrotliRunningState::BROTLI_STATE_INITIALIZE;
2834
        }
2835
        BrotliRunningState::BROTLI_STATE_INITIALIZE => {
2836
17.4k
          s.max_backward_distance = (1 << s.window_bits) - kBrotliWindowGap as i32;
2837
17.4k
          s.max_backward_distance_minus_custom_dict_size = (s.max_backward_distance as isize -
2838
17.4k
                                                           s.custom_dict_size) as i32;
2839
2840
          // (formerly) Allocate memory for both block_type_trees and block_len_trees.
2841
17.4k
          s.block_type_length_state.block_type_trees = s.alloc_hc
2842
17.4k
            .alloc_cell(3 * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as usize);
2843
17.4k
          if (s.block_type_length_state.block_type_trees.slice().len() == 0) {
2844
0
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES;
2845
0
            break;
2846
17.4k
          }
2847
17.4k
          s.block_type_length_state.block_len_trees = s.alloc_hc
2848
17.4k
            .alloc_cell(3 * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as usize);
2849
2850
17.4k
          s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN;
2851
          // No break, continue to next state
2852
        }
2853
70.2k
        BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN => {
2854
70.2k
          s.BrotliStateMetablockBegin();
2855
70.2k
          BROTLI_LOG_UINT!(s.pos);
2856
70.2k
          s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER;
2857
70.2k
          // No break, continue to next state
2858
70.2k
        }
2859
        BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER => {
2860
84.4k
          result = DecodeMetaBlockLength(&mut s, local_input); // Reads 2 - 31 bits.
2861
84.4k
          match result {
2862
69.9k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2863
14.4k
            _ => break,
2864
          }
2865
          BROTLI_LOG_UINT!(s.is_last_metablock);
2866
          BROTLI_LOG_UINT!(s.meta_block_remaining_len);
2867
          BROTLI_LOG_UINT!(s.is_metadata);
2868
          BROTLI_LOG_UINT!(s.is_uncompressed);
2869
69.9k
          if (s.is_metadata != 0 || s.is_uncompressed != 0) &&
2870
14.1k
             !bit_reader::BrotliJumpToByteBoundary(&mut s.br) {
2871
34
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_2;
2872
34
            break;
2873
69.9k
          }
2874
69.9k
          if s.is_metadata != 0 {
2875
1.68k
            s.state = BrotliRunningState::BROTLI_STATE_METADATA;
2876
1.68k
            break;
2877
68.2k
          }
2878
68.2k
          if s.meta_block_remaining_len == 0 {
2879
4.00k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2880
4.00k
            break;
2881
64.2k
          }
2882
64.2k
          if s.ringbuffer.slice().len() == 0 && !BrotliAllocateRingBuffer(&mut s, local_input) {
2883
0
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2;
2884
0
            break;
2885
64.2k
          }
2886
64.2k
          if s.is_uncompressed != 0 {
2887
12.4k
            s.state = BrotliRunningState::BROTLI_STATE_UNCOMPRESSED;
2888
12.4k
            break;
2889
51.8k
          }
2890
51.8k
          s.loop_counter = 0;
2891
51.8k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0;
2892
51.8k
          break;
2893
        }
2894
        BrotliRunningState::BROTLI_STATE_UNCOMPRESSED => {
2895
699k
          let mut _bytes_copied = s.meta_block_remaining_len;
2896
699k
          result = CopyUncompressedBlockToOutput(&mut available_out,
2897
699k
                                                 &mut output,
2898
699k
                                                 &mut output_offset,
2899
699k
                                                 &mut total_out,
2900
699k
                                                 &mut s,
2901
699k
                                                 local_input);
2902
699k
          _bytes_copied -= s.meta_block_remaining_len;
2903
699k
          match result {
2904
11.9k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2905
687k
            _ => break,
2906
          }
2907
11.9k
          s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2908
11.9k
          break;
2909
        }
2910
        BrotliRunningState::BROTLI_STATE_METADATA => {
2911
4.17M
          while s.meta_block_remaining_len > 0 {
2912
4.17M
            let mut bits = 0u32;
2913
            // Read one byte and ignore it.
2914
4.17M
            if !bit_reader::BrotliSafeReadBits(&mut s.br, 8, &mut bits, local_input) {
2915
112
              result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2916
112
              break;
2917
4.17M
            }
2918
4.17M
            s.meta_block_remaining_len -= 1;
2919
          }
2920
1.74k
          if let BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS = result {
2921
1.63k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE
2922
112
          }
2923
1.74k
          break;
2924
        }
2925
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0 => {
2926
207k
          if s.loop_counter >= 3 {
2927
51.1k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER_2;
2928
51.1k
            break;
2929
156k
          }
2930
          // Reads 1..11 bits.
2931
156k
          {
2932
156k
            let index = s.loop_counter as usize;
2933
156k
            result =
2934
156k
              DecodeVarLenUint8(&mut s.substate_decode_uint8,
2935
156k
                                &mut s.br,
2936
156k
                                &mut fast_mut!((s.block_type_length_state.num_block_types)[index]),
2937
156k
                                local_input);
2938
156k
          }
2939
156k
          match result {
2940
154k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2941
1.78k
            _ => break,
2942
          }
2943
154k
          fast_mut!((s.block_type_length_state.num_block_types)[s.loop_counter as usize]) += 1;
2944
          BROTLI_LOG_UINT!(s.block_type_length_state.num_block_types[s.loop_counter as usize]);
2945
154k
          if fast!((s.block_type_length_state.num_block_types)[s.loop_counter as usize]) < 2 {
2946
145k
            s.loop_counter += 1;
2947
145k
            break;
2948
9.14k
          }
2949
9.14k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_1;
2950
          // No break, continue to next state
2951
        }
2952
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_1 => {
2953
9.67k
          let tree_offset = s.loop_counter as u32 * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as u32;
2954
9.67k
          let mut new_huffman_table = mem::replace(&mut s.block_type_length_state.block_type_trees,
2955
9.67k
                                                   AllocHC::AllocatedMemory::default());
2956
9.67k
          let loop_counter = s.loop_counter as usize;
2957
9.67k
          let alphabet_size = fast!((s.block_type_length_state.num_block_types)[loop_counter]) + 2;
2958
9.67k
          result =
2959
9.67k
            ReadHuffmanCode(alphabet_size, alphabet_size,
2960
9.67k
                            new_huffman_table.slice_mut(),
2961
9.67k
                            tree_offset as usize,
2962
9.67k
                            None,
2963
9.67k
                            &mut s,
2964
9.67k
                            local_input);
2965
9.67k
          let _ = mem::replace(&mut s.block_type_length_state.block_type_trees,
2966
9.67k
                       new_huffman_table);
2967
9.67k
          match result {
2968
8.76k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2969
913
            _ => break,
2970
          }
2971
8.76k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_2;
2972
          // No break, continue to next state
2973
        }
2974
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_2 => {
2975
9.56k
          let tree_offset = s.loop_counter * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as i32;
2976
9.56k
          let mut new_huffman_table = mem::replace(&mut s.block_type_length_state.block_len_trees,
2977
9.56k
                                                   AllocHC::AllocatedMemory::default());
2978
9.56k
          result = ReadHuffmanCode(kNumBlockLengthCodes, kNumBlockLengthCodes,
2979
9.56k
                                   new_huffman_table.slice_mut(),
2980
9.56k
                                   tree_offset as usize,
2981
9.56k
                                   None,
2982
9.56k
                                   &mut s,
2983
9.56k
                                   local_input);
2984
9.56k
          let _ = mem::replace(&mut s.block_type_length_state.block_len_trees,
2985
9.56k
                       new_huffman_table);
2986
9.56k
          match result {
2987
8.60k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2988
952
            _ => break,
2989
          }
2990
8.60k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_3;
2991
          // No break, continue to next state
2992
        }
2993
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_3 => {
2994
8.80k
          let tree_offset = s.loop_counter * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as i32;
2995
2996
8.80k
          let mut block_length_out: u32 = 0;
2997
          let ind_ret: (bool, u32);
2998
          
2999
8.80k
          ind_ret = SafeReadBlockLengthIndex(&s.block_type_length_state.substate_read_block_length,
3000
8.80k
                                             s.block_type_length_state.block_length_index,
3001
8.80k
                                             fast_slice!((s.block_type_length_state.block_len_trees)
3002
8.80k
                                                           [tree_offset as usize;]),
3003
8.80k
                                             &mut s.br, local_input);
3004
3005
8.80k
          if !SafeReadBlockLengthFromIndex(&mut s.block_type_length_state,
3006
8.80k
                                           &mut s.br,
3007
8.80k
                                           &mut block_length_out,
3008
8.80k
                                           ind_ret,
3009
8.80k
                                           local_input) {
3010
232
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
3011
232
            break;
3012
8.57k
          }
3013
8.57k
          fast_mut!((s.block_type_length_state.block_length)[s.loop_counter as usize]) =
3014
8.57k
            block_length_out;
3015
          BROTLI_LOG_UINT!(s.block_type_length_state.block_length[s.loop_counter as usize]);
3016
8.57k
          s.loop_counter += 1;
3017
8.57k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0;
3018
8.57k
          break;
3019
        }
3020
        BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER_2 => {
3021
54.0k
          let mut bits: u32 = 0;
3022
54.0k
          if (!bit_reader::BrotliSafeReadBits(&mut s.br, 6, &mut bits, local_input)) {
3023
2.88k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
3024
2.88k
            break;
3025
51.1k
          }
3026
51.1k
          s.distance_postfix_bits = bits & bit_reader::BitMask(2);
3027
51.1k
          bits >>= 2;
3028
51.1k
          s.num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
3029
51.1k
                                        (bits << s.distance_postfix_bits);
3030
          BROTLI_LOG_UINT!(s.num_direct_distance_codes);
3031
          BROTLI_LOG_UINT!(s.distance_postfix_bits);
3032
51.1k
          s.distance_postfix_mask = bit_reader::BitMask(s.distance_postfix_bits) as i32;
3033
51.1k
          s.context_modes = s.alloc_u8
3034
51.1k
            .alloc_cell(fast!((s.block_type_length_state.num_block_types)[0]) as usize);
3035
51.1k
          if (s.context_modes.slice().len() == 0) {
3036
0
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES;
3037
0
            break;
3038
51.1k
          }
3039
51.1k
          s.loop_counter = 0;
3040
51.1k
          s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MODES;
3041
          // No break, continue to next state
3042
        }
3043
        BrotliRunningState::BROTLI_STATE_CONTEXT_MODES => {
3044
52.1k
          result = ReadContextModes(&mut s, local_input);
3045
52.1k
          match result {
3046
51.1k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3047
1.01k
            _ => break,
3048
          }
3049
51.1k
          s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1;
3050
          // No break, continue to next state
3051
        }
3052
        BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1 => {
3053
67.1k
          result =
3054
67.1k
            DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[0]) as usize) <<
3055
67.1k
                             kLiteralContextBits as usize,
3056
67.1k
                             false,
3057
67.1k
                             &mut s,
3058
67.1k
                             local_input);
3059
67.1k
          match result {
3060
51.0k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3061
16.1k
            _ => break,
3062
          }
3063
51.0k
          DetectTrivialLiteralBlockTypes(s);
3064
51.0k
          s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2;
3065
          // No break, continue to next state
3066
        }
3067
        BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2 => {
3068
51.8k
            let num_direct_codes =
3069
51.8k
              s.num_direct_distance_codes - NUM_DISTANCE_SHORT_CODES;
3070
51.8k
            let num_distance_codes = BROTLI_DISTANCE_ALPHABET_SIZE(
3071
51.8k
              s.distance_postfix_bits, num_direct_codes,
3072
51.8k
                (if s.large_window { BROTLI_LARGE_MAX_DISTANCE_BITS } else {
3073
51.2k
                    BROTLI_MAX_DISTANCE_BITS}));
3074
51.8k
            let max_distance_symbol = if s.large_window {
3075
686
                BrotliMaxDistanceSymbol(
3076
686
                    num_direct_codes, s.distance_postfix_bits)
3077
            } else {
3078
51.2k
                num_distance_codes
3079
            };
3080
51.8k
            result =
3081
51.8k
              DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[2]) as usize) <<
3082
51.8k
                               kDistanceContextBits as usize,
3083
51.8k
                               true,
3084
51.8k
                               s,
3085
51.8k
                               local_input);
3086
51.8k
            match result {
3087
50.8k
              BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3088
1.00k
              _ => break,
3089
            }
3090
50.8k
            s.literal_hgroup.init(&mut s.alloc_u32,
3091
50.8k
                                  &mut s.alloc_hc,
3092
                                  kNumLiteralCodes,
3093
                                  kNumLiteralCodes,
3094
50.8k
                                  s.num_literal_htrees as u16);
3095
50.8k
            s.insert_copy_hgroup.init(&mut s.alloc_u32,
3096
50.8k
                                      &mut s.alloc_hc,
3097
                                      kNumInsertAndCopyCodes,
3098
                                      kNumInsertAndCopyCodes,
3099
50.8k
                                      fast!((s.block_type_length_state.num_block_types)[1]) as u16);
3100
50.8k
            s.distance_hgroup.init(&mut s.alloc_u32,
3101
50.8k
                                   &mut s.alloc_hc,
3102
50.8k
                                   num_distance_codes as u16,
3103
50.8k
                                   max_distance_symbol as u16,
3104
50.8k
                                   s.num_dist_htrees as u16);
3105
50.8k
            if (s.literal_hgroup.codes.slice().len() == 0 ||
3106
50.8k
                s.insert_copy_hgroup.codes.slice().len() == 0 ||
3107
50.8k
                s.distance_hgroup.codes.slice().len() == 0) {
3108
0
              return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE);
3109
50.8k
            }
3110
3111
          /*{
3112
            let num_distance_codes: u32 = s.num_direct_distance_codes +
3113
                                          (48u32 << s.distance_postfix_bits);
3114
            result =
3115
              DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[2]) as usize) <<
3116
                               kDistanceContextBits as usize,
3117
                               true,
3118
                               s,
3119
                               local_input);
3120
            match result {
3121
              BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3122
              _ => break,
3123
            }
3124
            s.literal_hgroup.init(&mut s.alloc_u32,
3125
                                  &mut s.alloc_hc,
3126
                                  kNumLiteralCodes,
3127
                                  s.num_literal_htrees as u16);
3128
            s.insert_copy_hgroup.init(&mut s.alloc_u32,
3129
                                      &mut s.alloc_hc,
3130
                                      kNumInsertAndCopyCodes,
3131
                                      fast!((s.block_type_length_state.num_block_types)[1]) as u16);
3132
            s.distance_hgroup.init(&mut s.alloc_u32,
3133
                                   &mut s.alloc_hc,
3134
                                   num_distance_codes as u16,
3135
                                   s.num_dist_htrees as u16);
3136
            if (s.literal_hgroup.codes.slice().len() == 0 ||
3137
                s.insert_copy_hgroup.codes.slice().len() == 0 ||
3138
                s.distance_hgroup.codes.slice().len() == 0) {
3139
              return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS);
3140
            }
3141
          }*/
3142
50.8k
          s.loop_counter = 0;
3143
50.8k
          s.state = BrotliRunningState::BROTLI_STATE_TREE_GROUP;
3144
          // No break, continue to next state
3145
        }
3146
        BrotliRunningState::BROTLI_STATE_TREE_GROUP => {
3147
544k
          result = HuffmanTreeGroupDecode(s.loop_counter, &mut s, local_input);
3148
544k
          match result {
3149
150k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3150
394k
            _ => break,
3151
          }
3152
150k
          s.loop_counter += 1;
3153
150k
          if (s.loop_counter >= 3) {
3154
50.0k
            PrepareLiteralDecoding(s);
3155
50.0k
            s.dist_context_map_slice_index = 0;
3156
50.0k
              /*
3157
50.0k
            s.context_map_slice_index = 0;
3158
50.0k
            let context_mode_index = fast!((s.block_type_length_state.block_type_rb)[1]);
3159
50.0k
            let context_mode = fast_slice!((s.context_modes)[context_mode_index as usize]);
3160
50.0k
            s.context_lookup = &kContextLookup[context_mode as usize & 3];
3161
50.0k
               */
3162
50.0k
            s.htree_command_index = 0;
3163
50.0k
            // look it up each time s.literal_htree=s.literal_hgroup.htrees[s.literal_htree_index];
3164
50.0k
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
3165
100k
          }
3166
150k
          break;
3167
        }
3168
        BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN |
3169
        BrotliRunningState::BROTLI_STATE_COMMAND_INNER |
3170
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS |
3171
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY => {
3172
3.92M
          result = ProcessCommands(s, local_input);
3173
3.92M
          if let BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT = result {
3174
3.44M
            result = SafeProcessCommands(s, local_input)
3175
472k
          }
3176
3.92M
          break;
3177
        }
3178
        BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE |
3179
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1 |
3180
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2 => {
3181
166M
          let (xresult, _) = WriteRingBuffer(&mut available_out,
3182
166M
                                             Some(&mut output),
3183
166M
                                             &mut output_offset,
3184
166M
                                             &mut total_out,
3185
166M
                                             false,
3186
166M
                                             &mut s);
3187
166M
          result = xresult;
3188
166M
          match result {
3189
527k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3190
166M
            _ => break,
3191
          }
3192
527k
          WrapRingBuffer(s);
3193
527k
          if s.ringbuffer_size == 1 << s.window_bits {
3194
527k
            s.max_distance = s.max_backward_distance;
3195
527k
          }
3196
527k
          match s.state {
3197
            BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1 => {
3198
9.38k
              if (s.meta_block_remaining_len <= 0) {
3199
2
                // Next metablock, if any
3200
2
                s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
3201
9.38k
              } else {
3202
9.38k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
3203
9.38k
              }
3204
9.38k
              break;
3205
            }
3206
337k
            BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2 => {
3207
337k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
3208
337k
            }
3209
            _ => {
3210
              // BROTLI_STATE_COMMAND_INNER_WRITE
3211
181k
              if (s.loop_counter == 0) {
3212
16.1k
                if (s.meta_block_remaining_len <= 0) {
3213
828
                  s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
3214
15.2k
                } else {
3215
15.2k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
3216
15.2k
                }
3217
16.1k
                break;
3218
165k
              }
3219
165k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
3220
            }
3221
          }
3222
502k
          break;
3223
        }
3224
        BrotliRunningState::BROTLI_STATE_METABLOCK_DONE => {
3225
65.6k
          s.BrotliStateCleanupAfterMetablock();
3226
65.6k
          if (s.is_last_metablock == 0) {
3227
52.7k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN;
3228
52.7k
            break;
3229
12.8k
          }
3230
12.8k
          if (!bit_reader::BrotliJumpToByteBoundary(&mut s.br)) {
3231
158
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_2;
3232
12.7k
          }
3233
12.8k
          if (s.buffer_length == 0) {
3234
12.8k
            bit_reader::BrotliBitReaderUnload(&mut s.br);
3235
12.8k
            *available_in = s.br.avail_in as usize;
3236
12.8k
            *input_offset = s.br.next_in as usize;
3237
12.8k
          }
3238
12.8k
          s.state = BrotliRunningState::BROTLI_STATE_DONE;
3239
          // No break, continue to next state
3240
        }
3241
        BrotliRunningState::BROTLI_STATE_DONE => {
3242
467k
          if (s.ringbuffer.slice().len() != 0) {
3243
467k
            let (xresult, _) = WriteRingBuffer(&mut available_out,
3244
467k
                                               Some(&mut output),
3245
467k
                                               &mut output_offset,
3246
467k
                                               &mut total_out,
3247
467k
                                               true,
3248
467k
                                               &mut s);
3249
467k
            result = xresult;
3250
467k
            match result {
3251
25.3k
              BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3252
442k
              _ => break,
3253
            }
3254
129
          }
3255
25.4k
          return SaveErrorCode!(s, result);
3256
        }
3257
      }
3258
    }
3259
  }
3260
3261
171M
  SaveErrorCode!(s, result)
3262
171M
}
Unexecuted instantiation: brotli_decompressor::decode::BrotliDecompressStream::<alloc_no_stdlib::stack_allocator::StackAllocator<u8, brotli_decompressor::MemPool<u8>>, alloc_no_stdlib::stack_allocator::StackAllocator<u32, brotli_decompressor::MemPool<u32>>, alloc_no_stdlib::stack_allocator::StackAllocator<brotli_decompressor::huffman::HuffmanCode, brotli_decompressor::MemPool<brotli_decompressor::huffman::HuffmanCode>>>
brotli_decompressor::decode::BrotliDecompressStream::<alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc, alloc_stdlib::std_alloc::StandardAlloc>
Line
Count
Source
2664
171M
pub fn BrotliDecompressStream<AllocU8: alloc::Allocator<u8>,
2665
171M
                              AllocU32: alloc::Allocator<u32>,
2666
171M
                              AllocHC: alloc::Allocator<HuffmanCode>>
2667
171M
  (available_in: &mut usize,
2668
171M
   input_offset: &mut usize,
2669
171M
   xinput: &[u8],
2670
171M
   mut available_out: &mut usize,
2671
171M
   mut output_offset: &mut usize,
2672
171M
   mut output: &mut [u8],
2673
171M
   mut total_out: &mut usize,
2674
171M
   mut s: &mut BrotliState<AllocU8, AllocU32, AllocHC>)
2675
171M
   -> BrotliResult {
2676
2677
171M
  let mut result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2678
2679
171M
  let mut saved_buffer: [u8; 8] = s.buffer;
2680
  let mut local_input: &[u8];
2681
171M
  if is_fatal(s.error_code) {
2682
1.10k
    return BrotliResult::ResultFailure;
2683
171M
  }
2684
171M
  if *available_in as u64 >= (1u64 << 32) {
2685
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2686
171M
  }
2687
171M
  if *input_offset as u64 >= (1u64 << 32) {
2688
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2689
171M
  }
2690
171M
  if *input_offset + *available_in > xinput.len() {
2691
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2692
171M
  }
2693
171M
  if *output_offset + *available_out > output.len() {
2694
0
    return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS);
2695
171M
  }
2696
171M
  if s.buffer_length == 0 {
2697
171M
    local_input = xinput;
2698
171M
    s.br.avail_in = *available_in as u32;
2699
171M
    s.br.next_in = *input_offset as u32;
2700
171M
  } else {
2701
57.8k
    result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2702
57.8k
    let copy_len = core::cmp::min(saved_buffer.len() - s.buffer_length as usize, *available_in);
2703
57.8k
    if copy_len > 0 {
2704
55.2k
      fast_mut!((saved_buffer)[s.buffer_length as usize ; (s.buffer_length as usize + copy_len)])
2705
55.2k
        .clone_from_slice(fast!((xinput)[*input_offset ; copy_len + *input_offset]));
2706
55.2k
      fast_mut!((s.buffer)[s.buffer_length as usize ; (s.buffer_length as usize + copy_len)])
2707
55.2k
        .clone_from_slice(fast!((xinput)[*input_offset ; copy_len + *input_offset]));
2708
55.2k
    }
2709
57.8k
    local_input = &saved_buffer[..];
2710
57.8k
    s.br.next_in = 0;
2711
  }
2712
  loop {
2713
344M
    match result {
2714
172M
      BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2715
      _ => {
2716
171M
        match result {
2717
          BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT => {
2718
4.50M
            if s.ringbuffer.slice().len() != 0 {
2719
4.49M
              let (intermediate_result, _) = WriteRingBuffer(available_out,
2720
4.49M
                                                             Some(&mut output),
2721
4.49M
                                                             &mut output_offset,
2722
4.49M
                                                             &mut total_out,
2723
4.49M
                                                             true,
2724
4.49M
                                                             &mut s);
2725
4.49M
              if is_fatal(intermediate_result) {
2726
92
                result = intermediate_result;
2727
92
                break;
2728
4.49M
              }
2729
14.1k
            }
2730
4.50M
            if s.buffer_length != 0 {
2731
              // Used with internal buffer.
2732
113k
              if s.br.avail_in == 0 {
2733
                // Successfully finished read transaction.
2734
                // Accamulator contains less than 8 bits, because internal buffer
2735
                // is expanded byte-by-byte until it is enough to complete read.
2736
53.4k
                s.buffer_length = 0;
2737
                // Switch to input stream and restart.
2738
53.4k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2739
53.4k
                local_input = xinput;
2740
53.4k
                s.br.avail_in = *available_in as u32;
2741
53.4k
                s.br.next_in = *input_offset as u32;
2742
53.4k
                continue;
2743
59.9k
              } else if *available_in != 0 {
2744
                // Not enough data in buffer, but can take one more byte from
2745
                // input stream.
2746
55.5k
                result = BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS;
2747
55.5k
                let new_byte = fast!((xinput)[*input_offset]);
2748
55.5k
                fast_mut!((s.buffer)[s.buffer_length as usize]) = new_byte;
2749
                // we did the following copy upfront, so we wouldn't have to do it here
2750
                // since saved_buffer[s.buffer_length as usize] = new_byte violates borrow rules
2751
55.5k
                assert_eq!(fast!((saved_buffer)[s.buffer_length as usize]), new_byte);
2752
55.5k
                s.buffer_length += 1;
2753
55.5k
                s.br.avail_in = s.buffer_length;
2754
55.5k
                (*input_offset) += 1;
2755
55.5k
                (*available_in) -= 1;
2756
                // Retry with more data in buffer.
2757
                // we can't re-borrow the saved buffer...so we have to do this recursively
2758
55.5k
                continue;
2759
4.37k
              }
2760
              // Can't finish reading and no more input.
2761
2762
              // FIXME :: NOT SURE WHAT THIS MEANT
2763
              // saved_buffer = core::mem::replace(
2764
              //  &mut s.br.input_,
2765
              //  &mut[]); // clear input
2766
4.37k
              break;
2767
            } else {
2768
              // Input stream doesn't contain enough input.
2769
              // Copy tail to internal buffer and return.
2770
4.39M
              *input_offset = s.br.next_in as usize;
2771
4.39M
              *available_in = s.br.avail_in as usize;
2772
4.44M
              while *available_in != 0 {
2773
54.3k
                fast_mut!((s.buffer)[s.buffer_length as usize]) = fast!((xinput)[*input_offset]);
2774
54.3k
                s.buffer_length += 1;
2775
54.3k
                (*input_offset) += 1;
2776
54.3k
                (*available_in) -= 1;
2777
54.3k
              }
2778
4.39M
              break;
2779
            }
2780
            // unreachable!(); <- dead code
2781
          }
2782
          _ => {
2783
            // Fail or needs more output.
2784
166M
            if s.buffer_length != 0 {
2785
2
              // Just consumed the buffered input and produced some output. Otherwise
2786
2
              // it would result in "needs more input". Reset internal buffer.
2787
2
              s.buffer_length = 0;
2788
166M
            } else {
2789
166M
              // Using input stream in last iteration. When decoder switches to input
2790
166M
              // stream it has less than 8 bits in accamulator, so it is safe to
2791
166M
              // return unused accamulator bits there.
2792
166M
              bit_reader::BrotliBitReaderUnload(&mut s.br);
2793
166M
              *available_in = s.br.avail_in as usize;
2794
166M
              *input_offset = s.br.next_in as usize;
2795
166M
            }
2796
          }
2797
        }
2798
166M
        break;
2799
      }
2800
    }
2801
    loop {
2802
      // this emulates fallthrough behavior
2803
173M
      match s.state {
2804
        BrotliRunningState::BROTLI_STATE_UNINITED => {
2805
          // Prepare to the first read.
2806
30.1k
          if (!bit_reader::BrotliWarmupBitReader(&mut s.br, local_input)) {
2807
12.6k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2808
12.6k
            break;
2809
17.4k
          }
2810
          // Decode window size.
2811
          /* Reads 1..8 bits. */
2812
17.4k
          result = DecodeWindowBits(&mut s.large_window, &mut s.window_bits, &mut s.br);
2813
17.4k
          match result {
2814
17.4k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2815
1
            _ => break,
2816
          }
2817
17.4k
          if s.large_window {
2818
201
              s.state = BrotliRunningState::BROTLI_STATE_LARGE_WINDOW_BITS;
2819
17.2k
          } else {
2820
17.2k
              s.state = BrotliRunningState::BROTLI_STATE_INITIALIZE;
2821
17.2k
          }
2822
        }
2823
        BrotliRunningState::BROTLI_STATE_LARGE_WINDOW_BITS => {
2824
202
          if (!bit_reader::BrotliSafeReadBits(&mut s.br, 6, &mut s.window_bits, local_input)) {
2825
2
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2826
2
            break;
2827
200
          }
2828
200
          if (s.window_bits < kBrotliLargeMinWbits ||
2829
195
              s.window_bits > kBrotliLargeMaxWbits) {
2830
9
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS;
2831
9
            break;
2832
191
          }
2833
191
          s.state = BrotliRunningState::BROTLI_STATE_INITIALIZE;
2834
        }
2835
        BrotliRunningState::BROTLI_STATE_INITIALIZE => {
2836
17.4k
          s.max_backward_distance = (1 << s.window_bits) - kBrotliWindowGap as i32;
2837
17.4k
          s.max_backward_distance_minus_custom_dict_size = (s.max_backward_distance as isize -
2838
17.4k
                                                           s.custom_dict_size) as i32;
2839
2840
          // (formerly) Allocate memory for both block_type_trees and block_len_trees.
2841
17.4k
          s.block_type_length_state.block_type_trees = s.alloc_hc
2842
17.4k
            .alloc_cell(3 * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as usize);
2843
17.4k
          if (s.block_type_length_state.block_type_trees.slice().len() == 0) {
2844
0
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES;
2845
0
            break;
2846
17.4k
          }
2847
17.4k
          s.block_type_length_state.block_len_trees = s.alloc_hc
2848
17.4k
            .alloc_cell(3 * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as usize);
2849
2850
17.4k
          s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN;
2851
          // No break, continue to next state
2852
        }
2853
70.2k
        BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN => {
2854
70.2k
          s.BrotliStateMetablockBegin();
2855
70.2k
          BROTLI_LOG_UINT!(s.pos);
2856
70.2k
          s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER;
2857
70.2k
          // No break, continue to next state
2858
70.2k
        }
2859
        BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER => {
2860
84.4k
          result = DecodeMetaBlockLength(&mut s, local_input); // Reads 2 - 31 bits.
2861
84.4k
          match result {
2862
69.9k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2863
14.4k
            _ => break,
2864
          }
2865
          BROTLI_LOG_UINT!(s.is_last_metablock);
2866
          BROTLI_LOG_UINT!(s.meta_block_remaining_len);
2867
          BROTLI_LOG_UINT!(s.is_metadata);
2868
          BROTLI_LOG_UINT!(s.is_uncompressed);
2869
69.9k
          if (s.is_metadata != 0 || s.is_uncompressed != 0) &&
2870
14.1k
             !bit_reader::BrotliJumpToByteBoundary(&mut s.br) {
2871
34
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_2;
2872
34
            break;
2873
69.9k
          }
2874
69.9k
          if s.is_metadata != 0 {
2875
1.68k
            s.state = BrotliRunningState::BROTLI_STATE_METADATA;
2876
1.68k
            break;
2877
68.2k
          }
2878
68.2k
          if s.meta_block_remaining_len == 0 {
2879
4.00k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2880
4.00k
            break;
2881
64.2k
          }
2882
64.2k
          if s.ringbuffer.slice().len() == 0 && !BrotliAllocateRingBuffer(&mut s, local_input) {
2883
0
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2;
2884
0
            break;
2885
64.2k
          }
2886
64.2k
          if s.is_uncompressed != 0 {
2887
12.4k
            s.state = BrotliRunningState::BROTLI_STATE_UNCOMPRESSED;
2888
12.4k
            break;
2889
51.8k
          }
2890
51.8k
          s.loop_counter = 0;
2891
51.8k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0;
2892
51.8k
          break;
2893
        }
2894
        BrotliRunningState::BROTLI_STATE_UNCOMPRESSED => {
2895
699k
          let mut _bytes_copied = s.meta_block_remaining_len;
2896
699k
          result = CopyUncompressedBlockToOutput(&mut available_out,
2897
699k
                                                 &mut output,
2898
699k
                                                 &mut output_offset,
2899
699k
                                                 &mut total_out,
2900
699k
                                                 &mut s,
2901
699k
                                                 local_input);
2902
699k
          _bytes_copied -= s.meta_block_remaining_len;
2903
699k
          match result {
2904
11.9k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2905
687k
            _ => break,
2906
          }
2907
11.9k
          s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
2908
11.9k
          break;
2909
        }
2910
        BrotliRunningState::BROTLI_STATE_METADATA => {
2911
4.17M
          while s.meta_block_remaining_len > 0 {
2912
4.17M
            let mut bits = 0u32;
2913
            // Read one byte and ignore it.
2914
4.17M
            if !bit_reader::BrotliSafeReadBits(&mut s.br, 8, &mut bits, local_input) {
2915
112
              result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
2916
112
              break;
2917
4.17M
            }
2918
4.17M
            s.meta_block_remaining_len -= 1;
2919
          }
2920
1.74k
          if let BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS = result {
2921
1.63k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE
2922
112
          }
2923
1.74k
          break;
2924
        }
2925
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0 => {
2926
207k
          if s.loop_counter >= 3 {
2927
51.1k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER_2;
2928
51.1k
            break;
2929
156k
          }
2930
          // Reads 1..11 bits.
2931
156k
          {
2932
156k
            let index = s.loop_counter as usize;
2933
156k
            result =
2934
156k
              DecodeVarLenUint8(&mut s.substate_decode_uint8,
2935
156k
                                &mut s.br,
2936
156k
                                &mut fast_mut!((s.block_type_length_state.num_block_types)[index]),
2937
156k
                                local_input);
2938
156k
          }
2939
156k
          match result {
2940
154k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2941
1.78k
            _ => break,
2942
          }
2943
154k
          fast_mut!((s.block_type_length_state.num_block_types)[s.loop_counter as usize]) += 1;
2944
          BROTLI_LOG_UINT!(s.block_type_length_state.num_block_types[s.loop_counter as usize]);
2945
154k
          if fast!((s.block_type_length_state.num_block_types)[s.loop_counter as usize]) < 2 {
2946
145k
            s.loop_counter += 1;
2947
145k
            break;
2948
9.14k
          }
2949
9.14k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_1;
2950
          // No break, continue to next state
2951
        }
2952
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_1 => {
2953
9.67k
          let tree_offset = s.loop_counter as u32 * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as u32;
2954
9.67k
          let mut new_huffman_table = mem::replace(&mut s.block_type_length_state.block_type_trees,
2955
9.67k
                                                   AllocHC::AllocatedMemory::default());
2956
9.67k
          let loop_counter = s.loop_counter as usize;
2957
9.67k
          let alphabet_size = fast!((s.block_type_length_state.num_block_types)[loop_counter]) + 2;
2958
9.67k
          result =
2959
9.67k
            ReadHuffmanCode(alphabet_size, alphabet_size,
2960
9.67k
                            new_huffman_table.slice_mut(),
2961
9.67k
                            tree_offset as usize,
2962
9.67k
                            None,
2963
9.67k
                            &mut s,
2964
9.67k
                            local_input);
2965
9.67k
          let _ = mem::replace(&mut s.block_type_length_state.block_type_trees,
2966
9.67k
                       new_huffman_table);
2967
9.67k
          match result {
2968
8.76k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2969
913
            _ => break,
2970
          }
2971
8.76k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_2;
2972
          // No break, continue to next state
2973
        }
2974
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_2 => {
2975
9.56k
          let tree_offset = s.loop_counter * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as i32;
2976
9.56k
          let mut new_huffman_table = mem::replace(&mut s.block_type_length_state.block_len_trees,
2977
9.56k
                                                   AllocHC::AllocatedMemory::default());
2978
9.56k
          result = ReadHuffmanCode(kNumBlockLengthCodes, kNumBlockLengthCodes,
2979
9.56k
                                   new_huffman_table.slice_mut(),
2980
9.56k
                                   tree_offset as usize,
2981
9.56k
                                   None,
2982
9.56k
                                   &mut s,
2983
9.56k
                                   local_input);
2984
9.56k
          let _ = mem::replace(&mut s.block_type_length_state.block_len_trees,
2985
9.56k
                       new_huffman_table);
2986
9.56k
          match result {
2987
8.60k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
2988
952
            _ => break,
2989
          }
2990
8.60k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_3;
2991
          // No break, continue to next state
2992
        }
2993
        BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_3 => {
2994
8.80k
          let tree_offset = s.loop_counter * huffman::BROTLI_HUFFMAN_MAX_TABLE_SIZE as i32;
2995
2996
8.80k
          let mut block_length_out: u32 = 0;
2997
          let ind_ret: (bool, u32);
2998
          
2999
8.80k
          ind_ret = SafeReadBlockLengthIndex(&s.block_type_length_state.substate_read_block_length,
3000
8.80k
                                             s.block_type_length_state.block_length_index,
3001
8.80k
                                             fast_slice!((s.block_type_length_state.block_len_trees)
3002
8.80k
                                                           [tree_offset as usize;]),
3003
8.80k
                                             &mut s.br, local_input);
3004
3005
8.80k
          if !SafeReadBlockLengthFromIndex(&mut s.block_type_length_state,
3006
8.80k
                                           &mut s.br,
3007
8.80k
                                           &mut block_length_out,
3008
8.80k
                                           ind_ret,
3009
8.80k
                                           local_input) {
3010
232
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
3011
232
            break;
3012
8.57k
          }
3013
8.57k
          fast_mut!((s.block_type_length_state.block_length)[s.loop_counter as usize]) =
3014
8.57k
            block_length_out;
3015
          BROTLI_LOG_UINT!(s.block_type_length_state.block_length[s.loop_counter as usize]);
3016
8.57k
          s.loop_counter += 1;
3017
8.57k
          s.state = BrotliRunningState::BROTLI_STATE_HUFFMAN_CODE_0;
3018
8.57k
          break;
3019
        }
3020
        BrotliRunningState::BROTLI_STATE_METABLOCK_HEADER_2 => {
3021
54.0k
          let mut bits: u32 = 0;
3022
54.0k
          if (!bit_reader::BrotliSafeReadBits(&mut s.br, 6, &mut bits, local_input)) {
3023
2.88k
            result = BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT;
3024
2.88k
            break;
3025
51.1k
          }
3026
51.1k
          s.distance_postfix_bits = bits & bit_reader::BitMask(2);
3027
51.1k
          bits >>= 2;
3028
51.1k
          s.num_direct_distance_codes = NUM_DISTANCE_SHORT_CODES +
3029
51.1k
                                        (bits << s.distance_postfix_bits);
3030
          BROTLI_LOG_UINT!(s.num_direct_distance_codes);
3031
          BROTLI_LOG_UINT!(s.distance_postfix_bits);
3032
51.1k
          s.distance_postfix_mask = bit_reader::BitMask(s.distance_postfix_bits) as i32;
3033
51.1k
          s.context_modes = s.alloc_u8
3034
51.1k
            .alloc_cell(fast!((s.block_type_length_state.num_block_types)[0]) as usize);
3035
51.1k
          if (s.context_modes.slice().len() == 0) {
3036
0
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES;
3037
0
            break;
3038
51.1k
          }
3039
51.1k
          s.loop_counter = 0;
3040
51.1k
          s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MODES;
3041
          // No break, continue to next state
3042
        }
3043
        BrotliRunningState::BROTLI_STATE_CONTEXT_MODES => {
3044
52.1k
          result = ReadContextModes(&mut s, local_input);
3045
52.1k
          match result {
3046
51.1k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3047
1.01k
            _ => break,
3048
          }
3049
51.1k
          s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1;
3050
          // No break, continue to next state
3051
        }
3052
        BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_1 => {
3053
67.1k
          result =
3054
67.1k
            DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[0]) as usize) <<
3055
67.1k
                             kLiteralContextBits as usize,
3056
67.1k
                             false,
3057
67.1k
                             &mut s,
3058
67.1k
                             local_input);
3059
67.1k
          match result {
3060
51.0k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3061
16.1k
            _ => break,
3062
          }
3063
51.0k
          DetectTrivialLiteralBlockTypes(s);
3064
51.0k
          s.state = BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2;
3065
          // No break, continue to next state
3066
        }
3067
        BrotliRunningState::BROTLI_STATE_CONTEXT_MAP_2 => {
3068
51.8k
            let num_direct_codes =
3069
51.8k
              s.num_direct_distance_codes - NUM_DISTANCE_SHORT_CODES;
3070
51.8k
            let num_distance_codes = BROTLI_DISTANCE_ALPHABET_SIZE(
3071
51.8k
              s.distance_postfix_bits, num_direct_codes,
3072
51.8k
                (if s.large_window { BROTLI_LARGE_MAX_DISTANCE_BITS } else {
3073
51.2k
                    BROTLI_MAX_DISTANCE_BITS}));
3074
51.8k
            let max_distance_symbol = if s.large_window {
3075
686
                BrotliMaxDistanceSymbol(
3076
686
                    num_direct_codes, s.distance_postfix_bits)
3077
            } else {
3078
51.2k
                num_distance_codes
3079
            };
3080
51.8k
            result =
3081
51.8k
              DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[2]) as usize) <<
3082
51.8k
                               kDistanceContextBits as usize,
3083
51.8k
                               true,
3084
51.8k
                               s,
3085
51.8k
                               local_input);
3086
51.8k
            match result {
3087
50.8k
              BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3088
1.00k
              _ => break,
3089
            }
3090
50.8k
            s.literal_hgroup.init(&mut s.alloc_u32,
3091
50.8k
                                  &mut s.alloc_hc,
3092
                                  kNumLiteralCodes,
3093
                                  kNumLiteralCodes,
3094
50.8k
                                  s.num_literal_htrees as u16);
3095
50.8k
            s.insert_copy_hgroup.init(&mut s.alloc_u32,
3096
50.8k
                                      &mut s.alloc_hc,
3097
                                      kNumInsertAndCopyCodes,
3098
                                      kNumInsertAndCopyCodes,
3099
50.8k
                                      fast!((s.block_type_length_state.num_block_types)[1]) as u16);
3100
50.8k
            s.distance_hgroup.init(&mut s.alloc_u32,
3101
50.8k
                                   &mut s.alloc_hc,
3102
50.8k
                                   num_distance_codes as u16,
3103
50.8k
                                   max_distance_symbol as u16,
3104
50.8k
                                   s.num_dist_htrees as u16);
3105
50.8k
            if (s.literal_hgroup.codes.slice().len() == 0 ||
3106
50.8k
                s.insert_copy_hgroup.codes.slice().len() == 0 ||
3107
50.8k
                s.distance_hgroup.codes.slice().len() == 0) {
3108
0
              return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE);
3109
50.8k
            }
3110
3111
          /*{
3112
            let num_distance_codes: u32 = s.num_direct_distance_codes +
3113
                                          (48u32 << s.distance_postfix_bits);
3114
            result =
3115
              DecodeContextMap((fast!((s.block_type_length_state.num_block_types)[2]) as usize) <<
3116
                               kDistanceContextBits as usize,
3117
                               true,
3118
                               s,
3119
                               local_input);
3120
            match result {
3121
              BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3122
              _ => break,
3123
            }
3124
            s.literal_hgroup.init(&mut s.alloc_u32,
3125
                                  &mut s.alloc_hc,
3126
                                  kNumLiteralCodes,
3127
                                  s.num_literal_htrees as u16);
3128
            s.insert_copy_hgroup.init(&mut s.alloc_u32,
3129
                                      &mut s.alloc_hc,
3130
                                      kNumInsertAndCopyCodes,
3131
                                      fast!((s.block_type_length_state.num_block_types)[1]) as u16);
3132
            s.distance_hgroup.init(&mut s.alloc_u32,
3133
                                   &mut s.alloc_hc,
3134
                                   num_distance_codes as u16,
3135
                                   s.num_dist_htrees as u16);
3136
            if (s.literal_hgroup.codes.slice().len() == 0 ||
3137
                s.insert_copy_hgroup.codes.slice().len() == 0 ||
3138
                s.distance_hgroup.codes.slice().len() == 0) {
3139
              return SaveErrorCode!(s, BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS);
3140
            }
3141
          }*/
3142
50.8k
          s.loop_counter = 0;
3143
50.8k
          s.state = BrotliRunningState::BROTLI_STATE_TREE_GROUP;
3144
          // No break, continue to next state
3145
        }
3146
        BrotliRunningState::BROTLI_STATE_TREE_GROUP => {
3147
544k
          result = HuffmanTreeGroupDecode(s.loop_counter, &mut s, local_input);
3148
544k
          match result {
3149
150k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3150
394k
            _ => break,
3151
          }
3152
150k
          s.loop_counter += 1;
3153
150k
          if (s.loop_counter >= 3) {
3154
50.0k
            PrepareLiteralDecoding(s);
3155
50.0k
            s.dist_context_map_slice_index = 0;
3156
50.0k
              /*
3157
50.0k
            s.context_map_slice_index = 0;
3158
50.0k
            let context_mode_index = fast!((s.block_type_length_state.block_type_rb)[1]);
3159
50.0k
            let context_mode = fast_slice!((s.context_modes)[context_mode_index as usize]);
3160
50.0k
            s.context_lookup = &kContextLookup[context_mode as usize & 3];
3161
50.0k
               */
3162
50.0k
            s.htree_command_index = 0;
3163
50.0k
            // look it up each time s.literal_htree=s.literal_hgroup.htrees[s.literal_htree_index];
3164
50.0k
            s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
3165
100k
          }
3166
150k
          break;
3167
        }
3168
        BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN |
3169
        BrotliRunningState::BROTLI_STATE_COMMAND_INNER |
3170
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS |
3171
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY => {
3172
3.92M
          result = ProcessCommands(s, local_input);
3173
3.92M
          if let BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT = result {
3174
3.44M
            result = SafeProcessCommands(s, local_input)
3175
472k
          }
3176
3.92M
          break;
3177
        }
3178
        BrotliRunningState::BROTLI_STATE_COMMAND_INNER_WRITE |
3179
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1 |
3180
        BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2 => {
3181
166M
          let (xresult, _) = WriteRingBuffer(&mut available_out,
3182
166M
                                             Some(&mut output),
3183
166M
                                             &mut output_offset,
3184
166M
                                             &mut total_out,
3185
166M
                                             false,
3186
166M
                                             &mut s);
3187
166M
          result = xresult;
3188
166M
          match result {
3189
527k
            BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3190
166M
            _ => break,
3191
          }
3192
527k
          WrapRingBuffer(s);
3193
527k
          if s.ringbuffer_size == 1 << s.window_bits {
3194
527k
            s.max_distance = s.max_backward_distance;
3195
527k
          }
3196
527k
          match s.state {
3197
            BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_1 => {
3198
9.38k
              if (s.meta_block_remaining_len <= 0) {
3199
2
                // Next metablock, if any
3200
2
                s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
3201
9.38k
              } else {
3202
9.38k
                s.state = BrotliRunningState::BROTLI_STATE_COMMAND_BEGIN;
3203
9.38k
              }
3204
9.38k
              break;
3205
            }
3206
337k
            BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRITE_2 => {
3207
337k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_WRAP_COPY;
3208
337k
            }
3209
            _ => {
3210
              // BROTLI_STATE_COMMAND_INNER_WRITE
3211
181k
              if (s.loop_counter == 0) {
3212
16.1k
                if (s.meta_block_remaining_len <= 0) {
3213
828
                  s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_DONE;
3214
15.2k
                } else {
3215
15.2k
                  s.state = BrotliRunningState::BROTLI_STATE_COMMAND_POST_DECODE_LITERALS;
3216
15.2k
                }
3217
16.1k
                break;
3218
165k
              }
3219
165k
              s.state = BrotliRunningState::BROTLI_STATE_COMMAND_INNER;
3220
            }
3221
          }
3222
502k
          break;
3223
        }
3224
        BrotliRunningState::BROTLI_STATE_METABLOCK_DONE => {
3225
65.6k
          s.BrotliStateCleanupAfterMetablock();
3226
65.6k
          if (s.is_last_metablock == 0) {
3227
52.7k
            s.state = BrotliRunningState::BROTLI_STATE_METABLOCK_BEGIN;
3228
52.7k
            break;
3229
12.8k
          }
3230
12.8k
          if (!bit_reader::BrotliJumpToByteBoundary(&mut s.br)) {
3231
158
            result = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_2;
3232
12.7k
          }
3233
12.8k
          if (s.buffer_length == 0) {
3234
12.8k
            bit_reader::BrotliBitReaderUnload(&mut s.br);
3235
12.8k
            *available_in = s.br.avail_in as usize;
3236
12.8k
            *input_offset = s.br.next_in as usize;
3237
12.8k
          }
3238
12.8k
          s.state = BrotliRunningState::BROTLI_STATE_DONE;
3239
          // No break, continue to next state
3240
        }
3241
        BrotliRunningState::BROTLI_STATE_DONE => {
3242
467k
          if (s.ringbuffer.slice().len() != 0) {
3243
467k
            let (xresult, _) = WriteRingBuffer(&mut available_out,
3244
467k
                                               Some(&mut output),
3245
467k
                                               &mut output_offset,
3246
467k
                                               &mut total_out,
3247
467k
                                               true,
3248
467k
                                               &mut s);
3249
467k
            result = xresult;
3250
467k
            match result {
3251
25.3k
              BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => {}
3252
442k
              _ => break,
3253
            }
3254
129
          }
3255
25.4k
          return SaveErrorCode!(s, result);
3256
        }
3257
      }
3258
    }
3259
  }
3260
3261
171M
  SaveErrorCode!(s, result)
3262
171M
}
3263