]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - e2fsck/gen_crc32table.c
debugfs, e2fsck: fix s_desc_size handling
[thirdparty/e2fsprogs.git] / e2fsck / gen_crc32table.c
1 /*
2 * gen_crc32table.c --- Generate CRC32 tables.
3 *
4 * Copyright (C) 2008 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Public
8 * License.
9 * %End-Header%
10 */
11
12 #include <stdio.h>
13 #include "crc32defs.h"
14 #include <inttypes.h>
15
16 #define ENTRIES_PER_LINE 4
17
18 #define LE_TABLE_SIZE (1 << CRC_LE_BITS)
19 #define BE_TABLE_SIZE (1 << CRC_BE_BITS)
20
21 static uint32_t crc32table_le[LE_TABLE_SIZE];
22 static uint32_t crc32table_be[BE_TABLE_SIZE];
23
24 /**
25 * crc32init_le() - allocate and initialize LE table data
26 *
27 * crc is the crc of the byte i; other entries are filled in based on the
28 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
29 *
30 */
31 static void crc32init_le(void)
32 {
33 unsigned i, j;
34 uint32_t crc = 1;
35
36 crc32table_le[0] = 0;
37
38 for (i = 1 << (CRC_LE_BITS - 1); i; i >>= 1) {
39 crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
40 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
41 crc32table_le[i + j] = crc ^ crc32table_le[j];
42 }
43 }
44
45 /**
46 * crc32init_be() - allocate and initialize BE table data
47 */
48 static void crc32init_be(void)
49 {
50 unsigned i, j;
51 uint32_t crc = 0x80000000;
52
53 crc32table_be[0] = 0;
54
55 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
56 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
57 for (j = 0; j < i; j++)
58 crc32table_be[i + j] = crc ^ crc32table_be[j];
59 }
60 }
61
62 static void output_table(uint32_t table[], int len, const char *trans)
63 {
64 int i;
65
66 for (i = 0; i < len - 1; i++) {
67 if (i % ENTRIES_PER_LINE == 0)
68 printf("\n");
69 printf("%s(0x%8.8xL), ", trans, table[i]);
70 }
71 printf("%s(0x%8.8xL)\n", trans, table[len - 1]);
72 }
73
74 #ifdef __GNUC__
75 #define ATTR(x) __attribute__(x)
76 #else
77 #define ATTR(x)
78 #endif
79
80 int main(int argc ATTR((unused)), char** argv ATTR((unused)))
81 {
82 printf("/* this file is generated - do not edit */\n\n");
83
84 printf("#ifdef UNITTEST\n");
85 if (CRC_LE_BITS > 1) {
86 crc32init_le();
87 printf("static const __u32 crc32table_le[] = {");
88 output_table(crc32table_le, LE_TABLE_SIZE, "tole");
89 printf("};\n");
90 }
91 printf("#endif /* UNITTEST */\n");
92
93 if (CRC_BE_BITS > 1) {
94 crc32init_be();
95 printf("static const __u32 crc32table_be[] = {");
96 output_table(crc32table_be, BE_TABLE_SIZE, "tobe");
97 printf("};\n");
98 }
99
100 return 0;
101 }