Skip to content

Commit

Permalink
Fixed extraction of TNEF EmbeddedMessage attachment data to skip the …
Browse files Browse the repository at this point in the history
…leading GUID

Final fix for issue #538
  • Loading branch information
jstedfast committed Mar 6, 2020
1 parent 34a4d29 commit c739051
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
37 changes: 21 additions & 16 deletions MimeKit/Tnef/TnefPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,6 @@ static TnefPart PromoteToTnefPart (MimePart part)
tnef.ContentDisposition = part.ContentDisposition;

tnef.ContentTransferEncoding = part.ContentTransferEncoding;
tnef.Content = part.Content;

return tnef;
}
Expand Down Expand Up @@ -410,6 +409,8 @@ static void ExtractAttachments (TnefReader reader, BodyBuilder builder)
if (attachment == null)
break;

attachData = null;

while (prop.ReadNextProperty ()) {
switch (prop.PropertyTag.Id) {
case TnefPropertyId.AttachLongFilename:
Expand Down Expand Up @@ -440,25 +441,10 @@ static void ExtractAttachments (TnefReader reader, BodyBuilder builder)
attachment.ContentDisposition = disposition;
break;
case TnefPropertyId.AttachData:
if (attachMethod == TnefAttachMethod.EmbeddedMessage)
attachment = PromoteToTnefPart (attachment);

attachData = prop.ReadValueAsBytes ();
filter.Flush (attachData, 0, attachData.Length, out outIndex, out outLength);
attachment.ContentTransferEncoding = filter.GetBestEncoding (EncodingConstraint.SevenBit);
attachment.Content = new MimeContent (new MemoryStream (attachData, false));
filter.Reset ();

builder.Attachments.Add (attachment);
break;
case TnefPropertyId.AttachMethod:
attachMethod = (TnefAttachMethod) prop.ReadValueAsInt32 ();

if (attachMethod == TnefAttachMethod.EmbeddedMessage) {
builder.Attachments.Remove (attachment);
attachment = PromoteToTnefPart (attachment);
builder.Attachments.Add (attachment);
}
break;
case TnefPropertyId.AttachMimeTag:
mimeType = prop.ReadValueAsString ().Split ('/');
Expand Down Expand Up @@ -487,6 +473,25 @@ static void ExtractAttachments (TnefReader reader, BodyBuilder builder)
break;
}
}

if (attachData != null) {
int count = attachData.Length;
int index = 0;

if (attachMethod == TnefAttachMethod.EmbeddedMessage) {
attachment.ContentTransferEncoding = ContentEncoding.Base64;
attachment = PromoteToTnefPart (attachment);
count -= 16;
index = 16;
} else {
filter.Flush (attachData, index, count, out outIndex, out outLength);
attachment.ContentTransferEncoding = filter.GetBestEncoding (EncodingConstraint.SevenBit);
filter.Reset ();
}

attachment.Content = new MimeContent (new MemoryStream (attachData, index, count, false));
builder.Attachments.Add (attachment);
}
break;
case TnefAttributeTag.AttachCreateDate:
if (attachment != null) {
Expand Down
Binary file modified UnitTests/TestData/tnef/christmas/Untitled Attachment.1
Binary file not shown.
Binary file modified UnitTests/TestData/tnef/christmas/Untitled Attachment.2
Binary file not shown.
Binary file modified UnitTests/TestData/tnef/christmas/Untitled Attachment.3
Binary file not shown.
Binary file modified UnitTests/TestData/tnef/christmas/Untitled Attachment.4
Binary file not shown.
4 changes: 2 additions & 2 deletions UnitTests/Tnef/TnefTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ public void TestRichTextEml ()
Assert.AreEqual (9217, task1.ContentDisposition.Size, "Size");

var task2 = (MimePart) multipart[3];
Assert.AreEqual ("application/octet-stream", task2.ContentType.MimeType, "MimeType");
Assert.AreEqual ("application/vnd.ms-tnef", task2.ContentType.MimeType, "MimeType");
Assert.AreEqual ("Build a train table", task2.ContentType.Name, "Name");
Assert.AreEqual ("attachment", task2.ContentDisposition.Disposition, "Disposition");
Assert.AreEqual ("Untitled Attachment", task2.ContentDisposition.FileName, "FileName");
Expand All @@ -1009,7 +1009,7 @@ public void TestRichTextEml ()
Assert.AreEqual (387453, appointment1.ContentDisposition.Size, "Size");

var appointment2 = (MimePart) multipart[5];
Assert.AreEqual ("application/octet-stream", appointment2.ContentType.MimeType, "MimeType");
Assert.AreEqual ("application/vnd.ms-tnef", appointment2.ContentType.MimeType, "MimeType");
Assert.AreEqual ("Christmas Celebration!", appointment2.ContentType.Name, "Name");
Assert.AreEqual ("attachment", appointment2.ContentDisposition.Disposition, "Disposition");
Assert.AreEqual ("Untitled Attachment", appointment2.ContentDisposition.FileName, "FileName");
Expand Down

0 comments on commit c739051

Please sign in to comment.