diff --git a/src/ImageSharp/Formats/Tga/TgaConstants.cs b/src/ImageSharp/Formats/Tga/TgaConstants.cs index 88c98b06a9..5aabe92a1d 100644 --- a/src/ImageSharp/Formats/Tga/TgaConstants.cs +++ b/src/ImageSharp/Formats/Tga/TgaConstants.cs @@ -16,5 +16,10 @@ namespace SixLabors.ImageSharp.Formats.Tga /// The list of file extensions that equate to a targa file. /// public static readonly IEnumerable FileExtensions = new[] { "tga", "vda", "icb", "vst" }; + + /// + /// The file header length of a tga image in bytes. + /// + public const int FileHeaderLength = 18; } } diff --git a/src/ImageSharp/Formats/Tga/TgaFileHeader.cs b/src/ImageSharp/Formats/Tga/TgaFileHeader.cs index 72c275b289..e2bbb6fbd2 100644 --- a/src/ImageSharp/Formats/Tga/TgaFileHeader.cs +++ b/src/ImageSharp/Formats/Tga/TgaFileHeader.cs @@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Tga /// /// Defines the size of the data structure in the targa file. /// - public const int Size = 18; + public const int Size = TgaConstants.FileHeaderLength; public TgaFileHeader( byte idLength, diff --git a/src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs b/src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs index e305728473..5a0b0f44ca 100644 --- a/src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs +++ b/src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs @@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tga public sealed class TgaImageFormatDetector : IImageFormatDetector { /// - public int HeaderSize => 18; + public int HeaderSize => TgaConstants.FileHeaderLength; /// public IImageFormat DetectFormat(ReadOnlySpan header) @@ -21,7 +21,14 @@ namespace SixLabors.ImageSharp.Formats.Tga private bool IsSupportedFileFormat(ReadOnlySpan header) { - return header.Length >= this.HeaderSize; + if (header.Length >= this.HeaderSize) + { + // There is no magick bytes in a tga file, so at least the image type in the header will be checked for a valid value. + var imageType = (TgaImageType)header[2]; + return imageType.IsValid(); + } + + return true; } } } diff --git a/src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs b/src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs index 406e12d08b..38477f09f5 100644 --- a/src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs +++ b/src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs @@ -22,5 +22,30 @@ namespace SixLabors.ImageSharp.Formats.Tga return false; } + + /// + /// Checks, if the image type has valid value. + /// + /// The image type. + /// true, if its a valid tga image type. + public static bool IsValid(this TgaImageType imageType) + { + byte imageTypeVal = (byte)imageType; + + switch (imageTypeVal) + { + case 0: + case 1: + case 2: + case 3: + case 9: + case 10: + case 11: + return true; + + default: + return false; + } + } } }