pb

package
v0.0.0-...-6f9c769 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 10, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WireVarint     = 0
	WireFixed32    = 5
	WireFixed64    = 1
	WireBytes      = 2
	WireStartGroup = 3
	WireEndGroup   = 4
)

Variables

View Source
var (
	ErrReadTagFirst   = errors.New("read next tag first")
	ErrExpectedNumber = errors.New("expected number")
	ErrExpectedString = errors.New("expected string")
)

Functions

func AppendBytes

func AppendBytes(b []byte, v []byte) []byte

AppendBytes appends v to b as a length-prefixed bytes value.

func AppendFixed32

func AppendFixed32(b []byte, v uint32) []byte

AppendFixed32 appends v to b as a little-endian uint32.

func AppendFixed64

func AppendFixed64(b []byte, v uint64) []byte

AppendFixed64 appends v to b as a little-endian uint64.

func AppendGroup

func AppendGroup(b []byte, num Number, v []byte) []byte

AppendGroup appends v to b as group value, with a trailing end group marker. The value v must not contain the end marker.

func AppendString

func AppendString(b []byte, v string) []byte

AppendString appends v to b as a length-prefixed bytes value.

func AppendTag

func AppendTag(b []byte, num Number, typ Type) []byte

AppendTag encodes num and typ as a varint-encoded tag and appends it to b.

func AppendVarint

func AppendVarint(b []byte, v uint64) []byte

AppendVarint appends v to b as a varint-encoded uint64.

func ConsumeBytes

func ConsumeBytes(b []byte) (v []byte, n int)

ConsumeBytes parses b as a length-prefixed bytes value, reporting its length. This returns a negative length upon an error (see ParseError).

func ConsumeField

func ConsumeField(b []byte) (Number, Type, int)

ConsumeField parses an entire field record (both tag and value) and returns the field number, the wire type, and the total length. This returns a negative length upon an error (see ParseError).

The total length includes the tag header and the end group marker (if the field is a group).

func ConsumeFieldValue

func ConsumeFieldValue(num Number, typ Type, b []byte) (n int)

ConsumeFieldValue parses a field value and returns its length. This assumes that the field Number and wire Type have already been parsed. This returns a negative length upon an error (see ParseError).

When parsing a group, the length includes the end group marker and the end group is verified to match the starting field number.

func ConsumeFixed32

func ConsumeFixed32(b []byte) (v uint32, n int)

ConsumeFixed32 parses b as a little-endian uint32, reporting its length. This returns a negative length upon an error (see ParseError).

func ConsumeFixed64

func ConsumeFixed64(b []byte) (v uint64, n int)

ConsumeFixed64 parses b as a little-endian uint64, reporting its length. This returns a negative length upon an error (see ParseError).

func ConsumeGroup

func ConsumeGroup(num Number, b []byte) (v []byte, n int)

ConsumeGroup parses b as a group value until the trailing end group marker, and verifies that the end marker matches the provided num. The value v does not contain the end marker, while the length does contain the end marker. This returns a negative length upon an error (see ParseError).

func ConsumeString

func ConsumeString(b []byte) (v string, n int)

ConsumeString parses b as a length-prefixed bytes value, reporting its length. This returns a negative length upon an error (see ParseError).

func ConsumeTag

func ConsumeTag(b []byte) (Number, Type, int)

ConsumeTag parses b as a varint-encoded tag, reporting its length. This returns a negative length upon an error (see ParseError).

func ConsumeVarint

func ConsumeVarint(b []byte) (v uint64, n int)

ConsumeVarint parses b as a varint-encoded uint64, reporting its length. This returns a negative length upon an error (see ParseError).

func DecodeBool

func DecodeBool(x uint64) bool

DecodeBool decodes a uint64 as a bool.

Input:  {    0,    1,    2, …}
Output: {false, true, true, …}

func DecodeTag

func DecodeTag(x uint64) (Number, Type)

DecodeTag decodes the field Number and wire Type from its unified form. The Number is -1 if the decoded field number overflows int32. Other than overflow, this does not check for field number validity.

func DecodeVarint

func DecodeVarint(b []byte) (uint64, int)

DecodeVarint parses a varint encoded integer from b, returning the integer value and the length of the varint. It returns (0, 0) if there is a parse error.

func DecodeZigZag

func DecodeZigZag(x uint64) int64

