blob: 740b62e8cde5f3a0a085055823959dced8f6200b [file] [log] [blame]
Nigel Tao3a7ccfe2018-03-25 18:09:51 +11001// Copyright 2018 The Wuffs Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
Nigel Tao788479d2021-08-22 10:52:51 +100015//go:build ignore
Nigel Tao3a7ccfe2018-03-25 18:09:51 +110016// +build ignore
17
18package main
19
20// print-crc32-magic-numbers.go prints the std/crc32 magic number tables.
21//
22// Usage: go run print-crc32-magic-numbers.go
23
24import (
25 "fmt"
26 "hash/crc32"
27 "os"
28)
29
30func main() {
31 if err := main1(); err != nil {
32 os.Stderr.WriteString(err.Error() + "\n")
33 os.Exit(1)
34 }
35}
36
37func main1() error {
Nigel Tao99597e12018-10-07 21:01:18 +110038 tables := [16]crc32.Table{}
Nigel Taoa106e2a2018-05-03 13:14:12 +100039 tables[0] = *crc32.MakeTable(crc32.IEEE)
40
Nigel Tao34fe0512018-11-11 10:25:19 +110041 // See "Multi-Byte Lookup Tables" in std/crc32/README.md for more detail on
42 // the slicing-by-M algorithm. We use an M of 16.
Nigel Taoa106e2a2018-05-03 13:14:12 +100043 for i := 0; i < 256; i++ {
44 crc := tables[0][i]
Nigel Tao99597e12018-10-07 21:01:18 +110045 for j := 1; j < 16; j++ {
Nigel Taoa106e2a2018-05-03 13:14:12 +100046 crc = tables[0][crc&0xFF] ^ (crc >> 8)
47 tables[j][i] = crc
48 }
49 }
50
51 for i, t := range tables {
52 if i != 0 {
Nigel Tao99597e12018-10-07 21:01:18 +110053 fmt.Println("],[")
Nigel Tao3a7ccfe2018-03-25 18:09:51 +110054 }
Nigel Taoa106e2a2018-05-03 13:14:12 +100055 for j, x := range t {
56 fmt.Printf("0x%08X,", x)
57 if j&7 == 7 {
58 fmt.Println()
59 }
60 }
Nigel Tao3a7ccfe2018-03-25 18:09:51 +110061 }
62 return nil
63}