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
39 #include <mLib/dstr.h>
43 #include <mLib/base32.h>
44 #include <mLib/base64.h>
49 /*----- Encoding and decoding ---------------------------------------------*/
51 /* --- Hex encoding --- */
53 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
56 fprintf(fp
, "%02x", *buf
++);
61 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
65 isxdigit((unsigned char)p
[0]) &&
66 isxdigit((unsigned char)p
[1])) {
71 *q
++ = strtoul(buf
, 0, 16);
81 /* --- Base64 encoding --- */
83 static void putbase64(const octet
*buf
, size_t sz
, FILE *fp
)
91 base64_encode(&b
, buf
, sz
, &d
);
92 base64_encode(&b
, 0, 0, &d
);
97 static size_t getbase64(const char *p
, octet
*q
, size_t sz
, char **pp
)
101 size_t n
= strlen(p
);
104 base64_decode(&b
, p
, n
, &d
);
105 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
106 base64_decode(&b
, 0, 0, &d
);
108 memcpy(q
, d
.buf
, sz
);
114 /* --- Base32 encoding --- */
116 static void putbase32(const octet
*buf
, size_t sz
, FILE *fp
)
124 base32_encode(&b
, buf
, sz
, &d
);
125 base32_encode(&b
, 0, 0, &d
);
130 static size_t getbase32(const char *p
, octet
*q
, size_t sz
, char **pp
)
134 size_t n
= strlen(p
);
137 base32_decode(&b
, p
, n
, &d
);
138 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
139 base32_decode(&b
, 0, 0, &d
);
141 memcpy(q
, d
.buf
, sz
);
149 const encodeops encodingtab
[] = {
150 #define TAB(tag, name) { #name, put##name, get##name },
156 const encodeops
*getencoding(const char *ename
)
160 for (e
= encodingtab
; e
->name
; e
++) {
161 if (strcmp(ename
, e
->name
) == 0)
167 /*----- File hashing ------------------------------------------------------*/
169 /* --- @gethash@ --- *
171 * Arguments: @const char *name@ = pointer to name string
173 * Returns: Pointer to appropriate hash class.
175 * Use: Chooses a hash function by name.
178 const gchash
*gethash(const char *name
)
180 const gchash
*const *g
, *gg
= 0;
181 size_t sz
= strlen(name
);
182 for (g
= ghashtab
; *g
; g
++) {
183 if (strncmp(name
, (*g
)->name
, sz
) == 0) {
184 if ((*g
)->name
[sz
] == 0) {
198 * Arguments: @const gchash *gch@ = pointer to hash function to use
199 * @unsigned f@ = flags to set
200 * @const char *file@ = file name to be hashed (null for stdin)
201 * @void *buf@ = pointer to hash output buffer
203 * Returns: Zero if it worked, nonzero on error.
205 * Use: Hashes a file.
208 int fhash(const gchash
*gch
, unsigned f
, const char *file
, void *buf
)
211 char fbuf
[1024 * 128];
217 if (!file
|| strcmp(file
, "-") == 0)
219 else if ((fp
= fopen(file
, f
& FHF_BINARY ?
"rb" : "r")) == 0)
222 if (f
& FHF_PROGRESS
) {
223 if (fprogress_init(&ff
, file
, fp
)) return (-1);
227 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0) {
228 GH_HASH(h
, fbuf
, sz
);
229 if (f
& FHF_PROGRESS
) fprogress_update(&ff
, sz
);
231 if (ferror(fp
)) rc
= -1;
232 if (fp
!= stdin
) fclose(fp
);
233 if (f
& FHF_PROGRESS
) fprogress_done(&ff
);
239 /* --- @hfparse@ --- *
241 * Arguments: @hfpctx *hfp@ = pointer to the context structure
243 * Returns: A code indicating what happened.
245 * Use: Parses a line from the input file.
248 int hfparse(hfpctx
*hfp
)
253 dstr
*d
= hfp
->dline
;
256 /* --- Fetch the input line and get ready to parse --- */
259 if (dstr_putline(d
, hfp
->fp
) == EOF
) return (HF_EOF
);
262 /* --- Parse magic comments --- */
266 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
267 if (strcmp(q
, "hash") == 0) {
268 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
269 if ((gch
= gethash(q
)) == 0) return (HF_BAD
);
272 } else if (strcmp(q
, "encoding") == 0) {
273 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
274 if ((ee
= getencoding(q
)) == 0) return (HF_BAD
);
277 } else if (strcmp(q
, "escape") == 0) {
278 hfp
->f
|= HFF_ESCAPE
;
284 /* --- Otherwise it's a file line --- */
287 while (*p
&& *p
!= ' ') p
++;
288 if (!*p
) return (HF_BAD
);
290 hsz
= hfp
->gch
->hashsz
;
291 if (hfp
->ee
->get(q
, hfp
->hbuf
, hsz
, 0) < hsz
) return (HF_BAD
);
293 case '*': hfp
->f
|= FHF_BINARY
; break;
294 case ' ': hfp
->f
&= ~FHF_BINARY
; break;
295 default: return (HF_BAD
);
300 if (hfp
->f
& HFF_ESCAPE
)
301 getstring(&p
, hfp
->dfile
, GSF_STRING
);
303 dstr_putm(hfp
->dfile
, p
, d
->len
- (p
- d
->buf
));
304 dstr_putz(hfp
->dfile
);
310 /*----- String I/O --------------------------------------------------------*/
312 /* --- @getstring@ --- *
314 * Arguments: @void *in@ = input source
315 * @dstr *d@ = destination string
316 * @unsigned f@ = input flags
318 * Returns: Zero if OK, nonzero on end-of-file.
320 * Use: Reads a filename (or something similar) from a stream.
323 static int nextch_file(void *in
)
324 { FILE *fp
= in
; return (getc(fp
)); }
326 static int nextch_string(void *in
)
327 { const unsigned char **p
= in
; return (*(*p
)++); }
329 int getstring(void *in
, dstr
*d
, unsigned f
)
332 int eofch
= (f
& GSF_STRING
) ?
0 : EOF
;
333 int (*nextch
)(void *) = (f
& GSF_STRING
) ? nextch_string
: nextch_file
;
336 /* --- Raw: just read exactly what's written up to a null byte --- */
339 if ((ch
= nextch(in
)) == eofch
)
345 if ((ch
= nextch(in
)) == eofch
)
352 /* --- Skip as far as whitespace --- *
354 * Also skip past comments.
362 do ch
= nextch(in
); while (ch
!= '\n' && ch
!= eofch
);
368 /* --- If the character is a quote then read a quoted string --- */
380 /* --- Now read all sorts of interesting things --- */
384 /* --- Handle an escaped thing --- */
391 case 'a': ch
= '\a'; break;
392 case 'b': ch
= '\b'; break;
393 case 'f': ch
= '\f'; break;
394 case 'n': ch
= '\n'; break;
395 case 'r': ch
= '\r'; break;
396 case 't': ch
= '\t'; break;
397 case 'v': ch
= '\v'; break;
404 /* --- If it's a quote or some other end marker then stop --- */
408 if (!q
&& isspace(ch
))
411 /* --- Otherwise contribute and continue --- */
414 if ((ch
= nextch(in
)) == eofch
)
424 /* --- @putstring@ --- *
426 * Arguments: @FILE *fp@ = stream to write on
427 * @const char *p@ = pointer to text
428 * @unsigned f@ = output flags
432 * Use: Emits a string to a stream.
435 void putstring(FILE *fp
, const char *p
, unsigned f
)
437 size_t sz
= strlen(p
);
441 /* --- Just write the string null terminated if raw --- */
444 fwrite(p
, 1, sz
+ 1, fp
);
448 /* --- Check for any dodgy characters --- */
451 for (q
= p
; *q
; q
++) {
452 if (isspace((unsigned char)*q
)) {
461 /* --- Emit the string --- */
463 for (q
= p
; *q
; q
++) {
465 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
466 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
467 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
468 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
469 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
470 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
471 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
472 case '`': fputc('\\', fp
); fputc('`', fp
); break;
473 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
474 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
487 /*----- That's all, folks -------------------------------------------------*/