Rewrite -c and -b processing to shrink code and eliminate buffer overrun.
[oweals/busybox.git] / coreutils / md5sum.c
index ffa9e6bce65b276e7232f586c72518ea6f883834..a9bbdef369f1c0b3b344f112a3bdde72d41bc466 100644 (file)
 /* Written by Ulrich Drepper <drepper@gnu.ai.mit.edu> */
 /* Hacked to work with BusyBox by Alfred M. Szmidt <ams@trillian.itslinux.org> */
 
-#include "internal.h"
 #include <stdio.h>
 #include <errno.h>
 #include <ctype.h>
+#include <getopt.h>
+#include "busybox.h"
+
+/* For some silly reason, this file uses backwards TRUE and FALSE conventions */
+#undef TRUE
+#undef FALSE
+#define FALSE   ((int) 1)
+#define TRUE    ((int) 0)
 
 //----------------------------------------------------------------------------
 //--------md5.c
@@ -58,7 +65,7 @@
 #include <string.h>
 #include <endian.h>
 
-#include "internal.h"
+#include "busybox.h"
 //----------------------------------------------------------------------------
 //--------md5.h
 //----------------------------------------------------------------------------
    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 #ifndef _MD5_H
-#define _MD5_H 1
+static const int _MD5_H = 1;
 
 #include <stdio.h>
 
-#if defined HAVE_LIMITS_H || _LIBC
+#if defined HAVE_LIMITS_H || defined _LIBC
 # include <limits.h>
 #endif
 
    the resulting executable.  Locally running cross-compiled executables
    is usually not possible.  */
 
-#ifdef _LIBC
 # include <sys/types.h>
 typedef u_int32_t md5_uint32;
-#else
-# if defined __STDC__ && __STDC__
-#  define UINT_MAX_32_BITS 4294967295U
-# else
-#  define UINT_MAX_32_BITS 0xFFFFFFFF
-# endif
-
-/* If UINT_MAX isn't defined, assume it's a 32-bit type.
-   This should be valid for all systems GNU cares about because
-   that doesn't include 16-bit systems, and only modern systems
-   (that certainly have <limits.h>) have 64+-bit integral types.  */
-
-# ifndef UINT_MAX
-#  define UINT_MAX UINT_MAX_32_BITS
-# endif
-
-# if UINT_MAX == UINT_MAX_32_BITS
-   typedef unsigned int md5_uint32;
-# else
-#  if USHRT_MAX == UINT_MAX_32_BITS
-    typedef unsigned short md5_uint32;
-#  else
-#   if ULONG_MAX == UINT_MAX_32_BITS
-     typedef unsigned long md5_uint32;
-#   else
-     /* The following line is intended to evoke an error.
-        Using #error is not portable enough.  */
-     "Cannot determine unsigned 32-bit data type."
-#   endif
-#  endif
-# endif
-#endif
-
-#undef __P
-#if defined (__STDC__) && __STDC__
-#define        __P(x) x
-#else
-#define        __P(x) ()
-#endif
 
 /* Structure to save state of computation between the single steps.  */
 struct md5_ctx
@@ -214,7 +181,14 @@ extern void *md5_buffer __P ((const char *buffer, size_t len, void *resblock));
 //--------end of md5.h
 //----------------------------------------------------------------------------
 
-#define SWAP(n) (n)
+/* Handle endian-ness */
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+       #define SWAP(n) (n)
+#else
+       #define SWAP(n) ((n << 24) | ((n&65280)<<8) | ((n&16711680)>>8) | (n>>24))
+#endif
+
+
 
 /* This array contains the bytes used to pad the buffer to the next
    64-byte boundary.  (RFC 1321, 3.1: Step 1)  */
