-
Notifications
You must be signed in to change notification settings - Fork 1
/
lrc.go
76 lines (64 loc) · 1.89 KB
/
lrc.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
package lrc
import (
"encoding"
"fmt"
"hash"
)
// LRC implements the Modbus longitudinal redundancy check hash, which is
// simply the 2's complements of the sum (mod 2⁸) of all input bytes.
// The zero value is ready to use.
type LRC byte
// interface checks
var _ hash.Hash = (*LRC)(nil)
var _ encoding.BinaryMarshaler = LRC(0)
var _ encoding.BinaryUnmarshaler = (*LRC)(nil)
// BlockSize implements hash.Hash.BlockSize.
func (LRC) BlockSize() int {
return 1
}
// MarshalBinary implements encoding.BinaryMarshaler.MarshalBinary.
func (b LRC) MarshalBinary() ([]byte, error) {
return []byte{byte(b)}, nil
}
// Reset implements hash.Hash.Reset.
func (b *LRC) Reset() {
*b = 0
}
// Size implements hash.Hash.Size.
func (LRC) Size() int {
return 1
}
// Sum implements hash.Hash.Sum.
func (b LRC) Sum(buf []byte) []byte {
return append(buf, b.Sum8())
}
// Sum8 returns the 8-bit LRC hash accumulated so far.
func (b LRC) Sum8() uint8 {
return uint8(-int8(b))
}
// HexSum appends the current hash to buf in hexadecimal format and returns the
// resulting slice. It does not change the underlying hash state.
// The hexadecimal representation is high-nibble first, using upper case
// hexadecimal digits. This format conforms to the specification of the ASCII
// transmission mode in the Modbus over serial line specification
// (section 2.5.2).
func (b LRC) HexSum(buf []byte) []byte {
const hexdigits = "0123456789ABCDEF"
sum := b.Sum8()
return append(buf, hexdigits[sum>>4], hexdigits[sum&0x0F])
}
// UnmarshalBinary implements encoding.BinaryUnmarshaler.UnmarshalBinary.
func (b *LRC) UnmarshalBinary(data []byte) error {
if len(data) != 1 {
return fmt.Errorf("expected data size 1, got %d", len(data))
}
*b = LRC(data[0])
return nil
}
// Write implements io.Writer.Write for hash.Hash.
func (b *LRC) Write(buf []byte) (int, error) {
for _, v := range buf {
*b += LRC(v)
}
return len(buf), nil
}