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/macros.h>
46 #include <mLib/report.h>
50 #include <mLib/base32.h>
51 #include <mLib/base64.h>
56 /*----- Encoding and decoding ---------------------------------------------*/
58 /* --- Hex encoding --- */
60 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
63 fprintf(fp
, "%02x", *buf
++);
68 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
71 while (sz
> 0 && ISXDIGIT(p
[0]) && ISXDIGIT(p
[1])) {
76 *q
++ = strtoul(buf
, 0, 16);
86 /* --- Base64 encoding --- */
88 static void putbase64(const octet
*buf
, size_t sz
, FILE *fp
)
96 base64_encode(&b
, buf
, sz
, &d
);
97 base64_encode(&b
, 0, 0, &d
);
102 static size_t getbase64(const char *p
, octet
*q
, size_t sz
, char **pp
)
106 size_t n
= strlen(p
);
109 base64_decode(&b
, p
, n
, &d
);
110 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
111 base64_decode(&b
, 0, 0, &d
);
113 memcpy(q
, d
.buf
, sz
);
119 /* --- Base32 encoding --- */
121 static void putbase32(const octet
*buf
, size_t sz
, FILE *fp
)
129 base32_encode(&b
, buf
, sz
, &d
);
130 base32_encode(&b
, 0, 0, &d
);
135 static size_t getbase32(const char *p
, octet
*q
, size_t sz
, char **pp
)
139 size_t n
= strlen(p
);
142 base32_decode(&b
, p
, n
, &d
);
143 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
144 base32_decode(&b
, 0, 0, &d
);
146 memcpy(q
, d
.buf
, sz
);
154 const encodeops encodingtab
[] = {
155 #define TAB(tag, name) { #name, put##name, get##name },
161 const encodeops
*getencoding(const char *ename
)
165 for (e
= encodingtab
; e
->name
; e
++) {
166 if (STRCMP(ename
, ==, e
->name
))
172 /*----- File hashing ------------------------------------------------------*/
179 typedef struct fhent
{
186 /* --- @gethash@ --- *
188 * Arguments: @const char *name@ = pointer to name string
190 * Returns: Pointer to appropriate hash class.
192 * Use: Chooses a hash function by name.
195 const gchash
*gethash(const char *name
)
197 const gchash
*const *g
, *gg
= 0;
198 size_t sz
= strlen(name
);
199 for (g
= ghashtab
; *g
; g
++) {
200 if (STRNCMP(name
, ==, (*g
)->name
, sz
)) {
201 if ((*g
)->name
[sz
] == 0) {
213 /* --- @describefile@ --- *
215 * Arguments: @const struct stat *st@ = pointer to file state
217 * Returns: A snappy one-word description of the file.
220 const char *describefile(const struct stat
*st
)
222 switch (st
->st_mode
& S_IFMT
) {
223 case S_IFBLK
: return ("block-special");
224 case S_IFCHR
: return ("char-special");
225 case S_IFIFO
: return ("fifo");
226 case S_IFREG
: return ("file");
227 case S_IFLNK
: return ("symlink");
228 case S_IFDIR
: return ("directory");
229 case S_IFSOCK
: return ("socket");
230 default: return ("unknown");
234 /* --- @fhash_init@ ---*
236 * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize
237 * @const gchash *gch@ = hash class to set
238 * @unsigned f@ initial flags to set
242 * Use: Initializes an @fhashstate@ structure.
245 void fhash_init(fhashstate
*fh
, const gchash
*gch
, unsigned f
)
246 { fh
->f
= f
; fh
->gch
= gch
; fh
->ents
= 0; }
248 /* --- @fhash_free@ --- *
250 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
254 * Use: Frees an fhash state.
257 static void freefhents(struct fhent
*fhe
)
261 for (; fhe
; fhe
= ffhe
) {
263 freefhents(fhe
->sub
);
268 void fhash_free(fhashstate
*fh
)
269 { freefhents(fh
->ents
); }
273 * Arguments: @fhashstate *fh@ = pointer to fhash state
274 * @const char *file@ = file name to be hashed (null for stdin)
275 * @void *buf@ = pointer to hash output buffer
277 * Returns: Zero if it worked, nonzero on error.
279 * Use: Hashes a file.
282 int fhash(fhashstate
*fh
, const char *file
, void *buf
)
285 char fbuf
[1024 * 128];
289 struct fhent
*fhe
, **ffhe
;
294 if (!file
|| STRCMP(file
, ==, "-"))
296 else if ((fp
= fopen(file
, fh
->f
& FHF_BINARY ?
"rb" : "r")) == 0)
299 if (fh
->f
& FHF_PROGRESS
) {
300 if (fprogress_init(&ff
, file ? file
: "<stdin>", fp
)) return (-1);
303 if (fh
->f
& FHF_JUNK
) {
305 if (STRNCMP(p
, ==, "./", 2)) p
+= 2;
309 if (*q
== '/' || *q
== 0) {
311 for (; *ffhe
; ffhe
= &(*ffhe
)->next
) {
313 if (STRNCMP(p
, ==, fhe
->name
, n
) && fhe
->name
[n
] == 0)
316 fhe
= xmalloc(offsetof(struct fhent
, name
) + n
+ 1);
318 fhe
->ty
= *q
== '/' ? FHETY_DIR
: FHETY_FILE
;
321 memcpy(fhe
->name
, p
, n
); fhe
->name
[n
] = 0;
332 h
= GH_INIT(fh
->gch
);
333 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0) {
334 GH_HASH(h
, fbuf
, sz
);
335 if (fh
->f
& FHF_PROGRESS
) fprogress_update(&ff
, sz
);
337 if (ferror(fp
)) rc
= -1;
338 if (fp
!= stdin
) fclose(fp
);
339 if (fh
->f
& FHF_PROGRESS
) fprogress_done(&ff
);
345 /* --- @fhash_junk@ --- *
347 * Arguments: @fhashstate *fh@ = pointer to fhash state
348 * @void (*func)(const char *, const struct stat *, void *)@
349 * @void *p@ = pointer to pass to function
351 * Returns: Positive if any junk was found, negative on error, zero if
352 * everything was fine.
354 * Use: Reports junk files in any directories covered by the hash
359 int (*func
)(const char *, const struct stat
*, void *);
364 static int fhjunk(struct fhjunk
*fhj
, struct fhent
*ents
)
371 size_t n
= fhj
->d
->len
;
374 dname
= n ? fhj
->d
->buf
: ".";
375 if ((dp
= opendir(dname
)) == 0) {
376 moan("failed to open directory `%s': %s", dname
, strerror(errno
));
381 dstr_putc(fhj
->d
, '/');
384 while (errno
= 0, (d
= readdir(dp
)) != 0) {
385 if (STRCMP(d
->d_name
, ==, ".") || STRCMP(d
->d_name
, ==, ".."))
387 for (fhe
= ents
; fhe
; fhe
= fhe
->next
) {
388 if (STRCMP(d
->d_name
, ==, fhe
->name
)) goto found
;
391 dstr_puts(fhj
->d
, d
->d_name
);
392 if (!lstat(fhj
->d
->buf
, &st
)) {
394 rrc
= fhj
->func(fhj
->d
->buf
, &st
, fhj
->p
);
397 rrc
= fhj
->func(fhj
->d
->buf
, 0, fhj
->p
);
399 if (rrc
< 0) rc
= -1;
404 moan("failed to read directory `%s': %s", dname
, strerror(errno
));
409 for (fhe
= ents
; fhe
; fhe
= fhe
->next
) {
410 if (fhe
->ty
== FHETY_DIR
) {
412 dstr_puts(fhj
->d
, fhe
->name
);
413 rrc
= fhjunk(fhj
, fhe
->sub
);
414 if (rrc
< 0) rc
= -1;
415 else if (!rc
) rc
= rrc
;
422 int fhash_junk(fhashstate
*fh
,
423 int (*func
)(const char *, const struct stat
*, void *),
433 rc
= fhjunk(&fhj
, fh
->ents
);
438 /* --- @hfparse@ --- *
440 * Arguments: @hfpctx *hfp@ = pointer to the context structure
442 * Returns: A code indicating what happened.
444 * Use: Parses a line from the input file.
447 int hfparse(hfpctx
*hfp
)
452 dstr
*d
= hfp
->dline
;
455 /* --- Fetch the input line and get ready to parse --- */
458 if (dstr_putline(d
, hfp
->fp
) == EOF
) return (HF_EOF
);
461 /* --- Parse magic comments --- */
465 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
466 if (STRCMP(q
, ==, "hash")) {
467 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
468 if ((gch
= gethash(q
)) == 0) return (HF_BAD
);
471 } else if (STRCMP(q
, ==, "encoding")) {
472 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
473 if ((ee
= getencoding(q
)) == 0) return (HF_BAD
);
476 } else if (STRCMP(q
, ==, "escape")) {
477 hfp
->f
|= HFF_ESCAPE
;
483 /* --- Otherwise it's a file line --- */
486 while (*p
&& *p
!= ' ') p
++;
487 if (!*p
) return (HF_BAD
);
489 hsz
= hfp
->gch
->hashsz
;
490 if (hfp
->ee
->get(q
, hfp
->hbuf
, hsz
, 0) < hsz
) return (HF_BAD
);
492 case '*': hfp
->f
|= FHF_BINARY
; break;
493 case ' ': hfp
->f
&= ~FHF_BINARY
; break;
494 default: return (HF_BAD
);
499 if (hfp
->f
& HFF_ESCAPE
)
500 getstring(&p
, hfp
->dfile
, GSF_STRING
);
502 dstr_putm(hfp
->dfile
, p
, d
->len
- (p
- d
->buf
));
503 dstr_putz(hfp
->dfile
);
509 /*----- String I/O --------------------------------------------------------*/
511 /* --- @getstring@ --- *
513 * Arguments: @void *in@ = input source
514 * @dstr *d@ = destination string
515 * @unsigned f@ = input flags
517 * Returns: Zero if OK, nonzero on end-of-file.
519 * Use: Reads a filename (or something similar) from a stream.
522 static int nextch_file(void *in
)
523 { FILE *fp
= in
; return (getc(fp
)); }
525 static int nextch_string(void *in
)
526 { const unsigned char **p
= in
; return (*(*p
)++); }
528 int getstring(void *in
, dstr
*d
, unsigned f
)
531 int eofch
= (f
& GSF_STRING
) ?
0 : EOF
;
532 int (*nextch
)(void *) = (f
& GSF_STRING
) ? nextch_string
: nextch_file
;
535 /* --- Raw: just read exactly what's written up to a null byte --- */
538 if ((ch
= nextch(in
)) == eofch
)
544 if ((ch
= nextch(in
)) == eofch
)
551 /* --- Skip as far as whitespace --- *
553 * Also skip past comments.
561 do ch
= nextch(in
); while (ch
!= '\n' && ch
!= eofch
);
567 /* --- If the character is a quote then read a quoted string --- */
579 /* --- Now read all sorts of interesting things --- */
583 /* --- Handle an escaped thing --- */
590 case 'a': ch
= '\a'; break;
591 case 'b': ch
= '\b'; break;
592 case 'f': ch
= '\f'; break;
593 case 'n': ch
= '\n'; break;
594 case 'r': ch
= '\r'; break;
595 case 't': ch
= '\t'; break;
596 case 'v': ch
= '\v'; break;
603 /* --- If it's a quote or some other end marker then stop --- */
607 if (!q
&& ISSPACE(ch
))
610 /* --- Otherwise contribute and continue --- */
613 if ((ch
= nextch(in
)) == eofch
)
623 /* --- @putstring@ --- *
625 * Arguments: @FILE *fp@ = stream to write on
626 * @const char *p@ = pointer to text
627 * @unsigned f@ = output flags
631 * Use: Emits a string to a stream.
634 void putstring(FILE *fp
, const char *p
, unsigned f
)
636 size_t sz
= strlen(p
);
640 /* --- Just write the string null terminated if raw --- */
643 fwrite(p
, 1, sz
+ 1, fp
);
647 /* --- Check for any dodgy characters --- */
650 for (q
= p
; *q
; q
++) {
660 /* --- Emit the string --- */
662 for (q
= p
; *q
; q
++) {
664 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
665 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
666 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
667 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
668 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
669 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
670 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
671 case '`': fputc('\\', fp
); fputc('`', fp
); break;
672 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
673 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
686 /*----- That's all, folks -------------------------------------------------*/