3 * Common functions for hashing utilities
5 * (c) 2011 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
40 #include <sys/types.h>
43 #include <mLib/alloc.h>
44 #include <mLib/dstr.h>
45 #include <mLib/report.h>
49 #include <mLib/base32.h>
50 #include <mLib/base64.h>
55 /*----- Encoding and decoding ---------------------------------------------*/
57 /* --- Hex encoding --- */
59 static void puthex(const octet *buf, size_t sz, FILE *fp)
62 fprintf(fp, "%02x", *buf++);
67 static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
71 isxdigit((unsigned char)p[0]) &&
72 isxdigit((unsigned char)p[1])) {
77 *q++ = strtoul(buf, 0, 16);
87 /* --- Base64 encoding --- */
89 static void putbase64(const octet *buf, size_t sz, FILE *fp)
97 base64_encode(&b, buf, sz, &d);
98 base64_encode(&b, 0, 0, &d);
103 static size_t getbase64(const char *p, octet *q, size_t sz, char **pp)
107 size_t n = strlen(p);
110 base64_decode(&b, p, n, &d);
111 if (pp) *pp = (/*unconst*/ char *)p + n;
112 base64_decode(&b, 0, 0, &d);
114 memcpy(q, d.buf, sz);
120 /* --- Base32 encoding --- */
122 static void putbase32(const octet *buf, size_t sz, FILE *fp)
130 base32_encode(&b, buf, sz, &d);
131 base32_encode(&b, 0, 0, &d);
136 static size_t getbase32(const char *p, octet *q, size_t sz, char **pp)
140 size_t n = strlen(p);
143 base32_decode(&b, p, n, &d);
144 if (pp) *pp = (/*unconst*/ char *)p + n;
145 base32_decode(&b, 0, 0, &d);
147 memcpy(q, d.buf, sz);
155 const encodeops encodingtab[] = {
156 #define TAB(tag, name) { #name, put##name, get##name },
162 const encodeops *getencoding(const char *ename)
166 for (e = encodingtab; e->name; e++) {
167 if (strcmp(ename, e->name) == 0)
173 /*----- File hashing ------------------------------------------------------*/
180 typedef struct fhent {
187 /* --- @gethash@ --- *
189 * Arguments: @const char *name@ = pointer to name string
191 * Returns: Pointer to appropriate hash class.
193 * Use: Chooses a hash function by name.
196 const gchash *gethash(const char *name)
198 const gchash *const *g, *gg = 0;
199 size_t sz = strlen(name);
200 for (g = ghashtab; *g; g++) {
201 if (strncmp(name, (*g)->name, sz) == 0) {
202 if ((*g)->name[sz] == 0) {
214 /* --- @describefile@ --- *
216 * Arguments: @const struct stat *st@ = pointer to file state
218 * Returns: A snappy one-word description of the file.
221 const char *describefile(const struct stat *st)
223 switch (st->st_mode & S_IFMT) {
224 case S_IFBLK: return ("block-special");
225 case S_IFCHR: return ("char-special");
226 case S_IFIFO: return ("fifo");
227 case S_IFREG: return ("file");
228 case S_IFLNK: return ("symlink");
229 case S_IFDIR: return ("directory");
230 case S_IFSOCK: return ("socket");
231 default: return ("unknown");
235 /* --- @fhash_init@ ---*
237 * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize
238 * @const gchash *gch@ = hash class to set
239 * @unsigned f@ initial flags to set
243 * Use: Initializes an @fhashstate@ structure.
246 void fhash_init(fhashstate *fh, const gchash *gch, unsigned f)
247 { fh->f = f; fh->gch = gch; fh->ents = 0; }
249 /* --- @fhash_free@ --- *
251 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
255 * Use: Frees an fhash state.
258 static void freefhents(struct fhent *fhe)
262 for (; fhe; fhe = ffhe) {
264 freefhents(fhe->sub);
269 void fhash_free(fhashstate *fh)
270 { freefhents(fh->ents); }
274 * Arguments: @fhashstate *fh@ = pointer to fhash state
275 * @const char *file@ = file name to be hashed (null for stdin)
276 * @void *buf@ = pointer to hash output buffer
278 * Returns: Zero if it worked, nonzero on error.
280 * Use: Hashes a file.
283 int fhash(fhashstate *fh, const char *file, void *buf)
286 char fbuf[1024 * 128];
290 struct fhent *fhe, **ffhe;
295 if (!file || strcmp(file, "-") == 0)
297 else if ((fp = fopen(file, fh->f & FHF_BINARY ? "rb" : "r")) == 0)
300 if (fh->f & FHF_PROGRESS) {
301 if (fprogress_init(&ff, file ? file : "<stdin>", fp)) return (-1);
304 if (fh->f & FHF_JUNK) {
306 if (strncmp(p, "./", 2) == 0) p += 2;
310 if (*q == '/' || *q == 0) {
312 for (; *ffhe; ffhe = &(*ffhe)->next) {
314 if (strncmp(p, fhe->name, n) == 0 && fhe->name[n] == 0)
317 fhe = xmalloc(offsetof(struct fhent, name) + n + 1);
319 fhe->ty = *q == '/' ? FHETY_DIR : FHETY_FILE;
322 memcpy(fhe->name, p, n); fhe->name[n] = 0;
333 h = GH_INIT(fh->gch);
334 while ((sz = fread(fbuf, 1, sizeof(fbuf), fp)) > 0) {
335 GH_HASH(h, fbuf, sz);
336 if (fh->f & FHF_PROGRESS) fprogress_update(&ff, sz);
338 if (ferror(fp)) rc = -1;
339 if (fp != stdin) fclose(fp);
340 if (fh->f & FHF_PROGRESS) fprogress_done(&ff);
346 /* --- @fhash_junk@ --- *
348 * Arguments: @fhashstate *fh@ = pointer to fhash state
349 * @void (*func)(const char *, const struct stat *, void *)@
350 * @void *p@ = pointer to pass to function
352 * Returns: Positive if any junk was found, negative on error, zero if
353 * everything was fine.
355 * Use: Reports junk files in any directories covered by the hash
360 int (*func)(const char *, const struct stat *, void *);
365 static int fhjunk(struct fhjunk *fhj, struct fhent *ents)
372 size_t n = fhj->d->len;
375 dname = n ? fhj->d->buf : ".";
376 if ((dp = opendir(dname)) == 0) {
377 moan("failed to open directory `%s': %s", dname, strerror(errno));
382 dstr_putc(fhj->d, '/');
385 while (errno = 0, (d = readdir(dp)) != 0) {
386 if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0)
388 for (fhe = ents; fhe; fhe = fhe->next) {
389 if (strcmp(d->d_name, fhe->name) == 0) goto found;
392 dstr_puts(fhj->d, d->d_name);
393 if (!lstat(fhj->d->buf, &st)) {
395 rrc = fhj->func(fhj->d->buf, &st, fhj->p);
398 rrc = fhj->func(fhj->d->buf, 0, fhj->p);
400 if (rrc < 0) rc = -1;
405 moan("failed to read directory `%s': %s", dname, strerror(errno));
410 for (fhe = ents; fhe; fhe = fhe->next) {
411 if (fhe->ty == FHETY_DIR) {
413 dstr_puts(fhj->d, fhe->name);
414 rrc = fhjunk(fhj, fhe->sub);
415 if (rrc < 0) rc = -1;
416 else if (!rc) rc = rrc;
423 int fhash_junk(fhashstate *fh,
424 int (*func)(const char *, const struct stat *, void *),
434 rc = fhjunk(&fhj, fh->ents);
439 /* --- @hfparse@ --- *
441 * Arguments: @hfpctx *hfp@ = pointer to the context structure
443 * Returns: A code indicating what happened.
445 * Use: Parses a line from the input file.
448 int hfparse(hfpctx *hfp)
453 dstr *d = hfp->dline;
456 /* --- Fetch the input line and get ready to parse --- */
459 if (dstr_putline(d, hfp->fp) == EOF) return (HF_EOF);
462 /* --- Parse magic comments --- */
466 if ((q = str_getword(&p)) == 0) return (HF_BAD);
467 if (strcmp(q, "hash") == 0) {
468 if ((q = str_getword(&p)) == 0) return (HF_BAD);
469 if ((gch = gethash(q)) == 0) return (HF_BAD);
472 } else if (strcmp(q, "encoding") == 0) {
473 if ((q = str_getword(&p)) == 0) return (HF_BAD);
474 if ((ee = getencoding(q)) == 0) return (HF_BAD);
477 } else if (strcmp(q, "escape") == 0) {
478 hfp->f |= HFF_ESCAPE;
484 /* --- Otherwise it's a file line --- */
487 while (*p && *p != ' ') p++;
488 if (!*p) return (HF_BAD);
490 hsz = hfp->gch->hashsz;
491 if (hfp->ee->get(q, hfp->hbuf, hsz, 0) < hsz) return (HF_BAD);
493 case '*': hfp->f |= FHF_BINARY; break;
494 case ' ': hfp->f &= ~FHF_BINARY; break;
495 default: return (HF_BAD);
500 if (hfp->f & HFF_ESCAPE)
501 getstring(&p, hfp->dfile, GSF_STRING);
503 dstr_putm(hfp->dfile, p, d->len - (p - d->buf));
504 dstr_putz(hfp->dfile);
510 /*----- String I/O --------------------------------------------------------*/
512 /* --- @getstring@ --- *
514 * Arguments: @void *in@ = input source
515 * @dstr *d@ = destination string
516 * @unsigned f@ = input flags
518 * Returns: Zero if OK, nonzero on end-of-file.
520 * Use: Reads a filename (or something similar) from a stream.
523 static int nextch_file(void *in)
524 { FILE *fp = in; return (getc(fp)); }
526 static int nextch_string(void *in)
527 { const unsigned char **p = in; return (*(*p)++); }
529 int getstring(void *in, dstr *d, unsigned f)
532 int eofch = (f & GSF_STRING) ? 0 : EOF;
533 int (*nextch)(void *) = (f & GSF_STRING) ? nextch_string : nextch_file;
536 /* --- Raw: just read exactly what's written up to a null byte --- */
539 if ((ch = nextch(in)) == eofch)
545 if ((ch = nextch(in)) == eofch)
552 /* --- Skip as far as whitespace --- *
554 * Also skip past comments.
562 do ch = nextch(in); while (ch != '\n' && ch != eofch);
568 /* --- If the character is a quote then read a quoted string --- */
580 /* --- Now read all sorts of interesting things --- */
584 /* --- Handle an escaped thing --- */
591 case 'a': ch = '\a'; break;
592 case 'b': ch = '\b'; break;
593 case 'f': ch = '\f'; break;
594 case 'n': ch = '\n'; break;
595 case 'r': ch = '\r'; break;
596 case 't': ch = '\t'; break;
597 case 'v': ch = '\v'; break;
604 /* --- If it's a quote or some other end marker then stop --- */
608 if (!q && isspace(ch))
611 /* --- Otherwise contribute and continue --- */
614 if ((ch = nextch(in)) == eofch)
624 /* --- @putstring@ --- *
626 * Arguments: @FILE *fp@ = stream to write on
627 * @const char *p@ = pointer to text
628 * @unsigned f@ = output flags
632 * Use: Emits a string to a stream.
635 void putstring(FILE *fp, const char *p, unsigned f)
637 size_t sz = strlen(p);
641 /* --- Just write the string null terminated if raw --- */
644 fwrite(p, 1, sz + 1, fp);
648 /* --- Check for any dodgy characters --- */
651 for (q = p; *q; q++) {
652 if (isspace((unsigned char)*q)) {
661 /* --- Emit the string --- */
663 for (q = p; *q; q++) {
665 case '\a': fputc('\\', fp); fputc('a', fp); break;
666 case '\b': fputc('\\', fp); fputc('b', fp); break;
667 case '\f': fputc('\\', fp); fputc('f', fp); break;
668 case '\n': fputc('\\', fp); fputc('n', fp); break;
669 case '\r': fputc('\\', fp); fputc('r', fp); break;
670 case '\t': fputc('\\', fp); fputc('t', fp); break;
671 case '\v': fputc('\\', fp); fputc('v', fp); break;
672 case '`': fputc('\\', fp); fputc('`', fp); break;
673 case '\'': fputc('\\', fp); fputc('\'', fp); break;
674 case '\"': fputc('\\', fp); fputc('\"', fp); break;
687 /*----- That's all, folks -------------------------------------------------*/