From 02b42081b97d1262d4dfe29158c5a5656d07524c Mon Sep 17 00:00:00 2001 From: Heiko Schaefer Date: Sun, 1 May 2022 17:16:22 +0200 Subject: [PATCH] Implement Display for CardholderRelatedData --- openpgp-card/src/card_do.rs | 17 +++++++++++++++++ openpgp-card/src/card_do/cardholder.rs | 8 ++++++++ tools/src/bin/opgpcard/main.rs | 1 + 3 files changed, 26 insertions(+) diff --git a/openpgp-card/src/card_do.rs b/openpgp-card/src/card_do.rs index 1a61abd..19b5ae5 100644 --- a/openpgp-card/src/card_do.rs +++ b/openpgp-card/src/card_do.rs @@ -737,6 +737,23 @@ pub struct CardholderRelatedData { sex: Option, } +impl Display for CardholderRelatedData { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + if let Some(name) = &self.name { + writeln!(f, "Name: {}", Self::latin1_to_string(name))?; + } + if let Some(sex) = self.sex { + writeln!(f, "Sex: {}", sex)?; + } + if let Some(lang) = &self.lang { + for (n, l) in lang.iter().enumerate() { + writeln!(f, "Lang {}: {}", n + 1, l)?; + } + } + Ok(()) + } +} + /// 4.4.3.5 Sex /// Encoded in accordance with https://en.wikipedia.org/wiki/ISO/IEC_5218 #[derive(Debug, PartialEq, Clone, Copy)] diff --git a/openpgp-card/src/card_do/cardholder.rs b/openpgp-card/src/card_do/cardholder.rs index 9f7704d..55d50a2 100644 --- a/openpgp-card/src/card_do/cardholder.rs +++ b/openpgp-card/src/card_do/cardholder.rs @@ -13,6 +13,14 @@ impl CardholderRelatedData { self.name.as_deref() } + /// The name field is defined as latin1 encoded, + /// with ´<´ and ´<<´ filler characters to separate elements and name-parts. + /// + /// (The filler/separation characters are not processed by this fn) + pub(crate) fn latin1_to_string(s: &[u8]) -> String { + s.iter().map(|&c| c as char).collect() + } + pub fn lang(&self) -> Option<&[Lang]> { self.lang.as_deref() } diff --git a/tools/src/bin/opgpcard/main.rs b/tools/src/bin/opgpcard/main.rs index 3ce015b..937ca09 100644 --- a/tools/src/bin/opgpcard/main.rs +++ b/tools/src/bin/opgpcard/main.rs @@ -419,6 +419,7 @@ fn print_status(ident: Option, verbose: bool) -> Result<()> { let crd = open.cardholder_related_data()?; if let Some(name) = crd.name() { + // FIXME: decoding as utf8 is wrong (the spec defines this field as latin1 encoded) let name = String::from_utf8_lossy(name).to_string(); print!("Cardholder: ");