1
2
3
4
5
6
7 package gzip
8
9 import (
10 "bufio"
11 "compress/flate"
12 "encoding/binary"
13 "errors"
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 var (
31
32 ErrChecksum = errors.New("gzip: invalid checksum")
33
34 ErrHeader = errors.New("gzip: invalid header")
35 )
36
37 var le = binary.LittleEndian
38
39
40 func noEOF(err error) error {
41 if err == io.EOF {
42 return io.ErrUnexpectedEOF
43 }
44 return err
45 }
46
47
48
49
50
51
52 type Header struct {
53 Comment string
54 Extra []byte
55 ModTime time.Time
56 Name string
57 OS byte
58 }
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74 type Reader struct {
75 Header
76 r flate.Reader
77 decompressor io.ReadCloser
78 digest uint32
79 size uint32
80 buf [512]byte
81 err error
82 multistream bool
83 }
84
85
86
87
88
89
90
91
92 func NewReader(r io.Reader) (*Reader, error) {
93 z := new(Reader)
94 if err := z.Reset(r); err != nil {
95 return nil, err
96 }
97 return z, nil
98 }
99
100
101
102
103 func (z *Reader) Reset(r io.Reader) error {
104 *z = Reader{
105 decompressor: z.decompressor,
106 multistream: true,
107 }
108 if rr, ok := r.(flate.Reader); ok {
109 z.r = rr
110 } else {
111 z.r = bufio.NewReader(r)
112 }
113 z.Header, z.err = z.readHeader()
114 return z.err
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133 func (z *Reader) Multistream(ok bool) {
134 z.multistream = ok
135 }
136
137
138
139
140
141 func (z *Reader) readString() (string, error) {
142 var err error
143 needConv := false
144 for i := 0; ; i++ {
145 if i >= len(z.buf) {
146 return "", ErrHeader
147 }
148 z.buf[i], err = z.r.ReadByte()
149 if err != nil {
150 return "", err
151 }
152 if z.buf[i] > 0x7f {
153 needConv = true
154 }
155 if z.buf[i] == 0 {
156
157 z.digest = crc32.Update(z.digest, crc32.IEEETable, z.buf[:i+1])
158
159
160 if needConv {
161 s := make([]rune, 0, i)
162 for _, v := range z.buf[:i] {
163 s = append(s, rune(v))
164 }
165 return string(s), nil
166 }
167 return string(z.buf[:i]), nil
168 }
169 }
170 }
171
172
173
174 func (z *Reader) readHeader() (hdr Header, err error) {
175 if _, err = io.ReadFull(z.r, z.buf[:10]); err != nil {
176
177
178
179
180
181
182
183 return hdr, err
184 }
185 if z.buf[0] != gzipID1 || z.buf[1] != gzipID2 || z.buf[2] != gzipDeflate {
186 return hdr, ErrHeader
187 }
188 flg := z.buf[3]
189 if t := int64(le.Uint32(z.buf[4:8])); t > 0 {
190
191
192 hdr.ModTime = time.Unix(t, 0)
193 }
194
195 hdr.OS = z.buf[9]
196 z.digest = crc32.ChecksumIEEE(z.buf[:10])
197
198 if flg&flagExtra != 0 {
199 if _, err = io.ReadFull(z.r, z.buf[:2]); err != nil {
200 return hdr, noEOF(err)
201 }
202 z.digest = crc32.Update(z.digest, crc32.IEEETable, z.buf[:2])
203 data := make([]byte, le.Uint16(z.buf[:2]))
204 if _, err = io.ReadFull(z.r, data); err != nil {
205 return hdr, noEOF(err)
206 }
207 z.digest = crc32.Update(z.digest, crc32.IEEETable, data)
208 hdr.Extra = data
209 }
210
211 var s string
212 if flg&flagName != 0 {
213 if s, err = z.readString(); err != nil {
214 return hdr, err
215 }
216 hdr.Name = s
217 }
218
219 if flg&flagComment != 0 {
220 if s, err = z.readString(); err != nil {
221 return hdr, err
222 }
223 hdr.Comment = s
224 }
225
226 if flg&flagHdrCrc != 0 {
227 if _, err = io.ReadFull(z.r, z.buf[:2]); err != nil {
228 return hdr, noEOF(err)
229 }
230 digest := le.Uint16(z.buf[:2])
231 if digest != uint16(z.digest) {
232 return hdr, ErrHeader
233 }
234 }
235
236 z.digest = 0
237 if z.decompressor == nil {
238 z.decompressor = flate.NewReader(z.r)
239 } else {
240 z.decompressor.(flate.Resetter).Reset(z.r, nil)
241 }
242 return hdr, nil
243 }
244
245
246 func (z *Reader) Read(p []byte) (n int, err error) {
247 if z.err != nil {
248 return 0, z.err
249 }
250
251 for n == 0 {
252 n, z.err = z.decompressor.Read(p)
253 z.digest = crc32.Update(z.digest, crc32.IEEETable, p[:n])
254 z.size += uint32(n)
255 if z.err != io.EOF {
256
257 return n, z.err
258 }
259
260
261 if _, err := io.ReadFull(z.r, z.buf[:8]); err != nil {
262 z.err = noEOF(err)
263 return n, z.err
264 }
265 digest := le.Uint32(z.buf[:4])
266 size := le.Uint32(z.buf[4:8])
267 if digest != z.digest || size != z.size {
268 z.err = ErrChecksum
269 return n, z.err
270 }
271 z.digest, z.size = 0, 0
272
273
274 if !z.multistream {
275 return n, io.EOF
276 }
277 z.err = nil
278
279 if _, z.err = z.readHeader(); z.err != nil {
280 return n, z.err
281 }
282 }
283
284 return n, nil
285 }
286
287
288
289
290 func (z *Reader) Close() error { return z.decompressor.Close() }
291
View as plain text