comparison mupdf-source/thirdparty/brotli/c/dec/state.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
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 #define BROTLI_DECODER_ON_START(s) (void)(s);
29 #endif
30 #if !defined(BROTLI_DECODER_ON_FINISH)
31 #define BROTLI_DECODER_ON_FINISH(s) (void)(s);
32 #endif
33 #endif
34
35 BROTLI_BOOL BrotliDecoderStateInit(BrotliDecoderState* s,
36 brotli_alloc_func alloc_func, brotli_free_func free_func, void* opaque) {
37 BROTLI_DECODER_ON_START(s);
38 if (!alloc_func) {
39 s->alloc_func = BrotliDefaultAllocFunc;
40 s->free_func = BrotliDefaultFreeFunc;
41 s->memory_manager_opaque = 0;
42 } else {
43 s->alloc_func = alloc_func;
44 s->free_func = free_func;
45 s->memory_manager_opaque = opaque;
46 }
47
48 s->error_code = 0; /* BROTLI_DECODER_NO_ERROR */
49
50 BrotliInitBitReader(&s->br);
51 s->state = BROTLI_STATE_UNINITED;
52 s->large_window = 0;
53 s->substate_metablock_header = BROTLI_STATE_METABLOCK_HEADER_NONE;
54 s->substate_uncompressed = BROTLI_STATE_UNCOMPRESSED_NONE;
55 s->substate_decode_uint8 = BROTLI_STATE_DECODE_UINT8_NONE;
56 s->substate_read_block_length = BROTLI_STATE_READ_BLOCK_LENGTH_NONE;
57
58 s->buffer_length = 0;
59 s->loop_counter = 0;
60 s->pos = 0;
61 s->rb_roundtrips = 0;
62 s->partial_pos_out = 0;
63 s->used_input = 0;
64
65 s->block_type_trees = NULL;
66 s->block_len_trees = NULL;
67 s->ringbuffer = NULL;
68 s->ringbuffer_size = 0;
69 s->new_ringbuffer_size = 0;
70 s->ringbuffer_mask = 0;
71
72 s->context_map = NULL;
73 s->context_modes = NULL;
74 s->dist_context_map = NULL;
75 s->context_map_slice = NULL;
76 s->dist_context_map_slice = NULL;
77
78 s->literal_hgroup.codes = NULL;
79 s->literal_hgroup.htrees = NULL;
80 s->insert_copy_hgroup.codes = NULL;
81 s->insert_copy_hgroup.htrees = NULL;
82 s->distance_hgroup.codes = NULL;
83 s->distance_hgroup.htrees = NULL;
84
85 s->is_last_metablock = 0;
86 s->is_uncompressed = 0;
87 s->is_metadata = 0;
88 s->should_wrap_ringbuffer = 0;
89 s->canny_ringbuffer_allocation = 1;
90
91 s->window_bits = 0;
92 s->max_distance = 0;
93 s->dist_rb[0] = 16;
94 s->dist_rb[1] = 15;
95 s->dist_rb[2] = 11;
96 s->dist_rb[3] = 4;
97 s->dist_rb_idx = 0;
98 s->block_type_trees = NULL;
99 s->block_len_trees = NULL;
100
101 s->mtf_upper_bound = 63;
102
103 s->compound_dictionary = NULL;
104 s->dictionary =
105 BrotliSharedDictionaryCreateInstance(alloc_func, free_func, opaque);
106 if (!s->dictionary) return BROTLI_FALSE;
107
108 s->metadata_start_func = NULL;
109 s->metadata_chunk_func = NULL;
110 s->metadata_callback_opaque = 0;
111
112 return BROTLI_TRUE;
113 }
114
115 void BrotliDecoderStateMetablockBegin(BrotliDecoderState* s) {
116 s->meta_block_remaining_len = 0;
117 s->block_length[0] = BROTLI_BLOCK_SIZE_CAP;
118 s->block_length[1] = BROTLI_BLOCK_SIZE_CAP;
119 s->block_length[2] = BROTLI_BLOCK_SIZE_CAP;
120 s->num_block_types[0] = 1;
121 s->num_block_types[1] = 1;
122 s->num_block_types[2] = 1;
123 s->block_type_rb[0] = 1;
124 s->block_type_rb[1] = 0;
125 s->block_type_rb[2] = 1;
126 s->block_type_rb[3] = 0;
127 s->block_type_rb[4] = 1;
128 s->block_type_rb[5] = 0;
129 s->context_map = NULL;
130 s->context_modes = NULL;
131 s->dist_context_map = NULL;
132 s->context_map_slice = NULL;
133 s->literal_htree = NULL;
134 s->dist_context_map_slice = NULL;
135 s->dist_htree_index = 0;
136 s->context_lookup = NULL;
137 s->literal_hgroup.codes = NULL;
138 s->literal_hgroup.htrees = NULL;
139 s->insert_copy_hgroup.codes = NULL;
140 s->insert_copy_hgroup.htrees = NULL;
141 s->distance_hgroup.codes = NULL;
142 s->distance_hgroup.htrees = NULL;
143 }
144
145 void BrotliDecoderStateCleanupAfterMetablock(BrotliDecoderState* s) {
146 BROTLI_DECODER_FREE(s, s->context_modes);
147 BROTLI_DECODER_FREE(s, s->context_map);
148 BROTLI_DECODER_FREE(s, s->dist_context_map);
149 BROTLI_DECODER_FREE(s, s->literal_hgroup.htrees);
150 BROTLI_DECODER_FREE(s, s->insert_copy_hgroup.htrees);
151 BROTLI_DECODER_FREE(s, s->distance_hgroup.htrees);
152 }
153
154 void BrotliDecoderStateCleanup(BrotliDecoderState* s) {
155 BrotliDecoderStateCleanupAfterMetablock(s);
156
157 BROTLI_DECODER_ON_FINISH(s);
158
159 BROTLI_DECODER_FREE(s, s->compound_dictionary);
160 BrotliSharedDictionaryDestroyInstance(s->dictionary);
161 s->dictionary = NULL;
162 BROTLI_DECODER_FREE(s, s->ringbuffer);
163 BROTLI_DECODER_FREE(s, s->block_type_trees);
164 }
165
166 BROTLI_BOOL BrotliDecoderHuffmanTreeGroupInit(BrotliDecoderState* s,
167 HuffmanTreeGroup* group, brotli_reg_t alphabet_size_max,
168 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 const size_t max_table_size = alphabet_size_limit + 376;
174 const size_t code_size = sizeof(HuffmanCode) * ntrees * max_table_size;
175 const size_t htree_size = sizeof(HuffmanCode*) * ntrees;
176 /* Pointer alignment is, hopefully, wider than sizeof(HuffmanCode). */
177 HuffmanCode** p = (HuffmanCode**)BROTLI_DECODER_ALLOC(s,
178 code_size + htree_size);
179 group->alphabet_size_max = (uint16_t)alphabet_size_max;
180 group->alphabet_size_limit = (uint16_t)alphabet_size_limit;
181 group->num_htrees = (uint16_t)ntrees;
182 group->htrees = p;
183 group->codes = p ? (HuffmanCode*)(&p[ntrees]) : NULL;
184 return !!p;
185 }
186
187 #if defined(__cplusplus) || defined(c_plusplus)
188 } /* extern "C" */
189 #endif