diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-03-18 17:23:15 +0100 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-03-18 17:29:27 +0100 |
commit | 97908d6f25f8973aee95a1850ae42af0323698c7 (patch) | |
tree | 3bc69e031e3e4dce5d3d1b60885f0bf50c04ee12 | |
parent | 153db43f8f4eae71a906beacaea149492d75a1ac (diff) |
openpgp: Move ASCII dumping code to the library.
-rw-r--r-- | openpgp/src/fmt.rs | 24 | ||||
-rw-r--r-- | tool/src/commands/dump.rs | 17 |
2 files changed, 25 insertions, 16 deletions
diff --git a/openpgp/src/fmt.rs b/openpgp/src/fmt.rs index 429f2b69..3b79ceba 100644 --- a/openpgp/src/fmt.rs +++ b/openpgp/src/fmt.rs @@ -89,6 +89,30 @@ pub mod hex { }) } + /// Writes a chunk of data with ASCII-representation. + /// + /// This produces output similar to `hexdump(1)`. + pub fn write_ascii<B>(&mut self, buf: B) -> io::Result<()> + where B: AsRef<[u8]>, + { + self.write_labeled(buf, |offset, data| { + let mut l = String::new(); + for _ in 0..offset { + l.push(' '); + } + for &c in data { + l.push(if c < 32 { + '.' + } else if c < 128 { + c.into() + } else { + '.' + }) + } + Some(l) + }) + } + /// Writes a chunk of data. /// /// For each line, the given function is called to compute a diff --git a/tool/src/commands/dump.rs b/tool/src/commands/dump.rs index 2ef4f1b0..a76fac27 100644 --- a/tool/src/commands/dump.rs +++ b/tool/src/commands/dump.rs @@ -690,22 +690,7 @@ impl PacketDumper { for field in map.iter() { if field.name() == "body" { - hd.write_labeled(field.data(), |offset, data| { - let mut l = String::new(); - for _ in 0..offset { - l.push(' '); - } - for &c in data { - l.push(if c < 32 { - '.' - } else if c < 128 { - c.into() - } else { - '.' - }) - } - Some(l) - })?; + hd.write_ascii(field.data())?; } else { hd.write(field.data(), field.name())?; } |