github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/compress/zlib/reader.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  /*
     6  Package zlib implements reading and writing of zlib format compressed data,
     7  as specified in RFC 1950.
     8  
     9  The implementation provides filters that uncompress during reading
    10  and compress during writing.  For example, to write compressed data
    11  to a buffer:
    12  
    13  	var b bytes.Buffer
    14  	w := zlib.NewWriter(&b)
    15  	w.Write([]byte("hello, world\n"))
    16  	w.Close()
    17  
    18  and to read that data back:
    19  
    20  	r, err := zlib.NewReader(&b)
    21  	io.Copy(os.Stdout, r)
    22  	r.Close()
    23  */
    24  package zlib
    25  
    26  import (
    27  	"bufio"
    28  	"compress/flate"
    29  	"errors"
    30  	"hash"
    31  	"hash/adler32"
    32  	"io"
    33  )
    34  
    35  const zlibDeflate = 8
    36  
    37  var (
    38  	// ErrChecksum is returned when reading ZLIB data that has an invalid checksum.
    39  	ErrChecksum = errors.New("zlib: invalid checksum")
    40  	// ErrDictionary is returned when reading ZLIB data that has an invalid dictionary.
    41  	ErrDictionary = errors.New("zlib: invalid dictionary")
    42  	// ErrHeader is returned when reading ZLIB data that has an invalid header.
    43  	ErrHeader = errors.New("zlib: invalid header")
    44  )
    45  
    46  type reader struct {
    47  	r            flate.Reader
    48  	decompressor io.ReadCloser
    49  	digest       hash.Hash32
    50  	err          error
    51  	scratch      [4]byte
    52  }
    53  
    54  // Resetter resets a ReadCloser returned by NewReader or NewReaderDict to
    55  // to switch to a new underlying Reader. This permits reusing a ReadCloser
    56  // instead of allocating a new one.
    57  type Resetter interface {
    58  	// Reset discards any buffered data and resets the Resetter as if it was
    59  	// newly initialized with the given reader.
    60  	Reset(r io.Reader, dict []byte) error
    61  }
    62  
    63  // NewReader creates a new ReadCloser.
    64  // Reads from the returned ReadCloser read and decompress data from r.
    65  // The implementation buffers input and may read more data than necessary from r.
    66  // It is the caller's responsibility to call Close on the ReadCloser when done.
    67  //
    68  // The ReadCloser returned by NewReader also implements Resetter.
    69  func NewReader(r io.Reader) (io.ReadCloser, error) {
    70  	return NewReaderDict(r, nil)
    71  }
    72  
    73  // NewReaderDict is like NewReader but uses a preset dictionary.
    74  // NewReaderDict ignores the dictionary if the compressed data does not refer to it.
    75  // If the compressed data refers to a different dictionary, NewReaderDict returns ErrDictionary.
    76  //
    77  // The ReadCloser returned by NewReaderDict also implements Resetter.
    78  func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error) {
    79  	z := new(reader)
    80  	err := z.Reset(r, dict)
    81  	if err != nil {
    82  		return nil, err
    83  	}
    84  	return z, nil
    85  }
    86  
    87  func (z *reader) Read(p []byte) (n int, err error) {
    88  	if z.err != nil {
    89  		return 0, z.err
    90  	}
    91  	if len(p) == 0 {
    92  		return 0, nil
    93  	}
    94  
    95  	n, err = z.decompressor.Read(p)
    96  	z.digest.Write(p[0:n])
    97  	if n != 0 || err != io.EOF {
    98  		z.err = err
    99  		return
   100  	}
   101  
   102  	// Finished file; check checksum.
   103  	if _, err := io.ReadFull(z.r, z.scratch[0:4]); err != nil {
   104  		if err == io.EOF {
   105  			err = io.ErrUnexpectedEOF
   106  		}
   107  		z.err = err
   108  		return 0, err
   109  	}
   110  	// ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952).
   111  	checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
   112  	if checksum != z.digest.Sum32() {
   113  		z.err = ErrChecksum
   114  		return 0, z.err
   115  	}
   116  	return
   117  }
   118  
   119  // Calling Close does not close the wrapped io.Reader originally passed to NewReader.
   120  func (z *reader) Close() error {
   121  	if z.err != nil {
   122  		return z.err
   123  	}
   124  	z.err = z.decompressor.Close()
   125  	return z.err
   126  }
   127  
   128  func (z *reader) Reset(r io.Reader, dict []byte) error {
   129  	if fr, ok := r.(flate.Reader); ok {
   130  		z.r = fr
   131  	} else {
   132  		z.r = bufio.NewReader(r)
   133  	}
   134  	_, err := io.ReadFull(z.r, z.scratch[0:2])
   135  	if err != nil {
   136  		if err == io.EOF {
   137  			err = io.ErrUnexpectedEOF
   138  		}
   139  		return err
   140  	}
   141  	h := uint(z.scratch[0])<<8 | uint(z.scratch[1])
   142  	if (z.scratch[0]&0x0f != zlibDeflate) || (h%31 != 0) {
   143  		return ErrHeader
   144  	}
   145  	haveDict := z.scratch[1]&0x20 != 0
   146  	if haveDict {
   147  		_, err = io.ReadFull(z.r, z.scratch[0:4])
   148  		if err != nil {
   149  			if err == io.EOF {
   150  				err = io.ErrUnexpectedEOF
   151  			}
   152  			return err
   153  		}
   154  		checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3])
   155  		if checksum != adler32.Checksum(dict) {
   156  			return ErrDictionary
   157  		}
   158  	}
   159  	if z.decompressor == nil {
   160  		if haveDict {
   161  			z.decompressor = flate.NewReaderDict(z.r, dict)
   162  		} else {
   163  			z.decompressor = flate.NewReader(z.r)
   164  		}
   165  	} else {
   166  		z.decompressor.(flate.Resetter).Reset(z.r, dict)
   167  	}
   168  	z.digest = adler32.New()
   169  	return nil
   170  }