DecodeZigZag decodes a zig-zag-encoded uint64 as an int64.

Input:  {…,  5,  3,  1,  0,  2,  4,  6, …}
Output: {…, -3, -2, -1,  0, +1, +2, +3, …}

func EncodeBool

func EncodeBool(x bool) uint64

EncodeBool encodes a bool as a uint64.

Input:  {false, true}
Output: {    0,    1}

func EncodeTag

func EncodeTag(num Number, typ Type) uint64

EncodeTag encodes the field Number and wire Type into its unified form.

func EncodeVarint

func EncodeVarint(v uint64) []byte

EncodeVarint returns the varint encoded bytes of v.

func EncodeZigZag

func EncodeZigZag(x int64) uint64

EncodeZigZag encodes an int64 as a zig-zag-encoded uint64.

Input:  {…, -3, -2, -1,  0, +1, +2, +3, …}
Output: {…,  5,  3,  1,  0,  2,  4,  6, …}

func ParseError

func ParseError(n int) error

ParseError converts an error code into an error value. This returns nil if n is a non-negative number.

func SizeBytes

func SizeBytes(n int) int

SizeBytes returns the encoded size of a length-prefixed bytes value, given only the length.

func SizeFixed32

func SizeFixed32() int

SizeFixed32 returns the encoded size of a fixed32; which is always 4.

func SizeFixed64

func SizeFixed64() int

SizeFixed64 returns the encoded size of a fixed64; which is always 8.

func SizeGroup

func SizeGroup(num Number, n int) int

SizeGroup returns the encoded size of a group, given only the length.

func SizeTag

func SizeTag(num Number) int

func SizeVarint

func SizeVarint(v uint64) int

SizeVarint returns the encoded size of a varint. The size is guaranteed to be within 1 and 10, inclusive.

Types

type Buffer

type Buffer struct {
	// contains filtered or unexported fields
}

Buffer is a buffer for encoding and decoding the protobuf wire format. It may be reused between invocations to reduce memory usage.

func NewBuffer

func NewBuffer(buf []byte) Buffer

NewBuffer allocates a new Buffer initialized with buf, where the contents of buf are considered the unread portion of the buffer.

func NewWriter

func NewWriter(buf []byte, typ uint16) Buffer

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns the internal buffer.

func (*Buffer) Consume

func (b *Buffer) Consume()

func (*Buffer) ConsumeFieldValue

func (b *Buffer) ConsumeFieldValue(num Number, t Type) error

func (*Buffer) Continue

func (b *Buffer) Continue() bool

func (*Buffer) DecodeFixed32

func (b *Buffer) DecodeFixed32() (uint64, error)

DecodeFixed32 consumes a 32-bit little-endian integer from the buffer.

func (*Buffer) DecodeFixed64

func (b *Buffer) DecodeFixed64() (uint64, error)

DecodeFixed64 consumes a 64-bit little-endian integer from the buffer.

func (*Buffer) DecodeRawBytes

func (b *Buffer) DecodeRawBytes(alloc bool) ([]byte, error)

DecodeRawBytes consumes a length-prefixed raw bytes from the buffer. If alloc is specified, it returns a copy the raw bytes rather than a sub-slice of the buffer.

func (*Buffer) DecodeStringBytes

func (b *Buffer) DecodeStringBytes() (string, error)

DecodeStringBytes consumes a length-prefixed raw bytes from the buffer. It does not validate whether the raw bytes contain valid UTF-8.

func (*Buffer) DecodeTag

func (b *Buffer) DecodeTag() (Number, Type, error)

DecodeTag consumes an encoded tag from the buffer

func (*Buffer) DecodeVarint

func (b *Buffer) DecodeVarint() (uint64, error)

DecodeVarint consumes an encoded unsigned varint from the buffer.

func (*Buffer) DecodeZigzag32

func (b *Buffer) DecodeZigzag32() (uint64, error)

DecodeZigzag32 consumes an encoded 32-bit zig-zag varint from the buffer.

func (*Buffer) DecodeZigzag64

func (b *Buffer) DecodeZigzag64() (uint64, error)

DecodeZigzag64 consumes an encoded 64-bit zig-zag varint from the buffer.

func (*Buffer) EOF

func (b *Buffer) EOF() bool

func (*Buffer) EncodeFixed32

func (b *Buffer) EncodeFixed32(v uint64)

