Click here to Skip to main content
15,889,116 members
Articles / Desktop Programming / ATL

En/Decode MIME-Content with MimeSniffer

Rate me:
Please Sign up or sign in to vote.
4.88/5 (26 votes)
2 Dec 20022 min read 374.9K   7K   74  
RFC-compliant Mime-En/Decoder
// DefaultCoder.cpp: implementation of the CDefaultCoder class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "DefaultCoder.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDefaultCoder::CDefaultCoder()
{

}

CDefaultCoder::~CDefaultCoder()
{

}

void CDefaultCoder::Decode(LPSTREAM pSource, LPSTREAM pDest)
{
	char	buf[4096];
	DWORD	dwRead;
	DWORD	dwPos = 0;
	
	memset(buf, 0, sizeof(buf));

	do
	{
		pSource->Read(buf+dwPos, 1, &dwRead);

		if (dwRead != 1 || buf[dwPos] == 10 || dwPos > (sizeof(buf)-2))
		{
			if (strlen(buf) > 0)
			{
				if (strcmp(buf, "..\r\n") == 0 )
				{
					strcpy(buf, ".\r\n");
				}
				pDest->Write(buf, strlen(buf), NULL);
			}
			memset(buf, 0, sizeof(buf));
			dwPos = 0;
		
			if (dwRead != 1)
				break;
		}
		else
			dwPos++;
	}
	while(true);
}

void CDefaultCoder::Encode(LPSTREAM pSource, LPSTREAM pDest)
{
	BYTE		c;
	DWORD		dwRead;
	char		buf[128];
	DWORD		nBreak = 75;
	int			nState = 0;
	BYTE		nLast;
	DWORD		dwWritten = 0;
	DWORD		dwWrittenWithoutLineFeed = 0;

	memset(buf, 0, sizeof(buf));

	do
	{
		pSource->Read(&c, 1, &dwRead);

		if (dwRead != 1)
		{
			if (strlen(buf) > 0)
			{
				pDest->Write(buf, strlen(buf), NULL);
				dwWritten += strlen(buf);
				nLast = buf[strlen(buf)-1];
			}
			break;
		}
		if (strlen(buf) > 40)
		{
			if (strstr(buf, "\n") != 0)
				dwWrittenWithoutLineFeed = 0;
			else
				dwWrittenWithoutLineFeed += 20;

			if (dwWrittenWithoutLineFeed > nBreak)
			{
				pDest->Write("=\r\n", 3, NULL);	
				dwWrittenWithoutLineFeed = 0;
			}

			pDest->Write(buf, 20, NULL);
			dwWritten += 20;

			memmove(buf, buf+20, sizeof(buf)-20);
		}
	   
		buf[strlen(buf)] = c;

		restate:

		switch(nState)
		{
			case 0:
			{
				if      (c == '\r')
				{
					nState = 1;
					continue;
				}
			}
			break;

			case 1:
			{
				if (c == '\n')
				{
					nState = 2;
					continue;
				}
				else
				{
					nState = 0;
					goto restate;
				}
			}
			break;

			case 2:
			{
				if (c == '.')
				{
					nState = 3;
					continue;
				}
				else
				{
					nState = 0;
					goto restate;
				}
			}
			break;

			case 3:
			{
				if (c == '\r')
				{
					nState = 4;
					continue;
				}
				else
				{
					nState = 0;
					goto restate;
				}
			}
			break;

			case 4:
			{
				if (c == '\n')
				{
					sprintf(buf+strlen(buf)-3, "..\r\n");
				}
				else
				{
					nState = 0;
					goto restate;
				}
			}
			break;

			default:
			{
				ATLASSERT(FALSE);
			}
			break;
		}
		if (strlen(buf) > 0)
		{
			if (strstr(buf, "\n") != 0)
				dwWrittenWithoutLineFeed = 0;
			else
				dwWrittenWithoutLineFeed += strlen(buf);

			if (dwWrittenWithoutLineFeed > nBreak)
			{
				pDest->Write("\r\n", 2, NULL);	
				dwWrittenWithoutLineFeed = 0;
			}

			pDest->Write(buf, strlen(buf), NULL);
			dwWritten += strlen(buf);
			nLast = buf[strlen(buf)-1];
		}
		memset(buf, 0, sizeof(buf));
		nState = 0;
	}
	while(true);

	if (dwWritten > 0 && nLast != 10)
		pDest->Write("\r\n", 2, NULL);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions