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.
5 // Package gzip implements reading and writing of gzip format compressed files,
6 // as specified in RFC 1952.
18 // BUG(nigeltao): Comments and Names don't properly map UTF-8 character codes outside of
19 // the 0x00-0x7f range to ISO 8859-1 (Latin-1).
32 func makeReader(r io.Reader) flate.Reader {
33 if rr, ok := r.(flate.Reader); ok {
36 return bufio.NewReader(r)
39 var HeaderError = errors.New("invalid gzip header")
40 var ChecksumError = errors.New("gzip checksum error")
42 // The gzip file stores a header giving metadata about the compressed file.
43 // That header is exposed as the fields of the Compressor and Decompressor structs.
45 Comment string // comment
46 Extra []byte // "extra data"
47 Mtime uint32 // modification time (seconds since January 1, 1970)
48 Name string // file name
49 OS byte // operating system type
52 // An Decompressor is an io.Reader that can be read to retrieve
53 // uncompressed data from a gzip-format compressed file.
55 // In general, a gzip file can be a concatenation of gzip files,
56 // each with its own header. Reads from the Decompressor
57 // return the concatenation of the uncompressed data of each.
58 // Only the first header is recorded in the Decompressor fields.
60 // Gzip files store a length and checksum of the uncompressed data.
61 // The Decompressor will return a ChecksumError when Read
62 // reaches the end of the uncompressed data if it does not
63 // have the expected length or checksum. Clients should treat data
64 // returned by Read as tentative until they receive the successful
65 // (zero length, nil error) Read marking the end of the data.
66 type Decompressor struct {
69 decompressor io.ReadCloser
77 // NewReader creates a new Decompressor reading the given reader.
78 // The implementation buffers input and may read more data than necessary from r.
79 // It is the caller's responsibility to call Close on the Decompressor when done.
80 func NewReader(r io.Reader) (*Decompressor, error) {
81 z := new(Decompressor)
83 z.digest = crc32.NewIEEE()
84 if err := z.readHeader(true); err != nil {
91 // GZIP (RFC 1952) is little-endian, unlike ZLIB (RFC 1950).
92 func get4(p []byte) uint32 {
93 return uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
96 func (z *Decompressor) readString() (string, error) {
100 return "", HeaderError
102 z.buf[i], err = z.r.ReadByte()
107 // GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1).
108 // TODO(nigeltao): Convert from ISO 8859-1 (Latin-1) to UTF-8.
109 return string(z.buf[0:i]), nil
115 func (z *Decompressor) read2() (uint32, error) {
116 _, err := io.ReadFull(z.r, z.buf[0:2])
120 return uint32(z.buf[0]) | uint32(z.buf[1])<<8, nil
123 func (z *Decompressor) readHeader(save bool) error {
124 _, err := io.ReadFull(z.r, z.buf[0:10])
128 if z.buf[0] != gzipID1 || z.buf[1] != gzipID2 || z.buf[2] != gzipDeflate {
133 z.Mtime = get4(z.buf[4:8])
134 // z.buf[8] is xfl, ignored
138 z.digest.Write(z.buf[0:10])
140 if z.flg&flagExtra != 0 {
145 data := make([]byte, n)
146 if _, err = io.ReadFull(z.r, data); err != nil {
155 if z.flg&flagName != 0 {
156 if s, err = z.readString(); err != nil {
164 if z.flg&flagComment != 0 {
165 if s, err = z.readString(); err != nil {
173 if z.flg&flagHdrCrc != 0 {
178 sum := z.digest.Sum32() & 0xFFFF
185 z.decompressor = flate.NewReader(z.r)
189 func (z *Decompressor) Read(p []byte) (n int, err error) {
197 n, err = z.decompressor.Read(p)
198 z.digest.Write(p[0:n])
200 if n != 0 || err != io.EOF {
205 // Finished file; check checksum + size.
206 if _, err := io.ReadFull(z.r, z.buf[0:8]); err != nil {
210 crc32, isize := get4(z.buf[0:4]), get4(z.buf[4:8])
211 sum := z.digest.Sum32()
212 if sum != crc32 || isize != z.size {
213 z.err = ChecksumError
217 // File is ok; is there another?
218 if err = z.readHeader(false); err != nil {
223 // Yes. Reset and read from it.
229 // Calling Close does not close the wrapped io.Reader originally passed to NewReader.
230 func (z *Decompressor) Close() error { return z.decompressor.Close() }