cc-hash.c: New file containing hash-related code from hashsum and dsig.
[u/mdw/catacomb] / cc-hash.c
CommitLineData
18b3351a
MW
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
41#include <mLib/hex.h>
42#include <mLib/base32.h>
43#include <mLib/base64.h>
44
45#include "ghash.h"
46#include "cc.h"
47
48/*----- Encoding and decoding ---------------------------------------------*/
49
50/* --- Hex encoding --- */
51
52static void puthex(const octet *buf, size_t sz, FILE *fp)
53{
54 while (sz) {
55 fprintf(fp, "%02x", *buf++);
56 sz--;
57 }
58}
59
60static size_t gethex(const char *p, octet *q, size_t sz, char **pp)
61{
62 size_t i = 0;
63 while (sz > 0 &&
64 isxdigit((unsigned char)p[0]) &&
65 isxdigit((unsigned char)p[1])) {
66 char buf[3];
67 buf[0] = p[0];
68 buf[1] = p[1];
69 buf[2] = 0;
70 *q++ = strtoul(buf, 0, 16);
71 sz--;
72 p += 2;
73 i++;
74 }
75 if (pp)
76 *pp = (char *)p;
77 return (i);
78}
79
80/* --- Base64 encoding --- */
81
82static void putbase64(const octet *buf, size_t sz, FILE *fp)
83{
84 base64_ctx b;
85 dstr d = DSTR_INIT;
86
87 base64_init(&b);
88 b.indent = "";
89 b.maxline = 0;
90 base64_encode(&b, buf, sz, &d);
91 base64_encode(&b, 0, 0, &d);
92 dstr_write(&d, fp);
93 dstr_destroy(&d);
94}
95
96static size_t getbase64(const char *p, octet *q, size_t sz, char **pp)
97{
98 base64_ctx b;
99 dstr d = DSTR_INIT;
100 size_t n = strlen(p);
101
102 base64_init(&b);
103 base64_decode(&b, p, n, &d);
104 if (pp) *pp = (/*unconst*/ char *)p + n;
105 base64_decode(&b, 0, 0, &d);
106 assert(d.len <= sz);
107 memcpy(q, d.buf, sz);
108 n = d.len;
109 dstr_destroy(&d);
110 return (n);
111}
112
113/* --- Base32 encoding --- */
114
115static void putbase32(const octet *buf, size_t sz, FILE *fp)
116{
117 base32_ctx b;
118 dstr d = DSTR_INIT;
119
120 base32_init(&b);
121 b.indent = "";
122 b.maxline = 0;
123 base32_encode(&b, buf, sz, &d);
124 base32_encode(&b, 0, 0, &d);
125 dstr_write(&d, fp);
126 dstr_destroy(&d);
127}
128
129static size_t getbase32(const char *p, octet *q, size_t sz, char **pp)
130{
131 base32_ctx b;
132 dstr d = DSTR_INIT;
133 size_t n = strlen(p);
134
135 base32_init(&b);
136 base32_decode(&b, p, n, &d);
137 if (pp) *pp = (/*unconst*/ char *)p + n;
138 base32_decode(&b, 0, 0, &d);
139 assert(d.len <= sz);
140 memcpy(q, d.buf, sz);
141 n = d.len;
142 dstr_destroy(&d);
143 return (n);
144}
145
146/* --- Table --- */
147
148const encodeops encodingtab[] = {
149#define TAB(tag, name) { #name, put##name, get##name },
150 ENCODINGS(TAB)
151#undef TAB
152 { 0, 0, 0 }
153};
154
155const encodeops *getencoding(const char *ename)
156{
157 const encodeops *e;
158
159 for (e = encodingtab; e->name; e++) {
160 if (strcmp(ename, e->name) == 0)
161 return (e);
162 }
163 return (0);
164}
165
166/*----- File hashing ------------------------------------------------------*/
167
168/* --- @fhash@ --- *
169 *
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
174 *
175 * Returns: Zero if it worked, nonzero on error.
176 *
177 * Use: Hashes a file.
178 */
179
180int fhash(const gchash *gch, unsigned f, const char *file, void *buf)
181{
182 FILE *fp;
183 char fbuf[1024 * 128];
184 size_t sz;
185 ghash *h;
186 int rc = 0;
187 fprogress ff;
188
189 if (!file || strcmp(file, "-") == 0)
190 fp = stdin;
191 else if ((fp = fopen(file, f & FHF_BINARY ? "rb" : "r")) == 0)
192 return (-1);
193
194 if (f & FHF_PROGRESS) {
195 if (fprogress_init(&ff, file, fp)) return (-1);
196 }
197
198 h = GH_INIT(gch);
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);
202 }
203 if (ferror(fp)) rc = -1;
204 if (fp != stdin) fclose(fp);
205 if (f & FHF_PROGRESS) fprogress_done(&ff);
206 GH_DONE(h, buf);
207 GH_DESTROY(h);
208 return (rc);
209}
210
211/*----- String I/O --------------------------------------------------------*/
212
213/* --- @getstring@ --- *
214 *
215 * Arguments: @void *in@ = input source
216 * @dstr *d@ = destination string
217 * @unsigned f@ = input flags
218 *
219 * Returns: Zero if OK, nonzero on end-of-file.
220 *
221 * Use: Reads a filename (or something similar) from a stream.
222 */
223
224static int nextch_file(void *in)
225 { FILE *fp = in; return (getc(fp)); }
226
227static int nextch_string(void *in)
228 { const unsigned char **p = in; return (*(*p)++); }
229
230int getstring(void *in, dstr *d, unsigned f)
231{
232 int ch;
233 int eofch = (f & GSF_STRING) ? 0 : EOF;
234 int (*nextch)(void *) = (f & GSF_STRING) ? nextch_string : nextch_file;
235 int q = 0;
236
237 /* --- Raw: just read exactly what's written up to a null byte --- */
238
239 if (f & GSF_RAW) {
240 if ((ch = nextch(in)) == eofch)
241 return (EOF);
242 for (;;) {
243 if (!ch)
244 break;
245 DPUTC(d, ch);
246 if ((ch = nextch(in)) == eofch)
247 break;
248 }
249 DPUTZ(d);
250 return (0);
251 }
252
253 /* --- Skip as far as whitespace --- *
254 *
255 * Also skip past comments.
256 */
257
258again:
259 ch = nextch(in);
260 while (isspace(ch))
261 ch = nextch(in);
262 if (ch == '#') {
263 do ch = nextch(in); while (ch != '\n' && ch != eofch);
264 goto again;
265 }
266 if (ch == eofch)
267 return (EOF);
268
269 /* --- If the character is a quote then read a quoted string --- */
270
271 switch (ch) {
272 case '`':
273 ch = '\'';
274 case '\'':
275 case '\"':
276 q = ch;
277 ch = nextch(in);
278 break;
279 }
280
281 /* --- Now read all sorts of interesting things --- */
282
283 for (;;) {
284
285 /* --- Handle an escaped thing --- */
286
287 if (ch == '\\') {
288 ch = nextch(in);
289 if (ch == eofch)
290 break;
291 switch (ch) {
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;
299 }
300 DPUTC(d, ch);
301 ch = nextch(in);
302 continue;
303 }
304
305 /* --- If it's a quote or some other end marker then stop --- */
306
307 if (ch == q)
308 break;
309 if (!q && isspace(ch))
310 break;
311
312 /* --- Otherwise contribute and continue --- */
313
314 DPUTC(d, ch);
315 if ((ch = nextch(in)) == eofch)
316 break;
317 }
318
319 /* --- Done --- */
320
321 DPUTZ(d);
322 return (0);
323}
324
325/* --- @putstring@ --- *
326 *
327 * Arguments: @FILE *fp@ = stream to write on
328 * @const char *p@ = pointer to text
329 * @unsigned f@ = output flags
330 *
331 * Returns: ---
332 *
333 * Use: Emits a string to a stream.
334 */
335
336void putstring(FILE *fp, const char *p, unsigned f)
337{
338 size_t sz = strlen(p);
339 unsigned qq;
340 const char *q;
341
342 /* --- Just write the string null terminated if raw --- */
343
344 if (f & GSF_RAW) {
345 fwrite(p, 1, sz + 1, fp);
346 return;
347 }
348
349 /* --- Check for any dodgy characters --- */
350
351 qq = 0;
352 for (q = p; *q; q++) {
353 if (isspace((unsigned char)*q)) {
354 qq = '\"';
355 break;
356 }
357 }
358
359 if (qq)
360 putc(qq, fp);
361
362 /* --- Emit the string --- */
363
364 for (q = p; *q; q++) {
365 switch (*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;
376 default:
377 putc(*q, fp);
378 break;
379 }
380 }
381
382 /* --- Done --- */
383
384 if (qq)
385 putc(qq, fp);
386}
387
388/*----- That's all, folks -------------------------------------------------*/