Skip to content

Commit

Permalink
feat(crypto-helper): asn1: implement Set rendering;
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBestTvarynka committed Jan 9, 2024
1 parent da575d3 commit 810c6dc
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/asn1-parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ This `asn1` parser is aimed to parse input bytes and return an AST as the result

- [X] [Sequence](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/sequence.html)
- [ ] [SequenceOf](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/sequenceof.html)
- [ ] [Set](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/set.html)
- [X] [Set](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/set.html)
- [ ] [SetOf](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/setof.html)
- [ ] [Choice](https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/choice.html)

Expand Down
5 changes: 1 addition & 4 deletions crates/prop-strategies/src/constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use proptest::collection::vec;
use proptest::prop_oneof;
use proptest::strategy::{Just, Strategy};

use crate::{
any_bit_string, any_bmp_string, any_bool, any_integer, any_null, any_octet_string,
any_utf8_string,
};
use crate::{any_bit_string, any_bmp_string, any_bool, any_integer, any_null, any_octet_string, any_utf8_string};

fn any_leaf_asn1_type() -> impl Strategy<Value = OwnedAsn1Type> {
prop_oneof![
Expand Down
6 changes: 6 additions & 0 deletions src/asn1/hex_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@ fn build_data_bytes(
.iter()
.for_each(move |asn1| build_hex_bytes(asn1, cur_node, set_cur_node.clone(), bytes, select_all));
}
Asn1Type::Set(set) => {
let set_cur_node = set_cur_node.clone();
set.fields()
.iter()
.for_each(move |asn1| build_hex_bytes(asn1, cur_node, set_cur_node.clone(), bytes, select_all));
}
Asn1Type::OctetString(octet) => match octet.inner() {
Some(asn1) => build_hex_bytes(asn1, cur_node, set_cur_node.clone(), bytes, select_all),
None => default_bytes(asn1_node_id, cur_node, set_cur_node, asn1, bytes, select_all),
Expand Down
7 changes: 7 additions & 0 deletions src/asn1/scheme.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod oid;
mod primitive;
mod sequence;
mod set;
mod strings;
mod tag;

Expand All @@ -14,6 +15,7 @@ use self::primitive::{BoolNode, IntegerNode, NullNode};
use self::sequence::SequenceNode;
use self::strings::{BitStringNode, BmpStringNode, OctetStringNode, Utf8StringNode};
use self::tag::{ApplicationTagNode, ExplicitTagNode};
use crate::asn1::scheme::set::SetNode;
use crate::asn1::HighlightAction;

#[derive(PartialEq, Properties, Clone)]
Expand Down Expand Up @@ -70,6 +72,11 @@ pub fn build_asn1_schema(asn1: &Asn1<'_>, cur_id: &Option<u64>, set_cur_node: &C
<SequenceNode node={sequence.to_owned()} cur_node={cur_id} set_cur_node={set_cur_node.clone()} meta={asn1.meta().to_owned()} />
</Asn1Node>
},
Asn1Type::Set(set) => html! {
<Asn1Node id={asn1.id()} {cur_id} set_cur_node={set_cur_node.clone()}>
<SetNode node={set.to_owned()} cur_node={cur_id} set_cur_node={set_cur_node.clone()} meta={asn1.meta().to_owned()} />
</Asn1Node>
},
Asn1Type::BitString(bit) => html! {
<Asn1Node id={asn1.id()} {cur_id} set_cur_node={set_cur_node.clone()}>
<BitStringNode node={bit.to_owned()} meta={asn1.meta().to_owned()} />
Expand Down
41 changes: 41 additions & 0 deletions src/asn1/scheme/set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use asn1_parser::{OwnedRawAsn1EntityData, OwnedSet};
use yew::{function_component, html, Callback, Html, Properties};

use crate::asn1::node_options::NodeOptions;
use crate::asn1::scheme::build_asn1_schema;
use crate::asn1::HighlightAction;

#[derive(PartialEq, Properties, Clone)]
pub struct SetNodeProps {
pub node: OwnedSet,
pub cur_node: Option<u64>,
pub set_cur_node: Callback<HighlightAction>,
pub meta: OwnedRawAsn1EntityData,
}

#[function_component(SetNode)]
pub fn set(props: &SetNodeProps) -> Html {
let fields = props.node.fields();

let set_cur_node = &props.set_cur_node;
let fields_components = fields
.iter()
.map(|f| build_asn1_schema(f, &props.cur_node, set_cur_node))
.collect::<Vec<_>>();

let offset = props.meta.tag_position();
let length_len = props.meta.length_range().len();
let data_len = props.meta.data_range().len();

html! {
<div style="cursor: crosshair; width: 100%">
<div class="asn1-constructor-header">
<NodeOptions node_bytes={props.meta.raw_bytes().to_vec()} {offset} {length_len} {data_len} name={String::from("Set")}/>
<span class="asn1-node-info-label">{format!("({} fields)", fields.len())}</span>
</div>
<div class="asn1-constructor-body">
{fields_components}
</div>
</div>
}
}

0 comments on commit 810c6dc

Please sign in to comment.