EncodeFixed32 appends a 32-bit little-endian integer to the buffer.

func (*Buffer) EncodeFixed64

func (b *Buffer) EncodeFixed64(v uint64)

EncodeFixed64 appends a 64-bit little-endian integer to the buffer.

func (*Buffer) EncodeRawBytes

func (b *Buffer) EncodeRawBytes(v []byte)

EncodeRawBytes appends a length-prefixed raw bytes to the buffer.

func (*Buffer) EncodeStringBytes

func (b *Buffer) EncodeStringBytes(v string)

EncodeStringBytes appends a length-prefixed raw bytes to the buffer. It does not validate whether v contains valid UTF-8.

func (*Buffer) EncodeTag

func (b *Buffer) EncodeTag(field Number, typ Type) *Buffer

EncodeTag encodes num and typ as a varint-encoded tag and appends it to b.

func (*Buffer) EncodeVarint

func (b *Buffer) EncodeVarint(v uint64)

EncodeVarint appends an unsigned varint encoding to the buffer.

func (*Buffer) EncodeZigzag32

func (b *Buffer) EncodeZigzag32(v uint64)

EncodeZigzag32 appends a 32-bit zig-zag varint encoding to the buffer.

func (*Buffer) EncodeZigzag64

func (b *Buffer) EncodeZigzag64(v uint64)

EncodeZigzag64 appends a 64-bit zig-zag varint encoding to the buffer.

func (*Buffer) Error

func (b *Buffer) Error() error

func (*Buffer) Finish

func (b *Buffer) Finish() []byte

func (*Buffer) ReadBool

func (b *Buffer) ReadBool() bool

func (*Buffer) ReadFloat32

func (b *Buffer) ReadFloat32() float32

ReadFloat32 reads next float32 value.

func (*Buffer) ReadFloat32ZigZag

func (b *Buffer) ReadFloat32ZigZag() float32

ReadFloat32ZigZag reads next zigzag encoded number preferring 32-bits

func (*Buffer) ReadFloat64

func (b *Buffer) ReadFloat64() float64

ReadFloat64 reads next float64 value

func (*Buffer) ReadFloat64ZigZag

func (b *Buffer) ReadFloat64ZigZag() float64

ReadFloat64ZigZag reads next zigzag encoded number preferring 64-bits

func (*Buffer) ReadInt16

func (b *Buffer) ReadInt16() int16

ReadInt16 consumes an encoded unsigned varint from the buffer.

func (*Buffer) ReadInt16ZigZag

func (b *Buffer) ReadInt16ZigZag() int16

ReadInt16ZigZag reads next zigzag encoded number preferring 32-bits

func (*Buffer) ReadInt32

func (b *Buffer) ReadInt32() int32

ReadInt32 reads next int32 value

func (*Buffer) ReadInt32Zigzag

func (b *Buffer) ReadInt32Zigzag() int32

ReadInt32Zigzag reads next zigzag encoded number preferring 32-bits

func (*Buffer) ReadInt64

func (b *Buffer) ReadInt64() int64

ReadInt64 reads next int64 value

func (*Buffer) ReadInt64ZigZag

func (b *Buffer) ReadInt64ZigZag() int64

ReadInt64ZigZag reads next zigzag encoded number preferring 32-bits

func (*Buffer) ReadInt8

func (b *Buffer) ReadInt8() int8

ReadInt8 reads next int8 value

func (*Buffer) ReadString

func (b *Buffer) ReadString() string

ReadString reads next string.

func (*Buffer) ReadUint16

func (b *Buffer) ReadUint16() uint16

ReadUint16 reads next uint16 value

func (*Buffer) ReadUint16ZigZag

func (b *Buffer) ReadUint16ZigZag() uint16

ReadUint16ZigZag reads next zigzag encoded number preferring 32-bits

func (*Buffer) ReadUint32

func (b *Buffer) ReadUint32() uint32

ReadUint32 reads next encoded number preferring 32-bits

func (*Buffer) ReadUint32ZigZag

func (b *Buffer) ReadUint32ZigZag() uint32

ReadUint32ZigZag reads next zigzag encoded number preferring 32-bits

func (*Buffer) ReadUint64

func (b *Buffer) ReadUint64() uint64

ReadUint64 reads next encoded number preferring 64-bits

func (*Buffer) ReadUint64ZigZag

func (b *Buffer) ReadUint64ZigZag() uint64

