]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | #ifndef INFLATE_H | |
31 | #define INFLATE_H | |
32 | ||
33 | #include <stdint.h> | |
34 | #include "huff_codes.h" | |
35 | ||
36 | #define DECOMPRESSION_FINISHED 0 | |
37 | #define END_OF_INPUT 1 | |
38 | #define OUT_BUFFER_OVERFLOW 2 | |
39 | #define INVALID_BLOCK_HEADER 3 | |
40 | #define INVALID_SYMBOL 4 | |
41 | #define INVALID_NON_COMPRESSED_BLOCK_LENGTH 5 | |
42 | #define INVALID_LOOK_BACK_DISTANCE 6 | |
43 | ||
44 | #define DECODE_LOOKUP_SIZE 10 | |
45 | ||
46 | #if DECODE_LOOKUP_SIZE > 15 | |
47 | # undef DECODE_LOOKUP_SIZE | |
48 | # define DECODE_LOOKUP_SIZE 15 | |
49 | #endif | |
50 | ||
51 | #if DECODE_LOOKUP_SIZE > 7 | |
52 | # define MAX_LONG_CODE ((2 << 8) + 1) * (2 << (15 - DECODE_LOOKUP_SIZE)) + 32 | |
53 | #else | |
54 | # define MAX_LONG_CODE (2 << (15 - DECODE_LOOKUP_SIZE)) + (2 << (8 + DECODE_LOOKUP_SIZE)) + 32 | |
55 | #endif | |
56 | ||
57 | /* Buffer used to manage decompressed output */ | |
58 | struct inflate_out_buffer{ | |
59 | uint8_t *next_out; | |
60 | uint32_t avail_out; | |
61 | uint32_t total_out; | |
62 | }; | |
63 | ||
64 | /* Buffer used to manager compressed input */ | |
65 | struct inflate_in_buffer{ | |
66 | uint8_t *start; | |
67 | uint8_t *next_in; | |
68 | uint32_t avail_in; | |
69 | uint64_t read_in; | |
70 | int32_t read_in_length; | |
71 | }; | |
72 | ||
73 | /* Data structure used to store a huffman code for fast look up */ | |
74 | struct inflate_huff_code{ | |
75 | uint16_t small_code_lookup[ 1 << (DECODE_LOOKUP_SIZE)]; | |
76 | uint16_t long_code_lookup[MAX_LONG_CODE]; | |
77 | }; | |
78 | ||
79 | /* Structure contained current state of decompression of data */ | |
80 | struct inflate_state { | |
81 | struct inflate_out_buffer out_buffer; | |
82 | struct inflate_in_buffer in_buffer; | |
83 | struct inflate_huff_code lit_huff_code; | |
84 | struct inflate_huff_code dist_huff_code; | |
85 | uint8_t new_block; | |
86 | uint8_t bfinal; | |
87 | uint8_t btype; | |
88 | }; | |
89 | ||
90 | /*Performs a copy of length repeat_length data starting at dest - | |
91 | * lookback_distance into dest. This copy copies data previously copied when the | |
92 | * src buffer and the dest buffer overlap. */ | |
93 | void byte_copy(uint8_t *dest, uint64_t lookback_distance, int repeat_length); | |
94 | ||
95 | /* Initialize a struct in_buffer for use */ | |
96 | void init_inflate_in_buffer(struct inflate_in_buffer *inflate_in); | |
97 | ||
98 | /* Set up the in_stream used for the in_buffer*/ | |
99 | void set_inflate_in_buffer(struct inflate_in_buffer *inflate_in, uint8_t *in_stream, | |
100 | uint32_t in_size); | |
101 | ||
102 | /* Set up the out_stream used for the out_buffer */ | |
103 | void set_inflate_out_buffer(struct inflate_out_buffer *inflate_out, uint8_t *out_stream, | |
104 | uint32_t out_size); | |
105 | ||
106 | /* Load data from the in_stream into a buffer to allow for handling unaligned data*/ | |
107 | void inflate_in_load(struct inflate_in_buffer *inflate_in, int min_load); | |
108 | ||
109 | /* Returns the next bit_count bits from the in stream*/ | |
110 | uint64_t inflate_in_peek_bits(struct inflate_in_buffer *inflate_in, uint8_t bit_count); | |
111 | ||
112 | /* Shifts the in stream over by bit-count bits */ | |
113 | void inflate_in_shift_bits(struct inflate_in_buffer *inflate_in, uint8_t bit_count); | |
114 | ||
115 | /* Returns the next bit_count bits from the in stream and shifts the stream over | |
116 | * by bit-count bits */ | |
117 | uint64_t inflate_in_read_bits(struct inflate_in_buffer *inflate_in, uint8_t bit_count); | |
118 | ||
119 | /* Sets the inflate_huff_codes in state to be the huffcodes corresponding to the | |
120 | * deflate static header */ | |
121 | int setup_static_header(struct inflate_state *state); | |
122 | ||
123 | /* Sets result to the inflate_huff_code corresponding to the huffcode defined by | |
124 | * the lengths in huff_code_table,where count is a histogram of the appearance | |
125 | * of each code length */ | |
126 | void make_inflate_huff_code(struct inflate_huff_code *result, struct huff_code *huff_code_table, | |
127 | int table_length, uint16_t * count); | |
128 | ||
129 | /* Decodes the next symbol symbol in in_buffer using the huff code defined by | |
130 | * huff_code */ | |
131 | uint16_t decode_next(struct inflate_in_buffer *in_buffer, struct inflate_huff_code *huff_code); | |
132 | ||
133 | /* Reads data from the in_buffer and sets the huff code corresponding to that | |
134 | * data */ | |
135 | int setup_dynamic_header(struct inflate_state *state); | |
136 | ||
137 | /* Reads in the header pointed to by in_stream and sets up state to reflect that | |
138 | * header information*/ | |
139 | int read_header(struct inflate_state *state); | |
140 | ||
141 | /* Initialize a struct inflate_state for deflate compressed input data at in_stream and to output | |
142 | * data into out_stream */ | |
143 | void igzip_inflate_init(struct inflate_state *state, uint8_t *in_stream, uint32_t in_size, | |
144 | uint8_t *out_stream, uint64_t out_size); | |
145 | ||
146 | /* Decompress a deflate data. This function assumes a call to igzip_inflate_init | |
147 | * has been made to set up the state structure to allow for decompression.*/ | |
148 | int igzip_inflate(struct inflate_state *state); | |
149 | ||
150 | #endif //INFLATE_H |