14
|
1 /*
|
|
2 Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
|
|
3
|
|
4 Permission to use, copy, modify, and distribute this material
|
|
5 for any purpose and without fee is hereby granted, provided
|
|
6 that the above copyright notice and this permission notice
|
|
7 appear in all copies, and that the name of Bellcore not be
|
|
8 used in advertising or publicity pertaining to this
|
|
9 material without the specific, prior written permission
|
|
10 of an authorized representative of Bellcore. BELLCORE
|
|
11 MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
|
|
12 OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
|
|
13 WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
|
|
14 */
|
|
15
|
|
16 #define NEWLINE_CHAR '\n'
|
|
17 #include <stdlib.h>
|
|
18 #include <stdio.h>
|
|
19 #include <ctype.h>
|
|
20 #include <string.h>
|
357
|
21 #include <errno.h>
|
14
|
22
|
|
23 static void
|
|
24 output64chunk(int c1, int c2, int c3, int pads, FILE *outfile);
|
|
25
|
163
|
26 static signed char basis_64[] =
|
14
|
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
28
|
163
|
29 static signed char index_64[128] = {
|
14
|
30 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
31 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
32 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
|
|
33 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
|
|
34 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
|
|
35 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
|
|
36 -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
|
|
37 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
|
|
38 };
|
|
39
|
|
40 #define char64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
|
|
41
|
|
42 /*
|
|
43 char64(c)
|
|
44 char c;
|
|
45 {
|
74
|
46 char *s = (char *) strchr(basis_64, c);
|
14
|
47 if (s) return(s-basis_64);
|
|
48 return(-1);
|
|
49 }
|
|
50 */
|
|
51
|
|
52 /* the following gets a character, but fakes it properly into two chars if there's a newline character */
|
|
53 static int InNewline=0;
|
|
54
|
|
55 static int
|
|
56 nextcharin(infile, PortableNewlines)
|
|
57 FILE *infile;
|
|
58 int PortableNewlines;
|
|
59 {
|
|
60 int c;
|
|
61
|
|
62 #ifndef NEWLINE_CHAR
|
|
63 return(getc(infile));
|
|
64 #else
|
|
65 if (!PortableNewlines) return(getc(infile));
|
|
66 if (InNewline) {
|
|
67 InNewline = 0;
|
|
68 return(10); /* LF */
|
|
69 }
|
|
70 c = getc(infile);
|
|
71 if (c == NEWLINE_CHAR) {
|
|
72 InNewline = 1;
|
|
73 return(13); /* CR */
|
|
74 }
|
|
75 return(c);
|
|
76 #endif
|
|
77 }
|
|
78
|
|
79 static void
|
|
80 to64(FILE *infile, FILE *outfile, int PortableNewlines)
|
|
81 {
|
|
82 int c1, c2, c3, ct=0;
|
|
83 InNewline = 0; /* always reset it */
|
|
84 while ((c1 = nextcharin(infile, PortableNewlines)) != EOF) {
|
|
85 c2 = nextcharin(infile, PortableNewlines);
|
|
86 if (c2 == EOF) {
|
|
87 output64chunk(c1, 0, 0, 2, outfile);
|
|
88 } else {
|
|
89 c3 = nextcharin(infile, PortableNewlines);
|
|
90 if (c3 == EOF) {
|
|
91 output64chunk(c1, c2, 0, 1, outfile);
|
|
92 } else {
|
|
93 output64chunk(c1, c2, c3, 0, outfile);
|
|
94 }
|
|
95 }
|
|
96 ct += 4;
|
|
97 if (ct > 71) {
|
|
98 putc('\n', outfile);
|
|
99 ct = 0;
|
|
100 }
|
|
101 }
|
|
102 if (ct) putc('\n', outfile);
|
|
103 fflush(outfile);
|
|
104 }
|
|
105
|
|
106 static void
|
|
107 output64chunk(int c1, int c2, int c3, int pads, FILE *outfile)
|
|
108 {
|
|
109 putc(basis_64[c1>>2], outfile);
|
|
110 putc(basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
|
|
111 if (pads == 2) {
|
|
112 putc('=', outfile);
|
|
113 putc('=', outfile);
|
|
114 } else if (pads) {
|
|
115 putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
|
|
116 putc('=', outfile);
|
|
117 } else {
|
|
118 putc(basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
|
|
119 putc(basis_64[c3 & 0x3F], outfile);
|
|
120 }
|
|
121 }
|
|
122
|
|
123 static int
|
|
124 PendingBoundary(char *s, char **Boundaries, int *BoundaryCt)
|
|
125 {
|
|
126 int i, len;
|
|
127
|
|
128 if (s[0] != '-' || s[1] != '-') return(0);
|
|
129
|
|
130
|
|
131 for (i=0; i < *BoundaryCt; ++i) {
|
|
132 len = strlen(Boundaries[i]);
|
|
133 if (!strncmp(s, Boundaries[i], len)) {
|
|
134 if (s[len] == '-' && s[len+1] == '-') *BoundaryCt = i;
|
|
135 return(1);
|
|
136 }
|
|
137 }
|
|
138 return(0);
|
|
139 }
|
|
140
|
|
141 /* If we're in portable newline mode, we have to convert CRLF to the
|
|
142 local newline convention on output */
|
|
143
|
|
144 static int CRpending = 0;
|
|
145
|
|
146 #ifdef NEWLINE_CHAR
|
|
147 static void
|
|
148 almostputc(int c, FILE *outfile, int PortableNewlines)
|
|
149 {
|
|
150 if (CRpending) {
|
|
151 if (c == 10) {
|
|
152 putc(NEWLINE_CHAR, outfile);
|
|
153 CRpending = 0;
|
|
154 } else {
|
|
155 putc(13, outfile);
|
|
156 if (c != 13) {
|
|
157 putc(c, outfile);
|
|
158 CRpending = 0;
|
|
159 }
|
|
160 }
|
|
161 } else {
|
|
162 if (PortableNewlines && c == 13) {
|
|
163 CRpending = 1;
|
|
164 } else {
|
|
165 putc(c, outfile);
|
|
166 }
|
|
167 }
|
|
168 }
|
|
169 #else
|
|
170 static void
|
|
171 almostputc(int c, FILE *outfile, int PortableNewlines)
|
|
172 {
|
|
173 putc(c, outfile);
|
|
174 }
|
|
175 #endif
|
|
176
|
|
177 static void
|
|
178 from64(FILE *infile, FILE *outfile,
|
|
179 char **boundaries, int *boundaryct, int PortableNewlines)
|
|
180 {
|
|
181 int c1, c2, c3, c4;
|
|
182 int newline = 1, DataDone = 0;
|
|
183
|
|
184 /* always reinitialize */
|
|
185 CRpending = 0;
|
|
186 while ((c1 = getc(infile)) != EOF) {
|
|
187 if (isspace(c1)) {
|
|
188 if (c1 == '\n') {
|
|
189 newline = 1;
|
|
190 } else {
|
|
191 newline = 0;
|
|
192 }
|
|
193 continue;
|
|
194 }
|
|
195 if (newline && boundaries && c1 == '-') {
|
|
196 char Buf[200];
|
|
197 /* a dash is NOT base 64, so all bets are off if NOT a boundary */
|
|
198 ungetc(c1, infile);
|
|
199 fgets(Buf, sizeof(Buf), infile);
|
|
200 if (boundaries
|
|
201 && (Buf[0] == '-')
|
|
202 && (Buf[1] == '-')
|
|
203 && PendingBoundary(Buf, boundaries, boundaryct)) {
|
|
204 return;
|
|
205 }
|
|
206 fprintf(stderr, "Ignoring unrecognized boundary line: %s\n", Buf);
|
|
207 continue;
|
|
208 }
|
|
209 if (DataDone) continue;
|
|
210 newline = 0;
|
|
211 do {
|
|
212 c2 = getc(infile);
|
|
213 } while (c2 != EOF && isspace(c2));
|
|
214 do {
|
|
215 c3 = getc(infile);
|
|
216 } while (c3 != EOF && isspace(c3));
|
|
217 do {
|
|
218 c4 = getc(infile);
|
|
219 } while (c4 != EOF && isspace(c4));
|
|
220 if (c2 == EOF || c3 == EOF || c4 == EOF) {
|
|
221 fprintf(stderr, "Warning: base64 decoder saw premature EOF!\n");
|
|
222 return;
|
|
223 }
|
|
224 if (c1 == '=' || c2 == '=') {
|
|
225 DataDone=1;
|
|
226 continue;
|
|
227 }
|
|
228 c1 = char64(c1);
|
|
229 c2 = char64(c2);
|
|
230 almostputc(((c1<<2) | ((c2&0x30)>>4)), outfile, PortableNewlines);
|
|
231 if (c3 == '=') {
|
|
232 DataDone = 1;
|
|
233 } else {
|
|
234 c3 = char64(c3);
|
|
235 almostputc((((c2&0XF) << 4) | ((c3&0x3C) >> 2)), outfile, PortableNewlines);
|
|
236 if (c4 == '=') {
|
|
237 DataDone = 1;
|
|
238 } else {
|
|
239 c4 = char64(c4);
|
|
240 almostputc((((c3&0x03) <<6) | c4), outfile, PortableNewlines);
|
|
241 }
|
|
242 }
|
|
243 }
|
|
244 if (CRpending) putc(13, outfile); /* Don't drop a lone trailing char 13 */
|
|
245 }
|
|
246
|
163
|
247 static signed char basis_hex[] = "0123456789ABCDEF";
|
|
248 static signed char index_hex[128] = {
|
14
|
249 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
250 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
251 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
252 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
|
|
253 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
254 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
255 -1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
|
|
256 -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
|
|
257 };
|
|
258
|
|
259 /* The following version generated complaints on Solaris. */
|
|
260 /* #define hexchar(c) (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)]) */
|
|
261 /* Since we're no longer ever calling it with anything signed, this should work: */
|
|
262 #define hexchar(c) (((c) > 127) ? -1 : index_hex[(c)])
|
|
263
|
|
264 /*
|
|
265 hexchar(c)
|
|
266 char c;
|
|
267 {
|
|
268 char *s;
|
|
269 if (islower(c)) c = toupper(c);
|
74
|
270 s = (char *) strchr(basis_hex, c);
|
14
|
271 if (s) return(s-basis_hex);
|
|
272 return(-1);
|
|
273 }
|
|
274 */
|
|
275
|
|
276 static void
|
|
277 toqp(FILE *infile, FILE *outfile)
|
|
278 {
|
|
279 int c, ct=0, prevc=255;
|
|
280 while ((c = getc(infile)) != EOF) {
|
|
281 if ((c < 32 && (c != '\n' && c != '\t'))
|
|
282 || (c == '=')
|
|
283 || (c >= 127)
|
|
284 /* Following line is to avoid single periods alone on lines,
|
|
285 which messes up some dumb smtp implementations, sigh... */
|
|
286 || (ct == 0 && c == '.')) {
|
|
287 putc('=', outfile);
|
|
288 putc(basis_hex[c>>4], outfile);
|
|
289 putc(basis_hex[c&0xF], outfile);
|
|
290 ct += 3;
|
|
291 prevc = 'A'; /* close enough */
|
|
292 } else if (c == '\n') {
|
|
293 if (prevc == ' ' || prevc == '\t') {
|
|
294 putc('=', outfile); /* soft & hard lines */
|
|
295 putc(c, outfile);
|
|
296 }
|
|
297 putc(c, outfile);
|
|
298 ct = 0;
|
|
299 prevc = c;
|
|
300 } else {
|
|
301 if (c == 'F' && prevc == '\n') {
|
|
302 /* HORRIBLE but clever hack suggested by MTR for sendmail-avoidance */
|
|
303 c = getc(infile);
|
|
304 if (c == 'r') {
|
|
305 c = getc(infile);
|
|
306 if (c == 'o') {
|
|
307 c = getc(infile);
|
|
308 if (c == 'm') {
|
|
309 c = getc(infile);
|
|
310 if (c == ' ') {
|
|
311 /* This is the case we are looking for */
|
|
312 fputs("=46rom", outfile);
|
|
313 ct += 6;
|
|
314 } else {
|
|
315 fputs("From", outfile);
|
|
316 ct += 4;
|
|
317 }
|
|
318 } else {
|
|
319 fputs("Fro", outfile);
|
|
320 ct += 3;
|
|
321 }
|
|
322 } else {
|
|
323 fputs("Fr", outfile);
|
|
324 ct += 2;
|
|
325 }
|
|
326 } else {
|
|
327 putc('F', outfile);
|
|
328 ++ct;
|
|
329 }
|
|
330 ungetc(c, infile);
|
|
331 prevc = 'x'; /* close enough -- printable */
|
|
332 } else { /* END horrible hack */
|
|
333 putc(c, outfile);
|
|
334 ++ct;
|
|
335 prevc = c;
|
|
336 }
|
|
337 }
|
|
338 if (ct > 72) {
|
|
339 putc('=', outfile);
|
|
340 putc('\n', outfile);
|
|
341 ct = 0;
|
|
342 prevc = '\n';
|
|
343 }
|
|
344 }
|
|
345 if (ct) {
|
|
346 putc('=', outfile);
|
|
347 putc('\n', outfile);
|
|
348 }
|
|
349 }
|
|
350
|
|
351 static void
|
|
352 fromqp(FILE *infile, FILE *outfile, char **boundaries, int *boundaryct)
|
|
353 {
|
|
354 unsigned int c1, c2;
|
|
355 int sawnewline = 1, neednewline = 0;
|
|
356 /* The neednewline hack is necessary because the newline leading into
|
|
357 a multipart boundary is part of the boundary, not the data */
|
|
358
|
|
359 while ((c1 = getc(infile)) != EOF) {
|
|
360 if (sawnewline && boundaries && (c1 == '-')) {
|
|
361 char Buf[200];
|
|
362 unsigned char *s;
|
|
363
|
|
364 ungetc(c1, infile);
|
|
365 fgets(Buf, sizeof(Buf), infile);
|
|
366 if (boundaries
|
|
367 && (Buf[0] == '-')
|
|
368 && (Buf[1] == '-')
|
|
369 && PendingBoundary(Buf, boundaries, boundaryct)) {
|
|
370 return;
|
|
371 }
|
|
372 /* Not a boundary, now we must treat THIS line as q-p, sigh */
|
|
373 if (neednewline) {
|
|
374 putc('\n', outfile);
|
|
375 neednewline = 0;
|
|
376 }
|
|
377 for (s=(unsigned char *) Buf; *s; ++s) {
|
|
378 if (*s == '=') {
|
|
379 if (!*++s) break;
|
|
380 if (*s == '\n') {
|
|
381 /* ignore it */
|
|
382 sawnewline = 1;
|
|
383 } else {
|
|
384 c1 = hexchar(*s);
|
|
385 if (!*++s) break;
|
|
386 c2 = hexchar(*s);
|
|
387 putc(c1<<4 | c2, outfile);
|
|
388 }
|
|
389 } else {
|
|
390 #ifdef MSDOS
|
|
391 if (*s == '\n')
|
|
392 putc('\r', outfile); /* insert CR for binary-mode write */
|
|
393 #endif
|
|
394 putc(*s, outfile);
|
|
395 }
|
|
396 }
|
|
397 } else {
|
|
398 if (neednewline) {
|
|
399 putc('\n', outfile);
|
|
400 neednewline = 0;
|
|
401 }
|
|
402 if (c1 == '=') {
|
|
403 sawnewline = 0;
|
|
404 c1 = getc(infile);
|
|
405 if (c1 == '\n') {
|
|
406 /* ignore it */
|
|
407 sawnewline = 1;
|
|
408 } else {
|
|
409 c2 = getc(infile);
|
|
410 c1 = hexchar(c1);
|
|
411 c2 = hexchar(c2);
|
|
412 putc(c1<<4 | c2, outfile);
|
|
413 if (c2 == '\n') sawnewline = 1;
|
|
414 }
|
|
415 } else {
|
|
416 if (c1 == '\n') {
|
|
417 sawnewline = 1;
|
|
418 neednewline = 1;
|
|
419 } else {
|
|
420 sawnewline = 0;
|
|
421 putc(c1, outfile);
|
|
422 }
|
|
423 }
|
|
424 }
|
|
425 }
|
|
426 if (neednewline) {
|
|
427 putc('\n', outfile);
|
|
428 neednewline = 0;
|
|
429 }
|
|
430 }
|
|
431
|
|
432
|
|
433 /*
|
|
434 Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
|
|
435
|
|
436 Permission to use, copy, modify, and distribute this material
|
|
437 for any purpose and without fee is hereby granted, provided
|
|
438 that the above copyright notice and this permission notice
|
|
439 appear in all copies, and that the name of Bellcore not be
|
|
440 used in advertising or publicity pertaining to this
|
|
441 material without the specific, prior written permission
|
|
442 of an authorized representative of Bellcore. BELLCORE
|
|
443 MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
|
|
444 OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
|
|
445 WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
|
|
446 */
|
|
447 #ifdef MSDOS
|
|
448 #include <fcntl.h>
|
|
449 #endif
|
|
450
|
|
451 #define BASE64 1
|
|
452 #define QP 2 /* quoted-printable */
|
|
453
|
|
454 int main(int argc, char *argv[])
|
|
455 {
|
|
456 int encode = 1, which = BASE64, i, portablenewlines = 0;
|
|
457 FILE *fp = stdin;
|
|
458 FILE *fpo = stdout;
|
|
459
|
|
460 for (i=1; i<argc; ++i) {
|
|
461 if (argv[i][0] == '-') {
|
|
462 switch (argv[i][1]) {
|
|
463 case 'o':
|
|
464 if (++i >= argc) {
|
|
465 fprintf(stderr, "mimencode: -o requires a file name.\n");
|
|
466 exit(-1);
|
|
467 }
|
|
468 fpo = fopen(argv[i], "w");
|
|
469 if (!fpo) {
|
|
470 perror(argv[i]);
|
|
471 exit(-1);
|
|
472 }
|
|
473 break;
|
|
474 case 'u':
|
|
475 encode = 0;
|
|
476 break;
|
|
477 case 'q':
|
|
478 which = QP;
|
|
479 break;
|
|
480 case 'p':
|
|
481 portablenewlines = 1;
|
|
482 break;
|
|
483 case 'b':
|
|
484 which = BASE64;
|
|
485 break;
|
|
486 default:
|
|
487 fprintf(stderr,
|
|
488 "Usage: mmencode [-u] [-q] [-b] [-p] [-o outputfile] [file name]\n");
|
|
489 exit(-1);
|
|
490 }
|
|
491 } else {
|
|
492 #ifdef MSDOS
|
|
493 if (encode)
|
|
494 fp = fopen(argv[i], "rb");
|
|
495 else
|
|
496 {
|
|
497 fp = fopen(argv[i], "rt");
|
|
498 setmode(fileno(fpo), O_BINARY);
|
|
499 } /* else */
|
|
500 #else
|
|
501 fp = fopen(argv[i], "r");
|
|
502 #endif /* MSDOS */
|
|
503 if (!fp) {
|
|
504 perror(argv[i]);
|
|
505 exit(-1);
|
|
506 }
|
|
507 }
|
|
508 }
|
|
509 #ifdef MSDOS
|
|
510 if (fp == stdin) setmode(fileno(fp), O_BINARY);
|
|
511 #endif /* MSDOS */
|
|
512 if (which == BASE64) {
|
|
513 if (encode) {
|
|
514 to64(fp, fpo, portablenewlines);
|
|
515 } else {
|
|
516 from64(fp,fpo, (char **) NULL, (int *) 0, portablenewlines);
|
|
517 }
|
|
518 } else {
|
|
519 if (encode) toqp(fp, fpo); else fromqp(fp, fpo, NULL, 0);
|
|
520 }
|
|
521 return(0);
|
|
522 }
|
|
523
|