iPXE
deflate.h
Go to the documentation of this file.
1 #ifndef _IPXE_DEFLATE_H
2 #define _IPXE_DEFLATE_H
3 
4 /** @file
5  *
6  * DEFLATE decompression algorithm
7  *
8  */
9 
10 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11 
12 #include <stdint.h>
13 #include <string.h>
14 
15 /** Compression formats */
17  /** Raw DEFLATE data (no header or footer) */
19  /** ZLIB header and footer */
21 };
22 
23 /** Block header length (in bits) */
24 #define DEFLATE_HEADER_BITS 3
25 
26 /** Block header final block flags bit */
27 #define DEFLATE_HEADER_BFINAL_BIT 0
28 
29 /** Block header type LSB */
30 #define DEFLATE_HEADER_BTYPE_LSB 1
31 
32 /** Block header type mask */
33 #define DEFLATE_HEADER_BTYPE_MASK 0x03
34 
35 /** Block header type: literal data */
36 #define DEFLATE_HEADER_BTYPE_LITERAL 0
37 
38 /** Block header type: static Huffman alphabet */
39 #define DEFLATE_HEADER_BTYPE_STATIC 1
40 
41 /** Block header type: dynamic Huffman alphabet */
42 #define DEFLATE_HEADER_BTYPE_DYNAMIC 2
43 
44 /** Literal header LEN/NLEN field length (in bits) */
45 #define DEFLATE_LITERAL_LEN_BITS 16
46 
47 /** Dynamic header length (in bits) */
48 #define DEFLATE_DYNAMIC_BITS 14
49 
50 /** Dynamic header HLIT field LSB */
51 #define DEFLATE_DYNAMIC_HLIT_LSB 0
52 
53 /** Dynamic header HLIT field mask */
54 #define DEFLATE_DYNAMIC_HLIT_MASK 0x1f
55 
56 /** Dynamic header HDIST field LSB */
57 #define DEFLATE_DYNAMIC_HDIST_LSB 5
58 
59 /** Dynamic header HDIST field mask */
60 #define DEFLATE_DYNAMIC_HDIST_MASK 0x1f
61 
62 /** Dynamic header HCLEN field LSB */
63 #define DEFLATE_DYNAMIC_HCLEN_LSB 10
64 
65 /** Dynamic header HCLEN field mask */
66 #define DEFLATE_DYNAMIC_HCLEN_MASK 0x0f
67 
68 /** Dynamic header code length length (in bits) */
69 #define DEFLATE_CODELEN_BITS 3
70 
71 /** Maximum length of a Huffman symbol (in bits) */
72 #define DEFLATE_HUFFMAN_BITS 15
73 
74 /** Quick lookup length for a Huffman symbol (in bits)
75  *
76  * This is a policy decision.
77  */
78 #define DEFLATE_HUFFMAN_QL_BITS 7
79 
80 /** Quick lookup shift */
81 #define DEFLATE_HUFFMAN_QL_SHIFT ( 16 - DEFLATE_HUFFMAN_QL_BITS )
82 
83 /** Literal/length end of block code */
84 #define DEFLATE_LITLEN_END 256
85 
86 /** Maximum value of a literal/length code */
87 #define DEFLATE_LITLEN_MAX_CODE 287
88 
89 /** Maximum value of a distance code */
90 #define DEFLATE_DISTANCE_MAX_CODE 31
91 
92 /** Maximum value of a code length code */
93 #define DEFLATE_CODELEN_MAX_CODE 18
94 
95 /** ZLIB header length (in bits) */
96 #define ZLIB_HEADER_BITS 16
97 
98 /** ZLIB header compression method LSB */
99 #define ZLIB_HEADER_CM_LSB 0
100 
101 /** ZLIB header compression method mask */
102 #define ZLIB_HEADER_CM_MASK 0x0f
103 
104 /** ZLIB header compression method: DEFLATE */
105 #define ZLIB_HEADER_CM_DEFLATE 8
106 
107 /** ZLIB header preset dictionary flag bit */
108 #define ZLIB_HEADER_FDICT_BIT 13
109 
110 /** ZLIB ADLER32 length (in bits) */
111 #define ZLIB_ADLER32_BITS 32
112 
113 /** A Huffman-coded set of symbols of a given length */
115  /** Length of Huffman-coded symbols */
117  /** Shift to normalise symbols of this length to 16 bits */
119  /** Number of Huffman-coded symbols having this length */
121  /** First symbol of this length (normalised to 16 bits)
122  *
123  * Stored as a 32-bit value to allow the value 0x10000 to be
124  * used for empty sets of symbols longer than the maximum
125  * utilised length.
126  */
128  /** Raw symbols having this length */
130 };
131 
132 /** A Huffman-coded alphabet */
134  /** Huffman-coded symbol set for each length */
136  /** Quick lookup table */
138  /** Raw symbols
139  *
140  * Ordered by Huffman-coded symbol length, then by symbol
141  * value. This field has a variable length.
142  */
144 };
145 
146 /** A static Huffman alphabet length pattern */
148  /** Length pair */
150  /** Repetition count */
152 } __attribute__ (( packed ));
153 
154 /** Decompressor */
155 struct deflate {
156  /** Resume point
157  *
158  * Used as the target of a computed goto to jump to the
159  * appropriate point within the state machine.
160  */
161  void *resume;
162  /** Format */
164 
165  /** Current input data pointer */
166  const uint8_t *in;
167  /** End of input data pointer */
168  const uint8_t *end;
169 
170  /** Accumulator */
172  /** Bit-reversed accumulator
173  *
174  * Don't ask.
175  */
177  /** Number of bits within the accumulator */
178  unsigned int bits;
179 
180  /** Current block header */
181  unsigned int header;
182  /** Remaining length of data (e.g. within a literal block) */
183  size_t remaining;
184  /** Current length index within a set of code lengths */
185  unsigned int length_index;
186  /** Target length index within a set of code lengths */
187  unsigned int length_target;
188  /** Current length within a set of code lengths */
189  unsigned int length;
190  /** Number of extra bits required */
191  unsigned int extra_bits;
192  /** Length of a duplicated string */
193  size_t dup_len;
194  /** Distance of a duplicated string */
195  size_t dup_distance;
196 
197  /** Literal/length Huffman alphabet */
199  /** Literal/length raw symbols
200  *
201  * Must immediately follow the literal/length Huffman alphabet.
202  */
204  /** Number of symbols in the literal/length Huffman alphabet */
205  unsigned int litlen_count;
206 
207  /** Distance and code length Huffman alphabet
208  *
209  * The code length Huffman alphabet has a maximum Huffman
210  * symbol length of 7 and a maximum code value of 18, and is
211  * thus strictly smaller than the distance Huffman alphabet.
212  * Since we never need both alphabets simultaneously, we can
213  * reuse the storage space for the distance alphabet to
214  * temporarily hold the code length alphabet.
215  */
217  /** Distance and code length raw symbols
218  *
219  * Must immediately follow the distance and code length
220  * Huffman alphabet.
221  */
223  /** Number of symbols in the distance Huffman alphabet */
224  unsigned int distance_count;
225 
226  /** Huffman code lengths
227  *
228  * The literal/length and distance code lengths are
229  * constructed as a single set of lengths.
230  *
231  * The code length Huffman alphabet has a maximum code value
232  * of 18 and the set of lengths is thus strictly smaller than
233  * the combined literal/length and distance set of lengths.
234  * Since we never need both alphabets simultaneously, we can
235  * reuse the storage space for the literal/length and distance
236  * code lengths to temporarily hold the code length code
237  * lengths.
238  */
240  ( DEFLATE_DISTANCE_MAX_CODE + 1 ) +
241  1 /* round up */ ) / 2 ];
242 };
243 
244 /** A chunk of data */
246  /** Data */
247  void *data;
248  /** Current offset */
249  size_t offset;
250  /** Length of data */
251  size_t len;
252 };
253 
254 /**
255  * Initialise chunk of data
256  *
257  * @v chunk Chunk of data to initialise
258  * @v data Data
259  * @v offset Starting offset
260  * @v len Length
261  */
262 static inline __attribute__ (( always_inline )) void
263 deflate_chunk_init ( struct deflate_chunk *chunk, void *data,
264  size_t offset, size_t len ) {
265 
266  chunk->data = data;
267  chunk->offset = offset;
268  chunk->len = len;
269 }
270 
271 /**
272  * Check if decompression has finished
273  *
274  * @v deflate Decompressor
275  * @ret finished Decompression has finished
276  */
277 static inline int deflate_finished ( struct deflate *deflate ) {
278  return ( deflate->resume == NULL );
279 }
280 
281 extern void deflate_init ( struct deflate *deflate,
282  enum deflate_format format );
283 extern int deflate_inflate ( struct deflate *deflate,
284  const void *data, size_t len,
285  struct deflate_chunk *out );
286 
287 #endif /* _IPXE_DEFLATE_H */
void * data
Data.
Definition: deflate.h:247
unsigned short uint16_t
Definition: stdint.h:11
unsigned int length_index
Current length index within a set of code lengths.
Definition: deflate.h:185
struct deflate_alphabet distance_codelen
Distance and code length Huffman alphabet.
Definition: deflate.h:216
#define DEFLATE_LITLEN_MAX_CODE
Maximum value of a literal/length code.
Definition: deflate.h:87
unsigned int length_target
Target length index within a set of code lengths.
Definition: deflate.h:187
int deflate_inflate(struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
Inflate compressed data.
Definition: deflate.c:483
unsigned int extra_bits
Number of extra bits required.
Definition: deflate.h:191
#define DEFLATE_HUFFMAN_BITS
Maximum length of a Huffman symbol (in bits)
Definition: deflate.h:72
size_t dup_distance
Distance of a duplicated string.
Definition: deflate.h:195
unsigned int header
Current block header.
Definition: deflate.h:181
static void * data
Definition: deflate.h:263
uint16_t raw[0]
Raw symbols.
Definition: deflate.h:143
enum deflate_format format
Format.
Definition: deflate.h:163
unsigned int length
Current length within a set of code lengths.
Definition: deflate.h:189
A Huffman-coded set of symbols of a given length.
Definition: deflate.h:114
uint32_t start
First symbol of this length (normalised to 16 bits)
Definition: deflate.h:127
A Huffman-coded alphabet.
Definition: deflate.h:133
static void size_t size_t len
Definition: deflate.h:264
uint8_t count
Repetition count.
Definition: deflate.h:151
__be32 out[4]
Definition: CIB_PRM.h:36
unsigned int bits
Number of bits within the accumulator.
Definition: deflate.h:178
uint16_t distance_codelen_raw[DEFLATE_DISTANCE_MAX_CODE+1]
Distance and code length raw symbols.
Definition: deflate.h:222
static void size_t offset
Definition: deflate.h:263
A chunk of data.
Definition: deflate.h:245
uint32_t accumulator
Accumulator.
Definition: deflate.h:171
size_t dup_len
Length of a duplicated string.
Definition: deflate.h:193
void * resume
Resume point.
Definition: deflate.h:161
static int deflate_finished(struct deflate *deflate)
Check if decompression has finished.
Definition: deflate.h:277
#define DEFLATE_HUFFMAN_QL_BITS
Quick lookup length for a Huffman symbol (in bits)
Definition: deflate.h:78
uint8_t lookup[1<< DEFLATE_HUFFMAN_QL_BITS]
Quick lookup table.
Definition: deflate.h:137
struct deflate_huf_symbols huf[DEFLATE_HUFFMAN_BITS]
Huffman-coded symbol set for each length.
Definition: deflate.h:135
uint16_t freq
Number of Huffman-coded symbols having this length.
Definition: deflate.h:120
ZLIB header and footer.
Definition: deflate.h:20
uint8_t fill
Length pair.
Definition: deflate.h:149
deflate_format
Compression formats.
Definition: deflate.h:16
struct deflate __attribute__
uint32_t rotalumucca
Bit-reversed accumulator.
Definition: deflate.h:176
unsigned char uint8_t
Definition: stdint.h:10
unsigned int distance_count
Number of symbols in the distance Huffman alphabet.
Definition: deflate.h:224
size_t offset
Current offset.
Definition: deflate.h:249
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
unsigned int uint32_t
Definition: stdint.h:12
struct deflate_alphabet litlen
Literal/length Huffman alphabet.
Definition: deflate.h:198
uint16_t * raw
Raw symbols having this length.
Definition: deflate.h:129
#define DEFLATE_DISTANCE_MAX_CODE
Maximum value of a distance code.
Definition: deflate.h:90
const uint8_t * in
Current input data pointer.
Definition: deflate.h:166
Raw DEFLATE data (no header or footer)
Definition: deflate.h:18
A static Huffman alphabet length pattern.
Definition: deflate.h:147
const uint8_t * end
End of input data pointer.
Definition: deflate.h:168
unsigned int litlen_count
Number of symbols in the literal/length Huffman alphabet.
Definition: deflate.h:205
uint8_t lengths[((DEFLATE_LITLEN_MAX_CODE+1)+(DEFLATE_DISTANCE_MAX_CODE+1)+1)/2]
Huffman code lengths.
Definition: deflate.h:241
uint8_t shift
Shift to normalise symbols of this length to 16 bits.
Definition: deflate.h:118
uint16_t litlen_raw[DEFLATE_LITLEN_MAX_CODE+1]
Literal/length raw symbols.
Definition: deflate.h:203
int const char * format
Definition: xfer.h:104
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
size_t len
Length of data.
Definition: deflate.h:251
uint8_t bits
Length of Huffman-coded symbols.
Definition: deflate.h:116
String functions.
size_t remaining
Remaining length of data (e.g.
Definition: deflate.h:183
void deflate_init(struct deflate *deflate, enum deflate_format format)
Initialise decompressor.
Definition: deflate.c:991
Decompressor.
Definition: deflate.h:155