Skip to content

Commit

Permalink
Fixed line length calculations in BestEncodingFilter
Browse files Browse the repository at this point in the history
Fixes issue #497
  • Loading branch information
jstedfast committed Aug 8, 2019
1 parent 630a96e commit 3ecff5b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
6 changes: 6 additions & 0 deletions MimeKit/IO/Filters/BestEncodingFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class BestEncodingFilter : IMimeFilter
int markerLength;
bool hasMarker;
int total;
byte pc;

/// <summary>
/// Initializes a new instance of the <see cref="BestEncodingFilter"/> class.
Expand Down Expand Up @@ -153,9 +154,13 @@ unsafe void Scan (byte* inptr, byte* inend)
marker[markerLength++] = c;

linelen++;
pc = c;
}

if (c == (byte) '\n') {
if (pc == (byte) '\r')
linelen--;

maxline = Math.Max (maxline, linelen);
linelen = 0;

Expand Down Expand Up @@ -259,6 +264,7 @@ public void Reset ()
count0 = 0;
count8 = 0;
total = 0;
pc = 0;
}

#endregion
Expand Down
24 changes: 24 additions & 0 deletions UnitTests/IO/Filters/FilterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ public void TestBestEncodingFilter ()
Assert.AreEqual (ContentEncoding.QuotedPrintable, encoding, "French (long lines) no constraint.");
}
}

filter.Reset ();

// Test 78 character line length with CRLF
using (var stream = new MemoryStream ()) {
using (var filtered = new FilteredStream (stream)) {
var buffer = Encoding.ASCII.GetBytes ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz\r\nabc\r\n");
ContentEncoding encoding;

filtered.Add (filter);

filtered.Write (buffer, 0, buffer.Length);
filtered.Flush ();

encoding = filter.GetBestEncoding (EncodingConstraint.SevenBit, 78);
Assert.AreEqual (ContentEncoding.SevenBit, encoding, "78-character line; 7bit constraint.");

encoding = filter.GetBestEncoding (EncodingConstraint.EightBit, 78);
Assert.AreEqual (ContentEncoding.SevenBit, encoding, "78-character line; 8bit constraint.");

encoding = filter.GetBestEncoding (EncodingConstraint.None, 78);
Assert.AreEqual (ContentEncoding.SevenBit, encoding, "78-character line; no constraint.");
}
}
}

[Test]
Expand Down

0 comments on commit 3ecff5b

Please sign in to comment.