summaryrefslogtreecommitdiffstats
path: root/openpgp/src/serialize/stream
diff options
context:
space:
mode:
authorJustus Winter <justus@sequoia-pgp.org>2020-04-03 13:38:05 +0200
committerJustus Winter <justus@sequoia-pgp.org>2020-04-03 13:59:25 +0200
commit6b4207285b182ca1a94f55608455646c1e36b859 (patch)
tree857f15b265b03d86c1836df8fa5066d687f10e7e /openpgp/src/serialize/stream
parent3537bf41630139cb4cd44a8b13a21e7cce27211e (diff)
openpgp: Unify Message and writer::Stack, hide writers.
- Previously, Message::new returned a writer::Stack, and Message was just an empty struct. Unify the types. This makes sense, because if you have a message, and encrypt it, you get a message. - Make the writer module private. This is an implementation detail.
Diffstat (limited to 'openpgp/src/serialize/stream')
-rw-r--r--openpgp/src/serialize/stream/padding.rs13
-rw-r--r--openpgp/src/serialize/stream/partial_body.rs15
-rw-r--r--openpgp/src/serialize/stream/writer/mod.rs51
-rw-r--r--openpgp/src/serialize/stream/writer/writer_bzip2.rs6
-rw-r--r--openpgp/src/serialize/stream/writer/writer_deflate.rs10
5 files changed, 41 insertions, 54 deletions
diff --git a/openpgp/src/serialize/stream/padding.rs b/openpgp/src/serialize/stream/padding.rs
index db190253..3b08aa50 100644
--- a/openpgp/src/serialize/stream/padding.rs
+++ b/openpgp/src/serialize/stream/padding.rs
@@ -51,6 +51,7 @@ use crate::serialize::{
stream::{
writer,
Cookie,
+ Message,
PartialBodyFilter,
},
};
@@ -132,26 +133,26 @@ pub struct Padder<'a, P: Fn(u64) -> u64 + 'a> {
impl<'a, P: Fn(u64) -> u64 + 'a> Padder<'a, P> {
/// Creates a new padder with the given policy.
- pub fn new(inner: writer::Stack<'a, Cookie>, p: P)
- -> Result<writer::Stack<'a, Cookie>> {
+ pub fn new(inner: Message<'a, Cookie>, p: P)
+ -> Result<Message<'a, Cookie>> {
let mut inner = writer::BoxStack::from(inner);
let level = inner.cookie_ref().level + 1;
// Packet header.
CTB::new(Tag::CompressedData).serialize(&mut inner)?;
- let mut inner: writer::Stack<'a, Cookie>
- = PartialBodyFilter::new(writer::Stack::from(inner),
+ let mut inner: Message<'a, Cookie>
+ = PartialBodyFilter::new(Message::from(inner),
Cookie::new(level));
// Compressed data header.
inner.as_mut().write_u8(CompressionAlgorithm::Zip.into())?;
// Create an appropriate filter.
- let inner: writer::Stack<'a, Cookie> =
+ let inner: Message<'a, Cookie> =
writer::ZIP::new(inner, Cookie::new(level),
writer::CompressionLevel::none());
- Ok(writer::Stack::from(Box::new(Self {
+ Ok(Message::from(Box::new(Self {
inner: inner.into(),
policy: p,
})))
diff --git a/openpgp/src/serialize/stream/partial_body.rs b/openpgp/src/serialize/stream/partial_body.rs
index e5aef8ca..24e0ea52 100644
--- a/openpgp/src/serialize/stream/partial_body.rs
+++ b/openpgp/src/serialize/stream/partial_body.rs
@@ -10,7 +10,10 @@ use crate::Result;
use crate::packet::header::BodyLength;
use crate::serialize::{
log2,
- stream::writer,
+ stream::{
+ writer,
+ Message,
+ },
write_byte,
Marshal,
};
@@ -48,8 +51,8 @@ const PARTIAL_BODY_FILTER_BUFFER_THRESHOLD : usize = 4 * 1024 * 1024;
impl<'a, C: 'a> PartialBodyFilter<'a, C> {
/// Returns a new partial body encoder.
- pub fn new(inner: writer::Stack<'a, C>, cookie: C)
- -> writer::Stack<'a, C> {
+ pub fn new(inner: Message<'a, C>, cookie: C)
+ -> Message<'a, C> {
Self::with_limits(inner, cookie,
PARTIAL_BODY_FILTER_BUFFER_THRESHOLD,
PARTIAL_BODY_FILTER_MAX_CHUNK_SIZE)
@@ -57,10 +60,10 @@ impl<'a, C: 'a> PartialBodyFilter<'a, C> {
}
/// Returns a new partial body encoder with the given limits.
- pub fn with_limits(inner: writer::Stack<'a, C>, cookie: C,
+ pub fn with_limits(inner: Message<'a, C>, cookie: C,
buffer_threshold: usize,
max_chunk_size: usize)
- -> Result<writer::Stack<'a, C>> {
+ -> Result<Message<'a, C>> {
if buffer_threshold.count_ones() != 1 {
return Err(Error::InvalidArgument(
"buffer_threshold is not a power of two".into()).into());
@@ -76,7 +79,7 @@ impl<'a, C: 'a> PartialBodyFilter<'a, C> {
"max_chunk_size exceeds limit".into()).into());
}
- Ok(writer::Stack::from(Box::new(PartialBodyFilter {
+ Ok(Message::from(Box::new(PartialBodyFilter {
inner: Some(inner.into()),
cookie,
buffer: Vec::with_capacity(buffer_threshold),
diff --git a/openpgp/src/serialize/stream/writer/mod.rs b/openpgp/src/serialize/stream/writer/mod.rs
index 2141ca98..1006f0f5 100644
--- a/openpgp/src/serialize/stream/writer/mod.rs
+++ b/openpgp/src/serialize/stream/writer/mod.rs
@@ -23,14 +23,11 @@ use crate::{
Result,
crypto::SessionKey,
};
+use super::Message;
-/// A stack of writers.
-#[derive(Debug)]
-pub struct Stack<'a, C>(BoxStack<'a, C>);
-
-impl<'a, C> Stack<'a, C> {
+impl<'a, C> Message<'a, C> {
pub(crate) fn from(bs: BoxStack<'a, C>) -> Self {
- Stack(bs)
+ Message(bs)
}
pub(crate) fn as_ref(&self) -> &BoxStack<'a, C> {
@@ -40,23 +37,9 @@ impl<'a, C> Stack<'a, C> {
pub(crate) fn as_mut(&mut self) -> &mut BoxStack<'a, C> {
&mut self.0
}
-
- /// Finalizes this writer, returning the underlying writer.
- pub fn finalize_one(self) -> Result<Option<Stack<'a, C>>> {
- Ok(self.0.into_inner()?.map(|bs| Self::from(bs)))
- }
-
- /// Finalizes all writers, tearing down the whole stack.
- pub fn finalize(self) -> Result<()> {
- let mut stack = self;
- while let Some(s) = stack.finalize_one()? {
- stack = s;
- }
- Ok(())
- }
}
-impl<'a, C> io::Write for Stack<'a, C> {
+impl<'a, C> io::Write for Message<'a, C> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.write(buf)
}
@@ -66,8 +49,8 @@ impl<'a, C> io::Write for Stack<'a, C> {
}
}
-impl<'a, C> From<Stack<'a, C>> for BoxStack<'a, C> {
- fn from(s: Stack<'a, C>) -> Self {
+impl<'a, C> From<Message<'a, C>> for BoxStack<'a, C> {
+ fn from(s: Message<'a, C>) -> Self {
s.0
}
}
@@ -214,9 +197,9 @@ pub struct Identity<'a, C> {
impl<'a, C: 'a> Identity<'a, C> {
/// Makes an identity writer.
- pub fn new(inner: Stack<'a, C>, cookie: C)
- -> Stack<'a, C> {
- Stack::from(Box::new(Self{inner: Some(inner.into()), cookie }))
+ pub fn new(inner: Message<'a, C>, cookie: C)
+ -> Message<'a, C> {
+ Message::from(Box::new(Self{inner: Some(inner.into()), cookie }))
}
}
@@ -294,8 +277,8 @@ pub struct Generic<W: io::Write, C> {
impl<'a, W: 'a + io::Write, C: 'a> Generic<W, C> {
/// Wraps an `io::Write`r.
- pub fn new(inner: W, cookie: C) -> Stack<'a, C> {
- Stack::from(Box::new(Self::new_unboxed(inner.into(), cookie)))
+ pub fn new(inner: W, cookie: C) -> Message<'a, C> {
+ Message::from(Box::new(Self::new_unboxed(inner.into(), cookie)))
}
fn new_unboxed(inner: W, cookie: C) -> Self {
@@ -378,11 +361,11 @@ pub struct Encryptor<'a, C: 'a> {
impl<'a, C: 'a> Encryptor<'a, C> {
/// Makes an encrypting writer.
- pub fn new(inner: Stack<'a, C>, cookie: C, algo: SymmetricAlgorithm,
+ pub fn new(inner: Message<'a, C>, cookie: C, algo: SymmetricAlgorithm,
key: &[u8])
- -> Result<Stack<'a, C>>
+ -> Result<Message<'a, C>>
{
- Ok(Stack::from(Box::new(Encryptor {
+ Ok(Message::from(Box::new(Encryptor {
inner: Generic::new_unboxed(
symmetric::Encryptor::new(algo, key, inner.into())?,
cookie),
@@ -449,12 +432,12 @@ pub struct AEADEncryptor<'a, C: 'a> {
impl<'a, C: 'a> AEADEncryptor<'a, C> {
/// Makes an encrypting writer.
- pub fn new(inner: Stack<'a, C>, cookie: C,
+ pub fn new(inner: Message<'a, C>, cookie: C,
cipher: SymmetricAlgorithm, aead: AEADAlgorithm,
chunk_size: usize, iv: &[u8], key: &SessionKey)
- -> Result<Stack<'a, C>>
+ -> Result<Message<'a, C>>
{
- Ok(Stack::from(Box::new(AEADEncryptor {
+ Ok(Message::from(Box::new(AEADEncryptor {
inner: Generic::new_unboxed(
aead::Encryptor::new(1, cipher, aead, chunk_size, iv, key,
inner.into())?,
diff --git a/openpgp/src/serialize/stream/writer/writer_bzip2.rs b/openpgp/src/serialize/stream/writer/writer_bzip2.rs
index 62affd0c..1f5b6cdf 100644
--- a/openpgp/src/serialize/stream/writer/writer_bzip2.rs
+++ b/openpgp/src/serialize/stream/writer/writer_bzip2.rs
@@ -3,7 +3,7 @@ use std::fmt;
use std::io;
use crate::Result;
-use super::{Generic, Stack, BoxStack, Stackable, CompressionLevel};
+use super::{Generic, Message, BoxStack, Stackable, CompressionLevel};
/// BZing writer.
pub struct BZ<'a, C: 'a> {
@@ -12,10 +12,10 @@ pub struct BZ<'a, C: 'a> {
impl<'a, C: 'a> BZ<'a, C> {
/// Makes a BZ compressing writer.
- pub fn new<L>(inner: Stack<'a, C>, cookie: C, level: L) -> Stack<'a, C>
+ pub fn new<L>(inner: Message<'a, C>, cookie: C, level: L) -> Message<'a, C>
where L: Into<Option<CompressionLevel>>
{
- Stack::from(Box::new(BZ {
+ Message::from(Box::new(BZ {
inner: Generic::new_unboxed(
BzEncoder::new(inner.into(),
level.into().unwrap_or_default().into()),
diff --git a/openpgp/src/serialize/stream/writer/writer_deflate.rs b/openpgp/src/serialize/stream/writer/writer_deflate.rs
index 387b3f2c..b2cdd74f 100644
--- a/openpgp/src/serialize/stream/writer/writer_deflate.rs
+++ b/openpgp/src/serialize/stream/writer/writer_deflate.rs
@@ -3,7 +3,7 @@ use std::fmt;
use std::io;
use crate::Result;
-use super::{Generic, Stack, BoxStack, Stackable, CompressionLevel};
+use super::{Generic, Message, BoxStack, Stackable, CompressionLevel};
/// ZIPing writer.
pub struct ZIP<'a, C: 'a> {
@@ -12,10 +12,10 @@ pub struct ZIP<'a, C: 'a> {
impl<'a, C: 'a> ZIP<'a, C> {
/// Makes a ZIP compressing writer.
- pub fn new<L>(inner: Stack<'a, C>, cookie: C, level: L) -> Stack<'a, C>
+ pub fn new<L>(inner: Message<'a, C>, cookie: C, level: L) -> Message<'a, C>
where L: Into<Option<CompressionLevel>>
{
- Stack::from(Box::new(ZIP {
+ Message::from(Box::new(ZIP {
inner: Generic::new_unboxed(
DeflateEncoder::new(inner.into(),
level.into().unwrap_or_default().into()),
@@ -80,10 +80,10 @@ pub struct ZLIB<'a, C: 'a> {
impl<'a, C: 'a> ZLIB<'a, C> {
/// Makes a ZLIB compressing writer.
- pub fn new<L>(inner: Stack<'a, C>, cookie: C, level: L) -> Stack<'a, C>
+ pub fn new<L>(inner: Message<'a, C>, cookie: C, level: L) -> Message<'a, C>
where L: Into<Option<CompressionLevel>>
{
- Stack::from(Box::new(ZLIB {
+ Message::from(Box::new(ZLIB {
inner: Generic::new_unboxed(
ZlibEncoder::new(inner.into(),
level.into().unwrap_or_default().into()),