blob: e48081e3f500dc48c9e626ec7ff42b7265f3a852 [file] [log] [blame]
Damien Neile91877d2019-06-27 10:54:42 -07001// 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
5package impl
6
7import (
8 "google.golang.org/protobuf/internal/encoding/wire"
9 "google.golang.org/protobuf/internal/errors"
Damien Neilce3384c2019-11-06 13:18:28 -080010 "google.golang.org/protobuf/internal/flags"
Damien Neile91877d2019-06-27 10:54:42 -070011 "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.
21type unmarshalOptions struct {
22 flags unmarshalOptionFlags
23 resolver preg.ExtensionTypeResolver
24}
25
26type unmarshalOptionFlags uint8
27
28const (
29 unmarshalDiscardUnknown unmarshalOptionFlags = 1 << iota
30)
31
32func 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
42func (o unmarshalOptions) Options() proto.UnmarshalOptions {
43 return proto.UnmarshalOptions{
Joe Tsai705acad2019-09-14 18:22:59 -070044 Merge: true,
Damien Neile91877d2019-06-27 10:54:42 -070045 AllowPartial: true,
46 DiscardUnknown: o.DiscardUnknown(),
47 Resolver: o.Resolver(),
48 }
49}
50
51func (o unmarshalOptions) DiscardUnknown() bool { return o.flags&unmarshalDiscardUnknown != 0 }
52func (o unmarshalOptions) Resolver() preg.ExtensionTypeResolver { return o.resolver }
53
54// unmarshal is protoreflect.Methods.Unmarshal.
Joe Tsai0f81b382019-07-10 23:14:31 -070055func (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 Neile91877d2019-06-27 10:54:42 -070063 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.
72var errUnknown = errors.New("unknown")
73
74func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag wire.Number, opts unmarshalOptions) (int, error) {
75 mi.init()
Damien Neilce3384c2019-11-06 13:18:28 -080076 if flags.ProtoLegacy && mi.isMessageSet {
77 return unmarshalMessageSet(mi, b, p, opts)
78 }
Damien Neile91877d2019-06-27 10:54:42 -070079 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 Neilfe15dd42019-12-06 15:36:03 -080088 if num > wire.MaxValidNumber {
89 return 0, errors.New("invalid field number")
90 }
Damien Neile91877d2019-06-27 10:54:42 -070091 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
144func (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 Neilef19a2a2019-11-01 12:00:37 -0700146 xt := x.Type()
Damien Neile91877d2019-06-27 10:54:42 -0700147 if xt == nil {
148 var err error
Damien Neil16163b42019-08-06 15:43:25 -0700149 xt, err = opts.Resolver().FindExtensionByNumber(mi.Desc.FullName(), num)
Damien Neile91877d2019-06-27 10:54:42 -0700150 if err != nil {
151 if err == preg.NotFound {
152 return 0, errUnknown
153 }
154 return 0, err
155 }
Damien Neile91877d2019-06-27 10:54:42 -0700156 }
Damien Neil79571e92019-12-09 10:24:36 -0800157 xi := getExtensionFieldInfo(xt)
Damien Neile91877d2019-06-27 10:54:42 -0700158 if xi.funcs.unmarshal == nil {
159 return 0, errUnknown
160 }
Damien Neil68b81c32019-08-22 11:41:32 -0700161 ival := x.Value()
162 if !ival.IsValid() && xi.unmarshalNeedsValue {
Damien Neile91877d2019-06-27 10:54:42 -0700163 // 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 Neil68b81c32019-08-22 11:41:32 -0700166 ival = xt.New()
Damien Neile91877d2019-06-27 10:54:42 -0700167 }
168 v, n, err := xi.funcs.unmarshal(b, ival, num, wtyp, opts)
169 if err != nil {
170 return 0, err
171 }
Damien Neil68b81c32019-08-22 11:41:32 -0700172 x.Set(xt, v)
Damien Neile91877d2019-06-27 10:54:42 -0700173 exts[int32(num)] = x
174 return n, nil
175}