Hash utilities: maintain a hash state object, not a bundle of arguments.
[u/mdw/catacomb] / cc-hash.c
1 /* -*-c-*-
2 *
3 * Common functions for hashing utilities
4 *
5 * (c) 2011 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of Catacomb.
11 *
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.
16 *
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.
21 *
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,
25 * MA 02111-1307, USA.
26 */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #define _FILE_OFFSET_BITS 64
31
32 #include "config.h"
33
34 #include <ctype.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <mLib/dstr.h>
40 #include <mLib/str.h>
41
42 #include <mLib/hex.h>
43 #include <mLib/base32.h>
44 #include <mLib/base64.h>
45
46 #include "ghash.h"
47 #include "cc.h"
48
49 /*----- Encoding and decoding ---------------------------------------------*/
50
51 /* --- Hex encoding --- */
52
53 static void puthex(const octet *buf, size_t sz, FILE *fp)
54 {
55 while (sz) {
56 fprintf(fp, "%02x", *buf++);
57 sz--;
58 }
59 }
60
61 static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
62 {
63 size_t i = 0;
64 while (sz > 0 &&
65 isxdigit((unsigned char)p[0]) &&
66 isxdigit((unsigned char)p[1])) {
67 char buf[3];
68 buf[0] = p[0];
69 buf[1] = p[1];
70 buf[2] = 0;
71 *q++ = strtoul(buf, 0, 16);
72 sz--;
73 p += 2;
74 i++;
75 }
76 if (pp)
77 *pp = (char *)p;
78 return (i);
79 }
80
81 /* --- Base64 encoding --- */
82
83 static void putbase64(const octet *buf, size_t sz, FILE *fp)
84 {
85 base64_ctx b;
86 dstr d = DSTR_INIT;
87
88 base64_init(&b);
89 b.indent = "";
90 b.maxline = 0;
91 base64_encode(&b, buf, sz, &d);
92 base64_encode(&b, 0, 0, &d);
93 dstr_write(&d, fp);
94 dstr_destroy(&d);
95 }
96
97 static size_t getbase64(const char *p, octet *q, size_t sz, char **pp)
98 {
99 base64_ctx b;
100 dstr d = DSTR_INIT;
101 size_t n = strlen(p);
102
103 base64_init(&b);
104 base64_decode(&b, p, n, &d);
105 if (pp) *pp = (/*unconst*/ char *)p + n;
106 base64_decode(&b, 0, 0, &d);
107 assert(d.len <= sz);
108 memcpy(q, d.buf, sz);
109 n = d.len;
110 dstr_destroy(&d);
111 return (n);
112 }
113
114 /* --- Base32 encoding --- */
115
116 static void putbase32(const octet *buf, size_t sz, FILE *fp)
117 {
118 base32_ctx b;
119 dstr d = DSTR_INIT;
120
121 base32_init(&b);
122 b.indent = "";
123 b.maxline = 0;
124 base32_encode(&b, buf, sz, &d);
125 base32_encode(&b, 0, 0, &d);
126 dstr_write(&d, fp);
127 dstr_destroy(&d);
128 }
129
130 static size_t getbase32(const char *p, octet *q, size_t sz, char **pp)
131 {
132 base32_ctx b;
133 dstr d = DSTR_INIT;
134 size_t n = strlen(p);
135
136 base32_init(&b);
137 base32_decode(&b, p, n, &d);
138 if (pp) *pp = (/*unconst*/ char *)p + n;
139 base32_decode(&b, 0, 0, &d);
140 assert(d.len <= sz);
141 memcpy(q, d.buf, sz);
142 n = d.len;
143 dstr_destroy(&d);
144 return (n);
145 }
146
147 /* --- Table --- */
148
149 const encodeops encodingtab[] = {
150 #define TAB(tag, name) { #name, put##name, get##name },
151 ENCODINGS(TAB)
152 #undef TAB
153 { 0, 0, 0 }
154 };
155
156 const encodeops *getencoding(const char *ename)
157 {
158 const encodeops *e;
159
160 for (e = encodingtab; e->name; e++) {
161 if (strcmp(ename, e->name) == 0)
162 return (e);
163 }
164 return (0);
165 }
166
167 /*----- File hashing ------------------------------------------------------*/
168
169 /* --- @gethash@ --- *
170 *
171 * Arguments: @const char *name@ = pointer to name string
172 *
173 * Returns: Pointer to appropriate hash class.
174 *
175 * Use: Chooses a hash function by name.
176 */
177
178 const gchash *gethash(const char *name)
179 {
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) {
185 gg = *g;
186 break;
187 } else if (gg)
188 return (0);
189 else
190 gg = *g;
191 }
192 }
193 return (gg);
194 }
195
196 /* --- @fhash_init@ ---*
197 *
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
201 *
202 * Returns: ---
203 *
204 * Use: Initializes an @fhashstate@ structure.
205 */
206
207 void fhash_init(fhashstate *fh, const gchash *gch, unsigned f)
208 { fh->f = f; fh->gch = gch; }
209
210 /* --- @fhash_free@ --- *
211 *
212 * Arguments: @fhashstate *fh@ = pointer to fhash state to free
213 *
214 * Returns: ---
215 *
216 * Use: Frees an fhash state.
217 */
218
219 void fhash_free(fhashstate *fh) { return; }
220
221 /* --- @fhash@ --- *
222 *
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
226 *
227 * Returns: Zero if it worked, nonzero on error.
228 *
229 * Use: Hashes a file.
230 */
231
232 int fhash(fhashstate *fh, const char *file, void *buf)
233 {
234 FILE *fp;
235 char fbuf[1024 * 128];
236 size_t sz;
237 ghash *h;
238 int rc = 0;
239 fprogress ff;
240
241 if (!file || strcmp(file, "-") == 0)
242 fp = stdin;
243 else if ((fp = fopen(file, fh->f & FHF_BINARY ? "rb" : "r")) == 0)
244 return (-1);
245
246 if (fh->f & FHF_PROGRESS) {
247 if (fprogress_init(&ff, file, fp)) return (-1);
248 }
249
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);
254 }
255 if (ferror(fp)) rc = -1;
256 if (fp != stdin) fclose(fp);
257 if (fh->f & FHF_PROGRESS) fprogress_done(&ff);
258 GH_DONE(h, buf);
259 GH_DESTROY(h);
260 return (rc);
261 }
262
263 /* --- @hfparse@ --- *
264 *
265 * Arguments: @hfpctx *hfp@ = pointer to the context structure
266 *
267 * Returns: A code indicating what happened.
268 *
269 * Use: Parses a line from the input file.
270 */
271
272 int hfparse(hfpctx *hfp)
273 {
274 char *p, *q;
275 const gchash *gch;
276 const encodeops *ee;
277 dstr *d = hfp->dline;
278 size_t hsz;
279
280 /* --- Fetch the input line and get ready to parse --- */
281
282 DRESET(d);
283 if (dstr_putline(d, hfp->fp) == EOF) return (HF_EOF);
284 p = d->buf;
285
286 /* --- Parse magic comments --- */
287
288 if (*p == '#') {
289 p++;
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);
294 hfp->gch = gch;
295 return (HF_HASH);
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);
299 hfp->ee = ee;
300 return (HF_ENC);
301 } else if (strcmp(q, "escape") == 0) {
302 hfp->f |= HFF_ESCAPE;
303 return (HF_ESC);
304 }
305 return (HF_BAD);
306 }
307
308 /* --- Otherwise it's a file line --- */
309
310 q = p;
311 while (*p && *p != ' ') p++;
312 if (!*p) return (HF_BAD);
313 *p++ = 0;
314 hsz = hfp->gch->hashsz;
315 if (hfp->ee->get(q, hfp->hbuf, hsz, 0) < hsz) return (HF_BAD);
316 switch (*p) {
317 case '*': hfp->f |= FHF_BINARY; break;
318 case ' ': hfp->f &= ~FHF_BINARY; break;
319 default: return (HF_BAD);
320 }
321 p++;
322
323 DRESET(hfp->dfile);
324 if (hfp->f & HFF_ESCAPE)
325 getstring(&p, hfp->dfile, GSF_STRING);
326 else {
327 dstr_putm(hfp->dfile, p, d->len - (p - d->buf));
328 dstr_putz(hfp->dfile);
329 }
330
331 return (HF_FILE);
332 }
333
334 /*----- String I/O --------------------------------------------------------*/
335
336 /* --- @getstring@ --- *
337 *
338 * Arguments: @void *in@ = input source
339 * @dstr *d@ = destination string
340 * @unsigned f@ = input flags
341 *
342 * Returns: Zero if OK, nonzero on end-of-file.
343 *
344 * Use: Reads a filename (or something similar) from a stream.
345 */
346
347 static int nextch_file(void *in)
348 { FILE *fp = in; return (getc(fp)); }
349
350 static int nextch_string(void *in)
351 { const unsigned char **p = in; return (*(*p)++); }
352
353 int getstring(void *in, dstr *d, unsigned f)
354 {
355 int ch;
356 int eofch = (f & GSF_STRING) ? 0 : EOF;
357 int (*nextch)(void *) = (f & GSF_STRING) ? nextch_string : nextch_file;
358 int q = 0;
359
360 /* --- Raw: just read exactly what's written up to a null byte --- */
361
362 if (f & GSF_RAW) {
363 if ((ch = nextch(in)) == eofch)
364 return (EOF);
365 for (;;) {
366 if (!ch)
367 break;
368 DPUTC(d, ch);
369 if ((ch = nextch(in)) == eofch)
370 break;
371 }
372 DPUTZ(d);
373 return (0);
374 }
375
376 /* --- Skip as far as whitespace --- *
377 *
378 * Also skip past comments.
379 */
380
381 again:
382 ch = nextch(in);
383 while (isspace(ch))
384 ch = nextch(in);
385 if (ch == '#') {
386 do ch = nextch(in); while (ch != '\n' && ch != eofch);
387 goto again;
388 }
389 if (ch == eofch)
390 return (EOF);
391
392 /* --- If the character is a quote then read a quoted string --- */
393
394 switch (ch) {
395 case '`':
396 ch = '\'';
397 case '\'':
398 case '\"':
399 q = ch;
400 ch = nextch(in);
401 break;
402 }
403
404 /* --- Now read all sorts of interesting things --- */
405
406 for (;;) {
407
408 /* --- Handle an escaped thing --- */
409
410 if (ch == '\\') {
411 ch = nextch(in);
412 if (ch == eofch)
413 break;
414 switch (ch) {
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;
422 }
423 DPUTC(d, ch);
424 ch = nextch(in);
425 continue;
426 }
427
428 /* --- If it's a quote or some other end marker then stop --- */
429
430 if (ch == q)
431 break;
432 if (!q && isspace(ch))
433 break;
434
435 /* --- Otherwise contribute and continue --- */
436
437 DPUTC(d, ch);
438 if ((ch = nextch(in)) == eofch)
439 break;
440 }
441
442 /* --- Done --- */
443
444 DPUTZ(d);
445 return (0);
446 }
447
448 /* --- @putstring@ --- *
449 *
450 * Arguments: @FILE *fp@ = stream to write on
451 * @const char *p@ = pointer to text
452 * @unsigned f@ = output flags
453 *
454 * Returns: ---
455 *
456 * Use: Emits a string to a stream.
457 */
458
459 void putstring(FILE *fp, const char *p, unsigned f)
460 {
461 size_t sz = strlen(p);
462 unsigned qq;
463 const char *q;
464
465 /* --- Just write the string null terminated if raw --- */
466
467 if (f & GSF_RAW) {
468 fwrite(p, 1, sz + 1, fp);
469 return;
470 }
471
472 /* --- Check for any dodgy characters --- */
473
474 qq = 0;
475 for (q = p; *q; q++) {
476 if (isspace((unsigned char)*q)) {
477 qq = '\"';
478 break;
479 }
480 }
481
482 if (qq)
483 putc(qq, fp);
484
485 /* --- Emit the string --- */
486
487 for (q = p; *q; q++) {
488 switch (*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;
499 default:
500 putc(*q, fp);
501 break;
502 }
503 }
504
505 /* --- Done --- */
506
507 if (qq)
508 putc(qq, fp);
509 }
510
511 /*----- That's all, folks -------------------------------------------------*/