ReadUint64ZigZag reads next zigzag encoded number preferring 64-bits

func (*Buffer) ReadUint8

func (b *Buffer) ReadUint8() uint8

ReadUint8 reads next uint8 value

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset clears the internal buffer of all written and unread data.

func (*Buffer) SetBuf

func (b *Buffer) SetBuf(buf []byte)

SetBuf sets buf as the internal buffer, where the contents of buf are considered the unread portion of the buffer.

func (*Buffer) SetDeterministic

func (b *Buffer) SetDeterministic(deterministic bool)

SetDeterministic specifies whether to use deterministic serialization.

Deterministic serialization guarantees that for a given binary, equal messages will always be serialized to the same bytes. This implies:

  • Repeated serialization of a message will return the same bytes.
  • Different processes of the same binary (which may be executing on different machines) will serialize equal messages to the same bytes.

Note that the deterministic serialization is NOT canonical across languages. It is not guaranteed to remain stable over time. It is unstable across different builds with schema changes due to unknown fields. Users who need canonical serialization (e.g., persistent storage in a canonical form, fingerprinting, etc.) should define their own canonicalization specification and implement their own serializer rather than relying on this API.

If deterministic serialization is requested, map entries will be sorted by keys in lexographical order. This is an implementation detail and subject to change.

func (*Buffer) Unread

func (b *Buffer) Unread() []byte

Unread returns the unread portion of the buffer.

func (*Buffer) WriteBool

func (b *Buffer) WriteBool(field Number, v bool)

func (*Buffer) WriteFixed32Float32

func (b *Buffer) WriteFixed32Float32(field Number, v float32)

func (*Buffer) WriteFixed32Float64

func (b *Buffer) WriteFixed32Float64(field Number, v float64)

func (*Buffer) WriteFixed32Int16

func (b *Buffer) WriteFixed32Int16(field Number, v int16)

func (*Buffer) WriteFixed32Int32

func (b *Buffer) WriteFixed32Int32(field Number, v int32)

func (*Buffer) WriteFixed32Int64

func (b *Buffer) WriteFixed32Int64(field Number, v int64)

func (*Buffer) WriteFixed32Int8

func (b *Buffer) WriteFixed32Int8(field Number, v int8)

func (*Buffer) WriteFixed32Uint16

func (b *Buffer) WriteFixed32Uint16(field Number, v uint16)

func (*Buffer) WriteFixed32Uint32

func (b *Buffer) WriteFixed32Uint32(field Number, v uint32)

func (*Buffer) WriteFixed32Uint64

func (b *Buffer) WriteFixed32Uint64(field Number, v uint64)

func (*Buffer) WriteFixed32Uint8

func (b *Buffer) WriteFixed32Uint8(field Number, v uint8)

func (*Buffer) WriteFixed64Float32

func (b *Buffer) WriteFixed64Float32(field Number, v float32)

func (*Buffer) WriteFixed64Float64

func (b *Buffer) WriteFixed64Float64(field Number, v float64)

func (*Buffer) WriteFixed64Int16

func (b *Buffer) WriteFixed64Int16(field Number, v int16)

func (*Buffer) WriteFixed64Int32

func (b *Buffer) WriteFixed64Int32(field Number, v int32)

func (*Buffer) WriteFixed64Int64

func (b *Buffer) WriteFixed64Int64(field Number, v int64)

func (*Buffer) WriteFixed64Int8

func (b *Buffer) WriteFixed64Int8(field Number, v int8)

func (*Buffer) WriteFixed64Uint16

func (b *Buffer) WriteFixed64Uint16(field Number, v uint16)

func (*Buffer) WriteFixed64Uint32

func (b *Buffer) WriteFixed64Uint32(field Number, v uint32)

func (*Buffer) WriteFixed64Uint64

func (b *Buffer) WriteFixed64Uint64(field Number, v uint64)

func (*Buffer) WriteFixed64Uint8

func (b *Buffer) WriteFixed64Uint8(field Number, v uint8)

func (*Buffer) WriteHeader

func (b *Buffer) WriteHeader(typ uint16)

func (*Buffer) WriteString

func (b *Buffer) WriteString(field Number, v string)

func (*Buffer) WriteUvarint16

func (b *Buffer) WriteUvarint16(field Number, v uint16)

func (*Buffer) WriteUvarint32

