pub fn poll_read_decrypted<R>(
        &mut self,
        ctx: &mut Context<'_>,
        r: &mut R,
        dst: &mut ReadBuf,
    ) -> Poll<io::Result<()>>
    where
        R: AsyncRead + Unpin,
    {
        while !self.buffered || self.pos >= self.buffer.len() {
            // Refill buffer
            match self.steps {
                DecryptReadStep::Init => {
                    // Cleanup buffer and ready for refill
                    self.buffer.clear();
                    self.pos = 0;
                    self.buffered = false;

                    let required_space = 2 + self.tag_size;

                    if dst.remaining() >= required_space {
                        // dst has enough space, use it directly
                        self.steps = DecryptReadStep::LengthReadBuf;
                        self.read_buf_filled = 0;
                    } else {
                        self.buffer.reserve(required_space);
                        self.steps = DecryptReadStep::LengthBuffered;
                    }
                }
                DecryptReadStep::LengthReadBuf => {
                    // Reset the ReadBuf offset
                    let filled = dst.filled().len();
                    if filled < self.read_buf_filled {
                        let diff = self.read_buf_filled - filled;
                        unsafe { dst.assume_init(diff) };
                        dst.set_filled(self.read_buf_filled);
                    }

                    match self.poll_read_decrypted_length_readbuf(ctx, r, dst) {
                        Poll::Ready(Ok(plen)) => {
                            // First of all, reset the dst buffer to the original length
                            dst.clear();

                            // Next step, read data
                            // Check if dst has enough space for reading data
                            let required_space = plen + self.tag_size;
                            if dst.remaining() >= required_space {
                                // Fast path, use dst as buffer
                                self.steps = DecryptReadStep::DataReadBuf(plen);
                                self.read_buf_filled = 0;
                            } else {
                                // Slow path, use self.buffer as buffer
                                self.buffer.clear();

                                self.steps = DecryptReadStep::DataBuffered(plen);
                                self.buffer.reserve(required_space);
                            }
                        }
                        Poll::Ready(Err(err)) => {
                            if err.kind() == ErrorKind::UnexpectedEof && dst.filled().len() == 0 {
                                self.steps = DecryptReadStep::Eof;
                            } else {
                                return Poll::Ready(Err(err));
                            }
                        }
                        Poll::Pending => {
                            // Record offset
                            //
                            // FIXME: This is unsafe. If caller clears or changes the buffer, everything is doomed.
                            self.read_buf_filled = dst.filled().len();
                            return Poll::Pending;
                        }
                    }
                }
                DecryptReadStep::LengthBuffered => {
                    match ready!(self.poll_read_decrypted_length_buffered(ctx, r)) {
                        Ok(plen) => {
                            // Clear buffer before overwriting it
                            self.buffer.clear();

                            // Next step, read data
                            let required_space = plen + self.tag_size;
                            if dst.remaining() < required_space {
                                // Slow path, use self.buffer as buffer
                                self.steps = DecryptReadStep::DataBuffered(plen);
                                self.buffer.reserve(required_space);
                            } else {
                                // Fast path, use dst as buffer
                                //
                                // This branch is very unlikely to be executed, unless plen < 2
                                dst.clear();

                                self.steps = DecryptReadStep::DataReadBuf(plen);
                                self.read_buf_filled = 0;
                            }
                        }
                        Err(err) => {
                            if err.kind() == ErrorKind::UnexpectedEof && self.buffer.is_empty() {
                                self.steps = DecryptReadStep::Eof;
                            } else {
                                return Poll::Ready(Err(err));
                            }
                        }
                    };
                }
                DecryptReadStep::DataReadBuf(plen) => {
                    // Reset the ReadBuf offset
                    let filled = dst.filled().len();
                    if filled < self.read_buf_filled {
                        let diff = self.read_buf_filled - filled;
                        unsafe { dst.assume_init(diff) };
                        dst.set_filled(self.read_buf_filled);
                    }

                    match self.poll_read_decrypted_data_readbuf(ctx, r, plen, dst) {
                        Poll::Ready(r) => return Poll::Ready(r),
                        Poll::Pending => {
                            // Record offset
                            //
                            // FIXME: This is unsafe. If caller clears or changes the buffer, everything is doomed.
                            self.read_buf_filled = dst.filled().len();
                            return Poll::Pending;
                        }
                    }
                }
                DecryptReadStep::DataBuffered(plen) => ready!(self.poll_read_decrypted_data_buffered(ctx, r, plen))?,
                DecryptReadStep::Eof => return Poll::Ready(Ok(())),
            }
        }

        let remaining_len = self.buffer.len() - self.pos;
        let n = cmp::min(dst.remaining(), remaining_len);
        dst.put_slice(&self.buffer[self.pos..self.pos + n]);
        self.pos += n;

        Poll::Ready(Ok(()))
    }
