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