1 /**********************************************************************
2 Copyright(c) 2011-2018 Intel Corporation All rights reserved.
3
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions
6 are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in
11 the documentation and/or other materials provided with the
12 distribution.
13 * Neither the name of Intel Corporation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 **********************************************************************/
29
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include "igzip_lib.h"
36
37 #define STATIC_INFLATE_FILE "static_inflate.h"
38 #define DOUBLE_SYM_THRESH (4 * 1024)
39
40 extern struct isal_hufftables hufftables_default;
41
42 /**
43 * @brief Prints a table of uint16_t elements to a file.
44 * @param outfile: the file the table is printed to.
45 * @param table: the table to be printed.
46 * @param length: number of elements to be printed.
47 * @param header: header to append in front of the table.
48 * @param footer: footer to append at the end of the table.
49 * @param begin_line: string printed at beginning of new line
50 */
51 void
fprint_uint16_table(FILE * outfile,uint16_t * table,uint64_t length,char * header,char * footer,char * begin_line)52 fprint_uint16_table(FILE *outfile, uint16_t *table, uint64_t length, char *header, char *footer,
53 char *begin_line)
54 {
55 int i;
56 fprintf(outfile, "%s", header);
57 for (i = 0; i < length - 1; i++) {
58 if ((i & 7) == 0)
59 fprintf(outfile, "\n%s", begin_line);
60 else
61 fprintf(outfile, " ");
62 fprintf(outfile, "0x%04x,", table[i]);
63 }
64
65 if ((i & 7) == 0)
66 fprintf(outfile, "\n%s", begin_line);
67 else
68 fprintf(outfile, " ");
69 fprintf(outfile, "0x%04x", table[i]);
70 fprintf(outfile, "%s", footer);
71 }
72
73 /**
74 * @brief Prints a table of uint32_t elements to a file.
75 * @param outfile: the file the table is printed to.
76 * @param table: the table to be printed.
77 * @param length: number of elements to be printed.
78 * @param header: header to append in front of the table.
79 * @param footer: footer to append at the end of the table.
80 * @param begin_line: string printed at beginning of new line
81 */
82 void
fprint_uint32_table(FILE * outfile,uint32_t * table,uint64_t length,char * header,char * footer,char * begin_line)83 fprint_uint32_table(FILE *outfile, uint32_t *table, uint64_t length, char *header, char *footer,
84 char *begin_line)
85 {
86 int i;
87 fprintf(outfile, "%s", header);
88 for (i = 0; i < length - 1; i++) {
89 if ((i & 3) == 0)
90 fprintf(outfile, "\n%s", begin_line);
91 else
92 fprintf(outfile, " ");
93 fprintf(outfile, "0x%08x,", table[i]);
94 }
95
96 if ((i & 3) == 0)
97 fprintf(outfile, "%s", begin_line);
98 else
99 fprintf(outfile, " ");
100 fprintf(outfile, "0x%08x", table[i]);
101 fprintf(outfile, "%s", footer);
102 }
103
104 void
fprint_header(FILE * output_file)105 fprint_header(FILE *output_file)
106 {
107 fprintf(output_file, "#include \"igzip_lib.h\"\n\n");
108 fprintf(output_file, "#define LONG_BITS_CHECK %d\n", ISAL_DECODE_LONG_BITS);
109 fprintf(output_file, "#define SHORT_BITS_CHECK %d\n", ISAL_DECODE_SHORT_BITS);
110 fprintf(output_file, "#if (LONG_BITS_CHECK == ISAL_DECODE_LONG_BITS) && (SHORT_BITS_CHECK "
111 "== ISAL_DECODE_SHORT_BITS)\n"
112 "# define ISAL_STATIC_INFLATE_TABLE\n"
113 "#else\n"
114 "# warning \"Incompatible compile time defines for optimized static "
115 "inflate table.\"\n"
116 "#endif\n\n");
117 }
118
119 int
main(int argc,char * argv[])120 main(int argc, char *argv[])
121 {
122 struct inflate_state state;
123 FILE *file;
124 uint8_t static_deflate_hdr = 3;
125 uint8_t tmp_space[8], *in_buf;
126
127 if (NULL == (in_buf = malloc(DOUBLE_SYM_THRESH + 1))) {
128 printf("Can not allocote memory\n");
129 return 1;
130 }
131
132 isal_inflate_init(&state);
133
134 memcpy(in_buf, &static_deflate_hdr, sizeof(static_deflate_hdr));
135 state.next_in = in_buf;
136 state.avail_in = DOUBLE_SYM_THRESH + 1;
137 state.next_out = tmp_space;
138 state.avail_out = sizeof(tmp_space);
139
140 isal_inflate(&state);
141
142 file = fopen(STATIC_INFLATE_FILE, "w");
143
144 if (file == NULL) {
145 printf("Error creating file hufftables_c.c\n");
146 return 1;
147 }
148 // Add decode tables describing a type 2 static (fixed) header
149
150 fprintf(file, "#ifndef STATIC_HEADER_H\n"
151 "#define STATIC_HEADER_H\n\n");
152
153 fprint_header(file);
154
155 fprintf(file, "struct inflate_huff_code_large static_lit_huff_code = {\n");
156 fprint_uint32_table(file, state.lit_huff_code.short_code_lookup,
157 sizeof(state.lit_huff_code.short_code_lookup) / sizeof(uint32_t),
158 "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
159 fprint_uint16_table(file, state.lit_huff_code.long_code_lookup,
160 sizeof(state.lit_huff_code.long_code_lookup) / sizeof(uint16_t),
161 "\t.long_code_lookup = {", "\t}\n", "\t\t");
162 fprintf(file, "};\n\n");
163
164 fprintf(file, "struct inflate_huff_code_small static_dist_huff_code = {\n");
165 fprint_uint16_table(file, state.dist_huff_code.short_code_lookup,
166 sizeof(state.dist_huff_code.short_code_lookup) / sizeof(uint16_t),
167 "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
168 fprint_uint16_table(file, state.dist_huff_code.long_code_lookup,
169 sizeof(state.dist_huff_code.long_code_lookup) / sizeof(uint16_t),
170 "\t.long_code_lookup = {", "\t}\n", "\t\t");
171 fprintf(file, "};\n\n");
172
173 fprintf(file, "#endif\n");
174
175 // Add other tables for known dynamic headers - level 0
176
177 isal_inflate_init(&state);
178
179 memcpy(in_buf, &hufftables_default.deflate_hdr, sizeof(hufftables_default.deflate_hdr));
180 state.next_in = in_buf;
181 state.avail_in = DOUBLE_SYM_THRESH + 1;
182 state.next_out = tmp_space;
183 state.avail_out = sizeof(tmp_space);
184
185 isal_inflate(&state);
186
187 fprintf(file, "struct inflate_huff_code_large pregen_lit_huff_code = {\n");
188 fprint_uint32_table(file, state.lit_huff_code.short_code_lookup,
189 sizeof(state.lit_huff_code.short_code_lookup) / sizeof(uint32_t),
190 "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
191 fprint_uint16_table(file, state.lit_huff_code.long_code_lookup,
192 sizeof(state.lit_huff_code.long_code_lookup) / sizeof(uint16_t),
193 "\t.long_code_lookup = {", "\t}\n", "\t\t");
194 fprintf(file, "};\n\n");
195
196 fprintf(file, "struct inflate_huff_code_small pregen_dist_huff_code = {\n");
197 fprint_uint16_table(file, state.dist_huff_code.short_code_lookup,
198 sizeof(state.dist_huff_code.short_code_lookup) / sizeof(uint16_t),
199 "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
200 fprint_uint16_table(file, state.dist_huff_code.long_code_lookup,
201 sizeof(state.dist_huff_code.long_code_lookup) / sizeof(uint16_t),
202 "\t.long_code_lookup = {", "\t}\n", "\t\t");
203 fprintf(file, "};\n\n");
204
205 fclose(file);
206 free(in_buf);
207 return 0;
208 }
209