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

import "fmt"

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

func short(b []byte) string {
	if len(b) > 5 {
		return fmt.Sprint(b[:5], " ...")
	}
	return fmt.Sprint(b)
}

func dump(b []byte, indent int) (s string) {
	class, kind, tag, _, value, rest := Split(b)

	s += fmt.Sprintf("%*s", indent*2, "")

	switch class {
	case classUniversal:
		switch tag {
		case tagInteger:
			s += fmt.Sprintln(tag, UnmarshalInt(value))
		case tagObjectIdentifier:
			s += fmt.Sprintln(tag, UnmarshalOID(value))
		case tagBitString:
			s += fmt.Sprintln(tag, UnmarshalBitString(value), value)
		default:
			s += fmt.Sprintln(tag, kind, short(value))
		}
	default:
		s += fmt.Sprintln(class, kind, byte(tag), short(value))
	}

	if len(value) > 0 && kind != kindPrimitive {
		s += dump(value, indent+1)
	}

	if len(rest) > 0 {
		s += dump(rest, indent)
	}
	return
}