// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package flate

import (
	
	
	
	
	
)

const (
	NoCompression      = 0
	BestSpeed          = 1
	BestCompression    = 9
	DefaultCompression = -1

	// HuffmanOnly disables Lempel-Ziv match searching and only performs Huffman
	// entropy encoding. This mode is useful in compressing data that has
	// already been compressed with an LZ style algorithm (e.g. Snappy or LZ4)
	// that lacks an entropy encoder. Compression gains are achieved when
	// certain bytes in the input stream occur more frequently than others.
	//
	// Note that HuffmanOnly produces a compressed output that is
	// RFC 1951 compliant. That is, any valid DEFLATE decompressor will
	// continue to be able to decompress this output.
	HuffmanOnly = -2
)

const (
	logWindowSize  = 15
	windowSize     = 1 << logWindowSize
	windowMask     = windowSize - 1
	minMatchLength = 4   // The smallest match that the compressor looks for
	maxMatchLength = 258 // The longest match for the compressor
	minOffsetSize  = 1   // The shortest offset that makes any sense

	// The maximum number of tokens we will encode at the time.
	// Smaller sizes usually creates less optimal blocks.
	// Bigger can make context switching slow.
	// We use this for levels 7-9, so we make it big.
	maxFlateBlockTokens = 1 << 15
	maxStoreBlockSize   = 65535
	hashBits            = 17 // After 17 performance degrades
	hashSize            = 1 << hashBits
	hashMask            = (1 << hashBits) - 1
	maxHashOffset       = 1 << 28

	skipNever = math.MaxInt32
)

// compressionLevel holds the parameters for levels 7-9.
type compressionLevel struct {
	good  int32 // "good enough" match length
	lazy  int32 // don't try to find a later, better match above this length
	nice  int32 // stop looking for a better match above this length
	chain int32 // maximum number of hash chain entries to search
	level int
}

var levels = []compressionLevel{
	{}, // 0
	// Level 1-6 uses specialized algorithm - values not used
	{0, 0, 0, 0, 1},
	{0, 0, 0, 0, 2},
	{0, 0, 0, 0, 3},
	{0, 0, 0, 0, 4},
	{0, 0, 0, 0, 5},
	{0, 0, 0, 0, 6},
	// Levels 7-9 use increasingly more lazy matching
	// and increasingly stringent conditions for "good enough".
	{8, 12, 16, 24, 7},
	{16, 30, 40, 64, 8},
	{32, 258, 258, 1024, 9},
}

// advancedState contains state for levels 7-9, with bigger hash tables, etc.
type advancedState struct {
	// deflate state
	length         int32
	offset         int32
	maxInsertIndex int32
	chainHead      int32
	hashOffset     int32

	literalCounter uint16 // consecutive literal count; overflows to reset after 64KB.

	// input window: unprocessed data is window[index:windowEnd]
	index     int32
	hashMatch [maxMatchLength + minMatchLength]uint32

	// Input hash chains
	// hashHead[hashValue] contains the largest inputIndex with the specified hash value
	// If hashHead[hashValue] is within the current window, then
	// hashPrev[hashHead[hashValue] & windowMask] contains the previous index
	// with the same hash value.
	hashHead [hashSize]int32
	hashPrev [windowSize]int32
}

type compressor struct {
	compressionLevel

	h *huffmanEncoder   // huffman encoder, with state
	w *huffmanBitWriter // writer for blocks

	// compression algorithm
	fill func(*compressor, []byte) int // copy data to window
	step func(*compressor)             // process window

	window     []byte // current window - size depends on encoder level
	windowEnd  int32  // filled bytes in window
	blockStart int32  // window index where current tokens start
	err        error  // stateful error

	// queued output tokens
	tokens tokens         // tokens store for each block
	fast   fastEnc        // encoder to use for blocks
	state  *advancedState // chained encoder for level 7-9

	sync          bool // requesting flush
	byteAvailable bool // if true, still need to process window[index-1].
}

