-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_table_test.go
59 lines (48 loc) · 1.63 KB
/
example_table_test.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
package crcutil_test
import (
"fmt"
"github.com/knieriem/crcutil"
"github.com/knieriem/crcutil/poly3"
)
// This example shows how to calculate a 3-bit CRC over
// 13 bits of data from two bytes of a frame of a serial
// communication protocol:
//
// byte 0: crc (3 bits) | device address (5 bits)
// byte 1: function code + length (8 bits)
//
// A specific algorithm will be used: 3-bit polynomial 0x3 (GSM),
// in reversed form. With an initial value of 5, the crc is calculated first
// over the five bits of the first byte, then the 8 bits of the second byte.
// After adding the 8 bit value, the crc result shall be mirrored (reflected).
// We will create
// - one table for the 5 bit lookup, which already
// regards for the initial value, and
// - one table for the 8 bit lookup, which produces a reflected result.
type crcTabs struct {
t1 []uint8
t2 []uint8
}
var crc3 crcTabs
func init() {
poly := poly3.GSM.ReversedForm()
initialValue := crcutil.WithInitialValue(5)
fiveBits := crcutil.WithDataWidth(5)
crc3.t1 = poly.MakeTable(initialValue, fiveBits)
crc3.t2 = poly.MakeTable(crcutil.WithReversedBits())
}
// Then we can calculate the checksum by performing
// a table lookup first for the 5-bit data part, XOR-ing the result
// with the 8-bit data part, and doing a lookup of this value with
// the other table.
func (tabs *crcTabs) checksum(v1, v2 uint8) uint8 {
crc := tabs.t1[v1]
return tabs.t2[crc^v2]
}
// With the 5-bit device address being 10, and a value of the second
// byte of 0b0111_0101, the checksum should be 4.
func ExamplePoly_MakeTable() {
sum := crc3.checksum(10, 0b0111_0101)
fmt.Println(sum)
// Output: 4
}