Repository navigation
Conversation
…en decoding jpegs A TEM marker (FF 01) between header segments was read as a marker with a length field, so the next segment header was consumed as its length and decoding failed. TEM stands alone like RSTn, so skip it the same way. Some producers write extraneous bytes before restart markers. Those that form a TEM or reserved marker (FF 01 to FF BF) were taken as the end of the scan, which corrupted the rest of the image or made the marker parser fail. Skip them while looking for the marker that ends a restart interval and resync at the next marker, as libjpeg does.
3 of 4 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prerequisites
Description
Scanners and other imperfect producers write JPEGs that libjpeg-based viewers open without trouble but that ImageSharp rejected or decoded wrongly. This PR fixes two such cases.
Stray TEM marker between header segments
FindNextFileMarkeralready skips strayRSTnmarkers between segments. It returnedTEM(FF 01) as an ordinary marker, soParseStreamread the next two bytes as a length field. Those two bytes were really the next segment's marker.IdentifyandDecodethen failed withInvalidImageContentException("not enough bytes to parse declared contents of the 01 marker"). ITU T.81 B.1.1.4 saysTEMstands alone with no length field, just likeRSTn, so the decoder now skips it in the same way. I added aJpegConstants.Markers.TEMconstant (internal) for this.Extraneous bytes before restart markers that form a reserved marker
Some producers write junk bytes before the
RSTnmarkers in the entropy-coded data. The decoder already handles plain junk such as00 00,FF 00or a single byte. But when the junk formed aTEMor reserved marker (FF 01toFF BF),HuffmanScanDecoder.HandleRestarttook it as the end of the scan. The rest of the image was then corrupted silently (baseline interleaved), or the marker parser failed on the junk (baseline non-interleaved and progressive). A marker in that range can never end a restart interval or a scan. SoHandleRestartnow skips such markers while it looks for the marker that ends the interval, and resyncs at the next marker, as libjpeg'sjpeg_resync_to_restartdoes. The newJpegBitReader.HasReservedMarkerhelper does the range check. The hot decoding loop is unchanged: the extra check runs only once per restart interval.Testing
JpegDecoderTests.Malformed.cs. The tests encode the input in memory withJpegEncoder, then change the bytes. No new test images.Decode_ReservedMarkerBeforeRestartMarkers_IsSkipped: putsFF 12,FF 01,FF BForFF 12 FF 34before every RST marker of a baseline interleaved, baseline non-interleaved or progressive 4:2:0 encode withRestartInterval = 1. The decoded result must match the clean file exactly.Decode_TemMarkerBetweenHeaderSegments_IsSkipped: putsFF 01after APP0.Identifymust give the right size, and the decoded result must match the clean file exactly.mainand pass with this change.b,b 00andFF bfor everyb) before the RST markers of these encodes and of an existing test image. Nothing raisedNullReferenceException. The only failures wereInvalidImageContentException, for junk that forms a real marker (SOFn, DHT, DQT, SOS, APPn and similar).Fixes #3237