annotate lib-src/mmencode.c @ 363:972bbb6d6ca2 r21-1-11

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