github.com/aloncn/graphics-go@v0.0.1/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 // valid after NewReader or Reader.Reset 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 // 87 // It is the caller's responsibility to call Close on the Reader when done. 88 // 89 // The Reader.Header fields will be valid in the Reader returned. 90 func NewReader(r io.Reader) (*Reader, error) { 91 z := new(Reader) 92 z.r = makeReader(r) 93 z.multistream = true 94 z.digest = crc32.NewIEEE() 95 if err := z.readHeader(true); err != nil { 96 return nil, err 97 } 98 return z, nil 99 } 100 101 // Reset discards the Reader z's state and makes it equivalent to the 102 // result of its original state from NewReader, but reading from r instead. 103 // This permits reusing a Reader rather than allocating a new one. 104 func (z *Reader) Reset(r io.Reader) error { 105 z.r = makeReader(r) 106 if z.digest == nil { 107 z.digest = crc32.NewIEEE() 108 } else { 109 z.digest.Reset() 110 } 111 z.size = 0 112 z.err = nil 113 z.multistream = true 114 return z.readHeader(true) 115 } 116 117 // Multistream controls whether the reader supports multistream files. 118 // 119 // If enabled (the default), the Reader expects the input to be a sequence 120 // of individually gzipped data streams, each with its own header and 121 // trailer, ending at EOF. The effect is that the concatenation of a sequence 122 // of gzipped files is treated as equivalent to the gzip of the concatenation 123 // of the sequence. This is standard behavior for gzip readers. 124 // 125 // Calling Multistream(false) disables this behavior; disabling the behavior 126 // can be useful when reading file formats that distinguish individual gzip 127 // data streams or mix gzip data streams with other data streams. 128 // In this mode, when the Reader reaches the end of the data stream, 129 // Read returns io.EOF. If the underlying reader implements io.ByteReader, 130 // it will be left positioned just after the gzip stream. 131 // To start the next stream, call z.Reset(r) followed by z.Multistream(false). 132 // If there is no next stream, z.Reset(r) will return io.EOF. 133 func (z *Reader) Multistream(ok bool) { 134 z.multistream = ok 135 } 136 137 // GZIP (RFC 1952) is little-endian, unlike ZLIB (RFC 1950). 138 func get4(p []byte) uint32 { 139 return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24 140 } 141 142 func (z *Reader) readString() (string, error) { 143 var err error 144 needconv := false 145 for i := 0; ; i++ { 146 if i >= len(z.buf) { 147 return "", ErrHeader 148 } 149 z.buf[i], err = z.r.ReadByte() 150 if err != nil { 151 return "", err 152 } 153 if z.buf[i] > 0x7f { 154 needconv = true 155 } 156 if z.buf[i] == 0 { 157 // GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1). 158 if needconv { 159 s := make([]rune, 0, i) 160 for _, v := range z.buf[0:i] { 161 s = append(s, rune(v)) 162 } 163 return string(s), nil 164 } 165 return string(z.buf[0:i]), nil 166 } 167 } 168 } 169 170 func (z *Reader) read2() (uint32, error) { 171 _, err := io.ReadFull(z.r, z.buf[0:2]) 172 if err != nil { 173 if err == io.EOF { 174 err = io.ErrUnexpectedEOF 175 } 176 return 0, err 177 } 178 return uint32(z.buf[0]) | uint32(z.buf[1])<<8, nil 179 } 180 181 func (z *Reader) readHeader(save bool) error { 182 _, err := io.ReadFull(z.r, z.buf[0:10]) 183 if err != nil { 184 // RFC1952 section 2.2 says the following: 185 // A gzip file consists of a series of "members" (compressed data sets). 186 // 187 // Other than this, the specification does not clarify whether a 188 // "series" is defined as "one or more" or "zero or more". To err on the 189 // side of caution, Go interprets this to mean "zero or more". 190 // Thus, it is okay to return io.EOF here. 191 return err 192 } 193 if z.buf[0] != gzipID1 || z.buf[1] != gzipID2 || z.buf[2] != gzipDeflate { 194 return ErrHeader 195 } 196 z.flg = z.buf[3] 197 if save { 198 z.ModTime = time.Unix(int64(get4(z.buf[4:8])), 0) 199 // z.buf[8] is xfl, ignored 200 z.OS = z.buf[9] 201 } 202 z.digest.Reset() 203 z.digest.Write(z.buf[0:10]) 204 205 if z.flg&flagExtra != 0 { 206 n, err := z.read2() 207 if err != nil { 208 return err 209 } 210 data := make([]byte, n) 211 if _, err = io.ReadFull(z.r, data); err != nil { 212 if err == io.EOF { 213 err = io.ErrUnexpectedEOF 214 } 215 return err 216 } 217 if save { 218 z.Extra = data 219 } 220 } 221 222 var s string 223 if z.flg&flagName != 0 { 224 if s, err = z.readString(); err != nil { 225 return err 226 } 227 if save { 228 z.Name = s 229 } 230 } 231 232 if z.flg&flagComment != 0 { 233 if s, err = z.readString(); err != nil { 234 return err 235 } 236 if save { 237 z.Comment = s 238 } 239 } 240 241 if z.flg&flagHdrCrc != 0 { 242 n, err := z.read2() 243 if err != nil { 244 return err 245 } 246 sum := z.digest.Sum32() & 0xFFFF 247 if n != sum { 248 return ErrHeader 249 } 250 } 251 252 z.digest.Reset() 253 if z.decompressor == nil { 254 z.decompressor = flate.NewReader(z.r) 255 } else { 256 z.decompressor.(flate.Resetter).Reset(z.r, nil) 257 } 258 return nil 259 } 260 261 func (z *Reader) Read(p []byte) (n int, err error) { 262 if z.err != nil { 263 return 0, z.err 264 } 265 if len(p) == 0 { 266 return 0, nil 267 } 268 269 n, err = z.decompressor.Read(p) 270 z.digest.Write(p[0:n]) 271 z.size += uint32(n) 272 if n != 0 || err != io.EOF { 273 z.err = err 274 return 275 } 276 277 // Finished file; check checksum + size. 278 if _, err := io.ReadFull(z.r, z.buf[0:8]); err != nil { 279 if err == io.EOF { 280 err = io.ErrUnexpectedEOF 281 } 282 z.err = err 283 return 0, err 284 } 285 crc32, isize := get4(z.buf[0:4]), get4(z.buf[4:8]) 286 sum := z.digest.Sum32() 287 if sum != crc32 || isize != z.size { 288 z.err = ErrChecksum 289 return 0, z.err 290 } 291 292 // File is ok; is there another? 293 if !z.multistream { 294 return 0, io.EOF 295 } 296 297 if err = z.readHeader(false); err != nil { 298 z.err = err 299 return 300 } 301 302 // Yes. Reset and read from it. 303 z.digest.Reset() 304 z.size = 0 305 return z.Read(p) 306 } 307 308 // Close closes the Reader. It does not close the underlying io.Reader. 309 func (z *Reader) Close() error { return z.decompressor.Close() }