Coverage Report

Created: 2025-07-11 06:32

/src/brotli/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
13
#include "../common/dictionary.h"
14
#include "huffman.h"
15
16
#if defined(__cplusplus) || defined(c_plusplus)
17
extern "C" {
18
#endif
19
20
#ifdef BROTLI_REPORTING
21
/* When BROTLI_REPORTING is defined extra reporting module have to be linked. */
22
void BrotliDecoderOnStart(const BrotliDecoderState* s);
23
void BrotliDecoderOnFinish(const BrotliDecoderState* s);
24
#define BROTLI_DECODER_ON_START(s) BrotliDecoderOnStart(s);
25
#define BROTLI_DECODER_ON_FINISH(s) BrotliDecoderOnFinish(s);
26
#else
27
#if !defined(BROTLI_DECODER_ON_START)
28
4.59k
#define BROTLI_DECODER_ON_START(s) (void)(s);
29
#endif
30
#if !defined(BROTLI_DECODER_ON_FINISH)
31
4.59k
#define BROTLI_DECODER_ON_FINISH(s) (void)(s);
32
#endif
33
#endif
34
35
BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
36
4.59k
    brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
37
4.59k
  BROTLI_DECODER_ON_START(s);
38
4.59k
  if (!alloc_func) {
39
4.59k
    s->alloc_func = BrotliDefaultAllocFunc;
40
4.59k
    s->free_func = BrotliDefaultFreeFunc;
41
4.59k
    s->memory_manager_opaque = 0;
42
4.59k
  } else {
43
0
    s->alloc_func = alloc_func;
44
0
    s->free_func = free_func;
45
0
    s->memory_manager_opaque = opaque;
46
0
  }
47
48
4.59k
  s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
49
50
4.59k
  BrotliInitBitReader(&s->br);
51
4.59k
  s->state = BROTLI_STATE_UNINITED;
52
4.59k
  s->large_window = 0;
53
4.59k
  s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
54
4.59k
  s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
55
4.59k
  s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
56
4.59k
  s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
57
58
4.59k
  s->buffer_length = 0;
59
4.59k
  s->loop_counter = 0;
60
4.59k
  s->pos = 0;
61
4.59k
  s->rb_roundtrips = 0;
62
4.59k
  s->partial_pos_out = 0;
63
4.59k
  s->used_input = 0;
64
65
4.59k
  s->block_type_trees = NULL;
66
4.59k
  s->block_len_trees = NULL;
67
4.59k
  s->ringbuffer = NULL;
68
4.59k
  s->ringbuffer_size = 0;
69
4.59k
  s->new_ringbuffer_size = 0;
70
4.59k
  s->ringbuffer_mask = 0;
71
72
4.59k
  s->context_map = NULL;
73
4.59k
  s->context_modes = NULL;
74
4.59k
  s->dist_context_map = NULL;
75
4.59k
  s->context_map_slice = NULL;
76
4.59k
  s->dist_context_map_slice = NULL;
77
78
4.59k
  s->literal_hgroup.codes = NULL;
79
4.59k
  s->literal_hgroup.htrees = NULL;
80
4.59k
  s->insert_copy_hgroup.codes = NULL;
81
4.59k
  s->insert_copy_hgroup.htrees = NULL;
82
4.59k
  s->distance_hgroup.codes = NULL;
83
4.59k
  s->distance_hgroup.htrees = NULL;
84
85
4.59k
  s->is_last_metablock = 0;
86
4.59k
  s->is_uncompressed = 0;
87
4.59k
  s->is_metadata = 0;
88
4.59k
  s->should_wrap_ringbuffer = 0;
89
4.59k
  s->canny_ringbuffer_allocation = 1;
90
91
4.59k
  s->window_bits = 0;
92
4.59k
  s->max_distance = 0;
93
4.59k
  s->dist_rb[0] = 16;
94
4.59k
  s->dist_rb[1] = 15;
95
4.59k
  s->dist_rb[2] = 11;
96
4.59k
  s->dist_rb[3] = 4;
97
4.59k
  s->dist_rb_idx = 0;
98
4.59k
  s->block_type_trees = NULL;
99
4.59k
  s->block_len_trees = NULL;
100
101
4.59k
  s->mtf_upper_bound = 63;
102
103
4.59k
  s->compound_dictionary = NULL;
104
4.59k
  s->dictionary =
105
4.59k
      BrotliSharedDictionaryCreateInstance(alloc_func, free_func, opaque);
106
4.59k
  if (!s->dictionary) return BROTLI_FALSE;
107
108
4.59k
  s->metadata_start_func = NULL;
109
4.59k
  s->metadata_chunk_func = NULL;
110
4.59k
  s->metadata_callback_opaque = 0;
111
112
4.59k
  return BROTLI_TRUE;
113
4.59k
}
114
115
20.1k
void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
116
20.1k
  s->meta_block_remaining_len = 0;
