1 // Go support for Protocol Buffers - Google's data interchange format
3 // Copyright 2016 The Go Authors. All rights reserved.
4 // https://github.com/golang/protobuf
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
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
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.
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.
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)
58 // Slow path: find unmarshal info for msg, update a with it.
59 u = getUnmarshalInfo(reflect.TypeOf(msg).Elem())
60 atomicStoreUnmarshalInfo(&a.unmarshal, u)
62 // Then do the unmarshaling.
63 err := u.unmarshal(toPointer(&msg), b)
67 type unmarshalInfo struct {
68 typ reflect.Type // type of the protobuf struct
70 // 0 = only typ field is initialized
71 // 1 = completely initialized
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
84 bytesExtensions field // offset of XXX_extensions with type []byte
87 // An unmarshaler takes a stream of bytes and a pointer to a field of a message.
88 // It decodes the field, stores it at f, and returns the unused bytes.
89 // w is the wire encoding.
90 // b is the data after the tag and wire encoding have been read.
91 type unmarshaler func(b []byte, f pointer, w int) ([]byte, error)
93 type unmarshalFieldInfo struct {
94 // location of the field in the proto message structure.
97 // function to unmarshal the data for the field.
100 // if a required field, contains a single set bit at this field's index in the required field list.
103 name string // name of the field, for error reporting
107 unmarshalInfoMap = map[reflect.Type]*unmarshalInfo{}
108 unmarshalInfoLock sync.Mutex
111 // getUnmarshalInfo returns the data structure which can be
112 // subsequently used to unmarshal a message of the given type.
113 // t is the type of the message (note: not pointer to message).
114 func getUnmarshalInfo(t reflect.Type) *unmarshalInfo {
115 // It would be correct to return a new unmarshalInfo
116 // unconditionally. We would end up allocating one
117 // per occurrence of that type as a message or submessage.
118 // We use a cache here just to reduce memory usage.
119 unmarshalInfoLock.Lock()
120 defer unmarshalInfoLock.Unlock()
121 u := unmarshalInfoMap[t]
123 u = &unmarshalInfo{typ: t}
124 // Note: we just set the type here. The rest of the fields
125 // will be initialized on first use.
126 unmarshalInfoMap[t] = u
131 // unmarshal does the main work of unmarshaling a message.
132 // u provides type information used to unmarshal the message.
133 // m is a pointer to a protocol buffer message.
134 // b is a byte stream to unmarshal into m.
135 // This is top routine used when recursively unmarshaling submessages.
136 func (u *unmarshalInfo) unmarshal(m pointer, b []byte) error {
137 if atomic.LoadInt32(&u.initialized) == 0 {
138 u.computeUnmarshalInfo()
141 return unmarshalMessageSet(b, m.offset(u.extensions).toExtensions())
143 var reqMask uint64 // bitmask of required fields we've seen.
146 // Read tag and wire type.
147 // Special case 1 and 2 byte varints.
152 } else if len(b) >= 2 && b[1] < 128 {
153 x = uint64(b[0]&0x7f) + uint64(b[1])<<7
157 x, n = decodeVarint(b)
159 return io.ErrUnexpectedEOF
166 // Dispatch on the tag to one of the unmarshal* functions below.
167 var f unmarshalFieldInfo
168 if tag < uint64(len(u.dense)) {
173 if fn := f.unmarshal; fn != nil {
175 b, err = fn(b, m.offset(f.field), wire)
180 if r, ok := err.(*RequiredNotSetError); ok {
181 // Remember this error, but keep parsing. We need to produce
182 // a full parse even if a required field is missing.
189 if err != errInternalBadWireType {
190 if err == errInvalidUTF8 {
192 fullName := revProtoTypes[reflect.PtrTo(u.typ)] + "." + f.name
193 errLater = &invalidUTF8Error{fullName}
199 // Fragments with bad wire type are treated as unknown fields.
203 if !u.unrecognized.IsValid() {
204 // Don't keep unrecognized data; just skip it.
206 b, err = skipField(b, wire)
212 // Keep unrecognized data around.
213 // maybe in extensions, maybe in the unrecognized field.
214 z := m.offset(u.unrecognized).toBytes()
215 var emap map[int32]Extension
217 for _, r := range u.extensionRanges {
218 if uint64(r.Start) <= tag && tag <= uint64(r.End) {
219 if u.extensions.IsValid() {
220 mp := m.offset(u.extensions).toExtensions()
221 emap = mp.extensionsWrite()
226 if u.oldExtensions.IsValid() {
227 p := m.offset(u.oldExtensions).toOldExtensions()
230 emap = map[int32]Extension{}
237 if u.bytesExtensions.IsValid() {
238 z = m.offset(u.bytesExtensions).toBytes()
241 panic("no extensions field available")
244 // Use wire type to skip data.
247 b, err = skipField(b, wire)
251 *z = encodeVarint(*z, tag<<3|uint64(wire))
252 *z = append(*z, b0[:len(b0)-len(b)]...)
258 if reqMask != u.reqMask && errLater == nil {
259 // A required field of this message is missing.
260 for _, n := range u.reqFields {
262 errLater = &RequiredNotSetError{n}
270 // computeUnmarshalInfo fills in u with information for use
271 // in unmarshaling protocol buffers of type u.typ.
272 func (u *unmarshalInfo) computeUnmarshalInfo() {
274 defer u.lock.Unlock()
275 if u.initialized != 0 {
281 // Set up the "not found" value for the unrecognized byte buffer.
282 // This is the default for proto3.
283 u.unrecognized = invalidField
284 u.extensions = invalidField
285 u.oldExtensions = invalidField
286 u.bytesExtensions = invalidField
288 // List of the generated type and offset for each oneof field.
289 type oneofField struct {
290 ityp reflect.Type // interface type of oneof field
291 field field // offset in containing message
293 var oneofFields []oneofField
295 for i := 0; i < n; i++ {
297 if f.Name == "XXX_unrecognized" {
298 // The byte slice used to hold unrecognized input is special.
299 if f.Type != reflect.TypeOf(([]byte)(nil)) {
300 panic("bad type for XXX_unrecognized field: " + f.Type.Name())
302 u.unrecognized = toField(&f)
305 if f.Name == "XXX_InternalExtensions" {
307 if f.Type != reflect.TypeOf(XXX_InternalExtensions{}) {
308 panic("bad type for XXX_InternalExtensions field: " + f.Type.Name())
310 u.extensions = toField(&f)
311 if f.Tag.Get("protobuf_messageset") == "1" {
312 u.isMessageSet = true
316 if f.Name == "XXX_extensions" {
317 // An older form of the extensions field.
318 if f.Type == reflect.TypeOf((map[int32]Extension)(nil)) {
319 u.oldExtensions = toField(&f)
321 } else if f.Type == reflect.TypeOf(([]byte)(nil)) {
322 u.bytesExtensions = toField(&f)
325 panic("bad type for XXX_extensions field: " + f.Type.Name())
327 if f.Name == "XXX_NoUnkeyedLiteral" || f.Name == "XXX_sizecache" {
331 oneof := f.Tag.Get("protobuf_oneof")
333 oneofFields = append(oneofFields, oneofField{f.Type, toField(&f)})
334 // The rest of oneof processing happens below.
338 tags := f.Tag.Get("protobuf")
339 tagArray := strings.Split(tags, ",")
340 if len(tagArray) < 2 {
341 panic("protobuf tag not enough fields in " + t.Name() + "." + f.Name + ": " + tags)
343 tag, err := strconv.Atoi(tagArray[1])
345 panic("protobuf tag field not an integer: " + tagArray[1])
349 for _, tag := range tagArray[3:] {
350 if strings.HasPrefix(tag, "name=") {
355 // Extract unmarshaling function from the field (its type and tags).
356 unmarshal := fieldUnmarshaler(&f)
360 if tagArray[2] == "req" {
361 bit := len(u.reqFields)
362 u.reqFields = append(u.reqFields, name)
363 reqMask = uint64(1) << uint(bit)
364 // TODO: if we have more than 64 required fields, we end up
365 // not verifying that all required fields are present.
366 // Fix this, perhaps using a count of required fields?
369 // Store the info in the correct slot in the message.
370 u.setTag(tag, toField(&f), unmarshal, reqMask, name)
373 // Find any types associated with oneof fields.
374 // TODO: XXX_OneofFuncs returns more info than we need. Get rid of some of it?
375 fn := reflect.Zero(reflect.PtrTo(t)).MethodByName("XXX_OneofFuncs")
376 // gogo: len(oneofFields) > 0 is needed for embedded oneof messages, without a marshaler and unmarshaler
377 if fn.IsValid() && len(oneofFields) > 0 {
378 res := fn.Call(nil)[3] // last return value from XXX_OneofFuncs: []interface{}
379 for i := res.Len() - 1; i >= 0; i-- {
380 v := res.Index(i) // interface{}
381 tptr := reflect.ValueOf(v.Interface()).Type() // *Msg_X
382 typ := tptr.Elem() // Msg_X
384 f := typ.Field(0) // oneof implementers have one field
385 baseUnmarshal := fieldUnmarshaler(&f)
386 tags := strings.Split(f.Tag.Get("protobuf"), ",")
387 fieldNum, err := strconv.Atoi(tags[1])
389 panic("protobuf tag field not an integer: " + tags[1])
392 for _, tag := range tags {
393 if strings.HasPrefix(tag, "name=") {
394 name = strings.TrimPrefix(tag, "name=")
399 // Find the oneof field that this struct implements.
400 // Might take O(n^2) to process all of the oneofs, but who cares.
401 for _, of := range oneofFields {
402 if tptr.Implements(of.ityp) {
403 // We have found the corresponding interface for this struct.
404 // That lets us know where this struct should be stored
405 // when we encounter it during unmarshaling.
406 unmarshal := makeUnmarshalOneof(typ, of.ityp, baseUnmarshal)
407 u.setTag(fieldNum, of.field, unmarshal, 0, name)
413 // Get extension ranges, if any.
414 fn = reflect.Zero(reflect.PtrTo(t)).MethodByName("ExtensionRangeArray")
416 if !u.extensions.IsValid() && !u.oldExtensions.IsValid() && !u.bytesExtensions.IsValid() {
417 panic("a message with extensions, but no extensions field in " + t.Name())
419 u.extensionRanges = fn.Call(nil)[0].Interface().([]ExtensionRange)
422 // Explicitly disallow tag 0. This will ensure we flag an error
423 // when decoding a buffer of all zeros. Without this code, we
424 // would decode and skip an all-zero buffer of even length.
425 // [0 0] is [tag=0/wiretype=varint varint-encoded-0].
426 u.setTag(0, zeroField, func(b []byte, f pointer, w int) ([]byte, error) {
427 return nil, fmt.Errorf("proto: %s: illegal tag 0 (wire type %d)", t, w)
430 // Set mask for required field check.
431 u.reqMask = uint64(1)<<uint(len(u.reqFields)) - 1
433 atomic.StoreInt32(&u.initialized, 1)
436 // setTag stores the unmarshal information for the given tag.
437 // tag = tag # for field
438 // field/unmarshal = unmarshal info for that field.
439 // reqMask = if required, bitmask for field position in required field list. 0 otherwise.
440 // name = short name of the field.
441 func (u *unmarshalInfo) setTag(tag int, field field, unmarshal unmarshaler, reqMask uint64, name string) {
442 i := unmarshalFieldInfo{field: field, unmarshal: unmarshal, reqMask: reqMask, name: name}
443 n := u.typ.NumField()
444 if tag >= 0 && (tag < 16 || tag < 2*n) { // TODO: what are the right numbers here?
445 for len(u.dense) <= tag {
446 u.dense = append(u.dense, unmarshalFieldInfo{})
452 u.sparse = map[uint64]unmarshalFieldInfo{}
454 u.sparse[uint64(tag)] = i
457 // fieldUnmarshaler returns an unmarshaler for the given field.
458 func fieldUnmarshaler(f *reflect.StructField) unmarshaler {
459 if f.Type.Kind() == reflect.Map {
460 return makeUnmarshalMap(f)
462 return typeUnmarshaler(f.Type, f.Tag.Get("protobuf"))
465 // typeUnmarshaler returns an unmarshaler for the given field type / field tag pair.
466 func typeUnmarshaler(t reflect.Type, tags string) unmarshaler {
467 tagArray := strings.Split(tags, ",")
468 encoding := tagArray[0]
473 isWktPointer := false
476 for _, tag := range tagArray[3:] {
477 if strings.HasPrefix(tag, "name=") {
483 if strings.HasPrefix(tag, "customtype=") {
486 if tag == "stdtime" {
489 if tag == "stdduration" {
496 validateUTF8 = validateUTF8 && proto3
498 // Figure out packaging (pointer, slice, or both)
501 if t.Kind() == reflect.Slice && t.Elem().Kind() != reflect.Uint8 {
505 if t.Kind() == reflect.Ptr {
511 if reflect.PtrTo(t).Implements(customType) {
513 return makeUnmarshalCustomSlice(getUnmarshalInfo(t), name)
516 return makeUnmarshalCustomPtr(getUnmarshalInfo(t), name)
518 return makeUnmarshalCustom(getUnmarshalInfo(t), name)
520 panic(fmt.Sprintf("custom type: type: %v, does not implement the proto.custom interface", t))
527 return makeUnmarshalTimePtrSlice(getUnmarshalInfo(t), name)
529 return makeUnmarshalTimePtr(getUnmarshalInfo(t), name)
532 return makeUnmarshalTimeSlice(getUnmarshalInfo(t), name)
534 return makeUnmarshalTime(getUnmarshalInfo(t), name)
540 return makeUnmarshalDurationPtrSlice(getUnmarshalInfo(t), name)
542 return makeUnmarshalDurationPtr(getUnmarshalInfo(t), name)
545 return makeUnmarshalDurationSlice(getUnmarshalInfo(t), name)
547 return makeUnmarshalDuration(getUnmarshalInfo(t), name)
552 case reflect.Float64:
555 return makeStdDoubleValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
557 return makeStdDoubleValuePtrUnmarshaler(getUnmarshalInfo(t), name)
560 return makeStdDoubleValueSliceUnmarshaler(getUnmarshalInfo(t), name)
562 return makeStdDoubleValueUnmarshaler(getUnmarshalInfo(t), name)
563 case reflect.Float32:
566 return makeStdFloatValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
568 return makeStdFloatValuePtrUnmarshaler(getUnmarshalInfo(t), name)
571 return makeStdFloatValueSliceUnmarshaler(getUnmarshalInfo(t), name)
573 return makeStdFloatValueUnmarshaler(getUnmarshalInfo(t), name)
577 return makeStdInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
579 return makeStdInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
582 return makeStdInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
584 return makeStdInt64ValueUnmarshaler(getUnmarshalInfo(t), name)
588 return makeStdUInt64ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
590 return makeStdUInt64ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
593 return makeStdUInt64ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
595 return makeStdUInt64ValueUnmarshaler(getUnmarshalInfo(t), name)
599 return makeStdInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
601 return makeStdInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
604 return makeStdInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
606 return makeStdInt32ValueUnmarshaler(getUnmarshalInfo(t), name)
610 return makeStdUInt32ValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
612 return makeStdUInt32ValuePtrUnmarshaler(getUnmarshalInfo(t), name)
615 return makeStdUInt32ValueSliceUnmarshaler(getUnmarshalInfo(t), name)
617 return makeStdUInt32ValueUnmarshaler(getUnmarshalInfo(t), name)
621 return makeStdBoolValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
623 return makeStdBoolValuePtrUnmarshaler(getUnmarshalInfo(t), name)
626 return makeStdBoolValueSliceUnmarshaler(getUnmarshalInfo(t), name)
628 return makeStdBoolValueUnmarshaler(getUnmarshalInfo(t), name)
632 return makeStdStringValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
634 return makeStdStringValuePtrUnmarshaler(getUnmarshalInfo(t), name)
637 return makeStdStringValueSliceUnmarshaler(getUnmarshalInfo(t), name)
639 return makeStdStringValueUnmarshaler(getUnmarshalInfo(t), name)
643 return makeStdBytesValuePtrSliceUnmarshaler(getUnmarshalInfo(t), name)
645 return makeStdBytesValuePtrUnmarshaler(getUnmarshalInfo(t), name)
648 return makeStdBytesValueSliceUnmarshaler(getUnmarshalInfo(t), name)
650 return makeStdBytesValueUnmarshaler(getUnmarshalInfo(t), name)
652 panic(fmt.Sprintf("unknown wktpointer type %#v", t))
656 // We'll never have both pointer and slice for basic types.
657 if pointer && slice && t.Kind() != reflect.Struct {
658 panic("both pointer and slice for basic type in " + t.Name())
664 return unmarshalBoolPtr
667 return unmarshalBoolSlice
669 return unmarshalBoolValue
674 return unmarshalFixedS32Ptr
677 return unmarshalFixedS32Slice
679 return unmarshalFixedS32Value
681 // this could be int32 or enum
683 return unmarshalInt32Ptr
686 return unmarshalInt32Slice
688 return unmarshalInt32Value
691 return unmarshalSint32Ptr
694 return unmarshalSint32Slice
696 return unmarshalSint32Value
702 return unmarshalFixedS64Ptr
705 return unmarshalFixedS64Slice
707 return unmarshalFixedS64Value
710 return unmarshalInt64Ptr
713 return unmarshalInt64Slice
715 return unmarshalInt64Value
718 return unmarshalSint64Ptr
721 return unmarshalSint64Slice
723 return unmarshalSint64Value
729 return unmarshalFixed32Ptr
732 return unmarshalFixed32Slice
734 return unmarshalFixed32Value
737 return unmarshalUint32Ptr
740 return unmarshalUint32Slice
742 return unmarshalUint32Value
748 return unmarshalFixed64Ptr
751 return unmarshalFixed64Slice
753 return unmarshalFixed64Value
756 return unmarshalUint64Ptr
759 return unmarshalUint64Slice
761 return unmarshalUint64Value
763 case reflect.Float32:
765 return unmarshalFloat32Ptr
768 return unmarshalFloat32Slice
770 return unmarshalFloat32Value
771 case reflect.Float64:
773 return unmarshalFloat64Ptr
776 return unmarshalFloat64Slice
778 return unmarshalFloat64Value
780 panic("map type in typeUnmarshaler in " + t.Name())
783 panic("bad pointer in slice case in " + t.Name())
786 return unmarshalBytesSlice
788 return unmarshalBytesValue
792 return unmarshalUTF8StringPtr
795 return unmarshalUTF8StringSlice
797 return unmarshalUTF8StringValue
800 return unmarshalStringPtr
803 return unmarshalStringSlice
805 return unmarshalStringValue
807 // message or group field
812 return makeUnmarshalMessageSlice(getUnmarshalInfo(t), name)
814 return makeUnmarshalMessage(getUnmarshalInfo(t), name)
820 return makeUnmarshalMessageSlicePtr(getUnmarshalInfo(t), name)
822 return makeUnmarshalMessagePtr(getUnmarshalInfo(t), name)
825 return makeUnmarshalGroupSlicePtr(getUnmarshalInfo(t), name)
827 return makeUnmarshalGroupPtr(getUnmarshalInfo(t), name)
830 panic(fmt.Sprintf("unmarshaler not found type:%s encoding:%s", t, encoding))
833 // Below are all the unmarshalers for individual fields of various types.
835 func unmarshalInt64Value(b []byte, f pointer, w int) ([]byte, error) {
837 return b, errInternalBadWireType
839 x, n := decodeVarint(b)
841 return nil, io.ErrUnexpectedEOF
849 func unmarshalInt64Ptr(b []byte, f pointer, w int) ([]byte, error) {
851 return b, errInternalBadWireType
853 x, n := decodeVarint(b)
855 return nil, io.ErrUnexpectedEOF
863 func unmarshalInt64Slice(b []byte, f pointer, w int) ([]byte, error) {
864 if w == WireBytes { // packed
865 x, n := decodeVarint(b)
867 return nil, io.ErrUnexpectedEOF
870 if x > uint64(len(b)) {
871 return nil, io.ErrUnexpectedEOF
876 x, n = decodeVarint(b)
878 return nil, io.ErrUnexpectedEOF
882 s := f.toInt64Slice()
888 return b, errInternalBadWireType
890 x, n := decodeVarint(b)
892 return nil, io.ErrUnexpectedEOF
896 s := f.toInt64Slice()
901 func unmarshalSint64Value(b []byte, f pointer, w int) ([]byte, error) {
903 return b, errInternalBadWireType
905 x, n := decodeVarint(b)
907 return nil, io.ErrUnexpectedEOF
910 v := int64(x>>1) ^ int64(x)<<63>>63
915 func unmarshalSint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
917 return b, errInternalBadWireType
919 x, n := decodeVarint(b)
921 return nil, io.ErrUnexpectedEOF
924 v := int64(x>>1) ^ int64(x)<<63>>63
929 func unmarshalSint64Slice(b []byte, f pointer, w int) ([]byte, error) {
930 if w == WireBytes { // packed
931 x, n := decodeVarint(b)
933 return nil, io.ErrUnexpectedEOF
936 if x > uint64(len(b)) {
937 return nil, io.ErrUnexpectedEOF
942 x, n = decodeVarint(b)
944 return nil, io.ErrUnexpectedEOF
947 v := int64(x>>1) ^ int64(x)<<63>>63
948 s := f.toInt64Slice()
954 return b, errInternalBadWireType
956 x, n := decodeVarint(b)
958 return nil, io.ErrUnexpectedEOF
961 v := int64(x>>1) ^ int64(x)<<63>>63
962 s := f.toInt64Slice()
967 func unmarshalUint64Value(b []byte, f pointer, w int) ([]byte, error) {
969 return b, errInternalBadWireType
971 x, n := decodeVarint(b)
973 return nil, io.ErrUnexpectedEOF
981 func unmarshalUint64Ptr(b []byte, f pointer, w int) ([]byte, error) {
983 return b, errInternalBadWireType
985 x, n := decodeVarint(b)
987 return nil, io.ErrUnexpectedEOF
991 *f.toUint64Ptr() = &v
995 func unmarshalUint64Slice(b []byte, f pointer, w int) ([]byte, error) {
996 if w == WireBytes { // packed
997 x, n := decodeVarint(b)
999 return nil, io.ErrUnexpectedEOF
1002 if x > uint64(len(b)) {
1003 return nil, io.ErrUnexpectedEOF
1008 x, n = decodeVarint(b)
1010 return nil, io.ErrUnexpectedEOF
1014 s := f.toUint64Slice()
1019 if w != WireVarint {
1020 return b, errInternalBadWireType
1022 x, n := decodeVarint(b)
1024 return nil, io.ErrUnexpectedEOF
1028 s := f.toUint64Slice()
1033 func unmarshalInt32Value(b []byte, f pointer, w int) ([]byte, error) {
1034 if w != WireVarint {
1035 return b, errInternalBadWireType
1037 x, n := decodeVarint(b)
1039 return nil, io.ErrUnexpectedEOF
1047 func unmarshalInt32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1048 if w != WireVarint {
1049 return b, errInternalBadWireType
1051 x, n := decodeVarint(b)
1053 return nil, io.ErrUnexpectedEOF
1061 func unmarshalInt32Slice(b []byte, f pointer, w int) ([]byte, error) {
1062 if w == WireBytes { // packed
1063 x, n := decodeVarint(b)
1065 return nil, io.ErrUnexpectedEOF
1068 if x > uint64(len(b)) {
1069 return nil, io.ErrUnexpectedEOF
1074 x, n = decodeVarint(b)
1076 return nil, io.ErrUnexpectedEOF
1080 f.appendInt32Slice(v)
1084 if w != WireVarint {
1085 return b, errInternalBadWireType
1087 x, n := decodeVarint(b)
1089 return nil, io.ErrUnexpectedEOF
1093 f.appendInt32Slice(v)
1097 func unmarshalSint32Value(b []byte, f pointer, w int) ([]byte, error) {
1098 if w != WireVarint {
1099 return b, errInternalBadWireType
1101 x, n := decodeVarint(b)
1103 return nil, io.ErrUnexpectedEOF
1106 v := int32(x>>1) ^ int32(x)<<31>>31
1111 func unmarshalSint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1112 if w != WireVarint {
1113 return b, errInternalBadWireType
1115 x, n := decodeVarint(b)
1117 return nil, io.ErrUnexpectedEOF
1120 v := int32(x>>1) ^ int32(x)<<31>>31
1125 func unmarshalSint32Slice(b []byte, f pointer, w int) ([]byte, error) {
1126 if w == WireBytes { // packed
1127 x, n := decodeVarint(b)
1129 return nil, io.ErrUnexpectedEOF
1132 if x > uint64(len(b)) {
1133 return nil, io.ErrUnexpectedEOF
1138 x, n = decodeVarint(b)
1140 return nil, io.ErrUnexpectedEOF
1143 v := int32(x>>1) ^ int32(x)<<31>>31
1144 f.appendInt32Slice(v)
1148 if w != WireVarint {
1149 return b, errInternalBadWireType
1151 x, n := decodeVarint(b)
1153 return nil, io.ErrUnexpectedEOF
1156 v := int32(x>>1) ^ int32(x)<<31>>31
1157 f.appendInt32Slice(v)
1161 func unmarshalUint32Value(b []byte, f pointer, w int) ([]byte, error) {
1162 if w != WireVarint {
1163 return b, errInternalBadWireType
1165 x, n := decodeVarint(b)
1167 return nil, io.ErrUnexpectedEOF
1175 func unmarshalUint32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1176 if w != WireVarint {
1177 return b, errInternalBadWireType
1179 x, n := decodeVarint(b)
1181 return nil, io.ErrUnexpectedEOF
1185 *f.toUint32Ptr() = &v
1189 func unmarshalUint32Slice(b []byte, f pointer, w int) ([]byte, error) {
1190 if w == WireBytes { // packed
1191 x, n := decodeVarint(b)
1193 return nil, io.ErrUnexpectedEOF
1196 if x > uint64(len(b)) {
1197 return nil, io.ErrUnexpectedEOF
1202 x, n = decodeVarint(b)
1204 return nil, io.ErrUnexpectedEOF
1208 s := f.toUint32Slice()
1213 if w != WireVarint {
1214 return b, errInternalBadWireType
1216 x, n := decodeVarint(b)
1218 return nil, io.ErrUnexpectedEOF
1222 s := f.toUint32Slice()
1227 func unmarshalFixed64Value(b []byte, f pointer, w int) ([]byte, error) {
1228 if w != WireFixed64 {
1229 return b, errInternalBadWireType
1232 return nil, io.ErrUnexpectedEOF
1234 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1239 func unmarshalFixed64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1240 if w != WireFixed64 {
1241 return b, errInternalBadWireType
1244 return nil, io.ErrUnexpectedEOF
1246 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1247 *f.toUint64Ptr() = &v
1251 func unmarshalFixed64Slice(b []byte, f pointer, w int) ([]byte, error) {
1252 if w == WireBytes { // packed
1253 x, n := decodeVarint(b)
1255 return nil, io.ErrUnexpectedEOF
1258 if x > uint64(len(b)) {
1259 return nil, io.ErrUnexpectedEOF
1265 return nil, io.ErrUnexpectedEOF
1267 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1268 s := f.toUint64Slice()
1274 if w != WireFixed64 {
1275 return b, errInternalBadWireType
1278 return nil, io.ErrUnexpectedEOF
1280 v := uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56
1281 s := f.toUint64Slice()
1286 func unmarshalFixedS64Value(b []byte, f pointer, w int) ([]byte, error) {
1287 if w != WireFixed64 {
1288 return b, errInternalBadWireType
1291 return nil, io.ErrUnexpectedEOF
1293 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1298 func unmarshalFixedS64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1299 if w != WireFixed64 {
1300 return b, errInternalBadWireType
1303 return nil, io.ErrUnexpectedEOF
1305 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1306 *f.toInt64Ptr() = &v
1310 func unmarshalFixedS64Slice(b []byte, f pointer, w int) ([]byte, error) {
1311 if w == WireBytes { // packed
1312 x, n := decodeVarint(b)
1314 return nil, io.ErrUnexpectedEOF
1317 if x > uint64(len(b)) {
1318 return nil, io.ErrUnexpectedEOF
1324 return nil, io.ErrUnexpectedEOF
1326 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1327 s := f.toInt64Slice()
1333 if w != WireFixed64 {
1334 return b, errInternalBadWireType
1337 return nil, io.ErrUnexpectedEOF
1339 v := int64(b[0]) | int64(b[1])<<8 | int64(b[2])<<16 | int64(b[3])<<24 | int64(b[4])<<32 | int64(b[5])<<40 | int64(b[6])<<48 | int64(b[7])<<56
1340 s := f.toInt64Slice()
1345 func unmarshalFixed32Value(b []byte, f pointer, w int) ([]byte, error) {
1346 if w != WireFixed32 {
1347 return b, errInternalBadWireType
1350 return nil, io.ErrUnexpectedEOF
1352 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1357 func unmarshalFixed32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1358 if w != WireFixed32 {
1359 return b, errInternalBadWireType
1362 return nil, io.ErrUnexpectedEOF
1364 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1365 *f.toUint32Ptr() = &v
1369 func unmarshalFixed32Slice(b []byte, f pointer, w int) ([]byte, error) {
1370 if w == WireBytes { // packed
1371 x, n := decodeVarint(b)
1373 return nil, io.ErrUnexpectedEOF
1376 if x > uint64(len(b)) {
1377 return nil, io.ErrUnexpectedEOF
1383 return nil, io.ErrUnexpectedEOF
1385 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1386 s := f.toUint32Slice()
1392 if w != WireFixed32 {
1393 return b, errInternalBadWireType
1396 return nil, io.ErrUnexpectedEOF
1398 v := uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24
1399 s := f.toUint32Slice()
1404 func unmarshalFixedS32Value(b []byte, f pointer, w int) ([]byte, error) {
1405 if w != WireFixed32 {
1406 return b, errInternalBadWireType
1409 return nil, io.ErrUnexpectedEOF
1411 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1416 func unmarshalFixedS32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1417 if w != WireFixed32 {
1418 return b, errInternalBadWireType
1421 return nil, io.ErrUnexpectedEOF
1423 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1428 func unmarshalFixedS32Slice(b []byte, f pointer, w int) ([]byte, error) {
1429 if w == WireBytes { // packed
1430 x, n := decodeVarint(b)
1432 return nil, io.ErrUnexpectedEOF
1435 if x > uint64(len(b)) {
1436 return nil, io.ErrUnexpectedEOF
1442 return nil, io.ErrUnexpectedEOF
1444 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1445 f.appendInt32Slice(v)
1450 if w != WireFixed32 {
1451 return b, errInternalBadWireType
1454 return nil, io.ErrUnexpectedEOF
1456 v := int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
1457 f.appendInt32Slice(v)
1461 func unmarshalBoolValue(b []byte, f pointer, w int) ([]byte, error) {
1462 if w != WireVarint {
1463 return b, errInternalBadWireType
1465 // Note: any length varint is allowed, even though any sane
1466 // encoder will use one byte.
1467 // See https://github.com/golang/protobuf/issues/76
1468 x, n := decodeVarint(b)
1470 return nil, io.ErrUnexpectedEOF
1472 // TODO: check if x>1? Tests seem to indicate no.
1478 func unmarshalBoolPtr(b []byte, f pointer, w int) ([]byte, error) {
1479 if w != WireVarint {
1480 return b, errInternalBadWireType
1482 x, n := decodeVarint(b)
1484 return nil, io.ErrUnexpectedEOF
1491 func unmarshalBoolSlice(b []byte, f pointer, w int) ([]byte, error) {
1492 if w == WireBytes { // packed
1493 x, n := decodeVarint(b)
1495 return nil, io.ErrUnexpectedEOF
1498 if x > uint64(len(b)) {
1499 return nil, io.ErrUnexpectedEOF
1504 x, n = decodeVarint(b)
1506 return nil, io.ErrUnexpectedEOF
1509 s := f.toBoolSlice()
1515 if w != WireVarint {
1516 return b, errInternalBadWireType
1518 x, n := decodeVarint(b)
1520 return nil, io.ErrUnexpectedEOF
1523 s := f.toBoolSlice()
1528 func unmarshalFloat64Value(b []byte, f pointer, w int) ([]byte, error) {
1529 if w != WireFixed64 {
1530 return b, errInternalBadWireType
1533 return nil, io.ErrUnexpectedEOF
1535 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1540 func unmarshalFloat64Ptr(b []byte, f pointer, w int) ([]byte, error) {
1541 if w != WireFixed64 {
1542 return b, errInternalBadWireType
1545 return nil, io.ErrUnexpectedEOF
1547 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1548 *f.toFloat64Ptr() = &v
1552 func unmarshalFloat64Slice(b []byte, f pointer, w int) ([]byte, error) {
1553 if w == WireBytes { // packed
1554 x, n := decodeVarint(b)
1556 return nil, io.ErrUnexpectedEOF
1559 if x > uint64(len(b)) {
1560 return nil, io.ErrUnexpectedEOF
1566 return nil, io.ErrUnexpectedEOF
1568 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1569 s := f.toFloat64Slice()
1575 if w != WireFixed64 {
1576 return b, errInternalBadWireType
1579 return nil, io.ErrUnexpectedEOF
1581 v := math.Float64frombits(uint64(b[0]) | uint64(b[1])<<8 | uint64(b[2])<<16 | uint64(b[3])<<24 | uint64(b[4])<<32 | uint64(b[5])<<40 | uint64(b[6])<<48 | uint64(b[7])<<56)
1582 s := f.toFloat64Slice()
1587 func unmarshalFloat32Value(b []byte, f pointer, w int) ([]byte, error) {
1588 if w != WireFixed32 {
1589 return b, errInternalBadWireType
1592 return nil, io.ErrUnexpectedEOF
1594 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1599 func unmarshalFloat32Ptr(b []byte, f pointer, w int) ([]byte, error) {
1600 if w != WireFixed32 {
1601 return b, errInternalBadWireType
1604 return nil, io.ErrUnexpectedEOF
1606 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1607 *f.toFloat32Ptr() = &v
1611 func unmarshalFloat32Slice(b []byte, f pointer, w int) ([]byte, error) {
1612 if w == WireBytes { // packed
1613 x, n := decodeVarint(b)
1615 return nil, io.ErrUnexpectedEOF
1618 if x > uint64(len(b)) {
1619 return nil, io.ErrUnexpectedEOF
1625 return nil, io.ErrUnexpectedEOF
1627 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1628 s := f.toFloat32Slice()
1634 if w != WireFixed32 {
1635 return b, errInternalBadWireType
1638 return nil, io.ErrUnexpectedEOF
1640 v := math.Float32frombits(uint32(b[0]) | uint32(b[1])<<8 | uint32(b[2])<<16 | uint32(b[3])<<24)
1641 s := f.toFloat32Slice()
1646 func unmarshalStringValue(b []byte, f pointer, w int) ([]byte, error) {
1648 return b, errInternalBadWireType
1650 x, n := decodeVarint(b)
1652 return nil, io.ErrUnexpectedEOF
1655 if x > uint64(len(b)) {
1656 return nil, io.ErrUnexpectedEOF
1663 func unmarshalStringPtr(b []byte, f pointer, w int) ([]byte, error) {
1665 return b, errInternalBadWireType
1667 x, n := decodeVarint(b)
1669 return nil, io.ErrUnexpectedEOF
1672 if x > uint64(len(b)) {
1673 return nil, io.ErrUnexpectedEOF
1676 *f.toStringPtr() = &v
1680 func unmarshalStringSlice(b []byte, f pointer, w int) ([]byte, error) {
1682 return b, errInternalBadWireType
1684 x, n := decodeVarint(b)
1686 return nil, io.ErrUnexpectedEOF
1689 if x > uint64(len(b)) {
1690 return nil, io.ErrUnexpectedEOF
1693 s := f.toStringSlice()
1698 func unmarshalUTF8StringValue(b []byte, f pointer, w int) ([]byte, error) {
1700 return b, errInternalBadWireType
1702 x, n := decodeVarint(b)
1704 return nil, io.ErrUnexpectedEOF
1707 if x > uint64(len(b)) {
1708 return nil, io.ErrUnexpectedEOF
1712 if !utf8.ValidString(v) {
1713 return b[x:], errInvalidUTF8
1718 func unmarshalUTF8StringPtr(b []byte, f pointer, w int) ([]byte, error) {
1720 return b, errInternalBadWireType
1722 x, n := decodeVarint(b)
1724 return nil, io.ErrUnexpectedEOF
1727 if x > uint64(len(b)) {
1728 return nil, io.ErrUnexpectedEOF
1731 *f.toStringPtr() = &v
1732 if !utf8.ValidString(v) {
1733 return b[x:], errInvalidUTF8
1738 func unmarshalUTF8StringSlice(b []byte, f pointer, w int) ([]byte, error) {
1740 return b, errInternalBadWireType
1742 x, n := decodeVarint(b)
1744 return nil, io.ErrUnexpectedEOF
1747 if x > uint64(len(b)) {
1748 return nil, io.ErrUnexpectedEOF
1751 s := f.toStringSlice()
1753 if !utf8.ValidString(v) {
1754 return b[x:], errInvalidUTF8
1759 var emptyBuf [0]byte
1761 func unmarshalBytesValue(b []byte, f pointer, w int) ([]byte, error) {
1763 return b, errInternalBadWireType
1765 x, n := decodeVarint(b)
1767 return nil, io.ErrUnexpectedEOF
1770 if x > uint64(len(b)) {
1771 return nil, io.ErrUnexpectedEOF
1773 // The use of append here is a trick which avoids the zeroing
1774 // that would be required if we used a make/copy pair.
1775 // We append to emptyBuf instead of nil because we want
1776 // a non-nil result even when the length is 0.
1777 v := append(emptyBuf[:], b[:x]...)
1782 func unmarshalBytesSlice(b []byte, f pointer, w int) ([]byte, error) {
1784 return b, errInternalBadWireType
1786 x, n := decodeVarint(b)
1788 return nil, io.ErrUnexpectedEOF
1791 if x > uint64(len(b)) {
1792 return nil, io.ErrUnexpectedEOF
1794 v := append(emptyBuf[:], b[:x]...)
1795 s := f.toBytesSlice()
1800 func makeUnmarshalMessagePtr(sub *unmarshalInfo, name string) unmarshaler {
1801 return func(b []byte, f pointer, w int) ([]byte, error) {
1803 return b, errInternalBadWireType
1805 x, n := decodeVarint(b)
1807 return nil, io.ErrUnexpectedEOF
1810 if x > uint64(len(b)) {
1811 return nil, io.ErrUnexpectedEOF
1813 // First read the message field to see if something is there.
1814 // The semantics of multiple submessages are weird. Instead of
1815 // the last one winning (as it is for all other fields), multiple
1816 // submessages are merged.
1819 v = valToPointer(reflect.New(sub.typ))
1822 err := sub.unmarshal(v, b[:x])
1824 if r, ok := err.(*RequiredNotSetError); ok {
1825 r.field = name + "." + r.field
1834 func makeUnmarshalMessageSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
1835 return func(b []byte, f pointer, w int) ([]byte, error) {
1837 return b, errInternalBadWireType
1839 x, n := decodeVarint(b)
1841 return nil, io.ErrUnexpectedEOF
1844 if x > uint64(len(b)) {
1845 return nil, io.ErrUnexpectedEOF
1847 v := valToPointer(reflect.New(sub.typ))
1848 err := sub.unmarshal(v, b[:x])
1850 if r, ok := err.(*RequiredNotSetError); ok {
1851 r.field = name + "." + r.field
1861 func makeUnmarshalGroupPtr(sub *unmarshalInfo, name string) unmarshaler {
1862 return func(b []byte, f pointer, w int) ([]byte, error) {
1863 if w != WireStartGroup {
1864 return b, errInternalBadWireType
1866 x, y := findEndGroup(b)
1868 return nil, io.ErrUnexpectedEOF
1872 v = valToPointer(reflect.New(sub.typ))
1875 err := sub.unmarshal(v, b[:x])
1877 if r, ok := err.(*RequiredNotSetError); ok {
1878 r.field = name + "." + r.field
1887 func makeUnmarshalGroupSlicePtr(sub *unmarshalInfo, name string) unmarshaler {
1888 return func(b []byte, f pointer, w int) ([]byte, error) {
1889 if w != WireStartGroup {
1890 return b, errInternalBadWireType
1892 x, y := findEndGroup(b)
1894 return nil, io.ErrUnexpectedEOF
1896 v := valToPointer(reflect.New(sub.typ))
1897 err := sub.unmarshal(v, b[:x])
1899 if r, ok := err.(*RequiredNotSetError); ok {
1900 r.field = name + "." + r.field
1910 func makeUnmarshalMap(f *reflect.StructField) unmarshaler {
1914 tagArray := strings.Split(f.Tag.Get("protobuf"), ",")
1915 valTags := strings.Split(f.Tag.Get("protobuf_val"), ",")
1916 for _, t := range tagArray {
1917 if strings.HasPrefix(t, "customtype=") {
1918 valTags = append(valTags, t)
1921 valTags = append(valTags, t)
1923 if t == "stdduration" {
1924 valTags = append(valTags, t)
1927 valTags = append(valTags, t)
1930 unmarshalKey := typeUnmarshaler(kt, f.Tag.Get("protobuf_key"))
1931 unmarshalVal := typeUnmarshaler(vt, strings.Join(valTags, ","))
1932 return func(b []byte, f pointer, w int) ([]byte, error) {
1933 // The map entry is a submessage. Figure out how big it is.
1935 return nil, fmt.Errorf("proto: bad wiretype for map field: got %d want %d", w, WireBytes)
1937 x, n := decodeVarint(b)
1939 return nil, io.ErrUnexpectedEOF
1942 if x > uint64(len(b)) {
1943 return nil, io.ErrUnexpectedEOF
1945 r := b[x:] // unused data to return
1946 b = b[:x] // data for map entry
1948 // Note: we could use #keys * #values ~= 200 functions
1949 // to do map decoding without reflection. Probably not worth it.
1950 // Maps will be somewhat slow. Oh well.
1952 // Read key and value from data.
1954 k := reflect.New(kt)
1955 v := reflect.New(vt)
1957 x, n := decodeVarint(b)
1959 return nil, io.ErrUnexpectedEOF
1967 b, err = unmarshalKey(b, valToPointer(k), wire)
1969 b, err = unmarshalVal(b, valToPointer(v), wire)
1971 err = errInternalBadWireType // skip unknown tag
1974 if nerr.Merge(err) {
1977 if err != errInternalBadWireType {
1981 // Skip past unknown fields.
1982 b, err = skipField(b, wire)
1988 // Get map, allocate if needed.
1989 m := f.asPointerTo(t).Elem() // an addressable map[K]T
1991 m.Set(reflect.MakeMap(t))
1995 m.SetMapIndex(k.Elem(), v.Elem())
2001 // makeUnmarshalOneof makes an unmarshaler for oneof fields.
2009 // typ is the type of the concrete entry for a oneof case (e.g. Msg_X).
2010 // ityp is the interface type of the oneof field (e.g. isMsg_F).
2011 // unmarshal is the unmarshaler for the base type of the oneof case (e.g. int64).
2012 // Note that this function will be called once for each case in the oneof.
2013 func makeUnmarshalOneof(typ, ityp reflect.Type, unmarshal unmarshaler) unmarshaler {
2015 field0 := toField(&sf)
2016 return func(b []byte, f pointer, w int) ([]byte, error) {
2017 // Allocate holder for value.
2018 v := reflect.New(typ)
2020 // Unmarshal data into holder.
2021 // We unmarshal into the first field of the holder object.
2024 b, err = unmarshal(b, valToPointer(v).offset(field0), w)
2025 if !nerr.Merge(err) {
2029 // Write pointer to holder into target field.
2030 f.asPointerTo(ityp).Elem().Set(v)
2036 // Error used by decode internally.
2037 var errInternalBadWireType = errors.New("proto: internal error: bad wiretype")
2039 // skipField skips past a field of type wire and returns the remaining bytes.
2040 func skipField(b []byte, wire int) ([]byte, error) {
2043 _, k := decodeVarint(b)
2045 return b, io.ErrUnexpectedEOF
2050 return b, io.ErrUnexpectedEOF
2055 return b, io.ErrUnexpectedEOF
2059 m, k := decodeVarint(b)
2060 if k == 0 || uint64(len(b)-k) < m {
2061 return b, io.ErrUnexpectedEOF
2064 case WireStartGroup:
2065 _, i := findEndGroup(b)
2067 return b, io.ErrUnexpectedEOF
2071 return b, fmt.Errorf("proto: can't skip unknown wire type %d", wire)
2076 // findEndGroup finds the index of the next EndGroup tag.
2077 // Groups may be nested, so the "next" EndGroup tag is the first
2078 // unpaired EndGroup.
2079 // findEndGroup returns the indexes of the start and end of the EndGroup tag.
2080 // Returns (-1,-1) if it can't find one.
2081 func findEndGroup(b []byte) (int, int) {
2085 x, n := decodeVarint(b[i:])
2093 _, k := decodeVarint(b[i:])
2109 m, k := decodeVarint(b[i:])
2114 if uint64(len(b)-i) < m {
2118 case WireStartGroup:
2131 // encodeVarint appends a varint-encoded integer to b and returns the result.
2132 func encodeVarint(b []byte, x uint64) []byte {
2134 b = append(b, byte(x&0x7f|0x80))
2137 return append(b, byte(x))
2140 // decodeVarint reads a varint-encoded integer from b.
2141 // Returns the decoded integer and the number of bytes read.
2142 // If there is an error, it returns 0,0.
2143 func decodeVarint(b []byte) (uint64, int) {