static void init_fixed_block (grub_file_t file) { int i; /* temporary variable */ unsigned l[288]; /* length list for huft_build */ grub_gzio_t gzio = file->data; /* set up literal table */ for (i = 0; i < 144; i++) l[i] = 8; for (; i < 256; i++) l[i] = 9; for (; i < 280; i++) l[i] = 7; for (; i < 288; i++) /* make a complete, but wrong code set */ l[i] = 8; gzio->bl = 7; if (huft_build (l, 288, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0) { if (grub_errno == GRUB_ERR_NONE) grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "failed in building a Huffman code table"); return; } /* set up distance table */ for (i = 0; i < 30; i++) /* make an incomplete code set */ l[i] = 5; gzio->bd = 5; if (huft_build (l, 30, 0, cpdist, cpdext, &gzio->td, &gzio->bd) > 1) { if (grub_errno == GRUB_ERR_NONE) grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "failed in building a Huffman code table"); huft_free (gzio->tl); gzio->tl = 0; return; } /* indicate we're now working on a block */ gzio->code_state = 0; gzio->block_len++; }
static void init_fixed_block () { int i; /* temporary variable */ unsigned l[288]; /* length list for huft_build */ /* set up literal table */ for (i = 0; i < 144; i++) l[i] = 8; for (; i < 256; i++) l[i] = 9; for (; i < 280; i++) l[i] = 7; for (; i < 288; i++) /* make a complete, but wrong code set */ l[i] = 8; bl = 7; if ((i = huft_build (l, 288, 257, cplens, cplext, &tl, &bl)) != 0) { errnum = ERR_BAD_GZIP_DATA; return; } /* set up distance table */ for (i = 0; i < 30; i++) /* make an incomplete code set */ l[i] = 5; bd = 5; if ((i = huft_build (l, 30, 0, cpdist, cpdext, &td, &bd)) > 1) { errnum = ERR_BAD_GZIP_DATA; return; } /* indicate we're now working on a block */ code_state = 0; block_len++; }
static void init_dynamic_block (grub_file_t file) { int i; /* temporary variables */ unsigned j; unsigned l; /* last length */ unsigned m; /* mask for bit lengths table */ unsigned n; /* number of lengths to get */ unsigned nb; /* number of bit length codes */ unsigned nl; /* number of literal/length codes */ unsigned nd; /* number of distance codes */ unsigned ll[286 + 30]; /* literal/length and distance code lengths */ register ulg b; /* bit buffer */ register unsigned k; /* number of bits in bit buffer */ grub_gzio_t gzio = file->data; /* make local bit buffer */ b = gzio->bb; k = gzio->bk; /* read in table lengths */ NEEDBITS (5); nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */ DUMPBITS (5); NEEDBITS (5); nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */ DUMPBITS (5); NEEDBITS (4); nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */ DUMPBITS (4); if (nl > 286 || nd > 30) { grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too much data"); return; } /* read in bit-length-code lengths */ for (j = 0; j < nb; j++) { NEEDBITS (3); ll[bitorder[j]] = (unsigned) b & 7; DUMPBITS (3); } for (; j < 19; j++) ll[bitorder[j]] = 0; /* build decoding table for trees--single level, 7 bit lookup */ gzio->bl = 7; if (huft_build (ll, 19, 19, NULL, NULL, &gzio->tl, &gzio->bl) != 0) { grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "failed in building a Huffman code table"); return; } /* read in literal and distance code lengths */ n = nl + nd; m = mask_bits[gzio->bl]; i = l = 0; while ((unsigned) i < n) { NEEDBITS ((unsigned) gzio->bl); j = (gzio->td = gzio->tl + ((unsigned) b & m))->b; DUMPBITS (j); j = gzio->td->v.n; if (j < 16) /* length of code in bits (0..15) */ ll[i++] = l = j; /* save last length in l */ else if (j == 16) /* repeat last length 3 to 6 times */ { NEEDBITS (2); j = 3 + ((unsigned) b & 3); DUMPBITS (2); if ((unsigned) i + j > n) { grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found"); return; } while (j--) ll[i++] = l; } else if (j == 17) /* 3 to 10 zero length codes */ { NEEDBITS (3); j = 3 + ((unsigned) b & 7); DUMPBITS (3); if ((unsigned) i + j > n) { grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found"); return; } while (j--) ll[i++] = 0; l = 0; } else /* j == 18: 11 to 138 zero length codes */ { NEEDBITS (7); j = 11 + ((unsigned) b & 0x7f); DUMPBITS (7); if ((unsigned) i + j > n) { grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "too many codes found"); return; } while (j--) ll[i++] = 0; l = 0; } } /* free decoding table for trees */ huft_free (gzio->tl); gzio->td = 0; gzio->tl = 0; /* restore the global bit buffer */ gzio->bb = b; gzio->bk = k; /* build the decoding tables for literal/length and distance codes */ gzio->bl = lbits; if (huft_build (ll, nl, 257, cplens, cplext, &gzio->tl, &gzio->bl) != 0) { grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "failed in building a Huffman code table"); return; } gzio->bd = dbits; if (huft_build (ll + nl, nd, 0, cpdist, cpdext, &gzio->td, &gzio->bd) != 0) { huft_free (gzio->tl); gzio->tl = 0; grub_error (GRUB_ERR_BAD_COMPRESSED_DATA, "failed in building a Huffman code table"); return; } /* indicate we're now working on a block */ gzio->code_state = 0; gzio->block_len++; }
static void init_dynamic_block (void) { int i; /* temporary variables */ unsigned j; unsigned l; /* last length */ unsigned m; /* mask for bit lengths table */ unsigned n; /* number of lengths to get */ unsigned nb; /* number of bit length codes */ unsigned nl; /* number of literal/length codes */ unsigned nd; /* number of distance codes */ unsigned ll[286 + 30]; /* literal/length and distance code lengths */ register ulg b; /* bit buffer */ register unsigned k; /* number of bits in bit buffer */ /* make local bit buffer */ b = bb; k = bk; /* read in table lengths */ NEEDBITS (5); nl = 257 + ((unsigned) b & 0x1f); /* number of literal/length codes */ DUMPBITS (5); NEEDBITS (5); nd = 1 + ((unsigned) b & 0x1f); /* number of distance codes */ DUMPBITS (5); NEEDBITS (4); nb = 4 + ((unsigned) b & 0xf); /* number of bit length codes */ DUMPBITS (4); if (nl > 286 || nd > 30) { errnum = ERR_BAD_GZIP_DATA; return; } /* read in bit-length-code lengths */ for (j = 0; j < nb; j++) { NEEDBITS (3); ll[bitorder[j]] = (unsigned) b & 7; DUMPBITS (3); } for (; j < 19; j++) ll[bitorder[j]] = 0; /* build decoding table for trees--single level, 7 bit lookup */ bl = 7; if ((i = huft_build (ll, 19, 19, NULL, NULL, &tl, &bl)) != 0) { errnum = ERR_BAD_GZIP_DATA; return; } /* read in literal and distance code lengths */ n = nl + nd; m = mask_bits[bl]; i = l = 0; while ((unsigned) i < n) { NEEDBITS ((unsigned) bl); j = (td = tl + ((unsigned) b & m))->b; DUMPBITS (j); j = td->v.n; if (j < 16) /* length of code in bits (0..15) */ ll[i++] = l = j; /* save last length in l */ else if (j == 16) /* repeat last length 3 to 6 times */ { NEEDBITS (2); j = 3 + ((unsigned) b & 3); DUMPBITS (2); if ((unsigned) i + j > n) { errnum = ERR_BAD_GZIP_DATA; return; } while (j--) ll[i++] = l; } else if (j == 17) /* 3 to 10 zero length codes */ { NEEDBITS (3); j = 3 + ((unsigned) b & 7); DUMPBITS (3); if ((unsigned) i + j > n) { errnum = ERR_BAD_GZIP_DATA; return; } while (j--) ll[i++] = 0; l = 0; } else /* j == 18: 11 to 138 zero length codes */ { NEEDBITS (7); j = 11 + ((unsigned) b & 0x7f); DUMPBITS (7); if ((unsigned) i + j > n) { errnum = ERR_BAD_GZIP_DATA; return; } while (j--) ll[i++] = 0; l = 0; } } /* free decoding table for trees */ reset_linalloc (); /* restore the global bit buffer */ bb = b; bk = k; /* build the decoding tables for literal/length and distance codes */ bl = lbits; if ((i = huft_build (ll, nl, 257, cplens, cplext, &tl, &bl)) != 0) { #if 0 if (i == 1) printf ("gunzip: incomplete literal tree\n"); #endif errnum = ERR_BAD_GZIP_DATA; return; } bd = dbits; if ((i = huft_build (ll + nl, nd, 0, cpdist, cpdext, &td, &bd)) != 0) { #if 0 if (i == 1) printf ("gunzip: incomplete distance tree\n"); #endif errnum = ERR_BAD_GZIP_DATA; return; } /* indicate we're now working on a block */ code_state = 0; block_len++; }
Author: Neville_Humphrys (Xara Group Ltd) <*****@*****.**> Created: 25/05/95 Inputs: c 19 code lengths bb bits tree desired/actual depth tb bits tree result z for zfree function Purpose: ********************************************************************************************/ INT32 ZipInflate::inflate_trees_bits(uIntf *c, uIntf *bb, inflate_huft * FAR *tb, ZStream *z) { INT32 r; r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z); if (r == Z_DATA_ERROR) z->msg = (char*)"oversubscribed dynamic bit lengths tree"; else if (r == Z_BUF_ERROR) { inflate_trees_free(*tb, z); z->msg = (char*)"incomplete dynamic bit lengths tree"; r = Z_DATA_ERROR; } return r; } /******************************************************************************************** >