-
Notifications
You must be signed in to change notification settings - Fork 451
/
reader.go
233 lines (199 loc) · 6.63 KB
/
reader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package digest
import (
"bufio"
"errors"
"hash"
"hash/adler32"
"io"
"os"
)
var (
// errReadFewerThanExpectedBytes returned when number of bytes read is fewer than expected
errReadFewerThanExpectedBytes = errors.New("number of bytes read is fewer than expected")
// errChecksumMismatch returned when the calculated checksum doesn't match the stored checksum
errChecksumMismatch = errors.New("calculated checksum doesn't match stored checksum")
// errBufferSizeMismatch returned when ReadAllAndValidate called without well sized buffer
errBufferSizeMismatch = errors.New("buffer passed is not an exact fit for contents")
)
// FdWithDigestReader provides a buffered reader for reading from the underlying file.
type FdWithDigestReader interface {
FdWithDigest
io.Reader
// ReadAllAndValidate reads everything in the underlying file and validates
// it against the expected digest, returning an error if they don't match.
// Note: the buffer "b" must be an exact match for how long the contents being
// read is, the signature is structured this way to allow for buffer reuse.
ReadAllAndValidate(b []byte, expectedDigest uint32) (int, error)
// Validate compares the current digest against the expected digest and returns
// an error if they don't match.
Validate(expectedDigest uint32) error
}
type fdWithDigestReader struct {
fd *os.File
bufReader *bufio.Reader
readerWithDigest ReaderWithDigest
single [1]byte
}
// NewFdWithDigestReader creates a new FdWithDigestReader.
func NewFdWithDigestReader(bufferSize int) FdWithDigestReader {
bufReader := bufio.NewReaderSize(nil, bufferSize)
return &fdWithDigestReader{
bufReader: bufReader,
readerWithDigest: NewReaderWithDigest(bufReader),
}
}
// Reset resets the underlying file descriptor and the buffered reader.
func (r *fdWithDigestReader) Reset(fd *os.File) {
r.fd = fd
r.bufReader.Reset(fd)
r.readerWithDigest.Reset(r.bufReader)
}
func (r *fdWithDigestReader) Read(b []byte) (int, error) {
return r.readerWithDigest.Read(b)
}
func (r *fdWithDigestReader) Fd() *os.File {
return r.fd
}
func (r *fdWithDigestReader) Digest() hash.Hash32 {
return r.readerWithDigest.Digest()
}
func (r *fdWithDigestReader) ReadAllAndValidate(b []byte, expectedDigest uint32) (int, error) {
n, err := r.Read(b)
if err != nil {
return n, err
}
// NB(r): Attempt next read to prove that the size of the buffer b
// was sized correctly to fit all contents into it and that we are
// correctly now at the end of input.
_, err = r.Read(r.single[:])
if err != io.EOF {
return 0, errBufferSizeMismatch
}
if err := r.Validate(expectedDigest); err != nil {
return n, err
}
return n, nil
}
func (r *fdWithDigestReader) Validate(expectedDigest uint32) error {
return r.readerWithDigest.Validate(expectedDigest)
}
func (r *fdWithDigestReader) Close() error {
if r.fd == nil {
return nil
}
err := r.fd.Close()
r.fd = nil
return err
}
// FdWithDigestContentsReader provides additional functionality of reading a digest from the underlying file.
type FdWithDigestContentsReader interface {
FdWithDigestReader
// ReadDigest reads a digest from the underlying file.
ReadDigest() (uint32, error)
}
type fdWithDigestContentsReader struct {
FdWithDigestReader
digestBuf Buffer
}
// NewFdWithDigestContentsReader creates a new FdWithDigestContentsReader.
func NewFdWithDigestContentsReader(bufferSize int) FdWithDigestContentsReader {
return &fdWithDigestContentsReader{
FdWithDigestReader: NewFdWithDigestReader(bufferSize),
digestBuf: NewBuffer(),
}
}
func (r *fdWithDigestContentsReader) ReadDigest() (uint32, error) {
n, err := r.Read(r.digestBuf)
if err != nil {
return 0, err
}
if n < len(r.digestBuf) {
return 0, errReadFewerThanExpectedBytes
}
return r.digestBuf.ReadDigest(), nil
}
// ReaderWithDigest is a reader that that calculates a digest
// as it is read.
type ReaderWithDigest interface {
io.Reader
// Reset resets the reader for use with a new reader.
Reset(reader io.Reader)
// Digest returns the digest.
Digest() hash.Hash32
// Validate compares the current digest against the expected digest and returns
// an error if they don't match.
Validate(expectedDigest uint32) error
}
type readerWithDigest struct {
reader io.Reader
digest hash.Hash32
}
// NewReaderWithDigest creates a new reader that calculates a digest as it
// reads an input.
func NewReaderWithDigest(reader io.Reader) ReaderWithDigest {
return &readerWithDigest{
reader: reader,
digest: adler32.New(),
}
}
func (r *readerWithDigest) Reset(reader io.Reader) {
r.reader = reader
r.digest.Reset()
}
func (r *readerWithDigest) Digest() hash.Hash32 {
return r.digest
}
func (r *readerWithDigest) readBytes(b []byte) (int, error) {
n, err := r.reader.Read(b)
if err != nil {
return 0, err
}
// In case the buffered reader only returns what's remaining in
// the buffer, recursively read what's left in the underlying reader.
if n < len(b) {
b = b[n:]
remainder, err := r.readBytes(b)
return n + remainder, err
}
return n, err
}
func (r *readerWithDigest) Read(b []byte) (int, error) {
n, err := r.readBytes(b)
if err != nil && err != io.EOF {
return n, err
}
// If we encountered an EOF error and didn't read any bytes
// given a non-empty slice, we return an EOF error.
if err == io.EOF && n == 0 && len(b) > 0 {
return 0, err
}
if _, err := r.digest.Write(b[:n]); err != nil {
return 0, err
}
return n, nil
}
func (r *readerWithDigest) Validate(expectedDigest uint32) error {
if r.digest.Sum32() != expectedDigest {
return errChecksumMismatch
}
return nil
}