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

Enumerations

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

Functions

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

Variables

uint8_t fill
 Length pair. More...
 
uint8_t count
 Repetition count. More...
 
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 24 of file deflate.h.

◆ DEFLATE_HEADER_BFINAL_BIT

#define DEFLATE_HEADER_BFINAL_BIT   0

Block header final block flags bit.

Definition at line 27 of file deflate.h.

◆ DEFLATE_HEADER_BTYPE_LSB

#define DEFLATE_HEADER_BTYPE_LSB   1

Block header type LSB.

Definition at line 30 of file deflate.h.

◆ DEFLATE_HEADER_BTYPE_MASK

#define DEFLATE_HEADER_BTYPE_MASK   0x03

Block header type mask.

Definition at line 33 of file deflate.h.

◆ DEFLATE_HEADER_BTYPE_LITERAL

#define DEFLATE_HEADER_BTYPE_LITERAL   0

Block header type: literal data.

Definition at line 36 of file deflate.h.

◆ DEFLATE_HEADER_BTYPE_STATIC

#define DEFLATE_HEADER_BTYPE_STATIC   1

Block header type: static Huffman alphabet.

Definition at line 39 of file deflate.h.

◆ DEFLATE_HEADER_BTYPE_DYNAMIC

#define DEFLATE_HEADER_BTYPE_DYNAMIC   2

Block header type: dynamic Huffman alphabet.

Definition at line 42 of file deflate.h.

◆ DEFLATE_LITERAL_LEN_BITS

#define DEFLATE_LITERAL_LEN_BITS   16

Literal header LEN/NLEN field length (in bits)

Definition at line 45 of file deflate.h.

◆ DEFLATE_DYNAMIC_BITS

#define DEFLATE_DYNAMIC_BITS   14

Dynamic header length (in bits)

Definition at line 48 of file deflate.h.

◆ DEFLATE_DYNAMIC_HLIT_LSB

#define DEFLATE_DYNAMIC_HLIT_LSB   0

Dynamic header HLIT field LSB.

Definition at line 51 of file deflate.h.

◆ DEFLATE_DYNAMIC_HLIT_MASK

#define DEFLATE_DYNAMIC_HLIT_MASK   0x1f

Dynamic header HLIT field mask.

Definition at line 54 of file deflate.h.

◆ DEFLATE_DYNAMIC_HDIST_LSB

#define DEFLATE_DYNAMIC_HDIST_LSB   5

Dynamic header HDIST field LSB.

Definition at line 57 of file deflate.h.

◆ DEFLATE_DYNAMIC_HDIST_MASK

#define DEFLATE_DYNAMIC_HDIST_MASK   0x1f

Dynamic header HDIST field mask.

Definition at line 60 of file deflate.h.

◆ DEFLATE_DYNAMIC_HCLEN_LSB

#define DEFLATE_DYNAMIC_HCLEN_LSB   10

Dynamic header HCLEN field LSB.

Definition at line 63 of file deflate.h.

◆ DEFLATE_DYNAMIC_HCLEN_MASK

#define DEFLATE_DYNAMIC_HCLEN_MASK   0x0f

Dynamic header HCLEN field mask.

Definition at line 66 of file deflate.h.

◆ DEFLATE_CODELEN_BITS

#define DEFLATE_CODELEN_BITS   3

Dynamic header code length length (in bits)

Definition at line 69 of file deflate.h.

◆ DEFLATE_HUFFMAN_BITS

#define DEFLATE_HUFFMAN_BITS   15

Maximum length of a Huffman symbol (in bits)

Definition at line 72 of file deflate.h.

◆ 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 78 of file deflate.h.

◆ DEFLATE_HUFFMAN_QL_SHIFT

#define DEFLATE_HUFFMAN_QL_SHIFT   ( 16 - DEFLATE_HUFFMAN_QL_BITS )

Quick lookup shift.

Definition at line 81 of file deflate.h.

