Skip to content

Commit

Permalink
Read to end of file if a RST section is terminated before end of file.
Browse files Browse the repository at this point in the history
  • Loading branch information
oozcitak committed Mar 19, 2018
1 parent b3573e1 commit 0f7781d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 39 deletions.
70 changes: 32 additions & 38 deletions ExifLibrary/JPEGFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,7 @@ protected internal JPEGFile(Stream stream, Encoding encoding)
long length = (long)BitConverterEx.BigEndian.ToUInt16(lengthbytes, 0);

// Read section header.
header = new byte[length - 2];
int bytestoread = header.Length;
while (bytestoread > 0)
{
int count = Math.Min(bytestoread, 4 * 1024);
int bytesread = stream.Read(header, header.Length - bytestoread, count);
if (bytesread == 0)
throw new NotValidJPEGFileException();
bytestoread -= bytesread;
}
header = ReadBuffer(stream, length - 2);
}

// Start of Scan (SOS) sections and RST sections are immediately
Expand All @@ -110,37 +101,32 @@ protected internal JPEGFile(Stream stream, Encoding encoding)
{
nextbyte = stream.ReadByte();
if (nextbyte == -1)
throw new NotValidJPEGFileException();
break;
} while ((byte)nextbyte != 0xFF);

// Skip filler bytes (0xFF)
do
{
nextbyte = stream.ReadByte();
if (nextbyte == -1)
throw new NotValidJPEGFileException();
break;
} while ((byte)nextbyte == 0xFF);

// Looks like a section marker. The next byte must not be 0x00.
if ((byte)nextbyte != 0x00)
// We either reached the end of file before a new marker(this would indicate
// a corrupt image file) or we are at a section marker. In that case the
// next byte must not be 0x00.
if (nextbyte != 0)
{
// We reached a section marker. Calculate the
// length of the entropy coded data.
stream.Seek(-2, SeekOrigin.Current);
// If we reached a section marker seek back to just before the marker.
if (nextbyte != -1)
stream.Seek(-2, SeekOrigin.Current);

// Calculate the length of the entropy coded data.
long edlength = stream.Position - position;
stream.Seek(-edlength, SeekOrigin.Current);
stream.Seek(position, SeekOrigin.Begin);

// Read entropy coded data
entropydata = new byte[edlength];
int bytestoread = entropydata.Length;
while (bytestoread > 0)
{
int count = Math.Min(bytestoread, 4 * 1024);
int bytesread = stream.Read(entropydata, entropydata.Length - bytestoread, count);
if (bytesread == 0)
throw new NotValidJPEGFileException();
bytestoread -= bytesread;
}
entropydata = ReadBuffer(stream, edlength);

break;
}
Expand All @@ -154,16 +140,8 @@ protected internal JPEGFile(Stream stream, Encoding encoding)
// Some propriety formats store data past the EOI marker
if (marker == JPEGMarker.EOI)
{
int bytestoread = (int)(stream.Length - stream.Position);
TrailingData = new byte[bytestoread];
while (bytestoread > 0)
{
int count = (int)Math.Min(bytestoread, 4 * 1024);
int bytesread = stream.Read(TrailingData, TrailingData.Length - bytestoread, count);
if (bytesread == 0)
throw new NotValidJPEGFileException();
bytestoread -= bytesread;
}
long eoflength = stream.Length - stream.Position;
TrailingData = ReadBuffer(stream, eoflength);
}
}

Expand Down Expand Up @@ -956,6 +934,22 @@ private void WriteIFD(MemoryStream stream, Dictionary<ExifTag, ExifProperty> ifd
}
}
}

private byte[] ReadBuffer(Stream stream, long length)
{
byte[] buffer = new byte[length];
long bytestoread = length;
while (bytestoread > 0)
{
// Read in chunks of 4K bytes
int count = (int)Math.Min(bytestoread, 4 * 1024);
int bytesread = stream.Read(buffer, (int)(length - bytestoread), count);
if (bytesread == 0)
throw new NotValidJPEGFileException();
bytestoread -= bytesread;
}
return buffer;
}
#endregion
}
}
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 1.0.{build}
version: 1.1.{build}
os: Visual Studio 2017
skip_tags: true
configuration: Release
Expand Down

0 comments on commit 0f7781d

Please sign in to comment.