bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] support BSD-style checksums in md5sum --check


From: Joe Orton
Subject: [PATCH] support BSD-style checksums in md5sum --check
Date: Fri, 28 Mar 2003 16:27:52 +0000
User-agent: Mutt/1.4i

Hi, this patch against 4.5.9 adds support to 'md5sum --check' for
parsing the checksum files produced by the 'md5' command on BSD systems.
(not sure where this command came from exactly, I have it on OpenBSD and
FreeBSD at least)

2003-03-28  Joe Orton  <address@hidden>

        * src/md5sum.c (bsd_split_3): New function.
        (split_3): Detect checksums from BSD 'md5' command and handle them
        using bsd_split_3.

        * tests/md5sum/basic-1: New tests for --check exit status, and for
        BSD-style checksum files.

--- ./src/md5sum.c.md5bsd       2003-03-28 10:34:36.000000000 +0000
+++ ./src/md5sum.c      2003-03-28 16:19:37.000000000 +0000
@@ -160,6 +160,45 @@
   exit (status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
+#define ISWHITE(c) ((c) == ' ' || (c) == '\t')
+
+/* Split the checksum string S (of length S_LEN) from a BSD 'md5'
+   command into two parts: a hexadecimal digest, and the file name.  S
+   is modified.  */
+
+static int
+bsd_split_3 (char *s, size_t s_len, unsigned char **hex_digest, char 
**file_name)
+{
+  size_t i;
+
+  *file_name = s;
+  
+  /* Find end of filename. The BSD 'md5' does not escape filenames, so
+   * search backwards for the last ')'. */
+  i = s_len - 1;
+  while (i && s[i] != ')')
+    i--;
+  
+  if (s[i] != ')')
+    return 1;
+  
+  s[i++] = '\0';
+  
+  while (ISWHITE(s[i]))
+    i++;
+  
+  if (s[i] != '=')
+    return 1;
+  
+  i++;
+  
+  while (ISWHITE(s[i]))
+    i++;
+  
+  *hex_digest = (unsigned char *) &s[i];
+  return 0;
+}
+
 /* Split the string S (of length S_LEN) into three parts:
    a hexadecimal digest, binary flag, and the file name.
    S is modified.  */
@@ -171,12 +210,17 @@
   size_t i;
   int escaped_filename = 0;
 
-#define ISWHITE(c) ((c) == ' ' || (c) == '\t')
-
   i = 0;
   while (ISWHITE (s[i]))
     ++i;
 
+  /* Check for BSD-style checksum line. */
+  if (strncmp(s + i, "MD5 (", 5) == 0)
+    {
+      *binary = 0;
+      return bsd_split_3(s + i + 5, s_len - i - 5, hex_digest, file_name);
+    }
+
   /* Ignore this line if it is too short.
      Each line must have at least `min_digest_line_length - 1' (or one more, if
      the first is a backslash) more characters to contain correct message 
digest
--- ./tests/md5sum/basic-1.md5bsd       2003-03-28 10:00:21.000000000 +0000
+++ ./tests/md5sum/basic-1      2003-03-28 10:33:38.000000000 +0000
@@ -36,13 +36,21 @@
                                {OUT=>"57edf4a22be3c955ac49da2e2107b67a  f\n"}],
      ['backslash', {IN=> {".\\foo"=> ''}},
                                {OUT=>"\\$degenerate  .\\\\foo\n"}],
+     ['check-1', '--check', {AUX=> {f=> ''}}, {IN=> {'f.md5' => "$degenerate  
f\n"}},
+                               {OUT=>"f: OK\n"}],
+     ['check-2', '--check', '--status', {IN=>{'f.md5' => "$degenerate  f\n"}},
+      {AUX=> {f=> 'foo'}}, {EXIT=> 1}],
+     ['check-bsd', '--check', {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
+      {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
+     ['check-bsd2', '--check', '--status', {IN=> {'f.md5' => "MD5 (f) = 
$degenerate\n"}},
+      {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
     );
 
 # Insert the `--text' argument for each test.
 my $t;
 foreach $t (@Tests)
   {
-    splice @$t, 1, 0, '--text';
+    splice @$t, 1, 0, '--text' unless @$t[1] eq '--check';
   }
 
 my $save_temps = $ENV{DEBUG};




reply via email to

[Prev in Thread] Current Thread [Next in Thread]