-
Notifications
You must be signed in to change notification settings - Fork 0
/
gopar3.go
49 lines (44 loc) · 1023 Bytes
/
gopar3.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
package gopar3
import (
"context"
"hash/crc32"
"io"
)
const (
ShardLimit = 1<<(TagBytesForShardOrder*8) - 1
ShardBatchLimit = 1<<(TagBytesForShardBatch*8) - 1
SourceSizeLimit = 1<<(TagBytesForSourceSize*8) - 1
)
// castagnoliTable sources [crc.New] with 0x82f63b78
// polynomial. It is known for superior error detection
// and use in BitTorrent and iSCSI protocols.
//
// Example of BitTorrent use:
// https://github.com/anacrolix/torrent/blob/master/bep40.go
var castagnoliTable = crc32.MakeTable(crc32.Castagnoli)
func CastagnoliSum(ctx context.Context, r io.Reader) (uint32, error) {
var (
crc = crc32.New(castagnoliTable)
b = make([]byte, 2*32*1024)
n int
rerr error
werr error
)
for rerr == nil {
select {
case <-ctx.Done():
return 0, ctx.Err()
default:
}
n, rerr = io.ReadFull(r, b)
if _, werr = crc.Write(b[:n]); werr != nil {
return 0, werr
}
switch rerr {
case io.EOF, io.ErrUnexpectedEOF:
default:
return 0, rerr
}
}
return crc.Sum32(), nil
}