func (b *Buffer) WriteUvarint32(field Number, v uint32)

func (*Buffer) WriteUvarint64

func (b *Buffer) WriteUvarint64(field Number, v uint64)

func (*Buffer) WriteUvarint8

func (b *Buffer) WriteUvarint8(field Number, v uint8)

func (*Buffer) WriteVarint16

func (b *Buffer) WriteVarint16(field Number, v int16)

func (*Buffer) WriteVarint32

func (b *Buffer) WriteVarint32(field Number, v int32)

func (*Buffer) WriteVarint64

func (b *Buffer) WriteVarint64(field Number, v int64)

func (*Buffer) WriteVarint8

func (b *Buffer) WriteVarint8(field Number, v int8)

func (*Buffer) WriteVarintFloat32

func (b *Buffer) WriteVarintFloat32(field Number, v float32)

func (*Buffer) WriteVarintFloat64

func (b *Buffer) WriteVarintFloat64(field Number, v float64)

func (*Buffer) WriteZigzag32Float32

func (b *Buffer) WriteZigzag32Float32(field Number, v float32)

func (*Buffer) WriteZigzag32Float64

func (b *Buffer) WriteZigzag32Float64(field Number, v float64)

func (*Buffer) WriteZigzag32Int16

func (b *Buffer) WriteZigzag32Int16(field Number, v int16)

func (*Buffer) WriteZigzag32Int32

func (b *Buffer) WriteZigzag32Int32(field Number, v int32)

func (*Buffer) WriteZigzag32Int64

func (b *Buffer) WriteZigzag32Int64(field Number, v int64)

func (*Buffer) WriteZigzag32Int8

func (b *Buffer) WriteZigzag32Int8(field Number, v int8)

func (*Buffer) WriteZigzag32Uint16

func (b *Buffer) WriteZigzag32Uint16(field Number, v uint16)

func (*Buffer) WriteZigzag32Uint32

func (b *Buffer) WriteZigzag32Uint32(field Number, v uint32)

func (*Buffer) WriteZigzag32Uint64

func (b *Buffer) WriteZigzag32Uint64(field Number, v uint64)

func (*Buffer) WriteZigzag32Uint8

func (b *Buffer) WriteZigzag32Uint8(field Number, v uint8)

func (*Buffer) WriteZigzag64Float32

func (b *Buffer) WriteZigzag64Float32(field Number, v float32)

func (*Buffer) WriteZigzag64Float64

func (b *Buffer) WriteZigzag64Float64(field Number, v float64)

func (*Buffer) WriteZigzag64Int16

func (b *Buffer) WriteZigzag64Int16(field Number, v int16)

func (*Buffer) WriteZigzag64Int32

func (b *Buffer) WriteZigzag64Int32(field Number, v int32)

func (*Buffer) WriteZigzag64Int64

func (b *Buffer) WriteZigzag64Int64(field Number, v int64)

func (*Buffer) WriteZigzag64Int8

func (b *Buffer) WriteZigzag64Int8(field Number, v int8)

func (*Buffer) WriteZigzag64Uint16

func (b *Buffer) WriteZigzag64Uint16(field Number, v uint16)

func (*Buffer) WriteZigzag64Uint32

func (b *Buffer) WriteZigzag64Uint32(field Number, v uint32)

func (*Buffer) WriteZigzag64Uint64

func (b *Buffer) WriteZigzag64Uint64(field Number, v uint64)

func (*Buffer) WriteZigzag64Uint8

func (b *Buffer) WriteZigzag64Uint8(field Number, v uint8)

type Number

type Number int32

Number represents the field number.

const (
	MinValidNumber        Number = 1
	FirstReservedNumber   Number = 19000
	LastReservedNumber    Number = 19999
	MaxValidNumber        Number = 1<<29 - 1
	DefaultRecursionLimit        = 10000
)

func (Number) IsValid

func (n Number) IsValid() bool

IsValid reports whether the field number is semantically valid.

Note that while numbers within the reserved range are semantically invalid, they are syntactically valid in the wire format. Implementations may treat records with reserved field numbers as unknown.

type Type

type Type int8

Type represents the wire type.

const (
	VarintType     Type = 0
	Fixed32Type    Type = 5
	Fixed64Type    Type = 1
	BytesType      Type = 2
	StartGroupType Type = 3
	EndGroupType   Type = 4
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL