1 /* Compute MD5 or SHA1 checksum of files or strings
2 Copyright (C) 1995-2004 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 2, or (at your option)
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, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>. */
24 #include <sys/types.h>
34 /* The official name of this program (e.g., no `g' prefix). */
35 #define PROGRAM_NAME (algorithm == ALG_MD5 ? "md5sum" : "shasum")
37 #define AUTHORS "Ulrich Drepper", "Scott Miller"
39 /* Most systems do not distinguish between external and internal
40 text representations. */
41 /* FIXME: This begs for an autoconf test. */
43 # define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)
44 # define TEXT1TO1 "rb"
48 # define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)
49 # define TEXT1TO1 "rb", "ctx=stm"
50 # define TEXTCNVT "r", "ctx=stm"
52 # if UNIX || __UNIX__ || unix || __unix__ || _POSIX_VERSION
53 # define OPENOPTS(BINARY) "r"
55 /* The following line is intended to evoke an error.
56 Using #error is not portable enough. */
57 "Cannot determine system type."
63 #define DIGEST_TYPE_STRING(Alg) ((Alg) == ALG_MD5 ? "MD5" : "SHA1")
64 #define DIGEST_STREAM(Alg) ((Alg) == ALG_MD5 ? md5_stream : sha_stream)
66 #define DIGEST_BITS(Alg) ((Alg) == ALG_MD5 ? 128 : 160)
67 #define DIGEST_HEX_BYTES(Alg) (DIGEST_BITS (Alg) / 4)
68 #define DIGEST_BIN_BYTES(Alg) (DIGEST_BITS (Alg) / 8)
70 #define MAX_DIGEST_BIN_BYTES MAX (DIGEST_BIN_BYTES (ALG_MD5), \
71 DIGEST_BIN_BYTES (ALG_SHA1))
73 /* The minimum length of a valid digest line. This length does
74 not include any newline character at the end of a line. */
75 #define MIN_DIGEST_LINE_LENGTH(Alg) \
76 (DIGEST_HEX_BYTES (Alg) /* length of hexadecimal message digest */ \
77 + 2 /* blank and binary indicator */ \
78 + 1 /* minimum filename length */ )
80 /* Nonzero if any of the files read were the standard input. */
81 static int have_read_stdin;
83 /* The minimum length of a valid checksum line for the selected algorithm. */
84 static size_t min_digest_line_length;
86 /* Set to the length of a digest hex string for the selected algorithm. */
87 static size_t digest_hex_bytes;
89 /* With --check, don't generate any output.
90 The exit code indicates success or failure. */
91 static int status_only = 0;
93 /* With --check, print a message to standard error warning about each
94 improperly formatted checksum line. */
97 /* Declared and set via one of the wrapper .c files. */
98 /* int algorithm = ALG_UNSPECIFIED; */
100 /* The name this program was run with. */
103 static const struct option long_options[] =
105 { "binary", no_argument, 0, 'b' },
106 { "check", no_argument, 0, 'c' },
107 { "status", no_argument, 0, 2 },
108 { "string", required_argument, 0, 1 },
109 { "text", no_argument, 0, 't' },
110 { "warn", no_argument, 0, 'w' },
111 { GETOPT_HELP_OPTION_DECL },
112 { GETOPT_VERSION_OPTION_DECL },
119 if (status != EXIT_SUCCESS)
120 fprintf (stderr, _("Try `%s --help' for more information.\n"),
125 Usage: %s [OPTION] [FILE]...\n\
126 or: %s [OPTION] --check [FILE]\n\
127 Print or check %s (%d-bit) checksums.\n\
128 With no FILE, or when FILE is -, read standard input.\n\
130 program_name, program_name,
131 DIGEST_TYPE_STRING (algorithm),
132 DIGEST_BITS (algorithm));
135 -b, --binary read files in binary mode (default on DOS/Windows)\n\
136 -c, --check check %s sums against given list\n\
137 -t, --text read files in text mode (default)\n\
140 DIGEST_TYPE_STRING (algorithm));
142 The following two options are useful only when verifying checksums:\n\
143 --status don't output anything, status code shows success\n\
144 -w, --warn warn about improperly formated checksum lines\n\
147 fputs (HELP_OPTION_DESCRIPTION, stdout);
148 fputs (VERSION_OPTION_DESCRIPTION, stdout);
151 The sums are computed as described in %s. When checking, the input\n\
152 should be a former output of this program. The default mode is to print\n\
153 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
154 text), and name for each FILE.\n"),
155 (algorithm == ALG_MD5 ? "RFC 1321" : "FIPS-180-1"));
156 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
162 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
164 /* Split the checksum string S (of length S_LEN) from a BSD 'md5' or
165 'sha1' command into two parts: a hexadecimal digest, and the file
166 name. S is modified. */
169 bsd_split_3 (char *s, size_t s_len, unsigned char **hex_digest, char **file_name)
175 /* Find end of filename. The BSD 'md5' and 'sha1' commands do not escape
176 filenames, so search backwards for the last ')'. */
178 while (i && s[i] != ')')
186 while (ISWHITE (s[i]))
194 while (ISWHITE (s[i]))
197 *hex_digest = (unsigned char *) &s[i];
201 /* Split the string S (of length S_LEN) into three parts:
202 a hexadecimal digest, binary flag, and the file name.
206 split_3 (char *s, size_t s_len,
207 unsigned char **hex_digest, int *binary, char **file_name)
210 int escaped_filename = 0;
211 size_t algo_name_len;
214 while (ISWHITE (s[i]))
217 /* Check for BSD-style checksum line. */
218 algo_name_len = strlen (DIGEST_TYPE_STRING (algorithm));
219 if (strncmp (s + i, DIGEST_TYPE_STRING (algorithm), algo_name_len) == 0)
221 if (strncmp (s + i + algo_name_len, " (", 2) == 0)
224 return bsd_split_3 (s + i + algo_name_len + 2,
225 s_len - (i + algo_name_len + 2),
226 hex_digest, file_name);
230 /* Ignore this line if it is too short.
231 Each line must have at least `min_digest_line_length - 1' (or one more, if
232 the first is a backslash) more characters to contain correct message digest
234 if (s_len - i < min_digest_line_length + (s[i] == '\\'))
240 escaped_filename = 1;
242 *hex_digest = (unsigned char *) &s[i];
244 /* The first field has to be the n-character hexadecimal
245 representation of the message digest. If it is not followed
246 immediately by a white space it's an error. */
247 i += digest_hex_bytes;
253 if (s[i] != ' ' && s[i] != '*')
255 *binary = (s[i++] == '*');
257 /* All characters between the type indicator and end of line are
258 significant -- that includes leading and trailing white space. */
261 if (escaped_filename)
263 /* Translate each `\n' string in the file name to a NEWLINE,
264 and each `\\' string to a backslash. */
275 /* A valid line does not end with a backslash. */
288 /* Only `\' or `n' may follow a backslash. */
294 /* The file name may not contain a NUL. */
309 hex_digits (unsigned char const *s)
320 /* An interface to the function, DIGEST_STREAM,
321 (either md5_stream or sha_stream).
322 Operate on FILENAME (it may be "-") and put the result in *BIN_RESULT.
323 Return non-zero upon failure, zero to indicate success. */
326 digest_file (const char *filename, int binary, unsigned char *bin_result,
327 int (*digest_stream)(FILE *, void *))
332 if (STREQ (filename, "-"))
337 /* If we need binary reads from a pipe or redirected stdin, we need
338 to switch it to BINARY mode here, since stdin is already open. */
340 SET_BINARY (fileno (stdin));
345 /* OPENOPTS is a macro. It varies with the system.
346 Some systems distinguish between internal and
347 external text representations. */
349 fp = fopen (filename, OPENOPTS (binary));
352 error (0, errno, "%s", filename);
357 err = (*digest_stream) (fp, bin_result);
360 error (0, errno, "%s", filename);
366 if (fp != stdin && fclose (fp) == EOF)
368 error (0, errno, "%s", filename);
376 digest_check (const char *checkfile_name, int (*digest_stream)(FILE *, void *))
378 FILE *checkfile_stream;
379 int n_properly_formated_lines = 0;
380 int n_mismatched_checksums = 0;
381 int n_open_or_read_failures = 0;
382 unsigned char bin_buffer[MAX_DIGEST_BIN_BYTES];
385 size_t line_chars_allocated;
387 if (STREQ (checkfile_name, "-"))
390 checkfile_name = _("standard input");
391 checkfile_stream = stdin;
395 checkfile_stream = fopen (checkfile_name, "r");
396 if (checkfile_stream == NULL)
398 error (0, errno, "%s", checkfile_name);
403 SET_MODE (fileno (checkfile_stream), O_TEXT);
406 line_chars_allocated = 0;
411 unsigned char *hex_digest;
417 line_length = getline (&line, &line_chars_allocated, checkfile_stream);
418 if (line_length <= 0)
421 /* Ignore comment lines, which begin with a '#' character. */
425 /* Remove any trailing newline. */
426 if (line[line_length - 1] == '\n')
427 line[--line_length] = '\0';
429 err = split_3 (line, line_length, &hex_digest, &binary, &filename);
430 if (err || !hex_digits (hex_digest))
435 _("%s: %lu: improperly formatted %s checksum line"),
436 checkfile_name, (unsigned long) line_number,
437 DIGEST_TYPE_STRING (algorithm));
442 static const char bin2hex[] = { '0', '1', '2', '3',
445 'c', 'd', 'e', 'f' };
448 ++n_properly_formated_lines;
450 fail = digest_file (filename, binary, bin_buffer, digest_stream);
454 ++n_open_or_read_failures;
457 printf (_("%s: FAILED open or read\n"), filename);
463 size_t digest_bin_bytes = digest_hex_bytes / 2;
465 /* Compare generated binary number with text representation
466 in check file. Ignore case of hex digits. */
467 for (cnt = 0; cnt < digest_bin_bytes; ++cnt)
469 if (TOLOWER (hex_digest[2 * cnt])
470 != bin2hex[bin_buffer[cnt] >> 4]
471 || (TOLOWER (hex_digest[2 * cnt + 1])
472 != (bin2hex[bin_buffer[cnt] & 0xf])))
475 if (cnt != digest_bin_bytes)
476 ++n_mismatched_checksums;
480 printf ("%s: %s\n", filename,
481 (cnt != digest_bin_bytes ? _("FAILED") : _("OK")));
487 while (!feof (checkfile_stream) && !ferror (checkfile_stream));
492 if (ferror (checkfile_stream))
494 error (0, 0, _("%s: read error"), checkfile_name);
498 if (checkfile_stream != stdin && fclose (checkfile_stream) == EOF)
500 error (0, errno, "%s", checkfile_name);
504 if (n_properly_formated_lines == 0)
506 /* Warn if no tests are found. */
507 error (0, 0, _("%s: no properly formatted %s checksum lines found"),
508 checkfile_name, DIGEST_TYPE_STRING (algorithm));
514 int n_computed_checkums = (n_properly_formated_lines
515 - n_open_or_read_failures);
517 if (n_open_or_read_failures > 0)
520 _("WARNING: %d of %d listed %s could not be read"),
521 n_open_or_read_failures, n_properly_formated_lines,
522 (n_properly_formated_lines == 1
523 ? _("file") : _("files")));
526 if (n_mismatched_checksums > 0)
529 _("WARNING: %d of %d computed %s did NOT match"),
530 n_mismatched_checksums, n_computed_checkums,
531 (n_computed_checkums == 1
532 ? _("checksum") : _("checksums")));
537 return ((n_properly_formated_lines > 0 && n_mismatched_checksums == 0
538 && n_open_or_read_failures == 0) ? 0 : 1);
542 main (int argc, char **argv)
544 unsigned char bin_buffer[MAX_DIGEST_BIN_BYTES];
547 char **string = NULL;
548 size_t n_strings = 0;
550 int file_type_specified = 0;
553 /* Binary is default on MSDOS, so the actual file contents
554 are used in computation. */
557 /* Text is default of the Plumb/Lankester format. */
561 /* Setting values of global variables. */
562 initialize_main (&argc, &argv);
563 program_name = argv[0];
564 setlocale (LC_ALL, "");
565 bindtextdomain (PACKAGE, LOCALEDIR);
566 textdomain (PACKAGE);
568 atexit (close_stdout);
570 while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1)
573 case 0: /* long option */
575 case 1: /* --string */
578 string = xmalloc ((argc - 1) * sizeof (char *));
582 string[n_strings++] = optarg;
586 file_type_specified = 1;
597 file_type_specified = 1;
604 case_GETOPT_HELP_CHAR;
605 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
607 usage (EXIT_FAILURE);
610 min_digest_line_length = MIN_DIGEST_LINE_LENGTH (algorithm);
611 digest_hex_bytes = DIGEST_HEX_BYTES (algorithm);
613 if (file_type_specified && do_check)
615 error (0, 0, _("the --binary and --text options are meaningless when \
616 verifying checksums"));
617 usage (EXIT_FAILURE);
620 if (n_strings > 0 && do_check)
623 _("the --string and --check options are mutually exclusive"));
624 usage (EXIT_FAILURE);
627 if (status_only && !do_check)
630 _("the --status option is meaningful only when verifying checksums"));
631 usage (EXIT_FAILURE);
634 if (warn && !do_check)
637 _("the --warn option is meaningful only when verifying checksums"));
638 usage (EXIT_FAILURE);
647 error (0, 0, _("no files may be specified when using --string"));
648 usage (EXIT_FAILURE);
650 for (i = 0; i < n_strings; ++i)
653 if (algorithm == ALG_MD5)
654 md5_buffer (string[i], strlen (string[i]), bin_buffer);
656 sha_buffer (string[i], strlen (string[i]), bin_buffer);
658 for (cnt = 0; cnt < (digest_hex_bytes / 2); ++cnt)
659 printf ("%02x", bin_buffer[cnt]);
661 printf (" \"%s\"\n", string[i]);
666 if (optind + 1 < argc)
669 _("only one argument may be specified when using --check"));
670 usage (EXIT_FAILURE);
673 err = digest_check ((optind == argc) ? "-" : argv[optind],
674 DIGEST_STREAM (algorithm));
681 for (; optind < argc; ++optind)
684 char *file = argv[optind];
686 fail = digest_file (file, binary, bin_buffer,
687 DIGEST_STREAM (algorithm));
693 /* Output a leading backslash if the file name contains
694 a newline or backslash. */
695 if (strchr (file, '\n') || strchr (file, '\\'))
698 for (i = 0; i < (digest_hex_bytes / 2); ++i)
699 printf ("%02x", bin_buffer[i]);
707 /* Translate each NEWLINE byte to the string, "\\n",
708 and each backslash to "\\\\". */
709 for (i = 0; i < strlen (file); ++i)
714 fputs ("\\n", stdout);
718 fputs ("\\\\", stdout);
731 if (have_read_stdin && fclose (stdin) == EOF)
732 error (EXIT_FAILURE, errno, _("standard input"));
734 exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);