blob: 13edcc7ee0263e532d48c094d7659bc843bd1e52 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001// Copyright 2018 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
5package proto
6
7import (
8 "google.golang.org/protobuf/encoding/protowire"
9 "google.golang.org/protobuf/internal/encoding/messageset"
10 "google.golang.org/protobuf/internal/errors"
11 "google.golang.org/protobuf/internal/flags"
12 "google.golang.org/protobuf/internal/genid"
13 "google.golang.org/protobuf/internal/pragma"
14 "google.golang.org/protobuf/reflect/protoreflect"
15 "google.golang.org/protobuf/reflect/protoregistry"
16 "google.golang.org/protobuf/runtime/protoiface"
17)
18
19// UnmarshalOptions configures the unmarshaler.
20//
21// Example usage:
Joey Armstrongba3d9d12024-01-15 14:22:11 -050022//
23// err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
khenaidoo5fc5cea2021-08-11 17:39:16 -040024type UnmarshalOptions struct {
25 pragma.NoUnkeyedLiterals
26
27 // Merge merges the input into the destination message.
28 // The default behavior is to always reset the message before unmarshaling,
29 // unless Merge is specified.
30 Merge bool
31
32 // AllowPartial accepts input for messages that will result in missing
33 // required fields. If AllowPartial is false (the default), Unmarshal will
34 // return an error if there are any missing required fields.
35 AllowPartial bool
36
37 // If DiscardUnknown is set, unknown fields are ignored.
38 DiscardUnknown bool
39
40 // Resolver is used for looking up types when unmarshaling extension fields.
41 // If nil, this defaults to using protoregistry.GlobalTypes.
42 Resolver interface {
43 FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
44 FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
45 }
46}
47
48// Unmarshal parses the wire-format message in b and places the result in m.
49// The provided message must be mutable (e.g., a non-nil pointer to a message).
50func Unmarshal(b []byte, m Message) error {
51 _, err := UnmarshalOptions{}.unmarshal(b, m.ProtoReflect())
52 return err
53}
54
55// Unmarshal parses the wire-format message in b and places the result in m.
56// The provided message must be mutable (e.g., a non-nil pointer to a message).
57func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
58 _, err := o.unmarshal(b, m.ProtoReflect())
59 return err
60}
61
62// UnmarshalState parses a wire-format message and places the result in m.
63//
64// This method permits fine-grained control over the unmarshaler.
65// Most users should use Unmarshal instead.
66func (o UnmarshalOptions) UnmarshalState(in protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) {
67 return o.unmarshal(in.Buf, in.Message)
68}
69
70// unmarshal is a centralized function that all unmarshal operations go through.
71// For profiling purposes, avoid changing the name of this function or
72// introducing other code paths for unmarshal that do not go through this.
73func (o UnmarshalOptions) unmarshal(b []byte, m protoreflect.Message) (out protoiface.UnmarshalOutput, err error) {
74 if o.Resolver == nil {
75 o.Resolver = protoregistry.GlobalTypes
76 }
77 if !o.Merge {
78 Reset(m.Interface())
79 }
80 allowPartial := o.AllowPartial
81 o.Merge = true
82 o.AllowPartial = true
83 methods := protoMethods(m)
84 if methods != nil && methods.Unmarshal != nil &&
85 !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
86 in := protoiface.UnmarshalInput{
87 Message: m,
88 Buf: b,
89 Resolver: o.Resolver,
90 }
91 if o.DiscardUnknown {
92 in.Flags |= protoiface.UnmarshalDiscardUnknown
93 }
94 out, err = methods.Unmarshal(in)
95 } else {
96 err = o.unmarshalMessageSlow(b, m)
97 }
98 if err != nil {
99 return out, err
100 }
101 if allowPartial || (out.Flags&protoiface.UnmarshalInitialized != 0) {
102 return out, nil
103 }
104 return out, checkInitialized(m)
105}
106
107func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
108 _, err := o.unmarshal(b, m)
109 return err
110}
111
112func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
113 md := m.Descriptor()
114 if messageset.IsMessageSet(md) {
115 return o.unmarshalMessageSet(b, m)
116 }
117 fields := md.Fields()
118 for len(b) > 0 {
119 // Parse the tag (field number and wire type).
120 num, wtyp, tagLen := protowire.ConsumeTag(b)
121 if tagLen < 0 {
122 return errDecode
123 }
124 if num > protowire.MaxValidNumber {
125 return errDecode
126 }
127
128 // Find the field descriptor for this field number.
129 fd := fields.ByNumber(num)
130 if fd == nil && md.ExtensionRanges().Has(num) {
131 extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
132 if err != nil && err != protoregistry.NotFound {
133 return errors.New("%v: unable to resolve extension %v: %v", md.FullName(), num, err)
134 }
135 if extType != nil {
136 fd = extType.TypeDescriptor()
137 }
138 }
139 var err error
140 if fd == nil {
141 err = errUnknown
142 } else if flags.ProtoLegacy {
143 if fd.IsWeak() && fd.Message().IsPlaceholder() {
144 err = errUnknown // weak referent is not linked in
145 }
146 }
147
148 // Parse the field value.
149 var valLen int
150 switch {
151 case err != nil:
152 case fd.IsList():
153 valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
154 case fd.IsMap():
155 valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
156 default:
157 valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
158 }
159 if err != nil {
160 if err != errUnknown {
161 return err
162 }
163 valLen = protowire.ConsumeFieldValue(num, wtyp, b[tagLen:])
164 if valLen < 0 {
165 return errDecode
166 }
167 if !o.DiscardUnknown {
168 m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
169 }
170 }
171 b = b[tagLen+valLen:]
172 }
173 return nil
174}
175
176func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp protowire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
177 v, n, err := o.unmarshalScalar(b, wtyp, fd)
178 if err != nil {
179 return 0, err
180 }
181 switch fd.Kind() {
182 case protoreflect.GroupKind, protoreflect.MessageKind:
183 m2 := m.Mutable(fd).Message()
184 if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
185 return n, err
186 }
187 default:
188 // Non-message scalars replace the previous value.
189 m.Set(fd, v)
190 }
191 return n, nil
192}
193
194func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp protowire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
195 if wtyp != protowire.BytesType {
196 return 0, errUnknown
197 }
198 b, n = protowire.ConsumeBytes(b)
199 if n < 0 {
200 return 0, errDecode
201 }
202 var (
203 keyField = fd.MapKey()
204 valField = fd.MapValue()
205 key protoreflect.Value
206 val protoreflect.Value
207 haveKey bool
208 haveVal bool
209 )
210 switch valField.Kind() {
211 case protoreflect.GroupKind, protoreflect.MessageKind:
212 val = mapv.NewValue()
213 }
214 // Map entries are represented as a two-element message with fields
215 // containing the key and value.
216 for len(b) > 0 {
217 num, wtyp, n := protowire.ConsumeTag(b)
218 if n < 0 {
219 return 0, errDecode
220 }
221 if num > protowire.MaxValidNumber {
222 return 0, errDecode
223 }
224 b = b[n:]
225 err = errUnknown
226 switch num {
227 case genid.MapEntry_Key_field_number:
228 key, n, err = o.unmarshalScalar(b, wtyp, keyField)
229 if err != nil {
230 break
231 }
232 haveKey = true
233 case genid.MapEntry_Value_field_number:
234 var v protoreflect.Value
235 v, n, err = o.unmarshalScalar(b, wtyp, valField)
236 if err != nil {
237 break
238 }
239 switch valField.Kind() {
240 case protoreflect.GroupKind, protoreflect.MessageKind:
241 if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
242 return 0, err
243 }
244 default:
245 val = v
246 }
247 haveVal = true
248 }
249 if err == errUnknown {
250 n = protowire.ConsumeFieldValue(num, wtyp, b)
251 if n < 0 {
252 return 0, errDecode
253 }
254 } else if err != nil {
255 return 0, err
256 }
257 b = b[n:]
258 }
259 // Every map entry should have entries for key and value, but this is not strictly required.
260 if !haveKey {
261 key = keyField.Default()
262 }
263 if !haveVal {
264 switch valField.Kind() {
265 case protoreflect.GroupKind, protoreflect.MessageKind:
266 default:
267 val = valField.Default()
268 }
269 }
270 mapv.Set(key.MapKey(), val)
271 return n, nil
272}
273
274// errUnknown is used internally to indicate fields which should be added
275// to the unknown field set of a message. It is never returned from an exported
276// function.
277var errUnknown = errors.New("BUG: internal error (unknown)")
278
279var errDecode = errors.New("cannot parse invalid wire-format data")