]> git.proxmox.com Git - ceph.git/blob - ceph/src/isa-l/igzip/generate_constant_block_header.c
99bcf4829a542fc543bdc606637ad902afd27dc7
[ceph.git] / ceph / src / isa-l / igzip / generate_constant_block_header.c
1 /**********************************************************************
2 Copyright(c) 2011-2016 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 "huff_codes.h"
33 #include "bitbuf2.h"
34
35 #define MAX_HEADER_SIZE 350
36 #define BLOCK_SIZE 16*1024
37
38 void fprint_header(FILE * outfile, uint8_t * header, uint64_t bit_count)
39 {
40 int i;
41 fprintf(outfile, "unsigned char data[] = {");
42 for (i = 0; i < bit_count / 8; i++) {
43 if ((i & 7) == 0)
44 fprintf(outfile, "\n\t");
45 else
46 fprintf(outfile, " ");
47 fprintf(outfile, "0x%02x,", header[i]);
48 }
49
50 if ((i & 7) == 0)
51 fprintf(outfile, "\n\t");
52 else
53 fprintf(outfile, " ");
54 fprintf(outfile, "0x%02x", header[i]);
55 fprintf(outfile, "\t};\n\n");
56
57 }
58
59 int main(int argc, char **argv)
60 {
61 /* Generates a header for a constant block, along with some manual
62 * twiddling to create a header with the desired properties*/
63 uint8_t stream[BLOCK_SIZE];
64 struct isal_huff_histogram histogram;
65 uint64_t *lit_histogram = histogram.lit_len_histogram;
66 uint64_t *dist_histogram = histogram.dist_histogram;
67 uint8_t header[MAX_HEADER_SIZE];
68 struct huff_tree lit_tree, dist_tree;
69 struct huff_tree lit_tree_array[2 * LIT_LEN - 1], dist_tree_array[2 * DIST_LEN - 1];
70 struct huff_code lit_huff_table[LIT_LEN], dist_huff_table[DIST_LEN];
71 uint64_t bit_count;
72
73 uint8_t repeated_char = 0x00;
74
75 memset(header, 0, sizeof(header));
76 memset(&histogram, 0, sizeof(histogram)); /* Initialize histograms. */
77 memset(stream, repeated_char, sizeof(stream));
78 memset(lit_tree_array, 0, sizeof(lit_tree_array));
79 memset(dist_tree_array, 0, sizeof(dist_tree_array));
80 memset(lit_huff_table, 0, sizeof(lit_huff_table));
81 memset(dist_huff_table, 0, sizeof(dist_huff_table));
82
83 isal_update_histogram(stream, sizeof(stream), &histogram);
84
85 /* These are set to manually change the histogram to create a header with the
86 * desired properties. In this case, the header is modified so that it is byte
87 * unaligned by 6 bits, so that 0 is a 2 bit code, so that the header plus the
88 * encoding of one 0 is byte aligned*/
89 lit_histogram[repeated_char] = 20;
90 lit_histogram[280] = 2;
91 lit_histogram[264] = 5;
92 lit_histogram[282] = 0;
93
94 lit_tree = create_symbol_subset_huff_tree(lit_tree_array, lit_histogram, LIT_LEN);
95 dist_tree = create_symbol_subset_huff_tree(dist_tree_array, dist_histogram, DIST_LEN);
96 if (create_huff_lookup(lit_huff_table, LIT_LEN, lit_tree, 15) > 0) {
97 printf("Error, code with invalid length for Deflate standard.\n");
98 return 1;
99 }
100
101 if (create_huff_lookup(dist_huff_table, DIST_LEN, dist_tree, 15) > 0) {
102 printf("Error, code with invalid length for Deflate standard.\n");
103 return 1;
104 }
105
106 /* Remove litral symbol corresponding to the unoptimal look back
107 * distance of 258 found by gen_histogram*/
108 dist_huff_table[16].length = 0;
109
110 bit_count = create_header(header, sizeof(header), lit_huff_table, dist_huff_table, 1);
111 printf("Header for %x\n", repeated_char);
112 fprintf(stdout, "Complete Bytes: %lu\n", bit_count / 8);
113 fprintf(stdout, "Byte Offset: %lu\n\n", (bit_count) & 7);
114 fprint_header(stdout, header, bit_count);
115 printf("\n");
116
117 return 0;
118 }