forked from wader/fq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotobuf.go
140 lines (125 loc) · 3.49 KB
/
protobuf.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
package protobuf
// https://developers.google.com/protocol-buffers/docs/encoding
import (
"embed"
"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathx"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
)
//go:embed protobuf.md
var protobufFS embed.FS
func init() {
interp.RegisterFormat(
format.Protobuf,
&decode.Format{
Description: "Protobuf",
DecodeFn: protobufDecode,
})
interp.RegisterFS(protobufFS)
}
const (
wireTypeVarint = 0
wireType64Bit = 1
wireTypeLengthDelimited = 2
wireType32Bit = 5
)
var wireTypeNames = scalar.UintMapSymStr{
0: "varint",
1: "64bit",
2: "length_delimited",
5: "32bit",
}
func protobufDecodeField(d *decode.D, pbm *format.ProtoBufMessage) {
d.FieldStruct("field", func(d *decode.D) {
keyN := d.FieldULEB128("key_n")
fieldNumber := keyN >> 3
wireType := keyN & 0x7
d.FieldValueUint("field_number", fieldNumber)
d.FieldValueUint("wire_type", wireType, scalar.UintSym(wireTypeNames[wireType]))
var value uint64
var length uint64
var valuePos int64
switch wireType {
case wireTypeVarint:
value = d.FieldULEB128("wire_value")
case wireType64Bit:
value = d.FieldU64("wire_value")
case wireTypeLengthDelimited:
length = d.FieldULEB128("length")
valuePos = d.Pos()
d.FieldRawLen("wire_value", int64(length)*8)
case wireType32Bit:
value = d.FieldU32("wire_value")
}
if pbm != nil {
if pbf, ok := (*pbm)[int(fieldNumber)]; ok {
d.FieldValueStr("name", pbf.Name)
d.FieldValueStr("type", format.ProtoBufTypeNames[uint64(pbf.Type)])
switch pbf.Type {
case format.ProtoBufTypeInt32, format.ProtoBufTypeInt64:
v := mathx.ZigZag[uint64, int64](value)
d.FieldValueSint("value", v)
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
}
case format.ProtoBufTypeUInt32, format.ProtoBufTypeUInt64:
d.FieldValueUint("value", value)
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[value])
}
case format.ProtoBufTypeSInt32, format.ProtoBufTypeSInt64:
// TODO: correct? 32 different?
v := mathx.TwosComplement(64, value)
d.FieldValueSint("value", v)
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
}
case format.ProtoBufTypeBool:
d.FieldValueBool("value", value != 0)
case format.ProtoBufTypeEnum:
d.FieldValueStr("enum", pbf.Enums[value])
case format.ProtoBufTypeFixed64:
// TODO:
case format.ProtoBufTypeSFixed64:
// TODO:
case format.ProtoBufTypeDouble:
// TODO:
case format.ProtoBufTypeString:
d.SeekAbs(valuePos)
d.FieldUTF8("value", int(length))
case format.ProtoBufTypeBytes:
d.SeekAbs(valuePos)
d.FieldRawLen("value", int64(length)*8)
case format.ProtoBufTypeMessage:
// TODO: test
d.FramedFn(int64(length)*8, func(d *decode.D) {
protobufDecodeFields(d, &pbf.Message)
})
case format.ProtoBufTypePackedRepeated:
// TODO:
case format.ProtoBufTypeFixed32:
// TODO:
case format.ProtoBufTypeSFixed32:
// TODO:
case format.ProtoBufTypeFloat:
// TODO:
}
}
}
})
}
func protobufDecodeFields(d *decode.D, pbm *format.ProtoBufMessage) {
d.FieldArray("fields", func(d *decode.D) {
for d.BitsLeft() > 0 {
protobufDecodeField(d, pbm)
}
})
}
func protobufDecode(d *decode.D) any {
var pbi format.Protobuf_In
d.ArgAs(&pbi)
protobufDecodeFields(d, &pbi.Message)
return nil
}