aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/digest/old_crc/gencrc/main.cpp
blob: d7816d9631eb29341166db8ee8399f236632daf5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <util/stream/output.h>

#define POLY_16 0x1021
#define POLY_32 0xEDB88320UL
#define POLY_64 ULL(0xE543279765927881)

static void crc16init() {
    ui32 CRCTAB16[256];
    ui32 crc;
    int i, j;

    for (i = 0; i < 256; CRCTAB16[i++] = 0xFFFF & ((crc << 8) ^ (crc >> 8)))
        for (crc = i, j = 8; j > 0; j--)
            if (crc & 1)
                crc = (crc >> 1) ^ POLY_16;
            else
                crc >>= 1;

    for (size_t k = 0; k < 256; ++k) { 
        Cout << "    ULL(" << CRCTAB16[k] << ")"; 

        if (k != 255) { 
            Cout << ",\n";
        }
    }
}

static void crc32init() {
    ui32 CRCTAB32[256];
    ui32 crc;
    int i, j;

    for (i = 0; i < 256; CRCTAB32[i++] = crc)
        for (crc = i, j = 8; j > 0; j--)
            if (crc & 1)
                crc = (crc >> 1) ^ POLY_32;
            else
                crc >>= 1;

    for (size_t k = 0; k < 256; ++k) { 
        Cout << "    ULL(" << CRCTAB32[k] << ")"; 

        if (k != 255) { 
            Cout << ",\n";
        }
    }
}

int main() {
    Cout << "static const ui32 CRCTAB16[] = {\n\n";
    crc16init();
    Cout << "\n};\n\n";
    Cout << "static const ui32 CRCTAB32[] = {\n\n";
    crc32init();
    Cout << "\n};\n\n";
}