// Copyright 2019 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package protoimport ()const (WireVarint = 0WireFixed32 = 5WireFixed64 = 1WireBytes = 2WireStartGroup = 3WireEndGroup = 4)// EncodeVarint returns the varint encoded bytes of v.func ( uint64) []byte {returnprotowire.AppendVarint(nil, )}// SizeVarint returns the length of the varint encoded bytes of v.// This is equal to len(EncodeVarint(v)).func ( uint64) int {returnprotowire.SizeVarint()}// 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 ( []byte) (uint64, int) { , := protowire.ConsumeVarint()if < 0 {return0, 0 }return , }// Buffer is a buffer for encoding and decoding the protobuf wire format.// It may be reused between invocations to reduce memory usage.typeBufferstruct {buf []byteidxintdeterministicbool}// NewBuffer allocates a new Buffer initialized with buf,// where the contents of buf are considered the unread portion of the buffer.func ( []byte) *Buffer {return &Buffer{buf: }}// 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) ( bool) { .deterministic = }// SetBuf sets buf as the internal buffer,// where the contents of buf are considered the unread portion of the buffer.func ( *Buffer) ( []byte) { .buf = .idx = 0}// Reset clears the internal buffer of all written and unread data.func ( *Buffer) () { .buf = .buf[:0] .idx = 0}// Bytes returns the internal buffer.func ( *Buffer) () []byte {return .buf}// Unread returns the unread portion of the buffer.func ( *Buffer) () []byte {return .buf[.idx:]}// Marshal appends the wire-format encoding of m to the buffer.func ( *Buffer) ( Message) error {varerror .buf, = marshalAppend(.buf, , .deterministic)return}// Unmarshal parses the wire-format message in the buffer and// places the decoded results in m.// It does not reset m before unmarshaling.func ( *Buffer) ( Message) error { := UnmarshalMerge(.Unread(), ) .idx = len(.buf)return}typeunknownFieldsstruct{ XXX_unrecognizedprotoimpl.UnknownFields }func ( *unknownFields) () string { panic("not implemented") }func ( *unknownFields) () { panic("not implemented") }func ( *unknownFields) () { panic("not implemented") }// DebugPrint dumps the encoded bytes of b with a header and footer including s// to stdout. This is only intended for debugging.func (*Buffer) ( string, []byte) { := MessageReflect(new(unknownFields)) .SetUnknown() , _ = prototext.MarshalOptions{AllowPartial: true, Indent: "\t"}.Marshal(.Interface())fmt.Printf("==== %s ====\n%s==== %s ====\n", , , )}// EncodeVarint appends an unsigned varint encoding to the buffer.func ( *Buffer) ( uint64) error { .buf = protowire.AppendVarint(.buf, )returnnil}// EncodeZigzag32 appends a 32-bit zig-zag varint encoding to the buffer.func ( *Buffer) ( uint64) error {return .EncodeVarint(uint64((uint32() << 1) ^ uint32((int32() >> 31))))}// EncodeZigzag64 appends a 64-bit zig-zag varint encoding to the buffer.func ( *Buffer) ( uint64) error {return .EncodeVarint(uint64((uint64() << 1) ^ uint64((int64() >> 63))))}// EncodeFixed32 appends a 32-bit little-endian integer to the buffer.func ( *Buffer) ( uint64) error { .buf = protowire.AppendFixed32(.buf, uint32())returnnil}// EncodeFixed64 appends a 64-bit little-endian integer to the buffer.func ( *Buffer) ( uint64) error { .buf = protowire.AppendFixed64(.buf, uint64())returnnil}// EncodeRawBytes appends a length-prefixed raw bytes to the buffer.func ( *Buffer) ( []byte) error { .buf = protowire.AppendBytes(.buf, )returnnil}// EncodeStringBytes appends a length-prefixed raw bytes to the buffer.// It does not validate whether v contains valid UTF-8.func ( *Buffer) ( string) error { .buf = protowire.AppendString(.buf, )returnnil}// EncodeMessage appends a length-prefixed encoded message to the buffer.func ( *Buffer) ( Message) error {varerror .buf = protowire.AppendVarint(.buf, uint64(Size())) .buf, = marshalAppend(.buf, , .deterministic)return}// DecodeVarint consumes an encoded unsigned varint from the buffer.func ( *Buffer) () (uint64, error) { , := protowire.ConsumeVarint(.buf[.idx:])if < 0 {return0, protowire.ParseError() } .idx += returnuint64(), nil}// DecodeZigzag32 consumes an encoded 32-bit zig-zag varint from the buffer.func ( *Buffer) () (uint64, error) { , := .DecodeVarint()if != nil {return0, }returnuint64((uint32() >> 1) ^ uint32((int32(&1)<<31)>>31)), nil}// DecodeZigzag64 consumes an encoded 64-bit zig-zag varint from the buffer.func ( *Buffer) () (uint64, error) { , := .DecodeVarint()if != nil {return0, }returnuint64((uint64() >> 1) ^ uint64((int64(&1)<<63)>>63)), nil}// DecodeFixed32 consumes a 32-bit little-endian integer from the buffer.func ( *Buffer) () (uint64, error) { , := protowire.ConsumeFixed32(.buf[.idx:])if < 0 {return0, protowire.ParseError() } .idx += returnuint64(), nil}// DecodeFixed64 consumes a 64-bit little-endian integer from the buffer.func ( *Buffer) () (uint64, error) { , := protowire.ConsumeFixed64(.buf[.idx:])if < 0 {return0, protowire.ParseError() } .idx += returnuint64(), nil}// 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) ( bool) ([]byte, error) { , := protowire.ConsumeBytes(.buf[.idx:])if < 0 {returnnil, protowire.ParseError() } .idx += if { = append([]byte(nil), ...) }return , nil}// DecodeStringBytes consumes a length-prefixed raw bytes from the buffer.// It does not validate whether the raw bytes contain valid UTF-8.func ( *Buffer) () (string, error) { , := protowire.ConsumeString(.buf[.idx:])if < 0 {return"", protowire.ParseError() } .idx += return , nil}// DecodeMessage consumes a length-prefixed message from the buffer.// It does not reset m before unmarshaling.func ( *Buffer) ( Message) error { , := .DecodeRawBytes(false)if != nil {return }returnUnmarshalMerge(, )}// DecodeGroup consumes a message group from the buffer.// It assumes that the start group marker has already been consumed and// consumes all bytes until (and including the end group marker).// It does not reset m before unmarshaling.func ( *Buffer) ( Message) error { , , := consumeGroup(.buf[.idx:])if != nil {return } .idx += returnUnmarshalMerge(, )}// consumeGroup parses b until it finds an end group marker, returning// the raw bytes of the message (excluding the end group marker) and the// the total length of the message (including the end group marker).func ( []byte) ([]byte, int, error) { := := 1// assume this follows a start group markerfor { , , := protowire.ConsumeTag()if < 0 {returnnil, 0, protowire.ParseError() } = [:]varintswitch {caseprotowire.VarintType: _, = protowire.ConsumeVarint()caseprotowire.Fixed32Type: _, = protowire.ConsumeFixed32()caseprotowire.Fixed64Type: _, = protowire.ConsumeFixed64()caseprotowire.BytesType: _, = protowire.ConsumeBytes()caseprotowire.StartGroupType: ++caseprotowire.EndGroupType: --default:returnnil, 0, errors.New("proto: cannot parse reserved wire type") }if < 0 {returnnil, 0, protowire.ParseError() } = [:]if == 0 {return [:len()-len()-], len() - len(), nil } }}
The pages are generated with Goldsv0.4.9. (GOOS=linux GOARCH=amd64)