cc-hash.c: New file containing hash-related code from hashsum and dsig.
[u/mdw/catacomb] / cc-hash.c
diff --git a/cc-hash.c b/cc-hash.c
new file mode 100644 (file)
index 0000000..81a06a2
--- /dev/null
+++ b/cc-hash.c
@@ -0,0 +1,388 @@
+/* -*-c-*-
+ *
+ * Common functions for hashing utilities
+ *
+ * (c) 2011 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#define _FILE_OFFSET_BITS 64
+
+#include "config.h"
+
+#include <ctype.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <mLib/dstr.h>
+
+#include <mLib/hex.h>
+#include <mLib/base32.h>
+#include <mLib/base64.h>
+
+#include "ghash.h"
+#include "cc.h"
+
+/*----- Encoding and decoding ---------------------------------------------*/
+
+/* --- Hex encoding --- */
+
+static void puthex(const octet *buf, size_t sz, FILE *fp)
+{
+  while (sz) {
+    fprintf(fp, "%02x", *buf++);
+    sz--;
+  }
+}
+
+static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
+{
+  size_t i = 0;
+  while (sz > 0 &&
+        isxdigit((unsigned char)p[0]) &&
+        isxdigit((unsigned char)p[1])) {
+    char buf[3];
+    buf[0] = p[0];
+    buf[1] = p[1];
+    buf[2] = 0;
+    *q++ = strtoul(buf, 0, 16);
+    sz--;
+    p += 2;
+    i++;
+  }
+  if (pp)
+    *pp = (char *)p;
+  return (i);
+}
+
+/* --- Base64 encoding --- */
+
+static void putbase64(const octet *buf, size_t sz, FILE *fp)
+{
+  base64_ctx b;
+  dstr d = DSTR_INIT;
+
+  base64_init(&b);
+  b.indent = "";
+  b.maxline = 0;
+  base64_encode(&b, buf, sz, &d);
+  base64_encode(&b, 0, 0, &d);
+  dstr_write(&d, fp);
+  dstr_destroy(&d);
+}
+
+static size_t getbase64(const char *p, octet *q, size_t sz, char **pp)
+{
+  base64_ctx b;
+  dstr d = DSTR_INIT;
+  size_t n = strlen(p);
+
+  base64_init(&b);
+  base64_decode(&b, p, n, &d);
+  if (pp) *pp = (/*unconst*/ char *)p + n;
+  base64_decode(&b, 0, 0, &d);
+  assert(d.len <= sz);
+  memcpy(q, d.buf, sz);
+  n = d.len;
+  dstr_destroy(&d);
+  return (n);
+}
+
+/* --- Base32 encoding --- */
+
+static void putbase32(const octet *buf, size_t sz, FILE *fp)
+{
+  base32_ctx b;
+  dstr d = DSTR_INIT;
+
+  base32_init(&b);
+  b.indent = "";
+  b.maxline = 0;
+  base32_encode(&b, buf, sz, &d);
+  base32_encode(&b, 0, 0, &d);
+  dstr_write(&d, fp);
+  dstr_destroy(&d);
+}
+
+static size_t getbase32(const char *p, octet *q, size_t sz, char **pp)
+{
+  base32_ctx b;
+  dstr d = DSTR_INIT;
+  size_t n = strlen(p);
+
+  base32_init(&b);
+  base32_decode(&b, p, n, &d);
+  if (pp) *pp = (/*unconst*/ char *)p + n;
+  base32_decode(&b, 0, 0, &d);
+  assert(d.len <= sz);
+  memcpy(q, d.buf, sz);
+  n = d.len;
+  dstr_destroy(&d);
+  return (n);
+}
+
+/* --- Table --- */
+
+const encodeops encodingtab[] = {
+#define TAB(tag, name) { #name, put##name, get##name },
+  ENCODINGS(TAB)
+#undef TAB
+  { 0, 0, 0 }
+};
+
+const encodeops *getencoding(const char *ename)
+{
+  const encodeops *e;
+
+  for (e = encodingtab; e->name; e++) {
+    if (strcmp(ename, e->name) == 0)
+      return (e);
+  }
+  return (0);
+}
+
+/*----- File hashing ------------------------------------------------------*/
+
+/* --- @fhash@ --- *
+ *
+ * Arguments:  @const gchash *gch@ = pointer to hash function to use
+ *             @unsigned f@ = flags to set
+ *             @const char *file@ = file name to be hashed (null for stdin)
+ *             @void *buf@ = pointer to hash output buffer
+ *
+ * Returns:    Zero if it worked, nonzero on error.
+ *
+ * Use:                Hashes a file.
+ */
+
+int fhash(const gchash *gch, unsigned f, const char *file, void *buf)
+{
+  FILE *fp;
+  char fbuf[1024 * 128];
+  size_t sz;
+  ghash *h;
+  int rc = 0;
+  fprogress ff;
+
+  if (!file || strcmp(file, "-") == 0)
+    fp = stdin;
+  else if ((fp = fopen(file, f & FHF_BINARY ? "rb" : "r")) == 0)
+    return (-1);
+
+  if (f & FHF_PROGRESS) {
+    if (fprogress_init(&ff, file, fp)) return (-1);
+  }
+
+  h = GH_INIT(gch);
+  while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0) {
+    GH_HASH(h, fbuf, sz);
+    if (f & FHF_PROGRESS) fprogress_update(&ff, sz);
+  }
+  if (ferror(fp)) rc = -1;
+  if (fp != stdin) fclose(fp);
+  if (f & FHF_PROGRESS) fprogress_done(&ff);
+  GH_DONE(h, buf);
+  GH_DESTROY(h);
+  return (rc);
+}
+
+/*----- String I/O --------------------------------------------------------*/
+
+/* --- @getstring@ --- *
+ *
+ * Arguments:  @void *in@ = input source
+ *             @dstr *d@ = destination string
+ *             @unsigned f@ = input flags
+ *
+ * Returns:    Zero if OK, nonzero on end-of-file.
+ *
+ * Use:                Reads a filename (or something similar) from a stream.
+ */
+
+static int nextch_file(void *in)
+  { FILE *fp = in; return (getc(fp)); }
+
+static int nextch_string(void *in)
+  { const unsigned char **p = in; return (*(*p)++); }
+
+int getstring(void *in, dstr *d, unsigned f)
+{
+  int ch;
+  int eofch = (f & GSF_STRING) ? 0 : EOF;
+  int (*nextch)(void *) = (f & GSF_STRING) ? nextch_string : nextch_file;
+  int q = 0;
+
+  /* --- Raw: just read exactly what's written up to a null byte --- */
+
+  if (f & GSF_RAW) {
+    if ((ch = nextch(in)) == eofch)
+      return (EOF);
+    for (;;) {
+      if (!ch)
+       break;
+      DPUTC(d, ch);
+      if ((ch = nextch(in)) == eofch)
+       break;
+    }
+    DPUTZ(d);
+    return (0);
+  }
+
+  /* --- Skip as far as whitespace --- *
+   *
+   * Also skip past comments.
+   */
+
+again:
+  ch = nextch(in);
+  while (isspace(ch))
+    ch = nextch(in);
+  if (ch == '#') {
+    do ch = nextch(in); while (ch != '\n' && ch != eofch);
+    goto again;
+  }
+  if (ch == eofch)
+    return (EOF);
+
+  /* --- If the character is a quote then read a quoted string --- */
+
+  switch (ch) {
+    case '`':
+      ch = '\'';
+    case '\'':
+    case '\"':
+      q = ch;
+      ch = nextch(in);
+      break;
+  }
+
+  /* --- Now read all sorts of interesting things --- */
+
+  for (;;) {
+
+    /* --- Handle an escaped thing --- */
+
+    if (ch == '\\') {
+      ch = nextch(in);
+      if (ch == eofch)
+       break;
+      switch (ch) {
+       case 'a': ch = '\a'; break;
+       case 'b': ch = '\b'; break;
+       case 'f': ch = '\f'; break;
+       case 'n': ch = '\n'; break;
+       case 'r': ch = '\r'; break;
+       case 't': ch = '\t'; break;
+       case 'v': ch = '\v'; break;
+      }
+      DPUTC(d, ch);
+      ch = nextch(in);
+      continue;
+    }
+
+    /* --- If it's a quote or some other end marker then stop --- */
+
+    if (ch == q)
+      break;
+    if (!q && isspace(ch))
+      break;
+
+    /* --- Otherwise contribute and continue --- */
+
+    DPUTC(d, ch);
+    if ((ch = nextch(in)) == eofch)
+      break;
+  }
+
+  /* --- Done --- */
+
+  DPUTZ(d);
+  return (0);
+}
+
+/* --- @putstring@ --- *
+ *
+ * Arguments:  @FILE *fp@ = stream to write on
+ *             @const char *p@ = pointer to text
+ *             @unsigned f@ = output flags
+ *
+ * Returns:    ---
+ *
+ * Use:                Emits a string to a stream.
+ */
+
+void putstring(FILE *fp, const char *p, unsigned f)
+{
+  size_t sz = strlen(p);
+  unsigned qq;
+  const char *q;
+
+  /* --- Just write the string null terminated if raw --- */
+
+  if (f & GSF_RAW) {
+    fwrite(p, 1, sz + 1, fp);
+    return;
+  }
+
+  /* --- Check for any dodgy characters --- */
+
+  qq = 0;
+  for (q = p; *q; q++) {
+    if (isspace((unsigned char)*q)) {
+      qq = '\"';
+      break;
+    }
+  }
+
+  if (qq)
+    putc(qq, fp);
+
+  /* --- Emit the string --- */
+
+  for (q = p; *q; q++) {
+    switch (*q) {
+      case '\a': fputc('\\', fp); fputc('a',  fp); break;
+      case '\b': fputc('\\', fp); fputc('b',  fp); break;
+      case '\f': fputc('\\', fp); fputc('f',  fp); break;
+      case '\n': fputc('\\', fp); fputc('n',  fp); break;
+      case '\r': fputc('\\', fp); fputc('r',  fp); break;
+      case '\t': fputc('\\', fp); fputc('t',  fp); break;
+      case '\v': fputc('\\', fp); fputc('v',  fp); break;
+      case '`':  fputc('\\', fp); fputc('`',  fp); break;
+      case '\'': fputc('\\', fp); fputc('\'', fp); break;
+      case '\"': fputc('\\', fp); fputc('\"', fp); break;
+      default:
+       putc(*q, fp);
+       break;
+    }
+  }
+
+  /* --- Done --- */
+
+  if (qq)
+    putc(qq, fp);
+}
+
+/*----- That's all, folks -------------------------------------------------*/