github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/src/compress/gzip/gunzip.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 // Package gzip implements reading and writing of gzip format compressed files, 6 // as specified in RFC 1952. 7 package gzip 8 9 import ( 10 "bufio" 11 "compress/flate" 12 "errors" 13 "hash" 14 "hash/crc32" 15 "io" 16 "time" 17 ) 18 19 const ( 20 gzipID1 = 0x1f 21 gzipID2 = 0x8b 22 gzipDeflate = 8 23 flagText = 1 << 0 24 flagHdrCrc = 1 << 1 25 flagExtra = 1 << 2 26 flagName = 1 << 3 27 flagComment = 1 << 4 28 ) 29 30 func makeReader(r io.Reader) flate.Reader { 31 if rr, ok := r.(flate.Reader); ok { 32 return rr 33 } 34 return bufio.NewReader(r) 35 } 36 37 var ( 38 // ErrChecksum is returned when reading GZIP data that has an invalid checksum. 39 ErrChecksum = errors.New("gzip: invalid checksum") 40 // ErrHeader is returned when reading GZIP data that has an invalid header. 41 ErrHeader = errors.New("gzip: invalid header") 42 ) 43 44 // The gzip file stores a header giving metadata about the compressed file. 45 // That header is exposed as the fields of the Writer and Reader structs. 46 // 47 // Strings must be UTF-8 encoded and may only contain Unicode code points 48 // U+0001 through U+00FF, due to limitations of the GZIP file format. 49 type Header struct { 50 Comment string // comment 51 Extra []byte // "extra data" 52 ModTime time.Time // modification time 53 Name string // file name 54 OS byte // operating system type 55 } 56 57 // A Reader is an io.Reader that can be read to retrieve 58 // uncompressed data from a gzip-format compressed file. 59 // 60 // In general, a gzip file can be a concatenation of gzip files, 61 // each with its own header. Reads from the Reader 62 // return the concatenation of the uncompressed data of each. 63 // Only the first header is recorded in the Reader fields. 64 // 65 // Gzip files store a length and checksum of the uncompressed data. 66 // The Reader will return a ErrChecksum when Read 67 // reaches the end of the uncompressed data if it does not 68 // have the expected length or checksum. Clients should treat data 69 // returned by Read as tentative until they receive the io.EOF 70 // marking the end of the data. 71 type Reader struct { 72 Header 73 r flate.Reader 74 decompressor io.ReadCloser 75 digest hash.Hash32 76 size uint32 77 flg byte 78 buf [512]byte 79 err error 80 multistream bool 81 } 82 83 // NewReader creates a new Reader reading the given reader. 84 // If r does not also implement io.ByteReader, 85 // the decompressor may read more data than necessary from r. 86 // It is the caller's responsibility to call Close on the Reader when done. 87 func NewReader(r io.Reader) (*Reader, error) { 88 z := new(Reader) 89 z.r = makeReader(r) 90 z.multistream = true 91 z.digest = crc32.NewIEEE() 92 if err := z.readHeader(true); err != nil { 93 return nil, err 94 } 95 return z, nil 96 } 97 98 // Reset discards the Reader z's state and makes it equivalent to the 99 // result of its original state from NewReader, but reading from r instead. 100 // This permits reusing a Reader rather than allocating a new one. 101 func (z *Reader) Reset(r io.Reader) error { 102 z.r = makeReader(r) 103 if z.digest == nil { 104 z.digest = crc32.NewIEEE() 105 } else { 106 z.digest.Reset() 107 } 108 z.size = 0 109 z.err = nil 110 z.multistream = true 111 return z.readHeader(true) 112 } 113 114 // Multistream controls whether the reader supports multistream files. 115 // 116 // If enabled (the default), the Reader expects the input to be a sequence 117 // of individually gzipped data streams, each with its own header and 118 // trailer, ending at EOF. The effect is that the concatenation of a sequence 119 // of gzipped files is treated as equivalent to the gzip of the concatenation 120 // of the sequence. This is standard behavior for gzip readers. 121 // 122 // Calling Multistream(false) disables this behavior; disabling the behavior 123 // can be useful when reading file formats that distinguish individual gzip 124 // data streams or mix gzip data streams with other data streams. 125 // In this mode, when the Reader reaches the end of the data stream, 126 // Read returns io.EOF. If the underlying reader implements io.ByteReader, 127 // it will be left positioned just after the gzip stream. 128 // To start the next stream, call z.Reset(r) followed by z.Multistream(false). 129 // If there is no next stream, z.Reset(r) will return io.EOF. 130 func (z *Reader) Multistream(ok bool) { 131 z.multistream = ok 132 } 133 134 // GZIP (RFC 1952) is little-endian, unlike ZLIB (RFC 1950). 135 func get4(p []byte) uint32 { 136 return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24 137 } 138 139 func (z *Reader) readString() (string, error) { 140 var err error 141 needconv := false 142 for i := 0; ; i++ { 143 if i >= len(z.buf) { 144 return "", ErrHeader 145 } 146 z.buf[i], err = z.r.ReadByte() 147 if err != nil { 148 return "", err 149 } 150 if z.buf[i] > 0x7f { 151 needconv = true 152 } 153 if z.buf[i] == 0 { 154 // GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1). 155 if needconv { 156 s := make([]rune, 0, i) 157 for _, v := range z.buf[0:i] { 158 s = append(s, rune(v)) 159 } 160 return string(s), nil 161 } 162 return string(z.buf[0:i]), nil 163 } 164 } 165 } 166 167 func (z *Reader) read2() (uint32, error) { 168 _, err := io.ReadFull(z.r, z.buf[0:2]) 169 if err != nil { 170 if err == io.EOF { 171 err = io.ErrUnexpectedEOF 172 } 173 return 0, err 174 } 175 return uint32(z.buf[0]) | uint32(z.buf[1])<<8, nil 176 } 177 178 func (z *Reader) readHeader(save bool) error { 179 _, err := io.ReadFull(z.r, z.buf[0:10]) 180 if err != nil { 181 // RFC1952 section 2.2 says the following: 182 // A gzip file consists of a series of "members" (compressed data sets). 183 // 184 // Other than this, the specification does not clarify whether a 185 // "series" is defined as "one or more" or "zero or more". To err on the 186 // side of caution, Go interprets this to mean "zero or more". 187 // Thus, it is okay to return io.EOF here. 188 return err 189 } 190 if z.buf[0] != gzipID1 || z.buf[1] != gzipID2 || z.buf[2] != gzipDeflate { 191 return ErrHeader 192 } 193 z.flg = z.buf[3] 194 if save { 195 z.ModTime = time.Unix(int64(get4(z.buf[4:8])), 0) 196 // z.buf[8] is xfl, ignored 197 z.OS = z.buf[9] 198 } 199 z.digest.Reset() 200 z.digest.Write(z.buf[0:10]) 201 202 if z.flg&flagExtra != 0 { 203 n, err := z.read2() 204 if err != nil { 205 return err 206 } 207 data := make([]byte, n) 208 if _, err = io.ReadFull(z.r, data); err != nil { 209 if err == io.EOF { 210 err = io.ErrUnexpectedEOF 211 } 212 return err 213 } 214 if save { 215 z.Extra = data 216 } 217 } 218 219 var s string 220 if z.flg&flagName != 0 { 221 if s, err = z.readString(); err != nil { 222 return err 223 } 224 if save { 225 z.Name = s 226 } 227 } 228 229 if z.flg&flagComment != 0 { 230 if s, err = z.readString(); err != nil { 231 return err 232 } 233 if save { 234 z.Comment = s 235 } 236 } 237 238 if z.flg&flagHdrCrc != 0 { 239 n, err := z.read2() 240 if err != nil { 241 return err 242 } 243 sum := z.digest.Sum32() & 0xFFFF 244 if n != sum { 245 return ErrHeader 246 } 247 } 248 249 z.digest.Reset() 250 if z.decompressor == nil { 251 z.decompressor = flate.NewReader(z.r) 252 } else { 253 z.decompressor.(flate.Resetter).Reset(z.r, nil) 254 } 255 return nil 256 } 257 258 func (z *Reader) Read(p []byte) (n int, err error) { 259 if z.err != nil { 260 return 0, z.err 261 } 262 if len(p) == 0 { 263 return 0, nil 264 } 265 266 n, err = z.decompressor.Read(p) 267 z.digest.Write(p[0:n]) 268 z.size += uint32(n) 269 if n != 0 || err != io.EOF { 270 z.err = err 271 return 272 } 273 274 // Finished file; check checksum + size. 275 if _, err := io.ReadFull(z.r, z.buf[0:8]); err != nil { 276 if err == io.EOF { 277 err = io.ErrUnexpectedEOF 278 } 279 z.err = err 280 return 0, err 281 } 282 crc32, isize := get4(z.buf[0:4]), get4(z.buf[4:8]) 283 sum := z.digest.Sum32() 284 if sum != crc32 || isize != z.size { 285 z.err = ErrChecksum 286 return 0, z.err 287 } 288 289 // File is ok; is there another? 290 if !z.multistream { 291 return 0, io.EOF 292 } 293 294 if err = z.readHeader(false); err != nil { 295 z.err = err 296 return 297 } 298 299 // Yes. Reset and read from it. 300 z.digest.Reset() 301 z.size = 0 302 return z.Read(p) 303 } 304 305 // Close closes the Reader. It does not close the underlying io.Reader. 306 func (z *Reader) Close() error { return z.decompressor.Close() }