iPXE
deflate.c File Reference

DEFLATE decompression algorithm. More...

#include <string.h>
#include <strings.h>
#include <errno.h>
#include <assert.h>
#include <ctype.h>
#include <ipxe/deflate.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
static const char * deflate_bin (unsigned long value, unsigned int bits)
 Transcribe binary value (for debugging)
static void deflate_set_length (struct deflate *deflate, unsigned int index, unsigned int bits)
 Set Huffman symbol length.
static unsigned int deflate_length (struct deflate *deflate, unsigned int index)
 Get Huffman symbol length.
static const char * deflate_alphabet_name (struct deflate *deflate, struct deflate_alphabet *alphabet)
 Determine Huffman alphabet name (for debugging)
static void deflate_dump_alphabet (struct deflate *deflate, struct deflate_alphabet *alphabet)
 Dump Huffman alphabet (for debugging)
static int deflate_alphabet (struct deflate *deflate, struct deflate_alphabet *alphabet, unsigned int count, unsigned int offset)
 Construct Huffman alphabet.
static int deflate_accumulate (struct deflate *deflate, unsigned int target)
 Attempt to accumulate bits from input stream.
static int deflate_consume (struct deflate *deflate, unsigned int count)
 Consume accumulated bits from the input stream.
static int deflate_extract (struct deflate *deflate, unsigned int target)
 Attempt to extract a fixed number of bits from input stream.
static int deflate_decode (struct deflate *deflate, struct deflate_alphabet *alphabet)
 Attempt to decode a Huffman-coded symbol from input stream.
static void deflate_discard_to_byte (struct deflate *deflate)
 Discard bits up to the next byte boundary.
static void deflate_copy (struct deflate_chunk *out, const void *in, size_t len)
 Copy data to output buffer (if available)
