readHeader reads the GZIP header according to section 2.3.1. This method does not set z.err.
()
| 181 | // readHeader reads the GZIP header according to section 2.3.1. |
| 182 | // This method does not set z.err. |
| 183 | func (z *Reader) readHeader() (hdr Header, err error) { |
| 184 | if _, err = io.ReadFull(z.r, z.buf[:10]); err != nil { |
| 185 | // RFC 1952, section 2.2, says the following: |
| 186 | // A gzip file consists of a series of "members" (compressed data sets). |
| 187 | // |
| 188 | // Other than this, the specification does not clarify whether a |
| 189 | // "series" is defined as "one or more" or "zero or more". To err on the |
| 190 | // side of caution, Go interprets this to mean "zero or more". |
| 191 | // Thus, it is okay to return io.EOF here. |
| 192 | return hdr, err |
| 193 | } |
| 194 | if z.buf[0] != gzipID1 || z.buf[1] != gzipID2 || z.buf[2] != gzipDeflate { |
| 195 | return hdr, ErrHeader |
| 196 | } |
| 197 | flg := z.buf[3] |
| 198 | hdr.ModTime = time.Unix(int64(le.Uint32(z.buf[4:8])), 0) |
| 199 | // z.buf[8] is XFL and is currently ignored. |
| 200 | hdr.OS = z.buf[9] |
| 201 | z.digest = crc32.ChecksumIEEE(z.buf[:10]) |
| 202 | |
| 203 | if flg&flagExtra != 0 { |
| 204 | if _, err = io.ReadFull(z.r, z.buf[:2]); err != nil { |
| 205 | return hdr, noEOF(err) |
| 206 | } |
| 207 | z.digest = crc32.Update(z.digest, crc32.IEEETable, z.buf[:2]) |
| 208 | data := make([]byte, le.Uint16(z.buf[:2])) |
| 209 | if _, err = io.ReadFull(z.r, data); err != nil { |
| 210 | return hdr, noEOF(err) |
| 211 | } |
| 212 | z.digest = crc32.Update(z.digest, crc32.IEEETable, data) |
| 213 | hdr.Extra = data |
| 214 | } |
| 215 | |
| 216 | var s string |
| 217 | if flg&flagName != 0 { |
| 218 | if s, err = z.readString(); err != nil { |
| 219 | return hdr, err |
| 220 | } |
| 221 | hdr.Name = s |
| 222 | } |
| 223 | |
| 224 | if flg&flagComment != 0 { |
| 225 | if s, err = z.readString(); err != nil { |
| 226 | return hdr, err |
| 227 | } |
| 228 | hdr.Comment = s |
| 229 | } |
| 230 | |
| 231 | if flg&flagHdrCrc != 0 { |
| 232 | if _, err = io.ReadFull(z.r, z.buf[:2]); err != nil { |
| 233 | return hdr, noEOF(err) |
| 234 | } |
| 235 | digest := le.Uint16(z.buf[:2]) |
| 236 | if digest != uint16(z.digest) { |
| 237 | return hdr, ErrHeader |
| 238 | } |
| 239 | } |
| 240 |