3 * Catcrypt data encoding
5 * (c) 2004 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
35 #include <mLib/alloc.h>
36 #include <mLib/base64.h>
37 #include <mLib/dstr.h>
38 #include <mLib/macros.h>
39 #include <mLib/mdwopt.h>
40 #include <mLib/report.h>
45 typedef int encbdryp(const char *, void *);
47 /*----- Main code ---------------------------------------------------------*/
51 static enc *bin_encinit(FILE *fp, const char *msg)
52 { enc *e = CREATE(enc); return (e); }
53 static enc *bin_decinit(FILE *fp, encbdryp *func, void *p)
54 { enc *e = CREATE(enc); return (e); }
56 static int bin_read(enc *e, void *p, size_t sz)
61 n = fread(p, 1, sz, e->fp);
62 if (!n || ferror(e->fp)) return (-1);
66 static int bin_write(enc *e, const void *p, size_t sz)
67 { if (sz && fwrite(p, 1, sz, e->fp) < sz) return (-1); return (0); }
69 static int bin_done(enc *e) { return (0); }
71 static void bin_destroy(enc *e) { DESTROY(e); }
75 typedef struct pem_encctx {
86 static enc *pem_encinit(FILE *fp, const char *msg)
88 pem_encctx *pe = CREATE(pem_encctx);
90 fprintf(fp, "-----BEGIN %s-----\n", msg);
91 pe->msg = xstrdup(msg);
98 int checkbdry(const char *b, void *p) { return (!p || STRCMP(b, ==, p)); }
100 static enc *pem_decinit(FILE *fp, encbdryp *func, void *p)
107 /* --- Go until I find a newline and `-----' --- */
112 if ((ch = getc(fp)) == EOF) goto fail;
114 case '\n': d = 0; break;
115 case '-': if (d >= 0) { d++; if (d == 5) goto banner; }; break;
116 default: d = -1; break;
120 /* --- See what the banner looks like --- */
125 if ((ch = getc(fp)) == EOF) goto fail;
126 if (ch == '-') { d++; continue; }
127 if (ch == '\n') break;
128 if (i + d + 1 >= sizeof(buf)) goto top;
129 while (d) { buf[i++] = '-'; d--; }
134 /* --- Check we have the right framing --- */
136 if (d != 5) goto top;
137 if (STRNCMP(buf, !=, "BEGIN ", 6) || (func && !func(buf + 6, p)))
142 pe = CREATE(pem_encctx);
144 pe->msg = xstrdup(buf + 6);
153 die(EXIT_FAILURE, "initial encapsulation boundary not found");
157 #define PEM_CHUNKSZ 4096
159 static int pem_read(enc *e, void *p, size_t sz)
161 pem_encctx *pe = (pem_encctx *)e;
162 char buf[PEM_CHUNKSZ];
169 n = pe->d.len - pe->n;
171 memcpy(pp, pe->d.buf + pe->n, n);
177 if (pe->f & PEMF_EOF) return (rc ? rc : -1);
181 if ((ch = getc(pe->e.fp)) == EOF) return (-1);
182 if ((pe->f & PEMF_NL) && ch == '-') {
183 ungetc(ch, pe->e.fp);
187 if (ch == '\n') { pe->f |= PEMF_NL; continue; }
190 if (n >= PEM_CHUNKSZ) break;
193 base64_decode(&pe->b, buf, n, &pe->d);
194 if (pe->f & PEMF_EOF)
195 base64_decode(&pe->b, 0, 0, &pe->d);
201 static int pem_write(enc *e, const void *p, size_t sz)
203 pem_encctx *pe = (pem_encctx *)e;
211 base64_encode(&pe->b, pp, n, &pe->d);
212 if (fwrite(pe->d.buf, 1, pe->d.len, pe->e.fp) < pe->d.len)
220 static int pem_encdone(enc *e)
222 pem_encctx *pe = (pem_encctx *)e;
224 base64_encode(&pe->b, 0, 0, &pe->d);
225 if (fwrite(pe->d.buf, 1, pe->d.len, pe->e.fp) < pe->d.len)
227 if (pe->b.lnlen) fputc('\n', pe->e.fp);
228 fprintf(pe->e.fp, "-----END %s-----\n", pe->msg);
232 static int pem_decdone(enc *e)
234 pem_encctx *pe = (pem_encctx *)e;
239 for (d = 0; d < 5; d++)
240 if ((ch = getc(pe->e.fp)) != '-') goto fail;
243 if ((ch = getc(pe->e.fp)) == EOF) goto fail;
244 if (ch == '-') { d++; continue; }
245 if (ch == '\n') break;
246 if (i + d + 1 >= sizeof(buf)) goto fail;
247 while (d) { buf[i++] = '-'; d--; }
250 if (d != 5) goto fail;
252 if (STRNCMP(buf, !=, "END ", 4) || STRCMP(buf + 4, !=, pe->msg))
257 die(EXIT_FAILURE, "final encapsulation boundary not found");
261 static void pem_destroy(enc *e)
263 pem_encctx *pe = (pem_encctx *)e;
264 dstr_destroy(&pe->d);
269 /* --- Encoder table --- */
271 const encops enctab[] = {
272 { "binary", "rb", "wb", 1, 1,
273 bin_encinit, bin_decinit,
277 { "pem", "r", "w", 3, 4,
278 pem_encinit, pem_decinit,
280 pem_encdone, pem_decdone,
285 /* --- @getenc@ --- *
287 * Arguments: @const char *enc@ = name of wanted encoding
289 * Returns: Pointer to encoder operations.
291 * Use: Finds a named encoder or decoder.
294 const encops *getenc(const char *enc)
298 for (eo = enctab; eo->name; eo++) {
299 if (STRCMP(eo->name, ==, enc))
302 die(EXIT_FAILURE, "couldn't find encoding `%s'", enc);
307 /* --- @initenc@ --- *
309 * Arguments: @const encops *eo@ = operations (from @getenc@)
310 * @FILE *fp@ = file handle to attach
311 * @const char *msg@ = banner message
313 * Returns: The encoder object.
315 * Use: Initializes an encoder.
318 enc *initenc(const encops *eo, FILE *fp, const char *msg)
320 enc *e = eo->initenc(fp, msg);
326 /* --- @initdec@ --- *
328 * Arguments: @const encops *eo@ = operations (from @getenc@)
329 * @FILE *fp@ = file handle to attach
330 * @int (*func)(const char *, void *)@ = banner check function
331 * @void *p@ = argument for @func@
333 * Returns: The encoder object.
335 * Use: Initializes an encoder.
338 enc *initdec(const encops *eo, FILE *fp,
339 int (*func)(const char *, void *), void *p)
341 enc *e = eo->initdec(fp, func, p);
347 /* --- @freeenc@ --- *
349 * Arguments: @enc *e@ = encoder object
353 * Use: Frees an encoder object.
356 void freeenc(enc *e) { e->ops->destroy(e); }
358 /*----- Encoding and decoding commands ------------------------------------*/
360 int cmd_encode(int argc, char *argv[])
362 const char *fn, *of = 0;
365 const char *ef = "binary";
366 const char *bd = "MESSAGE";
376 #define f_progress 2u
379 static const struct option opt[] = {
380 { "format", OPTF_ARGREQ, 0, 'f' },
381 { "boundary", OPTF_ARGREQ, 0, 'b' },
382 { "output", OPTF_ARGREQ, 0, 'o' },
383 { "progress", 0, 0, 'p' },
386 i = mdwopt(argc, argv, "f:b:o:p", opt, 0, 0, 0);
389 case 'f': ef = optarg; break;
390 case 'b': bd = optarg; break;
391 case 'o': of = optarg; break;
392 case 'p': f |= f_progress; break;
393 default: f |= f_bogus; break;
396 if (argc - optind > 1 || (f & f_bogus))
397 die(EXIT_FAILURE, "Usage: encode [-OPTIONS] [FILE]");
399 if ((eo = getenc(ef)) == 0)
400 die(EXIT_FAILURE, "encoding `%s' not found", ef);
402 fn = optind < argc ? argv[optind++] : "-";
403 if (STRCMP(fn, ==, "-"))
405 else if ((fp = fopen(fn, "rb")) == 0) {
406 die(EXIT_FAILURE, "couldn't open file `%s': %s",
407 fn, strerror(errno));
410 if (!of || STRCMP(of, ==, "-"))
412 else if ((ofp = fopen(of, eo->wmode)) == 0) {
413 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
414 of, strerror(errno));
417 e = initenc(eo, ofp, bd);
419 if (f & f_progress) {
420 if (fprogress_init(&ff, fn, fp)) {
421 die(EXIT_FAILURE, "failed to initialize progress display: %s",
427 n = fread(buf, 1, sizeof(buf), fp);
428 if (f & f_progress) fprogress_update(&ff, n);
429 if (e->ops->write(e, buf, n)) {
430 if (f & f_progress) fprogress_done(&ff);
431 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
433 } while (n == sizeof(buf));
434 if (f & f_progress) fprogress_done(&ff);
443 int cmd_decode(int argc, char *argv[])
445 const char *fn, *of = 0;
448 const char *ef = "binary";
458 #define f_progress 2u
461 static const struct option opt[] = {
462 { "format", OPTF_ARGREQ, 0, 'f' },
463 { "boundary", OPTF_ARGREQ, 0, 'b' },
464 { "output", OPTF_ARGREQ, 0, 'o' },
465 { "progress", 0, 0, 'p' },
468 i = mdwopt(argc, argv, "f:b:o:p", opt, 0, 0, 0);
471 case 'f': ef = optarg; break;
472 case 'b': bd = optarg; break;
473 case 'o': of = optarg; break;
474 case 'p': f |= f_progress; break;
475 default: f |= f_bogus; break;
478 if (argc - optind > 1 || (f & f_bogus))
479 die(EXIT_FAILURE, "Usage: decode [-OPTIONS] [FILE]");
481 if ((eo = getenc(ef)) == 0)
482 die(EXIT_FAILURE, "encoding `%s' not found", ef);
484 fn = optind < argc ? argv[optind++] : "-";
485 if (STRCMP(fn, ==, "-"))
487 else if ((fp = fopen(fn, eo->rmode)) == 0) {
488 die(EXIT_FAILURE, "couldn't open file `%s': %s",
489 fn, strerror(errno));
492 if (!of || STRCMP(of, ==, "-"))
494 else if ((ofp = fopen(of, "wb")) == 0) {
495 die(EXIT_FAILURE, "couldn't open file `%s' for output: %s",
496 of, strerror(errno));
499 e = initdec(eo, fp, checkbdry, (/*unconst*/ void *)bd);
501 if (f & f_progress) {
502 if (fprogress_init(&ff, fn, fp)) {
503 die(EXIT_FAILURE, "failed to initialize progress display: %s",
509 if ((i = e->ops->read(e, buf, sizeof(buf))) < 0) {
510 if (f & f_progress) fprogress_done(&ff);
511 die(EXIT_FAILURE, "error reading input: %s", strerror(errno));
514 fprogress_update(&ff, i*e->ops->ncook/e->ops->nraw);
515 if (fwrite(buf, 1, i, ofp) < i) {
516 if (f & f_progress) fprogress_done(&ff);
517 die(EXIT_FAILURE, "error writing output: %s", strerror(errno));
519 } while (i == sizeof(buf));
521 if (f & f_progress) fprogress_done(&ff);
529 /*----- That's all, folks -------------------------------------------------*/