@@ -164,38 +164,38 @@ public JpegDecoderCore(Configuration configuration, IJpegDecoderOptions options)
164164 /// <summary>
165165 /// Finds the next file marker within the byte stream.
166166 /// </summary>
167- /// <param name="marker">The buffer to read file markers to.</param>
168167 /// <param name="stream">The input stream.</param>
169- /// <returns>The <see cref="JpegFileMarker"/></returns>
170- public static JpegFileMarker FindNextFileMarker ( byte [ ] marker , BufferedReadStream stream )
168+ /// <returns>The <see cref="JpegFileMarker"/>. </returns>
169+ public static JpegFileMarker FindNextFileMarker ( BufferedReadStream stream )
171170 {
172- int value = stream . Read ( marker , 0 , 2 ) ;
173-
174- if ( value == 0 )
171+ while ( true )
175172 {
176- return new JpegFileMarker ( JpegConstants . Markers . EOI , stream . Length - 2 ) ;
177- }
173+ int b = stream . ReadByte ( ) ;
174+ if ( b == - 1 )
175+ {
176+ return new JpegFileMarker ( JpegConstants . Markers . EOI , stream . Length - 2 ) ;
177+ }
178178
179- if ( marker [ 0 ] == JpegConstants . Markers . XFF )
180- {
181- // According to Section B.1.1.2:
182- // "Any marker may optionally be preceded by any number of fill bytes, which are bytes assigned code 0xFF."
183- int m = marker [ 1 ] ;
184- while ( m == JpegConstants . Markers . XFF )
179+ // Found a marker.
180+ if ( b == JpegConstants . Markers . XFF )
185181 {
186- int suffix = stream . ReadByte ( ) ;
187- if ( suffix == - 1 )
182+ while ( b == JpegConstants . Markers . XFF )
188183 {
189- return new JpegFileMarker ( JpegConstants . Markers . EOI , stream . Length - 2 ) ;
184+ // Loop here to discard any padding FF bytes on terminating marker.
185+ b = stream . ReadByte ( ) ;
186+ if ( b == - 1 )
187+ {
188+ return new JpegFileMarker ( JpegConstants . Markers . EOI , stream . Length - 2 ) ;
189+ }
190190 }
191191
192- m = suffix ;
192+ // Found a valid marker. Exit loop
193+ if ( b is not 0 and ( < JpegConstants . Markers . RST0 or > JpegConstants . Markers . RST7 ) )
194+ {
195+ return new JpegFileMarker ( ( byte ) ( uint ) b , stream . Position - 2 ) ;
196+ }
193197 }
194-
195- return new JpegFileMarker ( ( byte ) m , stream . Position - 2 ) ;
196198 }
197-
198- return new JpegFileMarker ( marker [ 1 ] , stream . Position - 2 , true ) ;
199199 }
200200
201201 /// <inheritdoc/>
@@ -331,15 +331,12 @@ internal void ParseStream(BufferedReadStream stream, SpectralConverter spectralC
331331 JpegThrowHelper . ThrowInvalidImageContentException ( "Missing SOI marker." ) ;
332332 }
333333
334- stream . Read ( this . markerBuffer , 0 , 2 ) ;
335- byte marker = this . markerBuffer [ 1 ] ;
336- fileMarker = new JpegFileMarker ( marker , ( int ) stream . Position - 2 ) ;
334+ fileMarker = FindNextFileMarker ( stream ) ;
337335 this . QuantizationTables ??= new Block8x8F [ 4 ] ;
338336
339337 // Break only when we discover a valid EOI marker.
340338 // https://github.com/SixLabors/ImageSharp/issues/695
341- while ( fileMarker . Marker != JpegConstants . Markers . EOI
342- || ( fileMarker . Marker == JpegConstants . Markers . EOI && fileMarker . Invalid ) )
339+ while ( fileMarker . Marker != JpegConstants . Markers . EOI )
343340 {
344341 cancellationToken . ThrowIfCancellationRequested ( ) ;
345342
@@ -491,7 +488,7 @@ internal void ParseStream(BufferedReadStream stream, SpectralConverter spectralC
491488 }
492489
493490 // Read on.
494- fileMarker = FindNextFileMarker ( this . markerBuffer , stream ) ;
491+ fileMarker = FindNextFileMarker ( stream ) ;
495492 }
496493 }
497494
0 commit comments