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

md5.c patch - recognize GNU checksum format



Here's a simple patch to make md5/sha1's -c flag recognize GNU's
checksum format. Unlike BSD, GNU's checksums use the form:

   CHECKSUM  FILENAME

For example, a GNU MD5 line may look like:

   e25bd1eead91463817c3d98b60d64c62  somefile.txt

This patch would make it easier for OpenBSD admins/users to verify
MD5 checksum lists that are frequently distributed using the
GNU MD5 format.

Comments and feedback are welcome. If the code is not up to
OpenBSD's standards, I would appreciate any suggestions on how to
improve it. Thanks!

Lawrence Teo
lcteo_at_uncc.edu
Department of Software and Information Systems
University of North Carolina at Charlotte




--- md5.c.orig	Sun Feb 29 18:03:32 2004
+++ md5.c	Sun Feb 29 18:06:19 2004
@@ -263,26 +263,63 @@
  		 * Crack the line into an algorithm, filename, and checksum.
  		 * Lines are of the form:
  		 *  ALGORITHM (FILENAME) = CHECKSUM
+		 *
+		 * If that doesn't work, try GNU's form:
+		 *  CHECKSUM  FILENAME
+		 *
+		 * In GNU's case, the algorithm name is obtained from the
+		 * program name.
  		 */
  		algorithm = buf;
  		p = strchr(algorithm, ' ');
-		if (p == NULL || *(p + 1) != '(')
-			continue;
-		*p = '\0';
-		len = strlen(algorithm);
-		if (len > algorithm_max || len < algorithm_min)
-			continue;
+		if (p == NULL || *(p + 1) != '(') {
+			/* GNU form: Obtain algorithm name from program name */
+			for (hf = functions; hf->name != NULL; hf++) {
+				if (strcasecmp(__progname, hf->name) == 0)
+					break;
+			}
+			if (hf->name == NULL)
+				continue;
+			algorithm = hf->name;

-		filename = p + 2;
-		p = strrchr(filename, ')');
-		if (p == NULL || strncmp(p + 1, " = ", (size_t)3) != 0)
-			continue;
-		*p = '\0';
+			checksum = buf;
+			p = strchr(checksum, ' ');
+			if (p == NULL)
+				continue;
+			*p = '\0';
+			p++;

-		checksum = p + 4;
-		p = strpbrk(checksum, " \t\r");
-		if (p != NULL)
+			while (isspace(*p))
+				p++;
+
+			if (*p == '\0')
+				continue;
+
+			filename = p;
+			if (filename == NULL)
+				continue;
+
+			p = strpbrk(filename, " \t\r");
+			if (p != NULL)
+				*p = '\0';
+		} else {
+			/* Continue with BSD's form */
  			*p = '\0';
+			len = strlen(algorithm);
+			if (len > algorithm_max || len < algorithm_min)
+				continue;
+	
+			filename = p + 2;
+			p = strrchr(filename, ')');
+			if (p == NULL || strncmp(p + 1, " = ", (size_t)3) != 0)
+				continue;
+			*p = '\0';
+	
+			checksum = p + 4;
+			p = strpbrk(checksum, " \t\r");
+			if (p != NULL)
+				*p = '\0';
+		}

  		/*
  		 * Check that the algorithm is one we recognize.