Merge pull request #65 from tomoyuki-nakabayashi/tlv_tool/add-hexstring-option

tlv_tool: Add hexstring option.
This commit is contained in:
Kedar Sovani 2023-07-12 15:21:11 +05:30 committed by GitHub
commit 7504f6f5b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 34 deletions

View file

@ -10,3 +10,4 @@ matter-iot = { path = "../../matter" }
log = {version = "0.4.14", features = ["max_level_trace", "release_max_level_warn"]} log = {version = "0.4.14", features = ["max_level_trace", "release_max_level_warn"]}
simple_logger = "1.16.0" simple_logger = "1.16.0"
clap = "2.34" clap = "2.34"
hex = "0.4"

View file

@ -1,10 +1,14 @@
# TLV Tool # TLV Tool
A simple tool for printing Matter TLVs or Matter-encoded certificates. A simple tool for printing Matter TLVs or Matter-encoded certificates.
``` ```
$ # For printing a Matter TLV List $ # For printing a Matter TLV List
$ tlv_tool --hex "15, 24, 0, 1, 18" $ tlv_tool --hex "15, 24, 0, 1, 18"
$ # For printing a Matter TLV List in hexstring
$ tlv_tool --hexstring "1524000118"
$ # For printing a Matter encoded certificate $ # For printing a Matter encoded certificate
$ tlv_tool --cert "0x15, 0x00" $ tlv_tool --cert "0x15, 0x00"
``` ```

View file

@ -21,6 +21,41 @@ use matter::tlv;
use simple_logger::SimpleLogger; use simple_logger::SimpleLogger;
use std::process; use std::process;
fn decode_to_slice_radix<T: AsRef<str>>(data: T, out: &mut [u8], radix: u32) -> &[u8] {
let data = data.as_ref();
let list = data.split(',');
let mut index = 0;
for byte in list {
let byte = byte.strip_prefix("0x").unwrap_or(byte);
if let Ok(b) = u8::from_str_radix(byte, radix) {
out[index] = b;
index += 1;
} else {
eprintln!("Skipping unknown byte: {}", byte);
}
if index >= out.len() {
eprintln!("Input too long");
process::exit(1);
}
}
&out[..index]
}
fn print_tlv(matches: &clap::ArgMatches<'_>, tlv_list: &[u8]) {
if matches.is_present("cert") {
let cert = cert::Cert::new(tlv_list).unwrap();
println!("{}", cert);
} else if matches.is_present("as-asn1") {
let mut asn1_cert = [0_u8; 1024];
let cert = cert::Cert::new(tlv_list).unwrap();
let len = cert.as_asn1(&mut asn1_cert).unwrap();
println!("{:02x?}", &asn1_cert[..len]);
} else {
tlv::print_tlv_list(tlv_list);
}
}
fn main() { fn main() {
SimpleLogger::new() SimpleLogger::new()
.with_level(log::LevelFilter::Trace) .with_level(log::LevelFilter::Trace)
@ -42,6 +77,12 @@ fn main() {
.long("dec") .long("dec")
.help("The input is in Decimal"), .help("The input is in Decimal"),
) )
.arg(
Arg::with_name("hexstring")
.short("H")
.long("hexstring")
.help("The input is in Hexadecimal String"),
)
.arg( .arg(
Arg::with_name("cert") Arg::with_name("cert")
.long("cert") .long("cert")
@ -55,14 +96,12 @@ fn main() {
.arg(Arg::with_name("tlvs").help("List of TLVs").required(true)) .arg(Arg::with_name("tlvs").help("List of TLVs").required(true))
.get_matches(); .get_matches();
// Assume hexadecimal by-default if m.is_present("hex") || m.is_present("dec") {
let base = if m.is_present("hex") { if m.is_present("hexstring") {
16 eprintln!("Cannot use --hexstring with --hex or --dec");
} else if m.is_present("dec") { process::exit(1);
10 }
} else { }
16
};
let list: String = m let list: String = m
.value_of("tlvs") .value_of("tlvs")
@ -70,33 +109,24 @@ fn main() {
.chars() .chars()
.filter(|c| !c.is_whitespace()) .filter(|c| !c.is_whitespace())
.collect(); .collect();
let list = list.split(',');
let mut tlv_list: [u8; 1024] = [0; 1024];
let mut index = 0;
for byte in list {
let byte = byte.strip_prefix("0x").unwrap_or(byte);
if let Ok(b) = u8::from_str_radix(byte, base) {
tlv_list[index] = b;
index += 1;
} else {
eprintln!("Skipping unknown byte: {}", byte);
}
if index >= 1024 {
eprintln!("Input too long");
process::exit(1);
}
}
// println!("Decoding: {:x?}", &tlv_list[..index]); if m.is_present("hexstring") {
if m.is_present("cert") { let tlv_list = hex::decode(list).unwrap();
let cert = cert::Cert::new(&tlv_list[..index]).unwrap(); print_tlv(&m, &tlv_list);
println!("{}", cert);
} else if m.is_present("as-asn1") {
let mut asn1_cert = [0_u8; 1024];
let cert = cert::Cert::new(&tlv_list[..index]).unwrap();
let len = cert.as_asn1(&mut asn1_cert).unwrap();
println!("{:02x?}", &asn1_cert[..len]);
} else { } else {
tlv::print_tlv_list(&tlv_list[..index]); // Assume hexadecimal by-default
let base = if m.is_present("hex") {
16
} else if m.is_present("dec") {
10
} else {
16
};
let mut tlv_list: [u8; 1024] = [0; 1024];
let tlv_list = decode_to_slice_radix(list, &mut tlv_list, base);
// println!("Decoding: {:x?}", &tlv_list[..index]);
print_tlv(&m, tlv_list);
} }
} }