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

Variables

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

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  )

◆ deflate_bin()

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

97  {
98  static char buf[ ( 8 * sizeof ( value ) ) + 1 /* NUL */ ];
99  char *out = buf;
100 
101  /* Sanity check */
102  assert ( bits < sizeof ( buf ) );
103 
104  /* Transcribe value */
105  while ( bits-- )
106  *(out++) = ( ( value & ( 1 << bits ) ) ? '1' : '0' );
107  *out = '\0';
108 
109  return buf;
110 }
__be32 out[4]
Definition: CIB_PRM.h:36
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static volatile void * bits
Definition: bitops.h:27

References assert(), bits, out, and value.

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

◆ deflate_set_length()

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

120  {
121 
122  deflate->lengths[ index / 2 ] |= ( bits << ( 4 * ( index % 2 ) ) );
123 }
long index
Definition: bigint.h:62
static volatile void * bits
Definition: bitops.h:27
uint8_t lengths[((DEFLATE_LITLEN_MAX_CODE+1)+(DEFLATE_DISTANCE_MAX_CODE+1)+1)/2]
Huffman code lengths.
Definition: deflate.h:241
Decompressor.
Definition: deflate.h:155

References bits, index, and deflate::lengths.

Referenced by deflate_inflate().

◆ deflate_length()

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

133  {
134 
135  return ( ( deflate->lengths[ index / 2 ] >> ( 4 * ( index % 2 ) ) )
136  & 0x0f );
137 }
long index
Definition: bigint.h:62
uint8_t lengths[((DEFLATE_LITLEN_MAX_CODE+1)+(DEFLATE_DISTANCE_MAX_CODE+1)+1)/2]
Huffman code lengths.
Definition: deflate.h:241
Decompressor.
Definition: deflate.h:155

References index, and deflate::lengths.

Referenced by deflate_alphabet().

◆ deflate_alphabet_name()

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

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

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

Referenced by deflate_alphabet(), and deflate_dump_alphabet().

◆ deflate_dump_alphabet()

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

Dump Huffman alphabet (for debugging)

Parameters
deflateDecompressor
alphabetHuffman alphabet

Definition at line 164 of file deflate.c.

165  {
166  struct deflate_huf_symbols *huf_sym;
167  unsigned int bits;
168  unsigned int huf;
169  unsigned int i;
170 
171  /* Do nothing unless debugging is enabled */
172  if ( ! DBG_EXTRA )
173  return;
174 
175  /* Dump symbol table for each utilised length */
176  for ( bits = 1 ; bits <= ( sizeof ( alphabet->huf ) /
177  sizeof ( alphabet->huf[0] ) ) ; bits++ ) {
178  huf_sym = &alphabet->huf[ bits - 1 ];
179  if ( huf_sym->freq == 0 )
180  continue;
181  huf = ( huf_sym->start >> huf_sym->shift );
182  DBGC2 ( alphabet, "DEFLATE %p \"%s\" length %d start \"%s\" "
183  "freq %d:", deflate,
184  deflate_alphabet_name ( deflate, alphabet ), bits,
185  deflate_bin ( huf, huf_sym->bits ), huf_sym->freq );
186  for ( i = 0 ; i < huf_sym->freq ; i++ ) {
187  DBGC2 ( alphabet, " %03x",
188  huf_sym->raw[ huf + i ] );
189  }
190  DBGC2 ( alphabet, "\n" );
191  }
192 
193  /* Dump quick lookup table */
194  DBGC2 ( alphabet, "DEFLATE %p \"%s\" quick lookup:", deflate,
195  deflate_alphabet_name ( deflate, alphabet ) );
196  for ( i = 0 ; i < ( sizeof ( alphabet->lookup ) /
197  sizeof ( alphabet->lookup[0] ) ) ; i++ ) {
198  DBGC2 ( alphabet, " %d", ( alphabet->lookup[i] + 1 ) );
199  }
200  DBGC2 ( alphabet, "\n" );
201 }
A Huffman-coded set of symbols of a given length.
Definition: deflate.h:114
uint32_t start
First symbol of this length (normalised to 16 bits)
Definition: deflate.h:127
static const char * deflate_bin(unsigned long value, unsigned int bits)
Transcribe binary value (for debugging)
Definition: deflate.c:97
uint8_t lookup[1<< DEFLATE_HUFFMAN_QL_BITS]
Quick lookup table.
Definition: deflate.h:137
struct deflate_huf_symbols huf[DEFLATE_HUFFMAN_BITS]
Huffman-coded symbol set for each length.
Definition: deflate.h:135
uint16_t freq
Number of Huffman-coded symbols having this length.
Definition: deflate.h:120
static const char * deflate_alphabet_name(struct deflate *deflate, struct deflate_alphabet *alphabet)
Determine Huffman alphabet name (for debugging)
Definition: deflate.c:146
uint16_t * raw
Raw symbols having this length.
Definition: deflate.h:129
static volatile void * bits
Definition: bitops.h:27
#define DBGC2(...)
Definition: compiler.h:522
#define DBG_EXTRA
Definition: compiler.h:319
uint8_t shift
Shift to normalise symbols of this length to 16 bits.
Definition: deflate.h:118
uint8_t bits
Length of Huffman-coded symbols.
Definition: deflate.h:116
Decompressor.
Definition: deflate.h:155

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

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

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

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, raw, deflate_huf_symbols::raw, deflate_alphabet::raw, deflate_huf_symbols::shift, and deflate_huf_symbols::start.

