#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.
- */
-
-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);
-}
-
/*----- Guts --------------------------------------------------------------*/
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 & GSF_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 |= 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 (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 gchash *gch, unsigned f,
{
unsigned f = 0;
const gchash *gch = 0;
- const encodeops *e = &encodingtab[0];
+ const encodeops *e = &encodingtab[ENC_HEX];
int rc;
/* --- Initialization --- */