1
1

EXEDetector.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Text;
  6. using Masuit.Tools.Mime;
  7. namespace Masuit.Tools.Files.FileDetector.Detectors;
  8. [FormatCategory(FormatCategory.Executable)]
  9. internal class EXEDetector : AbstractSignatureDetector
  10. {
  11. private static readonly SignatureInformation[] ExeSignatureInfo = {
  12. new() { Position = 0, Signature = new byte [] { 0x4D, 0x5A } },
  13. };
  14. public override string Extension => "exe";
  15. protected override SignatureInformation[] SignatureInformations => ExeSignatureInfo;
  16. public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  17. public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  18. public override bool Detect(Stream stream)
  19. {
  20. if (base.Detect(stream))
  21. {
  22. stream.Position = 60;
  23. using BinaryReader reader = new BinaryReader(stream, Encoding.UTF8, true);
  24. stream.Position = reader.ReadInt32();
  25. return reader.ReadByte() == 0x50 && reader.ReadByte() == 0x45 && reader.ReadByte() == 0 && reader.ReadByte() == 0;
  26. }
  27. return false;
  28. }
  29. public override string ToString() => "Portable Execution File Format Detector";
  30. }