*** empty log message ***
[coreutils.git] / src / md5sum.c
blob0b44485359cc443fb60afa2a09d03d27f7b90d43
1 /* Compute MD5 or SHA1 checksum of files or strings
2 Copyright (C) 1995-2000 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)
7 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, 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>. */
20 #include <config.h>
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <sys/types.h>
26 #include "system.h"
28 #include "md5.h"
29 #include "sha.h"
30 #include "checksum.h"
31 #include "getline.h"
32 #include "closeout.h"
33 #include "error.h"
35 /* The official name of this program (e.g., no `g' prefix). */
36 #define PROGRAM_NAME (algorithm == ALG_MD5 ? "md5sum" : "shasum")
38 #define AUTHORS "Ulrich Drepper and Scott Miller"
40 /* Most systems do not distinguish between external and internal
41 text representations. */
42 /* FIXME: This begs for an autoconf test. */
43 #if O_BINARY
44 # define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)
45 # define TEXT1TO1 "rb"
46 # define TEXTCNVT "r"
47 #else
48 # if defined VMS
49 # define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)
50 # define TEXT1TO1 "rb", "ctx=stm"
51 # define TEXTCNVT "r", "ctx=stm"
52 # else
53 # if UNIX || __UNIX__ || unix || __unix__ || _POSIX_VERSION
54 # define OPENOPTS(BINARY) "r"
55 # else
56 /* The following line is intended to evoke an error.
57 Using #error is not portable enough. */
58 "Cannot determine system type."
59 # endif
60 # endif
61 #endif
64 #define DIGEST_TYPE_STRING(Alg) ((Alg) == ALG_MD5 ? "MD5" : "SHA1")
65 #define DIGEST_STREAM(Alg) ((Alg) == ALG_MD5 ? md5_stream : sha_stream)
67 #define DIGEST_BITS(Alg) ((Alg) == ALG_MD5 ? 128 : 160)
68 #define DIGEST_HEX_BYTES(Alg) (DIGEST_BITS (Alg) / 4)
69 #define DIGEST_BIN_BYTES(Alg) (DIGEST_BITS (Alg) / 8)
71 #define MAX_DIGEST_BIN_BYTES MAX (DIGEST_BIN_BYTES (ALG_MD5), \
72 DIGEST_BIN_BYTES (ALG_SHA1))
74 /* The minimum length of a valid digest line. This length does
75 not include any newline character at the end of a line. */
76 #define MIN_DIGEST_LINE_LENGTH(Alg) \
77 (DIGEST_HEX_BYTES (Alg) /* length of hexadecimal message digest */ \
78 + 2 /* blank and binary indicator */ \
79 + 1 /* minimum filename length */ )
81 /* Nonzero if any of the files read were the standard input. */
82 static int have_read_stdin;
84 /* The minimum length of a valid checksum line for the selected algorithm. */
85 static int min_digest_line_length;
87 /* Set to the length of a digest hex string for the selected algorithm. */
88 static size_t digest_hex_bytes;
90 /* With --check, don't generate any output.
91 The exit code indicates success or failure. */
92 static int status_only = 0;
94 /* With --check, print a message to standard error warning about each
95 improperly formatted checksum line. */
96 static int warn = 0;
98 /* Declared and set via one of the wrapper .c files. */
99 /* int algorithm = ALG_UNSPECIFIED; */
101 /* The name this program was run with. */
102 char *program_name;
104 static const struct option long_options[] =
106 { "binary", no_argument, 0, 'b' },
107 { "check", no_argument, 0, 'c' },
108 { "status", no_argument, 0, 2 },
109 { "string", required_argument, 0, 1 },
110 { "text", no_argument, 0, 't' },
111 { "warn", no_argument, 0, 'w' },
112 { GETOPT_HELP_OPTION_DECL },
113 { GETOPT_VERSION_OPTION_DECL },
114 { NULL, 0, NULL, 0 }
117 void
118 usage (int status)
120 if (status != 0)
121 fprintf (stderr, _("Try `%s --help' for more information.\n"),
122 program_name);
123 else
125 printf (_("\
126 Usage: %s [OPTION] [FILE]...\n\
127 or: %s [OPTION] --check [FILE]\n\
128 Print or check %s (%d-bit) checksums.\n\
129 With no FILE, or when FILE is -, read standard input.\n\
131 -b, --binary read files in binary mode (default on DOS/Windows)\n\
132 -c, --check check %s sums against given list\n\
133 -t, --text read files in text mode (default)\n\
135 The following two options are useful only when verifying checksums:\n\
136 --status don't output anything, status code shows success\n\
137 -w, --warn warn about improperly formated checksum lines\n\
139 --help display this help and exit\n\
140 --version output version information and exit\n\
142 The sums are computed as described in %s. When checking, the input\n\
143 should be a former output of this program. The default mode is to print\n\
144 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
145 text), and name for each FILE.\n"),
146 program_name, program_name,
147 DIGEST_TYPE_STRING (algorithm),
148 DIGEST_BITS (algorithm),
149 DIGEST_TYPE_STRING (algorithm),
150 (algorithm == ALG_MD5 ? "RFC 1321" : "FIPS-180-1")
152 puts (_("\nReport bugs to <bug-textutils@gnu.org>."));
155 exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
158 static int
159 split_3 (char *s, size_t s_len, unsigned char **u, int *binary, char **w)
161 size_t i;
162 int escaped_filename = 0;
164 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
166 i = 0;
167 while (ISWHITE (s[i]))
168 ++i;
170 /* The line must have at least `min_digest_line_length - 1' (or one more, if
171 the first is a backslash) more characters to contain correct message digest
172 information. Ignore this line if it is too short. */
173 if (!(s_len - i >= min_digest_line_length
174 || (s[i] == '\\' && s_len - i >= 1 + min_digest_line_length)))
175 return 1;
177 if (s[i] == '\\')
179 ++i;
180 escaped_filename = 1;
182 *u = (unsigned char *) &s[i];
184 /* The first field has to be the n-character hexadecimal
185 representation of the message digest. If it is not followed
186 immediately by a white space it's an error. */
187 i += digest_hex_bytes;
188 if (!ISWHITE (s[i]))
189 return 1;
191 s[i++] = '\0';
193 if (s[i] != ' ' && s[i] != '*')
194 return 1;
195 *binary = (s[i++] == '*');
197 /* All characters between the type indicator and end of line are
198 significant -- that includes leading and trailing white space. */
199 *w = &s[i];
201 if (escaped_filename)
203 /* Translate each `\n' string in the file name to a NEWLINE,
204 and each `\\' string to a backslash. */
206 char *dst = &s[i];
208 while (i < s_len)
210 switch (s[i])
212 case '\\':
213 if (i == s_len - 1)
215 /* A valid line does not end with a backslash. */
216 return 1;
218 ++i;
219 switch (s[i++])
221 case 'n':
222 *dst++ = '\n';
223 break;
224 case '\\':
225 *dst++ = '\\';
226 break;
227 default:
228 /* Only `\' or `n' may follow a backslash. */
229 return 1;
231 break;
233 case '\0':
234 /* The file name may not contain a NUL. */
235 return 1;
236 break;
238 default:
239 *dst++ = s[i++];
240 break;
243 *dst = '\0';
245 return 0;
248 static int
249 hex_digits (unsigned char const *s)
251 while (*s)
253 if (!ISXDIGIT (*s))
254 return 0;
255 ++s;
257 return 1;
260 /* An interface to the function, DIGEST_STREAM, (either md5_stream or sha_stream).
261 Operate on FILENAME (it may be "-") and put the result in *BIN_RESULT.
262 Return non-zero upon failure, zero to indicate success. */
264 static int
265 digest_file (const char *filename, int binary, unsigned char *bin_result,
266 int (*digest_stream)(FILE *, void *))
268 FILE *fp;
269 int err;
271 if (STREQ (filename, "-"))
273 have_read_stdin = 1;
274 fp = stdin;
275 #if O_BINARY
276 /* If we need binary reads from a pipe or redirected stdin, we need
277 to switch it to BINARY mode here, since stdin is already open. */
278 if (binary)
279 SET_BINARY (fileno (stdin));
280 #endif
282 else
284 /* OPENOPTS is a macro. It varies with the system.
285 Some systems distinguish between internal and
286 external text representations. */
288 fp = fopen (filename, OPENOPTS (binary));
289 if (fp == NULL)
291 error (0, errno, "%s", filename);
292 return 1;
296 err = (*digest_stream) (fp, bin_result);
297 if (err)
299 error (0, errno, "%s", filename);
300 if (fp != stdin)
301 fclose (fp);
302 return 1;
305 if (fp != stdin && fclose (fp) == EOF)
307 error (0, errno, "%s", filename);
308 return 1;
311 return 0;
314 static int
315 digest_check (const char *checkfile_name, int (*digest_stream)(FILE *, void *))
317 FILE *checkfile_stream;
318 int n_properly_formated_lines = 0;
319 int n_mismatched_checksums = 0;
320 int n_open_or_read_failures = 0;
321 unsigned char bin_buffer[MAX_DIGEST_BIN_BYTES];
322 size_t line_number;
323 char *line;
324 size_t line_chars_allocated;
326 if (STREQ (checkfile_name, "-"))
328 have_read_stdin = 1;
329 checkfile_name = _("standard input");
330 checkfile_stream = stdin;
332 else
334 checkfile_stream = fopen (checkfile_name, "r");
335 if (checkfile_stream == NULL)
337 error (0, errno, "%s", checkfile_name);
338 return 1;
342 line_number = 0;
343 line = NULL;
344 line_chars_allocated = 0;
347 char *filename;
348 int binary;
349 unsigned char *hex_digest;
350 int err;
351 int line_length;
353 ++line_number;
355 line_length = getline (&line, &line_chars_allocated, checkfile_stream);
356 if (line_length <= 0)
357 break;
359 /* Ignore comment lines, which begin with a '#' character. */
360 if (line[0] == '#')
361 continue;
363 /* Remove any trailing newline. */
364 if (line[line_length - 1] == '\n')
365 line[--line_length] = '\0';
367 err = split_3 (line, line_length, &hex_digest, &binary, &filename);
368 if (err || !hex_digits (hex_digest))
370 if (warn)
372 error (0, 0,
373 _("%s: %lu: improperly formatted %s checksum line"),
374 checkfile_name, (unsigned long) line_number,
375 DIGEST_TYPE_STRING (algorithm));
378 else
380 static const char bin2hex[] = { '0', '1', '2', '3',
381 '4', '5', '6', '7',
382 '8', '9', 'a', 'b',
383 'c', 'd', 'e', 'f' };
384 int fail;
386 ++n_properly_formated_lines;
388 fail = digest_file (filename, binary, bin_buffer, digest_stream);
390 if (fail)
392 ++n_open_or_read_failures;
393 if (!status_only)
395 printf (_("%s: FAILED open or read\n"), filename);
396 fflush (stdout);
399 else
401 size_t digest_bin_bytes = digest_hex_bytes / 2;
402 size_t cnt;
403 /* Compare generated binary number with text representation
404 in check file. Ignore case of hex digits. */
405 for (cnt = 0; cnt < digest_bin_bytes; ++cnt)
407 if (TOLOWER (hex_digest[2 * cnt]) != bin2hex[bin_buffer[cnt] >> 4]
408 || (TOLOWER (hex_digest[2 * cnt + 1])
409 != (bin2hex[bin_buffer[cnt] & 0xf])))
410 break;
412 if (cnt != digest_bin_bytes)
413 ++n_mismatched_checksums;
415 if (!status_only)
417 printf ("%s: %s\n", filename,
418 (cnt != digest_bin_bytes ? _("FAILED") : _("OK")));
419 fflush (stdout);
424 while (!feof (checkfile_stream) && !ferror (checkfile_stream));
426 if (line)
427 free (line);
429 if (ferror (checkfile_stream))
431 error (0, 0, _("%s: read error"), checkfile_name);
432 return 1;
435 if (checkfile_stream != stdin && fclose (checkfile_stream) == EOF)
437 error (0, errno, "%s", checkfile_name);
438 return 1;
441 if (n_properly_formated_lines == 0)
443 /* Warn if no tests are found. */
444 error (0, 0, _("%s: no properly formatted %s checksum lines found"),
445 checkfile_name, DIGEST_TYPE_STRING (algorithm));
447 else
449 if (!status_only)
451 int n_computed_checkums = (n_properly_formated_lines
452 - n_open_or_read_failures);
454 if (n_open_or_read_failures > 0)
456 error (0, 0,
457 _("WARNING: %d of %d listed %s could not be read"),
458 n_open_or_read_failures, n_properly_formated_lines,
459 (n_properly_formated_lines == 1
460 ? _("file") : _("files")));
463 if (n_mismatched_checksums > 0)
465 error (0, 0,
466 _("WARNING: %d of %d computed %s did NOT match"),
467 n_mismatched_checksums, n_computed_checkums,
468 (n_computed_checkums == 1
469 ? _("checksum") : _("checksums")));
474 return ((n_properly_formated_lines > 0 && n_mismatched_checksums == 0
475 && n_open_or_read_failures == 0) ? 0 : 1);
479 main (int argc, char **argv)
481 unsigned char bin_buffer[MAX_DIGEST_BIN_BYTES];
482 int do_check = 0;
483 int opt;
484 char **string = NULL;
485 size_t n_strings = 0;
486 size_t err = 0;
487 int file_type_specified = 0;
489 #if O_BINARY
490 /* Binary is default on MSDOS, so the actual file contents
491 are used in computation. */
492 int binary = 1;
493 #else
494 /* Text is default of the Plumb/Lankester format. */
495 int binary = 0;
496 #endif
498 /* Setting values of global variables. */
499 program_name = argv[0];
500 setlocale (LC_ALL, "");
501 bindtextdomain (PACKAGE, LOCALEDIR);
502 textdomain (PACKAGE);
504 atexit (close_stdout);
506 while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1)
507 switch (opt)
509 case 0: /* long option */
510 break;
511 case 1: /* --string */
513 if (string == NULL)
514 string = (char **) xmalloc ((argc - 1) * sizeof (char *));
516 if (optarg == NULL)
517 optarg = "";
518 string[n_strings++] = optarg;
520 break;
521 case 'b':
522 file_type_specified = 1;
523 binary = 1;
524 break;
525 case 'c':
526 do_check = 1;
527 break;
528 case 2:
529 status_only = 1;
530 warn = 0;
531 break;
532 case 't':
533 file_type_specified = 1;
534 binary = 0;
535 break;
536 case 'w':
537 status_only = 0;
538 warn = 1;
539 break;
540 case_GETOPT_HELP_CHAR;
541 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
542 default:
543 usage (EXIT_FAILURE);
546 min_digest_line_length = MIN_DIGEST_LINE_LENGTH (algorithm);
547 digest_hex_bytes = DIGEST_HEX_BYTES (algorithm);
549 if (file_type_specified && do_check)
551 error (0, 0, _("the --binary and --text options are meaningless when \
552 verifying checksums"));
553 usage (EXIT_FAILURE);
556 if (n_strings > 0 && do_check)
558 error (0, 0,
559 _("the --string and --check options are mutually exclusive"));
560 usage (EXIT_FAILURE);
563 if (status_only && !do_check)
565 error (0, 0,
566 _("the --status option is meaningful only when verifying checksums"));
567 usage (EXIT_FAILURE);
570 if (warn && !do_check)
572 error (0, 0,
573 _("the --warn option is meaningful only when verifying checksums"));
574 usage (EXIT_FAILURE);
577 if (n_strings > 0)
579 size_t i;
581 if (optind < argc)
583 error (0, 0, _("no files may be specified when using --string"));
584 usage (EXIT_FAILURE);
586 for (i = 0; i < n_strings; ++i)
588 size_t cnt;
589 if (algorithm == ALG_MD5)
590 md5_buffer (string[i], strlen (string[i]), bin_buffer);
591 else
592 sha_buffer (string[i], strlen (string[i]), bin_buffer);
594 for (cnt = 0; cnt < (digest_hex_bytes / 2); ++cnt)
595 printf ("%02x", bin_buffer[cnt]);
597 printf (" \"%s\"\n", string[i]);
600 else if (do_check)
602 if (optind + 1 < argc)
604 error (0, 0,
605 _("only one argument may be specified when using --check"));
606 usage (EXIT_FAILURE);
609 err = digest_check ((optind == argc) ? "-" : argv[optind],
610 DIGEST_STREAM (algorithm));
612 else
614 if (optind == argc)
615 argv[argc++] = "-";
617 for (; optind < argc; ++optind)
619 int fail;
620 char *file = argv[optind];
622 fail = digest_file (file, binary, bin_buffer,
623 DIGEST_STREAM (algorithm));
624 err |= fail;
625 if (!fail)
627 size_t i;
629 /* Output a leading backslash if the file name contains
630 a newline or backslash. */
631 if (strchr (file, '\n') || strchr (file, '\\'))
632 putchar ('\\');
634 for (i = 0; i < (digest_hex_bytes / 2); ++i)
635 printf ("%02x", bin_buffer[i]);
637 putchar (' ');
638 if (binary)
639 putchar ('*');
640 else
641 putchar (' ');
643 /* Translate each NEWLINE byte to the string, "\\n",
644 and each backslash to "\\\\". */
645 for (i = 0; i < strlen (file); ++i)
647 switch (file[i])
649 case '\n':
650 fputs ("\\n", stdout);
651 break;
653 case '\\':
654 fputs ("\\\\", stdout);
655 break;
657 default:
658 putchar (file[i]);
659 break;
662 putchar ('\n');
667 if (have_read_stdin && fclose (stdin) == EOF)
668 error (EXIT_FAILURE, errno, _("standard input"));
670 exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);