Debian Codegraph @

crt/aws-c-compression/README.md

raw symbols SCIP
1 ## AWS C Compression
2
3 This is a cross-platform C99 implementation of compression algorithms such as
4 gzip, and huffman encoding/decoding. Currently only huffman is implemented.
5
6 ## License
7
8 This library is licensed under the Apache 2.0 License.
9
10 ## Usage
11
12 ### Building
13
14 Note that aws-c-compression has a dependency on aws-c-common:
15
16 ```
17 git clone [email protected]:awslabs/aws-c-common.git
18 cmake -DCMAKE_PREFIX_PATH=<install-path> -DCMAKE_INSTALL_PREFIX=<install-path> -S aws-c-common -B aws-c-common/build
19 cmake --build aws-c-common/build --target install
20
21 git clone [email protected]:awslabs/aws-c-compression.git
22 cmake -DCMAKE_PREFIX_PATH=<install-path> -DCMAKE_INSTALL_PREFIX=<install-path> -S aws-c-compression -B aws-c-compression/build
23 cmake --build aws-c-compression/build --target install
24 ```
25
26 ### Huffman
27
28 The Huffman implemention in this library is designed around the concept of a
29 generic "symbol coder" object, which defines how each symbol (value between 0
30 and 255) is encoded and decoded. This object looks like this:
31 ```c
32 typedef struct aws_huffman_code (*aws_huffman_symbol_encoder)(uint8_t symbol, void *userdata);
33 typedef uint8_t (*aws_huffman_symbol_decoder)(uint32_t bits, uint8_t *symbol, void *userdata);
34
35 struct aws_huffman_symbol_coder {
36 aws_huffman_symbol_encoder encode;
37 aws_huffman_symbol_decoder decode;
38 void *userdata;
39 };
40 ```
41 These callbacks may be implemented manually, or you may use the included
42 Huffman coder generator to generate one from a table definition file. The
43 generator expects to be called with the following arguments:
44 ```shell
45 $ aws-c-compression-huffman-generator path/to/table.def path/to/generated.c coder_name
46 ```
47
48 The table definition file should be in the following format:
49 ```c
50 /* sym bits code len */
51 HUFFMAN_CODE( 0, "1100101110", 0x32e, 10)
52 HUFFMAN_CODE( 1, "1100101111", 0x32f, 10)
53 /* ... */
54 ```
55 The HUFFMAN_CODE macro expects 4 arguments:
56 * sym: the symbol value [0-255]
57 * bits: the bits representing the symbol in string form
58 * code: the bits representing the symbol in numeric form
59 * len: the number of bits used to represent the symbol
60
61 > #### Note
62 > This file may also be `#include`d in the following way to generate a static
63 > list of codes:
64 > ```c
65 > /* Provides the HUFFMAN_CODE macro */
66 > #include <aws/testing/compression/huffman.h>
67 >
68 > static struct huffman_test_code_point code_points[] = {
69 > #include "test_huffman_static_table.def"
70 > };
71 > ```
72
73 This will emit a c file which exports a function with the following signiture:
74 ```c
75 struct aws_huffman_symbol_coder *{coder_name}_get_coder();
76 ```
77 Note that this function does not allocate, but maintains a static instance of
78 the coder.
79
80
81 An example implementation of this file is provided in
82 `tests/test_huffman_static_table.def`.
83
84
85 To use the coder, forward declare that function, and pass the result as the
86 second argument to `aws_huffman_encoder_init` and `aws_huffman_decoder_init`.
87 ```c
88 struct aws_huffman_encoder encoder;
89 aws_huffman_encoder_init(&encoder, {coder_name}_get_coder());
90
91 struct aws_huffman_decoder decoder;
92 aws_huffman_decoder_init(&decoder, {coder_name}_get_coder())
93 ```
94
95 #### Encoding
96 ```c
97 /**
98 * Encode a symbol buffer into the output buffer.
99 *
100 * \param[in] encoder The encoder object to use
101 * \param[in] to_encode The symbol buffer to encode
102 * \param[in,out] length In: The length of to_decode. Out: The number of bytes read from to_encode
103 * \param[in] output The buffer to write encoded bytes to
104 * \param[in,out] output_size In: The size of output. Out: The number of bytes written to output
105 *
106 * \return AWS_OP_SUCCESS if encoding is successful, AWS_OP_ERR the code for the error that occured
107 */
108 int aws_huffman_encode(struct aws_huffman_encoder *encoder, const char *to_encode, size_t *length, uint8_t *output, size_t *output_size);
109 ```
110 The encoder is built to support partial encoding. This means that if there
111 isn't enough space in `output`, the encoder will encode as much as possible,
112 update `length` to indicate how much was consumed, `output_size` won't change,
113 and `AWS_ERROR_SHORT_BUFFER` will be raised. `aws_huffman_encode` may then be
114 called again like the following pseudo-code:
115 ```c
116 void encode_and_send(const char *to_encode, size_t size) {
117 while (size > 0) {
118 uint8_t output[some_chunk_size];
119 size_t output_size = sizeof(output);
120 size_t bytes_read = size;
121 aws_huffman_encode(encoder, to_encode, &bytes_read, output, &output_size);
122 /* AWS_ERROR_SHORT_BUFFER was raised... */
123 send_output_to_someone_else(output, output_size);
124
125 to_encode += bytes_read;
126 size -= bytes_read;
127 }
128 /* Be sure to reset the encoder after use */
129 aws_huffman_encoder_reset(encoder);
130 }
131 ```
132
133 `aws_huffman_encoder` also has a `uint8_t` field called `eos_padding` that
134 defines how any unwritten bits in the last byte of output are filled. The most
135 significant bits will used. For example, if the last byte contains only 3 bits
136 and `eos_padding` is `0b01010101`, `01010` will be appended to the byte.
137
138 #### Decoding
139 ```c
140 /**
141 * Decodes a byte buffer into the provided symbol array.
142 *
143 * \param[in] decoder The decoder object to use
144 * \param[in] to_decode The encoded byte buffer to read from
145 * \param[in,out] length In: The length of to_decode. Out: The number of bytes read from to_decode
146 * \param[in] output The buffer to write decoded symbols to
147 * \param[in,out] output_size In: The size of output. Out: The number of bytes written to output
148 *
149 * \return AWS_OP_SUCCESS if encoding is successful, AWS_OP_ERR the code for the error that occured
150 */
151 int aws_huffman_decode(struct aws_huffman_decoder *decoder, const uint8_t *to_decode, size_t *length, char *output, size_t *output_size);
152 ```
153 The decoder is built to support partial encoding. This means that if there
154 isn't enough space in `output`, the decoder will decode as much as possible,
155 update `length` to indicate how much was consumed, `output_size` won't change,
156 and `AWS_ERROR_SHORT_BUFFER` will be raised. `aws_huffman_decode` may then be
157 called again like the following pseudo-code:
158 ```c
159 void decode_and_send(const char *to_decode, size_t size) {
160 while (size > 0) {
161 uint8_t output[some_chunk_size];
162 size_t output_size = sizeof(output);
163 size_t bytes_read = size;
164 aws_huffman_decode(decoder, to_decode, &bytes_read, output, &output_size);
165 /* AWS_ERROR_SHORT_BUFFER was raised... */
166 send_output_to_someone_else(output, output_size);
167
168 to_decode += bytes_read;
169 size -= bytes_read;
170 }
171 /* Be sure to reset the decoder after use */
172 aws_huffman_decoder_reset(decoder);
173 }
174 ```
175
176 Upon completion of a decode, the most significant bits of
177 `decoder->working_bits` will contain the final bits of `to_decode` that could
178 not match a symbol. This is useful for verifying the padding bits of a stream.
179 For example, to validate that a stream ends in all 1's (like HPACK requires),
180 you could do the following:
181 ```c
182 AWS_ASSERT(decoder->working_bits == UINT64_MAX << (64 - decoder->num_bits));
183 ```