◆ DEFLATE_LITLEN_END

#define DEFLATE_LITLEN_END   256

Literal/length end of block code.

Definition at line 84 of file deflate.h.

◆ DEFLATE_LITLEN_MAX_CODE

#define DEFLATE_LITLEN_MAX_CODE   287

Maximum value of a literal/length code.

Definition at line 87 of file deflate.h.

◆ DEFLATE_DISTANCE_MAX_CODE

#define DEFLATE_DISTANCE_MAX_CODE   31

Maximum value of a distance code.

Definition at line 90 of file deflate.h.

◆ DEFLATE_CODELEN_MAX_CODE

#define DEFLATE_CODELEN_MAX_CODE   18

Maximum value of a code length code.

Definition at line 93 of file deflate.h.

◆ ZLIB_HEADER_BITS

#define ZLIB_HEADER_BITS   16

ZLIB header length (in bits)

Definition at line 96 of file deflate.h.

◆ ZLIB_HEADER_CM_LSB

#define ZLIB_HEADER_CM_LSB   0

ZLIB header compression method LSB.

Definition at line 99 of file deflate.h.

◆ ZLIB_HEADER_CM_MASK

#define ZLIB_HEADER_CM_MASK   0x0f

ZLIB header compression method mask.

Definition at line 102 of file deflate.h.

◆ ZLIB_HEADER_CM_DEFLATE

#define ZLIB_HEADER_CM_DEFLATE   8

ZLIB header compression method: DEFLATE.

Definition at line 105 of file deflate.h.

◆ ZLIB_HEADER_FDICT_BIT

#define ZLIB_HEADER_FDICT_BIT   13

ZLIB header preset dictionary flag bit.

Definition at line 108 of file deflate.h.

◆ ZLIB_ADLER32_BITS

#define ZLIB_ADLER32_BITS   32

ZLIB ADLER32 length (in bits)

Definition at line 111 of file deflate.h.

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 16 of file deflate.h.

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

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ __attribute__() [1/2]

◆ __attribute__() [2/2]

static __attribute__ ( (always_inline)  )
inlinestatic

Initialise chunk of data.

Parameters
chunkChunk of data to initialise
dataData
offsetStarting offset
lenLength

◆ deflate_finished()

static int deflate_finished ( struct deflate deflate)
inlinestatic

Check if decompression has finished.

Parameters
deflateDecompressor
Return values
finishedDecompression has finished

Definition at line 277 of file deflate.h.

277  {
278  return ( deflate->resume == NULL );
279 }
void * resume
Resume point.
Definition: deflate.h:161
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
Decompressor.
Definition: deflate.h:155

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 
)

Initialise decompressor.

Parameters
deflateDecompressor
formatCompression format code

Definition at line 991 of file deflate.c.

991  {
992  static int global_init_done;
993  uint8_t i;
994  uint8_t bit;
995  uint8_t byte;
996  unsigned int base;
997  unsigned int bits;
998 
999  /* Perform global initialisation if required */
1000  if ( ! global_init_done ) {
1001 
1002  /* Initialise byte reversal table */
1003  for ( i = 255 ; i ; i-- ) {
1004  for ( bit = 1, byte = 0 ; bit ; bit <<= 1 ) {
1005  byte <<= 1;
1006  if ( i & bit )
1007  byte |= 1;
1008  }
1009  deflate_reverse[i] = byte;
1010  }
1011 
1012  /* Initialise literal/length extra bits table */
1013  base = 3;
1014  for ( i = 0 ; i < 28 ; i++ ) {
1015  bits = ( i / 4 );
1016  if ( bits )
1017  bits--;
1019  base += ( 1 << bits );
1020  }
1021  assert ( base == 259 ); /* sic */
1022 
1023  /* Initialise distance extra bits table */
1024  base = 1;
1025  for ( i = 0 ; i < 30 ; i++ ) {
1026  bits = ( i / 2 );
1027  if ( bits )
1028  bits--;
1030  base += ( 1 << bits );
1031  }
1032  assert ( base == 32769 );
1033 
1034  /* Record global initialisation as complete */
1035  global_init_done = 1;
1036  }
1037 
1038  /* Initialise structure */
1039  memset ( deflate, 0, sizeof ( *deflate ) );
1040  deflate->format = format;
1041 }
uint32_t base
Base.
Definition: librm.h:138
static uint16_t deflate_distance_base[32]
Distance base values.
Definition: deflate.c:70
enum deflate_format format
Format.
Definition: deflate.h:163
static unsigned int unsigned int bit
Definition: bigint.h:391
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned char uint8_t
Definition: stdint.h:10
static uint8_t deflate_litlen_base[28]
Literal/length base values.
Definition: deflate.c:61
unsigned char byte
Definition: smc9000.h:38
static volatile void * bits
Definition: bitops.h:27
int const char * format
Definition: xfer.h:104
static uint8_t deflate_reverse[256]
Byte reversal table.
Definition: deflate.c:50
void * memset(void *dest, int character, size_t len) __nonnull
Decompressor.
Definition: deflate.h:155

