annotate lib-src/mmencode.c @ 357:4711e16a8e49 r21-1-8

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