9e23f79717d719ca44cdc2087e6f7c1e87041d4f
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) {
196 /* --- @fhash_init@ ---*
198 * Arguments: @fhashstate *fh@ = pointer to fhash state to initialize
199 * @const gchash *gch@ = hash class to set
200 * @unsigned f@ initial flags to set
204 * Use: Initializes an @fhashstate@ structure.
207 void fhash_init(fhashstate
*fh
, const gchash
*gch
, unsigned f
)
208 { fh
->f
= f
; fh
->gch
= gch
; }
210 /* --- @fhash_free@ --- *
212 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
216 * Use: Frees an fhash state.
219 void fhash_free(fhashstate
*fh
) { return; }
223 * Arguments: @fhashstate *fh@ = pointer to fhash state
224 * @const char *file@ = file name to be hashed (null for stdin)
225 * @void *buf@ = pointer to hash output buffer
227 * Returns: Zero if it worked, nonzero on error.
229 * Use: Hashes a file.
232 int fhash(fhashstate
*fh
, const char *file
, void *buf
)
235 char fbuf
[1024 * 128];
241 if (!file
|| strcmp(file
, "-") == 0)
243 else if ((fp
= fopen(file
, fh
->f
& FHF_BINARY ?
"rb" : "r")) == 0)
246 if (fh
->f
& FHF_PROGRESS
) {
247 if (fprogress_init(&ff
, file
, fp
)) return (-1);
250 h
= GH_INIT(fh
->gch
);
251 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0) {
252 GH_HASH(h
, fbuf
, sz
);
253 if (fh
->f
& FHF_PROGRESS
) fprogress_update(&ff
, sz
);
255 if (ferror(fp
)) rc
= -1;
256 if (fp
!= stdin
) fclose(fp
);
257 if (fh
->f
& FHF_PROGRESS
) fprogress_done(&ff
);
263 /* --- @hfparse@ --- *
265 * Arguments: @hfpctx *hfp@ = pointer to the context structure
267 * Returns: A code indicating what happened.
269 * Use: Parses a line from the input file.
272 int hfparse(hfpctx
*hfp
)
277 dstr
*d
= hfp
->dline
;
280 /* --- Fetch the input line and get ready to parse --- */
283 if (dstr_putline(d
, hfp
->fp
) == EOF
) return (HF_EOF
);
286 /* --- Parse magic comments --- */
290 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
291 if (strcmp(q
, "hash") == 0) {
292 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
293 if ((gch
= gethash(q
)) == 0) return (HF_BAD
);
296 } else if (strcmp(q
, "encoding") == 0) {
297 if ((q
= str_getword(&p
)) == 0) return (HF_BAD
);
298 if ((ee
= getencoding(q
)) == 0) return (HF_BAD
);
301 } else if (strcmp(q
, "escape") == 0) {
302 hfp
->f
|= HFF_ESCAPE
;
308 /* --- Otherwise it's a file line --- */
311 while (*p
&& *p
!= ' ') p
++;
312 if (!*p
) return (HF_BAD
);
314 hsz
= hfp
->gch
->hashsz
;
315 if (hfp
->ee
->get(q
, hfp
->hbuf
, hsz
, 0) < hsz
) return (HF_BAD
);
317 case '*': hfp
->f
|= FHF_BINARY
; break;
318 case ' ': hfp
->f
&= ~FHF_BINARY
; break;
319 default: return (HF_BAD
);
324 if (hfp
->f
& HFF_ESCAPE
)
325 getstring(&p
, hfp
->dfile
, GSF_STRING
);
327 dstr_putm(hfp
->dfile
, p
, d
->len
- (p
- d
->buf
));
328 dstr_putz(hfp
->dfile
);
334 /*----- String I/O --------------------------------------------------------*/
336 /* --- @getstring@ --- *
338 * Arguments: @void *in@ = input source
339 * @dstr *d@ = destination string
340 * @unsigned f@ = input flags
342 * Returns: Zero if OK, nonzero on end-of-file.
344 * Use: Reads a filename (or something similar) from a stream.
347 static int nextch_file(void *in
)
348 { FILE *fp
= in
; return (getc(fp
)); }
350 static int nextch_string(void *in
)
351 { const unsigned char **p
= in
; return (*(*p
)++); }
353 int getstring(void *in
, dstr
*d
, unsigned f
)
356 int eofch
= (f
& GSF_STRING
) ?
0 : EOF
;
357 int (*nextch
)(void *) = (f
& GSF_STRING
) ? nextch_string
: nextch_file
;
360 /* --- Raw: just read exactly what's written up to a null byte --- */
363 if ((ch
= nextch(in
)) == eofch
)
369 if ((ch
= nextch(in
)) == eofch
)
376 /* --- Skip as far as whitespace --- *
378 * Also skip past comments.
386 do ch
= nextch(in
); while (ch
!= '\n' && ch
!= eofch
);
392 /* --- If the character is a quote then read a quoted string --- */
404 /* --- Now read all sorts of interesting things --- */
408 /* --- Handle an escaped thing --- */
415 case 'a': ch
= '\a'; break;
416 case 'b': ch
= '\b'; break;
417 case 'f': ch
= '\f'; break;
418 case 'n': ch
= '\n'; break;
419 case 'r': ch
= '\r'; break;
420 case 't': ch
= '\t'; break;
421 case 'v': ch
= '\v'; break;
428 /* --- If it's a quote or some other end marker then stop --- */
432 if (!q
&& isspace(ch
))
435 /* --- Otherwise contribute and continue --- */
438 if ((ch
= nextch(in
)) == eofch
)
448 /* --- @putstring@ --- *
450 * Arguments: @FILE *fp@ = stream to write on
451 * @const char *p@ = pointer to text
452 * @unsigned f@ = output flags
456 * Use: Emits a string to a stream.
459 void putstring(FILE *fp
, const char *p
, unsigned f
)
461 size_t sz
= strlen(p
);
465 /* --- Just write the string null terminated if raw --- */
468 fwrite(p
, 1, sz
+ 1, fp
);
472 /* --- Check for any dodgy characters --- */
475 for (q
= p
; *q
; q
++) {
476 if (isspace((unsigned char)*q
)) {
485 /* --- Emit the string --- */
487 for (q
= p
; *q
; q
++) {
489 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
490 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
491 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
492 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
493 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
494 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
495 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
496 case '`': fputc('\\', fp
); fputc('`', fp
); break;
497 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
498 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
511 /*----- That's all, folks -------------------------------------------------*/