xref: /haiku/src/add-ons/kernel/file_systems/btrfs/crc_table.cpp (revision 2b76973fa2401f7a5edf68e6470f3d3210cbcff3)
1 //----------------------------------------------------------------------
2 //  This software is part of the OpenBeOS distribution and is covered
3 //  by the OpenBeOS license.
4 //
5 //  Copyright (c) 2003 Tyler Dauwalder, tyler@dauwalder.net
6 //---------------------------------------------------------------------
7 
8 /*! \file crc_table.cpp
9 
10         Standalone program to generate the CRC table used for calculating
11         UDF tag id CRC values.
12 
13         This code based off of crc code in UDF-2.50 specs, as permitted.
14         See UDF-2.50 6.5 for more information.
15 
16         Reflected version by Jéme Duval
17 */
18 
19 #include <stdio.h>
20 #include <sys/types.h>
21 
22 typedef unsigned int uint32 ;
23 
24 uint32
25 reflect32 (uint32 b)
26 {
27 	uint32 rw = 0;
28 
29 	for (int i = 0; i < 32; i++){
30 		if (b & 1)
31 			rw |= 1 << (31 - i);
32 		b >>= 1;
33 	}
34 	return rw;
35 }
36 
37 
38 int
39 main(int argc, char *argv[]) {
40         uint32 crc, poly;
41 
42         if (argc != 2) {
43                 fprintf(stderr, "USAGE: crc_table <octal polynomial=3667067501 for btrfs>\n");
44                 return 0;
45         }
46 
47         sscanf(argv[1], "%lo", &poly);
48 
49         printf("//! CRC 0%o table, as generated by crc_table.cpp\n", poly);
50         printf("static uint32 crc_table[256] = { \n");
51         for (int n = 0; n < 256; n++) {
52                 if (n%8 == 0)
53                         printf("    ");
54                 crc = reflect32(n);
55                 for (int i = 0; i < 8; i++) {
56                         if (crc & 0x80000000)
57                                 crc = (crc << 1) ^ poly;
58                         else
59                                 crc <<= 1;
60                 }
61 		crc = reflect32(crc);
62                 printf("0x%08x%s ", crc, (n != 255 ? "," : ""));
63                 if (n%8 == 7)
64                         printf("\n");
65         }
66         printf("};\n");
67         return 0;
68 }
69