From b0a11e197ecdd1ee2b97c92d0fefe0a611a2b8ce Mon Sep 17 00:00:00 2001 From: Abhijit Gadgil Date: Sun, 12 Feb 2023 13:07:41 +0530 Subject: [PATCH 1/2] asn-compiler: Getting started with Tags resolution Added `Asn1ResolvedTag` to a base type as an `Option`. In the 'resolve' value of each ingeger etc, we'll be resolving the actual tag if present (or the module level support suggests `IMPLICIT/EXPLICIT` tags etc. Right now just basic structure definitions etc. --- .../src/parser/asn/structs/types/mod.rs | 3 +- .../src/resolver/asn/structs/types/base.rs | 28 +++++++++++++++---- .../src/resolver/asn/structs/types/mod.rs | 10 +++++++ .../src/resolver/asn/types/base/mod.rs | 10 ++++--- 4 files changed, 40 insertions(+), 11 deletions(-) diff --git a/asn-compiler/src/parser/asn/structs/types/mod.rs b/asn-compiler/src/parser/asn/structs/types/mod.rs index fef7447..ee41252 100644 --- a/asn-compiler/src/parser/asn/structs/types/mod.rs +++ b/asn-compiler/src/parser/asn/structs/types/mod.rs @@ -147,8 +147,9 @@ pub(crate) enum Asn1TagMode { Implicit, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub(crate) enum Asn1TagClass { + #[default] Universal, Application, ContextSpecific, diff --git a/asn-compiler/src/resolver/asn/structs/types/base.rs b/asn-compiler/src/resolver/asn/structs/types/base.rs index 7b40e4c..9cf9fb0 100644 --- a/asn-compiler/src/resolver/asn/structs/types/base.rs +++ b/asn-compiler/src/resolver/asn/structs/types/base.rs @@ -1,7 +1,7 @@ //! Structs for the resolved Base Types use std::collections::{BTreeSet, HashMap}; -use crate::resolver::asn::structs::types::constraints::Asn1ConstraintValueSet; +use crate::resolver::asn::structs::types::{constraints::Asn1ConstraintValueSet, Asn1ResolvedTag}; #[derive(Debug, Clone)] pub(crate) enum ResolvedBaseType { @@ -26,6 +26,7 @@ pub(crate) struct Asn1ResolvedInteger { pub(crate) signed: bool, pub(crate) resolved_constraints: Option, pub(crate) named_values: Option>, + pub(crate) tag: Option, } impl Default for Asn1ResolvedInteger { @@ -35,6 +36,7 @@ impl Default for Asn1ResolvedInteger { signed: true, named_values: None, resolved_constraints: None, + tag: None, } } } @@ -49,6 +51,7 @@ pub(crate) struct Asn1ResolvedEnumerated { pub(crate) excepts: Option>, pub(crate) named_root_values: Vec<(String, i128)>, pub(crate) named_ext_values: Vec<(String, i128)>, + pub(crate) tag: Option, } impl Default for Asn1ResolvedEnumerated { @@ -62,6 +65,7 @@ impl Default for Asn1ResolvedEnumerated { excepts: None, named_root_values: vec![], named_ext_values: vec![], + tag: None, } } } @@ -74,25 +78,34 @@ pub(crate) struct Asn1ResolvedBitString { // We support only up to 128 named bits, if more than that is required, change this to appropriate. value pub(crate) named_values: HashMap, + + pub(crate) tag: Option, } // Just an empty structure for Resolved `BOOLEAN` type. #[derive(Debug, Default, Clone)] -pub(crate) struct Asn1ResolvedBoolean; +pub(crate) struct Asn1ResolvedBoolean { + pub(crate) tag: Option, +} // Just an empty structure for Resolved `NULL` type. #[derive(Debug, Default, Clone)] -pub(crate) struct Asn1ResolvedNull; +pub(crate) struct Asn1ResolvedNull { + pub(crate) tag: Option, +} -// Just an empty structure for Resolved `NULL` type. +// Just an empty structure for Resolved `REAL` type. #[derive(Debug, Default, Clone)] -pub(crate) struct Asn1ResolvedReal; +pub(crate) struct Asn1ResolvedReal { + pub(crate) tag: Option, +} // A structure representing a Resolved `OCTET STRING`. `SIZE` Constraint is resolved as well. The // `CONTAINING` Constraint is not resolved. #[derive(Debug, Default, Clone)] pub(crate) struct Asn1ResolvedOctetString { pub(crate) size: Option, + pub(crate) tag: Option, } // A structure representing a Resolved `CharacterString`. `SIZE` Constraint is resolved as well. The @@ -100,7 +113,10 @@ pub(crate) struct Asn1ResolvedOctetString { pub(crate) struct Asn1ResolvedCharacterString { pub(crate) str_type: String, pub(crate) size: Option, + pub(crate) tag: Option, } #[derive(Debug, Default, Clone)] -pub(crate) struct Asn1ResolvedObjectIdentifier; +pub(crate) struct Asn1ResolvedObjectIdentifier { + pub(crate) tag: Asn1ResolvedTag, +} diff --git a/asn-compiler/src/resolver/asn/structs/types/mod.rs b/asn-compiler/src/resolver/asn/structs/types/mod.rs index de7898d..0072be5 100644 --- a/asn-compiler/src/resolver/asn/structs/types/mod.rs +++ b/asn-compiler/src/resolver/asn/structs/types/mod.rs @@ -1,5 +1,7 @@ use std::collections::BTreeMap; +use crate::parser::asn::structs::types::Asn1TagClass; + pub(crate) mod constructed; use constructed::ResolvedConstructedType; @@ -32,3 +34,11 @@ pub(crate) enum Asn1ResolvedType { // A Set of Resolved Types. This is true if the type is obtained from Object Sets or Value Sets Set(ResolvedSetType), } + +// When tags are to be supported, an instance of this class will be available for each of the +// 'resolved' type. +#[derive(Debug, Clone, Default)] +pub(crate) struct Asn1ResolvedTag { + num: u32, + class: Asn1TagClass, +} diff --git a/asn-compiler/src/resolver/asn/types/base/mod.rs b/asn-compiler/src/resolver/asn/types/base/mod.rs index 5b359e4..187e86c 100644 --- a/asn-compiler/src/resolver/asn/types/base/mod.rs +++ b/asn-compiler/src/resolver/asn/types/base/mod.rs @@ -38,7 +38,9 @@ pub(crate) fn resolve_base_type( Asn1BuiltinType::BitString(ref b) => Ok(ResolvedBaseType::BitString( Asn1ResolvedBitString::resolve_bit_string(ty, b, resolver)?, )), - Asn1BuiltinType::Boolean => Ok(ResolvedBaseType::Boolean(Asn1ResolvedBoolean)), + Asn1BuiltinType::Boolean => { + Ok(ResolvedBaseType::Boolean(Asn1ResolvedBoolean::default())) + } Asn1BuiltinType::OctetString => Ok(ResolvedBaseType::OctetString( Asn1ResolvedOctetString::resolve_octet_string(ty, resolver)?, )), @@ -46,10 +48,10 @@ pub(crate) fn resolve_base_type( Asn1ResolvedCharacterString::resolve_character_string(ty, resolver)?, )), Asn1BuiltinType::ObjectIdentifier => Ok(ResolvedBaseType::ObjectIdentifier( - Asn1ResolvedObjectIdentifier, + Asn1ResolvedObjectIdentifier::default(), )), - Asn1BuiltinType::Null => Ok(ResolvedBaseType::Null(Asn1ResolvedNull)), - Asn1BuiltinType::Real => Ok(ResolvedBaseType::Real(Asn1ResolvedReal)), + Asn1BuiltinType::Null => Ok(ResolvedBaseType::Null(Asn1ResolvedNull::default())), + Asn1BuiltinType::Real => Ok(ResolvedBaseType::Real(Asn1ResolvedReal::default())), _ => Err(resolve_error!( "parse_base_type: Not Implemented! {:#?}", ty From 3e7afc4af0e22f97e4eb4846e9888b83f2d3f3c7 Mon Sep 17 00:00:00 2001 From: Abhijit Gadgil Date: Fri, 5 Jan 2024 12:54:58 +0530 Subject: [PATCH 2/2] Test CLA --- LICENSE | 1 + 1 file changed, 1 insertion(+) diff --git a/LICENSE b/LICENSE index 3bb80e2..f00c68c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ Copyright (c) 2021 Hampi Contributors +Copyright (c) 2024 hyphenOs Software Labs This code is dual licensed as MIT Apache-2.0. See LICENSE-MIT and LICENSE-Apache2