Debian Codegraph @

bin/mewencode.c

raw symbols SCIP
1 /*
2 * mewencode, mewdecode, and mewcat --- MIME encoding for Mew
3 *
4 * Author: Kazu Yamamoto <[email protected]>
5 * Created: Dec 8, 1994
6 *
7 * Code:
8 */
9
10 #include "mew.h"
11
12 private char version_message[] = "version 6.8 20180607 Kazu Yamamoto";
13
14 #ifdef HAVE_UNISTD_H
15 #include <sys/types.h>
16 #include <unistd.h>
17 #endif /* HAVE_UNISTD_H */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22
23 #ifdef HAVE_LIBZ
24 #include <zlib.h>
25 #endif
26
27 #define ENCODE_NAME "mewencode"
28 #define DECODE_NAME "mewdecode"
29 #define CAT_NAME "mewcat"
30
31 #define ENCODE 1
32 #define DECODE 2
33 #define CHECK8 8
34
35 #define LINELEN 70
36
37 #define BASE64 'b'
38 #define QP 'q'
39 #define GZIP64 'z'
40 #define UU 'u'
41
42 #define OOB -1
43 #define EOP -2
44 #define ELF -3
45 #define ECR -4
46
47 typedef unsigned char byte;
48
49 /****************************************************************
50 *
51 * prototype
52 *
53 */
54
55 private void usage(const char *);
56 private void help(const char *);
57 private void version(const char *);
58 private int read_text(byte *, unsigned int);
59 private int read_binary(byte *, unsigned int);
60 #ifdef HAVE_LIBZ
61 private int deflate_gzip(byte *, unsigned int);
62 #endif /* HAVE_LIBZ */
63 private int encode_base64(byte *, unsigned int);
64 private void write_base64(void);
65 private void base64_encode(int, int);
66 private void gzip64_encode(int, int);
67 private int read_base64(byte *, unsigned int);
68 private int decode_base64(byte *, unsigned int);
69 #ifdef HAVE_LIBZ
70 private int inflate_gzip(byte *, unsigned int);
71 #endif /* HAVE_LIBZ */
72 private void write_text(void);
73 private void write_binary(void);
74 private void base64_decode(int);
75 private void gzip64_decode(int);
76 private void quoted_printable_encode(int);
77 private int puthexchar(int, int);
78 private void quoted_printable_decode(void);
79 private void uu_decode(int);
80 private void check_8bit(void);
81
82 /****************************************************************
83 *
84 * long name convention for option
85 *
86 */
87
88 private void
89 usage(const char *progname)
90 {
91 fprintf(stderr, "Usage: %s [-e|-d|-8] [-b|-q|-g|-u] [-l length] [-t] [infile [outfile]]\n", progname);
92 }
93
94 private const char *
95 help_message[] = {
96 " -e Encoding <infile>",
97 " -d Decoding <infile> instead of encoding",
98 " Decoding is the default when called with the decoding",
99 " program name.",
100 " -b MIME base64 en/decoding.",
101 " -q MIME quoted-printable en/decoding.",
102 " -g MIME gzip64 en/decoding(not yet specified in RFC).",
103 " -z The same as -g.",
104 " -u Uudecoding.",
105 " -l Line length into which base64/quoted-printable/gzip64 ",
106 " encoding truncate. The default value is 70.",
107 " -t On base64/gzip64 encoding, local newline is treated as CRLF.",
108 " On base/gzip64 decoding,any newline is translated",
109 " into local newline.",
110 " Specify this option only when the input is a line",
111 " based object(e.g. Content-Type: is text/plain or",
112 " application/postscript).",
113 " -8 See if any 8bit characters are contained.",
114 " -h Display this help message.",
115 " -v Display the version.",
116 "",
117 "Default is Encoding, Base64, Length = 70, Binary.",
118 "If <infile> is \"-\", it means the standard input.",
119 "If <outfile> is \"-\", it means the standard output.",
120 "If <outfile> is given for uudecoding, an embedded file is used.",
121 NULL
122 };
123
124 private void
125 help(const char *progname)
126 {
127 const char **p = help_message;
128
129 fprintf(stderr, "Help: %s\n\n", progname);
130 fprintf(stderr, " MIME encoder/decoder.\n\n");
131 usage(progname);
132 while (*p) fprintf(stderr, "%s\n", *p++);
133 }
134
135 private void
136 version(const char *progname)
137 {
138 fprintf(stderr, "%s %s\n", progname, version_message);
139 }
140
141 /****************************************************************
142 *
143 * Base64 / Gzip64 encoder
144 *
145 */
146
147 struct {
148 int (*e_func1)(byte *, unsigned int);
149 int (*e_func2)(byte *, unsigned int);
150 int (*e_func3)(byte *, unsigned int);
151 unsigned int e_length;
152 } E_ctx;
153
154 #if HAVE_LIBZ
155 private byte e_buf1[BUFSIZ];
156 #endif /* HAVE_LIBZ */
157 private byte e_buf2[BUFSIZ];
158 private byte e_buf3[BUFSIZ];
159
160 private char
161 base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
162
163 private int
164 read_text(byte *p, unsigned int max)
165 {
166 static int done = NO;
167 static int cr_in = NO;
168 int c;
169 byte *start = p, *lim = p + max;
170
171 if (done) return 0;
172
173 while (p < lim) {
174 c = getchar();
175 if (cr_in == YES) {
176 cr_in = NO;
177 *p = LF;
178 switch (c) {
179 case EOF:
180 done = YES;
181 return p - start;
182 case LF:
183 break;
184 case CR:
185 default:
186 ungetc(c, stdin);
187 break;
188 }
189 } else {
190 switch (c) {
191 case EOF:
192 done = YES;
193 return p - start;
194 case LF:
195 *p = CR;
196 cr_in = YES;
197 ungetc(c, stdin);
198 break;
199 case CR:
200 *p = CR;
201 cr_in = YES;
202 break;
203 default:
204 *p = c;
205 break;
206 }
207 }
208 p++;
209 }
210 return p - start;
211 }
212
213 private int
214 read_binary(byte *p, unsigned int max)
215 {
216 return fread(p, sizeof(byte), max, stdin);
217 }
218
219 #ifdef HAVE_LIBZ
220 private int
221 deflate_gzip(byte *p, unsigned int max)
222 {
223 static int done = NO;
224 static int initialized = NO;
225 static int finalize = NO;
226 static uLong crc = 0;
227 static uLong total = 0;
228 static z_stream z;
229 int err, size, deflated = 0, old;
230 byte *q;
231
232 if (done == YES) return 0;
233
234 if (initialized == NO) {
235 initialized = YES;
236
237 crc = crc32(0L, Z_NULL, 0);
238 z.zalloc = (alloc_func)0;
239 z.zfree = (free_func)0;
240 z.opaque = (voidpf)0;
241 z.avail_in = 0;
242
243 err = deflateInit2(&z, Z_DEFAULT_COMPRESSION,
244 Z_DEFLATED, -MAX_WBITS,
245 8, Z_DEFAULT_STRATEGY);
246 if (err != Z_OK) warn_exit("deflateInit2 error.");
247 *p++ = 0x1f;
248 *p++ = 0x8b;
249 *p++ = Z_DEFLATED;
250 *p++ = 0; /* flag */
251 *p++ = 0; /* mtime */
252 *p++ = 0; /* mtime */
253 *p++ = 0; /* mtime */
254 *p++ = 0; /* mtime */
255 *p++ = 0; /* xf */
256 *p++ = 3; /* UNIX */
257 return 10;
258 }
259
260 z.next_out = p;
261 z.avail_out = max;
262
263 if (finalize == YES) goto finalize;
264
265 while (z.avail_out != 0) {
266 if (z.avail_in == 0) {
267 size = (*E_ctx.e_func1)(e_buf1, sizeof(e_buf1));
268 if (size == 0) {
269 finalize = YES;
270 if (deflated == 0) goto finalize;
271 return deflated;
272 }
273 z.next_in = e_buf1;
274 z.avail_in = size;
275 total += size;
276 crc = crc32(crc, e_buf1, (uInt)size);
277 }
278
279 old = z.avail_out;
280 err = deflate(&z, Z_SYNC_FLUSH);
281
282 if (err != Z_OK && err != Z_STREAM_END)
283 warn_exit("inflate error (%d).", err);
284
285 deflated = max - z.avail_out;
286
287 if (old == z.avail_out)
288 break;
289 }
290 return deflated;
291
292 finalize:
293 done = YES;
294
295 do {
296 err = deflate(&z, Z_FINISH);
297 } while (err != Z_STREAM_END);
298
299 if (deflateEnd(&z) != Z_OK)
300 warn_exit("deflateEnd error.");
301
302 deflated = max - z.avail_out;
303 q = z.next_out;
304 *q++ = (int)(crc & 0xff);
305 *q++ = (int)((crc >>= 8) & 0xff);
306 *q++ = (int)((crc >>= 8) & 0xff);
307 *q++ = (int)((crc >>= 8) & 0xff);
308 *q++ = (int)(total & 0xff);
309 *q++ = (int)((total >>= 8) & 0xff);
310 *q++ = (int)((total >>= 8) & 0xff);
311 *q++ = (int)((total >>= 8) & 0xff);
312 deflated += 8;
313 return deflated;
314 }
315 #endif /* HAVE_LIBZ */
316
317 private int
318 encode_base64(byte *p, unsigned int max)
319 {
320 static int done = NO;
321 static unsigned int avail = 0;
322 static byte *q;
323 unsigned int i, size, out = 0;
324 byte c1, c2, c3, *r, *lim = p + max;
325
326 if (done == YES) return 0;
327
328 while (p + 4 < lim) {
329 if (avail < 3) {
330 r = q;
331 q = e_buf2;
332 for (i = 0; i < avail; i++)
333 *q++ = *r++;
334 size = (*E_ctx.e_func2)(q, sizeof(e_buf2) - avail);
335 q = e_buf2;
336 avail += size;
337 if (size == 0) {
338 done = YES;
339 switch (avail) {
340 case 0:
341 return out;
342 case 1:
343 c1 = *q++;
344 *p++ = base64[c1 >> 2];
345 *p++ = base64[((c1 & 0x3) << 4)];
346 *p++ = EQ;
347 *p++ = EQ;
348 return out + 4;
349 case 2:
350 c1 = *q++;
351 c2 = *q++;
352 *p++ = base64[c1 >> 2];
353 *p++ = base64[((c1 & 0x3) << 4) |
354 ((c2 & 0xf0) >> 4)];
355 *p++ = base64[((c2 & 0xf) << 2)];
356 *p++ = EQ;
357 return out + 4;
358 }
359 }
360 }
361 if (avail >= 3) {
362 c1 = *q++;
363 c2 = *q++;
364 c3 = *q++;
365 *p++ = base64[c1 >> 2];
366 *p++ = base64[((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4)];
367 *p++ = base64[((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6)];
368 *p++ = base64[c3 & 0x3f];
369 avail -= 3;
370 out += 4;
371 }
372 }
373 return out;
374 }
375
376 private void
377 write_base64()
378 {
379 unsigned int length = E_ctx.e_length;
380 unsigned int size = 0, out = 0, gap;
381 byte *p;
382
383 while ((size = (*E_ctx.e_func3)(e_buf3, sizeof(e_buf3))) != 0) {
384 p = e_buf3;
385 if (out != 0) {
386 gap = length - out;
387 if (size < gap) {
388 fwrite(p, sizeof(byte), size, stdout);
389 out += size;
390 continue;
391 }
392 fwrite(p, sizeof(byte), gap, stdout);
393 p += gap;
394 size -= gap;
395 putchar(LF);
396 out = 0;
397 }
398
399 while (size >= length) {
400 fwrite(p, sizeof(byte), length, stdout);
401 p += length;
402 size -= length;
403 putchar(LF);
404 }
405
406 fwrite(p, sizeof(byte), size, stdout);
407 out = size;
408 }
409
410 if (out != 0) putchar(LF);
411 }
412
413 private void
414 base64_encode(int text, int length)
415 {
416 E_ctx.e_func1 = NULL;
417 if (text == YES)
418 E_ctx.e_func2 = read_text;
419 else
420 E_ctx.e_func2= read_binary;
421 E_ctx.e_func3 = encode_base64;
422 E_ctx.e_length = length;
423 write_base64();
424 }
425
426 private void
427 gzip64_encode(int text, int length)
428 {
429 #ifdef HAVE_LIBZ
430 if (text == YES)
431 E_ctx.e_func1 = read_text;
432 else
433 E_ctx.e_func1= read_binary;
434 E_ctx.e_func2 = deflate_gzip;
435 E_ctx.e_func3 = encode_base64;
436 E_ctx.e_length = length;
437 write_base64();
438 #else /* HAVE_LIBZ */
439 warn_exit("not linked to zlib.");
440 #endif /* HAVE_FORK */
441 }
442
443 /****************************************************************
444 *
445 * Base64 / Gzip64 decoder
446 *
447 */
448
449 struct {
450 int (*d_func1)(byte *, unsigned int);
451 int (*d_func2)(byte *, unsigned int);
452 int (*d_func3)(byte *, unsigned int);
453 } D_ctx;
454
455 private byte d_buf1[BUFSIZ];
456 #if HAVE_LIBZ
457 private byte d_buf2[BUFSIZ];
458 #endif /* HAVE_LIBZ */
459 private byte d_buf3[BUFSIZ];
460
461 private signed char
462 base256[] = {
463 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,ELF,OOB, OOB,ECR,OOB,OOB,
464
465 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
466 /* - / */
467 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB, 62, OOB,OOB,OOB, 63,
468 /* 0 1 2 3 4 5 6 7 8 9 = */
469 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,OOB,OOB, OOB,EOP,OOB,OOB,
470 /* A B C D E F G H I J K L M N O*/
471 OOB, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
472 /* P Q R S T U V W X Y Z */
473 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,OOB, OOB,OOB,OOB,OOB,
474 /* a b c d e f g h i j k l m n o*/
475 OOB, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
476 /* p q r s t u v w x y z */
477 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,OOB, OOB,OOB,OOB,OOB,
478 };
479
480 private int
481 read_base64(byte *p, unsigned int max)
482 {
483 static int done = NO, cnt = 0;
484 int c, d, out = 0, lf = 0, cr = 0;
485 byte *lim = p + max;
486
487 if (done == YES) return 0;
488
489 while (p < lim) {
490 c = getchar();
491 if (c == EOF) {
492 done = YES;
493 return out;
494 }
495 d = base256[c];
496 switch (d) {
497 case OOB:
498 warning("invalid base64 character.");
499 goto skiptail;
500 case EOP:
501 goto skiptail;
502 case ELF:
503 /* If cnt == 0, unnecessary null lines are
504 prepended. */
505 if (++lf >= 2 && cnt > 0) goto skiptail;
506 continue;
507 case ECR:
508 if (++cr >= 2 && cnt > 0) goto skiptail;
509 continue;
510 }
511 lf = cr = 0;
512 *p++ = d;
513 out++;
514 cnt++;
515 }
516 return out;
517 skiptail:
518 done = YES;
519 return out;
520 }
521
522 private int
523 decode_base64(byte *p, unsigned int max)
524 {
525 static int done = NO;
526 static unsigned int avail = 0;
527 static byte *q;
528 unsigned int i, size, out = 0;
529 byte c1, c2, c3, c4, *r, *lim = p + max;
530
531 if (done == YES) return 0;
532
533 while (p + 3 < lim) {
534 if (avail < 4) {
535 r = q;
536 q = d_buf1;
537 for (i = 0; i < avail; i++)
538 *q++ = *r++;
539 size = (*D_ctx.d_func1)(q, sizeof(d_buf1) - avail);
540 q = d_buf1;
541 avail += size;
542 if (size == 0) {
543 done = YES;
544 switch (avail) {
545 case 0:
546 return out;
547 case 1:
548 warning("invalid base64 length.");
549 return out; /* anyway */
550 case 2:
551 c1 = *q++;
552 c2 = *q++;
553 *p++ = (c1 << 2) | ((c2 & 0x30) >> 4);
554 return out + 1;
555 case 3:
556 c1 = *q++;
557 c2 = *q++;
558 c3 = *q++;
559 *p++ = (c1 << 2) | ((c2 & 0x30) >> 4);
560 *p++ = ((c2 & 0x0f) << 4) |
561 ((c3 & 0x3c) >> 2);
562 return out + 2;
563 }
564 }
565 }
566
567 if (avail >= 4) {
568 c1 = *q++;
569 c2 = *q++;
570 c3 = *q++;
571 c4 = *q++;
572 *p++ = (c1 << 2) | ((c2 & 0x30) >> 4);
573 *p++ = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
574 *p++ = ((c3 & 0x03) << 6) | c4;
575 avail -= 4;
576 out += 3;
577 }
578 }
579 return out;
580 }
581
582 #ifdef HAVE_LIBZ
583 private int
584 inflate_gzip(byte *p, unsigned int max)
585 {
586 static int done = NO;
587 static int initialized = NO;
588 static z_stream z;
589 int err, size, inflated = 0, old;
590
591 if (done == YES) return 0;
592
593 if (initialized == NO) {
594 byte flag = 0;
595 uInt len;
596 byte *q, *r;
597
598 initialized = YES;
599 z.zalloc = (alloc_func)0;
600 z.zfree = (free_func)0;
601 z.opaque = (voidpf)0;
602 err = inflateInit2(&z, -MAX_WBITS);
603 if (err != Z_OK) warn_exit("inflateInit2 error.");
604 size = (*D_ctx.d_func2)(d_buf2, sizeof(d_buf2));
605 q = d_buf2;
606 r = q + size;
607 if (*q++ != 0x1f) warn_exit("not gzip format (ID1).");
608 if (*q++ != 0x8b) warn_exit("not gzip format (ID2).");
609 if (*q++ != Z_DEFLATED) warn_exit("not gzip format (CM).");
610 flag = *q++;
611 q += 6; /* mtime, xf, os */
612
613 /*
614 * Optional header, probably omitted.
615 */
616 if (flag & 0x04) { /* xlen */
617 len = *q++;
618 len = len + (*q++ << 8);
619 q += len;
620 }
621 if (flag & 0x08) /* file name */
622 while (*q++ != NUL);
623
624 if (flag & 0x10) /* comment */
625 while (*q++ != NUL);
626
627 if (flag & 0x02) /* crc */
628 q += 2;
629
630 z.next_in = q;
631 z.avail_in = r - q;
632 }
633
634 z.next_out = p;
635 z.avail_out = max;
636
637 while (z.avail_out != 0) {
638 if (z.avail_in == 0) {
639 size = (*D_ctx.d_func2)(d_buf2, sizeof(d_buf2));
640 z.next_in = d_buf2;
641 z.avail_in = size;
642 }
643
644 old = z.avail_out;
645 err = inflate(&z, Z_SYNC_FLUSH);
646
647 if (err != Z_OK && err != Z_STREAM_END)
648 warn_exit("inflate error (%d).", err);
649
650 inflated = max - z.avail_out;
651
652 if (old == z.avail_out)
653 break;
654
655 if (err == Z_STREAM_END) {
656 done = YES;
657 /* 8 bytes (crc and isize) are left. */
658 if (inflateEnd(&z) != Z_OK)
659 warn_exit("inflateEnd error.");
660 break;
661 }
662 }
663
664 return inflated;
665 }
666 #endif /* HAVE_LIBZ */
667
668 private void
669 write_text()
670 {
671 int i, c, size, cr_out = NO;
672
673 while ((size = (*D_ctx.d_func3)(d_buf3, sizeof(d_buf3))) != 0) {
674 for (i = 0; i < size; i++) {
675 c = d_buf3[i];
676 if (cr_out == YES) {
677 cr_out = NO;
678 switch (c) {
679 case LF :
680 break;
681 case CR :
682 putchar(LF);
683 cr_out = YES;
684 break;
685 default:
686 putchar(c);
687 break;
688 }
689 continue;
690 }
691
692 switch (c) {
693 case CR :
694 putchar(LF);
695 cr_out = YES;
696 break;
697 default:
698 putchar(c);
699 break;
700 }
701 }
702 }
703 }
704
705 private void
706 write_binary()
707 {
708 unsigned int size;
709
710 while ((size = (*D_ctx.d_func3)(d_buf3, sizeof(d_buf3))) != 0)
711 fwrite(d_buf3, sizeof(byte), size, stdout);
712 }
713
714 private void
715 base64_decode(int text)
716 {
717 D_ctx.d_func1 = read_base64;
718 D_ctx.d_func2 = NULL;
719 D_ctx.d_func3 = decode_base64;
720 if (text == YES)
721 write_text();
722 else
723 write_binary();
724 }
725
726 private void
727 gzip64_decode(int text)
728 {
729 #ifdef HAVE_LIBZ
730 D_ctx.d_func1 = read_base64;
731 D_ctx.d_func2 = decode_base64;
732 D_ctx.d_func3 = inflate_gzip;
733 if (text == YES)
734 write_text();
735 else
736 write_binary();
737 #else /* HAVE_LIBZ */
738 warn_exit("not linked to zlib.");
739 #endif /* HAVE_LIBZ */
740 }
741 /****************************************************************
742 *
743 * Quoted_Printable
744 *
745 */
746
747 private char base16[] = "0123456789ABCDEF";
748 private char From[] = "\nFrom ";
749
750 #define softbreak() {putchar(EQ); putchar(LF);}
751
752 private void
753 quoted_printable_encode(int length)
754 {
755 int c, len = 0, sp = NO, lfdot = NO, Fromix = 1;
756
757 while ((c = getchar()) != EOF) {
758 if ((c == TAB) || (c == SP)) {
759 if (From[Fromix] == c) { /* SP */
760 putchar(EQ);
761 putchar(base16[c >> 4]);
762 putchar(base16[c & 0x0f]);
763 len += 3;
764 Fromix = 0;
765 continue;
766 }
767 Fromix = 0;
768 sp = YES;
769 putchar(c);
770 if ((++len) > length) {
771 sp = NO;
772 len = 0;
773 lfdot = LF;
774 Fromix = 1;
775 softbreak();
776 }
777 continue;
778 }
779 if (c == LF) {
780 if (sp || (lfdot == DOT))
781 softbreak();
782 len = 0;
783 sp = NO;
784 lfdot = LF;
785 Fromix = 1;
786 putchar(LF);
787 continue;
788 }
789 if ((c < SP) || (c == EQ) || (c >= DEL)) {
790 /* exclusive TAB, SP */
791 sp = NO;
792 putchar(EQ);
793 putchar(base16[c >> 4]);
794 putchar(base16[c & 0x0f]);
795 len += 3;
796 if (len > length) {
797 len = 0;
798 lfdot = LF;
799 Fromix = 1;
800 softbreak();
801 } else {
802 Fromix = 0;
803 lfdot = NO;
804 }
805 continue;
806 }
807 sp = NO;
808
809 if (From[Fromix] == c)
810 Fromix++;
811 else
812 Fromix = 0;
813
814 if (c == DOT && lfdot == LF)
815 lfdot = DOT;
816 else
817 lfdot = NO;
818
819 putchar(c);
820 if ((++len) > length) {
821 len = 0;
822 lfdot = LF;
823 Fromix = 1;
824 softbreak();
825 }
826 }
827 if (len > 0) softbreak(); /* ignored by decoder */
828 }
829
830 private signed char
831 base128[] = {
832 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
833 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
834 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
835 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,OOB,OOB, OOB,OOB,OOB,OOB,
836 OOB, 10, 11, 12, 13, 14, 15,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
837 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
838 OOB, 10, 11, 12, 13, 14, 15,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
839 OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB, OOB,OOB,OOB,OOB,
840 };
841
842 private int
843 puthexchar(int c1, int c2)
844 {
845 int a1, a2;
846 static int warned = NO;
847
848 if (((a1 = base128[c1]) != OOB) && ((a2 = base128[c2]) != OOB)) {
849 putchar(((a1 << 4) | a2));
850 return(TRUE);
851 } else {
852 if (warned == NO) { /* warn just once */
853 warning("can't translate hex to character: %c%c", c1, c2);
854 warned = YES;
855 }
856 return(FALSE);
857 }
858 }
859
860 private void
861 quoted_printable_decode()
862 {
863 int c1, c2, c3;
864
865 /* if an error occurs, print input sequence as it is, anyway, sigh */
866
867 while((c1 = getchar()) != EOF) {
868 skipgetc:
869 if (c1 == EQ) {
870 if ((c2 = getchar()) == EOF) {
871 putchar(EQ);
872 warn_exit("end of file after =.");
873 }
874 if (c2 == LF) continue;
875 if (c2 == CR) {
876 if ((c3 = getchar()) == EOF) break;
877 if (c3 == LF) {
878 continue;
879 } else {
880 ungetc(c3, stdin);
881 continue;
882 }
883
884 }
885 if ((c3 = getchar()) == EOF) {
886 putchar(EQ);
887 putchar(c2);
888 warn_exit("end of file after =.");
889 }
890 if (puthexchar(c2, c3) == FALSE) {
891 putchar(EQ);
892 if ((c1 = c2) == EOF)
893 warn_exit("invalid eof.");
894 ungetc(c3, stdin);
895 goto skipgetc;
896 } else
897 continue;
898 }
899 putchar(c1);
900 }
901 }
902
903 /****************************************************************
904 *
905 * uudecode
906 *
907 */
908
909 private char Uu_read_buf[BUFSIZ];
910 private char Uu_flnm_buf[BUFSIZ];
911
912 #define uuchar(c) (((c) - SP) & 0x3f)
913
914 private void
915 uu_decode(int use_file_name)
916 {
917 int n, c1, c2, c3, c4;
918 char *p, *q;
919
920 while (fgets(Uu_read_buf, sizeof(Uu_read_buf), stdin) != NULL) {
921 if (STRCMP(Uu_read_buf, "begin ") == 0) {
922 if (use_file_name == YES) {
923 if ((p = strchr(Uu_read_buf + 6, SP)) == NULL)
924 warn_exit("no filename for uudecode.");
925 p++;
926 for (q = Uu_flnm_buf; *p != LF; p++, q++)
927 *q = *p;
928 if (freopen(Uu_flnm_buf, FDWRITE, stdout) == NULL)
929 warn_exit("can't open a file.");
930 }
931 break;
932 }
933 }
934
935 while (fgets(Uu_read_buf, sizeof(Uu_read_buf), stdin) != NULL) {
936 if (STRCMP(Uu_read_buf, "end") == 0)
937 break;
938 p = Uu_read_buf;
939 n = uuchar(*p++);
940
941 if (((n + 2) / 3) != (strlen(Uu_read_buf) / 4))
942 continue;
943
944 while (n > 0) {
945 if (n >= 3) {
946 c1 = uuchar(*p++);
947 c2 = uuchar(*p++);
948 c3 = uuchar(*p++);
949 c4 = uuchar(*p++);
950 putchar(((c1 << 2) | ((c2 & 0x30) >> 4)));
951 putchar((((c2 & 0x0f) << 4) |
952 ((c3 & 0x3c) >> 2)));
953 putchar((((c3 & 0x03) << 6) | c4));
954 } else if (n == 2) {
955 c1 = uuchar(*p++);
956 c2 = uuchar(*p++);
957 c3 = uuchar(*p++);
958 putchar(((c1 << 2) | ((c2 & 0x30) >> 4)));
959 putchar((((c2 & 0x0f) << 4) |
960 ((c3 & 0x3c) >> 2)));
961 } else {
962 c1 = uuchar(*p++);
963 c2 = uuchar(*p++);
964 putchar(((c1 << 2) | ((c2 & 0x30) >> 4)));
965 }
966 n = n - 3;
967 }
968 }
969 if (use_file_name == YES)
970 fprintf(stderr, "filename: %s\n", Uu_flnm_buf);
971 }
972
973 /****************************************************************
974 *
975 * Checking 8bit
976 *
977 */
978
979 private void
980 check_8bit()
981 {
982 int c;
983 while ((c = getchar()) != EOF)
984 if (c & 0x80) {
985 printf("8bit\n");
986 exit(EXIT_FAILURE);
987 }
988 printf("7bit\n");
989 exit(EXIT_SUCCESS);
990 }
991
992 /****************************************************************
993 *
994 * main
995 *
996 */
997
998 int
999 main(int argc, char **argv)
1000 {
1001 int optc, file_count;
1002 /*
1003 * default option values
1004 */
1005 int action = ENCODE;
1006 char encode = BASE64; /* -b -q -g */
1007 int length = LINELEN; /* -l num */
1008 int text = NO; /* -t */
1009 int use_file_name = NO;
1010 char *progname = getprognm(argv[0]);
1011
1012 warn_prog = progname;
1013
1014 while((optc = Getopt(argc, argv, "esdbqgzl:tuhv8")) != EOF) {
1015 switch (optc) {
1016 case 'e':
1017 action = ENCODE;
1018 break;
1019 case 'd':
1020 action = DECODE;
1021 break;
1022 case 'b':
1023 encode = BASE64;
1024 break;
1025 case 'q':
1026 encode = QP;
1027 break;
1028 case 'g':
1029 case 'z':
1030 encode = GZIP64;
1031 break;
1032 case 'u':
1033 encode = UU;
1034 break;
1035 case '8':
1036 action = CHECK8;
1037 break;
1038 case 'l':
1039 length = atoi(Optarg);
1040 if (length == 0) length = LINELEN;
1041 break;
1042 case 't':
1043 text = YES;
1044 break;
1045 case 'h':
1046 help(progname);
1047 exit(EXIT_SUCCESS);
1048 break;
1049 case 'v':
1050 version(progname);
1051 exit(EXIT_SUCCESS);
1052 break;
1053 default:
1054 usage(progname);
1055 exit(EXIT_FAILURE);
1056 }
1057 }
1058
1059 file_count = argc - Optind;
1060
1061 /* Override arguments by progname. */
1062
1063 if (action == ENCODE) {
1064 if (STRCMP(progname, DECODE_NAME) == 0)
1065 action = DECODE;
1066 if (STRCMP(progname, CAT_NAME) == 0) {
1067 if (file_count != 0)
1068 warn_exit("too many arguments.");
1069 action = DECODE;
1070 }
1071 }
1072
1073 switch(file_count) {
1074 case 0:
1075 break;
1076 case 1:
1077 if (STRCMP(argv[Optind], "-") == 0)
1078 ;
1079 else if (freopen(argv[Optind], FDREAD, stdin) == NULL)
1080 warn_exit("can't open file %s.", argv[Optind]);
1081 break;
1082 case 2:
1083 if (STRCMP(argv[Optind], "-") == 0)
1084 ;
1085 else if (freopen(argv[Optind], FDREAD, stdin) == NULL)
1086 warn_exit("can't open file %s.", argv[Optind]);
1087 Optind++;
1088 if (STRCMP(argv[Optind], "-") == 0)
1089 ;
1090 else if (encode == UU)
1091 use_file_name = YES;
1092 else if (freopen(argv[Optind], FDWRITE, stdout) == NULL)
1093 warn_exit("can't open file %s.", argv[Optind]);
1094 break;
1095 default:
1096 usage(progname);
1097 exit(EXIT_FAILURE);
1098 break;
1099 }
1100
1101 SET_BINARY_MODE(stdin);
1102 SET_BINARY_MODE(stdout);
1103
1104 switch (action) {
1105 case ENCODE:
1106 switch (encode) {
1107 case BASE64:
1108 base64_encode(text, length);
1109 break;
1110 case QP:
1111 quoted_printable_encode(length);
1112 break;
1113 case GZIP64:
1114 gzip64_encode(text, length);
1115 break;
1116 }
1117 break;
1118 case DECODE:
1119 switch (encode) {
1120 case BASE64:
1121 base64_decode(text);
1122 break;
1123 case QP:
1124 quoted_printable_decode();
1125 break;
1126 case GZIP64:
1127 gzip64_decode(text);
1128 break;
1129 case UU:
1130 uu_decode(use_file_name);
1131 break;
1132 }
1133 break;
1134 case CHECK8:
1135 check_8bit();
1136 break;
1137 }
1138
1139 fclose(stdin);
1140 fclose(stdout);
1141 exit(EXIT_SUCCESS);
1142 }
1143
1144 /*
1145 * Copyright (C) 1994-2005 Mew developing team.
1146 * All rights reserved.
1147 *
1148 * Redistribution and use in source and binary forms, with or without
1149 * modification, are permitted provided that the following conditions
1150 * are met:
1151 *
1152 * 1. Redistributions of source code must retain the above copyright
1153 * notice, this list of conditions and the following disclaimer.
1154 * 2. Redistributions in binary form must reproduce the above copyright
1155 * notice, this list of conditions and the following disclaimer in the
1156 * documentation and/or other materials provided with the distribution.
1157 * 3. Neither the name of the team nor the names of its contributors
1158 * may be used to endorse or promote products derived from this software
1159 * without specific prior written permission.
1160 *
1161 * THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
1162 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1163 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1164 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
1165 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
1166 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
1167 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
1168 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
1169 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
1170 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
1171 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1172 */
1173
1174 /*
1175 * mewencode.c ends here
1176 */