...
Source file
src/hash/crc32/crc32_generic.go
1
2
3
4
5
6
7
8
9
10
11
12
13 package crc32
14
15
16
17
18 func simpleMakeTable(poly uint32) *Table {
19 t := new(Table)
20 simplePopulateTable(poly, t)
21 return t
22 }
23
24
25
26 func simplePopulateTable(poly uint32, t *Table) {
27 for i := 0; i < 256; i++ {
28 crc := uint32(i)
29 for j := 0; j < 8; j++ {
30 if crc&1 == 1 {
31 crc = (crc >> 1) ^ poly
32 } else {
33 crc >>= 1
34 }
35 }
36 t[i] = crc
37 }
38 }
39
40
41
42 func simpleUpdate(crc uint32, tab *Table, p []byte) uint32 {
43 crc = ^crc
44 for _, v := range p {
45 crc = tab[byte(crc)^v] ^ (crc >> 8)
46 }
47 return ^crc
48 }
49
50
51 const slicing8Cutoff = 16
52
53
54 type slicing8Table [8]Table
55
56
57
58 func slicingMakeTable(poly uint32) *slicing8Table {
59 t := new(slicing8Table)
60 simplePopulateTable(poly, &t[0])
61 for i := 0; i < 256; i++ {
62 crc := t[0][i]
63 for j := 1; j < 8; j++ {
64 crc = t[0][crc&0xFF] ^ (crc >> 8)
65 t[j][i] = crc
66 }
67 }
68 return t
69 }
70
71
72
73 func slicingUpdate(crc uint32, tab *slicing8Table, p []byte) uint32 {
74 if len(p) >= slicing8Cutoff {
75 crc = ^crc
76 for len(p) > 8 {
77 crc ^= uint32(p[0]) | uint32(p[1])<<8 | uint32(p[2])<<16 | uint32(p[3])<<24
78 crc = tab[0][p[7]] ^ tab[1][p[6]] ^ tab[2][p[5]] ^ tab[3][p[4]] ^
79 tab[4][crc>>24] ^ tab[5][(crc>>16)&0xFF] ^
80 tab[6][(crc>>8)&0xFF] ^ tab[7][crc&0xFF]
81 p = p[8:]
82 }
83 crc = ^crc
84 }
85 if len(p) == 0 {
86 return crc
87 }
88 return simpleUpdate(crc, &tab[0], p)
89 }
90
View as plain text