blob: 8c8a794c631da841d657412a04793754d33a0fb9 [file] [log] [blame]
mpagenkoaf801632020-07-03 10:00:42 +00001// 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 "math"
9 "sort"
10 "sync/atomic"
11
12 "google.golang.org/protobuf/internal/flags"
13 proto "google.golang.org/protobuf/proto"
14 piface "google.golang.org/protobuf/runtime/protoiface"
15)
16
17type marshalOptions struct {
18 flags piface.MarshalInputFlags
19}
20
21func (o marshalOptions) Options() proto.MarshalOptions {
22 return proto.MarshalOptions{
23 AllowPartial: true,
24 Deterministic: o.Deterministic(),
25 UseCachedSize: o.UseCachedSize(),
26 }
27}
28
29func (o marshalOptions) Deterministic() bool { return o.flags&piface.MarshalDeterministic != 0 }
30func (o marshalOptions) UseCachedSize() bool { return o.flags&piface.MarshalUseCachedSize != 0 }
31
32// size is protoreflect.Methods.Size.
33func (mi *MessageInfo) size(in piface.SizeInput) piface.SizeOutput {
34 var p pointer
35 if ms, ok := in.Message.(*messageState); ok {
36 p = ms.pointer()
37 } else {
38 p = in.Message.(*messageReflectWrapper).pointer()
39 }
40 size := mi.sizePointer(p, marshalOptions{
41 flags: in.Flags,
42 })
43 return piface.SizeOutput{Size: size}
44}
45
46func (mi *MessageInfo) sizePointer(p pointer, opts marshalOptions) (size int) {
47 mi.init()
48 if p.IsNil() {
49 return 0
50 }
51 if opts.UseCachedSize() && mi.sizecacheOffset.IsValid() {
52 if size := atomic.LoadInt32(p.Apply(mi.sizecacheOffset).Int32()); size >= 0 {
53 return int(size)
54 }
55 }
56 return mi.sizePointerSlow(p, opts)
57}
58
59func (mi *MessageInfo) sizePointerSlow(p pointer, opts marshalOptions) (size int) {
60 if flags.ProtoLegacy && mi.isMessageSet {
61 size = sizeMessageSet(mi, p, opts)
62 if mi.sizecacheOffset.IsValid() {
63 atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size))
64 }
65 return size
66 }
67 if mi.extensionOffset.IsValid() {
68 e := p.Apply(mi.extensionOffset).Extensions()
69 size += mi.sizeExtensions(e, opts)
70 }
71 for _, f := range mi.orderedCoderFields {
72 if f.funcs.size == nil {
73 continue
74 }
75 fptr := p.Apply(f.offset)
76 if f.isPointer && fptr.Elem().IsNil() {
77 continue
78 }
79 size += f.funcs.size(fptr, f, opts)
80 }
81 if mi.unknownOffset.IsValid() {
82 u := *p.Apply(mi.unknownOffset).Bytes()
83 size += len(u)
84 }
85 if mi.sizecacheOffset.IsValid() {
86 if size > math.MaxInt32 {
87 // The size is too large for the int32 sizecache field.
88 // We will need to recompute the size when encoding;
89 // unfortunately expensive, but better than invalid output.
90 atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), -1)
91 } else {
92 atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size))
93 }
94 }
95 return size
96}
97
98// marshal is protoreflect.Methods.Marshal.
99func (mi *MessageInfo) marshal(in piface.MarshalInput) (out piface.MarshalOutput, err error) {
100 var p pointer
101 if ms, ok := in.Message.(*messageState); ok {
102 p = ms.pointer()
103 } else {
104 p = in.Message.(*messageReflectWrapper).pointer()
105 }
106 b, err := mi.marshalAppendPointer(in.Buf, p, marshalOptions{
107 flags: in.Flags,
108 })
109 return piface.MarshalOutput{Buf: b}, err
110}
111
112func (mi *MessageInfo) marshalAppendPointer(b []byte, p pointer, opts marshalOptions) ([]byte, error) {
113 mi.init()
114 if p.IsNil() {
115 return b, nil
116 }
117 if flags.ProtoLegacy && mi.isMessageSet {
118 return marshalMessageSet(mi, b, p, opts)
119 }
120 var err error
121 // The old marshaler encodes extensions at beginning.
122 if mi.extensionOffset.IsValid() {
123 e := p.Apply(mi.extensionOffset).Extensions()
124 // TODO: Special handling for MessageSet?
125 b, err = mi.appendExtensions(b, e, opts)
126 if err != nil {
127 return b, err
128 }
129 }
130 for _, f := range mi.orderedCoderFields {
131 if f.funcs.marshal == nil {
132 continue
133 }
134 fptr := p.Apply(f.offset)
135 if f.isPointer && fptr.Elem().IsNil() {
136 continue
137 }
138 b, err = f.funcs.marshal(b, fptr, f, opts)
139 if err != nil {
140 return b, err
141 }
142 }
143 if mi.unknownOffset.IsValid() && !mi.isMessageSet {
144 u := *p.Apply(mi.unknownOffset).Bytes()
145 b = append(b, u...)
146 }
147 return b, nil
148}
149
150func (mi *MessageInfo) sizeExtensions(ext *map[int32]ExtensionField, opts marshalOptions) (n int) {
151 if ext == nil {
152 return 0
153 }
154 for _, x := range *ext {
155 xi := getExtensionFieldInfo(x.Type())
156 if xi.funcs.size == nil {
157 continue
158 }
159 n += xi.funcs.size(x.Value(), xi.tagsize, opts)
160 }
161 return n
162}
163
164func (mi *MessageInfo) appendExtensions(b []byte, ext *map[int32]ExtensionField, opts marshalOptions) ([]byte, error) {
165 if ext == nil {
166 return b, nil
167 }
168
169 switch len(*ext) {
170 case 0:
171 return b, nil
172 case 1:
173 // Fast-path for one extension: Don't bother sorting the keys.
174 var err error
175 for _, x := range *ext {
176 xi := getExtensionFieldInfo(x.Type())
177 b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)
178 }
179 return b, err
180 default:
181 // Sort the keys to provide a deterministic encoding.
182 // Not sure this is required, but the old code does it.
183 keys := make([]int, 0, len(*ext))
184 for k := range *ext {
185 keys = append(keys, int(k))
186 }
187 sort.Ints(keys)
188 var err error
189 for _, k := range keys {
190 x := (*ext)[int32(k)]
191 xi := getExtensionFieldInfo(x.Type())
192 b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)
193 if err != nil {
194 return b, err
195 }
196 }
197 return b, nil
198 }
199}