Browse Source

Add check for valid tga image type in the format detector

af/merge-core
Brian Popow 7 years ago
parent
commit
340af92134
  1. 5
      src/ImageSharp/Formats/Tga/TgaConstants.cs
  2. 2
      src/ImageSharp/Formats/Tga/TgaFileHeader.cs
  3. 11
      src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs
  4. 25
      src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs

5
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. /// The list of file extensions that equate to a targa file.
/// </summary> /// </summary>
public static readonly IEnumerable<string> FileExtensions = new[] { "tga", "vda", "icb", "vst" }; public static readonly IEnumerable<string> FileExtensions = new[] { "tga", "vda", "icb", "vst" };
/// <summary>
/// The file header length of a tga image in bytes.
/// </summary>
public const int FileHeaderLength = 18;
} }
} }

2
src/ImageSharp/Formats/Tga/TgaFileHeader.cs

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
/// <summary> /// <summary>
/// Defines the size of the data structure in the targa file. /// Defines the size of the data structure in the targa file.
/// </summary> /// </summary>
public const int Size = 18; public const int Size = TgaConstants.FileHeaderLength;
public TgaFileHeader( public TgaFileHeader(
byte idLength, byte idLength,

11
src/ImageSharp/Formats/Tga/TgaImageFormatDetector.cs

@ -11,7 +11,7 @@ namespace SixLabors.ImageSharp.Formats.Tga
public sealed class TgaImageFormatDetector : IImageFormatDetector public sealed class TgaImageFormatDetector : IImageFormatDetector
{ {
/// <inheritdoc/> /// <inheritdoc/>
public int HeaderSize => 18; public int HeaderSize => TgaConstants.FileHeaderLength;
/// <inheritdoc/> /// <inheritdoc/>
public IImageFormat DetectFormat(ReadOnlySpan<byte> header) public IImageFormat DetectFormat(ReadOnlySpan<byte> header)
@ -21,7 +21,14 @@ namespace SixLabors.ImageSharp.Formats.Tga
private bool IsSupportedFileFormat(ReadOnlySpan<byte> header) private bool IsSupportedFileFormat(ReadOnlySpan<byte> 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;
} }
} }
} }

25
src/ImageSharp/Formats/Tga/TgaImageTypeExtensions.cs

@ -22,5 +22,30 @@ namespace SixLabors.ImageSharp.Formats.Tga
return false; return false;
} }
/// <summary>
/// Checks, if the image type has valid value.
/// </summary>
/// <param name="imageType">The image type.</param>
/// <returns>true, if its a valid tga image type.</returns>
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;
}
}
} }
} }

Loading…
Cancel
Save