Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 1 | // Copyright 2019 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package impl |
| 6 | |
| 7 | import ( |
| 8 | "google.golang.org/protobuf/internal/encoding/wire" |
| 9 | "google.golang.org/protobuf/internal/errors" |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 10 | "google.golang.org/protobuf/internal/flags" |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/proto" |
| 12 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 13 | preg "google.golang.org/protobuf/reflect/protoregistry" |
| 14 | piface "google.golang.org/protobuf/runtime/protoiface" |
| 15 | ) |
| 16 | |
| 17 | // unmarshalOptions is a more efficient representation of UnmarshalOptions. |
| 18 | // |
| 19 | // We don't preserve the AllowPartial flag, because fast-path (un)marshal |
| 20 | // operations always allow partial messages. |
| 21 | type unmarshalOptions struct { |
| 22 | flags unmarshalOptionFlags |
| 23 | resolver preg.ExtensionTypeResolver |
| 24 | } |
| 25 | |
| 26 | type unmarshalOptionFlags uint8 |
| 27 | |
| 28 | const ( |
| 29 | unmarshalDiscardUnknown unmarshalOptionFlags = 1 << iota |
| 30 | ) |
| 31 | |
| 32 | func newUnmarshalOptions(opts piface.UnmarshalOptions) unmarshalOptions { |
| 33 | o := unmarshalOptions{ |
| 34 | resolver: opts.Resolver, |
| 35 | } |
| 36 | if opts.DiscardUnknown { |
| 37 | o.flags |= unmarshalDiscardUnknown |
| 38 | } |
| 39 | return o |
| 40 | } |
| 41 | |
| 42 | func (o unmarshalOptions) Options() proto.UnmarshalOptions { |
| 43 | return proto.UnmarshalOptions{ |
Joe Tsai | 705acad | 2019-09-14 18:22:59 -0700 | [diff] [blame] | 44 | Merge: true, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 45 | AllowPartial: true, |
| 46 | DiscardUnknown: o.DiscardUnknown(), |
| 47 | Resolver: o.Resolver(), |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | func (o unmarshalOptions) DiscardUnknown() bool { return o.flags&unmarshalDiscardUnknown != 0 } |
| 52 | func (o unmarshalOptions) Resolver() preg.ExtensionTypeResolver { return o.resolver } |
| 53 | |
| 54 | // unmarshal is protoreflect.Methods.Unmarshal. |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 55 | func (mi *MessageInfo) unmarshal(b []byte, m pref.Message, opts piface.UnmarshalOptions) error { |
| 56 | var p pointer |
| 57 | if ms, ok := m.(*messageState); ok { |
| 58 | p = ms.pointer() |
| 59 | } else { |
| 60 | p = m.(*messageReflectWrapper).pointer() |
| 61 | } |
| 62 | _, err := mi.unmarshalPointer(b, p, 0, newUnmarshalOptions(opts)) |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 63 | return err |
| 64 | } |
| 65 | |
| 66 | // errUnknown is returned during unmarshaling to indicate a parse error that |
| 67 | // should result in a field being placed in the unknown fields section (for example, |
| 68 | // when the wire type doesn't match) as opposed to the entire unmarshal operation |
| 69 | // failing (for example, when a field extends past the available input). |
| 70 | // |
| 71 | // This is a sentinel error which should never be visible to the user. |
| 72 | var errUnknown = errors.New("unknown") |
| 73 | |
| 74 | func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag wire.Number, opts unmarshalOptions) (int, error) { |
| 75 | mi.init() |
Damien Neil | ce3384c | 2019-11-06 13:18:28 -0800 | [diff] [blame] | 76 | if flags.ProtoLegacy && mi.isMessageSet { |
| 77 | return unmarshalMessageSet(mi, b, p, opts) |
| 78 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 79 | var exts *map[int32]ExtensionField |
| 80 | start := len(b) |
| 81 | for len(b) > 0 { |
| 82 | // Parse the tag (field number and wire type). |
| 83 | // TODO: inline 1 and 2 byte variants? |
| 84 | num, wtyp, n := wire.ConsumeTag(b) |
| 85 | if n < 0 { |
| 86 | return 0, wire.ParseError(n) |
| 87 | } |
Damien Neil | fe15dd4 | 2019-12-06 15:36:03 -0800 | [diff] [blame] | 88 | if num > wire.MaxValidNumber { |
| 89 | return 0, errors.New("invalid field number") |
| 90 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 91 | b = b[n:] |
| 92 | |
| 93 | var f *coderFieldInfo |
| 94 | if int(num) < len(mi.denseCoderFields) { |
| 95 | f = mi.denseCoderFields[num] |
| 96 | } else { |
| 97 | f = mi.coderFields[num] |
| 98 | } |
| 99 | err := errUnknown |
| 100 | switch { |
| 101 | case f != nil: |
| 102 | if f.funcs.unmarshal == nil { |
| 103 | break |
| 104 | } |
| 105 | n, err = f.funcs.unmarshal(b, p.Apply(f.offset), wtyp, opts) |
| 106 | case num == groupTag && wtyp == wire.EndGroupType: |
| 107 | // End of group. |
| 108 | return start - len(b), nil |
| 109 | default: |
| 110 | // Possible extension. |
| 111 | if exts == nil && mi.extensionOffset.IsValid() { |
| 112 | exts = p.Apply(mi.extensionOffset).Extensions() |
| 113 | if *exts == nil { |
| 114 | *exts = make(map[int32]ExtensionField) |
| 115 | } |
| 116 | } |
| 117 | if exts == nil { |
| 118 | break |
| 119 | } |
| 120 | n, err = mi.unmarshalExtension(b, num, wtyp, *exts, opts) |
| 121 | } |
| 122 | if err != nil { |
| 123 | if err != errUnknown { |
| 124 | return 0, err |
| 125 | } |
| 126 | n = wire.ConsumeFieldValue(num, wtyp, b) |
| 127 | if n < 0 { |
| 128 | return 0, wire.ParseError(n) |
| 129 | } |
| 130 | if mi.unknownOffset.IsValid() { |
| 131 | u := p.Apply(mi.unknownOffset).Bytes() |
| 132 | *u = wire.AppendTag(*u, num, wtyp) |
| 133 | *u = append(*u, b[:n]...) |
| 134 | } |
| 135 | } |
| 136 | b = b[n:] |
| 137 | } |
| 138 | if groupTag != 0 { |
| 139 | return 0, errors.New("missing end group marker") |
| 140 | } |
| 141 | return start, nil |
| 142 | } |
| 143 | |
| 144 | func (mi *MessageInfo) unmarshalExtension(b []byte, num wire.Number, wtyp wire.Type, exts map[int32]ExtensionField, opts unmarshalOptions) (n int, err error) { |
| 145 | x := exts[int32(num)] |
Damien Neil | ef19a2a | 2019-11-01 12:00:37 -0700 | [diff] [blame] | 146 | xt := x.Type() |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 147 | if xt == nil { |
| 148 | var err error |
Damien Neil | 16163b4 | 2019-08-06 15:43:25 -0700 | [diff] [blame] | 149 | xt, err = opts.Resolver().FindExtensionByNumber(mi.Desc.FullName(), num) |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 150 | if err != nil { |
| 151 | if err == preg.NotFound { |
| 152 | return 0, errUnknown |
| 153 | } |
| 154 | return 0, err |
| 155 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 156 | } |
Damien Neil | 79571e9 | 2019-12-09 10:24:36 -0800 | [diff] [blame^] | 157 | xi := getExtensionFieldInfo(xt) |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 158 | if xi.funcs.unmarshal == nil { |
| 159 | return 0, errUnknown |
| 160 | } |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 161 | ival := x.Value() |
| 162 | if !ival.IsValid() && xi.unmarshalNeedsValue { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 163 | // Create a new message, list, or map value to fill in. |
| 164 | // For enums, create a prototype value to let the unmarshal func know the |
| 165 | // concrete type. |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 166 | ival = xt.New() |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 167 | } |
| 168 | v, n, err := xi.funcs.unmarshal(b, ival, num, wtyp, opts) |
| 169 | if err != nil { |
| 170 | return 0, err |
| 171 | } |
Damien Neil | 68b81c3 | 2019-08-22 11:41:32 -0700 | [diff] [blame] | 172 | x.Set(xt, v) |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 173 | exts[int32(num)] = x |
| 174 | return n, nil |
| 175 | } |