1
// Go support for Protocol Buffers - Google's data interchange format
3
// Copyright 2010 The Go Authors. All rights reserved.
4
// https://github.com/golang/protobuf
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions are
10
// * Redistributions of source code must retain the above copyright
11
// notice, this list of conditions and the following disclaimer.
12
// * Redistributions in binary form must reproduce the above
13
// copyright notice, this list of conditions and the following disclaimer
14
// in the documentation and/or other materials provided with the
16
// * Neither the name of Google Inc. nor the names of its
17
// contributors may be used to endorse or promote products derived from
18
// this software without specific prior written permission.
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
* Routines for encoding data into the wire format for protocol buffers.
44
// errRepeatedHasNil is the error returned if Marshal is called with
45
// a struct with a repeated field containing a nil element.
46
errRepeatedHasNil = errors.New("proto: repeated field has nil element")
48
// errOneofHasNil is the error returned if Marshal is called with
49
// a struct with a oneof field containing a nil element.
50
errOneofHasNil = errors.New("proto: oneof field has nil value")
52
// ErrNil is the error returned if Marshal is called with nil.
53
ErrNil = errors.New("proto: Marshal called with nil")
55
// ErrTooLarge is the error returned if Marshal is called with a
56
// message that encodes to >2GB.
57
ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
60
// The fundamental encoders that put bytes on the wire.
61
// Those that take integer types all accept uint64 and are
62
// therefore of type valueEncoder.
64
const maxVarintBytes = 10 // maximum length of a varint
66
// EncodeVarint returns the varint encoding of x.
67
// This is the format for the
68
// int32, int64, uint32, uint64, bool, and enum
69
// protocol buffer types.
70
// Not used by the package itself, but helpful to clients
71
// wishing to use the same encoding.
72
func EncodeVarint(x uint64) []byte {
73
var buf [maxVarintBytes]byte
75
for n = 0; x > 127; n++ {
76
buf[n] = 0x80 | uint8(x&0x7F)
84
// EncodeVarint writes a varint-encoded integer to the Buffer.
85
// This is the format for the
86
// int32, int64, uint32, uint64, bool, and enum
87
// protocol buffer types.
88
func (p *Buffer) EncodeVarint(x uint64) error {
90
p.buf = append(p.buf, uint8(x&0x7f|0x80))
93
p.buf = append(p.buf, uint8(x))
97
// SizeVarint returns the varint encoding size of an integer.
98
func SizeVarint(x uint64) int {
122
// EncodeFixed64 writes a 64-bit integer to the Buffer.
123
// This is the format for the
124
// fixed64, sfixed64, and double protocol buffer types.
125
func (p *Buffer) EncodeFixed64(x uint64) error {
126
p.buf = append(p.buf,
138
// EncodeFixed32 writes a 32-bit integer to the Buffer.
139
// This is the format for the
140
// fixed32, sfixed32, and float protocol buffer types.
141
func (p *Buffer) EncodeFixed32(x uint64) error {
142
p.buf = append(p.buf,
150
// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
152
// This is the format used for the sint64 protocol buffer type.
153
func (p *Buffer) EncodeZigzag64(x uint64) error {
154
// use signed number to get arithmetic right shift.
155
return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
158
// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
160
// This is the format used for the sint32 protocol buffer type.
161
func (p *Buffer) EncodeZigzag32(x uint64) error {
162
// use signed number to get arithmetic right shift.
163
return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
166
// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
167
// This is the format used for the bytes protocol buffer
168
// type and for embedded messages.
169
func (p *Buffer) EncodeRawBytes(b []byte) error {
170
p.EncodeVarint(uint64(len(b)))
171
p.buf = append(p.buf, b...)
175
// EncodeStringBytes writes an encoded string to the Buffer.
176
// This is the format used for the proto2 string type.
177
func (p *Buffer) EncodeStringBytes(s string) error {
178
p.EncodeVarint(uint64(len(s)))
179
p.buf = append(p.buf, s...)
183
// Marshaler is the interface representing objects that can marshal themselves.
184
type Marshaler interface {
185
Marshal() ([]byte, error)
188
// EncodeMessage writes the protocol buffer to the Buffer,
189
// prefixed by a varint-encoded length.
190
func (p *Buffer) EncodeMessage(pb Message) error {
192
sizVar := SizeVarint(uint64(siz))
194
p.EncodeVarint(uint64(siz))
198
// All protocol buffer fields are nillable, but be careful.
199
func isNil(v reflect.Value) bool {
201
case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice: