Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect decode UTF-8 encoded mail address #50

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 68 additions & 46 deletions OpenPop/Mime/Decode/EncodedWord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,53 +80,75 @@ public static string Decode(string encodedWords)
string decodedWords = encodedWords;

MatchCollection matches = Regex.Matches(encodedWords, encodedWordRegex);
foreach (Match match in matches)
{
// If this match was not a success, we should not use it
if (!match.Success) continue;

string fullMatchValue = match.Value;

string encodedText = match.Groups["Content"].Value;
string encoding = match.Groups["Encoding"].Value;
string charset = match.Groups["Charset"].Value;

// Get the encoding which corrosponds to the character set
Encoding charsetEncoding = EncodingFinder.FindEncoding(charset);

// Store decoded text here when done
string decodedText;

// Encoding may also be written in lowercase
switch (encoding.ToUpperInvariant())
{
// RFC:
// The "B" encoding is identical to the "BASE64"
// encoding defined by RFC 2045.
// http://tools.ietf.org/html/rfc2045#section-6.8
case "B":
decodedText = Base64.Decode(encodedText, charsetEncoding);
break;

// RFC:
// The "Q" encoding is similar to the "Quoted-Printable" content-
// transfer-encoding defined in RFC 2045.
// There are more details to this. Please check
// http://tools.ietf.org/html/rfc2047#section-4.2
//
case "Q":
decodedText = QuotedPrintable.DecodeEncodedWord(encodedText, charsetEncoding);
break;

default:
throw new ArgumentException("The encoding " + encoding + " was not recognized");
}

// Repalce our encoded value with our decoded value
decodedWords = decodedWords.Replace(fullMatchValue, decodedText);
}
if (matches.Count > 0)
{
#region decode words
string encoding = null;
string charset = null;

// Find encoding and charset
foreach (Match match in matches)
{
if (match.Success)
{
encoding = match.Groups["Encoding"].Value;
charset = match.Groups["Charset"].Value;
break;
}
}
if (encoding == null)
throw new ArgumentException("The encoding " + encoding + " was not recognized");

// Join all ecncoded text
StringBuilder matchCache = new StringBuilder();
StringBuilder encodedCache = new StringBuilder();
foreach (Match match in matches)
{
// If this match was not a success, we should not use it
if (!match.Success)
continue;

matchCache.Append(match.Value);
encodedCache.Append(match.Groups["Content"].Value);
}
string encodedText = encodedCache.ToString();

// Get the encoding which corrosponds to the character set
Encoding charsetEncoding = EncodingFinder.FindEncoding(charset);

// Store decoded text here when done
string decodedText;

// Encoding may also be written in lowercase
switch (encoding.ToUpperInvariant())
{
// RFC:
// The "B" encoding is identical to the "BASE64"
// encoding defined by RFC 2045.
// http://tools.ietf.org/html/rfc2045#section-6.8
case "B":
decodedText = Base64.Decode(encodedText, charsetEncoding);
break;

// RFC:
// The "Q" encoding is similar to the "Quoted-Printable" content-
// transfer-encoding defined in RFC 2045.
// There are more details to this. Please check
// http://tools.ietf.org/html/rfc2047#section-4.2
//
case "Q":
decodedText = QuotedPrintable.DecodeEncodedWord(encodedText, charsetEncoding);
break;

default:
throw new ArgumentException("The encoding " + encoding + " was not recognized");
}

decodedWords = decodedWords.Replace(matchCache.ToString(), decodedText);
#endregion
}

return decodedWords;
}
}
}
}