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§
source§impl<'a> Reader<'a>
impl<'a> Reader<'a>
sourcepub fn new<R, M>(inner: R, mode: M) -> Self
👎Deprecated: Use Reader::from_reader. new
will be removed in version 2.0
pub fn new<R, M>(inner: R, mode: M) -> Self
new
will be removed in version 2.0Constructs 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_buffered_reader<R, M>(reader: R, mode: M) -> Result<Self>
pub fn from_buffered_reader<R, M>(reader: R, mode: M) -> Result<Self>
Constructs a new Reader
from the given BufferedReader
.
sourcepub fn from_reader<R, M>(reader: R, mode: M) -> Self
pub fn from_reader<R, M>(reader: R, mode: M) -> Self
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_bytes<M>(bytes: &'a [u8], mode: M) -> Self
pub fn from_bytes<M>(bytes: &'a [u8], mode: M) -> Self
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§
source§impl BufferedReader<Cookie> for Reader<'_>
impl BufferedReader<Cookie> for Reader<'_>
source§fn data(&mut self, amount: usize) -> Result<&[u8]>
fn data(&mut self, amount: usize) -> Result<&[u8]>
amount
bytes
of data, and returns it. Read moresource§fn data_hard(&mut self, amount: usize) -> Result<&[u8]>
fn data_hard(&mut self, amount: usize) -> Result<&[u8]>
BufferedReader::data
, but returns an error if there is not at least
amount
bytes available. Read moresource§fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8]>
fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8]>
BufferedReader::data_hard
and BufferedReader::consume
. Read moresource§fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<Cookie>>
fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<Cookie>>
BufferedReader
, if
any. Read moresource§fn get_ref(&self) -> Option<&dyn BufferedReader<Cookie>>
fn get_ref(&self) -> Option<&dyn BufferedReader<Cookie>>
BufferedReader
, if any.source§fn 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,
BufferedReader
’s cookie and returns the old value.BufferedReader
’s cookie.BufferedReader
’s cookie.source§fn data_eof(&mut self) -> Result<&[u8], Error>
fn data_eof(&mut self) -> Result<&[u8], Error>
BufferedReader::data
, this does not
actually consume the data that is read. Read moresource§fn consummated(&mut self) -> bool
fn consummated(&mut self) -> bool
source§fn read_be_u16(&mut self) -> Result<u16, Error>
fn read_be_u16(&mut self) -> Result<u16, Error>
source§fn read_be_u32(&mut self) -> Result<u32, Error>
fn read_be_u32(&mut self) -> Result<u32, Error>
source§fn read_to(&mut self, terminal: u8) -> Result<&[u8], Error>
fn read_to(&mut self, terminal: u8) -> Result<&[u8], Error>
terminal
is encountered or EOF. Read moresource§fn drop_until(&mut self, terminals: &[u8]) -> Result<usize, Error>
fn drop_until(&mut self, terminals: &[u8]) -> Result<usize, Error>
source§fn 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>
terminals
is
encountered. Read moresource§fn steal(&mut self, amount: usize) -> Result<Vec<u8>, Error>
fn steal(&mut self, amount: usize) -> Result<Vec<u8>, Error>
BufferedReader::data_consume_hard
, but returns the data in a
caller-owned buffer. Read moresource§fn steal_eof(&mut self) -> Result<Vec<u8>, Error>
fn steal_eof(&mut self) -> Result<Vec<u8>, Error>
BufferedReader::steal
, but instead of stealing a fixed number of
bytes, steals all of the data until the end of file.source§fn drop_eof(&mut self) -> Result<bool, Error>
fn drop_eof(&mut self) -> Result<bool, Error>
BufferedReader::steal_eof
, but instead of returning the data, the
data is discarded. Read moresource§fn copy(&mut self, sink: &mut dyn Write) -> Result<u64, Error>
fn copy(&mut self, sink: &mut dyn Write) -> Result<u64, Error>
source§fn dump(&self, sink: &mut dyn Write) -> Result<(), Error>where
Self: Sized,
fn dump(&self, sink: &mut dyn Write) -> Result<(), Error>where
Self: Sized,
source§fn into_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a>where
Self: Sized + 'a,
fn into_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a>where
Self: Sized + 'a,
source§impl Read for Reader<'_>
impl Read for Reader<'_>
source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
fn read(&mut self, buf: &mut [u8]) -> Result<usize>
1.36.0 · source§fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read
, except that it reads into a slice of buffers. Read moresource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector
)1.0.0 · source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf
. Read more1.0.0 · source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf
. Read more1.6.0 · source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf
. Read moresource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf
)cursor
. Read more1.0.0 · source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read
. Read more