1
1
mirror of https://github.com/wader/fq.git synced 2024-11-29 23:27:12 +03:00
fq/format/protobuf/protobuf.go

140 lines
3.6 KiB
Go
Raw Normal View History

2020-06-08 03:29:51 +03:00
package protobuf
// https://developers.google.com/protocol-buffers/docs/encoding
2020-06-08 03:29:51 +03:00
import (
"embed"
"github.com/wader/fq/format"
"github.com/wader/fq/internal/mathex"
"github.com/wader/fq/pkg/bitio"
"github.com/wader/fq/pkg/decode"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/scalar"
2020-06-08 03:29:51 +03:00
)
//go:embed protobuf.md
var protobufFS embed.FS
2020-06-08 03:29:51 +03:00
func init() {
interp.RegisterFormat(
format.Protobuf,
&decode.Format{
Description: "Protobuf",
DecodeFn: protobufDecode,
})
interp.RegisterFS(protobufFS)
2020-06-08 03:29:51 +03:00
}
const (
wireTypeVarint = 0
wireType64Bit = 1
wireTypeLengthDelimited = 2
wireType32Bit = 5
)
var wireTypeNames = scalar.UintMapSymStr{
0: "varint",
1: "64bit",
2: "length_delimited",
5: "32bit",
2020-06-08 03:29:51 +03:00
}
func protobufDecodeField(d *decode.D, pbm *format.ProtoBufMessage) {
d.FieldStruct("field", func(d *decode.D) {
2022-08-20 13:05:07 +03:00
keyN := d.FieldULEB128("key_n")
2020-06-08 03:29:51 +03:00
fieldNumber := keyN >> 3
wireType := keyN & 0x7
d.FieldValueUint("field_number", fieldNumber)
d.FieldValueUint("wire_type", wireType, scalar.UintSym(wireTypeNames[wireType]))
2020-06-08 03:29:51 +03:00
var value uint64
var length uint64
var valueStart int64
switch wireType {
case wireTypeVarint:
2022-08-20 13:05:07 +03:00
value = d.FieldULEB128("wire_value")
2020-06-08 03:29:51 +03:00
case wireType64Bit:
value = d.FieldU64("wire_value")
case wireTypeLengthDelimited:
2022-08-20 13:05:07 +03:00
length = d.FieldULEB128("length")
2020-06-08 03:29:51 +03:00
valueStart = d.Pos()
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 {
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-08-31 11:34:32 +03:00
v := mathex.ZigZag[uint64, int64](value)
d.FieldValueSint("value", v)
2020-06-08 03:29:51 +03:00
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
2020-06-08 03:29:51 +03:00
}
case format.ProtoBufTypeUInt32, format.ProtoBufTypeUInt64:
d.FieldValueUint("value", value)
2020-06-08 03:29:51 +03:00
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[value])
2020-06-08 03:29:51 +03:00
}
case format.ProtoBufTypeSInt32, format.ProtoBufTypeSInt64:
// TODO: correct? 32 different?
v := mathex.TwosComplement(64, value)
d.FieldValueSint("value", v)
2020-06-08 03:29:51 +03:00
if len(pbf.Enums) > 0 {
d.FieldValueStr("enum", pbf.Enums[uint64(v)])
2020-06-08 03:29:51 +03:00
}
case format.ProtoBufTypeBool:
d.FieldValueBool("value", value != 0)
2020-06-08 03:29:51 +03:00
case format.ProtoBufTypeEnum:
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:
d.FieldValueStr("value", string(d.BytesRange(valueStart, int(length))))
2020-06-08 03:29:51 +03:00
case format.ProtoBufTypeBytes:
d.FieldValueBitBuf("value", bitio.NewBitReader(d.BytesRange(valueStart, int(length)), -1))
2020-06-08 03:29:51 +03:00
case format.ProtoBufTypeMessage:
// TODO: test
d.FramedFn(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) {
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) any {
2023-05-01 14:19:04 +03:00
var pbi format.Protobuf_In
d.ArgAs(&pbi)
2020-06-08 03:29:51 +03:00
protobufDecodeFields(d, &pbi.Message)
2020-06-08 03:29:51 +03:00
return nil
}