bug-coreutils
[Top][All Lists]
Advanced

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

[PATCH] Support generic BSD message digest formats in md5sum.c (sha1sum


From: Lawrence Teo
Subject: [PATCH] Support generic BSD message digest formats in md5sum.c (sha1sum now --check's BSD SHA1 format)
Date: Fri, 22 Aug 2003 03:35:34 -0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5b) Gecko/20030727 Thunderbird/0.1

Here's a patch against 5.0.1 to allow md5sum.c to support generic BSD message digest formats. Version 5.0.1 already has support for BSD MD5 format; this patch modifies the code so that `sha1sum --check' will also work with BSD-style SHA1 checksum files.

The patch is intentionally made "generic" enough, so that in the event that coreutils chooses to implement other algorithms like RMD160 (which is present as `rmd160' in OpenBSD and NetBSD), it should still work.

The patch also makes tests/md5sum/basic-1 and tests/sha1sum/basic-1 consistent with each other, so that the following three tests are done for both md5sum and sha1sum:

1. make sure that md5sum --check does *not* work with BSD SHA1 format, and vice versa.
2. accept BSD message digest formats.
3. --check exit status.

However, I'm not too familiar with the test scripts in tests/, so I would really appreciate it if someone could verify the patch. ;-)


2003-08-22  Lawrence Teo  <address@hidden>

        * src/md5sum.c (split_3): Accept the BSD format for generic
        message digest modes.  Currently works with BSD's MD5 and SHA1
        formats since these are the two algorithms presently used in
        coreutils.  Updated comments to reflect this change.
        (bsd_split_3): Updated comments.

        * tests/md5sum/basic-1: New test to make sure that
        `md5sum --check' doesn't accept the BSD SHA1 format (adapted
        from `check-bsd' test in tests/sha1sum/basic-1).

        * tests/sha1sum/basic-1: New tests for --check exit status
        and allow BSD SHA1 format (adapted from tests in
        tests/md5sum/basic-1).



--- ./src/md5sum.c.genericbsd   2003-08-22 02:10:49.000000000 -0400
+++ ./src/md5sum.c      2003-08-22 01:50:36.000000000 -0400
@@ -162,9 +162,9 @@

 #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.  */
+/* Split the checksum string S (of length S_LEN) from a BSD 'md5' or
+   'sha1' 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)
@@ -173,8 +173,8 @@

   *file_name = s;

-  /* Find end of filename. The BSD 'md5' does not escape filenames, so
-     search backwards for the last ')'. */
+  /* Find end of filename. The BSD 'md5' and 'sha1' do not escape
+     filenames, so search backwards for the last ')'. */
   i = s_len - 1;
   while (i && s[i] != ')')
     i--;
@@ -209,16 +209,23 @@
 {
   size_t i;
   int escaped_filename = 0;
+  int algo_name_len = 0;

   i = 0;
   while (ISWHITE (s[i]))
     ++i;

   /* Check for BSD-style checksum line. */
-  if (algorithm == ALG_MD5 && strncmp (s + i, "MD5 (", 5) == 0)
+  algo_name_len = strlen (DIGEST_TYPE_STRING (algorithm));
+  if (strncmp (s + i, DIGEST_TYPE_STRING (algorithm), algo_name_len) == 0)
     {
-      *binary = 0;
-      return bsd_split_3 (s + i + 5, s_len - i - 5, hex_digest, file_name);
+      if (strncmp (s + i + algo_name_len, " (", 2) == 0)
+      {
+        *binary = 0;
+        return bsd_split_3 (s + i + algo_name_len + 2,
+                            s_len - i - algo_name_len - 2,
+                            hex_digest, file_name);
+      }
     }

   /* Ignore this line if it is too short.
--- ./tests/md5sum/basic-1.genericbsd   2003-08-22 02:08:35.000000000 -0400
+++ ./tests/md5sum/basic-1      2003-08-22 02:06:48.000000000 -0400
@@ -40,9 +40,17 @@
                                {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"}},
+     # The sha1sum and md5sum drivers share a lot of code.
+     # Ensure that md5sum does *not* share the part that makes
+     # sha1sum accept BSD format.
+ ['check-bsd', '--check', {IN=> {'f.sha1' => "SHA1 (f) = $degenerate\n"}},
+                       {AUX=> {f=> ''}},
+                       {ERR=>"md5sum: f.sha1: no properly formatted "
+                         . "MD5 checksum lines found\n"},
+                       {EXIT=> 1}],
+ ['check-bsd2', '--check', {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
                                {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
-     ['check-bsd2', '--check', '--status',
+     ['check-bsd3', '--check', '--status',
                                {IN=> {'f.md5' => "MD5 (f) = $degenerate\n"}},
                                {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
     );
--- ./tests/sha1sum/basic-1.genericbsd  2003-08-22 02:00:12.000000000 -0400
+++ ./tests/sha1sum/basic-1     2003-08-22 02:03:26.000000000 -0400
@@ -47,6 +47,11 @@
                        {ERR=>"sha1sum: f.md5: no properly formatted "
                          . "SHA1 checksum lines found\n"},
                        {EXIT=> 1}],
+ ['check-bsd2', '--check', {IN=> {'f.sha1' => "SHA1 (f) = $sha_degenerate\n"}},
+            {AUX=> {f=> ''}}, {OUT=>"f: OK\n"}],
+     ['check-bsd3', '--check', '--status',
+                               {IN=> {'f.sha1' => "SHA1 (f) = 
$sha_degenerate\n"}},
+                               {AUX=> {f=> 'bar'}}, {EXIT=> 1}],
     );

 # Insert the `--text' argument for each test.



--
Lawrence Teo / address@hidden
Laboratory of Information Integration, Security and Privacy (LIISP)
Department of Software and Information Systems
University of North Carolina at Charlotte





reply via email to

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