Skip to content

Commit

Permalink
Merge pull request #47 from neilharvey/nonstandard-office-files
Browse files Browse the repository at this point in the history
Nonstandard office files
  • Loading branch information
neilharvey authored May 15, 2022
2 parents 33fef7c + 8fe0642 commit e14962f
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ bld/

# Visual Studio 2015 cache/options directory
.vs/
# Visual Studio code
.vscode/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

Expand Down
8 changes: 4 additions & 4 deletions src/FileSignatures/FileSignatures.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<Version>4.3.0</Version>
<Version>4.3.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<AssemblyVersion>4.3.0</AssemblyVersion>
<LangVersion>8.0</LangVersion>
<AssemblyVersion>4.3.1</AssemblyVersion>
<LangVersion>Latest</LangVersion>
<Nullable>enable</Nullable>
<FileVersion>4.3.0</FileVersion>
<FileVersion>4.3.1</FileVersion>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>..\..\FileSignatures.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
Expand Down
9 changes: 7 additions & 2 deletions src/FileSignatures/Formats/OfficeOpenXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@ protected OfficeOpenXml(string identifiableEntry, string mediaType, string exten

public bool IsMatch(IDisposable? file)
{
if(file is ZipArchive archive)
if (file is ZipArchive archive)
{
return archive.Entries.Any(e => e.FullName.Equals(IdentifiableEntry, StringComparison.OrdinalIgnoreCase));
// Match archives which contain a non-standard version of the identifiable entry, e.g. document2.xml instead of document.xml.
var index = Math.Max(0, IdentifiableEntry.LastIndexOf('.'));
var fileName = IdentifiableEntry.Substring(0, IdentifiableEntry.Length - index);
var extension = IdentifiableEntry.Substring(index);
return archive.Entries.Any(e => e.FullName.StartsWith(fileName, StringComparison.OrdinalIgnoreCase)
&& e.FullName.EndsWith(extension, StringComparison.OrdinalIgnoreCase));
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions test/FileSignatures.Tests/FileSignatures.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>FileSignatures.Tests</AssemblyName>
<PackageId>FileSignatures.Tests</PackageId>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<LangVersion>8.0</LangVersion>
<LangVersion>Latest</LangVersion>
<StartupObject>FileSignatures.Tests.Benchmarks.Program</StartupObject>
</PropertyGroup>

Expand Down
26 changes: 26 additions & 0 deletions test/FileSignatures.Tests/Formats/OfficeOpenXmlTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using FileSignatures.Formats;
using System.IO;
using System.IO.Compression;
using Xunit;

namespace FileSignatures.Tests.Formats
Expand All @@ -17,5 +18,30 @@ public void InvalidZipArchiveDoesNotThrow()
Assert.NotNull(format);
Assert.IsType<Zip>(format);
}

[Fact]
public void IdentifierWithoutExtensionDoesNotThrow()
{
var format = new TestOfficeOpenXml("test", "example/test", "test");

using var stream = new MemoryStream();
using(var createArchive = new ZipArchive(stream, ZipArchiveMode.Create, true))
{
createArchive.CreateEntry("test");
}

using var archive = new ZipArchive(stream, ZipArchiveMode.Read);

var result = format.IsMatch(archive);

Assert.True(result);
}

private class TestOfficeOpenXml : OfficeOpenXml
{
public TestOfficeOpenXml(string identifiableEntry, string mediaType, string extension) : base(identifiableEntry, mediaType, extension)
{
}
}
}
}
1 change: 1 addition & 0 deletions test/FileSignatures.Tests/FunctionalTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class FunctionalTests
[InlineData("test.exif", "image/jpeg")]
[InlineData("saved.msg", "application/vnd.ms-outlook")]
[InlineData("dragndrop.msg", "application/vnd.ms-outlook")]
[InlineData("nonstandard.docx","application/vnd.openxmlformats-officedocument.wordprocessingml.document")]
[InlineData("test.pdf", "application/pdf")]
[InlineData("test.rtf", "application/rtf")]
[InlineData("test.png", "image/png")]
Expand Down
Binary file not shown.

0 comments on commit e14962f

Please sign in to comment.