Skip to content

Commit

Permalink
refuc(crypto-helper): asn1: replace some strings components with macr…
Browse files Browse the repository at this point in the history
…o generation;
  • Loading branch information
TheBestTvarynka committed Jan 21, 2024
1 parent c1abf5f commit 3b09192
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 84 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ flate2 = { version = "1.0.26", features = ["zlib"] }
# asn1
asn1-parser = { path = "./crates/asn1-parser" }
oid = { version = "0.2.1", default-features = false }
paste = "1.0.14"
2 changes: 1 addition & 1 deletion crates/asn1-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This `asn1` parser is aimed to parse input bytes and return an AST as the result
- [X] [BmpString](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/bmpstring.html)
- [ ] [GraphicString](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/graphicstring.html)
- [X] [IA5String](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/ia5string.html)
- [ ] [GeneralString](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/generalstring.html)
- [X] [GeneralString](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/generalstring.html)
- [X] [PrintableString](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/printablestring.html)
- [X] [OctetString](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/octetstring.html)
- [ ] [NumericString](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/numericstring.html)
Expand Down
3 changes: 3 additions & 0 deletions src/asn1.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
mod macros;

mod asn1_viewer;
mod hex_view;
mod node_options;
Expand Down
26 changes: 26 additions & 0 deletions src/asn1/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
macro_rules! define_string_node {
($name:ident) => {
paste::paste! {
#[derive(PartialEq, Properties, Clone)]
pub struct [<$name NodeProps>] {
pub node: [<Owned $name>],
pub meta: OwnedRawAsn1EntityData,
}

#[allow(non_snake_case)]
#[function_component([<$name Node >])]
pub fn [<__fn_ $name>](props: &[<$name NodeProps>]) -> Html {
let offset = props.meta.tag_position();
let length_len = props.meta.length_range().len();
let data_len = props.meta.data_range().len();

html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={stringify!($name)}/>
<span class="asn-simple-value">{props.node.string().to_owned()}</span>
</div>
}
}
}
};
}
91 changes: 8 additions & 83 deletions src/asn1/scheme/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,6 @@ use crate::asn1::node_options::NodeOptions;
use crate::asn1::scheme::build_asn1_schema;
use crate::asn1::HighlightAction;

#[derive(PartialEq, Properties, Clone)]
pub struct Utf8StringNodeProps {
pub node: OwnedUtf8String,
pub meta: OwnedRawAsn1EntityData,
}

#[function_component(Utf8StringNode)]
pub fn utf8_string(props: &Utf8StringNodeProps) -> Html {
let offset = props.meta.tag_position();
let length_len = props.meta.length_range().len();
let data_len = props.meta.data_range().len();

html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("UTF8String")}/>
<span class="asn-simple-value">{props.node.string().to_owned()}</span>
</div>
}
}

#[derive(PartialEq, Properties, Clone)]
pub struct OctetStringNodeProps {
pub node: OwnedOctetString,
Expand All @@ -48,7 +28,7 @@ pub fn octet_string(props: &OctetStringNodeProps) -> Html {
Some(asn1) => html! {
<div style="cursor: crosshair">
<div class="asn1-constructor-header">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("Octet String")}/>
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("OctetString")}/>
</div>
<div class="asn1-constructor-body">
{build_asn1_schema(asn1, &props.cur_node, &props.set_cur_node)}
Expand All @@ -62,7 +42,7 @@ pub fn octet_string(props: &OctetStringNodeProps) -> Html {
};
html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("Octet String")} />
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("OctetString")} />
<span class="asn1-node-info-label">{format!("({} bytes)", octets.len())}</span>
<span class="asn-simple-value">{encoded_octets}</span>
</div>
Expand Down Expand Up @@ -94,7 +74,7 @@ pub fn bit_string(props: &BitStringNodeProps) -> Html {

html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("Bit String")} />
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("BitString")} />
<span class="asn1-node-info-label">{format!("({} bits)", bits_amount)}</span>
<span class="asn-simple-value">{bits}</span>
</div>
Expand Down Expand Up @@ -123,68 +103,13 @@ pub fn bit_string(props: &BmpStringNodeProps) -> Html {

html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("Bmp String")} />
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("BmpString")} />
<span class="asn-simple-value">{s}</span>
</div>
}
}

#[derive(PartialEq, Properties, Clone)]
pub struct IA5StringNodeProps {
pub node: OwnedIA5String,
pub meta: OwnedRawAsn1EntityData,
}

#[function_component(IA5StringNode)]
pub fn utf8_string(props: &IA5StringNodeProps) -> Html {
let offset = props.meta.tag_position();
let length_len = props.meta.length_range().len();
let data_len = props.meta.data_range().len();

html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("IA5String")}/>
<span class="asn-simple-value">{props.node.string().to_owned()}</span>
</div>
}
}

#[derive(PartialEq, Properties, Clone)]
pub struct PrintableStringNodeProps {
pub node: OwnedPrintableString,
pub meta: OwnedRawAsn1EntityData,
}

#[function_component(PrintableStringNode)]
pub fn utf8_string(props: &PrintableStringNodeProps) -> Html {
let offset = props.meta.tag_position();
let length_len = props.meta.length_range().len();
let data_len = props.meta.data_range().len();

html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("PrintableString")}/>
<span class="asn-simple-value">{props.node.string().to_owned()}</span>
</div>
}
}

#[derive(PartialEq, Properties, Clone)]
pub struct GeneralStringNodeProps {
pub node: OwnedGeneralString,
pub meta: OwnedRawAsn1EntityData,
}

#[function_component(GeneralStringNode)]
pub fn utf8_string(props: &GeneralStringNodeProps) -> Html {
let offset = props.meta.tag_position();
let length_len = props.meta.length_range().len();
let data_len = props.meta.data_range().len();

html! {
<div class="terminal-asn1-node">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("GeneralString")}/>
<span class="asn-simple-value">{props.node.string().to_owned()}</span>
</div>
}
}
define_string_node!(GeneralString);
define_string_node!(IA5String);
define_string_node!(PrintableString);
define_string_node!(Utf8String);

0 comments on commit 3b09192

Please sign in to comment.