NASM 2.15rc10
[nasm.git] / misc / xcrcgen.c
blob01984800a5eeba4ed8f53e53a2db6b4dd847d2c4
1 /*
2 * Produce a "generalized CRC" table. Assumes a platform with
3 * /dev/urandom -- otherwise reimplement get_random_byte().
4 */
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <inttypes.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
13 static uint8_t get_random_byte(void)
15 static int fd = -1;
16 uint8_t buf;
17 int rv;
19 if (fd < 0)
20 fd = open("/dev/urandom", O_RDONLY);
22 do {
23 errno = 0;
24 rv = read(fd, &buf, 1);
25 if (rv < 1 && errno != EAGAIN)
26 abort();
27 } while (rv < 1);
29 return buf;
32 static void random_permute(uint8_t *buf)
34 int i, j, k;
35 int m;
37 for (i = 0; i < 256; i++)
38 buf[i] = i;
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;
53 static void xcrc_table(uint64_t *buf)
55 uint8_t perm[256];
56 int i, j;
58 memset(buf, 0, 8*256); /* Make static checkers happy */
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];
67 int main(void)
69 int i;
70 uint64_t buf[256];
72 xcrc_table(buf);
74 for (i = 0; i < 256; i++) {
75 printf("%016"PRIx64"\n", buf[i]);
78 return 0;