@ -1289,12 +1289,12 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
this . itemLinks . Add ( link ) ;
this . itemLinks . Add ( link ) ;
}
}
catch ( Exception ex ) when ( linkType = = Heif4CharCode . Cdsc & & Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( linkType = = Heif4CharCode . Cdsc & & Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// A malformed descriptive link cannot change reconstructed pixels, so non-strict modes omit it.
// A malformed descriptive link cannot change reconstructed pixels, so non-strict modes omit it.
bytesRead = referenceEnd ;
bytesRead = referenceEnd ;
}
}
catch ( Exception ex ) when ( linkType ! = Heif4CharCode . Cdsc & & Image DecoderCore. ShouldIgnoreImageDataSegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( linkType ! = Heif4CharCode . Cdsc & & Heif DecoderCore. ShouldIgnoreImageDataSegmentError ( this . Options , ex ) )
{
{
// IgnoreImageData permits a malformed optional image relationship to be omitted while retaining
// IgnoreImageData permits a malformed optional image relationship to be omitted while retaining
// independently reconstructable items and thumbnail fallbacks.
// independently reconstructable items and thumbnail fallbacks.
@ -1440,7 +1440,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
iccProfile = HeifPropertyParser . ParseIccProfile ( profileData ) ;
iccProfile = HeifPropertyParser . ParseIccProfile ( profileData ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1482,7 +1482,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
pixelAspectRatio = HeifPropertyParser . ParsePixelAspectRatio ( boxBuffer ) ;
pixelAspectRatio = HeifPropertyParser . ParsePixelAspectRatio ( boxBuffer ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1555,7 +1555,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
contentLightLevel = HeifPropertyParser . ParseContentLightLevel ( boxBuffer ) ;
contentLightLevel = HeifPropertyParser . ParseContentLightLevel ( boxBuffer ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1568,7 +1568,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
masteringDisplayColorVolume = HeifPropertyParser . ParseMasteringDisplayColorVolume ( boxBuffer ) ;
masteringDisplayColorVolume = HeifPropertyParser . ParseMasteringDisplayColorVolume ( boxBuffer ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1581,7 +1581,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
contentColorVolume = HeifPropertyParser . ParseContentColorVolume ( boxBuffer ) ;
contentColorVolume = HeifPropertyParser . ParseContentColorVolume ( boxBuffer ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1594,7 +1594,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
ambientViewingEnvironment = HeifPropertyParser . ParseAmbientViewingEnvironment ( boxBuffer ) ;
ambientViewingEnvironment = HeifPropertyParser . ParseAmbientViewingEnvironment ( boxBuffer ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1607,7 +1607,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
referenceViewingEnvironment = HeifPropertyParser . ParseReferenceViewingEnvironment ( boxBuffer ) ;
referenceViewingEnvironment = HeifPropertyParser . ParseReferenceViewingEnvironment ( boxBuffer ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1620,7 +1620,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
{
{
nominalDiffuseWhite = HeifPropertyParser . ParseNominalDiffuseWhite ( boxBuffer ) ;
nominalDiffuseWhite = HeifPropertyParser . ParseNominalDiffuseWhite ( boxBuffer ) ;
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreAncillarySegmentError ( this . Options , ex ) )
{
{
// Keep the understood property index without retaining invalid ancillary metadata.
// Keep the understood property index without retaining invalid ancillary metadata.
}
}
@ -1683,7 +1683,7 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
break ;
break ;
}
}
}
}
catch ( Exception ex ) when ( Image DecoderCore. ShouldIgnoreImageDataSegmentError ( this . Options , ex ) )
catch ( Exception ex ) when ( Heif DecoderCore. ShouldIgnoreImageDataSegmentError ( this . Options , ex ) )
{
{
// Invalid image properties retain their physical association index. Typed association handling ignores
// Invalid image properties retain their physical association index. Typed association handling ignores
// the placeholder so another decodable item or the coded-image defaults can remain usable.
// the placeholder so another decodable item or the coded-image defaults can remain usable.
@ -3153,4 +3153,42 @@ internal sealed class HeifDecoderCore : ImageDecoderCore
bytesRead = terminator + 1 ;
bytesRead = terminator + 1 ;
return Encoding . UTF8 . GetString ( span [ . . terminator ] ) ;
return Encoding . UTF8 . GetString ( span [ . . terminator ] ) ;
}
}
/// <summary>
/// Determines whether a malformed ancillary box can be omitted under the selected integrity policy.
/// </summary>
public static bool ShouldIgnoreAncillarySegmentError ( DecoderOptions options , Exception exception )
= > options . SegmentIntegrityHandling is not SegmentIntegrityHandling . Strict
& & IsRecoverableBoxError ( exception ) ;
/// <summary>
/// Determines whether a malformed image-data box can be omitted under the selected integrity policy.
/// </summary>
internal static bool ShouldIgnoreImageDataSegmentError ( DecoderOptions options , Exception exception )
= > options . SegmentIntegrityHandling is SegmentIntegrityHandling . IgnoreImageData
& & IsRecoverableBoxError ( exception ) ;
/// <summary>
/// Identifies malformed-box failures eligible for the segment integrity policy.
/// </summary>
/// <param name="exception">The failure raised while reading the box.</param>
/// <returns>Whether the failure describes recoverable image content.</returns>
private static bool IsRecoverableBoxError ( Exception exception )
= > exception is ImageFormatException
or InvalidIccProfileException
or InvalidImageContentException
or InvalidOperationException
or NotSupportedException ;
/// <summary>
/// Rejects malformed image properties unless image-data errors are explicitly ignored.
/// </summary>
/// <param name="message">The description of the malformed property.</param>
private void ThrowOrIgnoreImageDataSegmentError ( string message )
{
if ( this . Options . SegmentIntegrityHandling is not SegmentIntegrityHandling . IgnoreImageData )
{
throw new InvalidImageContentException ( message ) ;
}
}
}
}