int deflate_inflate (struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
 Inflate compressed data.
void deflate_init (struct deflate *deflate, enum deflate_format format)
 Initialise decompressor.

Variables

static uint8_t deflate_reverse [256]
 Byte reversal table.
static uint8_t deflate_litlen_base [28]
 Literal/length base values.
static uint16_t deflate_distance_base [32]
 Distance base values.
static uint8_t deflate_codelen_map [19]
 Code length map.
static struct deflate_static_length_pattern deflate_static_length_patterns []
 Static Huffman alphabet length patterns.

Detailed Description

DEFLATE decompression algorithm.

This file implements the decompression half of the DEFLATE algorithm specified in RFC 1951.

Portions of this code are derived from wimboot's xca.c.

Definition in file deflate.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ deflate_bin()

const char * deflate_bin ( unsigned long value,
unsigned int bits )
static

Transcribe binary value (for debugging)

Parameters
valueValue
bitsLength of value (in bits)
Return values
stringTranscribed value

Definition at line 98 of file deflate.c.

98 {
99 static char buf[ ( 8 * sizeof ( value ) ) + 1 /* NUL */ ];
100 char *out = buf;
101
102 /* Sanity check */
103 assert ( bits < sizeof ( buf ) );
104
105 /* Transcribe value */
106 while ( bits-- )
107 *(out++) = ( ( value & ( 1 << bits ) ) ? '1' : '0' );
108 *out = '\0';
109
110 return buf;
111}
__be32 out[4]
Definition CIB_PRM.h:8
pseudo_bit_t value[0x00020]
Definition arbel.h:2
static volatile void * bits
Definition bitops.h:28
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50

References assert, bits, out, and value.

Referenced by deflate_decode(), deflate_dump_alphabet(), and deflate_extract().

◆ deflate_set_length()

void deflate_set_length ( struct deflate * deflate,
unsigned int index,
unsigned int bits )
static

Set Huffman symbol length.

Parameters
deflateDecompressor
indexIndex within lengths
bitsSymbol length (in bits)

Definition at line 120 of file deflate.c.

121 {
122
123 deflate->lengths[ index / 2 ] |= ( bits << ( 4 * ( index % 2 ) ) );
124}
long index
Definition bigint.h:65
Decompressor.
Definition deflate.h:156
uint8_t lengths[((DEFLATE_LITLEN_MAX_CODE+1)+(DEFLATE_DISTANCE_MAX_CODE+1)+1)/2]
Huffman code lengths.
Definition deflate.h:242

References bits, index, and deflate::lengths.

Referenced by deflate_inflate().

◆ deflate_length()

unsigned int deflate_length ( struct deflate * deflate,
unsigned int index )
static

Get Huffman symbol length.

Parameters
deflateDecompressor
indexIndex within lengths
Return values
bitsSymbol length (in bits)

Definition at line 133 of file deflate.c.

134 {
135
136 return ( ( deflate->lengths[ index / 2 ] >> ( 4 * ( index % 2 ) ) )
137 & 0x0f );
138}

References index, and deflate::lengths.

Referenced by deflate_alphabet().

◆ deflate_alphabet_name()

const char * deflate_alphabet_name ( struct deflate * deflate,
struct deflate_alphabet * alphabet )
static

Determine Huffman alphabet name (for debugging)

Parameters
deflateDecompressor
alphabetHuffman alphabet
Return values
nameAlphabet name

Definition at line 147 of file deflate.c.

148 {
149
150 if ( alphabet == &deflate->litlen ) {
151 return "litlen";
152 } else if ( alphabet == &deflate->distance_codelen ) {
153 return "distance/codelen";
154 } else {
155 return "<UNKNOWN>";
156 }
157}
struct deflate_alphabet distance_codelen
Distance and code length Huffman alphabet.
Definition deflate.h:217
struct deflate_alphabet litlen
Literal/length Huffman alphabet.
Definition deflate.h:199

References deflate::distance_codelen, and deflate::litlen.

Referenced by deflate_alphabet(), and deflate_dump_alphabet().

◆ deflate_dump_alphabet()

void deflate_dump_alphabet ( struct deflate * deflate,
struct deflate_alphabet * alphabet )
static

Dump Huffman alphabet (for debugging)

Parameters
deflateDecompressor
alphabetHuffman alphabet

Definition at line 165 of file deflate.c.

166 {
167 struct deflate_huf_symbols *huf_sym;
168 unsigned int bits;
169 unsigned int huf;
170 unsigned int i;
171
172 /* Do nothing unless debugging is enabled */
173 if ( ! DBG_EXTRA )
174 return;
175
176 /* Dump symbol table for each utilised length */
177 for ( bits = 1 ; bits <= ( sizeof ( alphabet->huf ) /
178 sizeof ( alphabet->huf[0] ) ) ; bits++ ) {
179 huf_sym = &alphabet->huf[ bits - 1 ];
180 if ( huf_sym->freq == 0 )
181 continue;
182 huf = ( huf_sym->start >> huf_sym->shift );
183 DBGC2 ( alphabet, "DEFLATE %p \"%s\" length %d start \"%s\" "
184 "freq %d:", deflate,
185 deflate_alphabet_name ( deflate, alphabet ), bits,
186 deflate_bin ( huf, huf_sym->bits ), huf_sym->freq );
187 for ( i = 0 ; i < huf_sym->freq ; i++ ) {
188 DBGC2 ( alphabet, " %03x",
189 huf_sym->raw[ huf + i ] );
190 }
191 DBGC2 ( alphabet, "\n" );
192 }
193
194 /* Dump quick lookup table */
195 DBGC2 ( alphabet, "DEFLATE %p \"%s\" quick lookup:", deflate,
196 deflate_alphabet_name ( deflate, alphabet ) );
197 for ( i = 0 ; i < ( sizeof ( alphabet->lookup ) /
198 sizeof ( alphabet->lookup[0] ) ) ; i++ ) {
199 DBGC2 ( alphabet, " %d", ( alphabet->lookup[i] + 1 ) );
200 }
201 DBGC2 ( alphabet, "\n" );
202}
static const char * deflate_bin(unsigned long value, unsigned int bits)
Transcribe binary value (for debugging)
Definition deflate.c:98
static const char * deflate_alphabet_name(struct deflate *deflate, struct deflate_alphabet *alphabet)
Determine Huffman alphabet name (for debugging)
Definition deflate.c:147
#define DBGC2(...)
Definition compiler.h:522
#define DBG_EXTRA
Definition compiler.h:319
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 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

References bits, deflate_huf_symbols::bits, DBG_EXTRA, DBGC2, deflate_alphabet_name(), deflate_bin(), deflate_huf_symbols::freq, deflate_alphabet::huf, deflate_alphabet::lookup, deflate_huf_symbols::raw, deflate_huf_symbols::shift, and deflate_huf_symbols::start.

Referenced by deflate_alphabet().

◆ deflate_alphabet()

int deflate_alphabet ( struct deflate * deflate,
struct deflate_alphabet * alphabet,
unsigned int count,
unsigned int offset )
static

Construct Huffman alphabet.

Parameters
deflateDecompressor
alphabetHuffman alphabet
countNumber of symbols
offsetStarting offset within length table
Return values
rcReturn status code

Definition at line 213 of file deflate.c.

215 {
216 struct deflate_huf_symbols *huf_sym;
217 unsigned int huf;
218 unsigned int cum_freq;
219 unsigned int bits;
220 unsigned int raw;
221 unsigned int adjustment;
222 unsigned int prefix;
223 int complete;
224
225 /* Clear symbol table */
226 memset ( alphabet->huf, 0, sizeof ( alphabet->huf ) );
227
228 /* Count number of symbols with each Huffman-coded length */
229 for ( raw = 0 ; raw < count ; raw++ ) {
230 bits = deflate_length ( deflate, ( raw + offset ) );
231 if ( bits )
232 alphabet->huf[ bits - 1 ].freq++;
233 }
234
235 /* Populate Huffman-coded symbol table */
236 huf = 0;
237 cum_freq = 0;
238 for ( bits = 1 ; bits <= ( sizeof ( alphabet->huf ) /
239 sizeof ( alphabet->huf[0] ) ) ; bits++ ) {
240 huf_sym = &alphabet->huf[ bits - 1 ];
241 huf_sym->bits = bits;
242 huf_sym->shift = ( 16 - bits );
243 huf_sym->start = ( huf << huf_sym->shift );
244 huf_sym->raw = &alphabet->raw[cum_freq];
245 huf += huf_sym->freq;
246 if ( huf > ( 1U << bits ) ) {
247 DBGC ( alphabet, "DEFLATE %p \"%s\" has too many "
248 "symbols with lengths <=%d\n", deflate,
249 deflate_alphabet_name ( deflate, alphabet ),
250 bits );
251 return -EINVAL;
252 }
253 huf <<= 1;
254 cum_freq += huf_sym->freq;
255 }
256 complete = ( huf == ( 1U << bits ) );
257
258 /* Populate raw symbol table */
259 for ( raw = 0 ; raw < count ; raw++ ) {
260 bits = deflate_length ( deflate, ( raw + offset ) );
261 if ( bits ) {
262 huf_sym = &alphabet->huf[ bits - 1 ];
263 *(huf_sym->raw++) = raw;
264 }
265 }
266
267 /* Adjust Huffman-coded symbol table raw pointers and populate
268 * quick lookup table.
269 */
270 for ( bits = 1 ; bits <= ( sizeof ( alphabet->huf ) /
271 sizeof ( alphabet->huf[0] ) ) ; bits++ ) {
272 huf_sym = &alphabet->huf[ bits - 1 ];
273
274 /* Adjust raw pointer */
275 huf_sym->raw -= huf_sym->freq; /* Reset to first symbol */
276 adjustment = ( huf_sym->start >> huf_sym->shift );
277 huf_sym->raw -= adjustment; /* Adjust for quick indexing */
278
279 /* Populate quick lookup table */
280 for ( prefix = ( huf_sym->start >> DEFLATE_HUFFMAN_QL_SHIFT ) ;
281 prefix < ( 1 << DEFLATE_HUFFMAN_QL_BITS ) ; prefix++ ) {
282 alphabet->lookup[prefix] = ( bits - 1 );
283 }
284 }
285
286 /* Dump alphabet (for debugging) */
287 deflate_dump_alphabet ( deflate, alphabet );
288
289 /* Check that there are no invalid codes */
290 if ( ! complete ) {
291 DBGC ( alphabet, "DEFLATE %p \"%s\" is incomplete\n", deflate,
292 deflate_alphabet_name ( deflate, alphabet ) );
293 return -EINVAL;
294 }
295
296 return 0;
297}
__be32 raw[7]
Definition CIB_PRM.h:0
uint16_t offset
Offset to command line.
Definition bzimage.h:3
static unsigned int deflate_length(struct deflate *deflate, unsigned int index)
Get Huffman symbol length.
Definition deflate.c:133
static void deflate_dump_alphabet(struct deflate *deflate, struct deflate_alphabet *alphabet)
Dump Huffman alphabet (for debugging)
Definition deflate.c:165
#define DEFLATE_HUFFMAN_QL_SHIFT
Quick lookup shift.
Definition deflate.h:82
#define DEFLATE_HUFFMAN_QL_BITS
Quick lookup length for a Huffman symbol (in bits)
Definition deflate.h:79
#define DBGC(...)
Definition compiler.h:505
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define EINVAL
Invalid argument.
Definition errno.h:429
void * memset(void *dest, int character, size_t len) __nonnull
uint16_t raw[0]
Raw symbols.
Definition deflate.h:144
char prefix[4]
Definition vmconsole.c:53

References bits, deflate_huf_symbols::bits, count, DBGC, deflate_alphabet_name(), deflate_dump_alphabet(), DEFLATE_HUFFMAN_QL_BITS, DEFLATE_HUFFMAN_QL_SHIFT, deflate_length(), EINVAL, deflate_huf_symbols::freq, deflate_alphabet::huf, deflate_alphabet::lookup, memset(), offset, prefix, deflate_alphabet::raw, deflate_huf_symbols::raw, raw, deflate_huf_symbols::shift, and deflate_huf_symbols::start.

◆ deflate_accumulate()

int deflate_accumulate ( struct deflate * deflate,
unsigned int target )
static

Attempt to accumulate bits from input stream.

Parameters
deflateDecompressor
targetNumber of bits to accumulate
Return values
excessNumber of excess bits accumulated (may be negative)

Definition at line 306 of file deflate.c.

307 {
309
310 while ( deflate->bits < target ) {
311
312 /* Check for end of input */
313 if ( deflate->in == deflate->end )
314 break;
315
316 /* Acquire byte from input */
317 byte = *(deflate->in++);
319 ( byte << deflate->bits ) );
322 ( 24 - deflate->bits ) ) );
323 deflate->bits += 8;
324
325 /* Sanity check */
326 assert ( deflate->bits <=
327 ( 8 * sizeof ( deflate->accumulator ) ) );
328 }
329
330 return ( deflate->bits - target );
331}
unsigned char uint8_t
Definition stdint.h:10
static uint8_t deflate_reverse[256]
Byte reversal table.
Definition deflate.c:51
unsigned char byte
Definition smc9000.h:38
uint32_t rotalumucca
Bit-reversed accumulator.
Definition deflate.h:177
unsigned int bits
Number of bits within the accumulator.
Definition deflate.h:179
const uint8_t * end
End of input data pointer.
Definition deflate.h:169
const uint8_t * in
Current input data pointer.
Definition deflate.h:167
uint32_t accumulator
Accumulator.
Definition deflate.h:172

