Skip to main content

Reader

Struct 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>

Source

pub fn from_buffered_reader<R, M>( reader: R, mode: M, ) -> Result<Reader<'a>, Error>

Constructs a new Reader from the given BufferedReader.

Source

pub fn from_reader<R, M>(reader: R, mode: M) -> Reader<'a> ⓘ
where R: 'a + Read + Send + Sync, M: Into<Option<ReaderMode>>,

Constructs a new Reader from the given io::Reader.

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));
Source

pub fn from_file<P, M>(path: P, mode: M) -> Result<Reader<'a>, Error>
where P: AsRef<Path>, M: Into<Option<ReaderMode>>,

Creates a Reader from a file.

Source

pub fn from_bytes<M>(bytes: &'a [u8], mode: M) -> Reader<'a> ⓘ
where M: Into<Option<ReaderMode>>,

Creates a Reader from a buffer.

Source

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.

Source

pub fn headers(&mut self) -> Result<&[(String, String)], Error>

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<'_>

Source§

fn buffer(&self) -> &[u8] ⓘ

Returns a reference to the internal buffer. Read more
Source§

fn data(&mut self, amount: usize) -> Result<&[u8], Error>

Ensures that the internal buffer has at least amount bytes of data, and returns it. Read more
Source§

fn data_hard(&mut self, amount: usize) -> Result<&[u8], Error>

Like BufferedReader::data, but returns an error if there is not at least amount bytes available. Read more
Source§

fn consume(&mut self, amount: usize) -> &[u8] ⓘ

Consumes some of the data. Read more
Source§

fn data_consume(&mut self, amount: usize) -> Result<&[u8], Error>

A convenience function that combines BufferedReader::data and BufferedReader::consume. Read more
Source§

fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], Error>

A convenience function that effectively combines BufferedReader::data_hard and BufferedReader::consume. Read more
Source§

fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<Cookie>>

Returns a mutable reference to the inner BufferedReader, if any. Read more
Source§

fn get_ref(&self) -> Option<&dyn BufferedReader<Cookie>>

Returns a reference to the inner BufferedReader, if any.
Source§

fn into_inner<'b>( self: Box<Reader<'_>>, ) -> Option<Box<dyn BufferedReader<Cookie> + 'b>>
where Reader<'_>: 'b,

Returns the underlying reader, if any. Read more
Source§

fn cookie_set(&mut self, cookie: Cookie) -> Cookie

Sets the BufferedReader’s cookie and returns the old value.
Source§

fn cookie_ref(&self) -> &Cookie

Returns a reference to the BufferedReader’s cookie.
Source§

fn cookie_mut(&mut self) -> &mut Cookie

Returns a mutable reference to the BufferedReader’s cookie.
Source§

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
Source§

fn eof(&mut self) -> bool

Checks whether the end of the stream is reached.
Source§

fn consummated(&mut self) -> bool

Checks whether this reader is consummated. Read more
Source§

fn read_be_u16(&mut self) -> Result<u16, Error>

A convenience function for reading a 16-bit unsigned integer in big endian format.
Source§

fn read_be_u32(&mut self) -> Result<u32, Error>

A convenience function for reading a 32-bit unsigned integer in big endian format.
Source§

fn read_to(&mut self, terminal: u8) -> Result<&[u8], Error>

Reads until either terminal is encountered or EOF. Read more
Source§

fn drop_until(&mut self, terminals: &[u8]) -> Result<usize, Error>

Discards the input until one of the bytes in terminals is encountered. Read more
Source§

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
Source§

fn steal(&mut self, amount: usize) -> Result<Vec<u8>, Error>

Like BufferedReader::data_consume_hard, but returns the data in a caller-owned buffer. Read more
Source§

fn steal_eof(&mut self) -> Result<Vec<u8>, Error>

Like 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>

Like BufferedReader::steal_eof, but instead of returning the data, the data is discarded. Read more
Source§

fn copy(&mut self, sink: &mut dyn Write) -> Result<u64, Error>

Copies data to the given writer returning the copied amount. Read more
Source§

fn dump(&self, sink: &mut dyn Write) -> Result<(), Error>
where Self: Sized,

A helpful debugging aid to pretty print a Buffered Reader stack. Read more
Source§

fn into_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a>
where Self: Sized + 'a,

Boxes the reader.
Source§

fn as_boxed<'a>(self) -> Box<dyn BufferedReader<C> + 'a>
where Self: Sized + 'a,

👎Deprecated:

Use into_boxed

Boxes the reader.
Source§

impl<'a> Debug for Reader<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> Display for Reader<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Read for Reader<'_>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize, Error>

Pull some bytes from this source into the specified buffer, returning how many bytes were read. Read more
1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_, u8>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact( &mut self, cursor: BorrowedCursor<'_, u8>, ) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self> ⓘ
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R> ⓘ
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self> ⓘ
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

fn read_array<const N: usize>(&mut self) -> Result<[u8; N], Error>
where Self: Sized,

🔬This is a nightly-only experimental API. (read_array)
Read and return a fixed array of bytes from this source. Read more
Source§

fn read_le<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in little-endian order. Read more
Source§

fn read_be<T>(&mut self) -> Result<T, Error>
where T: FromEndianBytes, Self: Sized,

🔬This is a nightly-only experimental API. (read_le)
Read and return a type (e.g. an integer) in big-endian order. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Reader<'a>

§

impl<'a> !UnwindSafe for Reader<'a>

§

impl<'a> Freeze for Reader<'a>

§

impl<'a> Send for Reader<'a>

§

impl<'a> Sync for Reader<'a>

§

impl<'a> Unpin for Reader<'a>

§

impl<'a> UnsafeUnpin for Reader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<R> ElfRead for R
where R: Read + ?Sized,

Source§

fn read_bytes(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read enough bytes to fill the buffer buf. Read more
Source§

fn read_u8(&mut self) -> Result<u8, Error>

Read one byte as u8.
Source§

fn read_i8(&mut self) -> Result<i8, Error>

Read one byte as i8.
Source§

fn read_i16(&mut self, byte_order: ByteOrder) -> Result<i16, Error>

Read i16.
Source§

fn read_i32(&mut self, byte_order: ByteOrder) -> Result<i32, Error>

Read i32.
Source§

fn read_i64(&mut self, byte_order: ByteOrder) -> Result<i64, Error>

Read i64.
Source§

fn read_u16(&mut self, byte_order: ByteOrder) -> Result<u16, Error>

Read u16.
Source§

fn read_u32(&mut self, byte_order: ByteOrder) -> Result<u32, Error>

Read u32.
Source§

fn read_u64(&mut self, byte_order: ByteOrder) -> Result<u64, Error>

Read u64.
Source§

fn read_word( &mut self, class: Class, byte_order: ByteOrder, ) -> Result<u64, Error>

Read one word. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<R> LittleEndianReadExt for R
where R: Read,

Source§

fn read_u16_le(&mut self) -> Result<u16, Error>

Source§

fn read_u32_le(&mut self) -> Result<u32, Error>

Source§

fn read_u64_le(&mut self) -> Result<u64, Error>

Source§

impl<T> MaybeSend for T

Source§

impl<T> MaybeSync for T

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToCompactString for T
where T: Display,

Source§

impl<T> ToLine for T
where T: Display,

Source§

fn to_line(&self) -> Line<'_>

Converts the value to a Line.
Source§

impl<T> ToSpan for T
where T: Display,

Source§

fn to_span(&self) -> Span<'_>

Converts the value to a Span.
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T> ToText for T
where T: Display,

Source§

fn to_text(&self) -> Text<'_>

Converts the value to a Text.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more