1// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2010 The Go Authors.  All rights reserved.
4// https://github.com/golang/protobuf
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 (
39	"errors"
40	"fmt"
41	"reflect"
42	"sort"
43)
44
45// RequiredNotSetError is the error returned if Marshal is called with
46// a protocol buffer struct whose required fields have not
47// 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.
50//
51// When printed, RequiredNotSetError reports the first unset required field in a
52// message. If the field cannot be precisely determined, it is reported as
53// "{Unknown}".
54type RequiredNotSetError struct {
55	field string
56}
57
58func (e *RequiredNotSetError) Error() string {
59	return fmt.Sprintf("proto: required field %q not set", e.field)
60}
61
62var (
63	// errRepeatedHasNil is the error returned if Marshal is called with
64	// a struct with a repeated field containing a nil element.
65	errRepeatedHasNil = errors.New("proto: repeated field has nil element")
66
67	// errOneofHasNil is the error returned if Marshal is called with
68	// a struct with a oneof field containing a nil element.
69	errOneofHasNil = errors.New("proto: oneof field has nil value")
70
71	// ErrNil is the error returned if Marshal is called with nil.
72	ErrNil = errors.New("proto: Marshal called with nil")
73
74	// ErrTooLarge is the error returned if Marshal is called with a
75	// message that encodes to >2GB.
76	ErrTooLarge = errors.New("proto: message encodes to over 2 GB")
77)
78
79// The fundamental encoders that put bytes on the wire.
80// Those that take integer types all accept uint64 and are
81// therefore of type valueEncoder.
82
83const maxVarintBytes = 10 // maximum length of a varint
84
85// maxMarshalSize is the largest allowed size of an encoded protobuf,
86// since C++ and Java use signed int32s for the size.
87const maxMarshalSize = 1<<31 - 1
88
89// EncodeVarint returns the varint encoding of x.
90// This is the format for the
91// int32, int64, uint32, uint64, bool, and enum
92// protocol buffer types.
93// Not used by the package itself, but helpful to clients
94// wishing to use the same encoding.
95func EncodeVarint(x uint64) []byte {
96	var buf [maxVarintBytes]byte
97	var n int
98	for n = 0; x > 127; n++ {
99		buf[n] = 0x80 | uint8(x&0x7F)
100		x >>= 7
101	}
102	buf[n] = uint8(x)
103	n++
104	return buf[0:n]
105}
106
107// EncodeVarint writes a varint-encoded integer to the Buffer.
108// This is the format for the
109// int32, int64, uint32, uint64, bool, and enum
110// protocol buffer types.
111func (p *Buffer) EncodeVarint(x uint64) error {
112	for x >= 1<<7 {
113		p.buf = append(p.buf, uint8(x&0x7f|0x80))
114		x >>= 7
115	}
116	p.buf = append(p.buf, uint8(x))
117	return nil
118}
119
120// SizeVarint returns the varint encoding size of an integer.
121func SizeVarint(x uint64) int {
122	return sizeVarint(x)
123}
124
125func sizeVarint(x uint64) (n int) {
126	for {
127		n++
128		x >>= 7
129		if x == 0 {
130			break
131		}
132	}
133	return n
134}
135
136// EncodeFixed64 writes a 64-bit integer to the Buffer.
137// This is the format for the
138// fixed64, sfixed64, and double protocol buffer types.
139func (p *Buffer) EncodeFixed64(x uint64) error {
140	p.buf = append(p.buf,
141		uint8(x),
142		uint8(x>>8),
143		uint8(x>>16),
144		uint8(x>>24),
145		uint8(x>>32),
146		uint8(x>>40),
147		uint8(x>>48),
148		uint8(x>>56))
149	return nil
150}
151
152func sizeFixed64(x uint64) int {
153	return 8
154}
155
156// EncodeFixed32 writes a 32-bit integer to the Buffer.
157// This is the format for the
158// fixed32, sfixed32, and float protocol buffer types.
159func (p *Buffer) EncodeFixed32(x uint64) error {
160	p.buf = append(p.buf,
161		uint8(x),
162		uint8(x>>8),
163		uint8(x>>16),
164		uint8(x>>24))
165	return nil
166}
167
168func sizeFixed32(x uint64) int {
169	return 4
170}
171
172// EncodeZigzag64 writes a zigzag-encoded 64-bit integer
173// to the Buffer.
174// This is the format used for the sint64 protocol buffer type.
175func (p *Buffer) EncodeZigzag64(x uint64) error {
176	// use signed number to get arithmetic right shift.
177	return p.EncodeVarint((x << 1) ^ uint64((int64(x) >> 63)))
178}
179
180func sizeZigzag64(x uint64) int {
181	return sizeVarint((x << 1) ^ uint64((int64(x) >> 63)))
182}
183
184// EncodeZigzag32 writes a zigzag-encoded 32-bit integer
185// to the Buffer.
186// This is the format used for the sint32 protocol buffer type.
187func (p *Buffer) EncodeZigzag32(x uint64) error {
188	// use signed number to get arithmetic right shift.
189	return p.EncodeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
190}
191
192func sizeZigzag32(x uint64) int {
193	return sizeVarint(uint64((uint32(x) << 1) ^ uint32((int32(x) >> 31))))
194}
195
196// EncodeRawBytes writes a count-delimited byte buffer to the Buffer.
197// This is the format used for the bytes protocol buffer
198// type and for embedded messages.
199func (p *Buffer) EncodeRawBytes(b []byte) error {
200	p.EncodeVarint(uint64(len(b)))
201	p.buf = append(p.buf, b...)
202	return nil
203}
204
205func sizeRawBytes(b []byte) int {
206	return sizeVarint(uint64(len(b))) +
207		len(b)
208}
209
210// EncodeStringBytes writes an encoded string to the Buffer.
211// This is the format used for the proto2 string type.
212func (p *Buffer) EncodeStringBytes(s string) error {
213	p.EncodeVarint(uint64(len(s)))
214	p.buf = append(p.buf, s...)
215	return nil
216}
217
218func sizeStringBytes(s string) int {
219	return sizeVarint(uint64(len(s))) +
220		len(s)
221}
222
223// Marshaler is the interface representing objects that can marshal themselves.
224type Marshaler interface {
225	Marshal() ([]byte, error)
226}
227
228// Marshal takes the protocol buffer
229// and encodes it into the wire format, returning the data.
230func Marshal(pb Message) ([]byte, error) {
231	// Can the object marshal itself?
232	if m, ok := pb.(Marshaler); ok {
233		return m.Marshal()
234	}
235	p := NewBuffer(nil)
236	err := p.Marshal(pb)
237	if p.buf == nil && err == nil {
238		// Return a non-nil slice on success.
239		return []byte{}, nil
240	}
241	return p.buf, err
242}
243
244// EncodeMessage writes the protocol buffer to the Buffer,
245// prefixed by a varint-encoded length.
246func (p *Buffer) EncodeMessage(pb Message) error {
247	t, base, err := getbase(pb)
248	if structPointer_IsNil(base) {
249		return ErrNil
250	}
251	if err == nil {
252		var state errorState
253		err = p.enc_len_struct(GetProperties(t.Elem()), base, &state)
254	}
255	return err
256}
257
258// Marshal takes the protocol buffer
259// and encodes it into the wire format, writing the result to the
260// Buffer.
261func (p *Buffer) Marshal(pb Message) error {
262	// Can the object marshal itself?
263	if m, ok := pb.(Marshaler); ok {
264		data, err := m.Marshal()
265		p.buf = append(p.buf, data...)
266		return err
267	}
268
269	t, base, err := getbase(pb)
270	if structPointer_IsNil(base) {
271		return ErrNil
272	}
273	if err == nil {
274		err = p.enc_struct(GetProperties(t.Elem()), base)
275	}
276
277	if collectStats {
278		(stats).Encode++ // Parens are to work around a goimports bug.
279	}
280
281	if len(p.buf) > maxMarshalSize {
282		return ErrTooLarge
283	}
284	return err
285}
286
287// Size returns the encoded size of a protocol buffer.
288func Size(pb Message) (n int) {
289	// Can the object marshal itself?  If so, Size is slow.
290	// TODO: add Size to Marshaler, or add a Sizer interface.
291	if m, ok := pb.(Marshaler); ok {
292		b, _ := m.Marshal()
293		return len(b)
294	}
295
296	t, base, err := getbase(pb)
297	if structPointer_IsNil(base) {
298		return 0
299	}
300	if err == nil {
301		n = size_struct(GetProperties(t.Elem()), base)
302	}
303
304	if collectStats {
305		(stats).Size++ // Parens are to work around a goimports bug.
306	}
307
308	return
309}
310
311// Individual type encoders.
312
313// Encode a bool.
314func (o *Buffer) enc_bool(p *Properties, base structPointer) error {
315	v := *structPointer_Bool(base, p.field)
316	if v == nil {
317		return ErrNil
318	}
319	x := 0
320	if *v {
321		x = 1
322	}
323	o.buf = append(o.buf, p.tagcode...)
324	p.valEnc(o, uint64(x))
325	return nil
326}
327
328func (o *Buffer) enc_proto3_bool(p *Properties, base structPointer) error {
329	v := *structPointer_BoolVal(base, p.field)
330	if !v {
331		return ErrNil
332	}
333	o.buf = append(o.buf, p.tagcode...)
334	p.valEnc(o, 1)
335	return nil
336}
337
338func size_bool(p *Properties, base structPointer) int {
339	v := *structPointer_Bool(base, p.field)
340	if v == nil {
341		return 0
342	}
343	return len(p.tagcode) + 1 // each bool takes exactly one byte
344}
345
346func size_proto3_bool(p *Properties, base structPointer) int {
347	v := *structPointer_BoolVal(base, p.field)
348	if !v && !p.oneof {
349		return 0
350	}
351	return len(p.tagcode) + 1 // each bool takes exactly one byte
352}
353
354// Encode an int32.
355func (o *Buffer) enc_int32(p *Properties, base structPointer) error {
356	v := structPointer_Word32(base, p.field)
357	if word32_IsNil(v) {
358		return ErrNil
359	}
360	x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
361	o.buf = append(o.buf, p.tagcode...)
362	p.valEnc(o, uint64(x))
363	return nil
364}
365
366func (o *Buffer) enc_proto3_int32(p *Properties, base structPointer) error {
367	v := structPointer_Word32Val(base, p.field)
368	x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
369	if x == 0 {
370		return ErrNil
371	}
372	o.buf = append(o.buf, p.tagcode...)
373	p.valEnc(o, uint64(x))
374	return nil
375}
376
377func size_int32(p *Properties, base structPointer) (n int) {
378	v := structPointer_Word32(base, p.field)
379	if word32_IsNil(v) {
380		return 0
381	}
382	x := int32(word32_Get(v)) // permit sign extension to use full 64-bit range
383	n += len(p.tagcode)
384	n += p.valSize(uint64(x))
385	return
386}
387
388func size_proto3_int32(p *Properties, base structPointer) (n int) {
389	v := structPointer_Word32Val(base, p.field)
390	x := int32(word32Val_Get(v)) // permit sign extension to use full 64-bit range
391	if x == 0 && !p.oneof {
392		return 0
393	}
394	n += len(p.tagcode)
395	n += p.valSize(uint64(x))
396	return
397}
398
399// Encode a uint32.
400// Exactly the same as int32, except for no sign extension.
401func (o *Buffer) enc_uint32(p *Properties, base structPointer) error {
402	v := structPointer_Word32(base, p.field)
403	if word32_IsNil(v) {
404		return ErrNil
405	}
406	x := word32_Get(v)
407	o.buf = append(o.buf, p.tagcode...)
408	p.valEnc(o, uint64(x))
409	return nil
410}
411
412func (o *Buffer) enc_proto3_uint32(p *Properties, base structPointer) error {
413	v := structPointer_Word32Val(base, p.field)
414	x := word32Val_Get(v)
415	if x == 0 {
416		return ErrNil
417	}
418	o.buf = append(o.buf, p.tagcode...)
419	p.valEnc(o, uint64(x))
420	return nil
421}
422
423func size_uint32(p *Properties, base structPointer) (n int) {
424	v := structPointer_Word32(base, p.field)
425	if word32_IsNil(v) {
426		return 0
427	}
428	x := word32_Get(v)
429	n += len(p.tagcode)
430	n += p.valSize(uint64(x))
431	return
432}
433
434func size_proto3_uint32(p *Properties, base structPointer) (n int) {
435	v := structPointer_Word32Val(base, p.field)
436	x := word32Val_Get(v)
437	if x == 0 && !p.oneof {
438		return 0
439	}
440	n += len(p.tagcode)
441	n += p.valSize(uint64(x))
442	return
443}
444
445// Encode an int64.
446func (o *Buffer) enc_int64(p *Properties, base structPointer) error {
447	v := structPointer_Word64(base, p.field)
448	if word64_IsNil(v) {
449		return ErrNil
450	}
451	x := word64_Get(v)
452	o.buf = append(o.buf, p.tagcode...)
453	p.valEnc(o, x)
454	return nil
455}
456
457func (o *Buffer) enc_proto3_int64(p *Properties, base structPointer) error {
458	v := structPointer_Word64Val(base, p.field)
459	x := word64Val_Get(v)
460	if x == 0 {
461		return ErrNil
462	}
463	o.buf = append(o.buf, p.tagcode...)
464	p.valEnc(o, x)
465	return nil
466}
467
468func size_int64(p *Properties, base structPointer) (n int) {
469	v := structPointer_Word64(base, p.field)
470	if word64_IsNil(v) {
471		return 0
472	}
473	x := word64_Get(v)
474	n += len(p.tagcode)
475	n += p.valSize(x)
476	return
477}
478
479func size_proto3_int64(p *Properties, base structPointer) (n int) {
480	v := structPointer_Word64Val(base, p.field)
481	x := word64Val_Get(v)
482	if x == 0 && !p.oneof {
483		return 0
484	}
485	n += len(p.tagcode)
486	n += p.valSize(x)
487	return
488}
489
490// Encode a string.
491func (o *Buffer) enc_string(p *Properties, base structPointer) error {
492	v := *structPointer_String(base, p.field)
493	if v == nil {
494		return ErrNil
495	}
496	x := *v
497	o.buf = append(o.buf, p.tagcode...)
498	o.EncodeStringBytes(x)
499	return nil
500}
501
502func (o *Buffer) enc_proto3_string(p *Properties, base structPointer) error {
503	v := *structPointer_StringVal(base, p.field)
504	if v == "" {
505		return ErrNil
506	}
507	o.buf = append(o.buf, p.tagcode...)
508	o.EncodeStringBytes(v)
509	return nil
510}
511
512func size_string(p *Properties, base structPointer) (n int) {
513	v := *structPointer_String(base, p.field)
514	if v == nil {
515		return 0
516	}
517	x := *v
518	n += len(p.tagcode)
519	n += sizeStringBytes(x)
520	return
521}
522
523func size_proto3_string(p *Properties, base structPointer) (n int) {
524	v := *structPointer_StringVal(base, p.field)
525	if v == "" && !p.oneof {
526		return 0
527	}
528	n += len(p.tagcode)
529	n += sizeStringBytes(v)
530	return
531}
532
533// All protocol buffer fields are nillable, but be careful.
534func isNil(v reflect.Value) bool {
535	switch v.Kind() {
536	case reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
537		return v.IsNil()
538	}
539	return false
540}
541
542// Encode a message struct.
543func (o *Buffer) enc_struct_message(p *Properties, base structPointer) error {
544	var state errorState
545	structp := structPointer_GetStructPointer(base, p.field)
546	if structPointer_IsNil(structp) {
547		return ErrNil
548	}
549
550	// Can the object marshal itself?
551	if p.isMarshaler {
552		m := structPointer_Interface(structp, p.stype).(Marshaler)
553		data, err := m.Marshal()
554		if err != nil && !state.shouldContinue(err, nil) {
555			return err
556		}
557		o.buf = append(o.buf, p.tagcode...)
558		o.EncodeRawBytes(data)
559		return state.err
560	}
561
562	o.buf = append(o.buf, p.tagcode...)
563	return o.enc_len_struct(p.sprop, structp, &state)
564}
565
566func size_struct_message(p *Properties, base structPointer) int {
567	structp := structPointer_GetStructPointer(base, p.field)
568	if structPointer_IsNil(structp) {
569		return 0
570	}
571
572	// Can the object marshal itself?
573	if p.isMarshaler {
574		m := structPointer_Interface(structp, p.stype).(Marshaler)
575		data, _ := m.Marshal()
576		n0 := len(p.tagcode)
577		n1 := sizeRawBytes(data)
578		return n0 + n1
579	}
580
581	n0 := len(p.tagcode)
582	n1 := size_struct(p.sprop, structp)
583	n2 := sizeVarint(uint64(n1)) // size of encoded length
584	return n0 + n1 + n2
585}
586
587// Encode a group struct.
588func (o *Buffer) enc_struct_group(p *Properties, base structPointer) error {
589	var state errorState
590	b := structPointer_GetStructPointer(base, p.field)
591	if structPointer_IsNil(b) {
592		return ErrNil
593	}
594
595	o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
596	err := o.enc_struct(p.sprop, b)
597	if err != nil && !state.shouldContinue(err, nil) {
598		return err
599	}
600	o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
601	return state.err
602}
603
604func size_struct_group(p *Properties, base structPointer) (n int) {
605	b := structPointer_GetStructPointer(base, p.field)
606	if structPointer_IsNil(b) {
607		return 0
608	}
609
610	n += sizeVarint(uint64((p.Tag << 3) | WireStartGroup))
611	n += size_struct(p.sprop, b)
612	n += sizeVarint(uint64((p.Tag << 3) | WireEndGroup))
613	return
614}
615
616// Encode a slice of bools ([]bool).
617func (o *Buffer) enc_slice_bool(p *Properties, base structPointer) error {
618	s := *structPointer_BoolSlice(base, p.field)
619	l := len(s)
620	if l == 0 {
621		return ErrNil
622	}
623	for _, x := range s {
624		o.buf = append(o.buf, p.tagcode...)
625		v := uint64(0)
626		if x {
627			v = 1
628		}
629		p.valEnc(o, v)
630	}
631	return nil
632}
633
634func size_slice_bool(p *Properties, base structPointer) int {
635	s := *structPointer_BoolSlice(base, p.field)
636	l := len(s)
637	if l == 0 {
638		return 0
639	}
640	return l * (len(p.tagcode) + 1) // each bool takes exactly one byte
641}
642
643// Encode a slice of bools ([]bool) in packed format.
644func (o *Buffer) enc_slice_packed_bool(p *Properties, base structPointer) error {
645	s := *structPointer_BoolSlice(base, p.field)
646	l := len(s)
647	if l == 0 {
648		return ErrNil
649	}
650	o.buf = append(o.buf, p.tagcode...)
651	o.EncodeVarint(uint64(l)) // each bool takes exactly one byte
652	for _, x := range s {
653		v := uint64(0)
654		if x {
655			v = 1
656		}
657		p.valEnc(o, v)
658	}
659	return nil
660}
661
662func size_slice_packed_bool(p *Properties, base structPointer) (n int) {
663	s := *structPointer_BoolSlice(base, p.field)
664	l := len(s)
665	if l == 0 {
666		return 0
667	}
668	n += len(p.tagcode)
669	n += sizeVarint(uint64(l))
670	n += l // each bool takes exactly one byte
671	return
672}
673
674// Encode a slice of bytes ([]byte).
675func (o *Buffer) enc_slice_byte(p *Properties, base structPointer) error {
676	s := *structPointer_Bytes(base, p.field)
677	if s == nil {
678		return ErrNil
679	}
680	o.buf = append(o.buf, p.tagcode...)
681	o.EncodeRawBytes(s)
682	return nil
683}
684
685func (o *Buffer) enc_proto3_slice_byte(p *Properties, base structPointer) error {
686	s := *structPointer_Bytes(base, p.field)
687	if len(s) == 0 {
688		return ErrNil
689	}
690	o.buf = append(o.buf, p.tagcode...)
691	o.EncodeRawBytes(s)
692	return nil
693}
694
695func size_slice_byte(p *Properties, base structPointer) (n int) {
696	s := *structPointer_Bytes(base, p.field)
697	if s == nil && !p.oneof {
698		return 0
699	}
700	n += len(p.tagcode)
701	n += sizeRawBytes(s)
702	return
703}
704
705func size_proto3_slice_byte(p *Properties, base structPointer) (n int) {
706	s := *structPointer_Bytes(base, p.field)
707	if len(s) == 0 && !p.oneof {
708		return 0
709	}
710	n += len(p.tagcode)
711	n += sizeRawBytes(s)
712	return
713}
714
715// Encode a slice of int32s ([]int32).
716func (o *Buffer) enc_slice_int32(p *Properties, base structPointer) error {
717	s := structPointer_Word32Slice(base, p.field)
718	l := s.Len()
719	if l == 0 {
720		return ErrNil
721	}
722	for i := 0; i < l; i++ {
723		o.buf = append(o.buf, p.tagcode...)
724		x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
725		p.valEnc(o, uint64(x))
726	}
727	return nil
728}
729
730func size_slice_int32(p *Properties, base structPointer) (n int) {
731	s := structPointer_Word32Slice(base, p.field)
732	l := s.Len()
733	if l == 0 {
734		return 0
735	}
736	for i := 0; i < l; i++ {
737		n += len(p.tagcode)
738		x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
739		n += p.valSize(uint64(x))
740	}
741	return
742}
743
744// Encode a slice of int32s ([]int32) in packed format.
745func (o *Buffer) enc_slice_packed_int32(p *Properties, base structPointer) error {
746	s := structPointer_Word32Slice(base, p.field)
747	l := s.Len()
748	if l == 0 {
749		return ErrNil
750	}
751	// TODO: Reuse a Buffer.
752	buf := NewBuffer(nil)
753	for i := 0; i < l; i++ {
754		x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
755		p.valEnc(buf, uint64(x))
756	}
757
758	o.buf = append(o.buf, p.tagcode...)
759	o.EncodeVarint(uint64(len(buf.buf)))
760	o.buf = append(o.buf, buf.buf...)
761	return nil
762}
763
764func size_slice_packed_int32(p *Properties, base structPointer) (n int) {
765	s := structPointer_Word32Slice(base, p.field)
766	l := s.Len()
767	if l == 0 {
768		return 0
769	}
770	var bufSize int
771	for i := 0; i < l; i++ {
772		x := int32(s.Index(i)) // permit sign extension to use full 64-bit range
773		bufSize += p.valSize(uint64(x))
774	}
775
776	n += len(p.tagcode)
777	n += sizeVarint(uint64(bufSize))
778	n += bufSize
779	return
780}
781
782// Encode a slice of uint32s ([]uint32).
783// Exactly the same as int32, except for no sign extension.
784func (o *Buffer) enc_slice_uint32(p *Properties, base structPointer) error {
785	s := structPointer_Word32Slice(base, p.field)
786	l := s.Len()
787	if l == 0 {
788		return ErrNil
789	}
790	for i := 0; i < l; i++ {
791		o.buf = append(o.buf, p.tagcode...)
792		x := s.Index(i)
793		p.valEnc(o, uint64(x))
794	}
795	return nil
796}
797
798func size_slice_uint32(p *Properties, base structPointer) (n int) {
799	s := structPointer_Word32Slice(base, p.field)
800	l := s.Len()
801	if l == 0 {
802		return 0
803	}
804	for i := 0; i < l; i++ {
805		n += len(p.tagcode)
806		x := s.Index(i)
807		n += p.valSize(uint64(x))
808	}
809	return
810}
811
812// Encode a slice of uint32s ([]uint32) in packed format.
813// Exactly the same as int32, except for no sign extension.
814func (o *Buffer) enc_slice_packed_uint32(p *Properties, base structPointer) error {
815	s := structPointer_Word32Slice(base, p.field)
816	l := s.Len()
817	if l == 0 {
818		return ErrNil
819	}
820	// TODO: Reuse a Buffer.
821	buf := NewBuffer(nil)
822	for i := 0; i < l; i++ {
823		p.valEnc(buf, uint64(s.Index(i)))
824	}
825
826	o.buf = append(o.buf, p.tagcode...)
827	o.EncodeVarint(uint64(len(buf.buf)))
828	o.buf = append(o.buf, buf.buf...)
829	return nil
830}
831
832func size_slice_packed_uint32(p *Properties, base structPointer) (n int) {
833	s := structPointer_Word32Slice(base, p.field)
834	l := s.Len()
835	if l == 0 {
836		return 0
837	}
838	var bufSize int
839	for i := 0; i < l; i++ {
840		bufSize += p.valSize(uint64(s.Index(i)))
841	}
842
843	n += len(p.tagcode)
844	n += sizeVarint(uint64(bufSize))
845	n += bufSize
846	return
847}
848
849// Encode a slice of int64s ([]int64).
850func (o *Buffer) enc_slice_int64(p *Properties, base structPointer) error {
851	s := structPointer_Word64Slice(base, p.field)
852	l := s.Len()
853	if l == 0 {
854		return ErrNil
855	}
856	for i := 0; i < l; i++ {
857		o.buf = append(o.buf, p.tagcode...)
858		p.valEnc(o, s.Index(i))
859	}
860	return nil
861}
862
863func size_slice_int64(p *Properties, base structPointer) (n int) {
864	s := structPointer_Word64Slice(base, p.field)
865	l := s.Len()
866	if l == 0 {
867		return 0
868	}
869	for i := 0; i < l; i++ {
870		n += len(p.tagcode)
871		n += p.valSize(s.Index(i))
872	}
873	return
874}
875
876// Encode a slice of int64s ([]int64) in packed format.
877func (o *Buffer) enc_slice_packed_int64(p *Properties, base structPointer) error {
878	s := structPointer_Word64Slice(base, p.field)
879	l := s.Len()
880	if l == 0 {
881		return ErrNil
882	}
883	// TODO: Reuse a Buffer.
884	buf := NewBuffer(nil)
885	for i := 0; i < l; i++ {
886		p.valEnc(buf, s.Index(i))
887	}
888
889	o.buf = append(o.buf, p.tagcode...)
890	o.EncodeVarint(uint64(len(buf.buf)))
891	o.buf = append(o.buf, buf.buf...)
892	return nil
893}
894
895func size_slice_packed_int64(p *Properties, base structPointer) (n int) {
896	s := structPointer_Word64Slice(base, p.field)
897	l := s.Len()
898	if l == 0 {
899		return 0
900	}
901	var bufSize int
902	for i := 0; i < l; i++ {
903		bufSize += p.valSize(s.Index(i))
904	}
905
906	n += len(p.tagcode)
907	n += sizeVarint(uint64(bufSize))
908	n += bufSize
909	return
910}
911
912// Encode a slice of slice of bytes ([][]byte).
913func (o *Buffer) enc_slice_slice_byte(p *Properties, base structPointer) error {
914	ss := *structPointer_BytesSlice(base, p.field)
915	l := len(ss)
916	if l == 0 {
917		return ErrNil
918	}
919	for i := 0; i < l; i++ {
920		o.buf = append(o.buf, p.tagcode...)
921		o.EncodeRawBytes(ss[i])
922	}
923	return nil
924}
925
926func size_slice_slice_byte(p *Properties, base structPointer) (n int) {
927	ss := *structPointer_BytesSlice(base, p.field)
928	l := len(ss)
929	if l == 0 {
930		return 0
931	}
932	n += l * len(p.tagcode)
933	for i := 0; i < l; i++ {
934		n += sizeRawBytes(ss[i])
935	}
936	return
937}
938
939// Encode a slice of strings ([]string).
940func (o *Buffer) enc_slice_string(p *Properties, base structPointer) error {
941	ss := *structPointer_StringSlice(base, p.field)
942	l := len(ss)
943	for i := 0; i < l; i++ {
944		o.buf = append(o.buf, p.tagcode...)
945		o.EncodeStringBytes(ss[i])
946	}
947	return nil
948}
949
950func size_slice_string(p *Properties, base structPointer) (n int) {
951	ss := *structPointer_StringSlice(base, p.field)
952	l := len(ss)
953	n += l * len(p.tagcode)
954	for i := 0; i < l; i++ {
955		n += sizeStringBytes(ss[i])
956	}
957	return
958}
959
960// Encode a slice of message structs ([]*struct).
961func (o *Buffer) enc_slice_struct_message(p *Properties, base structPointer) error {
962	var state errorState
963	s := structPointer_StructPointerSlice(base, p.field)
964	l := s.Len()
965
966	for i := 0; i < l; i++ {
967		structp := s.Index(i)
968		if structPointer_IsNil(structp) {
969			return errRepeatedHasNil
970		}
971
972		// Can the object marshal itself?
973		if p.isMarshaler {
974			m := structPointer_Interface(structp, p.stype).(Marshaler)
975			data, err := m.Marshal()
976			if err != nil && !state.shouldContinue(err, nil) {
977				return err
978			}
979			o.buf = append(o.buf, p.tagcode...)
980			o.EncodeRawBytes(data)
981			continue
982		}
983
984		o.buf = append(o.buf, p.tagcode...)
985		err := o.enc_len_struct(p.sprop, structp, &state)
986		if err != nil && !state.shouldContinue(err, nil) {
987			if err == ErrNil {
988				return errRepeatedHasNil
989			}
990			return err
991		}
992	}
993	return state.err
994}
995
996func size_slice_struct_message(p *Properties, base structPointer) (n int) {
997	s := structPointer_StructPointerSlice(base, p.field)
998	l := s.Len()
999	n += l * len(p.tagcode)
1000	for i := 0; i < l; i++ {
1001		structp := s.Index(i)
1002		if structPointer_IsNil(structp) {
1003			return // return the size up to this point
1004		}
1005
1006		// Can the object marshal itself?
1007		if p.isMarshaler {
1008			m := structPointer_Interface(structp, p.stype).(Marshaler)
1009			data, _ := m.Marshal()
1010			n += sizeRawBytes(data)
1011			continue
1012		}
1013
1014		n0 := size_struct(p.sprop, structp)
1015		n1 := sizeVarint(uint64(n0)) // size of encoded length
1016		n += n0 + n1
1017	}
1018	return
1019}
1020
1021// Encode a slice of group structs ([]*struct).
1022func (o *Buffer) enc_slice_struct_group(p *Properties, base structPointer) error {
1023	var state errorState
1024	s := structPointer_StructPointerSlice(base, p.field)
1025	l := s.Len()
1026
1027	for i := 0; i < l; i++ {
1028		b := s.Index(i)
1029		if structPointer_IsNil(b) {
1030			return errRepeatedHasNil
1031		}
1032
1033		o.EncodeVarint(uint64((p.Tag << 3) | WireStartGroup))
1034
1035		err := o.enc_struct(p.sprop, b)
1036
1037		if err != nil && !state.shouldContinue(err, nil) {
1038			if err == ErrNil {
1039				return errRepeatedHasNil
1040			}
1041			return err
1042		}
1043
1044		o.EncodeVarint(uint64((p.Tag << 3) | WireEndGroup))
1045	}
1046	return state.err
1047}
1048
1049func size_slice_struct_group(p *Properties, base structPointer) (n int) {
1050	s := structPointer_StructPointerSlice(base, p.field)
1051	l := s.Len()
1052
1053	n += l * sizeVarint(uint64((p.Tag<<3)|WireStartGroup))
1054	n += l * sizeVarint(uint64((p.Tag<<3)|WireEndGroup))
1055	for i := 0; i < l; i++ {
1056		b := s.Index(i)
1057		if structPointer_IsNil(b) {
1058			return // return size up to this point
1059		}
1060
1061		n += size_struct(p.sprop, b)
1062	}
1063	return
1064}
1065
1066// Encode an extension map.
1067func (o *Buffer) enc_map(p *Properties, base structPointer) error {
1068	exts := structPointer_ExtMap(base, p.field)
1069	if err := encodeExtensionsMap(*exts); err != nil {
1070		return err
1071	}
1072
1073	return o.enc_map_body(*exts)
1074}
1075
1076func (o *Buffer) enc_exts(p *Properties, base structPointer) error {
1077	exts := structPointer_Extensions(base, p.field)
1078
1079	v, mu := exts.extensionsRead()
1080	if v == nil {
1081		return nil
1082	}
1083
1084	mu.Lock()
1085	defer mu.Unlock()
1086	if err := encodeExtensionsMap(v); err != nil {
1087		return err
1088	}
1089
1090	return o.enc_map_body(v)
1091}
1092
1093func (o *Buffer) enc_map_body(v map[int32]Extension) error {
1094	// Fast-path for common cases: zero or one extensions.
1095	if len(v) <= 1 {
1096		for _, e := range v {
1097			o.buf = append(o.buf, e.enc...)
1098		}
1099		return nil
1100	}
1101
1102	// Sort keys to provide a deterministic encoding.
1103	keys := make([]int, 0, len(v))
1104	for k := range v {
1105		keys = append(keys, int(k))
1106	}
1107	sort.Ints(keys)
1108
1109	for _, k := range keys {
1110		o.buf = append(o.buf, v[int32(k)].enc...)
1111	}
1112	return nil
1113}
1114
1115func size_map(p *Properties, base structPointer) int {
1116	v := structPointer_ExtMap(base, p.field)
1117	return extensionsMapSize(*v)
1118}
1119
1120func size_exts(p *Properties, base structPointer) int {
1121	v := structPointer_Extensions(base, p.field)
1122	return extensionsSize(v)
1123}
1124
1125// Encode a map field.
1126func (o *Buffer) enc_new_map(p *Properties, base structPointer) error {
1127	var state errorState // XXX: or do we need to plumb this through?
1128
1129	/*
1130		A map defined as
1131			map<key_type, value_type> map_field = N;
1132		is encoded in the same way as
1133			message MapFieldEntry {
1134				key_type key = 1;
1135				value_type value = 2;
1136			}
1137			repeated MapFieldEntry map_field = N;
1138	*/
1139
1140	v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
1141	if v.Len() == 0 {
1142		return nil
1143	}
1144
1145	keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1146
1147	enc := func() error {
1148		if err := p.mkeyprop.enc(o, p.mkeyprop, keybase); err != nil {
1149			return err
1150		}
1151		if err := p.mvalprop.enc(o, p.mvalprop, valbase); err != nil && err != ErrNil {
1152			return err
1153		}
1154		return nil
1155	}
1156
1157	// Don't sort map keys. It is not required by the spec, and C++ doesn't do it.
1158	for _, key := range v.MapKeys() {
1159		val := v.MapIndex(key)
1160
1161		keycopy.Set(key)
1162		valcopy.Set(val)
1163
1164		o.buf = append(o.buf, p.tagcode...)
1165		if err := o.enc_len_thing(enc, &state); err != nil {
1166			return err
1167		}
1168	}
1169	return nil
1170}
1171
1172func size_new_map(p *Properties, base structPointer) int {
1173	v := structPointer_NewAt(base, p.field, p.mtype).Elem() // map[K]V
1174
1175	keycopy, valcopy, keybase, valbase := mapEncodeScratch(p.mtype)
1176
1177	n := 0
1178	for _, key := range v.MapKeys() {
1179		val := v.MapIndex(key)
1180		keycopy.Set(key)
1181		valcopy.Set(val)
1182
1183		// Tag codes for key and val are the responsibility of the sub-sizer.
1184		keysize := p.mkeyprop.size(p.mkeyprop, keybase)
1185		valsize := p.mvalprop.size(p.mvalprop, valbase)
1186		entry := keysize + valsize
1187		// Add on tag code and length of map entry itself.
1188		n += len(p.tagcode) + sizeVarint(uint64(entry)) + entry
1189	}
1190	return n
1191}
1192
1193// mapEncodeScratch returns a new reflect.Value matching the map's value type,
1194// and a structPointer suitable for passing to an encoder or sizer.
1195func mapEncodeScratch(mapType reflect.Type) (keycopy, valcopy reflect.Value, keybase, valbase structPointer) {
1196	// Prepare addressable doubly-indirect placeholders for the key and value types.
1197	// This is needed because the element-type encoders expect **T, but the map iteration produces T.
1198
1199	keycopy = reflect.New(mapType.Key()).Elem()                 // addressable K
1200	keyptr := reflect.New(reflect.PtrTo(keycopy.Type())).Elem() // addressable *K
1201	keyptr.Set(keycopy.Addr())                                  //
1202	keybase = toStructPointer(keyptr.Addr())                    // **K
1203
1204	// Value types are more varied and require special handling.
1205	switch mapType.Elem().Kind() {
1206	case reflect.Slice:
1207		// []byte
1208		var dummy []byte
1209		valcopy = reflect.ValueOf(&dummy).Elem() // addressable []byte
1210		valbase = toStructPointer(valcopy.Addr())
1211	case reflect.Ptr:
1212		// message; the generated field type is map[K]*Msg (so V is *Msg),
1213		// so we only need one level of indirection.
1214		valcopy = reflect.New(mapType.Elem()).Elem() // addressable V
1215		valbase = toStructPointer(valcopy.Addr())
1216	default:
1217		// everything else
1218		valcopy = reflect.New(mapType.Elem()).Elem()                // addressable V
1219		valptr := reflect.New(reflect.PtrTo(valcopy.Type())).Elem() // addressable *V
1220		valptr.Set(valcopy.Addr())                                  //
1221		valbase = toStructPointer(valptr.Addr())                    // **V
1222	}
1223	return
1224}
1225
1226// Encode a struct.
1227func (o *Buffer) enc_struct(prop *StructProperties, base structPointer) error {
1228	var state errorState
1229	// Encode fields in tag order so that decoders may use optimizations
1230	// that depend on the ordering.
1231	// https://developers.google.com/protocol-buffers/docs/encoding#order
1232	for _, i := range prop.order {
1233		p := prop.Prop[i]
1234		if p.enc != nil {
1235			err := p.enc(o, p, base)
1236			if err != nil {
1237				if err == ErrNil {
1238					if p.Required && state.err == nil {
1239						state.err = &RequiredNotSetError{p.Name}
1240					}
1241				} else if err == errRepeatedHasNil {
1242					// Give more context to nil values in repeated fields.
1243					return errors.New("repeated field " + p.OrigName + " has nil element")
1244				} else if !state.shouldContinue(err, p) {
1245					return err
1246				}
1247			}
1248			if len(o.buf) > maxMarshalSize {
1249				return ErrTooLarge
1250			}
1251		}
1252	}
1253
1254	// Do oneof fields.
1255	if prop.oneofMarshaler != nil {
1256		m := structPointer_Interface(base, prop.stype).(Message)
1257		if err := prop.oneofMarshaler(m, o); err == ErrNil {
1258			return errOneofHasNil
1259		} else if err != nil {
1260			return err
1261		}
1262	}
1263
1264	// Add unrecognized fields at the end.
1265	if prop.unrecField.IsValid() {
1266		v := *structPointer_Bytes(base, prop.unrecField)
1267		if len(o.buf)+len(v) > maxMarshalSize {
1268			return ErrTooLarge
1269		}
1270		if len(v) > 0 {
1271			o.buf = append(o.buf, v...)
1272		}
1273	}
1274
1275	return state.err
1276}
1277
1278func size_struct(prop *StructProperties, base structPointer) (n int) {
1279	for _, i := range prop.order {
1280		p := prop.Prop[i]
1281		if p.size != nil {
1282			n += p.size(p, base)
1283		}
1284	}
1285
1286	// Add unrecognized fields at the end.
1287	if prop.unrecField.IsValid() {
1288		v := *structPointer_Bytes(base, prop.unrecField)
1289		n += len(v)
1290	}
1291
1292	// Factor in any oneof fields.
1293	if prop.oneofSizer != nil {
1294		m := structPointer_Interface(base, prop.stype).(Message)
1295		n += prop.oneofSizer(m)
1296	}
1297
1298	return
1299}
1300
1301var zeroes [20]byte // longer than any conceivable sizeVarint
1302
1303// Encode a struct, preceded by its encoded length (as a varint).
1304func (o *Buffer) enc_len_struct(prop *StructProperties, base structPointer, state *errorState) error {
1305	return o.enc_len_thing(func() error { return o.enc_struct(prop, base) }, state)
1306}
1307
1308// Encode something, preceded by its encoded length (as a varint).
1309func (o *Buffer) enc_len_thing(enc func() error, state *errorState) error {
1310	iLen := len(o.buf)
1311	o.buf = append(o.buf, 0, 0, 0, 0) // reserve four bytes for length
1312	iMsg := len(o.buf)
1313	err := enc()
1314	if err != nil && !state.shouldContinue(err, nil) {
1315		return err
1316	}
1317	lMsg := len(o.buf) - iMsg
1318	lLen := sizeVarint(uint64(lMsg))
1319	switch x := lLen - (iMsg - iLen); {
1320	case x > 0: // actual length is x bytes larger than the space we reserved
1321		// Move msg x bytes right.
1322		o.buf = append(o.buf, zeroes[:x]...)
1323		copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1324	case x < 0: // actual length is x bytes smaller than the space we reserved
1325		// Move msg x bytes left.
1326		copy(o.buf[iMsg+x:], o.buf[iMsg:iMsg+lMsg])
1327		o.buf = o.buf[:len(o.buf)+x] // x is negative
1328	}
1329	// Encode the length in the reserved space.
1330	o.buf = o.buf[:iLen]
1331	o.EncodeVarint(uint64(lMsg))
1332	o.buf = o.buf[:len(o.buf)+lMsg]
1333	return state.err
1334}
1335
1336// errorState maintains the first error that occurs and updates that error
1337// with additional context.
1338type errorState struct {
1339	err error
1340}
1341
1342// shouldContinue reports whether encoding should continue upon encountering the
1343// given error. If the error is RequiredNotSetError, shouldContinue returns true
1344// and, if this is the first appearance of that error, remembers it for future
1345// reporting.
1346//
1347// If prop is not nil, it may update any error with additional context about the
1348// field with the error.
1349func (s *errorState) shouldContinue(err error, prop *Properties) bool {
1350	// Ignore unset required fields.
1351	reqNotSet, ok := err.(*RequiredNotSetError)
1352	if !ok {
1353		return false
1354	}
1355	if s.err == nil {
1356		if prop != nil {
1357			err = &RequiredNotSetError{prop.Name + "." + reqNotSet.field}
1358		}
1359		s.err = err
1360	}
1361	return true
1362}
1363