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