iPXE
deflate.h File Reference

DEFLATE decompression algorithm. More...

#include <stdint.h>
#include <string.h>

Go to the source code of this file.

Data Structures

struct  deflate_huf_symbols
 A Huffman-coded set of symbols of a given length. More...
struct  deflate_alphabet
 A Huffman-coded alphabet. More...
struct  deflate_static_length_pattern
 A static Huffman alphabet length pattern. More...
struct  deflate
 Decompressor. More...
struct  deflate_chunk
 A chunk of data. More...

Macros

#define DEFLATE_HEADER_BITS   3
 Block header length (in bits)
#define DEFLATE_HEADER_BFINAL_BIT   0
 Block header final block flags bit.
#define DEFLATE_HEADER_BTYPE_LSB   1
 Block header type LSB.
#define DEFLATE_HEADER_BTYPE_MASK   0x03
 Block header type mask.
#define DEFLATE_HEADER_BTYPE_LITERAL   0
 Block header type: literal data.
#define DEFLATE_HEADER_BTYPE_STATIC   1
 Block header type: static Huffman alphabet.
#define DEFLATE_HEADER_BTYPE_DYNAMIC   2
 Block header type: dynamic Huffman alphabet.
#define DEFLATE_LITERAL_LEN_BITS   16
 Literal header LEN/NLEN field length (in bits)
#define DEFLATE_DYNAMIC_BITS   14
 Dynamic header length (in bits)
#define DEFLATE_DYNAMIC_HLIT_LSB   0
 Dynamic header HLIT field LSB.
#define DEFLATE_DYNAMIC_HLIT_MASK   0x1f
 Dynamic header HLIT field mask.
#define DEFLATE_DYNAMIC_HDIST_LSB   5
 Dynamic header HDIST field LSB.
#define DEFLATE_DYNAMIC_HDIST_MASK   0x1f
 Dynamic header HDIST field mask.
#define DEFLATE_DYNAMIC_HCLEN_LSB   10
 Dynamic header HCLEN field LSB.
#define DEFLATE_DYNAMIC_HCLEN_MASK   0x0f
 Dynamic header HCLEN field mask.
#define DEFLATE_CODELEN_BITS   3
 Dynamic header code length length (in bits)
#define DEFLATE_HUFFMAN_BITS   15
 Maximum length of a Huffman symbol (in bits)
#define DEFLATE_HUFFMAN_QL_BITS   7
 Quick lookup length for a Huffman symbol (in bits)
#define DEFLATE_HUFFMAN_QL_SHIFT   ( 16 - DEFLATE_HUFFMAN_QL_BITS )
 Quick lookup shift.
#define DEFLATE_LITLEN_END   256
 Literal/length end of block code.
#define DEFLATE_LITLEN_MAX_CODE   287
 Maximum value of a literal/length code.
#define DEFLATE_DISTANCE_MAX_CODE   31
 Maximum value of a distance code.
#define DEFLATE_CODELEN_MAX_CODE   18
 Maximum value of a code length code.
#define ZLIB_HEADER_BITS   16
 ZLIB header length (in bits)
#define ZLIB_HEADER_CM_LSB   0
 ZLIB header compression method LSB.
#define ZLIB_HEADER_CM_MASK   0x0f
 ZLIB header compression method mask.
#define ZLIB_HEADER_CM_DEFLATE   8
 ZLIB header compression method: DEFLATE.
#define ZLIB_HEADER_FDICT_BIT   13
 ZLIB header preset dictionary flag bit.
#define ZLIB_ADLER32_BITS   32
 ZLIB ADLER32 length (in bits)

Enumerations

enum  deflate_format { DEFLATE_RAW , DEFLATE_ZLIB }
 Compression formats. More...

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
struct deflate_static_length_pattern __attribute__ ((packed))
static __attribute__ ((always_inline)) void deflate_chunk_init(struct deflate_chunk *chunk
 Initialise chunk of data.
static int deflate_finished (struct deflate *deflate)
 Check if decompression has finished.
void deflate_init (struct deflate *deflate, enum deflate_format format)
 Initialise decompressor.
int deflate_inflate (struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
 Inflate compressed data.

Variables

uint8_t fill
 Length pair.
uint8_t count
 Repetition count.
struct deflate __attribute__
static void * data
static void size_t offset = offset
static void size_t size_t len

Detailed Description

DEFLATE decompression algorithm.

Definition in file deflate.h.

Macro Definition Documentation

◆ DEFLATE_HEADER_BITS

#define DEFLATE_HEADER_BITS   3

Block header length (in bits)

Definition at line 25 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_HEADER_BFINAL_BIT

#define DEFLATE_HEADER_BFINAL_BIT   0

Block header final block flags bit.

Definition at line 28 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_HEADER_BTYPE_LSB

#define DEFLATE_HEADER_BTYPE_LSB   1

Block header type LSB.

Definition at line 31 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_HEADER_BTYPE_MASK

#define DEFLATE_HEADER_BTYPE_MASK   0x03

Block header type mask.

Definition at line 34 of file deflate.h.

◆ DEFLATE_HEADER_BTYPE_LITERAL

#define DEFLATE_HEADER_BTYPE_LITERAL   0

Block header type: literal data.

Definition at line 37 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_HEADER_BTYPE_STATIC

#define DEFLATE_HEADER_BTYPE_STATIC   1

Block header type: static Huffman alphabet.

Definition at line 40 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_HEADER_BTYPE_DYNAMIC

#define DEFLATE_HEADER_BTYPE_DYNAMIC   2

Block header type: dynamic Huffman alphabet.

Definition at line 43 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_LITERAL_LEN_BITS

#define DEFLATE_LITERAL_LEN_BITS   16

Literal header LEN/NLEN field length (in bits)

Definition at line 46 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_DYNAMIC_BITS

#define DEFLATE_DYNAMIC_BITS   14

Dynamic header length (in bits)

Definition at line 49 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_DYNAMIC_HLIT_LSB

#define DEFLATE_DYNAMIC_HLIT_LSB   0

Dynamic header HLIT field LSB.

Definition at line 52 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_DYNAMIC_HLIT_MASK

#define DEFLATE_DYNAMIC_HLIT_MASK   0x1f

Dynamic header HLIT field mask.

Definition at line 55 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_DYNAMIC_HDIST_LSB

#define DEFLATE_DYNAMIC_HDIST_LSB   5

Dynamic header HDIST field LSB.

Definition at line 58 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_DYNAMIC_HDIST_MASK

#define DEFLATE_DYNAMIC_HDIST_MASK   0x1f

Dynamic header HDIST field mask.

Definition at line 61 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_DYNAMIC_HCLEN_LSB

#define DEFLATE_DYNAMIC_HCLEN_LSB   10

Dynamic header HCLEN field LSB.

Definition at line 64 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_DYNAMIC_HCLEN_MASK

#define DEFLATE_DYNAMIC_HCLEN_MASK   0x0f

Dynamic header HCLEN field mask.

Definition at line 67 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_CODELEN_BITS

#define DEFLATE_CODELEN_BITS   3

Dynamic header code length length (in bits)

Definition at line 70 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_HUFFMAN_BITS

#define DEFLATE_HUFFMAN_BITS   15

Maximum length of a Huffman symbol (in bits)

Definition at line 73 of file deflate.h.

Referenced by deflate_decode().

◆ DEFLATE_HUFFMAN_QL_BITS

#define DEFLATE_HUFFMAN_QL_BITS   7

Quick lookup length for a Huffman symbol (in bits)

This is a policy decision.

Definition at line 79 of file deflate.h.

Referenced by deflate_alphabet().

◆ DEFLATE_HUFFMAN_QL_SHIFT

#define DEFLATE_HUFFMAN_QL_SHIFT   ( 16 - DEFLATE_HUFFMAN_QL_BITS )

Quick lookup shift.

Definition at line 82 of file deflate.h.

Referenced by deflate_alphabet(), and deflate_decode().

◆ DEFLATE_LITLEN_END

#define DEFLATE_LITLEN_END   256

Literal/length end of block code.

Definition at line 85 of file deflate.h.

Referenced by deflate_inflate().

◆ DEFLATE_LITLEN_MAX_CODE

#define DEFLATE_LITLEN_MAX_CODE   287

Maximum value of a literal/length code.

Definition at line 88 of file deflate.h.

◆ DEFLATE_DISTANCE_MAX_CODE

#define DEFLATE_DISTANCE_MAX_CODE   31

Maximum value of a distance code.

Definition at line 91 of file deflate.h.

◆ DEFLATE_CODELEN_MAX_CODE

#define DEFLATE_CODELEN_MAX_CODE   18

Maximum value of a code length code.

Definition at line 94 of file deflate.h.

Referenced by deflate_inflate().

◆ ZLIB_HEADER_BITS

#define ZLIB_HEADER_BITS   16

ZLIB header length (in bits)

Definition at line 97 of file deflate.h.

Referenced by deflate_inflate().

◆ ZLIB_HEADER_CM_LSB

#define ZLIB_HEADER_CM_LSB   0

ZLIB header compression method LSB.

Definition at line 100 of file deflate.h.

Referenced by deflate_inflate().

◆ ZLIB_HEADER_CM_MASK

#define ZLIB_HEADER_CM_MASK   0x0f

ZLIB header compression method mask.

Definition at line 103 of file deflate.h.

Referenced by deflate_inflate().

◆ ZLIB_HEADER_CM_DEFLATE

#define ZLIB_HEADER_CM_DEFLATE   8

ZLIB header compression method: DEFLATE.

Definition at line 106 of file deflate.h.

Referenced by deflate_inflate().

◆ ZLIB_HEADER_FDICT_BIT

#define ZLIB_HEADER_FDICT_BIT   13

ZLIB header preset dictionary flag bit.

Definition at line 109 of file deflate.h.

Referenced by deflate_inflate().

◆ ZLIB_ADLER32_BITS

#define ZLIB_ADLER32_BITS   32

ZLIB ADLER32 length (in bits)

Definition at line 112 of file deflate.h.

Referenced by deflate_inflate().

Enumeration Type Documentation

◆ deflate_format

Compression formats.

Enumerator
DEFLATE_RAW 

Raw DEFLATE data (no header or footer)

DEFLATE_ZLIB 

ZLIB header and footer.

Definition at line 17 of file deflate.h.

17 {
18 /** Raw DEFLATE data (no header or footer) */
20 /** ZLIB header and footer */
22};
@ DEFLATE_ZLIB
ZLIB header and footer.
Definition deflate.h:21
@ DEFLATE_RAW
Raw DEFLATE data (no header or footer)
Definition deflate.h:19

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ __attribute__() [1/2]

struct deflate_static_length_pattern __attribute__ ( (packed) )

◆ __attribute__() [2/2]

__attribute__ ( (always_inline) )
inlinestatic

Initialise chunk of data.

Parameters
chunkChunk of data to initialise
dataData
offsetStarting offset
lenLength

◆ deflate_finished()

int deflate_finished ( struct deflate * deflate)
inlinestatic

Check if decompression has finished.

Parameters
deflateDecompressor
Return values
finishedDecompression has finished

Definition at line 278 of file deflate.h.

278 {
279 return ( deflate->resume == NULL );
280}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
Decompressor.
Definition deflate.h:156
void * resume
Resume point.
Definition deflate.h:162

References NULL, and deflate::resume.

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

◆ deflate_init()

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

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}
unsigned char uint8_t
Definition stdint.h:10
static volatile void * bits
Definition bitops.h:28
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
static uint8_t deflate_reverse[256]
Byte reversal table.
Definition deflate.c:51
static uint16_t deflate_distance_base[32]
Distance base values.
Definition deflate.c:71
static uint8_t deflate_litlen_base[28]
Literal/length base values.
Definition deflate.c:62
static unsigned int unsigned int bit
Definition bigint.h:392
void * memset(void *dest, int character, size_t len) __nonnull
uint32_t base
Base.
Definition librm.h:3
unsigned char byte
Definition smc9000.h:38
enum deflate_format format
Format.
Definition deflate.h:164
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().

◆ deflate_inflate()

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

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}
__be32 out[4]
Definition CIB_PRM.h:8
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
static unsigned int code
Response code.
Definition hyperv.h:26
long index
Definition bigint.h:65
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 int deflate_accumulate(struct deflate *deflate, unsigned int target)
Attempt to accumulate bits from input stream.
Definition deflate.c:306
static void deflate_set_length(struct deflate *deflate, unsigned int index, unsigned int bits)
Set Huffman symbol length.
Definition deflate.c:120
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 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
ring len
Length.
Definition dwmac.h:226
uint8_t data[48]
Additional event data.
Definition ena.h:11
struct ena_llq_option header
Header locations.
Definition ena.h:5
#define DBGC2(...)
Definition compiler.h:522
#define DBGCP(...)
Definition compiler.h:539
#define DBGC(...)
Definition compiler.h:505
#define EINVAL
Invalid argument.
Definition errno.h:429
#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
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
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

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().

Variable Documentation

◆ fill

uint8_t fill

Length pair.

Definition at line 1 of file deflate.h.

◆ count

uint8_t count

Repetition count.

Definition at line 3 of file deflate.h.

◆ data

void* data

Definition at line 264 of file deflate.h.

◆ offset

chunk offset = offset

Definition at line 265 of file deflate.h.

◆ len

chunk len
Initial value:
{
chunk->data = data

Definition at line 265 of file deflate.h.