1 /* Compute MD5 checksum of files or strings according to the definition
2 of MD5 in RFC 1321 from April 1992.
3 Copyright (C) 95, 1996 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>. */
27 #include <sys/types.h>
29 #include "long-options.h"
35 /* Most systems do not distinguish between external and internal
36 text representations. */
37 #if UNIX || __UNIX__ || unix || __unix__ || _POSIX_VERSION
38 # define OPENOPTS(BINARY) "r"
41 # define TEXT1TO1 "rb"
45 # define TEXT1TO1 "rb", "ctx=stm"
46 # define TEXTCNVT "r", "ctx=stm"
48 /* The following line is intended to evoke an error.
49 Using #error is not portable enough. */
50 "Cannot determine system type."
53 # define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)
56 #if _LIBC || STDC_HEADERS
57 # define TOLOWER(c) tolower (c)
59 # define TOLOWER(c) (ISUPPER (c) ? tolower (c) : (c))
62 /* The string with which to replace NEWLINE characters in filenames.
63 This is required to make it so md5sum --check can parse the output
64 of `md5sum FILENAME' for FILENAME contain NL characters. */
65 #define NEWLINE_REPLACEMENT_STRING "<\"NL'\\>"
67 #define NEWLINE_REPLACEMENT_STRING_LENGTH \
68 (sizeof (NEWLINE_REPLACEMENT_STRING) - 1)
70 /* Nonzero if any of the files read were the standard input. */
71 static int have_read_stdin
;
73 /* With --check, don't generate any output.
74 The exit code indicates success or failure. */
75 static int status_only
= 0;
77 /* With --check, print a message to standard error warning about each
78 improperly formatted MD5 checksum line. */
81 /* The name this program was run with. */
84 static const struct option long_options
[] =
86 { "binary", no_argument
, 0, 'b' },
87 { "check", no_argument
, 0, 'c' },
88 { "status", no_argument
, 0, 2 },
89 { "string", required_argument
, 0, 1 },
90 { "text", no_argument
, 0, 't' },
91 { "warn", no_argument
, 0, 'w' },
101 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
105 Usage: %s [OPTION] [FILE]...\n\
106 or: %s [OPTION] --check [FILE]\n\
107 or: %s [OPTION] --string=STRING ...\n\
108 Print or check MD5 checksums.\n\
109 With no FILE, or when FILE is -, read standard input.\n\
111 -b, --binary read files in binary mode\n\
112 -t, --text read files in text mode (default)\n\
113 -c, --check check MD5 sums against given list\n\
115 The following two options are useful only when verifying checksums:\n\
116 --status don't output anything, status code shows success\n\
117 -w, --warn warn about improperly formated MD5 checksum lines\n\
119 --string=STRING compute checksum for STRING\n\
120 --help display this help and exit\n\
121 --version output version information and exit\n\
123 The sums are computed as described in RFC 1321. When checking, the input\n\
124 should be a former output of this program. The default mode is to print\n\
125 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
126 text), and name for each FILE.\n"),
127 program_name
, program_name
, program_name
);
129 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
133 split_3 (char *s
, size_t s_len
, char **u
, int *binary
, char **w
)
137 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
140 while (ISWHITE (s
[i
]))
143 /* The line has to be at least 35 characters long to contain correct
144 message digest information. */
145 if (s_len
>= 32 + 2 + 1)
150 /* The first field has to be the 32-character hexadecimal
151 representation of the message digest. If it is not followed
152 immediately by a white space it's an error. */
159 if (s
[i
] != ' ' && s
[i
] != '*')
161 *binary
= (s
[i
++] == '*');
163 /* All characters between the type indicator and end of line are
164 significant -- that includes leading and trailing white space. */
167 /* Translate each NEWLINE_REPLACEMENT_STRING in the file name
170 while ((p
= strstr (p
, NEWLINE_REPLACEMENT_STRING
)))
175 len
= s_len
- (p
- s
) - (NEWLINE_REPLACEMENT_STRING_LENGTH
- 1) + 1;
176 memmove (p
, p
+ NEWLINE_REPLACEMENT_STRING_LENGTH
- 1, len
);
177 s_len
-= NEWLINE_REPLACEMENT_STRING_LENGTH
- 1;
185 hex_digits (const char *s
)
196 /* FIXME: allow newline in filename by encoding it. */
199 md5_file (const char *filename
, int binary
, unsigned char *md5_result
)
204 if (strcmp (filename
, "-") == 0)
211 /* OPENOPTS is a macro. It varies with the system.
212 Some systems distinguish between internal and
213 external text representations. */
215 fp
= fopen (filename
, OPENOPTS (binary
));
218 error (0, errno
, "%s", filename
);
223 err
= md5_stream (fp
, md5_result
);
226 error (0, errno
, "%s", filename
);
232 if (fp
!= stdin
&& fclose (fp
) == EOF
)
234 error (0, errno
, "%s", filename
);
242 md5_check (const char *checkfile_name
, int binary
)
244 FILE *checkfile_stream
;
245 int n_properly_formated_lines
= 0;
246 int n_mismatched_checksums
= 0;
247 int n_open_or_read_failures
= 0;
248 unsigned char md5buffer
[16];
251 size_t line_chars_allocated
;
253 if (strcmp (checkfile_name
, "-") == 0)
256 checkfile_name
= _("standard input");
257 checkfile_stream
= stdin
;
261 checkfile_stream
= fopen (checkfile_name
, "r");
262 if (checkfile_stream
== NULL
)
264 error (0, errno
, "%s", checkfile_name
);
271 line_chars_allocated
= 0;
282 line_length
= getline (&line
, &line_chars_allocated
, checkfile_stream
);
283 if (line_length
<= 0)
286 /* Ignore comment lines, which begin with a '#' character. */
290 /* Remove any trailing newline. */
291 if (line
[line_length
- 1] == '\n')
292 line
[--line_length
] = '\0';
294 err
= split_3 (line
, line_length
, &md5num
, &type_flag
, &filename
);
295 if (err
|| !hex_digits (md5num
))
300 _("%s: %lu: improperly formatted MD5 checksum line"),
301 checkfile_name
, (unsigned long) line_number
);
306 static const char bin2hex
[] = { '0', '1', '2', '3',
309 'c', 'd', 'e', 'f' };
312 ++n_properly_formated_lines
;
314 fail
= md5_file (filename
, binary
, md5buffer
);
318 ++n_open_or_read_failures
;
321 printf (_("%s: FAILED open or read\n"), filename
);
328 /* Compare generated binary number with text representation
329 in check file. Ignore case of hex digits. */
330 for (cnt
= 0; cnt
< 16; ++cnt
)
332 if (TOLOWER (md5num
[2 * cnt
]) != bin2hex
[md5buffer
[cnt
] >> 4]
333 || (TOLOWER (md5num
[2 * cnt
+ 1])
334 != (bin2hex
[md5buffer
[cnt
] & 0xf])))
338 ++n_mismatched_checksums
;
342 printf ("%s: %s\n", filename
,
343 (cnt
!= 16 ? _("FAILED") : _("OK")));
349 while (!feof (checkfile_stream
) && !ferror (checkfile_stream
));
354 if (ferror (checkfile_stream
))
356 error (0, 0, _("%s: read error"), checkfile_name
);
360 if (checkfile_stream
!= stdin
&& fclose (checkfile_stream
) == EOF
)
362 error (0, errno
, "%s", checkfile_name
);
366 if (n_properly_formated_lines
== 0)
368 /* Warn if no tests are found. */
369 error (0, 0, _("%s: no properly formatted MD5 checksum lines found"),
376 int n_computed_checkums
= (n_properly_formated_lines
377 - n_open_or_read_failures
);
379 if (n_open_or_read_failures
> 0)
382 _("WARNING: %d of %d listed %s could not be read\n"),
383 n_open_or_read_failures
, n_properly_formated_lines
,
384 (n_properly_formated_lines
== 1
385 ? _("file") : _("files")));
388 if (n_mismatched_checksums
> 0)
391 _("WARNING: %d of %d computed checksum%s did NOT match"),
392 n_mismatched_checksums
, n_computed_checkums
,
393 (n_computed_checkums
== 1 ? "" : "s"));
398 return ((n_properly_formated_lines
> 0 && n_mismatched_checksums
== 0
399 && n_open_or_read_failures
== 0) ? 0 : 1);
403 main (int argc
, char **argv
)
405 unsigned char md5buffer
[16];
409 char **string
= NULL
;
410 size_t n_strings
= 0;
414 /* Text is default of the Plumb/Lankester format. */
417 /* Setting values of global variables. */
418 program_name
= argv
[0];
419 setlocale (LC_ALL
, "");
420 bindtextdomain (PACKAGE
, LOCALEDIR
);
421 textdomain (PACKAGE
);
423 parse_long_options (argc
, argv
, "md5sum", PACKAGE_VERSION
, usage
);
425 while ((opt
= getopt_long (argc
, argv
, "bctw", long_options
, NULL
))
429 case 0: /* long option */
431 case 1: /* --string */
434 string
= (char **) xmalloc ((argc
- 1) * sizeof (char *));
438 string
[n_strings
++] = optarg
;
459 usage (EXIT_FAILURE
);
464 printf ("md5sum - %s\n", PACKAGE_VERSION
);
468 if (n_strings
> 0 && do_check
)
471 _("the --string and --check options are mutually exclusive"));
472 usage (EXIT_FAILURE
);
475 if (status_only
&& !do_check
)
478 _("the --status option is meaningful only when verifying checksums"));
479 usage (EXIT_FAILURE
);
482 if (warn
&& !do_check
)
485 _("the --warn option is meaningful only when verifying checksums"));
486 usage (EXIT_FAILURE
);
493 error (0, 0, _("no files may be specified when using --string"));
494 usage (EXIT_FAILURE
);
496 for (i
= 0; i
< n_strings
; ++i
)
499 md5_buffer (string
[i
], strlen (string
[i
]), md5buffer
);
501 for (cnt
= 0; cnt
< 16; ++cnt
)
502 printf ("%02x", md5buffer
[cnt
]);
504 printf (" \"%s\"\n", string
[i
]);
509 if (optind
+ 1 < argc
)
512 _("only one argument may be specified when using --check"));
513 usage (EXIT_FAILURE
);
516 err
= md5_check ((optind
== argc
) ? "-" : argv
[optind
], binary
);
523 for (; optind
< argc
; ++optind
)
527 char *file
= argv
[optind
];
529 fail
= md5_file (file
, binary
, md5buffer
);
535 for (i
= 0; i
< 16; ++i
)
536 printf ("%02x", md5buffer
[i
]);
544 /* Translate each NEWLINE byte to the string,
545 NEWLINE_REPLACEMENT_STRING. But first record
546 the length of the filename, FILE. */
547 filename_len
= strlen (file
);
548 for (i
= 0; i
< filename_len
; ++i
)
551 fputs (NEWLINE_REPLACEMENT_STRING
, stdout
);
560 if (fclose (stdout
) == EOF
)
561 error (EXIT_FAILURE
, errno
, _("write error"));
563 if (have_read_stdin
&& fclose (stdin
) == EOF
)
564 error (EXIT_FAILURE
, errno
, _("standard input"));
566 exit (err
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);