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