◆ deflate_accumulate()

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

306  {
307  uint8_t byte;
308 
309  while ( deflate->bits < target ) {
310 
311  /* Check for end of input */
312  if ( deflate->in == deflate->end )
313  break;
314 
315  /* Acquire byte from input */
316  byte = *(deflate->in++);
318  ( byte << deflate->bits ) );
320  ( deflate_reverse[byte] <<
321  ( 24 - deflate->bits ) ) );
322  deflate->bits += 8;
323 
324  /* Sanity check */
325  assert ( deflate->bits <=
326  ( 8 * sizeof ( deflate->accumulator ) ) );
327  }
328 
329  return ( deflate->bits - target );
330 }
unsigned int bits
Number of bits within the accumulator.
Definition: deflate.h:178
uint32_t accumulator
Accumulator.
Definition: deflate.h:171
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
uint32_t rotalumucca
Bit-reversed accumulator.
Definition: deflate.h:176
unsigned char uint8_t
Definition: stdint.h:10
unsigned char byte
Definition: smc9000.h:38
const uint8_t * in
Current input data pointer.
Definition: deflate.h:166
const uint8_t * end
End of input data pointer.
Definition: deflate.h:168
static uint8_t deflate_reverse[256]
Byte reversal table.
Definition: deflate.c:50
Decompressor.
Definition: deflate.h:155

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

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

339  {
340  int data;
341 
342  /* Sanity check */
343  assert ( count <= deflate->bits );
344 
345  /* Extract data and consume bits */
346  data = ( deflate->accumulator & ( ( 1 << count ) - 1 ) );
349  deflate->bits -= count;
350 
351  return data;
352 }
unsigned int bits
Number of bits within the accumulator.
Definition: deflate.h:178
uint32_t accumulator
Accumulator.
Definition: deflate.h:171
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static unsigned int count
Number of entries.
Definition: dwmac.h:225
uint32_t rotalumucca
Bit-reversed accumulator.
Definition: deflate.h:176
static volatile void * bits
Definition: bitops.h:27
uint8_t data[48]
Additional event data.
Definition: ena.h:22
Decompressor.
Definition: deflate.h:155

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

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

361  {
362  int excess;
363  int data;
364 
365  /* Return immediately if we are attempting to extract zero bits */
366  if ( target == 0 )
367  return 0;
368 
369  /* Attempt to accumulate bits */
370  excess = deflate_accumulate ( deflate, target );
371  if ( excess < 0 )
372  return excess;
373 
374  /* Extract data and consume bits */
375  data = deflate_consume ( deflate, target );
376  DBGCP ( deflate, "DEFLATE %p extracted %s = %#x = %d\n", deflate,
377  deflate_bin ( data, target ), data, data );
378 
379  return data;
380 }
static const char * deflate_bin(unsigned long value, unsigned int bits)
Transcribe binary value (for debugging)
Definition: deflate.c:97
static int deflate_accumulate(struct deflate *deflate, unsigned int target)
Attempt to accumulate bits from input stream.
Definition: deflate.c:305
static int deflate_consume(struct deflate *deflate, unsigned int count)
Consume accumulated bits from the input stream.
Definition: deflate.c:339
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define DBGCP(...)
Definition: compiler.h:539
Decompressor.
Definition: deflate.h:155

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

