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