-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathsign.go
287 lines (247 loc) · 5.81 KB
/
sign.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package rsync
import (
"bytes"
"fmt"
"io"
"sort"
)
var (
defaultBlockLen uint32 = 2048
defaultSumLen uint32 = 64
)
type SignHdr struct {
magic uint32
blockLen uint32
sumLen uint32
totalLen int64 //总长度
}
func (hdr *SignHdr) toBytes() (res []byte) {
res = append(res, htonl(hdr.magic)...)
res = append(res, htonl(hdr.blockLen)...)
res = append(res, htonl(hdr.sumLen)...)
res = append(res, vhtonll(uint64(hdr.totalLen), 8)...)
return
}
// signature header:
// magic 4 bytes
func signHeader(rdLen int64, sumLen, blockLen uint32) (hdr SignHdr) {
hdr.magic = BlakeMagic
hdr.blockLen = blockLen
hdr.sumLen = sumLen
hdr.totalLen = rdLen
return
}
// generates signature
func GenSign(rd io.Reader, rdLen int64, blockLen uint32, result io.Writer) (err error) {
var (
n int
sumLen uint32 = 64
hdr SignHdr
buf []byte
sig []byte
)
if blockLen == 0 {
blockLen = defaultBlockLen
}
hdr = signHeader(rdLen, sumLen, blockLen)
sig = append(sig, hdr.toBytes()...)
result.Write(sig)
buf = make([]byte, blockLen)
for {
/*
ReadFull reads exactly len(buf) bytes from r into buf. It returns
the number of bytes copied and an error if fewer bytes were read.
The error is EOF only if no bytes were read. If an EOF happens after
reading some but not all the bytes, ReadFull returns ErrUnexpectedEOF.
On return, n == len(buf) if and only if err == nil.
*/
n, err = io.ReadFull(rd, buf)
if uint32(n) == blockLen || err == io.ErrUnexpectedEOF {
wsum := weakSum(buf[0:n])
result.Write(htonl(wsum))
ssum := strongSum(buf[0:n], sumLen)
//fmt.Printf("Sign: length=%d p=%s wsum=0x%x ssum=0x%x\n", n, string(buf[0:n]), wsum, string(ssum))
result.Write(ssum)
}
if err != nil {
break
}
}
if err == io.EOF || err == io.ErrUnexpectedEOF {
err = nil
}
return
}
func LoadSign(rd io.Reader, debug bool) (sig *Signature, err error) {
var (
n int
ok bool
count int
tlen uint64
block *rs_block_sig
blocks []*rs_block_sig
tag *rs_tag_table_entry
)
sig = new(Signature)
if sig.magic, err = ntohl(rd); err != nil {
err = fmt.Errorf("read signature maigin failed: %s", err.Error())
return
}
if sig.block_len, err = ntohl(rd); err != nil {
err = fmt.Errorf("read signature block lenght failed: %s", err.Error())
return
}
if sig.strong_sum_len, err = ntohl(rd); err != nil {
err = fmt.Errorf("read signature strong sum length failed: %s", err.Error())
return
}
if tlen, err = ntohll(rd); err != nil {
err = fmt.Errorf("read signature remainer length failed: %s", err.Error())
return
}
sig.flength = int64(tlen)
// 初始化map
sig.block_sigs = make(map[uint32][]*rs_block_sig)
sig.tag_tables = make(map[uint32]*rs_tag_table_entry)
if tlen == 0 {
return
}
// read weak sum & strong sum
for {
block = new(rs_block_sig)
block.ssum = make([]byte, sig.strong_sum_len)
block.i = count
if block.wsum, err = ntohl(rd); err != nil {
if err == io.EOF {
err = nil
break
}
return
}
if n, err = io.ReadFull(rd, block.ssum); err != nil {
err = fmt.Errorf("LoadSign: read strong sum failed: n=%d expect=%d error=%s",
n, sig.strong_sum_len, err.Error())
return
}
sig.block_sigs[block.wsum] = append(sig.block_sigs[block.wsum], block)
if tag, ok = sig.tag_tables[block.wsum]; ok {
if count > tag.l {
tag.r = count
}
} else {
sig.tag_tables[block.wsum] = &rs_tag_table_entry{count, count}
}
count++
if debug {
fmt.Printf("LoadSign: block count %d, wsum: 0x%x ssum: 0x%x\n", count, block.wsum, block.ssum)
}
}
if debug {
fmt.Printf("LoadSign: block len: %d strong sum len: %d total len: %d count: %d\n",
sig.block_len, sig.strong_sum_len, sig.flength, count)
}
if count == 0 {
return
}
// 对sig.block_sigs排序
for _, blocks = range sig.block_sigs {
sort.Sort(blockSlice(blocks))
}
sig.count = count
// build
//err = buildSign(sig)
return
}
type blockSlice []*rs_block_sig
func (s blockSlice) Len() int {
return len(s)
}
func (s blockSlice) Less(i, j int) bool {
c := bytes.Compare(s[i].ssum, s[j].ssum)
if c < 0 {
return true
}
if c == 0 {
return s[i].i < s[j].i
}
return false
}
func (s blockSlice) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// 查找
// 由于blockSlice的weaksum都相同(map的key相同),search时,只需比较strongsum
// 当strongSum相同时,比较pos与各个block的pos的关系,取最小的
func (s blockSlice) search(ssum []byte, pos int64, blockLen uint32) (matchAt int64) {
matchAt = -1
found := -1
bl := int64(blockLen)
i, j := 0, len(s)
for i < j {
h := i + (j-i)/2 // avoid overflow when computing h
// i ≤ h < j
c := bytes.Compare(ssum, s[h].ssum)
if c == 0 {
found = h
if int64(s[h].i)*bl == pos {
// 精确匹配到,直接返回
return pos
}
break
} else if c > 0 {
i = h + 1 // preserves f(j) == true
} else {
j = h
}
}
if found == -1 {
return
}
matchAt = int64(s[found].i) * int64(blockLen)
if len(s) == 1 {
return
}
var (
idx int
minIdx int = found
minDist int64
)
minDist = abs(int64(s[found].i)*bl - pos)
// 向前轮询,找到最小distance的idx
for idx = found - 1; idx >= 0; idx-- {
if bytes.Compare(ssum, s[idx].ssum) == 0 {
dist := abs(int64(s[idx].i)*bl - pos)
if dist <= minDist {
minDist = dist
minIdx = idx
} else {
break
}
} else {
break
}
}
// 向后轮询,找到最小distance的idx
for idx = found + 1; idx < len(s); idx++ {
if bytes.Compare(ssum, s[idx].ssum) == 0 {
dist := abs(int64(s[idx].i)*bl - pos)
//println("idx:", idx, "minDist:", minDist, "dist:", dist)
if dist < minDist {
minDist = dist
minIdx = idx
} else {
break
}
} else {
break
}
}
matchAt = int64(s[minIdx].i) * int64(blockLen)
return
}
func abs(i int64) int64 {
if i < 0 {
return -i
}
return i
}