5 * Catcrypt data encoding
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
32 #define _FILE_OFFSET_BITS 64
37 #include <mLib/alloc.h>
38 #include <mLib/base64.h>
39 #include <mLib/dstr.h>
40 #include <mLib/mdwopt.h>
41 #include <mLib/report.h>
46 typedef int encbdryp(const char *, void *);
48 /*----- Main code ---------------------------------------------------------*/
52 static enc *bin_encinit(FILE *fp, const char *msg)
53 { enc *e = CREATE(enc); return (e); }
54 static enc *bin_decinit(FILE *fp, encbdryp *func, void *p)
55 { enc *e = CREATE(enc); return (e); }
57 static int bin_read(enc *e, void *p, size_t sz)
62 n = fread(p, 1, sz, e->fp);
63 if (!n || ferror(e->fp)) return (-1);
67 static int bin_write(enc *e, const void *p, size_t sz)
68 { if (sz && fwrite(p, 1, sz, e->fp) < sz) return (-1); return (0); }
70 static int bin_done(enc *e) { return (0); }
72 static void bin_destroy(enc *e) { DESTROY(e); }
76 typedef struct pem_encctx {
87 static enc *pem_encinit(FILE *fp, const char *msg)
89 pem_encctx *pe = CREATE(pem_encctx);
91 fprintf(fp, "-----BEGIN %s-----\n", msg);
92 pe->msg = xstrdup(msg);
99 int checkbdry(const char *b, void *p) { return (!p || strcmp(b, p) == 0); }
101 static enc *pem_decinit(FILE *fp, encbdryp *func, void *p)
108 /* --- Go until I find a newline and `-----' --- */
113 if ((ch = getc(fp)) == EOF) goto fail;
115 case '\n': d = 0; break;
116 case '-': if (d >= 0) { d++; if (d == 5) goto banner; }; break;
117 default: d = -1; break;
121 /* --- See what the banner looks like --- */
126 if ((ch = getc(fp)) == EOF) goto fail;
127 if (ch == '-') { d++; continue; }
128 if (ch == '\n') break;
129 if (i + d + 1 >= sizeof(buf)) goto top;
130 while (d) { buf[i++] = '-'; d--; }
135 /* --- Check we have the right framing --- */
137 if (d != 5) goto top;
138 if (strncmp(buf, "BEGIN ", 6) != 0 || (func && !func(buf + 6, p)))
143 pe = CREATE(pem_encctx);
145 pe->msg = xstrdup(buf + 6);
154 die(EXIT_FAILURE, "initial encapsulation boundary not found");
158 #define PEM_CHUNKSZ 4096
160 static int pem_read(enc *e, void *p, size_t sz)
162 pem_encctx *pe = (pem_encctx *)e;
163 char buf[PEM_CHUNKSZ];
170 n = pe->d.len - pe->n;
172 memcpy(pp, pe->d.buf + pe->n, n);
178 if (pe->f & PEMF_EOF) return (rc ? rc : -1);
182 if ((ch = getc(pe->e.fp)) == EOF) return (-1);
183 if ((pe->f & PEMF_NL) && ch == '-') {
184 ungetc(ch, pe->e.fp);
188 if (ch == '\n') { pe->f |= PEMF_NL; continue; }
191 if (n >= PEM_CHUNKSZ) break;
194 base64_decode(&pe->b, buf, n, &pe->d);
195 if (pe->f & PEMF_EOF)
196 base64_decode(&pe->b, 0, 0, &pe->d);
202 static int pem_write(enc *e, const void *p, size_t sz)
204 pem_encctx *pe = (pem_encctx *)e;
212 base64_encode(&pe->b, pp, n, &pe->d);
213 if (fwrite(pe->d.buf, 1, pe->d.len, pe->e.fp) < pe->d.len)
221 static int pem_encdone(enc *e)
223 pem_encctx *pe = (pem_encctx *)e;
225 base64_encode(&pe->b, 0, 0, &pe->d);
226 if (fwrite(pe->d.buf, 1, pe->d.len, pe->e.fp) < pe->d.len)
228 if (pe->b.lnlen) fputc('\n', pe->e.fp);
229 fprintf(pe->e.fp, "-----END %s-----\n", pe->msg);
233 static int pem_decdone(enc *e)
235 pem_encctx *pe = (pem_encctx *)e;
240 for (d = 0; d < 5; d++)
241 if ((ch = getc(pe->e.fp)) != '-') goto fail;
244 if ((ch = getc(pe->e.fp)) == EOF) goto fail;
245 if (ch == '-') { d++; continue; }
246 if (ch == '\n') break;
247 if (i + d + 1 >= sizeof(buf)) goto fail;
248 while (d) { buf[i++] = '-'; d--; }
251 if (d != 5) goto fail;
253 if (strncmp(buf, "END ", 4) != 0 || strcmp(buf + 4, pe->msg) != 0)
258 die(EXIT_FAILURE, "final encapsulation boundary not found");
262 static void pem_destroy(enc *e)
264 pem_encctx *pe = (pem_encctx *)e;
265 dstr_destroy(&pe->d);
270 /* --- Encoder table --- */
272 const encops enctab[] = {
273 { "binary", "rb", "wb", 1, 1,
274 bin_encinit, bin_decinit,
278 { "pem", "r", "w", 3, 4,
279 pem_encinit, pem_decinit,
281 pem_encdone, pem_decdone,
286 /* --- @getenc@ --- *
288 * Arguments: @const char *enc@ = name of wanted encoding
290 * Returns: Pointer to encoder operations.
292 * Use: Finds a named encoder or decoder.
295 const encops *getenc(const char *enc)
299 for (eo = enctab; eo->name; eo++) {
300 if (strcmp(eo->name, enc) == 0)
303 die(EXIT_FAILURE, "couldn't find encoding `%s'", enc);
308 /* --- @initenc@ --- *
310 * Arguments: @const encops *eo@ = operations (from @getenc@)
311 * @FILE *fp@ = file handle to attach
312 * @const char *msg@ = banner message
314 * Returns: The encoder object.
316 * Use: Initializes an encoder.
319 enc *initenc(const encops *eo, FILE *fp, const char *msg)
321 enc *e = eo->initenc(fp, msg);
327 /* --- @initdec@ --- *
329 * Arguments: @const encops *eo@ = operations (from @getenc@)
330 * @FILE *fp@ = file handle to attach
331 * @int (*func)(const char *, void *)@ = banner check function
332 * @void *p@ = argument for @func@
334 * Returns: The encoder object.
336 * Use: Initializes an encoder.
339 enc *initdec(const encops *eo, FILE *fp,
340 int (*func)(const char *, void *), void *p)
342 enc *e = eo->initdec(fp, func, p);
348 /* --- @freeenc@ --- *
350 * Arguments: @enc *e@ = encoder object
354 * Use: Frees an encoder object.
357 void freeenc(enc *e) { e->ops->destroy(e); }
359 /*----- Encoding and decoding commands ------------------------------------*/
361 int cmd_encode(int argc, char *argv[])
363 const char *fn, *of = 0;
366 const char *ef = "binary";
367 const char *bd = "MESSAGE";
377 #define f_progress 2u
380 static const struct option opt[] = {
381 { "format", OPTF_ARGREQ, 0, 'f' },
382 { "boundary", OPTF_ARGREQ, 0, 'b' },
383 { "output", OPTF_ARGREQ, 0, 'o' },
384 { "progress", 0, 0, 'p' },
387 i = mdwopt(argc, argv, "f:b:o:p", opt, 0, 0, 0);
390 case 'f': ef = optarg; break;
391 case 'b': bd = optarg; break;
392 case 'o': of = optarg; break;
393 case 'p': f |= f_progress; break;
394 default: f |= f_bogus; break;
397 if (argc - optind > 1 || (f & f_bogus))
398 die(EXIT_FAILURE, "Usage: encode [-OPTIONS] [FILE]");
400 if ((eo = getenc(ef)) == 0)
401 die(EXIT_FAILURE, "encoding `%s' not found", ef);
403 fn = optind < argc ? argv[optind++] : "-";
404 if (strcmp(fn, "-") == 0)
406 else if ((fp = fopen(fn, "rb")) == 0) {
407 die(EXIT_FAILURE, "couldn't open file `%s': %s",
408 fn, strerror(errno));
411 if (!of || strcmp(of, "-") == 0)
413 else if ((ofp = fopen(of, eo->wmode)) == 0) {
414 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
415 ofp, strerror(errno));
418 e = initenc(eo, ofp, bd);
420 if (f & f_progress) {
421 if (fprogress_init(&ff, fn, fp)) {
422 die(EXIT_FAILURE, "failed to initialize progress display: %s",
428 n = fread(buf, 1, sizeof(buf), fp);
429 if (f & f_progress) fprogress_update(&ff, n);
430 if (e->ops->write(e, buf, n)) {
431 if (f & f_progress) fprogress_done(&ff);
432 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
434 } while (n == sizeof(buf));
435 if (f & f_progress) fprogress_done(&ff);
444 int cmd_decode(int argc, char *argv[])
446 const char *fn, *of = 0;
449 const char *ef = "binary";
459 #define f_progress 2u
462 static const struct option opt[] = {
463 { "format", OPTF_ARGREQ, 0, 'f' },
464 { "boundary", OPTF_ARGREQ, 0, 'b' },
465 { "output", OPTF_ARGREQ, 0, 'o' },
466 { "progress", 0, 0, 'p' },
469 i = mdwopt(argc, argv, "f:b:o:p", opt, 0, 0, 0);
472 case 'f': ef = optarg; break;
473 case 'b': bd = optarg; break;
474 case 'o': of = optarg; break;
475 case 'p': f |= f_progress; break;
476 default: f |= f_bogus; break;
479 if (argc - optind > 1 || (f & f_bogus))
480 die(EXIT_FAILURE, "Usage: decode [-OPTIONS] [FILE]");
482 if ((eo = getenc(ef)) == 0)
483 die(EXIT_FAILURE, "encoding `%s' not found", ef);
485 fn = optind < argc ? argv[optind++] : "-";
486 if (strcmp(fn, "-") == 0)
488 else if ((fp = fopen(fn, eo->rmode)) == 0) {
489 die(EXIT_FAILURE, "couldn't open file `%s': %s",
490 fn, strerror(errno));
493 if (!of || strcmp(of, "-") == 0)
495 else if ((ofp = fopen(of, "wb")) == 0) {
496 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
497 ofp, strerror(errno));
500 e = initdec(eo, fp, checkbdry, (/*unconst*/ void *)bd);
502 if (f & f_progress) {
503 if (fprogress_init(&ff, fn, fp)) {
504 die(EXIT_FAILURE, "failed to initialize progress display: %s",
510 if ((i = e->ops->read(e, buf, sizeof(buf))) < 0) {
511 if (f & f_progress) fprogress_done(&ff);
512 die(EXIT_FAILURE, "error reading input: %s", strerror(errno));
515 fprogress_update(&ff, i*e->ops->ncook/e->ops->nraw);
516 if (fwrite(buf, 1, i, ofp) < i) {
517 if (f & f_progress) fprogress_done(&ff);
518 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
520 } while (i == sizeof(buf));
522 if (f & f_progress) fprogress_done(&ff);
530 /*----- That's all, folks -------------------------------------------------*/