xref: /illumos-gate/usr/src/cmd/sendmail/util/rfc2047.c (revision 955eb5e1)
1 /*
2  * rfc2047.c -- decode RFC-2047 header format
3  */
4 
5 #pragma ident	"%Z%%M%	%I%	%E% SMI"
6 
7 #ifndef lint
8 static char sccsi2[] = "%W% (Sun) %G%";
9 #endif
10 
11 /*
12  * Copyright (c) 1997-1998 Richard Coleman
13  * All rights reserved.
14  *
15  * Permission is hereby granted, without written agreement and without
16  * license or royalty fees, to use, copy, modify, and distribute this
17  * software and to distribute modified versions of this software for any
18  * purpose, provided that the above copyright notice and the following two
19  * paragraphs appear in all copies of this software.
20  *
21  * In no event shall Richard Coleman be liable to any party for direct,
22  * indirect, special, incidental, or consequential damages arising out of
23  * the use of this software and its documentation, even if Richard Coleman
24  * has been advised of the possibility of such damage.
25  *
26  * Richard Coleman specifically disclaims any warranties, including, but
27  * not limited to, the implied warranties of merchantability and fitness
28  * for a particular purpose.  The software provided hereunder is on an "as
29  * is" basis, and Richard Coleman has no obligation to provide maintenance,
30  * support, updates, enhancements, or modifications.
31  */
32 
33 /*
34  * Parts of this code were derived from metamail, which is ...
35  *
36  * Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
37  *
38  * Permission to use, copy, modify, and distribute this material
39  * for any purpose and without fee is hereby granted, provided
40  * that the above copyright notice and this permission notice
41  * appear in all copies, and that the name of Bellcore not be
42  * used in advertising or publicity pertaining to this
43  * material without the specific, prior written permission
44  * of an authorized representative of Bellcore.  BELLCORE
45  * MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
46  * OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS",
47  * WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
48  */
49 
50 /*
51  * Copyright (c) 1998, by Sun Microsystems, Inc.
52  * All rights reserved.
53  */
54 
55 #include <string.h>
56 
57 typedef int bool;
58 
59 #define	FALSE	0
60 #define	TRUE	1
61 
62 static signed char hexindex[] = {
63 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
64 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
65 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
66 	0,   1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1,
67 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
68 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
69 	-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
70 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
71 };
72 
73 static signed char index_64[128] = {
74 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
75 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
76 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
77 	52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
78 	-1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
79 	15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
80 	-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
81 	41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
82 };
83 
84 #define	char64(c) (((unsigned char) (c) > 127) ? -1 : \
85 	index_64[(unsigned char) (c)])
86 
87 static int
88 unqp(unsigned char byte1, unsigned char byte2)
89 {
90 	if (hexindex[byte1] == -1 || hexindex[byte2] == -1)
91 		return (-1);
92 	return (hexindex[byte1] << 4 | hexindex[byte2]);
93 }
94 
95 /* Check if character is linear whitespace */
96 #define	is_lws(c)  ((c) == ' ' || (c) == '\t' || (c) == '\n')
97 
98 /*
99  * Decode the string as a RFC-2047 header field
100  */
101 
102 bool
103 decode_rfc2047(char *str, char *dst, char *charset)
104 {
105 	char *p, *q, *pp;
106 	char *startofmime, *endofmime;
107 	int c, quoted_printable;
108 	bool encoding_found = FALSE;	/* did we decode anything?	  */
109 	bool between_encodings = FALSE;	/* are we between two encodings?  */
110 	bool equals_pending = FALSE;	/* is there a '=' pending?	  */
111 	int whitespace = 0;	/* how much whitespace between encodings? */
112 
113 	if (str == NULL)
114 		return (FALSE);
115 
116 	/*
117 	 * Do a quick and dirty check for the '=' character.
118 	 * This should quickly eliminate many cases.
119 	 */
120 	if (!strchr(str, '='))
121 		return (FALSE);
122 
123 	for (p = str, q = dst; *p; p++) {
124 		/*
125 		 * If we had an '=' character pending from
126 		 * last iteration, then add it first.
127 		 */
128 		if (equals_pending) {
129 			*q++ = '=';
130 			equals_pending = FALSE;
131 			between_encodings = FALSE; /* we added non-WS text */
132 		}
133 
134 		if (*p != '=') {
135 			/* count linear whitespace while between encodings */
136 			if (between_encodings && is_lws(*p))
137 				whitespace++;
138 			else
139 				between_encodings = FALSE; /* non-WS added */
140 			*q++ = *p;
141 			continue;
142 		}
143 
144 		equals_pending = TRUE;	/* we have a '=' pending */
145 
146 		/* Check for initial =? */
147 		if (*p == '=' && p[1] && p[1] == '?' && p[2]) {
148 			startofmime = p + 2;
149 
150 			/* Scan ahead for the next '?' character */
151 			for (pp = startofmime; *pp && *pp != '?'; pp++)
152 				;
153 
154 			if (!*pp)
155 				continue;
156 
157 			strncpy(charset, startofmime, pp - startofmime);
158 			charset[pp - startofmime] = '\0';
159 
160 			startofmime = pp + 1;
161 
162 			/* Check for valid encoding type */
163 			if (*startofmime != 'B' && *startofmime != 'b' &&
164 			    *startofmime != 'Q' && *startofmime != 'q')
165 				continue;
166 
167 			/* Is encoding quoted printable or base64? */
168 			quoted_printable = (*startofmime == 'Q' ||
169 					    *startofmime == 'q');
170 			startofmime++;
171 
172 			/* Check for next '?' character */
173 			if (*startofmime != '?')
174 				continue;
175 			startofmime++;
176 
177 			/*
178 			 * Scan ahead for the ending ?=
179 			 *
180 			 * While doing this, we will also check if encoded
181 			 * word has any embedded linear whitespace.
182 			 */
183 			endofmime = NULL;
184 			for (pp = startofmime; *pp && *(pp+1); pp++) {
185 				if (is_lws(*pp))
186 					break;
187 				else if (*pp == '?' && pp[1] == '=') {
188 					endofmime = pp;
189 					break;
190 				}
191 			}
192 			if (is_lws(*pp) || endofmime == NULL)
193 				continue;
194 
195 			/*
196 			 * We've found an encoded word, so we can drop
197 			 * the '=' that was pending
198 			 */
199 			equals_pending = FALSE;
200 
201 			/*
202 			 * If we are between two encoded words separated only
203 			 * by linear whitespace, then we ignore the whitespace.
204 			 * We will roll back the buffer the number of whitespace
205 			 * characters we've seen since last encoded word.
206 			 */
207 			if (between_encodings)
208 				q -= whitespace;
209 
210 			/* Now decode the text */
211 			if (quoted_printable) {
212 				for (pp = startofmime; pp < endofmime; pp++) {
213 					if (*pp == '=') {
214 						c = unqp(pp[1], pp[2]);
215 						if (c == -1)
216 							continue;
217 						if (c != 0)
218 							*q++ = c;
219 						pp += 2;
220 					} else if (*pp == '_')
221 						*q++ = ' ';
222 					else
223 						*q++ = *pp;
224 				}
225 			} else {
226 				/* base64 */
227 				int c1, c2, c3, c4;
228 
229 				pp = startofmime;
230 				while (pp < endofmime) {
231 					/* 6 + 2 bits */
232 					while ((pp < endofmime) &&
233 						((c1 = char64(*pp)) == -1)) {
234 						pp++;
235 					}
236 					if (pp < endofmime)
237 						pp++;
238 					while ((pp < endofmime) &&
239 						((c2 = char64(*pp)) == -1)) {
240 						pp++;
241 					}
242 					if (pp < endofmime && c1 != -1 &&
243 								c2 != -1) {
244 						*q++ = (c1 << 2) | (c2 >> 4);
245 						pp++;
246 					}
247 					/* 4 + 4 bits */
248 					while ((pp < endofmime) &&
249 						((c3 = char64(*pp)) == -1)) {
250 						pp++;
251 					}
252 					if (pp < endofmime && c2 != -1 &&
253 								c3 != -1) {
254 						*q++ = ((c2 & 0xF) << 4) |
255 								(c3 >> 2);
256 						pp++;
257 					}
258 					/* 2 + 6 bits */
259 					while ((pp < endofmime) &&
260 						((c4 = char64(*pp)) == -1)) {
261 						pp++;
262 					}
263 					if (pp < endofmime && c3 != -1 &&
264 								c4 != -1) {
265 						*q++ = ((c3 & 0x3) << 6) | (c4);
266 						pp++;
267 					}
268 				}
269 			}
270 
271 			/*
272 			 * Now that we are done decoding this particular
273 			 * encoded word, advance string to trailing '='.
274 			 */
275 			p = endofmime + 1;
276 
277 			encoding_found = TRUE;	 /* found (>= 1) encoded word */
278 			between_encodings = TRUE; /* just decoded something   */
279 			whitespace = 0; /* re-initialize amount of whitespace */
280 		}
281 	}
282 
283 	/* If an equals was pending at end of string, add it now. */
284 	if (equals_pending)
285 		*q++ = '=';
286 	*q = '\0';
287 
288 	return (encoding_found);
289 }
290