forked from bluenviron/gomavlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx25.go
More file actions
54 lines (44 loc) · 889 Bytes
/
x25.go
File metadata and controls
54 lines (44 loc) · 889 Bytes
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
package gomavlib
import (
"hash"
)
// Hash16 is an interface modeled on the standard hash.Hash32 and hash.Hash64.
// In this library, it is implemented by NewX25().
type Hash16 interface {
hash.Hash
Sum16() uint16
}
type x25 struct {
crc uint16
}
// NewX25 allocates a X25 hasher.
// X25 is the hash used to compute Frame checksums.
func NewX25() Hash16 {
x := &x25{}
x.Reset()
return x
}
func (x *x25) Reset() {
x.crc = 0xFFFF
}
func (x *x25) Size() int {
return 2
}
func (x *x25) BlockSize() int {
return 1
}
func (x *x25) Write(p []byte) (int, error) {
for _, b := range p {
tmp := uint16(b) ^ (x.crc & 0xFF)
tmp ^= (tmp << 4)
tmp &= 0xFF
x.crc = (x.crc >> 8) ^ (tmp << 8) ^ (tmp << 3) ^ (tmp >> 4)
}
return len(p), nil
}
func (x *x25) Sum16() uint16 {
return x.crc
}
func (x *x25) Sum(b []byte) []byte {
return append(b, byte(x.crc), byte(x.crc>>8))
}