diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-04-20 17:15:08 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-04-20 18:44:00 +0200 |
commit | bc4e1564ea816a66d1191fc310ac6d2b02d1a92f (patch) | |
tree | 4ee0e4ee0706fa9cedab061fef8cffbc549b0d62 | |
parent | 15723e28cc6e18b0ca7a51212bbcd72fb0e40c72 (diff) |
openpgp: Add armor::Writer::with_headers.
- Add a new constructor that takes headers. This allows us to make
the header argument polymorphic.
-rw-r--r-- | autocrypt/src/lib.rs | 8 | ||||
-rw-r--r-- | ipc/examples/gpg-agent-sign.rs | 2 | ||||
-rw-r--r-- | net/src/lib.rs | 3 | ||||
-rw-r--r-- | openpgp-ffi/src/armor.rs | 2 | ||||
-rw-r--r-- | openpgp/examples/encrypt-for.rs | 2 | ||||
-rw-r--r-- | openpgp/examples/notarize.rs | 2 | ||||
-rw-r--r-- | openpgp/examples/pad.rs | 2 | ||||
-rw-r--r-- | openpgp/examples/sign-detached.rs | 2 | ||||
-rw-r--r-- | openpgp/examples/sign.rs | 2 | ||||
-rw-r--r-- | openpgp/examples/wrap-literal.rs | 2 | ||||
-rw-r--r-- | openpgp/src/armor.rs | 52 | ||||
-rw-r--r-- | openpgp/src/serialize/cert_armored.rs | 3 | ||||
-rw-r--r-- | openpgp/tests/for-each-artifact.rs | 3 | ||||
-rw-r--r-- | tool/src/commands/key.rs | 4 | ||||
-rw-r--r-- | tool/src/commands/sign.rs | 2 | ||||
-rw-r--r-- | tool/src/sq.rs | 12 |
16 files changed, 68 insertions, 35 deletions
diff --git a/autocrypt/src/lib.rs b/autocrypt/src/lib.rs index 1c8de979..64b7fa58 100644 --- a/autocrypt/src/lib.rs +++ b/autocrypt/src/lib.rs @@ -482,7 +482,7 @@ impl AutocryptSetupMessage { } let mut armor_writer = - armor::Writer::new(w, armor::Kind::Message, &headers[..])?; + armor::Writer::with_headers(w, armor::Kind::Message, headers)?; { // Passphrase-Format header with value numeric9x4 @@ -493,10 +493,10 @@ impl AutocryptSetupMessage { let m = LiteralWriter::new(m).build()?; // The inner message is an ASCII-armored encoded Cert. - let mut w = armor::Writer::new( + let mut w = armor::Writer::with_headers( m, armor::Kind::SecretKey, - &[ (&"Autocrypt-Prefer-Encrypt"[..], - self.prefer_encrypt().unwrap_or(&"nopreference"[..])) ])?; + vec![("Autocrypt-Prefer-Encrypt", + self.prefer_encrypt().unwrap_or(&"nopreference"[..]))])?; self.cert.as_tsk().serialize(&mut w)?; let m = w.finalize()?; diff --git a/ipc/examples/gpg-agent-sign.rs b/ipc/examples/gpg-agent-sign.rs index 5bad17cd..2991089a 100644 --- a/ipc/examples/gpg-agent-sign.rs +++ b/ipc/examples/gpg-agent-sign.rs @@ -51,7 +51,7 @@ fn main() { // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be // ASCII armored. - let sink = armor::Writer::new(io::stdout(), armor::Kind::Message, &[]) + let sink = armor::Writer::new(io::stdout(), armor::Kind::Message) .expect("Failed to create an armored writer."); // Stream an OpenPGP message. diff --git a/net/src/lib.rs b/net/src/lib.rs index 17c790aa..b339daf9 100644 --- a/net/src/lib.rs +++ b/net/src/lib.rs @@ -214,8 +214,7 @@ impl KeyServer { Ok(u) => u, }; - let mut w = match Writer::new(Vec::new(), - Kind::PublicKey, &[]) { + let mut w = match Writer::new(Vec::new(), Kind::PublicKey) { Ok(v) => v, Err(e) => return Box::new(future::err(e.into())), }; diff --git a/openpgp-ffi/src/armor.rs b/openpgp-ffi/src/armor.rs index dd79c9e3..094d4dc7 100644 --- a/openpgp-ffi/src/armor.rs +++ b/openpgp-ffi/src/armor.rs @@ -389,7 +389,7 @@ pub extern "C" fn pgp_armor_writer_new let header: Vec<(&str, &str)> = header_.iter().map(|h| (h.0.as_ref(), h.1.as_ref())).collect(); - armor::Writer::new(inner, kind, &header) + armor::Writer::with_headers(inner, kind, header) .map(|w| WriterKind::Armored(w)) .map_err(|e| ::anyhow::Error::from(e)) .move_into_raw(errp) diff --git a/openpgp/examples/encrypt-for.rs b/openpgp/examples/encrypt-for.rs index d8c3affd..505c34fe 100644 --- a/openpgp/examples/encrypt-for.rs +++ b/openpgp/examples/encrypt-for.rs @@ -50,7 +50,7 @@ fn main() { // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be // ASCII armored. - let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message, &[]) + let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message) .expect("Failed to create an armored writer"); // Stream an OpenPGP message. diff --git a/openpgp/examples/notarize.rs b/openpgp/examples/notarize.rs index f7a9842a..d4243f2c 100644 --- a/openpgp/examples/notarize.rs +++ b/openpgp/examples/notarize.rs @@ -58,7 +58,7 @@ fn main() { // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be // ASCII armored. - let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message, &[]) + let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message) .expect("Failed to create an armored writer."); // Stream an OpenPGP message. diff --git a/openpgp/examples/pad.rs b/openpgp/examples/pad.rs index 8e5915a8..e34c354d 100644 --- a/openpgp/examples/pad.rs +++ b/openpgp/examples/pad.rs @@ -50,7 +50,7 @@ fn main() { // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be // ASCII armored. - let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message, &[]) + let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message) .expect("Failed to create an armored writer"); // Stream an OpenPGP message. diff --git a/openpgp/examples/sign-detached.rs b/openpgp/examples/sign-detached.rs index a185cef9..30da708b 100644 --- a/openpgp/examples/sign-detached.rs +++ b/openpgp/examples/sign-detached.rs @@ -54,7 +54,7 @@ fn main() { // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be // ASCII armored. - let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Signature, &[]) + let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Signature) .expect("Failed to create armored writer."); // Stream an OpenPGP message. diff --git a/openpgp/examples/sign.rs b/openpgp/examples/sign.rs index bfef64de..cb50a0bf 100644 --- a/openpgp/examples/sign.rs +++ b/openpgp/examples/sign.rs @@ -53,7 +53,7 @@ fn main() { // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be // ASCII armored. - let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message, &[]) + let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message) .expect("Failed to create an armored writer."); // Stream an OpenPGP message. diff --git a/openpgp/examples/wrap-literal.rs b/openpgp/examples/wrap-literal.rs index 07cad384..7f86e164 100644 --- a/openpgp/examples/wrap-literal.rs +++ b/openpgp/examples/wrap-literal.rs @@ -20,7 +20,7 @@ fn main() { // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be // ASCII armored. - let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message, &[]) + let mut sink = armor::Writer::new(io::stdout(), armor::Kind::Message) .expect("Failed to create armored writer."); // Stream an OpenPGP message. diff --git a/openpgp/src/armor.rs b/openpgp/src/armor.rs index 34b068bc..ef528cba 100644 --- a/openpgp/src/armor.rs +++ b/openpgp/src/armor.rs @@ -160,8 +160,36 @@ impl<W: Write> Writer<W> { /// use openpgp::armor::{Writer, Kind}; /// # fn main() { f().unwrap(); } /// # fn f() -> std::io::Result<()> { - /// let mut writer = Writer::new(Vec::new(), Kind::File, - /// &[ ("Key", "Value") ][..])?; + /// let mut writer = Writer::new(Vec::new(), Kind::File)?; + /// writer.write_all(b"Hello world!")?; + /// let buffer = writer.finalize()?; + /// assert_eq!( + /// String::from_utf8_lossy(&buffer), + /// "-----BEGIN PGP ARMORED FILE----- + /// + /// SGVsbG8gd29ybGQh + /// =s4Gu + /// -----END PGP ARMORED FILE----- + /// "); + /// # Ok(()) + /// # } + /// ``` + pub fn new(inner: W, kind: Kind) -> Result<Self> { + Self::with_headers(inner, kind, Option::<(&str, &str)>::None) + } + + /// Constructs a new filter for the given type of data. + /// + /// # Example + /// + /// ``` + /// use std::io::{Read, Write, Cursor}; + /// use sequoia_openpgp as openpgp; + /// use openpgp::armor::{Writer, Kind}; + /// # fn main() { f().unwrap(); } + /// # fn f() -> std::io::Result<()> { + /// let mut writer = Writer::with_headers(Vec::new(), Kind::File, + /// vec![ ("Key", "Value") ])?; /// writer.write_all(b"Hello world!")?; /// let buffer = writer.finalize()?; /// assert_eq!( @@ -176,7 +204,12 @@ impl<W: Write> Writer<W> { /// # Ok(()) /// # } /// ``` - pub fn new(inner: W, kind: Kind, headers: &[(&str, &str)]) -> Result<Self> { + pub fn with_headers<I, K, V>(inner: W, kind: Kind, headers: I) + -> Result<Self> + where I: IntoIterator<Item = (K, V)>, + K: AsRef<str>, + V: AsRef<str>, + { let mut w = Writer { sink: inner, kind, @@ -192,7 +225,8 @@ impl<W: Write> Writer<W> { write!(&mut cur, "{}{}", kind.begin(), LINE_ENDING)?; for h in headers { - write!(&mut cur, "{}: {}{}", h.0, h.1, LINE_ENDING)?; + write!(&mut cur, "{}: {}{}", h.0.as_ref(), h.1.as_ref(), + LINE_ENDING)?; } // A blank line separates the headers from the body. @@ -1294,7 +1328,7 @@ mod test { fn enarmor() { for (bin, asc) in TEST_BIN.iter().zip(TEST_ASC.iter()) { let mut w = - Writer::new(Vec::new(), Kind::File, &[]).unwrap(); + Writer::new(Vec::new(), Kind::File).unwrap(); w.write(&[]).unwrap(); // Avoid zero-length optimization. w.write_all(bin).unwrap(); let buf = w.finalize().unwrap(); @@ -1306,7 +1340,7 @@ mod test { #[test] fn enarmor_bytewise() { for (bin, asc) in TEST_BIN.iter().zip(TEST_ASC.iter()) { - let mut w = Writer::new(Vec::new(), Kind::File, &[]).unwrap(); + let mut w = Writer::new(Vec::new(), Kind::File).unwrap(); w.write(&[]).unwrap(); // Avoid zero-length optimization. for b in bin.iter() { w.write(&[*b]).unwrap(); @@ -1321,12 +1355,12 @@ mod test { fn drop_writer() { // No ASCII frame shall be emitted if the writer is dropped // unused. - assert!(Writer::new(Vec::new(), Kind::File, &[]).unwrap() + assert!(Writer::new(Vec::new(), Kind::File).unwrap() .finalize().unwrap().is_empty()); // However, if the user insists, we will encode a zero-byte // string. - let mut w = Writer::new(Vec::new(), Kind::File, &[]).unwrap(); + let mut w = Writer::new(Vec::new(), Kind::File).unwrap(); w.write(&[]).unwrap(); let buf = w.finalize().unwrap(); assert_eq!( @@ -1567,7 +1601,7 @@ mod test { return true; } - let mut w = Writer::new(Vec::new(), kind, &[]).unwrap(); + let mut w = Writer::new(Vec::new(), kind).unwrap(); w.write_all(&payload).unwrap(); let encoded = w.finalize().unwrap(); diff --git a/openpgp/src/serialize/cert_armored.rs b/openpgp/src/serialize/cert_armored.rs index bc52c657..6ff69846 100644 --- a/openpgp/src/serialize/cert_armored.rs +++ b/openpgp/src/serialize/cert_armored.rs @@ -116,7 +116,8 @@ impl<'a> Encoder<'a> { .map(|value| ("Comment", value.as_str())) .collect(); - let mut w = armor::Writer::new(o, armor::Kind::PublicKey, &headers)?; + let mut w = + armor::Writer::with_headers(o, armor::Kind::PublicKey, headers)?; if export { self.cert.export(&mut w)?; } else { diff --git a/openpgp/tests/for-each-artifact.rs b/openpgp/tests/for-each-artifact.rs index b45364ea..8f049ef4 100644 --- a/openpgp/tests/for-each-artifact.rs +++ b/openpgp/tests/for-each-artifact.rs @@ -193,8 +193,7 @@ fn for_all_packets<F>(src: &Path, mut fun: F) -> openpgp::Result<()> let mut sink = io::stderr(); let mut w = openpgp::armor::Writer::new( &mut sink, - openpgp::armor::Kind::File, - &[])?; + openpgp::armor::Kind::File)?; packet.serialize(&mut w)?; w.finalize()?; return Err(e); diff --git a/tool/src/commands/key.rs b/tool/src/commands/key.rs index 688633a8..9e83b49f 100644 --- a/tool/src/commands/key.rs +++ b/tool/src/commands/key.rs @@ -158,7 +158,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> { .collect(); let w = create_or_stdout(Some(&key_path), force)?; - let mut w = Writer::new(w, Kind::SecretKey, &headers)?; + let mut w = Writer::with_headers(w, Kind::SecretKey, headers)?; cert.as_tsk().serialize(&mut w)?; w.finalize()?; } @@ -171,7 +171,7 @@ pub fn generate(m: &ArgMatches, force: bool) -> Result<()> { headers.insert(0, ("Comment", "Revocation certificate for")); let w = create_or_stdout(Some(&rev_path), force)?; - let mut w = Writer::new(w, Kind::Signature, &headers)?; + let mut w = Writer::with_headers(w, Kind::Signature, headers)?; Packet::Signature(rev).serialize(&mut w)?; w.finalize()?; } diff --git a/tool/src/commands/sign.rs b/tool/src/commands/sign.rs index 9a8e6819..a91ea54b 100644 --- a/tool/src/commands/sign.rs +++ b/tool/src/commands/sign.rs @@ -86,7 +86,7 @@ fn sign_data(policy: &dyn Policy, } else { armor::Kind::Message }, - &[])?; + Vec::new())?; } let mut keypairs = super::get_signing_keys(&secrets, policy, time)?; diff --git a/tool/src/sq.rs b/tool/src/sq.rs index 1d6c0a92..6d770e6d 100644 --- a/tool/src/sq.rs +++ b/tool/src/sq.rs @@ -93,12 +93,12 @@ impl<T: Write> From<openpgp::armor::Writer<T>> for Writer<T> { } impl<T: Write> Writer<T> { - pub fn armor(self, kind: openpgp::armor::Kind, headers: &[(&str, &str)]) + pub fn armor(self, kind: openpgp::armor::Kind, headers: Vec<(&str, &str)>) -> openpgp::Result<Self> { match self { Writer::Binary { inner } => - Ok(openpgp::armor::Writer::new(inner, kind, headers)? + Ok(openpgp::armor::Writer::with_headers(inner, kind, headers)? .into()), Writer::Armored { .. } => Err(openpgp::Error::InvalidOperation("already armored".into()) @@ -136,7 +136,7 @@ fn create_or_stdout_pgp(f: Option<&str>, force: bool, let sink = create_or_stdout(f, force)?; let mut sink = Writer::from(sink); if ! binary { - sink = sink.armor(kind, &[])?; + sink = sink.armor(kind, Vec::new())?; } Ok(sink) } @@ -178,9 +178,9 @@ fn serialize_keyring(mut output: &mut dyn io::Write, certs: &[Cert], binary: boo let headers: Vec<_> = headers.iter() .map(|value| ("Comment", value.as_str())) .collect(); - let mut output = armor::Writer::new(&mut output, - armor::Kind::PublicKey, - &headers)?; + let mut output = armor::Writer::with_headers(&mut output, + armor::Kind::PublicKey, + headers)?; for cert in certs { cert.serialize(&mut output)?; } |