Coverage Report

Created: 2025-06-12 06:11

/src/brunsli/_deps/brotli-src/c/dec/state.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright 2015 Google Inc. All Rights Reserved.
2
3
   Distributed under MIT license.
4
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
5
*/
6
7
#include "./state.h"
8
9
#include <stdlib.h>  /* free, malloc */
10
11
#include <brotli/types.h>
12
#include "./huffman.h"
13
14
#if defined(__cplusplus) || defined(c_plusplus)
15
extern "C" {
16
#endif
17
18
BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
19
7.57k
    brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
20
7.57k
  if (!alloc_func) {
21
7.57k
    s->alloc_func = BrotliDefaultAllocFunc;
22
7.57k
    s->free_func = BrotliDefaultFreeFunc;
23
7.57k
    s->memory_manager_opaque = 0;
24
7.57k
  } else {
25
0
    s->alloc_func = alloc_func;
26
0
    s->free_func = free_func;
27
0
    s->memory_manager_opaque = opaque;
28
0
  }
29
30
7.57k
  s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
31
32
7.57k
  BrotliInitBitReader(&s->br);
33
7.57k
  s->state = BROTLI_STATE_UNINITED;
34
7.57k
  s->large_window = 0;
35
7.57k
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
36
7.57k
  s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
37
7.57k
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
38
7.57k
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
39
40
7.57k
  s->buffer_length = 0;
41
7.57k
  s->loop_counter = 0;
42
7.57k
  s->pos = 0;
43
7.57k
  s->rb_roundtrips = 0;
44
7.57k
  s->partial_pos_out = 0;
45
46
7.57k
  s->block_type_trees = NULL;
47
7.57k
  s->block_len_trees = NULL;
48
7.57k
  s->ringbuffer = NULL;
49
7.57k
  s->ringbuffer_size = 0;
50
7.57k
  s->new_ringbuffer_size = 0;
51
7.57k
  s->ringbuffer_mask = 0;
52
53
7.57k
  s->context_map = NULL;
54
7.57k
  s->context_modes = NULL;
55
7.57k
  s->dist_context_map = NULL;
56
7.57k
  s->context_map_slice = NULL;
57
7.57k
  s->dist_context_map_slice = NULL;
58
59
7.57k
  s->literal_hgroup.codes = NULL;
60
7.57k
  s->literal_hgroup.htrees = NULL;
61
7.57k
  s->insert_copy_hgroup.codes = NULL;
62
7.57k
  s->insert_copy_hgroup.htrees = NULL;
63
7.57k
  s->distance_hgroup.codes = NULL;
64
7.57k
  s->distance_hgroup.htrees = NULL;
65
66
7.57k
  s->is_last_metablock = 0;
67
7.57k
  s->is_uncompressed = 0;
68
7.57k
  s->is_metadata = 0;
69
7.57k
  s->should_wrap_ringbuffer = 0;
70
7.57k
  s->canny_ringbuffer_allocation = 1;
71
72
7.57k
  s->window_bits = 0;
73
7.57k
  s->max_distance = 0;
74
7.57k
  s->dist_rb[0] = 16;
75
7.57k
  s->dist_rb[1] = 15;
76
7.57k
  s->dist_rb[2] = 11;
77
7.57k
  s->dist_rb[3] = 4;
78
7.57k
  s->dist_rb_idx = 0;
79
7.57k
  s->block_type_trees = NULL;
80
7.57k
  s->block_len_trees = NULL;
81
82
7.57k
  s->mtf_upper_bound = 63;
83
84
7.57k
  s->dictionary = BrotliGetDictionary();
85
7.57k
  s->transforms = BrotliGetTransforms();
86
87
7.57k
  return BROTLI_TRUE;
88
7.57k
}
89
90
25.2k
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
91
25.2k
  s->meta_block_remaining_len = 0;
92
25.2k
  s->block_length[0] = 1U << 24;
93
25.2k
  s->block_length[1] = 1U << 24;
94
25.2k
  s->block_length[2] = 1U << 24;
95
25.2k
  s->num_block_types[0] = 1;
96
25.2k
  s->num_block_types[1] = 1;
97
25.2k
  s->num_block_types[2] = 1;
98
25.2k
  s->block_type_rb[0] = 1;
99
25.2k
  s->block_type_rb[1] = 0;
100
25.2k
  s->block_type_rb[2] = 1;
101
25.2k
  s->block_type_rb[3] = 0;
102
25.2k
  s->block_type_rb[4] = 1;
103
25.2k
  s->block_type_rb[5] = 0;
104
25.2k
  s->context_map = NULL;
105
25.2k
  s->context_modes = NULL;
106
25.2k
  s->dist_context_map = NULL;
107
25.2k
  s->context_map_slice = NULL;
108
25.2k
  s->literal_htree = NULL;
109
25.2k
  s->dist_context_map_slice = NULL;
110
25.2k
  s->dist_htree_index = 0;
111
25.2k
  s->context_lookup = NULL;
112
25.2k
  s->literal_hgroup.codes = NULL;
113
25.2k
  s->literal_hgroup.htrees = NULL;
114
25.2k
  s->insert_copy_hgroup.codes = NULL;
115
25.2k
  s->insert_copy_hgroup.htrees = NULL;
116
25.2k
  s->distance_hgroup.codes = NULL;
117
25.2k
  s->distance_hgroup.htrees = NULL;
118
25.2k
}
119
120
26.0k
void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
121
26.0k
  BROTLI_DECODER_FREE(s, s->context_modes);
122
26.0k
  BROTLI_DECODER_FREE(s, s->context_map);
123
26.0k
  BROTLI_DECODER_FREE(s, s->dist_context_map);
124
26.0k
  BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
125
26.0k
  BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
126
26.0k
  BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
127
26.0k
}
128
129
7.57k
void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
130
7.57k
  BrotliDecoderStateCleanupAfterMetablock(s);
131
132
7.57k
  BROTLI_DECODER_FREE(s, s->ringbuffer);
133
7.57k
  BROTLI_DECODER_FREE(s, s->block_type_trees);
134
7.57k
}
135
136
BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
137
    HuffmanTreeGroup* group, uint32_t alphabet_size_max,
138
48.1k
    uint32_t alphabet_size_limit, uint32_t ntrees) {
139
  /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
140
     This number is discovered "unlimited" "enough" calculator; it is actually
141
     a wee bigger than required in several cases (especially for alphabets with
142
     less than 16 symbols). */
143
48.1k
  const size_t max_table_size = alphabet_size_limit + 376;
144
48.1k
  const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
145
48.1k
  const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
146
  /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
147
48.1k
  HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
148
48.1k
      code_size + htree_size);
149
48.1k
  group->alphabet_size_max = (uint16_t)alphabet_size_max;
150
48.1k
  group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
151
48.1k
  group->num_htrees = (uint16_t)ntrees;
152
48.1k
  group->htrees = p;
153
48.1k
  group->codes = (HuffmanCode*)(&p[ntrees]);
154
48.1k
  return !!p;
155
48.1k
}
156
157
#if defined(__cplusplus) || defined(c_plusplus)
158
}  /* extern "C" */
159
#endif