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 /* Nonzero if any of the files read were the standard input. */
63 static int have_read_stdin
;
65 /* With --check, don't generate any output.
66 The exit code indicates success or failure. */
67 static int status_only
= 0;
69 /* With --check, print a message to standard error warning about each
70 improperly formatted MD5 checksum line. */
73 /* The name this program was run with. */
76 static const struct option long_options
[] =
78 { "binary", no_argument
, 0, 'b' },
79 { "check", no_argument
, 0, 'c' },
80 { "status", no_argument
, 0, 2 },
81 { "string", required_argument
, 0, 1 },
82 { "text", no_argument
, 0, 't' },
83 { "warn", no_argument
, 0, 'w' },
93 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
97 Usage: %s [OPTION] [FILE]...\n\
98 or: %s [OPTION] --check [FILE]\n\
99 or: %s [OPTION] --string=STRING ...\n\
100 Print or check MD5 checksums.\n\
101 With no FILE, or when FILE is -, read standard input.\n\
103 -b, --binary read files in binary mode\n\
104 -t, --text read files in text mode (default)\n\
105 -c, --check check MD5 sums against given list\n\
107 The following two options are useful only when verifying checksums:\n\
108 --status don't output anything, status code shows success\n\
109 -w, --warn warn about improperly formated MD5 checksum lines\n\
111 --string=STRING compute checksum for STRING\n\
112 --help display this help and exit\n\
113 --version output version information and exit\n\
115 The sums are computed as described in RFC 1321. When checking, the input\n\
116 should be a former output of this program. The default mode is to print\n\
117 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
118 text), and name for each FILE.\n"),
119 program_name
, program_name
, program_name
);
121 exit (status
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);
124 /* FIXME: this format loses with filenames containing newline. */
127 split_3 (char *s
, char **u
, int *binary
, char **w
)
131 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
134 while (ISWHITE (s
[i
]))
137 /* The line has to be at least 35 characters long to contain correct
138 message digest information. */
139 if (strlen (&s
[i
]) >= 35)
143 /* The first field has to be the 32-character hexadecimal
144 representation of the message digest. If it not immediately
145 followed by a white space it's an error. */
152 if (s
[i
] != ' ' && s
[i
] != '*')
154 *binary
= s
[i
++] == '*';
156 /* All characters between the type indicator and end of line are
157 significant -- that includes leading and trailing white space. */
160 /* So this line is valid as long as there is at least one character
162 return (**w
? 0 : 1);
168 hex_digits (const char *s
)
179 /* FIXME: allow newline in filename by encoding it. */
182 md5_file (const char *filename
, int binary
, unsigned char *md5_result
)
187 if (strcmp (filename
, "-") == 0)
194 /* OPENOPTS is a macro. It varies with the system.
195 Some systems distinguish between internal and
196 external text representations. */
198 fp
= fopen (filename
, OPENOPTS (binary
));
201 error (0, errno
, "%s", filename
);
206 err
= md5_stream (fp
, md5_result
);
209 error (0, errno
, "%s", filename
);
215 if (fp
!= stdin
&& fclose (fp
) == EOF
)
217 error (0, errno
, "%s", filename
);
225 md5_check (const char *checkfile_name
, int binary
)
227 FILE *checkfile_stream
;
228 int n_properly_formated_lines
= 0;
229 int n_mismatched_checksums
= 0;
230 int n_open_or_read_failures
= 0;
231 unsigned char md5buffer
[16];
234 size_t line_chars_allocated
;
236 if (strcmp (checkfile_name
, "-") == 0)
239 checkfile_name
= _("standard input");
240 checkfile_stream
= stdin
;
244 checkfile_stream
= fopen (checkfile_name
, "r");
245 if (checkfile_stream
== NULL
)
247 error (0, errno
, "%s", checkfile_name
);
254 line_chars_allocated
= 0;
265 line_length
= getline (&line
, &line_chars_allocated
, checkfile_stream
);
266 if (line_length
<= 0)
269 /* Ignore comment lines, which begin with a '#' character. */
273 /* Remove any trailing newline. */
274 if (line
[line_length
- 1] == '\n')
275 line
[--line_length
] = '\0';
277 err
= split_3 (line
, &md5num
, &type_flag
, &filename
);
278 if (err
|| !hex_digits (md5num
))
283 _("%s: %lu: improperly formatted MD5 checksum line"),
284 checkfile_name
, (unsigned long) line_number
);
289 static const char bin2hex
[] = { '0', '1', '2', '3',
292 'c', 'd', 'e', 'f' };
295 ++n_properly_formated_lines
;
297 fail
= md5_file (filename
, binary
, md5buffer
);
301 ++n_open_or_read_failures
;
304 printf (_("%s: FAILED open or read\n"), filename
);
311 /* Compare generated binary number with text representation
312 in check file. Ignore case of hex digits. */
313 for (cnt
= 0; cnt
< 16; ++cnt
)
315 if (TOLOWER (md5num
[2 * cnt
]) != bin2hex
[md5buffer
[cnt
] >> 4]
316 || (TOLOWER (md5num
[2 * cnt
+ 1])
317 != (bin2hex
[md5buffer
[cnt
] & 0xf])))
321 ++n_mismatched_checksums
;
325 printf ("%s: %s\n", filename
,
326 (cnt
!= 16 ? _("FAILED") : _("OK")));
332 while (!feof (checkfile_stream
) && !ferror (checkfile_stream
));
337 if (ferror (checkfile_stream
))
339 error (0, 0, _("%s: read error"), checkfile_name
);
343 if (checkfile_stream
!= stdin
&& fclose (checkfile_stream
) == EOF
)
345 error (0, errno
, "%s", checkfile_name
);
349 if (n_properly_formated_lines
== 0)
351 /* Warn if no tests are found. */
352 error (0, 0, _("%s: no properly formatted MD5 checksum lines found"),
359 int n_computed_checkums
= (n_properly_formated_lines
360 - n_open_or_read_failures
);
362 if (n_open_or_read_failures
> 0)
365 _("WARNING: %d of %d listed %s could not be read\n"),
366 n_open_or_read_failures
, n_properly_formated_lines
,
367 (n_properly_formated_lines
== 1
368 ? _("file") : _("files")));
371 if (n_mismatched_checksums
> 0)
374 _("WARNING: %d of %d computed checksum%s did NOT match"),
375 n_mismatched_checksums
, n_computed_checkums
,
376 (n_computed_checkums
== 1 ? "" : "s"));
381 return ((n_properly_formated_lines
> 0 && n_mismatched_checksums
== 0
382 && n_open_or_read_failures
== 0) ? 0 : 1);
386 main (int argc
, char **argv
)
388 unsigned char md5buffer
[16];
392 char **string
= NULL
;
393 size_t n_strings
= 0;
397 /* Text is default of the Plumb/Lankester format. */
400 /* Setting values of global variables. */
401 program_name
= argv
[0];
402 setlocale (LC_ALL
, "");
403 bindtextdomain (PACKAGE
, LOCALEDIR
);
404 textdomain (PACKAGE
);
406 parse_long_options (argc
, argv
, "md5sum", PACKAGE_VERSION
, usage
);
408 while ((opt
= getopt_long (argc
, argv
, "bctw", long_options
, NULL
))
412 case 0: /* long option */
414 case 1: /* --string */
417 string
= (char **) xmalloc ((argc
- 1) * sizeof (char *));
421 string
[n_strings
++] = optarg
;
442 usage (EXIT_FAILURE
);
447 printf ("md5sum - %s\n", PACKAGE_VERSION
);
451 if (n_strings
> 0 && do_check
)
454 _("the --string and --check options are mutually exclusive"));
455 usage (EXIT_FAILURE
);
458 if (status_only
&& !do_check
)
461 _("the --status option is meaningful only when verifying checksums"));
462 usage (EXIT_FAILURE
);
465 if (warn
&& !do_check
)
468 _("the --warn option is meaningful only when verifying checksums"));
469 usage (EXIT_FAILURE
);
476 error (0, 0, _("no files may be specified when using --string"));
477 usage (EXIT_FAILURE
);
479 for (i
= 0; i
< n_strings
; ++i
)
482 md5_buffer (string
[i
], strlen (string
[i
]), md5buffer
);
484 for (cnt
= 0; cnt
< 16; ++cnt
)
485 printf ("%02x", md5buffer
[cnt
]);
487 printf (" \"%s\"\n", string
[i
]);
492 if (optind
+ 1 < argc
)
495 _("only one argument may be specified when using --check"));
496 usage (EXIT_FAILURE
);
499 err
= md5_check ((optind
== argc
) ? "-" : argv
[optind
], binary
);
506 for (; optind
< argc
; ++optind
)
511 fail
= md5_file (argv
[optind
], binary
, md5buffer
);
515 for (i
= 0; i
< 16; ++i
)
516 printf ("%02x", md5buffer
[i
]);
518 printf (" %c%s\n", binary
? '*' : ' ', argv
[optind
]);
523 if (fclose (stdout
) == EOF
)
524 error (EXIT_FAILURE
, errno
, _("write error"));
526 if (have_read_stdin
&& fclose (stdin
) == EOF
)
527 error (EXIT_FAILURE
, errno
, _("standard input"));
529 exit (err
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
);