Click here to Skip to main content
Click here to Skip to main content

AMMimeUtils

By , 13 Jun 2001
 

Introduction

There are already other articles here on The Code Project that shows how to decode Base64 and Quoted-Printable, but they all use MFC. I needed some code that didn't use MFC, so I wrote AMMimeUtils.

I wrote these classes because I was working with receiving and sending emails and Usenet messages. Almost all email messages and attachments are either Base64 or Quoted-Printable encoded. Attachments in Usenet messages are often UU encoded, I still need to write a class to handle this, but it might come in a later version.

When you get an email, the subject and other header fields might also be encoded, so this code also includes some code to decode these fields. Different mail programs encode the subject in different ways. The following text:

Just a small text (for demo), and some more text...

can look both like

=?iso-8859-1?Q?Just a small text =28for demo=29, and some more text...?=

or like

Just a small text =?iso-8859-1?Q?=28for demo=29?=, and some more text...

The first line is easy, because we can see that the entire string is encoded with Quoted-Printable (the ?Q? part means Quoted-Printable). In the second string, it's only a part of it that's encoded, so we have to get the first non-encoded part, decode the encoded part, and get the last non-encoded part, and add the 3 parts together to get the final subject.

I made a function char* MimeDecodeMailHeaderField(char *s); to handle this. If you have a string called s containing the subject you want to decode, simply call it like this:

s = MimeDecodeMailHeaderField(s);

Now s contains the decoded text.

I have 2 classes CBase64Utils and CQPUtils for general encoding and decoding of Base64 and Quoted-Printable. The interface looks like:

//class to handle all base64 stuff...
class CBase64Utils
{
private:
  int ErrorCode;
public:
  int GetLastError() {return ErrorCode;};
  CBase64Utils();
  ~CBase64Utils();
  //caller must free the result, bufsize holds the decoded length
  char* Decode(char *input, int *bufsize);
  //caller must free the result, bufsize is the length of the input buffer
  char* Encode(char *input, int bufsize);
};

//class to handle quoted-printable stuff
class CQPUtils
{
private:
  char* ExpandBuffer(char *buffer, int UsedSize, 
             int *BufSize, bool SingleChar = true);
  int ErrorCode;
public:
  int GetLastError() {return ErrorCode;};
  char* Decode(char *input); //caller must free the result
  char* Encode(char *input); //caller must free the result
  CQPUtils();
  ~CQPUtils();
};

The only difference is the Decode() and Encode() functions. Quoted-Printable is always text, therefore it only takes one parameter, the string containing encoded text, and returns a pointer to a new buffer containing the decoded text. Base64 might be an encoded binary file, so it puts the length of the returned buffer in the bufsize variable. Then it's possible to save the decoded buffer as a binary file.

Both classes have a function GetLastError(), if you decode something, and this variable is zero, everything is fine, if it's non-zero there was an error in the input, but you still get the (maybe) encoded/decoded result.

Right now, this code only has functions for what I needed when I wrote it. In the future, it I might add some better error handling.

If you want to know more about MIME and email messages, you can take a look at:

  • RFC 2045 - Multipurpose Internet Mail Extensions (MIME) Part One, Format of Internet Message Bodies
  • RFC 2046 - Multipurpose Internet Mail Extensions (MIME) Part Two, Format of Internet Message Bodies
  • RFC 2044 - Multipurpose Internet Mail Extensions (MIME) Part Three, Format of Internet Message Bodies
  • RFC 822 - STANDARD FOR THE FORMAT OF ARPA INTERNET TEXT MESSAGES

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

About the Author

Anders Molin
Software Developer (Senior)
Denmark Denmark
Member
Huh! Wink | ;-)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
BugMajor bug [modified]memberPavel6729 Mar '12 - 4:06 
Hi, in MSDN is "realloc returns a void pointer to the reallocated (and possibly moved)" and "moved" is problem. *(fresult++) = '=' etc. writes to bad place   Pavel
Generalunicode versionmembermengkim3 Apr '07 - 22:39 
Hi, the code does not support unicod. Anyone has a unicode version?
GeneralNotice!!!membersongzd7 Dec '06 - 22:51 
void CUFMovImage::GetData(unsigned char* str) { int pBuffsize =strlen((char *) str); //Set pBuffsize !!! pBase64 = Decode((char*)str, &pBuffsize); if (image4.m_pPicture != NULL) image4.UnLoad();   if (image4.m_pPicture == NULL) { image4.LoadFromBuffer((unsigned...
GeneralBug in mail decodermembersuperagenteg22 Nov '06 - 23:24 
I think there´s a bug in the function that decodes the mail´s header. My problem was that the realloc gets out of memory. To fix that I´ve declared another char *, malloc memory for it, operating then with that string and returning it at the end.   Gr
QuestionBUG when decode b64 HTML!?memberKFC1234 Jul '06 - 21:45 
Hi there I use CBase64Utils to decode a b64 HTML code, and get the original code back. However, there is always a  at the end of the decoded HTML? I try other decoder, also try the decode the same source with some email client, with which I can get the correct HTML! I am just wondering if it...
GeneralSmall, but nasty bugs :mad:memberscjurgen16 Sep '04 - 2:41 
The bugs are still in the source code, even they are known since some time. Is it to difficult to update the sources?     (Lost 1 hour to understand the 8-bit sign problem)
General2 bugs foundmemberDavid Connet28 Apr '04 - 13:45 
I found 2 bugs: one when encoding and one when decoding.   If you attempt to encode a 1 or 2 byte buffer, nothing will encode. The fix is to add: +   if (bufsize > 2) +   {          while (count <= bufsize) ... (about line 266) (and,...
GeneralRe: 2 bugs foundmemberNate Austin8 Mar '07 - 18:53 
You are correct sir.
GeneralQ-Printable decode improvementmemberIrek Zielinski13 Apr '04 - 8:53 
I was using your code for a some time and I found that sometimes it fails to decode quoted-printable format of some e-mail messages.   I found that the problem is that those messages don't use capital letters in QP text.   Here is a modified code to handle decoding:   bool...
GeneralRe: Q-Printable decode improvementsussthenickname19 Jan '05 - 9:55 
I'm not sure I would call that an improvement. The MIME RFC specifies that the quoted printable characters must always be in uppercase ("Uppercase letters must be used; lowercase letters are not allowed." / RFC 20459). So using toupper would violate this requirement and hence make your...
GeneralRe: Q-Printable decode improvementmemberIrek Zielinski19 Jan '05 - 10:21 
I agree. But the world is not perfect. There is a lot of software around that produces non RFC content. If you want to create a reliable e-mail reader (like in my case) you need to handle also a non fully RFC compatible messages (otherwise your users will start complaing that your e-mail reader...
GeneralI think it is bug (base64 encode)memberKiangGiap Lau8 Jan '04 - 16:52 
Line: 322 in AMMimeUtils.cpp Function: CBase64Utils::Encode(char *input, int bufsize) 320: unsigned char mid = (256 - (0 - *s)); 321: tmp |= mid; 322: //tmp |= *s; 323: tmp <<= 8; 324: count++; 325: s++;   KG
GeneralRe: I think it is bug (base64 encode)memberratzfatz18 Jan '04 - 23:27 
Yes, i think it is correkt to delete line 322.
Generaldecoding string with many ISOmemberDevCrazy17 Jun '03 - 4:01 
your softwar doesn't work if you have lot of "=?iso-8859-1?Q?" in the string (exception....) and you've some troubles with your free !!!! DevCrazy
GeneralBinary datamemberJuan Carlos Cobas5 Sep '02 - 23:10 
I can't see how this class will be able to encode/decode binary data since input values are char instead of unsigned char. I tried to encoded an array of numbers without success  
GeneralRe: Binary datamemberIrek Zielinski9 Apr '04 - 7:29 
I needed also this feature. In fact the char is not a problem here. The problem is that output is threated as a ascii string. Here is a modified, stand alone decode funtion + unchanged hex table. I decided also to dump some of pointers (I dont like using raw pointers too much) and added...
GeneralGet Base64 Encoded LengthmemberPaul Kissel22 Aug '02 - 8:25 
Hi,   If its not too much trouble, can you tell me the algorithm that I can use with your class to predetermine the encoded length of an uncoded buffer.   For example, lets say that I will be encoding a file of data and the filelength is 100. I'm looking for an algorithm to tell...
GeneralRe: Get Base64 Encoded LengthmemberPaul Kissel22 Aug '02 - 8:49 
I see the line in your BASE64 encoder routine:   int alsize = ((bufsize * 4) / 3); char *finalresult = (char*)calloc(alsize + ((alsize / MaxLineLength) * 2) + (10 * sizeof(char)), sizeof(char));   So you do the standard calc. of * 4 / 3. Then to add in 2 bytes for every line...
GeneralRe: Get Base64 Encoded LengthmemberAnders Molin26 Aug '02 - 13:55 
That looks ok   - Anders   Money talks, but all mine ever says is "Goodbye!"
Generalquoted-printable result truncated.memberAnonymous2 Nov '01 - 13:06 
This code return a truncated string : CQPUtils qp2; char *result22 = qp2.Encode(buf);   Example: Microsoft welcome message in Outlook after calling this routine looks like:           Welcome to   Microsoft Outlook 2000  ...
GeneralRe: quoted-printable result truncated.memberAnonymous5 Nov '01 - 7:23 
I think I found the bug: when mids = "9", mids[1]='\0' and it will insert '\0' and terminate the string!   //add the hex value for the char...          char mids[3];          itoa(mid, mids, 16);      ...
GeneralRe: quoted-printable result truncated.memberAnders Molin17 Nov '01 - 9:30 
Hey thanks, I'll take a look at it   - Anders   Money talks, but all mine ever says is "Goodbye!"
GeneralBug in decodememberAnonymous25 Oct '01 - 4:55 
Change line   //check to see if it's a legal base64 char... while (base64map[*s] == SKIP) to   //check to see if it's a legal base64 char... while (base64map[(unsigned char)(*s)] == SKIP)
QuestionIn the class CBase64,why the decode fun can't decode the jpg file whice had been encode??memberAnonymous17 Oct '01 - 17:02 
I test the decode fun can decode the .txt file,but .jpg and .word can't!
AnswerRe: In the class CBase64,why the decode fun can't decode the jpg file whice had been encode??memberAnders Molin19 Oct '01 - 10:09 
I'll take a look at it, but I have been testing the class with both executable and zip files, and it seems to be working fine...   - Anders   Money talks, but all mine ever says is "Goodbye!"

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 14 Jun 2001
Article Copyright 2001 by Anders Molin
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid