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"
|
2020-08-18 09:14:47 +00:00
|
|
|
"reflect"
|
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
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func BytesMarshal(field int, buf, v []byte) 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
|
|
|
}
|
2022-08-26 11:18:07 +00:00
|
|
|
return bytesMarshal(field, buf, v)
|
|
|
|
}
|
2020-08-14 10:57:19 +00:00
|
|
|
|
2022-08-26 11:18:07 +00:00
|
|
|
func bytesMarshal(field int, buf, v []byte) int {
|
2020-08-14 12:53:57 +00:00
|
|
|
prefix := field<<3 | 0x2
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func BytesSize(field int, v []byte) int {
|
|
|
|
ln := len(v)
|
|
|
|
if ln == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2022-08-26 11:18:07 +00:00
|
|
|
return bytesSize(field, v)
|
|
|
|
}
|
2020-08-14 10:57:19 +00:00
|
|
|
|
2022-08-26 11:18:07 +00:00
|
|
|
func bytesSize(field int, v []byte) int {
|
2020-08-14 10:57:19 +00:00
|
|
|
prefix := field<<3 | 0x2
|
|
|
|
|
2022-08-26 11:18:07 +00:00
|
|
|
return VarUIntSize(uint64(prefix)) + VarUIntSize(uint64(len(v))) + 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 {
|
2020-08-14 12:29:48 +00:00
|
|
|
return BytesMarshal(field, buf, []byte(v))
|
|
|
|
}
|
|
|
|
|
|
|
|
func StringSize(field int, v string) int {
|
|
|
|
return BytesSize(field, []byte(v))
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field << 3
|
|
|
|
|
|
|
|
// buf length check can prevent panic at PutUvarint, but it will make
|
|
|
|
// marshaller a bit slower.
|
|
|
|
i := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
buf[i] = 0x1
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field << 3
|
2020-08-14 13:32:19 +00:00
|
|
|
|
2020-08-14 12:53:57 +00:00
|
|
|
return VarUIntSize(uint64(prefix)) + 1 // bool is always 1 byte long
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field << 3
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field << 3
|
|
|
|
|
|
|
|
return VarUIntSize(uint64(prefix)) + VarUIntSize(v)
|
|
|
|
}
|
|
|
|
|
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 {
|
2022-08-26 11:18:07 +00:00
|
|
|
offset += bytesMarshal(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 {
|
2022-08-26 11:18:07 +00:00
|
|
|
size += bytesSize(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 {
|
2022-08-26 11:18:07 +00:00
|
|
|
offset += bytesMarshal(field, buf[offset:], []byte(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 {
|
2022-08-26 11:18:07 +00:00
|
|
|
size += bytesSize(field, []byte(v[i]))
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func RepeatedUInt64Marshal(field int, buf []byte, v []uint64) 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
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field<<3 | 0x02
|
|
|
|
offset := binary.PutUvarint(buf, uint64(prefix))
|
|
|
|
|
|
|
|
_, arrSize := RepeatedUInt64Size(field, v)
|
|
|
|
offset += binary.PutUvarint(buf[offset:], uint64(arrSize))
|
|
|
|
for i := range v {
|
|
|
|
offset += binary.PutUvarint(buf[offset:], v[i])
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
return offset
|
2020-08-14 14:22:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedUInt64Size(field int, v []uint64) (size, arraySize int) {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range v {
|
|
|
|
size += VarUIntSize(v[i])
|
|
|
|
}
|
|
|
|
arraySize = size
|
|
|
|
|
|
|
|
size += VarUIntSize(uint64(size))
|
|
|
|
|
|
|
|
prefix := field<<3 | 0x2
|
|
|
|
size += VarUIntSize(uint64(prefix))
|
|
|
|
|
|
|
|
return size, arraySize
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func RepeatedInt64Marshal(field int, buf []byte, v []int64) 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
|
|
|
}
|
|
|
|
|
|
|
|
convert := make([]uint64, len(v))
|
|
|
|
for i := range v {
|
|
|
|
convert[i] = uint64(v[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return RepeatedUInt64Marshal(field, buf, convert)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedInt64Size(field int, v []int64) (size, arraySize int) {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
convert := make([]uint64, len(v))
|
|
|
|
for i := range v {
|
|
|
|
convert[i] = uint64(v[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return RepeatedUInt64Size(field, convert)
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func RepeatedUInt32Marshal(field int, buf []byte, v []uint32) 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
|
|
|
}
|
|
|
|
|
|
|
|
convert := make([]uint64, len(v))
|
|
|
|
for i := range v {
|
|
|
|
convert[i] = uint64(v[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return RepeatedUInt64Marshal(field, buf, convert)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedUInt32Size(field int, v []uint32) (size, arraySize int) {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
convert := make([]uint64, len(v))
|
|
|
|
for i := range v {
|
|
|
|
convert[i] = uint64(v[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return RepeatedUInt64Size(field, convert)
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:20:19 +00:00
|
|
|
func RepeatedInt32Marshal(field int, buf []byte, v []int32) 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
|
|
|
}
|
|
|
|
|
|
|
|
convert := make([]uint64, len(v))
|
|
|
|
for i := range v {
|
|
|
|
convert[i] = uint64(v[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return RepeatedUInt64Marshal(field, buf, convert)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RepeatedInt32Size(field int, v []int32) (size, arraySize int) {
|
|
|
|
if len(v) == 0 {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
convert := make([]uint64, len(v))
|
|
|
|
for i := range v {
|
|
|
|
convert[i] = uint64(v[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
return RepeatedUInt64Size(field, convert)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2020-08-17 10:40:41 +00:00
|
|
|
func NestedStructurePrefix(field int64) (prefix uint64, ln int) {
|
|
|
|
prefix = uint64(field<<3 | 0x02)
|
|
|
|
return prefix, VarUIntSize(prefix)
|
2020-08-14 13:32:19 +00:00
|
|
|
}
|
2020-08-18 06:48:57 +00:00
|
|
|
|
2022-04-05 08:24:34 +00:00
|
|
|
func NestedStructureMarshal(field int64, buf []byte, v stableMarshaller) int {
|
2020-08-18 09:14:47 +00:00
|
|
|
if v == nil || reflect.ValueOf(v).IsNil() {
|
2022-04-05 08:24:34 +00:00
|
|
|
return 0
|
2020-08-18 06:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prefix, _ := NestedStructurePrefix(field)
|
|
|
|
offset := binary.PutUvarint(buf, prefix)
|
|
|
|
|
|
|
|
n := v.StableSize()
|
|
|
|
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
|
|
|
|
2022-04-05 08:24:34 +00:00
|
|
|
return offset + n
|
2020-08-18 06:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NestedStructureSize(field int64, v stableMarshaller) (size int) {
|
2020-08-18 09:14:47 +00:00
|
|
|
if v == nil || reflect.ValueOf(v).IsNil() {
|
2020-08-18 06:48:57 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ln := NestedStructurePrefix(field)
|
|
|
|
n := v.StableSize()
|
|
|
|
size = ln + VarUIntSize(uint64(n)) + n
|
|
|
|
|
|
|
|
return size
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field<<3 | 1
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := fNum<<3 | 1
|
|
|
|
|
|
|
|
return VarUIntSize(uint64(prefix)) + 8
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field<<3 | 1
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := fNum<<3 | 1
|
|
|
|
|
|
|
|
return VarUIntSize(uint64(prefix)) + 8
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := field<<3 | 5
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
prefix := fNum<<3 | 5
|
|
|
|
|
|
|
|
return VarUIntSize(uint64(prefix)) + 4
|
|
|
|
}
|