From cc09a1f247ab7d4d796d73e9b5b499e014ade037 Mon Sep 17 00:00:00 2001 From: Pavlo Myroniuk Date: Sat, 24 Aug 2024 10:18:52 -0700 Subject: [PATCH] Fix `OctetString` inner bytes rendering (#75) --- crates/asn1-parser/tests/decode_encode.rs | 12 ++++++++++++ src/asn1/scheme/strings.rs | 8 +++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/crates/asn1-parser/tests/decode_encode.rs b/crates/asn1-parser/tests/decode_encode.rs index 33971adb..a397def4 100644 --- a/crates/asn1-parser/tests/decode_encode.rs +++ b/crates/asn1-parser/tests/decode_encode.rs @@ -293,3 +293,15 @@ fn test_1() { let asn1 = Asn1::decode_buff(raw).unwrap(); println!("{:?}", asn1); } + +#[test] +fn test_2() { + init_logging(); + + let raw = &[ + 4, 24, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 0, 0, 0, + ]; + + let asn1 = Asn1::decode_buff(raw).unwrap(); + println!("{:?}", asn1); +} diff --git a/src/asn1/scheme/strings.rs b/src/asn1/scheme/strings.rs index 8fa72500..31b21b5f 100644 --- a/src/asn1/scheme/strings.rs +++ b/src/asn1/scheme/strings.rs @@ -39,7 +39,13 @@ pub fn octet_string(props: &OctetStringNodeProps) -> Html { }, None => { let encoded_octets = match std::str::from_utf8(octets) { - Ok(s) => s.to_owned(), + Ok(s) => { + if s.chars().any(|c| (c as u8) < 32) { + hex::encode(octets) + } else { + s.to_owned() + } + } Err(_) => hex::encode(octets), }; html! {