117
20.1k
  s->block_length[0] = BROTLI_BLOCK_SIZE_CAP;
118
20.1k
  s->block_length[1] = BROTLI_BLOCK_SIZE_CAP;
119
20.1k
  s->block_length[2] = BROTLI_BLOCK_SIZE_CAP;
120
20.1k
  s->num_block_types[0] = 1;
121
20.1k
  s->num_block_types[1] = 1;
122
20.1k
  s->num_block_types[2] = 1;
123
20.1k
  s->block_type_rb[0] = 1;
124
20.1k
  s->block_type_rb[1] = 0;
125
20.1k
  s->block_type_rb[2] = 1;
126
20.1k
  s->block_type_rb[3] = 0;
127
20.1k
  s->block_type_rb[4] = 1;
128
20.1k
  s->block_type_rb[5] = 0;
129
20.1k
  s->context_map = NULL;
130
20.1k
  s->context_modes = NULL;
131
20.1k
  s->dist_context_map = NULL;
132
20.1k
  s->context_map_slice = NULL;
133
20.1k
  s->literal_htree = NULL;
134
20.1k
  s->dist_context_map_slice = NULL;
135
20.1k
  s->dist_htree_index = 0;
136
20.1k
  s->context_lookup = NULL;
137
20.1k
  s->literal_hgroup.codes = NULL;
138
20.1k
  s->literal_hgroup.htrees = NULL;
139
20.1k
  s->insert_copy_hgroup.codes = NULL;
140
20.1k
  s->insert_copy_hgroup.htrees = NULL;
141
20.1k
  s->distance_hgroup.codes = NULL;
142
20.1k
  s->distance_hgroup.htrees = NULL;
143
20.1k
}
144
145
20.2k
void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
146
20.2k
  BROTLI_DECODER_FREE(s, s->context_modes);
147
20.2k
  BROTLI_DECODER_FREE(s, s->context_map);
148
20.2k
  BROTLI_DECODER_FREE(s, s->dist_context_map);
149
20.2k
  BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
150
20.2k
  BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
151
20.2k
  BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
152
20.2k
}
153
154
4.59k
void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
155
4.59k
  BrotliDecoderStateCleanupAfterMetablock(s);
156
157
4.59k
  BROTLI_DECODER_ON_FINISH(s);
158
159
4.59k
  BROTLI_DECODER_FREE(s, s->compound_dictionary);
160
4.59k
  BrotliSharedDictionaryDestroyInstance(s->dictionary);
161
4.59k
  s->dictionary = NULL;
162
4.59k
  BROTLI_DECODER_FREE(s, s->ringbuffer);
163
4.59k
  BROTLI_DECODER_FREE(s, s->block_type_trees);
164
4.59k
}
165
166
BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
167
    HuffmanTreeGroup* group, brotli_reg_t alphabet_size_max,
168
36.0k
    brotli_reg_t alphabet_size_limit, brotli_reg_t ntrees) {
169
  /* 376 = 256 (1-st level table) + 4 + 7 + 15 + 31 + 63 (2-nd level mix-tables)
170
     This number is discovered "unlimited" "enough" calculator; it is actually
171
     a wee bigger than required in several cases (especially for alphabets with
172
     less than 16 symbols). */
173
36.0k
  const size_t max_table_size = alphabet_size_limit + 376;
174
36.0k
  const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
175
36.0k
  const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
176
  /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
177
36.0k
  HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
178
36.0k
      code_size + htree_size);
179
36.0k
  group->alphabet_size_max = (uint16_t)alphabet_size_max;
180
36.0k
  group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
181
36.0k
  group->num_htrees = (uint16_t)ntrees;
182
36.0k
  group->htrees = p;
183
36.0k
  group->codes = p ? (HuffmanCode*)(&p[ntrees]) : NULL;
184
36.0k
  return !!p;
185
36.0k
}
186
187
#if defined(__cplusplus) || defined(c_plusplus)
188
}  /* extern "C" */
189
#endif