Răsfoiți Sursa

增加visio文件检测

懒得勤快 1 an în urmă
părinte
comite
da71b89967

+ 1 - 1
Directory.Build.props

@@ -1,6 +1,6 @@
 <Project>
  <PropertyGroup>
-   <Version>2024.5.5</Version>
+   <Version>2024.5.6</Version>
    <Deterministic>true</Deterministic>
  </PropertyGroup>
 </Project>

+ 4 - 4
Masuit.Tools.Abstractions/Files/FileDetector/AbstractSignatureDetector.cs

@@ -10,17 +10,17 @@ public record struct SignatureInformation : IEquatable<SignatureInformation>
     /// <summary>
     ///
     /// </summary>
-    public int Position;
+    public int Position{get;set;}
 
     /// <summary>
     ///
     /// </summary>
-    public byte[] Signature;
+    public byte[] Signature{get;set;}
 
     /// <summary>
     ///
     /// </summary>
-    public byte[] Presignature;
+    public byte[] Presignature{get;set;}
 
     /// <summary>指示当前对象是否等于同一类型的另一个对象。</summary>
     /// <param name="other">一个与此对象进行比较的对象。</param>
@@ -95,6 +95,6 @@ public abstract class AbstractSignatureDetector : IDetector
         {
             return false;
         }
-        return a1.SequenceEqual(a2);
+        return a1.Zip(a2,(x, y) => (x,y)).All(t => t.x==t.y);
     }
 }

+ 35 - 18
Masuit.Tools.Abstractions/Files/FileDetector/Detectors/DLLDetector.cs

@@ -1,7 +1,4 @@
-using System.Collections.Generic;
-using System.IO;
-using System.Linq;
-using System.Reflection;
+using System.Reflection;
 using System.Text;
 using Masuit.Tools.Mime;
 
@@ -9,29 +6,49 @@ namespace Masuit.Tools.Files.FileDetector.Detectors;
 
 [FormatCategory(FormatCategory.System)]
 [FormatCategory(FormatCategory.Executable)]
-internal sealed class DLLDetector : IDetector
+internal sealed class DLLDetector : AbstractSignatureDetector
 {
-    public string Precondition => "exe";
+    public override string Precondition => "exe";
 
-    public string Extension => "dll";
+    protected override SignatureInformation[] SignatureInformations { get; } =
+    {
+        new()
+        {
+            Position = 0,
+            Signature = "MZ"u8.ToArray()
+        },
+    };
 
-    public string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
+    public override string Extension => "dll";
 
-    public List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
+    public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
 
-    public bool Detect(Stream stream)
+    public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
+
+    public override bool Detect(Stream stream)
     {
-        stream.Position = 60;
-        using var reader = new BinaryReader(stream, Encoding.UTF8, true);
-        var num = reader.ReadInt32();
-        if (num<0)
+        if (stream.Length < 100)
         {
             return false;
         }
-        stream.Position = num + 4 + 18;
-        short characteristics = reader.ReadInt16();
-        return (characteristics & 0x2000) != 0;
+
+        if (base.Detect(stream))
+        {
+            stream.Position = 60;
+            var reader = new BinaryReader(stream, Encoding.UTF8, true);
+            var num = reader.ReadInt32();
+            if (num < 0)
+            {
+                return false;
+            }
+
+            stream.Position = num + 4 + 18;
+            short characteristics = reader.ReadInt16();
+            return (characteristics & 0x2000) != 0;
+        }
+
+        return false;
     }
 
     public override string ToString() => "Windows Dynamic Linkage Library Detector";
-}
+}

+ 1 - 1
Masuit.Tools.Abstractions/Files/FileDetector/Detectors/DOCXDetector.cs

@@ -28,4 +28,4 @@ internal sealed class DOCXDetector : AbstractZipDetailDetector
     public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
 
     public override string ToString() => "Microsoft Word Open XML Document(DOCX) Detector";
-}
+}

+ 4 - 0
Masuit.Tools.Abstractions/Files/FileDetector/Detectors/EXEDetector.cs

@@ -24,6 +24,10 @@ internal sealed class EXEDetector : AbstractSignatureDetector
 
     public override bool Detect(Stream stream)
     {
+        if (stream.Length<100)
+        {
+            return false;
+        }
         if (base.Detect(stream))
         {
             stream.Position = 60;

+ 2 - 1
Masuit.Tools.Abstractions/Files/FileDetector/Detectors/JpegDetector.cs

@@ -7,7 +7,7 @@ using Masuit.Tools.Mime;
 namespace Masuit.Tools.Files.FileDetector.Detectors;
 
 [FormatCategory(FormatCategory.Image)]
-internal sealed class JpegDetector : AbstractSignatureDetector
+public sealed class JpegDetector : AbstractSignatureDetector
 {
     private static readonly SignatureInformation[] JpegSignatureInfo = {
         new() { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xFE, 0x00 } },
@@ -17,6 +17,7 @@ internal sealed class JpegDetector : AbstractSignatureDetector
         new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE2 } },
         new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE3 } },
         new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xE8 } },
+        new () { Position = 0, Signature = new byte [] { 0xFF, 0xD8, 0xFF, 0xEE } },
     };
 
     public override string Extension => "jpg";

+ 2 - 1
Masuit.Tools.Abstractions/Files/FileDetector/Detectors/TextDetector.cs

@@ -24,7 +24,8 @@ internal sealed class TextDetector : IDetector
         Encoding.UTF8,
         Encoding.GetEncoding ( "utf-32" ),
         Encoding.Unicode,
-        Encoding.BigEndianUnicode
+        Encoding.BigEndianUnicode,
+        Encoding.ASCII, 
     };
 
     public string Extension => "txt";

+ 28 - 0
Masuit.Tools.Abstractions/Files/FileDetector/Detectors/VSDXDetector.cs

@@ -0,0 +1,28 @@
+using System.Reflection;
+using Masuit.Tools.Mime;
+
+namespace Masuit.Tools.Files.FileDetector.Detectors;
+
+[FormatCategory(FormatCategory.Document)]
+internal sealed class VSDXDetector : AbstractZipDetailDetector
+{
+    public override IEnumerable<string> Files
+    {
+        get
+        {
+            yield return "[Content_Types].xml";
+            yield return "_rels/.rels";
+            yield return "visio/_rels/document.xml.rels";
+        }
+    }
+
+    public override string Precondition => "zip";
+
+    public override string Extension => "vsdx";
+
+    public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
+
+    public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
+
+    public override string ToString() => "Microsoft Visio Open XML Document(DOCX) Detector";
+}

+ 1 - 1
Masuit.Tools.Abstractions/Files/FileDetector/FileSignatureDetector.cs

@@ -54,7 +54,7 @@ public static class FileSignatureDetector
 
     public static IDetector DetectFiletype(this Stream stream)
     {
-        if (stream.CanSeek)
+        if (stream.CanSeek&&stream.Length>0)
         {
             foreach (var detector in Detectors)
             {

+ 0 - 1
Masuit.Tools.Abstractions/Masuit.Tools.Abstractions.csproj

@@ -124,7 +124,6 @@
 
     <ItemGroup>
         <Folder Include="Extensions\Dynamics\" />
-        <Folder Include="Files\FileDetector\" />
     </ItemGroup>
 
     <ItemGroup>