Tizen 2.0 Release
[external/tizen-coreutils.git] / src / md5sum.c
1 /* Compute MD5, SHA1, SHA224, SHA256, SHA384 or SHA512 checksum of files or strings
2    Copyright (C) 1995-2006 Free Software Foundation, Inc.
3
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.
8
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.
13
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu>.  */
19
20 #include <config.h>
21
22 #include <getopt.h>
23 #include <sys/types.h>
24
25 #include "system.h"
26
27 #if HASH_ALGO_MD5
28 # include "md5.h"
29 #endif
30 #if HASH_ALGO_SHA1
31 # include "sha1.h"
32 #endif
33 #if HASH_ALGO_SHA256 || HASH_ALGO_SHA224
34 # include "sha256.h"
35 #endif
36 #if HASH_ALGO_SHA512 || HASH_ALGO_SHA384
37 # include "sha512.h"
38 #endif
39 #include "getline.h"
40 #include "error.h"
41 #include "quote.h"
42 #include "stdio--.h"
43
44 /* The official name of this program (e.g., no `g' prefix).  */
45 #if HASH_ALGO_MD5
46 # define PROGRAM_NAME "md5sum"
47 # define DIGEST_TYPE_STRING "MD5"
48 # define DIGEST_STREAM md5_stream
49 # define DIGEST_BITS 128
50 # define DIGEST_REFERENCE "RFC 1321"
51 # define DIGEST_ALIGN 4
52 #elif HASH_ALGO_SHA1
53 # define PROGRAM_NAME "sha1sum"
54 # define DIGEST_TYPE_STRING "SHA1"
55 # define DIGEST_STREAM sha1_stream
56 # define DIGEST_BITS 160
57 # define DIGEST_REFERENCE "FIPS-180-1"
58 # define DIGEST_ALIGN 4
59 #elif HASH_ALGO_SHA256
60 # define PROGRAM_NAME "sha256sum"
61 # define DIGEST_TYPE_STRING "SHA256"
62 # define DIGEST_STREAM sha256_stream
63 # define DIGEST_BITS 256
64 # define DIGEST_REFERENCE "FIPS-180-2"
65 # define DIGEST_ALIGN 4
66 #elif HASH_ALGO_SHA224
67 # define PROGRAM_NAME "sha224sum"
68 # define DIGEST_TYPE_STRING "SHA224"
69 # define DIGEST_STREAM sha224_stream
70 # define DIGEST_BITS 224
71 # define DIGEST_REFERENCE "RFC 3874"
72 # define DIGEST_ALIGN 4
73 #elif HASH_ALGO_SHA512
74 # define PROGRAM_NAME "sha512sum"
75 # define DIGEST_TYPE_STRING "SHA512"
76 # define DIGEST_STREAM sha512_stream
77 # define DIGEST_BITS 512
78 # define DIGEST_REFERENCE "FIPS-180-2"
79 # define DIGEST_ALIGN 8
80 #elif HASH_ALGO_SHA384
81 # define PROGRAM_NAME "sha384sum"
82 # define DIGEST_TYPE_STRING "SHA384"
83 # define DIGEST_STREAM sha384_stream
84 # define DIGEST_BITS 384
85 # define DIGEST_REFERENCE "FIPS-180-2"
86 # define DIGEST_ALIGN 8
87 #else
88 # error "Can't decide which hash algorithm to compile."
89 #endif
90
91 #define DIGEST_HEX_BYTES (DIGEST_BITS / 4)
92 #define DIGEST_BIN_BYTES (DIGEST_BITS / 8)
93
94 #define AUTHORS "Ulrich Drepper", "Scott Miller", "David Madore"
95
96 /* The minimum length of a valid digest line.  This length does
97    not include any newline character at the end of a line.  */
98 #define MIN_DIGEST_LINE_LENGTH \
99   (DIGEST_HEX_BYTES /* length of hexadecimal message digest */ \
100    + 2 /* blank and binary indicator */ \
101    + 1 /* minimum filename length */ )
102
103 /* True if any of the files read were the standard input. */
104 static bool have_read_stdin;
105
106 /* The minimum length of a valid checksum line for the selected algorithm.  */
107 static size_t min_digest_line_length;
108
109 /* Set to the length of a digest hex string for the selected algorithm.  */
110 static size_t digest_hex_bytes;
111
112 /* With --check, don't generate any output.
113    The exit code indicates success or failure.  */
114 static bool status_only = false;
115
116 /* With --check, print a message to standard error warning about each
117    improperly formatted checksum line.  */
118 static bool warn = false;
119
120 /* The name this program was run with.  */
121 char *program_name;
122
123 /* For long options that have no equivalent short option, use a
124    non-character as a pseudo short option, starting with CHAR_MAX + 1.  */
125 enum
126 {
127   STATUS_OPTION = CHAR_MAX + 1
128 };
129
130 static const struct option long_options[] =
131 {
132   { "binary", no_argument, NULL, 'b' },
133   { "check", no_argument, NULL, 'c' },
134   { "status", no_argument, NULL, STATUS_OPTION },
135   { "text", no_argument, NULL, 't' },
136   { "warn", no_argument, NULL, 'w' },
137   { GETOPT_HELP_OPTION_DECL },
138   { GETOPT_VERSION_OPTION_DECL },
139   { NULL, 0, NULL, 0 }
140 };
141
142 void
143 usage (int status)
144 {
145   if (status != EXIT_SUCCESS)
146     fprintf (stderr, _("Try `%s --help' for more information.\n"),
147              program_name);
148   else
149     {
150       printf (_("\
151 Usage: %s [OPTION] [FILE]...\n\
152 Print or check %s (%d-bit) checksums.\n\
153 With no FILE, or when FILE is -, read standard input.\n\
154 \n\
155 "),
156               program_name,
157               DIGEST_TYPE_STRING,
158               DIGEST_BITS);
159       if (O_BINARY)
160         fputs (_("\
161   -b, --binary            read in binary mode (default unless reading tty stdin)\n\
162 "), stdout);
163       else
164         fputs (_("\
165   -b, --binary            read in binary mode\n\
166 "), stdout);
167       printf (_("\
168   -c, --check             read %s sums from the FILEs and check them\n"),
169               DIGEST_TYPE_STRING);
170       if (O_BINARY)
171         fputs (_("\
172   -t, --text              read in text mode (default if reading tty stdin)\n\
173 "), stdout);
174       else
175         fputs (_("\
176   -t, --text              read in text mode (default)\n\
177 "), stdout);
178       fputs (_("\
179 \n\
180 The following two options are useful only when verifying checksums:\n\
181       --status            don't output anything, status code shows success\n\
182   -w, --warn              warn about improperly formatted checksum lines\n\
183 \n\
184 "), stdout);
185       fputs (HELP_OPTION_DESCRIPTION, stdout);
186       fputs (VERSION_OPTION_DESCRIPTION, stdout);
187       printf (_("\
188 \n\
189 The sums are computed as described in %s.  When checking, the input\n\
190 should be a former output of this program.  The default mode is to print\n\
191 a line with checksum, a character indicating type (`*' for binary, ` ' for\n\
192 text), and name for each FILE.\n"),
193               DIGEST_REFERENCE);
194       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
195     }
196
197   exit (status);
198 }
199
200 #define ISWHITE(c) ((c) == ' ' || (c) == '\t')
201
202 /* Split the checksum string S (of length S_LEN) from a BSD 'md5' or
203    'sha1' command into two parts: a hexadecimal digest, and the file
204    name.  S is modified.  Return true if successful.  */
205
206 static bool
207 bsd_split_3 (char *s, size_t s_len, unsigned char **hex_digest, char **file_name)
208 {
209   size_t i;
210
211   *file_name = s;
212
213   /* Find end of filename. The BSD 'md5' and 'sha1' commands do not escape
214      filenames, so search backwards for the last ')'. */
215   i = s_len - 1;
216   while (i && s[i] != ')')
217     i--;
218
219   if (s[i] != ')')
220     return false;
221
222   s[i++] = '\0';
223
224   while (ISWHITE (s[i]))
225     i++;
226
227   if (s[i] != '=')
228     return false;
229
230   i++;
231
232   while (ISWHITE (s[i]))
233     i++;
234
235   *hex_digest = (unsigned char *) &s[i];
236   return true;
237 }
238
239 /* Split the string S (of length S_LEN) into three parts:
240    a hexadecimal digest, binary flag, and the file name.
241    S is modified.  Return true if successful.  */
242
243 static bool
244 split_3 (char *s, size_t s_len,
245          unsigned char **hex_digest, int *binary, char **file_name)
246 {
247   size_t i;
248   bool escaped_filename = false;
249   size_t algo_name_len;
250
251   i = 0;
252   while (ISWHITE (s[i]))
253     ++i;
254
255   /* Check for BSD-style checksum line. */
256   algo_name_len = strlen (DIGEST_TYPE_STRING);
257   if (strncmp (s + i, DIGEST_TYPE_STRING, algo_name_len) == 0)
258     {
259       if (strncmp (s + i + algo_name_len, " (", 2) == 0)
260         {
261           *binary = 0;
262           return bsd_split_3 (s +      i + algo_name_len + 2,
263                               s_len - (i + algo_name_len + 2),
264                               hex_digest, file_name);
265         }
266     }
267
268   /* Ignore this line if it is too short.
269      Each line must have at least `min_digest_line_length - 1' (or one more, if
270      the first is a backslash) more characters to contain correct message digest
271      information.  */
272   if (s_len - i < min_digest_line_length + (s[i] == '\\'))
273     return false;
274
275   if (s[i] == '\\')
276     {
277       ++i;
278       escaped_filename = true;
279     }
280   *hex_digest = (unsigned char *) &s[i];
281
282   /* The first field has to be the n-character hexadecimal
283      representation of the message digest.  If it is not followed
284      immediately by a white space it's an error.  */
285   i += digest_hex_bytes;
286   if (!ISWHITE (s[i]))
287     return false;
288
289   s[i++] = '\0';
290
291   if (s[i] != ' ' && s[i] != '*')
292     return false;
293   *binary = (s[i++] == '*');
294
295   /* All characters between the type indicator and end of line are
296      significant -- that includes leading and trailing white space.  */
297   *file_name = &s[i];
298
299   if (escaped_filename)
300     {
301       /* Translate each `\n' string in the file name to a NEWLINE,
302          and each `\\' string to a backslash.  */
303
304       char *dst = &s[i];
305
306       while (i < s_len)
307         {
308           switch (s[i])
309             {
310             case '\\':
311               if (i == s_len - 1)
312                 {
313                   /* A valid line does not end with a backslash.  */
314                   return false;
315                 }
316               ++i;
317               switch (s[i++])
318                 {
319                 case 'n':
320                   *dst++ = '\n';
321                   break;
322                 case '\\':
323                   *dst++ = '\\';
324                   break;
325                 default:
326                   /* Only `\' or `n' may follow a backslash.  */
327                   return false;
328                 }
329               break;
330
331             case '\0':
332               /* The file name may not contain a NUL.  */
333               return false;
334               break;
335
336             default:
337               *dst++ = s[i++];
338               break;
339             }
340         }
341       *dst = '\0';
342     }
343   return true;
344 }
345
346 static bool
347 hex_digits (unsigned char const *s)
348 {
349   while (*s)
350     {
351       if (!isxdigit (*s))
352         return false;
353       ++s;
354     }
355   return true;
356 }
357
358 /* An interface to the function, DIGEST_STREAM.
359    Operate on FILENAME (it may be "-").
360
361    *BINARY indicates whether the file is binary.  BINARY < 0 means it
362    depends on whether binary mode makes any difference and the file is
363    a terminal; in that case, clear *BINARY if the file was treated as
364    text because it was a terminal.
365
366    Put the checksum in *BIN_RESULT, which must be properly aligned.
367    Return true if successful.  */
368
369 static bool
370 digest_file (const char *filename, int *binary, unsigned char *bin_result)
371 {
372   FILE *fp;
373   int err;
374   bool is_stdin = STREQ (filename, "-");
375
376   if (is_stdin)
377     {
378       have_read_stdin = true;
379       fp = stdin;
380       if (O_BINARY && *binary)
381         {
382           if (*binary < 0)
383             *binary = ! isatty (STDIN_FILENO);
384           if (*binary)
385             freopen (NULL, "rb", stdin);
386         }
387     }
388   else
389     {
390       fp = fopen (filename, (O_BINARY && *binary ? "rb" : "r"));
391       if (fp == NULL)
392         {
393           error (0, errno, "%s", filename);
394           return false;
395         }
396     }
397
398   err = DIGEST_STREAM (fp, bin_result);
399   if (err)
400     {
401       error (0, errno, "%s", filename);
402       if (fp != stdin)
403         fclose (fp);
404       return false;
405     }
406
407   if (!is_stdin && fclose (fp) != 0)
408     {
409       error (0, errno, "%s", filename);
410       return false;
411     }
412
413   return true;
414 }
415
416 static bool
417 digest_check (const char *checkfile_name)
418 {
419   FILE *checkfile_stream;
420   uintmax_t n_properly_formatted_lines = 0;
421   uintmax_t n_mismatched_checksums = 0;
422   uintmax_t n_open_or_read_failures = 0;
423   unsigned char bin_buffer_unaligned[DIGEST_BIN_BYTES + DIGEST_ALIGN];
424   /* Make sure bin_buffer is properly aligned. */
425   unsigned char *bin_buffer = ptr_align (bin_buffer_unaligned, DIGEST_ALIGN);
426   uintmax_t line_number;
427   char *line;
428   size_t line_chars_allocated;
429   bool is_stdin = STREQ (checkfile_name, "-");
430
431   if (is_stdin)
432     {
433       have_read_stdin = true;
434       checkfile_name = _("standard input");
435       checkfile_stream = stdin;
436     }
437   else
438     {
439       checkfile_stream = fopen (checkfile_name, "r");
440       if (checkfile_stream == NULL)
441         {
442           error (0, errno, "%s", checkfile_name);
443           return false;
444         }
445     }
446
447   line_number = 0;
448   line = NULL;
449   line_chars_allocated = 0;
450   do
451     {
452       char *filename;
453       int binary;
454       unsigned char *hex_digest IF_LINT (= NULL);
455       ssize_t line_length;
456
457       ++line_number;
458       if (line_number == 0)
459         error (EXIT_FAILURE, 0, _("%s: too many checksum lines"),
460                checkfile_name);
461
462       line_length = getline (&line, &line_chars_allocated, checkfile_stream);
463       if (line_length <= 0)
464         break;
465
466       /* Ignore comment lines, which begin with a '#' character.  */
467       if (line[0] == '#')
468         continue;
469
470       /* Remove any trailing newline.  */
471       if (line[line_length - 1] == '\n')
472         line[--line_length] = '\0';
473
474       if (! (split_3 (line, line_length, &hex_digest, &binary, &filename)
475              && ! (is_stdin && STREQ (filename, "-"))
476              && hex_digits (hex_digest)))
477         {
478           if (warn)
479             {
480               error (0, 0,
481                      _("%s: %" PRIuMAX
482                        ": improperly formatted %s checksum line"),
483                      checkfile_name, line_number,
484                      DIGEST_TYPE_STRING);
485             }
486         }
487       else
488         {
489           static const char bin2hex[] = { '0', '1', '2', '3',
490                                           '4', '5', '6', '7',
491                                           '8', '9', 'a', 'b',
492                                           'c', 'd', 'e', 'f' };
493           bool ok;
494
495           ++n_properly_formatted_lines;
496
497           ok = digest_file (filename, &binary, bin_buffer);
498
499           if (!ok)
500             {
501               ++n_open_or_read_failures;
502               if (!status_only)
503                 {
504                   printf (_("%s: FAILED open or read\n"), filename);
505                   fflush (stdout);
506                 }
507             }
508           else
509             {
510               size_t digest_bin_bytes = digest_hex_bytes / 2;
511               size_t cnt;
512               /* Compare generated binary number with text representation
513                  in check file.  Ignore case of hex digits.  */
514               for (cnt = 0; cnt < digest_bin_bytes; ++cnt)
515                 {
516                   if (tolower (hex_digest[2 * cnt])
517                       != bin2hex[bin_buffer[cnt] >> 4]
518                       || (tolower (hex_digest[2 * cnt + 1])
519                           != (bin2hex[bin_buffer[cnt] & 0xf])))
520                     break;
521                 }
522               if (cnt != digest_bin_bytes)
523                 ++n_mismatched_checksums;
524
525               if (!status_only)
526                 {
527                   printf ("%s: %s\n", filename,
528                           (cnt != digest_bin_bytes ? _("FAILED") : _("OK")));
529                   fflush (stdout);
530                 }
531             }
532         }
533     }
534   while (!feof (checkfile_stream) && !ferror (checkfile_stream));
535
536   free (line);
537
538   if (ferror (checkfile_stream))
539     {
540       error (0, 0, _("%s: read error"), checkfile_name);
541       return false;
542     }
543
544   if (!is_stdin && fclose (checkfile_stream) != 0)
545     {
546       error (0, errno, "%s", checkfile_name);
547       return false;
548     }
549
550   if (n_properly_formatted_lines == 0)
551     {
552       /* Warn if no tests are found.  */
553       error (0, 0, _("%s: no properly formatted %s checksum lines found"),
554              checkfile_name, DIGEST_TYPE_STRING);
555     }
556   else
557     {
558       if (!status_only)
559         {
560           if (n_open_or_read_failures != 0)
561             error (0, 0,
562                    ngettext ("WARNING: %" PRIuMAX " of %" PRIuMAX
563                              " listed file could not be read",
564                              "WARNING: %" PRIuMAX " of %" PRIuMAX
565                              " listed files could not be read",
566                              select_plural (n_properly_formatted_lines)),
567                    n_open_or_read_failures, n_properly_formatted_lines);
568
569           if (n_mismatched_checksums != 0)
570             {
571               uintmax_t n_computed_checksums =
572                 (n_properly_formatted_lines - n_open_or_read_failures);
573               error (0, 0,
574                      ngettext ("WARNING: %" PRIuMAX " of %" PRIuMAX
575                                " computed checksum did NOT match",
576                                "WARNING: %" PRIuMAX " of %" PRIuMAX
577                                " computed checksums did NOT match",
578                                select_plural (n_computed_checksums)),
579                      n_mismatched_checksums, n_computed_checksums);
580             }
581         }
582     }
583
584   return (n_properly_formatted_lines != 0
585           && n_mismatched_checksums == 0
586           && n_open_or_read_failures == 0);
587 }
588
589 int
590 main (int argc, char **argv)
591 {
592   unsigned char bin_buffer_unaligned[DIGEST_BIN_BYTES + DIGEST_ALIGN];
593   /* Make sure bin_buffer is properly aligned. */
594   unsigned char *bin_buffer = ptr_align (bin_buffer_unaligned, DIGEST_ALIGN);
595   bool do_check = false;
596   int opt;
597   bool ok = true;
598   int binary = -1;
599
600   /* Setting values of global variables.  */
601   initialize_main (&argc, &argv);
602   program_name = argv[0];
603   setlocale (LC_ALL, "");
604   bindtextdomain (PACKAGE, LOCALEDIR);
605   textdomain (PACKAGE);
606
607   atexit (close_stdout);
608
609   while ((opt = getopt_long (argc, argv, "bctw", long_options, NULL)) != -1)
610     switch (opt)
611       {
612       case 'b':
613         binary = 1;
614         break;
615       case 'c':
616         do_check = true;
617         break;
618       case STATUS_OPTION:
619         status_only = true;
620         warn = false;
621         break;
622       case 't':
623         binary = 0;
624         break;
625       case 'w':
626         status_only = false;
627         warn = true;
628         break;
629       case_GETOPT_HELP_CHAR;
630       case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
631       default:
632         usage (EXIT_FAILURE);
633       }
634
635   min_digest_line_length = MIN_DIGEST_LINE_LENGTH;
636   digest_hex_bytes = DIGEST_HEX_BYTES;
637
638   if (0 <= binary && do_check)
639     {
640       error (0, 0, _("the --binary and --text options are meaningless when "
641                      "verifying checksums"));
642       usage (EXIT_FAILURE);
643     }
644
645   if (status_only & !do_check)
646     {
647       error (0, 0,
648        _("the --status option is meaningful only when verifying checksums"));
649       usage (EXIT_FAILURE);
650     }
651
652   if (warn & !do_check)
653     {
654       error (0, 0,
655        _("the --warn option is meaningful only when verifying checksums"));
656       usage (EXIT_FAILURE);
657     }
658
659   if (!O_BINARY && binary < 0)
660     binary = 0;
661
662   if (optind == argc)
663     argv[argc++] = "-";
664
665   for (; optind < argc; ++optind)
666     {
667       char *file = argv[optind];
668
669       if (do_check)
670         ok &= digest_check (file);
671       else
672         {
673           int file_is_binary = binary;
674
675           if (! digest_file (file, &file_is_binary, bin_buffer))
676             ok = false;
677           else
678             {
679               size_t i;
680
681               /* Output a leading backslash if the file name contains
682                  a newline or backslash.  */
683               if (strchr (file, '\n') || strchr (file, '\\'))
684                 putchar ('\\');
685
686               for (i = 0; i < (digest_hex_bytes / 2); ++i)
687                 printf ("%02x", bin_buffer[i]);
688
689               putchar (' ');
690               if (file_is_binary)
691                 putchar ('*');
692               else
693                 putchar (' ');
694
695               /* Translate each NEWLINE byte to the string, "\\n",
696                  and each backslash to "\\\\".  */
697               for (i = 0; i < strlen (file); ++i)
698                 {
699                   switch (file[i])
700                     {
701                     case '\n':
702                       fputs ("\\n", stdout);
703                       break;
704
705                     case '\\':
706                       fputs ("\\\\", stdout);
707                       break;
708
709                     default:
710                       putchar (file[i]);
711                       break;
712                     }
713                 }
714               putchar ('\n');
715             }
716         }
717     }
718
719   if (have_read_stdin && fclose (stdin) == EOF)
720     error (EXIT_FAILURE, errno, _("standard input"));
721
722   exit (ok ? EXIT_SUCCESS : EXIT_FAILURE);
723 }