annotate lib-src/mmencode.c @ 4940:9113c5044de8

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