Struct sequoia_openpgp::armor::Reader
source · [−]pub struct Reader<'a> { /* private fields */ }
Expand description
A filter that strips ASCII Armor from a stream of data.
Implementations
sourceimpl<'a> Reader<'a>
impl<'a> Reader<'a>
sourcepub fn new<R, M>(inner: R, mode: M) -> Self where
R: 'a + Read + Send + Sync,
M: Into<Option<ReaderMode>>,
👎 Deprecated: Use Reader::from_reader. new
will be removed in version 2.0
pub fn new<R, M>(inner: R, mode: M) -> Self where
R: 'a + Read + Send + Sync,
M: Into<Option<ReaderMode>>,
Use Reader::from_reader. new
will be removed in version 2.0
Constructs a new filter for the given type of data.
This function is deprecated and will be removed in version
2.0. Please use Reader::from_reader
.
sourcepub fn from_reader<R, M>(reader: R, mode: M) -> Self where
R: 'a + Read + Send + Sync,
M: Into<Option<ReaderMode>>,
pub fn from_reader<R, M>(reader: R, mode: M) -> Self where
R: 'a + Read + Send + Sync,
M: Into<Option<ReaderMode>>,
Constructs a new Reader
from the given io::Read
er.
ASCII Armor, designed to protect OpenPGP data in transit, has been a source of problems if the armor structure is damaged. For example, copying data manually from one program to another might introduce or drop newlines.
By default, the reader operates in tolerant mode. It will ignore common formatting errors but the header and footer lines must be intact.
To select stricter mode, specify the kind argument for tolerant mode. In this mode only ASCII Armor blocks with the appropriate header are recognized.
There is also very tolerant mode that is appropriate when reading text that the user cut and pasted into a text area. This mode of operation is CPU intense, particularly on large text files.
Examples
use std::io::{self, Read};
use sequoia_openpgp as openpgp;
use openpgp::Message;
use openpgp::armor::{Reader, ReaderMode};
use openpgp::parse::Parse;
let data = "yxJiAAAAAABIZWxsbyB3b3JsZCE="; // base64 over literal data packet
let mut cursor = io::Cursor::new(&data);
let mut reader = Reader::from_reader(&mut cursor, ReaderMode::VeryTolerant);
let mut buf = Vec::new();
reader.read_to_end(&mut buf)?;
let message = Message::from_bytes(&buf)?;
assert_eq!(message.body().unwrap().body(),
b"Hello world!");
Or, in strict mode:
use std::io::{self, Result, Read};
use sequoia_openpgp as openpgp;
use openpgp::armor::{Reader, ReaderMode, Kind};
let data =
"-----BEGIN PGP ARMORED FILE-----
SGVsbG8gd29ybGQh
=s4Gu
-----END PGP ARMORED FILE-----";
let mut cursor = io::Cursor::new(&data);
let mut reader = Reader::from_reader(&mut cursor, ReaderMode::Tolerant(Some(Kind::File)));
let mut content = String::new();
reader.read_to_string(&mut content)?;
assert_eq!(content, "Hello world!");
assert_eq!(reader.kind(), Some(Kind::File));
sourcepub fn from_file<P, M>(path: P, mode: M) -> Result<Self> where
P: AsRef<Path>,
M: Into<Option<ReaderMode>>,
pub fn from_file<P, M>(path: P, mode: M) -> Result<Self> where
P: AsRef<Path>,
M: Into<Option<ReaderMode>>,
Creates a Reader
from a file.
sourcepub fn from_bytes<M>(bytes: &'a [u8], mode: M) -> Self where
M: Into<Option<ReaderMode>>,
pub fn from_bytes<M>(bytes: &'a [u8], mode: M) -> Self where
M: Into<Option<ReaderMode>>,
Creates a Reader
from a buffer.
sourcepub fn kind(&self) -> Option<Kind>
pub fn kind(&self) -> Option<Kind>
Returns the kind of data this reader is for.
Useful if the kind of data is not known in advance. If the header has not been encountered yet (try reading some data first!), this function returns None.
sourcepub fn headers(&mut self) -> Result<&[(String, String)]>
pub fn headers(&mut self) -> Result<&[(String, String)]>
Returns the armored headers.
The tuples contain a key and a value.
Note: if a key occurs multiple times, then there are multiple entries in the vector with the same key; values with the same key are not combined.
Examples
use std::io::{self, Read};
use sequoia_openpgp as openpgp;
use openpgp::armor::{Reader, ReaderMode, Kind};
let data =
"-----BEGIN PGP ARMORED FILE-----
First: value
Header: value
SGVsbG8gd29ybGQh
=s4Gu
-----END PGP ARMORED FILE-----";
let mut cursor = io::Cursor::new(&data);
let mut reader = Reader::from_reader(&mut cursor, ReaderMode::Tolerant(Some(Kind::File)));
let mut content = String::new();
reader.read_to_string(&mut content)?;
assert_eq!(reader.headers()?,
&[("First".into(), "value".into()),
("Header".into(), "value".into())]);
Trait Implementations
sourceimpl BufferedReader<Cookie> for Reader<'_>
impl BufferedReader<Cookie> for Reader<'_>
sourcefn buffer(&self) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
fn buffer(&self) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
Returns a reference to the internal buffer. Read more
sourcefn data(&mut self, amount: usize) -> Result<&[u8]>
fn data(&mut self, amount: usize) -> Result<&[u8]>
Ensures that the internal buffer has at least amount
bytes
of data, and returns it. Read more
sourcefn data_hard(&mut self, amount: usize) -> Result<&[u8]>
fn data_hard(&mut self, amount: usize) -> Result<&[u8]>
Like BufferedReader::data
, but returns an error if there is not at least
amount
bytes available. Read more
sourcefn consume(&mut self, amount: usize) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
fn consume(&mut self, amount: usize) -> &[u8]ⓘNotable traits for &[u8]impl<'_> Read for &[u8]impl<'_> Write for &mut [u8]
Consumes some of the data. Read more
sourcefn data_consume(&mut self, amount: usize) -> Result<&[u8]>
fn data_consume(&mut self, amount: usize) -> Result<&[u8]>
A convenience function that combines BufferedReader::data
and BufferedReader::consume
. Read more
sourcefn data_consume_hard(&mut self, amount: usize) -> Result<&[u8]>
fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8]>
A convenience function that effectively combines BufferedReader::data_hard
and BufferedReader::consume
. Read more
sourcefn get_mut(&mut self) -> Option<&mut dyn BufferedReader<Cookie>>
fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<Cookie>>
Returns a mutable reference to the inner BufferedReader
, if
any. Read more
sourcefn get_ref(&self) -> Option<&dyn BufferedReader<Cookie>>
fn get_ref(&self) -> Option<&dyn BufferedReader<Cookie>>
Returns a reference to the inner BufferedReader
, if any.
sourcefn into_inner<'b>(
self: Box<Self>
) -> Option<Box<dyn BufferedReader<Cookie> + 'b>> where
Self: 'b,
fn into_inner<'b>(
self: Box<Self>
) -> Option<Box<dyn BufferedReader<Cookie> + 'b>> where
Self: 'b,
Returns the underlying reader, if any. Read more
Sets the BufferedReader
’s cookie and returns the old value.
Returns a reference to the BufferedReader
’s cookie.
Returns a mutable reference to the BufferedReader
’s cookie.
sourcefn data_eof(&mut self) -> Result<&[u8], Error>
fn data_eof(&mut self) -> Result<&[u8], Error>
Returns all of the data until EOF. Like BufferedReader::data
, this does not
actually consume the data that is read. Read more
sourcefn consummated(&mut self) -> bool
fn consummated(&mut self) -> bool
Checks whether this reader is consummated. Read more
sourcefn read_be_u16(&mut self) -> Result<u16, Error>
fn read_be_u16(&mut self) -> Result<u16, Error>
A convenience function for reading a 16-bit unsigned integer in big endian format. Read more
sourcefn read_be_u32(&mut self) -> Result<u32, Error>
fn read_be_u32(&mut self) -> Result<u32, Error>
A convenience function for reading a 32-bit unsigned integer in big endian format. Read more
sourcefn read_to(&mut self, terminal: u8) -> Result<&[u8], Error>
fn read_to(&mut self, terminal: u8) -> Result<&[u8], Error>
Reads until either terminal
is encountered or EOF. Read more
sourcefn drop_until(&mut self, terminals: &[u8]) -> Result<usize, Error>
fn drop_until(&mut self, terminals: &[u8]) -> Result<usize, Error>
Discards the input until one of the bytes in terminals is encountered. Read more
sourcefn drop_through(
&mut self,
terminals: &[u8],
match_eof: bool
) -> Result<(Option<u8>, usize), Error>
fn drop_through(
&mut self,
terminals: &[u8],
match_eof: bool
) -> Result<(Option<u8>, usize), Error>
Discards the input until one of the bytes in terminals
is
encountered. Read more
sourcefn steal(&mut self, amount: usize) -> Result<Vec<u8, Global>, Error>
fn steal(&mut self, amount: usize) -> Result<Vec<u8, Global>, Error>
Like BufferedReader::data_consume_hard
, but returns the data in a
caller-owned buffer. Read more
sourcefn steal_eof(&mut self) -> Result<Vec<u8, Global>, Error>
fn steal_eof(&mut self) -> Result<Vec<u8, Global>, Error>
Like BufferedReader::steal
, but instead of stealing a fixed number of
bytes, steals all of the data until the end of file. Read more
sourcefn drop_eof(&mut self) -> Result<bool, Error>
fn drop_eof(&mut self) -> Result<bool, Error>
Like BufferedReader::steal_eof
, but instead of returning the data, the
data is discarded. Read more
sourcefn copy(&mut self, sink: &mut dyn Write) -> Result<u64, Error>
fn copy(&mut self, sink: &mut dyn Write) -> Result<u64, Error>
Copies data to the given writer returning the copied amount. Read more
sourcefn dump(&self, sink: &mut dyn Write) -> Result<(), Error>
fn dump(&self, sink: &mut dyn Write) -> Result<(), Error>
A helpful debugging aid to pretty print a Buffered Reader stack. Read more
sourcefn into_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a, Global>ⓘNotable traits for Box<R, Global>impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
where
Self: 'a,
fn into_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a, Global>ⓘNotable traits for Box<R, Global>impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
where
Self: 'a,
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
Boxes the reader.
sourcefn as_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a, Global>ⓘNotable traits for Box<R, Global>impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
where
Self: 'a,
fn as_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a, Global>ⓘNotable traits for Box<R, Global>impl<R> Read for Box<R, Global> where
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
where
Self: 'a,
R: Read + ?Sized, impl<W> Write for Box<W, Global> where
W: Write + ?Sized, impl<I, A> Iterator for Box<I, A> where
I: Iterator + ?Sized,
A: Allocator, type Item = <I as Iterator>::Item;impl<F, A> Future for Box<F, A> where
F: Future + Unpin + ?Sized,
A: Allocator + 'static, type Output = <F as Future>::Output;
Use into_boxed
Boxes the reader.
sourceimpl Read for Reader<'_>
impl Read for Reader<'_>
sourcefn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · sourcefn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
Like read
, except that it reads into a slice of buffers. Read more
sourcefn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)Determines if this Read
er has an efficient read_vectored
implementation. Read more
1.0.0 · sourcefn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8, Global>) -> Result<usize, Error>
Read all bytes until EOF in this source, placing them into buf
. Read more
1.0.0 · sourcefn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
Read all bytes until EOF in this source, appending them to buf
. Read more
1.6.0 · sourcefn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
Read the exact number of bytes required to fill buf
. Read more
sourcefn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Pull some bytes from this source into the specified buffer. Read more
sourcefn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, buf: &mut ReadBuf<'_>) -> Result<(), Error>
read_buf
)Read the exact number of bytes required to fill buf
. Read more
1.0.0 · sourcefn by_ref(&mut self) -> &mut Self
fn by_ref(&mut self) -> &mut Self
Creates a “by reference” adaptor for this instance of Read
. Read more
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Reader<'a>
impl<'a> Send for Reader<'a>
impl<'a> Sync for Reader<'a>
impl<'a> Unpin for Reader<'a>
impl<'a> !UnwindSafe for Reader<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more