DirectDrawSurfaceFormatDetector.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using Masuit.Tools.Mime;
  6. namespace Masuit.Tools.Files.FileDetector.Detectors;
  7. [FormatCategory(FormatCategory.Image)]
  8. internal class DirectDrawSurfaceFormatDetector : AbstractSignatureDetector
  9. {
  10. private static readonly SignatureInformation[] DdsSignatureInfo = {
  11. new() { Position = 0, Signature = new byte [] { 0x44, 0x44, 0x53, 0x20 } },
  12. };
  13. public override string Extension => "dds";
  14. protected override SignatureInformation[] SignatureInformations => DdsSignatureInfo;
  15. public override string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  16. public override List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  17. public override bool Detect(Stream stream)
  18. {
  19. if (base.Detect(stream))
  20. {
  21. byte[] buffer = new byte[4];
  22. stream.Read(buffer, 0, 4);
  23. int dwSize = (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) + buffer[0];
  24. if (dwSize != 0x7C)
  25. {
  26. return false;
  27. }
  28. stream.Read(buffer, 0, 4);
  29. int dwFlags = (buffer[3] << 24) + (buffer[2] << 16) + (buffer[1] << 8) + buffer[0];
  30. if ((dwFlags & 0x1) != 0 && (dwFlags & 0x2) != 0 && (dwFlags & 0x4) != 0 && (dwFlags & 0x1000) != 0)
  31. {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. public override string ToString() => "DirectDraw Surface(DDS) Detector";
  38. }