DLLDetector.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132
  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.System)]
  9. [FormatCategory(FormatCategory.Executable)]
  10. internal sealed class DLLDetector : IDetector
  11. {
  12. public string Precondition => "exe";
  13. public string Extension => "dll";
  14. public string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  15. public List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  16. public bool Detect(Stream stream)
  17. {
  18. stream.Position = 60;
  19. using var reader = new BinaryReader(stream, Encoding.UTF8, true);
  20. stream.Position = reader.ReadInt32() + 4 + 18;
  21. short characteristics = reader.ReadInt16();
  22. return (characteristics & 0x2000) != 0;
  23. }
  24. public override string ToString() => "Windows Dynamic Linkage Library Detector";
  25. }