Skip to content

Commit

Permalink
Refactor OpenDocument formats to be in the zip hierarchy to prevent m…
Browse files Browse the repository at this point in the history
…ultiple format matches
  • Loading branch information
Neil Harvey committed Nov 19, 2024
1 parent fc30460 commit fe9e759
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
45 changes: 45 additions & 0 deletions src/FileSignatures/Formats/OpenDocument.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Text;

namespace FileSignatures.Formats;

/// <summary>
/// Specifies the format of an OpenDocument file.
/// </summary>
public abstract class OpenDocument : Zip
{
private const int SubsignatureOffset = 30;

protected OpenDocument(byte[] subsignature, string mediaType, string extension) : base(SubsignatureOffset + subsignature.Length, mediaType, extension)
{
Subsignature = new ReadOnlyCollection<byte>(subsignature); ;
}

/// <summary>
/// Gets the subsignature of the OpenDocument file.
/// This will be the media type in byte format e.g. 'mimetypeapplication/vnd.oasis.opendocument.presentation'
/// </summary>
public ReadOnlyCollection<byte> Subsignature { get; }

public override bool IsMatch(Stream stream)
{
if (!base.IsMatch(stream))
return false;

stream.Position = SubsignatureOffset;

for (int i = 0; i < Subsignature.Count; i++)
{
var b = stream.ReadByte();
if (b != Subsignature[i])
{
return false;
}
}

return true;
}
}
8 changes: 4 additions & 4 deletions src/FileSignatures/Formats/OpenDocumentPresentation.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
namespace FileSignatures.Formats
{
/// <summary>
/// Specifies the format of a OffsetFileFormat file.
/// Specifies the format of a OpenDocument Presentation file.
/// </summary>
public class OpenDocumentPresentation : FileFormat
public class OpenDocumentPresentation : OpenDocument
{
public OpenDocumentPresentation() : base(new byte[] {
public OpenDocumentPresentation() : base([
0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F, 0x6E,
0x2F, 0x76, 0x6E, 0x64, 0x2E, 0x6F, 0x61, 0x73, 0x69, 0x73, 0x2E, 0x6F, 0x70, 0x65, 0x6E, 0x64, 0x6F, 0x63, 0x75,
0x6D, 0x65, 0x6E, 0x74, 0x2E, 0x70, 0x72, 0x65, 0x73, 0x65, 0x6E, 0x74, 0x61, 0x74, 0x69, 0x6F, 0x6E
}, "application/vnd.oasis.opendocument.presentation", "odp", 30)
], "application/vnd.oasis.opendocument.presentation", "odp")
{
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/FileSignatures/Formats/OpenDocumentSpreadsheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/// <summary>
/// Specifies the format of a OffsetFileFormat file.
/// </summary>
public class OpenDocumentSpreadsheet : FileFormat
public class OpenDocumentSpreadsheet : OpenDocument
{
public OpenDocumentSpreadsheet() : base(new byte[] {
public OpenDocumentSpreadsheet() : base([
0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6F,
0x6E, 0x2F, 0x76, 0x6E, 0x64, 0x2E, 0x6F, 0x61, 0x73, 0x69, 0x73, 0x2E, 0x6F, 0x70, 0x65, 0x6E, 0x64, 0x6F,
0x63, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x2E, 0x73, 0x70, 0x72, 0x65, 0x61, 0x64, 0x73, 0x68, 0x65, 0x65, 0x74
}, "application/vnd.oasis.opendocument.spreadsheet", "ods", 30)
], "application/vnd.oasis.opendocument.spreadsheet", "ods")
{
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/FileSignatures/Formats/OpenDocumentText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
/// <summary>
/// Specifies the format of a OpenDocumentText file.
/// </summary>
public class OpenDocumentText : FileFormat
public class OpenDocumentText : OpenDocument
{
public OpenDocumentText() : base(new byte[] {
public OpenDocumentText() : base([
0x6D, 0x69, 0x6D, 0x65, 0x74, 0x79, 0x70, 0x65, 0x61, 0x70, 0x70, 0x6C, 0x69, 0x63, 0x61, 0x74,
0x69, 0x6F, 0x6E, 0x2F, 0x76, 0x6E, 0x64, 0x2E, 0x6F, 0x61, 0x73, 0x69, 0x73, 0x2E, 0x6F, 0x70,
0x65, 0x6E, 0x64, 0x6F, 0x63, 0x75, 0x6D, 0x65, 0x6E, 0x74, 0x2E, 0x74, 0x65, 0x78, 0x74
}, "application/vnd.oasis.opendocument.text", "odt", 30)
], "application/vnd.oasis.opendocument.text", "odt")
{
}
}
Expand Down

0 comments on commit fe9e759

Please sign in to comment.