Debian Codegraph @

crt/aws-c-compression/include/aws/compression/huffman.h

raw symbols SCIP
1 #ifndef AWS_COMPRESSION_HUFFMAN_H
2 #define AWS_COMPRESSION_HUFFMAN_H
3
4 /**
5 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
6 * SPDX-License-Identifier: Apache-2.0.
7 */
8
9 #include <aws/compression/compression.h>
10
11 #include <aws/common/byte_buf.h>
12
13 AWS_PUSH_SANE_WARNING_LEVEL
14
15 /**
16 * Represents an encoded code
17 */
18 struct aws_huffman_code {
19 /**
20 * The value of the code
21 * \note The pattern is stored in the least significant bits
22 */
23 uint32_t pattern;
24 /** The number of bits in pattern to use */
25 uint8_t num_bits;
26 };
27
28 /**
29 * Function used to encode a single symbol to an aws_huffman_code
30 *
31 * \param[in] symbol The symbol to encode
32 * \param[in] userdata Optional userdata (aws_huffman_symbol_coder.userdata)
33 *
34 * \returns The code representing the symbol. If this symbol is not recognized,
35 * return a code with num_bits set to 0.
36 */
37 typedef struct aws_huffman_code(aws_huffman_symbol_encoder_fn)(uint8_t symbol, void *userdata);
38 /**
39 * Function used to decode a code into a symbol
40 *
41 * \param[in] bits The bits to attept to decode a symbol from
42 * \param[out] symbol The symbol found. Do not write to if no valid symbol
43 * found \param[in] userdata Optional userdata
44 * (aws_huffman_symbol_coder.userdata)
45 *
46 * \returns The number of bits read from bits
47 */
48 typedef uint8_t(aws_huffman_symbol_decoder_fn)(uint32_t bits, uint8_t *symbol, void *userdata);
49
50 /**
51 * Structure used to define how symbols are encoded and decoded
52 */
53 struct aws_huffman_symbol_coder {
54 aws_huffman_symbol_encoder_fn *encode;
55 aws_huffman_symbol_decoder_fn *decode;
56 void *userdata;
57 };
58
59 /**
60 * Structure used for persistent encoding.
61 * Allows for reading from or writing to incomplete buffers.
62 */
63 struct aws_huffman_encoder {
64 /* Params */
65 struct aws_huffman_symbol_coder *coder;
66 uint8_t eos_padding;
67
68 /* State */
69 struct aws_huffman_code overflow_bits;
70 };
71
72 /**
73 * Structure used for persistent decoding.
74 * Allows for reading from or writing to incomplete buffers.
75 */
76 struct aws_huffman_decoder {
77 /* Param */
78 struct aws_huffman_symbol_coder *coder;
79 bool allow_growth;
80
81 /* State */
82 uint64_t working_bits;
83 uint8_t num_bits;
84 };
85
86 AWS_EXTERN_C_BEGIN
87
88 /**
89 * Initialize a encoder object with a symbol coder.
90 */
91 AWS_COMPRESSION_API
92 void aws_huffman_encoder_init(struct aws_huffman_encoder *encoder, struct aws_huffman_symbol_coder *coder);
93
94 /**
95 * Resets a decoder for use with a new binary stream
96 */
97 AWS_COMPRESSION_API
98 void aws_huffman_encoder_reset(struct aws_huffman_encoder *encoder);
99
100 /**
101 * Initialize a decoder object with a symbol coder.
102 */
103 AWS_COMPRESSION_API
104 void aws_huffman_decoder_init(struct aws_huffman_decoder *decoder, struct aws_huffman_symbol_coder *coder);
105
106 /**
107 * Resets a decoder for use with a new binary stream
108 */
109 AWS_COMPRESSION_API
110 void aws_huffman_decoder_reset(struct aws_huffman_decoder *decoder);
111
112 /**
113 * Get the byte length of to_encode post-encoding.
114 *
115 * \param[in] encoder The encoder object to use
116 * \param[in] to_encode The symbol buffer to encode
117 *
118 * \return The length of the encoded string.
119 */
120 AWS_COMPRESSION_API
121 size_t aws_huffman_get_encoded_length(struct aws_huffman_encoder *encoder, struct aws_byte_cursor to_encode);
122
123 /**
124 * Encode a symbol buffer into the output buffer.
125 *
126 * \param[in] encoder The encoder object to use
127 * \param[in] to_encode The symbol buffer to encode
128 * \param[in] output The buffer to write encoded bytes to
129 *
130 * \return AWS_OP_SUCCESS if encoding is successful, AWS_OP_ERR otherwise
131 */
132 AWS_COMPRESSION_API
133 int aws_huffman_encode(
134 struct aws_huffman_encoder *encoder,
135 struct aws_byte_cursor *to_encode,
136 struct aws_byte_buf *output);
137
138 /**
139 * Decodes a byte buffer into the provided symbol array.
140 *
141 * \param[in] decoder The decoder object to use
142 * \param[in] to_decode The encoded byte buffer to read from
143 * \param[in] output The buffer to write decoded symbols to.
144 * If decoder is set to allow growth, capacity will be increased when necessary.
145 *
146 * \return AWS_OP_SUCCESS if encoding is successful, AWS_OP_ERR otherwise
147 */
148 AWS_COMPRESSION_API
149 int aws_huffman_decode(
150 struct aws_huffman_decoder *decoder,
151 struct aws_byte_cursor *to_decode,
152 struct aws_byte_buf *output);
153
154 /**
155 * Set whether or not to increase capacity when the output buffer fills up while decoding.
156 * This is false by default.
157 */
158 AWS_COMPRESSION_API
159 void aws_huffman_decoder_allow_growth(struct aws_huffman_decoder *decoder, bool allow_growth);
160
161 AWS_EXTERN_C_END
162 AWS_POP_SANE_WARNING_LEVEL
163
164 #endif /* AWS_COMPRESSION_HUFFMAN_H */