]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/gen_crc32table.c
xfs: harden directory integrity checks some more
[thirdparty/xfsprogs-dev.git] / libxfs / gen_crc32table.c
1 #include <stdio.h>
2 #include "crc32defs.h"
3 #include <inttypes.h>
4
5 #define ENTRIES_PER_LINE 4
6
7 #if CRC_LE_BITS > 8
8 # define LE_TABLE_ROWS (CRC_LE_BITS/8)
9 # define LE_TABLE_SIZE 256
10 #else
11 # define LE_TABLE_ROWS 1
12 # define LE_TABLE_SIZE (1 << CRC_LE_BITS)
13 #endif
14
15 #if CRC_BE_BITS > 8
16 # define BE_TABLE_ROWS (CRC_BE_BITS/8)
17 # define BE_TABLE_SIZE 256
18 #else
19 # define BE_TABLE_ROWS 1
20 # define BE_TABLE_SIZE (1 << CRC_BE_BITS)
21 #endif
22
23 static uint32_t crc32table_le[LE_TABLE_ROWS][256];
24 static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
25
26 /*
27 * big endian ordered CRC not used by XFS.
28 static uint32_t crc32table_be[BE_TABLE_ROWS][256];
29 */
30
31 /**
32 * crc32init_le() - allocate and initialize LE table data
33 *
34 * crc is the crc of the byte i; other entries are filled in based on the
35 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
36 *
37 */
38 static void crc32init_le_generic(const uint32_t polynomial,
39 uint32_t (*tab)[256])
40 {
41 unsigned i, j;
42 uint32_t crc = 1;
43
44 tab[0][0] = 0;
45
46 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
47 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
48 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
49 tab[0][i + j] = crc ^ tab[0][j];
50 }
51 for (i = 0; i < LE_TABLE_SIZE; i++) {
52 crc = tab[0][i];
53 for (j = 1; j < LE_TABLE_ROWS; j++) {
54 crc = tab[0][crc & 0xff] ^ (crc >> 8);
55 tab[j][i] = crc;
56 }
57 }
58 }
59
60 static void crc32init_le(void)
61 {
62 crc32init_le_generic(CRCPOLY_LE, crc32table_le);
63 }
64
65 static void crc32cinit_le(void)
66 {
67 crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
68 }
69
70 /**
71 * crc32init_be() - allocate and initialize BE table data
72 */
73 #if 0 /* not used */
74 static void crc32init_be(void)
75 {
76 unsigned i, j;
77 uint32_t crc = 0x80000000;
78
79 crc32table_be[0][0] = 0;
80
81 for (i = 1; i < BE_TABLE_SIZE; i <<= 1) {
82 crc = (crc << 1) ^ ((crc & 0x80000000) ? CRCPOLY_BE : 0);
83 for (j = 0; j < i; j++)
84 crc32table_be[0][i + j] = crc ^ crc32table_be[0][j];
85 }
86 for (i = 0; i < BE_TABLE_SIZE; i++) {
87 crc = crc32table_be[0][i];
88 for (j = 1; j < BE_TABLE_ROWS; j++) {
89 crc = crc32table_be[0][(crc >> 24) & 0xff] ^ (crc << 8);
90 crc32table_be[j][i] = crc;
91 }
92 }
93 }
94 #endif
95
96 static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
97 {
98 int i, j;
99
100 for (j = 0 ; j < rows; j++) {
101 printf("{");
102 for (i = 0; i < len - 1; i++) {
103 if (i % ENTRIES_PER_LINE == 0)
104 printf("\n");
105 printf("%s(0x%8.8xL), ", trans, table[j][i]);
106 }
107 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
108 }
109 }
110
111 int main(int argc, char** argv)
112 {
113 printf("/* this file is generated - do not edit */\n\n");
114
115 if (CRC_LE_BITS > 1) {
116 crc32init_le();
117 printf("static u32 crc32table_le[%d][%d] = {",
118 LE_TABLE_ROWS, LE_TABLE_SIZE);
119 output_table(crc32table_le, LE_TABLE_ROWS,
120 LE_TABLE_SIZE, "tole");
121 printf("};\n");
122 }
123
124 #if 0 /* not used by xfsprogs */
125 if (CRC_BE_BITS > 1) {
126 crc32init_be();
127 printf("static u32 crc32table_be[%d][%d] = {",
128 BE_TABLE_ROWS, BE_TABLE_SIZE);
129 output_table(crc32table_be, LE_TABLE_ROWS,
130 BE_TABLE_SIZE, "tobe");
131 printf("};\n");
132 }
133 #endif
134 if (CRC_LE_BITS > 1) {
135 crc32cinit_le();
136 printf("static u32 crc32ctable_le[%d][%d] = {",
137 LE_TABLE_ROWS, LE_TABLE_SIZE);
138 output_table(crc32ctable_le, LE_TABLE_ROWS,
139 LE_TABLE_SIZE, "tole");
140 printf("};\n");
141 }
142
143 return 0;
144 }