xref: /haiku/src/add-ons/kernel/file_systems/ext2/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 	Reversed version by Jérôme Duval
17 */
18 
19 #include <stdio.h>
20 #include <sys/types.h>
21 
22 int
23 main(int argc, char *argv[]) {
24 	ulong crc, poly;
25 
26 	if (argc != 2) {
27 		fprintf(stderr, "USAGE: crc_table <octal polynomial=120001 for ext2>\n");
28 		return 0;
29 	}
30 
31 	sscanf(argv[1], "%lo", &poly);
32 	if (poly & 0xffff0000) {
33 		fprintf(stderr, "ERROR: polynomial is too large, sucka.\n");
34 		return 0;
35 	}
36 
37 	printf("//! CRC 0%o table, as generated by crc_table.cpp\n", poly);
38 	printf("static uint16 crc_table[256] = { \n");
39 	for (int n = 0; n < 256; n++) {
40 		if (n%8 == 0)
41 			printf("    ");
42 		crc = n;
43 		for (int i = 0; i < 8; i++) {
44 			if (crc & 0x0001)
45 				crc = (crc >> 1) ^ poly;
46 			else
47 				crc >>= 1;
48 		}
49 		printf("0x%04x%s ", crc, (n != 255 ? "," : ""));
50 		if (n%8 == 7)
51 			printf("\n");
52 	}
53 	printf("};\n");
54 	return 0;
55 }
56