-static const gchash *hashtab[] = {
- &md5, &md4, &md2,
- &sha, &sha256, &sha384, &sha512,
- &rmd128, &rmd160, &rmd256, &rmd320,
- &tiger, &gcrc32,
- 0
-};
-
-#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 -------------------------------------------------*/
-
-/* --- @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.
- */
-
-static int fhash(const char *file, unsigned f, const gchash *gch, void *buf)
-{
- FILE *fp;
- char fbuf[BUFSIZ];
- size_t sz;
- ghash *h;
- int e;
-
- if (!file)
- fp = stdin;
- else if ((fp = fopen(file, f & f_binary ? "rb" : "r")) == 0)
- return (-1);
-
- h = gch->init();
- while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0)
- h->ops->hash(h, fbuf, sz);
- h->ops->done(h, buf);
- h->ops->destroy(h);
- e = ferror(fp);
- if (file)
- fclose(fp);
- return (e ? -1 : 0);
-}
-
-/* --- @puthex@ --- *
- *
- * Arguments: @const octet *buf@ = pointer to a binary buffer
- * @size_t sz@ = size of the buffer
- * @FILE *fp@ = pointer to output file handle
- *
- * Returns: ---
- *
- * Use: Writes a hex dump of a block of memory.
- */
-
-static void puthex(const octet *buf, size_t sz, FILE *fp)
-{
- while (sz) {
- fprintf(fp, "%02x", *buf++);
- sz--;
- }
-}
-
-/* --- @gethex@ --- *
- *
- * Arguments: @const char *p@ = pointer to input string
- * @octet *q@ = pointer to output buffer
- * @size_t sz@ = size of the output buffer
- * @char **pp@ = where to put the end pointer
- *
- * Returns: The number of bytes written to the buffer.
- *
- * Use: Reads hex dumps from the input string.
- */
-
-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);
-}
-
-/* --- @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 **g, *gg = 0;
- size_t sz = strlen(name);
- for (g = hashtab; *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);
-}