Referenced by deflate_inflate().

◆ deflate_decode()

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

390  {
391  struct deflate_huf_symbols *huf_sym;
392  uint16_t huf;
393  unsigned int lookup_index;
394  int excess;
395  unsigned int raw;
396 
397  /* Attempt to accumulate maximum required number of bits.
398  * There may be fewer bits than this remaining in the stream,
399  * even if the stream still contains some complete
400  * Huffman-coded symbols.
401  */
403 
404  /* Normalise the bit-reversed accumulated value to 16 bits */
405  huf = ( deflate->rotalumucca >> 16 );
406 
407  /* Find symbol set for this length */
408  lookup_index = ( huf >> DEFLATE_HUFFMAN_QL_SHIFT );
409  huf_sym = &alphabet->huf[ alphabet->lookup[ lookup_index ] ];
410  while ( huf < huf_sym->start )
411  huf_sym--;
412 
413  /* Calculate number of excess bits, and return if not yet complete */
414  excess = ( deflate->bits - huf_sym->bits );
415  if ( excess < 0 )
416  return excess;
417 
418  /* Consume bits */
419  deflate_consume ( deflate, huf_sym->bits );
420 
421  /* Look up raw symbol */
422  raw = huf_sym->raw[ huf >> huf_sym->shift ];
423  DBGCP ( deflate, "DEFLATE %p decoded %s = %#x = %d\n", deflate,
424  deflate_bin ( ( huf >> huf_sym->shift ), huf_sym->bits ),
425  raw, raw );
426 
427  return raw;
428 }
unsigned short uint16_t
Definition: stdint.h:11
#define DEFLATE_HUFFMAN_QL_SHIFT
Quick lookup shift.
Definition: deflate.h:81
#define DEFLATE_HUFFMAN_BITS
Maximum length of a Huffman symbol (in bits)
Definition: deflate.h:72
A Huffman-coded set of symbols of a given length.
Definition: deflate.h:114
unsigned int bits
Number of bits within the accumulator.
Definition: deflate.h:178
static const char * deflate_bin(unsigned long value, unsigned int bits)
Transcribe binary value (for debugging)
Definition: deflate.c:97
uint32_t start
Starting offset.
Definition: netvsc.h:12
static int deflate_accumulate(struct deflate *deflate, unsigned int target)
Attempt to accumulate bits from input stream.
Definition: deflate.c:305
uint8_t lookup[1<< DEFLATE_HUFFMAN_QL_BITS]
Quick lookup table.
Definition: deflate.h:137
struct deflate_huf_symbols huf[DEFLATE_HUFFMAN_BITS]
Huffman-coded symbol set for each length.
Definition: deflate.h:135
uint32_t rotalumucca
Bit-reversed accumulator.
Definition: deflate.h:176
static int deflate_consume(struct deflate *deflate, unsigned int count)
Consume accumulated bits from the input stream.
Definition: deflate.c:339
uint16_t * raw
Raw symbols having this length.
Definition: deflate.h:129
#define DBGCP(...)
Definition: compiler.h:539
__be32 raw[7]
Definition: CIB_PRM.h:28
uint8_t shift
Shift to normalise symbols of this length to 16 bits.
Definition: deflate.h:118
uint8_t bits
Length of Huffman-coded symbols.
Definition: deflate.h:116
Decompressor.
Definition: deflate.h:155

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

Referenced by deflate_inflate().

◆ deflate_discard_to_byte()

static void deflate_discard_to_byte ( struct deflate deflate)
static

Discard bits up to the next byte boundary.

Parameters
deflateDecompressor

Definition at line 435 of file deflate.c.

435  {
436 
437  deflate_consume ( deflate, ( deflate->bits & 7 ) );
438 }
unsigned int bits
Number of bits within the accumulator.
Definition: deflate.h:178
static int deflate_consume(struct deflate *deflate, unsigned int count)
Consume accumulated bits from the input stream.
Definition: deflate.c:339
Decompressor.
Definition: deflate.h:155

References deflate::bits, and deflate_consume().

Referenced by deflate_inflate().

◆ deflate_copy()

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

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

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

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

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 50 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 61 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 70 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 73 of file deflate.c.

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

Referenced by deflate_inflate().