github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/klauspost/compress/flate/inflate.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:generate go run gen.go -output fixedhuff.go
     6  
     7  // Package flate implements the DEFLATE compressed data format, described in
     8  // RFC 1951.  The gzip and zlib packages implement access to DEFLATE-based file
     9  // formats.
    10  package flate
    11  
    12  import (
    13  	"bufio"
    14  	"io"
    15  	"strconv"
    16  )
    17  
    18  const (
    19  	maxCodeLen = 16    // max length of Huffman code
    20  	maxHist    = 32768 // max history required
    21  	// The next three numbers come from the RFC section 3.2.7, with the
    22  	// additional proviso in section 3.2.5 which implies that distance codes
    23  	// 30 and 31 should never occur in compressed data.
    24  	maxNumLit  = 286
    25  	maxNumDist = 30
    26  	numCodes   = 19 // number of codes in Huffman meta-code
    27  )
    28  
    29  // A CorruptInputError reports the presence of corrupt input at a given offset.
    30  type CorruptInputError int64
    31  
    32  func (e CorruptInputError) Error() string {
    33  	return "flate: corrupt input before offset " + strconv.FormatInt(int64(e), 10)
    34  }
    35  
    36  // An InternalError reports an error in the flate code itself.
    37  type InternalError string
    38  
    39  func (e InternalError) Error() string { return "flate: internal error: " + string(e) }
    40  
    41  // A ReadError reports an error encountered while reading input.
    42  type ReadError struct {
    43  	Offset int64 // byte offset where error occurred
    44  	Err    error // error returned by underlying Read
    45  }
    46  
    47  func (e *ReadError) Error() string {
    48  	return "flate: read error at offset " + strconv.FormatInt(e.Offset, 10) + ": " + e.Err.Error()
    49  }
    50  
    51  // A WriteError reports an error encountered while writing output.
    52  type WriteError struct {
    53  	Offset int64 // byte offset where error occurred
    54  	Err    error // error returned by underlying Write
    55  }
    56  
    57  func (e *WriteError) Error() string {
    58  	return "flate: write error at offset " + strconv.FormatInt(e.Offset, 10) + ": " + e.Err.Error()
    59  }
    60  
    61  // Resetter resets a ReadCloser returned by NewReader or NewReaderDict to
    62  // to switch to a new underlying Reader. This permits reusing a ReadCloser
    63  // instead of allocating a new one.
    64  type Resetter interface {
    65  	// Reset discards any buffered data and resets the Resetter as if it was
    66  	// newly initialized with the given reader.
    67  	Reset(r io.Reader, dict []byte) error
    68  }
    69  
    70  // Note that much of the implementation of huffmanDecoder is also copied
    71  // into gen.go (in package main) for the purpose of precomputing the
    72  // fixed huffman tables so they can be included statically.
    73  
    74  // The data structure for decoding Huffman tables is based on that of
    75  // zlib. There is a lookup table of a fixed bit width (huffmanChunkBits),
    76  // For codes smaller than the table width, there are multiple entries
    77  // (each combination of trailing bits has the same value). For codes
    78  // larger than the table width, the table contains a link to an overflow
    79  // table. The width of each entry in the link table is the maximum code
    80  // size minus the chunk width.
    81  
    82  // Note that you can do a lookup in the table even without all bits
    83  // filled. Since the extra bits are zero, and the DEFLATE Huffman codes
    84  // have the property that shorter codes come before longer ones, the
    85  // bit length estimate in the result is a lower bound on the actual
    86  // number of bits.
    87  
    88  // chunk & 15 is number of bits
    89  // chunk >> 4 is value, including table link
    90  
    91  const (
    92  	huffmanChunkBits  = 9
    93  	huffmanNumChunks  = 1 << huffmanChunkBits
    94  	huffmanCountMask  = 15
    95  	huffmanValueShift = 4
    96  )
    97  
    98  type huffmanDecoder struct {
    99  	min      int                      // the minimum code length
   100  	chunks   [huffmanNumChunks]uint32 // chunks as described above
   101  	links    [][]uint32               // overflow links
   102  	linkMask uint32                   // mask the width of the link table
   103  }
   104  
   105  // Initialize Huffman decoding tables from array of code lengths.
   106  // Following this function, h is guaranteed to be initialized into a complete
   107  // tree (i.e., neither over-subscribed nor under-subscribed). The exception is a
   108  // degenerate case where the tree has only a single symbol with length 1. Empty
   109  // trees are permitted.
   110  func (h *huffmanDecoder) init(bits []int) bool {
   111  	// Sanity enables additional runtime tests during Huffman
   112  	// table construction.  It's intended to be used during
   113  	// development to supplement the currently ad-hoc unit tests.
   114  	const sanity = false
   115  
   116  	if h.min != 0 {
   117  		*h = huffmanDecoder{}
   118  	}
   119  
   120  	// Count number of codes of each length,
   121  	// compute min and max length.
   122  	var count [maxCodeLen]int
   123  	var min, max int
   124  	for _, n := range bits {
   125  		if n == 0 {
   126  			continue
   127  		}
   128  		if min == 0 || n < min {
   129  			min = n
   130  		}
   131  		if n > max {
   132  			max = n
   133  		}
   134  		count[n]++
   135  	}
   136  
   137  	// Empty tree. The decompressor.huffSym function will fail later if the tree
   138  	// is used. Technically, an empty tree is only valid for the HDIST tree and
   139  	// not the HCLEN and HLIT tree. However, a stream with an empty HCLEN tree
   140  	// is guaranteed to fail since it will attempt to use the tree to decode the
   141  	// codes for the HLIT and HDIST trees. Similarly, an empty HLIT tree is
   142  	// guaranteed to fail later since the compressed data section must be
   143  	// composed of at least one symbol (the end-of-block marker).
   144  	if max == 0 {
   145  		return true
   146  	}
   147  
   148  	code := 0
   149  	var nextcode [maxCodeLen]int
   150  	for i := min; i <= max; i++ {
   151  		code <<= 1
   152  		nextcode[i] = code
   153  		code += count[i]
   154  	}
   155  
   156  	// Check that the coding is complete (i.e., that we've
   157  	// assigned all 2-to-the-max possible bit sequences).
   158  	// Exception: To be compatible with zlib, we also need to
   159  	// accept degenerate single-code codings.  See also
   160  	// TestDegenerateHuffmanCoding.
   161  	if code != 1<<uint(max) && !(code == 1 && max == 1) {
   162  		return false
   163  	}
   164  
   165  	h.min = min
   166  	if max > huffmanChunkBits {
   167  		numLinks := 1 << (uint(max) - huffmanChunkBits)
   168  		h.linkMask = uint32(numLinks - 1)
   169  
   170  		// create link tables
   171  		link := nextcode[huffmanChunkBits+1] >> 1
   172  		h.links = make([][]uint32, huffmanNumChunks-link)
   173  		for j := uint(link); j < huffmanNumChunks; j++ {
   174  			reverse := int(reverseByte[j>>8]) | int(reverseByte[j&0xff])<<8
   175  			reverse >>= uint(16 - huffmanChunkBits)
   176  			off := j - uint(link)
   177  			if sanity && h.chunks[reverse] != 0 {
   178  				panic("impossible: overwriting existing chunk")
   179  			}
   180  			h.chunks[reverse] = uint32(off<<huffmanValueShift | (huffmanChunkBits + 1))
   181  			h.links[off] = make([]uint32, numLinks)
   182  		}
   183  	}
   184  
   185  	for i, n := range bits {
   186  		if n == 0 {
   187  			continue
   188  		}
   189  		code := nextcode[n]
   190  		nextcode[n]++
   191  		chunk := uint32(i<<huffmanValueShift | n)
   192  		reverse := int(reverseByte[code>>8]) | int(reverseByte[code&0xff])<<8
   193  		reverse >>= uint(16 - n)
   194  		if n <= huffmanChunkBits {
   195  			for off := reverse; off < len(h.chunks); off += 1 << uint(n) {
   196  				// We should never need to overwrite
   197  				// an existing chunk.  Also, 0 is
   198  				// never a valid chunk, because the
   199  				// lower 4 "count" bits should be
   200  				// between 1 and 15.
   201  				if sanity && h.chunks[off] != 0 {
   202  					panic("impossible: overwriting existing chunk")
   203  				}
   204  				h.chunks[off] = chunk
   205  			}
   206  		} else {
   207  			j := reverse & (huffmanNumChunks - 1)
   208  			if sanity && h.chunks[j]&huffmanCountMask != huffmanChunkBits+1 {
   209  				// Longer codes should have been
   210  				// associated with a link table above.
   211  				panic("impossible: not an indirect chunk")
   212  			}
   213  			value := h.chunks[j] >> huffmanValueShift
   214  			linktab := h.links[value]
   215  			reverse >>= huffmanChunkBits
   216  			for off := reverse; off < len(linktab); off += 1 << uint(n-huffmanChunkBits) {
   217  				if sanity && linktab[off] != 0 {
   218  					panic("impossible: overwriting existing chunk")
   219  				}
   220  				linktab[off] = chunk
   221  			}
   222  		}
   223  	}
   224  
   225  	if sanity {
   226  		// Above we've sanity checked that we never overwrote
   227  		// an existing entry.  Here we additionally check that
   228  		// we filled the tables completely.
   229  		for i, chunk := range h.chunks {
   230  			if chunk == 0 {
   231  				// As an exception, in the degenerate
   232  				// single-code case, we allow odd
   233  				// chunks to be missing.
   234  				if code == 1 && i%2 == 1 {
   235  					continue
   236  				}
   237  				panic("impossible: missing chunk")
   238  			}
   239  		}
   240  		for _, linktab := range h.links {
   241  			for _, chunk := range linktab {
   242  				if chunk == 0 {
   243  					panic("impossible: missing chunk")
   244  				}
   245  			}
   246  		}
   247  	}
   248  
   249  	return true
   250  }
   251  
   252  // The actual read interface needed by NewReader.
   253  // If the passed in io.Reader does not also have ReadByte,
   254  // the NewReader will introduce its own buffering.
   255  type Reader interface {
   256  	io.Reader
   257  	io.ByteReader
   258  }
   259  
   260  // Decompress state.
   261  type decompressor struct {
   262  	// Input source.
   263  	r       Reader
   264  	roffset int64
   265  	woffset int64
   266  
   267  	// Input bits, in top of b.
   268  	b  uint32
   269  	nb uint
   270  
   271  	// Huffman decoders for literal/length, distance.
   272  	h1, h2 huffmanDecoder
   273  
   274  	// Length arrays used to define Huffman codes.
   275  	bits     *[maxNumLit + maxNumDist]int
   276  	codebits *[numCodes]int
   277  
   278  	// Output history, buffer.
   279  	hist  *[maxHist]byte
   280  	hp    int  // current output position in buffer
   281  	hw    int  // have written hist[0:hw] already
   282  	hfull bool // buffer has filled at least once
   283  
   284  	// Temporary buffer (avoids repeated allocation).
   285  	buf [4]byte
   286  
   287  	// Next step in the decompression,
   288  	// and decompression state.
   289  	step     func(*decompressor)
   290  	final    bool
   291  	err      error
   292  	toRead   []byte
   293  	hl, hd   *huffmanDecoder
   294  	copyLen  int
   295  	copyDist int
   296  }
   297  
   298  func (f *decompressor) nextBlock() {
   299  	if f.final {
   300  		if f.hw != f.hp {
   301  			f.flush((*decompressor).nextBlock)
   302  			return
   303  		}
   304  		f.err = io.EOF
   305  		return
   306  	}
   307  	for f.nb < 1+2 {
   308  		if f.err = f.moreBits(); f.err != nil {
   309  			return
   310  		}
   311  	}
   312  	f.final = f.b&1 == 1
   313  	f.b >>= 1
   314  	typ := f.b & 3
   315  	f.b >>= 2
   316  	f.nb -= 1 + 2
   317  	switch typ {
   318  	case 0:
   319  		f.dataBlock()
   320  	case 1:
   321  		// compressed, fixed Huffman tables
   322  		f.hl = &fixedHuffmanDecoder
   323  		f.hd = nil
   324  		f.huffmanBlock()
   325  	case 2:
   326  		// compressed, dynamic Huffman tables
   327  		if f.err = f.readHuffman(); f.err != nil {
   328  			break
   329  		}
   330  		f.hl = &f.h1
   331  		f.hd = &f.h2
   332  		f.huffmanBlock()
   333  	default:
   334  		// 3 is reserved.
   335  		f.err = CorruptInputError(f.roffset)
   336  	}
   337  }
   338  
   339  func (f *decompressor) Read(b []byte) (int, error) {
   340  	for {
   341  		if len(f.toRead) > 0 {
   342  			n := copy(b, f.toRead)
   343  			f.toRead = f.toRead[n:]
   344  			return n, nil
   345  		}
   346  		if f.err != nil {
   347  			return 0, f.err
   348  		}
   349  		f.step(f)
   350  	}
   351  }
   352  
   353  // Support the io.WriteTo interface for io.Copy and friends.
   354  func (f *decompressor) WriteTo(w io.Writer) (int64, error) {
   355  	total := int64(0)
   356  	for {
   357  		if f.err != nil {
   358  			if f.err == io.EOF {
   359  				return total, nil
   360  			}
   361  			return total, f.err
   362  		}
   363  		if len(f.toRead) > 0 {
   364  			var n int
   365  			n, f.err = w.Write(f.toRead)
   366  			if f.err != nil {
   367  				return total, f.err
   368  			}
   369  			if n != len(f.toRead) {
   370  				return total, io.ErrShortWrite
   371  			}
   372  			f.toRead = f.toRead[:0]
   373  			total += int64(n)
   374  		}
   375  		f.step(f)
   376  	}
   377  }
   378  
   379  func (f *decompressor) Close() error {
   380  	if f.err == io.EOF {
   381  		return nil
   382  	}
   383  	return f.err
   384  }
   385  
   386  // RFC 1951 section 3.2.7.
   387  // Compression with dynamic Huffman codes
   388  
   389  var codeOrder = [...]int{16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}
   390  
   391  func (f *decompressor) readHuffman() error {
   392  	// HLIT[5], HDIST[5], HCLEN[4].
   393  	for f.nb < 5+5+4 {
   394  		if err := f.moreBits(); err != nil {
   395  			return err
   396  		}
   397  	}
   398  	nlit := int(f.b&0x1F) + 257
   399  	if nlit > maxNumLit {
   400  		return CorruptInputError(f.roffset)
   401  	}
   402  	f.b >>= 5
   403  	ndist := int(f.b&0x1F) + 1
   404  	if ndist > maxNumDist {
   405  		return CorruptInputError(f.roffset)
   406  	}
   407  	f.b >>= 5
   408  	nclen := int(f.b&0xF) + 4
   409  	// numCodes is 19, so nclen is always valid.
   410  	f.b >>= 4
   411  	f.nb -= 5 + 5 + 4
   412  
   413  	// (HCLEN+4)*3 bits: code lengths in the magic codeOrder order.
   414  	for i := 0; i < nclen; i++ {
   415  		for f.nb < 3 {
   416  			if err := f.moreBits(); err != nil {
   417  				return err
   418  			}
   419  		}
   420  		f.codebits[codeOrder[i]] = int(f.b & 0x7)
   421  		f.b >>= 3
   422  		f.nb -= 3
   423  	}
   424  	for i := nclen; i < len(codeOrder); i++ {
   425  		f.codebits[codeOrder[i]] = 0
   426  	}
   427  	if !f.h1.init(f.codebits[0:]) {
   428  		return CorruptInputError(f.roffset)
   429  	}
   430  
   431  	// HLIT + 257 code lengths, HDIST + 1 code lengths,
   432  	// using the code length Huffman code.
   433  	for i, n := 0, nlit+ndist; i < n; {
   434  		x, err := f.huffSym(&f.h1)
   435  		if err != nil {
   436  			return err
   437  		}
   438  		if x < 16 {
   439  			// Actual length.
   440  			f.bits[i] = x
   441  			i++
   442  			continue
   443  		}
   444  		// Repeat previous length or zero.
   445  		var rep int
   446  		var nb uint
   447  		var b int
   448  		switch x {
   449  		default:
   450  			return InternalError("unexpected length code")
   451  		case 16:
   452  			rep = 3
   453  			nb = 2
   454  			if i == 0 {
   455  				return CorruptInputError(f.roffset)
   456  			}
   457  			b = f.bits[i-1]
   458  		case 17:
   459  			rep = 3
   460  			nb = 3
   461  			b = 0
   462  		case 18:
   463  			rep = 11
   464  			nb = 7
   465  			b = 0
   466  		}
   467  		for f.nb < nb {
   468  			if err := f.moreBits(); err != nil {
   469  				return err
   470  			}
   471  		}
   472  		rep += int(f.b & uint32(1<<nb-1))
   473  		f.b >>= nb
   474  		f.nb -= nb
   475  		if i+rep > n {
   476  			return CorruptInputError(f.roffset)
   477  		}
   478  		for j := 0; j < rep; j++ {
   479  			f.bits[i] = b
   480  			i++
   481  		}
   482  	}
   483  
   484  	if !f.h1.init(f.bits[0:nlit]) || !f.h2.init(f.bits[nlit:nlit+ndist]) {
   485  		return CorruptInputError(f.roffset)
   486  	}
   487  
   488  	// In order to preserve the property that we never read any extra bytes
   489  	// after the end of the DEFLATE stream, huffSym conservatively reads min
   490  	// bits at a time until it decodes the symbol. However, since every block
   491  	// must end with an EOB marker, we can use that as the minimum number of
   492  	// bits to read and guarantee we never read past the end of the stream.
   493  	if f.bits[endBlockMarker] > 0 {
   494  		f.h1.min = f.bits[endBlockMarker] // Length of EOB marker
   495  	}
   496  
   497  	return nil
   498  }
   499  
   500  // Decode a single Huffman block from f.
   501  // hl and hd are the Huffman states for the lit/length values
   502  // and the distance values, respectively.  If hd == nil, using the
   503  // fixed distance encoding associated with fixed Huffman blocks.
   504  func (f *decompressor) huffmanBlock() {
   505  	for {
   506  		v, err := f.huffSym(f.hl)
   507  		if err != nil {
   508  			f.err = err
   509  			return
   510  		}
   511  		var n uint // number of bits extra
   512  		var length int
   513  		switch {
   514  		case v < 256:
   515  			f.hist[f.hp] = byte(v)
   516  			f.hp++
   517  			if f.hp == len(f.hist) {
   518  				// After the flush, continue this loop.
   519  				f.flush((*decompressor).huffmanBlock)
   520  				return
   521  			}
   522  			continue
   523  		case v == 256:
   524  			// Done with huffman block; read next block.
   525  			f.step = (*decompressor).nextBlock
   526  			return
   527  		// otherwise, reference to older data
   528  		case v < 265:
   529  			length = v - (257 - 3)
   530  			n = 0
   531  		case v < 269:
   532  			length = v*2 - (265*2 - 11)
   533  			n = 1
   534  		case v < 273:
   535  			length = v*4 - (269*4 - 19)
   536  			n = 2
   537  		case v < 277:
   538  			length = v*8 - (273*8 - 35)
   539  			n = 3
   540  		case v < 281:
   541  			length = v*16 - (277*16 - 67)
   542  			n = 4
   543  		case v < 285:
   544  			length = v*32 - (281*32 - 131)
   545  			n = 5
   546  		case v < maxNumLit:
   547  			length = 258
   548  			n = 0
   549  		default:
   550  			f.err = CorruptInputError(f.roffset)
   551  			return
   552  		}
   553  		if n > 0 {
   554  			for f.nb < n {
   555  				if err = f.moreBits(); err != nil {
   556  					f.err = err
   557  					return
   558  				}
   559  			}
   560  			length += int(f.b & uint32(1<<n-1))
   561  			f.b >>= n
   562  			f.nb -= n
   563  		}
   564  
   565  		var dist int
   566  		if f.hd == nil {
   567  			for f.nb < 5 {
   568  				if err = f.moreBits(); err != nil {
   569  					f.err = err
   570  					return
   571  				}
   572  			}
   573  			dist = int(reverseByte[(f.b&0x1F)<<3])
   574  			f.b >>= 5
   575  			f.nb -= 5
   576  		} else {
   577  			if dist, err = f.huffSym(f.hd); err != nil {
   578  				f.err = err
   579  				return
   580  			}
   581  		}
   582  
   583  		switch {
   584  		case dist < 4:
   585  			dist++
   586  		case dist < maxNumDist:
   587  			nb := uint(dist-2) >> 1
   588  			// have 1 bit in bottom of dist, need nb more.
   589  			extra := (dist & 1) << nb
   590  			for f.nb < nb {
   591  				if err = f.moreBits(); err != nil {
   592  					f.err = err
   593  					return
   594  				}
   595  			}
   596  			extra |= int(f.b & uint32(1<<nb-1))
   597  			f.b >>= nb
   598  			f.nb -= nb
   599  			dist = 1<<(nb+1) + 1 + extra
   600  		default:
   601  			f.err = CorruptInputError(f.roffset)
   602  			return
   603  		}
   604  
   605  		// Copy history[-dist:-dist+length] into output.
   606  		if dist > len(f.hist) {
   607  			f.err = InternalError("bad history distance")
   608  			return
   609  		}
   610  
   611  		// No check on length; encoding can be prescient.
   612  		if !f.hfull && dist > f.hp {
   613  			f.err = CorruptInputError(f.roffset)
   614  			return
   615  		}
   616  
   617  		f.copyLen, f.copyDist = length, dist
   618  		if f.copyHist() {
   619  			return
   620  		}
   621  	}
   622  }
   623  
   624  // copyHist copies f.copyLen bytes from f.hist (f.copyDist bytes ago) to itself.
   625  // It reports whether the f.hist buffer is full.
   626  func (f *decompressor) copyHist() bool {
   627  	p := f.hp - f.copyDist
   628  	if p < 0 {
   629  		p += len(f.hist)
   630  	}
   631  	for f.copyLen > 0 {
   632  		n := f.copyLen
   633  		if x := len(f.hist) - f.hp; n > x {
   634  			n = x
   635  		}
   636  		if x := len(f.hist) - p; n > x {
   637  			n = x
   638  		}
   639  		forwardCopy(f.hist[:], f.hp, p, n)
   640  		p += n
   641  		f.hp += n
   642  		f.copyLen -= n
   643  		if f.hp == len(f.hist) {
   644  			// After flush continue copying out of history.
   645  			f.flush((*decompressor).copyHuff)
   646  			return true
   647  		}
   648  		if p == len(f.hist) {
   649  			p = 0
   650  		}
   651  	}
   652  	return false
   653  }
   654  
   655  func (f *decompressor) copyHuff() {
   656  	if f.copyHist() {
   657  		return
   658  	}
   659  	f.huffmanBlock()
   660  }
   661  
   662  // Copy a single uncompressed data block from input to output.
   663  func (f *decompressor) dataBlock() {
   664  	// Uncompressed.
   665  	// Discard current half-byte.
   666  	f.nb = 0
   667  	f.b = 0
   668  
   669  	// Length then ones-complement of length.
   670  	nr, err := io.ReadFull(f.r, f.buf[0:4])
   671  	f.roffset += int64(nr)
   672  	if err != nil {
   673  		f.err = &ReadError{f.roffset, err}
   674  		return
   675  	}
   676  	n := int(f.buf[0]) | int(f.buf[1])<<8
   677  	nn := int(f.buf[2]) | int(f.buf[3])<<8
   678  	if uint16(nn) != uint16(^n) {
   679  		f.err = CorruptInputError(f.roffset)
   680  		return
   681  	}
   682  
   683  	if n == 0 {
   684  		// 0-length block means sync
   685  		f.flush((*decompressor).nextBlock)
   686  		return
   687  	}
   688  
   689  	f.copyLen = n
   690  	f.copyData()
   691  }
   692  
   693  // copyData copies f.copyLen bytes from the underlying reader into f.hist.
   694  // It pauses for reads when f.hist is full.
   695  func (f *decompressor) copyData() {
   696  	n := f.copyLen
   697  	for n > 0 {
   698  		m := len(f.hist) - f.hp
   699  		if m > n {
   700  			m = n
   701  		}
   702  		m, err := io.ReadFull(f.r, f.hist[f.hp:f.hp+m])
   703  		f.roffset += int64(m)
   704  		if err != nil {
   705  			f.err = &ReadError{f.roffset, err}
   706  			return
   707  		}
   708  		n -= m
   709  		f.hp += m
   710  		if f.hp == len(f.hist) {
   711  			f.copyLen = n
   712  			f.flush((*decompressor).copyData)
   713  			return
   714  		}
   715  	}
   716  	f.step = (*decompressor).nextBlock
   717  }
   718  
   719  func (f *decompressor) setDict(dict []byte) {
   720  	if len(dict) > len(f.hist) {
   721  		// Will only remember the tail.
   722  		dict = dict[len(dict)-len(f.hist):]
   723  	}
   724  
   725  	f.hp = copy(f.hist[:], dict)
   726  	if f.hp == len(f.hist) {
   727  		f.hp = 0
   728  		f.hfull = true
   729  	}
   730  	f.hw = f.hp
   731  }
   732  
   733  func (f *decompressor) moreBits() error {
   734  	c, err := f.r.ReadByte()
   735  	if err != nil {
   736  		if err == io.EOF {
   737  			err = io.ErrUnexpectedEOF
   738  		}
   739  		return err
   740  	}
   741  	f.roffset++
   742  	f.b |= uint32(c) << f.nb
   743  	f.nb += 8
   744  	return nil
   745  }
   746  
   747  // Read the next Huffman-encoded symbol from f according to h.
   748  func (f *decompressor) huffSym(h *huffmanDecoder) (int, error) {
   749  	// Since a huffmanDecoder can be empty or be composed of a degenerate tree
   750  	// with single element, huffSym must error on these two edge cases. In both
   751  	// cases, the chunks slice will be 0 for the invalid sequence, leading it
   752  	// satisfy the n == 0 check below.
   753  	n := uint(h.min)
   754  	for {
   755  		for f.nb < n {
   756  			if err := f.moreBits(); err != nil {
   757  				return 0, err
   758  			}
   759  		}
   760  		chunk := h.chunks[f.b&(huffmanNumChunks-1)]
   761  		n = uint(chunk & huffmanCountMask)
   762  		if n > huffmanChunkBits {
   763  			chunk = h.links[chunk>>huffmanValueShift][(f.b>>huffmanChunkBits)&h.linkMask]
   764  			n = uint(chunk & huffmanCountMask)
   765  		}
   766  		if n <= f.nb {
   767  			if n == 0 {
   768  				f.err = CorruptInputError(f.roffset)
   769  				return 0, f.err
   770  			}
   771  			f.b >>= n
   772  			f.nb -= n
   773  			return int(chunk >> huffmanValueShift), nil
   774  		}
   775  	}
   776  }
   777  
   778  // Flush any buffered output to the underlying writer.
   779  func (f *decompressor) flush(step func(*decompressor)) {
   780  	f.toRead = f.hist[f.hw:f.hp]
   781  	f.woffset += int64(f.hp - f.hw)
   782  	f.hw = f.hp
   783  	if f.hp == len(f.hist) {
   784  		f.hp = 0
   785  		f.hw = 0
   786  		f.hfull = true
   787  	}
   788  	f.step = step
   789  }
   790  
   791  func makeReader(r io.Reader) Reader {
   792  	if rr, ok := r.(Reader); ok {
   793  		return rr
   794  	}
   795  	return bufio.NewReader(r)
   796  }
   797  
   798  func (f *decompressor) Reset(r io.Reader, dict []byte) error {
   799  	*f = decompressor{
   800  		r:        makeReader(r),
   801  		bits:     f.bits,
   802  		codebits: f.codebits,
   803  		hist:     f.hist,
   804  		step:     (*decompressor).nextBlock,
   805  	}
   806  	if dict != nil {
   807  		f.setDict(dict)
   808  	}
   809  	return nil
   810  }
   811  
   812  // NewReader returns a new ReadCloser that can be used
   813  // to read the uncompressed version of r.
   814  // If r does not also implement io.ByteReader,
   815  // the decompressor may read more data than necessary from r.
   816  // It is the caller's responsibility to call Close on the ReadCloser
   817  // when finished reading.
   818  //
   819  // The ReadCloser returned by NewReader also implements Resetter.
   820  func NewReader(r io.Reader) io.ReadCloser {
   821  	var f decompressor
   822  	f.bits = new([maxNumLit + maxNumDist]int)
   823  	f.codebits = new([numCodes]int)
   824  	f.r = makeReader(r)
   825  	f.hist = new([maxHist]byte)
   826  	f.step = (*decompressor).nextBlock
   827  	return &f
   828  }
   829  
   830  // NewReaderDict is like NewReader but initializes the reader
   831  // with a preset dictionary.  The returned Reader behaves as if
   832  // the uncompressed data stream started with the given dictionary,
   833  // which has already been read.  NewReaderDict is typically used
   834  // to read data compressed by NewWriterDict.
   835  //
   836  // The ReadCloser returned by NewReader also implements Resetter.
   837  func NewReaderDict(r io.Reader, dict []byte) io.ReadCloser {
   838  	var f decompressor
   839  	f.r = makeReader(r)
   840  	f.hist = new([maxHist]byte)
   841  	f.bits = new([maxNumLit + maxNumDist]int)
   842  	f.codebits = new([numCodes]int)
   843  	f.step = (*decompressor).nextBlock
   844  	f.setDict(dict)
   845  	return &f
   846  }