diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2019-08-29 13:13:45 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2019-08-30 16:52:39 +0200 |
commit | 1922a36df75f1713a07f6c4c4bdfb5c8dffd5a1b (patch) | |
tree | f2bff0017b18218ceadb6cf3d9534b1bece9a20e /openpgp/src/serialize/writer | |
parent | 9fe16b02919a45f192372fc1250293f4ccfa040d (diff) |
openpgp: Keep track of a writers position.
Diffstat (limited to 'openpgp/src/serialize/writer')
-rw-r--r-- | openpgp/src/serialize/writer/mod.rs | 28 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/writer_bzip2.rs | 3 | ||||
-rw-r--r-- | openpgp/src/serialize/writer/writer_deflate.rs | 6 |
3 files changed, 36 insertions, 1 deletions
diff --git a/openpgp/src/serialize/writer/mod.rs b/openpgp/src/serialize/writer/mod.rs index a982d8ac..4545cdd2 100644 --- a/openpgp/src/serialize/writer/mod.rs +++ b/openpgp/src/serialize/writer/mod.rs @@ -111,6 +111,9 @@ pub(crate) trait Stackable<'a, C> : io::Write + fmt::Debug { /// Returns a mutable reference to the cookie. fn cookie_mut(&mut self) -> &mut C; + /// Returns the number of bytes written to this filter. + fn position(&self) -> u64; + /// Writes a byte. fn write_u8(&mut self, b: u8) -> io::Result<()> { let b : [u8; 1] = [b; 1]; @@ -159,6 +162,9 @@ impl <'a, C> Stackable<'a, C> for BoxStack<'a, C> { fn cookie_mut(&mut self) -> &mut C { self.as_mut().cookie_mut() } + fn position(&self) -> u64 { + self.as_ref().position() + } } /// Maps a function over the stack of writers. @@ -272,12 +278,16 @@ impl<'a, C> Stackable<'a, C> for Identity<'a, C> { fn cookie_mut(&mut self) -> &mut C { &mut self.cookie } + fn position(&self) -> u64 { + self.inner.as_ref().map(|i| i.position()).unwrap_or(0) + } } /// Generic writer wrapping `io::Write`. pub struct Generic<W: io::Write, C> { inner: W, cookie: C, + position: u64, } impl<'a, W: 'a + io::Write, C: 'a> Generic<W, C> { @@ -290,6 +300,7 @@ impl<'a, W: 'a + io::Write, C: 'a> Generic<W, C> { Generic { inner: inner, cookie: cookie, + position: 0, } } } @@ -303,7 +314,13 @@ impl<W: io::Write, C> fmt::Debug for Generic<W, C> { impl<W: io::Write, C> io::Write for Generic<W, C> { fn write(&mut self, bytes: &[u8]) -> io::Result<usize> { - self.inner.write(bytes) + match self.inner.write(bytes) { + Ok(n) => { + self.position += n as u64; + Ok(n) + }, + Err(e) => Err(e), + } } fn flush(&mut self) -> io::Result<()> { @@ -338,6 +355,9 @@ impl<'a, W: io::Write, C> Stackable<'a, C> for Generic<W, C> { fn cookie_mut(&mut self) -> &mut C { &mut self.cookie } + fn position(&self) -> u64 { + self.position + } } @@ -404,6 +424,9 @@ impl<'a, C: 'a> Stackable<'a, C> for Encryptor<'a, C> { fn cookie_mut(&mut self) -> &mut C { self.inner.cookie_mut() } + fn position(&self) -> u64 { + self.inner.position + } } @@ -472,6 +495,9 @@ impl<'a, C: 'a> Stackable<'a, C> for AEADEncryptor<'a, C> { fn cookie_mut(&mut self) -> &mut C { self.inner.cookie_mut() } + fn position(&self) -> u64 { + self.inner.position + } } #[cfg(test)] diff --git a/openpgp/src/serialize/writer/writer_bzip2.rs b/openpgp/src/serialize/writer/writer_bzip2.rs index 8f51fe18..b951b596 100644 --- a/openpgp/src/serialize/writer/writer_bzip2.rs +++ b/openpgp/src/serialize/writer/writer_bzip2.rs @@ -66,4 +66,7 @@ impl<'a, C: 'a> Stackable<'a, C> for BZ<'a, C> { fn cookie_mut(&mut self) -> &mut C { self.inner.cookie_mut() } + fn position(&self) -> u64 { + self.inner.position + } } diff --git a/openpgp/src/serialize/writer/writer_deflate.rs b/openpgp/src/serialize/writer/writer_deflate.rs index a0e1ea60..664b5319 100644 --- a/openpgp/src/serialize/writer/writer_deflate.rs +++ b/openpgp/src/serialize/writer/writer_deflate.rs @@ -66,6 +66,9 @@ impl<'a, C: 'a> Stackable<'a, C> for ZIP<'a, C> { fn cookie_mut(&mut self) -> &mut C { self.inner.cookie_mut() } + fn position(&self) -> u64 { + self.inner.position + } } /// ZLIBing writer. @@ -128,4 +131,7 @@ impl<'a, C: 'a> Stackable<'a, C> for ZLIB<'a, C> { fn cookie_mut(&mut self) -> &mut C { self.inner.cookie_mut() } + fn position(&self) -> u64 { + self.inner.position + } } |