annotate lib-src/mmencode.c @ 388:aabb7f5b1c81 r21-2-9

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