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 
26 #include <string.h>
27 #include <strings.h>
28 #include <errno.h>
29 #include <assert.h>
30 #include <ctype.h>
31 #include <ipxe/deflate.h>
32 
33 /** @file
34  *
35  * DEFLATE decompression algorithm
36  *
37  * This file implements the decompression half of the DEFLATE
38  * algorithm specified in RFC 1951.
39  *
40  * Portions of this code are derived from wimboot's xca.c.
41  *
42  */
43 
44 /**
45  * Byte reversal table
46  *
47  * For some insane reason, the DEFLATE format stores some values in
48  * bit-reversed order.
49  */
51 
52 /** Literal/length base values
53  *
54  * We include entries only for literal/length codes 257-284. Code 285
55  * does not fit the pattern (it represents a length of 258; following
56  * the pattern from the earlier codes would give a length of 259), and
57  * has no extra bits. Codes 286-287 are invalid, but can occur. We
58  * treat any code greater than 284 as meaning "length 258, no extra
59  * bits".
60  */
62 
63 /** Distance base values
64  *
65  * We include entries for all possible codes 0-31, avoiding the need
66  * to check for undefined codes 30 and 31 before performing the
67  * lookup. Codes 30 and 31 are never initialised, and will therefore
68  * be treated as meaning "14 extra bits, base distance 0".
69  */
71 
72 /** Code length map */
74  16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
75 };
76 
77 /** Static Huffman alphabet length patterns */
79  /* Literal/length code lengths */
80  { 0x88, ( ( ( 143 - 0 ) + 1 ) / 2 ) },
81  { 0x99, ( ( ( 255 - 144 ) + 1 ) / 2 ) },
82  { 0x77, ( ( ( 279 - 256 ) + 1 ) / 2 ) },
83  { 0x88, ( ( ( 287 - 280 ) + 1 ) / 2 ) },
84  /* Distance code lengths */
85  { 0x55, ( ( ( 31 - 0 ) + 1 ) / 2 ) },
86  /* End marker */
87  { 0, 0 }
88 };
89 
90 /**
91  * Transcribe binary value (for debugging)
92  *
93  * @v value Value
94  * @v bits Length of value (in bits)
95  * @ret string Transcribed value
96  */
97 static const char * deflate_bin ( unsigned long value, unsigned int bits ) {
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 }
111 
112 /**
113  * Set Huffman symbol length
114  *
115  * @v deflate Decompressor
116  * @v index Index within lengths
117  * @v bits Symbol length (in bits)
118  */
119 static void deflate_set_length ( struct deflate *deflate, unsigned int index,
120  unsigned int bits ) {
121 
122  deflate->lengths[ index / 2 ] |= ( bits << ( 4 * ( index % 2 ) ) );
123 }
124 
125 /**
126  * Get Huffman symbol length
127  *
128  * @v deflate Decompressor
129  * @v index Index within lengths
130  * @ret bits Symbol length (in bits)
131  */
132 static unsigned int deflate_length ( struct deflate *deflate,
133  unsigned int index ) {
134 
135  return ( ( deflate->lengths[ index / 2 ] >> ( 4 * ( index % 2 ) ) )
136  & 0x0f );
137 }
138 
139 /**
140  * Determine Huffman alphabet name (for debugging)
141  *
142  * @v deflate Decompressor
143  * @v alphabet Huffman alphabet
144  * @ret name Alphabet name
145  */
146 static const char * deflate_alphabet_name ( struct deflate *deflate,
147  struct deflate_alphabet *alphabet ){
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 }
157 
158 /**
159  * Dump Huffman alphabet (for debugging)
160  *
161  * @v deflate Decompressor
162  * @v alphabet Huffman alphabet
163  */
164 static void deflate_dump_alphabet ( struct deflate *deflate,
165  struct deflate_alphabet *alphabet ) {
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 }
202 
203 /**
204  * Construct Huffman alphabet
205  *
206  * @v deflate Decompressor
207  * @v alphabet Huffman alphabet
208  * @v count Number of symbols
209  * @v offset Starting offset within length table
210  * @ret rc Return status code
211  */
212 static int deflate_alphabet ( struct deflate *deflate,
213  struct deflate_alphabet *alphabet,
214  unsigned int count, unsigned int offset ) {
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 }
297 
298 /**
299  * Attempt to accumulate bits from input stream
300  *
301  * @v deflate Decompressor
302  * @v target Number of bits to accumulate
303  * @ret excess Number of excess bits accumulated (may be negative)
304  */
305 static int deflate_accumulate ( struct deflate *deflate,
306  unsigned int target ) {
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 }
331 
332 /**
333  * Consume accumulated bits from the input stream
334  *
335  * @v deflate Decompressor
336  * @v count Number of accumulated bits to consume
337  * @ret data Consumed bits
338  */
339 static int deflate_consume ( struct deflate *deflate, unsigned int count ) {
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 }
353 
354 /**
355  * Attempt to extract a fixed number of bits from input stream
356  *
357  * @v deflate Decompressor
358  * @v target Number of bits to extract
359  * @ret data Extracted bits (or negative if not yet accumulated)
360  */
361 static int deflate_extract ( struct deflate *deflate, unsigned int target ) {
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 }
381 
382 /**
383  * Attempt to decode a Huffman-coded symbol from input stream
384  *
385  * @v deflate Decompressor
386  * @v alphabet Huffman alphabet
387  * @ret code Raw code (or negative if not yet accumulated)
388  */
389 static int deflate_decode ( struct deflate *deflate,
390  struct deflate_alphabet *alphabet ) {
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 }
429 
430 /**
431  * Discard bits up to the next byte boundary
432  *
433  * @v deflate Decompressor
434  */
435 static void deflate_discard_to_byte ( struct deflate *deflate ) {
436 
437  deflate_consume ( deflate, ( deflate->bits & 7 ) );
438 }
439 
440 /**
441  * Copy data to output buffer (if available)
442  *
443  * @v out Output data buffer
444  * @v in Input data
445  * @v len Length to copy
446  */
447 static void deflate_copy ( struct deflate_chunk *out, const void *in,
448  size_t len ) {
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 }
463 
464 /**
465  * Inflate compressed data
466  *
467  * @v deflate Decompressor
468  * @v data Compressed input data
469  * @v len Length of compressed input data
470  * @v out Output data buffer
471  * @ret rc Return status code
472  *
473  * The caller can use deflate_finished() to determine whether a
474  * successful return indicates that the decompressor is merely waiting
475  * for more input.
476  *
477  * Data will not be written beyond the specified end of the output
478  * data buffer, but the offset within the output data buffer will be
479  * updated to reflect the amount that should have been written. The
480  * caller can use this to find the length of the decompressed data
481  * before allocating the output data buffer.
482  */
483 int deflate_inflate ( struct deflate *deflate, const void *data, size_t len,
484  struct deflate_chunk *out ) {
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 }
984 
985 /**
986  * Initialise decompressor
987  *
988  * @v deflate Decompressor
989  * @v format Compression format code
990  */
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
#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 short uint16_t
Definition: stdint.h:11
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
__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: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
Error codes.
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
#define DEFLATE_HUFFMAN_QL_SHIFT
Quick lookup shift.
Definition: deflate.h:81
unsigned int extra_bits
Number of extra bits required.
Definition: deflate.h:191
#define DEFLATE_HUFFMAN_BITS
Maximum length of a Huffman symbol (in bits)
Definition: deflate.h:72
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
uint16_t raw[0]
Raw symbols.
Definition: deflate.h:143
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
A Huffman-coded set of symbols of a given length.
Definition: deflate.h:114
char prefix[4]
Definition: vmconsole.c:53
#define ZLIB_ADLER32_BITS
ZLIB ADLER32 length (in bits)
Definition: deflate.h:111
uint32_t start
First symbol of this length (normalised to 16 bits)
Definition: deflate.h:127
static unsigned int unsigned int bit
Definition: bigint.h:391
A Huffman-coded alphabet.
Definition: deflate.h:133
#define DEFLATE_LITLEN_END
Literal/length end of block code.
Definition: deflate.h:84
static unsigned int deflate_length(struct deflate *deflate, unsigned int index)
Get Huffman symbol length.
Definition: deflate.c:132
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:212
#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
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
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
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:164
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
A chunk of data.
Definition: deflate.h:245
void deflate_init(struct deflate *deflate, enum deflate_format format)
Initialise decompressor.
Definition: deflate.c:991
#define DEFLATE_LITERAL_LEN_BITS
Literal header LEN/NLEN field length (in bits)
Definition: deflate.h:45
uint32_t accumulator
Accumulator.
Definition: deflate.h:171
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
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
void * resume
Resume point.
Definition: deflate.h:161
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: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
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:361
uint16_t freq
Number of Huffman-coded symbols having this length.
Definition: deflate.h:120
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
deflate_format
Compression formats.
Definition: deflate.h:16
#define DEFLATE_CODELEN_MAX_CODE
Maximum value of a code length code.
Definition: deflate.h:93
static const char * deflate_alphabet_name(struct deflate *deflate, struct deflate_alphabet *alphabet)
Determine Huffman alphabet name (for debugging)
Definition: deflate.c:146
uint32_t rotalumucca
Bit-reversed accumulator.
Definition: deflate.h:176
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
static int deflate_consume(struct deflate *deflate, unsigned int count)
Consume accumulated bits from the input stream.
Definition: deflate.c:339
int deflate_inflate(struct deflate *deflate, const void *data, size_t len, struct deflate_chunk *out)
Inflate compressed data.
Definition: deflate.c:483
unsigned char byte
Definition: smc9000.h:38
struct deflate_alphabet litlen
Literal/length Huffman alphabet.
Definition: deflate.h:198
uint16_t * raw
Raw symbols having this length.
Definition: deflate.h:129
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
__be32 raw[7]
Definition: CIB_PRM.h:28
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 DBG_EXTRA
Definition: compiler.h:319
#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
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:118
int const char * format
Definition: xfer.h:104
#define ZLIB_HEADER_FDICT_BIT
ZLIB header preset dictionary flag bit.
Definition: deflate.h:108
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
#define DEFLATE_DYNAMIC_HDIST_LSB
Dynamic header HDIST field LSB.
Definition: deflate.h:57
uint8_t bits
Length of Huffman-coded symbols.
Definition: deflate.h:116
static uint8_t deflate_reverse[256]
Byte reversal table.
Definition: deflate.c:50
size_t remaining
Remaining length of data (e.g.
Definition: deflate.h:183
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:42
Decompressor.
Definition: deflate.h:155