// fillDeflate will add b to the current window for levels 7-9.
func ( *compressor) ( []byte) int {
	 := .state
	if .index >= 2*windowSize-(minMatchLength+maxMatchLength) {
		// shift the window by windowSize
		copy(.window[:], .window[windowSize:2*windowSize])
		.index -= windowSize
		.windowEnd -= windowSize
		if .blockStart >= windowSize {
			.blockStart -= windowSize
		} else {
			.blockStart = math.MaxInt32
		}
		.hashOffset += windowSize
		if .hashOffset > maxHashOffset {
			 := .hashOffset - 1
			.hashOffset -= 
			.chainHead -= 
			// Note: range over &array to avoid copy (see go.dev/issue/18625).
			for ,  := range &.hashPrev {
				.hashPrev[] = max(-, 0)
			}
			for ,  := range &.hashHead {
				.hashHead[] = max(-, 0)
			}
		}
	}
	 := copy(.window[.windowEnd:], )
	.windowEnd += int32()
	return 
}

// writeBlock will write tokens to output.
// The provided index is where the block starts in d.window.
func ( *compressor) ( *tokens,  int32,  bool) error {
	if  > 0 ||  {
		var  []byte
		if .blockStart <=  {
			 = .window[.blockStart:]
		}
		.blockStart = 
		.w.writeBlockDynamic(, , , .sync)
		return .w.err
	}
	return nil
}

// writeBlockSkip writes the current block and uses the number of tokens
// to determine if the block should be stored when there are no matches, or
// only Huffman encoded.
func ( *compressor) ( *tokens,  int32,  bool) error {
	if  > 0 ||  {
		if .blockStart <=  {
			 := .window[.blockStart:]
			// If we removed less than a 64th of all literals
			// we huffman compress the block.
			if int(.n) > len()-(len()>>6) {
				.w.writeBlockHuff(, , .sync)
			} else {
				// Write a dynamic huffman block.
				.w.writeBlockDynamic(, , , .sync)
			}
		} else {
			.w.writeBlock(, , nil)
		}
		.blockStart = 
		return .w.err
	}
	return nil
}

// fillWindow will fill the current window with the supplied
// dictionary and calculate all hashes.
// This is much faster than doing a full encode.
// Should only be used after a start/reset.
func ( *compressor) ( []byte) {
	// Do not fill window if we are in store-only or huffman mode.
	if .level <= 0 {
		return
	}
	if .fast != nil {
		// encode the last data, but discard the result
		if len() > maxMatchOffset {
			 = [len()-maxMatchOffset:]
		}
		.fast.encode(&.tokens, )
		.tokens.Reset()
		return
	}
	 := .state
	// If we are given too much, cut it.
	if len() > windowSize {
		 = [len()-windowSize:]
	}
	// Add all to window.
	 := int32(copy(.window[.windowEnd:], ))

	// Calculate 256 hashes at the time (more L1 cache hits)
	 := ( + 256 - minMatchLength) / 256
	for  := range  {
		 :=  * 256
		 := min(+256+minMatchLength-1, )
		 := .window[:]
		 := len() - minMatchLength + 1

		if  <= 0 {
			continue
		}

		 := .hashMatch[:]
		bulkHash4(, )
		var  uint32
		for ,  := range  {
			 := int32() + 
			 =  & hashMask
			// Get previous value with the same hash.
			// Our chain should point to the previous value.
			.hashPrev[&windowMask] = .hashHead[]
			// Set the head of the hash chain to us.
			.hashHead[] =  + .hashOffset
		}
	}
	// Update window information.
	.windowEnd += 
	.index = 
	.blockStart = .windowEnd
}

// findMatch finds the longest match starting at pos in the hash chain starting
// at prevHead. It searches up to d.chain entries in the chain.
func ( *compressor) ( int32,  int32,  int32) (,  int32,  bool) {
	 := min(, maxMatchLength)

	 := .window[0 : +]

	// We quit when we get a match that's at least nice long
	 := min(.nice, int32(len())-)

	// If we've got a match that's good enough, only look in 1/4 the chain.
	 := .chain
	 = minMatchLength - 1

	 := [+]
	 := [:]
	 := max(-windowSize, 0)
	 = 0

	// Minimum gain to accept a match.
	 := 4

	// Some like it higher (CSV), some like it lower (JSON)
	const  = 3
	// Base is 4 bytes at with an additional cost.
	// Matches must be better than this.

	for  := ;  > 0; -- {
		if  == [+] {
			 := int32(matchLen([:+], ))
			if  >  {
				if .chain >= 100 {
					// Calculate gain. Estimates the gains of the new match compared to emitting as literals.
					 := .h.bitLengthRaw([:]) - int(offsetExtraBits[offsetCode(uint32(-))]) -  - int(lengthExtraBits[lengthCodes[(-3)&255]])
					if  <=  {
						goto 
					}
					 = 
				}
				 = 
				 =  - 
				 = true
				if  >=  {
					// The match is good enough that we don't try to find a better one.
					break
				}
				 = [+]
			}
		}
	:
		if  <=  {
			// hashPrev[i & windowMask] has already been overwritten, so stop now.
			break
		}
		 = .state.hashPrev[&windowMask] - .state.hashOffset
		if  <  {
			break
		}
	}
	return
}

