blob: e6b15c76cabeb059a10cfa78c4025cedc479a6a1 [file] [log] [blame]
khenaidooac637102019-01-14 15:44:34 -05001// 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 "io"
38 "math"
39 "reflect"
40 "strconv"
41 "strings"
42 "sync"
43 "sync/atomic"
44 "unicode/utf8"
45)
46
47// Unmarshal is the entry point from the generated .pb.go files.
48// This function is not intended to be used by non-generated code.
49// This function is not subject to any compatibility guarantee.
50// msg contains a pointer to a protocol buffer struct.
51// b is the data to be unmarshaled into the protocol buffer.
52// a is a pointer to a place to store cached unmarshal information.
53func (a *InternalMessageInfo) Unmarshal(msg Message, b []byte) error {
54 // Load the unmarshal information for this message type.
55 // The atomic load ensures memory consistency.
56 u := atomicLoadUnmarshalInfo(&a.unmarshal)
57 if u == nil {
58 // Slow path: find unmarshal info for msg, update a with it.
59 u = getUnmarshalInfo(reflect.TypeOf(msg).Elem())
60 atomicStoreUnmarshalInfo(&a.unmarshal, u)
61 }
62 // Then do the unmarshaling.
63 err := u.unmarshal(toPointer(&msg), b)
64 return err
65}
66
67type unmarshalInfo struct {
68 typ reflect.Type // type of the protobuf struct
69
70 // 0 = only typ field is initialized
71 // 1 = completely initialized
72 initialized int32
73 lock sync.Mutex // prevents double initialization
74 dense []unmarshalFieldInfo // fields indexed by tag #
75 sparse map[uint64]unmarshalFieldInfo // fields indexed by tag #
76 reqFields []string // names of required fields
77 reqMask uint64 // 1<<len(reqFields)-1
78 unrecognized field // offset of []byte to put unrecognized data (or invalidField if we should throw it away)
79 extensions field // offset of extensions field (of type proto.XXX_InternalExtensions), or invalidField if it does not exist
80 oldExtensions field // offset of old-form extensions field (of type map[int]Extension)
81 extensionRanges []ExtensionRange // if non-nil, implies extensions field is valid
82 isMessageSet bool // if true, implies extensions field is valid
83
84 bytesExtensions field // offset of XXX_extensions with type []byte
85}
86
87// An unmarshaler takes a stream of bytes and a pointer to a field of a message.
88// It decodes the field, stores it at f, and returns the unused bytes.
89// w is the wire encoding.
90// b is the data after the tag and wire encoding have been read.
91type unmarshaler func(b []byte, f pointer, w int) ([]byte, error)
92
93type unmarshalFieldInfo struct {
94 // location of the field in the proto message structure.
95 field field
96
97 // function to unmarshal the data for the field.
98 unmarshal unmarshaler
99
100 // if a required field, contains a single set bit at this field's index in the required field list.
101 reqMask uint64
102
103 name string // name of the field, for error reporting
104}
105
106var (
107 unmarshalInfoMap = map[reflect.Type]*unmarshalInfo{}
108 unmarshalInfoLock sync.Mutex
109)
110
111// getUnmarshalInfo returns the data structure which can be
112// subsequently used to unmarshal a message of the given type.
113// t is the type of the message (note: not pointer to message).
114func getUnmarshalInfo(t reflect.Type) *unmarshalInfo {
115 // It would be correct to return a new unmarshalInfo
116 // unconditionally. We would end up allocating one
117 // per occurrence of that type as a message or submessage.
118 // We use a cache here just to reduce memory usage.
119 unmarshalInfoLock.Lock()
120 defer unmarshalInfoLock.Unlock()
121 u := unmarshalInfoMap[t]
122 if u == nil {
123 u = &unmarshalInfo{typ: t}
124 // Note: we just set the type here. The rest of the fields
125 // will be initialized on first use.
126 unmarshalInfoMap[t] = u
127 }
128 return u
129}
130
131// unmarshal does the main work of unmarshaling a message.
132// u provides type information used to unmarshal the message.
133// m is a pointer to a protocol buffer message.
134// b is a byte stream to unmarshal into m.
135// This is top routine used when recursively unmarshaling submessages.
136func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error {
137 if atomic.LoadInt32(&u.initialized) == 0 {
138 u.computeUnmarshalInfo()
139 }
140 if u.isMessageSet {
141 return UnmarshalMessageSet(b, m.offset(u.extensions).toExtensions())
142 }
143 var reqMask uint64 // bitmask of required fields we've seen.
144 var errLater error
145 for len(b) > 0 {
146 // Read tag and wire type.
147 // Special case 1 and 2 byte varints.
148 var x uint64
149 if b[0] < 128 {
150 x = uint64(b[0])
151 b = b[1:]
152 } else if len(b) >= 2 && b[1] < 128 {
153 x = uint64(b[0]&0x7f) + uint64(b[1])<<7
154 b = b[2:]
155 } else {
156 var n int
157 x, n = decodeVarint(b)
158 if n == 0 {
159 return io.ErrUnexpectedEOF
160 }
161 b = b[n:]
162 }
163 tag := x >> 3
164 wire := int(x) & 7
165
166 // Dispatch on the tag to one of the unmarshal* functions below.
167 var f unmarshalFieldInfo
168 if tag < uint64(len(u.dense)) {
169 f = u.dense[tag]
170 } else {
171 f = u.sparse[tag]
172 }
173 if fn := f.unmarshal; fn != nil {
174 var err error
175 b, err = fn(b, m.offset(f.field), wire)
176 if err == nil {
177 reqMask |= f.reqMask
178 continue
179 }
180 if r, ok := err.(*RequiredNotSetError); ok {
181 // Remember this error, but keep parsing. We need to produce
182 // a full parse even if a required field is missing.
183 if errLater == nil {
184 errLater = r
185 }
186 reqMask |= f.reqMask
187 continue
188 }
189 if err != errInternalBadWireType {
190 if err == errInvalidUTF8 {
191 if errLater == nil {
192 fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
193 errLater = &invalidUTF8Error{fullName}
194 }
195 continue
196 }
197 return err
198 }
199 // Fragments with bad wire type are treated as unknown fields.
200 }
201
202 // Unknown tag.
203 if !u.unrecognized.IsValid() {
204 // Don't keep unrecognized data; just skip it.
205 var err error
206 b, err = skipField(b, wire)
207 if err != nil {
208 return err
209 }
210 continue
211 }
212 // Keep unrecognized data around.
213 // maybe in extensions, maybe in the unrecognized field.
214 z := m.offset(u.unrecognized).toBytes()
215 var emap map[int32]Extension
216 var e Extension
217 for _, r := range u.extensionRanges {
218 if uint64(r.Start) <= tag && tag <= uint64(r.End) {
219 if u.extensions.IsValid() {
220 mp := m.offset(u.extensions).toExtensions()
221 emap = mp.extensionsWrite()
222 e = emap[int32(tag)]
223 z = &e.enc
224 break
225 }
226 if u.oldExtensions.IsValid() {
227 p := m.offset(u.oldExtensions).toOldExtensions()
228 emap = *p
229 if emap == nil {
230 emap = map[int32]Extension{}
231 *p = emap
232 }
233 e = emap[int32(tag)]
234 z = &e.enc
235 break
236 }
237 if u.bytesExtensions.IsValid() {
238 z = m.offset(u.bytesExtensions).toBytes()
239 break
240 }
241 panic("no extensions field available")
242 }
243 }
244 // Use wire type to skip data.
245 var err error
246 b0 := b
247 b, err = skipField(b, wire)
248 if err != nil {
249 return err
250 }
251 *z = encodeVarint(*z, tag<<3|uint64(wire))
252 *z = append(*z, b0[:len(b0)-len(b)]...)
253
254 if emap != nil {
255 emap[int32(tag)] = e
256 }
257 }
258 if reqMask != u.reqMask && errLater == nil {
259 // A required field of this message is missing.
260 for _, n := range u.reqFields {
261 if reqMask&1 == 0 {
262 errLater = &RequiredNotSetError{n}
263 }
264 reqMask >>= 1
265 }
266 }
267 return errLater
268}
269
270// computeUnmarshalInfo fills in u with information for use
271// in unmarshaling protocol buffers of type u.typ.
272func (u *unmarshalInfo) computeUnmarshalInfo() {
273 u.lock.Lock()
274 defer u.lock.Unlock()
275 if u.initialized != 0 {
276 return
277 }
278 t := u.typ
279 n := t.NumField()
280
281 // Set up the "not found" value for the unrecognized byte buffer.
282 // This is the default for proto3.
283 u.unrecognized = invalidField
284 u.extensions = invalidField
285 u.oldExtensions = invalidField
286 u.bytesExtensions = invalidField
287
288 // List of the generated type and offset for each oneof field.
289 type oneofField struct {
290 ityp reflect.Type // interface type of oneof field
291 field field // offset in containing message
292 }
293 var oneofFields []oneofField
294
295 for i := 0; i < n; i++ {
296 f := t.Field(i)
297 if f.Name == "XXX_unrecognized" {
298 // The byte slice used to hold unrecognized input is special.
299 if f.Type != reflect.TypeOf(([]byte)(nil)) {
300 panic("bad type for XXX_unrecognized field: " + f.Type.Name())
301 }
302 u.unrecognized = toField(&f)
303 continue
304 }
305 if f.Name == "XXX_InternalExtensions" {
306 // Ditto here.
307 if f.Type != reflect.TypeOf(XXX_InternalExtensions{}) {
308 panic("bad type for XXX_InternalExtensions field: " + f.Type.Name())
309 }
310 u.extensions = toField(&f)
311 if f.Tag.Get("protobuf_messageset") == "1" {
312 u.isMessageSet = true
313 }
314 continue
315 }
316 if f.Name == "XXX_extensions" {
317 // An older form of the extensions field.
318 if f.Type == reflect.TypeOf((map[int32]Extension)(nil)) {
319 u.oldExtensions = toField(&f)
320 continue
321 } else if f.Type == reflect.TypeOf(([]byte)(nil)) {
322 u.bytesExtensions = toField(&f)
323 continue
324 }
325 panic("bad type for XXX_extensions field: " + f.Type.Name())
326 }
327 if f.Name == "XXX_NoUnkeyedLiteral" || f.Name == "XXX_sizecache" {
328 continue
329 }
330
331 oneof := f.Tag.Get("protobuf_oneof")
332 if oneof != "" {
333 oneofFields = append(oneofFields, oneofField{f.Type, toField(&f)})
334 // The rest of oneof processing happens below.
335 continue
336 }
337
338 tags := f.Tag.Get("protobuf")
339 tagArray := strings.Split(tags, ",")
340 if len(tagArray) < 2 {
341 panic("protobuf tag not enough fields in " + t.Name() + "." + f.Name + ": " + tags)
342 }
343 tag, err := strconv.Atoi(tagArray[1])
344 if err != nil {
345 panic("protobuf tag field not an integer: " + tagArray[1])
346 }
347
348 name := ""
349 for _, tag := range tagArray[3:] {
350 if strings.HasPrefix(tag, "name=") {
351 name = tag[5:]
352 }
353 }
354
355 // Extract unmarshaling function from the field (its type and tags).
356 unmarshal := fieldUnmarshaler(&f)
357
358 // Required field?
359 var reqMask uint64
360 if tagArray[2] == "req" {
361 bit := len(u.reqFields)
362 u.reqFields = append(u.reqFields, name)
363 reqMask = uint64(1) << uint(bit)
364 // TODO: if we have more than 64 required fields, we end up
365 // not verifying that all required fields are present.
366 // Fix this, perhaps using a count of required fields?
367 }
368
369 // Store the info in the correct slot in the message.
370 u.setTag(tag, toField(&f), unmarshal, reqMask, name)
371 }
372
373 // Find any types associated with oneof fields.
374 // TODO: XXX_OneofFuncs returns more info than we need. Get rid of some of it?
375 fn := reflect.Zero(reflect.PtrTo(t)).MethodByName("XXX_OneofFuncs")
376 // gogo: len(oneofFields) > 0 is needed for embedded oneof messages, without a marshaler and unmarshaler
377 if fn.IsValid() && len(oneofFields) > 0 {
378 res := fn.Call(nil)[3] // last return value from XXX_OneofFuncs: []interface{}
379 for i := res.Len() - 1; i >= 0; i-- {
380 v := res.Index(i) // interface{}
381 tptr := reflect.ValueOf(v.Interface()).Type() // *Msg_X
382 typ := tptr.Elem() // Msg_X
383
384 f := typ.Field(0) // oneof implementers have one field
385 baseUnmarshal := fieldUnmarshaler(&f)
386 tags := strings.Split(f.Tag.Get("protobuf"), ",")
387 fieldNum, err := strconv.Atoi(tags[1])
388 if err != nil {
389 panic("protobuf tag field not an integer: " + tags[1])
390 }
391 var name string
392 for _, tag := range tags {
393 if strings.HasPrefix(tag, "name=") {
394 name = strings.TrimPrefix(tag, "name=")
395 break
396 }
397 }
398
399 // Find the oneof field that this struct implements.
400 // Might take O(n^2) to process all of the oneofs, but who cares.
401 for _, of := range oneofFields {
402 if tptr.Implements(of.ityp) {
403 // We have found the corresponding interface for this struct.
404 // That lets us know where this struct should be stored
405 // when we encounter it during unmarshaling.
406 unmarshal := makeUnmarshalOneof(typ, of.ityp, baseUnmarshal)
407 u.setTag(fieldNum, of.field, unmarshal, 0, name)
408 }
409 }
410 }
411 }
412
413 // Get extension ranges, if any.
414 fn = reflect.Zero(reflect.PtrTo(t)).MethodByName("ExtensionRangeArray")
415 if fn.IsValid() {
416 if !u.extensions.IsValid() && !u.oldExtensions.IsValid() && !u.bytesExtensions.IsValid() {
417 panic("a message with extensions, but no extensions field in " + t.Name())
418 }
419 u.extensionRanges = fn.Call(nil)[0].Interface().([]ExtensionRange)
420 }
421
422 // Explicitly disallow tag 0. This will ensure we flag an error
423 // when decoding a buffer of all zeros. Without this code, we
424 // would decode and skip an all-zero buffer of even length.
425 // [0 0] is [tag=0/wiretype=varint varint-encoded-0].
426 u.setTag(0, zeroField, func(b []byte, f pointer, w int) ([]byte, error) {
427 return nil, fmt.Errorf("proto: %s: illegal tag 0 (wire type %d)", t, w)
428 }, 0, "")
429
430 // Set mask for required field check.
431 u.reqMask = uint64(1)<<uint(len(u.reqFields)) - 1
432
433 atomic.StoreInt32(&u.initialized, 1)
434}
435
436// setTag stores the unmarshal information for the given tag.
437// tag = tag # for field
438// field/unmarshal = unmarshal info for that field.
439// reqMask = if required, bitmask for field position in required field list. 0 otherwise.
440// name = short name of the field.
441func (u *unmarshalInfo) setTag(tag int, field field, unmarshal unmarshaler, reqMask uint64, name string) {
442 i := unmarshalFieldInfo{field: field, unmarshal: unmarshal, reqMask: reqMask, name: name}
443 n := u.typ.NumField()
444 if tag >= 0 && (tag < 16 || tag < 2*n) { // TODO: what are the right numbers here?
445 for len(u.dense) <= tag {
446 u.dense = append(u.dense, unmarshalFieldInfo{})
447 }
448 u.dense[tag] = i
449 return
450 }
451 if u.sparse == nil {
452 u.sparse = map[uint64]unmarshalFieldInfo{}
453 }
454 u.sparse[uint64(tag)] = i
455}
456
457// fieldUnmarshaler returns an unmarshaler for the given field.
458func fieldUnmarshaler(f *reflect.StructField) unmarshaler {
459 if f.Type.Kind() == reflect.Map {
460 return makeUnmarshalMap(f)
461 }
462 return typeUnmarshaler(f.Type, f.Tag.Get("protobuf"))
463}
464
465// typeUnmarshaler returns an unmarshaler for the given field type / field tag pair.
466func typeUnmarshaler(t reflect.Type, tags string) unmarshaler {
467 tagArray := strings.Split(tags, ",")
468 encoding := tagArray[0]
469 name := "unknown"
470 ctype := false
471 isTime := false
472 isDuration := false
473 isWktPointer := false
474 proto3 := false
475 validateUTF8 := true
476 for _, tag := range tagArray[3:] {
477 if strings.HasPrefix(tag, "name=") {
478 name = tag[5:]
479 }
480 if tag == "proto3" {
481 proto3 = true
482 }
483 if strings.HasPrefix(tag, "customtype=") {
484 ctype = true
485 }
486 if tag == "stdtime" {
487 isTime = true
488 }
489 if tag == "stdduration" {
490 isDuration = true
491 }
492 if tag == "wktptr" {
493 isWktPointer = true
494 }
495 }
496 validateUTF8 = validateUTF8 && proto3
497
498 // Figure out packaging (pointer, slice, or both)
499 slice := false
500 pointer := false
501 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
502 slice = true
503 t = t.Elem()
504 }
505 if t.Kind() == reflect.Ptr {
506 pointer = true
507 t = t.Elem()
508 }
509
510 if ctype {
511 if reflect.PtrTo(t).Implements(customType) {
512 if slice {
513 return makeUnmarshalCustomSlice(getUnmarshalInfo(t), name)
514 }
515 if pointer {
516 return makeUnmarshalCustomPtr(getUnmarshalInfo(t), name)
517 }
518 return makeUnmarshalCustom(getUnmarshalInfo(t), name)
519 } else {
520 panic(fmt.Sprintf("custom type: type: %v, does not implement the proto.custom interface", t))
521 }
522 }
523
524 if isTime {
525 if pointer {
526 if slice {
527 return makeUnmarshalTimePtrSlice(getUnmarshalInfo(t), name)
528 }
529 return makeUnmarshalTimePtr(getUnmarshalInfo(t), name)
530 }
531 if slice {
532 return makeUnmarshalTimeSlice(getUnmarshalInfo(t), name)
533 }
534 return makeUnmarshalTime(getUnmarshalInfo(t), name)
535 }
536
537 if isDuration {
538 if pointer {
539 if slice {
540 return makeUnmarshalDurationPtrSlice(getUnmarshalInfo(t), name)
541 }
542 return makeUnmarshalDurationPtr(getUnmarshalInfo(t), name)
543 }
544 if slice {
545 return makeUnmarshalDurationSlice(getUnmarshalInfo(t), name)
546 }
547 return makeUnmarshalDuration(getUnmarshalInfo(t), name)
548 }
549
550 if isWktPointer {
551 switch t.Kind() {
552 case reflect.Float64:
553 if pointer {
554 if slice {
555 return makeStdDoubleValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
556 }
557 return makeStdDoubleValuePtrUnmarshaler(getUnmarshalInfo(t), name)
558 }
559 if slice {
560 return makeStdDoubleValueSliceUnmarshaler(getUnmarshalInfo(t), name)
561 }
562 return makeStdDoubleValueUnmarshaler(getUnmarshalInfo(t), name)
563 case reflect.Float32:
564 if pointer {
565 if slice {
566 return makeStdFloatValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
567 }
568 return makeStdFloatValuePtrUnmarshaler(getUnmarshalInfo(t), name)
569 }
570 if slice {
571 return makeStdFloatValueSliceUnmarshaler(getUnmarshalInfo(t), name)
572 }
573 return makeStdFloatValueUnmarshaler(getUnmarshalInfo(t), name)
574 case reflect.Int64:
575 if pointer {
576 if slice {
577 return makeStdInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
578 }
579 return makeStdInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
580 }
581 if slice {
582 return makeStdInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
583 }
584 return makeStdInt64ValueUnmarshaler(getUnmarshalInfo(t), name)
585 case reflect.Uint64:
586 if pointer {
587 if slice {
588 return makeStdUInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
589 }
590 return makeStdUInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
591 }
592 if slice {
593 return makeStdUInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
594 }
595 return makeStdUInt64ValueUnmarshaler(getUnmarshalInfo(t), name)
596 case reflect.Int32:
597 if pointer {
598 if slice {
599 return makeStdInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
600 }
601 return makeStdInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
602 }
603 if slice {
604 return makeStdInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
605 }
606 return makeStdInt32ValueUnmarshaler(getUnmarshalInfo(t), name)
607 case reflect.Uint32:
608 if pointer {
609 if slice {
610 return makeStdUInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
611 }
612 return makeStdUInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
613 }
614 if slice {
615 return makeStdUInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
616 }
617 return makeStdUInt32ValueUnmarshaler(getUnmarshalInfo(t), name)
618 case reflect.Bool:
619 if pointer {
620 if slice {
621 return makeStdBoolValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
622 }
623 return makeStdBoolValuePtrUnmarshaler(getUnmarshalInfo(t), name)
624 }
625 if slice {
626 return makeStdBoolValueSliceUnmarshaler(getUnmarshalInfo(t), name)
627 }
628 return makeStdBoolValueUnmarshaler(getUnmarshalInfo(t), name)
629 case reflect.String:
630 if pointer {
631 if slice {
632 return makeStdStringValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
633 }
634 return makeStdStringValuePtrUnmarshaler(getUnmarshalInfo(t), name)
635 }
636 if slice {
637 return makeStdStringValueSliceUnmarshaler(getUnmarshalInfo(t), name)
638 }
639 return makeStdStringValueUnmarshaler(getUnmarshalInfo(t), name)
640 case uint8SliceType:
641 if pointer {
642 if slice {
643 return makeStdBytesValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
644 }
645 return makeStdBytesValuePtrUnmarshaler(getUnmarshalInfo(t), name)
646 }
647 if slice {
648 return makeStdBytesValueSliceUnmarshaler(getUnmarshalInfo(t), name)
649 }
650 return makeStdBytesValueUnmarshaler(getUnmarshalInfo(t), name)
651 default:
652 panic(fmt.Sprintf("unknown wktpointer type %#v", t))
653 }
654 }
655
656 // We'll never have both pointer and slice for basic types.
657 if pointer && slice && t.Kind() != reflect.Struct {
658 panic("both pointer and slice for basic type in " + t.Name())
659 }
660
661 switch t.Kind() {
662 case reflect.Bool:
663 if pointer {
664 return unmarshalBoolPtr
665 }
666 if slice {
667 return unmarshalBoolSlice
668 }
669 return unmarshalBoolValue
670 case reflect.Int32:
671 switch encoding {
672 case "fixed32":
673 if pointer {
674 return unmarshalFixedS32Ptr
675 }
676 if slice {
677 return unmarshalFixedS32Slice
678 }
679 return unmarshalFixedS32Value
680 case "varint":
681 // this could be int32 or enum
682 if pointer {
683 return unmarshalInt32Ptr
684 }
685 if slice {
686 return unmarshalInt32Slice
687 }
688 return unmarshalInt32Value
689 case "zigzag32":
690 if pointer {
691 return unmarshalSint32Ptr
692 }
693 if slice {
694 return unmarshalSint32Slice
695 }
696 return unmarshalSint32Value
697 }
698 case reflect.Int64:
699 switch encoding {
700 case "fixed64":
701 if pointer {
702 return unmarshalFixedS64Ptr
703 }
704 if slice {
705 return unmarshalFixedS64Slice
706 }
707 return unmarshalFixedS64Value
708 case "varint":
709 if pointer {
710 return unmarshalInt64Ptr
711 }
712 if slice {
713 return unmarshalInt64Slice
714 }
715 return unmarshalInt64Value
716 case "zigzag64":
717 if pointer {
718 return unmarshalSint64Ptr
719 }
720 if slice {
721 return unmarshalSint64Slice
722 }
723 return unmarshalSint64Value
724 }
725 case reflect.Uint32:
726 switch encoding {
727 case "fixed32":
728 if pointer {
729 return unmarshalFixed32Ptr
730 }
731 if slice {
732 return unmarshalFixed32Slice
733 }
734 return unmarshalFixed32Value
735 case "varint":
736 if pointer {
737 return unmarshalUint32Ptr
738 }
739 if slice {
740 return unmarshalUint32Slice
741 }
742 return unmarshalUint32Value
743 }
744 case reflect.Uint64:
745 switch encoding {
746 case "fixed64":
747 if pointer {
748 return unmarshalFixed64Ptr
749 }
750 if slice {
751 return unmarshalFixed64Slice
752 }
753 return unmarshalFixed64Value
754 case "varint":
755 if pointer {
756 return unmarshalUint64Ptr
757 }
758 if slice {
759 return unmarshalUint64Slice
760 }
761 return unmarshalUint64Value
762 }
763 case reflect.Float32:
764 if pointer {
765 return unmarshalFloat32Ptr
766 }
767 if slice {
768 return unmarshalFloat32Slice
769 }
770 return unmarshalFloat32Value
771 case reflect.Float64:
772 if pointer {
773 return unmarshalFloat64Ptr
774 }
775 if slice {
776 return unmarshalFloat64Slice
777 }
778 return unmarshalFloat64Value
779 case reflect.Map:
780 panic("map type in typeUnmarshaler in " + t.Name())
781 case reflect.Slice:
782 if pointer {
783 panic("bad pointer in slice case in " + t.Name())
784 }
785 if slice {
786 return unmarshalBytesSlice
787 }
788 return unmarshalBytesValue
789 case reflect.String:
790 if validateUTF8 {
791 if pointer {
792 return unmarshalUTF8StringPtr
793 }
794 if slice {
795 return unmarshalUTF8StringSlice
796 }
797 return unmarshalUTF8StringValue
798 }
799 if pointer {
800 return unmarshalStringPtr
801 }
802 if slice {
803 return unmarshalStringSlice
804 }
805 return unmarshalStringValue
806 case reflect.Struct:
807 // message or group field
808 if !pointer {
809 switch encoding {
810 case "bytes":
811 if slice {
812 return makeUnmarshalMessageSlice(getUnmarshalInfo(t), name)
813 }
814 return makeUnmarshalMessage(getUnmarshalInfo(t), name)
815 }
816 }
817 switch encoding {
818 case "bytes":
819 if slice {
820 return makeUnmarshalMessageSlicePtr(getUnmarshalInfo(t), name)
821 }
822 return makeUnmarshalMessagePtr(getUnmarshalInfo(t), name)
823 case "group":
824 if slice {
825 return makeUnmarshalGroupSlicePtr(getUnmarshalInfo(t), name)
826 }
827 return makeUnmarshalGroupPtr(getUnmarshalInfo(t), name)
828 }
829 }
830 panic(fmt.Sprintf("unmarshaler not found type:%s encoding:%s", t, encoding))
831}
832
833// Below are all the unmarshalers for individual fields of various types.
834
835func unmarshalInt64Value(b []byte, f pointer, w int) ([]byte, error) {
836 if w != WireVarint {
837 return b, errInternalBadWireType
838 }
839 x, n := decodeVarint(b)
840 if n == 0 {
841 return nil, io.ErrUnexpectedEOF
842 }
843 b = b[n:]
844 v := int64(x)
845 *f.toInt64() = v
846 return b, nil
847}
848
849func unmarshalInt64Ptr(b []byte, f pointer, w int) ([]byte, error) {
850 if w != WireVarint {
851 return b, errInternalBadWireType
852 }
853 x, n := decodeVarint(b)
854 if n == 0 {
855 return nil, io.ErrUnexpectedEOF
856 }
857 b = b[n:]
858 v := int64(x)
859 *f.toInt64Ptr() = &v
860 return b, nil
861}
862
863func unmarshalInt64Slice(b []byte, f pointer, w int) ([]byte, error) {
864 if w == WireBytes { // packed
865 x, n := decodeVarint(b)
866 if n == 0 {
867 return nil, io.ErrUnexpectedEOF
868 }
869 b = b[n:]
870 if x > uint64(len(b)) {
871 return nil, io.ErrUnexpectedEOF
872 }
873 res := b[x:]
874 b = b[:x]
875 for len(b) > 0 {
876 x, n = decodeVarint(b)
877 if n == 0 {
878 return nil, io.ErrUnexpectedEOF
879 }
880 b = b[n:]
881 v := int64(x)
882 s := f.toInt64Slice()
883 *s = append(*s, v)
884 }
885 return res, nil
886 }
887 if w != WireVarint {
888 return b, errInternalBadWireType
889 }
890 x, n := decodeVarint(b)
891 if n == 0 {
892 return nil, io.ErrUnexpectedEOF
893 }
894 b = b[n:]
895 v := int64(x)
896 s := f.toInt64Slice()
897 *s = append(*s, v)
898 return b, nil
899}
900
901func unmarshalSint64Value(b []byte, f pointer, w int) ([]byte, error) {
902 if w != WireVarint {
903 return b, errInternalBadWireType
904 }
905 x, n := decodeVarint(b)
906 if n == 0 {
907 return nil, io.ErrUnexpectedEOF
908 }
909 b = b[n:]
910 v := int64(x>>1) ^ int64(x)<<63>>63
911 *f.toInt64() = v
912 return b, nil
913}
914
915func unmarshalSint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
916 if w != WireVarint {
917 return b, errInternalBadWireType
918 }
919 x, n := decodeVarint(b)
920 if n == 0 {
921 return nil, io.ErrUnexpectedEOF
922 }
923 b = b[n:]
924 v := int64(x>>1) ^ int64(x)<<63>>63
925 *f.toInt64Ptr() = &v
926 return b, nil
927}
928
929func unmarshalSint64Slice(b []byte, f pointer, w int) ([]byte, error) {
930 if w == WireBytes { // packed
931 x, n := decodeVarint(b)
932 if n == 0 {
933 return nil, io.ErrUnexpectedEOF
934 }
935 b = b[n:]
936 if x > uint64(len(b)) {
937 return nil, io.ErrUnexpectedEOF
938 }
939 res := b[x:]
940 b = b[:x]
941 for len(b) > 0 {
942 x, n = decodeVarint(b)
943 if n == 0 {
944 return nil, io.ErrUnexpectedEOF
945 }
946 b = b[n:]
947 v := int64(x>>1) ^ int64(x)<<63>>63
948 s := f.toInt64Slice()
949 *s = append(*s, v)
950 }
951 return res, nil
952 }
953 if w != WireVarint {
954 return b, errInternalBadWireType
955 }
956 x, n := decodeVarint(b)
957 if n == 0 {
958 return nil, io.ErrUnexpectedEOF
959 }
960 b = b[n:]
961 v := int64(x>>1) ^ int64(x)<<63>>63
962 s := f.toInt64Slice()
963 *s = append(*s, v)
964 return b, nil
965}
966
967func unmarshalUint64Value(b []byte, f pointer, w int) ([]byte, error) {
968 if w != WireVarint {
969 return b, errInternalBadWireType
970 }
971 x, n := decodeVarint(b)
972 if n == 0 {
973 return nil, io.ErrUnexpectedEOF
974 }
975 b = b[n:]
976 v := uint64(x)
977 *f.toUint64() = v
978 return b, nil
979}
980
981func unmarshalUint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
982 if w != WireVarint {
983 return b, errInternalBadWireType
984 }
985 x, n := decodeVarint(b)
986 if n == 0 {
987 return nil, io.ErrUnexpectedEOF
988 }
989 b = b[n:]
990 v := uint64(x)
991 *f.toUint64Ptr() = &v
992 return b, nil
993}
994
995func unmarshalUint64Slice(b []byte, f pointer, w int) ([]byte, error) {
996 if w == WireBytes { // packed
997 x, n := decodeVarint(b)
998 if n == 0 {
999 return nil, io.ErrUnexpectedEOF
1000 }
1001 b = b[n:]
1002 if x > uint64(len(b)) {
1003 return nil, io.ErrUnexpectedEOF
1004 }
1005 res := b[x:]
1006 b = b[:x]
1007 for len(b) > 0 {
1008 x, n = decodeVarint(b)
1009 if n == 0 {
1010 return nil, io.ErrUnexpectedEOF
1011 }
1012 b = b[n:]
1013 v := uint64(x)
1014 s := f.toUint64Slice()
1015 *s = append(*s, v)
1016 }
1017 return res, nil
1018 }
1019 if w != WireVarint {
1020 return b, errInternalBadWireType
1021 }
1022 x, n := decodeVarint(b)
1023 if n == 0 {
1024 return nil, io.ErrUnexpectedEOF
1025 }
1026 b = b[n:]
1027 v := uint64(x)
1028 s := f.toUint64Slice()
1029 *s = append(*s, v)
1030 return b, nil
1031}
1032
1033func unmarshalInt32Value(b []byte, f pointer, w int) ([]byte, error) {
1034 if w != WireVarint {
1035 return b, errInternalBadWireType
1036 }
1037 x, n := decodeVarint(b)
1038 if n == 0 {
1039 return nil, io.ErrUnexpectedEOF
1040 }
1041 b = b[n:]
1042 v := int32(x)
1043 *f.toInt32() = v
1044 return b, nil
1045}
1046
1047func unmarshalInt32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1048 if w != WireVarint {
1049 return b, errInternalBadWireType
1050 }
1051 x, n := decodeVarint(b)
1052 if n == 0 {
1053 return nil, io.ErrUnexpectedEOF
1054 }
1055 b = b[n:]
1056 v := int32(x)
1057 f.setInt32Ptr(v)
1058 return b, nil
1059}
1060
1061func unmarshalInt32Slice(b []byte, f pointer, w int) ([]byte, error) {
1062 if w == WireBytes { // packed
1063 x, n := decodeVarint(b)
1064 if n == 0 {
1065 return nil, io.ErrUnexpectedEOF
1066 }
1067 b = b[n:]
1068 if x > uint64(len(b)) {
1069 return nil, io.ErrUnexpectedEOF
1070 }
1071 res := b[x:]
1072 b = b[:x]
1073 for len(b) > 0 {
1074 x, n = decodeVarint(b)
1075 if n == 0 {
1076 return nil, io.ErrUnexpectedEOF
1077 }
1078 b = b[n:]
1079 v := int32(x)
1080 f.appendInt32Slice(v)
1081 }
1082 return res, nil
1083 }
1084 if w != WireVarint {
1085 return b, errInternalBadWireType
1086 }
1087 x, n := decodeVarint(b)
1088 if n == 0 {
1089 return nil, io.ErrUnexpectedEOF
1090 }
1091 b = b[n:]
1092 v := int32(x)
1093 f.appendInt32Slice(v)
1094 return b, nil
1095}
1096
1097func unmarshalSint32Value(b []byte, f pointer, w int) ([]byte, error) {
1098 if w != WireVarint {
1099 return b, errInternalBadWireType
1100 }
1101 x, n := decodeVarint(b)
1102 if n == 0 {
1103 return nil, io.ErrUnexpectedEOF
1104 }
1105 b = b[n:]
1106 v := int32(x>>1) ^ int32(x)<<31>>31
1107 *f.toInt32() = v
1108 return b, nil
1109}
1110
1111func unmarshalSint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1112 if w != WireVarint {
1113 return b, errInternalBadWireType
1114 }
1115 x, n := decodeVarint(b)
1116 if n == 0 {
1117 return nil, io.ErrUnexpectedEOF
1118 }
1119 b = b[n:]
1120 v := int32(x>>1) ^ int32(x)<<31>>31
1121 f.setInt32Ptr(v)
1122 return b, nil
1123}
1124
1125func unmarshalSint32Slice(b []byte, f pointer, w int) ([]byte, error) {
1126 if w == WireBytes { // packed
1127 x, n := decodeVarint(b)
1128 if n == 0 {
1129 return nil, io.ErrUnexpectedEOF
1130 }
1131 b = b[n:]
1132 if x > uint64(len(b)) {
1133 return nil, io.ErrUnexpectedEOF
1134 }
1135 res := b[x:]
1136 b = b[:x]
1137 for len(b) > 0 {
1138 x, n = decodeVarint(b)
1139 if n == 0 {
1140 return nil, io.ErrUnexpectedEOF
1141 }
1142 b = b[n:]
1143 v := int32(x>>1) ^ int32(x)<<31>>31
1144 f.appendInt32Slice(v)
1145 }
1146 return res, nil
1147 }
1148 if w != WireVarint {
1149 return b, errInternalBadWireType
1150 }
1151 x, n := decodeVarint(b)
1152 if n == 0 {
1153 return nil, io.ErrUnexpectedEOF
1154 }
1155 b = b[n:]
1156 v := int32(x>>1) ^ int32(x)<<31>>31
1157 f.appendInt32Slice(v)
1158 return b, nil
1159}
1160
1161func unmarshalUint32Value(b []byte, f pointer, w int) ([]byte, error) {
1162 if w != WireVarint {
1163 return b, errInternalBadWireType
1164 }
1165 x, n := decodeVarint(b)
1166 if n == 0 {
1167 return nil, io.ErrUnexpectedEOF
1168 }
1169 b = b[n:]
1170 v := uint32(x)
1171 *f.toUint32() = v
1172 return b, nil
1173}
1174
1175func unmarshalUint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1176 if w != WireVarint {
1177 return b, errInternalBadWireType
1178 }
1179 x, n := decodeVarint(b)
1180 if n == 0 {
1181 return nil, io.ErrUnexpectedEOF
1182 }
1183 b = b[n:]
1184 v := uint32(x)
1185 *f.toUint32Ptr() = &v
1186 return b, nil
1187}
1188
1189func unmarshalUint32Slice(b []byte, f pointer, w int) ([]byte, error) {
1190 if w == WireBytes { // packed
1191 x, n := decodeVarint(b)
1192 if n == 0 {
1193 return nil, io.ErrUnexpectedEOF
1194 }
1195 b = b[n:]
1196 if x > uint64(len(b)) {
1197 return nil, io.ErrUnexpectedEOF
1198 }
1199 res := b[x:]
1200 b = b[:x]
1201 for len(b) > 0 {
1202 x, n = decodeVarint(b)
1203 if n == 0 {
1204 return nil, io.ErrUnexpectedEOF
1205 }
1206 b = b[n:]
1207 v := uint32(x)
1208 s := f.toUint32Slice()
1209 *s = append(*s, v)
1210 }
1211 return res, nil
1212 }
1213 if w != WireVarint {
1214 return b, errInternalBadWireType
1215 }
1216 x, n := decodeVarint(b)
1217 if n == 0 {
1218 return nil, io.ErrUnexpectedEOF
1219 }
1220 b = b[n:]
1221 v := uint32(x)
1222 s := f.toUint32Slice()
1223 *s = append(*s, v)
1224 return b, nil
1225}
1226
1227func unmarshalFixed64Value(b []byte, f pointer, w int) ([]byte, error) {
1228 if w != WireFixed64 {
1229 return b, errInternalBadWireType
1230 }
1231 if len(b) < 8 {
1232 return nil, io.ErrUnexpectedEOF
1233 }
1234 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1235 *f.toUint64() = v
1236 return b[8:], nil
1237}
1238
1239func unmarshalFixed64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1240 if w != WireFixed64 {
1241 return b, errInternalBadWireType
1242 }
1243 if len(b) < 8 {
1244 return nil, io.ErrUnexpectedEOF
1245 }
1246 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1247 *f.toUint64Ptr() = &v
1248 return b[8:], nil
1249}
1250
1251func unmarshalFixed64Slice(b []byte, f pointer, w int) ([]byte, error) {
1252 if w == WireBytes { // packed
1253 x, n := decodeVarint(b)
1254 if n == 0 {
1255 return nil, io.ErrUnexpectedEOF
1256 }
1257 b = b[n:]
1258 if x > uint64(len(b)) {
1259 return nil, io.ErrUnexpectedEOF
1260 }
1261 res := b[x:]
1262 b = b[:x]
1263 for len(b) > 0 {
1264 if len(b) < 8 {
1265 return nil, io.ErrUnexpectedEOF
1266 }
1267 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1268 s := f.toUint64Slice()
1269 *s = append(*s, v)
1270 b = b[8:]
1271 }
1272 return res, nil
1273 }
1274 if w != WireFixed64 {
1275 return b, errInternalBadWireType
1276 }
1277 if len(b) < 8 {
1278 return nil, io.ErrUnexpectedEOF
1279 }
1280 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1281 s := f.toUint64Slice()
1282 *s = append(*s, v)
1283 return b[8:], nil
1284}
1285
1286func unmarshalFixedS64Value(b []byte, f pointer, w int) ([]byte, error) {
1287 if w != WireFixed64 {
1288 return b, errInternalBadWireType
1289 }
1290 if len(b) < 8 {
1291 return nil, io.ErrUnexpectedEOF
1292 }
1293 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1294 *f.toInt64() = v
1295 return b[8:], nil
1296}
1297
1298func unmarshalFixedS64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1299 if w != WireFixed64 {
1300 return b, errInternalBadWireType
1301 }
1302 if len(b) < 8 {
1303 return nil, io.ErrUnexpectedEOF
1304 }
1305 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1306 *f.toInt64Ptr() = &v
1307 return b[8:], nil
1308}
1309
1310func unmarshalFixedS64Slice(b []byte, f pointer, w int) ([]byte, error) {
1311 if w == WireBytes { // packed
1312 x, n := decodeVarint(b)
1313 if n == 0 {
1314 return nil, io.ErrUnexpectedEOF
1315 }
1316 b = b[n:]
1317 if x > uint64(len(b)) {
1318 return nil, io.ErrUnexpectedEOF
1319 }
1320 res := b[x:]
1321 b = b[:x]
1322 for len(b) > 0 {
1323 if len(b) < 8 {
1324 return nil, io.ErrUnexpectedEOF
1325 }
1326 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1327 s := f.toInt64Slice()
1328 *s = append(*s, v)
1329 b = b[8:]
1330 }
1331 return res, nil
1332 }
1333 if w != WireFixed64 {
1334 return b, errInternalBadWireType
1335 }
1336 if len(b) < 8 {
1337 return nil, io.ErrUnexpectedEOF
1338 }
1339 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1340 s := f.toInt64Slice()
1341 *s = append(*s, v)
1342 return b[8:], nil
1343}
1344
1345func unmarshalFixed32Value(b []byte, f pointer, w int) ([]byte, error) {
1346 if w != WireFixed32 {
1347 return b, errInternalBadWireType
1348 }
1349 if len(b) < 4 {
1350 return nil, io.ErrUnexpectedEOF
1351 }
1352 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1353 *f.toUint32() = v
1354 return b[4:], nil
1355}
1356
1357func unmarshalFixed32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1358 if w != WireFixed32 {
1359 return b, errInternalBadWireType
1360 }
1361 if len(b) < 4 {
1362 return nil, io.ErrUnexpectedEOF
1363 }
1364 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1365 *f.toUint32Ptr() = &v
1366 return b[4:], nil
1367}
1368
1369func unmarshalFixed32Slice(b []byte, f pointer, w int) ([]byte, error) {
1370 if w == WireBytes { // packed
1371 x, n := decodeVarint(b)
1372 if n == 0 {
1373 return nil, io.ErrUnexpectedEOF
1374 }
1375 b = b[n:]
1376 if x > uint64(len(b)) {
1377 return nil, io.ErrUnexpectedEOF
1378 }
1379 res := b[x:]
1380 b = b[:x]
1381 for len(b) > 0 {
1382 if len(b) < 4 {
1383 return nil, io.ErrUnexpectedEOF
1384 }
1385 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1386 s := f.toUint32Slice()
1387 *s = append(*s, v)
1388 b = b[4:]
1389 }
1390 return res, nil
1391 }
1392 if w != WireFixed32 {
1393 return b, errInternalBadWireType
1394 }
1395 if len(b) < 4 {
1396 return nil, io.ErrUnexpectedEOF
1397 }
1398 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1399 s := f.toUint32Slice()
1400 *s = append(*s, v)
1401 return b[4:], nil
1402}
1403
1404func unmarshalFixedS32Value(b []byte, f pointer, w int) ([]byte, error) {
1405 if w != WireFixed32 {
1406 return b, errInternalBadWireType
1407 }
1408 if len(b) < 4 {
1409 return nil, io.ErrUnexpectedEOF
1410 }
1411 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1412 *f.toInt32() = v
1413 return b[4:], nil
1414}
1415
1416func unmarshalFixedS32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1417 if w != WireFixed32 {
1418 return b, errInternalBadWireType
1419 }
1420 if len(b) < 4 {
1421 return nil, io.ErrUnexpectedEOF
1422 }
1423 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1424 f.setInt32Ptr(v)
1425 return b[4:], nil
1426}
1427
1428func unmarshalFixedS32Slice(b []byte, f pointer, w int) ([]byte, error) {
1429 if w == WireBytes { // packed
1430 x, n := decodeVarint(b)
1431 if n == 0 {
1432 return nil, io.ErrUnexpectedEOF
1433 }
1434 b = b[n:]
1435 if x > uint64(len(b)) {
1436 return nil, io.ErrUnexpectedEOF
1437 }
1438 res := b[x:]
1439 b = b[:x]
1440 for len(b) > 0 {
1441 if len(b) < 4 {
1442 return nil, io.ErrUnexpectedEOF
1443 }
1444 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1445 f.appendInt32Slice(v)
1446 b = b[4:]
1447 }
1448 return res, nil
1449 }
1450 if w != WireFixed32 {
1451 return b, errInternalBadWireType
1452 }
1453 if len(b) < 4 {
1454 return nil, io.ErrUnexpectedEOF
1455 }
1456 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1457 f.appendInt32Slice(v)
1458 return b[4:], nil
1459}
1460
1461func unmarshalBoolValue(b []byte, f pointer, w int) ([]byte, error) {
1462 if w != WireVarint {
1463 return b, errInternalBadWireType
1464 }
1465 // Note: any length varint is allowed, even though any sane
1466 // encoder will use one byte.
1467 // See https://github.com/golang/protobuf/issues/76
1468 x, n := decodeVarint(b)
1469 if n == 0 {
1470 return nil, io.ErrUnexpectedEOF
1471 }
1472 // TODO: check if x>1? Tests seem to indicate no.
1473 v := x != 0
1474 *f.toBool() = v
1475 return b[n:], nil
1476}
1477
1478func unmarshalBoolPtr(b []byte, f pointer, w int) ([]byte, error) {
1479 if w != WireVarint {
1480 return b, errInternalBadWireType
1481 }
1482 x, n := decodeVarint(b)
1483 if n == 0 {
1484 return nil, io.ErrUnexpectedEOF
1485 }
1486 v := x != 0
1487 *f.toBoolPtr() = &v
1488 return b[n:], nil
1489}
1490
1491func unmarshalBoolSlice(b []byte, f pointer, w int) ([]byte, error) {
1492 if w == WireBytes { // packed
1493 x, n := decodeVarint(b)
1494 if n == 0 {
1495 return nil, io.ErrUnexpectedEOF
1496 }
1497 b = b[n:]
1498 if x > uint64(len(b)) {
1499 return nil, io.ErrUnexpectedEOF
1500 }
1501 res := b[x:]
1502 b = b[:x]
1503 for len(b) > 0 {
1504 x, n = decodeVarint(b)
1505 if n == 0 {
1506 return nil, io.ErrUnexpectedEOF
1507 }
1508 v := x != 0
1509 s := f.toBoolSlice()
1510 *s = append(*s, v)
1511 b = b[n:]
1512 }
1513 return res, nil
1514 }
1515 if w != WireVarint {
1516 return b, errInternalBadWireType
1517 }
1518 x, n := decodeVarint(b)
1519 if n == 0 {
1520 return nil, io.ErrUnexpectedEOF
1521 }
1522 v := x != 0
1523 s := f.toBoolSlice()
1524 *s = append(*s, v)
1525 return b[n:], nil
1526}
1527
1528func unmarshalFloat64Value(b []byte, f pointer, w int) ([]byte, error) {
1529 if w != WireFixed64 {
1530 return b, errInternalBadWireType
1531 }
1532 if len(b) < 8 {
1533 return nil, io.ErrUnexpectedEOF
1534 }
1535 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1536 *f.toFloat64() = v
1537 return b[8:], nil
1538}
1539
1540func unmarshalFloat64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1541 if w != WireFixed64 {
1542 return b, errInternalBadWireType
1543 }
1544 if len(b) < 8 {
1545 return nil, io.ErrUnexpectedEOF
1546 }
1547 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1548 *f.toFloat64Ptr() = &v
1549 return b[8:], nil
1550}
1551
1552func unmarshalFloat64Slice(b []byte, f pointer, w int) ([]byte, error) {
1553 if w == WireBytes { // packed
1554 x, n := decodeVarint(b)
1555 if n == 0 {
1556 return nil, io.ErrUnexpectedEOF
1557 }
1558 b = b[n:]
1559 if x > uint64(len(b)) {
1560 return nil, io.ErrUnexpectedEOF
1561 }
1562 res := b[x:]
1563 b = b[:x]
1564 for len(b) > 0 {
1565 if len(b) < 8 {
1566 return nil, io.ErrUnexpectedEOF
1567 }
1568 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1569 s := f.toFloat64Slice()
1570 *s = append(*s, v)
1571 b = b[8:]
1572 }
1573 return res, nil
1574 }
1575 if w != WireFixed64 {
1576 return b, errInternalBadWireType
1577 }
1578 if len(b) < 8 {
1579 return nil, io.ErrUnexpectedEOF
1580 }
1581 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1582 s := f.toFloat64Slice()
1583 *s = append(*s, v)
1584 return b[8:], nil
1585}
1586
1587func unmarshalFloat32Value(b []byte, f pointer, w int) ([]byte, error) {
1588 if w != WireFixed32 {
1589 return b, errInternalBadWireType
1590 }
1591 if len(b) < 4 {
1592 return nil, io.ErrUnexpectedEOF
1593 }
1594 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1595 *f.toFloat32() = v
1596 return b[4:], nil
1597}
1598
1599func unmarshalFloat32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1600 if w != WireFixed32 {
1601 return b, errInternalBadWireType
1602 }
1603 if len(b) < 4 {
1604 return nil, io.ErrUnexpectedEOF
1605 }
1606 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1607 *f.toFloat32Ptr() = &v
1608 return b[4:], nil
1609}
1610
1611func unmarshalFloat32Slice(b []byte, f pointer, w int) ([]byte, error) {
1612 if w == WireBytes { // packed
1613 x, n := decodeVarint(b)
1614 if n == 0 {
1615 return nil, io.ErrUnexpectedEOF
1616 }
1617 b = b[n:]
1618 if x > uint64(len(b)) {
1619 return nil, io.ErrUnexpectedEOF
1620 }
1621 res := b[x:]
1622 b = b[:x]
1623 for len(b) > 0 {
1624 if len(b) < 4 {
1625 return nil, io.ErrUnexpectedEOF
1626 }
1627 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1628 s := f.toFloat32Slice()
1629 *s = append(*s, v)
1630 b = b[4:]
1631 }
1632 return res, nil
1633 }
1634 if w != WireFixed32 {
1635 return b, errInternalBadWireType
1636 }
1637 if len(b) < 4 {
1638 return nil, io.ErrUnexpectedEOF
1639 }
1640 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1641 s := f.toFloat32Slice()
1642 *s = append(*s, v)
1643 return b[4:], nil
1644}
1645
1646func unmarshalStringValue(b []byte, f pointer, w int) ([]byte, error) {
1647 if w != WireBytes {
1648 return b, errInternalBadWireType
1649 }
1650 x, n := decodeVarint(b)
1651 if n == 0 {
1652 return nil, io.ErrUnexpectedEOF
1653 }
1654 b = b[n:]
1655 if x > uint64(len(b)) {
1656 return nil, io.ErrUnexpectedEOF
1657 }
1658 v := string(b[:x])
1659 *f.toString() = v
1660 return b[x:], nil
1661}
1662
1663func unmarshalStringPtr(b []byte, f pointer, w int) ([]byte, error) {
1664 if w != WireBytes {
1665 return b, errInternalBadWireType
1666 }
1667 x, n := decodeVarint(b)
1668 if n == 0 {
1669 return nil, io.ErrUnexpectedEOF
1670 }
1671 b = b[n:]
1672 if x > uint64(len(b)) {
1673 return nil, io.ErrUnexpectedEOF
1674 }
1675 v := string(b[:x])
1676 *f.toStringPtr() = &v
1677 return b[x:], nil
1678}
1679
1680func unmarshalStringSlice(b []byte, f pointer, w int) ([]byte, error) {
1681 if w != WireBytes {
1682 return b, errInternalBadWireType
1683 }
1684 x, n := decodeVarint(b)
1685 if n == 0 {
1686 return nil, io.ErrUnexpectedEOF
1687 }
1688 b = b[n:]
1689 if x > uint64(len(b)) {
1690 return nil, io.ErrUnexpectedEOF
1691 }
1692 v := string(b[:x])
1693 s := f.toStringSlice()
1694 *s = append(*s, v)
1695 return b[x:], nil
1696}
1697
1698func unmarshalUTF8StringValue(b []byte, f pointer, w int) ([]byte, error) {
1699 if w != WireBytes {
1700 return b, errInternalBadWireType
1701 }
1702 x, n := decodeVarint(b)
1703 if n == 0 {
1704 return nil, io.ErrUnexpectedEOF
1705 }
1706 b = b[n:]
1707 if x > uint64(len(b)) {
1708 return nil, io.ErrUnexpectedEOF
1709 }
1710 v := string(b[:x])
1711 *f.toString() = v
1712 if !utf8.ValidString(v) {
1713 return b[x:], errInvalidUTF8
1714 }
1715 return b[x:], nil
1716}
1717
1718func unmarshalUTF8StringPtr(b []byte, f pointer, w int) ([]byte, error) {
1719 if w != WireBytes {
1720 return b, errInternalBadWireType
1721 }
1722 x, n := decodeVarint(b)
1723 if n == 0 {
1724 return nil, io.ErrUnexpectedEOF
1725 }
1726 b = b[n:]
1727 if x > uint64(len(b)) {
1728 return nil, io.ErrUnexpectedEOF
1729 }
1730 v := string(b[:x])
1731 *f.toStringPtr() = &v
1732 if !utf8.ValidString(v) {
1733 return b[x:], errInvalidUTF8
1734 }
1735 return b[x:], nil
1736}
1737
1738func unmarshalUTF8StringSlice(b []byte, f pointer, w int) ([]byte, error) {
1739 if w != WireBytes {
1740 return b, errInternalBadWireType
1741 }
1742 x, n := decodeVarint(b)
1743 if n == 0 {
1744 return nil, io.ErrUnexpectedEOF
1745 }
1746 b = b[n:]
1747 if x > uint64(len(b)) {
1748 return nil, io.ErrUnexpectedEOF
1749 }
1750 v := string(b[:x])
1751 s := f.toStringSlice()
1752 *s = append(*s, v)
1753 if !utf8.ValidString(v) {
1754 return b[x:], errInvalidUTF8
1755 }
1756 return b[x:], nil
1757}
1758
1759var emptyBuf [0]byte
1760
1761func unmarshalBytesValue(b []byte, f pointer, w int) ([]byte, error) {
1762 if w != WireBytes {
1763 return b, errInternalBadWireType
1764 }
1765 x, n := decodeVarint(b)
1766 if n == 0 {
1767 return nil, io.ErrUnexpectedEOF
1768 }
1769 b = b[n:]
1770 if x > uint64(len(b)) {
1771 return nil, io.ErrUnexpectedEOF
1772 }
1773 // The use of append here is a trick which avoids the zeroing
1774 // that would be required if we used a make/copy pair.
1775 // We append to emptyBuf instead of nil because we want
1776 // a non-nil result even when the length is 0.
1777 v := append(emptyBuf[:], b[:x]...)
1778 *f.toBytes() = v
1779 return b[x:], nil
1780}
1781
1782func unmarshalBytesSlice(b []byte, f pointer, w int) ([]byte, error) {
1783 if w != WireBytes {
1784 return b, errInternalBadWireType
1785 }
1786 x, n := decodeVarint(b)
1787 if n == 0 {
1788 return nil, io.ErrUnexpectedEOF
1789 }
1790 b = b[n:]
1791 if x > uint64(len(b)) {
1792 return nil, io.ErrUnexpectedEOF
1793 }
1794 v := append(emptyBuf[:], b[:x]...)
1795 s := f.toBytesSlice()
1796 *s = append(*s, v)
1797 return b[x:], nil
1798}
1799
1800func makeUnmarshalMessagePtr(sub *unmarshalInfo, name string) unmarshaler {
1801 return func(b []byte, f pointer, w int) ([]byte, error) {
1802 if w != WireBytes {
1803 return b, errInternalBadWireType
1804 }
1805 x, n := decodeVarint(b)
1806 if n == 0 {
1807 return nil, io.ErrUnexpectedEOF
1808 }
1809 b = b[n:]
1810 if x > uint64(len(b)) {
1811 return nil, io.ErrUnexpectedEOF
1812 }
1813 // First read the message field to see if something is there.
1814 // The semantics of multiple submessages are weird. Instead of
1815 // the last one winning (as it is for all other fields), multiple
1816 // submessages are merged.
1817 v := f.getPointer()
1818 if v.isNil() {
1819 v = valToPointer(reflect.New(sub.typ))
1820 f.setPointer(v)
1821 }
1822 err := sub.unmarshal(v, b[:x])
1823 if err != nil {
1824 if r, ok := err.(*RequiredNotSetError); ok {
1825 r.field = name + "." + r.field
1826 } else {
1827 return nil, err
1828 }
1829 }
1830 return b[x:], err
1831 }
1832}
1833
1834func makeUnmarshalMessageSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
1835 return func(b []byte, f pointer, w int) ([]byte, error) {
1836 if w != WireBytes {
1837 return b, errInternalBadWireType
1838 }
1839 x, n := decodeVarint(b)
1840 if n == 0 {
1841 return nil, io.ErrUnexpectedEOF
1842 }
1843 b = b[n:]
1844 if x > uint64(len(b)) {
1845 return nil, io.ErrUnexpectedEOF
1846 }
1847 v := valToPointer(reflect.New(sub.typ))
1848 err := sub.unmarshal(v, b[:x])
1849 if err != nil {
1850 if r, ok := err.(*RequiredNotSetError); ok {
1851 r.field = name + "." + r.field
1852 } else {
1853 return nil, err
1854 }
1855 }
1856 f.appendPointer(v)
1857 return b[x:], err
1858 }
1859}
1860
1861func makeUnmarshalGroupPtr(sub *unmarshalInfo, name string) unmarshaler {
1862 return func(b []byte, f pointer, w int) ([]byte, error) {
1863 if w != WireStartGroup {
1864 return b, errInternalBadWireType
1865 }
1866 x, y := findEndGroup(b)
1867 if x < 0 {
1868 return nil, io.ErrUnexpectedEOF
1869 }
1870 v := f.getPointer()
1871 if v.isNil() {
1872 v = valToPointer(reflect.New(sub.typ))
1873 f.setPointer(v)
1874 }
1875 err := sub.unmarshal(v, b[:x])
1876 if err != nil {
1877 if r, ok := err.(*RequiredNotSetError); ok {
1878 r.field = name + "." + r.field
1879 } else {
1880 return nil, err
1881 }
1882 }
1883 return b[y:], err
1884 }
1885}
1886
1887func makeUnmarshalGroupSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
1888 return func(b []byte, f pointer, w int) ([]byte, error) {
1889 if w != WireStartGroup {
1890 return b, errInternalBadWireType
1891 }
1892 x, y := findEndGroup(b)
1893 if x < 0 {
1894 return nil, io.ErrUnexpectedEOF
1895 }
1896 v := valToPointer(reflect.New(sub.typ))
1897 err := sub.unmarshal(v, b[:x])
1898 if err != nil {
1899 if r, ok := err.(*RequiredNotSetError); ok {
1900 r.field = name + "." + r.field
1901 } else {
1902 return nil, err
1903 }
1904 }
1905 f.appendPointer(v)
1906 return b[y:], err
1907 }
1908}
1909
1910func makeUnmarshalMap(f *reflect.StructField) unmarshaler {
1911 t := f.Type
1912 kt := t.Key()
1913 vt := t.Elem()
1914 tagArray := strings.Split(f.Tag.Get("protobuf"), ",")
1915 valTags := strings.Split(f.Tag.Get("protobuf_val"), ",")
1916 for _, t := range tagArray {
1917 if strings.HasPrefix(t, "customtype=") {
1918 valTags = append(valTags, t)
1919 }
1920 if t == "stdtime" {
1921 valTags = append(valTags, t)
1922 }
1923 if t == "stdduration" {
1924 valTags = append(valTags, t)
1925 }
1926 if t == "wktptr" {
1927 valTags = append(valTags, t)
1928 }
1929 }
1930 unmarshalKey := typeUnmarshaler(kt, f.Tag.Get("protobuf_key"))
1931 unmarshalVal := typeUnmarshaler(vt, strings.Join(valTags, ","))
1932 return func(b []byte, f pointer, w int) ([]byte, error) {
1933 // The map entry is a submessage. Figure out how big it is.
1934 if w != WireBytes {
1935 return nil, fmt.Errorf("proto: bad wiretype for map field: got %d want %d", w, WireBytes)
1936 }
1937 x, n := decodeVarint(b)
1938 if n == 0 {
1939 return nil, io.ErrUnexpectedEOF
1940 }
1941 b = b[n:]
1942 if x > uint64(len(b)) {
1943 return nil, io.ErrUnexpectedEOF
1944 }
1945 r := b[x:] // unused data to return
1946 b = b[:x] // data for map entry
1947
1948 // Note: we could use #keys * #values ~= 200 functions
1949 // to do map decoding without reflection. Probably not worth it.
1950 // Maps will be somewhat slow. Oh well.
1951
1952 // Read key and value from data.
1953 var nerr nonFatal
1954 k := reflect.New(kt)
1955 v := reflect.New(vt)
1956 for len(b) > 0 {
1957 x, n := decodeVarint(b)
1958 if n == 0 {
1959 return nil, io.ErrUnexpectedEOF
1960 }
1961 wire := int(x) & 7
1962 b = b[n:]
1963
1964 var err error
1965 switch x >> 3 {
1966 case 1:
1967 b, err = unmarshalKey(b, valToPointer(k), wire)
1968 case 2:
1969 b, err = unmarshalVal(b, valToPointer(v), wire)
1970 default:
1971 err = errInternalBadWireType // skip unknown tag
1972 }
1973
1974 if nerr.Merge(err) {
1975 continue
1976 }
1977 if err != errInternalBadWireType {
1978 return nil, err
1979 }
1980
1981 // Skip past unknown fields.
1982 b, err = skipField(b, wire)
1983 if err != nil {
1984 return nil, err
1985 }
1986 }
1987
1988 // Get map, allocate if needed.
1989 m := f.asPointerTo(t).Elem() // an addressable map[K]T
1990 if m.IsNil() {
1991 m.Set(reflect.MakeMap(t))
1992 }
1993
1994 // Insert into map.
1995 m.SetMapIndex(k.Elem(), v.Elem())
1996
1997 return r, nerr.E
1998 }
1999}
2000
2001// makeUnmarshalOneof makes an unmarshaler for oneof fields.
2002// for:
2003// message Msg {
2004// oneof F {
2005// int64 X = 1;
2006// float64 Y = 2;
2007// }
2008// }
2009// typ is the type of the concrete entry for a oneof case (e.g. Msg_X).
2010// ityp is the interface type of the oneof field (e.g. isMsg_F).
2011// unmarshal is the unmarshaler for the base type of the oneof case (e.g. int64).
2012// Note that this function will be called once for each case in the oneof.
2013func makeUnmarshalOneof(typ, ityp reflect.Type, unmarshal unmarshaler) unmarshaler {
2014 sf := typ.Field(0)
2015 field0 := toField(&sf)
2016 return func(b []byte, f pointer, w int) ([]byte, error) {
2017 // Allocate holder for value.
2018 v := reflect.New(typ)
2019
2020 // Unmarshal data into holder.
2021 // We unmarshal into the first field of the holder object.
2022 var err error
2023 var nerr nonFatal
2024 b, err = unmarshal(b, valToPointer(v).offset(field0), w)
2025 if !nerr.Merge(err) {
2026 return nil, err
2027 }
2028
2029 // Write pointer to holder into target field.
2030 f.asPointerTo(ityp).Elem().Set(v)
2031
2032 return b, nerr.E
2033 }
2034}
2035
2036// Error used by decode internally.
2037var errInternalBadWireType = errors.New("proto: internal error: bad wiretype")
2038
2039// skipField skips past a field of type wire and returns the remaining bytes.
2040func skipField(b []byte, wire int) ([]byte, error) {
2041 switch wire {
2042 case WireVarint:
2043 _, k := decodeVarint(b)
2044 if k == 0 {
2045 return b, io.ErrUnexpectedEOF
2046 }
2047 b = b[k:]
2048 case WireFixed32:
2049 if len(b) < 4 {
2050 return b, io.ErrUnexpectedEOF
2051 }
2052 b = b[4:]
2053 case WireFixed64:
2054 if len(b) < 8 {
2055 return b, io.ErrUnexpectedEOF
2056 }
2057 b = b[8:]
2058 case WireBytes:
2059 m, k := decodeVarint(b)
2060 if k == 0 || uint64(len(b)-k) < m {
2061 return b, io.ErrUnexpectedEOF
2062 }
2063 b = b[uint64(k)+m:]
2064 case WireStartGroup:
2065 _, i := findEndGroup(b)
2066 if i == -1 {
2067 return b, io.ErrUnexpectedEOF
2068 }
2069 b = b[i:]
2070 default:
2071 return b, fmt.Errorf("proto: can't skip unknown wire type %d", wire)
2072 }
2073 return b, nil
2074}
2075
2076// findEndGroup finds the index of the next EndGroup tag.
2077// Groups may be nested, so the "next" EndGroup tag is the first
2078// unpaired EndGroup.
2079// findEndGroup returns the indexes of the start and end of the EndGroup tag.
2080// Returns (-1,-1) if it can't find one.
2081func findEndGroup(b []byte) (int, int) {
2082 depth := 1
2083 i := 0
2084 for {
2085 x, n := decodeVarint(b[i:])
2086 if n == 0 {
2087 return -1, -1
2088 }
2089 j := i
2090 i += n
2091 switch x & 7 {
2092 case WireVarint:
2093 _, k := decodeVarint(b[i:])
2094 if k == 0 {
2095 return -1, -1
2096 }
2097 i += k
2098 case WireFixed32:
2099 if len(b)-4 < i {
2100 return -1, -1
2101 }
2102 i += 4
2103 case WireFixed64:
2104 if len(b)-8 < i {
2105 return -1, -1
2106 }
2107 i += 8
2108 case WireBytes:
2109 m, k := decodeVarint(b[i:])
2110 if k == 0 {
2111 return -1, -1
2112 }
2113 i += k
2114 if uint64(len(b)-i) < m {
2115 return -1, -1
2116 }
2117 i += int(m)
2118 case WireStartGroup:
2119 depth++
2120 case WireEndGroup:
2121 depth--
2122 if depth == 0 {
2123 return j, i
2124 }
2125 default:
2126 return -1, -1
2127 }
2128 }
2129}
2130
2131// encodeVarint appends a varint-encoded integer to b and returns the result.
2132func encodeVarint(b []byte, x uint64) []byte {
2133 for x >= 1<<7 {
2134 b = append(b, byte(x&0x7f|0x80))
2135 x >>= 7
2136 }
2137 return append(b, byte(x))
2138}
2139
2140// decodeVarint reads a varint-encoded integer from b.
2141// Returns the decoded integer and the number of bytes read.
2142// If there is an error, it returns 0,0.
2143func decodeVarint(b []byte) (uint64, int) {
2144 var x, y uint64
2145 if len(b) <= 0 {
2146 goto bad
2147 }
2148 x = uint64(b[0])
2149 if x < 0x80 {
2150 return x, 1
2151 }
2152 x -= 0x80
2153
2154 if len(b) <= 1 {
2155 goto bad
2156 }
2157 y = uint64(b[1])
2158 x += y << 7
2159 if y < 0x80 {
2160 return x, 2
2161 }
2162 x -= 0x80 << 7
2163
2164 if len(b) <= 2 {
2165 goto bad
2166 }
2167 y = uint64(b[2])
2168 x += y << 14
2169 if y < 0x80 {
2170 return x, 3
2171 }
2172 x -= 0x80 << 14
2173
2174 if len(b) <= 3 {
2175 goto bad
2176 }
2177 y = uint64(b[3])
2178 x += y << 21
2179 if y < 0x80 {
2180 return x, 4
2181 }
2182 x -= 0x80 << 21
2183
2184 if len(b) <= 4 {
2185 goto bad
2186 }
2187 y = uint64(b[4])
2188 x += y << 28
2189 if y < 0x80 {
2190 return x, 5
2191 }
2192 x -= 0x80 << 28
2193
2194 if len(b) <= 5 {
2195 goto bad
2196 }
2197 y = uint64(b[5])
2198 x += y << 35
2199 if y < 0x80 {
2200 return x, 6
2201 }
2202 x -= 0x80 << 35
2203
2204 if len(b) <= 6 {
2205 goto bad
2206 }
2207 y = uint64(b[6])
2208 x += y << 42
2209 if y < 0x80 {
2210 return x, 7
2211 }
2212 x -= 0x80 << 42
2213
2214 if len(b) <= 7 {
2215 goto bad
2216 }
2217 y = uint64(b[7])
2218 x += y << 49
2219 if y < 0x80 {
2220 return x, 8
2221 }
2222 x -= 0x80 << 49
2223
2224 if len(b) <= 8 {
2225 goto bad
2226 }
2227 y = uint64(b[8])
2228 x += y << 56
2229 if y < 0x80 {
2230 return x, 9
2231 }
2232 x -= 0x80 << 56
2233
2234 if len(b) <= 9 {
2235 goto bad
2236 }
2237 y = uint64(b[9])
2238 x += y << 63
2239 if y < 2 {
2240 return x, 10
2241 }
2242
2243bad:
2244 return 0, 0
2245}