aboutsummaryrefslogtreecommitdiff
path: root/ber/class.go
blob: 0a98b80ea6fdfc69bc11be2deb570d5a171a8891 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package ber

type Class byte

const (
	Universal Class = iota << 6
	Application
	ContextSpecific
	Private
	classMask Class = 3 << 6
)

var classNames = map[Class]string{
	Universal:       "Universal",
	Application:     "Application",
	ContextSpecific: "Context-specific",
	Private:         "Private",
}

func (c Class) String() string { return classNames[c] }

type Kind byte

const (
	Primitive Kind = iota << 5
	Constructed
	kindMask Kind = 1 << 5
)

var kindNames = map[Kind]string{
	Primitive:   "Primitive",
	Constructed: "Constructed",
}

func (k Kind) String() string { return kindNames[k] }

type Tag int

const (
	EOT Tag = iota
	Boolean
	Integer
	BitString
	OctetString
	Null
	ObjectIdentifier
	ObjectDescriptor
	InstanceOf
	Real
	Enumerated
	EmbeddedPDV
	UTF8String
	RelativeOID
	_
	_
	Sequence // SequenceOf
	Set      // SetOf
	NumericString
	PrintableString
	TeletexString // T61String
	VideotexString
	IA5String
	UTCTime
	GeneralizedTime
	GraphicString
	VisibleString // ISO646String
	GeneralString
	UniversalString
	CharacterString
	BMPString
	tagMask Tag = (1 << 5) - 1
)

var tagNames = map[Tag]string{
	EOT:              "End-of-Content",
	Boolean:          "Boolean",
	Integer:          "Integer",
	BitString:        "Bit String",
	OctetString:      "Octet String",
	Null:             "Null",
	ObjectIdentifier: "Object Identifier",
	ObjectDescriptor: "Object Descriptor",
	InstanceOf:       "Instance Of",
	Real:             "Real",
	Enumerated:       "Enumerated",
	EmbeddedPDV:      "Embedded PDV",
	UTF8String:       "UTF8 String",
	RelativeOID:      "Relative OID",
	Sequence:         "Sequence / Of",
	Set:              "Set / Of",
	NumericString:    "Numeric String",
	PrintableString:  "Printable String",
	TeletexString:    "Teletext String",
	VideotexString:   "Videotex String",
	IA5String:        "IA5 String",
	UTCTime:          "UTC Time",
	GeneralizedTime:  "Generalized Time",
	GraphicString:    "Graphic String",
	VisibleString:    "Visible String",
	GeneralString:    "General String",
	UniversalString:  "Universal String",
	CharacterString:  "Character String",
	BMPString:        "BMP String",
}

func (t Tag) String() string { return tagNames[t] }

func Ident(b []byte) (Class, Kind, Tag, int) {
	var d, n int
	c := Class(b[0]) & classMask
	k := Kind(b[0]) & kindMask
	t := Tag(b[0]) & tagMask
	if t == tagMask {
		d, n = debase128(b[1:])
		t = Tag(d)
	}
	return c, k, t, n + 1
}

func Length(b []byte) (int, int) {
	if b[0] & 0x80 != 0 {
		l := int(b[0] & 0x7f)
		i := unmarshalUint(b[1:l+1])
		return int(i), int(l+1)
	} else {
		return int(b[0]), 1
	}
}