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  */
fprint_uint16_table(FILE * outfile,uint16_t * table,uint64_t length,char * header,char * footer,char * begin_line)51 void fprint_uint16_table(FILE * outfile, uint16_t * table, uint64_t length, char *header,
52 			 char *footer, char *begin_line)
53 {
54 	int i;
55 	fprintf(outfile, "%s", header);
56 	for (i = 0; i < length - 1; i++) {
57 		if ((i & 7) == 0)
58 			fprintf(outfile, "\n%s", begin_line);
59 		else
60 			fprintf(outfile, " ");
61 		fprintf(outfile, "0x%04x,", table[i]);
62 	}
63 
64 	if ((i & 7) == 0)
65 		fprintf(outfile, "\n%s", begin_line);
66 	else
67 		fprintf(outfile, " ");
68 	fprintf(outfile, "0x%04x", table[i]);
69 	fprintf(outfile, "%s", footer);
70 
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  */
fprint_uint32_table(FILE * outfile,uint32_t * table,uint64_t length,char * header,char * footer,char * begin_line)82 void fprint_uint32_table(FILE * outfile, uint32_t * table, uint64_t length, char *header,
83 			 char *footer, char *begin_line)
84 {
85 	int i;
86 	fprintf(outfile, "%s", header);
87 	for (i = 0; i < length - 1; i++) {
88 		if ((i & 3) == 0)
89 			fprintf(outfile, "\n%s", begin_line);
90 		else
91 			fprintf(outfile, " ");
92 		fprintf(outfile, "0x%08x,", table[i]);
93 	}
94 
95 	if ((i & 3) == 0)
96 		fprintf(outfile, "%s", begin_line);
97 	else
98 		fprintf(outfile, " ");
99 	fprintf(outfile, "0x%08x", table[i]);
100 	fprintf(outfile, "%s", footer);
101 
102 }
103 
fprint_header(FILE * output_file)104 void fprint_header(FILE * output_file)
105 {
106 	fprintf(output_file, "#include \"igzip_lib.h\"\n\n");
107 	fprintf(output_file, "#define LONG_BITS_CHECK %d\n", ISAL_DECODE_LONG_BITS);
108 	fprintf(output_file, "#define SHORT_BITS_CHECK %d\n", ISAL_DECODE_SHORT_BITS);
109 	fprintf(output_file,
110 		"#if (LONG_BITS_CHECK == ISAL_DECODE_LONG_BITS) && (SHORT_BITS_CHECK == ISAL_DECODE_SHORT_BITS)\n"
111 		"# define ISAL_STATIC_INFLATE_TABLE\n"
112 		"#else\n"
113 		"# warning \"Incompatible compile time defines for optimized static inflate table.\"\n"
114 		"#endif\n\n");
115 }
116 
main(int argc,char * argv[])117 int main(int argc, char *argv[])
118 {
119 	struct inflate_state state;
120 	FILE *file;
121 	uint8_t static_deflate_hdr = 3;
122 	uint8_t tmp_space[8], *in_buf;
123 
124 	if (NULL == (in_buf = malloc(DOUBLE_SYM_THRESH + 1))) {
125 		printf("Can not allocote memory\n");
126 		return 1;
127 	}
128 
129 	isal_inflate_init(&state);
130 
131 	memcpy(in_buf, &static_deflate_hdr, sizeof(static_deflate_hdr));
132 	state.next_in = in_buf;
133 	state.avail_in = DOUBLE_SYM_THRESH + 1;
134 	state.next_out = tmp_space;
135 	state.avail_out = sizeof(tmp_space);
136 
137 	isal_inflate(&state);
138 
139 	file = fopen(STATIC_INFLATE_FILE, "w");
140 
141 	if (file == NULL) {
142 		printf("Error creating file hufftables_c.c\n");
143 		return 1;
144 	}
145 	// Add decode tables describing a type 2 static (fixed) header
146 
147 	fprintf(file, "#ifndef STATIC_HEADER_H\n" "#define STATIC_HEADER_H\n\n");
148 
149 	fprint_header(file);
150 
151 	fprintf(file, "struct inflate_huff_code_large static_lit_huff_code = {\n");
152 	fprint_uint32_table(file, state.lit_huff_code.short_code_lookup,
153 			    sizeof(state.lit_huff_code.short_code_lookup) / sizeof(uint32_t),
154 			    "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
155 	fprint_uint16_table(file, state.lit_huff_code.long_code_lookup,
156 			    sizeof(state.lit_huff_code.long_code_lookup) / sizeof(uint16_t),
157 			    "\t.long_code_lookup = {", "\t}\n", "\t\t");
158 	fprintf(file, "};\n\n");
159 
160 	fprintf(file, "struct inflate_huff_code_small static_dist_huff_code = {\n");
161 	fprint_uint16_table(file, state.dist_huff_code.short_code_lookup,
162 			    sizeof(state.dist_huff_code.short_code_lookup) / sizeof(uint16_t),
163 			    "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
164 	fprint_uint16_table(file, state.dist_huff_code.long_code_lookup,
165 			    sizeof(state.dist_huff_code.long_code_lookup) / sizeof(uint16_t),
166 			    "\t.long_code_lookup = {", "\t}\n", "\t\t");
167 	fprintf(file, "};\n\n");
168 
169 	fprintf(file, "#endif\n");
170 
171 	// Add other tables for known dynamic headers - level 0
172 
173 	isal_inflate_init(&state);
174 
175 	memcpy(in_buf, &hufftables_default.deflate_hdr,
176 	       sizeof(hufftables_default.deflate_hdr));
177 	state.next_in = in_buf;
178 	state.avail_in = DOUBLE_SYM_THRESH + 1;
179 	state.next_out = tmp_space;
180 	state.avail_out = sizeof(tmp_space);
181 
182 	isal_inflate(&state);
183 
184 	fprintf(file, "struct inflate_huff_code_large pregen_lit_huff_code = {\n");
185 	fprint_uint32_table(file, state.lit_huff_code.short_code_lookup,
186 			    sizeof(state.lit_huff_code.short_code_lookup) / sizeof(uint32_t),
187 			    "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
188 	fprint_uint16_table(file, state.lit_huff_code.long_code_lookup,
189 			    sizeof(state.lit_huff_code.long_code_lookup) / sizeof(uint16_t),
190 			    "\t.long_code_lookup = {", "\t}\n", "\t\t");
191 	fprintf(file, "};\n\n");
192 
193 	fprintf(file, "struct inflate_huff_code_small pregen_dist_huff_code = {\n");
194 	fprint_uint16_table(file, state.dist_huff_code.short_code_lookup,
195 			    sizeof(state.dist_huff_code.short_code_lookup) / sizeof(uint16_t),
196 			    "\t.short_code_lookup = {", "\t},\n\n", "\t\t");
197 	fprint_uint16_table(file, state.dist_huff_code.long_code_lookup,
198 			    sizeof(state.dist_huff_code.long_code_lookup) / sizeof(uint16_t),
199 			    "\t.long_code_lookup = {", "\t}\n", "\t\t");
200 	fprintf(file, "};\n\n");
201 
202 	fclose(file);
203 	free(in_buf);
204 	return 0;
205 }
206