png/decoder/
read_decoder.rs

1use super::stream::{
2    DecodeOptions, Decoded, DecodingError, FormatErrorInner, StreamingDecoder, CHUNK_BUFFER_SIZE,
3};
4use super::Limits;
5
6use std::io::{BufRead, BufReader, ErrorKind, Read};
7
8use crate::chunk;
9use crate::common::Info;
10
11/// Helper for encapsulating reading input from `Read` and feeding it into a `StreamingDecoder`
12/// while hiding low-level `Decoded` events and only exposing a few high-level reading operations
13/// like:
14///
15/// * `read_header_info` - reading until `IHDR` chunk
16/// * `read_until_image_data` - reading until `IDAT` / `fdAT` sequence
17/// * `decode_image_data` - reading from `IDAT` / `fdAT` sequence into `Vec<u8>`
18/// * `finish_decoding_image_data()` - discarding remaining data from `IDAT` / `fdAT` sequence
19/// * `read_until_end_of_input()` - reading until `IEND` chunk
20pub(crate) struct ReadDecoder<R: Read> {
21    reader: BufReader<R>,
22    decoder: StreamingDecoder,
23}
24
25impl<R: Read> ReadDecoder<R> {
26    pub fn new(r: R) -> Self {
27        Self {
28            reader: BufReader::with_capacity(CHUNK_BUFFER_SIZE, r),
29            decoder: StreamingDecoder::new(),
30        }
31    }
32
33    pub fn with_options(r: R, options: DecodeOptions) -> Self {
34        let mut decoder = StreamingDecoder::new_with_options(options);
35        decoder.limits = Limits::default();
36
37        Self {
38            reader: BufReader::with_capacity(CHUNK_BUFFER_SIZE, r),
39            decoder,
40        }
41    }
42
43    pub fn set_limits(&mut self, limits: Limits) {
44        self.decoder.limits = limits;
45    }
46
47    pub fn reserve_bytes(&mut self, bytes: usize) -> Result<(), DecodingError> {
48        self.decoder.limits.reserve_bytes(bytes)
49    }
50
51    pub fn set_ignore_text_chunk(&mut self, ignore_text_chunk: bool) {
52        self.decoder.set_ignore_text_chunk(ignore_text_chunk);
53    }
54
55    pub fn set_ignore_iccp_chunk(&mut self, ignore_iccp_chunk: bool) {
56        self.decoder.set_ignore_iccp_chunk(ignore_iccp_chunk);
57    }
58
59    pub fn ignore_checksums(&mut self, ignore_checksums: bool) {
60        self.decoder.set_ignore_adler32(ignore_checksums);
61        self.decoder.set_ignore_crc(ignore_checksums);
62    }
63
64    /// Returns the next decoded chunk. If the chunk is an ImageData chunk, its contents are written
65    /// into image_data.
66    fn decode_next(&mut self, image_data: &mut Vec<u8>) -> Result<Decoded, DecodingError> {
67        let (consumed, result) = {
68            let buf = self.reader.fill_buf()?;
69            if buf.is_empty() {
70                return Err(DecodingError::IoError(ErrorKind::UnexpectedEof.into()));
71            }
72            self.decoder.update(buf, image_data)?
73        };
74        self.reader.consume(consumed);
75        Ok(result)
76    }
77
78    fn decode_next_without_image_data(&mut self) -> Result<Decoded, DecodingError> {
79        // This is somewhat ugly. The API requires us to pass a buffer to decode_next but we
80        // know that we will stop before reading any image data from the stream. Thus pass an
81        // empty buffer and assert that remains empty.
82        let mut buf = Vec::new();
83        let state = self.decode_next(&mut buf)?;
84        assert!(buf.is_empty());
85        Ok(state)
86    }
87
88    fn decode_next_and_discard_image_data(&mut self) -> Result<Decoded, DecodingError> {
89        let mut to_be_discarded = Vec::new();
90        self.decode_next(&mut to_be_discarded)
91    }
92
93    /// Reads until the end of `IHDR` chunk.
94    ///
95    /// Prerequisite: None (idempotent).
96    pub fn read_header_info(&mut self) -> Result<&Info<'static>, DecodingError> {
97        while self.info().is_none() {
98            if let Decoded::ImageEnd = self.decode_next_without_image_data()? {
99                unreachable!()
100            }
101        }
102        Ok(self.info().unwrap())
103    }
104
105    /// Reads until the start of the next `IDAT` or `fdAT` chunk.
106    ///
107    /// Prerequisite: **Not** within `IDAT` / `fdAT` chunk sequence.
108    pub fn read_until_image_data(&mut self) -> Result<(), DecodingError> {
109        loop {
110            match self.decode_next_without_image_data()? {
111                Decoded::ChunkBegin(_, chunk::IDAT) | Decoded::ChunkBegin(_, chunk::fdAT) => break,
112                Decoded::ImageEnd => {
113                    return Err(DecodingError::Format(
114                        FormatErrorInner::MissingImageData.into(),
115                    ))
116                }
117                // Ignore all other chunk events. Any other chunk may be between IDAT chunks, fdAT
118                // chunks and their control chunks.
119                _ => {}
120            }
121        }
122        Ok(())
123    }
124
125    /// Reads `image_data` and reports whether there may be additional data afterwards (i.e. if it
126    /// is okay to call `decode_image_data` and/or `finish_decoding_image_data` again)..
127    ///
128    /// Prerequisite: Input is currently positioned within `IDAT` / `fdAT` chunk sequence.
129    pub fn decode_image_data(
130        &mut self,
131        image_data: &mut Vec<u8>,
132    ) -> Result<ImageDataCompletionStatus, DecodingError> {
133        match self.decode_next(image_data)? {
134            Decoded::ImageData => Ok(ImageDataCompletionStatus::ExpectingMoreData),
135            Decoded::ImageDataFlushed => Ok(ImageDataCompletionStatus::Done),
136            // Ignore other events that may happen within an `IDAT` / `fdAT` chunks sequence.
137            Decoded::Nothing
138            | Decoded::ChunkComplete(_, _)
139            | Decoded::ChunkBegin(_, _)
140            | Decoded::PartialChunk(_) => Ok(ImageDataCompletionStatus::ExpectingMoreData),
141            // Other kinds of events shouldn't happen, unless we have been (incorrectly) called
142            // when outside of a sequence of `IDAT` / `fdAT` chunks.
143            unexpected => unreachable!("{:?}", unexpected),
144        }
145    }
146
147    /// Consumes and discards the rest of an `IDAT` / `fdAT` chunk sequence.
148    ///
149    /// Prerequisite: Input is currently positioned within `IDAT` / `fdAT` chunk sequence.
150    pub fn finish_decoding_image_data(&mut self) -> Result<(), DecodingError> {
151        loop {
152            let mut to_be_discarded = vec![];
153            if let ImageDataCompletionStatus::Done = self.decode_image_data(&mut to_be_discarded)? {
154                return Ok(());
155            }
156        }
157    }
158
159    /// Reads until the `IEND` chunk.
160    ///
161    /// Prerequisite: `IEND` chunk hasn't been reached yet.
162    pub fn read_until_end_of_input(&mut self) -> Result<(), DecodingError> {
163        while !matches!(
164            self.decode_next_and_discard_image_data()?,
165            Decoded::ImageEnd
166        ) {}
167        Ok(())
168    }
169
170    pub fn info(&self) -> Option<&Info<'static>> {
171        self.decoder.info.as_ref()
172    }
173}
174
175#[derive(Debug, Eq, PartialEq)]
176pub(crate) enum ImageDataCompletionStatus {
177    ExpectingMoreData,
178    Done,
179}