aboutsummaryrefslogtreecommitdiff
path: root/ber/new/common.go
blob: 823dc23456e4526226f9a26837078d1eb2a3d225 (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
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 {
	return s.Next(s.unmarshalLen())
}

type OID []int

func (o OID) Equal(p OID) bool {
	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 {
	for i := range o {
		if o[i] != p[i] {
			return false
		}
	}
	return true
}

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