// 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 zlib implements reading and writing of zlib format compressed data, as specified in RFC 1950. The implementation provides filters that uncompress during reading and compress during writing. For example, to write compressed data to a buffer: var b bytes.Buffer w := zlib.NewWriter(&b) w.Write([]byte("hello, world\n")) w.Close() and to read that data back: r, err := zlib.NewReader(&b) io.Copy(os.Stdout, r) r.Close() */
package zlib import ( ) const zlibDeflate = 8 var ( // ErrChecksum is returned when reading ZLIB data that has an invalid checksum. ErrChecksum = errors.New("zlib: invalid checksum") // ErrDictionary is returned when reading ZLIB data that has an invalid dictionary. ErrDictionary = errors.New("zlib: invalid dictionary") // ErrHeader is returned when reading ZLIB data that has an invalid header. ErrHeader = errors.New("zlib: invalid header") ) type reader struct { r flate.Reader decompressor io.ReadCloser digest hash.Hash32 err error scratch [4]byte } // Resetter resets a ReadCloser returned by NewReader or NewReaderDict // to switch to a new underlying Reader. This permits reusing a ReadCloser // instead of allocating a new one. type Resetter interface { // Reset discards any buffered data and resets the Resetter as if it was // newly initialized with the given reader. Reset(r io.Reader, dict []byte) error } // NewReader creates a new ReadCloser. // Reads from the returned ReadCloser read and decompress data from r. // If r does not implement io.ByteReader, the decompressor may read more // data than necessary from r. // It is the caller's responsibility to call Close on the ReadCloser when done. // // The ReadCloser returned by NewReader also implements Resetter. func ( io.Reader) (io.ReadCloser, error) { return NewReaderDict(, nil) } // NewReaderDict is like NewReader but uses a preset dictionary. // NewReaderDict ignores the dictionary if the compressed data does not refer to it. // If the compressed data refers to a different dictionary, NewReaderDict returns ErrDictionary. // // The ReadCloser returned by NewReaderDict also implements Resetter. func ( io.Reader, []byte) (io.ReadCloser, error) { := new(reader) := .Reset(, ) if != nil { return nil, } return , nil } func ( *reader) ( []byte) (int, error) { if .err != nil { return 0, .err } var int , .err = .decompressor.Read() .digest.Write([0:]) if .err != io.EOF { // In the normal case we return here. return , .err } // Finished file; check checksum. if , := io.ReadFull(.r, .scratch[0:4]); != nil { if == io.EOF { = io.ErrUnexpectedEOF } .err = return , .err } // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952). := uint32(.scratch[0])<<24 | uint32(.scratch[1])<<16 | uint32(.scratch[2])<<8 | uint32(.scratch[3]) if != .digest.Sum32() { .err = ErrChecksum return , .err } return , io.EOF } // Calling Close does not close the wrapped io.Reader originally passed to NewReader. // In order for the ZLIB checksum to be verified, the reader must be // fully consumed until the io.EOF. func ( *reader) () error { if .err != nil && .err != io.EOF { return .err } .err = .decompressor.Close() return .err } func ( *reader) ( io.Reader, []byte) error { * = reader{decompressor: .decompressor} if , := .(flate.Reader); { .r = } else { .r = bufio.NewReader() } // Read the header (RFC 1950 section 2.2.). _, .err = io.ReadFull(.r, .scratch[0:2]) if .err != nil { if .err == io.EOF { .err = io.ErrUnexpectedEOF } return .err } := uint(.scratch[0])<<8 | uint(.scratch[1]) if (.scratch[0]&0x0f != zlibDeflate) || (%31 != 0) { .err = ErrHeader return .err } := .scratch[1]&0x20 != 0 if { _, .err = io.ReadFull(.r, .scratch[0:4]) if .err != nil { if .err == io.EOF { .err = io.ErrUnexpectedEOF } return .err } := uint32(.scratch[0])<<24 | uint32(.scratch[1])<<16 | uint32(.scratch[2])<<8 | uint32(.scratch[3]) if != adler32.Checksum() { .err = ErrDictionary return .err } } if .decompressor == nil { if { .decompressor = flate.NewReaderDict(.r, ) } else { .decompressor = flate.NewReader(.r) } } else { .decompressor.(flate.Resetter).Reset(.r, ) } .digest = adler32.New() return nil }