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