References deflate::accumulator, assert, deflate::bits, deflate_reverse, deflate::end, deflate::in, and deflate::rotalumucca.

Referenced by deflate_decode(), deflate_extract(), and deflate_inflate().

◆ deflate_consume()

int deflate_consume ( struct deflate * deflate,
unsigned int count )
static

Consume accumulated bits from the input stream.

Parameters
deflateDecompressor
countNumber of accumulated bits to consume
Return values
dataConsumed bits

Definition at line 340 of file deflate.c.

340 {
341 int data;
342
343 /* Sanity check */
345
346 /* Extract data and consume bits */
347 data = ( deflate->accumulator & ( ( 1 << count ) - 1 ) );
350 deflate->bits -= count;
351
352 return data;
353}
uint8_t data[48]
Additional event data.
Definition ena.h:11

References deflate::accumulator, assert, bits, deflate::bits, count, data, and deflate::rotalumucca.

Referenced by deflate_decode(), deflate_discard_to_byte(), and deflate_extract().

◆ deflate_extract()

int deflate_extract ( struct deflate * deflate,
unsigned int target )
static

Attempt to extract a fixed number of bits from input stream.

Parameters
deflateDecompressor
targetNumber of bits to extract
Return values
dataExtracted bits (or negative if not yet accumulated)

Definition at line 362 of file deflate.c.

362 {
363 int excess;
364 int data;
365
366 /* Return immediately if we are attempting to extract zero bits */
367 if ( target == 0 )
368 return 0;
369
370 /* Attempt to accumulate bits */
371 excess = deflate_accumulate ( deflate, target );
372 if ( excess < 0 )
373 return excess;
374
375 /* Extract data and consume bits */
376 data = deflate_consume ( deflate, target );
377 DBGCP ( deflate, "DEFLATE %p extracted %s = %#x = %d\n", deflate,
378 deflate_bin ( data, target ), data, data );
379
380 return data;
381}
static int deflate_consume(struct deflate *deflate, unsigned int count)
Consume accumulated bits from the input stream.
Definition deflate.c:340
static int deflate_accumulate(struct deflate *deflate, unsigned int target)
Attempt to accumulate bits from input stream.
Definition deflate.c:306
#define DBGCP(...)
Definition compiler.h:539

References data, DBGCP, deflate_accumulate(), deflate_bin(), and deflate_consume().

Referenced by deflate_inflate().

◆ deflate_decode()

int deflate_decode ( struct deflate * deflate,
struct deflate_alphabet * alphabet )
static

Attempt to decode a Huffman-coded symbol from input stream.

Parameters
deflateDecompressor
alphabetHuffman alphabet
Return values
codeRaw code (or negative if not yet accumulated)

Definition at line 390 of file deflate.c.

391 {
392 struct deflate_huf_symbols *huf_sym;
393 uint16_t huf;
394 unsigned int lookup_index;
395 int excess;
396 unsigned int raw;
397
398 /* Attempt to accumulate maximum required number of bits.
399 * There may be fewer bits than this remaining in the stream,
400 * even if the stream still contains some complete
401 * Huffman-coded symbols.
402 */
404
405 /* Normalise the bit-reversed accumulated value to 16 bits */
406 huf = ( deflate->rotalumucca >> 16 );
407
408 /* Find symbol set for this length */
409 lookup_index = ( huf >> DEFLATE_HUFFMAN_QL_SHIFT );
410 huf_sym = &alphabet->huf[ alphabet->lookup[ lookup_index ] ];
411 while ( huf < huf_sym->start )
412 huf_sym--;
413
414 /* Calculate number of excess bits, and return if not yet complete */
415 excess = ( deflate->bits - huf_sym->bits );
416 if ( excess < 0 )
417 return excess;
418
419 /* Consume bits */
420 deflate_consume ( deflate, huf_sym->bits );
421
422 /* Look up raw symbol */
423 raw = huf_sym->raw[ huf >> huf_sym->shift ];
424 DBGCP ( deflate, "DEFLATE %p decoded %s = %#x = %d\n", deflate,
425 deflate_bin ( ( huf >> huf_sym->shift ), huf_sym->bits ),
426 raw, raw );
427
428 return raw;
429}
unsigned short uint16_t
Definition stdint.h:11
#define DEFLATE_HUFFMAN_BITS
Maximum length of a Huffman symbol (in bits)
Definition deflate.h:73
uint32_t start
Starting offset.
Definition netvsc.h:1

References deflate::bits, deflate_huf_symbols::bits, DBGCP, deflate_accumulate(), deflate_bin(), deflate_consume(), DEFLATE_HUFFMAN_BITS, DEFLATE_HUFFMAN_QL_SHIFT, deflate_alphabet::huf, deflate_alphabet::lookup, deflate_huf_symbols::raw, raw, deflate::rotalumucca, deflate_huf_symbols::shift, and start.

Referenced by deflate_inflate().

◆ deflate_discard_to_byte()

void deflate_discard_to_byte ( struct deflate * deflate)
static

Discard bits up to the next byte boundary.

Parameters
deflateDecompressor

Definition at line 436 of file deflate.c.

436 {
437
438 deflate_consume ( deflate, ( deflate->bits & 7 ) );
439}

References deflate::bits, and deflate_consume().

Referenced by deflate_inflate().

◆ deflate_copy()

void deflate_copy ( struct deflate_chunk * out,
const void * in,
size_t len )
static

Copy data to output buffer (if available)

Parameters
outOutput data buffer
inInput data
lenLength to copy

Definition at line 448 of file deflate.c.

449 {
450 const uint8_t *in_byte = in;
451 uint8_t *out_byte = ( out->data + out->offset );
452 size_t copy_len;
453
454 /* Copy data one byte at a time, to allow for overlap */
455 if ( out->offset < out->len ) {
456 copy_len = ( out->len - out->offset );
457 if ( copy_len > len )
458 copy_len = len;
459 while ( copy_len-- )
460 *(out_byte++) = *(in_byte++);
461 }
462 out->offset += len;
463}
__be32 in[4]
Definition CIB_PRM.h:7
ring len
Length.
Definition dwmac.h:226

References in, len, and out.

Referenced by deflate_inflate().

◆ deflate_inflate()

int deflate_inflate ( struct deflate * deflate,
const void * data,
size_t len,
struct deflate_chunk * out )

Inflate compressed data.

Parameters
deflateDecompressor
dataCompressed input data
lenLength of compressed input data
outOutput data buffer
Return values
rcReturn status code

The caller can use deflate_finished() to determine whether a successful return indicates that the decompressor is merely waiting for more input.

Data will not be written beyond the specified end of the output data buffer, but the offset within the output data buffer will be updated to reflect the amount that should have been written. The caller can use this to find the length of the decompressed data before allocating the output data buffer.

Definition at line 484 of file deflate.c.

485 {
486
487 /* Store input data pointers */
488 deflate->in = data;
489 deflate->end = ( data + len );
490
491 /* This could be implemented more neatly if gcc offered a
492 * means for enforcing tail recursion.
493 */
494 if ( deflate->resume ) {
495 goto *(deflate->resume);
496 } else switch ( deflate->format ) {
497 case DEFLATE_RAW: goto block_header;
498 case DEFLATE_ZLIB: goto zlib_header;
499 default: assert ( 0 );
500 }
501
502 zlib_header: {
503 int header;
504 int cm;
505
506 /* Extract header */
508 if ( header < 0 ) {
509 deflate->resume = &&zlib_header;
510 return 0;
511 }
512
513 /* Parse header */
515 if ( cm != ZLIB_HEADER_CM_DEFLATE ) {
516 DBGC ( deflate, "DEFLATE %p unsupported ZLIB "
517 "compression method %d\n", deflate, cm );
518 return -ENOTSUP;
519 }
520 if ( header & ( 1 << ZLIB_HEADER_FDICT_BIT ) ) {
521 DBGC ( deflate, "DEFLATE %p unsupported ZLIB preset "
522 "dictionary\n", deflate );
523 return -ENOTSUP;
524 }
525
526 /* Process first block header */
527 goto block_header;
528 }
529
530 block_header: {
531 int header;
532 int bfinal;
533 int btype;
534
535 /* Extract block header */
537 if ( header < 0 ) {
538 deflate->resume = &&block_header;
539 return 0;
540 }
541
542 /* Parse header */
544 bfinal = ( header & ( 1 << DEFLATE_HEADER_BFINAL_BIT ) );
545 btype = ( header >> DEFLATE_HEADER_BTYPE_LSB );
546 DBGC ( deflate, "DEFLATE %p found %sblock type %#x\n",
547 deflate, ( bfinal ? "final " : "" ), btype );
548 switch ( btype ) {
550 goto literal_block;
552 goto static_block;
554 goto dynamic_block;
555 default:
556 DBGC ( deflate, "DEFLATE %p unsupported block type "
557 "%#x\n", deflate, btype );
558 return -ENOTSUP;
559 }
560 }
561
562 literal_block: {
563
564 /* Discard any bits up to the next byte boundary */
566 }
567
568 literal_len: {
569 int llen;
570
571 /* Extract LEN field */
573 if ( llen < 0 ) {
574 deflate->resume = &&literal_len;
575 return 0;
576 }
577
578 /* Record length of literal data */
579 deflate->remaining = llen;
580 DBGC2 ( deflate, "DEFLATE %p literal block length %#04zx\n",
582 }
583
584 literal_nlen: {
585 int nlen;
586
587 /* Extract NLEN field */
589 if ( nlen < 0 ) {
590 deflate->resume = &&literal_nlen;
591 return 0;
592 }
593
594 /* Verify NLEN */
595 if ( ( ( deflate->remaining ^ ~nlen ) &
596 ( ( 1 << DEFLATE_LITERAL_LEN_BITS ) - 1 ) ) != 0 ) {
597 DBGC ( deflate, "DEFLATE %p invalid len/nlen "
598 "%#04zx/%#04x\n", deflate,
599 deflate->remaining, nlen );
600 return -EINVAL;
601 }
602 }
603
604 literal_data: {
605 size_t in_remaining;
606 size_t dlen;
607
608 /* Calculate available amount of literal data */
609 in_remaining = ( deflate->end - deflate->in );
610 dlen = deflate->remaining;
611 if ( dlen > in_remaining )
612 dlen = in_remaining;
613
614 /* Copy data to output buffer */
615 deflate_copy ( out, deflate->in, dlen );
616
617 /* Consume data from input buffer */
618 deflate->in += dlen;
619 deflate->remaining -= dlen;
620
621 /* Finish processing if we are blocked */
622 if ( deflate->remaining ) {
623 deflate->resume = &&literal_data;
624 return 0;
625 }
626
627 /* Otherwise, finish block */
628 goto block_done;
629 }
630
631 static_block: {
632 struct deflate_static_length_pattern *pattern;
633 uint8_t *lengths = deflate->lengths;
634
635 /* Construct static Huffman lengths as per RFC 1950 */
636 for ( pattern = deflate_static_length_patterns ;
637 pattern->count ; pattern++ ) {
638 memset ( lengths, pattern->fill, pattern->count );
639 lengths += pattern->count;
640 }
641 deflate->litlen_count = 288;
643 goto construct_alphabets;
644 }
645
646 dynamic_block:
647
648 dynamic_header: {
649 int header;
650 unsigned int hlit;
651 unsigned int hdist;
652 unsigned int hclen;
653
654 /* Extract block header */
656 if ( header < 0 ) {
657 deflate->resume = &&dynamic_header;
658 return 0;
659 }
660
661 /* Parse header */
662 hlit = ( ( header >> DEFLATE_DYNAMIC_HLIT_LSB ) &
664 hdist = ( ( header >> DEFLATE_DYNAMIC_HDIST_LSB ) &
666 hclen = ( ( header >> DEFLATE_DYNAMIC_HCLEN_LSB ) &
668 deflate->litlen_count = ( hlit + 257 );
669 deflate->distance_count = ( hdist + 1 );
671 deflate->length_target = ( hclen + 4 );
672 DBGC2 ( deflate, "DEFLATE %p dynamic block %d codelen, %d "
673 "litlen, %d distance\n", deflate,
676
677 /* Prepare for decoding code length code lengths */
678 memset ( &deflate->lengths, 0, sizeof ( deflate->lengths ) );
679 }
680
681 dynamic_codelen: {
682 int clen;
683 unsigned int index;
684 int rc;
685
686 /* Extract all code lengths */
688
689 /* Extract code length length */
690 clen = deflate_extract ( deflate,
692 if ( clen < 0 ) {
693 deflate->resume = &&dynamic_codelen;
694 return 0;
695 }
696
697 /* Store code length */
700 DBGCP ( deflate, "DEFLATE %p codelen for %d is %d\n",
701 deflate, index, clen );
702 }
703
704 /* Generate code length alphabet */
705 if ( ( rc = deflate_alphabet ( deflate,
708 0 ) ) != 0 )
709 return rc;
710
711 /* Prepare for decoding literal/length/distance code lengths */
712 memset ( &deflate->lengths, 0, sizeof ( deflate->lengths ) );
716 deflate->length = 0;
717 }
718
719 dynamic_litlen_distance: {
720 int clen;
721 int index;
722
723 /* Decode literal/length/distance code length */
725 if ( clen < 0 ) {
726 deflate->resume = &&dynamic_litlen_distance;
727 return 0;
728 }
729
730 /* Prepare for extra bits */
731 if ( clen < 16 ) {
732 deflate->length = clen;
733 deflate->extra_bits = 0;
734 deflate->dup_len = 1;
735 } else {
736 static const uint8_t dup_len[3] = { 3, 3, 11 };
737 static const uint8_t extra_bits[3] = { 2, 3, 7 };
738 index = ( clen - 16 );
739 deflate->dup_len = dup_len[index];
740 deflate->extra_bits = extra_bits[index];
741 if ( index )
742 deflate->length = 0;
743 }
744 }
745
746 dynamic_litlen_distance_extra: {
747 int extra;
748 unsigned int dup_len;
749
750 /* Extract extra bits */
752 if ( extra < 0 ) {
753 deflate->resume = &&dynamic_litlen_distance_extra;
754 return 0;
755 }
756
757 /* Store code lengths */
758 dup_len = ( deflate->dup_len + extra );
759 while ( ( deflate->length_index < deflate->length_target ) &&
760 dup_len-- ) {
762 deflate->length );
763 }
764
765 /* Process next literal/length or distance code
766 * length, if more are required.
767 */
769 goto dynamic_litlen_distance;
770
771 /* Construct alphabets */
772 goto construct_alphabets;
773 }
774
775 construct_alphabets: {
776 unsigned int distance_offset = deflate->litlen_count;
777 unsigned int distance_count = deflate->distance_count;
778 int rc;
779
780 /* Generate literal/length alphabet */
782 deflate->litlen_count, 0 ) ) !=0)
783 return rc;
784
785 /* Handle degenerate case of a single distance code
786 * (for which it is impossible to construct a valid,
787 * complete Huffman alphabet). RFC 1951 states:
788 *
789 * If only one distance code is used, it is encoded
790 * using one bit, not zero bits; in this case there
791 * is a single code length of one, with one unused
792 * code. One distance code of zero bits means that
793 * there are no distance codes used at all (the data
794 * is all literals).
795 *
796 * If we have only a single distance code, then we
797 * instead use two distance codes both with length 1.
798 * This results in a valid Huffman alphabet. The code
799 * "0" will mean distance code 0 (which is either
800 * correct or irrelevant), and the code "1" will mean
801 * distance code 1 (which is always irrelevant).
802 */
803 if ( deflate->distance_count == 1 ) {
804
805 deflate->lengths[0] = 0x11;
806 distance_offset = 0;
807 distance_count = 2;
808 }
809
810 /* Generate distance alphabet */
811 if ( ( rc = deflate_alphabet ( deflate,
813 distance_count,
814 distance_offset ) ) != 0 )
815 return rc;
816 }
817
818 lzhuf_litlen: {
819 int code;
821 unsigned int extra;
822 unsigned int bits;
823
824 /* Decode Huffman codes */
825 while ( 1 ) {
826
827 /* Decode Huffman code */
829 if ( code < 0 ) {
830 deflate->resume = &&lzhuf_litlen;
831 return 0;
832 }
833
834 /* Handle according to code type */
835 if ( code < DEFLATE_LITLEN_END ) {
836
837 /* Literal value: copy to output buffer */
838 byte = code;
839 DBGCP ( deflate, "DEFLATE %p literal %#02x "
840 "('%c')\n", deflate, byte,
841 ( isprint ( byte ) ? byte : '.' ) );
842 deflate_copy ( out, &byte, sizeof ( byte ) );
843
844 } else if ( code == DEFLATE_LITLEN_END ) {
845
846 /* End of block */
847 goto block_done;
848
849 } else {
850
851 /* Length code: process extra bits */
852 extra = ( code - DEFLATE_LITLEN_END - 1 );
853 if ( extra < 28 ) {
854 bits = ( extra / 4 );
855 if ( bits )
856 bits--;
860 } else {
861 deflate->extra_bits = 0;
862 deflate->dup_len = 258;
863 }
864 goto lzhuf_litlen_extra;
865 }
866 }
867 }
868
869 lzhuf_litlen_extra: {
870 int extra;
871
872 /* Extract extra bits */
874 if ( extra < 0 ) {
875 deflate->resume = &&lzhuf_litlen_extra;
876 return 0;
877 }
878
879 /* Update duplicate length */
881 }
882
883 lzhuf_distance: {
884 int code;
885 unsigned int extra;
886 unsigned int bits;
887
888 /* Decode Huffman code */
890 if ( code < 0 ) {
891 deflate->resume = &&lzhuf_distance;
892 return 0;
893 }
894
895 /* Process extra bits */
896 extra = code;
897 bits = ( extra / 2 );
898 if ( bits )
899 bits--;
902 }
903
904 lzhuf_distance_extra: {
905 int extra;
906 size_t dup_len;
907 size_t dup_distance;
908
909 /* Extract extra bits */
911 if ( extra < 0 ) {
912 deflate->resume = &&lzhuf_distance_extra;
913 return 0;
914 }
915
916 /* Update duplicate distance */
917 dup_distance = ( deflate->dup_distance + extra );
918 dup_len = deflate->dup_len;
919 DBGCP ( deflate, "DEFLATE %p duplicate length %zd distance "
920 "%zd\n", deflate, dup_len, dup_distance );
921
922 /* Sanity check */
923 if ( dup_distance > out->offset ) {
924 DBGC ( deflate, "DEFLATE %p bad distance %zd (max "
925 "%zd)\n", deflate, dup_distance, out->offset );
926 return -EINVAL;
927 }
928
929 /* Copy data, allowing for overlap */
930 deflate_copy ( out, ( out->data + out->offset - dup_distance ),
931 dup_len );
932
933 /* Process next literal/length symbol */
934 goto lzhuf_litlen;
935 }
936
937 block_done: {
938
939 DBGCP ( deflate, "DEFLATE %p end of block\n", deflate );
940
941 /* If this was not the final block, process next block header */
942 if ( ! ( deflate->header & ( 1 << DEFLATE_HEADER_BFINAL_BIT ) ))
943 goto block_header;
944
945 /* Otherwise, process footer (if any) */
946 switch ( deflate->format ) {
947 case DEFLATE_RAW: goto finished;
948 case DEFLATE_ZLIB: goto zlib_footer;
949 default: assert ( 0 );
950 }
951 }
952
953 zlib_footer: {
954
955 /* Discard any bits up to the next byte boundary */
957 }
958
959 zlib_adler32: {
960 int excess;
961
962 /* Accumulate the 32 bits of checksum. We don't check
963 * the value, stop processing immediately afterwards,
964 * and so don't have to worry about the nasty corner
965 * cases involved in calling deflate_extract() to
966 * obtain a full 32 bits.
967 */
969 if ( excess < 0 ) {
970 deflate->resume = &&zlib_adler32;
971 return 0;
972 }
973
974 /* Finish processing */
975 goto finished;
976 }
977
978 finished: {
979 /* Mark as finished and terminate */
980 DBGCP ( deflate, "DEFLATE %p finished\n", deflate );
982 return 0;
983 }
984}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static unsigned int code
Response code.
Definition hyperv.h:26
static int isprint(int character)
Check if character is printable.
Definition ctype.h:98
static void deflate_discard_to_byte(struct deflate *deflate)
Discard bits up to the next byte boundary.
Definition deflate.c:436
static int deflate_extract(struct deflate *deflate, unsigned int target)
Attempt to extract a fixed number of bits from input stream.
Definition deflate.c:362
static void deflate_set_length(struct deflate *deflate, unsigned int index, unsigned int bits)
Set Huffman symbol length.
Definition deflate.c:120
static uint16_t deflate_distance_base[32]
Distance base values.
Definition deflate.c:71
static void deflate_copy(struct deflate_chunk *out, const void *in, size_t len)
Copy data to output buffer (if available)
Definition deflate.c:448
static uint8_t deflate_litlen_base[28]
Literal/length base values.
Definition deflate.c:62
static int deflate_decode(struct deflate *deflate, struct deflate_alphabet *alphabet)
Attempt to decode a Huffman-coded symbol from input stream.
Definition deflate.c:390
static uint8_t deflate_codelen_map[19]
Code length map.
Definition deflate.c:74
static struct deflate_static_length_pattern deflate_static_length_patterns[]
Static Huffman alphabet length patterns.
Definition deflate.c:79
#define DEFLATE_HEADER_BFINAL_BIT
Block header final block flags bit.
Definition deflate.h:28
#define ZLIB_HEADER_CM_MASK
ZLIB header compression method mask.
Definition deflate.h:103
#define DEFLATE_HEADER_BITS
Block header length (in bits)
Definition deflate.h:25
#define DEFLATE_HEADER_BTYPE_STATIC
Block header type: static Huffman alphabet.
Definition deflate.h:40
#define ZLIB_HEADER_CM_DEFLATE
ZLIB header compression method: DEFLATE.
Definition deflate.h:106
#define DEFLATE_DYNAMIC_HDIST_MASK
Dynamic header HDIST field mask.
Definition deflate.h:61
#define DEFLATE_HEADER_BTYPE_LSB
Block header type LSB.
Definition deflate.h:31
#define DEFLATE_DYNAMIC_HLIT_LSB
Dynamic header HLIT field LSB.
Definition deflate.h:52
#define DEFLATE_DYNAMIC_HLIT_MASK
Dynamic header HLIT field mask.
Definition deflate.h:55
#define ZLIB_HEADER_BITS
ZLIB header length (in bits)
Definition deflate.h:97
#define ZLIB_HEADER_CM_LSB
ZLIB header compression method LSB.
Definition deflate.h:100
#define ZLIB_ADLER32_BITS
ZLIB ADLER32 length (in bits)
Definition deflate.h:112
#define DEFLATE_DYNAMIC_HCLEN_MASK
Dynamic header HCLEN field mask.
Definition deflate.h:67
#define DEFLATE_CODELEN_BITS
Dynamic header code length length (in bits)
Definition deflate.h:70
#define DEFLATE_DYNAMIC_BITS
Dynamic header length (in bits)
Definition deflate.h:49
#define DEFLATE_HEADER_BTYPE_DYNAMIC
Block header type: dynamic Huffman alphabet.
Definition deflate.h:43
#define DEFLATE_DYNAMIC_HCLEN_LSB
Dynamic header HCLEN field LSB.
Definition deflate.h:64
#define DEFLATE_LITERAL_LEN_BITS
Literal header LEN/NLEN field length (in bits)
Definition deflate.h:46
#define ZLIB_HEADER_FDICT_BIT
ZLIB header preset dictionary flag bit.
Definition deflate.h:109
#define DEFLATE_HEADER_BTYPE_LITERAL
Block header type: literal data.
Definition deflate.h:37
#define DEFLATE_DYNAMIC_HDIST_LSB
Dynamic header HDIST field LSB.
Definition deflate.h:58
#define DEFLATE_LITLEN_END
Literal/length end of block code.
Definition deflate.h:85
#define DEFLATE_CODELEN_MAX_CODE
Maximum value of a code length code.
Definition deflate.h:94
@ DEFLATE_ZLIB
ZLIB header and footer.
Definition deflate.h:21
@ DEFLATE_RAW
Raw DEFLATE data (no header or footer)
Definition deflate.h:19
struct ena_llq_option header
Header locations.
Definition ena.h:5
#define ENOTSUP
Operation not supported.
Definition errno.h:590
struct ib_mad_cm cm
Definition ib_mad.h:3
uint8_t extra
Signature extra byte.
Definition smbios.h:6
A Huffman-coded alphabet.
Definition deflate.h:134
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
unsigned int distance_count
Number of symbols in the distance Huffman alphabet.
Definition deflate.h:225
unsigned int header
Current block header.
Definition deflate.h:182
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
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
unsigned int length_index
Current length index within a set of code lengths.
Definition deflate.h:186
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
unsigned int extra_bits
Number of extra bits required.
Definition deflate.h:192

References assert, bits, cm, code, deflate_static_length_pattern::count, data, DBGC, DBGC2, DBGCP, deflate_accumulate(), DEFLATE_CODELEN_BITS, deflate_codelen_map, DEFLATE_CODELEN_MAX_CODE, deflate_copy(), deflate_decode(), deflate_discard_to_byte(), deflate_distance_base, DEFLATE_DYNAMIC_BITS, DEFLATE_DYNAMIC_HCLEN_LSB, DEFLATE_DYNAMIC_HCLEN_MASK, DEFLATE_DYNAMIC_HDIST_LSB, DEFLATE_DYNAMIC_HDIST_MASK, DEFLATE_DYNAMIC_HLIT_LSB, DEFLATE_DYNAMIC_HLIT_MASK, deflate_extract(), DEFLATE_HEADER_BFINAL_BIT, DEFLATE_HEADER_BITS, DEFLATE_HEADER_BTYPE_DYNAMIC, DEFLATE_HEADER_BTYPE_LITERAL, DEFLATE_HEADER_BTYPE_LSB, DEFLATE_HEADER_BTYPE_STATIC, DEFLATE_LITERAL_LEN_BITS, deflate_litlen_base, DEFLATE_LITLEN_END, DEFLATE_RAW, deflate_set_length(), deflate_static_length_patterns, DEFLATE_ZLIB, deflate::distance_codelen, deflate::distance_count, deflate::dup_distance, deflate::dup_len, EINVAL, deflate::end, ENOTSUP, extra, deflate::extra_bits, deflate_static_length_pattern::fill, deflate::format, deflate::header, header, deflate::in, index, isprint(), len, deflate::length, deflate::length_index, deflate::length_target, deflate::lengths, deflate::litlen, deflate::litlen_count, memset(), NULL, out, rc, deflate::remaining, deflate::resume, ZLIB_ADLER32_BITS, ZLIB_HEADER_BITS, ZLIB_HEADER_CM_DEFLATE, ZLIB_HEADER_CM_LSB, ZLIB_HEADER_CM_MASK, and ZLIB_HEADER_FDICT_BIT.

Referenced by deflate_okx(), png_image_data(), and zlib_deflate().

◆ deflate_init()

void deflate_init ( struct deflate * deflate,
enum deflate_format format )

Initialise decompressor.

Parameters
deflateDecompressor
formatCompression format code

Definition at line 992 of file deflate.c.

992 {
993 static int global_init_done;
994 uint8_t i;
995 uint8_t bit;
997 unsigned int base;
998 unsigned int bits;
999
1000 /* Perform global initialisation if required */
1001 if ( ! global_init_done ) {
1002
1003 /* Initialise byte reversal table */
1004 for ( i = 255 ; i ; i-- ) {
1005 for ( bit = 1, byte = 0 ; bit ; bit <<= 1 ) {
1006 byte <<= 1;
1007 if ( i & bit )
1008 byte |= 1;
1009 }
1010 deflate_reverse[i] = byte;
1011 }
1012
1013 /* Initialise literal/length extra bits table */
1014 base = 3;
1015 for ( i = 0 ; i < 28 ; i++ ) {
1016 bits = ( i / 4 );
1017 if ( bits )
1018 bits--;
1020 base += ( 1 << bits );
1021 }
1022 assert ( base == 259 ); /* sic */
1023
1024 /* Initialise distance extra bits table */
1025 base = 1;
1026 for ( i = 0 ; i < 30 ; i++ ) {
1027 bits = ( i / 2 );
1028 if ( bits )
1029 bits--;
1031 base += ( 1 << bits );
1032 }
1033 assert ( base == 32769 );
1034
1035 /* Record global initialisation as complete */
1036 global_init_done = 1;
1037 }
1038
1039 /* Initialise structure */
1040 memset ( deflate, 0, sizeof ( *deflate ) );
1042}
static unsigned int unsigned int bit
Definition bigint.h:392
uint32_t base
Base.
Definition librm.h:3
int const char * format
Definition xfer.h:105

References assert, base, bit, bits, deflate_distance_base, deflate_litlen_base, deflate_reverse, deflate::format, format, and memset().

Referenced by deflate_okx(), png_pixbuf(), and zlib_deflate().

Variable Documentation

◆ deflate_reverse

uint8_t deflate_reverse[256]
static

Byte reversal table.

For some insane reason, the DEFLATE format stores some values in bit-reversed order.

Definition at line 51 of file deflate.c.

Referenced by deflate_accumulate(), and deflate_init().

◆ deflate_litlen_base

uint8_t deflate_litlen_base[28]
static

Literal/length base values.

We include entries only for literal/length codes 257-284. Code 285 does not fit the pattern (it represents a length of 258; following the pattern from the earlier codes would give a length of 259), and has no extra bits. Codes 286-287 are invalid, but can occur. We treat any code greater than 284 as meaning "length 258, no extra bits".

Definition at line 62 of file deflate.c.

Referenced by deflate_inflate(), and deflate_init().

◆ deflate_distance_base

uint16_t deflate_distance_base[32]
static

Distance base values.

We include entries for all possible codes 0-31, avoiding the need to check for undefined codes 30 and 31 before performing the lookup. Codes 30 and 31 are never initialised, and will therefore be treated as meaning "14 extra bits, base distance 0".

Definition at line 71 of file deflate.c.

Referenced by deflate_inflate(), and deflate_init().

◆ deflate_codelen_map

uint8_t deflate_codelen_map[19]
static
Initial value:
= {
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
}

Code length map.

Definition at line 74 of file deflate.c.

74 {
75 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
76};

Referenced by deflate_inflate().

◆ deflate_static_length_patterns

struct deflate_static_length_pattern deflate_static_length_patterns[]
static
Initial value:
= {
{ 0x88, ( ( ( 143 - 0 ) + 1 ) / 2 ) },
{ 0x99, ( ( ( 255 - 144 ) + 1 ) / 2 ) },
{ 0x77, ( ( ( 279 - 256 ) + 1 ) / 2 ) },
{ 0x88, ( ( ( 287 - 280 ) + 1 ) / 2 ) },
{ 0x55, ( ( ( 31 - 0 ) + 1 ) / 2 ) },
{ 0, 0 }
}

Static Huffman alphabet length patterns.

Definition at line 79 of file deflate.c.

79 {
80 /* Literal/length code lengths */
81 { 0x88, ( ( ( 143 - 0 ) + 1 ) / 2 ) },
82 { 0x99, ( ( ( 255 - 144 ) + 1 ) / 2 ) },
83 { 0x77, ( ( ( 279 - 256 ) + 1 ) / 2 ) },
84 { 0x88, ( ( ( 287 - 280 ) + 1 ) / 2 ) },
85 /* Distance code lengths */
86 { 0x55, ( ( ( 31 - 0 ) + 1 ) / 2 ) },
87 /* End marker */
88 { 0, 0 }
89};

Referenced by deflate_inflate().