blob: 9b1538d0559b27765e64160ec28ec55bb192a0c7 [file] [log] [blame]
Scott Bakere7144bc2019-10-01 14:16:47 -07001// Go support for Protocol Buffers - Google's data interchange format
2//
3// Copyright 2016 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
34import (
35 "errors"
36 "fmt"
37 "math"
38 "reflect"
39 "sort"
40 "strconv"
41 "strings"
42 "sync"
43 "sync/atomic"
44 "unicode/utf8"
45)
46
47// a sizer takes a pointer to a field and the size of its tag, computes the size of
48// the encoded data.
49type sizer func(pointer, int) int
50
51// a marshaler takes a byte slice, a pointer to a field, and its tag (in wire format),
52// marshals the field to the end of the slice, returns the slice and error (if any).
53type marshaler func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error)
54
55// marshalInfo is the information used for marshaling a message.
56type marshalInfo struct {
57 typ reflect.Type
58 fields []*marshalFieldInfo
59 unrecognized field // offset of XXX_unrecognized
60 extensions field // offset of XXX_InternalExtensions
61 v1extensions field // offset of XXX_extensions
62 sizecache field // offset of XXX_sizecache
63 initialized int32 // 0 -- only typ is set, 1 -- fully initialized
64 messageset bool // uses message set wire format
65 hasmarshaler bool // has custom marshaler
66 sync.RWMutex // protect extElems map, also for initialization
67 extElems map[int32]*marshalElemInfo // info of extension elements
68
69 hassizer bool // has custom sizer
70 hasprotosizer bool // has custom protosizer
71
72 bytesExtensions field // offset of XXX_extensions where the field type is []byte
73}
74
75// marshalFieldInfo is the information used for marshaling a field of a message.
76type marshalFieldInfo struct {
77 field field
78 wiretag uint64 // tag in wire format
79 tagsize int // size of tag in wire format
80 sizer sizer
81 marshaler marshaler
82 isPointer bool
83 required bool // field is required
84 name string // name of the field, for error reporting
85 oneofElems map[reflect.Type]*marshalElemInfo // info of oneof elements
86}
87
88// marshalElemInfo is the information used for marshaling an extension or oneof element.
89type marshalElemInfo struct {
90 wiretag uint64 // tag in wire format
91 tagsize int // size of tag in wire format
92 sizer sizer
93 marshaler marshaler
94 isptr bool // elem is pointer typed, thus interface of this type is a direct interface (extension only)
95}
96
97var (
98 marshalInfoMap = map[reflect.Type]*marshalInfo{}
99 marshalInfoLock sync.Mutex
100
101 uint8SliceType = reflect.TypeOf(([]uint8)(nil)).Kind()
102)
103
104// getMarshalInfo returns the information to marshal a given type of message.
105// The info it returns may not necessarily initialized.
106// t is the type of the message (NOT the pointer to it).
107func getMarshalInfo(t reflect.Type) *marshalInfo {
108 marshalInfoLock.Lock()
109 u, ok := marshalInfoMap[t]
110 if !ok {
111 u = &marshalInfo{typ: t}
112 marshalInfoMap[t] = u
113 }
114 marshalInfoLock.Unlock()
115 return u
116}
117
118// Size is the entry point from generated code,
119// and should be ONLY called by generated code.
120// It computes the size of encoded data of msg.
121// a is a pointer to a place to store cached marshal info.
122func (a *InternalMessageInfo) Size(msg Message) int {
123 u := getMessageMarshalInfo(msg, a)
124 ptr := toPointer(&msg)
125 if ptr.isNil() {
126 // We get here if msg is a typed nil ((*SomeMessage)(nil)),
127 // so it satisfies the interface, and msg == nil wouldn't
128 // catch it. We don't want crash in this case.
129 return 0
130 }
131 return u.size(ptr)
132}
133
134// Marshal is the entry point from generated code,
135// and should be ONLY called by generated code.
136// It marshals msg to the end of b.
137// a is a pointer to a place to store cached marshal info.
138func (a *InternalMessageInfo) Marshal(b []byte, msg Message, deterministic bool) ([]byte, error) {
139 u := getMessageMarshalInfo(msg, a)
140 ptr := toPointer(&msg)
141 if ptr.isNil() {
142 // We get here if msg is a typed nil ((*SomeMessage)(nil)),
143 // so it satisfies the interface, and msg == nil wouldn't
144 // catch it. We don't want crash in this case.
145 return b, ErrNil
146 }
147 return u.marshal(b, ptr, deterministic)
148}
149
150func getMessageMarshalInfo(msg interface{}, a *InternalMessageInfo) *marshalInfo {
151 // u := a.marshal, but atomically.
152 // We use an atomic here to ensure memory consistency.
153 u := atomicLoadMarshalInfo(&a.marshal)
154 if u == nil {
155 // Get marshal information from type of message.
156 t := reflect.ValueOf(msg).Type()
157 if t.Kind() != reflect.Ptr {
158 panic(fmt.Sprintf("cannot handle non-pointer message type %v", t))
159 }
160 u = getMarshalInfo(t.Elem())
161 // Store it in the cache for later users.
162 // a.marshal = u, but atomically.
163 atomicStoreMarshalInfo(&a.marshal, u)
164 }
165 return u
166}
167
168// size is the main function to compute the size of the encoded data of a message.
169// ptr is the pointer to the message.
170func (u *marshalInfo) size(ptr pointer) int {
171 if atomic.LoadInt32(&u.initialized) == 0 {
172 u.computeMarshalInfo()
173 }
174
175 // If the message can marshal itself, let it do it, for compatibility.
176 // NOTE: This is not efficient.
177 if u.hasmarshaler {
178 // Uses the message's Size method if available
179 if u.hassizer {
180 s := ptr.asPointerTo(u.typ).Interface().(Sizer)
181 return s.Size()
182 }
183 // Uses the message's ProtoSize method if available
184 if u.hasprotosizer {
185 s := ptr.asPointerTo(u.typ).Interface().(ProtoSizer)
186 return s.ProtoSize()
187 }
188
189 m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
190 b, _ := m.Marshal()
191 return len(b)
192 }
193
194 n := 0
195 for _, f := range u.fields {
196 if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
197 // nil pointer always marshals to nothing
198 continue
199 }
200 n += f.sizer(ptr.offset(f.field), f.tagsize)
201 }
202 if u.extensions.IsValid() {
203 e := ptr.offset(u.extensions).toExtensions()
204 if u.messageset {
205 n += u.sizeMessageSet(e)
206 } else {
207 n += u.sizeExtensions(e)
208 }
209 }
210 if u.v1extensions.IsValid() {
211 m := *ptr.offset(u.v1extensions).toOldExtensions()
212 n += u.sizeV1Extensions(m)
213 }
214 if u.bytesExtensions.IsValid() {
215 s := *ptr.offset(u.bytesExtensions).toBytes()
216 n += len(s)
217 }
218 if u.unrecognized.IsValid() {
219 s := *ptr.offset(u.unrecognized).toBytes()
220 n += len(s)
221 }
222
223 // cache the result for use in marshal
224 if u.sizecache.IsValid() {
225 atomic.StoreInt32(ptr.offset(u.sizecache).toInt32(), int32(n))
226 }
227 return n
228}
229
230// cachedsize gets the size from cache. If there is no cache (i.e. message is not generated),
231// fall back to compute the size.
232func (u *marshalInfo) cachedsize(ptr pointer) int {
233 if u.sizecache.IsValid() {
234 return int(atomic.LoadInt32(ptr.offset(u.sizecache).toInt32()))
235 }
236 return u.size(ptr)
237}
238
239// marshal is the main function to marshal a message. It takes a byte slice and appends
240// the encoded data to the end of the slice, returns the slice and error (if any).
241// ptr is the pointer to the message.
242// If deterministic is true, map is marshaled in deterministic order.
243func (u *marshalInfo) marshal(b []byte, ptr pointer, deterministic bool) ([]byte, error) {
244 if atomic.LoadInt32(&u.initialized) == 0 {
245 u.computeMarshalInfo()
246 }
247
248 // If the message can marshal itself, let it do it, for compatibility.
249 // NOTE: This is not efficient.
250 if u.hasmarshaler {
251 m := ptr.asPointerTo(u.typ).Interface().(Marshaler)
252 b1, err := m.Marshal()
253 b = append(b, b1...)
254 return b, err
255 }
256
257 var err, errLater error
258 // The old marshaler encodes extensions at beginning.
259 if u.extensions.IsValid() {
260 e := ptr.offset(u.extensions).toExtensions()
261 if u.messageset {
262 b, err = u.appendMessageSet(b, e, deterministic)
263 } else {
264 b, err = u.appendExtensions(b, e, deterministic)
265 }
266 if err != nil {
267 return b, err
268 }
269 }
270 if u.v1extensions.IsValid() {
271 m := *ptr.offset(u.v1extensions).toOldExtensions()
272 b, err = u.appendV1Extensions(b, m, deterministic)
273 if err != nil {
274 return b, err
275 }
276 }
277 if u.bytesExtensions.IsValid() {
278 s := *ptr.offset(u.bytesExtensions).toBytes()
279 b = append(b, s...)
280 }
281 for _, f := range u.fields {
282 if f.required {
283 if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
284 // Required field is not set.
285 // We record the error but keep going, to give a complete marshaling.
286 if errLater == nil {
287 errLater = &RequiredNotSetError{f.name}
288 }
289 continue
290 }
291 }
292 if f.isPointer && ptr.offset(f.field).getPointer().isNil() {
293 // nil pointer always marshals to nothing
294 continue
295 }
296 b, err = f.marshaler(b, ptr.offset(f.field), f.wiretag, deterministic)
297 if err != nil {
298 if err1, ok := err.(*RequiredNotSetError); ok {
299 // Required field in submessage is not set.
300 // We record the error but keep going, to give a complete marshaling.
301 if errLater == nil {
302 errLater = &RequiredNotSetError{f.name + "." + err1.field}
303 }
304 continue
305 }
306 if err == errRepeatedHasNil {
307 err = errors.New("proto: repeated field " + f.name + " has nil element")
308 }
309 if err == errInvalidUTF8 {
310 if errLater == nil {
311 fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
312 errLater = &invalidUTF8Error{fullName}
313 }
314 continue
315 }
316 return b, err
317 }
318 }
319 if u.unrecognized.IsValid() {
320 s := *ptr.offset(u.unrecognized).toBytes()
321 b = append(b, s...)
322 }
323 return b, errLater
324}
325
326// computeMarshalInfo initializes the marshal info.
327func (u *marshalInfo) computeMarshalInfo() {
328 u.Lock()
329 defer u.Unlock()
330 if u.initialized != 0 { // non-atomic read is ok as it is protected by the lock
331 return
332 }
333
334 t := u.typ
335 u.unrecognized = invalidField
336 u.extensions = invalidField
337 u.v1extensions = invalidField
338 u.bytesExtensions = invalidField
339 u.sizecache = invalidField
340 isOneofMessage := false
341
342 if reflect.PtrTo(t).Implements(sizerType) {
343 u.hassizer = true
344 }
345 if reflect.PtrTo(t).Implements(protosizerType) {
346 u.hasprotosizer = true
347 }
348 // If the message can marshal itself, let it do it, for compatibility.
349 // NOTE: This is not efficient.
350 if reflect.PtrTo(t).Implements(marshalerType) {
351 u.hasmarshaler = true
352 atomic.StoreInt32(&u.initialized, 1)
353 return
354 }
355
356 n := t.NumField()
357
358 // deal with XXX fields first
359 for i := 0; i < t.NumField(); i++ {
360 f := t.Field(i)
361 if f.Tag.Get("protobuf_oneof") != "" {
362 isOneofMessage = true
363 }
364 if !strings.HasPrefix(f.Name, "XXX_") {
365 continue
366 }
367 switch f.Name {
368 case "XXX_sizecache":
369 u.sizecache = toField(&f)
370 case "XXX_unrecognized":
371 u.unrecognized = toField(&f)
372 case "XXX_InternalExtensions":
373 u.extensions = toField(&f)
374 u.messageset = f.Tag.Get("protobuf_messageset") == "1"
375 case "XXX_extensions":
376 if f.Type.Kind() == reflect.Map {
377 u.v1extensions = toField(&f)
378 } else {
379 u.bytesExtensions = toField(&f)
380 }
381 case "XXX_NoUnkeyedLiteral":
382 // nothing to do
383 default:
384 panic("unknown XXX field: " + f.Name)
385 }
386 n--
387 }
388
389 // get oneof implementers
390 var oneofImplementers []interface{}
391 // gogo: isOneofMessage is needed for embedded oneof messages, without a marshaler and unmarshaler
392 if m, ok := reflect.Zero(reflect.PtrTo(t)).Interface().(oneofMessage); ok && isOneofMessage {
393 _, _, _, oneofImplementers = m.XXX_OneofFuncs()
394 }
395
396 // normal fields
397 fields := make([]marshalFieldInfo, n) // batch allocation
398 u.fields = make([]*marshalFieldInfo, 0, n)
399 for i, j := 0, 0; i < t.NumField(); i++ {
400 f := t.Field(i)
401
402 if strings.HasPrefix(f.Name, "XXX_") {
403 continue
404 }
405 field := &fields[j]
406 j++
407 field.name = f.Name
408 u.fields = append(u.fields, field)
409 if f.Tag.Get("protobuf_oneof") != "" {
410 field.computeOneofFieldInfo(&f, oneofImplementers)
411 continue
412 }
413 if f.Tag.Get("protobuf") == "" {
414 // field has no tag (not in generated message), ignore it
415 u.fields = u.fields[:len(u.fields)-1]
416 j--
417 continue
418 }
419 field.computeMarshalFieldInfo(&f)
420 }
421
422 // fields are marshaled in tag order on the wire.
423 sort.Sort(byTag(u.fields))
424
425 atomic.StoreInt32(&u.initialized, 1)
426}
427
428// helper for sorting fields by tag
429type byTag []*marshalFieldInfo
430
431func (a byTag) Len() int { return len(a) }
432func (a byTag) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
433func (a byTag) Less(i, j int) bool { return a[i].wiretag < a[j].wiretag }
434
435// getExtElemInfo returns the information to marshal an extension element.
436// The info it returns is initialized.
437func (u *marshalInfo) getExtElemInfo(desc *ExtensionDesc) *marshalElemInfo {
438 // get from cache first
439 u.RLock()
440 e, ok := u.extElems[desc.Field]
441 u.RUnlock()
442 if ok {
443 return e
444 }
445
446 t := reflect.TypeOf(desc.ExtensionType) // pointer or slice to basic type or struct
447 tags := strings.Split(desc.Tag, ",")
448 tag, err := strconv.Atoi(tags[1])
449 if err != nil {
450 panic("tag is not an integer")
451 }
452 wt := wiretype(tags[0])
453 sizr, marshalr := typeMarshaler(t, tags, false, false)
454 e = &marshalElemInfo{
455 wiretag: uint64(tag)<<3 | wt,
456 tagsize: SizeVarint(uint64(tag) << 3),
457 sizer: sizr,
458 marshaler: marshalr,
459 isptr: t.Kind() == reflect.Ptr,
460 }
461
462 // update cache
463 u.Lock()
464 if u.extElems == nil {
465 u.extElems = make(map[int32]*marshalElemInfo)
466 }
467 u.extElems[desc.Field] = e
468 u.Unlock()
469 return e
470}
471
472// computeMarshalFieldInfo fills up the information to marshal a field.
473func (fi *marshalFieldInfo) computeMarshalFieldInfo(f *reflect.StructField) {
474 // parse protobuf tag of the field.
475 // tag has format of "bytes,49,opt,name=foo,def=hello!"
476 tags := strings.Split(f.Tag.Get("protobuf"), ",")
477 if tags[0] == "" {
478 return
479 }
480 tag, err := strconv.Atoi(tags[1])
481 if err != nil {
482 panic("tag is not an integer")
483 }
484 wt := wiretype(tags[0])
485 if tags[2] == "req" {
486 fi.required = true
487 }
488 fi.setTag(f, tag, wt)
489 fi.setMarshaler(f, tags)
490}
491
492func (fi *marshalFieldInfo) computeOneofFieldInfo(f *reflect.StructField, oneofImplementers []interface{}) {
493 fi.field = toField(f)
494 fi.wiretag = math.MaxInt32 // Use a large tag number, make oneofs sorted at the end. This tag will not appear on the wire.
495 fi.isPointer = true
496 fi.sizer, fi.marshaler = makeOneOfMarshaler(fi, f)
497 fi.oneofElems = make(map[reflect.Type]*marshalElemInfo)
498
499 ityp := f.Type // interface type
500 for _, o := range oneofImplementers {
501 t := reflect.TypeOf(o)
502 if !t.Implements(ityp) {
503 continue
504 }
505 sf := t.Elem().Field(0) // oneof implementer is a struct with a single field
506 tags := strings.Split(sf.Tag.Get("protobuf"), ",")
507 tag, err := strconv.Atoi(tags[1])
508 if err != nil {
509 panic("tag is not an integer")
510 }
511 wt := wiretype(tags[0])
512 sizr, marshalr := typeMarshaler(sf.Type, tags, false, true) // oneof should not omit any zero value
513 fi.oneofElems[t.Elem()] = &marshalElemInfo{
514 wiretag: uint64(tag)<<3 | wt,
515 tagsize: SizeVarint(uint64(tag) << 3),
516 sizer: sizr,
517 marshaler: marshalr,
518 }
519 }
520}
521
522type oneofMessage interface {
523 XXX_OneofFuncs() (func(Message, *Buffer) error, func(Message, int, int, *Buffer) (bool, error), func(Message) int, []interface{})
524}
525
526// wiretype returns the wire encoding of the type.
527func wiretype(encoding string) uint64 {
528 switch encoding {
529 case "fixed32":
530 return WireFixed32
531 case "fixed64":
532 return WireFixed64
533 case "varint", "zigzag32", "zigzag64":
534 return WireVarint
535 case "bytes":
536 return WireBytes
537 case "group":
538 return WireStartGroup
539 }
540 panic("unknown wire type " + encoding)
541}
542
543// setTag fills up the tag (in wire format) and its size in the info of a field.
544func (fi *marshalFieldInfo) setTag(f *reflect.StructField, tag int, wt uint64) {
545 fi.field = toField(f)
546 fi.wiretag = uint64(tag)<<3 | wt
547 fi.tagsize = SizeVarint(uint64(tag) << 3)
548}
549
550// setMarshaler fills up the sizer and marshaler in the info of a field.
551func (fi *marshalFieldInfo) setMarshaler(f *reflect.StructField, tags []string) {
552 switch f.Type.Kind() {
553 case reflect.Map:
554 // map field
555 fi.isPointer = true
556 fi.sizer, fi.marshaler = makeMapMarshaler(f)
557 return
558 case reflect.Ptr, reflect.Slice:
559 fi.isPointer = true
560 }
561 fi.sizer, fi.marshaler = typeMarshaler(f.Type, tags, true, false)
562}
563
564// typeMarshaler returns the sizer and marshaler of a given field.
565// t is the type of the field.
566// tags is the generated "protobuf" tag of the field.
567// If nozero is true, zero value is not marshaled to the wire.
568// If oneof is true, it is a oneof field.
569func typeMarshaler(t reflect.Type, tags []string, nozero, oneof bool) (sizer, marshaler) {
570 encoding := tags[0]
571
572 pointer := false
573 slice := false
574 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
575 slice = true
576 t = t.Elem()
577 }
578 if t.Kind() == reflect.Ptr {
579 pointer = true
580 t = t.Elem()
581 }
582
583 packed := false
584 proto3 := false
585 ctype := false
586 isTime := false
587 isDuration := false
588 isWktPointer := false
589 validateUTF8 := true
590 for i := 2; i < len(tags); i++ {
591 if tags[i] == "packed" {
592 packed = true
593 }
594 if tags[i] == "proto3" {
595 proto3 = true
596 }
597 if strings.HasPrefix(tags[i], "customtype=") {
598 ctype = true
599 }
600 if tags[i] == "stdtime" {
601 isTime = true
602 }
603 if tags[i] == "stdduration" {
604 isDuration = true
605 }
606 if tags[i] == "wktptr" {
607 isWktPointer = true
608 }
609 }
610 validateUTF8 = validateUTF8 && proto3
611 if !proto3 && !pointer && !slice {
612 nozero = false
613 }
614
615 if ctype {
616 if reflect.PtrTo(t).Implements(customType) {
617 if slice {
618 return makeMessageRefSliceMarshaler(getMarshalInfo(t))
619 }
620 if pointer {
621 return makeCustomPtrMarshaler(getMarshalInfo(t))
622 }
623 return makeCustomMarshaler(getMarshalInfo(t))
624 } else {
625 panic(fmt.Sprintf("custom type: type: %v, does not implement the proto.custom interface", t))
626 }
627 }
628
629 if isTime {
630 if pointer {
631 if slice {
632 return makeTimePtrSliceMarshaler(getMarshalInfo(t))
633 }
634 return makeTimePtrMarshaler(getMarshalInfo(t))
635 }
636 if slice {
637 return makeTimeSliceMarshaler(getMarshalInfo(t))
638 }
639 return makeTimeMarshaler(getMarshalInfo(t))
640 }
641
642 if isDuration {
643 if pointer {
644 if slice {
645 return makeDurationPtrSliceMarshaler(getMarshalInfo(t))
646 }
647 return makeDurationPtrMarshaler(getMarshalInfo(t))
648 }
649 if slice {
650 return makeDurationSliceMarshaler(getMarshalInfo(t))
651 }
652 return makeDurationMarshaler(getMarshalInfo(t))
653 }
654
655 if isWktPointer {
656 switch t.Kind() {
657 case reflect.Float64:
658 if pointer {
659 if slice {
660 return makeStdDoubleValuePtrSliceMarshaler(getMarshalInfo(t))
661 }
662 return makeStdDoubleValuePtrMarshaler(getMarshalInfo(t))
663 }
664 if slice {
665 return makeStdDoubleValueSliceMarshaler(getMarshalInfo(t))
666 }
667 return makeStdDoubleValueMarshaler(getMarshalInfo(t))
668 case reflect.Float32:
669 if pointer {
670 if slice {
671 return makeStdFloatValuePtrSliceMarshaler(getMarshalInfo(t))
672 }
673 return makeStdFloatValuePtrMarshaler(getMarshalInfo(t))
674 }
675 if slice {
676 return makeStdFloatValueSliceMarshaler(getMarshalInfo(t))
677 }
678 return makeStdFloatValueMarshaler(getMarshalInfo(t))
679 case reflect.Int64:
680 if pointer {
681 if slice {
682 return makeStdInt64ValuePtrSliceMarshaler(getMarshalInfo(t))
683 }
684 return makeStdInt64ValuePtrMarshaler(getMarshalInfo(t))
685 }
686 if slice {
687 return makeStdInt64ValueSliceMarshaler(getMarshalInfo(t))
688 }
689 return makeStdInt64ValueMarshaler(getMarshalInfo(t))
690 case reflect.Uint64:
691 if pointer {
692 if slice {
693 return makeStdUInt64ValuePtrSliceMarshaler(getMarshalInfo(t))
694 }
695 return makeStdUInt64ValuePtrMarshaler(getMarshalInfo(t))
696 }
697 if slice {
698 return makeStdUInt64ValueSliceMarshaler(getMarshalInfo(t))
699 }
700 return makeStdUInt64ValueMarshaler(getMarshalInfo(t))
701 case reflect.Int32:
702 if pointer {
703 if slice {
704 return makeStdInt32ValuePtrSliceMarshaler(getMarshalInfo(t))
705 }
706 return makeStdInt32ValuePtrMarshaler(getMarshalInfo(t))
707 }
708 if slice {
709 return makeStdInt32ValueSliceMarshaler(getMarshalInfo(t))
710 }
711 return makeStdInt32ValueMarshaler(getMarshalInfo(t))
712 case reflect.Uint32:
713 if pointer {
714 if slice {
715 return makeStdUInt32ValuePtrSliceMarshaler(getMarshalInfo(t))
716 }
717 return makeStdUInt32ValuePtrMarshaler(getMarshalInfo(t))
718 }
719 if slice {
720 return makeStdUInt32ValueSliceMarshaler(getMarshalInfo(t))
721 }
722 return makeStdUInt32ValueMarshaler(getMarshalInfo(t))
723 case reflect.Bool:
724 if pointer {
725 if slice {
726 return makeStdBoolValuePtrSliceMarshaler(getMarshalInfo(t))
727 }
728 return makeStdBoolValuePtrMarshaler(getMarshalInfo(t))
729 }
730 if slice {
731 return makeStdBoolValueSliceMarshaler(getMarshalInfo(t))
732 }
733 return makeStdBoolValueMarshaler(getMarshalInfo(t))
734 case reflect.String:
735 if pointer {
736 if slice {
737 return makeStdStringValuePtrSliceMarshaler(getMarshalInfo(t))
738 }
739 return makeStdStringValuePtrMarshaler(getMarshalInfo(t))
740 }
741 if slice {
742 return makeStdStringValueSliceMarshaler(getMarshalInfo(t))
743 }
744 return makeStdStringValueMarshaler(getMarshalInfo(t))
745 case uint8SliceType:
746 if pointer {
747 if slice {
748 return makeStdBytesValuePtrSliceMarshaler(getMarshalInfo(t))
749 }
750 return makeStdBytesValuePtrMarshaler(getMarshalInfo(t))
751 }
752 if slice {
753 return makeStdBytesValueSliceMarshaler(getMarshalInfo(t))
754 }
755 return makeStdBytesValueMarshaler(getMarshalInfo(t))
756 default:
757 panic(fmt.Sprintf("unknown wktpointer type %#v", t))
758 }
759 }
760
761 switch t.Kind() {
762 case reflect.Bool:
763 if pointer {
764 return sizeBoolPtr, appendBoolPtr
765 }
766 if slice {
767 if packed {
768 return sizeBoolPackedSlice, appendBoolPackedSlice
769 }
770 return sizeBoolSlice, appendBoolSlice
771 }
772 if nozero {
773 return sizeBoolValueNoZero, appendBoolValueNoZero
774 }
775 return sizeBoolValue, appendBoolValue
776 case reflect.Uint32:
777 switch encoding {
778 case "fixed32":
779 if pointer {
780 return sizeFixed32Ptr, appendFixed32Ptr
781 }
782 if slice {
783 if packed {
784 return sizeFixed32PackedSlice, appendFixed32PackedSlice
785 }
786 return sizeFixed32Slice, appendFixed32Slice
787 }
788 if nozero {
789 return sizeFixed32ValueNoZero, appendFixed32ValueNoZero
790 }
791 return sizeFixed32Value, appendFixed32Value
792 case "varint":
793 if pointer {
794 return sizeVarint32Ptr, appendVarint32Ptr
795 }
796 if slice {
797 if packed {
798 return sizeVarint32PackedSlice, appendVarint32PackedSlice
799 }
800 return sizeVarint32Slice, appendVarint32Slice
801 }
802 if nozero {
803 return sizeVarint32ValueNoZero, appendVarint32ValueNoZero
804 }
805 return sizeVarint32Value, appendVarint32Value
806 }
807 case reflect.Int32:
808 switch encoding {
809 case "fixed32":
810 if pointer {
811 return sizeFixedS32Ptr, appendFixedS32Ptr
812 }
813 if slice {
814 if packed {
815 return sizeFixedS32PackedSlice, appendFixedS32PackedSlice
816 }
817 return sizeFixedS32Slice, appendFixedS32Slice
818 }
819 if nozero {
820 return sizeFixedS32ValueNoZero, appendFixedS32ValueNoZero
821 }
822 return sizeFixedS32Value, appendFixedS32Value
823 case "varint":
824 if pointer {
825 return sizeVarintS32Ptr, appendVarintS32Ptr
826 }
827 if slice {
828 if packed {
829 return sizeVarintS32PackedSlice, appendVarintS32PackedSlice
830 }
831 return sizeVarintS32Slice, appendVarintS32Slice
832 }
833 if nozero {
834 return sizeVarintS32ValueNoZero, appendVarintS32ValueNoZero
835 }
836 return sizeVarintS32Value, appendVarintS32Value
837 case "zigzag32":
838 if pointer {
839 return sizeZigzag32Ptr, appendZigzag32Ptr
840 }
841 if slice {
842 if packed {
843 return sizeZigzag32PackedSlice, appendZigzag32PackedSlice
844 }
845 return sizeZigzag32Slice, appendZigzag32Slice
846 }
847 if nozero {
848 return sizeZigzag32ValueNoZero, appendZigzag32ValueNoZero
849 }
850 return sizeZigzag32Value, appendZigzag32Value
851 }
852 case reflect.Uint64:
853 switch encoding {
854 case "fixed64":
855 if pointer {
856 return sizeFixed64Ptr, appendFixed64Ptr
857 }
858 if slice {
859 if packed {
860 return sizeFixed64PackedSlice, appendFixed64PackedSlice
861 }
862 return sizeFixed64Slice, appendFixed64Slice
863 }
864 if nozero {
865 return sizeFixed64ValueNoZero, appendFixed64ValueNoZero
866 }
867 return sizeFixed64Value, appendFixed64Value
868 case "varint":
869 if pointer {
870 return sizeVarint64Ptr, appendVarint64Ptr
871 }
872 if slice {
873 if packed {
874 return sizeVarint64PackedSlice, appendVarint64PackedSlice
875 }
876 return sizeVarint64Slice, appendVarint64Slice
877 }
878 if nozero {
879 return sizeVarint64ValueNoZero, appendVarint64ValueNoZero
880 }
881 return sizeVarint64Value, appendVarint64Value
882 }
883 case reflect.Int64:
884 switch encoding {
885 case "fixed64":
886 if pointer {
887 return sizeFixedS64Ptr, appendFixedS64Ptr
888 }
889 if slice {
890 if packed {
891 return sizeFixedS64PackedSlice, appendFixedS64PackedSlice
892 }
893 return sizeFixedS64Slice, appendFixedS64Slice
894 }
895 if nozero {
896 return sizeFixedS64ValueNoZero, appendFixedS64ValueNoZero
897 }
898 return sizeFixedS64Value, appendFixedS64Value
899 case "varint":
900 if pointer {
901 return sizeVarintS64Ptr, appendVarintS64Ptr
902 }
903 if slice {
904 if packed {
905 return sizeVarintS64PackedSlice, appendVarintS64PackedSlice
906 }
907 return sizeVarintS64Slice, appendVarintS64Slice
908 }
909 if nozero {
910 return sizeVarintS64ValueNoZero, appendVarintS64ValueNoZero
911 }
912 return sizeVarintS64Value, appendVarintS64Value
913 case "zigzag64":
914 if pointer {
915 return sizeZigzag64Ptr, appendZigzag64Ptr
916 }
917 if slice {
918 if packed {
919 return sizeZigzag64PackedSlice, appendZigzag64PackedSlice
920 }
921 return sizeZigzag64Slice, appendZigzag64Slice
922 }
923 if nozero {
924 return sizeZigzag64ValueNoZero, appendZigzag64ValueNoZero
925 }
926 return sizeZigzag64Value, appendZigzag64Value
927 }
928 case reflect.Float32:
929 if pointer {
930 return sizeFloat32Ptr, appendFloat32Ptr
931 }
932 if slice {
933 if packed {
934 return sizeFloat32PackedSlice, appendFloat32PackedSlice
935 }
936 return sizeFloat32Slice, appendFloat32Slice
937 }
938 if nozero {
939 return sizeFloat32ValueNoZero, appendFloat32ValueNoZero
940 }
941 return sizeFloat32Value, appendFloat32Value
942 case reflect.Float64:
943 if pointer {
944 return sizeFloat64Ptr, appendFloat64Ptr
945 }
946 if slice {
947 if packed {
948 return sizeFloat64PackedSlice, appendFloat64PackedSlice
949 }
950 return sizeFloat64Slice, appendFloat64Slice
951 }
952 if nozero {
953 return sizeFloat64ValueNoZero, appendFloat64ValueNoZero
954 }
955 return sizeFloat64Value, appendFloat64Value
956 case reflect.String:
957 if validateUTF8 {
958 if pointer {
959 return sizeStringPtr, appendUTF8StringPtr
960 }
961 if slice {
962 return sizeStringSlice, appendUTF8StringSlice
963 }
964 if nozero {
965 return sizeStringValueNoZero, appendUTF8StringValueNoZero
966 }
967 return sizeStringValue, appendUTF8StringValue
968 }
969 if pointer {
970 return sizeStringPtr, appendStringPtr
971 }
972 if slice {
973 return sizeStringSlice, appendStringSlice
974 }
975 if nozero {
976 return sizeStringValueNoZero, appendStringValueNoZero
977 }
978 return sizeStringValue, appendStringValue
979 case reflect.Slice:
980 if slice {
981 return sizeBytesSlice, appendBytesSlice
982 }
983 if oneof {
984 // Oneof bytes field may also have "proto3" tag.
985 // We want to marshal it as a oneof field. Do this
986 // check before the proto3 check.
987 return sizeBytesOneof, appendBytesOneof
988 }
989 if proto3 {
990 return sizeBytes3, appendBytes3
991 }
992 return sizeBytes, appendBytes
993 case reflect.Struct:
994 switch encoding {
995 case "group":
996 if slice {
997 return makeGroupSliceMarshaler(getMarshalInfo(t))
998 }
999 return makeGroupMarshaler(getMarshalInfo(t))
1000 case "bytes":
1001 if pointer {
1002 if slice {
1003 return makeMessageSliceMarshaler(getMarshalInfo(t))
1004 }
1005 return makeMessageMarshaler(getMarshalInfo(t))
1006 } else {
1007 if slice {
1008 return makeMessageRefSliceMarshaler(getMarshalInfo(t))
1009 }
1010 return makeMessageRefMarshaler(getMarshalInfo(t))
1011 }
1012 }
1013 }
1014 panic(fmt.Sprintf("unknown or mismatched type: type: %v, wire type: %v", t, encoding))
1015}
1016
1017// Below are functions to size/marshal a specific type of a field.
1018// They are stored in the field's info, and called by function pointers.
1019// They have type sizer or marshaler.
1020
1021func sizeFixed32Value(_ pointer, tagsize int) int {
1022 return 4 + tagsize
1023}
1024func sizeFixed32ValueNoZero(ptr pointer, tagsize int) int {
1025 v := *ptr.toUint32()
1026 if v == 0 {
1027 return 0
1028 }
1029 return 4 + tagsize
1030}
1031func sizeFixed32Ptr(ptr pointer, tagsize int) int {
1032 p := *ptr.toUint32Ptr()
1033 if p == nil {
1034 return 0
1035 }
1036 return 4 + tagsize
1037}
1038func sizeFixed32Slice(ptr pointer, tagsize int) int {
1039 s := *ptr.toUint32Slice()
1040 return (4 + tagsize) * len(s)
1041}
1042func sizeFixed32PackedSlice(ptr pointer, tagsize int) int {
1043 s := *ptr.toUint32Slice()
1044 if len(s) == 0 {
1045 return 0
1046 }
1047 return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
1048}
1049func sizeFixedS32Value(_ pointer, tagsize int) int {
1050 return 4 + tagsize
1051}
1052func sizeFixedS32ValueNoZero(ptr pointer, tagsize int) int {
1053 v := *ptr.toInt32()
1054 if v == 0 {
1055 return 0
1056 }
1057 return 4 + tagsize
1058}
1059func sizeFixedS32Ptr(ptr pointer, tagsize int) int {
1060 p := ptr.getInt32Ptr()
1061 if p == nil {
1062 return 0
1063 }
1064 return 4 + tagsize
1065}
1066func sizeFixedS32Slice(ptr pointer, tagsize int) int {
1067 s := ptr.getInt32Slice()
1068 return (4 + tagsize) * len(s)
1069}
1070func sizeFixedS32PackedSlice(ptr pointer, tagsize int) int {
1071 s := ptr.getInt32Slice()
1072 if len(s) == 0 {
1073 return 0
1074 }
1075 return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
1076}
1077func sizeFloat32Value(_ pointer, tagsize int) int {
1078 return 4 + tagsize
1079}
1080func sizeFloat32ValueNoZero(ptr pointer, tagsize int) int {
1081 v := math.Float32bits(*ptr.toFloat32())
1082 if v == 0 {
1083 return 0
1084 }
1085 return 4 + tagsize
1086}
1087func sizeFloat32Ptr(ptr pointer, tagsize int) int {
1088 p := *ptr.toFloat32Ptr()
1089 if p == nil {
1090 return 0
1091 }
1092 return 4 + tagsize
1093}
1094func sizeFloat32Slice(ptr pointer, tagsize int) int {
1095 s := *ptr.toFloat32Slice()
1096 return (4 + tagsize) * len(s)
1097}
1098func sizeFloat32PackedSlice(ptr pointer, tagsize int) int {
1099 s := *ptr.toFloat32Slice()
1100 if len(s) == 0 {
1101 return 0
1102 }
1103 return 4*len(s) + SizeVarint(uint64(4*len(s))) + tagsize
1104}
1105func sizeFixed64Value(_ pointer, tagsize int) int {
1106 return 8 + tagsize
1107}
1108func sizeFixed64ValueNoZero(ptr pointer, tagsize int) int {
1109 v := *ptr.toUint64()
1110 if v == 0 {
1111 return 0
1112 }
1113 return 8 + tagsize
1114}
1115func sizeFixed64Ptr(ptr pointer, tagsize int) int {
1116 p := *ptr.toUint64Ptr()
1117 if p == nil {
1118 return 0
1119 }
1120 return 8 + tagsize
1121}
1122func sizeFixed64Slice(ptr pointer, tagsize int) int {
1123 s := *ptr.toUint64Slice()
1124 return (8 + tagsize) * len(s)
1125}
1126func sizeFixed64PackedSlice(ptr pointer, tagsize int) int {
1127 s := *ptr.toUint64Slice()
1128 if len(s) == 0 {
1129 return 0
1130 }
1131 return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
1132}
1133func sizeFixedS64Value(_ pointer, tagsize int) int {
1134 return 8 + tagsize
1135}
1136func sizeFixedS64ValueNoZero(ptr pointer, tagsize int) int {
1137 v := *ptr.toInt64()
1138 if v == 0 {
1139 return 0
1140 }
1141 return 8 + tagsize
1142}
1143func sizeFixedS64Ptr(ptr pointer, tagsize int) int {
1144 p := *ptr.toInt64Ptr()
1145 if p == nil {
1146 return 0
1147 }
1148 return 8 + tagsize
1149}
1150func sizeFixedS64Slice(ptr pointer, tagsize int) int {
1151 s := *ptr.toInt64Slice()
1152 return (8 + tagsize) * len(s)
1153}
1154func sizeFixedS64PackedSlice(ptr pointer, tagsize int) int {
1155 s := *ptr.toInt64Slice()
1156 if len(s) == 0 {
1157 return 0
1158 }
1159 return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
1160}
1161func sizeFloat64Value(_ pointer, tagsize int) int {
1162 return 8 + tagsize
1163}
1164func sizeFloat64ValueNoZero(ptr pointer, tagsize int) int {
1165 v := math.Float64bits(*ptr.toFloat64())
1166 if v == 0 {
1167 return 0
1168 }
1169 return 8 + tagsize
1170}
1171func sizeFloat64Ptr(ptr pointer, tagsize int) int {
1172 p := *ptr.toFloat64Ptr()
1173 if p == nil {
1174 return 0
1175 }
1176 return 8 + tagsize
1177}
1178func sizeFloat64Slice(ptr pointer, tagsize int) int {
1179 s := *ptr.toFloat64Slice()
1180 return (8 + tagsize) * len(s)
1181}
1182func sizeFloat64PackedSlice(ptr pointer, tagsize int) int {
1183 s := *ptr.toFloat64Slice()
1184 if len(s) == 0 {
1185 return 0
1186 }
1187 return 8*len(s) + SizeVarint(uint64(8*len(s))) + tagsize
1188}
1189func sizeVarint32Value(ptr pointer, tagsize int) int {
1190 v := *ptr.toUint32()
1191 return SizeVarint(uint64(v)) + tagsize
1192}
1193func sizeVarint32ValueNoZero(ptr pointer, tagsize int) int {
1194 v := *ptr.toUint32()
1195 if v == 0 {
1196 return 0
1197 }
1198 return SizeVarint(uint64(v)) + tagsize
1199}
1200func sizeVarint32Ptr(ptr pointer, tagsize int) int {
1201 p := *ptr.toUint32Ptr()
1202 if p == nil {
1203 return 0
1204 }
1205 return SizeVarint(uint64(*p)) + tagsize
1206}
1207func sizeVarint32Slice(ptr pointer, tagsize int) int {
1208 s := *ptr.toUint32Slice()
1209 n := 0
1210 for _, v := range s {
1211 n += SizeVarint(uint64(v)) + tagsize
1212 }
1213 return n
1214}
1215func sizeVarint32PackedSlice(ptr pointer, tagsize int) int {
1216 s := *ptr.toUint32Slice()
1217 if len(s) == 0 {
1218 return 0
1219 }
1220 n := 0
1221 for _, v := range s {
1222 n += SizeVarint(uint64(v))
1223 }
1224 return n + SizeVarint(uint64(n)) + tagsize
1225}
1226func sizeVarintS32Value(ptr pointer, tagsize int) int {
1227 v := *ptr.toInt32()
1228 return SizeVarint(uint64(v)) + tagsize
1229}
1230func sizeVarintS32ValueNoZero(ptr pointer, tagsize int) int {
1231 v := *ptr.toInt32()
1232 if v == 0 {
1233 return 0
1234 }
1235 return SizeVarint(uint64(v)) + tagsize
1236}
1237func sizeVarintS32Ptr(ptr pointer, tagsize int) int {
1238 p := ptr.getInt32Ptr()
1239 if p == nil {
1240 return 0
1241 }
1242 return SizeVarint(uint64(*p)) + tagsize
1243}
1244func sizeVarintS32Slice(ptr pointer, tagsize int) int {
1245 s := ptr.getInt32Slice()
1246 n := 0
1247 for _, v := range s {
1248 n += SizeVarint(uint64(v)) + tagsize
1249 }
1250 return n
1251}
1252func sizeVarintS32PackedSlice(ptr pointer, tagsize int) int {
1253 s := ptr.getInt32Slice()
1254 if len(s) == 0 {
1255 return 0
1256 }
1257 n := 0
1258 for _, v := range s {
1259 n += SizeVarint(uint64(v))
1260 }
1261 return n + SizeVarint(uint64(n)) + tagsize
1262}
1263func sizeVarint64Value(ptr pointer, tagsize int) int {
1264 v := *ptr.toUint64()
1265 return SizeVarint(v) + tagsize
1266}
1267func sizeVarint64ValueNoZero(ptr pointer, tagsize int) int {
1268 v := *ptr.toUint64()
1269 if v == 0 {
1270 return 0
1271 }
1272 return SizeVarint(v) + tagsize
1273}
1274func sizeVarint64Ptr(ptr pointer, tagsize int) int {
1275 p := *ptr.toUint64Ptr()
1276 if p == nil {
1277 return 0
1278 }
1279 return SizeVarint(*p) + tagsize
1280}
1281func sizeVarint64Slice(ptr pointer, tagsize int) int {
1282 s := *ptr.toUint64Slice()
1283 n := 0
1284 for _, v := range s {
1285 n += SizeVarint(v) + tagsize
1286 }
1287 return n
1288}
1289func sizeVarint64PackedSlice(ptr pointer, tagsize int) int {
1290 s := *ptr.toUint64Slice()
1291 if len(s) == 0 {
1292 return 0
1293 }
1294 n := 0
1295 for _, v := range s {
1296 n += SizeVarint(v)
1297 }
1298 return n + SizeVarint(uint64(n)) + tagsize
1299}
1300func sizeVarintS64Value(ptr pointer, tagsize int) int {
1301 v := *ptr.toInt64()
1302 return SizeVarint(uint64(v)) + tagsize
1303}
1304func sizeVarintS64ValueNoZero(ptr pointer, tagsize int) int {
1305 v := *ptr.toInt64()
1306 if v == 0 {
1307 return 0
1308 }
1309 return SizeVarint(uint64(v)) + tagsize
1310}
1311func sizeVarintS64Ptr(ptr pointer, tagsize int) int {
1312 p := *ptr.toInt64Ptr()
1313 if p == nil {
1314 return 0
1315 }
1316 return SizeVarint(uint64(*p)) + tagsize
1317}
1318func sizeVarintS64Slice(ptr pointer, tagsize int) int {
1319 s := *ptr.toInt64Slice()
1320 n := 0
1321 for _, v := range s {
1322 n += SizeVarint(uint64(v)) + tagsize
1323 }
1324 return n
1325}
1326func sizeVarintS64PackedSlice(ptr pointer, tagsize int) int {
1327 s := *ptr.toInt64Slice()
1328 if len(s) == 0 {
1329 return 0
1330 }
1331 n := 0
1332 for _, v := range s {
1333 n += SizeVarint(uint64(v))
1334 }
1335 return n + SizeVarint(uint64(n)) + tagsize
1336}
1337func sizeZigzag32Value(ptr pointer, tagsize int) int {
1338 v := *ptr.toInt32()
1339 return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1340}
1341func sizeZigzag32ValueNoZero(ptr pointer, tagsize int) int {
1342 v := *ptr.toInt32()
1343 if v == 0 {
1344 return 0
1345 }
1346 return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1347}
1348func sizeZigzag32Ptr(ptr pointer, tagsize int) int {
1349 p := ptr.getInt32Ptr()
1350 if p == nil {
1351 return 0
1352 }
1353 v := *p
1354 return SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1355}
1356func sizeZigzag32Slice(ptr pointer, tagsize int) int {
1357 s := ptr.getInt32Slice()
1358 n := 0
1359 for _, v := range s {
1360 n += SizeVarint(uint64((uint32(v)<<1)^uint32((int32(v)>>31)))) + tagsize
1361 }
1362 return n
1363}
1364func sizeZigzag32PackedSlice(ptr pointer, tagsize int) int {
1365 s := ptr.getInt32Slice()
1366 if len(s) == 0 {
1367 return 0
1368 }
1369 n := 0
1370 for _, v := range s {
1371 n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
1372 }
1373 return n + SizeVarint(uint64(n)) + tagsize
1374}
1375func sizeZigzag64Value(ptr pointer, tagsize int) int {
1376 v := *ptr.toInt64()
1377 return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1378}
1379func sizeZigzag64ValueNoZero(ptr pointer, tagsize int) int {
1380 v := *ptr.toInt64()
1381 if v == 0 {
1382 return 0
1383 }
1384 return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1385}
1386func sizeZigzag64Ptr(ptr pointer, tagsize int) int {
1387 p := *ptr.toInt64Ptr()
1388 if p == nil {
1389 return 0
1390 }
1391 v := *p
1392 return SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1393}
1394func sizeZigzag64Slice(ptr pointer, tagsize int) int {
1395 s := *ptr.toInt64Slice()
1396 n := 0
1397 for _, v := range s {
1398 n += SizeVarint(uint64(v<<1)^uint64((int64(v)>>63))) + tagsize
1399 }
1400 return n
1401}
1402func sizeZigzag64PackedSlice(ptr pointer, tagsize int) int {
1403 s := *ptr.toInt64Slice()
1404 if len(s) == 0 {
1405 return 0
1406 }
1407 n := 0
1408 for _, v := range s {
1409 n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
1410 }
1411 return n + SizeVarint(uint64(n)) + tagsize
1412}
1413func sizeBoolValue(_ pointer, tagsize int) int {
1414 return 1 + tagsize
1415}
1416func sizeBoolValueNoZero(ptr pointer, tagsize int) int {
1417 v := *ptr.toBool()
1418 if !v {
1419 return 0
1420 }
1421 return 1 + tagsize
1422}
1423func sizeBoolPtr(ptr pointer, tagsize int) int {
1424 p := *ptr.toBoolPtr()
1425 if p == nil {
1426 return 0
1427 }
1428 return 1 + tagsize
1429}
1430func sizeBoolSlice(ptr pointer, tagsize int) int {
1431 s := *ptr.toBoolSlice()
1432 return (1 + tagsize) * len(s)
1433}
1434func sizeBoolPackedSlice(ptr pointer, tagsize int) int {
1435 s := *ptr.toBoolSlice()
1436 if len(s) == 0 {
1437 return 0
1438 }
1439 return len(s) + SizeVarint(uint64(len(s))) + tagsize
1440}
1441func sizeStringValue(ptr pointer, tagsize int) int {
1442 v := *ptr.toString()
1443 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1444}
1445func sizeStringValueNoZero(ptr pointer, tagsize int) int {
1446 v := *ptr.toString()
1447 if v == "" {
1448 return 0
1449 }
1450 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1451}
1452func sizeStringPtr(ptr pointer, tagsize int) int {
1453 p := *ptr.toStringPtr()
1454 if p == nil {
1455 return 0
1456 }
1457 v := *p
1458 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1459}
1460func sizeStringSlice(ptr pointer, tagsize int) int {
1461 s := *ptr.toStringSlice()
1462 n := 0
1463 for _, v := range s {
1464 n += len(v) + SizeVarint(uint64(len(v))) + tagsize
1465 }
1466 return n
1467}
1468func sizeBytes(ptr pointer, tagsize int) int {
1469 v := *ptr.toBytes()
1470 if v == nil {
1471 return 0
1472 }
1473 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1474}
1475func sizeBytes3(ptr pointer, tagsize int) int {
1476 v := *ptr.toBytes()
1477 if len(v) == 0 {
1478 return 0
1479 }
1480 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1481}
1482func sizeBytesOneof(ptr pointer, tagsize int) int {
1483 v := *ptr.toBytes()
1484 return len(v) + SizeVarint(uint64(len(v))) + tagsize
1485}
1486func sizeBytesSlice(ptr pointer, tagsize int) int {
1487 s := *ptr.toBytesSlice()
1488 n := 0
1489 for _, v := range s {
1490 n += len(v) + SizeVarint(uint64(len(v))) + tagsize
1491 }
1492 return n
1493}
1494
1495// appendFixed32 appends an encoded fixed32 to b.
1496func appendFixed32(b []byte, v uint32) []byte {
1497 b = append(b,
1498 byte(v),
1499 byte(v>>8),
1500 byte(v>>16),
1501 byte(v>>24))
1502 return b
1503}
1504
1505// appendFixed64 appends an encoded fixed64 to b.
1506func appendFixed64(b []byte, v uint64) []byte {
1507 b = append(b,
1508 byte(v),
1509 byte(v>>8),
1510 byte(v>>16),
1511 byte(v>>24),
1512 byte(v>>32),
1513 byte(v>>40),
1514 byte(v>>48),
1515 byte(v>>56))
1516 return b
1517}
1518
1519// appendVarint appends an encoded varint to b.
1520func appendVarint(b []byte, v uint64) []byte {
1521 // TODO: make 1-byte (maybe 2-byte) case inline-able, once we
1522 // have non-leaf inliner.
1523 switch {
1524 case v < 1<<7:
1525 b = append(b, byte(v))
1526 case v < 1<<14:
1527 b = append(b,
1528 byte(v&0x7f|0x80),
1529 byte(v>>7))
1530 case v < 1<<21:
1531 b = append(b,
1532 byte(v&0x7f|0x80),
1533 byte((v>>7)&0x7f|0x80),
1534 byte(v>>14))
1535 case v < 1<<28:
1536 b = append(b,
1537 byte(v&0x7f|0x80),
1538 byte((v>>7)&0x7f|0x80),
1539 byte((v>>14)&0x7f|0x80),
1540 byte(v>>21))
1541 case v < 1<<35:
1542 b = append(b,
1543 byte(v&0x7f|0x80),
1544 byte((v>>7)&0x7f|0x80),
1545 byte((v>>14)&0x7f|0x80),
1546 byte((v>>21)&0x7f|0x80),
1547 byte(v>>28))
1548 case v < 1<<42:
1549 b = append(b,
1550 byte(v&0x7f|0x80),
1551 byte((v>>7)&0x7f|0x80),
1552 byte((v>>14)&0x7f|0x80),
1553 byte((v>>21)&0x7f|0x80),
1554 byte((v>>28)&0x7f|0x80),
1555 byte(v>>35))
1556 case v < 1<<49:
1557 b = append(b,
1558 byte(v&0x7f|0x80),
1559 byte((v>>7)&0x7f|0x80),
1560 byte((v>>14)&0x7f|0x80),
1561 byte((v>>21)&0x7f|0x80),
1562 byte((v>>28)&0x7f|0x80),
1563 byte((v>>35)&0x7f|0x80),
1564 byte(v>>42))
1565 case v < 1<<56:
1566 b = append(b,
1567 byte(v&0x7f|0x80),
1568 byte((v>>7)&0x7f|0x80),
1569 byte((v>>14)&0x7f|0x80),
1570 byte((v>>21)&0x7f|0x80),
1571 byte((v>>28)&0x7f|0x80),
1572 byte((v>>35)&0x7f|0x80),
1573 byte((v>>42)&0x7f|0x80),
1574 byte(v>>49))
1575 case v < 1<<63:
1576 b = append(b,
1577 byte(v&0x7f|0x80),
1578 byte((v>>7)&0x7f|0x80),
1579 byte((v>>14)&0x7f|0x80),
1580 byte((v>>21)&0x7f|0x80),
1581 byte((v>>28)&0x7f|0x80),
1582 byte((v>>35)&0x7f|0x80),
1583 byte((v>>42)&0x7f|0x80),
1584 byte((v>>49)&0x7f|0x80),
1585 byte(v>>56))
1586 default:
1587 b = append(b,
1588 byte(v&0x7f|0x80),
1589 byte((v>>7)&0x7f|0x80),
1590 byte((v>>14)&0x7f|0x80),
1591 byte((v>>21)&0x7f|0x80),
1592 byte((v>>28)&0x7f|0x80),
1593 byte((v>>35)&0x7f|0x80),
1594 byte((v>>42)&0x7f|0x80),
1595 byte((v>>49)&0x7f|0x80),
1596 byte((v>>56)&0x7f|0x80),
1597 1)
1598 }
1599 return b
1600}
1601
1602func appendFixed32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1603 v := *ptr.toUint32()
1604 b = appendVarint(b, wiretag)
1605 b = appendFixed32(b, v)
1606 return b, nil
1607}
1608func appendFixed32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1609 v := *ptr.toUint32()
1610 if v == 0 {
1611 return b, nil
1612 }
1613 b = appendVarint(b, wiretag)
1614 b = appendFixed32(b, v)
1615 return b, nil
1616}
1617func appendFixed32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1618 p := *ptr.toUint32Ptr()
1619 if p == nil {
1620 return b, nil
1621 }
1622 b = appendVarint(b, wiretag)
1623 b = appendFixed32(b, *p)
1624 return b, nil
1625}
1626func appendFixed32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1627 s := *ptr.toUint32Slice()
1628 for _, v := range s {
1629 b = appendVarint(b, wiretag)
1630 b = appendFixed32(b, v)
1631 }
1632 return b, nil
1633}
1634func appendFixed32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1635 s := *ptr.toUint32Slice()
1636 if len(s) == 0 {
1637 return b, nil
1638 }
1639 b = appendVarint(b, wiretag&^7|WireBytes)
1640 b = appendVarint(b, uint64(4*len(s)))
1641 for _, v := range s {
1642 b = appendFixed32(b, v)
1643 }
1644 return b, nil
1645}
1646func appendFixedS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1647 v := *ptr.toInt32()
1648 b = appendVarint(b, wiretag)
1649 b = appendFixed32(b, uint32(v))
1650 return b, nil
1651}
1652func appendFixedS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1653 v := *ptr.toInt32()
1654 if v == 0 {
1655 return b, nil
1656 }
1657 b = appendVarint(b, wiretag)
1658 b = appendFixed32(b, uint32(v))
1659 return b, nil
1660}
1661func appendFixedS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1662 p := ptr.getInt32Ptr()
1663 if p == nil {
1664 return b, nil
1665 }
1666 b = appendVarint(b, wiretag)
1667 b = appendFixed32(b, uint32(*p))
1668 return b, nil
1669}
1670func appendFixedS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1671 s := ptr.getInt32Slice()
1672 for _, v := range s {
1673 b = appendVarint(b, wiretag)
1674 b = appendFixed32(b, uint32(v))
1675 }
1676 return b, nil
1677}
1678func appendFixedS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1679 s := ptr.getInt32Slice()
1680 if len(s) == 0 {
1681 return b, nil
1682 }
1683 b = appendVarint(b, wiretag&^7|WireBytes)
1684 b = appendVarint(b, uint64(4*len(s)))
1685 for _, v := range s {
1686 b = appendFixed32(b, uint32(v))
1687 }
1688 return b, nil
1689}
1690func appendFloat32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1691 v := math.Float32bits(*ptr.toFloat32())
1692 b = appendVarint(b, wiretag)
1693 b = appendFixed32(b, v)
1694 return b, nil
1695}
1696func appendFloat32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1697 v := math.Float32bits(*ptr.toFloat32())
1698 if v == 0 {
1699 return b, nil
1700 }
1701 b = appendVarint(b, wiretag)
1702 b = appendFixed32(b, v)
1703 return b, nil
1704}
1705func appendFloat32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1706 p := *ptr.toFloat32Ptr()
1707 if p == nil {
1708 return b, nil
1709 }
1710 b = appendVarint(b, wiretag)
1711 b = appendFixed32(b, math.Float32bits(*p))
1712 return b, nil
1713}
1714func appendFloat32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1715 s := *ptr.toFloat32Slice()
1716 for _, v := range s {
1717 b = appendVarint(b, wiretag)
1718 b = appendFixed32(b, math.Float32bits(v))
1719 }
1720 return b, nil
1721}
1722func appendFloat32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1723 s := *ptr.toFloat32Slice()
1724 if len(s) == 0 {
1725 return b, nil
1726 }
1727 b = appendVarint(b, wiretag&^7|WireBytes)
1728 b = appendVarint(b, uint64(4*len(s)))
1729 for _, v := range s {
1730 b = appendFixed32(b, math.Float32bits(v))
1731 }
1732 return b, nil
1733}
1734func appendFixed64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1735 v := *ptr.toUint64()
1736 b = appendVarint(b, wiretag)
1737 b = appendFixed64(b, v)
1738 return b, nil
1739}
1740func appendFixed64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1741 v := *ptr.toUint64()
1742 if v == 0 {
1743 return b, nil
1744 }
1745 b = appendVarint(b, wiretag)
1746 b = appendFixed64(b, v)
1747 return b, nil
1748}
1749func appendFixed64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1750 p := *ptr.toUint64Ptr()
1751 if p == nil {
1752 return b, nil
1753 }
1754 b = appendVarint(b, wiretag)
1755 b = appendFixed64(b, *p)
1756 return b, nil
1757}
1758func appendFixed64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1759 s := *ptr.toUint64Slice()
1760 for _, v := range s {
1761 b = appendVarint(b, wiretag)
1762 b = appendFixed64(b, v)
1763 }
1764 return b, nil
1765}
1766func appendFixed64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1767 s := *ptr.toUint64Slice()
1768 if len(s) == 0 {
1769 return b, nil
1770 }
1771 b = appendVarint(b, wiretag&^7|WireBytes)
1772 b = appendVarint(b, uint64(8*len(s)))
1773 for _, v := range s {
1774 b = appendFixed64(b, v)
1775 }
1776 return b, nil
1777}
1778func appendFixedS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1779 v := *ptr.toInt64()
1780 b = appendVarint(b, wiretag)
1781 b = appendFixed64(b, uint64(v))
1782 return b, nil
1783}
1784func appendFixedS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1785 v := *ptr.toInt64()
1786 if v == 0 {
1787 return b, nil
1788 }
1789 b = appendVarint(b, wiretag)
1790 b = appendFixed64(b, uint64(v))
1791 return b, nil
1792}
1793func appendFixedS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1794 p := *ptr.toInt64Ptr()
1795 if p == nil {
1796 return b, nil
1797 }
1798 b = appendVarint(b, wiretag)
1799 b = appendFixed64(b, uint64(*p))
1800 return b, nil
1801}
1802func appendFixedS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1803 s := *ptr.toInt64Slice()
1804 for _, v := range s {
1805 b = appendVarint(b, wiretag)
1806 b = appendFixed64(b, uint64(v))
1807 }
1808 return b, nil
1809}
1810func appendFixedS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1811 s := *ptr.toInt64Slice()
1812 if len(s) == 0 {
1813 return b, nil
1814 }
1815 b = appendVarint(b, wiretag&^7|WireBytes)
1816 b = appendVarint(b, uint64(8*len(s)))
1817 for _, v := range s {
1818 b = appendFixed64(b, uint64(v))
1819 }
1820 return b, nil
1821}
1822func appendFloat64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1823 v := math.Float64bits(*ptr.toFloat64())
1824 b = appendVarint(b, wiretag)
1825 b = appendFixed64(b, v)
1826 return b, nil
1827}
1828func appendFloat64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1829 v := math.Float64bits(*ptr.toFloat64())
1830 if v == 0 {
1831 return b, nil
1832 }
1833 b = appendVarint(b, wiretag)
1834 b = appendFixed64(b, v)
1835 return b, nil
1836}
1837func appendFloat64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1838 p := *ptr.toFloat64Ptr()
1839 if p == nil {
1840 return b, nil
1841 }
1842 b = appendVarint(b, wiretag)
1843 b = appendFixed64(b, math.Float64bits(*p))
1844 return b, nil
1845}
1846func appendFloat64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1847 s := *ptr.toFloat64Slice()
1848 for _, v := range s {
1849 b = appendVarint(b, wiretag)
1850 b = appendFixed64(b, math.Float64bits(v))
1851 }
1852 return b, nil
1853}
1854func appendFloat64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1855 s := *ptr.toFloat64Slice()
1856 if len(s) == 0 {
1857 return b, nil
1858 }
1859 b = appendVarint(b, wiretag&^7|WireBytes)
1860 b = appendVarint(b, uint64(8*len(s)))
1861 for _, v := range s {
1862 b = appendFixed64(b, math.Float64bits(v))
1863 }
1864 return b, nil
1865}
1866func appendVarint32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1867 v := *ptr.toUint32()
1868 b = appendVarint(b, wiretag)
1869 b = appendVarint(b, uint64(v))
1870 return b, nil
1871}
1872func appendVarint32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1873 v := *ptr.toUint32()
1874 if v == 0 {
1875 return b, nil
1876 }
1877 b = appendVarint(b, wiretag)
1878 b = appendVarint(b, uint64(v))
1879 return b, nil
1880}
1881func appendVarint32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1882 p := *ptr.toUint32Ptr()
1883 if p == nil {
1884 return b, nil
1885 }
1886 b = appendVarint(b, wiretag)
1887 b = appendVarint(b, uint64(*p))
1888 return b, nil
1889}
1890func appendVarint32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1891 s := *ptr.toUint32Slice()
1892 for _, v := range s {
1893 b = appendVarint(b, wiretag)
1894 b = appendVarint(b, uint64(v))
1895 }
1896 return b, nil
1897}
1898func appendVarint32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1899 s := *ptr.toUint32Slice()
1900 if len(s) == 0 {
1901 return b, nil
1902 }
1903 b = appendVarint(b, wiretag&^7|WireBytes)
1904 // compute size
1905 n := 0
1906 for _, v := range s {
1907 n += SizeVarint(uint64(v))
1908 }
1909 b = appendVarint(b, uint64(n))
1910 for _, v := range s {
1911 b = appendVarint(b, uint64(v))
1912 }
1913 return b, nil
1914}
1915func appendVarintS32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1916 v := *ptr.toInt32()
1917 b = appendVarint(b, wiretag)
1918 b = appendVarint(b, uint64(v))
1919 return b, nil
1920}
1921func appendVarintS32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1922 v := *ptr.toInt32()
1923 if v == 0 {
1924 return b, nil
1925 }
1926 b = appendVarint(b, wiretag)
1927 b = appendVarint(b, uint64(v))
1928 return b, nil
1929}
1930func appendVarintS32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1931 p := ptr.getInt32Ptr()
1932 if p == nil {
1933 return b, nil
1934 }
1935 b = appendVarint(b, wiretag)
1936 b = appendVarint(b, uint64(*p))
1937 return b, nil
1938}
1939func appendVarintS32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1940 s := ptr.getInt32Slice()
1941 for _, v := range s {
1942 b = appendVarint(b, wiretag)
1943 b = appendVarint(b, uint64(v))
1944 }
1945 return b, nil
1946}
1947func appendVarintS32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1948 s := ptr.getInt32Slice()
1949 if len(s) == 0 {
1950 return b, nil
1951 }
1952 b = appendVarint(b, wiretag&^7|WireBytes)
1953 // compute size
1954 n := 0
1955 for _, v := range s {
1956 n += SizeVarint(uint64(v))
1957 }
1958 b = appendVarint(b, uint64(n))
1959 for _, v := range s {
1960 b = appendVarint(b, uint64(v))
1961 }
1962 return b, nil
1963}
1964func appendVarint64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1965 v := *ptr.toUint64()
1966 b = appendVarint(b, wiretag)
1967 b = appendVarint(b, v)
1968 return b, nil
1969}
1970func appendVarint64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1971 v := *ptr.toUint64()
1972 if v == 0 {
1973 return b, nil
1974 }
1975 b = appendVarint(b, wiretag)
1976 b = appendVarint(b, v)
1977 return b, nil
1978}
1979func appendVarint64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1980 p := *ptr.toUint64Ptr()
1981 if p == nil {
1982 return b, nil
1983 }
1984 b = appendVarint(b, wiretag)
1985 b = appendVarint(b, *p)
1986 return b, nil
1987}
1988func appendVarint64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1989 s := *ptr.toUint64Slice()
1990 for _, v := range s {
1991 b = appendVarint(b, wiretag)
1992 b = appendVarint(b, v)
1993 }
1994 return b, nil
1995}
1996func appendVarint64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
1997 s := *ptr.toUint64Slice()
1998 if len(s) == 0 {
1999 return b, nil
2000 }
2001 b = appendVarint(b, wiretag&^7|WireBytes)
2002 // compute size
2003 n := 0
2004 for _, v := range s {
2005 n += SizeVarint(v)
2006 }
2007 b = appendVarint(b, uint64(n))
2008 for _, v := range s {
2009 b = appendVarint(b, v)
2010 }
2011 return b, nil
2012}
2013func appendVarintS64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2014 v := *ptr.toInt64()
2015 b = appendVarint(b, wiretag)
2016 b = appendVarint(b, uint64(v))
2017 return b, nil
2018}
2019func appendVarintS64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2020 v := *ptr.toInt64()
2021 if v == 0 {
2022 return b, nil
2023 }
2024 b = appendVarint(b, wiretag)
2025 b = appendVarint(b, uint64(v))
2026 return b, nil
2027}
2028func appendVarintS64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2029 p := *ptr.toInt64Ptr()
2030 if p == nil {
2031 return b, nil
2032 }
2033 b = appendVarint(b, wiretag)
2034 b = appendVarint(b, uint64(*p))
2035 return b, nil
2036}
2037func appendVarintS64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2038 s := *ptr.toInt64Slice()
2039 for _, v := range s {
2040 b = appendVarint(b, wiretag)
2041 b = appendVarint(b, uint64(v))
2042 }
2043 return b, nil
2044}
2045func appendVarintS64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2046 s := *ptr.toInt64Slice()
2047 if len(s) == 0 {
2048 return b, nil
2049 }
2050 b = appendVarint(b, wiretag&^7|WireBytes)
2051 // compute size
2052 n := 0
2053 for _, v := range s {
2054 n += SizeVarint(uint64(v))
2055 }
2056 b = appendVarint(b, uint64(n))
2057 for _, v := range s {
2058 b = appendVarint(b, uint64(v))
2059 }
2060 return b, nil
2061}
2062func appendZigzag32Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2063 v := *ptr.toInt32()
2064 b = appendVarint(b, wiretag)
2065 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
2066 return b, nil
2067}
2068func appendZigzag32ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2069 v := *ptr.toInt32()
2070 if v == 0 {
2071 return b, nil
2072 }
2073 b = appendVarint(b, wiretag)
2074 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
2075 return b, nil
2076}
2077func appendZigzag32Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2078 p := ptr.getInt32Ptr()
2079 if p == nil {
2080 return b, nil
2081 }
2082 b = appendVarint(b, wiretag)
2083 v := *p
2084 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
2085 return b, nil
2086}
2087func appendZigzag32Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2088 s := ptr.getInt32Slice()
2089 for _, v := range s {
2090 b = appendVarint(b, wiretag)
2091 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
2092 }
2093 return b, nil
2094}
2095func appendZigzag32PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2096 s := ptr.getInt32Slice()
2097 if len(s) == 0 {
2098 return b, nil
2099 }
2100 b = appendVarint(b, wiretag&^7|WireBytes)
2101 // compute size
2102 n := 0
2103 for _, v := range s {
2104 n += SizeVarint(uint64((uint32(v) << 1) ^ uint32((int32(v) >> 31))))
2105 }
2106 b = appendVarint(b, uint64(n))
2107 for _, v := range s {
2108 b = appendVarint(b, uint64((uint32(v)<<1)^uint32((int32(v)>>31))))
2109 }
2110 return b, nil
2111}
2112func appendZigzag64Value(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2113 v := *ptr.toInt64()
2114 b = appendVarint(b, wiretag)
2115 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
2116 return b, nil
2117}
2118func appendZigzag64ValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2119 v := *ptr.toInt64()
2120 if v == 0 {
2121 return b, nil
2122 }
2123 b = appendVarint(b, wiretag)
2124 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
2125 return b, nil
2126}
2127func appendZigzag64Ptr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2128 p := *ptr.toInt64Ptr()
2129 if p == nil {
2130 return b, nil
2131 }
2132 b = appendVarint(b, wiretag)
2133 v := *p
2134 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
2135 return b, nil
2136}
2137func appendZigzag64Slice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2138 s := *ptr.toInt64Slice()
2139 for _, v := range s {
2140 b = appendVarint(b, wiretag)
2141 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
2142 }
2143 return b, nil
2144}
2145func appendZigzag64PackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2146 s := *ptr.toInt64Slice()
2147 if len(s) == 0 {
2148 return b, nil
2149 }
2150 b = appendVarint(b, wiretag&^7|WireBytes)
2151 // compute size
2152 n := 0
2153 for _, v := range s {
2154 n += SizeVarint(uint64(v<<1) ^ uint64((int64(v) >> 63)))
2155 }
2156 b = appendVarint(b, uint64(n))
2157 for _, v := range s {
2158 b = appendVarint(b, uint64(v<<1)^uint64((int64(v)>>63)))
2159 }
2160 return b, nil
2161}
2162func appendBoolValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2163 v := *ptr.toBool()
2164 b = appendVarint(b, wiretag)
2165 if v {
2166 b = append(b, 1)
2167 } else {
2168 b = append(b, 0)
2169 }
2170 return b, nil
2171}
2172func appendBoolValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2173 v := *ptr.toBool()
2174 if !v {
2175 return b, nil
2176 }
2177 b = appendVarint(b, wiretag)
2178 b = append(b, 1)
2179 return b, nil
2180}
2181
2182func appendBoolPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2183 p := *ptr.toBoolPtr()
2184 if p == nil {
2185 return b, nil
2186 }
2187 b = appendVarint(b, wiretag)
2188 if *p {
2189 b = append(b, 1)
2190 } else {
2191 b = append(b, 0)
2192 }
2193 return b, nil
2194}
2195func appendBoolSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2196 s := *ptr.toBoolSlice()
2197 for _, v := range s {
2198 b = appendVarint(b, wiretag)
2199 if v {
2200 b = append(b, 1)
2201 } else {
2202 b = append(b, 0)
2203 }
2204 }
2205 return b, nil
2206}
2207func appendBoolPackedSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2208 s := *ptr.toBoolSlice()
2209 if len(s) == 0 {
2210 return b, nil
2211 }
2212 b = appendVarint(b, wiretag&^7|WireBytes)
2213 b = appendVarint(b, uint64(len(s)))
2214 for _, v := range s {
2215 if v {
2216 b = append(b, 1)
2217 } else {
2218 b = append(b, 0)
2219 }
2220 }
2221 return b, nil
2222}
2223func appendStringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2224 v := *ptr.toString()
2225 b = appendVarint(b, wiretag)
2226 b = appendVarint(b, uint64(len(v)))
2227 b = append(b, v...)
2228 return b, nil
2229}
2230func appendStringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2231 v := *ptr.toString()
2232 if v == "" {
2233 return b, nil
2234 }
2235 b = appendVarint(b, wiretag)
2236 b = appendVarint(b, uint64(len(v)))
2237 b = append(b, v...)
2238 return b, nil
2239}
2240func appendStringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2241 p := *ptr.toStringPtr()
2242 if p == nil {
2243 return b, nil
2244 }
2245 v := *p
2246 b = appendVarint(b, wiretag)
2247 b = appendVarint(b, uint64(len(v)))
2248 b = append(b, v...)
2249 return b, nil
2250}
2251func appendStringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2252 s := *ptr.toStringSlice()
2253 for _, v := range s {
2254 b = appendVarint(b, wiretag)
2255 b = appendVarint(b, uint64(len(v)))
2256 b = append(b, v...)
2257 }
2258 return b, nil
2259}
2260func appendUTF8StringValue(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2261 var invalidUTF8 bool
2262 v := *ptr.toString()
2263 if !utf8.ValidString(v) {
2264 invalidUTF8 = true
2265 }
2266 b = appendVarint(b, wiretag)
2267 b = appendVarint(b, uint64(len(v)))
2268 b = append(b, v...)
2269 if invalidUTF8 {
2270 return b, errInvalidUTF8
2271 }
2272 return b, nil
2273}
2274func appendUTF8StringValueNoZero(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2275 var invalidUTF8 bool
2276 v := *ptr.toString()
2277 if v == "" {
2278 return b, nil
2279 }
2280 if !utf8.ValidString(v) {
2281 invalidUTF8 = true
2282 }
2283 b = appendVarint(b, wiretag)
2284 b = appendVarint(b, uint64(len(v)))
2285 b = append(b, v...)
2286 if invalidUTF8 {
2287 return b, errInvalidUTF8
2288 }
2289 return b, nil
2290}
2291func appendUTF8StringPtr(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2292 var invalidUTF8 bool
2293 p := *ptr.toStringPtr()
2294 if p == nil {
2295 return b, nil
2296 }
2297 v := *p
2298 if !utf8.ValidString(v) {
2299 invalidUTF8 = true
2300 }
2301 b = appendVarint(b, wiretag)
2302 b = appendVarint(b, uint64(len(v)))
2303 b = append(b, v...)
2304 if invalidUTF8 {
2305 return b, errInvalidUTF8
2306 }
2307 return b, nil
2308}
2309func appendUTF8StringSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2310 var invalidUTF8 bool
2311 s := *ptr.toStringSlice()
2312 for _, v := range s {
2313 if !utf8.ValidString(v) {
2314 invalidUTF8 = true
2315 }
2316 b = appendVarint(b, wiretag)
2317 b = appendVarint(b, uint64(len(v)))
2318 b = append(b, v...)
2319 }
2320 if invalidUTF8 {
2321 return b, errInvalidUTF8
2322 }
2323 return b, nil
2324}
2325func appendBytes(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2326 v := *ptr.toBytes()
2327 if v == nil {
2328 return b, nil
2329 }
2330 b = appendVarint(b, wiretag)
2331 b = appendVarint(b, uint64(len(v)))
2332 b = append(b, v...)
2333 return b, nil
2334}
2335func appendBytes3(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2336 v := *ptr.toBytes()
2337 if len(v) == 0 {
2338 return b, nil
2339 }
2340 b = appendVarint(b, wiretag)
2341 b = appendVarint(b, uint64(len(v)))
2342 b = append(b, v...)
2343 return b, nil
2344}
2345func appendBytesOneof(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2346 v := *ptr.toBytes()
2347 b = appendVarint(b, wiretag)
2348 b = appendVarint(b, uint64(len(v)))
2349 b = append(b, v...)
2350 return b, nil
2351}
2352func appendBytesSlice(b []byte, ptr pointer, wiretag uint64, _ bool) ([]byte, error) {
2353 s := *ptr.toBytesSlice()
2354 for _, v := range s {
2355 b = appendVarint(b, wiretag)
2356 b = appendVarint(b, uint64(len(v)))
2357 b = append(b, v...)
2358 }
2359 return b, nil
2360}
2361
2362// makeGroupMarshaler returns the sizer and marshaler for a group.
2363// u is the marshal info of the underlying message.
2364func makeGroupMarshaler(u *marshalInfo) (sizer, marshaler) {
2365 return func(ptr pointer, tagsize int) int {
2366 p := ptr.getPointer()
2367 if p.isNil() {
2368 return 0
2369 }
2370 return u.size(p) + 2*tagsize
2371 },
2372 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2373 p := ptr.getPointer()
2374 if p.isNil() {
2375 return b, nil
2376 }
2377 var err error
2378 b = appendVarint(b, wiretag) // start group
2379 b, err = u.marshal(b, p, deterministic)
2380 b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
2381 return b, err
2382 }
2383}
2384
2385// makeGroupSliceMarshaler returns the sizer and marshaler for a group slice.
2386// u is the marshal info of the underlying message.
2387func makeGroupSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
2388 return func(ptr pointer, tagsize int) int {
2389 s := ptr.getPointerSlice()
2390 n := 0
2391 for _, v := range s {
2392 if v.isNil() {
2393 continue
2394 }
2395 n += u.size(v) + 2*tagsize
2396 }
2397 return n
2398 },
2399 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2400 s := ptr.getPointerSlice()
2401 var err error
2402 var nerr nonFatal
2403 for _, v := range s {
2404 if v.isNil() {
2405 return b, errRepeatedHasNil
2406 }
2407 b = appendVarint(b, wiretag) // start group
2408 b, err = u.marshal(b, v, deterministic)
2409 b = appendVarint(b, wiretag+(WireEndGroup-WireStartGroup)) // end group
2410 if !nerr.Merge(err) {
2411 if err == ErrNil {
2412 err = errRepeatedHasNil
2413 }
2414 return b, err
2415 }
2416 }
2417 return b, nerr.E
2418 }
2419}
2420
2421// makeMessageMarshaler returns the sizer and marshaler for a message field.
2422// u is the marshal info of the message.
2423func makeMessageMarshaler(u *marshalInfo) (sizer, marshaler) {
2424 return func(ptr pointer, tagsize int) int {
2425 p := ptr.getPointer()
2426 if p.isNil() {
2427 return 0
2428 }
2429 siz := u.size(p)
2430 return siz + SizeVarint(uint64(siz)) + tagsize
2431 },
2432 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2433 p := ptr.getPointer()
2434 if p.isNil() {
2435 return b, nil
2436 }
2437 b = appendVarint(b, wiretag)
2438 siz := u.cachedsize(p)
2439 b = appendVarint(b, uint64(siz))
2440 return u.marshal(b, p, deterministic)
2441 }
2442}
2443
2444// makeMessageSliceMarshaler returns the sizer and marshaler for a message slice.
2445// u is the marshal info of the message.
2446func makeMessageSliceMarshaler(u *marshalInfo) (sizer, marshaler) {
2447 return func(ptr pointer, tagsize int) int {
2448 s := ptr.getPointerSlice()
2449 n := 0
2450 for _, v := range s {
2451 if v.isNil() {
2452 continue
2453 }
2454 siz := u.size(v)
2455 n += siz + SizeVarint(uint64(siz)) + tagsize
2456 }
2457 return n
2458 },
2459 func(b []byte, ptr pointer, wiretag uint64, deterministic bool) ([]byte, error) {
2460 s := ptr.getPointerSlice()
2461 var err error
2462 var nerr nonFatal
2463 for _, v := range s {
2464 if v.isNil() {
2465 return b, errRepeatedHasNil
2466 }
2467 b = appendVarint(b, wiretag)
2468 siz := u.cachedsize(v)
2469 b = appendVarint(b, uint64(siz))
2470 b, err = u.marshal(b, v, deterministic)
2471
2472 if !nerr.Merge(err) {
2473 if err == ErrNil {
2474 err = errRepeatedHasNil
2475 }
2476 return b, err
2477 }
2478 }
2479 return b, nerr.E
2480 }
2481}
2482
2483// makeMapMarshaler returns the sizer and marshaler for a map field.
2484// f is the pointer to the reflect data structure of the field.
2485func makeMapMarshaler(f *reflect.StructField) (sizer, marshaler) {
2486 // figure out key and value type
2487 t := f.Type
2488 keyType := t.Key()
2489 valType := t.Elem()
2490 tags := strings.Split(f.Tag.Get("protobuf"), ",")
2491 keyTags := strings.Split(f.Tag.Get("protobuf_key"), ",")
2492 valTags := strings.Split(f.Tag.Get("protobuf_val"), ",")
2493 stdOptions := false
2494 for _, t := range tags {
2495 if strings.HasPrefix(t, "customtype=") {
2496 valTags = append(valTags, t)
2497 }
2498 if t == "stdtime" {
2499 valTags = append(valTags, t)
2500 stdOptions = true
2501 }
2502 if t == "stdduration" {
2503 valTags = append(valTags, t)
2504 stdOptions = true
2505 }
2506 if t == "wktptr" {
2507 valTags = append(valTags, t)
2508 }
2509 }
2510 keySizer, keyMarshaler := typeMarshaler(keyType, keyTags, false, false) // don't omit zero value in map
2511 valSizer, valMarshaler := typeMarshaler(valType, valTags, false, false) // don't omit zero value in map
2512 keyWireTag := 1<<3 | wiretype(keyTags[0])
2513 valWireTag := 2<<3 | wiretype(valTags[0])
2514
2515 // We create an interface to get the addresses of the map key and value.
2516 // If value is pointer-typed, the interface is a direct interface, the
2517 // idata itself is the value. Otherwise, the idata is the pointer to the
2518 // value.
2519 // Key cannot be pointer-typed.
2520 valIsPtr := valType.Kind() == reflect.Ptr
2521
2522 // If value is a message with nested maps, calling
2523 // valSizer in marshal may be quadratic. We should use
2524 // cached version in marshal (but not in size).
2525 // If value is not message type, we don't have size cache,
2526 // but it cannot be nested either. Just use valSizer.
2527 valCachedSizer := valSizer
2528 if valIsPtr && !stdOptions && valType.Elem().Kind() == reflect.Struct {
2529 u := getMarshalInfo(valType.Elem())
2530 valCachedSizer = func(ptr pointer, tagsize int) int {
2531 // Same as message sizer, but use cache.
2532 p := ptr.getPointer()
2533 if p.isNil() {
2534 return 0
2535 }
2536 siz := u.cachedsize(p)
2537 return siz + SizeVarint(uint64(siz)) + tagsize
2538 }
2539 }
2540 return func(ptr pointer, tagsize int) int {
2541 m := ptr.asPointerTo(t).Elem() // the map
2542 n := 0
2543 for _, k := range m.MapKeys() {
2544 ki := k.Interface()
2545 vi := m.MapIndex(k).Interface()
2546 kaddr := toAddrPointer(&ki, false) // pointer to key
2547 vaddr := toAddrPointer(&vi, valIsPtr) // pointer to value
2548 siz := keySizer(kaddr, 1) + valSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
2549 n += siz + SizeVarint(uint64(siz)) + tagsize
2550 }
2551 return n
2552 },
2553 func(b []byte, ptr pointer, tag uint64, deterministic bool) ([]byte, error) {
2554 m := ptr.asPointerTo(t).Elem() // the map
2555 var err error
2556 keys := m.MapKeys()
2557 if len(keys) > 1 && deterministic {
2558 sort.Sort(mapKeys(keys))
2559 }
2560
2561 var nerr nonFatal
2562 for _, k := range keys {
2563 ki := k.Interface()
2564 vi := m.MapIndex(k).Interface()
2565 kaddr := toAddrPointer(&ki, false) // pointer to key
2566 vaddr := toAddrPointer(&vi, valIsPtr) // pointer to value
2567 b = appendVarint(b, tag)
2568 siz := keySizer(kaddr, 1) + valCachedSizer(vaddr, 1) // tag of key = 1 (size=1), tag of val = 2 (size=1)
2569 b = appendVarint(b, uint64(siz))
2570 b, err = keyMarshaler(b, kaddr, keyWireTag, deterministic)
2571 if !nerr.Merge(err) {
2572 return b, err
2573 }
2574 b, err = valMarshaler(b, vaddr, valWireTag, deterministic)
2575 if err != ErrNil && !nerr.Merge(err) { // allow nil value in map
2576 return b, err
2577 }
2578 }
2579 return b, nerr.E
2580 }
2581}
2582
2583// makeOneOfMarshaler returns the sizer and marshaler for a oneof field.
2584// fi is the marshal info of the field.
2585// f is the pointer to the reflect data structure of the field.
2586func makeOneOfMarshaler(fi *marshalFieldInfo, f *reflect.StructField) (sizer, marshaler) {
2587 // Oneof field is an interface. We need to get the actual data type on the fly.
2588 t := f.Type
2589 return func(ptr pointer, _ int) int {
2590 p := ptr.getInterfacePointer()
2591 if p.isNil() {
2592 return 0
2593 }
2594 v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
2595 telem := v.Type()
2596 e := fi.oneofElems[telem]
2597 return e.sizer(p, e.tagsize)
2598 },
2599 func(b []byte, ptr pointer, _ uint64, deterministic bool) ([]byte, error) {
2600 p := ptr.getInterfacePointer()
2601 if p.isNil() {
2602 return b, nil
2603 }
2604 v := ptr.asPointerTo(t).Elem().Elem().Elem() // *interface -> interface -> *struct -> struct
2605 telem := v.Type()
2606 if telem.Field(0).Type.Kind() == reflect.Ptr && p.getPointer().isNil() {
2607 return b, errOneofHasNil
2608 }
2609 e := fi.oneofElems[telem]
2610 return e.marshaler(b, p, e.wiretag, deterministic)
2611 }
2612}
2613
2614// sizeExtensions computes the size of encoded data for a XXX_InternalExtensions field.
2615func (u *marshalInfo) sizeExtensions(ext *XXX_InternalExtensions) int {
2616 m, mu := ext.extensionsRead()
2617 if m == nil {
2618 return 0
2619 }
2620 mu.Lock()
2621
2622 n := 0
2623 for _, e := range m {
2624 if e.value == nil || e.desc == nil {
2625 // Extension is only in its encoded form.
2626 n += len(e.enc)
2627 continue
2628 }
2629
2630 // We don't skip extensions that have an encoded form set,
2631 // because the extension value may have been mutated after
2632 // the last time this function was called.
2633 ei := u.getExtElemInfo(e.desc)
2634 v := e.value
2635 p := toAddrPointer(&v, ei.isptr)
2636 n += ei.sizer(p, ei.tagsize)
2637 }
2638 mu.Unlock()
2639 return n
2640}
2641
2642// appendExtensions marshals a XXX_InternalExtensions field to the end of byte slice b.
2643func (u *marshalInfo) appendExtensions(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
2644 m, mu := ext.extensionsRead()
2645 if m == nil {
2646 return b, nil
2647 }
2648 mu.Lock()
2649 defer mu.Unlock()
2650
2651 var err error
2652 var nerr nonFatal
2653
2654 // Fast-path for common cases: zero or one extensions.
2655 // Don't bother sorting the keys.
2656 if len(m) <= 1 {
2657 for _, e := range m {
2658 if e.value == nil || e.desc == nil {
2659 // Extension is only in its encoded form.
2660 b = append(b, e.enc...)
2661 continue
2662 }
2663
2664 // We don't skip extensions that have an encoded form set,
2665 // because the extension value may have been mutated after
2666 // the last time this function was called.
2667
2668 ei := u.getExtElemInfo(e.desc)
2669 v := e.value
2670 p := toAddrPointer(&v, ei.isptr)
2671 b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
2672 if !nerr.Merge(err) {
2673 return b, err
2674 }
2675 }
2676 return b, nerr.E
2677 }
2678
2679 // Sort the keys to provide a deterministic encoding.
2680 // Not sure this is required, but the old code does it.
2681 keys := make([]int, 0, len(m))
2682 for k := range m {
2683 keys = append(keys, int(k))
2684 }
2685 sort.Ints(keys)
2686
2687 for _, k := range keys {
2688 e := m[int32(k)]
2689 if e.value == nil || e.desc == nil {
2690 // Extension is only in its encoded form.
2691 b = append(b, e.enc...)
2692 continue
2693 }
2694
2695 // We don't skip extensions that have an encoded form set,
2696 // because the extension value may have been mutated after
2697 // the last time this function was called.
2698
2699 ei := u.getExtElemInfo(e.desc)
2700 v := e.value
2701 p := toAddrPointer(&v, ei.isptr)
2702 b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
2703 if !nerr.Merge(err) {
2704 return b, err
2705 }
2706 }
2707 return b, nerr.E
2708}
2709
2710// message set format is:
2711// message MessageSet {
2712// repeated group Item = 1 {
2713// required int32 type_id = 2;
2714// required string message = 3;
2715// };
2716// }
2717
2718// sizeMessageSet computes the size of encoded data for a XXX_InternalExtensions field
2719// in message set format (above).
2720func (u *marshalInfo) sizeMessageSet(ext *XXX_InternalExtensions) int {
2721 m, mu := ext.extensionsRead()
2722 if m == nil {
2723 return 0
2724 }
2725 mu.Lock()
2726
2727 n := 0
2728 for id, e := range m {
2729 n += 2 // start group, end group. tag = 1 (size=1)
2730 n += SizeVarint(uint64(id)) + 1 // type_id, tag = 2 (size=1)
2731
2732 if e.value == nil || e.desc == nil {
2733 // Extension is only in its encoded form.
2734 msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
2735 siz := len(msgWithLen)
2736 n += siz + 1 // message, tag = 3 (size=1)
2737 continue
2738 }
2739
2740 // We don't skip extensions that have an encoded form set,
2741 // because the extension value may have been mutated after
2742 // the last time this function was called.
2743
2744 ei := u.getExtElemInfo(e.desc)
2745 v := e.value
2746 p := toAddrPointer(&v, ei.isptr)
2747 n += ei.sizer(p, 1) // message, tag = 3 (size=1)
2748 }
2749 mu.Unlock()
2750 return n
2751}
2752
2753// appendMessageSet marshals a XXX_InternalExtensions field in message set format (above)
2754// to the end of byte slice b.
2755func (u *marshalInfo) appendMessageSet(b []byte, ext *XXX_InternalExtensions, deterministic bool) ([]byte, error) {
2756 m, mu := ext.extensionsRead()
2757 if m == nil {
2758 return b, nil
2759 }
2760 mu.Lock()
2761 defer mu.Unlock()
2762
2763 var err error
2764 var nerr nonFatal
2765
2766 // Fast-path for common cases: zero or one extensions.
2767 // Don't bother sorting the keys.
2768 if len(m) <= 1 {
2769 for id, e := range m {
2770 b = append(b, 1<<3|WireStartGroup)
2771 b = append(b, 2<<3|WireVarint)
2772 b = appendVarint(b, uint64(id))
2773
2774 if e.value == nil || e.desc == nil {
2775 // Extension is only in its encoded form.
2776 msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
2777 b = append(b, 3<<3|WireBytes)
2778 b = append(b, msgWithLen...)
2779 b = append(b, 1<<3|WireEndGroup)
2780 continue
2781 }
2782
2783 // We don't skip extensions that have an encoded form set,
2784 // because the extension value may have been mutated after
2785 // the last time this function was called.
2786
2787 ei := u.getExtElemInfo(e.desc)
2788 v := e.value
2789 p := toAddrPointer(&v, ei.isptr)
2790 b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
2791 if !nerr.Merge(err) {
2792 return b, err
2793 }
2794 b = append(b, 1<<3|WireEndGroup)
2795 }
2796 return b, nerr.E
2797 }
2798
2799 // Sort the keys to provide a deterministic encoding.
2800 keys := make([]int, 0, len(m))
2801 for k := range m {
2802 keys = append(keys, int(k))
2803 }
2804 sort.Ints(keys)
2805
2806 for _, id := range keys {
2807 e := m[int32(id)]
2808 b = append(b, 1<<3|WireStartGroup)
2809 b = append(b, 2<<3|WireVarint)
2810 b = appendVarint(b, uint64(id))
2811
2812 if e.value == nil || e.desc == nil {
2813 // Extension is only in its encoded form.
2814 msgWithLen := skipVarint(e.enc) // skip old tag, but leave the length varint
2815 b = append(b, 3<<3|WireBytes)
2816 b = append(b, msgWithLen...)
2817 b = append(b, 1<<3|WireEndGroup)
2818 continue
2819 }
2820
2821 // We don't skip extensions that have an encoded form set,
2822 // because the extension value may have been mutated after
2823 // the last time this function was called.
2824
2825 ei := u.getExtElemInfo(e.desc)
2826 v := e.value
2827 p := toAddrPointer(&v, ei.isptr)
2828 b, err = ei.marshaler(b, p, 3<<3|WireBytes, deterministic)
2829 b = append(b, 1<<3|WireEndGroup)
2830 if !nerr.Merge(err) {
2831 return b, err
2832 }
2833 }
2834 return b, nerr.E
2835}
2836
2837// sizeV1Extensions computes the size of encoded data for a V1-API extension field.
2838func (u *marshalInfo) sizeV1Extensions(m map[int32]Extension) int {
2839 if m == nil {
2840 return 0
2841 }
2842
2843 n := 0
2844 for _, e := range m {
2845 if e.value == nil || e.desc == nil {
2846 // Extension is only in its encoded form.
2847 n += len(e.enc)
2848 continue
2849 }
2850
2851 // We don't skip extensions that have an encoded form set,
2852 // because the extension value may have been mutated after
2853 // the last time this function was called.
2854
2855 ei := u.getExtElemInfo(e.desc)
2856 v := e.value
2857 p := toAddrPointer(&v, ei.isptr)
2858 n += ei.sizer(p, ei.tagsize)
2859 }
2860 return n
2861}
2862
2863// appendV1Extensions marshals a V1-API extension field to the end of byte slice b.
2864func (u *marshalInfo) appendV1Extensions(b []byte, m map[int32]Extension, deterministic bool) ([]byte, error) {
2865 if m == nil {
2866 return b, nil
2867 }
2868
2869 // Sort the keys to provide a deterministic encoding.
2870 keys := make([]int, 0, len(m))
2871 for k := range m {
2872 keys = append(keys, int(k))
2873 }
2874 sort.Ints(keys)
2875
2876 var err error
2877 var nerr nonFatal
2878 for _, k := range keys {
2879 e := m[int32(k)]
2880 if e.value == nil || e.desc == nil {
2881 // Extension is only in its encoded form.
2882 b = append(b, e.enc...)
2883 continue
2884 }
2885
2886 // We don't skip extensions that have an encoded form set,
2887 // because the extension value may have been mutated after
2888 // the last time this function was called.
2889
2890 ei := u.getExtElemInfo(e.desc)
2891 v := e.value
2892 p := toAddrPointer(&v, ei.isptr)
2893 b, err = ei.marshaler(b, p, ei.wiretag, deterministic)
2894 if !nerr.Merge(err) {
2895 return b, err
2896 }
2897 }
2898 return b, nerr.E
2899}
2900
2901// newMarshaler is the interface representing objects that can marshal themselves.
2902//
2903// This exists to support protoc-gen-go generated messages.
2904// The proto package will stop type-asserting to this interface in the future.
2905//
2906// DO NOT DEPEND ON THIS.
2907type newMarshaler interface {
2908 XXX_Size() int
2909 XXX_Marshal(b []byte, deterministic bool) ([]byte, error)
2910}
2911
2912// Size returns the encoded size of a protocol buffer message.
2913// This is the main entry point.
2914func Size(pb Message) int {
2915 if m, ok := pb.(newMarshaler); ok {
2916 return m.XXX_Size()
2917 }
2918 if m, ok := pb.(Marshaler); ok {
2919 // If the message can marshal itself, let it do it, for compatibility.
2920 // NOTE: This is not efficient.
2921 b, _ := m.Marshal()
2922 return len(b)
2923 }
2924 // in case somehow we didn't generate the wrapper
2925 if pb == nil {
2926 return 0
2927 }
2928 var info InternalMessageInfo
2929 return info.Size(pb)
2930}
2931
2932// Marshal takes a protocol buffer message
2933// and encodes it into the wire format, returning the data.
2934// This is the main entry point.
2935func Marshal(pb Message) ([]byte, error) {
2936 if m, ok := pb.(newMarshaler); ok {
2937 siz := m.XXX_Size()
2938 b := make([]byte, 0, siz)
2939 return m.XXX_Marshal(b, false)
2940 }
2941 if m, ok := pb.(Marshaler); ok {
2942 // If the message can marshal itself, let it do it, for compatibility.
2943 // NOTE: This is not efficient.
2944 return m.Marshal()
2945 }
2946 // in case somehow we didn't generate the wrapper
2947 if pb == nil {
2948 return nil, ErrNil
2949 }
2950 var info InternalMessageInfo
2951 siz := info.Size(pb)
2952 b := make([]byte, 0, siz)
2953 return info.Marshal(b, pb, false)
2954}
2955
2956// Marshal takes a protocol buffer message
2957// and encodes it into the wire format, writing the result to the
2958// Buffer.
2959// This is an alternative entry point. It is not necessary to use
2960// a Buffer for most applications.
2961func (p *Buffer) Marshal(pb Message) error {
2962 var err error
2963 if p.deterministic {
2964 if _, ok := pb.(Marshaler); ok {
2965 return fmt.Errorf("proto: deterministic not supported by the Marshal method of %T", pb)
2966 }
2967 }
2968 if m, ok := pb.(newMarshaler); ok {
2969 siz := m.XXX_Size()
2970 p.grow(siz) // make sure buf has enough capacity
2971 p.buf, err = m.XXX_Marshal(p.buf, p.deterministic)
2972 return err
2973 }
2974 if m, ok := pb.(Marshaler); ok {
2975 // If the message can marshal itself, let it do it, for compatibility.
2976 // NOTE: This is not efficient.
2977 var b []byte
2978 b, err = m.Marshal()
2979 p.buf = append(p.buf, b...)
2980 return err
2981 }
2982 // in case somehow we didn't generate the wrapper
2983 if pb == nil {
2984 return ErrNil
2985 }
2986 var info InternalMessageInfo
2987 siz := info.Size(pb)
2988 p.grow(siz) // make sure buf has enough capacity
2989 p.buf, err = info.Marshal(p.buf, pb, p.deterministic)
2990 return err
2991}
2992
2993// grow grows the buffer's capacity, if necessary, to guarantee space for
2994// another n bytes. After grow(n), at least n bytes can be written to the
2995// buffer without another allocation.
2996func (p *Buffer) grow(n int) {
2997 need := len(p.buf) + n
2998 if need <= cap(p.buf) {
2999 return
3000 }
3001 newCap := len(p.buf) * 2
3002 if newCap < need {
3003 newCap = need
3004 }
3005 p.buf = append(make([]byte, 0, newCap), p.buf...)
3006}