2020-08-14 10:57:19 +00:00
|
|
|
/*
|
|
|
|
This package contains help functions for stable marshaller. Their usage is
|
|
|
|
totally optional. One can implement fast stable marshaller without these
|
|
|
|
runtime function calls.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package proto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
2021-03-24 07:10:30 +00:00
|
|
|
"math"
|
2020-08-14 10:57:19 +00:00
|
|
|
"math/bits"
|
2023-08-02 13:53:47 +00:00
|
|
|
|
|
|
|
"google.golang.org/protobuf/encoding/protowire"
|
2020-08-14 10:57:19 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 06:48:57 +00:00
|
|
|
type (
|
|
|
|
stableMarshaller interface {
|
2022-04-05 08:24:34 +00:00
|
|
|
StableMarshal([]byte) []byte
|
2020-08-18 06:48:57 +00:00
|
|
|
StableSize() int
|
|
|
|
}
|
2023-10-06 12:05:30 +00:00
|
|
|
|
|
|
|
setMarshalData interface {
|
|
|
|
SetMarshalData([]byte)
|
|
|
|
StableSize() int
|
|
|
|
}
|
2020-08-18 06:48:57 +00:00
|
|
|
)
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func BytesMarshal(field int, buf, v []byte) int {
|
2023-07-26 15:51:37 +00:00
|
|
|
return bytesMarshal(field, buf, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func BytesSize(field int, v []byte) int {
|
|
|
|
return bytesSize(field, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func bytesMarshal[T ~[]byte | ~string](field int, buf []byte, v T) int {
|
2020-08-14 10:57:19 +00:00
|
|
|
if len(v) == 0 {
|
2022-03-12 12:20:19 +00:00
|
|
|
return 0
|
2020-08-14 10:57:19 +00:00
|
|
|
}
|
2023-07-26 15:51:37 +00:00
|
|
|
return bytesMarshalNoCheck(field, buf, v)
|
2022-08-26 11:18:07 +00:00
|
|
|
}
|
2020-08-14 10:57:19 +00:00
|
|
|
|
2023-07-26 15:51:37 +00:00
|
|
|
func bytesMarshalNoCheck[T ~[]byte | ~string](field int, buf []byte, v T) int {
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType)
|
2020-08-14 12:53:57 +00:00
|
|
|
|
2020-08-14 10:57:19 +00:00
|
|
|
// buf length check can prevent panic at PutUvarint, but it will make
|
|
|
|
// marshaller a bit slower.
|
|
|
|
i := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
i += binary.PutUvarint(buf[i:], uint64(len(v)))
|
|
|
|
i += copy(buf[i:], v)
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return i
|
2020-08-14 10:57:19 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:51:37 +00:00
|
|
|
func bytesSize[T ~[]byte | ~string](field int, v T) int {
|
|
|
|
if len(v) == 0 {
|
2020-08-14 10:57:19 +00:00
|
|
|
return 0
|
|
|
|
}
|
2023-07-26 15:51:37 +00:00
|
|
|
return bytesSizeNoCheck(field, v)
|
2022-08-26 11:18:07 +00:00
|
|
|
}
|
2020-08-14 10:57:19 +00:00
|
|
|
|
2023-07-26 15:51:37 +00:00
|
|
|
func bytesSizeNoCheck[T ~[]byte | ~string](field int, v T) int {
|
2023-08-02 13:53:47 +00:00
|
|
|
return protowire.SizeGroup(protowire.Number(field), protowire.SizeBytes(len(v)))
|
2020-08-14 10:57:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func StringMarshal(field int, buf []byte, v string) int {
|
2023-07-26 15:51:37 +00:00
|
|
|
return bytesMarshal(field, buf, v)
|
2020-08-14 12:29:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func StringSize(field int, v string) int {
|
2023-07-26 15:51:37 +00:00
|
|
|
return bytesSize(field, v)
|
2020-08-14 12:29:48 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func BoolMarshal(field int, buf []byte, v bool) int {
|
2020-08-14 12:53:57 +00:00
|
|
|
if !v {
|
2022-03-12 12:20:19 +00:00
|
|
|
return 0
|
2020-08-14 12:53:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.VarintType)
|
2020-08-14 12:53:57 +00:00
|
|
|
|
|
|
|
// buf length check can prevent panic at PutUvarint, but it will make
|
|
|
|
// marshaller a bit slower.
|
|
|
|
i := binary.PutUvarint(buf, uint64(prefix))
|
2023-08-02 13:53:47 +00:00
|
|
|
const boolTrueValue = 0x1
|
|
|
|
buf[i] = boolTrueValue
|
2020-08-14 12:53:57 +00:00
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return i + 1
|
2020-08-14 12:53:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BoolSize(field int, v bool) int {
|
|
|
|
if !v {
|
|
|
|
return 0
|
|
|
|
}
|
2023-08-02 13:53:47 +00:00
|
|
|
const boolLength = 1
|
|
|
|
return protowire.SizeGroup(protowire.Number(field), boolLength)
|
2020-08-14 12:53:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func UInt64Marshal(field int, buf []byte, v uint64) int {
|
2020-08-14 13:32:19 +00:00
|
|
|
if v == 0 {
|
2022-03-12 12:20:19 +00:00
|
|
|
return 0
|
2020-08-14 13:32:19 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.VarintType)
|
2020-08-14 13:32:19 +00:00
|
|
|
|
|
|
|
// buf length check can prevent panic at PutUvarint, but it will make
|
|
|
|
// marshaller a bit slower.
|
|
|
|
i := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
i += binary.PutUvarint(buf[i:], v)
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return i
|
2020-08-14 13:32:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func UInt64Size(field int, v uint64) int {
|
|
|
|
if v == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2023-08-02 13:53:47 +00:00
|
|
|
return protowire.SizeGroup(protowire.Number(field), protowire.SizeVarint(v))
|
2020-08-14 13:32:19 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func Int64Marshal(field int, buf []byte, v int64) int {
|
2020-08-14 13:32:19 +00:00
|
|
|
return UInt64Marshal(field, buf, uint64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Int64Size(field int, v int64) int {
|
|
|
|
return UInt64Size(field, uint64(v))
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func UInt32Marshal(field int, buf []byte, v uint32) int {
|
2020-08-14 13:32:19 +00:00
|
|
|
return UInt64Marshal(field, buf, uint64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func UInt32Size(field int, v uint32) int {
|
|
|
|
return UInt64Size(field, uint64(v))
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func Int32Marshal(field int, buf []byte, v int32) int {
|
2020-08-14 13:32:19 +00:00
|
|
|
return UInt64Marshal(field, buf, uint64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func Int32Size(field int, v int32) int {
|
|
|
|
return UInt64Size(field, uint64(v))
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func EnumMarshal(field int, buf []byte, v int32) int {
|
2020-08-14 14:10:15 +00:00
|
|
|
return UInt64Marshal(field, buf, uint64(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func EnumSize(field int, v int32) int {
|
|
|
|
return UInt64Size(field, uint64(v))
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func RepeatedBytesMarshal(field int, buf []byte, v [][]byte) int {
|
2020-08-14 14:22:47 +00:00
|
|
|
var offset int
|
|
|
|
|
|
|
|
for i := range v {
|
2023-07-26 15:51:37 +00:00
|
|
|
offset += bytesMarshalNoCheck(field, buf[offset:], v[i])
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return offset
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedBytesSize(field int, v [][]byte) (size int) {
|
|
|
|
for i := range v {
|
2023-07-26 15:51:37 +00:00
|
|
|
size += bytesSizeNoCheck(field, v[i])
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func RepeatedStringMarshal(field int, buf []byte, v []string) int {
|
2020-08-14 14:22:47 +00:00
|
|
|
var offset int
|
|
|
|
|
|
|
|
for i := range v {
|
2023-07-26 15:51:37 +00:00
|
|
|
offset += bytesMarshalNoCheck(field, buf[offset:], v[i])
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return offset
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedStringSize(field int, v []string) (size int) {
|
|
|
|
for i := range v {
|
2023-07-26 15:51:37 +00:00
|
|
|
size += bytesSizeNoCheck(field, v[i])
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
func repeatedUIntSize[T ~uint64 | ~int64 | ~uint32 | ~int32](field int, v []T) (size, arraySize int) {
|
2020-08-14 14:22:47 +00:00
|
|
|
if len(v) == 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range v {
|
2023-08-02 13:53:47 +00:00
|
|
|
arraySize += protowire.SizeVarint(uint64(v[i]))
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
size = protowire.SizeGroup(protowire.Number(field), protowire.SizeBytes(arraySize))
|
2020-08-14 14:22:47 +00:00
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
return
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
func repeatedUIntMarshal[T ~uint64 | ~int64 | ~uint32 | ~int32](field int, buf []byte, v []T) int {
|
2020-08-14 14:22:47 +00:00
|
|
|
if len(v) == 0 {
|
2022-03-12 12:20:19 +00:00
|
|
|
return 0
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType)
|
2023-07-26 14:52:54 +00:00
|
|
|
offset := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
|
|
|
|
_, arrSize := repeatedUIntSize(field, v)
|
|
|
|
offset += binary.PutUvarint(buf[offset:], uint64(arrSize))
|
2020-08-14 14:22:47 +00:00
|
|
|
for i := range v {
|
2023-07-26 14:52:54 +00:00
|
|
|
offset += binary.PutUvarint(buf[offset:], uint64(v[i]))
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
return offset
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
func RepeatedUInt64Marshal(field int, buf []byte, v []uint64) int {
|
|
|
|
return repeatedUIntMarshal(field, buf, v)
|
|
|
|
}
|
2020-08-14 14:22:47 +00:00
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
func RepeatedUInt64Size(field int, v []uint64) (size, arraySize int) {
|
|
|
|
return repeatedUIntSize(field, v)
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
func RepeatedInt64Marshal(field int, buf []byte, v []int64) int {
|
|
|
|
return repeatedUIntMarshal(field, buf, v)
|
|
|
|
}
|
2020-08-14 14:22:47 +00:00
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
func RepeatedInt64Size(field int, v []int64) (size, arraySize int) {
|
|
|
|
return repeatedUIntSize(field, v)
|
|
|
|
}
|
2020-08-14 14:22:47 +00:00
|
|
|
|
2023-07-26 14:52:54 +00:00
|
|
|
func RepeatedUInt32Marshal(field int, buf []byte, v []uint32) int {
|
|
|
|
return repeatedUIntMarshal(field, buf, v)
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedUInt32Size(field int, v []uint32) (size, arraySize int) {
|
2023-07-26 14:52:54 +00:00
|
|
|
return repeatedUIntSize(field, v)
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func RepeatedInt32Marshal(field int, buf []byte, v []int32) int {
|
2023-07-26 14:52:54 +00:00
|
|
|
return repeatedUIntMarshal(field, buf, v)
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedInt32Size(field int, v []int32) (size, arraySize int) {
|
2023-07-26 14:52:54 +00:00
|
|
|
return repeatedUIntSize(field, v)
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-05 08:43:02 +00:00
|
|
|
// VarUIntSize returns length of varint byte sequence for uint64 value 'x'.
|
2020-08-14 10:57:19 +00:00
|
|
|
func VarUIntSize(x uint64) int {
|
|
|
|
return (bits.Len64(x|1) + 6) / 7
|
|
|
|
}
|
2020-08-14 13:32:19 +00:00
|
|
|
|
2023-07-26 15:12:27 +00:00
|
|
|
func NestedStructureMarshal[T stableMarshaller](field int64, buf []byte, v T) int {
|
2023-07-26 13:53:20 +00:00
|
|
|
n := v.StableSize()
|
|
|
|
if n == 0 {
|
2022-04-05 08:24:34 +00:00
|
|
|
return 0
|
2020-08-18 06:48:57 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType)
|
2020-08-18 06:48:57 +00:00
|
|
|
offset := binary.PutUvarint(buf, prefix)
|
|
|
|
offset += binary.PutUvarint(buf[offset:], uint64(n))
|
2022-04-05 08:24:34 +00:00
|
|
|
v.StableMarshal(buf[offset:])
|
2020-08-18 06:48:57 +00:00
|
|
|
|
2023-10-06 12:05:30 +00:00
|
|
|
return offset + n
|
|
|
|
}
|
|
|
|
|
|
|
|
// NestedStructureSetMarshalData calculates offset for field in parentData
|
|
|
|
// and calls SetMarshalData for nested structure.
|
|
|
|
//
|
|
|
|
// Returns marshalled data length of nested structure.
|
|
|
|
func NestedStructureSetMarshalData(field int64, parentData []byte, v setMarshalData) int {
|
|
|
|
n := v.StableSize()
|
|
|
|
if n == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, binary.MaxVarintLen64)
|
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.BytesType)
|
|
|
|
offset := binary.PutUvarint(buf, prefix)
|
|
|
|
offset += binary.PutUvarint(buf, uint64(n))
|
|
|
|
|
|
|
|
v.SetMarshalData(parentData[offset : offset+n])
|
|
|
|
|
2022-04-05 08:24:34 +00:00
|
|
|
return offset + n
|
2020-08-18 06:48:57 +00:00
|
|
|
}
|
|
|
|
|
2023-07-26 15:12:27 +00:00
|
|
|
func NestedStructureSize[T stableMarshaller](field int64, v T) (size int) {
|
2023-07-26 13:53:20 +00:00
|
|
|
n := v.StableSize()
|
|
|
|
if n == 0 {
|
2020-08-18 06:48:57 +00:00
|
|
|
return 0
|
|
|
|
}
|
2023-08-02 13:53:47 +00:00
|
|
|
size = protowire.SizeGroup(protowire.Number(field), protowire.SizeBytes(n))
|
|
|
|
return
|
2020-08-18 06:48:57 +00:00
|
|
|
}
|
2020-12-21 07:51:04 +00:00
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func Fixed64Marshal(field int, buf []byte, v uint64) int {
|
2020-12-21 07:51:04 +00:00
|
|
|
if v == 0 {
|
2022-03-12 12:20:19 +00:00
|
|
|
return 0
|
2020-12-21 07:51:04 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.Fixed64Type)
|
2020-12-21 07:51:04 +00:00
|
|
|
|
|
|
|
// buf length check can prevent panic at PutUvarint, but it will make
|
|
|
|
// marshaller a bit slower.
|
|
|
|
i := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
binary.LittleEndian.PutUint64(buf[i:], v)
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return i + 8
|
2020-12-21 07:51:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fixed64Size(fNum int, v uint64) int {
|
|
|
|
if v == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2023-08-02 13:53:47 +00:00
|
|
|
return protowire.SizeGroup(protowire.Number(fNum), protowire.SizeFixed64())
|
2020-12-21 07:51:04 +00:00
|
|
|
}
|
2021-03-24 07:10:30 +00:00
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func Float64Marshal(field int, buf []byte, v float64) int {
|
2021-03-24 07:10:30 +00:00
|
|
|
if v == 0 {
|
2022-03-12 12:20:19 +00:00
|
|
|
return 0
|
2021-03-24 07:10:30 +00:00
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.Fixed64Type)
|
2021-03-24 07:10:30 +00:00
|
|
|
|
|
|
|
i := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
binary.LittleEndian.PutUint64(buf[i:], math.Float64bits(v))
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return i + 8
|
2021-03-24 07:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Float64Size(fNum int, v float64) int {
|
|
|
|
if v == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2023-08-02 13:53:47 +00:00
|
|
|
return protowire.SizeGroup(protowire.Number(fNum), protowire.SizeFixed64())
|
2021-03-24 07:10:30 +00:00
|
|
|
}
|
2021-11-18 13:14:49 +00:00
|
|
|
|
|
|
|
// Fixed32Marshal encodes uint32 value to Protocol Buffers fixed32 field with specified number,
|
|
|
|
// and writes it to specified buffer. Returns number of bytes written.
|
|
|
|
//
|
|
|
|
// Panics if the buffer is undersized.
|
|
|
|
func Fixed32Marshal(field int, buf []byte, v uint32) int {
|
|
|
|
if v == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-08-02 13:53:47 +00:00
|
|
|
prefix := protowire.EncodeTag(protowire.Number(field), protowire.Fixed32Type)
|
2021-11-18 13:14:49 +00:00
|
|
|
|
|
|
|
// buf length check can prevent panic at PutUvarint, but it will make
|
|
|
|
// marshaller a bit slower.
|
|
|
|
i := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
binary.LittleEndian.PutUint32(buf[i:], v)
|
|
|
|
|
|
|
|
return i + 4
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fixed32Size returns number of bytes required to encode uint32 value to Protocol Buffers fixed32 field
|
|
|
|
// with specified number.
|
|
|
|
func Fixed32Size(fNum int, v uint32) int {
|
|
|
|
if v == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2023-08-02 13:53:47 +00:00
|
|
|
return protowire.SizeGroup(protowire.Number(fNum), protowire.SizeFixed32())
|
2021-11-18 13:14:49 +00:00
|
|
|
}
|