From 0a776204f9aee179b04da5402fef07f767d2d550 Mon Sep 17 00:00:00 2001 From: Jesse Coretta Date: Sun, 12 May 2024 20:31:07 -0700 Subject: [PATCH] major codec update --- asn.go | 7 +++++-- oid.go | 30 ++++++++++++++++++++++-------- oid_test.go | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/asn.go b/asn.go index 879125c..e9ed3b0 100644 --- a/asn.go +++ b/asn.go @@ -114,8 +114,11 @@ func (r ASN1Notation) Index(idx int) (nanf NameAndNumberForm, ok bool) { /* NewASN1Notation returns an instance of *[ASN1Notation] alongside an error. -Valid input forms for ASN.1 values are string (e.g.: "{iso(1)}") and string -slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...}). +Valid input forms for ASN.1 values are: + + - string (e.g.: "{iso(1) ... }") + - string slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...}) + - [NameAndNumberForm] slices ([][NameAndNumberForm]{...}) Note that the following root node abbreviations are supported: diff --git a/oid.go b/oid.go index 143ab8c..e150be1 100644 --- a/oid.go +++ b/oid.go @@ -113,11 +113,14 @@ func (r OID) Root() (nanf NameAndNumberForm) { /* NewOID creates an instance of [OID] and returns it alongside an error. -The correct raw input syntax is the ASN.1 [NameAndNumberForm] sequence syntax, i.e.: +Valid input forms for ASN.1 values are: - {iso(1) identified-organization(3) dod(6)} + - string (e.g.: "{iso(1) ... }") + - string slices (e.g.: []string{"iso(1)", "identified-organization(3)" ...}) + - [NameAndNumberForm] slices ([][NameAndNumberForm]{...}) -Not all [NameAndNumberForm] values (arcs) require actual names; they can be numbers alone or in the so-called nameAndNumber syntax (name(Number)). For example: +Not all [NameAndNumberForm] values (arcs) require actual names; they can be +numbers alone or in the so-called nameAndNumber syntax (name(Number)). For example: {iso(1) identified-organization(3) 6} @@ -136,10 +139,21 @@ other than as the respective root node. [NumberForm] values CANNOT be negative, but are unbounded in their magnitude. */ func NewOID(x any) (r *OID, err error) { + // prepare temporary instance t := new(OID) + r = new(OID) var nfs []string switch tv := x.(type) { + case []NameAndNumberForm: + t.nanf = ASN1Notation(tv) + if !t.Valid() { + err = errorf("%T instance did not pass validity checks: %#v", t, t) + break + } + r.nanf = t.nanf + r.parsed = true + return case string: nfs = fields(condenseWHSP(trimR(trimL(tv, `{`), `}`))) case []string: @@ -149,11 +163,12 @@ func NewOID(x any) (r *OID, err error) { return } - for i := 0; i < len(nfs) && err == nil; i++ { + for i := 0; i < len(nfs); i++ { var nanf *NameAndNumberForm - if nanf, err = NewNameAndNumberForm(nfs[i]); nanf != nil { - t.nanf = append(t.nanf, *nanf) + if nanf, err = NewNameAndNumberForm(nfs[i]); err != nil { + break } + t.nanf = append(t.nanf, *nanf) } if err == nil { @@ -162,9 +177,8 @@ func NewOID(x any) (r *OID, err error) { return } - r = new(OID) r.parsed = true - *r = *t + r.nanf = t.nanf } return diff --git a/oid_test.go b/oid_test.go index a0228c9..9ce219c 100644 --- a/oid_test.go +++ b/oid_test.go @@ -2,6 +2,7 @@ package objectid import ( "fmt" + "math/big" "testing" ) @@ -148,4 +149,17 @@ func TestOID_bogus(t *testing.T) { t.Errorf("%s successfully parsed bogus value; expected an error", t.Name()) return } + + if _, err := NewOID([]NameAndNumberForm{ + {identifier: `iso`, primaryIdentifier: NumberForm(*big.NewInt(1)), parsed: true}, + {identifier: `identified-organization`, primaryIdentifier: NumberForm(*big.NewInt(3)), parsed: true}, + }); err != nil { + t.Errorf("%s error: %v", t.Name(), err) + return + } + + if _, err := NewOID([]NameAndNumberForm{}); err == nil { + t.Errorf("%s error: %v", t.Name(), err) + return + } }