From 15723e28cc6e18b0ca7a51212bbcd72fb0e40c72 Mon Sep 17 00:00:00 2001 From: Justus Winter Date: Mon, 20 Apr 2020 17:42:46 +0200 Subject: openpgp: Make armor::Writer's inner writer non-optional. --- openpgp/src/armor.rs | 97 +++++++++++++++++++++++----------------------------- 1 file changed, 42 insertions(+), 55 deletions(-) (limited to 'openpgp/src') diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs index 6433dae5..34b068bc 100644 --- a/openpgp/src/armor.rs +++ b/openpgp/src/armor.rs @@ -140,7 +140,7 @@ impl Kind { /// A filter that applies ASCII Armor to the data written to it. pub struct Writer { - sink: Option, + sink: W, kind: Kind, stash: Vec, column: usize, @@ -178,7 +178,7 @@ impl Writer { /// ``` pub fn new(inner: W, kind: Kind, headers: &[(&str, &str)]) -> Result { let mut w = Writer { - sink: Some(inner), + sink: inner, kind, stash: Vec::::with_capacity(2), column: 0, @@ -202,15 +202,10 @@ impl Writer { Ok(w) } - fn e_finalized() -> Error { - Error::new(ErrorKind::BrokenPipe, "Writer is finalized.") - } - fn finalize_headers(&mut self) -> Result<()> { if ! self.dirty { self.dirty = true; - self.sink.as_mut().ok_or_else(Self::e_finalized)? - .write_all(&self.header)?; + self.sink.write_all(&self.header)?; // Release memory. crate::vec_truncate(&mut self.header, 0); self.header.shrink_to_fit(); @@ -226,14 +221,10 @@ impl Writer { pub fn finalize(mut self) -> Result { if ! self.dirty { // No data was written to us, don't emit anything. - return Ok(self.sink.take().ok_or_else(Self::e_finalized)?); + return Ok(self.sink); } self.finalize_armor()?; - if let Some(sink) = self.sink.take() { - Ok(sink) - } else { - Err(Self::e_finalized()) - } + Ok(self.sink) } /// Writes the footer. @@ -243,55 +234,51 @@ impl Writer { return Ok(()); } self.finalize_headers()?; - if let Some(sink) = self.sink.as_mut() { - // Write any stashed bytes and pad. - if self.stash.len() > 0 { - sink.write_all(base64::encode_config( - &self.stash, base64::STANDARD).as_bytes())?; - self.column += 4; - } - // Inserts a line break if necessary. - // - // Unfortunately, we cannot use - //self.linebreak()?; - // - // Therefore, we inline it here. This is a bit sad. - assert!(self.column <= LINE_LENGTH); - if self.column == LINE_LENGTH { - write!(sink, "{}", LINE_ENDING)?; - self.column = 0; - } + // Write any stashed bytes and pad. + if self.stash.len() > 0 { + self.sink.write_all(base64::encode_config( + &self.stash, base64::STANDARD).as_bytes())?; + self.column += 4; + } - if self.column > 0 { - write!(sink, "{}", LINE_ENDING)?; - } + // Inserts a line break if necessary. + // + // Unfortunately, we cannot use + //self.linebreak()?; + // + // Therefore, we inline it here. This is a bit sad. + assert!(self.column <= LINE_LENGTH); + if self.column == LINE_LENGTH { + write!(self.sink, "{}", LINE_ENDING)?; + self.column = 0; + } - let crc = self.crc.finalize(); - let bytes: [u8; 3] = [ - (crc >> 16) as u8, - (crc >> 8) as u8, - (crc >> 0) as u8, - ]; + if self.column > 0 { + write!(self.sink, "{}", LINE_ENDING)?; + } - // CRC and footer. - write!(sink, "={}{}{}{}", - base64::encode_config(&bytes, base64::STANDARD_NO_PAD), - LINE_ENDING, self.kind.end(), LINE_ENDING)?; + let crc = self.crc.finalize(); + let bytes: [u8; 3] = [ + (crc >> 16) as u8, + (crc >> 8) as u8, + (crc >> 0) as u8, + ]; - self.dirty = false; - Ok(()) - } else { - Err(Self::e_finalized()) - } + // CRC and footer. + write!(self.sink, "={}{}{}{}", + base64::encode_config(&bytes, base64::STANDARD_NO_PAD), + LINE_ENDING, self.kind.end(), LINE_ENDING)?; + + self.dirty = false; + Ok(()) } /// Inserts a line break if necessary. fn linebreak(&mut self) -> Result<()> { assert!(self.column <= LINE_LENGTH); if self.column == LINE_LENGTH { - write!(self.sink.as_mut().ok_or_else(Self::e_finalized)?, - "{}", LINE_ENDING)?; + write!(self.sink, "{}", LINE_ENDING)?; self.column = 0; } Ok(()) @@ -328,7 +315,7 @@ impl Write for Writer { // If this fails for some reason, and the caller retries // the write, we might end up with a stash of size 3. - self.sink.as_mut().ok_or_else(Self::e_finalized)? + self.sink .write_all(base64::encode_config( &self.stash, base64::STANDARD_NO_PAD).as_bytes())?; self.column += 4; @@ -354,7 +341,7 @@ impl Write for Writer { let mut enc = encoded.as_bytes(); while enc.len() > 0 { let n = cmp::min(LINE_LENGTH - self.column, enc.len()); - self.sink.as_mut().ok_or_else(Self::e_finalized)? + self.sink .write_all(&enc[..n])?; enc = &enc[n..]; self.column += n; @@ -366,7 +353,7 @@ impl Write for Writer { } fn flush(&mut self) -> Result<()> { - self.sink.as_mut().ok_or_else(Self::e_finalized)?.flush() + self.sink.flush() } } -- cgit v1.2.3