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>
42 #include <mLib/base32.h>
43 #include <mLib/base64.h>
48 /*----- Encoding and decoding ---------------------------------------------*/
50 /* --- Hex encoding --- */
52 static void puthex(const octet
*buf
, size_t sz
, FILE *fp
)
55 fprintf(fp
, "%02x", *buf
++);
60 static size_t gethex(const char *p
, octet
*q
, size_t sz
, char **pp
)
64 isxdigit((unsigned char)p
[0]) &&
65 isxdigit((unsigned char)p
[1])) {
70 *q
++ = strtoul(buf
, 0, 16);
80 /* --- Base64 encoding --- */
82 static void putbase64(const octet
*buf
, size_t sz
, FILE *fp
)
90 base64_encode(&b
, buf
, sz
, &d
);
91 base64_encode(&b
, 0, 0, &d
);
96 static size_t getbase64(const char *p
, octet
*q
, size_t sz
, char **pp
)
100 size_t n
= strlen(p
);
103 base64_decode(&b
, p
, n
, &d
);
104 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
105 base64_decode(&b
, 0, 0, &d
);
107 memcpy(q
, d
.buf
, sz
);
113 /* --- Base32 encoding --- */
115 static void putbase32(const octet
*buf
, size_t sz
, FILE *fp
)
123 base32_encode(&b
, buf
, sz
, &d
);
124 base32_encode(&b
, 0, 0, &d
);
129 static size_t getbase32(const char *p
, octet
*q
, size_t sz
, char **pp
)
133 size_t n
= strlen(p
);
136 base32_decode(&b
, p
, n
, &d
);
137 if (pp
) *pp
= (/*unconst*/ char *)p
+ n
;
138 base32_decode(&b
, 0, 0, &d
);
140 memcpy(q
, d
.buf
, sz
);
148 const encodeops encodingtab
[] = {
149 #define TAB(tag, name) { #name, put##name, get##name },
155 const encodeops
*getencoding(const char *ename
)
159 for (e
= encodingtab
; e
->name
; e
++) {
160 if (strcmp(ename
, e
->name
) == 0)
166 /*----- File hashing ------------------------------------------------------*/
170 * Arguments: @const gchash *gch@ = pointer to hash function to use
171 * @unsigned f@ = flags to set
172 * @const char *file@ = file name to be hashed (null for stdin)
173 * @void *buf@ = pointer to hash output buffer
175 * Returns: Zero if it worked, nonzero on error.
177 * Use: Hashes a file.
180 int fhash(const gchash
*gch
, unsigned f
, const char *file
, void *buf
)
183 char fbuf
[1024 * 128];
189 if (!file
|| strcmp(file
, "-") == 0)
191 else if ((fp
= fopen(file
, f
& FHF_BINARY ?
"rb" : "r")) == 0)
194 if (f
& FHF_PROGRESS
) {
195 if (fprogress_init(&ff
, file
, fp
)) return (-1);
199 while ((sz
= fread(fbuf
, 1, sizeof(fbuf
), fp
)) > 0) {
200 GH_HASH(h
, fbuf
, sz
);
201 if (f
& FHF_PROGRESS
) fprogress_update(&ff
, sz
);
203 if (ferror(fp
)) rc
= -1;
204 if (fp
!= stdin
) fclose(fp
);
205 if (f
& FHF_PROGRESS
) fprogress_done(&ff
);
211 /*----- String I/O --------------------------------------------------------*/
213 /* --- @getstring@ --- *
215 * Arguments: @void *in@ = input source
216 * @dstr *d@ = destination string
217 * @unsigned f@ = input flags
219 * Returns: Zero if OK, nonzero on end-of-file.
221 * Use: Reads a filename (or something similar) from a stream.
224 static int nextch_file(void *in
)
225 { FILE *fp
= in
; return (getc(fp
)); }
227 static int nextch_string(void *in
)
228 { const unsigned char **p
= in
; return (*(*p
)++); }
230 int getstring(void *in
, dstr
*d
, unsigned f
)
233 int eofch
= (f
& GSF_STRING
) ?
0 : EOF
;
234 int (*nextch
)(void *) = (f
& GSF_STRING
) ? nextch_string
: nextch_file
;
237 /* --- Raw: just read exactly what's written up to a null byte --- */
240 if ((ch
= nextch(in
)) == eofch
)
246 if ((ch
= nextch(in
)) == eofch
)
253 /* --- Skip as far as whitespace --- *
255 * Also skip past comments.
263 do ch
= nextch(in
); while (ch
!= '\n' && ch
!= eofch
);
269 /* --- If the character is a quote then read a quoted string --- */
281 /* --- Now read all sorts of interesting things --- */
285 /* --- Handle an escaped thing --- */
292 case 'a': ch
= '\a'; break;
293 case 'b': ch
= '\b'; break;
294 case 'f': ch
= '\f'; break;
295 case 'n': ch
= '\n'; break;
296 case 'r': ch
= '\r'; break;
297 case 't': ch
= '\t'; break;
298 case 'v': ch
= '\v'; break;
305 /* --- If it's a quote or some other end marker then stop --- */
309 if (!q
&& isspace(ch
))
312 /* --- Otherwise contribute and continue --- */
315 if ((ch
= nextch(in
)) == eofch
)
325 /* --- @putstring@ --- *
327 * Arguments: @FILE *fp@ = stream to write on
328 * @const char *p@ = pointer to text
329 * @unsigned f@ = output flags
333 * Use: Emits a string to a stream.
336 void putstring(FILE *fp
, const char *p
, unsigned f
)
338 size_t sz
= strlen(p
);
342 /* --- Just write the string null terminated if raw --- */
345 fwrite(p
, 1, sz
+ 1, fp
);
349 /* --- Check for any dodgy characters --- */
352 for (q
= p
; *q
; q
++) {
353 if (isspace((unsigned char)*q
)) {
362 /* --- Emit the string --- */
364 for (q
= p
; *q
; q
++) {
366 case '\a': fputc('\\', fp
); fputc('a', fp
); break;
367 case '\b': fputc('\\', fp
); fputc('b', fp
); break;
368 case '\f': fputc('\\', fp
); fputc('f', fp
); break;
369 case '\n': fputc('\\', fp
); fputc('n', fp
); break;
370 case '\r': fputc('\\', fp
); fputc('r', fp
); break;
371 case '\t': fputc('\\', fp
); fputc('t', fp
); break;
372 case '\v': fputc('\\', fp
); fputc('v', fp
); break;
373 case '`': fputc('\\', fp
); fputc('`', fp
); break;
374 case '\'': fputc('\\', fp
); fputc('\'', fp
); break;
375 case '\"': fputc('\\', fp
); fputc('\"', fp
); break;
388 /*----- That's all, folks -------------------------------------------------*/