-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathprint-crc32-example.go
150 lines (132 loc) · 3.02 KB
/
print-crc32-example.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Copyright 2018 The Wuffs Authors.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//
// SPDX-License-Identifier: Apache-2.0 OR MIT
//go:build ignore
// +build ignore
package main
// print-crc32-example.go prints the worked examples from std/crc32/README.md.
//
// Usage: go run print-crc32-example.go
import (
"bytes"
"os"
)
func main() {
if err := main1(); err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(1)
}
}
func main1() error {
do(false, 4, 0xC, makeBinaryData("H"))
os.Stdout.WriteString("\n")
do(true, 32, 0xEDB88320, makeBinaryData("Hi\n"))
return nil
}
func bit(b bool) byte {
if b {
return '1'
}
return '0'
}
func hex(b []byte) byte {
x := byte(0)
x |= (b[0] - '0') << 0
x |= (b[1] - '0') << 1
x |= (b[2] - '0') << 2
x |= (b[3] - '0') << 3
if x < 10 {
return '0' + x
}
return 'A' + (x - 10)
}
func makeBinaryData(data string) []byte {
b := make([]byte, len(data)*8)
for i := range b {
mask := uint8(1) << uint(i&7)
b[i] = bit(data[i>>3]&mask != 0)
}
return b
}
func makePoly(n int, bits uint64) []byte {
b := make([]byte, n+1)
b[0] = '1'
for i := 0; i < n; i++ {
mask := uint64(1) << uint(i)
b[i+1] = bit(bits&mask != 0)
}
return b
}
func show(prefix string, indent int, binaryData []byte) {
const nineSpaces = " "
os.Stdout.WriteString(prefix)
for ; indent >= 8; indent -= 8 {
os.Stdout.WriteString(nineSpaces)
}
os.Stdout.WriteString(nineSpaces[:indent])
for {
b, remaining := binaryData, []byte(nil)
if len(b) > 8-indent {
b, remaining = b[:8-indent], b[8-indent:]
indent = 0
}
os.Stdout.WriteString(string(b))
if len(remaining) == 0 {
break
}
os.Stdout.WriteString(" ")
binaryData = remaining
}
os.Stdout.WriteString("\n")
}
func do(invert bool, polyN int, polyBits uint64, binaryData []byte) {
poly := makePoly(polyN, polyBits)
indent := 0
n := len(binaryData)
show("input ", 0, binaryData)
binaryData = append(binaryData, bytes.Repeat([]byte("0"), polyN)...)
show("pad ", 0, binaryData)
if invert {
for i := 0; i < polyN; i++ {
binaryData[i] ^= 1
}
show("invert ", 0, binaryData)
}
for {
indent = bytes.IndexByte(binaryData[:n], '1')
if indent < 0 {
break
}
show("divisor ", indent, poly)
for i := range poly {
binaryData[indent+i] = bit(binaryData[indent+i] != poly[i])
}
show("xor ", 0, binaryData)
}
for i := 0; i < n; i++ {
binaryData[i] = ' '
}
show("remain ", 0, binaryData)
if invert {
for i := 0; i < polyN; i++ {
binaryData[n+i] ^= 1
}
show("invert ", 0, binaryData)
if polyN&3 == 0 {
for i := 0; i < polyN; i += 4 {
h := hex(binaryData[n+i : n+i+4])
binaryData[n+i+0] = ' '
binaryData[n+i+1] = ' '
binaryData[n+i+2] = ' '
binaryData[n+i+3] = h
}
show("hex ", 0, binaryData)
}
}
}