Add an internal-representation no-op function.
[u/mdw/catacomb] / hashsum.c
index 73ad9dc..f2ced4b 100644 (file)
--- a/hashsum.c
+++ b/hashsum.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: hashsum.c,v 1.1 2000/07/15 20:52:34 mdw Exp $
+ * $Id: hashsum.c,v 1.8 2001/04/19 18:26:33 mdw Exp $
  *
  * Hash files using some secure hash function
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: hashsum.c,v $
+ * Revision 1.8  2001/04/19 18:26:33  mdw
+ * Add CRC as another hash function.
+ *
+ * Revision 1.7  2001/02/21 20:03:22  mdw
+ * Added support for MD2 hash function.
+ *
+ * Revision 1.6  2001/01/25 21:40:14  mdw
+ * Support for new SHA variants added.
+ *
+ * Revision 1.5  2000/12/06 20:33:27  mdw
+ * Make flags be macros rather than enumerations, to ensure that they're
+ * unsigned.
+ *
+ * Revision 1.4  2000/08/04 23:23:44  mdw
+ * Various <ctype.h> fixes.
+ *
+ * Revision 1.3  2000/07/29 17:02:43  mdw
+ * (checkhash): Be pettier about spaces between the hash and filename, for
+ * compatiblity with `md5sum'.
+ *
+ * Revision 1.2  2000/07/15 21:14:05  mdw
+ * Missed `-e' out of the usage string.
+ *
  * Revision 1.1  2000/07/15 20:52:34  mdw
  * Useful replacement for `md5sum' with support for many different hash
  * functions and for reading filename lists from `find'.
@@ -56,6 +79,8 @@
 
 #include "ghash.h"
 
+#include "crc32.h"
+#include "md2.h"
 #include "md4.h"
 #include "md5.h"
 #include "rmd128.h"
 #include "rmd256.h"
 #include "rmd320.h"
 #include "sha.h"
+#include "sha256.h"
+#include "sha384.h"
+#include "sha512.h"
 #include "tiger.h"
 
 /*----- Static variables --------------------------------------------------*/
 
 static const gchash *hashtab[] = {
-  &md5, &md4, &sha, &rmd128, &rmd160, &rmd256, &rmd320, &tiger,
+  &md5, &md4, &md2,
+  &sha, &sha256, &sha384, &sha512,
+  &rmd128, &rmd160, &rmd256, &rmd320,
+  &tiger, &gcrc32,
   0
 };
 
-enum {
-  f_binary = 1,
-  f_bogus = 2,
-  f_verbose = 4,
-  f_check = 8,
-  f_files = 16,
-  f_raw = 32,
-  f_oddhash = 64,
-  f_escape = 128
-};
+#define f_binary 1u
+#define f_bogus 2u
+#define f_verbose 4u
+#define f_check 8u
+#define f_files 16u
+#define f_raw 32u
+#define f_oddhash 64u
+#define f_escape 128u
 
 /*----- Support functions -------------------------------------------------*/
 
@@ -218,7 +247,7 @@ static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw)
 
   /* --- Raw: just read exactly what's written up to a null byte --- */
 
-#define NEXTCH (fp ? getc(fp) : *p++)
+#define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
 #define EOFCH (fp ? EOF : 0)
 
   if (raw) {
@@ -242,7 +271,7 @@ static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw)
 
 again:
   ch = NEXTCH;
-  while (isspace((unsigned char)ch))
+  while (isspace(ch))
     ch = NEXTCH;
   if (ch == '#') {
     do ch = NEXTCH; while (ch != '\n' && ch != EOFCH);
@@ -291,7 +320,7 @@ again:
 
     if (ch == q)
       break;
-    if (!q && isspace((unsigned char)ch))
+    if (!q && isspace(ch))
       break;
 
     /* --- Otherwise contribute and continue --- */
@@ -419,18 +448,19 @@ static int checkhash(const char *file, unsigned f, const gchash *gch)
 
     /* --- Otherwise it's a hex thing --- */
 
-    if ((q = str_getword(&p)) == 0)
+    q = p;
+    while (*p && *p != ' ')
+      p++;
+    if (!*p)
       continue;
+    *p++ = 0;
     if (gethex(q, buf, gch->hashsz, 0) < gch->hashsz)
       continue;
-    while (isspace((unsigned char)*p))
-      p++;
-    if (*p == '*') {
-      p++;
+    if (*p == '*')
       ff |= f_binary;
-    }
-    if (!*p)
+    else if (*p != ' ')
       continue;
+    p++;
 
     if (f & f_escape) {
       DRESET(&dd);
@@ -492,6 +522,11 @@ static int dohash(const char *file, unsigned f, const gchash *gch)
   return (rc);
 }
 
+static int dofile(const char *file, unsigned f, const gchash *gch)
+{
+  return (f & f_check ? checkhash : dohash)(file, f, gch);
+}
+
 static int hashfiles(const char *file, unsigned f, const gchash *gch)
 {
   FILE *fp;
@@ -510,7 +545,7 @@ static int hashfiles(const char *file, unsigned f, const gchash *gch)
     DRESET(&d);
     if (getstring(fp, 0, &d, f & f_raw))
       break;
-    if ((rrc = dohash(d.buf, f, gch)) != 0)
+    if ((rrc = dofile(d.buf, f, gch)) != 0)
       rc = rrc;
   }
 
@@ -519,11 +554,7 @@ static int hashfiles(const char *file, unsigned f, const gchash *gch)
 
 static int hashsum(const char *file, unsigned f, const gchash *gch)
 {
-  if (f & f_check)
-    return (checkhash(file, f, gch));
-  if (f & f_files)
-    return (hashfiles(file, f, gch));
-  return (dohash(file, f, gch));
+  return (f & f_files ? hashfiles : dofile)(file, f, gch);
 }
 
 /*----- Main driver -------------------------------------------------------*/
@@ -535,7 +566,7 @@ static void version(FILE *fp)
 
 static void usage(FILE *fp)
 {
-  pquis(fp, "Usage: $ [-f0bcv] [-a algorithm] [files...]\n");
+  pquis(fp, "Usage: $ [-f0ebcv] [-a algorithm] [files...]\n");
 }
 
 static void help(FILE *fp, const gchash *gch)