1 /* Compute checksums of files or strings.
2 Copyright (C) 1995-2024 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 Ulrich Drepper <drepper@gnu.ai.mit.edu>. */
22 #include <sys/types.h>
28 #include "xdectoint.h"
31 #ifndef HASH_ALGO_CKSUM
32 # define HASH_ALGO_CKSUM 0
35 #if HASH_ALGO_SUM || HASH_ALGO_CKSUM
42 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
43 # include "blake2/b2sum.h"
45 #if HASH_ALGO_MD5 || HASH_ALGO_CKSUM
48 #if HASH_ALGO_SHA1 || HASH_ALGO_CKSUM
51 #if HASH_ALGO_SHA256 || HASH_ALGO_SHA224 || HASH_ALGO_CKSUM
54 #if HASH_ALGO_SHA512 || HASH_ALGO_SHA384 || HASH_ALGO_CKSUM
62 #include "xbinary-io.h"
64 /* The official name of this program (e.g., no 'g' prefix). */
66 # define PROGRAM_NAME "sum"
67 # define DIGEST_TYPE_STRING "BSD"
68 # define DIGEST_STREAM sumfns[sum_algorithm]
69 # define DIGEST_OUT sum_output_fns[sum_algorithm]
70 # define DIGEST_BITS 16
71 # define DIGEST_ALIGN 4
73 # define MAX_DIGEST_BITS 512
74 # define MAX_DIGEST_ALIGN 8
75 # define PROGRAM_NAME "cksum"
76 # define DIGEST_TYPE_STRING algorithm_tags[cksum_algorithm]
77 # define DIGEST_STREAM cksumfns[cksum_algorithm]
78 # define DIGEST_OUT cksum_output_fns[cksum_algorithm]
79 # define DIGEST_BITS MAX_DIGEST_BITS
80 # define DIGEST_ALIGN MAX_DIGEST_ALIGN
82 # define PROGRAM_NAME "md5sum"
83 # define DIGEST_TYPE_STRING "MD5"
84 # define DIGEST_STREAM md5_stream
85 # define DIGEST_BITS 128
86 # define DIGEST_REFERENCE "RFC 1321"
87 # define DIGEST_ALIGN 4
88 #elif HASH_ALGO_BLAKE2
89 # define PROGRAM_NAME "b2sum"
90 # define DIGEST_TYPE_STRING "BLAKE2b"
91 # define DIGEST_STREAM blake2b_stream
92 # define DIGEST_BITS 512
93 # define DIGEST_REFERENCE "RFC 7693"
94 # define DIGEST_ALIGN 8
96 # define PROGRAM_NAME "sha1sum"
97 # define DIGEST_TYPE_STRING "SHA1"
98 # define DIGEST_STREAM sha1_stream
99 # define DIGEST_BITS 160
100 # define DIGEST_REFERENCE "FIPS-180-1"
101 # define DIGEST_ALIGN 4
102 #elif HASH_ALGO_SHA256
103 # define PROGRAM_NAME "sha256sum"
104 # define DIGEST_TYPE_STRING "SHA256"
105 # define DIGEST_STREAM sha256_stream
106 # define DIGEST_BITS 256
107 # define DIGEST_REFERENCE "FIPS-180-2"
108 # define DIGEST_ALIGN 4
109 #elif HASH_ALGO_SHA224
110 # define PROGRAM_NAME "sha224sum"
111 # define DIGEST_TYPE_STRING "SHA224"
112 # define DIGEST_STREAM sha224_stream
113 # define DIGEST_BITS 224
114 # define DIGEST_REFERENCE "RFC 3874"
115 # define DIGEST_ALIGN 4
116 #elif HASH_ALGO_SHA512
117 # define PROGRAM_NAME "sha512sum"
118 # define DIGEST_TYPE_STRING "SHA512"
119 # define DIGEST_STREAM sha512_stream
120 # define DIGEST_BITS 512
121 # define DIGEST_REFERENCE "FIPS-180-2"
122 # define DIGEST_ALIGN 8
123 #elif HASH_ALGO_SHA384
124 # define PROGRAM_NAME "sha384sum"
125 # define DIGEST_TYPE_STRING "SHA384"
126 # define DIGEST_STREAM sha384_stream
127 # define DIGEST_BITS 384
128 # define DIGEST_REFERENCE "FIPS-180-2"
129 # define DIGEST_ALIGN 8
131 # error "Can't decide which hash algorithm to compile."
133 #if !HASH_ALGO_SUM && !HASH_ALGO_CKSUM
134 # define DIGEST_OUT output_file
139 proper_name ("Kayvan Aghaiepour"), \
140 proper_name ("David MacKenzie")
141 #elif HASH_ALGO_CKSUM
143 proper_name_lite ("Padraig Brady", "P\303\241draig Brady"), \
144 proper_name ("Q. Frank Xia")
145 #elif HASH_ALGO_BLAKE2
147 proper_name_lite ("Padraig Brady", "P\303\241draig Brady"), \
148 proper_name ("Samuel Neves")
151 proper_name ("Ulrich Drepper"), \
152 proper_name ("Scott Miller"), \
153 proper_name ("David Madore")
155 #if !HASH_ALGO_BLAKE2 && !HASH_ALGO_CKSUM
156 # define DIGEST_HEX_BYTES (DIGEST_BITS / 4)
158 #define DIGEST_BIN_BYTES (DIGEST_BITS / 8)
160 /* The minimum length of a valid digest line. This length does
161 not include any newline character at the end of a line. */
162 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
163 # define MIN_DIGEST_LINE_LENGTH 3 /* With -l 8. */
165 # define MIN_DIGEST_LINE_LENGTH \
166 (DIGEST_HEX_BYTES /* length of hexadecimal message digest */ \
168 + 1 /* minimum filename length */ )
173 output_file (char const *file
, int binary_file
, void const *digest
,
174 bool raw
, bool tagged
, unsigned char delim
, bool args
,
178 /* True if any of the files read were the standard input. */
179 static bool have_read_stdin
;
181 /* The minimum length of a valid checksum line for the selected algorithm. */
182 static size_t min_digest_line_length
;
184 /* Set to the length of a digest hex string for the selected algorithm. */
185 static size_t digest_hex_bytes
;
187 /* With --check, don't generate any output.
188 The exit code indicates success or failure. */
189 static bool status_only
= false;
191 /* With --check, print a message to standard error warning about each
192 improperly formatted checksum line. */
193 static bool warn
= false;
195 /* With --check, ignore missing files. */
196 static bool ignore_missing
= false;
198 /* With --check, suppress the "OK" printed for each verified file. */
199 static bool quiet
= false;
201 /* With --check, exit with a non-zero return code if any line is
202 improperly formatted. */
203 static bool strict
= false;
205 /* Whether a BSD reversed format checksum is detected. */
206 static int bsd_reversed
= -1;
208 /* line delimiter. */
209 static unsigned char digest_delim
= '\n';
212 /* If true, print base64-encoded digests, not hex. */
213 static bool base64_digest
= false;
216 /* If true, print binary digests, not hex. */
217 static bool raw_digest
= false;
219 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
220 # define BLAKE2B_MAX_LEN BLAKE2B_OUTBYTES
221 static uintmax_t digest_length
;
222 #endif /* HASH_ALGO_BLAKE2 */
224 typedef void (*digest_output_fn
)(char const *, int, void const *, bool,
225 bool, unsigned char, bool, uintmax_t);
233 static enum Algorithm sum_algorithm
;
234 static sumfn sumfns
[]=
239 static digest_output_fn sum_output_fns
[]=
248 md5_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
250 return md5_stream (stream
, resstream
);
253 sha1_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
255 return sha1_stream (stream
, resstream
);
258 sha224_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
260 return sha224_stream (stream
, resstream
);
263 sha256_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
265 return sha256_stream (stream
, resstream
);
268 sha384_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
270 return sha384_stream (stream
, resstream
);
273 sha512_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
275 return sha512_stream (stream
, resstream
);
278 blake2b_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
280 return blake2b_stream (stream
, resstream
, *length
);
283 sm3_sum_stream (FILE *stream
, void *resstream
, uintmax_t *length
)
285 return sm3_stream (stream
, resstream
);
304 static char const *const algorithm_args
[] =
306 "bsd", "sysv", "crc", "crc32b", "md5", "sha1", "sha224",
307 "sha256", "sha384", "sha512", "blake2b", "sm3", nullptr
309 static enum Algorithm
const algorithm_types
[] =
311 bsd
, sysv
, crc
, crc32b
, md5
, sha1
, sha224
,
312 sha256
, sha384
, sha512
, blake2b
, sm3
,
314 ARGMATCH_VERIFY (algorithm_args
, algorithm_types
);
316 static char const *const algorithm_tags
[] =
318 "BSD", "SYSV", "CRC", "CRC32B", "MD5", "SHA1", "SHA224",
319 "SHA256", "SHA384", "SHA512", "BLAKE2b", "SM3", nullptr
321 static int const algorithm_bits
[] =
323 16, 16, 32, 32, 128, 160, 224,
324 256, 384, 512, 512, 256, 0
327 static_assert (ARRAY_CARDINALITY (algorithm_bits
)
328 == ARRAY_CARDINALITY (algorithm_args
));
330 static bool algorithm_specified
= false;
331 static enum Algorithm cksum_algorithm
= crc
;
332 static sumfn cksumfns
[]=
347 static digest_output_fn cksum_output_fns
[]=
365 /* For long options that have no equivalent short option, use a
366 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
370 IGNORE_MISSING_OPTION
= CHAR_MAX
+ 1,
376 DEBUG_PROGRAM_OPTION
,
381 static struct option
const long_options
[] =
383 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
384 { "length", required_argument
, nullptr, 'l'},
388 { "check", no_argument
, nullptr, 'c' },
389 { "ignore-missing", no_argument
, nullptr, IGNORE_MISSING_OPTION
},
390 { "quiet", no_argument
, nullptr, QUIET_OPTION
},
391 { "status", no_argument
, nullptr, STATUS_OPTION
},
392 { "warn", no_argument
, nullptr, 'w' },
393 { "strict", no_argument
, nullptr, STRICT_OPTION
},
394 { "tag", no_argument
, nullptr, TAG_OPTION
},
395 { "zero", no_argument
, nullptr, 'z' },
398 { "algorithm", required_argument
, nullptr, 'a'},
399 { "base64", no_argument
, nullptr, BASE64_OPTION
},
400 { "debug", no_argument
, nullptr, DEBUG_PROGRAM_OPTION
},
401 { "raw", no_argument
, nullptr, RAW_OPTION
},
402 { "untagged", no_argument
, nullptr, UNTAG_OPTION
},
404 { "binary", no_argument
, nullptr, 'b' },
405 { "text", no_argument
, nullptr, 't' },
408 {"sysv", no_argument
, nullptr, 's'},
411 { GETOPT_HELP_OPTION_DECL
},
412 { GETOPT_VERSION_OPTION_DECL
},
413 { nullptr, 0, nullptr, 0 }
419 if (status
!= EXIT_SUCCESS
)
424 Usage: %s [OPTION]... [FILE]...\n\
428 Print or verify checksums.\n\
429 By default use the 32 bit CRC algorithm.\n\
433 Print or check %s (%d-bit) checksums.\n\
443 -r use BSD sum algorithm (the default), use 1K blocks\n\
444 -s, --sysv use System V sum algorithm, use 512 bytes blocks\n\
447 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
448 emit_mandatory_arg_note ();
452 -a, --algorithm=TYPE select the digest type to use. See DIGEST below\
456 --base64 emit base64-encoded digests, not hexadecimal\
461 # if !HASH_ALGO_CKSUM
464 -b, --binary read in binary mode (default unless reading tty stdin)\
469 -b, --binary read in binary mode\n\
473 -c, --check read checksums from the FILEs and check them\n\
475 # if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
477 -l, --length=BITS digest length in bits; must not exceed the max for\n\
478 the blake2 algorithm and must be a multiple of 8\n\
483 --raw emit a raw binary digest, not hexadecimal\
487 --tag create a BSD-style checksum (the default)\n\
490 --untagged create a reversed style checksum, without digest type\n\
494 --tag create a BSD-style checksum\n\
497 # if !HASH_ALGO_CKSUM
500 -t, --text read in text mode (default if reading tty stdin)\n\
504 -t, --text read in text mode (default)\n\
508 -z, --zero end each output line with NUL, not newline,\n\
509 and disable file name escaping\n\
513 The following five options are useful only when verifying checksums:\n\
514 --ignore-missing don't fail or report status for missing files\n\
515 --quiet don't print OK for each successfully verified file\n\
516 --status don't output anything, status code shows success\n\
517 --strict exit non-zero for improperly formatted checksum lines\n\
518 -w, --warn warn about improperly formatted checksum lines\n\
524 --debug indicate which implementation used\n\
527 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
528 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
532 DIGEST determines the digest algorithm and default output format:\n\
533 sysv (equivalent to sum -s)\n\
534 bsd (equivalent to sum -r)\n\
535 crc (equivalent to cksum)\n\
536 crc32b (only available through cksum)\n\
537 md5 (equivalent to md5sum)\n\
538 sha1 (equivalent to sha1sum)\n\
539 sha224 (equivalent to sha224sum)\n\
540 sha256 (equivalent to sha256sum)\n\
541 sha384 (equivalent to sha384sum)\n\
542 sha512 (equivalent to sha512sum)\n\
543 blake2b (equivalent to b2sum)\n\
544 sm3 (only available through cksum)\n\
547 #if !HASH_ALGO_SUM && !HASH_ALGO_CKSUM
550 The sums are computed as described in %s.\n"), DIGEST_REFERENCE
);
552 When checking, the input should be a former output of this program.\n\
553 The default mode is to print a line with: checksum, a space,\n\
554 a character indicating input mode ('*' for binary, ' ' for text\n\
555 or where binary is insignificant), and name for each FILE.\n\
557 There is no difference between binary mode and text mode on GNU systems.\
562 When checking, the input should be a former output of this program,\n\
563 or equivalent standalone program.\
566 emit_ancillary_info (PROGRAM_NAME
);
572 /* Given a string S, return TRUE if it contains problematic characters
573 that need escaping. Note we escape '\' itself to provide some forward
574 compat to introduce escaping of other characters. */
578 problematic_chars (char const *s
)
580 size_t length
= strcspn (s
, "\\\n\r");
581 return s
[length
] != '\0';
584 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
586 /* Given a file name, S of length S_LEN, that is not NUL-terminated,
587 modify it in place, performing the equivalent of this sed substitution:
588 's/\\n/\n/g;s/\\r/\r/g;s/\\\\/\\/g' i.e., replacing each "\\n" string
589 with a newline, each "\\r" string with a carriage return,
590 and each "\\\\" with a single backslash, NUL-terminate it and return S.
591 If S is not a valid escaped file name, i.e., if it ends with an odd number
592 of backslashes or if it contains a backslash followed by anything other
593 than "n" or another backslash, return nullptr. */
596 filename_unescape (char *s
, size_t s_len
)
600 for (size_t i
= 0; i
< s_len
; i
++)
607 /* File name ends with an unescaped backslash: invalid. */
623 /* Only '\', 'n' or 'r' may follow a backslash. */
629 /* The file name may not contain a NUL. */
643 /* Return true if S is a LEN-byte NUL-terminated string of hex or base64
644 digits and has the expected length. Otherwise, return false. */
647 valid_digits (unsigned char const *s
, size_t len
)
650 if (len
== BASE64_LENGTH (digest_length
/ 8))
653 for (i
= 0; i
< len
- digest_length
% 3; i
++)
659 for ( ; i
< len
; i
++)
668 if (len
== digest_hex_bytes
)
670 for (idx_t i
= 0; i
< digest_hex_bytes
; i
++)
672 if (!c_isxdigit (*s
))
683 /* Split the checksum string S (of length S_LEN) from a BSD 'md5' or
684 'sha1' command into two parts: a hexadecimal digest, and the file
685 name. S is modified. Set *D_LEN to the length of the digest string.
686 Return true if successful. */
689 bsd_split_3 (char *s
, size_t s_len
,
690 unsigned char **digest
, size_t *d_len
,
691 char **file_name
, bool escaped_filename
)
696 /* Find end of filename. */
697 size_t i
= s_len
- 1;
698 while (i
&& s
[i
] != ')')
706 if (escaped_filename
&& filename_unescape (s
, i
) == nullptr)
711 while (ISWHITE (s
[i
]))
719 while (ISWHITE (s
[i
]))
722 *digest
= (unsigned char *) &s
[i
];
725 return valid_digits (*digest
, *d_len
);
729 /* Return the corresponding Algorithm for the string S,
730 or -1 for no match. */
733 algorithm_from_tag (char *s
)
735 /* Limit check size to this length for perf reasons. */
736 static size_t max_tag_len
;
739 char const * const * tag
= algorithm_tags
;
742 size_t tag_len
= strlen (*tag
++);
743 max_tag_len
= MAX (tag_len
, max_tag_len
);
749 /* Find end of tag */
750 while (i
<= max_tag_len
&& s
[i
] && ! ISWHITE (s
[i
])
751 && s
[i
] != '-' && s
[i
] != '(')
757 /* Terminate tag, and lookup. */
760 ptrdiff_t algo
= argmatch_exact (s
, algorithm_tags
);
767 /* Split the string S (of length S_LEN) into three parts:
768 a hexadecimal digest, binary flag, and the file name.
769 S is modified. Set *D_LEN to the length of the digest string.
770 Return true if successful. */
773 split_3 (char *s
, size_t s_len
,
774 unsigned char **digest
, size_t *d_len
, int *binary
, char **file_name
)
776 bool escaped_filename
= false;
777 size_t algo_name_len
;
780 while (ISWHITE (s
[i
]))
786 escaped_filename
= true;
789 /* Check for BSD-style checksum line. */
792 if (! algorithm_specified
)
794 ptrdiff_t algo_tag
= algorithm_from_tag (s
+ i
);
797 if (algo_tag
<= crc32b
)
798 return false; /* We don't support checking these older formats. */
799 cksum_algorithm
= algo_tag
;
802 return false; /* We only support tagged format without -a. */
806 algo_name_len
= strlen (DIGEST_TYPE_STRING
);
807 if (STREQ_LEN (s
+ i
, DIGEST_TYPE_STRING
, algo_name_len
))
810 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
811 /* Terminate and match algorithm name. */
812 char const *algo_name
= &s
[i
- algo_name_len
];
813 bool length_specified
= s
[i
] == '-';
814 bool openssl_format
= s
[i
] == '('; /* and no length_specified */
816 if (!STREQ (algo_name
, DIGEST_TYPE_STRING
))
821 # if HASH_ALGO_BLAKE2
822 digest_length
= BLAKE2B_MAX_LEN
* 8;
824 digest_length
= algorithm_bits
[cksum_algorithm
];
826 if (length_specified
)
830 if (! (xstrtoumax (s
+ i
, &siend
, 0, &length
, nullptr) == LONGINT_OK
831 && 0 < length
&& length
<= digest_length
836 digest_length
= length
;
838 digest_hex_bytes
= digest_length
/ 4;
846 return bsd_split_3 (s
+ i
, s_len
- i
,
847 digest
, d_len
, file_name
, escaped_filename
);
852 /* Ignore this line if it is too short.
853 Each line must have at least 'min_digest_line_length - 1' (or one more, if
854 the first is a backslash) more characters to contain correct message digest
856 if (s_len
- i
< min_digest_line_length
+ (s
[i
] == '\\'))
859 *digest
= (unsigned char *) &s
[i
];
861 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
862 /* Auto determine length. */
864 if (cksum_algorithm
== blake2b
) {
866 unsigned char const *hp
= *digest
;
867 digest_hex_bytes
= 0;
868 while (c_isxdigit (*hp
++))
870 if (digest_hex_bytes
< 2 || digest_hex_bytes
% 2
871 || BLAKE2B_MAX_LEN
* 2 < digest_hex_bytes
)
873 digest_length
= digest_hex_bytes
* 4;
879 /* This field must be the hexadecimal or base64 representation
880 of the message digest. */
881 while (s
[i
] && !ISWHITE (s
[i
]))
884 /* The digest must be followed by at least one whitespace character. */
888 *d_len
= &s
[i
] - (char *) *digest
;
891 if (! valid_digits (*digest
, *d_len
))
894 /* If "bsd reversed" format detected. */
895 if ((s_len
- i
== 1) || (s
[i
] != ' ' && s
[i
] != '*'))
897 /* Don't allow mixing bsd and standard formats,
898 to minimize security issues with attackers
899 renaming files with leading spaces.
900 This assumes that with bsd format checksums
901 that the first file name does not have
902 a leading ' ' or '*'. */
903 if (bsd_reversed
== 0)
907 else if (bsd_reversed
!= 1)
910 *binary
= (s
[i
++] == '*');
913 /* All characters between the type indicator and end of line are
914 significant -- that includes leading and trailing white space. */
917 if (escaped_filename
)
918 return filename_unescape (&s
[i
], s_len
- i
) != nullptr;
923 /* If ESCAPE is true, then translate each:
924 NEWLINE byte to the string, "\\n",
925 CARRIAGE RETURN byte to the string, "\\r",
926 and each backslash to "\\\\". */
928 print_filename (char const *file
, bool escape
)
932 fputs (file
, stdout
);
941 fputs ("\\n", stdout
);
945 fputs ("\\r", stdout
);
949 fputs ("\\\\", stdout
);
960 /* An interface to the function, DIGEST_STREAM.
961 Operate on FILENAME (it may be "-").
963 *BINARY indicates whether the file is binary. BINARY < 0 means it
964 depends on whether binary mode makes any difference and the file is
965 a terminal; in that case, clear *BINARY if the file was treated as
966 text because it was a terminal.
968 Put the checksum in *BIN_RESULT, which must be properly aligned.
969 Put true in *MISSING if the file can't be opened due to ENOENT.
970 Return true if successful. */
973 digest_file (char const *filename
, int *binary
, unsigned char *bin_result
,
974 bool *missing
, MAYBE_UNUSED
uintmax_t *length
)
978 bool is_stdin
= STREQ (filename
, "-");
984 have_read_stdin
= true;
986 if (O_BINARY
&& *binary
)
989 *binary
= ! isatty (STDIN_FILENO
);
991 xset_binary_mode (STDIN_FILENO
, O_BINARY
);
996 fp
= fopen (filename
, (O_BINARY
&& *binary
? "rb" : "r"));
999 if (ignore_missing
&& errno
== ENOENT
)
1004 error (0, errno
, "%s", quotef (filename
));
1009 fadvise (fp
, FADVISE_SEQUENTIAL
);
1012 if (cksum_algorithm
== blake2b
)
1013 *length
= digest_length
/ 8;
1014 err
= DIGEST_STREAM (fp
, bin_result
, length
);
1016 err
= DIGEST_STREAM (fp
, bin_result
, length
);
1017 #elif HASH_ALGO_BLAKE2
1018 err
= DIGEST_STREAM (fp
, bin_result
, digest_length
/ 8);
1020 err
= DIGEST_STREAM (fp
, bin_result
);
1022 err
= err
? errno
: 0;
1025 else if (fclose (fp
) != 0 && !err
)
1030 error (0, err
, "%s", quotef (filename
));
1039 output_file (char const *file
, int binary_file
, void const *digest
,
1040 bool raw
, bool tagged
, unsigned char delim
, MAYBE_UNUSED
bool args
,
1041 MAYBE_UNUSED
uintmax_t length
)
1043 # if HASH_ALGO_CKSUM
1046 fwrite (digest
, 1, digest_length
/ 8, stdout
);
1051 unsigned char const *bin_buffer
= digest
;
1053 /* Output a leading backslash if the file name contains problematic chars. */
1054 bool needs_escape
= delim
== '\n' && problematic_chars (file
);
1061 fputs (DIGEST_TYPE_STRING
, stdout
);
1062 # if HASH_ALGO_BLAKE2
1063 if (digest_length
< BLAKE2B_MAX_LEN
* 8)
1064 printf ("-%ju", digest_length
);
1065 # elif HASH_ALGO_CKSUM
1066 if (cksum_algorithm
== blake2b
)
1068 if (digest_length
< BLAKE2B_MAX_LEN
* 8)
1069 printf ("-%ju", digest_length
);
1072 fputs (" (", stdout
);
1073 print_filename (file
, needs_escape
);
1074 fputs (") = ", stdout
);
1077 # if HASH_ALGO_CKSUM
1080 char b64
[BASE64_LENGTH (DIGEST_BIN_BYTES
) + 1];
1081 base64_encode ((char const *) bin_buffer
, digest_length
/ 8,
1083 fputs (b64
, stdout
);
1088 for (size_t i
= 0; i
< (digest_hex_bytes
/ 2); ++i
)
1089 printf ("%02x", bin_buffer
[i
]);
1095 putchar (binary_file
? '*' : ' ');
1096 print_filename (file
, needs_escape
);
1104 /* Return true if B64_DIGEST is the same as the base64 digest of the
1105 DIGEST_LENGTH/8 bytes at BIN_BUFFER. */
1107 b64_equal (unsigned char const *b64_digest
, unsigned char const *bin_buffer
)
1109 size_t b64_n_bytes
= BASE64_LENGTH (digest_length
/ 8);
1110 char b64
[BASE64_LENGTH (DIGEST_BIN_BYTES
) + 1];
1111 base64_encode ((char const *) bin_buffer
, digest_length
/ 8, b64
, sizeof b64
);
1112 return memcmp (b64_digest
, b64
, b64_n_bytes
+ 1) == 0;
1116 /* Return true if HEX_DIGEST is the same as the hex-encoded digest of the
1117 DIGEST_LENGTH/8 bytes at BIN_BUFFER. */
1119 hex_equal (unsigned char const *hex_digest
, unsigned char const *bin_buffer
)
1121 static const char bin2hex
[] = { '0', '1', '2', '3',
1124 'c', 'd', 'e', 'f' };
1125 size_t digest_bin_bytes
= digest_hex_bytes
/ 2;
1127 /* Compare generated binary number with text representation
1128 in check file. Ignore case of hex digits. */
1130 for (cnt
= 0; cnt
< digest_bin_bytes
; ++cnt
)
1132 if (c_tolower (hex_digest
[2 * cnt
])
1133 != bin2hex
[bin_buffer
[cnt
] >> 4]
1134 || (c_tolower (hex_digest
[2 * cnt
+ 1])
1135 != (bin2hex
[bin_buffer
[cnt
] & 0xf])))
1138 return cnt
== digest_bin_bytes
;
1142 digest_check (char const *checkfile_name
)
1144 FILE *checkfile_stream
;
1145 uintmax_t n_misformatted_lines
= 0;
1146 uintmax_t n_mismatched_checksums
= 0;
1147 uintmax_t n_open_or_read_failures
= 0;
1148 bool properly_formatted_lines
= false;
1149 bool matched_checksums
= false;
1150 unsigned char bin_buffer_unaligned
[DIGEST_BIN_BYTES
+ DIGEST_ALIGN
];
1151 /* Make sure bin_buffer is properly aligned. */
1152 unsigned char *bin_buffer
= ptr_align (bin_buffer_unaligned
, DIGEST_ALIGN
);
1153 uintmax_t line_number
;
1155 size_t line_chars_allocated
;
1156 bool is_stdin
= STREQ (checkfile_name
, "-");
1160 have_read_stdin
= true;
1161 checkfile_name
= _("standard input");
1162 checkfile_stream
= stdin
;
1166 checkfile_stream
= fopen (checkfile_name
, "r");
1167 if (checkfile_stream
== nullptr)
1169 error (0, errno
, "%s", quotef (checkfile_name
));
1176 line_chars_allocated
= 0;
1181 unsigned char *digest
;
1182 ssize_t line_length
;
1185 if (line_number
== 0)
1186 error (EXIT_FAILURE
, 0, _("%s: too many checksum lines"),
1187 quotef (checkfile_name
));
1189 line_length
= getline (&line
, &line_chars_allocated
, checkfile_stream
);
1190 if (line_length
<= 0)
1193 /* Ignore comment lines, which begin with a '#' character. */
1197 /* Remove any trailing newline. */
1198 line_length
-= line
[line_length
- 1] == '\n';
1199 /* Remove any trailing carriage return. */
1200 line_length
-= line
[line_length
- (0 < line_length
)] == '\r';
1202 /* Ignore empty lines. */
1203 if (line_length
== 0)
1206 line
[line_length
] = '\0';
1209 if (! (split_3 (line
, line_length
, &digest
, &d_len
, &binary
, &filename
)
1210 && ! (is_stdin
&& STREQ (filename
, "-"))))
1212 ++n_misformatted_lines
;
1218 ": improperly formatted %s checksum line"),
1219 quotef (checkfile_name
), line_number
,
1220 DIGEST_TYPE_STRING
);
1227 bool needs_escape
= ! status_only
&& problematic_chars (filename
);
1229 properly_formatted_lines
= true;
1232 ok
= digest_file (filename
, &binary
, bin_buffer
, &missing
, &length
);
1236 ++n_open_or_read_failures
;
1241 print_filename (filename
, needs_escape
);
1242 printf (": %s\n", _("FAILED open or read"));
1245 else if (ignore_missing
&& missing
)
1247 /* Ignore missing files with --ignore-missing. */
1254 if (d_len
< digest_hex_bytes
)
1255 match
= b64_equal (digest
, bin_buffer
);
1258 if (d_len
== digest_hex_bytes
)
1259 match
= hex_equal (digest
, bin_buffer
);
1262 matched_checksums
= true;
1264 ++n_mismatched_checksums
;
1268 if (! match
|| ! quiet
)
1272 print_filename (filename
, needs_escape
);
1276 printf (": %s\n", _("FAILED"));
1278 printf (": %s\n", _("OK"));
1283 while (!feof (checkfile_stream
) && !ferror (checkfile_stream
));
1287 int err
= ferror (checkfile_stream
) ? 0 : -1;
1289 clearerr (checkfile_stream
);
1290 else if (fclose (checkfile_stream
) != 0 && err
< 0)
1295 error (0, err
, err
? "%s" : _("%s: read error"),
1296 quotef (checkfile_name
));
1300 if (! properly_formatted_lines
)
1302 /* Warn if no tests are found. */
1303 error (0, 0, _("%s: no properly formatted checksum lines found"),
1304 quotef (checkfile_name
));
1310 if (n_misformatted_lines
!= 0)
1313 ("WARNING: %ju line is improperly formatted",
1314 "WARNING: %ju lines are improperly formatted",
1315 select_plural (n_misformatted_lines
))),
1316 n_misformatted_lines
);
1318 if (n_open_or_read_failures
!= 0)
1321 ("WARNING: %ju listed file could not be read",
1322 "WARNING: %ju listed files could not be read",
1323 select_plural (n_open_or_read_failures
))),
1324 n_open_or_read_failures
);
1326 if (n_mismatched_checksums
!= 0)
1329 ("WARNING: %ju computed checksum did NOT match",
1330 "WARNING: %ju computed checksums did NOT match",
1331 select_plural (n_mismatched_checksums
))),
1332 n_mismatched_checksums
);
1334 if (ignore_missing
&& ! matched_checksums
)
1335 error (0, 0, _("%s: no file was verified"),
1336 quotef (checkfile_name
));
1340 return (properly_formatted_lines
1341 && matched_checksums
1342 && n_mismatched_checksums
== 0
1343 && n_open_or_read_failures
== 0
1344 && (!strict
|| n_misformatted_lines
== 0));
1348 main (int argc
, char **argv
)
1350 unsigned char bin_buffer_unaligned
[DIGEST_BIN_BYTES
+ DIGEST_ALIGN
];
1351 /* Make sure bin_buffer is properly aligned. */
1352 unsigned char *bin_buffer
= ptr_align (bin_buffer_unaligned
, DIGEST_ALIGN
);
1353 bool do_check
= false;
1357 int prefix_tag
= -1;
1359 /* Setting values of global variables. */
1360 initialize_main (&argc
, &argv
);
1361 set_program_name (argv
[0]);
1362 setlocale (LC_ALL
, "");
1363 bindtextdomain (PACKAGE
, LOCALEDIR
);
1364 textdomain (PACKAGE
);
1366 atexit (close_stdout
);
1368 /* Line buffer stdout to ensure lines are written atomically and immediately
1369 so that processes running in parallel do not intersperse their output. */
1370 setvbuf (stdout
, nullptr, _IOLBF
, 0);
1373 char const *short_opts
= "rs";
1374 #elif HASH_ALGO_CKSUM
1375 char const *short_opts
= "a:l:bctwz";
1376 char const *digest_length_str
= "";
1377 #elif HASH_ALGO_BLAKE2
1378 char const *short_opts
= "l:bctwz";
1379 char const *digest_length_str
= "";
1381 char const *short_opts
= "bctwz";
1384 while ((opt
= getopt_long (argc
, argv
, short_opts
, long_options
, nullptr))
1390 cksum_algorithm
= XARGMATCH_EXACT ("--algorithm", optarg
,
1391 algorithm_args
, algorithm_types
);
1392 algorithm_specified
= true;
1395 case DEBUG_PROGRAM_OPTION
:
1399 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
1401 digest_length
= xnumtoumax (optarg
, 10, 0, BLAKE2B_MAX_LEN
* 8, "",
1402 _("invalid length"), 0,
1404 digest_length_str
= optarg
;
1423 status_only
= false;
1427 case IGNORE_MISSING_OPTION
:
1428 ignore_missing
= true;
1431 status_only
= false;
1438 # if HASH_ALGO_CKSUM
1440 base64_digest
= true;
1446 if (prefix_tag
== 1)
1456 digest_delim
= '\0';
1460 case 'r': /* For SysV compatibility. */
1461 sum_algorithm
= bsd
;
1465 sum_algorithm
= sysv
;
1468 case_GETOPT_HELP_CHAR
;
1469 case_GETOPT_VERSION_CHAR (PROGRAM_NAME
, AUTHORS
);
1471 usage (EXIT_FAILURE
);
1474 min_digest_line_length
= MIN_DIGEST_LINE_LENGTH
;
1475 #if HASH_ALGO_BLAKE2 || HASH_ALGO_CKSUM
1476 # if HASH_ALGO_CKSUM
1477 if (digest_length
&& cksum_algorithm
!= blake2b
)
1478 error (EXIT_FAILURE
, 0,
1479 _("--length is only supported with --algorithm=blake2b"));
1481 if (digest_length
> BLAKE2B_MAX_LEN
* 8)
1483 error (0, 0, _("invalid length: %s"), quote (digest_length_str
));
1484 error (EXIT_FAILURE
, 0,
1485 _("maximum digest length for %s is %d bits"),
1486 quote (DIGEST_TYPE_STRING
),
1487 BLAKE2B_MAX_LEN
* 8);
1489 if (digest_length
% 8 != 0)
1491 error (0, 0, _("invalid length: %s"), quote (digest_length_str
));
1492 error (EXIT_FAILURE
, 0, _("length is not a multiple of 8"));
1494 if (digest_length
== 0)
1496 # if HASH_ALGO_BLAKE2
1497 digest_length
= BLAKE2B_MAX_LEN
* 8;
1499 digest_length
= algorithm_bits
[cksum_algorithm
];
1502 digest_hex_bytes
= digest_length
/ 4;
1504 digest_hex_bytes
= DIGEST_HEX_BYTES
;
1508 switch (cksum_algorithm
)
1514 if (do_check
&& algorithm_specified
)
1515 error (EXIT_FAILURE
, 0,
1516 _("--check is not supported with "
1517 "--algorithm={bsd,sysv,crc,crc32b}"));
1523 if (base64_digest
&& raw_digest
)
1525 error (0, 0, _("--base64 and --raw are mutually exclusive"));
1526 usage (EXIT_FAILURE
);
1530 if (prefix_tag
== -1)
1531 prefix_tag
= HASH_ALGO_CKSUM
;
1533 if (prefix_tag
&& !binary
)
1535 /* This could be supported in a backwards compatible way
1536 by prefixing the output line with a space in text mode.
1537 However that's invasive enough that it was agreed to
1538 not support this mode with --tag, as --text use cases
1539 are adequately supported by the default output format. */
1540 #if !HASH_ALGO_CKSUM
1541 error (0, 0, _("--tag does not support --text mode"));
1543 error (0, 0, _("--text mode is only supported with --untagged"));
1545 usage (EXIT_FAILURE
);
1548 if (digest_delim
!= '\n' && do_check
)
1550 error (0, 0, _("the --zero option is not supported when "
1551 "verifying checksums"));
1552 usage (EXIT_FAILURE
);
1554 #if !HASH_ALGO_CKSUM
1555 if (prefix_tag
&& do_check
)
1557 error (0, 0, _("the --tag option is meaningless when "
1558 "verifying checksums"));
1559 usage (EXIT_FAILURE
);
1563 if (0 <= binary
&& do_check
)
1565 error (0, 0, _("the --binary and --text options are meaningless when "
1566 "verifying checksums"));
1567 usage (EXIT_FAILURE
);
1570 if (ignore_missing
&& !do_check
)
1573 _("the --ignore-missing option is meaningful only when "
1574 "verifying checksums"));
1575 usage (EXIT_FAILURE
);
1578 if (status_only
&& !do_check
)
1581 _("the --status option is meaningful only when verifying checksums"));
1582 usage (EXIT_FAILURE
);
1585 if (warn
&& !do_check
)
1588 _("the --warn option is meaningful only when verifying checksums"));
1589 usage (EXIT_FAILURE
);
1592 if (quiet
&& !do_check
)
1595 _("the --quiet option is meaningful only when verifying checksums"));
1596 usage (EXIT_FAILURE
);
1599 if (strict
& !do_check
)
1602 _("the --strict option is meaningful only when verifying checksums"));
1603 usage (EXIT_FAILURE
);
1606 if (!O_BINARY
&& binary
< 0)
1609 char **operand_lim
= argv
+ argc
;
1611 *operand_lim
++ = bad_cast ("-");
1612 else if (1 < argc
- optind
&& raw_digest
)
1613 error (EXIT_FAILURE
, 0,
1614 _("the --raw option is not supported with multiple files"));
1616 for (char **operandp
= argv
+ optind
; operandp
< operand_lim
; operandp
++)
1618 char *file
= *operandp
;
1620 ok
&= digest_check (file
);
1623 int binary_file
= binary
;
1627 if (! digest_file (file
, &binary_file
, bin_buffer
, &missing
, &length
))
1631 DIGEST_OUT (file
, binary_file
, bin_buffer
, raw_digest
, prefix_tag
,
1632 digest_delim
, optind
!= argc
, length
);
1637 if (have_read_stdin
&& fclose (stdin
) == EOF
)
1638 error (EXIT_FAILURE
, errno
, _("standard input"));
1640 return ok
? EXIT_SUCCESS
: EXIT_FAILURE
;