#include <mLib/quis.h>
#include <mLib/report.h>
#include <mLib/sub.h>
-#include <mLib/str.h>
#include "ghash.h"
#include "cc.h"
#define f_escape 32u
#define f_oddenc 64u
-/*----- Support functions -------------------------------------------------*/
-
-/* --- @gethash@ --- *
- *
- * Arguments: @const char *name@ = pointer to name string
- *
- * Returns: Pointer to appropriate hash class.
- *
- * Use: Chooses a hash function by name.
- */
+/*----- Guts --------------------------------------------------------------*/
-static const gchash *gethash(const char *name)
+static int checkjunk(const char *path, const struct stat *st, void *p)
{
- 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;
- }
+ const char *what;
+ fhashstate *fh = p;
+
+ if (!st) {
+ if (fh->f & f_verbose)
+ fprintf(stderr, "JUNK (error %s) %s\n", strerror(errno), path);
+ else
+ moan("couldn't stat junk file `%s': %s", path, strerror(errno));
+ } else {
+ what = describefile(st);
+ if (fh->f & f_verbose)
+ fprintf(stderr, "JUNK %s %s\n", what, path);
+ else
+ moan("found junk %s `%s'", what, path);
}
- return (gg);
+ return (0);
}
-/*----- Guts --------------------------------------------------------------*/
+static int warnjunk(const char *path, const struct stat *st, void *p)
+{
+ if (st)
+ moan("unexpected %s `%s'", describefile(st), path);
+ else
+ moan("couldn't stat unexpected file `%s': %s", path, strerror(errno));
+ return (0);
+}
-static int checkhash(const gchash *gch, unsigned f,
- const char *file, const encodeops *e)
+static int checkhash(fhashstate *fh, 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 & GSF_RAW ? "r" : "rb")) == 0) {
+ hfp.fp = stdin;
+ else if ((hfp.fp = fopen(file, fh->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 * fh->gch->hashsz);
+ hfp.gch = fh->gch;
+ hfp.ee = e;
+ hfp.f = fh->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(fh, 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 |= FHF_BINARY;
- else if (*p != ' ')
- continue;
- p++;
-
- if (f & f_escape) {
- DRESET(&dd);
- getstring(&p, &dd, GSF_STRING);
- p = dd.buf;
- }
-
- if (fhash(gch, ff, p, 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 ((f & f_verbose) && nfail)
- moan("%lu of %lu file(s) failed %s check", nfail, n, gch->name);
+ 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 ((fh->f & f_verbose) && nfail)
+ 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 gchash *gch, unsigned f,
- const char *file, const encodeops *e)
+static int dohash(fhashstate *fh, const char *file, const encodeops *e)
{
int rc = 0;
- octet *p = xmalloc(gch->hashsz);
+ octet *p = xmalloc(fh->gch->hashsz);
- if (fhash(gch, f, file, p)) {
+ if (fhash(fh, file, p)) {
moan("couldn't read `%s': %s", file ? file : "<stdin>", strerror(errno));
rc = EXIT_FAILURE;
} else {
- e->put(p, gch->hashsz, stdout);
+ e->put(p, fh->gch->hashsz, stdout);
if (file) {
fputc(' ', stdout);
- fputc(f & FHF_BINARY ? '*' : ' ', stdout);
- if (f & f_escape)
+ fputc(fh->f & FHF_BINARY ? '*' : ' ', stdout);
+ if (fh->f & f_escape)
putstring(stdout, file, 0);
else
fputs(file, stdout);
return (rc);
}
-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 dofile(fhashstate *fh, const char *file, const encodeops *e)
+ { return (fh->f & f_check ? checkhash : dohash)(fh, file, e); }
-static int hashfiles(const gchash *gch, unsigned f,
- const char *file, const encodeops *e)
+static int hashfiles(fhashstate *fh, 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 & GSF_RAW ? "r" : "rb")) == 0) {
+ else if ((fp = fopen(file, fh->f & GSF_RAW ? "r" : "rb")) == 0) {
moan("couldn't open `%s': %s", file, strerror(errno));
return (EXIT_FAILURE);
}
for (;;) {
DRESET(&d);
- if (getstring(fp, &d, GSF_FILE | f))
+ if (getstring(fp, &d, GSF_FILE | fh->f))
break;
- if ((rrc = dofile(gch, f, d.buf, e)) != 0)
+ if ((rrc = dofile(fh, d.buf, e)) != 0)
rc = rrc;
}
return (rc);
}
-static int hashsum(const gchash *gch, unsigned f,
- const char *file, const encodeops *e)
- { return (f & f_files ? hashfiles : dofile)(gch, f, file, e); }
+static int hashsum(fhashstate *fh, const char *file, const encodeops *e)
+ { return (fh->f & f_files ? hashfiles : dofile)(fh, file, e); }
/*----- Main driver -------------------------------------------------------*/
-e, --escape Escape funny characters in filenames.\n\
-c, --check Check message digests rather than emitting them.\n\
-b, --binary When reading files, treat them as binary.\n\
+-p, --progress Show a progress indicator for large files.\n\
-v, --verbose Be verbose when checking digests.\n\
\n\
For a list of hashing algorithms and encodings, type `$ --list'.\n\
int main(int argc, char *argv[])
{
- unsigned f = 0;
- const gchash *gch = 0;
- const encodeops *e = &encodingtab[0];
+ fhashstate fh;
+ const encodeops *e = &encodingtab[ENC_HEX];
int rc;
/* --- Initialization --- */
ego(argv[0]);
sub_init();
+ fhash_init(&fh, 0, 0);
/* --- Choose a hash function from the name --- */
size_t len = strlen(q);
if (len > 3 && strcmp(q + len - 3, "sum") == 0) {
q[len - 3] = 0;
- gch = gethash(q);
+ fh.gch = gethash(q);
}
- if (!gch)
- gch = gethash("md5");
+ if (!fh.gch)
+ fh.gch = gethash("md5");
xfree(q);
}
{ "escape", 0, 0, 'e' },
{ "check", 0, 0, 'c' },
+ { "junk", 0, 0, 'j' },
{ "binary", 0, 0, 'b' },
{ "verbose", 0, 0, 'v' },
{ "progress", 0, 0, 'p' },
{ 0, 0, 0, 0 }
};
- int i = mdwopt(argc, argv, "hVu a:E:l f0 ecbvp", opts, 0, 0, 0);
+ int i = mdwopt(argc, argv, "hVu a:E:l f0 ecjbvp", opts, 0, 0, 0);
if (i < 0)
break;
switch (i) {
case 'h':
- help(stdout, gch);
+ help(stdout, fh.gch);
exit(0);
case 'V':
version(stdout);
case 'l':
exit(displaylists(listtab, argv + optind));
case 'a':
- if ((gch = gethash(optarg)) == 0)
+ if ((fh.gch = gethash(optarg)) == 0)
die(EXIT_FAILURE, "unknown hash algorithm `%s'", optarg);
- f |= f_oddhash;
+ fh.f |= f_oddhash;
break;
case 'E':
if ((e = getencoding(optarg)) == 0)
die(EXIT_FAILURE, "unknown encoding `%s'", optarg);
- f |= f_oddenc;
+ fh.f |= f_oddenc;
break;
case 'f':
- f |= f_files;
+ fh.f |= f_files;
break;
case '0':
- f |= GSF_RAW;
+ fh.f |= GSF_RAW;
break;
case 'e':
- f |= f_escape;
+ fh.f |= f_escape;
break;
case 'c':
- f |= f_check;
+ fh.f |= f_check;
+ break;
+ case 'j':
+ fh.f |= FHF_JUNK;
break;
case 'b':
- f |= FHF_BINARY;
+ fh.f |= FHF_BINARY;
break;
case 'v':
- f |= f_verbose;
+ fh.f |= f_verbose;
break;
case 'p':
- f |= FHF_PROGRESS;
+ fh.f |= FHF_PROGRESS;
break;
default:
- f |= f_bogus;
+ fh.f |= f_bogus;
break;
}
}
- if (f & f_bogus) {
+ if (fh.f & f_bogus) {
usage(stderr);
exit(EXIT_FAILURE);
}
/* --- Generate output --- */
- if (!(f & f_check) && (argc || (f & f_files))) {
- if (f & f_oddhash) printf("#hash %s\n", gch->name);
- if (f & f_oddenc) printf("#encoding %s\n", e->name);
- if (f & f_escape) fputs("#escape\n", stdout);
+ if (!(fh.f & f_check) && (argc || (fh.f & f_files))) {
+ if (fh.f & f_oddhash) printf("#hash %s\n", fh.gch->name);
+ if (fh.f & f_oddenc) printf("#encoding %s\n", e->name);
+ if (fh.f & f_escape) fputs("#escape\n", stdout);
}
if (!argc)
- rc = hashsum(gch, f, 0, e);
+ rc = hashsum(&fh, 0, e);
else {
int i;
int rrc;
rc = 0;
for (i = 0; i < argc; i++) {
- if ((rrc = hashsum(gch, f, argv[i], e)) != 0)
+ if ((rrc = hashsum(&fh, argv[i], e)) != 0)
rc = rrc;
}
}
+ if (fh.f & FHF_JUNK) {
+ if (fh.f & f_check) {
+ if (fhash_junk(&fh, checkjunk, &fh)) rc = EXIT_FAILURE;
+ } else {
+ if (fhash_junk(&fh, warnjunk, 0) < 0) rc = EXIT_FAILURE;
+ }
+ }
+ fhash_free(&fh);
+
return (rc);
}