@@ -2,7 +2,7 @@ use byteorder_lite::{LittleEndian, ReadBytesExt};
22use quick_error:: quick_error;
33
44use std:: collections:: HashMap ;
5- use std:: io:: { self , BufRead , BufReader , Cursor , Read , Seek } ;
5+ use std:: io:: { self , BufRead , Cursor , Read , Seek } ;
66use std:: num:: NonZeroU16 ;
77use std:: ops:: Range ;
88
@@ -385,15 +385,8 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
385385 let max_position = position + riff_size. saturating_sub ( 12 ) ;
386386 self . r . seek ( io:: SeekFrom :: Start ( position) ) ?;
387387
388- // Resist denial of service attacks by using a BufReader. In most images there
389- // should be a very small number of chunks. However, nothing prevents a malicious
390- // image from having an extremely large number of "unknown" chunks. Issuing
391- // millions of reads and seeks against the underlying reader might be very
392- // expensive.
393- let mut reader = BufReader :: with_capacity ( 64 << 10 , & mut self . r ) ;
394-
395388 while position < max_position {
396- match read_chunk_header ( & mut reader ) {
389+ match read_chunk_header ( & mut self . r ) {
397390 Ok ( ( chunk, chunk_size, chunk_size_rounded) ) => {
398391 let range = position + 8 ..position + 8 + chunk_size;
399392 position += 8 + chunk_size_rounded;
@@ -408,8 +401,8 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
408401 return Err ( DecodingError :: InvalidChunkSize ) ;
409402 }
410403
411- reader . seek_relative ( 12 ) ?;
412- let duration = reader . read_u32 :: < LittleEndian > ( ) ? & 0xffffff ;
404+ self . r . seek_relative ( 12 ) ?;
405+ let duration = self . r . read_u32 :: < LittleEndian > ( ) ? & 0xffffff ;
413406 self . loop_duration =
414407 self . loop_duration . wrapping_add ( u64:: from ( duration) ) ;
415408
@@ -419,19 +412,19 @@ impl<R: BufRead + Seek> WebPDecoder<R> {
419412 // and the spec says that lossless images SHOULD NOT contain ALPH
420413 // chunks, so we treat both as indicators of lossy images.
421414 if !self . is_lossy {
422- let ( subchunk, ..) = read_chunk_header ( & mut reader ) ?;
415+ let ( subchunk, ..) = read_chunk_header ( & mut self . r ) ?;
423416 if let WebPRiffChunk :: VP8 | WebPRiffChunk :: ALPH = subchunk {
424417 self . is_lossy = true ;
425418 }
426- reader . seek_relative ( chunk_size_rounded as i64 - 24 ) ?;
419+ self . r . seek_relative ( chunk_size_rounded as i64 - 24 ) ?;
427420 } else {
428- reader . seek_relative ( chunk_size_rounded as i64 - 16 ) ?;
421+ self . r . seek_relative ( chunk_size_rounded as i64 - 16 ) ?;
429422 }
430423
431424 continue ;
432425 }
433426
434- reader . seek_relative ( chunk_size_rounded as i64 ) ?;
427+ self . r . seek_relative ( chunk_size_rounded as i64 ) ?;
435428 }
436429 Err ( DecodingError :: IoError ( e) )
437430 if e. kind ( ) == io:: ErrorKind :: UnexpectedEof =>
@@ -885,13 +878,13 @@ pub(crate) fn range_reader<R: BufRead + Seek>(
885878 Ok ( r. take ( range. end - range. start ) )
886879}
887880
888- pub ( crate ) fn read_fourcc < R : Read > ( mut r : R ) -> Result < WebPRiffChunk , DecodingError > {
881+ pub ( crate ) fn read_fourcc < R : BufRead > ( mut r : R ) -> Result < WebPRiffChunk , DecodingError > {
889882 let mut chunk_fourcc = [ 0 ; 4 ] ;
890883 r. read_exact ( & mut chunk_fourcc) ?;
891884 Ok ( WebPRiffChunk :: from_fourcc ( chunk_fourcc) )
892885}
893886
894- pub ( crate ) fn read_chunk_header < R : Read > (
887+ pub ( crate ) fn read_chunk_header < R : BufRead > (
895888 mut r : R ,
896889) -> Result < ( WebPRiffChunk , u64 , u64 ) , DecodingError > {
897890 let chunk = read_fourcc ( & mut r) ?;
0 commit comments