Skip to content

Commit 555a0d7

Browse files
committed
Fix app1 parsing
1 parent 823e785 commit 555a0d7

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ private void ProcessApplicationHeaderMarker(BufferedReadStream stream, int remai
677677
}
678678

679679
/// <summary>
680-
/// Processes the App1 marker retrieving any stored metadata
680+
/// Processes the App1 marker retrieving any stored metadata.
681681
/// </summary>
682682
/// <param name="stream">The input stream.</param>
683683
/// <param name="remaining">The remaining bytes in the segment block.</param>
@@ -687,7 +687,7 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
687687
const int XmpMarkerLength = 29;
688688
if (remaining < ExifMarkerLength || this.IgnoreMetadata)
689689
{
690-
// Skip the application header length
690+
// Skip the application header length.
691691
stream.Skip(remaining);
692692
return;
693693
}
@@ -697,12 +697,12 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
697697
JpegThrowHelper.ThrowInvalidImageContentException("Bad App1 Marker length.");
698698
}
699699

700-
// XMP marker is the longest, so read at least that many bytes into temp.
700+
// XMP marker is the longer then the EXIF marker, so first try read the EXIF marker bytes.
701701
stream.Read(this.temp, 0, ExifMarkerLength);
702+
remaining -= ExifMarkerLength;
702703

703704
if (ProfileResolver.IsProfile(this.temp, ProfileResolver.ExifMarker))
704705
{
705-
remaining -= ExifMarkerLength;
706706
this.hasExif = true;
707707
byte[] profile = new byte[remaining];
708708
stream.Read(profile, 0, remaining);
@@ -713,7 +713,7 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
713713
}
714714
else
715715
{
716-
// If the EXIF information exceeds 64K, it will be split over multiple APP1 markers
716+
// If the EXIF information exceeds 64K, it will be split over multiple APP1 markers.
717717
this.ExtendProfile(ref this.exifData, profile);
718718
}
719719

@@ -722,9 +722,10 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
722722

723723
if (ProfileResolver.IsProfile(this.temp, ProfileResolver.XmpMarker.Slice(0, ExifMarkerLength)))
724724
{
725-
stream.Read(this.temp, 0, XmpMarkerLength - ExifMarkerLength);
726-
remaining -= XmpMarkerLength;
727-
if (ProfileResolver.IsProfile(this.temp, ProfileResolver.XmpMarker.Slice(ExifMarkerLength)))
725+
int remainingXmpMarkerBytes = XmpMarkerLength - ExifMarkerLength;
726+
stream.Read(this.temp, ExifMarkerLength, remainingXmpMarkerBytes);
727+
remaining -= remainingXmpMarkerBytes;
728+
if (ProfileResolver.IsProfile(this.temp, ProfileResolver.XmpMarker))
728729
{
729730
this.hasXmp = true;
730731
byte[] profile = new byte[remaining];
@@ -736,7 +737,7 @@ private void ProcessApp1Marker(BufferedReadStream stream, int remaining)
736737
}
737738
else
738739
{
739-
// If the XMP information exceeds 64K, it will be split over multiple APP1 markers
740+
// If the XMP information exceeds 64K, it will be split over multiple APP1 markers.
740741
this.ExtendProfile(ref this.xmpData, profile);
741742
}
742743

0 commit comments

Comments
 (0)