Skip to content

Commit

Permalink
Merge pull request #1 from JesseCoretta/v1-dev
Browse files Browse the repository at this point in the history
stable release prep
  • Loading branch information
JesseCoretta authored Oct 8, 2023
2 parents 2d40225 + 7a38683 commit ab9703f
Show file tree
Hide file tree
Showing 16 changed files with 1,262 additions and 503 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
# go-objectid

[![Go Report Card](https://goreportcard.com/badge/JesseCoretta/go-objectid)](https://goreportcard.com/report/github.com/JesseCoretta/go-objectid) [![Go Reference](https://pkg.go.dev/badge/github.com/JesseCoretta/go-objectid.svg)](https://pkg.go.dev/github.com/JesseCoretta/go-objectid) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/JesseCoretta/go-objectid/blob/main/LICENSE) [![codecov](https://codecov.io/gh/JesseCoretta/go-objectid/graph/badge.svg?token=RLW4DHLKQP)](https://codecov.io/gh/JesseCoretta/go-objectid) [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/JesseCoretta/go-objectid/issues) [![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/JesseCoretta/go-objectid/go.yml)](https://github.com/JesseCoretta/go-objectid/actions/workflows/go.yml) [![Author](https://img.shields.io/badge/author-Jesse_Coretta-darkred?label=%F0%9F%94%BA&labelColor=indigo&color=maroon)](https://www.linkedin.com/in/jessecoretta/) [![Libraries.io dependency status for GitHub repo](https://img.shields.io/librariesio/github/JesseCoretta/go-objectid)](https://github/JesseCoretta/go-objectid) [![Help Animals](https://img.shields.io/badge/donations-yellow?label=%F0%9F%98%BA&labelColor=Yellow)](https://github.com/JesseCoretta/JesseCoretta/blob/main/DONATIONS.md)
[![Go Report Card](https://goreportcard.com/badge/JesseCoretta/go-objectid)](https://goreportcard.com/report/github.com/JesseCoretta/go-objectid) [![Go Reference](https://pkg.go.dev/badge/github.com/JesseCoretta/go-objectid.svg)](https://pkg.go.dev/github.com/JesseCoretta/go-objectid) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat)](https://github.com/JesseCoretta/go-objectid/blob/main/LICENSE) [![codecov](https://codecov.io/gh/JesseCoretta/go-objectid/graph/badge.svg?token=RLW4DHLKQP)](https://codecov.io/gh/JesseCoretta/go-objectid) [![contributions welcome](https://img.shields.io/badge/contributions-welcome-brightgreen.svg?style=flat)](https://github.com/JesseCoretta/go-objectid/issues) [![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/JesseCoretta/go-objectid/go.yml)](https://github.com/JesseCoretta/go-objectid/actions/workflows/go.yml) [![Author](https://img.shields.io/badge/author-Jesse_Coretta-darkred?label=%F0%9F%94%BA&labelColor=indigo&color=maroon)](https://www.linkedin.com/in/jessecoretta/) [![Help Animals](https://img.shields.io/badge/donations-yellow?label=%F0%9F%98%BA&labelColor=Yellow)](https://github.com/JesseCoretta/JesseCoretta/blob/main/DONATIONS.md)

<!-- [![GitHub release (with filter)](https://img.shields.io/github/v/release/JesseCoretta/go-objectid)](https://github.com/JesseCoretta/go-objectid/releases) -->
Package objectid offers convenient ASN.1 Object Identifier types with useful methods and a uint128-based NumberForm type allowing X.670 support.

<!-- [![GitHub Workflow Status (with event)](https://img.shields.io/github/actions/workflow/status/JesseCoretta/go-objectid/go.yml)](https://github.com/JesseCoretta/go-objectid/actions/workflows/go.yml) -->

Package objectid offers a convenient ASN.1 Object Identifier type and associated methods.

ASN.1 Object Identifiers encompass information that goes beyond their dotted representation. This tiny package merely facilitates the handling of ASN.1 NameAndNumberForm values and alternate names that may be associated with a given OID in the wild.

## Uint128 Support

Unsigned 128-bit integer support for individual NumberForm values is made possible due to the private incorporation of Luke Champine's awesome Uint128 type, which manifests here through instances of the package-provided NumberForm type.
202 changes: 86 additions & 116 deletions asn.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ func (a ASN1Notation) Len() int { return len(a) }
IsZero returns a boolean indicative of whether the receiver
is unset.
*/
func (a ASN1Notation) IsZero() bool {
if &a == nil {
return true
func (a ASN1Notation) IsZero() (is bool) {
if is = &a == nil; !is {
is = a.Len() == 0
}
return a.Len() == 0

return
}

/*
Expand All @@ -68,21 +69,19 @@ func (a ASN1Notation) Index(idx int) (nanf NameAndNumberForm, ok bool) {
L := a.Len()

// Bail if receiver is empty.
if L == 0 {
return
}

if idx < 0 {
var x int = L + idx
if x < 0 {
nanf = a[0]
if L > 0 {
if idx < 0 {
var x int = L + idx
if x < 0 {
nanf = a[0]
} else {
nanf = a[x]
}
} else if idx > L {
nanf = a[L-1]
} else {
nanf = a[x]
nanf = a[idx]
}
} else if idx > L {
nanf = a[L-1]
} else {
nanf = a[idx]
}

// Make sure the instance was produced
Expand All @@ -104,39 +103,37 @@ func NewASN1Notation(x any) (a *ASN1Notation, err error) {
// prepare temporary instance
t := new(ASN1Notation)

var nfs []string
switch tv := x.(type) {
case string:
f := fields(condenseWHSP(trimR(trimL(tv, `{`), `}`)))
for i := 0; i < len(f); i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(f[i]); err != nil {
return
}
*t = append(*t, *nanf)
}
nfs = fields(condenseWHSP(trimR(trimL(tv, `{`), `}`)))
case []string:
for i := 0; i < len(tv); i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(condenseWHSP(tv[i])); err != nil {
return
}
*t = append(*t, *nanf)
}
nfs = tv
default:
err = errorf("Unsupported %T input type: %#v\n", x, x)
err = errorf("Unsupported %T input type: %#v", x, x)
return
}

// verify content is valid
if !t.Valid() {
err = errorf("%T instance did not pass validity checks: %#v", t, *t)
for i := 0; i < len(nfs) && err == nil; i++ {
var nanf *NameAndNumberForm
if nanf, err = NewNameAndNumberForm(nfs[i]); nanf != nil {
*t = append(*t, *nanf)
}
}

if err != nil {
return
}

// transfer temporary content
// to return value instance.
a = new(ASN1Notation)
*a = *t
// verify content is valid
err = errorf("%T instance did not pass validity checks: %#v", t, *t)
if t.Valid() {
// transfer temporary content
// to return value instance.
a = new(ASN1Notation)
*a = *t
err = nil
}

return
}
Expand All @@ -145,28 +142,17 @@ func NewASN1Notation(x any) (a *ASN1Notation, err error) {
Valid returns a boolean value indicative of whether the receiver's
length is greater than or equal to one (1) slice member.
*/
func (a ASN1Notation) Valid() bool {
func (a ASN1Notation) Valid() (is bool) {
// Don't waste time on
// zero instances.
if a.Len() == 0 {
return false
}

// bail out if any of the slice
// values are unparsed.
for i := 0; i < a.Len(); i++ {
if !a[i].parsed {
return false
if L := a.Len(); L > 0 {
if root, ok := a.Index(0); ok {
// root cannot be greater than 2
is = root.NumberForm().Lt(3)
}
}

root, ok := a.Index(0)
if !ok {
return false
}

// root cannot be greater than 2
return root.NumberForm().Lt(3)
return
}

/*
Expand All @@ -177,12 +163,10 @@ Empty slices of DotNotation are returned if the dotNotation value
within the receiver is less than two (2) NumberForm values in length.
*/
func (a ASN1Notation) Ancestry() (anc []ASN1Notation) {
if a.Len() < 2 {
return
}

for i := a.Len(); i > 0; i-- {
anc = append(anc, a[:i])
if a.Len() >= 2 {
for i := a.Len(); i > 0; i-- {
anc = append(anc, a[:i])
}
}

return
Expand All @@ -195,23 +179,18 @@ subordinate value. This creates a fully-qualified child ASN1Notation
value of the receiver.
*/
func (a ASN1Notation) NewSubordinate(nanf any) *ASN1Notation {
// Don't bother processing
if a.Len() == 0 {
return nil
}

// Prepare the new leaf numberForm,
// or die trying.
n, err := NewNameAndNumberForm(nanf)
if err != nil {
return nil
}

A := make(ASN1Notation, a.Len()+1, a.Len()+1)
for i := 0; i < a.Len(); i++ {
A[i] = a[i]
var A ASN1Notation
if a.Len() > 0 {
// Prepare the new leaf numberForm,
// or die trying.
if n, err := NewNameAndNumberForm(nanf); err == nil {
A = make(ASN1Notation, a.Len()+1, a.Len()+1)
for i := 0; i < a.Len(); i++ {
A[i] = a[i]
}
A[A.Len()-1] = *n
}
}
A[A.Len()-1] = *n

return &A
}
Expand All @@ -220,51 +199,42 @@ func (a ASN1Notation) NewSubordinate(nanf any) *ASN1Notation {
AncestorOf returns a boolean value indicative of whether the receiver
is an ancestor of the input value, which can be string or ASN1Notation.
*/
func (a ASN1Notation) AncestorOf(asn any) bool {
if a.IsZero() {
return false
}

var A *ASN1Notation

switch tv := asn.(type) {
case string:
var err error
if A, err = NewASN1Notation(tv); err != nil {
return false
}
case *ASN1Notation:
if tv == nil {
return false
}
A = tv
case ASN1Notation:
if tv.Len() == 0 {
return false
func (a ASN1Notation) AncestorOf(asn any) (anc bool) {
if !a.IsZero() {
var A *ASN1Notation

switch tv := asn.(type) {
case string:
A, _ = NewASN1Notation(tv)
case *ASN1Notation:
if tv != nil {
A = tv
}
case ASN1Notation:
if tv.Len() >= 0 {
A = &tv
}
}
A = &tv
default:
return false
}

if A.Len() < a.Len() {
return false
if A.Len() > a.Len() {
anc = a.matchASN1(A)
}
}

return a.matchASN1(A)
return
}

func (a ASN1Notation) matchASN1(asn *ASN1Notation) bool {
for i := 0; i < a.Len(); i++ {
func (a ASN1Notation) matchASN1(asn *ASN1Notation) (matched bool) {
L := a.Len()
ct := 0
for i := 0; i < L; i++ {
x, _ := a.Index(i)
y, ok := asn.Index(i)
if !ok {
return false
}
if !x.Equal(y) {
return false
if y, ok := asn.Index(i); ok {
if x.Equal(y) {
ct++
}
}
}

return true
return ct == L
}
Loading

0 comments on commit ab9703f

Please sign in to comment.