// writeStoredBlock writes an uncompressed block to the stream.
func ( *compressor) ( []byte) error {
	if .w.writeStoredHeader(len(), false); .w.err != nil {
		return .w.err
	}
	.w.writeBytes()
	return .w.err
}

// hash4 returns a hash representation of the first 4 bytes
// of the supplied slice.
// The caller must ensure that len(b) >= 4.
func ( []byte) uint32 {
	return hash4u(loadLE32(, 0), hashBits)
}

// hash4 returns the hash of u to fit in a hash table with h bits.
// Preferably h should be a constant and should always be <32.
func ( uint32,  uint8) uint32 {
	return ( * prime4bytes) >> (32 - )
}

// bulkHash4 sets dst[i] = hash4(b[i:i+4]) for all i <= len(b)-4.
func ( []byte,  []uint32) {
	if len() < 4 {
		return
	}
	 := loadLE32(, 0)

	[0] = hash4u(, hashBits)
	 := len() - 4 + 1
	for  := 1;  < ; ++ {
		 = ( >> 8) | uint32([+3])<<24
		[] = hash4u(, hashBits)
	}
}

// initDeflate initializes d for levels 7-9.
func ( *compressor) () {
	.window = make([]byte, 2*windowSize)
	.byteAvailable = false
	.err = nil
	if .state == nil {
		return
	}
	 := .state
	.index = 0
	.hashOffset = 1
	.length = minMatchLength - 1
	.offset = 0
	.chainHead = -1
}

// tryBetterMatchAtEnd checks whether a better match exists at the end of the
// previous match and, if so, emits the skipped literals and adjusts the match.
// Returns the (possibly updated) prevLength and prevOffset.
func ( *compressor) (, ,  int32) (,  int32) {
	// We start checking at checkOff from the current match position.
	// This allows up to two additional literals, but that could be
	// compensated by a higher quality match.
	// If the match looks better, we extend backwards.
	const  = 2
	 := .state

	if  >= maxMatchLength- {
		return , 
	}
	 := .index - 1
	if + >= .maxInsertIndex {
		return , 
	}

	 := min(, maxMatchLength+) + 
	 := max(.index-windowSize, 0)

	 := hash4(.window[+:])
	 := .hashHead[] - .hashOffset - 
	if - ==  ||  <= + {
		return , 
	}

	 := int32(matchLen(.window[+:], .window[+:]))
	if  <=  {
		return , 
	}

	 = 
	 =  - 

	for  := int32( - 1);  >= 0; -- {
		if  >= maxMatchLength || .window[+] != .window[+] {
			for  := range  + 1 {
				.tokens.AddLiteral(.window[+])
				if .tokens.n == maxFlateBlockTokens {
					if .err = .writeBlock(&.tokens, .index, false); .err != nil {
						return , 
					}
					.tokens.Reset()
				}
				.index++
				if .index < .maxInsertIndex {
					 := hash4(.window[.index:])
					 := .hashHead[]
					.chainHead = 
					.hashPrev[.index&windowMask] = 
					.hashHead[] = .index + .hashOffset
				}
			}
			break
		}
		++
	}
	return , 
}

// skipLiterals emits extra literal bytes during long runs of incompressible data,
// skipping ahead to avoid futile match searches. Returns false on write error.
func ( *compressor) () bool {
	 := .state
	 := int32(.literalCounter) - .chain
	if  <= 0 {
		return true
	}
	 = 1 + >>6
	for range  {
		if .index >= .windowEnd-1 {
			break
		}
		.tokens.AddLiteral(.window[.index-1])
		if .tokens.n == maxFlateBlockTokens {
			if .err = .writeBlock(&.tokens, .index, false); .err != nil {
				return false
			}
			.tokens.Reset()
		}
		if .index < .maxInsertIndex {
			 := hash4(.window[.index:])
			 := .hashHead[]
			.chainHead = 
			.hashPrev[.index&windowMask] = 
			.hashHead[] = .index + .hashOffset
		}
		.index++
	}
	.tokens.AddLiteral(.window[.index-1])
	.byteAvailable = false
	if .tokens.n == maxFlateBlockTokens {
		if .err = .writeBlock(&.tokens, .index, false); .err != nil {
			return false
		}
		.tokens.Reset()
	}
	return true
}

// deflateLazy encodes the current window using lazy matching.
// Lazy matching defers emitting a match to see if the next position yields a better one.
// Unique to levels 7-9 is that more than 2 matches are potentially checked
// until a good/nice one is found.
func ( *compressor) () {
	 := .state

	if .windowEnd-.index < minMatchLength+maxMatchLength && !.sync {
		return
	}
	if .windowEnd != .index && .chain > 100 {
		// Get literal huffman coder.
		// This is used to estimate the cost of emitting a literal.
		if .h == nil {
			.h = newHuffmanEncoder(maxFlateBlockTokens)
		}
		var  [256]uint16
		 := .window[.index:.windowEnd]
		 = [:min(len(), maxFlateBlockTokens)]
		for ,  := range  {
			[]++
		}
		.h.generate([:], 15)
	}

	.maxInsertIndex = .windowEnd - (minMatchLength - 1)

	for {
		 := .windowEnd - .index
		if  < minMatchLength+maxMatchLength {
			if !.sync {
				return
			}
			if  == 0 {
				// Flush current output block if any.
				if .byteAvailable {
					// There is still one pending token that needs to be flushed
					.tokens.AddLiteral(.window[.index-1])
					.byteAvailable = false
				}
				if .tokens.n > 0 {
					if .err = .writeBlock(&.tokens, .index, false); .err != nil {
						return
					}
					.tokens.Reset()
				}
				return
			}
		}
		if .index < .maxInsertIndex {
			 := hash4(.window[.index:])
			 := .hashHead[]
			.chainHead = 
			.hashPrev[.index&windowMask] = 
			.hashHead[] = .index + .hashOffset
		}
		 := .length
		 := .offset
		.length = minMatchLength - 1
		.offset = 0
		 := max(.index-windowSize, 0)

		if .chainHead-.hashOffset >=  &&  >  &&  < .lazy {
			if , ,  := .findMatch(.index, .chainHead-.hashOffset, );  {
				.length = 
				.offset = 
			}
		}

		if  >= minMatchLength && .length <=  {
			,  = .tryBetterMatchAtEnd(, , )
			if .err != nil {
				return
			}

			// There was a match at the previous step, and the current match is
			// not better. Output the previous match.
			.tokens.AddMatch(uint32(-3), uint32(-minOffsetSize))

			// Insert in the hash table all strings up to the end of the match.
			// index and index-1 are already inserted. If there is not enough
			// lookahead, the last two strings are not inserted into the hash
			// table.
			 := .index +  - 1
			 := min(, .maxInsertIndex)
			 += minMatchLength - 1
			 := min(.index+1, .maxInsertIndex)
			 := .window[:]
			 := len() - minMatchLength + 1
			if  > 0 {
				 := .hashMatch[:]
				bulkHash4(, )
				var  uint32
				for ,  := range  {
					 := int32() + 
					 =  & hashMask
					.hashPrev[&windowMask] = .hashHead[]
					.hashHead[] =  + .hashOffset
				}
			}

			.index = 
			.byteAvailable = false
			.length = minMatchLength - 1
			if .tokens.n == maxFlateBlockTokens {
				if .err = .writeBlock(&.tokens, .index, false); .err != nil {
					return
				}
				.tokens.Reset()
			}
			.literalCounter = 0
			continue
		}
		if .length >= minMatchLength {
			.literalCounter = 0
		}
		if .byteAvailable {
			.literalCounter++
			.tokens.AddLiteral(.window[.index-1])
			if .tokens.n == maxFlateBlockTokens {
				if .err = .writeBlock(&.tokens, .index, false); .err != nil {
					return
				}
				.tokens.Reset()
			}
			.index++
			if !.skipLiterals() {
				return
			}
		} else {
			.index++
			.byteAvailable = true
		}
	}
}

// store will store the current window if it has filled or if we are in sync.
func ( *compressor) () {
	if .windowEnd > 0 && (.windowEnd == maxStoreBlockSize || .sync) {
		.err = .writeStoredBlock(.window[:.windowEnd])
		.windowEnd = 0
	}
}

// fillBlock appends b to d.window, returning the number of bytes copied.
// If n < len(b), the window is filled.
func ( *compressor) ( []byte) int {
	 := copy(.window[.windowEnd:], )
	.windowEnd += int32()
	return 
}

// deflateHuff compresses and stores the current window
// (if it has filled or if we are in sync or flush).
// It uses Huffman-only encoding.
func ( *compressor) () {
	if int(.windowEnd) < len(.window) && !.sync || .windowEnd == 0 {
		return
	}
	.w.writeBlockHuff(false, .window[:.windowEnd], .sync)
	.err = .w.err
	.windowEnd = 0
}

// deflateFast encodes the current window
// if it has filled or if we are doing sync/flush.
// It uses the level 1-6 fast encoding.
func ( *compressor) () {
	// We only compress if we have maxStoreBlockSize.
	if int(.windowEnd) < len(.window) {
		if !.sync {
			return
		}
		// Handle extremely small sizes.
		if .windowEnd < 128 {
			if .windowEnd == 0 {
				return
			}
			if .windowEnd <= 32 {
				.err = .writeStoredBlock(.window[:.windowEnd])
			} else {
				.w.writeBlockHuff(false, .window[:.windowEnd], true)
				.err = .w.err
			}
			.tokens.Reset()
			.windowEnd = 0
			.fast.reset()
			return
		}
	}

	.fast.encode(&.tokens, .window[:.windowEnd])
	// If we made zero matches, store the block as is.
	if .tokens.n == 0 {
		.err = .writeStoredBlock(.window[:.windowEnd])
		// If we removed less than 1/16th, huffman compress the block.
	} else if int32(.tokens.n) > .windowEnd-(.windowEnd>>4) {
		.w.writeBlockHuff(false, .window[:.windowEnd], .sync)
		.err = .w.err
	} else {
		.w.writeBlockDynamic(&.tokens, false, .window[:.windowEnd], .sync)
		.err = .w.err
	}
	.tokens.Reset()
	.windowEnd = 0
}

// write adds b to the compressor.
// It can only return a short length if an error occurs.
func ( *compressor) ( []byte) ( int,  error) {
	if .err != nil {
		return 0, .err
	}
	 = len()
	for len() > 0 {
		if int(.windowEnd) == len(.window) || .sync {
			.step()
		}
		 = [.fill(, ):]
		if .err != nil {
			return 0, .err
		}
	}
	return , .err
}

// syncFlush will flush the compressor by writing
// any remaining window and writing a stored block
// to byte-align the output.
func ( *compressor) () error {
	if .err != nil {
		return .err
	}
	.sync = true
	.step()
	if .err == nil {
		.w.writeStoredHeader(0, false)
		.w.flush()
		.err = .w.err
	}
	.sync = false
	return .err
}

// init a new encode with new writer and compression level.
func ( *compressor) ( io.Writer,  int) ( error) {
	.w = newHuffmanBitWriter()

	switch {
	case  == NoCompression:
		.window = make([]byte, maxStoreBlockSize)
		.fill = (*compressor).fillBlock
		.step = (*compressor).store
	case  == HuffmanOnly:
		.w.logNewTablePenalty = 10
		.window = make([]byte, 32<<10)
		.fill = (*compressor).fillBlock
		.step = (*compressor).deflateHuff
	case  == DefaultCompression:
		 = 6
		fallthrough
	case 1 <=  &&  <= 6:
		.w.logNewTablePenalty = 7
		.fast = newFastEnc()
		.window = make([]byte, maxStoreBlockSize)
		.fill = (*compressor).fillBlock
		.step = (*compressor).deflateFast
	case 7 <=  &&  <= 9:
		.w.logNewTablePenalty = 8
		.state = &advancedState{}
		.compressionLevel = levels[]
		.initDeflate()
		.fill = (*compressor).fillDeflate
		.step = (*compressor).deflateLazy
	default:
		return fmt.Errorf("flate: invalid compression level %d: want value in range [-2, 9]", )
	}
	.level = 
	return nil
}

// reset resets the compressor with a new output writer.
func ( *compressor) ( io.Writer) {
	.w.reset()
	.sync = false
	.err = nil
	.windowEnd = 0
	// We only need to reset a few things for fast encoders.
	if .fast != nil {
		.fast.reset()
		.tokens.Reset()
		return
	}
	if .compressionLevel.chain == 0 {
		return
	}
	 := .state
	.chainHead = -1
	clear(.hashHead[:])
	clear(.hashPrev[:])
	.hashOffset = 1
	.index = 0
	.blockStart, .byteAvailable = 0, false
	.tokens.Reset()
	.length = minMatchLength - 1
	.offset = 0
	.literalCounter = 0
	.maxInsertIndex = 0
}

var errWriterClosed = errors.New("flate: closed writer")

// close flushes any uncompressed data and writes an EOF block.
func ( *compressor) () error {
	if .err == errWriterClosed {
		return nil
	}
	if .err != nil {
		return .err
	}
	.sync = true
	.step()
	if .err != nil {
		return .err
	}
	if .w.writeStoredHeader(0, true); .w.err != nil {
		return .w.err
	}
	.w.flush()
	if .w.err != nil {
		return .w.err
	}
	.err = errWriterClosed
	.w.reset(nil)
	return nil
}

// NewWriter returns a new [Writer] compressing data at the given level.
// Following zlib, levels range from 1 ([BestSpeed]) to 9 ([BestCompression]);
// higher levels typically run slower but compress more. Level 0
// ([NoCompression]) does not attempt any compression; it only adds the
// necessary DEFLATE framing.
// Level -1 ([DefaultCompression]) uses the default compression level.
// Level -2 ([HuffmanOnly]) will use Huffman compression only, giving
// a very fast compression for all types of input, but sacrificing considerable
// compression efficiency.
//
// If level is in the range [-2, 9] then the error returned will be nil.
// Otherwise the error returned will be non-nil.
//
// Note that the exact bytes written to w are not covered by the Go 1
// compatibility promise. Callers, including tests, should not depend on the
// exact written bytes.
func ( io.Writer,  int) (*Writer, error) {
	var  Writer
	if  := .d.init(, );  != nil {
		return nil, 
	}
	return &, nil
}

// NewWriterDict is like [NewWriter] but initializes the new
// [Writer] with a preset dictionary. The returned [Writer] behaves
// as if the dictionary had been written to it without producing
// any compressed output. The compressed data written to w
// can only be decompressed by a reader initialized with the
// same dictionary (see [NewReaderDict]).
//
// Note that the exact bytes written to w are not covered by the Go 1
// compatibility promise. Callers, including tests, should not depend on the
// exact written bytes.
func ( io.Writer,  int,  []byte) (*Writer, error) {
	,  := NewWriter(, )
	if  != nil {
		return nil, 
	}
	.d.fillWindow()
	// Clone dict so we can Reset without changing the provided slice.
	.dict = slices.Clone()
	return , 
}

// A Writer takes data written to it and writes the compressed
// form of that data to an underlying writer (see [NewWriter]).
type Writer struct {
	d    compressor
	dict []byte
}

// Write writes data to w, which will eventually write the
// compressed form of data to its underlying writer.
func ( *Writer) ( []byte) ( int,  error) {
	return .d.write()
}

// Flush flushes any pending data to the underlying writer.
// It is useful mainly in compressed network protocols, to ensure that
// a remote reader has enough data to reconstruct a packet.
// Flush does not return until the data has been written.
// Calling Flush when there is no pending data still causes the [Writer]
// to emit a sync marker of at least 4 bytes.
// If the underlying writer returns an error, Flush returns that error.
//
// In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.
func ( *Writer) () error {
	// For more about flushing:
	// https://www.bolet.org/~pornin/deflate-flush.html
	return .d.syncFlush()
}

// Close flushes and closes the writer.
func ( *Writer) () error {
	return .d.close()
}

// Reset discards the writer's state and makes it equivalent to
// the result of NewWriter or NewWriterDict called with dst
// and w's level and dictionary.
func ( *Writer) ( io.Writer) {
	.d.reset()
	.d.fillWindow(.dict)
}