jasn1/oid/oid.go

58 lines
1.1 KiB
Go

package oid
import (
"fmt"
"strconv"
)
type ObjectIdentifier struct {
Components []uint64
Name string
Alternate string
}
func (obj *ObjectIdentifier) String() string {
id := OidToStr(obj.Components)
return fmt.Sprintf("(%s:%s)", id, obj.Name)
}
func OidToStr(components []uint64) string {
oid := strconv.FormatUint(components[0], 10)
for _, component := range(components[1:]) {
oid += "." + strconv.FormatUint(component, 10)
}
return oid
}
func FindObjectByEncodedId(enc_id []byte) (*ObjectIdentifier, error) {
component := uint64(0)
components := []uint64 { 0, }
for _, piece := range enc_id {
component = (component << 7) | uint64(piece & 0x7F)
if ((piece & 0x80) >> 7) == 0 {
components = append(components, component)
component = 0
}
}
if components[1] < 80 {
components[0] = components[1] / 40
components[1] = components[1] % 40
} else {
components[0] = 2
components[1] = components[1] - 80
}
lookup := OidToStr(components)
oid, ok := OID_CATALOG[lookup]
if !ok {
return nil, fmt.Errorf("unknown oid %s", lookup)
}
return &oid, nil
}