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