tests: pwd-long: diagnose failure earlier
[coreutils/ericb.git] / src / md5sum.c
blobff9538a280765fe96775906315cad14d66bfdce6
1 /* Compute checksums of files or strings.
2 Copyright (C) 1995-2011 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>. */
19 #include <config.h>
21 #include <getopt.h>
22 #include <sys/types.h>
24 #include "system.h"
26 #if HASH_ALGO_MD5
27 # include "md5.h"
28 #endif
29 #if HASH_ALGO_SHA1
30 # include "sha1.h"
31 #endif
32 #if HASH_ALGO_SHA256 || HASH_ALGO_SHA224
33 # include "sha256.h"
34 #endif
35 #if HASH_ALGO_SHA512 || HASH_ALGO_SHA384
36 # include "sha512.h"
37 #endif
38 #include "error.h"
39 #include "fadvise.h"
40 #include "stdio--.h"
41 #include "xfreopen.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #if HASH_ALGO_MD5
45 # define PROGRAM_NAME "md5sum"
46 # define DIGEST_TYPE_STRING "MD5"
47 # define DIGEST_STREAM md5_stream
48 # define DIGEST_BITS 128
49 # define DIGEST_REFERENCE "RFC 1321"
50 # define DIGEST_ALIGN 4
51 #elif HASH_ALGO_SHA1
52 # define PROGRAM_NAME "sha1sum"
53 # define DIGEST_TYPE_STRING "SHA1"
54 # define DIGEST_STREAM sha1_stream
55 # define DIGEST_BITS 160
56 # define DIGEST_REFERENCE "FIPS-180-1"
57 # define DIGEST_ALIGN 4
58 #elif HASH_ALGO_SHA256
59 # define PROGRAM_NAME "sha256sum"
60 # define DIGEST_TYPE_STRING "SHA256"
61 # define DIGEST_STREAM sha256_stream
62 # define DIGEST_BITS 256
63 # define DIGEST_REFERENCE "FIPS-180-2"
64 # define DIGEST_ALIGN 4
65 #elif HASH_ALGO_SHA224
66 # define PROGRAM_NAME "sha224sum"
67 # define DIGEST_TYPE_STRING "SHA224"
68 # define DIGEST_STREAM sha224_stream
69 # define DIGEST_BITS 224
70 # define DIGEST_REFERENCE "RFC 3874"
71 # define DIGEST_ALIGN 4
72 #elif HASH_ALGO_SHA512
73 # define PROGRAM_NAME "sha512sum"
74 # define DIGEST_TYPE_STRING "SHA512"
75 # define DIGEST_STREAM sha512_stream
76 # define DIGEST_BITS 512
77 # define DIGEST_REFERENCE "FIPS-180-2"
78 # define DIGEST_ALIGN 8
79 #elif HASH_ALGO_SHA384
80 # define PROGRAM_NAME "sha384sum"
81 # define DIGEST_TYPE_STRING "SHA384"
82 # define DIGEST_STREAM sha384_stream
83 # define DIGEST_BITS 384
84 # define DIGEST_REFERENCE "FIPS-180-2"
85 # define DIGEST_ALIGN 8
86 #else
87 # error "Can't decide which hash algorithm to compile."
88 #endif
90 #define DIGEST_HEX_BYTES (DIGEST_BITS / 4)
91 #define DIGEST_BIN_BYTES (DIGEST_BITS / 8)
93 #define AUTHORS \
94 proper_name ("Ulrich Drepper"), \
95 proper_name ("Scott Miller"), \
96 proper_name ("David Madore")
98 /* The minimum length of a valid digest line. This length does
99 not include any newline character at the end of a line. */
100 #define MIN_DIGEST_LINE_LENGTH \
101 (DIGEST_HEX_BYTES /* length of hexadecimal message digest */ \
102 + 2 /* blank and binary indicator */ \
103 + 1 /* minimum filename length */ )
105 /* True if any of the files read were the standard input. */
106 static bool have_read_stdin;
108 /* The minimum length of a valid checksum line for the selected algorithm. */
109 static size_t min_digest_line_length;
111 /* Set to the length of a digest hex string for the selected algorithm. */
112 static size_t digest_hex_bytes;
114 /* With --check, don't generate any output.
115 The exit code indicates success or failure. */
116 static bool status_only = false;
118 /* With --check, print a message to standard error warning about each
119 improperly formatted checksum line. */
120 static bool warn = false;
122 /* With --check, suppress the "OK" printed for each verified file. */
123 static bool quiet = false;
125 /* With --check, exit with a non-zero return code if any line is
126 improperly formatted. */
127 static bool strict = false;
129 /* For long options that have no equivalent short option, use a
130 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
131 enum
133 STATUS_OPTION = CHAR_MAX + 1,
134 QUIET_OPTION,
135 STRICT_OPTION
138 static struct option const long_options[] =
140 { "binary", no_argument, NULL, 'b' },
141 { "check", no_argument, NULL, 'c' },
142 { "quiet", no_argument, NULL, QUIET_OPTION },
143 { "status", no_argument, NULL, STATUS_OPTION },
144 { "text", no_argument, NULL, 't' },
145 { "warn", no_argument, NULL, 'w' },
146 { "strict", no_argument, NULL, STRICT_OPTION },
147 { GETOPT_HELP_OPTION_DECL },
148 { GETOPT_VERSION_OPTION_DECL },
149 { NULL, 0, NULL, 0 }
152 void
153 usage (int status)
155 if (status != EXIT_SUCCESS)
156 fprintf (stderr, _("Try `%s --help' for more information.\n"),
157 program_name);
158 else
160 printf (_("\
161 Usage: %s [OPTION]... [FILE]...\n\
162 Print or check %s (%d-bit) checksums.\n\
163 With no FILE, or when FILE is -, read standard input.\n\
166 program_name,
167 DIGEST_TYPE_STRING,
168 DIGEST_BITS);
169 if (O_BINARY)
170 fputs (_("\
171 -b, --binary read in binary mode (default unless reading tty stdin)\n\
172 "), stdout);
173 else
174 fputs (_("\
175 -b, --binary read in binary mode\n\
176 "), stdout);
177 printf (_("\
178 -c, --check read %s sums from the FILEs and check them\n"),
179 DIGEST_TYPE_STRING);
180 if (O_BINARY)
181 fputs (_("\
182 -t, --text read in text mode (default if reading tty stdin)\n\
183 "), stdout);
184 else
185 fputs (_("\
186 -t, --text read in text mode (default)\n\
187 "), stdout);
188 fputs (_("\
190 The following three options are useful only when verifying checksums:\n\
191 --quiet don't print OK for each successfully verified file\n\
192 --status don't output anything, status code shows success\n\
193 -w, --warn warn about improperly formatted checksum lines\n\
195 "), stdout);
196 fputs (_("\
197 --strict with --check, exit non-zero for any invalid input\n\
198 "), stdout);
199 fputs (HELP_OPTION_DESCRIPTION, stdout);
200 fputs (VERSION_OPTION_DESCRIPTION, stdout);
201 printf (_("\
203 The sums are computed as described in %s. When checking, the input\n\
204 should be a former output of this program. The default mode is to print\n\
205 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
206 text), and name for each FILE.\n"),
207 DIGEST_REFERENCE);
208 emit_ancillary_info ();
211 exit (status);
214 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
216 /* Split the checksum string S (of length S_LEN) from a BSD 'md5' or
217 'sha1' command into two parts: a hexadecimal digest, and the file
218 name. S is modified. Return true if successful. */
220 static bool
221 bsd_split_3 (char *s, size_t s_len, unsigned char **hex_digest,
222 char **file_name)
224 size_t i;
226 if (s_len == 0)
227 return false;
229 *file_name = s;
231 /* Find end of filename. The BSD 'md5' and 'sha1' commands do not escape
232 filenames, so search backwards for the last ')'. */
233 i = s_len - 1;
234 while (i && s[i] != ')')
235 i--;
237 if (s[i] != ')')
238 return false;
240 s[i++] = '\0';
242 while (ISWHITE (s[i]))
243 i++;
245 if (s[i] != '=')
246 return false;
248 i++;
250 while (ISWHITE (s[i]))
251 i++;
253 *hex_digest = (unsigned char *) &s[i];
254 return true;
257 /* Split the string S (of length S_LEN) into three parts:
258 a hexadecimal digest, binary flag, and the file name.
259 S is modified. Return true if successful. */
261 static bool
262 split_3 (char *s, size_t s_len,
263 unsigned char **hex_digest, int *binary, char **file_name)
265 bool escaped_filename = false;
266 size_t algo_name_len;
268 size_t i = 0;
269 while (ISWHITE (s[i]))
270 ++i;
272 /* Check for BSD-style checksum line. */
273 algo_name_len = strlen (DIGEST_TYPE_STRING);
274 if (STREQ_LEN (s + i, DIGEST_TYPE_STRING, algo_name_len))
276 if (s[i + algo_name_len] == ' ')
277 ++i;
278 if (s[i + algo_name_len] == '(')
280 *binary = 0;
281 return bsd_split_3 (s + i + algo_name_len + 1,
282 s_len - (i + algo_name_len + 1),
283 hex_digest, file_name);
287 /* Ignore this line if it is too short.
288 Each line must have at least `min_digest_line_length - 1' (or one more, if
289 the first is a backslash) more characters to contain correct message digest
290 information. */
291 if (s_len - i < min_digest_line_length + (s[i] == '\\'))
292 return false;
294 if (s[i] == '\\')
296 ++i;
297 escaped_filename = true;
299 *hex_digest = (unsigned char *) &s[i];
301 /* The first field has to be the n-character hexadecimal
302 representation of the message digest. If it is not followed
303 immediately by a white space it's an error. */
304 i += digest_hex_bytes;
305 if (!ISWHITE (s[i]))
306 return false;
308 s[i++] = '\0';
310 if (s[i] != ' ' && s[i] != '*')
311 return false;
312 *binary = (s[i++] == '*');
314 /* All characters between the type indicator and end of line are
315 significant -- that includes leading and trailing white space. */
316 *file_name = &s[i];
318 if (escaped_filename)
320 /* Translate each `\n' string in the file name to a NEWLINE,
321 and each `\\' string to a backslash. */
323 char *dst = &s[i];
325 while (i < s_len)
327 switch (s[i])
329 case '\\':
330 if (i == s_len - 1)
332 /* A valid line does not end with a backslash. */
333 return false;
335 ++i;
336 switch (s[i++])
338 case 'n':
339 *dst++ = '\n';
340 break;
341 case '\\':
342 *dst++ = '\\';
343 break;
344 default:
345 /* Only `\' or `n' may follow a backslash. */
346 return false;
348 break;
350 case '\0':
351 /* The file name may not contain a NUL. */
352 return false;
353 break;
355 default:
356 *dst++ = s[i++];
357 break;
360 *dst = '\0';
362 return true;
365 /* Return true if S is a NUL-terminated string of DIGEST_HEX_BYTES hex digits.
366 Otherwise, return false. */
367 static bool _GL_ATTRIBUTE_PURE
368 hex_digits (unsigned char const *s)
370 unsigned int i;
371 for (i = 0; i < digest_hex_bytes; i++)
373 if (!isxdigit (*s))
374 return false;
375 ++s;
377 return *s == '\0';
380 /* An interface to the function, DIGEST_STREAM.
381 Operate on FILENAME (it may be "-").
383 *BINARY indicates whether the file is binary. BINARY < 0 means it
384 depends on whether binary mode makes any difference and the file is
385 a terminal; in that case, clear *BINARY if the file was treated as
386 text because it was a terminal.
388 Put the checksum in *BIN_RESULT, which must be properly aligned.
389 Return true if successful. */
391 static bool
392 digest_file (const char *filename, int *binary, unsigned char *bin_result)
394 FILE *fp;
395 int err;
396 bool is_stdin = STREQ (filename, "-");
398 if (is_stdin)
400 have_read_stdin = true;
401 fp = stdin;
402 if (O_BINARY && *binary)
404 if (*binary < 0)
405 *binary = ! isatty (STDIN_FILENO);
406 if (*binary)
407 xfreopen (NULL, "rb", stdin);
410 else
412 fp = fopen (filename, (O_BINARY && *binary ? "rb" : "r"));
413 if (fp == NULL)
415 error (0, errno, "%s", filename);
416 return false;
420 fadvise (fp, FADVISE_SEQUENTIAL);
422 err = DIGEST_STREAM (fp, bin_result);
423 if (err)
425 error (0, errno, "%s", filename);
426 if (fp != stdin)
427 fclose (fp);
428 return false;
431 if (!is_stdin && fclose (fp) != 0)
433 error (0, errno, "%s", filename);
434 return false;
437 return true;
440 static bool
441 digest_check (const char *checkfile_name)
443 FILE *checkfile_stream;
444 uintmax_t n_misformatted_lines = 0;
445 uintmax_t n_properly_formatted_lines = 0;
446 uintmax_t n_improperly_formatted_lines = 0;
447 uintmax_t n_mismatched_checksums = 0;
448 uintmax_t n_open_or_read_failures = 0;
449 unsigned char bin_buffer_unaligned[DIGEST_BIN_BYTES + DIGEST_ALIGN];
450 /* Make sure bin_buffer is properly aligned. */
451 unsigned char *bin_buffer = ptr_align (bin_buffer_unaligned, DIGEST_ALIGN);
452 uintmax_t line_number;
453 char *line;
454 size_t line_chars_allocated;
455 bool is_stdin = STREQ (checkfile_name, "-");
457 if (is_stdin)
459 have_read_stdin = true;
460 checkfile_name = _("standard input");
461 checkfile_stream = stdin;
463 else
465 checkfile_stream = fopen (checkfile_name, "r");
466 if (checkfile_stream == NULL)
468 error (0, errno, "%s", checkfile_name);
469 return false;
473 line_number = 0;
474 line = NULL;
475 line_chars_allocated = 0;
478 char *filename IF_LINT ( = NULL);
479 int binary;
480 unsigned char *hex_digest IF_LINT ( = NULL);
481 ssize_t line_length;
483 ++line_number;
484 if (line_number == 0)
485 error (EXIT_FAILURE, 0, _("%s: too many checksum lines"),
486 checkfile_name);
488 line_length = getline (&line, &line_chars_allocated, checkfile_stream);
489 if (line_length <= 0)
490 break;
492 /* Ignore comment lines, which begin with a '#' character. */
493 if (line[0] == '#')
494 continue;
496 /* Remove any trailing newline. */
497 if (line[line_length - 1] == '\n')
498 line[--line_length] = '\0';
500 if (! (split_3 (line, line_length, &hex_digest, &binary, &filename)
501 && ! (is_stdin && STREQ (filename, "-"))
502 && hex_digits (hex_digest)))
504 ++n_misformatted_lines;
506 if (warn)
508 error (0, 0,
509 _("%s: %" PRIuMAX
510 ": improperly formatted %s checksum line"),
511 checkfile_name, line_number,
512 DIGEST_TYPE_STRING);
515 ++n_improperly_formatted_lines;
517 else
519 static const char bin2hex[] = { '0', '1', '2', '3',
520 '4', '5', '6', '7',
521 '8', '9', 'a', 'b',
522 'c', 'd', 'e', 'f' };
523 bool ok;
525 ++n_properly_formatted_lines;
527 ok = digest_file (filename, &binary, bin_buffer);
529 if (!ok)
531 ++n_open_or_read_failures;
532 if (!status_only)
534 printf (_("%s: FAILED open or read\n"), filename);
537 else
539 size_t digest_bin_bytes = digest_hex_bytes / 2;
540 size_t cnt;
541 /* Compare generated binary number with text representation
542 in check file. Ignore case of hex digits. */
543 for (cnt = 0; cnt < digest_bin_bytes; ++cnt)
545 if (tolower (hex_digest[2 * cnt])
546 != bin2hex[bin_buffer[cnt] >> 4]
547 || (tolower (hex_digest[2 * cnt + 1])
548 != (bin2hex[bin_buffer[cnt] & 0xf])))
549 break;
551 if (cnt != digest_bin_bytes)
552 ++n_mismatched_checksums;
554 if (!status_only)
556 if (cnt != digest_bin_bytes)
557 printf ("%s: %s\n", filename, _("FAILED"));
558 else if (!quiet)
559 printf ("%s: %s\n", filename, _("OK"));
564 while (!feof (checkfile_stream) && !ferror (checkfile_stream));
566 free (line);
568 if (ferror (checkfile_stream))
570 error (0, 0, _("%s: read error"), checkfile_name);
571 return false;
574 if (!is_stdin && fclose (checkfile_stream) != 0)
576 error (0, errno, "%s", checkfile_name);
577 return false;
580 if (n_properly_formatted_lines == 0)
582 /* Warn if no tests are found. */
583 error (0, 0, _("%s: no properly formatted %s checksum lines found"),
584 checkfile_name, DIGEST_TYPE_STRING);
586 else
588 if (!status_only)
590 if (n_misformatted_lines != 0)
591 error (0, 0,
592 (ngettext
593 ("WARNING: %" PRIuMAX " line is improperly formatted",
594 "WARNING: %" PRIuMAX " lines are improperly formatted",
595 select_plural (n_misformatted_lines))),
596 n_misformatted_lines);
598 if (n_open_or_read_failures != 0)
599 error (0, 0,
600 (ngettext
601 ("WARNING: %" PRIuMAX " listed file could not be read",
602 "WARNING: %" PRIuMAX " listed files could not be read",
603 select_plural (n_open_or_read_failures))),
604 n_open_or_read_failures);
606 if (n_mismatched_checksums != 0)
607 error (0, 0,
608 (ngettext
609 ("WARNING: %" PRIuMAX " computed checksum did NOT match",
610 "WARNING: %" PRIuMAX " computed checksums did NOT match",
611 select_plural (n_mismatched_checksums))),
612 n_mismatched_checksums);
616 return (n_properly_formatted_lines != 0
617 && n_mismatched_checksums == 0
618 && n_open_or_read_failures == 0
619 && (!strict || n_improperly_formatted_lines == 0));
623 main (int argc, char **argv)
625 unsigned char bin_buffer_unaligned[DIGEST_BIN_BYTES + DIGEST_ALIGN];
626 /* Make sure bin_buffer is properly aligned. */
627 unsigned char *bin_buffer = ptr_align (bin_buffer_unaligned, DIGEST_ALIGN);
628 bool do_check = false;
629 int opt;
630 bool ok = true;
631 int binary = -1;
633 /* Setting values of global variables. */
634 initialize_main (&argc, &argv);
635 set_program_name (argv[0]);
636 setlocale (LC_ALL, "");
637 bindtextdomain (PACKAGE, LOCALEDIR);
638 textdomain (PACKAGE);
640 atexit (close_stdout);
642 /* Line buffer stdout to ensure lines are written atomically and immediately
643 so that processes running in parallel do not intersperse their output. */
644 setvbuf (stdout, NULL, _IOLBF, 0);
646 while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1)
647 switch (opt)
649 case 'b':
650 binary = 1;
651 break;
652 case 'c':
653 do_check = true;
654 break;
655 case STATUS_OPTION:
656 status_only = true;
657 warn = false;
658 quiet = false;
659 break;
660 case 't':
661 binary = 0;
662 break;
663 case 'w':
664 status_only = false;
665 warn = true;
666 quiet = false;
667 break;
668 case QUIET_OPTION:
669 status_only = false;
670 warn = false;
671 quiet = true;
672 break;
673 case STRICT_OPTION:
674 strict = true;
675 break;
676 case_GETOPT_HELP_CHAR;
677 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
678 default:
679 usage (EXIT_FAILURE);
682 min_digest_line_length = MIN_DIGEST_LINE_LENGTH;
683 digest_hex_bytes = DIGEST_HEX_BYTES;
685 if (0 <= binary && do_check)
687 error (0, 0, _("the --binary and --text options are meaningless when "
688 "verifying checksums"));
689 usage (EXIT_FAILURE);
692 if (status_only && !do_check)
694 error (0, 0,
695 _("the --status option is meaningful only when verifying checksums"));
696 usage (EXIT_FAILURE);
699 if (warn && !do_check)
701 error (0, 0,
702 _("the --warn option is meaningful only when verifying checksums"));
703 usage (EXIT_FAILURE);
706 if (quiet && !do_check)
708 error (0, 0,
709 _("the --quiet option is meaningful only when verifying checksums"));
710 usage (EXIT_FAILURE);
713 if (strict & !do_check)
715 error (0, 0,
716 _("the --strict option is meaningful only when verifying checksums"));
717 usage (EXIT_FAILURE);
720 if (!O_BINARY && binary < 0)
721 binary = 0;
723 if (optind == argc)
724 argv[argc++] = bad_cast ("-");
726 for (; optind < argc; ++optind)
728 char *file = argv[optind];
730 if (do_check)
731 ok &= digest_check (file);
732 else
734 int file_is_binary = binary;
736 if (! digest_file (file, &file_is_binary, bin_buffer))
737 ok = false;
738 else
740 size_t i;
742 /* Output a leading backslash if the file name contains
743 a newline or backslash. */
744 if (strchr (file, '\n') || strchr (file, '\\'))
745 putchar ('\\');
747 for (i = 0; i < (digest_hex_bytes / 2); ++i)
748 printf ("%02x", bin_buffer[i]);
750 putchar (' ');
751 if (file_is_binary)
752 putchar ('*');
753 else
754 putchar (' ');
756 /* Translate each NEWLINE byte to the string, "\\n",
757 and each backslash to "\\\\". */
758 for (i = 0; i < strlen (file); ++i)
760 switch (file[i])
762 case '\n':
763 fputs ("\\n", stdout);
764 break;
766 case '\\':
767 fputs ("\\\\", stdout);
768 break;
770 default:
771 putchar (file[i]);
772 break;
775 putchar ('\n');
780 if (have_read_stdin && fclose (stdin) == EOF)
781 error (EXIT_FAILURE, errno, _("standard input"));
783 exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);