Expunge revision histories in files.
[u/mdw/catacomb] / hashsum.c
index 73ad9dc..a175397 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.10 2004/04/08 01:36:15 mdw Exp $
  *
  * Hash files using some secure hash function
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: hashsum.c,v $
- * 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'.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "config.h"
 
 #include "ghash.h"
 
-#include "md4.h"
-#include "md5.h"
-#include "rmd128.h"
-#include "rmd160.h"
-#include "rmd256.h"
-#include "rmd320.h"
-#include "sha.h"
-#include "tiger.h"
-
 /*----- Static variables --------------------------------------------------*/
 
-static const gchash *hashtab[] = {
-  &md5, &md4, &sha, &rmd128, &rmd160, &rmd256, &rmd320, &tiger,
-  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 -------------------------------------------------*/
 
@@ -110,11 +85,11 @@ static int fhash(const char *file, unsigned f, const gchash *gch, void *buf)
   else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0)
     return (-1);
 
-  h = gch->init();
+  h = GH_INIT(gch);
   while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0)
-    h->ops->hash(h, fbuf, sz);
-  h->ops->done(h, buf);
-  h->ops->destroy(h);
+    GH_HASH(h, fbuf, sz);
+  GH_DONE(h, buf);
+  GH_DESTROY(h);
   e = ferror(fp);
   if (file)
     fclose(fp);
@@ -183,9 +158,9 @@ static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
 
 static const gchash *gethash(const char *name)
 {
-  const gchash **g, *gg = 0;
+  const gchash *const *g, *gg = 0;
   size_t sz = strlen(name);
-  for (g = hashtab; *g; g++) {
+  for (g = ghashtab; *g; g++) {
     if (strncmp(name, (*g)->name, sz) == 0) {
       if ((*g)->name[sz] == 0) {
        gg = *g;
@@ -218,7 +193,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 +217,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 +266,7 @@ again:
 
     if (ch == q)
       break;
-    if (!q && isspace((unsigned char)ch))
+    if (!q && isspace(ch))
       break;
 
     /* --- Otherwise contribute and continue --- */
@@ -419,18 +394,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 +468,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 +491,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 +500,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 +512,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)
@@ -587,7 +564,7 @@ int main(int argc, char *argv[])
       gch = gethash(q);
     }
     if (!gch)
-      gch = hashtab[0];
+      gch = gethash("md5");
     xfree(q);
   }
 
@@ -635,10 +612,10 @@ int main(int argc, char *argv[])
        break;
       case 'l': {
        unsigned j;
-       for (j = 0; hashtab[j]; j++) {
+       for (j = 0; ghashtab[j]; j++) {
          if (j)
            fputc(' ', stdout);
-         printf("%s", hashtab[j]->name);
+         printf("%s", ghashtab[j]->name);
        }
        fputc('\n', stdout);
        exit(0);