Click here to Skip to main content
15,896,153 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 377.2K   7K   74  
RFC-compliant Mime-En/Decoder
// MIMECode.cpp: implementation of the CMIMECode class.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MimeSniffer.h"
#include "MIMECode.h"
#include "MimeDecoder.h"
#include "QuotedPrinted.h"
#include "Base64.h"
#include "DefaultCoder.h"
#include "ISSHelper.h"

//////////////////////////////////////////////////////////////////////
// CMIMECode
//////////////////////////////////////////////////////////////////////

CMIMECode::CMIMECode()
{
}

CMIMECode::~CMIMECode()
{
}

BOOL CMIMECode::IsMailSafeChar(char c)
{
	if (	c >= 'A' && c <= 'Z'
		||	c >= 'a' && c <= 'z'
        ||	c == 39
        ||	c == 40
        ||	c == 41
        ||	c == 43
        ||	c == 44
        ||	c == 45
        ||	c == 46
        ||	c == 47
        ||	c == 58
        ||	c == 61
        ||	c == 63
	   )
	   return TRUE;
	return FALSE;
}


CMIMECode* CMIMECode::GetCoderByTransferType(long nType)
{
	switch (nType)
	{
		case CMimeDecoder::mechanism_quoted_printable:
		{
			return new CQuotedPrintable;
		}
		break;

		case CMimeDecoder::mechanism_base64:
		{
			return new CBase64;
		}
		break;

		case CMimeDecoder::mechanism_7bit:
		case CMimeDecoder::mechanism_8bit:
		case CMimeDecoder::mechanism_binary:
		{
			return new CDefaultCoder;
		}
		break;
	}
	return new CMIMECodeRaw;
}

int CMIMECode::Decode( LPCBYTE szDecoding, LPTSTR szOutput, DWORD len /*= MAXDWORD*/ )
{
	if (len == MAXDWORD)
	   len = strlen((const char*)szDecoding);

	CISSHelper src;
	CISSHelper dest;

	src.Write(szDecoding, len, NULL);
	
	Decode(&src, &dest);

	DWORD result;

	LENGTH_OF_STREAM(result, (&dest))

	if (szOutput != NULL)
		dest.Read(szOutput, result, NULL);

	return result;
}

void CMIMECode::Encode( LPCBYTE szEncoding, int nSize, string& strResult )
{
	CISSHelper src;
	CISSHelper dest;

	src.Write(szEncoding, nSize, NULL);
	
	Encode(&src, &dest);

	DWORD result;

	LENGTH_OF_STREAM(result, (&dest))

	if (result > 0)
	{
		char* pBuf = new char[result];

		dest.Read(pBuf, result, NULL);

		strResult = pBuf;

		delete [] pBuf;
	}
	else
		strResult = "";
}

//////////////////////////////////////////////////////////////////////
// CMIMECodeRaw
//////////////////////////////////////////////////////////////////////

void CMIMECodeRaw::Decode(LPSTREAM pSource, LPSTREAM pDest)
{
	LPBYTE	pBuf = new BYTE[65536];

	ATLASSERT(pBuf != NULL);

	DWORD dwRead = 0;

	do
	{
		pSource->Read(pBuf, 65536, &dwRead);

		if (dwRead == 0)
			break;

		pDest->Write(pBuf, dwRead, NULL);
	}
	while(true);

	delete [] pBuf;
}

void CMIMECodeRaw::Encode(LPSTREAM pSource, LPSTREAM pDest)
{
	Decode(pSource, pDest);
}

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