xref: /haiku/src/add-ons/kernel/file_systems/btrfs/crc_table.cpp (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 /*
2  *  This software is part of the Haiku distribution and is covered
3  *  by the MIT 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 
20 
21 #include "system_dependencies.h"
22 
23 
24 typedef unsigned int uint32 ;
25 
26 
27 uint32
28 reflect32 (uint32 b)
29 {
30 	uint32 rw = 0;
31 
32 	for (int i = 0; i < 32; i++) {
33 		if (b & 1)
34 			rw |= 1 << (31 - i);
35 		b >>= 1;
36 	}
37 	return rw;
38 }
39 
40 
41 int
42 main(int argc, char* argv[]) {
43 	uint32 crc, poly;
44 
45 	if (argc != 2) {
46 		fprintf(stderr, "USAGE: crc_table <octal polynomial=3667067501 for btrfs>\n");
47 		return 0;
48 	}
49 	sscanf(argv[1], "%lo", &poly);
50 	printf("//! CRC 0%o table, as generated by crc_table.cpp\n", poly);
51 	printf("static uint32 kCrcTable[256] = {\n\t");
52 	for (int n = 0; n < 256; n++) {
53 		crc = reflect32(n);
54 		for (int i = 0; i < 8; i++) {
55 			if (crc & 0x80000000)
56 				crc = (crc << 1) ^ poly;
57 			else
58 				crc <<= 1;
59 		}
60 		crc = reflect32(crc);
61 		printf("0x%08x%s%s", crc, (n != 255 ? "," : ""), (n % 6 != 5 ? " " : ""));
62 		if (n % 6 == 5)
63 			printf("\n\t");
64 	}
65 	printf("\n};\n");
66 	return 0;
67 }
68 
69