Browse Source

Read header from stack allocated buffer

pull/2633/head
Ynse Hoornenborg 2 years ago
parent
commit
de392d8c91
  1. 22
      src/ImageSharp/Formats/Heif/HeifDecoderCore.cs

22
src/ImageSharp/Formats/Heif/HeifDecoderCore.cs

@ -4,6 +4,7 @@
using System.Buffers; using System.Buffers;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Text; using System.Text;
using SixLabors.ImageSharp.Formats.Webp;
using SixLabors.ImageSharp.IO; using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata;
@ -166,7 +167,13 @@ internal sealed class HeifDecoderCore : IImageDecoderInternals
private long ReadBoxHeader(BufferedReadStream stream, out Heif4CharCode boxType) private long ReadBoxHeader(BufferedReadStream stream, out Heif4CharCode boxType)
{ {
// Read 4 bytes of length of box // Read 4 bytes of length of box
Span<byte> buf = this.ReadIntoBuffer(stream, 8); Span<byte> buf = stackalloc byte[8];
int bytesRead = stream.Read(buf);
if (bytesRead != 8)
{
throw new InvalidImageContentException("Not enough data to read the Box header");
}
long boxSize = BinaryPrimitives.ReadUInt32BigEndian(buf); long boxSize = BinaryPrimitives.ReadUInt32BigEndian(buf);
long headerSize = 8; long headerSize = 8;
@ -175,7 +182,12 @@ internal sealed class HeifDecoderCore : IImageDecoderInternals
if (boxSize == 1) if (boxSize == 1)
{ {
buf = this.ReadIntoBuffer(stream, 8); bytesRead = stream.Read(buf);
if (bytesRead != 8)
{
throw new InvalidImageContentException("Not enough data to read the Large Box header");
}
boxSize = (long)BinaryPrimitives.ReadUInt64BigEndian(buf); boxSize = (long)BinaryPrimitives.ReadUInt64BigEndian(buf);
headerSize += 8; headerSize += 8;
} }
@ -638,13 +650,13 @@ internal sealed class HeifDecoderCore : IImageDecoderInternals
{ {
if (length <= this.buffer.Length) if (length <= this.buffer.Length)
{ {
stream.Read(this.buffer, 0, (int)length); int bytesRead = stream.Read(this.buffer, 0, (int)length);
return this.buffer; return this.buffer.AsSpan(0, bytesRead);
} }
else else
{ {
Span<byte> temp = new byte[length]; Span<byte> temp = new byte[length];
stream.Read(temp); int bytesRead = stream.Read(temp);
return temp; return temp;
} }
} }

Loading…
Cancel
Save