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