File: crc-generate-table.c

package info (click to toggle)
coreutils 9.7-3
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,780 kB
  • sloc: ansic: 243,477; sh: 29,063; perl: 7,908; yacc: 1,858; makefile: 196; python: 47; sed: 16
file content (155 lines) | stat: -rw-r--r-- 4,669 bytes parent folder | download | duplicates (4)
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* crc-generate-table.c -- cyclic redundancy check table generator
   Copyright (C) 2024-2025 Free Software Foundation, Inc.

   This file is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   This file is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */

/* Written by Sam Russell. */

#include <stdio.h>
#include <stdlib.h>

/*
 * The following function was extracted from RFC 1952 by Sam
 * Russell.  It was modified to remove the reference to the
 * crc_table_computed flag, to extend it to accept a variable number
 * of bits (described below), and reformatted to follow GNU
 * formatting guidelines.
 *
 * make_crc_table() takes a number of bits and generates a lookup
 * table for the CRC32 algorithm. Usage is as follows:
 *
 * make_crc_table(8) : generate for CRC32(0x00) to CRC32(0xFF)
 * make_crc_table(16) : generate for CRC32(0x0000) to CRC32 (0xFF00)
 * in increments of 0x100
 *
 * This is used for the Sarwate algorithm specified in RFC 1952
 * which uses a single lookup table of make_crc_table(8), and for
 * the slice-by-8 algorithm which uses 8 tables from in 8-bit
 * increments from make_crc_table(8) to make_crc_table(64)
 */
unsigned long crc_table[256];

static void
make_crc_table (int bits)
{
  unsigned long c;

  int n, k;
  for (n = 0; n < 256; n++)
    {
      c = (unsigned long) n;
      for (k = 0; k < bits; k++)
        {
          if (c & 1)
            c = 0xedb88320L ^ (c >> 1);
          else
            c = c >> 1;
        }
      crc_table[n] = c;
    }
}

static void
print_crc_table (FILE * stream, int bits)
{
  make_crc_table (bits);

  fprintf (stream, "  {");

  for (int i = 0; i < 256; i++)
    {
      if ((i % 6) == 0)
        fprintf (stream, "\n   ");
      if (i != 255)
        fprintf (stream, " 0x%08lX,", crc_table[i]);
      else
        fprintf (stream, " 0x%08lX\n", crc_table[i]);
    }

  fprintf (stream, "  }");
}

static void
print_header (FILE * stream)
{
  fprintf (stream, "/* Slice-by-8 lookup tables */\n");
  fprintf (stream, "static const uint32_t crc32_sliceby8_table[][256] = {\n");
  for (int i = 8; i <= 64; i += 8)
    {
      print_crc_table (stream, i);
      if (i < 64)
        fprintf (stream, ",");
      fprintf (stream, "\n");
    }
  fprintf (stream, "};\n");
}

static void
print_copyright_notice (FILE * stream)
{
  fprintf (stream, "/* DO NOT EDIT! GENERATED AUTOMATICALLY! */\n");
  fprintf (stream, "/* crc.c -- cyclic redundancy checks\n");
  fprintf (stream,
           "Copyright (C) 2005-2006, 2009-2025 Free Software Foundation, Inc.\n");
  fprintf (stream, "\n");
  fprintf (stream,
           "This file is free software: you can redistribute it and/or modify\n");
  fprintf (stream,
           "it under the terms of the GNU Lesser General Public License as\n");
  fprintf (stream,
           "published by the Free Software Foundation, either version 3 of the\n");
  fprintf (stream, "License, or (at your option) any later version.\n");
  fprintf (stream, "\n");
  fprintf (stream,
           "This file is distributed in the hope that it will be useful,\n");
  fprintf (stream,
           "but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
  fprintf (stream,
           "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
  fprintf (stream, "GNU Lesser General Public License for more details.\n");
  fprintf (stream, "\n");
  fprintf (stream,
           "You should have received a copy of the GNU Lesser General Public License\n");
  fprintf (stream,
           "along with this program.  If not, see <https://www.gnu.org/licenses/>.  */\n\n");
}

int
main (int argc, char *argv[])
{
  if (argc != 2)
    {
      fprintf (stderr, " Usage: %s crc-sliceby8.h\n", argv[0]);
      exit (1);
    }

  const char *filename = argv[1];
  FILE *stream = fopen (filename, "w");
  if (stream == NULL)
    {
      fprintf (stderr, "cannot open '%s' for writing\n", filename);
      exit (1);
    }

  print_copyright_notice (stream);
  print_header (stream);

  if (ferror (stream) || fclose (stream))
    {
      fprintf (stderr, "error writing to '%s'\n", filename);
      exit (1);
    }

  return 0;
}