/// Attempt to read decrypted data from reader
///
/// ## Implementation Notes
///
/// `DecryptedReader` will try to use `dst` to store immediate data. Any implementations that call `poll_read_decrypted` MUST-NOT
/// modify `dst`'s underlying buffer when `Poll::Pending`.
Attempt to read decrypted data from reader
Attempt to read decrypted data from reader
pub fn poll_read_decrypted<R>(
        &mut self,
        ctx: &mut Context<'_>,
        r: &mut R,
        dst: &mut ReadBuf,
    ) -> Poll<io::Result<()>>
    where
        R: AsyncRead + Unpin,
    {
        while !self.buffered || self.pos >= self.buffer.len() {
            // Refill buffer
            match self.steps {
                DecryptReadStep::Init => {
                    // Cleanup buffer and ready for refill
                    self.buffer.clear();
                    self.pos = 0;
                    self.buffered = false;
                    let required_space = 2 + self.tag_size;
                    if dst.remaining() >= required_space {
                        // dst has enough space, use it directly
                        self.steps = DecryptReadStep::LengthReadBuf;
                        self.read_buf_filled = 0;
                    } else {
                        self.buffer.reserve(required_space);
                        self.steps = DecryptReadStep::LengthBuffered;
                    }
                }
                DecryptReadStep::LengthReadBuf => {
                    // Reset the ReadBuf offset
                    let filled = dst.filled().len();
                    if filled < self.read_buf_filled {
                        let diff = self.read_buf_filled - filled;
                        unsafe { dst.assume_init(diff) };
                        dst.set_filled(self.read_buf_filled);
                    }
                    match self.poll_read_decrypted_length_readbuf(ctx, r, dst) {
                        Poll::Ready(Ok(plen)) => {
                            // First of all, reset the dst buffer to the original length
                            dst.clear();
                            // Next step, read data
                            // Check if dst has enough space for reading data
                            let required_space = plen + self.tag_size;
                            if dst.remaining() >= required_space {
                                // Fast path, use dst as buffer
                                self.steps = DecryptReadStep::DataReadBuf(plen);
                                self.read_buf_filled = 0;
                            } else {
                                // Slow path, use self.buffer as buffer
                                self.buffer.clear();
                                self.steps = DecryptReadStep::DataBuffered(plen);
                                self.buffer.reserve(required_space);
                            }
                        }
                        Poll::Ready(Err(err)) => {
                            if err.kind() == ErrorKind::UnexpectedEof && dst.filled().len() == 0 {
                                self.steps = DecryptReadStep::Eof;
                            } else {
                                return Poll::Ready(Err(err));
                            }
                        }
                        Poll::Pending => {
                            // Record offset
                            //
                            // FIXME: This is unsafe. If caller clears or changes the buffer, everything is doomed.
                            self.read_buf_filled = dst.filled().len();
                            return Poll::Pending;
                        }
                    }
                }
                DecryptReadStep::LengthBuffered => {
                    match ready!(self.poll_read_decrypted_length_buffered(ctx, r)) {
                        Ok(plen) => {
                            // Clear buffer before overwriting it
                            self.buffer.clear();
                            // Next step, read data
                            let required_space = plen + self.tag_size;
                            if dst.remaining() < required_space {
                                // Slow path, use self.buffer as buffer
                                self.steps = DecryptReadStep::DataBuffered(plen);
                                self.buffer.reserve(required_space);
                            } else {
                                // Fast path, use dst as buffer
                                //
                                // This branch is very unlikely to be executed, unless plen < 2
                                dst.clear();
                                self.steps = DecryptReadStep::DataReadBuf(plen);
                                self.read_buf_filled = 0;
                            }
                        }
                        Err(err) => {
                            if err.kind() == ErrorKind::UnexpectedEof && self.buffer.is_empty() {
                                self.steps = DecryptReadStep::Eof;
                            } else {
                                return Poll::Ready(Err(err));
                            }
                        }
                    };
                }
                DecryptReadStep::DataReadBuf(plen) => {
                    // Reset the ReadBuf offset
                    let filled = dst.filled().len();
                    if filled < self.read_buf_filled {
                        let diff = self.read_buf_filled - filled;
                        unsafe { dst.assume_init(diff) };
                        dst.set_filled(self.read_buf_filled);
                    }
                    match self.poll_read_decrypted_data_readbuf(ctx, r, plen, dst) {
                        Poll::Ready(r) => return Poll::Ready(r),
                        Poll::Pending => {
                            // Record offset
                            //
                            // FIXME: This is unsafe. If caller clears or changes the buffer, everything is doomed.
                            self.read_buf_filled = dst.filled().len();
                            return Poll::Pending;
                        }
                    }
                }
                DecryptReadStep::DataBuffered(plen) => ready!(self.poll_read_decrypted_data_buffered(ctx, r, plen))?,
                DecryptReadStep::Eof => return Poll::Ready(Ok(())),
            }
        }
        let remaining_len = self.buffer.len() - self.pos;
        let n = cmp::min(dst.remaining(), remaining_len);
        dst.put_slice(&self.buffer[self.pos..self.pos + n]);
        self.pos += n;
        Poll::Ready(Ok(()))
    }
