]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libfrog/gen_crc32table.c
libfrog: fix bitmap error communication problems
[thirdparty/xfsprogs-dev.git] / libfrog / gen_crc32table.c
CommitLineData
959ef981 1// SPDX-License-Identifier: GPL-2.0
7e280e68
DC
2#include <stdio.h>
3#include "crc32defs.h"
4#include <inttypes.h>
5
6#define ENTRIES_PER_LINE 4
7
8#if CRC_LE_BITS > 8
9# define LE_TABLE_ROWS (CRC_LE_BITS/8)
10# define LE_TABLE_SIZE 256
11#else
12# define LE_TABLE_ROWS 1
13# define LE_TABLE_SIZE (1 << CRC_LE_BITS)
14#endif
15
7e280e68
DC
16static uint32_t crc32ctable_le[LE_TABLE_ROWS][256];
17
7e280e68
DC
18/**
19 * crc32init_le() - allocate and initialize LE table data
20 *
21 * crc is the crc of the byte i; other entries are filled in based on the
22 * fact that crctable[i^j] = crctable[i] ^ crctable[j].
23 *
24 */
25static void crc32init_le_generic(const uint32_t polynomial,
26 uint32_t (*tab)[256])
27{
28 unsigned i, j;
29 uint32_t crc = 1;
30
31 tab[0][0] = 0;
32
33 for (i = LE_TABLE_SIZE >> 1; i; i >>= 1) {
34 crc = (crc >> 1) ^ ((crc & 1) ? polynomial : 0);
35 for (j = 0; j < LE_TABLE_SIZE; j += 2 * i)
36 tab[0][i + j] = crc ^ tab[0][j];
37 }
38 for (i = 0; i < LE_TABLE_SIZE; i++) {
39 crc = tab[0][i];
40 for (j = 1; j < LE_TABLE_ROWS; j++) {
41 crc = tab[0][crc & 0xff] ^ (crc >> 8);
42 tab[j][i] = crc;
43 }
44 }
45}
46
7e280e68
DC
47static void crc32cinit_le(void)
48{
49 crc32init_le_generic(CRC32C_POLY_LE, crc32ctable_le);
50}
51
7e280e68
DC
52static void output_table(uint32_t (*table)[256], int rows, int len, char *trans)
53{
54 int i, j;
55
56 for (j = 0 ; j < rows; j++) {
57 printf("{");
58 for (i = 0; i < len - 1; i++) {
59 if (i % ENTRIES_PER_LINE == 0)
60 printf("\n");
61 printf("%s(0x%8.8xL), ", trans, table[j][i]);
62 }
63 printf("%s(0x%8.8xL)},\n", trans, table[j][len - 1]);
64 }
65}
66
67int main(int argc, char** argv)
68{
69 printf("/* this file is generated - do not edit */\n\n");
70
7e280e68
DC
71 if (CRC_LE_BITS > 1) {
72 crc32cinit_le();
73 printf("static u32 crc32ctable_le[%d][%d] = {",
74 LE_TABLE_ROWS, LE_TABLE_SIZE);
75 output_table(crc32ctable_le, LE_TABLE_ROWS,
76 LE_TABLE_SIZE, "tole");
77 printf("};\n");
78 }
79
80 return 0;
81}