AbstractSignatureDetector.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6. using Masuit.Tools.Mime;
  7. namespace Masuit.Tools.Files.FileDetector;
  8. [StructLayout(LayoutKind.Sequential)]
  9. public struct SignatureInformation
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public int Position;
  15. /// <summary>
  16. ///
  17. /// </summary>
  18. public byte[] Signature;
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public byte[] Presignature;
  23. }
  24. public abstract class AbstractSignatureDetector : IDetector
  25. {
  26. public abstract string Extension { get; }
  27. public virtual string Precondition => null;
  28. protected abstract SignatureInformation[] SignatureInformations { get; }
  29. public virtual string MimeType => new MimeMapper().GetMimeFromExtension("." + Extension);
  30. public virtual List<FormatCategory> FormatCategories => GetType().GetCustomAttributes<FormatCategoryAttribute>().Select(a => a.Category).ToList();
  31. private int _cachedLongestLength = -1;
  32. public virtual bool Detect(Stream stream)
  33. {
  34. if (_cachedLongestLength == -1)
  35. {
  36. foreach (var sig in SignatureInformations)
  37. {
  38. _cachedLongestLength = _cachedLongestLength < sig.Signature.Length ? sig.Signature.Length : _cachedLongestLength;
  39. }
  40. }
  41. byte[] buffer = new byte[_cachedLongestLength];
  42. byte[] preSignature = null;
  43. bool correct = false;
  44. while (true)
  45. {
  46. bool found = false;
  47. foreach (var siginfo in SignatureInformations.Where(si => CompareArray(si.Presignature, preSignature)).OrderBy(si => si.Position))
  48. {
  49. correct = false;
  50. stream.Position = siginfo.Position;
  51. stream.Read(buffer, 0, _cachedLongestLength);
  52. if (CompareArray(siginfo.Signature, buffer))
  53. {
  54. preSignature = siginfo.Signature;
  55. correct = true;
  56. found = true;
  57. break;
  58. }
  59. }
  60. if (!found)
  61. {
  62. break;
  63. }
  64. }
  65. return correct;
  66. }
  67. private bool CompareArray(byte[] a1, byte[] a2)
  68. {
  69. if (a1 == null && a2 == null) return true;
  70. if (a1 == null || a2 == null) return false;
  71. bool failed = false;
  72. int min = a1.Length > a2.Length ? a2.Length : a1.Length;
  73. for (int i = 0; i < min; ++i)
  74. {
  75. if (a1[i] != a2[i])
  76. {
  77. failed = true;
  78. break;
  79. }
  80. }
  81. return !failed;
  82. }
  83. }