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

import (
	"bytes"
	"fmt"
)

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

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

func dump(r *bytes.Buffer, indent int) (s string) {
	class, kind, tag, _, value := Split(r)

	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))
		case tagIA5String, tagOctetString:
			s += fmt.Sprintln(tag, string(value))
		case tagBoolean:
			s += fmt.Sprintln(tag, UnmarshalBool(value[0]))
		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(bytes.NewBuffer(value), indent+1)
	}

	if r.Len() > 0 {
		s += dump(r, indent)
	}
	return
}