-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor OpenDocument formats to be in the zip hierarchy to prevent m…
…ultiple format matches
- Loading branch information
Neil Harvey
committed
Nov 19, 2024
1 parent
fc30460
commit fe9e759
Showing
4 changed files
with
55 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters