#include "config.h"
#include <assert.h>
-#include <ctype.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <mLib/quis.h>
#include <mLib/report.h>
#include <mLib/sub.h>
-#include <mLib/str.h>
-
-#include <mLib/hex.h>
-#include <mLib/base32.h>
-#include <mLib/base64.h>
#include "ghash.h"
#include "cc.h"
-#ifndef PATHSEP
-# if defined(__riscos)
-# define PATHSEP '.'
-# elif defined(__unix) || defined(unix)
-# define PATHSEP '/'
-# else
-# define PATHSEP '\\'
-# endif
-#endif
-
/*----- Static variables --------------------------------------------------*/
-#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
-#define f_oddenc 256u
-#define f_progress 512u
-
-/*----- 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 putb64(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 getb64(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 putb32(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 getb32(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 --- */
-
-typedef struct encodeops {
- const char *name;
- void (*put)(const octet *, size_t, FILE *);
- size_t (*get)(const char *, octet *, size_t, char **);
-} encodeops;
-
-static const encodeops encodingtab[] = {
- { "hex", puthex, gethex },
- { "base64", putb64, getb64 },
- { "base32", putb32, getb32 },
- { 0, 0, 0 }
-};
-
-static 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);
-}
-
-/*----- Support functions -------------------------------------------------*/
-
-/* --- @fhash@ --- *
- *
- * Arguments: @const char *file@ = file name to be hashed (null for stdin)
- * @unsigned f@ = flags to set
- * @const gchash *gch@ = pointer to hash function to use
- * @void *buf@ = pointer to hash output buffer
- *
- * Returns: Zero if it worked, nonzero on error.
- *
- * Use: Hashes a file.
- */
-
-struct unit {
- const char *name;
- int m;
-};
-
-static void prhuman_time(FILE *fp, unsigned long n)
-{
- const static struct unit utime[] = {
- { "s", 60 }, { "m", 60 }, { "h", 24 }, { "d", 0 }
- };
-
- unsigned long m = 0;
- const struct unit *u = utime;
-
- while (u[1].m && n > u[0].m*u[1].m) { n /= u->m; u++; }
- m = n / u[1].m; n %= u[0].m;
- if (m) fprintf(fp, "%3lu%s%02lu%s", m, u[1].name, n, u[0].name);
- else fprintf(fp, " %2lu%s", n, u[0].name);
-}
-
-static void prhuman_data(FILE *fp, off_t n)
-{
- const static struct unit udata[] = {
- { " ", 1024 }, { "k", 1024 }, { "M", 1024 }, { "G", 1024 },
- { "T", 1024 }, { "P", 1024 }, { "E", 1024 }, { "Z", 1024 },
- { "Y", 0 }
- };
-
- double x = n;
- const struct unit *u = udata;
-
- while (u->m && x >= u->m) { x /= u->m; u++; }
- fprintf(fp, "%6.1f%s", x, u->name);
-}
-
-static int fhash(const char *file, unsigned f, const gchash *gch, void *buf)
-{
- FILE *fp;
- char fbuf[1024 * 128];
- size_t sz;
- ghash *h;
- int e;
- off_t fsz = -1, fo;
- const char *p;
- dstr d = DSTR_INIT;
- static char baton[] = "-\\|/";
- char *bp = baton;
- time_t now, last, start;
- int i, pc;
-
- if (!file || strcmp(file, "-") == 0)
- fp = stdin;
- else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0)
- return (-1);
-
- if (f & f_progress) {
- if ((fo = ftello(fp)) >= 0 &&
- fseeko(fp, 0, SEEK_END) >= 0 &&
- (fsz = ftello(fp),
- fseeko(fp, fo, SEEK_SET) < 0))
- return (-1);
- if (fo != -1 && fsz != -1) fsz -= fo;
- fo = 0;
- sz = strlen(file);
- if (sz < 24)
- dstr_puts(&d, file);
- else if ((p = strchr(file + sz - 20, PATHSEP)) != 0) {
- dstr_puts(&d, "..."); dstr_puts(&d, p);
- } else {
- p = strrchr(file, PATHSEP);
- if (!p) dstr_putf(&d, "%.20s...", file);
- else dstr_putf(&d, "...%.17s...", p);
- }
- start = last = time(0);
- }
-
- h = GH_INIT(gch);
- while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0) {
- GH_HASH(h, fbuf, sz);
- if (f & f_progress) {
- fo += sz;
- now = time(0);
- if (difftime(now, last) < 1) continue;
- last = now;
- fprintf(stderr, "\r%-24s", d.buf);
- fprintf(stderr, "%c ", *bp++); if (!*bp) bp = baton;
- prhuman_data(stderr, fo);
- if (fsz >= fo) {
- fputc('/', stderr);
- prhuman_data(stderr, fsz);
- fputs(" [", stderr);
- pc = (fo*16 + fsz/2)/fsz;
- for (i = 0; i < pc; i++) fputc('.', stderr);
- for (; i < 16; i++) fputc(' ', stderr);
- fprintf(stderr, "] %3d%%", (int)((fo*100 + 50)/fsz));
- fprintf(stderr, " ETA ");
- prhuman_time(stderr, difftime(now, start)*(fsz - fo)/fo);
- }
- }
- }
- GH_DONE(h, buf);
- GH_DESTROY(h);
- if (f & f_progress) fprintf(stderr, "\r%78s\r", "");
- e = ferror(fp);
- if (file)
- fclose(fp);
- return (e ? -1 : 0);
-}
-
-/* --- @gethash@ --- *
- *
- * Arguments: @const char *name@ = pointer to name string
- *
- * Returns: Pointer to appropriate hash class.
- *
- * Use: Chooses a hash function by name.
- */
-
-static const gchash *gethash(const char *name)
-{
- const gchash *const *g, *gg = 0;
- size_t sz = strlen(name);
- for (g = ghashtab; *g; g++) {
- if (strncmp(name, (*g)->name, sz) == 0) {
- if ((*g)->name[sz] == 0) {
- gg = *g;
- break;
- } else if (gg)
- return (0);
- else
- gg = *g;
- }
- }
- return (gg);
-}
-
-/* --- @getstring@ --- *
- *
- * Arguments: @FILE *fp@ = stream from which to read
- * @const char *p@ = string to read from instead
- * @dstr *d@ = destination string
- * @unsigned raw@ = raw or cooked read
- *
- * Returns: Zero if OK, nonzero on end-of-file.
- *
- * Use: Reads a filename (or something similar) from a stream.
- */
-
-static int getstring(FILE *fp, const char *p, dstr *d, unsigned raw)
-{
- int ch;
- int q = 0;
-
- /* --- Raw: just read exactly what's written up to a null byte --- */
-
-#define NEXTCH (fp ? getc(fp) : (unsigned char)*p++)
-#define EOFCH (fp ? EOF : 0)
-
- if (raw) {
- if ((ch = NEXTCH) == EOFCH)
- return (EOF);
- for (;;) {
- if (!ch)
- break;
- DPUTC(d, ch);
- if ((ch = NEXTCH) == EOFCH)
- break;
- }
- DPUTZ(d);
- return (0);
- }
-
- /* --- Skip as far as whitespace --- *
- *
- * Also skip past comments.
- */
-
-again:
- ch = NEXTCH;
- while (isspace(ch))
- ch = NEXTCH;
- if (ch == '#') {
- do ch = NEXTCH; 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;
- break;
- }
-
- /* --- Now read all sorts of interesting things --- */
-
- for (;;) {
-
- /* --- Handle an escaped thing --- */
-
- if (ch == '\\') {
- ch = NEXTCH;
- 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;
- 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) == EOFCH)
- break;
- }
-
- /* --- Done --- */
-
- DPUTZ(d);
- return (0);
-
-#undef NEXTCH
-#undef EOFCH
-}
-
-/* --- @putstring@ --- *
- *
- * Arguments: @FILE *fp@ = stream to write on
- * @const char *p@ = pointer to text
- * @unsigned raw@ = whether the string is to be written raw
- *
- * Returns: ---
- *
- * Use: Emits a string to a stream.
- */
-
-static void putstring(FILE *fp, const char *p, unsigned raw)
-{
- size_t sz = strlen(p);
- unsigned qq;
- const char *q;
-
- /* --- Just write the string null terminated if raw --- */
-
- if (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;
- case '#': fputc('\\', fp); fputc('#', fp); break;
- default:
- putc(*q, fp);
- break;
- }
- }
-
- /* --- Done --- */
-
- if (qq)
- putc(qq, fp);
-}
+#define f_bogus 1u
+#define f_verbose 2u
+#define f_check 4u
+#define f_files 8u
+#define f_oddhash 16u
+#define f_escape 32u
+#define f_oddenc 64u
/*----- Guts --------------------------------------------------------------*/
-static int checkhash(const char *file, unsigned f,
- const gchash *gch, const encodeops *e)
+static int checkhash(const gchash *gch, unsigned f,
+ const char *file, const encodeops *e)
{
int rc;
- FILE *fp;
- dstr d = DSTR_INIT;
- dstr dd = DSTR_INIT;
+ hfpctx hfp;
+ dstr dl = DSTR_INIT;
+ dstr df = DSTR_INIT;
unsigned long n = 0, nfail = 0;
- octet *buf = xmalloc(2 * gch->hashsz);
+ int hf;
if (!file || strcmp(file, "-") == 0)
- fp = stdin;
- else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
+ hfp.fp = stdin;
+ else if ((hfp.fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) {
moan("couldn't open `%s': %s", file, strerror(errno));
return (EXIT_FAILURE);
}
- while (DRESET(&d), dstr_putline(&d, fp) != EOF) {
- char *p = d.buf;
- char *q;
- unsigned ff = f;
-
- /* --- Handle a directive --- */
-
- if (*p == '#') {
- p++;
- if ((q = str_getword(&p)) == 0)
- continue;
- if (strcmp(q, "hash") == 0) {
- const gchash *g;
- if ((q = str_getword(&p)) == 0)
- continue;
- if ((g = gethash(q)) == 0)
- continue;
- gch = g;
- xfree(buf);
- buf = xmalloc(2 * gch->hashsz);
- } else if (strcmp(q, "encoding") == 0) {
- const encodeops *ee;
- if ((q = str_getword(&p)) == 0)
- continue;
- if ((ee = getencoding(q)) == 0)
+ hfp.dline = &dl;
+ hfp.dfile = &df;
+ hfp.hbuf = xmalloc(2 * gch->hashsz);
+ hfp.gch = gch;
+ hfp.ee = e;
+ hfp.f = f;
+
+ while ((hf = hfparse(&hfp)) != HF_EOF) {
+ switch (hf) {
+ case HF_HASH:
+ xfree(hfp.hbuf);
+ hfp.hbuf = xmalloc(2 * hfp.gch->hashsz);
+ break;
+ case HF_FILE:
+ if (fhash(hfp.gch, hfp.f, df.buf, hfp.hbuf + hfp.gch->hashsz)) {
+ moan("couldn't read `%s': %s", df.buf, strerror(errno));
+ rc = EXIT_FAILURE;
continue;
- e = ee;
- } else if (strcmp(q, "escape") == 0)
- f |= f_escape;
- continue;
- }
-
- /* --- Otherwise it's a hex thing --- */
-
- q = p;
- while (*p && *p != ' ')
- p++;
- if (!*p)
- continue;
- *p++ = 0;
- if (e->get(q, buf, gch->hashsz, 0) < gch->hashsz)
- continue;
- if (*p == '*')
- ff |= f_binary;
- else if (*p != ' ')
- continue;
- p++;
-
- if (f & f_escape) {
- DRESET(&dd);
- getstring(0, p, &dd, 0);
- p = dd.buf;
- }
-
- if (fhash(p, ff, gch, buf + gch->hashsz)) {
- moan("couldn't read `%s': %s", p, strerror(errno));
- rc = EXIT_FAILURE;
- continue;
- }
- if (memcmp(buf, buf + gch->hashsz, gch->hashsz) != 0) {
- if (ff & f_verbose)
- fprintf(stderr, "FAIL %s\n", p);
- else
- moan("%s check failed for `%s'", gch->name, p);
- nfail++;
- rc = EXIT_FAILURE;
- } else {
- if (ff & f_verbose)
- fprintf(stderr, "OK %s\n", p);
+ }
+ if (memcmp(hfp.hbuf, hfp.hbuf + hfp.gch->hashsz,
+ hfp.gch->hashsz) != 0) {
+ if (hfp.f & f_verbose)
+ fprintf(stderr, "FAIL %s\n", df.buf);
+ else
+ moan("%s check failed for `%s'", hfp.gch->name, df.buf);
+ nfail++;
+ rc = EXIT_FAILURE;
+ } else {
+ if (hfp.f & f_verbose)
+ fprintf(stderr, "OK %s\n", df.buf);
+ }
+ n++;
}
- n++;
}
- dstr_destroy(&d);
- dstr_destroy(&dd);
- xfree(buf);
+ if (ferror(hfp.fp)) {
+ moan("error reading input `%s': %s",
+ file ? file : "<stdin>", strerror(errno));
+ rc = EXIT_FAILURE;
+ }
+ dstr_destroy(&dl);
+ dstr_destroy(&df);
+ xfree(hfp.hbuf);
if ((f & f_verbose) && nfail)
- moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name);
+ moan("%lu of %lu file(s) failed %s check", nfail, n, hfp.gch->name);
else if (!n)
moan("no files checked");
- return (0);
+ return (rc);
}
-static int dohash(const char *file, unsigned f,
- const gchash *gch, const encodeops *e)
+static int dohash(const gchash *gch, unsigned f,
+ const char *file, const encodeops *e)
{
int rc = 0;
octet *p = xmalloc(gch->hashsz);
- if (fhash(file, f, gch, p)) {
+ if (fhash(gch, f, file, p)) {
moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
rc = EXIT_FAILURE;
} else {
e->put(p, gch->hashsz, stdout);
if (file) {
fputc(' ', stdout);
- fputc(f & f_binary ? '*' : ' ', stdout);
+ fputc(f & FHF_BINARY ? '*' : ' ', stdout);
if (f & f_escape)
putstring(stdout, file, 0);
else
return (rc);
}
-static int dofile(const char *file, unsigned f,
- const gchash *gch, const encodeops *e)
-{
- return (f & f_check ? checkhash : dohash)(file, f, gch, e);
-}
+static int dofile(const gchash *gch, unsigned f,
+ const char *file, const encodeops *e)
+ { return (f & f_check ? checkhash : dohash)(gch, f, file, e); }
-static int hashfiles(const char *file, unsigned f,
- const gchash *gch, const encodeops *e)
+static int hashfiles(const gchash *gch, unsigned f,
+ const char *file, const encodeops *e)
{
FILE *fp;
dstr d = DSTR_INIT;
if (!file || strcmp(file, "-") == 0)
fp = stdin;
- else if ((fp = fopen(file, f & f_raw ? "r" : "rb")) == 0) {
+ else if ((fp = fopen(file, f & GSF_RAW ? "r" : "rb")) == 0) {
moan("couldn't open `%s': %s", file, strerror(errno));
return (EXIT_FAILURE);
}
for (;;) {
DRESET(&d);
- if (getstring(fp, 0, &d, f & f_raw))
+ if (getstring(fp, &d, GSF_FILE | f))
break;
- if ((rrc = dofile(d.buf, f, gch, e)) != 0)
+ if ((rrc = dofile(gch, f, d.buf, e)) != 0)
rc = rrc;
}
return (rc);
}
-static int hashsum(const char *file, unsigned f,
- const gchash *gch, const encodeops *e)
-{
- return (f & f_files ? hashfiles : dofile)(file, f, gch, e);
-}
+static int hashsum(const gchash *gch, unsigned f,
+ const char *file, const encodeops *e)
+ { return (f & f_files ? hashfiles : dofile)(gch, f, file, e); }
/*----- Main driver -------------------------------------------------------*/
{
unsigned f = 0;
const gchash *gch = 0;
- const encodeops *e = &encodingtab[0];
+ const encodeops *e = &encodingtab[ENC_HEX];
int rc;
/* --- Initialization --- */
f |= f_files;
break;
case '0':
- f |= f_raw;
+ f |= GSF_RAW;
break;
case 'e':
f |= f_escape;
f |= f_check;
break;
case 'b':
- f |= f_binary;
+ f |= FHF_BINARY;
break;
case 'v':
f |= f_verbose;
break;
case 'p':
- f |= f_progress;
+ f |= FHF_PROGRESS;
break;
default:
f |= f_bogus;
if (f & f_escape) fputs("#escape\n", stdout);
}
if (!argc)
- rc = hashsum(0, f, gch, e);
+ rc = hashsum(gch, f, 0, e);
else {
int i;
int rrc;
rc = 0;
for (i = 0; i < argc; i++) {
- if ((rrc = hashsum(argv[i], f, gch, e)) != 0)
+ if ((rrc = hashsum(gch, f, argv[i], e)) != 0)
rc = rrc;
}
}