Skip to content

Commit

Permalink
removing regex from DecodeQuotedPrintable; closes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
andyedinborough committed Apr 8, 2012
1 parent 70ac458 commit bce8c78
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
6 changes: 5 additions & 1 deletion Tests/Parsing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ public void TestQuotedPrintable() {
test = Utilities.DecodeQuotedPrintable(test);
test.Should().Equal("\r\n\r\n\r\n\r\n\r\n");

test = "H=C3=BAsv=C3=A9ti=20=C3=9Cnnepeket!";
test = Utilities.DecodeQuotedPrintable(test);
test.Should().Equal("Húsvéti Ünnepeket!");

test = Utilities.DecodeWords("coucou =?ISO-8859-1?Q?=E0_tous?=");
test.Should().Equal("coucou à tous");
test = Utilities.DecodeWords("=?iso-8859-1?Q?h=E9llo=5Fthere?=");
Expand Down Expand Up @@ -248,7 +252,7 @@ public void TestParseMessageFromIPhone() {
msg = GetMessage(anotherMessage);
msg.Body.Should().Contain("Joplin");
}

[TestMethod]
public void TestBasicMessage() {
var msg = GetMessage(@"From: test@localhost
Expand Down
43 changes: 24 additions & 19 deletions Utilities.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;
Expand Down Expand Up @@ -82,8 +81,6 @@ internal static bool StartsWithWhiteSpace(this string line) {
return chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r';
}

private static Regex rxNewLines = new Regex(@"\=[\r\n]+", RegexOptions.Singleline | RegexOptions.Compiled);
private static Regex rxEscaped = new Regex(@"(\=[0-9A-F]{2}){1,2}", RegexOptions.Compiled);
internal static string DecodeQuotedPrintable(string value, Encoding encoding = null) {
if (encoding == null) {
encoding = System.Text.Encoding.UTF8;
Expand All @@ -92,27 +89,35 @@ internal static string DecodeQuotedPrintable(string value, Encoding encoding = n
if (value.IndexOf('_') > -1 && value.IndexOf(' ') == -1)
value = value.Replace('_', ' ');

value = rxNewLines.Replace(value, string.Empty);
var matches = rxEscaped.Matches(value);
foreach (var match in matches.Cast<Match>().Reverse()) {
var data = System.Text.Encoding.ASCII.GetBytes(value);
var eq = Convert.ToByte('=');
var n = 0;
for (int i = 0; i < data.Length; i++) {
var b = data[i];
if (b == 10 || b == 13) {
continue;

int ascii;
try {
ascii = int.Parse(match.Value.Replace("=", string.Empty), System.Globalization.NumberStyles.HexNumber);
} catch (Exception ex) {
throw new Exception("Failed parsing \"" + match.Value + "\" as an integer", ex);
}
} else if (b == eq) {
byte b1 = data[i + 1], b2 = data[i + 2];
if (b1 == 10 || b1 == 13) {
i++;
if (b2 == 10 || b2 == 13) {
i++;
}
continue;
}

//http://stackoverflow.com/questions/1318933/c-sharp-int-to-byte
var result = BitConverter.GetBytes(ascii);
if (BitConverter.IsLittleEndian)
Array.Reverse(result);
data[n] = (byte)int.Parse(value.Substring(i + 1, 2), NumberStyles.HexNumber);
n++;
i += 2;

value = value.Substring(0, match.Index)
+ encoding.GetString(result).Trim('\0')
+ value.Substring(match.Index + match.Length);
} else {
data[n] = b;
n++;
}
}

value = encoding.GetString(data, 0, n);
return value;
}

Expand Down

0 comments on commit bce8c78

Please sign in to comment.