-
Notifications
You must be signed in to change notification settings - Fork 1
/
bcc.go
63 lines (52 loc) · 1.3 KB
/
bcc.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
package lrc
import (
"encoding"
"fmt"
"hash"
)
// BCC implements the block check character hash, which is simply the
// XOR of all input bytes. The zero value is ready to use.
type BCC byte
// interface checks
var _ hash.Hash = (*BCC)(nil)
var _ encoding.BinaryMarshaler = BCC(0)
var _ encoding.BinaryUnmarshaler = (*BCC)(nil)
// BlockSize implements hash.Hash.BlockSize.
func (BCC) BlockSize() int {
return 1
}
// MarshalBinary implements encoding.BinaryMarshaler.MarshalBinary.
func (b BCC) MarshalBinary() ([]byte, error) {
return []byte{byte(b)}, nil
}
// Reset implements hash.Hash.Reset.
func (b *BCC) Reset() {
*b = 0
}
// Size implements hash.Hash.Size.
func (BCC) Size() int {
return 1
}
// Sum implements hash.Hash.Sum.
func (b BCC) Sum(buf []byte) []byte {
return append(buf, byte(b))
}
// Sum8 returns the 8-bit BCC hash accumulated so far.
func (b BCC) Sum8() uint8 {
return uint8(b)
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.UnmarshalBinary.
func (b *BCC) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return fmt.Errorf("expected data size 1, got %d", len(data))
}
*b = BCC(data[0])
return nil
}
// Write implements io.Writer.Write for hash.Hash.
func (b *BCC) Write(buf []byte) (int, error) {
for _, v := range buf {
*b ^= BCC(v)
}
return len(buf), nil
}