References assert(), base, bit, bits, deflate_distance_base, deflate_litlen_base, deflate_reverse, format, deflate::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 
)

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 483 of file deflate.c.

484  {
485 
486  /* Store input data pointers */
487  deflate->in = data;
488  deflate->end = ( data + len );
489 
490  /* This could be implemented more neatly if gcc offered a
491  * means for enforcing tail recursion.
492  */
493  if ( deflate->resume ) {
494  goto *(deflate->resume);
495  } else switch ( deflate->format ) {
496  case DEFLATE_RAW: goto block_header;
497  case DEFLATE_ZLIB: goto zlib_header;
498  default: assert ( 0 );
499  }
500 
501  zlib_header: {
502  int header;
503  int cm;
504 
505  /* Extract header */
507  if ( header < 0 ) {
508  deflate->resume = &&zlib_header;
509  return 0;
510  }
511 
512  /* Parse header */
514  if ( cm != ZLIB_HEADER_CM_DEFLATE ) {
515  DBGC ( deflate, "DEFLATE %p unsupported ZLIB "
516  "compression method %d\n", deflate, cm );
517  return -ENOTSUP;
518  }
519  if ( header & ( 1 << ZLIB_HEADER_FDICT_BIT ) ) {
520  DBGC ( deflate, "DEFLATE %p unsupported ZLIB preset "
521  "dictionary\n", deflate );
522  return -ENOTSUP;
523  }
524 
525  /* Process first block header */
526  goto block_header;
527  }
528 
529  block_header: {
530  int header;
531  int bfinal;
532  int btype;
533 
534  /* Extract block header */
536  if ( header < 0 ) {
537  deflate->resume = &&block_header;
538  return 0;
539  }
540 
541  /* Parse header */
542  deflate->header = header;
543  bfinal = ( header & ( 1 << DEFLATE_HEADER_BFINAL_BIT ) );
544  btype = ( header >> DEFLATE_HEADER_BTYPE_LSB );
545  DBGC ( deflate, "DEFLATE %p found %sblock type %#x\n",
546  deflate, ( bfinal ? "final " : "" ), btype );
547  switch ( btype ) {
549  goto literal_block;
551  goto static_block;
553  goto dynamic_block;
554  default:
555  DBGC ( deflate, "DEFLATE %p unsupported block type "
556  "%#x\n", deflate, btype );
557  return -ENOTSUP;
558  }
559  }
560 
561  literal_block: {
562 
563  /* Discard any bits up to the next byte boundary */
565  }
566 
567  literal_len: {
568  int llen;
569 
570  /* Extract LEN field */
572  if ( llen < 0 ) {
573  deflate->resume = &&literal_len;
574  return 0;
575  }
576 
577  /* Record length of literal data */
578  deflate->remaining = llen;
579  DBGC2 ( deflate, "DEFLATE %p literal block length %#04zx\n",
581  }
582 
583  literal_nlen: {
584  int nlen;
585 
586  /* Extract NLEN field */
588  if ( nlen < 0 ) {
589  deflate->resume = &&literal_nlen;
590  return 0;
591  }
592 
593  /* Verify NLEN */
594  if ( ( ( deflate->remaining ^ ~nlen ) &
595  ( ( 1 << DEFLATE_LITERAL_LEN_BITS ) - 1 ) ) != 0 ) {
596  DBGC ( deflate, "DEFLATE %p invalid len/nlen "
597  "%#04zx/%#04x\n", deflate,
598  deflate->remaining, nlen );
599  return -EINVAL;
600  }
601  }
602 
603  literal_data: {
604  size_t in_remaining;
605  size_t dlen;
606 
607  /* Calculate available amount of literal data */
608  in_remaining = ( deflate->end - deflate->in );
609  dlen = deflate->remaining;
610  if ( dlen > in_remaining )
611  dlen = in_remaining;
612 
613  /* Copy data to output buffer */
614  deflate_copy ( out, deflate->in, dlen );
615 
616  /* Consume data from input buffer */
617  deflate->in += dlen;
618  deflate->remaining -= dlen;
619 
620  /* Finish processing if we are blocked */
621  if ( deflate->remaining ) {
622  deflate->resume = &&literal_data;
623  return 0;
624  }
625 
626  /* Otherwise, finish block */
627  goto block_done;
628  }
629 
630  static_block: {
631  struct deflate_static_length_pattern *pattern;
632  uint8_t *lengths = deflate->lengths;
633 
634  /* Construct static Huffman lengths as per RFC 1950 */
635  for ( pattern = deflate_static_length_patterns ;
636  pattern->count ; pattern++ ) {
637  memset ( lengths, pattern->fill, pattern->count );
638  lengths += pattern->count;
639  }
640  deflate->litlen_count = 288;
641  deflate->distance_count = 32;
642  goto construct_alphabets;
643  }
644 
645  dynamic_block:
646 
647  dynamic_header: {
648  int header;
649  unsigned int hlit;
650  unsigned int hdist;
651  unsigned int hclen;
652 
653  /* Extract block header */
655  if ( header < 0 ) {
656  deflate->resume = &&dynamic_header;
657  return 0;
658  }
659 
660  /* Parse header */
661  hlit = ( ( header >> DEFLATE_DYNAMIC_HLIT_LSB ) &
663  hdist = ( ( header >> DEFLATE_DYNAMIC_HDIST_LSB ) &
665  hclen = ( ( header >> DEFLATE_DYNAMIC_HCLEN_LSB ) &
667  deflate->litlen_count = ( hlit + 257 );
668  deflate->distance_count = ( hdist + 1 );
669  deflate->length_index = 0;
670  deflate->length_target = ( hclen + 4 );
671  DBGC2 ( deflate, "DEFLATE %p dynamic block %d codelen, %d "
672  "litlen, %d distance\n", deflate,
675 
676  /* Prepare for decoding code length code lengths */
677  memset ( &deflate->lengths, 0, sizeof ( deflate->lengths ) );
678  }
679 
680  dynamic_codelen: {
681  int clen;
682  unsigned int index;
683  int rc;
684 
685  /* Extract all code lengths */
686  while ( deflate->length_index < deflate->length_target ) {
687 
688  /* Extract code length length */
689  clen = deflate_extract ( deflate,
691  if ( clen < 0 ) {
692  deflate->resume = &&dynamic_codelen;
693  return 0;
694  }
695 
696  /* Store code length */
698  deflate_set_length ( deflate, index, clen );
699  DBGCP ( deflate, "DEFLATE %p codelen for %d is %d\n",
700  deflate, index, clen );
701  }
702 
703  /* Generate code length alphabet */
704  if ( ( rc = deflate_alphabet ( deflate,
706  ( DEFLATE_CODELEN_MAX_CODE + 1 ),
707  0 ) ) != 0 )
708  return rc;
709 
710  /* Prepare for decoding literal/length/distance code lengths */
711  memset ( &deflate->lengths, 0, sizeof ( deflate->lengths ) );
712  deflate->length_index = 0;
715  deflate->length = 0;
716  }
717 
718  dynamic_litlen_distance: {
719  int clen;
720  int index;
721 
722  /* Decode literal/length/distance code length */
724  if ( clen < 0 ) {
725  deflate->resume = &&dynamic_litlen_distance;
726  return 0;
727  }
728 
729  /* Prepare for extra bits */
730  if ( clen < 16 ) {
731  deflate->length = clen;
732  deflate->extra_bits = 0;
733  deflate->dup_len = 1;
734  } else {
735  static const uint8_t dup_len[3] = { 3, 3, 11 };
736  static const uint8_t extra_bits[3] = { 2, 3, 7 };
737  index = ( clen - 16 );
738  deflate->dup_len = dup_len[index];
739  deflate->extra_bits = extra_bits[index];
740  if ( index )
741  deflate->length = 0;
742  }
743  }
744 
745  dynamic_litlen_distance_extra: {
746  int extra;
747  unsigned int dup_len;
748 
749  /* Extract extra bits */
751  if ( extra < 0 ) {
752  deflate->resume = &&dynamic_litlen_distance_extra;
753  return 0;
754  }
755 
756  /* Store code lengths */
757  dup_len = ( deflate->dup_len + extra );
758  while ( ( deflate->length_index < deflate->length_target ) &&
759  dup_len-- ) {
761  deflate->length );
762  }
763 
764  /* Process next literal/length or distance code
765  * length, if more are required.
766  */
768  goto dynamic_litlen_distance;
769 
770  /* Construct alphabets */
771  goto construct_alphabets;
772  }
773 
774  construct_alphabets: {
775  unsigned int distance_offset = deflate->litlen_count;
776  unsigned int distance_count = deflate->distance_count;
777  int rc;
778 
779  /* Generate literal/length alphabet */
780  if ( ( rc = deflate_alphabet ( deflate, &deflate->litlen,
781  deflate->litlen_count, 0 ) ) !=0)
782  return rc;
783 
784  /* Handle degenerate case of a single distance code
785  * (for which it is impossible to construct a valid,
786  * complete Huffman alphabet). RFC 1951 states:
787  *
788  * If only one distance code is used, it is encoded
789  * using one bit, not zero bits; in this case there
790  * is a single code length of one, with one unused
791  * code. One distance code of zero bits means that
792  * there are no distance codes used at all (the data
793  * is all literals).
794  *
795  * If we have only a single distance code, then we
796  * instead use two distance codes both with length 1.
797  * This results in a valid Huffman alphabet. The code
798  * "0" will mean distance code 0 (which is either
799  * correct or irrelevant), and the code "1" will mean
800  * distance code 1 (which is always irrelevant).
801  */
802  if ( deflate->distance_count == 1 ) {
803 
804  deflate->lengths[0] = 0x11;
805  distance_offset = 0;
806  distance_count = 2;
807  }
808 
809  /* Generate distance alphabet */
810  if ( ( rc = deflate_alphabet ( deflate,
812  distance_count,
813  distance_offset ) ) != 0 )
814  return rc;
815  }
816 
817  lzhuf_litlen: {
818  int code;
819  uint8_t byte;
820  unsigned int extra;
821  unsigned int bits;
822 
823  /* Decode Huffman codes */
824  while ( 1 ) {
825 
826  /* Decode Huffman code */
828  if ( code < 0 ) {
829  deflate->resume = &&lzhuf_litlen;
830  return 0;
831  }
832 
833  /* Handle according to code type */
834  if ( code < DEFLATE_LITLEN_END ) {
835 
836  /* Literal value: copy to output buffer */
837  byte = code;
838  DBGCP ( deflate, "DEFLATE %p literal %#02x "
839  "('%c')\n", deflate, byte,
840  ( isprint ( byte ) ? byte : '.' ) );
841  deflate_copy ( out, &byte, sizeof ( byte ) );
842 
843  } else if ( code == DEFLATE_LITLEN_END ) {
844 
845  /* End of block */
846  goto block_done;
847 
848  } else {
849 
850  /* Length code: process extra bits */
851  extra = ( code - DEFLATE_LITLEN_END - 1 );
852  if ( extra < 28 ) {
853  bits = ( extra / 4 );
854  if ( bits )
855  bits--;
857  deflate->dup_len =
859  } else {
860  deflate->extra_bits = 0;
861  deflate->dup_len = 258;
862  }
863  goto lzhuf_litlen_extra;
864  }
865  }
866  }
867 
868  lzhuf_litlen_extra: {
869  int extra;
870 
871  /* Extract extra bits */
873  if ( extra < 0 ) {
874  deflate->resume = &&lzhuf_litlen_extra;
875  return 0;
876  }
877 
878  /* Update duplicate length */
879  deflate->dup_len += extra;
880  }
881 
882  lzhuf_distance: {
883  int code;
884  unsigned int extra;
885  unsigned int bits;
886 
887  /* Decode Huffman code */
889  if ( code < 0 ) {
890  deflate->resume = &&lzhuf_distance;
891  return 0;
892  }
893 
894  /* Process extra bits */
895  extra = code;
896  bits = ( extra / 2 );
897  if ( bits )
898  bits--;
901  }
902 
903  lzhuf_distance_extra: {
904  int extra;
905  size_t dup_len;
906  size_t dup_distance;
907 
908  /* Extract extra bits */
910  if ( extra < 0 ) {
911  deflate->resume = &&lzhuf_distance_extra;
912  return 0;
913  }
914 
915  /* Update duplicate distance */
916  dup_distance = ( deflate->dup_distance + extra );
917  dup_len = deflate->dup_len;
918  DBGCP ( deflate, "DEFLATE %p duplicate length %zd distance "
919  "%zd\n", deflate, dup_len, dup_distance );
920 
921  /* Sanity check */
922  if ( dup_distance > out->offset ) {
923  DBGC ( deflate, "DEFLATE %p bad distance %zd (max "
924  "%zd)\n", deflate, dup_distance, out->offset );
925  return -EINVAL;
926  }
927 
928  /* Copy data, allowing for overlap */
929  deflate_copy ( out, ( out->data + out->offset - dup_distance ),
930  dup_len );
931 
932  /* Process next literal/length symbol */
933  goto lzhuf_litlen;
934  }
935 
936  block_done: {
937 
938  DBGCP ( deflate, "DEFLATE %p end of block\n", deflate );
939 
940  /* If this was not the final block, process next block header */
941  if ( ! ( deflate->header & ( 1 << DEFLATE_HEADER_BFINAL_BIT ) ))
942  goto block_header;
943 
944  /* Otherwise, process footer (if any) */
945  switch ( deflate->format ) {
946  case DEFLATE_RAW: goto finished;
947  case DEFLATE_ZLIB: goto zlib_footer;
948  default: assert ( 0 );
949  }
950  }
951 
952  zlib_footer: {
953 
954  /* Discard any bits up to the next byte boundary */
956  }
957 
958  zlib_adler32: {
959  int excess;
960 
961  /* Accumulate the 32 bits of checksum. We don't check
962  * the value, stop processing immediately afterwards,
963  * and so don't have to worry about the nasty corner
964  * cases involved in calling deflate_extract() to
965  * obtain a full 32 bits.
966  */
968  if ( excess < 0 ) {
969  deflate->resume = &&zlib_adler32;
970  return 0;
971  }
972 
973  /* Finish processing */
974  goto finished;
975  }
976 
977  finished: {
978  /* Mark as finished and terminate */
979  DBGCP ( deflate, "DEFLATE %p finished\n", deflate );
980  deflate->resume = NULL;
981  return 0;
982  }
983 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
static void deflate_set_length(struct deflate *deflate, unsigned int index, unsigned int bits)
Set Huffman symbol length.
Definition: deflate.c:119
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned int length_index
Current length index within a set of code lengths.
Definition: deflate.h:185
#define DEFLATE_DYNAMIC_HLIT_LSB
Dynamic header HLIT field LSB.
Definition: deflate.h:51
static uint16_t deflate_distance_base[32]
Distance base values.
Definition: deflate.c:70
static void deflate_copy(struct deflate_chunk *out, const void *in, size_t len)
Copy data to output buffer (if available)
Definition: deflate.c:447
#define DEFLATE_CODELEN_BITS
Dynamic header code length length (in bits)
Definition: deflate.h:69
struct deflate_alphabet distance_codelen
Distance and code length Huffman alphabet.
Definition: deflate.h:216
static int deflate_decode(struct deflate *deflate, struct deflate_alphabet *alphabet)
Attempt to decode a Huffman-coded symbol from input stream.
Definition: deflate.c:389
unsigned int length_target
Target length index within a set of code lengths.
Definition: deflate.h:187
uint8_t extra
Signature extra byte.
Definition: smbios.h:17
#define DEFLATE_DYNAMIC_HCLEN_MASK
Dynamic header HCLEN field mask.
Definition: deflate.h:66
unsigned int extra_bits
Number of extra bits required.
Definition: deflate.h:191
size_t dup_distance
Distance of a duplicated string.
Definition: deflate.h:195
#define DEFLATE_HEADER_BTYPE_STATIC
Block header type: static Huffman alphabet.
Definition: deflate.h:39
unsigned int header
Current block header.
Definition: deflate.h:181
#define DBGC(...)
Definition: compiler.h:505
enum deflate_format format
Format.
Definition: deflate.h:163
long index
Definition: bigint.h:62
unsigned int length
Current length within a set of code lengths.
Definition: deflate.h:189
#define ZLIB_ADLER32_BITS
ZLIB ADLER32 length (in bits)
Definition: deflate.h:111
A Huffman-coded alphabet.
Definition: deflate.h:133
#define DEFLATE_LITLEN_END
Literal/length end of block code.
Definition: deflate.h:84
#define DEFLATE_DYNAMIC_HDIST_MASK
Dynamic header HDIST field mask.
Definition: deflate.h:60
uint8_t count
Repetition count.
Definition: deflate.h:151
static unsigned int code
Response code.
Definition: hyperv.h:26
__be32 out[4]
Definition: CIB_PRM.h:36
struct ib_mad_cm cm
Definition: ib_mad.h:14
#define ENOTSUP
Operation not supported.
Definition: errno.h:589
static int isprint(int character)
Check if character is printable.
Definition: ctype.h:97
#define DEFLATE_DYNAMIC_HCLEN_LSB
Dynamic header HCLEN field LSB.
Definition: deflate.h:63
static int deflate_accumulate(struct deflate *deflate, unsigned int target)
Attempt to accumulate bits from input stream.
Definition: deflate.c:305
#define DEFLATE_HEADER_BTYPE_LSB
Block header type LSB.
Definition: deflate.h:30
#define ZLIB_HEADER_BITS
ZLIB header length (in bits)
Definition: deflate.h:96
#define DEFLATE_LITERAL_LEN_BITS
Literal header LEN/NLEN field length (in bits)
Definition: deflate.h:45
size_t dup_len
Length of a duplicated string.
Definition: deflate.h:193
#define ZLIB_HEADER_CM_DEFLATE
ZLIB header compression method: DEFLATE.
Definition: deflate.h:105
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void * resume
Resume point.
Definition: deflate.h:161
ring len
Length.
Definition: dwmac.h:231
static int deflate_extract(struct deflate *deflate, unsigned int target)
Attempt to extract a fixed number of bits from input stream.
Definition: deflate.c:361
ZLIB header and footer.
Definition: deflate.h:20
#define DEFLATE_DYNAMIC_HLIT_MASK
Dynamic header HLIT field mask.
Definition: deflate.h:54
uint8_t fill
Length pair.
Definition: deflate.h:149
#define DEFLATE_CODELEN_MAX_CODE
Maximum value of a code length code.
Definition: deflate.h:93
unsigned char uint8_t
Definition: stdint.h:10
unsigned int distance_count
Number of symbols in the distance Huffman alphabet.
Definition: deflate.h:224
static uint8_t deflate_litlen_base[28]
Literal/length base values.
Definition: deflate.c:61
unsigned char byte
Definition: smc9000.h:38
struct deflate_alphabet litlen
Literal/length Huffman alphabet.
Definition: deflate.h:198
static uint8_t deflate_codelen_map[19]
Code length map.
Definition: deflate.c:73
const uint8_t * in
Current input data pointer.
Definition: deflate.h:166
#define DEFLATE_DYNAMIC_BITS
Dynamic header length (in bits)
Definition: deflate.h:48
Raw DEFLATE data (no header or footer)
Definition: deflate.h:18
static volatile void * bits
Definition: bitops.h:27
#define DBGC2(...)
Definition: compiler.h:522
A static Huffman alphabet length pattern.
Definition: deflate.h:147
#define DEFLATE_HEADER_BTYPE_LITERAL
Block header type: literal data.
Definition: deflate.h:36
const uint8_t * end
End of input data pointer.
Definition: deflate.h:168
struct ena_llq_option header
Header locations.
Definition: ena.h:16
#define DEFLATE_HEADER_BITS
Block header length (in bits)
Definition: deflate.h:24
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define ZLIB_HEADER_CM_MASK
ZLIB header compression method mask.
Definition: deflate.h:102
static struct deflate_static_length_pattern deflate_static_length_patterns[]
Static Huffman alphabet length patterns.
Definition: deflate.c:78
#define DBGCP(...)
Definition: compiler.h:539
unsigned int litlen_count
Number of symbols in the literal/length Huffman alphabet.
Definition: deflate.h:205
#define ZLIB_HEADER_CM_LSB
ZLIB header compression method LSB.
Definition: deflate.h:99
#define DEFLATE_HEADER_BFINAL_BIT
Block header final block flags bit.
Definition: deflate.h:27
uint8_t lengths[((DEFLATE_LITLEN_MAX_CODE+1)+(DEFLATE_DISTANCE_MAX_CODE+1)+1)/2]
Huffman code lengths.
Definition: deflate.h:241
static void deflate_discard_to_byte(struct deflate *deflate)
Discard bits up to the next byte boundary.
Definition: deflate.c:435
#define ZLIB_HEADER_FDICT_BIT
ZLIB header preset dictionary flag bit.
Definition: deflate.h:108
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define DEFLATE_DYNAMIC_HDIST_LSB
Dynamic header HDIST field LSB.
Definition: deflate.h:57
size_t remaining
Remaining length of data (e.g.
Definition: deflate.h:183
void * memset(void *dest, int character, size_t len) __nonnull
#define DEFLATE_HEADER_BTYPE_DYNAMIC
Block header type: dynamic Huffman alphabet.
Definition: deflate.h:42
Decompressor.
Definition: deflate.h:155

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, header, deflate::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 12 of file deflate.h.

◆ count

uint8_t count

Repetition count.

Definition at line 14 of file deflate.h.

◆ __attribute__

◆ data

void* data

Definition at line 263 of file deflate.h.

◆ offset

chunk offset = offset

Definition at line 263 of file deflate.h.

◆ len

chunk len
Initial value:
{
chunk->data = data
static void * data
Definition: deflate.h:263

Definition at line 264 of file deflate.h.