Skip to content

Commit

Permalink
When using a try/catch for charset conversion, make sure the Encoding…
Browse files Browse the repository at this point in the history
… does not use a fallback char

Fixes issue #88
  • Loading branch information
jstedfast committed Dec 12, 2014
1 parent 17399d8 commit 5db8684
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 6 deletions.
4 changes: 2 additions & 2 deletions MimeKit/InternetAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ static bool TryParseLocalPart (byte[] text, ref int index, int endIndex, bool th
return false;

try {
token.Append (Encoding.UTF8.GetString (text, start, index - start));
} catch (Exception ex) {
token.Append (CharsetUtils.UTF8.GetString (text, start, index - start));
} catch (DecoderFallbackException ex) {
if (throwOnError)
throw new ParseException ("Internationalized local-part tokens may only contain UTF-8 characters.", start, start, ex);

Expand Down
4 changes: 2 additions & 2 deletions MimeKit/TextPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,8 @@ public TextPart () : base ("text", "plain")

if (encoding == null) {
try {
return Encoding.UTF8.GetString (content, 0, (int) memory.Length);
} catch {
return CharsetUtils.UTF8.GetString (content, 0, (int) memory.Length);
} catch (DecoderFallbackException) {
// fall back to iso-8859-1
encoding = Encoding.GetEncoding (28591); // iso-8859-1
}
Expand Down
5 changes: 5 additions & 0 deletions MimeKit/Utils/CharsetUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#if PORTABLE
using EncoderReplacementFallback = Portable.Text.EncoderReplacementFallback;
using DecoderReplacementFallback = Portable.Text.DecoderReplacementFallback;
using EncoderExceptionFallback = Portable.Text.EncoderExceptionFallback;
using DecoderExceptionFallback = Portable.Text.DecoderExceptionFallback;
using DecoderFallbackBuffer = Portable.Text.DecoderFallbackBuffer;
using DecoderFallback = Portable.Text.DecoderFallback;
using Encoding = Portable.Text.Encoding;
Expand All @@ -41,6 +43,9 @@
namespace MimeKit.Utils {
static class CharsetUtils
{
// Note: Encoding.UTF8.GetString() replaces invalid bytes with a unicode '?' character,
// so we use our own UTF8 instance when using GetString() if we do not want it to do that.
public static readonly Encoding UTF8 = Encoding.GetEncoding (65001, new EncoderExceptionFallback (), new DecoderExceptionFallback ());
static readonly Dictionary<string, int> aliases;

static CharsetUtils ()
Expand Down
4 changes: 2 additions & 2 deletions MimeKit/Utils/ParseUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ static bool TryParseDotAtom (byte[] text, ref int index, int endIndex, byte[] se
index++;

try {
token.Append (Encoding.UTF8.GetString (text, start, index - start));
} catch (ParseException ex) {
token.Append (CharsetUtils.UTF8.GetString (text, start, index - start));
} catch (DecoderFallbackException ex) {
if (throwOnError)
throw new ParseException ("Internationalized domains may only contain UTF-8 characters.", start, start, ex);

Expand Down

0 comments on commit 5db8684

Please sign in to comment.