@@ -284,7 +258,7 @@ void *md5_finish_ctx(struct md5_ctx *ctx, void *resbuf)
 int md5_stream(FILE *stream, void *resblock)
 {
   /* Important: BLOCKSIZE must be a multiple of 64.  */
-#define BLOCKSIZE 4096
+static const int BLOCKSIZE = 4096;
   struct md5_ctx ctx;
   char buffer[BLOCKSIZE + 72];
   size_t sum;
@@ -562,7 +536,7 @@ void md5_process_block(const void *buffer, size_t len, struct md5_ctx *ctx)
 /* The minimum length of a valid digest line in a file produced
    by `md5sum FILE' and read by `md5sum -c'.  This length does
    not include any newline character at the end of a line.  */
-#define MIN_DIGEST_LINE_LENGTH 35 /* 32 - message digest length
+static const int MIN_DIGEST_LINE_LENGTH = 35; /* 32 - message digest length
                                       2 - blank and binary indicator
                                       1 - minimum filename length */
 
@@ -574,23 +548,6 @@ static int status_only = 0; /* With -c, don't generate any output.
 static int warn = 0; /* With -w, print a message to standard error warning
                         about each improperly formatted MD5 checksum line */
 
-static const char md5sum_usage[] =
-    "md5sum [OPTION] [FILE]...\n"
-    "or:    md5sum [OPTION] -c [FILE]\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-    "\nPrint or check MD5 checksums.\n\n"
-    "Options:\n"
-    "With no FILE, or when FILE is -, read standard input.\n\n"
-    "\t-b\tread files in binary mode\n"
-    "\t-c\tcheck MD5 sums against given list\n"
-    "\t-t\tread files in text mode (default)\n"
-    "\t-g\tread a string\n"
-    "\nThe following two options are useful only when verifying checksums:\n"
-    "\t-s,\tdon't output anything, status code shows success\n"
-    "\t-w,\twarn about improperly formated MD5 checksum lines\n"
-#endif
-;
-
 static int split_3(char *s,
                    size_t s_len,
                    unsigned char **u,
@@ -700,13 +657,13 @@ static int md5_file(const char *filename,
   } else {
     fp = fopen(filename, OPENOPTS(binary));
     if (fp == NULL) {
-      errorMsg("md5sum: %s: %s\n", filename, strerror(errno));
+      perror_msg("%s", filename);
       return FALSE;
     }
   }
 
   if (md5_stream(fp, md5_result)) {
-    errorMsg("md5sum: %s: %s\n", filename, strerror(errno));
+    perror_msg("%s", filename);
 
     if (fp != stdin)
       fclose(fp);
@@ -714,7 +671,7 @@ static int md5_file(const char *filename,
   }
 
   if (fp != stdin && fclose(fp) == EOF) {
-    errorMsg("md5sum: %s: %s\n", filename, strerror(errno));
+    perror_msg("%s", filename);
     return FALSE;
   }
 
@@ -729,8 +686,7 @@ static int md5_check(const char *checkfile_name)
   int n_open_or_read_failures = 0;
   unsigned char md5buffer[16];
   size_t line_number;
-  char *line;
-  size_t line_chars_allocated;
+  char line[BUFSIZ];
 
   if (STREQ(checkfile_name, "-")) {
     have_read_stdin = 1;
@@ -738,14 +694,12 @@ static int md5_check(const char *checkfile_name)
   } else {
     checkfile_stream = fopen(checkfile_name, "r");
     if (checkfile_stream == NULL) {
-      errorMsg("md5sum: %s: %s\n", checkfile_name, strerror(errno));
+      perror_msg("%s", checkfile_name);
       return FALSE;
     }
   }
 
   line_number = 0;
-  line = 0;
-  line_chars_allocated = 0;
 
   do {
     char *filename;
@@ -755,9 +709,10 @@ static int md5_check(const char *checkfile_name)
 
     ++line_number;
 
-    line_length = getline(&line, &line_chars_allocated, checkfile_stream);
+    fgets(line, BUFSIZ-1, checkfile_stream);
+    line_length = strlen(line);
 
-    if (line_length <= 0)
+    if (line_length <= 0 || line==NULL)
       break;
 
     /* Ignore comment lines, which begin with a '#' character.  */
@@ -771,7 +726,7 @@ static int md5_check(const char *checkfile_name)
     if (split_3(line, line_length, &md5num, &binary, &filename)
         || !hex_digits(md5num)) {
       if (warn) {
-        errorMsg("%s: %lu: improperly formatted MD5 checksum line\n",
+        error_msg("%s: %lu: improperly formatted MD5 checksum line",
                  checkfile_name, (unsigned long) line_number);
       }
     } else {
@@ -815,22 +770,19 @@ static int md5_check(const char *checkfile_name)
 
   while (!feof(checkfile_stream) && !ferror(checkfile_stream));
 
-  if (line)
-    free(line);
-
   if (ferror(checkfile_stream)) {
-    errorMsg("%s: read error", checkfile_name); /* */
+    error_msg("%s: read error", checkfile_name);
     return FALSE;
   }
 
   if (checkfile_stream != stdin && fclose(checkfile_stream) == EOF) {
-    errorMsg("md5sum: %s: %s\n", checkfile_name, strerror(errno));
+    perror_msg("md5sum: %s", checkfile_name);
     return FALSE;
   }
 
   if (n_properly_formated_lines == 0) {
     /* Warn if no tests are found.  */
-    errorMsg("%s: no properly formatted MD5 checksum lines found\n",
+    error_msg("%s: no properly formatted MD5 checksum lines found",
              checkfile_name);
     return FALSE;
   } else {
@@ -839,13 +791,13 @@ static int md5_check(const char *checkfile_name)
                                  - n_open_or_read_failures);
 
       if (n_open_or_read_failures > 0) {
-        errorMsg("WARNING: %d of %d listed files could not be read\n",
+        error_msg("WARNING: %d of %d listed files could not be read",
                  n_open_or_read_failures, n_properly_formated_lines);
         return FALSE;
       }
 
       if (n_mismatched_checksums > 0) {
-        errorMsg("WARNING: %d of %d computed checksums did NOT match\n",
+        error_msg("WARNING: %d of %d computed checksums did NOT match",
                  n_mismatched_checksums, n_computed_checkums);
         return FALSE;
       }
@@ -874,8 +826,6 @@ int md5sum_main(int argc,
        if (string == NULL)
          string = (char **) xmalloc ((argc - 1) * sizeof (char *));
 
-       if (optarg == NULL)
-         optarg = "";
        string[n_strings++] = optarg;
        break;
      }
@@ -905,36 +855,31 @@ int md5sum_main(int argc,
       break;
 
      default:
-      usage(md5sum_usage);
+      show_usage();
     }
   }
 
   if (file_type_specified && do_check) {
-    errorMsg("the -b and -t options are meaningless when verifying checksums\n");
-    exit FALSE;
+    error_msg_and_die("the -b and -t options are meaningless when verifying checksums");
   }
 
   if (n_strings > 0 && do_check) {
-    errorMsg("the -g and -c options are mutually exclusive\n");
-    exit FALSE;
+    error_msg_and_die("the -g and -c options are mutually exclusive");
   }
 
   if (status_only && !do_check) {
-    errorMsg("the -s option is meaningful only when verifying checksums\n");
-    exit FALSE;
+    error_msg_and_die("the -s option is meaningful only when verifying checksums");
   }
 
   if (warn && !do_check) {
-    errorMsg("the -w option is meaningful only when verifying checksums\n");
-    exit FALSE;
+    error_msg_and_die("the -w option is meaningful only when verifying checksums");
   }
 
   if (n_strings > 0) {
     size_t i;
 
     if (optind < argc) {
-      errorMsg("no files may be specified when using -g\n");
-      exit FALSE;
+      error_msg_and_die("no files may be specified when using -g");
     }
     for (i = 0; i < n_strings; ++i) {
       size_t cnt;
@@ -947,7 +892,7 @@ int md5sum_main(int argc,
     }
   } else if (do_check) {
     if (optind + 1 < argc) {
-      errorMsg("only one argument may be specified when using -c\n");
+      error_msg("only one argument may be specified when using -c");
     }
 
     err = md5_check ((optind == argc) ? "-" : argv[optind]);
@@ -961,7 +906,12 @@ int md5sum_main(int argc,
 
       fail = md5_file (file, binary, md5buffer);
       err |= fail;
-      if (!fail) {
+      if (!fail && STREQ(file, "-")) {
+         size_t i;
+         for (i = 0; i < 16; ++i)
+             printf ("%02x", md5buffer[i]);
+         putchar ('\n');
+      } else if (!fail) {
         size_t i;
         /* Output a leading backslash if the file name contains
            a newline or backslash.  */
@@ -1000,14 +950,15 @@ int md5sum_main(int argc,
   }
 
   if (fclose (stdout) == EOF) {
-    errorMsg("write error");
-    exit FALSE;
+    error_msg_and_die("write error");
   }
 
   if (have_read_stdin && fclose (stdin) == EOF) {
-    errorMsg("standard input");
-    exit FALSE;
+    error_msg_and_die("standard input");
   }
 
-  exit (err == 0 ? TRUE : FALSE);
+  if (err == 0)
+         return EXIT_SUCCESS;
+  else
+         return EXIT_FAILURE;
 }