pub fn poll_read_decrypted < R > ( & mut self , ctx : & mut Context < ' _ > , r : & mut R , dst : & mut ReadBuf , ) -> Poll < io :: Result < ( ) > > where R : AsyncRead + Unpin , { while ! self . buffered || self . pos >= self . buffer . len ( ) { // Refill buffer match self . steps { DecryptReadStep :: Init => { // Cleanup buffer and ready for refill self . buffer . clear ( ) ; self . pos = 0 ; self . buffered = false ; let required_space = 2 + self . tag_size ; if dst . remaining ( ) >= required_space { // dst has enough space, use it directly self . steps = DecryptReadStep :: LengthReadBuf ; self . read_buf_filled = 0 ; } else { self . buffer . reserve ( required_space ) ; self . steps = DecryptReadStep :: LengthBuffered ; } } DecryptReadStep :: LengthReadBuf => { // Reset the ReadBuf offset let filled = dst . filled ( ) . len ( ) ; if filled < self . read_buf_filled { let diff = self . read_buf_filled - filled ; unsafe { dst . assume_init ( diff ) } ; dst . set_filled ( self . read_buf_filled ) ; } match self . poll_read_decrypted_length_readbuf ( ctx , r , dst ) { Poll :: Ready ( Ok ( plen ) ) => { // First of all, reset the dst buffer to the original length dst . clear ( ) ; // Next step, read data // Check if dst has enough space for reading data let required_space = plen + self . tag_size ; if dst . remaining ( ) >= required_space { // Fast path, use dst as buffer self . steps = DecryptReadStep :: DataReadBuf ( plen ) ; self . read_buf_filled = 0 ; } else { // Slow path, use self.buffer as buffer self . buffer . clear ( ) ; self . steps = DecryptReadStep :: DataBuffered ( plen ) ; self . buffer . reserve ( required_space ) ; } } Poll :: Ready ( Err ( err ) ) => { if err . kind ( ) == ErrorKind :: UnexpectedEof && dst . filled ( ) . len ( ) == 0 { self . steps = DecryptReadStep :: Eof ; } else { return Poll :: Ready ( Err ( err ) ) ; } } Poll :: Pending => { // Record offset // // FIXME: This is unsafe. If caller clears or changes the buffer, everything is doomed. self . read_buf_filled = dst . filled ( ) . len ( ) ; return Poll :: Pending ; } } } DecryptReadStep :: LengthBuffered => { match ready ! ( self poll_read_decrypted_length_buffered ( ctx r ) ) { Ok ( plen ) => { // Clear buffer before overwriting it self . buffer . clear ( ) ; // Next step, read data let required_space = plen + self . tag_size ; if dst . remaining ( ) < required_space { // Slow path, use self.buffer as buffer self . steps = DecryptReadStep :: DataBuffered ( plen ) ; self . buffer . reserve ( required_space ) ; } else { // Fast path, use dst as buffer // // This branch is very unlikely to be executed, unless plen < 2 dst . clear ( ) ; self . steps = DecryptReadStep :: DataReadBuf ( plen ) ; self . read_buf_filled = 0 ; } } Err ( err ) => { if err . kind ( ) == ErrorKind :: UnexpectedEof && self . buffer . is_empty ( ) { self . steps = DecryptReadStep :: Eof ; } else { return Poll :: Ready ( Err ( err ) ) ; } } } ; } DecryptReadStep :: DataReadBuf ( plen ) => { // Reset the ReadBuf offset let filled = dst . filled ( ) . len ( ) ; if filled < self . read_buf_filled { let diff = self . read_buf_filled - filled ; unsafe { dst . assume_init ( diff ) } ; dst . set_filled ( self . read_buf_filled ) ; } match self . poll_read_decrypted_data_readbuf ( ctx , r , plen , dst ) { Poll :: Ready ( r ) => return Poll :: Ready ( r ) , Poll :: Pending => { // Record offset // // FIXME: This is unsafe. If caller clears or changes the buffer, everything is doomed. self . read_buf_filled = dst . filled ( ) . len ( ) ; return Poll :: Pending ; } } } DecryptReadStep :: DataBuffered ( plen ) => ready ! ( self poll_read_decrypted_data_buffered ( ctx r plen ) ) ? , DecryptReadStep :: Eof => return Poll :: Ready ( Ok ( ( ) ) ) , } } let remaining_len = self . buffer . len ( ) - self . pos ; let n = cmp :: min ( dst . remaining ( ) , remaining_len ) ; dst . put_slice ( & self . buffer [ self . pos .. self . pos + n ] ) ; self . pos += n ; Poll :: Ready ( Ok ( ( ) ) ) }
Attempt to read decrypted data from reader
Attempt to read decrypted data from reader
&mut self
ctx
&mut Context<'_>
r
&mut R
dst
&mut ReadBuf
implementation notes
`DecryptedReader` will try to use `dst` to store immediate data. Any implementations that call `poll_read_decrypted` MUST-NOT
modify `dst`'s underlying buffer when `Poll::Pending`.
` DecryptedReader ` will try to use ` dst ` to store immediate data . Any implementations that call ` poll_read_decrypted ` MUST - NOT modify ` dst ` ' s underlying buffer when ` Poll :: Pending ` .