...
1
2
3
4
5
24 package zlib
25
26 import (
27 "bufio"
28 "compress/flate"
29 "encoding/binary"
30 "errors"
31 "hash"
32 "hash/adler32"
33 "io"
34 )
35
36 const (
37 zlibDeflate = 8
38 zlibMaxWindow = 7
39 )
40
41 var (
42
43 ErrChecksum = errors.New("zlib: invalid checksum")
44
45 ErrDictionary = errors.New("zlib: invalid dictionary")
46
47 ErrHeader = errors.New("zlib: invalid header")
48 )
49
50 type reader struct {
51 r flate.Reader
52 decompressor io.ReadCloser
53 digest hash.Hash32
54 err error
55 scratch [4]byte
56 }
57
58
59
60
61 type Resetter interface {
62
63
64 Reset(r io.Reader, dict []byte) error
65 }
66
67
68
69
70
71
72
73
74 func NewReader(r io.Reader) (io.ReadCloser, error) {
75 return NewReaderDict(r, nil)
76 }
77
78
79
80
81
82
83 func NewReaderDict(r io.Reader, dict []byte) (io.ReadCloser, error) {
84 z := new(reader)
85 err := z.Reset(r, dict)
86 if err != nil {
87 return nil, err
88 }
89 return z, nil
90 }
91
92 func (z *reader) Read(p []byte) (int, error) {
93 if z.err != nil {
94 return 0, z.err
95 }
96
97 var n int
98 n, z.err = z.decompressor.Read(p)
99 z.digest.Write(p[0:n])
100 if z.err != io.EOF {
101
102 return n, z.err
103 }
104
105
106 if _, err := io.ReadFull(z.r, z.scratch[0:4]); err != nil {
107 if err == io.EOF {
108 err = io.ErrUnexpectedEOF
109 }
110 z.err = err
111 return n, z.err
112 }
113
114 checksum := binary.BigEndian.Uint32(z.scratch[:4])
115 if checksum != z.digest.Sum32() {
116 z.err = ErrChecksum
117 return n, z.err
118 }
119 return n, io.EOF
120 }
121
122
123
124
125 func (z *reader) Close() error {
126 if z.err != nil && z.err != io.EOF {
127 return z.err
128 }
129 z.err = z.decompressor.Close()
130 return z.err
131 }
132
133 func (z *reader) Reset(r io.Reader, dict []byte) error {
134 *z = reader{decompressor: z.decompressor}
135 if fr, ok := r.(flate.Reader); ok {
136 z.r = fr
137 } else {
138 z.r = bufio.NewReader(r)
139 }
140
141
142 _, z.err = io.ReadFull(z.r, z.scratch[0:2])
143 if z.err != nil {
144 if z.err == io.EOF {
145 z.err = io.ErrUnexpectedEOF
146 }
147 return z.err
148 }
149 h := binary.BigEndian.Uint16(z.scratch[:2])
150 if (z.scratch[0]&0x0f != zlibDeflate) || (z.scratch[0]>>4 > zlibMaxWindow) || (h%31 != 0) {
151 z.err = ErrHeader
152 return z.err
153 }
154 haveDict := z.scratch[1]&0x20 != 0
155 if haveDict {
156 _, z.err = io.ReadFull(z.r, z.scratch[0:4])
157 if z.err != nil {
158 if z.err == io.EOF {
159 z.err = io.ErrUnexpectedEOF
160 }
161 return z.err
162 }
163 checksum := binary.BigEndian.Uint32(z.scratch[:4])
164 if checksum != adler32.Checksum(dict) {
165 z.err = ErrDictionary
166 return z.err
167 }
168 }
169
170 if z.decompressor == nil {
171 if haveDict {
172 z.decompressor = flate.NewReaderDict(z.r, dict)
173 } else {
174 z.decompressor = flate.NewReader(z.r)
175 }
176 } else {
177 z.decompressor.(flate.Resetter).Reset(z.r, dict)
178 }
179 z.digest = adler32.New()
180 return nil
181 }
182
View as plain text