blob: d75711027d362552829a178cf7665bb728411217 [file] [log] [blame]
Rob Pikeaaa3a622010-03-20 22:32:34 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
David Symondsee6e9c52012-11-29 08:51:07 +11003// Copyright 2010 The Go Authors. All rights reserved.
Rob Pikeaaa3a622010-03-20 22:32:34 -07004// http://code.google.com/p/goprotobuf/
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions are
8// met:
9//
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
15// distribution.
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.
19//
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.
31
32package proto
33
34/*
35 * Routines for encoding data into the wire format for protocol buffers.
36 */
37
38import (
Rob Pikea17fdd92011-11-02 12:43:05 -070039 "errors"
David Symonds4646c372013-09-09 13:18:58 +100040 "fmt"
Rob Pikeaaa3a622010-03-20 22:32:34 -070041 "reflect"
David Symondsdf583ae2012-12-06 14:06:53 +110042 "sort"
Rob Pikeaaa3a622010-03-20 22:32:34 -070043)
44
David Symondse583a5f2013-09-27 10:02:37 +100045// RequiredNotSetError is the error returned if Marshal is called with
Rob Pikeaaa3a622010-03-20 22:32:34 -070046// a protocol buffer struct whose required fields have not
Rob Pikec6d8e4a2010-07-28 15:34:32 -070047// all been initialized. It is also the error returned if Unmarshal is
48// called with an encoded protocol buffer that does not include all the
49// required fields.
David Symonds4646c372013-09-09 13:18:58 +100050//
David Symondse583a5f2013-09-27 10:02:37 +100051// When printed, RequiredNotSetError reports the first unset required field in a
David Symonds4646c372013-09-09 13:18:58 +100052// message. If the field cannot be precisely determined, it is reported as
53// "{Unknown}".
David Symondse583a5f2013-09-27 10:02:37 +100054type RequiredNotSetError struct {
David Symonds4646c372013-09-09 13:18:58 +100055 field string
David Symonds5b7775e2010-12-01 10:09:04 +110056}
57
David Symondse583a5f2013-09-27 10:02:37 +100058func (e *RequiredNotSetError) Error() string {
David Symonds4646c372013-09-09 13:18:58 +100059 return fmt.Sprintf("proto: required field %q not set", e.field)
David Symonds5b7775e2010-12-01 10:09:04 +110060}
Rob Pikeaaa3a622010-03-20 22:32:34 -070061
David Symonds7656e742011-07-22 14:54:17 +100062var (
63 // ErrRepeatedHasNil is the error returned if Marshal is called with
64 // a struct with a repeated field containing a nil element.
David Symonds381349d2012-09-18 15:11:46 +100065 ErrRepeatedHasNil = errors.New("proto: repeated field has nil element")
Rob Pikeaaa3a622010-03-20 22:32:34 -070066
David Symonds7656e742011-07-22 14:54:17 +100067 // ErrNil is the error returned if Marshal is called with nil.
Rob Pikea17fdd92011-11-02 12:43:05 -070068 ErrNil = errors.New("proto: Marshal called with nil")
David Symonds7656e742011-07-22 14:54:17 +100069)
Rob Pikeaaa3a622010-03-20 22:32:34 -070070
71// The fundamental encoders that put bytes on the wire.
72// Those that take integer types all accept uint64 and are
73// therefore of type valueEncoder.
74
David Symonds4fee3b12010-11-11 10:00:13 +110075const maxVarintBytes = 10 // maximum length of a varint
76
Rob Pikeaaa3a622010-03-20 22:32:34 -070077// EncodeVarint returns the varint encoding of x.
78// This is the format for the
79// int32, int64, uint32, uint64, bool, and enum
80// protocol buffer types.
81// Not used by the package itself, but helpful to clients
82// wishing to use the same encoding.
83func EncodeVarint(x uint64) []byte {
David Symonds4fee3b12010-11-11 10:00:13 +110084 var buf [maxVarintBytes]byte
Rob Pikeaaa3a622010-03-20 22:32:34 -070085 var n int
86 for n = 0; x > 127; n++ {
87 buf[n] = 0x80 | uint8(x&0x7F)
88 x >>= 7
89 }
90 buf[n] = uint8(x)
91 n++
92 return buf[0:n]
93}
94
95// EncodeVarint writes a varint-encoded integer to the Buffer.
96// This is the format for the
97// int32, int64, uint32, uint64, bool, and enum
98// protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -070099func (p *Buffer) EncodeVarint(x uint64) error {
David Symonds4fee3b12010-11-11 10:00:13 +1100100 for x >= 1<<7 {
David Symondsd9da6ba2011-08-30 14:41:30 +1000101 p.buf = append(p.buf, uint8(x&0x7f|0x80))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700102 x >>= 7
103 }
David Symondsd9da6ba2011-08-30 14:41:30 +1000104 p.buf = append(p.buf, uint8(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700105 return nil
106}
107
David Symonds0bf1ad52013-10-11 09:07:50 +1100108func sizeVarint(x uint64) (n int) {
109 for {
110 n++
111 x >>= 7
112 if x == 0 {
113 break
114 }
115 }
116 return n
117}
118
Rob Pikeaaa3a622010-03-20 22:32:34 -0700119// EncodeFixed64 writes a 64-bit integer to the Buffer.
120// This is the format for the
121// fixed64, sfixed64, and double protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700122func (p *Buffer) EncodeFixed64(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000123 p.buf = append(p.buf,
124 uint8(x),
125 uint8(x>>8),
126 uint8(x>>16),
127 uint8(x>>24),
128 uint8(x>>32),
129 uint8(x>>40),
130 uint8(x>>48),
131 uint8(x>>56))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700132 return nil
133}
134
David Symonds0bf1ad52013-10-11 09:07:50 +1100135func sizeFixed64(x uint64) int {
136 return 8
137}
138
Rob Pikeaaa3a622010-03-20 22:32:34 -0700139// EncodeFixed32 writes a 32-bit integer to the Buffer.
140// This is the format for the
141// fixed32, sfixed32, and float protocol buffer types.
Rob Pikea17fdd92011-11-02 12:43:05 -0700142func (p *Buffer) EncodeFixed32(x uint64) error {
David Symondsd9da6ba2011-08-30 14:41:30 +1000143 p.buf = append(p.buf,
144 uint8(x),
145 uint8(x>>8),
146 uint8(x>>16),
147 uint8(x>>24))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700148 return nil
149}
150
David Symonds0bf1ad52013-10-11 09:07:50 +1100151func sizeFixed32(x uint64) int {
152 return 4
153}
154
Rob Pikeaaa3a622010-03-20 22:32:34 -0700155// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
156// to the Buffer.
157// This is the format used for the sint64 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700158func (p *Buffer) EncodeZigzag64(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700159 // use signed number to get arithmetic right shift.
160 return p.EncodeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
161}
162
David Symonds0bf1ad52013-10-11 09:07:50 +1100163func sizeZigzag64(x uint64) int {
164 return sizeVarint(uint64((x << 1) ^ uint64((int64(x) >> 63))))
165}
166
Rob Pikeaaa3a622010-03-20 22:32:34 -0700167// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
168// to the Buffer.
169// This is the format used for the sint32 protocol buffer type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700170func (p *Buffer) EncodeZigzag32(x uint64) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700171 // use signed number to get arithmetic right shift.
172 return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
173}
174
David Symonds0bf1ad52013-10-11 09:07:50 +1100175func sizeZigzag32(x uint64) int {
176 return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
177}
178
Rob Pikeaaa3a622010-03-20 22:32:34 -0700179// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
180// This is the format used for the bytes protocol buffer
181// type and for embedded messages.
Rob Pikea17fdd92011-11-02 12:43:05 -0700182func (p *Buffer) EncodeRawBytes(b []byte) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700183 p.EncodeVarint(uint64(len(b)))
Rob Pike99fa2b62010-12-02 10:39:42 -0800184 p.buf = append(p.buf, b...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700185 return nil
186}
187
David Symonds0bf1ad52013-10-11 09:07:50 +1100188func sizeRawBytes(b []byte) int {
189 return sizeVarint(uint64(len(b))) +
190 len(b)
191}
192
Rob Pikeaaa3a622010-03-20 22:32:34 -0700193// EncodeStringBytes writes an encoded string to the Buffer.
194// This is the format used for the proto2 string type.
Rob Pikea17fdd92011-11-02 12:43:05 -0700195func (p *Buffer) EncodeStringBytes(s string) error {
Rob Pike76f6ee52011-10-20 12:58:28 -0700196 p.EncodeVarint(uint64(len(s)))
197 p.buf = append(p.buf, s...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700198 return nil
199}
200
David Symonds0bf1ad52013-10-11 09:07:50 +1100201func sizeStringBytes(s string) int {
202 return sizeVarint(uint64(len(s))) +
203 len(s)
204}
205
Rob Pikeaaa3a622010-03-20 22:32:34 -0700206// Marshaler is the interface representing objects that can marshal themselves.
207type Marshaler interface {
Rob Pikea17fdd92011-11-02 12:43:05 -0700208 Marshal() ([]byte, error)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700209}
210
David Symonds9f60f432012-06-14 09:45:25 +1000211// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700212// and encodes it into the wire format, returning the data.
David Symonds9f60f432012-06-14 09:45:25 +1000213func Marshal(pb Message) ([]byte, error) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700214 // Can the object marshal itself?
215 if m, ok := pb.(Marshaler); ok {
216 return m.Marshal()
217 }
218 p := NewBuffer(nil)
219 err := p.Marshal(pb)
David Symonds4646c372013-09-09 13:18:58 +1000220 var state errorState
221 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700222 return nil, err
223 }
224 return p.buf, err
225}
226
David Symonds9f60f432012-06-14 09:45:25 +1000227// Marshal takes the protocol buffer
Rob Pikeaaa3a622010-03-20 22:32:34 -0700228// and encodes it into the wire format, writing the result to the
229// Buffer.
David Symonds9f60f432012-06-14 09:45:25 +1000230func (p *Buffer) Marshal(pb Message) error {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700231 // Can the object marshal itself?
232 if m, ok := pb.(Marshaler); ok {
233 data, err := m.Marshal()
234 if err != nil {
235 return err
236 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800237 p.buf = append(p.buf, data...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700238 return nil
239 }
240
Russ Coxd4ce3f12012-09-12 10:36:26 +1000241 t, base, err := getbase(pb)
242 if structPointer_IsNil(base) {
David Symondsd4661c52012-08-30 15:17:53 +1000243 return ErrNil
244 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700245 if err == nil {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000246 err = p.enc_struct(t.Elem(), GetProperties(t.Elem()), base)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700247 }
248
David Symonds9f60f432012-06-14 09:45:25 +1000249 if collectStats {
250 stats.Encode++
251 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700252
253 return err
254}
255
David Symonds0bf1ad52013-10-11 09:07:50 +1100256// Size returns the encoded size of a protocol buffer.
257func Size(pb Message) (n int) {
258 // Can the object marshal itself? If so, Size is slow.
259 // TODO: add Size to Marshaler, or add a Sizer interface.
260 if m, ok := pb.(Marshaler); ok {
261 b, _ := m.Marshal()
262 return len(b)
263 }
264
265 t, base, err := getbase(pb)
266 if structPointer_IsNil(base) {
267 return 0
268 }
269 if err == nil {
270 n = size_struct(t.Elem(), GetProperties(t.Elem()), base)
271 }
272
273 if collectStats {
274 stats.Size++
275 }
276
277 return
278}
279
Rob Pikeaaa3a622010-03-20 22:32:34 -0700280// Individual type encoders.
281
282// Encode a bool.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000283func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
284 v := *structPointer_Bool(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700285 if v == nil {
286 return ErrNil
287 }
Rob Pike0f42a272011-10-20 16:03:11 -0700288 x := 0
289 if *v {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700290 x = 1
291 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800292 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700293 p.valEnc(o, uint64(x))
294 return nil
295}
296
David Symonds0bf1ad52013-10-11 09:07:50 +1100297func size_bool(p *Properties, base structPointer) int {
298 v := *structPointer_Bool(base, p.field)
299 if v == nil {
300 return 0
301 }
302 return len(p.tagcode) + 1 // each bool takes exactly one byte
303}
304
Rob Pikeaaa3a622010-03-20 22:32:34 -0700305// Encode an int32.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000306func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
307 v := structPointer_Word32(base, p.field)
308 if word32_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700309 return ErrNil
310 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000311 x := word32_Get(v)
Rob Pike99fa2b62010-12-02 10:39:42 -0800312 o.buf = append(o.buf, p.tagcode...)
David Symondsc31645c2013-06-22 17:57:36 +1000313 p.valEnc(o, uint64(x))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700314 return nil
315}
316
David Symonds0bf1ad52013-10-11 09:07:50 +1100317func size_int32(p *Properties, base structPointer) (n int) {
318 v := structPointer_Word32(base, p.field)
319 if word32_IsNil(v) {
320 return 0
321 }
322 x := word32_Get(v)
323 n += len(p.tagcode)
324 n += p.valSize(uint64(x))
325 return
326}
327
Rob Pikeaaa3a622010-03-20 22:32:34 -0700328// Encode an int64.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000329func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
330 v := structPointer_Word64(base, p.field)
331 if word64_IsNil(v) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700332 return ErrNil
333 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000334 x := word64_Get(v)
Rob Pike99fa2b62010-12-02 10:39:42 -0800335 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000336 p.valEnc(o, x)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700337 return nil
338}
339
David Symonds0bf1ad52013-10-11 09:07:50 +1100340func size_int64(p *Properties, base structPointer) (n int) {
341 v := structPointer_Word64(base, p.field)
342 if word64_IsNil(v) {
343 return 0
344 }
345 x := word64_Get(v)
346 n += len(p.tagcode)
347 n += p.valSize(x)
348 return
349}
350
Rob Pikeaaa3a622010-03-20 22:32:34 -0700351// Encode a string.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000352func (o *Buffer) enc_string(p *Properties, base structPointer) error {
353 v := *structPointer_String(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700354 if v == nil {
355 return ErrNil
356 }
357 x := *v
Rob Pike99fa2b62010-12-02 10:39:42 -0800358 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700359 o.EncodeStringBytes(x)
360 return nil
361}
362
David Symonds0bf1ad52013-10-11 09:07:50 +1100363func size_string(p *Properties, base structPointer) (n int) {
364 v := *structPointer_String(base, p.field)
365 if v == nil {
366 return 0
367 }
368 x := *v
369 n += len(p.tagcode)
370 n += sizeStringBytes(x)
371 return
372}
373
Rob Pike97e934d2011-04-11 12:52:49 -0700374// All protocol buffer fields are nillable, but be careful.
375func isNil(v reflect.Value) bool {
376 switch v.Kind() {
David Symonds007ed9d2012-07-24 10:59:36 +1000377 case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
Rob Pike97e934d2011-04-11 12:52:49 -0700378 return v.IsNil()
379 }
380 return false
381}
382
Rob Pikeaaa3a622010-03-20 22:32:34 -0700383// Encode a message struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000384func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000385 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000386 structp := structPointer_GetStructPointer(base, p.field)
387 if structPointer_IsNil(structp) {
David Symondsa80b2822012-03-14 14:31:25 +1100388 return ErrNil
389 }
390
Rob Pikeaaa3a622010-03-20 22:32:34 -0700391 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100392 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000393 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700394 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000395 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700396 return err
397 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800398 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700399 o.EncodeRawBytes(data)
400 return nil
401 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700402
403 // need the length before we can write out the message itself,
404 // so marshal into a separate byte buffer first.
405 obuf := o.buf
406 o.buf = o.bufalloc()
407
Russ Coxd4ce3f12012-09-12 10:36:26 +1000408 err := o.enc_struct(p.stype, p.sprop, structp)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700409
410 nbuf := o.buf
411 o.buf = obuf
David Symonds4646c372013-09-09 13:18:58 +1000412 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700413 o.buffree(nbuf)
414 return err
415 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800416 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700417 o.EncodeRawBytes(nbuf)
418 o.buffree(nbuf)
David Symonds4646c372013-09-09 13:18:58 +1000419 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700420}
421
David Symonds0bf1ad52013-10-11 09:07:50 +1100422func size_struct_message(p *Properties, base structPointer) int {
423 structp := structPointer_GetStructPointer(base, p.field)
424 if structPointer_IsNil(structp) {
425 return 0
426 }
427
428 // Can the object marshal itself?
429 if p.isMarshaler {
430 m := structPointer_Interface(structp, p.stype).(Marshaler)
431 data, _ := m.Marshal()
432 n0 := len(p.tagcode)
433 n1 := sizeRawBytes(data)
434 return n0 + n1
435 }
436
437 n0 := len(p.tagcode)
438 n1 := size_struct(p.stype, p.sprop, structp)
439 n2 := sizeVarint(uint64(n1)) // size of encoded length
440 return n0 + n1 + n2
441}
442
Rob Pikeaaa3a622010-03-20 22:32:34 -0700443// Encode a group struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000444func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000445 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000446 b := structPointer_GetStructPointer(base, p.field)
447 if structPointer_IsNil(b) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700448 return ErrNil
449 }
450
451 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
David Symonds6a6f82c2012-08-22 09:18:54 +1000452 err := o.enc_struct(p.stype, p.sprop, b)
David Symonds4646c372013-09-09 13:18:58 +1000453 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700454 return err
455 }
456 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
David Symonds4646c372013-09-09 13:18:58 +1000457 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700458}
459
David Symonds0bf1ad52013-10-11 09:07:50 +1100460func size_struct_group(p *Properties, base structPointer) (n int) {
461 b := structPointer_GetStructPointer(base, p.field)
462 if structPointer_IsNil(b) {
463 return 0
464 }
465
466 n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
467 n += size_struct(p.stype, p.sprop, b)
468 n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
469 return
470}
471
Rob Pikeaaa3a622010-03-20 22:32:34 -0700472// Encode a slice of bools ([]bool).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000473func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
474 s := *structPointer_BoolSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700475 l := len(s)
476 if l == 0 {
477 return ErrNil
478 }
479 for _, x := range s {
Rob Pike99fa2b62010-12-02 10:39:42 -0800480 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000481 v := uint64(0)
482 if x {
483 v = 1
Rob Pikeaaa3a622010-03-20 22:32:34 -0700484 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000485 p.valEnc(o, v)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700486 }
487 return nil
488}
489
David Symonds0bf1ad52013-10-11 09:07:50 +1100490func size_slice_bool(p *Properties, base structPointer) int {
491 s := *structPointer_BoolSlice(base, p.field)
492 l := len(s)
493 if l == 0 {
494 return 0
495 }
496 return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
497}
498
David Symonds5b7775e2010-12-01 10:09:04 +1100499// Encode a slice of bools ([]bool) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000500func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
501 s := *structPointer_BoolSlice(base, p.field)
David Symonds5b7775e2010-12-01 10:09:04 +1100502 l := len(s)
503 if l == 0 {
504 return ErrNil
505 }
506 o.buf = append(o.buf, p.tagcode...)
507 o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
508 for _, x := range s {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000509 v := uint64(0)
510 if x {
511 v = 1
David Symonds5b7775e2010-12-01 10:09:04 +1100512 }
Russ Coxd4ce3f12012-09-12 10:36:26 +1000513 p.valEnc(o, v)
David Symonds5b7775e2010-12-01 10:09:04 +1100514 }
515 return nil
516}
517
David Symonds0bf1ad52013-10-11 09:07:50 +1100518func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
519 s := *structPointer_BoolSlice(base, p.field)
520 l := len(s)
521 if l == 0 {
522 return 0
523 }
524 n += len(p.tagcode)
525 n += sizeVarint(uint64(l))
526 n += l // each bool takes exactly one byte
527 return
528}
529
Rob Pikeaaa3a622010-03-20 22:32:34 -0700530// Encode a slice of bytes ([]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000531func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
532 s := *structPointer_Bytes(base, p.field)
Mikkel Krautzdb488aa2010-11-29 14:15:51 -0800533 if s == nil {
534 return ErrNil
535 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800536 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700537 o.EncodeRawBytes(s)
538 return nil
539}
540
David Symonds0bf1ad52013-10-11 09:07:50 +1100541func size_slice_byte(p *Properties, base structPointer) (n int) {
542 s := *structPointer_Bytes(base, p.field)
543 if s == nil {
544 return 0
545 }
546 n += len(p.tagcode)
547 n += sizeRawBytes(s)
548 return
549}
550
Rob Pikeaaa3a622010-03-20 22:32:34 -0700551// Encode a slice of int32s ([]int32).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000552func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
553 s := structPointer_Word32Slice(base, p.field)
554 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700555 if l == 0 {
556 return ErrNil
557 }
558 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800559 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000560 x := s.Index(i)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700561 p.valEnc(o, uint64(x))
562 }
563 return nil
564}
565
David Symonds0bf1ad52013-10-11 09:07:50 +1100566func size_slice_int32(p *Properties, base structPointer) (n int) {
567 s := structPointer_Word32Slice(base, p.field)
568 l := s.Len()
569 if l == 0 {
570 return 0
571 }
572 for i := 0; i < l; i++ {
573 n += len(p.tagcode)
574 x := s.Index(i)
575 n += p.valSize(uint64(x))
576 }
577 return
578}
579
David Symonds5b7775e2010-12-01 10:09:04 +1100580// Encode a slice of int32s ([]int32) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000581func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
582 s := structPointer_Word32Slice(base, p.field)
583 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100584 if l == 0 {
585 return ErrNil
586 }
587 // TODO: Reuse a Buffer.
588 buf := NewBuffer(nil)
589 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000590 p.valEnc(buf, uint64(s.Index(i)))
David Symonds5b7775e2010-12-01 10:09:04 +1100591 }
592
593 o.buf = append(o.buf, p.tagcode...)
594 o.EncodeVarint(uint64(len(buf.buf)))
595 o.buf = append(o.buf, buf.buf...)
596 return nil
597}
598
David Symonds0bf1ad52013-10-11 09:07:50 +1100599func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
600 s := structPointer_Word32Slice(base, p.field)
601 l := s.Len()
602 if l == 0 {
603 return 0
604 }
605 var bufSize int
606 for i := 0; i < l; i++ {
607 bufSize += p.valSize(uint64(s.Index(i)))
608 }
609
610 n += len(p.tagcode)
611 n += sizeVarint(uint64(bufSize))
612 n += bufSize
613 return
614}
615
Rob Pikeaaa3a622010-03-20 22:32:34 -0700616// Encode a slice of int64s ([]int64).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000617func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
618 s := structPointer_Word64Slice(base, p.field)
619 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700620 if l == 0 {
621 return ErrNil
622 }
623 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800624 o.buf = append(o.buf, p.tagcode...)
Russ Coxd4ce3f12012-09-12 10:36:26 +1000625 p.valEnc(o, s.Index(i))
Rob Pikeaaa3a622010-03-20 22:32:34 -0700626 }
627 return nil
628}
629
David Symonds0bf1ad52013-10-11 09:07:50 +1100630func size_slice_int64(p *Properties, base structPointer) (n int) {
631 s := structPointer_Word64Slice(base, p.field)
632 l := s.Len()
633 if l == 0 {
634 return 0
635 }
636 for i := 0; i < l; i++ {
637 n += len(p.tagcode)
638 n += p.valSize(s.Index(i))
639 }
640 return
641}
642
David Symonds5b7775e2010-12-01 10:09:04 +1100643// Encode a slice of int64s ([]int64) in packed format.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000644func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
645 s := structPointer_Word64Slice(base, p.field)
646 l := s.Len()
David Symonds5b7775e2010-12-01 10:09:04 +1100647 if l == 0 {
648 return ErrNil
649 }
650 // TODO: Reuse a Buffer.
651 buf := NewBuffer(nil)
652 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000653 p.valEnc(buf, s.Index(i))
David Symonds5b7775e2010-12-01 10:09:04 +1100654 }
655
656 o.buf = append(o.buf, p.tagcode...)
657 o.EncodeVarint(uint64(len(buf.buf)))
658 o.buf = append(o.buf, buf.buf...)
659 return nil
660}
661
David Symonds0bf1ad52013-10-11 09:07:50 +1100662func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
663 s := structPointer_Word64Slice(base, p.field)
664 l := s.Len()
665 if l == 0 {
666 return 0
667 }
668 var bufSize int
669 for i := 0; i < l; i++ {
670 bufSize += p.valSize(s.Index(i))
671 }
672
673 n += len(p.tagcode)
674 n += sizeVarint(uint64(bufSize))
675 n += bufSize
676 return
677}
678
Rob Pikeaaa3a622010-03-20 22:32:34 -0700679// Encode a slice of slice of bytes ([][]byte).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000680func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
681 ss := *structPointer_BytesSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700682 l := len(ss)
683 if l == 0 {
684 return ErrNil
685 }
686 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800687 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100688 o.EncodeRawBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700689 }
690 return nil
691}
692
David Symonds0bf1ad52013-10-11 09:07:50 +1100693func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
694 ss := *structPointer_BytesSlice(base, p.field)
695 l := len(ss)
696 if l == 0 {
697 return 0
698 }
699 n += l * len(p.tagcode)
700 for i := 0; i < l; i++ {
701 n += sizeRawBytes(ss[i])
702 }
703 return
704}
705
Rob Pikeaaa3a622010-03-20 22:32:34 -0700706// Encode a slice of strings ([]string).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000707func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
708 ss := *structPointer_StringSlice(base, p.field)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700709 l := len(ss)
710 for i := 0; i < l; i++ {
Rob Pike99fa2b62010-12-02 10:39:42 -0800711 o.buf = append(o.buf, p.tagcode...)
David Symonds0bf1ad52013-10-11 09:07:50 +1100712 o.EncodeStringBytes(ss[i])
Rob Pikeaaa3a622010-03-20 22:32:34 -0700713 }
714 return nil
715}
716
David Symonds0bf1ad52013-10-11 09:07:50 +1100717func size_slice_string(p *Properties, base structPointer) (n int) {
718 ss := *structPointer_StringSlice(base, p.field)
719 l := len(ss)
720 n += l * len(p.tagcode)
721 for i := 0; i < l; i++ {
722 n += sizeStringBytes(ss[i])
723 }
724 return
725}
726
Rob Pikeaaa3a622010-03-20 22:32:34 -0700727// Encode a slice of message structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000728func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000729 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000730 s := structPointer_StructPointerSlice(base, p.field)
731 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700732
733 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000734 structp := s.Index(i)
735 if structPointer_IsNil(structp) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700736 return ErrRepeatedHasNil
737 }
738
739 // Can the object marshal itself?
David Symondsa80b2822012-03-14 14:31:25 +1100740 if p.isMarshaler {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000741 m := structPointer_Interface(structp, p.stype).(Marshaler)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700742 data, err := m.Marshal()
David Symonds4646c372013-09-09 13:18:58 +1000743 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700744 return err
745 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800746 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700747 o.EncodeRawBytes(data)
748 continue
749 }
750
751 obuf := o.buf
752 o.buf = o.bufalloc()
753
Russ Coxd4ce3f12012-09-12 10:36:26 +1000754 err := o.enc_struct(p.stype, p.sprop, structp)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700755
756 nbuf := o.buf
757 o.buf = obuf
David Symonds4646c372013-09-09 13:18:58 +1000758 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700759 o.buffree(nbuf)
760 if err == ErrNil {
761 return ErrRepeatedHasNil
762 }
763 return err
764 }
Rob Pike99fa2b62010-12-02 10:39:42 -0800765 o.buf = append(o.buf, p.tagcode...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700766 o.EncodeRawBytes(nbuf)
767
768 o.buffree(nbuf)
769 }
David Symonds4646c372013-09-09 13:18:58 +1000770 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700771}
772
David Symonds0bf1ad52013-10-11 09:07:50 +1100773func size_slice_struct_message(p *Properties, base structPointer) (n int) {
774 s := structPointer_StructPointerSlice(base, p.field)
775 l := s.Len()
776 n += l * len(p.tagcode)
777 for i := 0; i < l; i++ {
778 structp := s.Index(i)
779 if structPointer_IsNil(structp) {
780 return // return the size up to this point
781 }
782
783 // Can the object marshal itself?
784 if p.isMarshaler {
785 m := structPointer_Interface(structp, p.stype).(Marshaler)
786 data, _ := m.Marshal()
787 n += len(p.tagcode)
788 n += sizeRawBytes(data)
789 continue
790 }
791
792 n0 := size_struct(p.stype, p.sprop, structp)
793 n1 := sizeVarint(uint64(n0)) // size of encoded length
794 n += n0 + n1
795 }
796 return
797}
798
Rob Pikeaaa3a622010-03-20 22:32:34 -0700799// Encode a slice of group structs ([]*struct).
Russ Coxd4ce3f12012-09-12 10:36:26 +1000800func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000801 var state errorState
Russ Coxd4ce3f12012-09-12 10:36:26 +1000802 s := structPointer_StructPointerSlice(base, p.field)
803 l := s.Len()
Rob Pikeaaa3a622010-03-20 22:32:34 -0700804
805 for i := 0; i < l; i++ {
Russ Coxd4ce3f12012-09-12 10:36:26 +1000806 b := s.Index(i)
807 if structPointer_IsNil(b) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700808 return ErrRepeatedHasNil
809 }
810
811 o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
812
David Symonds6a6f82c2012-08-22 09:18:54 +1000813 err := o.enc_struct(p.stype, p.sprop, b)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700814
David Symonds4646c372013-09-09 13:18:58 +1000815 if err != nil && !state.shouldContinue(err, nil) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700816 if err == ErrNil {
817 return ErrRepeatedHasNil
818 }
819 return err
820 }
821
822 o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
823 }
David Symonds4646c372013-09-09 13:18:58 +1000824 return state.err
Rob Pikeaaa3a622010-03-20 22:32:34 -0700825}
826
David Symonds0bf1ad52013-10-11 09:07:50 +1100827func size_slice_struct_group(p *Properties, base structPointer) (n int) {
828 s := structPointer_StructPointerSlice(base, p.field)
829 l := s.Len()
830
831 n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
832 n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
833 for i := 0; i < l; i++ {
834 b := s.Index(i)
835 if structPointer_IsNil(b) {
836 return // return size up to this point
837 }
838
839 n += size_struct(p.stype, p.sprop, b)
840 }
841 return
842}
843
Rob Pikeaaa3a622010-03-20 22:32:34 -0700844// Encode an extension map.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000845func (o *Buffer) enc_map(p *Properties, base structPointer) error {
846 v := *structPointer_ExtMap(base, p.field)
David Symonds1d72f7a2011-08-19 18:28:52 +1000847 if err := encodeExtensionMap(v); err != nil {
848 return err
849 }
David Symondsdf583ae2012-12-06 14:06:53 +1100850 // Fast-path for common cases: zero or one extensions.
851 if len(v) <= 1 {
852 for _, e := range v {
853 o.buf = append(o.buf, e.enc...)
854 }
855 return nil
856 }
857
858 // Sort keys to provide a deterministic encoding.
859 keys := make([]int, 0, len(v))
860 for k := range v {
861 keys = append(keys, int(k))
862 }
863 sort.Ints(keys)
864
865 for _, k := range keys {
866 o.buf = append(o.buf, v[int32(k)].enc...)
Rob Pikeaaa3a622010-03-20 22:32:34 -0700867 }
868 return nil
869}
870
David Symonds0bf1ad52013-10-11 09:07:50 +1100871func size_map(p *Properties, base structPointer) int {
872 v := *structPointer_ExtMap(base, p.field)
873 return sizeExtensionMap(v)
874}
875
Rob Pikeaaa3a622010-03-20 22:32:34 -0700876// Encode a struct.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000877func (o *Buffer) enc_struct(t reflect.Type, prop *StructProperties, base structPointer) error {
David Symonds4646c372013-09-09 13:18:58 +1000878 var state errorState
David Symondsd15e81b2011-10-03 14:31:12 -0700879 // Encode fields in tag order so that decoders may use optimizations
880 // that depend on the ordering.
881 // http://code.google.com/apis/protocolbuffers/docs/encoding.html#order
882 for _, i := range prop.order {
883 p := prop.Prop[i]
Rob Pikeaaa3a622010-03-20 22:32:34 -0700884 if p.enc != nil {
885 err := p.enc(o, p, base)
David Symonds4a2eeb52013-09-25 11:54:08 +1000886 if err != nil {
887 if err == ErrNil {
888 if p.Required && state.err == nil {
David Symondse583a5f2013-09-27 10:02:37 +1000889 state.err = &RequiredNotSetError{p.Name}
David Symonds4a2eeb52013-09-25 11:54:08 +1000890 }
891 } else if !state.shouldContinue(err, p) {
Rob Pikeaaa3a622010-03-20 22:32:34 -0700892 return err
893 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700894 }
895 }
896 }
Rob Pikeaaa3a622010-03-20 22:32:34 -0700897
David Symonds10c93ba2012-08-04 16:38:08 +1000898 // Add unrecognized fields at the end.
Russ Coxd4ce3f12012-09-12 10:36:26 +1000899 if prop.unrecField.IsValid() {
900 v := *structPointer_Bytes(base, prop.unrecField)
901 if len(v) > 0 {
902 o.buf = append(o.buf, v...)
903 }
David Symonds10c93ba2012-08-04 16:38:08 +1000904 }
905
David Symonds4646c372013-09-09 13:18:58 +1000906 return state.err
907}
908
David Symonds0bf1ad52013-10-11 09:07:50 +1100909func size_struct(t reflect.Type, prop *StructProperties, base structPointer) (n int) {
910 for _, i := range prop.order {
911 p := prop.Prop[i]
912 if p.size != nil {
913 n += p.size(p, base)
914 }
915 }
916
917 // Add unrecognized fields at the end.
918 if prop.unrecField.IsValid() {
919 v := *structPointer_Bytes(base, prop.unrecField)
920 n += len(v)
921 }
922
923 return
924}
925
David Symonds4646c372013-09-09 13:18:58 +1000926// errorState maintains the first error that occurs and updates that error
927// with additional context.
928type errorState struct {
929 err error
930}
931
932// shouldContinue reports whether encoding should continue upon encountering the
David Symondse583a5f2013-09-27 10:02:37 +1000933// given error. If the error is RequiredNotSetError, shouldContinue returns true
David Symonds4646c372013-09-09 13:18:58 +1000934// and, if this is the first appearance of that error, remembers it for future
935// reporting.
936//
937// If prop is not nil, it may update any error with additional context about the
938// field with the error.
939func (s *errorState) shouldContinue(err error, prop *Properties) bool {
940 // Ignore unset required fields.
David Symondse583a5f2013-09-27 10:02:37 +1000941 reqNotSet, ok := err.(*RequiredNotSetError)
David Symonds4646c372013-09-09 13:18:58 +1000942 if !ok {
943 return false
944 }
945 if s.err == nil {
946 if prop != nil {
David Symondse583a5f2013-09-27 10:02:37 +1000947 err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
David Symonds4646c372013-09-09 13:18:58 +1000948 }
949 s.err = err
950 }
951 return true
Rob Pikeaaa3a622010-03-20 22:32:34 -0700952}