1 #ifndef _IPXE_DEFLATE_H
2 #define _IPXE_DEFLATE_H
6 * DEFLATE decompression algorithm
10 FILE_LICENCE ( GPL2_OR_LATER );
14 #include <ipxe/uaccess.h>
16 /** Compression formats */
18 /** Raw DEFLATE data (no header or footer) */
20 /** ZLIB header and footer */
24 /** Block header length (in bits) */
25 #define DEFLATE_HEADER_BITS 3
27 /** Block header final block flags bit */
28 #define DEFLATE_HEADER_BFINAL_BIT 0
30 /** Block header type LSB */
31 #define DEFLATE_HEADER_BTYPE_LSB 1
33 /** Block header type mask */
34 #define DEFLATE_HEADER_BTYPE_MASK 0x03
36 /** Block header type: literal data */
37 #define DEFLATE_HEADER_BTYPE_LITERAL 0
39 /** Block header type: static Huffman alphabet */
40 #define DEFLATE_HEADER_BTYPE_STATIC 1
42 /** Block header type: dynamic Huffman alphabet */
43 #define DEFLATE_HEADER_BTYPE_DYNAMIC 2
45 /** Literal header LEN/NLEN field length (in bits) */
46 #define DEFLATE_LITERAL_LEN_BITS 16
48 /** Dynamic header length (in bits) */
49 #define DEFLATE_DYNAMIC_BITS 14
51 /** Dynamic header HLIT field LSB */
52 #define DEFLATE_DYNAMIC_HLIT_LSB 0
54 /** Dynamic header HLIT field mask */
55 #define DEFLATE_DYNAMIC_HLIT_MASK 0x1f
57 /** Dynamic header HDIST field LSB */
58 #define DEFLATE_DYNAMIC_HDIST_LSB 5
60 /** Dynamic header HDIST field mask */
61 #define DEFLATE_DYNAMIC_HDIST_MASK 0x1f
63 /** Dynamic header HCLEN field LSB */
64 #define DEFLATE_DYNAMIC_HCLEN_LSB 10
66 /** Dynamic header HCLEN field mask */
67 #define DEFLATE_DYNAMIC_HCLEN_MASK 0x0f
69 /** Dynamic header code length length (in bits) */
70 #define DEFLATE_CODELEN_BITS 3
72 /** Maximum length of a Huffman symbol (in bits) */
73 #define DEFLATE_HUFFMAN_BITS 15
75 /** Quick lookup length for a Huffman symbol (in bits)
77 * This is a policy decision.
79 #define DEFLATE_HUFFMAN_QL_BITS 7
81 /** Quick lookup shift */
82 #define DEFLATE_HUFFMAN_QL_SHIFT ( 16 - DEFLATE_HUFFMAN_QL_BITS )
84 /** Literal/length end of block code */
85 #define DEFLATE_LITLEN_END 256
87 /** Maximum value of a literal/length code */
88 #define DEFLATE_LITLEN_MAX_CODE 287
90 /** Maximum value of a distance code */
91 #define DEFLATE_DISTANCE_MAX_CODE 31
93 /** Maximum value of a code length code */
94 #define DEFLATE_CODELEN_MAX_CODE 18
96 /** ZLIB header length (in bits) */
97 #define ZLIB_HEADER_BITS 16
99 /** ZLIB header compression method LSB */
100 #define ZLIB_HEADER_CM_LSB 0
102 /** ZLIB header compression method mask */
103 #define ZLIB_HEADER_CM_MASK 0x0f
105 /** ZLIB header compression method: DEFLATE */
106 #define ZLIB_HEADER_CM_DEFLATE 8
108 /** ZLIB header preset dictionary flag bit */
109 #define ZLIB_HEADER_FDICT_BIT 13
111 /** ZLIB ADLER32 length (in bits) */
112 #define ZLIB_ADLER32_BITS 32
114 /** A Huffman-coded set of symbols of a given length */
115 struct deflate_huf_symbols {
116 /** Length of Huffman-coded symbols */
118 /** Shift to normalise symbols of this length to 16 bits */
120 /** Number of Huffman-coded symbols having this length */
122 /** First symbol of this length (normalised to 16 bits)
124 * Stored as a 32-bit value to allow the value 0x10000 to be
125 * used for empty sets of symbols longer than the maximum
129 /** Raw symbols having this length */
133 /** A Huffman-coded alphabet */
134 struct deflate_alphabet {
135 /** Huffman-coded symbol set for each length */
136 struct deflate_huf_symbols huf[DEFLATE_HUFFMAN_BITS];
137 /** Quick lookup table */
138 uint8_t lookup[ 1 << DEFLATE_HUFFMAN_QL_BITS ];
141 * Ordered by Huffman-coded symbol length, then by symbol
142 * value. This field has a variable length.
147 /** A static Huffman alphabet length pattern */
148 struct deflate_static_length_pattern {
151 /** Repetition count */
153 } __attribute__ (( packed ));
159 * Used as the target of a computed goto to jump to the
160 * appropriate point within the state machine.
164 enum deflate_format format;
167 uint32_t accumulator;
168 /** Bit-reversed accumulator
172 uint32_t rotalumucca;
173 /** Number of bits within the accumulator */
176 /** Current block header */
178 /** Remaining length of data (e.g. within a literal block) */
180 /** Current length index within a set of code lengths */
181 unsigned int length_index;
182 /** Target length index within a set of code lengths */
183 unsigned int length_target;
184 /** Current length within a set of code lengths */
186 /** Number of extra bits required */
187 unsigned int extra_bits;
188 /** Length of a duplicated string */
190 /** Distance of a duplicated string */
193 /** Literal/length Huffman alphabet */
194 struct deflate_alphabet litlen;
195 /** Literal/length raw symbols
197 * Must immediately follow the literal/length Huffman alphabet.
199 uint16_t litlen_raw[ DEFLATE_LITLEN_MAX_CODE + 1 ];
200 /** Number of symbols in the literal/length Huffman alphabet */
201 unsigned int litlen_count;
203 /** Distance and code length Huffman alphabet
205 * The code length Huffman alphabet has a maximum Huffman
206 * symbol length of 7 and a maximum code value of 18, and is
207 * thus strictly smaller than the distance Huffman alphabet.
208 * Since we never need both alphabets simultaneously, we can
209 * reuse the storage space for the distance alphabet to
210 * temporarily hold the code length alphabet.
212 struct deflate_alphabet distance_codelen;
213 /** Distance and code length raw symbols
215 * Must immediately follow the distance and code length
218 uint16_t distance_codelen_raw[ DEFLATE_DISTANCE_MAX_CODE + 1 ];
219 /** Number of symbols in the distance Huffman alphabet */
220 unsigned int distance_count;
222 /** Huffman code lengths
224 * The literal/length and distance code lengths are
225 * constructed as a single set of lengths.
227 * The code length Huffman alphabet has a maximum code value
228 * of 18 and the set of lengths is thus strictly smaller than
229 * the combined literal/length and distance set of lengths.
230 * Since we never need both alphabets simultaneously, we can
231 * reuse the storage space for the literal/length and distance
232 * code lengths to temporarily hold the code length code
235 uint8_t lengths[ ( ( DEFLATE_LITLEN_MAX_CODE + 1 ) +
236 ( DEFLATE_DISTANCE_MAX_CODE + 1 ) +
237 1 /* round up */ ) / 2 ];
240 /** A chunk of data */
241 struct deflate_chunk {
244 /** Current offset */
246 /** Length of data */
251 * Initialise chunk of data
253 * @v chunk Chunk of data to initialise
255 * @v offset Starting offset
258 static inline __attribute__ (( always_inline )) void
259 deflate_chunk_init ( struct deflate_chunk *chunk, userptr_t data,
260 size_t offset, size_t len ) {
263 chunk->offset = offset;
268 * Check if decompression has finished
270 * @v deflate Decompressor
271 * @ret finished Decompression has finished
273 static inline int deflate_finished ( struct deflate *deflate ) {
274 return ( deflate->resume == NULL );
277 extern void deflate_init ( struct deflate *deflate,
278 enum deflate_format format );
279 extern int deflate_inflate ( struct deflate *deflate,
280 struct deflate_chunk *in,
281 struct deflate_chunk *out );
283 #endif /* _IPXE_DEFLATE_H */