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 z.err = err 105 return 0, err 106 } 107 // ZLIB (RFC 1950) is big-endian, unlike GZIP (RFC 1952). 108 checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3]) 109 if checksum != z.digest.Sum32() { 110 z.err = ErrChecksum 111 return 0, z.err 112 } 113 return 114 } 115 116 // Calling Close does not close the wrapped io.Reader originally passed to NewReader. 117 func (z *reader) Close() error { 118 if z.err != nil { 119 return z.err 120 } 121 z.err = z.decompressor.Close() 122 return z.err 123 } 124 125 func (z *reader) Reset(r io.Reader, dict []byte) error { 126 if fr, ok := r.(flate.Reader); ok { 127 z.r = fr 128 } else { 129 z.r = bufio.NewReader(r) 130 } 131 _, err := io.ReadFull(z.r, z.scratch[0:2]) 132 if err != nil { 133 return err 134 } 135 h := uint(z.scratch[0])<<8 | uint(z.scratch[1]) 136 if (z.scratch[0]&0x0f != zlibDeflate) || (h%31 != 0) { 137 return ErrHeader 138 } 139 haveDict := z.scratch[1]&0x20 != 0 140 if haveDict { 141 _, err = io.ReadFull(z.r, z.scratch[0:4]) 142 if err != nil { 143 return err 144 } 145 checksum := uint32(z.scratch[0])<<24 | uint32(z.scratch[1])<<16 | uint32(z.scratch[2])<<8 | uint32(z.scratch[3]) 146 if checksum != adler32.Checksum(dict) { 147 return ErrDictionary 148 } 149 } 150 if z.decompressor == nil { 151 if haveDict { 152 z.decompressor = flate.NewReaderDict(z.r, dict) 153 } else { 154 z.decompressor = flate.NewReader(z.r) 155 } 156 } else { 157 z.decompressor.(flate.Resetter).Reset(z.r, dict) 158 } 159 z.digest = adler32.New() 160 return nil 161 } 162