// 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 proto

import (
	
	

	
	
	
)

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

// EncodeVarint returns the varint encoded bytes of v.
func ( uint64) []byte {
	return protowire.AppendVarint(nil, )
}

// SizeVarint returns the length of the varint encoded bytes of v.
// This is equal to len(EncodeVarint(v)).
func ( uint64) int {
	return protowire.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 {
		return 0, 0
	}
	return , 
}

// Buffer is a buffer for encoding and decoding the protobuf wire format.
// It may be reused between invocations to reduce memory usage.
type Buffer struct {
	buf           []byte
	idx           int
	deterministic bool
}

// 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 {
	var  error
	.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 
}

type unknownFields struct{ XXX_unrecognized protoimpl.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, )
	return nil
}

// 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())
	return nil
}

// EncodeFixed64 appends a 64-bit little-endian integer to the buffer.
func ( *Buffer) ( uint64) error {
	.buf = protowire.AppendFixed64(.buf, uint64())
	return nil
}

// EncodeRawBytes appends a length-prefixed raw bytes to the buffer.
func ( *Buffer) ( []byte) error {
	.buf = protowire.AppendBytes(.buf, )
	return nil
}

// 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, )
	return nil
}

// EncodeMessage appends a length-prefixed encoded message to the buffer.
func ( *Buffer) ( Message) error {
	var  error
	.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 {
		return 0, protowire.ParseError()
	}
	.idx += 
	return uint64(), nil
}

// DecodeZigzag32 consumes an encoded 32-bit zig-zag varint from the buffer.
func ( *Buffer) () (uint64, error) {
	,  := .DecodeVarint()
	if  != nil {
		return 0, 
	}
	return uint64((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 {
		return 0, 
	}
	return uint64((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 {
		return 0, protowire.ParseError()
	}
	.idx += 
	return uint64(), nil
}

// DecodeFixed64 consumes a 64-bit little-endian integer from the buffer.
func ( *Buffer) () (uint64, error) {
	,  := protowire.ConsumeFixed64(.buf[.idx:])
	if  < 0 {
		return 0, protowire.ParseError()
	}
	.idx += 
	return uint64(), 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 {
		return nil, 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 
	}
	return UnmarshalMerge(, )
}

// 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 += 
	return UnmarshalMerge(, )
}

// 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 marker
	for {
		, ,  := protowire.ConsumeTag()
		if  < 0 {
			return nil, 0, protowire.ParseError()
		}
		 = [:]

		var  int
		switch  {
		case protowire.VarintType:
			_,  = protowire.ConsumeVarint()
		case protowire.Fixed32Type:
			_,  = protowire.ConsumeFixed32()
		case protowire.Fixed64Type:
			_,  = protowire.ConsumeFixed64()
		case protowire.BytesType:
			_,  = protowire.ConsumeBytes()
		case protowire.StartGroupType:
			++
		case protowire.EndGroupType:
			--
		default:
			return nil, 0, errors.New("proto: cannot parse reserved wire type")
		}
		if  < 0 {
			return nil, 0, protowire.ParseError()
		}
		 = [:]

		if  == 0 {
			return [:len()-len()-], len() - len(), nil
		}
	}
}