Debian Codegraph @

crt/aws-c-compression/source/huffman_generator/generator.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 <assert.h> /* NOLINT(fuchsia-restrict-system-includes) */
7 #include <ctype.h>
8 #include <stdint.h> /* NOLINT(fuchsia-restrict-system-includes) */
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 struct huffman_code {
14 uint8_t num_bits;
15 uint32_t bits;
16 };
17
18 struct huffman_code_point {
19 uint8_t symbol;
20 struct huffman_code code;
21 };
22
23 enum { num_code_points = 256 };
24 static struct huffman_code_point code_points[num_code_points];
25
26 static size_t skip_whitespace(const char *str) {
27 size_t offset = 0;
28 while (str[offset] == ' ' || str[offset] == '\t') {
29 ++offset;
30 }
31 return offset;
32 }
33
34 static size_t read_past_comma(const char *str) {
35 size_t offset = 0;
36 while (str[offset] != ',') {
37 ++offset;
38 }
39 return offset + 1;
40 }
41
42 int read_code_points(const char *input_path) {
43
44 memset(code_points, 0, sizeof(code_points));
45 FILE *file = fopen(input_path, "r");
46 if (!file) {
47 printf("Failed to open file '%s' for read.", input_path);
48 return 1;
49 }
50
51 static const char HC_KEYWORD[] = "HUFFMAN_CODE";
52 static const size_t HC_KW_LEN = sizeof(HC_KEYWORD) - 1;
53
54 int is_comment = 0;
55 char line[120];
56 while (fgets(line, sizeof(line), file) != NULL) {
57 const size_t line_length = strlen(line);
58
59 if (line[0] == '#') {
60 /* Ignore preprocessor directives */
61 continue;
62 }
63 for (size_t i = 0; i < line_length - 1; ++i) {
64 if (!is_comment) {
65 if (line[i] == '/' && line[i + 1] == '*') {
66 is_comment = 1;
67 } else if (strncmp(&line[i], HC_KEYWORD, HC_KW_LEN) == 0) {
68 /* Found code, parse it */
69
70 /* Skip macro */
71 const char *current_char = &line[i + HC_KW_LEN];
72 current_char += skip_whitespace(current_char);
73 /* Skip ( */
74 assert(*current_char == '(');
75 ++current_char;
76
77 /* Parse symbol */
78 uint8_t symbol = (uint8_t)atoi(current_char);
79 struct huffman_code_point *code_point = &code_points[symbol];
80
81 assert(!code_point->symbol && "Symbol already found!");
82
83 code_point->symbol = symbol;
84
85 current_char += read_past_comma(current_char);
86 /* Skip the binary string form */
87 current_char += read_past_comma(current_char);
88
89 /* Parse bits */
90 code_point->code.bits = (uint32_t)strtol(current_char, NULL, 16);
91
92 current_char += read_past_comma(current_char);
93
94 code_point->code.num_bits = (uint8_t)atoi(current_char);
95 }
96 } else if (line[i] == '*' && line[i + 1] == '/') {
97 is_comment = 0;
98 }
99 }
100 }
101
102 fclose(file);
103
104 return 0;
105 }
106
107 void code_write(struct huffman_code *code, FILE *file) {
108
109 for (int bit_idx = code->num_bits - 1; bit_idx >= 0; --bit_idx) {
110 char bit = ((code->bits >> bit_idx) & 0x1) ? '1' : '0';
111 fprintf(file, "%c", bit);
112 }
113 }
114
115 struct huffman_node {
116 struct huffman_code_point *value;
117
118 struct huffman_code code;
119 struct huffman_node *children[2];
120 };
121
122 struct huffman_node *huffman_node_new(struct huffman_code code) {
123
124 struct huffman_node *node = malloc(sizeof(struct huffman_node));
125 memset(node, 0, sizeof(struct huffman_node));
126 node->code = code;
127 return node;
128 }
129
130 struct huffman_node *huffman_node_new_value(struct huffman_code_point *value) {
131
132 struct huffman_node *node = malloc(sizeof(struct huffman_node));
133 memset(node, 0, sizeof(struct huffman_node));
134 node->value = value;
135 node->code = value->code;
136 return node;
137 }
138
139 /* note: Does not actually free the memory.
140 This is useful so this function may be called on the tree root. */
141 void huffman_node_clean_up(struct huffman_node *node) {
142
143 for (int i = 0; i < 2; ++i) {
144 if (node->children[i]) {
145 huffman_node_clean_up(node->children[i]);
146 free(node->children[i]);
147 }
148 }
149
150 memset(node, 0, sizeof(struct huffman_node));
151 }
152
153 /* This function writes what to do if the pattern for node is a match */
154 void huffman_node_write_decode_handle_value(struct huffman_node *node, FILE *file) {
155
156 if (!node) {
157 /* Invalid node, return 0 */
158 fprintf(file, " return 0; /* invalid node */\n");
159 } else if (node->value) {
160 /* Attempt to inline value return */
161 fprintf(
162 file,
163 " *symbol = %u;\n"
164 " return %u;\n",
165 node->value->symbol,
166 node->value->code.num_bits);
167 } else {
168 /* Otherwise go to branch check */
169 fprintf(file, " goto node_");
170 code_write(&node->code, file);
171 fprintf(file, ";\n");
172 }
173 }
174
175 void huffman_node_write_decode(struct huffman_node *node, FILE *file, uint8_t current_bit) {
176
177 /* Value nodes should have been inlined into parent branch checks */
178 assert(!node->value);
179 assert(node->children[0] || node->children[1]);
180
181 static int write_label = 0;
182
183 if (write_label) {
184 /* Write this node's label after the first run */
185 fprintf(file, "node_");
186 code_write(&node->code, file);
187 fprintf(file, ":\n");
188 }
189
190 write_label = 1;
191
192 /* Check 1 bit pattern */
193 uint32_t single_bit_mask = (uint32_t)(1ull << (31 - current_bit));
194 uint32_t left_aligned_pattern = ((node->code.bits << 1) + 1) << (31 - node->code.num_bits);
195 uint32_t check_pattern = left_aligned_pattern & single_bit_mask;
196 fprintf(file, " if (bits & 0x%x) {\n", check_pattern);
197
198 huffman_node_write_decode_handle_value(node->children[1], file);
199
200 fprintf(file, " } else {\n");
201
202 /* Child 0 is valid, go there */
203 huffman_node_write_decode_handle_value(node->children[0], file);
204
205 fprintf(file, " }\n\n");
206
207 /* Recursively write child nodes */
208 for (uint8_t i = 0; i < 2; ++i) {
209 struct huffman_node *child = node->children[i];
210 if (child && !child->value) {
211 huffman_node_write_decode(child, file, current_bit + 1);
212 }
213 }
214 }
215
216 int main(int argc, char *argv[]) {
217
218 if (argc != 4) {
219 fprintf(
220 stderr,
221 "generator expects 3 arguments: [input file] [output file] "
222 "[encoding name]\n"
223 "A function of the following signature will be exported:\n"
224 "struct aws_huffman_symbol_coder *[encoding name]_get_coder()\n");
225 return 1;
226 }
227
228 const char *input_file = argv[1];
229 const char *output_file = argv[2];
230 const char *decoder_name = argv[3];
231
232 if (read_code_points(input_file)) {
233 return 1;
234 }
235
236 struct huffman_node tree_root;
237 memset(&tree_root, 0, sizeof(struct huffman_node));
238
239 /* Populate the tree */
240 for (size_t i = 0; i < num_code_points; ++i) {
241
242 struct huffman_code_point *value = &code_points[i];
243 if (value->code.num_bits == 0) {
244 continue;
245 }
246
247 struct huffman_node *current = &tree_root;
248
249 uint8_t bit_idx = value->code.num_bits - 1;
250 while (1) {
251 struct huffman_code code = value->code;
252 code.bits >>= bit_idx;
253 code.num_bits = value->code.num_bits - bit_idx;
254
255 uint8_t encoded_bit = code.bits & 0x01;
256 assert(encoded_bit == 0 || encoded_bit == 1);
257
258 if (bit_idx == 0) {
259 /* Done traversing, add value as leaf */
260 assert(!current->children[encoded_bit]);
261 current->children[encoded_bit] = huffman_node_new_value(value);
262 break;
263 }
264
265 if (current->children[encoded_bit]) {
266 /* Not at the end yet, keep traversing */
267 current = current->children[encoded_bit];
268 } else {
269 /* Not at the end yet, but this is the first time down this
270 * path. */
271 struct huffman_node *new_node = huffman_node_new(code);
272 current->children[encoded_bit] = new_node;
273 current = new_node;
274 }
275
276 --bit_idx;
277 }
278 }
279
280 /* Open the file */
281 FILE *file = fopen(output_file, "w");
282 if (!file) {
283 printf("Failed to open file '%s' for write.", output_file);
284 return 1;
285 }
286
287 /* Write the file/function header */
288 fprintf(
289 file,
290 "/**\n"
291 " * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n"
292 " * SPDX-License-Identifier: Apache-2.0.\n"
293 " */\n"
294 "\n"
295 "/* WARNING: THIS FILE WAS AUTOMATICALLY GENERATED. DO NOT EDIT. */\n"
296 "/* clang-format off */\n"
297 "\n"
298 "#include <aws/compression/huffman.h>\n"
299 "\n"
300 "static struct aws_huffman_code code_points[] = {\n");
301
302 for (size_t i = 0; i < num_code_points; ++i) {
303 struct huffman_code_point *cp = &code_points[i];
304 fprintf(
305 file,
306 " { .pattern = 0x%x, .num_bits = %u }, /* '%c' %u */\n",
307 cp->code.bits,
308 cp->code.num_bits,
309 isprint(cp->symbol) ? cp->symbol : ' ',
310 cp->symbol);
311 }
312
313 fprintf(
314 file,
315 "};\n"
316 "\n"
317 "static struct aws_huffman_code encode_symbol(uint8_t symbol, void "
318 "*userdata) {\n"
319 " (void)userdata;\n\n"
320 " return code_points[symbol];\n"
321 "}\n"
322 "\n"
323 "/* NOLINTNEXTLINE(readability-function-size) */\n"
324 "static uint8_t decode_symbol(uint32_t bits, uint8_t *symbol, void "
325 "*userdata) {\n"
326 " (void)userdata;\n\n");
327
328 /* Traverse the tree */
329 huffman_node_write_decode(&tree_root, file, 0);
330
331 /* Write the function footer & encode header */
332 fprintf(
333 file,
334 "}\n"
335 "\n"
336 "struct aws_huffman_symbol_coder *%s_get_coder(void) {\n"
337 "\n"
338 " static struct aws_huffman_symbol_coder coder = {\n"
339 " .encode = encode_symbol,\n"
340 " .decode = decode_symbol,\n"
341 " .userdata = NULL,\n"
342 " };\n"
343 " return &coder;\n"
344 "}\n",
345 decoder_name);
346
347 fclose(file);
348
349 huffman_node_clean_up(&tree_root);
350
351 return 0;
352 }