-
Notifications
You must be signed in to change notification settings - Fork 0
/
pbdump.go
178 lines (153 loc) · 3.46 KB
/
pbdump.go
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package pbdump
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"strconv"
"strings"
)
type decoder func(io.ByteReader) (fmt.Stringer, error)
var (
decoders []decoder
)
func init() {
decoders = []decoder{decodeVarint, decodeDouble, decodeLengthDelimited}
}
type key struct {
Tag, Type int
}
type StringerString string
func (s StringerString) String() string {
return string(s)
}
type StringerVarint uint64
func (s StringerVarint) String() string {
return fmt.Sprint(uint64(s))
}
type StringerRepeated []fmt.Stringer
func (s StringerRepeated) String() string {
tmp := make([]string, len(s))
for i, v := range s {
tmp[i] = "'" + v.String() + "'"
}
return "{" + strings.Join(tmp, ";") + "}"
}
type StringerMessage struct {
attributes map[int]StringerRepeated
rawPayload []byte
}
func (s StringerMessage) String() string {
buf := ""
for k, v := range s.attributes {
buf += fmt.Sprint(k) + " -> " + fmt.Sprint(v) + ", "
}
buf += "raw: " + fmt.Sprint(string(s.rawPayload))
return buf
}
type StringerDouble float64
func (s StringerDouble) String() string {
return fmt.Sprint(float64(s))
}
func Dump(r io.ByteReader) (StringerMessage, error) {
if m, err := decodeMessage(r); err != nil {
return StringerMessage{}, err
} else {
return m.(StringerMessage), nil
}
}
func decodeMessage(r io.ByteReader) (fmt.Stringer, error) {
m := StringerMessage{make(map[int]StringerRepeated), nil}
for {
k, err := readKey(r)
if err == io.EOF {
break
} else if err != nil {
return nil, err
}
v, err := decoders[k.Type](r)
if err != nil {
return nil, err
}
if s, ok := m.attributes[k.Tag]; ok {
m.attributes[k.Tag] = append(s, v)
} else {
m.attributes[k.Tag] = StringerRepeated{v}
}
}
return m, nil
}
func decodeVarint(r io.ByteReader) (fmt.Stringer, error) {
if n, err := binary.ReadUvarint(r); err != nil {
return nil, err
} else {
return StringerVarint(n), nil
}
}
type ByteReaderReader struct {
r io.ByteReader
}
func (r *ByteReaderReader) Read(b []byte) (int, error) {
for i := 0; i < len(b); i++ {
c, err := r.r.ReadByte()
if err != nil {
return i, err
}
b[i] = c
}
return len(b), nil
}
func decodeLengthDelimited(r io.ByteReader) (fmt.Stringer, error) {
if l, err := binary.ReadUvarint(r); err != nil {
return nil, err
} else {
b := make([]byte, l)
fullReader := ByteReaderReader{r}
if n, err := fullReader.Read(b); err != nil {
return nil, err
} else if uint64(n) != l {
return nil, fmt.Errorf("Too little data")
}
buf := bytes.NewBuffer(b)
raw := make([]byte, len(b))
copy(raw, b)
if msg, err := decodeMessage(buf); err != nil {
return StringerString(string(b)), nil
} else {
smsg, _ := msg.(StringerMessage)
smsg.rawPayload = raw
return smsg, nil
}
}
}
func decodeDouble(r io.ByteReader) (fmt.Stringer, error) {
fullReader := ByteReaderReader{r}
var d float64
if err := binary.Read(&fullReader, binary.LittleEndian, &d); err != nil {
return nil, err
} else {
return StringerDouble(d), nil
}
}
func readKey(r io.ByteReader) (key, error) {
n, err := binary.ReadUvarint(r)
if err != nil {
return key{}, err
}
k := key{int(n >> 3), int(n & 0x7)}
if !isSupportedWireType(k.Type) {
return key{}, fmt.Errorf("Unsupported wire type '%d'", k.Type)
}
return k, nil
}
func isSupportedWireType(t int) bool {
return (t >= 0) && (t < len(decoders)) && (decoders[t] != nil)
}
func isPrintable(s string) bool {
for _, r := range s {
if !strconv.IsPrint(r) {
return false
}
}
return true
}