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