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