aboutsummaryrefslogtreecommitdiff
path: root/ber/dump.go
blob: 4b4b1b9b64d0031b5323439be25920e3ec045cd0 (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
package ber

import (
	"fmt"
)

func Dump(b []byte) string {
	return newState(b).dump(0)
}

func (s *state) dump(indent int) (ret string) {
	if s.Len() == 0 {
		return ""
	}
	for s.Len() > 0 {
		h := s.unmarshalClass()
		ret += fmt.Sprintf("%*s%v: ", 2*indent, "", h)
		if h.Class == classUniversal && h.Kind == kindPrimitive {
			switch h.Tag {
			case tagInteger, tagEnumerated:
				ret += fmt.Sprintln(s.unmarshalInt())
			case tagBoolean:
				ret += fmt.Sprintln(s.unmarshalBool())
			case tagObjectIdentifier:
				ret += fmt.Sprintln(s.unmarshalOID())
			case tagBitString:
				ret += fmt.Sprintln(s.unmarshalBitString())
			case tagIA5String, tagOctetString:
				ret += fmt.Sprintln(s.unmarshalString())
			default:
				ret += fmt.Sprintln(s.next())
			}
		} else {
			ret += fmt.Sprintln("→")
			ret += s.subState().dump(indent + 1)
		}
	}
	return ret + s.dump(indent)
}