aboutsummaryrefslogtreecommitdiff
path: root/ber/common.go
blob: aded3ef09bcdacd7bf230bd66fbc1f44f71627ba (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
package ber

import (
	"bytes"
	"strconv"
	"strings"
)

type state struct{ bytes.Buffer }

func newState(b []byte) *state {
	s := &state{}
	s.Write(b)
	return s
}

func (s *state) subState() *state {
	ss := state{}
	ss.Write(s.next())
	return &ss
}

func (s *state) next() []byte {
	l := s.unmarshalLen()
	return s.Next(l)
}

type OID []int

func (o OID) Equal(p OID) bool {
	if len(o) != len(p) {
		return false
	}
	for i := range o {
		if o[i] != p[i] {
			return false
		}
	}
	return true
}

func (o OID) String() string {
	s := make([]string, len(o))
	for i, v := range o {
		s[i] = strconv.Itoa(v)
	}
	return strings.Join(s, ".")
}

type BitString []bool

func (o BitString) Equal(p BitString) bool {
	if len(o) != len(p) {
		return false
	}
	for i := range o {
		if o[i] != p[i] {
			return false
		}
	}
	return true
}

func (o BitString) String() string {
	var s string
	for i, bit := range o {
		if i != 0 && i%4 == 0 {
			s += " "
		}
		switch bit {
		case true:
			s += "1"
		case false:
			s += "0"
		}
	}
	return s
}

type Raw []byte