Debian Codegraph @

crt/aws-c-compression/source/huffman.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/compression/huffman.h>
7
8 #define BITSIZEOF(val) (sizeof(val) * 8)
9
10 static uint8_t MAX_PATTERN_BITS = BITSIZEOF(((struct aws_huffman_code *)0)->pattern);
11
12 void aws_huffman_encoder_init(struct aws_huffman_encoder *encoder, struct aws_huffman_symbol_coder *coder) {
13
14 AWS_ASSERT(encoder);
15 AWS_ASSERT(coder);
16
17 AWS_ZERO_STRUCT(*encoder);
18 encoder->coder = coder;
19 encoder->eos_padding = UINT8_MAX;
20 }
21
22 void aws_huffman_encoder_reset(struct aws_huffman_encoder *encoder) {
23
24 AWS_ASSERT(encoder);
25
26 AWS_ZERO_STRUCT(encoder->overflow_bits);
27 }
28
29 void aws_huffman_decoder_init(struct aws_huffman_decoder *decoder, struct aws_huffman_symbol_coder *coder) {
30
31 AWS_ASSERT(decoder);
32 AWS_ASSERT(coder);
33
34 AWS_ZERO_STRUCT(*decoder);
35 decoder->coder = coder;
36 }
37
38 void aws_huffman_decoder_reset(struct aws_huffman_decoder *decoder) {
39
40 decoder->working_bits = 0;
41 decoder->num_bits = 0;
42 }
43
44 void aws_huffman_decoder_allow_growth(struct aws_huffman_decoder *decoder, bool allow_growth) {
45 decoder->allow_growth = allow_growth;
46 }
47
48 /* Much of encode is written in a helper function,
49 so this struct helps avoid passing all the parameters through by hand */
50 struct encoder_state {
51 struct aws_huffman_encoder *encoder;
52 struct aws_byte_buf *output_buf;
53 uint8_t working;
54 uint8_t bit_pos;
55 };
56
57 /* Helper function to write a single bit_pattern to memory (or working_bits if
58 * out of buffer space) */
59 static int encode_write_bit_pattern(struct encoder_state *state, struct aws_huffman_code bit_pattern) {
60 AWS_PRECONDITION(state->output_buf->len < state->output_buf->capacity);
61
62 if (bit_pattern.num_bits == 0) {
63 return aws_raise_error(AWS_ERROR_COMPRESSION_UNKNOWN_SYMBOL);
64 }
65
66 uint8_t bits_to_write = bit_pattern.num_bits;
67 while (bits_to_write > 0) {
68 uint8_t bits_for_current = bits_to_write > state->bit_pos ? state->bit_pos : bits_to_write;
69 /* Chop off the top 0s and bits that have already been read */
70 uint8_t bits_to_cut =
71 (BITSIZEOF(bit_pattern.pattern) - bit_pattern.num_bits) + (bit_pattern.num_bits - bits_to_write);
72
73 /* Write the appropiate number of bits to this byte
74 Shift to the left to cut any unneeded bits
75 Shift to the right to position the bits correctly */
76 state->working |= (bit_pattern.pattern << bits_to_cut) >> (MAX_PATTERN_BITS - state->bit_pos);
77
78 bits_to_write -= bits_for_current;
79 state->bit_pos -= bits_for_current;
80
81 if (state->bit_pos == 0) {
82 /* Save the whole byte */
83 aws_byte_buf_write_u8(state->output_buf, state->working);
84
85 state->bit_pos = 8;
86 state->working = 0;
87
88 if (state->output_buf->len == state->output_buf->capacity) {
89 state->encoder->overflow_bits.num_bits = bits_to_write;
90
91 if (bits_to_write) {
92 /* If buffer is full and there are remaining bits, save them to overflow and return */
93 bits_to_cut += bits_for_current;
94
95 state->encoder->overflow_bits.pattern =
96 (bit_pattern.pattern << bits_to_cut) >> (MAX_PATTERN_BITS - bits_to_write);
97
98 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
99 }
100 }
101 }
102 }
103
104 return AWS_OP_SUCCESS;
105 }
106
107 size_t aws_huffman_get_encoded_length(struct aws_huffman_encoder *encoder, struct aws_byte_cursor to_encode) {
108
109 AWS_PRECONDITION(encoder);
110 AWS_PRECONDITION(aws_byte_cursor_is_valid(&to_encode));
111
112 size_t num_bits = 0;
113
114 while (to_encode.len) {
115 uint8_t new_byte = 0;
116 aws_byte_cursor_read_u8(&to_encode, &new_byte);
117 struct aws_huffman_code code_point = encoder->coder->encode(new_byte, encoder->coder->userdata);
118 num_bits += code_point.num_bits;
119 }
120
121 size_t length = num_bits / 8;
122
123 /* Round up */
124 if (num_bits % 8) {
125 ++length;
126 }
127
128 return length;
129 }
130
131 int aws_huffman_encode(
132 struct aws_huffman_encoder *encoder,
133 struct aws_byte_cursor *to_encode,
134 struct aws_byte_buf *output) {
135
136 AWS_ASSERT(encoder);
137 AWS_ASSERT(encoder->coder);
138 AWS_ASSERT(to_encode);
139 AWS_ASSERT(output);
140
141 struct encoder_state state = {
142 .working = 0,
143 .bit_pos = 8,
144 };
145 state.encoder = encoder;
146 state.output_buf = output;
147
148 /* Write any bits leftover from previous invocation */
149 if (encoder->overflow_bits.num_bits) {
150 if (output->len == output->capacity) {
151 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
152 }
153
154 if (encode_write_bit_pattern(&state, encoder->overflow_bits)) {
155 return AWS_OP_ERR;
156 }
157
158 encoder->overflow_bits.num_bits = 0;
159 }
160
161 while (to_encode->len) {
162 if (output->len == output->capacity) {
163 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
164 }
165
166 uint8_t new_byte = 0;
167 aws_byte_cursor_read_u8(to_encode, &new_byte);
168 struct aws_huffman_code code_point = encoder->coder->encode(new_byte, encoder->coder->userdata);
169
170 if (encode_write_bit_pattern(&state, code_point)) {
171 return AWS_OP_ERR;
172 }
173 }
174
175 /* The following code only runs when the buffer has written successfully */
176
177 /* If whole buffer processed, write EOS */
178 if (state.bit_pos != 8) {
179 struct aws_huffman_code eos_cp;
180 eos_cp.pattern = encoder->eos_padding;
181 eos_cp.num_bits = state.bit_pos;
182 encode_write_bit_pattern(&state, eos_cp);
183 AWS_ASSERT(state.bit_pos == 8);
184 }
185
186 return AWS_OP_SUCCESS;
187 }
188
189 /* Decode's reading is written in a helper function,
190 so this struct helps avoid passing all the parameters through by hand */
191 struct huffman_decoder_state {
192 struct aws_huffman_decoder *decoder;
193 struct aws_byte_cursor *input_cursor;
194 };
195
196 static void decode_fill_working_bits(struct huffman_decoder_state *state) {
197
198 /* Read from bytes in the buffer until there are enough bytes to process */
199 while (state->decoder->num_bits < MAX_PATTERN_BITS && state->input_cursor->len) {
200
201 /* Read the appropiate number of bits from this byte */
202 uint8_t new_byte = 0;
203 aws_byte_cursor_read_u8(state->input_cursor, &new_byte);
204
205 uint64_t positioned = ((uint64_t)new_byte)
206 << (BITSIZEOF(state->decoder->working_bits) - 8 - state->decoder->num_bits);
207 state->decoder->working_bits |= positioned;
208
209 state->decoder->num_bits += 8;
210 }
211 }
212
213 int aws_huffman_decode(
214 struct aws_huffman_decoder *decoder,
215 struct aws_byte_cursor *to_decode,
216 struct aws_byte_buf *output) {
217
218 AWS_ASSERT(decoder);
219 AWS_ASSERT(decoder->coder);
220 AWS_ASSERT(to_decode);
221 AWS_ASSERT(output);
222
223 struct huffman_decoder_state state;
224 state.decoder = decoder;
225 state.input_cursor = to_decode;
226
227 /* Measures how much of the input was read */
228 size_t bits_left = decoder->num_bits + to_decode->len * 8;
229
230 while (1) {
231
232 decode_fill_working_bits(&state);
233
234 uint8_t symbol;
235 uint8_t bits_read = decoder->coder->decode(
236 (uint32_t)(decoder->working_bits >> (BITSIZEOF(decoder->working_bits) - MAX_PATTERN_BITS)),
237 &symbol,
238 decoder->coder->userdata);
239
240 if (bits_read == 0) {
241 if (bits_left < MAX_PATTERN_BITS) {
242 /* More input is needed to continue */
243 return AWS_OP_SUCCESS;
244 }
245 /* Unknown symbol found */
246 return aws_raise_error(AWS_ERROR_COMPRESSION_UNKNOWN_SYMBOL);
247 }
248 if (bits_read > bits_left) {
249 /* Check if the buffer has been overrun.
250 Note: because of the check in decode_fill_working_bits,
251 the buffer won't actually overrun, instead there will
252 be 0's in the bottom of working_bits. */
253
254 return AWS_OP_SUCCESS;
255 }
256
257 if (output->len == output->capacity) {
258 /* Check if we've hit the end of the output buffer.
259 * Grow buffer, or raise error, depending on settings */
260 if (decoder->allow_growth) {
261 /* Double the capacity */
262 if (aws_byte_buf_reserve_relative(output, output->capacity)) {
263 return AWS_OP_ERR;
264 }
265 } else {
266 return aws_raise_error(AWS_ERROR_SHORT_BUFFER);
267 }
268 }
269
270 bits_left -= bits_read;
271 decoder->working_bits <<= bits_read;
272 decoder->num_bits -= bits_read;
273
274 /* Store the found symbol */
275 aws_byte_buf_write_u8(output, symbol);
276
277 /* Successfully decoded whole buffer */
278 if (bits_left == 0) {
279 return AWS_OP_SUCCESS;
280 }
281 }
282
283 /* This case is unreachable */
284 AWS_ASSERT(0);
285 return aws_raise_error(AWS_ERROR_INVALID_STATE);
286 }