iPXE
deflate.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 #include <string.h>
28 #include <strings.h>
29 #include <errno.h>
30 #include <assert.h>
31 #include <ctype.h>
32 #include <ipxe/deflate.h>
33 
34 /** @file
35  *
36  * DEFLATE decompression algorithm
37  *
38  * This file implements the decompression half of the DEFLATE
39  * algorithm specified in RFC 1951.
40  *
41  * Portions of this code are derived from wimboot's xca.c.
42  *
43  */
44 
45 /**
46  * Byte reversal table
47  *
48  * For some insane reason, the DEFLATE format stores some values in
49  * bit-reversed order.
50  */
52 
53 /** Literal/length base values
54  *
55  * We include entries only for literal/length codes 257-284. Code 285
56  * does not fit the pattern (it represents a length of 258; following
57  * the pattern from the earlier codes would give a length of 259), and
58  * has no extra bits. Codes 286-287 are invalid, but can occur. We
59  * treat any code greater than 284 as meaning "length 258, no extra
60  * bits".
61  */
63 
64 /** Distance base values
65  *
66  * We include entries for all possible codes 0-31, avoiding the need
67  * to check for undefined codes 30 and 31 before performing the
68  * lookup. Codes 30 and 31 are never initialised, and will therefore
69  * be treated as meaning "14 extra bits, base distance 0".
70  */
72 
73 /** Code length map */
75  16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
76 };
77 
78 /** Static Huffman alphabet length patterns */
80  /* Literal/length code lengths */
81  { 0x88, ( ( ( 143 - 0 ) + 1 ) / 2 ) },
82  { 0x99, ( ( ( 255 - 144 ) + 1 ) / 2 ) },
83  { 0x77, ( ( ( 279 - 256 ) + 1 ) / 2 ) },
84  { 0x88, ( ( ( 287 - 280 ) + 1 ) / 2 ) },
85  /* Distance code lengths */
86  { 0x55, ( ( ( 31 - 0 ) + 1 ) / 2 ) },
87  /* End marker */
88  { 0, 0 }
89 };
90 
91 /**
92  * Transcribe binary value (for debugging)
93  *
94  * @v value Value
95  * @v bits Length of value (in bits)
96  * @ret string Transcribed value
97  */
98 static const char * deflate_bin ( unsigned long value, unsigned int bits ) {
99  static char buf[ ( 8 * sizeof ( value ) ) + 1 /* NUL */ ];
100  char *out = buf;
101 
102  /* Sanity check */
103  assert ( bits < sizeof ( buf ) );
104 
105  /* Transcribe value */
106  while ( bits-- )
107  *(out++) = ( ( value & ( 1 << bits ) ) ? '1' : '0' );
108  *out = '\0';
109 
110  return buf;
111 }
112 
113 /**
114  * Set Huffman symbol length
115  *
116  * @v deflate Decompressor
117  * @v index Index within lengths
118  * @v bits Symbol length (in bits)
119  */
120 static void deflate_set_length ( struct deflate *deflate, unsigned int index,
121  unsigned int bits ) {
122 
123  deflate->lengths[ index / 2 ] |= ( bits << ( 4 * ( index % 2 ) ) );
124 }
125 
126 /**
127  * Get Huffman symbol length
128  *
129  * @v deflate Decompressor
130  * @v index Index within lengths
131  * @ret bits Symbol length (in bits)
132  */
133 static unsigned int deflate_length ( struct deflate *deflate,
134  unsigned int index ) {
135 
136  return ( ( deflate->lengths[ index / 2 ] >> ( 4 * ( index % 2 ) ) )
137  & 0x0f );
138 }
139 
140 /**
141  * Determine Huffman alphabet name (for debugging)
142  *
143  * @v deflate Decompressor
144  * @v alphabet Huffman alphabet
145  * @ret name Alphabet name
146  */
147 static const char * deflate_alphabet_name ( struct deflate *deflate,
148  struct deflate_alphabet *alphabet ){
149 
150  if ( alphabet == &deflate->litlen ) {
151  return "litlen";
152  } else if ( alphabet == &deflate->distance_codelen ) {
153  return "distance/codelen";
154  } else {
155  return "<UNKNOWN>";
156  }
157 }
158 
159 /**
160  * Dump Huffman alphabet (for debugging)
161  *
162  * @v deflate Decompressor
163  * @v alphabet Huffman alphabet
164  */
165 static void deflate_dump_alphabet ( struct deflate *deflate,
166  struct deflate_alphabet *alphabet ) {
167  struct deflate_huf_symbols *huf_sym;
168  unsigned int bits;
169  unsigned int huf;
170  unsigned int i;
171 
172  /* Do nothing unless debugging is enabled */
173  if ( ! DBG_EXTRA )
174  return;
175 
176  /* Dump symbol table for each utilised length */
177  for ( bits = 1 ; bits <= ( sizeof ( alphabet->huf ) /
178  sizeof ( alphabet->huf[0] ) ) ; bits++ ) {
179  huf_sym = &alphabet->huf[ bits - 1 ];
180  if ( huf_sym->freq == 0 )
181  continue;
182  huf = ( huf_sym->start >> huf_sym->shift );
183  DBGC2 ( alphabet, "DEFLATE %p \"%s\" length %d start \"%s\" "
184  "freq %d:", deflate,
185  deflate_alphabet_name ( deflate, alphabet ), bits,
186  deflate_bin ( huf, huf_sym->bits ), huf_sym->freq );
187  for ( i = 0 ; i < huf_sym->freq ; i++ ) {
188  DBGC2 ( alphabet, " %03x",
189  huf_sym->raw[ huf + i ] );
190  }
191  DBGC2 ( alphabet, "\n" );
192  }
193 
194  /* Dump quick lookup table */
195  DBGC2 ( alphabet, "DEFLATE %p \"%s\" quick lookup:", deflate,
196  deflate_alphabet_name ( deflate, alphabet ) );
197  for ( i = 0 ; i < ( sizeof ( alphabet->lookup ) /
198  sizeof ( alphabet->lookup[0] ) ) ; i++ ) {
199  DBGC2 ( alphabet, " %d", ( alphabet->lookup[i] + 1 ) );
200  }
201  DBGC2 ( alphabet, "\n" );
202 }
203 
204 /**
205  * Construct Huffman alphabet
206  *
207  * @v deflate Decompressor
208  * @v alphabet Huffman alphabet
209  * @v count Number of symbols
210  * @v offset Starting offset within length table
211  * @ret rc Return status code
212  */
213 static int deflate_alphabet ( struct deflate *deflate,
214  struct deflate_alphabet *alphabet,
215  unsigned int count, unsigned int offset ) {
216  struct deflate_huf_symbols *huf_sym;
217  unsigned int huf;
218  unsigned int cum_freq;
219  unsigned int bits;
220  unsigned int raw;
221  unsigned int adjustment;
222  unsigned int prefix;
223  int complete;
224 
225  /* Clear symbol table */
226  memset ( alphabet->huf, 0, sizeof ( alphabet->huf ) );
227 
228  /* Count number of symbols with each Huffman-coded length */
229  for ( raw = 0 ; raw < count ; raw++ ) {
230  bits = deflate_length ( deflate, ( raw + offset ) );
231  if ( bits )
232  alphabet->huf[ bits - 1 ].freq++;
233  }
234 
235  /* Populate Huffman-coded symbol table */
236  huf = 0;
237  cum_freq = 0;
238  for ( bits = 1 ; bits <= ( sizeof ( alphabet->huf ) /
239  sizeof ( alphabet->huf[0] ) ) ; bits++ ) {
240  huf_sym = &alphabet->huf[ bits - 1 ];
241  huf_sym->bits = bits;
242  huf_sym->shift = ( 16 - bits );
243  huf_sym->start = ( huf << huf_sym->shift );
244  huf_sym->raw = &alphabet->raw[cum_freq];
245  huf += huf_sym->freq;
246  if ( huf > ( 1U << bits ) ) {
247  DBGC ( alphabet, "DEFLATE %p \"%s\" has too many "
248  "symbols with lengths <=%d\n", deflate,
249  deflate_alphabet_name ( deflate, alphabet ),
250  bits );
251  return -EINVAL;
252  }
253  huf <<= 1;
254  cum_freq += huf_sym->freq;
255  }
256  complete = ( huf == ( 1U << bits ) );
257 
258  /* Populate raw symbol table */
259  for ( raw = 0 ; raw < count ; raw++ ) {
260  bits = deflate_length ( deflate, ( raw + offset ) );
261  if ( bits ) {
262  huf_sym = &alphabet->huf[ bits - 1 ];
263  *(huf_sym->raw++) = raw;
264  }
265  }
266 
267  /* Adjust Huffman-coded symbol table raw pointers and populate
268  * quick lookup table.
269  */
270  for ( bits = 1 ; bits <= ( sizeof ( alphabet->huf ) /
271  sizeof ( alphabet->huf[0] ) ) ; bits++ ) {
272  huf_sym = &alphabet->huf[ bits - 1 ];
273 
274  /* Adjust raw pointer */
275  huf_sym->raw -= huf_sym->freq; /* Reset to first symbol */
276  adjustment = ( huf_sym->start >> huf_sym->shift );
277  huf_sym->raw -= adjustment; /* Adjust for quick indexing */
278 
279  /* Populate quick lookup table */
280  for ( prefix = ( huf_sym->start >> DEFLATE_HUFFMAN_QL_SHIFT ) ;
281  prefix < ( 1 << DEFLATE_HUFFMAN_QL_BITS ) ; prefix++ ) {
282  alphabet->lookup[prefix] = ( bits - 1 );
283  }
284  }
285 
286  /* Dump alphabet (for debugging) */
287  deflate_dump_alphabet ( deflate, alphabet );
288 
289  /* Check that there are no invalid codes */
290  if ( ! complete ) {
291  DBGC ( alphabet, "DEFLATE %p \"%s\" is incomplete\n", deflate,
292  deflate_alphabet_name ( deflate, alphabet ) );
293  return -EINVAL;
294  }
295 
296  return 0;
297 }
298 
299 /**
300  * Attempt to accumulate bits from input stream
301  *
302  * @v deflate Decompressor
303  * @v target Number of bits to accumulate
304  * @ret excess Number of excess bits accumulated (may be negative)
305  */
306 static int deflate_accumulate ( struct deflate *deflate,
307  unsigned int target ) {
308  uint8_t byte;
309 
310  while ( deflate->bits < target ) {
311 
312  /* Check for end of input */
313  if ( deflate->in == deflate->end )
314  break;
315 
316  /* Acquire byte from input */
317  byte = *(deflate->in++);
319  ( byte << deflate->bits ) );
321  ( deflate_reverse[byte] <<
322  ( 24 - deflate->bits ) ) );
323  deflate->bits += 8;
324 
325  /* Sanity check */
326  assert ( deflate->bits <=
327  ( 8 * sizeof ( deflate->accumulator ) ) );
328  }
329 
330  return ( deflate->bits - target );
331 }
332 
333 /**
334  * Consume accumulated bits from the input stream
335  *
336  * @v deflate Decompressor
337  * @v count Number of accumulated bits to consume
338  * @ret data Consumed bits
339  */
340 static int deflate_consume ( struct deflate *deflate, unsigned int count ) {
341  int data;
342 
343  /* Sanity check */
344  assert ( count <= deflate->bits );
345 
346  /* Extract data and consume bits */
347  data = ( deflate->accumulator & ( ( 1 << count ) - 1 ) );
350  deflate->bits -= count;
351 
352  return data;
353 }
354 
355 /**
356  * Attempt to extract a fixed number of bits from input stream
357  *
358  * @v deflate Decompressor
359  * @v target Number of bits to extract
360  * @ret data Extracted bits (or negative if not yet accumulated)
361  */
362 static int deflate_extract ( struct deflate *deflate, unsigned int target ) {
363  int excess;
364  int data;
365 
366  /* Return immediately if we are attempting to extract zero bits */
367  if ( target == 0 )
368  return 0;
369 
370  /* Attempt to accumulate bits */
371  excess = deflate_accumulate ( deflate, target );
372  if ( excess < 0 )
373  return excess;
374 
375  /* Extract data and consume bits */
376  data = deflate_consume ( deflate, target );
377  DBGCP ( deflate, "DEFLATE %p extracted %s = %#x = %d\n", deflate,
378  deflate_bin ( data, target ), data, data );
379 
380  return data;
381 }
382 
383 /**
384  * Attempt to decode a Huffman-coded symbol from input stream
385  *
386  * @v deflate Decompressor
387  * @v alphabet Huffman alphabet
388  * @ret code Raw code (or negative if not yet accumulated)
389  */
390 static int deflate_decode ( struct deflate *deflate,
391  struct deflate_alphabet *alphabet ) {
392  struct deflate_huf_symbols *huf_sym;
393  uint16_t huf;
394  unsigned int lookup_index;
395  int excess;
396  unsigned int raw;
397 
398  /* Attempt to accumulate maximum required number of bits.
399  * There may be fewer bits than this remaining in the stream,
400  * even if the stream still contains some complete
401  * Huffman-coded symbols.
402  */
404 
405  /* Normalise the bit-reversed accumulated value to 16 bits */
406  huf = ( deflate->rotalumucca >> 16 );
407 
408  /* Find symbol set for this length */
409  lookup_index = ( huf >> DEFLATE_HUFFMAN_QL_SHIFT );
410  huf_sym = &alphabet->huf[ alphabet->lookup[ lookup_index ] ];
411  while ( huf < huf_sym->start )
412  huf_sym--;
413 
414  /* Calculate number of excess bits, and return if not yet complete */
415  excess = ( deflate->bits - huf_sym->bits );
416  if ( excess < 0 )
417  return excess;
418 
419  /* Consume bits */
420  deflate_consume ( deflate, huf_sym->bits );
421 
422  /* Look up raw symbol */
423  raw = huf_sym->raw[ huf >> huf_sym->shift ];
424  DBGCP ( deflate, "DEFLATE %p decoded %s = %#x = %d\n", deflate,
425  deflate_bin ( ( huf >> huf_sym->shift ), huf_sym->bits ),
426  raw, raw );
427 
428  return raw;
429 }
430 
431 /**
432  * Discard bits up to the next byte boundary
433  *
434  * @v deflate Decompressor
435  */
436 static void deflate_discard_to_byte ( struct deflate *deflate ) {
437 
438  deflate_consume ( deflate, ( deflate->bits & 7 ) );
439 }
440 
441 /**
442  * Copy data to output buffer (if available)
443  *
444  * @v out Output data buffer
445  * @v in Input data
446  * @v len Length to copy
447  */
448 static void deflate_copy ( struct deflate_chunk *out, const void *in,
449  size_t len ) {
450  const uint8_t *in_byte = in;
451  uint8_t *out_byte = ( out->data + out->offset );
452  size_t copy_len;
453 
454  /* Copy data one byte at a time, to allow for overlap */
455  if ( out->offset < out->len ) {
456  copy_len = ( out->len - out->offset );
457  if ( copy_len > len )
458  copy_len = len;
459  while ( copy_len-- )
460  *(out_byte++) = *(in_byte++);
461  }
462  out->offset += len;
463 }
464 
465 /**
466  * Inflate compressed data
467  *
468  * @v deflate Decompressor
469  * @v data Compressed input data
470  * @v len Length of compressed input data
471  * @v out Output data buffer
472  * @ret rc Return status code
473  *
474  * The caller can use deflate_finished() to determine whether a
475  * successful return indicates that the decompressor is merely waiting
476  * for more input.
477  *
478  * Data will not be written beyond the specified end of the output
479  * data buffer, but the offset within the output data buffer will be
480  * updated to reflect the amount that should have been written. The
481  * caller can use this to find the length of the decompressed data
482  * before allocating the output data buffer.
483  */
484 int deflate_inflate ( struct deflate *deflate, const void *data, size_t len,
485  struct deflate_chunk *out ) {
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 */
543  deflate->header = 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;
642  deflate->distance_count = 32;
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 );
670  deflate->length_index = 0;
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 */
687  while ( deflate->length_index < deflate->length_target ) {
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 */
699  deflate_set_length ( deflate, index, clen );
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,
707  ( DEFLATE_CODELEN_MAX_CODE + 1 ),
708  0 ) ) != 0 )
709  return rc;
710 
711  /* Prepare for decoding literal/length/distance code lengths */
712  memset ( &deflate->lengths, 0, sizeof ( deflate->lengths ) );
713  deflate->length_index = 0;
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 */
781  if ( ( rc = deflate_alphabet ( deflate, &deflate->litlen,
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;
820  uint8_t byte;
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--;
858  deflate->dup_len =
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 */
880  deflate->dup_len += extra;
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 );
981  deflate->resume = NULL;
982  return 0;
983  }
984 }
985 
986 /**
987  * Initialise decompressor
988  *
989  * @v deflate Decompressor
990  * @v format Compression format code
991  */
993  static int global_init_done;
994  uint8_t i;
995  uint8_t bit;
996  uint8_t byte;
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 ) );
1041  deflate->format = format;
1042 }
uint32_t base
Base.
Definition: librm.h:138
#define EINVAL
Invalid argument.
Definition: errno.h:429
static void deflate_set_length(struct deflate *deflate, unsigned int index, unsigned int bits)
Set Huffman symbol length.
Definition: deflate.c:120
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned short uint16_t
Definition: stdint.h:11
unsigned int length_index
Current length index within a set of code lengths.
Definition: deflate.h:186
#define DEFLATE_DYNAMIC_HLIT_LSB
Dynamic header HLIT field LSB.
Definition: deflate.h:52
static uint16_t deflate_distance_base[32]
Distance base values.
Definition: deflate.c:71
__be32 in[4]
Definition: CIB_PRM.h:35
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
#define DEFLATE_CODELEN_BITS
Dynamic header code length length (in bits)
Definition: deflate.h:70
struct deflate_alphabet distance_codelen
Distance and code length Huffman alphabet.
Definition: deflate.h:217
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
Error codes.
unsigned int length_target
Target length index within a set of code lengths.
Definition: deflate.h:188
uint8_t extra
Signature extra byte.
Definition: smbios.h:18
#define DEFLATE_DYNAMIC_HCLEN_MASK
Dynamic header HCLEN field mask.
Definition: deflate.h:67
#define DEFLATE_HUFFMAN_QL_SHIFT
Quick lookup shift.
Definition: deflate.h:82
unsigned int extra_bits
Number of extra bits required.
Definition: deflate.h:192
#define DEFLATE_HUFFMAN_BITS
Maximum length of a Huffman symbol (in bits)
Definition: deflate.h:73
size_t dup_distance
Distance of a duplicated string.
Definition: deflate.h:196
#define DEFLATE_HEADER_BTYPE_STATIC
Block header type: static Huffman alphabet.
Definition: deflate.h:40
unsigned int header
Current block header.
Definition: deflate.h:182
#define DBGC(...)
Definition: compiler.h:505
uint16_t raw[0]
Raw symbols.
Definition: deflate.h:144
enum deflate_format format
Format.
Definition: deflate.h:164
long index
Definition: bigint.h:65
unsigned int length
Current length within a set of code lengths.
Definition: deflate.h:190
A Huffman-coded set of symbols of a given length.
Definition: deflate.h:115
char prefix[4]
Definition: vmconsole.c:53
#define ZLIB_ADLER32_BITS
ZLIB ADLER32 length (in bits)
Definition: deflate.h:112
uint32_t start
First symbol of this length (normalised to 16 bits)
Definition: deflate.h:128
static unsigned int unsigned int bit
Definition: bigint.h:392
A Huffman-coded alphabet.
Definition: deflate.h:134
#define DEFLATE_LITLEN_END
Literal/length end of block code.
Definition: deflate.h:85
static unsigned int deflate_length(struct deflate *deflate, unsigned int index)
Get Huffman symbol length.
Definition: deflate.c:133
Character types.
static int deflate_alphabet(struct deflate *deflate, struct deflate_alphabet *alphabet, unsigned int count, unsigned int offset)
Construct Huffman alphabet.
Definition: deflate.c:213
#define DEFLATE_DYNAMIC_HDIST_MASK
Dynamic header HDIST field mask.
Definition: deflate.h:61
uint8_t count
Repetition count.
Definition: deflate.h:152
static unsigned int code
Response code.
Definition: hyperv.h:26
__be32 out[4]
Definition: CIB_PRM.h:36
unsigned int bits
Number of bits within the accumulator.
Definition: deflate.h:179
static const char * deflate_bin(unsigned long value, unsigned int bits)
Transcribe binary value (for debugging)
Definition: deflate.c:98
struct ib_mad_cm cm
Definition: ib_mad.h:15
#define ENOTSUP
Operation not supported.
Definition: errno.h:590
static int isprint(int character)
Check if character is printable.
Definition: ctype.h:98
#define DEFLATE_DYNAMIC_HCLEN_LSB
Dynamic header HCLEN field LSB.
Definition: deflate.h:64
uint32_t start
Starting offset.
Definition: netvsc.h:12
static void deflate_dump_alphabet(struct deflate *deflate, struct deflate_alphabet *alphabet)
Dump Huffman alphabet (for debugging)
Definition: deflate.c:165
static int deflate_accumulate(struct deflate *deflate, unsigned int target)
Attempt to accumulate bits from input stream.
Definition: deflate.c:306
#define DEFLATE_HEADER_BTYPE_LSB
Block header type LSB.
Definition: deflate.h:31
#define ZLIB_HEADER_BITS
ZLIB header length (in bits)
Definition: deflate.h:97
A chunk of data.
Definition: deflate.h:246
void deflate_init(struct deflate *deflate, enum deflate_format format)
Initialise decompressor.
Definition: deflate.c:992
#define DEFLATE_LITERAL_LEN_BITS
Literal header LEN/NLEN field length (in bits)
Definition: deflate.h:46
uint32_t accumulator
Accumulator.
Definition: deflate.h:172
size_t dup_len
Length of a duplicated string.
Definition: deflate.h:194
#define ZLIB_HEADER_CM_DEFLATE
ZLIB header compression method: DEFLATE.
Definition: deflate.h:106
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void * resume
Resume point.
Definition: deflate.h:162
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
ring len
Length.
Definition: dwmac.h:231
#define DEFLATE_HUFFMAN_QL_BITS
Quick lookup length for a Huffman symbol (in bits)
Definition: deflate.h:79
uint8_t lookup[1<< DEFLATE_HUFFMAN_QL_BITS]
Quick lookup table.
Definition: deflate.h:138
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:136
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
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
uint16_t freq
Number of Huffman-coded symbols having this length.
Definition: deflate.h:121
ZLIB header and footer.
Definition: deflate.h:21
#define DEFLATE_DYNAMIC_HLIT_MASK
Dynamic header HLIT field mask.
Definition: deflate.h:55
uint8_t fill
Length pair.
Definition: deflate.h:150
deflate_format
Compression formats.
Definition: deflate.h:17
#define DEFLATE_CODELEN_MAX_CODE
Maximum value of a code length code.
Definition: deflate.h:94
static const char * deflate_alphabet_name(struct deflate *deflate, struct deflate_alphabet *alphabet)
Determine Huffman alphabet name (for debugging)
Definition: deflate.c:147
uint32_t rotalumucca
Bit-reversed accumulator.
Definition: deflate.h:177
unsigned char uint8_t
Definition: stdint.h:10
unsigned int distance_count
Number of symbols in the distance Huffman alphabet.
Definition: deflate.h:225
static uint8_t deflate_litlen_base[28]
Literal/length base values.
Definition: deflate.c:62
static int deflate_consume(struct deflate *deflate, unsigned int count)
Consume accumulated bits from the input stream.
Definition: deflate.c:340
int deflate_inflate(struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
Inflate compressed data.
Definition: deflate.c:484
unsigned char byte
Definition: smc9000.h:38
struct deflate_alphabet litlen
Literal/length Huffman alphabet.
Definition: deflate.h:199
uint16_t * raw
Raw symbols having this length.
Definition: deflate.h:130
static uint8_t deflate_codelen_map[19]
Code length map.
Definition: deflate.c:74
const uint8_t * in
Current input data pointer.
Definition: deflate.h:167
#define DEFLATE_DYNAMIC_BITS
Dynamic header length (in bits)
Definition: deflate.h:49
Raw DEFLATE data (no header or footer)
Definition: deflate.h:19
static volatile void * bits
Definition: bitops.h:28
#define DBGC2(...)
Definition: compiler.h:522
A static Huffman alphabet length pattern.
Definition: deflate.h:148
#define DEFLATE_HEADER_BTYPE_LITERAL
Block header type: literal data.
Definition: deflate.h:37
const uint8_t * end
End of input data pointer.
Definition: deflate.h:169
struct ena_llq_option header
Header locations.
Definition: ena.h:16
#define DEFLATE_HEADER_BITS
Block header length (in bits)
Definition: deflate.h:25
uint8_t data[48]
Additional event data.
Definition: ena.h:22
#define ZLIB_HEADER_CM_MASK
ZLIB header compression method mask.
Definition: deflate.h:103
static struct deflate_static_length_pattern deflate_static_length_patterns[]
Static Huffman alphabet length patterns.
Definition: deflate.c:79
#define DBGCP(...)
Definition: compiler.h:539
__be32 raw[7]
Definition: CIB_PRM.h:28
unsigned int litlen_count
Number of symbols in the literal/length Huffman alphabet.
Definition: deflate.h:206
#define ZLIB_HEADER_CM_LSB
ZLIB header compression method LSB.
Definition: deflate.h:100
#define DBG_EXTRA
Definition: compiler.h:319
#define DEFLATE_HEADER_BFINAL_BIT
Block header final block flags bit.
Definition: deflate.h:28
uint8_t lengths[((DEFLATE_LITLEN_MAX_CODE+1)+(DEFLATE_DISTANCE_MAX_CODE+1)+1)/2]
Huffman code lengths.
Definition: deflate.h:242
static void deflate_discard_to_byte(struct deflate *deflate)
Discard bits up to the next byte boundary.
Definition: deflate.c:436
uint16_t offset
Offset to command line.
Definition: bzimage.h:8
DEFLATE decompression algorithm.
uint8_t shift
Shift to normalise symbols of this length to 16 bits.
Definition: deflate.h:119
int const char * format
Definition: xfer.h:105
FILE_SECBOOT(PERMITTED)
#define ZLIB_HEADER_FDICT_BIT
ZLIB header preset dictionary flag bit.
Definition: deflate.h:109
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
#define DEFLATE_DYNAMIC_HDIST_LSB
Dynamic header HDIST field LSB.
Definition: deflate.h:58
uint8_t bits
Length of Huffman-coded symbols.
Definition: deflate.h:117
static uint8_t deflate_reverse[256]
Byte reversal table.
Definition: deflate.c:51
size_t remaining
Remaining length of data (e.g.
Definition: deflate.h:184
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
#define DEFLATE_HEADER_BTYPE_DYNAMIC
Block header type: dynamic Huffman alphabet.
Definition: deflate.h:43
Decompressor.
Definition: deflate.h:156