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

import "fmt"

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

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

	for i := indent; i > 0; i-- {
		fmt.Print("\t")
	}

	switch class {
	case classUniversal:
		switch tag {
		case tagInteger:
			fmt.Println(tag, UnmarshalInt(value))
		case tagObjectIdentifier:
			fmt.Println(tag, UnmarshalOID(value))
		case tagBitString:
			fmt.Println(tag, UnmarshalBitString(value), value)
		default:
			if len(value) > 5 {
				fmt.Println(tag, kind, value[:5], "...")
			} else {
				fmt.Println(tag, kind, value)
			}
		}
	default:
		if len(value) > 5 {
			fmt.Println(class, kind, byte(tag), value[:5], "...")
		} else {
			fmt.Println(class, kind, byte(tag), value)
		}
	}

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

	if len(rest) > 0 {
		dump(rest, indent)
	}

}