2020-06-08 03:29:51 +03:00
|
|
|
package protobuf
|
|
|
|
|
|
|
|
import (
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/format"
|
|
|
|
"github.com/wader/fq/format/registry"
|
2022-01-15 20:55:57 +03:00
|
|
|
"github.com/wader/fq/internal/mathextra"
|
2021-08-17 13:06:32 +03:00
|
|
|
"github.com/wader/fq/pkg/decode"
|
2021-12-02 00:48:25 +03:00
|
|
|
"github.com/wader/fq/pkg/scalar"
|
2020-06-08 03:29:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-11-17 18:46:10 +03:00
|
|
|
registry.MustRegister(decode.Format{
|
2020-06-08 03:29:51 +03:00
|
|
|
Name: format.PROTOBUF,
|
|
|
|
Description: "Protobuf",
|
|
|
|
DecodeFn: protobufDecode,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
wireTypeVarint = 0
|
|
|
|
wireType64Bit = 1
|
|
|
|
wireTypeLengthDelimited = 2
|
|
|
|
wireType32Bit = 5
|
|
|
|
)
|
|
|
|
|
2021-12-02 00:48:25 +03:00
|
|
|
var wireTypeNames = scalar.UToSymStr{
|
2020-06-08 03:29:51 +03:00
|
|
|
0: "Varint",
|
|
|
|
1: "64-bit",
|
|
|
|
2: "Length-delimited",
|
|
|
|
5: "32-bit",
|
|
|
|
}
|
|
|
|
|
|
|
|
func varInt(d *decode.D) uint64 {
|
|
|
|
var n uint64
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
b := d.U8()
|
|
|
|
n = n | (b&0x7f)<<(7*i)
|
|
|
|
if b&0x80 == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func protobufDecodeField(d *decode.D, pbm *format.ProtoBufMessage) {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldStruct("field", func(d *decode.D) {
|
|
|
|
keyN := d.FieldUFn("key_n", varInt)
|
2020-06-08 03:29:51 +03:00
|
|
|
fieldNumber := keyN >> 3
|
|
|
|
wireType := keyN & 0x7
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueU("field_number", fieldNumber)
|
2021-12-02 00:48:25 +03:00
|
|
|
d.FieldValueU("wire_type", wireType, scalar.Sym(wireTypeNames[wireType]))
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
var value uint64
|
|
|
|
var length uint64
|
|
|
|
var valueStart int64
|
|
|
|
switch wireType {
|
|
|
|
case wireTypeVarint:
|
2021-11-05 17:04:26 +03:00
|
|
|
value = d.FieldUFn("wire_value", varInt)
|
2020-06-08 03:29:51 +03:00
|
|
|
case wireType64Bit:
|
|
|
|
value = d.FieldU64("wire_value")
|
|
|
|
case wireTypeLengthDelimited:
|
2021-11-05 17:04:26 +03:00
|
|
|
length = d.FieldUFn("length", varInt)
|
2020-06-08 03:29:51 +03:00
|
|
|
valueStart = d.Pos()
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldRawLen("wire_value", int64(length)*8)
|
2020-06-08 03:29:51 +03:00
|
|
|
case wireType32Bit:
|
|
|
|
value = d.FieldU32("wire_value")
|
|
|
|
}
|
|
|
|
|
|
|
|
if pbm != nil {
|
|
|
|
if pbf, ok := (*pbm)[int(fieldNumber)]; ok {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueStr("name", pbf.Name)
|
|
|
|
d.FieldValueStr("type", format.ProtoBufTypeNames[uint64(pbf.Type)])
|
2020-06-08 03:29:51 +03:00
|
|
|
|
|
|
|
switch pbf.Type {
|
|
|
|
case format.ProtoBufTypeInt32, format.ProtoBufTypeInt64:
|
2022-01-15 20:55:57 +03:00
|
|
|
v := mathextra.ZigZag(value)
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueS("value", v)
|
2020-06-08 03:29:51 +03:00
|
|
|
if len(pbf.Enums) > 0 {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
case format.ProtoBufTypeUInt32, format.ProtoBufTypeUInt64:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueU("value", value)
|
2020-06-08 03:29:51 +03:00
|
|
|
if len(pbf.Enums) > 0 {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueStr("enum", pbf.Enums[value])
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
case format.ProtoBufTypeSInt32, format.ProtoBufTypeSInt64:
|
|
|
|
// TODO: correct? 32 different?
|
2022-01-15 20:55:57 +03:00
|
|
|
v := mathextra.TwosComplement(64, value)
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueS("value", v)
|
2020-06-08 03:29:51 +03:00
|
|
|
if len(pbf.Enums) > 0 {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
|
2020-06-08 03:29:51 +03:00
|
|
|
}
|
|
|
|
case format.ProtoBufTypeBool:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueBool("value", value != 0)
|
2020-06-08 03:29:51 +03:00
|
|
|
case format.ProtoBufTypeEnum:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueStr("enum", pbf.Enums[value])
|
2020-06-08 03:29:51 +03:00
|
|
|
case format.ProtoBufTypeFixed64:
|
|
|
|
// TODO:
|
|
|
|
case format.ProtoBufTypeSFixed64:
|
|
|
|
// TODO:
|
|
|
|
case format.ProtoBufTypeDouble:
|
|
|
|
// TODO:
|
|
|
|
case format.ProtoBufTypeString:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueStr("value", string(d.BytesRange(valueStart, int(length))))
|
2020-06-08 03:29:51 +03:00
|
|
|
case format.ProtoBufTypeBytes:
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldValueRaw("value", d.BytesRange(valueStart, int(length)))
|
2020-06-08 03:29:51 +03:00
|
|
|
case format.ProtoBufTypeMessage:
|
|
|
|
// TODO: test
|
2021-11-05 17:04:26 +03:00
|
|
|
d.LenFn(int64(length)*8, func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
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) {
|
2021-11-05 17:04:26 +03:00
|
|
|
d.FieldArray("fields", func(d *decode.D) {
|
2020-06-08 03:29:51 +03:00
|
|
|
for d.BitsLeft() > 0 {
|
|
|
|
protobufDecodeField(d, pbm)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func protobufDecode(d *decode.D, in interface{}) interface{} {
|
|
|
|
var pbm *format.ProtoBufMessage
|
|
|
|
pbi, ok := in.(format.ProtoBufIn)
|
|
|
|
if ok {
|
|
|
|
pbm = &pbi.Message
|
|
|
|
}
|
|
|
|
|
|
|
|
protobufDecodeFields(d, pbm)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|