xref: /titanic_44/usr/src/cmd/sendmail/src/mime.c (revision 7800901e60d340b6af88e94a2149805dcfcaaf56)
17c478bd9Sstevel@tonic-gate /*
23ee0e492Sjbeck  * Copyright (c) 1998-2003, 2006 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *	All rights reserved.
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1994, 1996-1997 Eric P. Allman.  All rights reserved.
57c478bd9Sstevel@tonic-gate  * Copyright (c) 1994
67c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
97c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
107c478bd9Sstevel@tonic-gate  * the sendmail distribution.
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  */
137c478bd9Sstevel@tonic-gate 
147c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
157c478bd9Sstevel@tonic-gate 
167c478bd9Sstevel@tonic-gate #include <sendmail.h>
177c478bd9Sstevel@tonic-gate #include <string.h>
187c478bd9Sstevel@tonic-gate 
19*7800901eSjbeck SM_RCSID("@(#)$Id: mime.c,v 8.147 2007/09/26 23:29:11 ca Exp $")
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate **  MIME support.
237c478bd9Sstevel@tonic-gate **
247c478bd9Sstevel@tonic-gate **	I am indebted to John Beck of Hewlett-Packard, who contributed
257c478bd9Sstevel@tonic-gate **	his code to me for inclusion.  As it turns out, I did not use
267c478bd9Sstevel@tonic-gate **	his code since he used a "minimum change" approach that used
277c478bd9Sstevel@tonic-gate **	several temp files, and I wanted a "minimum impact" approach
287c478bd9Sstevel@tonic-gate **	that would avoid copying.  However, looking over his code
297c478bd9Sstevel@tonic-gate **	helped me cement my understanding of the problem.
307c478bd9Sstevel@tonic-gate **
317c478bd9Sstevel@tonic-gate **	I also looked at, but did not directly use, Nathaniel
327c478bd9Sstevel@tonic-gate **	Borenstein's "code.c" module.  Again, it functioned as
337c478bd9Sstevel@tonic-gate **	a file-to-file translator, which did not fit within my
347c478bd9Sstevel@tonic-gate **	design bounds, but it was a useful base for understanding
357c478bd9Sstevel@tonic-gate **	the problem.
367c478bd9Sstevel@tonic-gate */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /* use "old" mime 7 to 8 algorithm by default */
397c478bd9Sstevel@tonic-gate #ifndef MIME7TO8_OLD
407c478bd9Sstevel@tonic-gate # define MIME7TO8_OLD	1
417c478bd9Sstevel@tonic-gate #endif /* ! MIME7TO8_OLD */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #if MIME8TO7
447c478bd9Sstevel@tonic-gate static int	isboundary __P((char *, char **));
457c478bd9Sstevel@tonic-gate static int	mimeboundary __P((char *, char **));
467c478bd9Sstevel@tonic-gate static int	mime_getchar __P((SM_FILE_T *, char **, int *));
477c478bd9Sstevel@tonic-gate static int	mime_getchar_crlf __P((SM_FILE_T *, char **, int *));
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /* character set for hex and base64 encoding */
507c478bd9Sstevel@tonic-gate static char	Base16Code[] =	"0123456789ABCDEF";
517c478bd9Sstevel@tonic-gate static char	Base64Code[] =	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /* types of MIME boundaries */
547c478bd9Sstevel@tonic-gate # define MBT_SYNTAX	0	/* syntax error */
557c478bd9Sstevel@tonic-gate # define MBT_NOTSEP	1	/* not a boundary */
567c478bd9Sstevel@tonic-gate # define MBT_INTERMED	2	/* intermediate boundary (no trailing --) */
577c478bd9Sstevel@tonic-gate # define MBT_FINAL	3	/* final boundary (trailing -- included) */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate static char	*MimeBoundaryNames[] =
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	"SYNTAX",	"NOTSEP",	"INTERMED",	"FINAL"
627c478bd9Sstevel@tonic-gate };
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate static bool	MapNLtoCRLF;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate /*
677c478bd9Sstevel@tonic-gate **  MIME8TO7 -- output 8 bit body in 7 bit format
687c478bd9Sstevel@tonic-gate **
697c478bd9Sstevel@tonic-gate **	The header has already been output -- this has to do the
707c478bd9Sstevel@tonic-gate **	8 to 7 bit conversion.  It would be easy if we didn't have
717c478bd9Sstevel@tonic-gate **	to deal with nested formats (multipart/xxx and message/rfc822).
727c478bd9Sstevel@tonic-gate **
737c478bd9Sstevel@tonic-gate **	We won't be called if we don't have to do a conversion, and
747c478bd9Sstevel@tonic-gate **	appropriate MIME-Version: and Content-Type: fields have been
757c478bd9Sstevel@tonic-gate **	output.  Any Content-Transfer-Encoding: field has not been
767c478bd9Sstevel@tonic-gate **	output, and we can add it here.
777c478bd9Sstevel@tonic-gate **
787c478bd9Sstevel@tonic-gate **	Parameters:
797c478bd9Sstevel@tonic-gate **		mci -- mailer connection information.
807c478bd9Sstevel@tonic-gate **		header -- the header for this body part.
817c478bd9Sstevel@tonic-gate **		e -- envelope.
827c478bd9Sstevel@tonic-gate **		boundaries -- the currently pending message boundaries.
837c478bd9Sstevel@tonic-gate **			NULL if we are processing the outer portion.
847c478bd9Sstevel@tonic-gate **		flags -- to tweak processing.
853ee0e492Sjbeck **		level -- recursion level.
867c478bd9Sstevel@tonic-gate **
877c478bd9Sstevel@tonic-gate **	Returns:
887c478bd9Sstevel@tonic-gate **		An indicator of what terminated the message part:
897c478bd9Sstevel@tonic-gate **		  MBT_FINAL -- the final boundary
907c478bd9Sstevel@tonic-gate **		  MBT_INTERMED -- an intermediate boundary
917c478bd9Sstevel@tonic-gate **		  MBT_NOTSEP -- an end of file
92445f2479Sjbeck **		  SM_IO_EOF -- I/O error occurred
937c478bd9Sstevel@tonic-gate */
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate struct args
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	char	*a_field;	/* name of field */
987c478bd9Sstevel@tonic-gate 	char	*a_value;	/* value of that field */
997c478bd9Sstevel@tonic-gate };
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate int
mime8to7(mci,header,e,boundaries,flags,level)1023ee0e492Sjbeck mime8to7(mci, header, e, boundaries, flags, level)
1037c478bd9Sstevel@tonic-gate 	register MCI *mci;
1047c478bd9Sstevel@tonic-gate 	HDR *header;
1057c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
1067c478bd9Sstevel@tonic-gate 	char **boundaries;
1077c478bd9Sstevel@tonic-gate 	int flags;
1083ee0e492Sjbeck 	int level;
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	register char *p;
1117c478bd9Sstevel@tonic-gate 	int linelen;
1127c478bd9Sstevel@tonic-gate 	int bt;
1137c478bd9Sstevel@tonic-gate 	off_t offset;
1147c478bd9Sstevel@tonic-gate 	size_t sectionsize, sectionhighbits;
1157c478bd9Sstevel@tonic-gate 	int i;
1167c478bd9Sstevel@tonic-gate 	char *type;
1177c478bd9Sstevel@tonic-gate 	char *subtype;
1187c478bd9Sstevel@tonic-gate 	char *cte;
1197c478bd9Sstevel@tonic-gate 	char **pvp;
1207c478bd9Sstevel@tonic-gate 	int argc = 0;
1217c478bd9Sstevel@tonic-gate 	char *bp;
1227c478bd9Sstevel@tonic-gate 	bool use_qp = false;
1237c478bd9Sstevel@tonic-gate 	struct args argv[MAXMIMEARGS];
1247c478bd9Sstevel@tonic-gate 	char bbuf[128];
1257c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
1267c478bd9Sstevel@tonic-gate 	char pvpbuf[MAXLINE];
1277c478bd9Sstevel@tonic-gate 	extern unsigned char MimeTokenTab[256];
1287c478bd9Sstevel@tonic-gate 
1293ee0e492Sjbeck 	if (level > MAXMIMENESTING)
1303ee0e492Sjbeck 	{
1313ee0e492Sjbeck 		if (!bitset(EF_TOODEEP, e->e_flags))
1323ee0e492Sjbeck 		{
1333ee0e492Sjbeck 			if (tTd(43, 4))
1343ee0e492Sjbeck 				sm_dprintf("mime8to7: too deep, level=%d\n",
1353ee0e492Sjbeck 					   level);
1363ee0e492Sjbeck 			usrerr("mime8to7: recursion level %d exceeded",
1373ee0e492Sjbeck 				level);
1383ee0e492Sjbeck 			e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
1393ee0e492Sjbeck 		}
1403ee0e492Sjbeck 	}
1417c478bd9Sstevel@tonic-gate 	if (tTd(43, 1))
1427c478bd9Sstevel@tonic-gate 	{
1437c478bd9Sstevel@tonic-gate 		sm_dprintf("mime8to7: flags = %x, boundaries =", flags);
1447c478bd9Sstevel@tonic-gate 		if (boundaries[0] == NULL)
1457c478bd9Sstevel@tonic-gate 			sm_dprintf(" <none>");
1467c478bd9Sstevel@tonic-gate 		else
1477c478bd9Sstevel@tonic-gate 		{
1487c478bd9Sstevel@tonic-gate 			for (i = 0; boundaries[i] != NULL; i++)
1497c478bd9Sstevel@tonic-gate 				sm_dprintf(" %s", boundaries[i]);
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 		sm_dprintf("\n");
1527c478bd9Sstevel@tonic-gate 	}
1537c478bd9Sstevel@tonic-gate 	MapNLtoCRLF = true;
1547c478bd9Sstevel@tonic-gate 	p = hvalue("Content-Transfer-Encoding", header);
1557c478bd9Sstevel@tonic-gate 	if (p == NULL ||
156058561cbSjbeck 	    (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1577c478bd9Sstevel@tonic-gate 			   MimeTokenTab, false)) == NULL ||
1587c478bd9Sstevel@tonic-gate 	    pvp[0] == NULL)
1597c478bd9Sstevel@tonic-gate 	{
1607c478bd9Sstevel@tonic-gate 		cte = NULL;
1617c478bd9Sstevel@tonic-gate 	}
1627c478bd9Sstevel@tonic-gate 	else
1637c478bd9Sstevel@tonic-gate 	{
164058561cbSjbeck 		cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
1657c478bd9Sstevel@tonic-gate 		cte = sm_rpool_strdup_x(e->e_rpool, buf);
1667c478bd9Sstevel@tonic-gate 	}
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	type = subtype = NULL;
1697c478bd9Sstevel@tonic-gate 	p = hvalue("Content-Type", header);
1707c478bd9Sstevel@tonic-gate 	if (p == NULL)
1717c478bd9Sstevel@tonic-gate 	{
1727c478bd9Sstevel@tonic-gate 		if (bitset(M87F_DIGEST, flags))
1737c478bd9Sstevel@tonic-gate 			p = "message/rfc822";
1747c478bd9Sstevel@tonic-gate 		else
1757c478bd9Sstevel@tonic-gate 			p = "text/plain";
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate 	if (p != NULL &&
178058561cbSjbeck 	    (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
1797c478bd9Sstevel@tonic-gate 			   MimeTokenTab, false)) != NULL &&
1807c478bd9Sstevel@tonic-gate 	    pvp[0] != NULL)
1817c478bd9Sstevel@tonic-gate 	{
1827c478bd9Sstevel@tonic-gate 		if (tTd(43, 40))
1837c478bd9Sstevel@tonic-gate 		{
1847c478bd9Sstevel@tonic-gate 			for (i = 0; pvp[i] != NULL; i++)
1857c478bd9Sstevel@tonic-gate 				sm_dprintf("pvp[%d] = \"%s\"\n", i, pvp[i]);
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 		type = *pvp++;
1887c478bd9Sstevel@tonic-gate 		if (*pvp != NULL && strcmp(*pvp, "/") == 0 &&
1897c478bd9Sstevel@tonic-gate 		    *++pvp != NULL)
1907c478bd9Sstevel@tonic-gate 		{
1917c478bd9Sstevel@tonic-gate 			subtype = *pvp++;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		/* break out parameters */
1957c478bd9Sstevel@tonic-gate 		while (*pvp != NULL && argc < MAXMIMEARGS)
1967c478bd9Sstevel@tonic-gate 		{
1977c478bd9Sstevel@tonic-gate 			/* skip to semicolon separator */
1987c478bd9Sstevel@tonic-gate 			while (*pvp != NULL && strcmp(*pvp, ";") != 0)
1997c478bd9Sstevel@tonic-gate 				pvp++;
2007c478bd9Sstevel@tonic-gate 			if (*pvp++ == NULL || *pvp == NULL)
2017c478bd9Sstevel@tonic-gate 				break;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 			/* complain about empty values */
2047c478bd9Sstevel@tonic-gate 			if (strcmp(*pvp, ";") == 0)
2057c478bd9Sstevel@tonic-gate 			{
2067c478bd9Sstevel@tonic-gate 				usrerr("mime8to7: Empty parameter in Content-Type header");
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 				/* avoid bounce loops */
2097c478bd9Sstevel@tonic-gate 				e->e_flags |= EF_DONT_MIME;
2107c478bd9Sstevel@tonic-gate 				continue;
2117c478bd9Sstevel@tonic-gate 			}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate 			/* extract field name */
2147c478bd9Sstevel@tonic-gate 			argv[argc].a_field = *pvp++;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 			/* see if there is a value */
2177c478bd9Sstevel@tonic-gate 			if (*pvp != NULL && strcmp(*pvp, "=") == 0 &&
2187c478bd9Sstevel@tonic-gate 			    (*++pvp == NULL || strcmp(*pvp, ";") != 0))
2197c478bd9Sstevel@tonic-gate 			{
2207c478bd9Sstevel@tonic-gate 				argv[argc].a_value = *pvp;
2217c478bd9Sstevel@tonic-gate 				argc++;
2227c478bd9Sstevel@tonic-gate 			}
2237c478bd9Sstevel@tonic-gate 		}
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	/* check for disaster cases */
2277c478bd9Sstevel@tonic-gate 	if (type == NULL)
2287c478bd9Sstevel@tonic-gate 		type = "-none-";
2297c478bd9Sstevel@tonic-gate 	if (subtype == NULL)
2307c478bd9Sstevel@tonic-gate 		subtype = "-none-";
2317c478bd9Sstevel@tonic-gate 
2323ee0e492Sjbeck 	/* don't propagate some flags more than one level into the message */
2337c478bd9Sstevel@tonic-gate 	flags &= ~M87F_DIGEST;
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 	/*
2367c478bd9Sstevel@tonic-gate 	**  Check for cases that can not be encoded.
2377c478bd9Sstevel@tonic-gate 	**
2387c478bd9Sstevel@tonic-gate 	**	For example, you can't encode certain kinds of types
2397c478bd9Sstevel@tonic-gate 	**	or already-encoded messages.  If we find this case,
2407c478bd9Sstevel@tonic-gate 	**	just copy it through.
2417c478bd9Sstevel@tonic-gate 	*/
2427c478bd9Sstevel@tonic-gate 
243058561cbSjbeck 	(void) sm_snprintf(buf, sizeof(buf), "%.100s/%.100s", type, subtype);
2447c478bd9Sstevel@tonic-gate 	if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e')))
2457c478bd9Sstevel@tonic-gate 		flags |= M87F_NO8BIT;
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate # ifdef USE_B_CLASS
2487c478bd9Sstevel@tonic-gate 	if (wordinclass(buf, 'b') || wordinclass(type, 'b'))
2497c478bd9Sstevel@tonic-gate 		MapNLtoCRLF = false;
2507c478bd9Sstevel@tonic-gate # endif /* USE_B_CLASS */
2517c478bd9Sstevel@tonic-gate 	if (wordinclass(buf, 'q') || wordinclass(type, 'q'))
2527c478bd9Sstevel@tonic-gate 		use_qp = true;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	/*
2557c478bd9Sstevel@tonic-gate 	**  Multipart requires special processing.
2567c478bd9Sstevel@tonic-gate 	**
2577c478bd9Sstevel@tonic-gate 	**	Do a recursive descent into the message.
2587c478bd9Sstevel@tonic-gate 	*/
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(type, "multipart") == 0 &&
2613ee0e492Sjbeck 	    (!bitset(M87F_NO8BIT, flags) || bitset(M87F_NO8TO7, flags)) &&
2623ee0e492Sjbeck 	    !bitset(EF_TOODEEP, e->e_flags)
2633ee0e492Sjbeck 	   )
2647c478bd9Sstevel@tonic-gate 	{
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		if (sm_strcasecmp(subtype, "digest") == 0)
2677c478bd9Sstevel@tonic-gate 			flags |= M87F_DIGEST;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++)
2707c478bd9Sstevel@tonic-gate 		{
2717c478bd9Sstevel@tonic-gate 			if (sm_strcasecmp(argv[i].a_field, "boundary") == 0)
2727c478bd9Sstevel@tonic-gate 				break;
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		if (i >= argc || argv[i].a_value == NULL)
2757c478bd9Sstevel@tonic-gate 		{
2767c478bd9Sstevel@tonic-gate 			usrerr("mime8to7: Content-Type: \"%s\": %s boundary",
2777c478bd9Sstevel@tonic-gate 				i >= argc ? "missing" : "bogus", p);
2787c478bd9Sstevel@tonic-gate 			p = "---";
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 			/* avoid bounce loops */
2817c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_DONT_MIME;
2827c478bd9Sstevel@tonic-gate 		}
2837c478bd9Sstevel@tonic-gate 		else
2847c478bd9Sstevel@tonic-gate 		{
2857c478bd9Sstevel@tonic-gate 			p = argv[i].a_value;
2867c478bd9Sstevel@tonic-gate 			stripquotes(p);
2877c478bd9Sstevel@tonic-gate 		}
288058561cbSjbeck 		if (sm_strlcpy(bbuf, p, sizeof(bbuf)) >= sizeof(bbuf))
2897c478bd9Sstevel@tonic-gate 		{
2907c478bd9Sstevel@tonic-gate 			usrerr("mime8to7: multipart boundary \"%s\" too long",
2917c478bd9Sstevel@tonic-gate 				p);
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 			/* avoid bounce loops */
2947c478bd9Sstevel@tonic-gate 			e->e_flags |= EF_DONT_MIME;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		if (tTd(43, 1))
2987c478bd9Sstevel@tonic-gate 			sm_dprintf("mime8to7: multipart boundary \"%s\"\n",
2997c478bd9Sstevel@tonic-gate 				bbuf);
3007c478bd9Sstevel@tonic-gate 		for (i = 0; i < MAXMIMENESTING; i++)
3017c478bd9Sstevel@tonic-gate 		{
3027c478bd9Sstevel@tonic-gate 			if (boundaries[i] == NULL)
3037c478bd9Sstevel@tonic-gate 				break;
3047c478bd9Sstevel@tonic-gate 		}
3057c478bd9Sstevel@tonic-gate 		if (i >= MAXMIMENESTING)
3067c478bd9Sstevel@tonic-gate 		{
3073ee0e492Sjbeck 			if (tTd(43, 4))
3083ee0e492Sjbeck 				sm_dprintf("mime8to7: too deep, i=%d\n", i);
3093ee0e492Sjbeck 			if (!bitset(EF_TOODEEP, e->e_flags))
3107c478bd9Sstevel@tonic-gate 				usrerr("mime8to7: multipart nesting boundary too deep");
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			/* avoid bounce loops */
3133ee0e492Sjbeck 			e->e_flags |= EF_DONT_MIME|EF_TOODEEP;
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 		else
3167c478bd9Sstevel@tonic-gate 		{
3177c478bd9Sstevel@tonic-gate 			boundaries[i] = bbuf;
3187c478bd9Sstevel@tonic-gate 			boundaries[i + 1] = NULL;
3197c478bd9Sstevel@tonic-gate 		}
3207c478bd9Sstevel@tonic-gate 		mci->mci_flags |= MCIF_INMIME;
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 		/* skip the early "comment" prologue */
323445f2479Sjbeck 		if (!putline("", mci))
324445f2479Sjbeck 			goto writeerr;
3257c478bd9Sstevel@tonic-gate 		mci->mci_flags &= ~MCIF_INHEADER;
3267c478bd9Sstevel@tonic-gate 		bt = MBT_FINAL;
327058561cbSjbeck 		while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
3287c478bd9Sstevel@tonic-gate 			!= NULL)
3297c478bd9Sstevel@tonic-gate 		{
3307c478bd9Sstevel@tonic-gate 			bt = mimeboundary(buf, boundaries);
3317c478bd9Sstevel@tonic-gate 			if (bt != MBT_NOTSEP)
3327c478bd9Sstevel@tonic-gate 				break;
333445f2479Sjbeck 			if (!putxline(buf, strlen(buf), mci,
334445f2479Sjbeck 					PXLF_MAPFROM|PXLF_STRIP8BIT))
335445f2479Sjbeck 				goto writeerr;
3367c478bd9Sstevel@tonic-gate 			if (tTd(43, 99))
3377c478bd9Sstevel@tonic-gate 				sm_dprintf("  ...%s", buf);
3387c478bd9Sstevel@tonic-gate 		}
3397c478bd9Sstevel@tonic-gate 		if (sm_io_eof(e->e_dfp))
3407c478bd9Sstevel@tonic-gate 			bt = MBT_FINAL;
3417c478bd9Sstevel@tonic-gate 		while (bt != MBT_FINAL)
3427c478bd9Sstevel@tonic-gate 		{
3437c478bd9Sstevel@tonic-gate 			auto HDR *hdr = NULL;
3447c478bd9Sstevel@tonic-gate 
345058561cbSjbeck 			(void) sm_strlcpyn(buf, sizeof(buf), 2, "--", bbuf);
346445f2479Sjbeck 			if (!putline(buf, mci))
347445f2479Sjbeck 				goto writeerr;
3487c478bd9Sstevel@tonic-gate 			if (tTd(43, 35))
3497c478bd9Sstevel@tonic-gate 				sm_dprintf("  ...%s\n", buf);
3507c478bd9Sstevel@tonic-gate 			collect(e->e_dfp, false, &hdr, e, false);
3517c478bd9Sstevel@tonic-gate 			if (tTd(43, 101))
3527c478bd9Sstevel@tonic-gate 				putline("+++after collect", mci);
353445f2479Sjbeck 			if (!putheader(mci, hdr, e, flags))
354445f2479Sjbeck 				goto writeerr;
3557c478bd9Sstevel@tonic-gate 			if (tTd(43, 101))
3567c478bd9Sstevel@tonic-gate 				putline("+++after putheader", mci);
3573ee0e492Sjbeck 			bt = mime8to7(mci, hdr, e, boundaries, flags,
3583ee0e492Sjbeck 				      level + 1);
359445f2479Sjbeck 			if (bt == SM_IO_EOF)
360445f2479Sjbeck 				goto writeerr;
3617c478bd9Sstevel@tonic-gate 		}
362058561cbSjbeck 		(void) sm_strlcpyn(buf, sizeof(buf), 3, "--", bbuf, "--");
363445f2479Sjbeck 		if (!putline(buf, mci))
364445f2479Sjbeck 			goto writeerr;
3657c478bd9Sstevel@tonic-gate 		if (tTd(43, 35))
3667c478bd9Sstevel@tonic-gate 			sm_dprintf("  ...%s\n", buf);
3677c478bd9Sstevel@tonic-gate 		boundaries[i] = NULL;
3687c478bd9Sstevel@tonic-gate 		mci->mci_flags &= ~MCIF_INMIME;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 		/* skip the late "comment" epilogue */
371058561cbSjbeck 		while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
3727c478bd9Sstevel@tonic-gate 			!= NULL)
3737c478bd9Sstevel@tonic-gate 		{
3747c478bd9Sstevel@tonic-gate 			bt = mimeboundary(buf, boundaries);
3757c478bd9Sstevel@tonic-gate 			if (bt != MBT_NOTSEP)
3767c478bd9Sstevel@tonic-gate 				break;
377445f2479Sjbeck 			if (!putxline(buf, strlen(buf), mci,
378445f2479Sjbeck 					PXLF_MAPFROM|PXLF_STRIP8BIT))
379445f2479Sjbeck 				goto writeerr;
3807c478bd9Sstevel@tonic-gate 			if (tTd(43, 99))
3817c478bd9Sstevel@tonic-gate 				sm_dprintf("  ...%s", buf);
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 		if (sm_io_eof(e->e_dfp))
3847c478bd9Sstevel@tonic-gate 			bt = MBT_FINAL;
3857c478bd9Sstevel@tonic-gate 		if (tTd(43, 3))
3867c478bd9Sstevel@tonic-gate 			sm_dprintf("\t\t\tmime8to7=>%s (multipart)\n",
3877c478bd9Sstevel@tonic-gate 				MimeBoundaryNames[bt]);
3887c478bd9Sstevel@tonic-gate 		return bt;
3897c478bd9Sstevel@tonic-gate 	}
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 	/*
3927c478bd9Sstevel@tonic-gate 	**  Message/xxx types -- recurse exactly once.
3937c478bd9Sstevel@tonic-gate 	**
3947c478bd9Sstevel@tonic-gate 	**	Class 's' is predefined to have "rfc822" only.
3957c478bd9Sstevel@tonic-gate 	*/
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(type, "message") == 0)
3987c478bd9Sstevel@tonic-gate 	{
3993ee0e492Sjbeck 		if (!wordinclass(subtype, 's') ||
4003ee0e492Sjbeck 		    bitset(EF_TOODEEP, e->e_flags))
4017c478bd9Sstevel@tonic-gate 		{
4027c478bd9Sstevel@tonic-gate 			flags |= M87F_NO8BIT;
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 		else
4057c478bd9Sstevel@tonic-gate 		{
4067c478bd9Sstevel@tonic-gate 			auto HDR *hdr = NULL;
4077c478bd9Sstevel@tonic-gate 
408445f2479Sjbeck 			if (!putline("", mci))
409445f2479Sjbeck 				goto writeerr;
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 			mci->mci_flags |= MCIF_INMIME;
4127c478bd9Sstevel@tonic-gate 			collect(e->e_dfp, false, &hdr, e, false);
4137c478bd9Sstevel@tonic-gate 			if (tTd(43, 101))
4147c478bd9Sstevel@tonic-gate 				putline("+++after collect", mci);
415445f2479Sjbeck 			if (!putheader(mci, hdr, e, flags))
416445f2479Sjbeck 				goto writeerr;
4177c478bd9Sstevel@tonic-gate 			if (tTd(43, 101))
4187c478bd9Sstevel@tonic-gate 				putline("+++after putheader", mci);
4197c478bd9Sstevel@tonic-gate 			if (hvalue("MIME-Version", hdr) == NULL &&
420445f2479Sjbeck 			    !bitset(M87F_NO8TO7, flags) &&
421445f2479Sjbeck 			    !putline("MIME-Version: 1.0", mci))
422445f2479Sjbeck 				goto writeerr;
4233ee0e492Sjbeck 			bt = mime8to7(mci, hdr, e, boundaries, flags,
4243ee0e492Sjbeck 				      level + 1);
4257c478bd9Sstevel@tonic-gate 			mci->mci_flags &= ~MCIF_INMIME;
4267c478bd9Sstevel@tonic-gate 			return bt;
4277c478bd9Sstevel@tonic-gate 		}
4287c478bd9Sstevel@tonic-gate 	}
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	/*
4317c478bd9Sstevel@tonic-gate 	**  Non-compound body type
4327c478bd9Sstevel@tonic-gate 	**
4337c478bd9Sstevel@tonic-gate 	**	Compute the ratio of seven to eight bit characters;
4347c478bd9Sstevel@tonic-gate 	**	use that as a heuristic to decide how to do the
4357c478bd9Sstevel@tonic-gate 	**	encoding.
4367c478bd9Sstevel@tonic-gate 	*/
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 	sectionsize = sectionhighbits = 0;
4397c478bd9Sstevel@tonic-gate 	if (!bitset(M87F_NO8BIT|M87F_NO8TO7, flags))
4407c478bd9Sstevel@tonic-gate 	{
4417c478bd9Sstevel@tonic-gate 		/* remember where we were */
4427c478bd9Sstevel@tonic-gate 		offset = sm_io_tell(e->e_dfp, SM_TIME_DEFAULT);
4437c478bd9Sstevel@tonic-gate 		if (offset == -1)
4447c478bd9Sstevel@tonic-gate 			syserr("mime8to7: cannot sm_io_tell on %cf%s",
4457c478bd9Sstevel@tonic-gate 			       DATAFL_LETTER, e->e_id);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 		/* do a scan of this body type to count character types */
448058561cbSjbeck 		while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
4497c478bd9Sstevel@tonic-gate 			!= NULL)
4507c478bd9Sstevel@tonic-gate 		{
4517c478bd9Sstevel@tonic-gate 			if (mimeboundary(buf, boundaries) != MBT_NOTSEP)
4527c478bd9Sstevel@tonic-gate 				break;
4537c478bd9Sstevel@tonic-gate 			for (p = buf; *p != '\0'; p++)
4547c478bd9Sstevel@tonic-gate 			{
4557c478bd9Sstevel@tonic-gate 				/* count bytes with the high bit set */
4567c478bd9Sstevel@tonic-gate 				sectionsize++;
4577c478bd9Sstevel@tonic-gate 				if (bitset(0200, *p))
4587c478bd9Sstevel@tonic-gate 					sectionhighbits++;
4597c478bd9Sstevel@tonic-gate 			}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 			/*
4627c478bd9Sstevel@tonic-gate 			**  Heuristic: if 1/4 of the first 4K bytes are 8-bit,
4637c478bd9Sstevel@tonic-gate 			**  assume base64.  This heuristic avoids double-reading
4647c478bd9Sstevel@tonic-gate 			**  large graphics or video files.
4657c478bd9Sstevel@tonic-gate 			*/
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 			if (sectionsize >= 4096 &&
4687c478bd9Sstevel@tonic-gate 			    sectionhighbits > sectionsize / 4)
4697c478bd9Sstevel@tonic-gate 				break;
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		/* return to the original offset for processing */
4737c478bd9Sstevel@tonic-gate 		/* XXX use relative seeks to handle >31 bit file sizes? */
4747c478bd9Sstevel@tonic-gate 		if (sm_io_seek(e->e_dfp, SM_TIME_DEFAULT, offset, SEEK_SET) < 0)
4757c478bd9Sstevel@tonic-gate 			syserr("mime8to7: cannot sm_io_fseek on %cf%s",
4767c478bd9Sstevel@tonic-gate 			       DATAFL_LETTER, e->e_id);
4777c478bd9Sstevel@tonic-gate 		else
4787c478bd9Sstevel@tonic-gate 			sm_io_clearerr(e->e_dfp);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	/*
4827c478bd9Sstevel@tonic-gate 	**  Heuristically determine encoding method.
4837c478bd9Sstevel@tonic-gate 	**	If more than 1/8 of the total characters have the
4847c478bd9Sstevel@tonic-gate 	**	eighth bit set, use base64; else use quoted-printable.
4857c478bd9Sstevel@tonic-gate 	**	However, only encode binary encoded data as base64,
4867c478bd9Sstevel@tonic-gate 	**	since otherwise the NL=>CRLF mapping will be a problem.
4877c478bd9Sstevel@tonic-gate 	*/
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (tTd(43, 8))
4907c478bd9Sstevel@tonic-gate 	{
4917c478bd9Sstevel@tonic-gate 		sm_dprintf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s, type=%s/%s\n",
4927c478bd9Sstevel@tonic-gate 			(long) sectionhighbits, (long) sectionsize,
4937c478bd9Sstevel@tonic-gate 			cte == NULL ? "[none]" : cte,
4947c478bd9Sstevel@tonic-gate 			type == NULL ? "[none]" : type,
4957c478bd9Sstevel@tonic-gate 			subtype == NULL ? "[none]" : subtype);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 	if (cte != NULL && sm_strcasecmp(cte, "binary") == 0)
4987c478bd9Sstevel@tonic-gate 		sectionsize = sectionhighbits;
4997c478bd9Sstevel@tonic-gate 	linelen = 0;
5007c478bd9Sstevel@tonic-gate 	bp = buf;
5017c478bd9Sstevel@tonic-gate 	if (sectionhighbits == 0)
5027c478bd9Sstevel@tonic-gate 	{
5037c478bd9Sstevel@tonic-gate 		/* no encoding necessary */
5047c478bd9Sstevel@tonic-gate 		if (cte != NULL &&
5057c478bd9Sstevel@tonic-gate 		    bitset(MCIF_CVT8TO7|MCIF_CVT7TO8|MCIF_INMIME,
5067c478bd9Sstevel@tonic-gate 			   mci->mci_flags) &&
5077c478bd9Sstevel@tonic-gate 		    !bitset(M87F_NO8TO7, flags))
5087c478bd9Sstevel@tonic-gate 		{
5097c478bd9Sstevel@tonic-gate 			/*
5107c478bd9Sstevel@tonic-gate 			**  Skip _unless_ in MIME mode and potentially
5117c478bd9Sstevel@tonic-gate 			**  converting from 8 bit to 7 bit MIME.  See
5127c478bd9Sstevel@tonic-gate 			**  putheader() for the counterpart where the
5137c478bd9Sstevel@tonic-gate 			**  CTE header is skipped in the opposite
5147c478bd9Sstevel@tonic-gate 			**  situation.
5157c478bd9Sstevel@tonic-gate 			*/
5167c478bd9Sstevel@tonic-gate 
517058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf),
5187c478bd9Sstevel@tonic-gate 				"Content-Transfer-Encoding: %.200s", cte);
519445f2479Sjbeck 			if (!putline(buf, mci))
520445f2479Sjbeck 				goto writeerr;
5217c478bd9Sstevel@tonic-gate 			if (tTd(43, 36))
5227c478bd9Sstevel@tonic-gate 				sm_dprintf("  ...%s\n", buf);
5237c478bd9Sstevel@tonic-gate 		}
524445f2479Sjbeck 		if (!putline("", mci))
525445f2479Sjbeck 			goto writeerr;
5267c478bd9Sstevel@tonic-gate 		mci->mci_flags &= ~MCIF_INHEADER;
527058561cbSjbeck 		while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
5287c478bd9Sstevel@tonic-gate 			!= NULL)
5297c478bd9Sstevel@tonic-gate 		{
530*7800901eSjbeck 			if (!bitset(MCIF_INLONGLINE, mci->mci_flags))
531*7800901eSjbeck 			{
5327c478bd9Sstevel@tonic-gate 				bt = mimeboundary(buf, boundaries);
5337c478bd9Sstevel@tonic-gate 				if (bt != MBT_NOTSEP)
5347c478bd9Sstevel@tonic-gate 					break;
535*7800901eSjbeck 			}
536*7800901eSjbeck 			if (!putxline(buf, strlen(buf), mci,
537*7800901eSjbeck 				      PXLF_MAPFROM|PXLF_NOADDEOL))
538445f2479Sjbeck 				goto writeerr;
5397c478bd9Sstevel@tonic-gate 		}
5407c478bd9Sstevel@tonic-gate 		if (sm_io_eof(e->e_dfp))
5417c478bd9Sstevel@tonic-gate 			bt = MBT_FINAL;
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 	else if (!MapNLtoCRLF ||
5447c478bd9Sstevel@tonic-gate 		 (sectionsize / 8 < sectionhighbits && !use_qp))
5457c478bd9Sstevel@tonic-gate 	{
5467c478bd9Sstevel@tonic-gate 		/* use base64 encoding */
5477c478bd9Sstevel@tonic-gate 		int c1, c2;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		if (tTd(43, 36))
5507c478bd9Sstevel@tonic-gate 			sm_dprintf("  ...Content-Transfer-Encoding: base64\n");
551445f2479Sjbeck 		if (!putline("Content-Transfer-Encoding: base64", mci))
552445f2479Sjbeck 			goto writeerr;
553058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf),
5547c478bd9Sstevel@tonic-gate 			"X-MIME-Autoconverted: from 8bit to base64 by %s id %s",
5557c478bd9Sstevel@tonic-gate 			MyHostName, e->e_id);
556445f2479Sjbeck 		if (!putline(buf, mci) || !putline("", mci))
557445f2479Sjbeck 			goto writeerr;
5587c478bd9Sstevel@tonic-gate 		mci->mci_flags &= ~MCIF_INHEADER;
5597c478bd9Sstevel@tonic-gate 		while ((c1 = mime_getchar_crlf(e->e_dfp, boundaries, &bt)) !=
5607c478bd9Sstevel@tonic-gate 			SM_IO_EOF)
5617c478bd9Sstevel@tonic-gate 		{
5627c478bd9Sstevel@tonic-gate 			if (linelen > 71)
5637c478bd9Sstevel@tonic-gate 			{
5647c478bd9Sstevel@tonic-gate 				*bp = '\0';
565445f2479Sjbeck 				if (!putline(buf, mci))
566445f2479Sjbeck 					goto writeerr;
5677c478bd9Sstevel@tonic-gate 				linelen = 0;
5687c478bd9Sstevel@tonic-gate 				bp = buf;
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 			linelen += 4;
5717c478bd9Sstevel@tonic-gate 			*bp++ = Base64Code[(c1 >> 2)];
5727c478bd9Sstevel@tonic-gate 			c1 = (c1 & 0x03) << 4;
5737c478bd9Sstevel@tonic-gate 			c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
5747c478bd9Sstevel@tonic-gate 			if (c2 == SM_IO_EOF)
5757c478bd9Sstevel@tonic-gate 			{
5767c478bd9Sstevel@tonic-gate 				*bp++ = Base64Code[c1];
5777c478bd9Sstevel@tonic-gate 				*bp++ = '=';
5787c478bd9Sstevel@tonic-gate 				*bp++ = '=';
5797c478bd9Sstevel@tonic-gate 				break;
5807c478bd9Sstevel@tonic-gate 			}
5817c478bd9Sstevel@tonic-gate 			c1 |= (c2 >> 4) & 0x0f;
5827c478bd9Sstevel@tonic-gate 			*bp++ = Base64Code[c1];
5837c478bd9Sstevel@tonic-gate 			c1 = (c2 & 0x0f) << 2;
5847c478bd9Sstevel@tonic-gate 			c2 = mime_getchar_crlf(e->e_dfp, boundaries, &bt);
5857c478bd9Sstevel@tonic-gate 			if (c2 == SM_IO_EOF)
5867c478bd9Sstevel@tonic-gate 			{
5877c478bd9Sstevel@tonic-gate 				*bp++ = Base64Code[c1];
5887c478bd9Sstevel@tonic-gate 				*bp++ = '=';
5897c478bd9Sstevel@tonic-gate 				break;
5907c478bd9Sstevel@tonic-gate 			}
5917c478bd9Sstevel@tonic-gate 			c1 |= (c2 >> 6) & 0x03;
5927c478bd9Sstevel@tonic-gate 			*bp++ = Base64Code[c1];
5937c478bd9Sstevel@tonic-gate 			*bp++ = Base64Code[c2 & 0x3f];
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 		*bp = '\0';
596445f2479Sjbeck 		if (!putline(buf, mci))
597445f2479Sjbeck 			goto writeerr;
5987c478bd9Sstevel@tonic-gate 	}
5997c478bd9Sstevel@tonic-gate 	else
6007c478bd9Sstevel@tonic-gate 	{
6017c478bd9Sstevel@tonic-gate 		/* use quoted-printable encoding */
6027c478bd9Sstevel@tonic-gate 		int c1, c2;
6037c478bd9Sstevel@tonic-gate 		int fromstate;
6047c478bd9Sstevel@tonic-gate 		BITMAP256 badchars;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		/* set up map of characters that must be mapped */
6077c478bd9Sstevel@tonic-gate 		clrbitmap(badchars);
6087c478bd9Sstevel@tonic-gate 		for (c1 = 0x00; c1 < 0x20; c1++)
6097c478bd9Sstevel@tonic-gate 			setbitn(c1, badchars);
6107c478bd9Sstevel@tonic-gate 		clrbitn('\t', badchars);
6117c478bd9Sstevel@tonic-gate 		for (c1 = 0x7f; c1 < 0x100; c1++)
6127c478bd9Sstevel@tonic-gate 			setbitn(c1, badchars);
6137c478bd9Sstevel@tonic-gate 		setbitn('=', badchars);
6147c478bd9Sstevel@tonic-gate 		if (bitnset(M_EBCDIC, mci->mci_mailer->m_flags))
6157c478bd9Sstevel@tonic-gate 			for (p = "!\"#$@[\\]^`{|}~"; *p != '\0'; p++)
6167c478bd9Sstevel@tonic-gate 				setbitn(*p, badchars);
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate 		if (tTd(43, 36))
6197c478bd9Sstevel@tonic-gate 			sm_dprintf("  ...Content-Transfer-Encoding: quoted-printable\n");
620445f2479Sjbeck 		if (!putline("Content-Transfer-Encoding: quoted-printable",
621445f2479Sjbeck 				mci))
622445f2479Sjbeck 			goto writeerr;
623058561cbSjbeck 		(void) sm_snprintf(buf, sizeof(buf),
6247c478bd9Sstevel@tonic-gate 			"X-MIME-Autoconverted: from 8bit to quoted-printable by %s id %s",
6257c478bd9Sstevel@tonic-gate 			MyHostName, e->e_id);
626445f2479Sjbeck 		if (!putline(buf, mci) || !putline("", mci))
627445f2479Sjbeck 			goto writeerr;
6287c478bd9Sstevel@tonic-gate 		mci->mci_flags &= ~MCIF_INHEADER;
6297c478bd9Sstevel@tonic-gate 		fromstate = 0;
6307c478bd9Sstevel@tonic-gate 		c2 = '\n';
6317c478bd9Sstevel@tonic-gate 		while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) !=
6327c478bd9Sstevel@tonic-gate 			SM_IO_EOF)
6337c478bd9Sstevel@tonic-gate 		{
6347c478bd9Sstevel@tonic-gate 			if (c1 == '\n')
6357c478bd9Sstevel@tonic-gate 			{
6367c478bd9Sstevel@tonic-gate 				if (c2 == ' ' || c2 == '\t')
6377c478bd9Sstevel@tonic-gate 				{
6387c478bd9Sstevel@tonic-gate 					*bp++ = '=';
6397c478bd9Sstevel@tonic-gate 					*bp++ = Base16Code[(c2 >> 4) & 0x0f];
6407c478bd9Sstevel@tonic-gate 					*bp++ = Base16Code[c2 & 0x0f];
6417c478bd9Sstevel@tonic-gate 				}
6427c478bd9Sstevel@tonic-gate 				if (buf[0] == '.' && bp == &buf[1])
6437c478bd9Sstevel@tonic-gate 				{
6447c478bd9Sstevel@tonic-gate 					buf[0] = '=';
6457c478bd9Sstevel@tonic-gate 					*bp++ = Base16Code[('.' >> 4) & 0x0f];
6467c478bd9Sstevel@tonic-gate 					*bp++ = Base16Code['.' & 0x0f];
6477c478bd9Sstevel@tonic-gate 				}
6487c478bd9Sstevel@tonic-gate 				*bp = '\0';
649445f2479Sjbeck 				if (!putline(buf, mci))
650445f2479Sjbeck 					goto writeerr;
6517c478bd9Sstevel@tonic-gate 				linelen = fromstate = 0;
6527c478bd9Sstevel@tonic-gate 				bp = buf;
6537c478bd9Sstevel@tonic-gate 				c2 = c1;
6547c478bd9Sstevel@tonic-gate 				continue;
6557c478bd9Sstevel@tonic-gate 			}
6567c478bd9Sstevel@tonic-gate 			if (c2 == ' ' && linelen == 4 && fromstate == 4 &&
6577c478bd9Sstevel@tonic-gate 			    bitnset(M_ESCFROM, mci->mci_mailer->m_flags))
6587c478bd9Sstevel@tonic-gate 			{
6597c478bd9Sstevel@tonic-gate 				*bp++ = '=';
6607c478bd9Sstevel@tonic-gate 				*bp++ = '2';
6617c478bd9Sstevel@tonic-gate 				*bp++ = '0';
6627c478bd9Sstevel@tonic-gate 				linelen += 3;
6637c478bd9Sstevel@tonic-gate 			}
6647c478bd9Sstevel@tonic-gate 			else if (c2 == ' ' || c2 == '\t')
6657c478bd9Sstevel@tonic-gate 			{
6667c478bd9Sstevel@tonic-gate 				*bp++ = c2;
6677c478bd9Sstevel@tonic-gate 				linelen++;
6687c478bd9Sstevel@tonic-gate 			}
6697c478bd9Sstevel@tonic-gate 			if (linelen > 72 &&
6707c478bd9Sstevel@tonic-gate 			    (linelen > 75 || c1 != '.' ||
6717c478bd9Sstevel@tonic-gate 			     (linelen > 73 && c2 == '.')))
6727c478bd9Sstevel@tonic-gate 			{
6737c478bd9Sstevel@tonic-gate 				if (linelen > 73 && c2 == '.')
6747c478bd9Sstevel@tonic-gate 					bp--;
6757c478bd9Sstevel@tonic-gate 				else
6767c478bd9Sstevel@tonic-gate 					c2 = '\n';
6777c478bd9Sstevel@tonic-gate 				*bp++ = '=';
6787c478bd9Sstevel@tonic-gate 				*bp = '\0';
679445f2479Sjbeck 				if (!putline(buf, mci))
680445f2479Sjbeck 					goto writeerr;
6817c478bd9Sstevel@tonic-gate 				linelen = fromstate = 0;
6827c478bd9Sstevel@tonic-gate 				bp = buf;
6837c478bd9Sstevel@tonic-gate 				if (c2 == '.')
6847c478bd9Sstevel@tonic-gate 				{
6857c478bd9Sstevel@tonic-gate 					*bp++ = '.';
6867c478bd9Sstevel@tonic-gate 					linelen++;
6877c478bd9Sstevel@tonic-gate 				}
6887c478bd9Sstevel@tonic-gate 			}
6897c478bd9Sstevel@tonic-gate 			if (bitnset(bitidx(c1), badchars))
6907c478bd9Sstevel@tonic-gate 			{
6917c478bd9Sstevel@tonic-gate 				*bp++ = '=';
6927c478bd9Sstevel@tonic-gate 				*bp++ = Base16Code[(c1 >> 4) & 0x0f];
6937c478bd9Sstevel@tonic-gate 				*bp++ = Base16Code[c1 & 0x0f];
6947c478bd9Sstevel@tonic-gate 				linelen += 3;
6957c478bd9Sstevel@tonic-gate 			}
6967c478bd9Sstevel@tonic-gate 			else if (c1 != ' ' && c1 != '\t')
6977c478bd9Sstevel@tonic-gate 			{
6987c478bd9Sstevel@tonic-gate 				if (linelen < 4 && c1 == "From"[linelen])
6997c478bd9Sstevel@tonic-gate 					fromstate++;
7007c478bd9Sstevel@tonic-gate 				*bp++ = c1;
7017c478bd9Sstevel@tonic-gate 				linelen++;
7027c478bd9Sstevel@tonic-gate 			}
7037c478bd9Sstevel@tonic-gate 			c2 = c1;
7047c478bd9Sstevel@tonic-gate 		}
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 		/* output any saved character */
7077c478bd9Sstevel@tonic-gate 		if (c2 == ' ' || c2 == '\t')
7087c478bd9Sstevel@tonic-gate 		{
7097c478bd9Sstevel@tonic-gate 			*bp++ = '=';
7107c478bd9Sstevel@tonic-gate 			*bp++ = Base16Code[(c2 >> 4) & 0x0f];
7117c478bd9Sstevel@tonic-gate 			*bp++ = Base16Code[c2 & 0x0f];
7127c478bd9Sstevel@tonic-gate 			linelen += 3;
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		if (linelen > 0 || boundaries[0] != NULL)
7167c478bd9Sstevel@tonic-gate 		{
7177c478bd9Sstevel@tonic-gate 			*bp = '\0';
718445f2479Sjbeck 			if (!putline(buf, mci))
719445f2479Sjbeck 				goto writeerr;
7207c478bd9Sstevel@tonic-gate 		}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 	if (tTd(43, 3))
7247c478bd9Sstevel@tonic-gate 		sm_dprintf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]);
7257c478bd9Sstevel@tonic-gate 	return bt;
726445f2479Sjbeck 
727445f2479Sjbeck   writeerr:
728445f2479Sjbeck 	return SM_IO_EOF;
7297c478bd9Sstevel@tonic-gate }
7307c478bd9Sstevel@tonic-gate /*
7317c478bd9Sstevel@tonic-gate **  MIME_GETCHAR -- get a character for MIME processing
7327c478bd9Sstevel@tonic-gate **
7337c478bd9Sstevel@tonic-gate **	Treats boundaries as SM_IO_EOF.
7347c478bd9Sstevel@tonic-gate **
7357c478bd9Sstevel@tonic-gate **	Parameters:
7367c478bd9Sstevel@tonic-gate **		fp -- the input file.
7377c478bd9Sstevel@tonic-gate **		boundaries -- the current MIME boundaries.
7387c478bd9Sstevel@tonic-gate **		btp -- if the return value is SM_IO_EOF, *btp is set to
7397c478bd9Sstevel@tonic-gate **			the type of the boundary.
7407c478bd9Sstevel@tonic-gate **
7417c478bd9Sstevel@tonic-gate **	Returns:
7427c478bd9Sstevel@tonic-gate **		The next character in the input stream.
7437c478bd9Sstevel@tonic-gate */
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate static int
mime_getchar(fp,boundaries,btp)7467c478bd9Sstevel@tonic-gate mime_getchar(fp, boundaries, btp)
7477c478bd9Sstevel@tonic-gate 	register SM_FILE_T *fp;
7487c478bd9Sstevel@tonic-gate 	char **boundaries;
7497c478bd9Sstevel@tonic-gate 	int *btp;
7507c478bd9Sstevel@tonic-gate {
7517c478bd9Sstevel@tonic-gate 	int c;
7527c478bd9Sstevel@tonic-gate 	static unsigned char *bp = NULL;
7537c478bd9Sstevel@tonic-gate 	static int buflen = 0;
7547c478bd9Sstevel@tonic-gate 	static bool atbol = true;	/* at beginning of line */
7557c478bd9Sstevel@tonic-gate 	static int bt = MBT_SYNTAX;	/* boundary type of next SM_IO_EOF */
7567c478bd9Sstevel@tonic-gate 	static unsigned char buf[128];	/* need not be a full line */
7577c478bd9Sstevel@tonic-gate 	int start = 0;			/* indicates position of - in buffer */
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (buflen == 1 && *bp == '\n')
7607c478bd9Sstevel@tonic-gate 	{
7617c478bd9Sstevel@tonic-gate 		/* last \n in buffer may be part of next MIME boundary */
7627c478bd9Sstevel@tonic-gate 		c = *bp;
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	else if (buflen > 0)
7657c478bd9Sstevel@tonic-gate 	{
7667c478bd9Sstevel@tonic-gate 		buflen--;
7677c478bd9Sstevel@tonic-gate 		return *bp++;
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate 	else
7707c478bd9Sstevel@tonic-gate 		c = sm_io_getc(fp, SM_TIME_DEFAULT);
7717c478bd9Sstevel@tonic-gate 	bp = buf;
7727c478bd9Sstevel@tonic-gate 	buflen = 0;
7737c478bd9Sstevel@tonic-gate 	if (c == '\n')
7747c478bd9Sstevel@tonic-gate 	{
7757c478bd9Sstevel@tonic-gate 		/* might be part of a MIME boundary */
7767c478bd9Sstevel@tonic-gate 		*bp++ = c;
7777c478bd9Sstevel@tonic-gate 		atbol = true;
7787c478bd9Sstevel@tonic-gate 		c = sm_io_getc(fp, SM_TIME_DEFAULT);
7797c478bd9Sstevel@tonic-gate 		if (c == '\n')
7807c478bd9Sstevel@tonic-gate 		{
7817c478bd9Sstevel@tonic-gate 			(void) sm_io_ungetc(fp, SM_TIME_DEFAULT, c);
7827c478bd9Sstevel@tonic-gate 			return c;
7837c478bd9Sstevel@tonic-gate 		}
7847c478bd9Sstevel@tonic-gate 		start = 1;
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 	if (c != SM_IO_EOF)
7877c478bd9Sstevel@tonic-gate 		*bp++ = c;
7887c478bd9Sstevel@tonic-gate 	else
7897c478bd9Sstevel@tonic-gate 		bt = MBT_FINAL;
7907c478bd9Sstevel@tonic-gate 	if (atbol && c == '-')
7917c478bd9Sstevel@tonic-gate 	{
7927c478bd9Sstevel@tonic-gate 		/* check for a message boundary */
7937c478bd9Sstevel@tonic-gate 		c = sm_io_getc(fp, SM_TIME_DEFAULT);
7947c478bd9Sstevel@tonic-gate 		if (c != '-')
7957c478bd9Sstevel@tonic-gate 		{
7967c478bd9Sstevel@tonic-gate 			if (c != SM_IO_EOF)
7977c478bd9Sstevel@tonic-gate 				*bp++ = c;
7987c478bd9Sstevel@tonic-gate 			else
7997c478bd9Sstevel@tonic-gate 				bt = MBT_FINAL;
8007c478bd9Sstevel@tonic-gate 			buflen = bp - buf - 1;
8017c478bd9Sstevel@tonic-gate 			bp = buf;
8027c478bd9Sstevel@tonic-gate 			return *bp++;
8037c478bd9Sstevel@tonic-gate 		}
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 		/* got "--", now check for rest of separator */
8067c478bd9Sstevel@tonic-gate 		*bp++ = '-';
807058561cbSjbeck 		while (bp < &buf[sizeof(buf) - 2] &&
8087c478bd9Sstevel@tonic-gate 		       (c = sm_io_getc(fp, SM_TIME_DEFAULT)) != SM_IO_EOF &&
8097c478bd9Sstevel@tonic-gate 		       c != '\n')
8107c478bd9Sstevel@tonic-gate 		{
8117c478bd9Sstevel@tonic-gate 			*bp++ = c;
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 		*bp = '\0';	/* XXX simply cut off? */
8147c478bd9Sstevel@tonic-gate 		bt = mimeboundary((char *) &buf[start], boundaries);
8157c478bd9Sstevel@tonic-gate 		switch (bt)
8167c478bd9Sstevel@tonic-gate 		{
8177c478bd9Sstevel@tonic-gate 		  case MBT_FINAL:
8187c478bd9Sstevel@tonic-gate 		  case MBT_INTERMED:
8197c478bd9Sstevel@tonic-gate 			/* we have a message boundary */
8207c478bd9Sstevel@tonic-gate 			buflen = 0;
8217c478bd9Sstevel@tonic-gate 			*btp = bt;
8227c478bd9Sstevel@tonic-gate 			return SM_IO_EOF;
8237c478bd9Sstevel@tonic-gate 		}
8247c478bd9Sstevel@tonic-gate 
825058561cbSjbeck 		if (bp < &buf[sizeof(buf) - 2] && c != SM_IO_EOF)
8267c478bd9Sstevel@tonic-gate 			*bp++ = c;
8277c478bd9Sstevel@tonic-gate 	}
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	atbol = c == '\n';
8307c478bd9Sstevel@tonic-gate 	buflen = bp - buf - 1;
8317c478bd9Sstevel@tonic-gate 	if (buflen < 0)
8327c478bd9Sstevel@tonic-gate 	{
8337c478bd9Sstevel@tonic-gate 		*btp = bt;
8347c478bd9Sstevel@tonic-gate 		return SM_IO_EOF;
8357c478bd9Sstevel@tonic-gate 	}
8367c478bd9Sstevel@tonic-gate 	bp = buf;
8377c478bd9Sstevel@tonic-gate 	return *bp++;
8387c478bd9Sstevel@tonic-gate }
8397c478bd9Sstevel@tonic-gate /*
8407c478bd9Sstevel@tonic-gate **  MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF
8417c478bd9Sstevel@tonic-gate **
8427c478bd9Sstevel@tonic-gate **	Parameters:
8437c478bd9Sstevel@tonic-gate **		fp -- the input file.
8447c478bd9Sstevel@tonic-gate **		boundaries -- the current MIME boundaries.
8457c478bd9Sstevel@tonic-gate **		btp -- if the return value is SM_IO_EOF, *btp is set to
8467c478bd9Sstevel@tonic-gate **			the type of the boundary.
8477c478bd9Sstevel@tonic-gate **
8487c478bd9Sstevel@tonic-gate **	Returns:
8497c478bd9Sstevel@tonic-gate **		The next character in the input stream.
8507c478bd9Sstevel@tonic-gate */
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate static int
mime_getchar_crlf(fp,boundaries,btp)8537c478bd9Sstevel@tonic-gate mime_getchar_crlf(fp, boundaries, btp)
8547c478bd9Sstevel@tonic-gate 	register SM_FILE_T *fp;
8557c478bd9Sstevel@tonic-gate 	char **boundaries;
8567c478bd9Sstevel@tonic-gate 	int *btp;
8577c478bd9Sstevel@tonic-gate {
8587c478bd9Sstevel@tonic-gate 	static bool sendlf = false;
8597c478bd9Sstevel@tonic-gate 	int c;
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	if (sendlf)
8627c478bd9Sstevel@tonic-gate 	{
8637c478bd9Sstevel@tonic-gate 		sendlf = false;
8647c478bd9Sstevel@tonic-gate 		return '\n';
8657c478bd9Sstevel@tonic-gate 	}
8667c478bd9Sstevel@tonic-gate 	c = mime_getchar(fp, boundaries, btp);
8677c478bd9Sstevel@tonic-gate 	if (c == '\n' && MapNLtoCRLF)
8687c478bd9Sstevel@tonic-gate 	{
8697c478bd9Sstevel@tonic-gate 		sendlf = true;
8707c478bd9Sstevel@tonic-gate 		return '\r';
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	return c;
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate /*
8757c478bd9Sstevel@tonic-gate **  MIMEBOUNDARY -- determine if this line is a MIME boundary & its type
8767c478bd9Sstevel@tonic-gate **
8777c478bd9Sstevel@tonic-gate **	Parameters:
8787c478bd9Sstevel@tonic-gate **		line -- the input line.
8797c478bd9Sstevel@tonic-gate **		boundaries -- the set of currently pending boundaries.
8807c478bd9Sstevel@tonic-gate **
8817c478bd9Sstevel@tonic-gate **	Returns:
8827c478bd9Sstevel@tonic-gate **		MBT_NOTSEP -- if this is not a separator line
8837c478bd9Sstevel@tonic-gate **		MBT_INTERMED -- if this is an intermediate separator
8847c478bd9Sstevel@tonic-gate **		MBT_FINAL -- if this is a final boundary
8857c478bd9Sstevel@tonic-gate **		MBT_SYNTAX -- if this is a boundary for the wrong
8867c478bd9Sstevel@tonic-gate **			enclosure -- i.e., a syntax error.
8877c478bd9Sstevel@tonic-gate */
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate static int
mimeboundary(line,boundaries)8907c478bd9Sstevel@tonic-gate mimeboundary(line, boundaries)
8917c478bd9Sstevel@tonic-gate 	register char *line;
8927c478bd9Sstevel@tonic-gate 	char **boundaries;
8937c478bd9Sstevel@tonic-gate {
8947c478bd9Sstevel@tonic-gate 	int type = MBT_NOTSEP;
8957c478bd9Sstevel@tonic-gate 	int i;
8967c478bd9Sstevel@tonic-gate 	int savec;
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	if (line[0] != '-' || line[1] != '-' || boundaries == NULL)
8997c478bd9Sstevel@tonic-gate 		return MBT_NOTSEP;
9007c478bd9Sstevel@tonic-gate 	i = strlen(line);
9017c478bd9Sstevel@tonic-gate 	if (i > 0 && line[i - 1] == '\n')
9027c478bd9Sstevel@tonic-gate 		i--;
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	/* strip off trailing whitespace */
9057c478bd9Sstevel@tonic-gate 	while (i > 0 && (line[i - 1] == ' ' || line[i - 1] == '\t'
9067c478bd9Sstevel@tonic-gate #if _FFR_MIME_CR_OK
9077c478bd9Sstevel@tonic-gate 		|| line[i - 1] == '\r'
9087c478bd9Sstevel@tonic-gate #endif /* _FFR_MIME_CR_OK */
9097c478bd9Sstevel@tonic-gate 	       ))
9107c478bd9Sstevel@tonic-gate 		i--;
9117c478bd9Sstevel@tonic-gate 	savec = line[i];
9127c478bd9Sstevel@tonic-gate 	line[i] = '\0';
9137c478bd9Sstevel@tonic-gate 
9147c478bd9Sstevel@tonic-gate 	if (tTd(43, 5))
9157c478bd9Sstevel@tonic-gate 		sm_dprintf("mimeboundary: line=\"%s\"... ", line);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 	/* check for this as an intermediate boundary */
9187c478bd9Sstevel@tonic-gate 	if (isboundary(&line[2], boundaries) >= 0)
9197c478bd9Sstevel@tonic-gate 		type = MBT_INTERMED;
9207c478bd9Sstevel@tonic-gate 	else if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0)
9217c478bd9Sstevel@tonic-gate 	{
9227c478bd9Sstevel@tonic-gate 		/* check for a final boundary */
9237c478bd9Sstevel@tonic-gate 		line[i - 2] = '\0';
9247c478bd9Sstevel@tonic-gate 		if (isboundary(&line[2], boundaries) >= 0)
9257c478bd9Sstevel@tonic-gate 			type = MBT_FINAL;
9267c478bd9Sstevel@tonic-gate 		line[i - 2] = '-';
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	line[i] = savec;
9307c478bd9Sstevel@tonic-gate 	if (tTd(43, 5))
9317c478bd9Sstevel@tonic-gate 		sm_dprintf("%s\n", MimeBoundaryNames[type]);
9327c478bd9Sstevel@tonic-gate 	return type;
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate /*
9357c478bd9Sstevel@tonic-gate **  DEFCHARSET -- return default character set for message
9367c478bd9Sstevel@tonic-gate **
9377c478bd9Sstevel@tonic-gate **	The first choice for character set is for the mailer
9387c478bd9Sstevel@tonic-gate **	corresponding to the envelope sender.  If neither that
9397c478bd9Sstevel@tonic-gate **	nor the global configuration file has a default character
9407c478bd9Sstevel@tonic-gate **	set defined, return "unknown-8bit" as recommended by
9417c478bd9Sstevel@tonic-gate **	RFC 1428 section 3.
9427c478bd9Sstevel@tonic-gate **
9437c478bd9Sstevel@tonic-gate **	Parameters:
9447c478bd9Sstevel@tonic-gate **		e -- the envelope for this message.
9457c478bd9Sstevel@tonic-gate **
9467c478bd9Sstevel@tonic-gate **	Returns:
9477c478bd9Sstevel@tonic-gate **		The default character set for that mailer.
9487c478bd9Sstevel@tonic-gate */
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate char *
defcharset(e)9517c478bd9Sstevel@tonic-gate defcharset(e)
9527c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
9537c478bd9Sstevel@tonic-gate {
9547c478bd9Sstevel@tonic-gate 	if (e != NULL && e->e_from.q_mailer != NULL &&
9557c478bd9Sstevel@tonic-gate 	    e->e_from.q_mailer->m_defcharset != NULL)
9567c478bd9Sstevel@tonic-gate 		return e->e_from.q_mailer->m_defcharset;
9577c478bd9Sstevel@tonic-gate 	if (DefaultCharSet != NULL)
9587c478bd9Sstevel@tonic-gate 		return DefaultCharSet;
9597c478bd9Sstevel@tonic-gate 	return "unknown-8bit";
9607c478bd9Sstevel@tonic-gate }
9617c478bd9Sstevel@tonic-gate /*
9627c478bd9Sstevel@tonic-gate **  ISBOUNDARY -- is a given string a currently valid boundary?
9637c478bd9Sstevel@tonic-gate **
9647c478bd9Sstevel@tonic-gate **	Parameters:
9657c478bd9Sstevel@tonic-gate **		line -- the current input line.
9667c478bd9Sstevel@tonic-gate **		boundaries -- the list of valid boundaries.
9677c478bd9Sstevel@tonic-gate **
9687c478bd9Sstevel@tonic-gate **	Returns:
9697c478bd9Sstevel@tonic-gate **		The index number in boundaries if the line is found.
9707c478bd9Sstevel@tonic-gate **		-1 -- otherwise.
9717c478bd9Sstevel@tonic-gate **
9727c478bd9Sstevel@tonic-gate */
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate static int
isboundary(line,boundaries)9757c478bd9Sstevel@tonic-gate isboundary(line, boundaries)
9767c478bd9Sstevel@tonic-gate 	char *line;
9777c478bd9Sstevel@tonic-gate 	char **boundaries;
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	register int i;
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	for (i = 0; i <= MAXMIMENESTING && boundaries[i] != NULL; i++)
9827c478bd9Sstevel@tonic-gate 	{
9837c478bd9Sstevel@tonic-gate 		if (strcmp(line, boundaries[i]) == 0)
9847c478bd9Sstevel@tonic-gate 			return i;
9857c478bd9Sstevel@tonic-gate 	}
9867c478bd9Sstevel@tonic-gate 	return -1;
9877c478bd9Sstevel@tonic-gate }
9887c478bd9Sstevel@tonic-gate #endif /* MIME8TO7 */
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate #if MIME7TO8
9917c478bd9Sstevel@tonic-gate static int	mime_fromqp __P((unsigned char *, unsigned char **, int));
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate /*
9947c478bd9Sstevel@tonic-gate **  MIME7TO8 -- output 7 bit encoded MIME body in 8 bit format
9957c478bd9Sstevel@tonic-gate **
9967c478bd9Sstevel@tonic-gate **  This is a hack. Supports translating the two 7-bit body-encodings
9977c478bd9Sstevel@tonic-gate **  (quoted-printable and base64) to 8-bit coded bodies.
9987c478bd9Sstevel@tonic-gate **
9997c478bd9Sstevel@tonic-gate **  There is not much point in supporting multipart here, as the UA
10007c478bd9Sstevel@tonic-gate **  will be able to deal with encoded MIME bodies if it can parse MIME
10017c478bd9Sstevel@tonic-gate **  multipart messages.
10027c478bd9Sstevel@tonic-gate **
10037c478bd9Sstevel@tonic-gate **  Note also that we won't be called unless it is a text/plain MIME
10047c478bd9Sstevel@tonic-gate **  message, encoded base64 or QP and mailer flag '9' has been defined
10057c478bd9Sstevel@tonic-gate **  on mailer.
10067c478bd9Sstevel@tonic-gate **
10077c478bd9Sstevel@tonic-gate **  Contributed by Marius Olaffson <marius@rhi.hi.is>.
10087c478bd9Sstevel@tonic-gate **
10097c478bd9Sstevel@tonic-gate **	Parameters:
10107c478bd9Sstevel@tonic-gate **		mci -- mailer connection information.
10117c478bd9Sstevel@tonic-gate **		header -- the header for this body part.
10127c478bd9Sstevel@tonic-gate **		e -- envelope.
10137c478bd9Sstevel@tonic-gate **
10147c478bd9Sstevel@tonic-gate **	Returns:
1015445f2479Sjbeck **		true iff body was written successfully
10167c478bd9Sstevel@tonic-gate */
10177c478bd9Sstevel@tonic-gate 
10187c478bd9Sstevel@tonic-gate static char index_64[128] =
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
10217c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
10227c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
10237c478bd9Sstevel@tonic-gate 	52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
10247c478bd9Sstevel@tonic-gate 	-1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
10257c478bd9Sstevel@tonic-gate 	15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
10267c478bd9Sstevel@tonic-gate 	-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
10277c478bd9Sstevel@tonic-gate 	41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
10287c478bd9Sstevel@tonic-gate };
10297c478bd9Sstevel@tonic-gate 
10307c478bd9Sstevel@tonic-gate # define CHAR64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
10317c478bd9Sstevel@tonic-gate 
1032445f2479Sjbeck bool
mime7to8(mci,header,e)10337c478bd9Sstevel@tonic-gate mime7to8(mci, header, e)
10347c478bd9Sstevel@tonic-gate 	register MCI *mci;
10357c478bd9Sstevel@tonic-gate 	HDR *header;
10367c478bd9Sstevel@tonic-gate 	register ENVELOPE *e;
10377c478bd9Sstevel@tonic-gate {
10387c478bd9Sstevel@tonic-gate 	int pxflags;
10397c478bd9Sstevel@tonic-gate 	register char *p;
10407c478bd9Sstevel@tonic-gate 	char *cte;
10417c478bd9Sstevel@tonic-gate 	char **pvp;
10427c478bd9Sstevel@tonic-gate 	unsigned char *fbufp;
10437c478bd9Sstevel@tonic-gate 	char buf[MAXLINE];
10447c478bd9Sstevel@tonic-gate 	unsigned char fbuf[MAXLINE + 1];
10457c478bd9Sstevel@tonic-gate 	char pvpbuf[MAXLINE];
10467c478bd9Sstevel@tonic-gate 	extern unsigned char MimeTokenTab[256];
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	p = hvalue("Content-Transfer-Encoding", header);
10497c478bd9Sstevel@tonic-gate 	if (p == NULL ||
1050058561cbSjbeck 	    (pvp = prescan(p, '\0', pvpbuf, sizeof(pvpbuf), NULL,
10517c478bd9Sstevel@tonic-gate 			   MimeTokenTab, false)) == NULL ||
10527c478bd9Sstevel@tonic-gate 	    pvp[0] == NULL)
10537c478bd9Sstevel@tonic-gate 	{
10547c478bd9Sstevel@tonic-gate 		/* "can't happen" -- upper level should have caught this */
10557c478bd9Sstevel@tonic-gate 		syserr("mime7to8: unparsable CTE %s", p == NULL ? "<NULL>" : p);
10567c478bd9Sstevel@tonic-gate 
10577c478bd9Sstevel@tonic-gate 		/* avoid bounce loops */
10587c478bd9Sstevel@tonic-gate 		e->e_flags |= EF_DONT_MIME;
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 		/* cheap failsafe algorithm -- should work on text/plain */
10617c478bd9Sstevel@tonic-gate 		if (p != NULL)
10627c478bd9Sstevel@tonic-gate 		{
1063058561cbSjbeck 			(void) sm_snprintf(buf, sizeof(buf),
10647c478bd9Sstevel@tonic-gate 				"Content-Transfer-Encoding: %s", p);
1065445f2479Sjbeck 			if (!putline(buf, mci))
1066445f2479Sjbeck 				goto writeerr;
10677c478bd9Sstevel@tonic-gate 		}
1068445f2479Sjbeck 		if (!putline("", mci))
1069445f2479Sjbeck 			goto writeerr;
10707c478bd9Sstevel@tonic-gate 		mci->mci_flags &= ~MCIF_INHEADER;
1071058561cbSjbeck 		while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf, sizeof(buf))
10727c478bd9Sstevel@tonic-gate 			!= NULL)
1073445f2479Sjbeck 		{
1074445f2479Sjbeck 			if (!putline(buf, mci))
1075445f2479Sjbeck 				goto writeerr;
1076445f2479Sjbeck 		}
1077445f2479Sjbeck 		return true;
10787c478bd9Sstevel@tonic-gate 	}
1079058561cbSjbeck 	cataddr(pvp, NULL, buf, sizeof(buf), '\0', false);
10807c478bd9Sstevel@tonic-gate 	cte = sm_rpool_strdup_x(e->e_rpool, buf);
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	mci->mci_flags |= MCIF_INHEADER;
1083445f2479Sjbeck 	if (!putline("Content-Transfer-Encoding: 8bit", mci))
1084445f2479Sjbeck 		goto writeerr;
1085058561cbSjbeck 	(void) sm_snprintf(buf, sizeof(buf),
10867c478bd9Sstevel@tonic-gate 		"X-MIME-Autoconverted: from %.200s to 8bit by %s id %s",
10877c478bd9Sstevel@tonic-gate 		cte, MyHostName, e->e_id);
1088445f2479Sjbeck 	if (!putline(buf, mci) || !putline("", mci))
1089445f2479Sjbeck 		goto writeerr;
10907c478bd9Sstevel@tonic-gate 	mci->mci_flags &= ~MCIF_INHEADER;
10917c478bd9Sstevel@tonic-gate 
10927c478bd9Sstevel@tonic-gate 	/*
10937c478bd9Sstevel@tonic-gate 	**  Translate body encoding to 8-bit.  Supports two types of
10947c478bd9Sstevel@tonic-gate 	**  encodings; "base64" and "quoted-printable". Assume qp if
10957c478bd9Sstevel@tonic-gate 	**  it is not base64.
10967c478bd9Sstevel@tonic-gate 	*/
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	pxflags = PXLF_MAPFROM;
10997c478bd9Sstevel@tonic-gate 	if (sm_strcasecmp(cte, "base64") == 0)
11007c478bd9Sstevel@tonic-gate 	{
11017c478bd9Sstevel@tonic-gate 		int c1, c2, c3, c4;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 		fbufp = fbuf;
11047c478bd9Sstevel@tonic-gate 		while ((c1 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT)) !=
11057c478bd9Sstevel@tonic-gate 			SM_IO_EOF)
11067c478bd9Sstevel@tonic-gate 		{
11077c478bd9Sstevel@tonic-gate 			if (isascii(c1) && isspace(c1))
11087c478bd9Sstevel@tonic-gate 				continue;
11097c478bd9Sstevel@tonic-gate 
11107c478bd9Sstevel@tonic-gate 			do
11117c478bd9Sstevel@tonic-gate 			{
11127c478bd9Sstevel@tonic-gate 				c2 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
11137c478bd9Sstevel@tonic-gate 			} while (isascii(c2) && isspace(c2));
11147c478bd9Sstevel@tonic-gate 			if (c2 == SM_IO_EOF)
11157c478bd9Sstevel@tonic-gate 				break;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 			do
11187c478bd9Sstevel@tonic-gate 			{
11197c478bd9Sstevel@tonic-gate 				c3 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
11207c478bd9Sstevel@tonic-gate 			} while (isascii(c3) && isspace(c3));
11217c478bd9Sstevel@tonic-gate 			if (c3 == SM_IO_EOF)
11227c478bd9Sstevel@tonic-gate 				break;
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate 			do
11257c478bd9Sstevel@tonic-gate 			{
11267c478bd9Sstevel@tonic-gate 				c4 = sm_io_getc(e->e_dfp, SM_TIME_DEFAULT);
11277c478bd9Sstevel@tonic-gate 			} while (isascii(c4) && isspace(c4));
11287c478bd9Sstevel@tonic-gate 			if (c4 == SM_IO_EOF)
11297c478bd9Sstevel@tonic-gate 				break;
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 			if (c1 == '=' || c2 == '=')
11327c478bd9Sstevel@tonic-gate 				continue;
11337c478bd9Sstevel@tonic-gate 			c1 = CHAR64(c1);
11347c478bd9Sstevel@tonic-gate 			c2 = CHAR64(c2);
11357c478bd9Sstevel@tonic-gate 
11367c478bd9Sstevel@tonic-gate #if MIME7TO8_OLD
11377c478bd9Sstevel@tonic-gate #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
11387c478bd9Sstevel@tonic-gate 			++fbufp;
11397c478bd9Sstevel@tonic-gate #else /* MIME7TO8_OLD */
11407c478bd9Sstevel@tonic-gate #define CHK_EOL if (*--fbufp != '\n' || (fbufp > fbuf && *--fbufp != '\r')) \
11417c478bd9Sstevel@tonic-gate 		{					\
11427c478bd9Sstevel@tonic-gate 			++fbufp;			\
11437c478bd9Sstevel@tonic-gate 			pxflags |= PXLF_NOADDEOL;	\
11447c478bd9Sstevel@tonic-gate 		}
11457c478bd9Sstevel@tonic-gate #endif /* MIME7TO8_OLD */
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate #define PUTLINE64	\
11487c478bd9Sstevel@tonic-gate 	do		\
11497c478bd9Sstevel@tonic-gate 	{		\
11507c478bd9Sstevel@tonic-gate 		if (*fbufp++ == '\n' || fbufp >= &fbuf[MAXLINE])	\
11517c478bd9Sstevel@tonic-gate 		{							\
11527c478bd9Sstevel@tonic-gate 			CHK_EOL;					\
1153445f2479Sjbeck 			if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags)) \
1154445f2479Sjbeck 				goto writeerr;				\
11557c478bd9Sstevel@tonic-gate 			pxflags &= ~PXLF_NOADDEOL;			\
11567c478bd9Sstevel@tonic-gate 			fbufp = fbuf;					\
11577c478bd9Sstevel@tonic-gate 		}	\
11587c478bd9Sstevel@tonic-gate 	} while (0)
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate 			*fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
11617c478bd9Sstevel@tonic-gate 			PUTLINE64;
11627c478bd9Sstevel@tonic-gate 			if (c3 == '=')
11637c478bd9Sstevel@tonic-gate 				continue;
11647c478bd9Sstevel@tonic-gate 			c3 = CHAR64(c3);
11657c478bd9Sstevel@tonic-gate 			*fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
11667c478bd9Sstevel@tonic-gate 			PUTLINE64;
11677c478bd9Sstevel@tonic-gate 			if (c4 == '=')
11687c478bd9Sstevel@tonic-gate 				continue;
11697c478bd9Sstevel@tonic-gate 			c4 = CHAR64(c4);
11707c478bd9Sstevel@tonic-gate 			*fbufp = ((c3 & 0x03) << 6) | c4;
11717c478bd9Sstevel@tonic-gate 			PUTLINE64;
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 	else
11757c478bd9Sstevel@tonic-gate 	{
11767c478bd9Sstevel@tonic-gate 		int off;
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate 		/* quoted-printable */
11797c478bd9Sstevel@tonic-gate 		pxflags |= PXLF_NOADDEOL;
11807c478bd9Sstevel@tonic-gate 		fbufp = fbuf;
11817c478bd9Sstevel@tonic-gate 		while (sm_io_fgets(e->e_dfp, SM_TIME_DEFAULT, buf,
1182058561cbSjbeck 				   sizeof(buf)) != NULL)
11837c478bd9Sstevel@tonic-gate 		{
11847c478bd9Sstevel@tonic-gate 			off = mime_fromqp((unsigned char *) buf, &fbufp,
11857c478bd9Sstevel@tonic-gate 					  &fbuf[MAXLINE] - fbufp);
11867c478bd9Sstevel@tonic-gate again:
11877c478bd9Sstevel@tonic-gate 			if (off < -1)
11887c478bd9Sstevel@tonic-gate 				continue;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 			if (fbufp - fbuf > 0)
1191445f2479Sjbeck 			{
1192445f2479Sjbeck 				if (!putxline((char *) fbuf, fbufp - fbuf - 1,
1193445f2479Sjbeck 						mci, pxflags))
1194445f2479Sjbeck 					goto writeerr;
1195445f2479Sjbeck 			}
11967c478bd9Sstevel@tonic-gate 			fbufp = fbuf;
11977c478bd9Sstevel@tonic-gate 			if (off >= 0 && buf[off] != '\0')
11987c478bd9Sstevel@tonic-gate 			{
11997c478bd9Sstevel@tonic-gate 				off = mime_fromqp((unsigned char *) (buf + off),
12007c478bd9Sstevel@tonic-gate 						  &fbufp,
12017c478bd9Sstevel@tonic-gate 						  &fbuf[MAXLINE] - fbufp);
12027c478bd9Sstevel@tonic-gate 				goto again;
12037c478bd9Sstevel@tonic-gate 			}
12047c478bd9Sstevel@tonic-gate 		}
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	/* force out partial last line */
12087c478bd9Sstevel@tonic-gate 	if (fbufp > fbuf)
12097c478bd9Sstevel@tonic-gate 	{
12107c478bd9Sstevel@tonic-gate 		*fbufp = '\0';
1211445f2479Sjbeck 		if (!putxline((char *) fbuf, fbufp - fbuf, mci, pxflags))
1212445f2479Sjbeck 			goto writeerr;
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	/*
12167c478bd9Sstevel@tonic-gate 	**  The decoded text may end without an EOL.  Since this function
12177c478bd9Sstevel@tonic-gate 	**  is only called for text/plain MIME messages, it is safe to
12187c478bd9Sstevel@tonic-gate 	**  add an extra one at the end just in case.  This is a hack,
12197c478bd9Sstevel@tonic-gate 	**  but so is auto-converting MIME in the first place.
12207c478bd9Sstevel@tonic-gate 	*/
12217c478bd9Sstevel@tonic-gate 
1222445f2479Sjbeck 	if (!putline("", mci))
1223445f2479Sjbeck 		goto writeerr;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	if (tTd(43, 3))
12267c478bd9Sstevel@tonic-gate 		sm_dprintf("\t\t\tmime7to8 => %s to 8bit done\n", cte);
1227445f2479Sjbeck 	return true;
1228445f2479Sjbeck 
1229445f2479Sjbeck   writeerr:
1230445f2479Sjbeck 	return false;
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate /*
12337c478bd9Sstevel@tonic-gate **  The following is based on Borenstein's "codes.c" module, with simplifying
12347c478bd9Sstevel@tonic-gate **  changes as we do not deal with multipart, and to do the translation in-core,
12357c478bd9Sstevel@tonic-gate **  with an attempt to prevent overrun of output buffers.
12367c478bd9Sstevel@tonic-gate **
12377c478bd9Sstevel@tonic-gate **  What is needed here are changes to defend this code better against
12387c478bd9Sstevel@tonic-gate **  bad encodings. Questionable to always return 0xFF for bad mappings.
12397c478bd9Sstevel@tonic-gate */
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate static char index_hex[128] =
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12447c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12457c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12467c478bd9Sstevel@tonic-gate 	0, 1, 2, 3,  4, 5, 6, 7,  8, 9,-1,-1, -1,-1,-1,-1,
12477c478bd9Sstevel@tonic-gate 	-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12487c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12497c478bd9Sstevel@tonic-gate 	-1,10,11,12, 13,14,15,-1, -1,-1,-1,-1, -1,-1,-1,-1,
12507c478bd9Sstevel@tonic-gate 	-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1
12517c478bd9Sstevel@tonic-gate };
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate # define HEXCHAR(c)  (((c) < 0 || (c) > 127) ? -1 : index_hex[(c)])
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate /*
12567c478bd9Sstevel@tonic-gate **  MIME_FROMQP -- decode quoted printable string
12577c478bd9Sstevel@tonic-gate **
12587c478bd9Sstevel@tonic-gate **	Parameters:
12597c478bd9Sstevel@tonic-gate **		infile -- input (encoded) string
12607c478bd9Sstevel@tonic-gate **		outfile -- output string
12617c478bd9Sstevel@tonic-gate **		maxlen -- size of output buffer
12627c478bd9Sstevel@tonic-gate **
12637c478bd9Sstevel@tonic-gate **	Returns:
12647c478bd9Sstevel@tonic-gate **		-2 if decoding failure
12657c478bd9Sstevel@tonic-gate **		-1 if infile completely decoded into outfile
12667c478bd9Sstevel@tonic-gate **		>= 0 is the position in infile decoding
12677c478bd9Sstevel@tonic-gate **			reached before maxlen was reached
12687c478bd9Sstevel@tonic-gate */
12697c478bd9Sstevel@tonic-gate 
12707c478bd9Sstevel@tonic-gate static int
mime_fromqp(infile,outfile,maxlen)12717c478bd9Sstevel@tonic-gate mime_fromqp(infile, outfile, maxlen)
12727c478bd9Sstevel@tonic-gate 	unsigned char *infile;
12737c478bd9Sstevel@tonic-gate 	unsigned char **outfile;
12747c478bd9Sstevel@tonic-gate 	int maxlen;		/* Max # of chars allowed in outfile */
12757c478bd9Sstevel@tonic-gate {
12767c478bd9Sstevel@tonic-gate 	int c1, c2;
12777c478bd9Sstevel@tonic-gate 	int nchar = 0;
12787c478bd9Sstevel@tonic-gate 	unsigned char *b;
12797c478bd9Sstevel@tonic-gate 
12807c478bd9Sstevel@tonic-gate 	/* decrement by one for trailing '\0', at least one other char */
12817c478bd9Sstevel@tonic-gate 	if (--maxlen < 1)
12827c478bd9Sstevel@tonic-gate 		return 0;
12837c478bd9Sstevel@tonic-gate 
12847c478bd9Sstevel@tonic-gate 	b = infile;
12857c478bd9Sstevel@tonic-gate 	while ((c1 = *infile++) != '\0' && nchar < maxlen)
12867c478bd9Sstevel@tonic-gate 	{
12877c478bd9Sstevel@tonic-gate 		if (c1 == '=')
12887c478bd9Sstevel@tonic-gate 		{
12897c478bd9Sstevel@tonic-gate 			if ((c1 = *infile++) == '\0')
12907c478bd9Sstevel@tonic-gate 				break;
12917c478bd9Sstevel@tonic-gate 
12927c478bd9Sstevel@tonic-gate 			if (c1 == '\n' || (c1 = HEXCHAR(c1)) == -1)
12937c478bd9Sstevel@tonic-gate 			{
12947c478bd9Sstevel@tonic-gate 				/* ignore it and the rest of the buffer */
12957c478bd9Sstevel@tonic-gate 				return -2;
12967c478bd9Sstevel@tonic-gate 			}
12977c478bd9Sstevel@tonic-gate 			else
12987c478bd9Sstevel@tonic-gate 			{
12997c478bd9Sstevel@tonic-gate 				do
13007c478bd9Sstevel@tonic-gate 				{
13017c478bd9Sstevel@tonic-gate 					if ((c2 = *infile++) == '\0')
13027c478bd9Sstevel@tonic-gate 					{
13037c478bd9Sstevel@tonic-gate 						c2 = -1;
13047c478bd9Sstevel@tonic-gate 						break;
13057c478bd9Sstevel@tonic-gate 					}
13067c478bd9Sstevel@tonic-gate 				} while ((c2 = HEXCHAR(c2)) == -1);
13077c478bd9Sstevel@tonic-gate 
13087c478bd9Sstevel@tonic-gate 				if (c2 == -1)
13097c478bd9Sstevel@tonic-gate 					break;
13107c478bd9Sstevel@tonic-gate 				nchar++;
13117c478bd9Sstevel@tonic-gate 				*(*outfile)++ = c1 << 4 | c2;
13127c478bd9Sstevel@tonic-gate 			}
13137c478bd9Sstevel@tonic-gate 		}
13147c478bd9Sstevel@tonic-gate 		else
13157c478bd9Sstevel@tonic-gate 		{
13167c478bd9Sstevel@tonic-gate 			nchar++;
13177c478bd9Sstevel@tonic-gate 			*(*outfile)++ = c1;
13187c478bd9Sstevel@tonic-gate 			if (c1 == '\n')
13197c478bd9Sstevel@tonic-gate 				break;
13207c478bd9Sstevel@tonic-gate 		}
13217c478bd9Sstevel@tonic-gate 	}
13227c478bd9Sstevel@tonic-gate 	*(*outfile)++ = '\0';
13237c478bd9Sstevel@tonic-gate 	if (nchar >= maxlen)
13247c478bd9Sstevel@tonic-gate 		return (infile - b - 1);
13257c478bd9Sstevel@tonic-gate 	return -1;
13267c478bd9Sstevel@tonic-gate }
13277c478bd9Sstevel@tonic-gate #endif /* MIME7TO8 */
1328