(split_3): Take an additional parameter, S_LEN.
[platform/upstream/coreutils.git] / src / md5sum.c
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.
4
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)
8    any later version.
9
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.
14
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.  */
18
19 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include <getopt.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28
29 #include "long-options.h"
30 #include "md5.h"
31 #include "getline.h"
32 #include "system.h"
33 #include "error.h"
34
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"
39 #else
40 # ifdef MSDOS
41 #  define TEXT1TO1 "rb"
42 #  define TEXTCNVT "r"
43 # else
44 #  if defined VMS
45 #   define TEXT1TO1 "rb", "ctx=stm"
46 #   define TEXTCNVT "r", "ctx=stm"
47 #  else
48     /* The following line is intended to evoke an error.
49        Using #error is not portable enough.  */
50     "Cannot determine system type."
51 #  endif
52 # endif
53 # define OPENOPTS(BINARY) ((BINARY) != 0 ? TEXT1TO1 : TEXTCNVT)
54 #endif
55
56 #if _LIBC || STDC_HEADERS
57 # define TOLOWER(c) tolower (c)
58 #else
59 # define TOLOWER(c) (ISUPPER (c) ? tolower (c) : (c))
60 #endif
61
62 /* Nonzero if any of the files read were the standard input. */
63 static int have_read_stdin;
64
65 /* With --check, don't generate any output.
66    The exit code indicates success or failure.  */
67 static int status_only = 0;
68
69 /* With --check, print a message to standard error warning about each
70    improperly formatted MD5 checksum line.  */
71 static int warn = 0;
72
73 /* The name this program was run with.  */
74 char *program_name;
75
76 static const struct option long_options[] =
77 {
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' },
84   { NULL, 0, NULL, 0 }
85 };
86
87 char *xmalloc ();
88
89 static void
90 usage (int status)
91 {
92   if (status != 0)
93     fprintf (stderr, _("Try `%s --help' for more information.\n"),
94              program_name);
95   else
96     printf (_("\
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\
102 \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\
106 \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\
110 \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\
114 \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);
120
121   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
122 }
123
124 static int
125 split_3 (char *s, size_t s_len, char **u, int *binary, char **w)
126 {
127   size_t i;
128
129 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
130
131   i = 0;
132   while (ISWHITE (s[i]))
133     ++i;
134
135   /* The line has to be at least 35 characters long to contain correct
136      message digest information.  */
137   if (s_len >= 32 + 2 + 1)
138     {
139       *u = &s[i];
140
141       /* The first field has to be the 32-character hexadecimal
142          representation of the message digest.  If it is not followed
143          immediately by a white space it's an error.  */
144       i += 32;
145       if (!ISWHITE (s[i]))
146         return 1;
147
148       s[i++] = '\0';
149
150       if (s[i] != ' ' && s[i] != '*')
151         return 1;
152       *binary = (s[i++] == '*');
153
154       /* All characters between the type indicator and end of line are
155          significant -- that includes leading and trailing white space.  */
156       *w = &s[i];
157
158       /* Translate each NUL byte in the file name to a NEWLINE.
159          But not the last.  */
160       for (/* empty */ ; i < s_len; ++i)
161         {
162           if (s[i] == '\0')
163             s[i] = '\n';
164         }
165       return 0;
166     }
167   return 1;
168 }
169
170 static int
171 hex_digits (const char *s)
172 {
173   while (*s)
174     {
175       if (!ISXDIGIT (*s))
176         return 0;
177       ++s;
178     }
179   return 1;
180 }
181
182 /* FIXME: allow newline in filename by encoding it. */
183
184 static int
185 md5_file (const char *filename, int binary, unsigned char *md5_result)
186 {
187   FILE *fp;
188   int err;
189
190   if (strcmp (filename, "-") == 0)
191     {
192       have_read_stdin = 1;
193       fp = stdin;
194     }
195   else
196     {
197       /* OPENOPTS is a macro.  It varies with the system.
198          Some systems distinguish between internal and
199          external text representations.  */
200
201       fp = fopen (filename, OPENOPTS (binary));
202       if (fp == NULL)
203         {
204           error (0, errno, "%s", filename);
205           return 1;
206         }
207     }
208
209   err = md5_stream (fp, md5_result);
210   if (err)
211     {
212       error (0, errno, "%s", filename);
213       if (fp != stdin)
214         fclose (fp);
215       return 1;
216     }
217
218   if (fp != stdin && fclose (fp) == EOF)
219     {
220       error (0, errno, "%s", filename);
221       return 1;
222     }
223
224   return 0;
225 }
226
227 static int
228 md5_check (const char *checkfile_name, int binary)
229 {
230   FILE *checkfile_stream;
231   int n_properly_formated_lines = 0;
232   int n_mismatched_checksums = 0;
233   int n_open_or_read_failures = 0;
234   unsigned char md5buffer[16];
235   size_t line_number;
236   char *line;
237   size_t line_chars_allocated;
238
239   if (strcmp (checkfile_name, "-") == 0)
240     {
241       have_read_stdin = 1;
242       checkfile_name = _("standard input");
243       checkfile_stream = stdin;
244     }
245   else
246     {
247       checkfile_stream = fopen (checkfile_name, "r");
248       if (checkfile_stream == NULL)
249         {
250           error (0, errno, "%s", checkfile_name);
251           return 1;
252         }
253     }
254
255   line_number = 0;
256   line = NULL;
257   line_chars_allocated = 0;
258   do
259     {
260       char *filename;
261       int type_flag;
262       char *md5num;
263       int err;
264       int line_length;
265
266       ++line_number;
267
268       line_length = getline (&line, &line_chars_allocated, checkfile_stream);
269       if (line_length <= 0)
270         break;
271
272       /* Ignore comment lines, which begin with a '#' character.  */
273       if (line[0] == '#')
274         continue;
275
276       /* Remove any trailing newline.  */
277       if (line[line_length - 1] == '\n')
278         line[--line_length] = '\0';
279
280       /* FIXME: filename might contain NUL bytes.  Map then to NEWLINEs
281          in split_3.  */
282       err = split_3 (line, line_length, &md5num, &type_flag, &filename);
283       if (err || !hex_digits (md5num))
284         {
285           if (warn)
286             {
287               error (0, 0,
288                      _("%s: %lu: improperly formatted MD5 checksum line"),
289                      checkfile_name, (unsigned long) line_number);
290             }
291         }
292       else
293         {
294           static const char bin2hex[] = { '0', '1', '2', '3',
295                                           '4', '5', '6', '7',
296                                           '8', '9', 'a', 'b',
297                                           'c', 'd', 'e', 'f' };
298           int fail;
299
300           ++n_properly_formated_lines;
301
302           fail = md5_file (filename, binary, md5buffer);
303
304           if (fail)
305             {
306               ++n_open_or_read_failures;
307               if (!status_only)
308                 {
309                   printf (_("%s: FAILED open or read\n"), filename);
310                   fflush (stdout);
311                 }
312             }
313           else
314             {
315               size_t cnt;
316               /* Compare generated binary number with text representation
317                  in check file.  Ignore case of hex digits.  */
318               for (cnt = 0; cnt < 16; ++cnt)
319                 {
320                   if (TOLOWER (md5num[2 * cnt]) != bin2hex[md5buffer[cnt] >> 4]
321                       || (TOLOWER (md5num[2 * cnt + 1])
322                           != (bin2hex[md5buffer[cnt] & 0xf])))
323                     break;
324                 }
325               if (cnt != 16)
326                 ++n_mismatched_checksums;
327
328               if (!status_only)
329                 {
330                   printf ("%s: %s\n", filename,
331                           (cnt != 16 ? _("FAILED") : _("OK")));
332                   fflush (stdout);
333                 }
334             }
335         }
336     }
337   while (!feof (checkfile_stream) && !ferror (checkfile_stream));
338
339   if (line)
340     free (line);
341
342   if (ferror (checkfile_stream))
343     {
344       error (0, 0, _("%s: read error"), checkfile_name);
345       return 1;
346     }
347
348   if (checkfile_stream != stdin && fclose (checkfile_stream) == EOF)
349     {
350       error (0, errno, "%s", checkfile_name);
351       return 1;
352     }
353
354   if (n_properly_formated_lines == 0)
355     {
356       /* Warn if no tests are found.  */
357       error (0, 0, _("%s: no properly formatted MD5 checksum lines found"),
358              checkfile_name);
359     }
360   else
361     {
362       if (!status_only)
363         {
364           int n_computed_checkums = (n_properly_formated_lines
365                                      - n_open_or_read_failures);
366
367           if (n_open_or_read_failures > 0)
368             {
369               error (0, 0,
370                    _("WARNING: %d of %d listed %s could not be read\n"),
371                      n_open_or_read_failures, n_properly_formated_lines,
372                      (n_properly_formated_lines == 1
373                       ? _("file") : _("files")));
374             }
375
376           if (n_mismatched_checksums > 0)
377             {
378               error (0, 0,
379                    _("WARNING: %d of %d computed checksum%s did NOT match"),
380                      n_mismatched_checksums, n_computed_checkums,
381                      (n_computed_checkums == 1 ? "" : "s"));
382             }
383         }
384     }
385
386   return ((n_properly_formated_lines > 0 && n_mismatched_checksums == 0
387            && n_open_or_read_failures == 0) ? 0 : 1);
388 }
389
390 int
391 main (int argc, char **argv)
392 {
393   unsigned char md5buffer[16];
394   int do_check = 0;
395   int do_version = 0;
396   int opt;
397   char **string = NULL;
398   size_t n_strings = 0;
399   size_t i;
400   size_t err = 0;
401
402   /* Text is default of the Plumb/Lankester format.  */
403   int binary = 0;
404
405   /* Setting values of global variables.  */
406   program_name = argv[0];
407   setlocale (LC_ALL, "");
408   bindtextdomain (PACKAGE, LOCALEDIR);
409   textdomain (PACKAGE);
410
411   parse_long_options (argc, argv, "md5sum", PACKAGE_VERSION, usage);
412
413   while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL))
414          != EOF)
415     switch (opt)
416       {
417       case 0:                   /* long option */
418         break;
419       case 1: /* --string */
420         {
421           if (string == NULL)
422             string = (char **) xmalloc ((argc - 1) * sizeof (char *));
423
424           if (optarg == NULL)
425             optarg = "";
426           string[n_strings++] = optarg;
427         }
428         break;
429       case 'b':
430         binary = 1;
431         break;
432       case 'c':
433         do_check = 1;
434         break;
435       case 2:
436         status_only = 1;
437         warn = 0;
438         break;
439       case 't':
440         binary = 0;
441         break;
442       case 'w':
443         status_only = 0;
444         warn = 1;
445         break;
446       default:
447         usage (EXIT_FAILURE);
448       }
449
450   if (do_version)
451     {
452       printf ("md5sum - %s\n", PACKAGE_VERSION);
453       exit (EXIT_SUCCESS);
454     }
455
456   if (n_strings > 0 && do_check)
457     {
458       error (0, 0,
459              _("the --string and --check options are mutually exclusive"));
460       usage (EXIT_FAILURE);
461     }
462
463   if (status_only && !do_check)
464     {
465       error (0, 0,
466        _("the --status option is meaningful only when verifying checksums"));
467       usage (EXIT_FAILURE);
468     }
469
470   if (warn && !do_check)
471     {
472       error (0, 0,
473        _("the --warn option is meaningful only when verifying checksums"));
474       usage (EXIT_FAILURE);
475     }
476
477   if (n_strings > 0)
478     {
479       if (optind < argc)
480         {
481           error (0, 0, _("no files may be specified when using --string"));
482           usage (EXIT_FAILURE);
483         }
484       for (i = 0; i < n_strings; ++i)
485         {
486           size_t cnt;
487           md5_buffer (string[i], strlen (string[i]), md5buffer);
488
489           for (cnt = 0; cnt < 16; ++cnt)
490             printf ("%02x", md5buffer[cnt]);
491
492           printf ("  \"%s\"\n", string[i]);
493         }
494     }
495   else if (do_check)
496     {
497       if (optind + 1 < argc)
498         {
499           error (0, 0,
500                  _("only one argument may be specified when using --check"));
501           usage (EXIT_FAILURE);
502         }
503
504       err = md5_check ((optind == argc) ? "-" : argv[optind], binary);
505     }
506   else
507     {
508       if (optind == argc)
509         argv[argc++] = "-";
510
511       for (; optind < argc; ++optind)
512         {
513           size_t i;
514           int fail;
515           char *file = argv[optind];
516
517           fail = md5_file (file, binary, md5buffer);
518           err |= fail;
519           if (!fail)
520             {
521               size_t filename_len;
522
523               if (strchr (file, '\n'))
524                 error (0, 0,
525                        _("\
526 warning: filename contains a NEWLINE character `%s';  \
527 you will not be able to verify this checksum using `md5sum --check'"),
528                        file);
529
530               for (i = 0; i < 16; ++i)
531                 printf ("%02x", md5buffer[i]);
532
533               putchar (' ');
534               if (binary)
535                 putchar ('*');
536               else
537                 putchar (' ');
538
539               /* Translate NEWLINE bytes to NUL bytes.
540                  But first record the length of the filename, FILE.  */
541               filename_len = strlen (file);
542               for (i = 0; i < strlen (file); ++i)
543                 {
544                   if (file[i] == '\n')
545                     file[i] = '\0';
546                 }
547               /* Use fwrite, not printf, to output FILE --
548                  now it may contain NUL bytes.  */
549               fwrite (file, sizeof (char), filename_len, stdout);
550               putchar ('\n');
551             }
552         }
553     }
554
555   if (fclose (stdout) == EOF)
556     error (EXIT_FAILURE, errno, _("write error"));
557
558   if (have_read_stdin && fclose (stdin) == EOF)
559     error (EXIT_FAILURE, errno, _("standard input"));
560
561   exit (err == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
562 }