/
signaturereader.go
129 lines (106 loc) · 3.79 KB
/
signaturereader.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
package octodiff
import (
"bytes"
"errors"
"fmt"
"io"
)
type SignatureReader struct {
ProgressReporter ProgressReporter // must be non-null
}
func NewSignatureReader() *SignatureReader {
return &SignatureReader{
ProgressReporter: NopProgressReporter(),
}
}
func (s *SignatureReader) ReadSignature(input io.Reader, inputLength int64) (*Signature, error) {
pos := int64(0)
s.ProgressReporter.ReportProgress("Reading signature", pos, inputLength)
headerBytes := make([]byte, len(BinarySignatureHeader))
bytesRead, err := input.Read(headerBytes)
if err != nil {
return nil, err
}
if bytesRead != len(BinarySignatureHeader) || !bytes.Equal(headerBytes, BinarySignatureHeader) {
return nil, errors.New("the signature file appears to be corrupt")
}
pos += int64(bytesRead)
var versionBytes = make([]byte, len(BinaryVersion))
bytesRead, err = input.Read(versionBytes)
if err != nil {
return nil, err
}
if bytesRead != len(BinaryVersion) || !bytes.Equal(versionBytes, BinaryVersion) {
return nil, errors.New("the signature file uses a newer file format than this program can handle")
}
pos += int64(bytesRead)
hashAlgorithmStr, bytesRead, err := readLengthPrefixedString(input)
if err != nil {
return nil, err
}
pos += int64(bytesRead)
rollingChecksumAlgorithmStr, bytesRead, err := readLengthPrefixedString(input)
if err != nil {
return nil, err
}
pos += int64(bytesRead)
var endBytes = make([]byte, len(BinaryEndOfMetadata))
bytesRead, err = input.Read(endBytes)
if err != nil {
return nil, err
}
if bytesRead != len(endBytes) || !bytes.Equal(endBytes, BinaryEndOfMetadata) {
return nil, errors.New("the signature file appears to be corrupt")
}
pos += int64(bytesRead)
s.ProgressReporter.ReportProgress("Reading signature", pos, inputLength)
if hashAlgorithmStr != DefaultHashAlgorithm.Name() {
return nil, fmt.Errorf("signature uses unsupported hash algorithm %s", hashAlgorithmStr)
}
hashAlgorithm := DefaultHashAlgorithm
var rollingChecksum RollingChecksum
switch rollingChecksumAlgorithmStr {
case Adler32RollingChecksumName:
rollingChecksum = NewAdler32RollingChecksum()
case Adler32RollingChecksumV2Name:
rollingChecksum = NewAdler32RollingChecksumV2()
default:
return nil, fmt.Errorf("signature uses unsupported rolling checksum algorithm %s", rollingChecksumAlgorithmStr)
}
expectedHashLength := hashAlgorithm.HashLength()
remainingBytes := inputLength - pos
signatureSize := 2 + 4 + expectedHashLength
if remainingBytes%int64(signatureSize) != 0 {
return nil, errors.New("the signature file appears to be corrupt; at least one chunk has data missing")
}
expectedNumberOfChunks := remainingBytes / int64(signatureSize)
chunks := make([]*ChunkSignature, 0, expectedNumberOfChunks)
chunkStart := int64(0)
iter := NewReaderIteratorSize(input, signatureSize)
for iter.Next() {
block := iter.Current
blockBytesRead := len(iter.Current)
if blockBytesRead != signatureSize {
return nil, fmt.Errorf("expecting to read %d bytes for ChunkSignature but only got %d", signatureSize, blockBytesRead)
}
pos += int64(blockBytesRead)
length := uint16(block[0]) | uint16(block[1])<<8
checksum := uint32(block[2]) | uint32(block[3])<<8 | uint32(block[4])<<16 | uint32(block[5])<<24
chunks = append(chunks, &ChunkSignature{
StartOffset: chunkStart,
Length: length,
RollingChecksum: checksum,
Hash: append([]byte(nil), block[6:]...), // copy the buffer as the next read around the loop is going to overwrite 'block'
})
chunkStart += int64(length)
s.ProgressReporter.ReportProgress("Reading signature", pos, inputLength)
}
if err = iter.Err(); err != nil {
return nil, err
}
return &Signature{
HashAlgorithm: hashAlgorithm,
RollingChecksumAlgorithm: rollingChecksum,
Chunks: chunks,
}, nil
}