split: port ‘split -n N /dev/null’ better to macOS
[coreutils.git] / src / cksum.c
blob5e38fef3a7ec864aa4ba0217bf799d8865f46e1f
1 /* cksum -- calculate and print POSIX checksums and sizes of files
2 Copyright (C) 1992-2023 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Q. Frank Xia, qx@math.columbia.edu.
18 Cosmetic changes and reorganization by David MacKenzie, djm@gnu.ai.mit.edu.
20 Usage: cksum [file...]
22 The code segment between "#ifdef CRCTAB" and "#else" is the code
23 which calculates the "crctab". It is included for those who want
24 verify the correctness of the "crctab". To recreate the "crctab",
25 do something like the following:
27 cc -I../lib -DCRCTAB -o crctab cksum.c
28 crctab > crctab.c
30 This software is compatible with neither the System V nor the BSD
31 'sum' program. It is supposed to conform to POSIX, except perhaps
32 for foreign language support. Any inconsistency with the standard
33 (other than foreign language support) is a bug. */
35 #include <config.h>
37 #include <stdio.h>
38 #include <sys/types.h>
39 #include <stdint.h>
40 #include "system.h"
42 #include <byteswap.h>
43 #ifdef WORDS_BIGENDIAN
44 # define SWAP(n) (n)
45 #else
46 # define SWAP(n) bswap_32 (n)
47 #endif
49 #ifdef CRCTAB
51 # define BIT(x) ((uint_fast32_t) 1 << (x))
52 # define SBIT BIT (31)
54 /* The generating polynomial is
56 32 26 23 22 16 12 11 10 8 7 5 4 2 1
57 G(X)=X + X + X + X + X + X + X + X + X + X + X + X + X + X + 1
59 The i bit in GEN is set if X^i is a summand of G(X) except X^32. */
61 # define GEN (BIT (26) | BIT (23) | BIT (22) | BIT (16) | BIT (12) \
62 | BIT (11) | BIT (10) | BIT (8) | BIT (7) | BIT (5) \
63 | BIT (4) | BIT (2) | BIT (1) | BIT (0))
65 static uint_fast32_t r[8];
67 static void
68 fill_r (void)
70 r[0] = GEN;
71 for (int i = 1; i < 8; i++)
72 r[i] = (r[i - 1] << 1) ^ ((r[i - 1] & SBIT) ? GEN : 0);
75 static uint_fast32_t
76 crc_remainder (int m)
78 uint_fast32_t rem = 0;
80 for (int i = 0; i < 8; i++)
81 if (BIT (i) & m)
82 rem ^= r[i];
84 return rem & 0xFFFFFFFF; /* Make it run on 64-bit machine. */
87 int
88 main (void)
90 int i;
91 static uint_fast32_t crctab[8][256];
93 fill_r ();
95 for (i = 0; i < 256; i++)
97 crctab[0][i] = crc_remainder (i);
100 /* CRC(0x11 0x22 0x33 0x44)
101 is equal to
102 CRC(0x11 0x00 0x00 0x00) XOR CRC(0x22 0x00 0x00) XOR
103 CRC(0x33 0x00) XOR CRC(0x44)
104 We precompute the CRC values for the offset values into
105 separate CRC tables. We can then use them to speed up
106 CRC calculation by processing multiple bytes at the time. */
107 for (i = 0; i < 256; i++)
109 uint32_t crc = 0;
111 crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ (i & 0xFF)) & 0xFF];
112 for (unsigned int offset = 1; offset < 8; offset++)
114 crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ 0x00) & 0xFF];
115 crctab[offset][i] = crc;
119 printf ("#include <stdint.h>\n\n");
120 printf ("uint_fast32_t const crctab[8][256] = {\n");
121 for (int y = 0; y < 8; y++)
123 printf ("{\n 0x%08x", crctab[y][0]);
124 for (i = 0; i < 51; i++)
126 printf (",\n 0x%08x, 0x%08x, 0x%08x, 0x%08x, 0x%08x",
127 crctab[y][i * 5 + 1], crctab[y][i * 5 + 2],
128 crctab[y][i * 5 + 3], crctab[y][i * 5 + 4],
129 crctab[y][i * 5 + 5]);
131 printf ("\n},\n");
133 printf ("};\n");
134 return EXIT_SUCCESS;
137 #else /* !CRCTAB */
139 # include "die.h"
140 # include "error.h"
142 # include "cksum.h"
143 # if USE_PCLMUL_CRC32
144 # include "cpuid.h"
145 # else
146 # define cksum_pclmul cksum_slice8
147 # endif /* USE_PCLMUL_CRC32 */
149 /* Number of bytes to read at once. */
150 # define BUFLEN (1 << 16)
153 static bool
154 cksum_slice8 (FILE *fp, uint_fast32_t *crc_out, uintmax_t *length_out);
155 static bool
156 (*cksum_fp)(FILE *, uint_fast32_t *, uintmax_t *);
158 static bool
159 pclmul_supported (void)
161 # if USE_PCLMUL_CRC32
162 unsigned int eax = 0;
163 unsigned int ebx = 0;
164 unsigned int ecx = 0;
165 unsigned int edx = 0;
167 if (! __get_cpuid (1, &eax, &ebx, &ecx, &edx))
169 if (cksum_debug)
170 error (0, 0, "%s", _("failed to get cpuid"));
171 return false;
174 if (! (ecx & bit_PCLMUL) || ! (ecx & bit_AVX))
176 if (cksum_debug)
177 error (0, 0, "%s", _("pclmul support not detected"));
178 return false;
181 if (cksum_debug)
182 error (0, 0, "%s", _("using pclmul hardware support"));
184 return true;
185 # else
186 if (cksum_debug)
187 error (0, 0, "%s", _("using generic hardware support"));
188 return false;
189 # endif /* USE_PCLMUL_CRC32 */
192 static bool
193 cksum_slice8 (FILE *fp, uint_fast32_t *crc_out, uintmax_t *length_out)
195 uint32_t buf[BUFLEN / sizeof (uint32_t)];
196 uint_fast32_t crc = 0;
197 uintmax_t length = 0;
198 size_t bytes_read;
200 if (!fp || !crc_out || !length_out)
201 return false;
203 while ((bytes_read = fread (buf, 1, BUFLEN, fp)) > 0)
205 uint32_t *datap;
207 if (length + bytes_read < length)
209 errno = EOVERFLOW;
210 return false;
212 length += bytes_read;
214 if (bytes_read == 0)
216 if (ferror (fp))
217 return false;
220 /* Process multiples of 8 bytes */
221 datap = (uint32_t *)buf;
222 while (bytes_read >= 8)
224 uint32_t first = *datap++, second = *datap++;
225 crc ^= SWAP (first);
226 second = SWAP (second);
227 crc = (crctab[7][(crc >> 24) & 0xFF]
228 ^ crctab[6][(crc >> 16) & 0xFF]
229 ^ crctab[5][(crc >> 8) & 0xFF]
230 ^ crctab[4][(crc) & 0xFF]
231 ^ crctab[3][(second >> 24) & 0xFF]
232 ^ crctab[2][(second >> 16) & 0xFF]
233 ^ crctab[1][(second >> 8) & 0xFF]
234 ^ crctab[0][(second) & 0xFF]);
235 bytes_read -= 8;
238 /* And finish up last 0-7 bytes in a byte by byte fashion */
239 unsigned char *cp = (unsigned char *)datap;
240 while (bytes_read--)
241 crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ *cp++) & 0xFF];
242 if (feof (fp))
243 break;
246 *crc_out = crc;
247 *length_out = length;
249 return true;
252 /* Calculate the checksum and length in bytes of stream STREAM.
253 Return -1 on error, 0 on success. */
256 crc_sum_stream (FILE *stream, void *resstream, uintmax_t *length)
258 uintmax_t total_bytes = 0;
259 uint_fast32_t crc = 0;
261 if (! cksum_fp)
263 if (pclmul_supported ())
264 cksum_fp = cksum_pclmul;
265 else
266 cksum_fp = cksum_slice8;
269 if (! cksum_fp (stream, &crc, &total_bytes))
270 return -1;
272 *length = total_bytes;
274 for (; total_bytes; total_bytes >>= 8)
275 crc = (crc << 8) ^ crctab[0][((crc >> 24) ^ total_bytes) & 0xFF];
276 crc = ~crc & 0xFFFFFFFF;
278 unsigned int crc_out = crc;
279 memcpy (resstream, &crc_out, sizeof crc_out);
281 return 0;
284 /* Print the checksum and size to stdout.
285 If ARGS is true, also print the FILE name. */
287 void
288 output_crc (char const *file, int binary_file, void const *digest, bool raw,
289 bool tagged, unsigned char delim, bool args, uintmax_t length)
291 if (raw)
293 /* Output in network byte order (big endian). */
294 uint32_t out_int = SWAP (*(uint32_t *)digest);
295 fwrite (&out_int, 1, 32/8, stdout);
296 return;
299 char length_buf[INT_BUFSIZE_BOUND (uintmax_t)];
300 printf ("%u %s", *(unsigned int *)digest, umaxtostr (length, length_buf));
301 if (args)
302 printf (" %s", file);
303 putchar (delim);
306 #endif /* !CRCTAB */