Debian Codegraph @

misc/xcrcgen.c

raw
1 /*
2 * Produce a "generalized CRC" table. Assumes a platform with
3 * /dev/urandom -- otherwise reimplement get_random_byte().
4 */
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <inttypes.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12
13 static uint8_t get_random_byte(void)
14 {
15 static int fd = -1;
16 uint8_t buf;
17 int rv;
18
19 if (fd < 0)
20 fd = open("/dev/urandom", O_RDONLY);
21
22 do {
23 errno = 0;
24 rv = read(fd, &buf, 1);
25 if (rv < 1 && errno != EAGAIN)
26 abort();
27 } while (rv < 1);
28
29 return buf;
30 }
31
32 static void random_permute(uint8_t *buf)
33 {
34 int i, j, k;
35 int m;
36
37 for (i = 0; i < 256; i++)
38 buf[i] = i;
39
40 m = 255;
41 for (i = 255; i > 0; i--) {
42 if (i <= (m >> 1))
43 m >>= 1;
44 do {
45 j = get_random_byte() & m;
46 } while (j > i);
47 k = buf[i];
48 buf[i] = buf[j];
49 buf[j] = k;
50 }
51 }
52
53 static void xcrc_table(uint64_t *buf)
54 {
55 uint8_t perm[256];
56 int i, j;
57
58 memset(buf, 0, 8*256); /* Make static checkers happy */
59
60 for (i = 0; i < 8; i++) {
61 random_permute(perm);
62 for (j = 0; j < 256; j++)
63 buf[j] = (buf[j] << 8) | perm[j];
64 }
65 }
66
67 int main(void)
68 {
69 int i;
70 uint64_t buf[256];
71
72 xcrc_table(buf);
73
74 for (i = 0; i < 256; i++) {
75 printf("%016"PRIx64"\n", buf[i]);
76 }
77
78 return 0;
79 }