summaryrefslogtreecommitdiff
path: root/vendor/google.golang.org/protobuf/internal/impl/encode.go
blob: b2e212291d6db27c09806442e301382089b26738 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright 2019 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package impl

import (
	"math"
	"sort"
	"sync/atomic"

	"google.golang.org/protobuf/internal/flags"
	"google.golang.org/protobuf/internal/protolazy"
	"google.golang.org/protobuf/proto"
	piface "google.golang.org/protobuf/runtime/protoiface"
)

type marshalOptions struct {
	flags piface.MarshalInputFlags
}

func (o marshalOptions) Options() proto.MarshalOptions {
	return proto.MarshalOptions{
		AllowPartial:  true,
		Deterministic: o.Deterministic(),
		UseCachedSize: o.UseCachedSize(),
	}
}

func (o marshalOptions) Deterministic() bool { return o.flags&piface.MarshalDeterministic != 0 }
func (o marshalOptions) UseCachedSize() bool { return o.flags&piface.MarshalUseCachedSize != 0 }

// size is protoreflect.Methods.Size.
func (mi *MessageInfo) size(in piface.SizeInput) piface.SizeOutput {
	var p pointer
	if ms, ok := in.Message.(*messageState); ok {
		p = ms.pointer()
	} else {
		p = in.Message.(*messageReflectWrapper).pointer()
	}
	size := mi.sizePointer(p, marshalOptions{
		flags: in.Flags,
	})
	return piface.SizeOutput{Size: size}
}

func (mi *MessageInfo) sizePointer(p pointer, opts marshalOptions) (size int) {
	mi.init()
	if p.IsNil() {
		return 0
	}
	if opts.UseCachedSize() && mi.sizecacheOffset.IsValid() {
		// The size cache contains the size + 1, to allow the
		// zero value to be invalid, while also allowing for a
		// 0 size to be cached.
		if size := atomic.LoadInt32(p.Apply(mi.sizecacheOffset).Int32()); size > 0 {
			return int(size - 1)
		}
	}
	return mi.sizePointerSlow(p, opts)
}

func (mi *MessageInfo) sizePointerSlow(p pointer, opts marshalOptions) (size int) {
	if flags.ProtoLegacy && mi.isMessageSet {
		size = sizeMessageSet(mi, p, opts)
		if mi.sizecacheOffset.IsValid() {
			atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size+1))
		}
		return size
	}
	if mi.extensionOffset.IsValid() {
		e := p.Apply(mi.extensionOffset).Extensions()
		size += mi.sizeExtensions(e, opts)
	}

	var lazy **protolazy.XXX_lazyUnmarshalInfo
	var presence presence
	if mi.presenceOffset.IsValid() {
		presence = p.Apply(mi.presenceOffset).PresenceInfo()
		if mi.lazyOffset.IsValid() {
			lazy = p.Apply(mi.lazyOffset).LazyInfoPtr()
		}
	}

	for _, f := range mi.orderedCoderFields {
		if f.funcs.size == nil {
			continue
		}
		fptr := p.Apply(f.offset)

		if f.presenceIndex != noPresence {
			if !presence.Present(f.presenceIndex) {
				continue
			}

			if f.isLazy && fptr.AtomicGetPointer().IsNil() {
				if lazyFields(opts) {
					size += (*lazy).SizeField(uint32(f.num))
					continue
				} else {
					mi.lazyUnmarshal(p, f.num)
				}
			}
			size += f.funcs.size(fptr, f, opts)
			continue
		}

		if f.isPointer && fptr.Elem().IsNil() {
			continue
		}
		size += f.funcs.size(fptr, f, opts)
	}
	if mi.unknownOffset.IsValid() {
		if u := mi.getUnknownBytes(p); u != nil {
			size += len(*u)
		}
	}
	if mi.sizecacheOffset.IsValid() {
		if size > (math.MaxInt32 - 1) {
			// The size is too large for the int32 sizecache field.
			// We will need to recompute the size when encoding;
			// unfortunately expensive, but better than invalid output.
			atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), 0)
		} else {
			// The size cache contains the size + 1, to allow the
			// zero value to be invalid, while also allowing for a
			// 0 size to be cached.
			atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size+1))
		}
	}
	return size
}

// marshal is protoreflect.Methods.Marshal.
func (mi *MessageInfo) marshal(in piface.MarshalInput) (out piface.MarshalOutput, err error) {
	var p pointer
	if ms, ok := in.Message.(*messageState); ok {
		p = ms.pointer()
	} else {
		p = in.Message.(*messageReflectWrapper).pointer()
	}
	b, err := mi.marshalAppendPointer(in.Buf, p, marshalOptions{
		flags: in.Flags,
	})
	return piface.MarshalOutput{Buf: b}, err
}

func (mi *MessageInfo) marshalAppendPointer(b []byte, p pointer, opts marshalOptions) ([]byte, error) {
	mi.init()
	if p.IsNil() {
		return b, nil
	}
	if flags.ProtoLegacy && mi.isMessageSet {
		return marshalMessageSet(mi, b, p, opts)
	}
	var err error
	// The old marshaler encodes extensions at beginning.
	if mi.extensionOffset.IsValid() {
		e := p.Apply(mi.extensionOffset).Extensions()
		// TODO: Special handling for MessageSet?
		b, err = mi.appendExtensions(b, e, opts)
		if err != nil {
			return b, err
		}
	}

	var lazy **protolazy.XXX_lazyUnmarshalInfo
	var presence presence
	if mi.presenceOffset.IsValid() {
		presence = p.Apply(mi.presenceOffset).PresenceInfo()
		if mi.lazyOffset.IsValid() {
			lazy = p.Apply(mi.lazyOffset).LazyInfoPtr()
		}
	}

	for _, f := range mi.orderedCoderFields {
		if f.funcs.marshal == nil {
			continue
		}
		fptr := p.Apply(f.offset)

		if f.presenceIndex != noPresence {
			if !presence.Present(f.presenceIndex) {
				continue
			}
			if f.isLazy {
				// Be careful, this field needs to be read atomically, like for a get
				if f.isPointer && fptr.AtomicGetPointer().IsNil() {
					if lazyFields(opts) {
						b, _ = (*lazy).AppendField(b, uint32(f.num))
						continue
					} else {
						mi.lazyUnmarshal(p, f.num)
					}
				}

				b, err = f.funcs.marshal(b, fptr, f, opts)
				if err != nil {
					return b, err
				}
				continue
			} else if f.isPointer && fptr.Elem().IsNil() {
				continue
			}
			b, err = f.funcs.marshal(b, fptr, f, opts)
			if err != nil {
				return b, err
			}
			continue
		}

		if f.isPointer && fptr.Elem().IsNil() {
			continue
		}
		b, err = f.funcs.marshal(b, fptr, f, opts)
		if err != nil {
			return b, err
		}
	}
	if mi.unknownOffset.IsValid() && !mi.isMessageSet {
		if u := mi.getUnknownBytes(p); u != nil {
			b = append(b, (*u)...)
		}
	}
	return b, nil
}

// fullyLazyExtensions returns true if we should attempt to keep extensions lazy over size and marshal.
func fullyLazyExtensions(opts marshalOptions) bool {
	// When deterministic marshaling is requested, force an unmarshal for lazy
	// extensions to produce a deterministic result, instead of passing through
	// bytes lazily that may or may not match what Go Protobuf would produce.
	return opts.flags&piface.MarshalDeterministic == 0
}

// lazyFields returns true if we should attempt to keep fields lazy over size and marshal.
func lazyFields(opts marshalOptions) bool {
	// When deterministic marshaling is requested, force an unmarshal for lazy
	// fields to produce a deterministic result, instead of passing through
	// bytes lazily that may or may not match what Go Protobuf would produce.
	return opts.flags&piface.MarshalDeterministic == 0
}

func (mi *MessageInfo) sizeExtensions(ext *map[int32]ExtensionField, opts marshalOptions) (n int) {
	if ext == nil {
		return 0
	}
	for _, x := range *ext {
		xi := getExtensionFieldInfo(x.Type())
		if xi.funcs.size == nil {
			continue
		}
		if fullyLazyExtensions(opts) {
			// Don't expand the extension, instead use the buffer to calculate size
			if lb := x.lazyBuffer(); lb != nil {
				// We got hold of the buffer, so it's still lazy.
				n += len(lb)
				continue
			}
		}
		n += xi.funcs.size(x.Value(), xi.tagsize, opts)
	}
	return n
}

func (mi *MessageInfo) appendExtensions(b []byte, ext *map[int32]ExtensionField, opts marshalOptions) ([]byte, error) {
	if ext == nil {
		return b, nil
	}

	switch len(*ext) {
	case 0:
		return b, nil
	case 1:
		// Fast-path for one extension: Don't bother sorting the keys.
		var err error
		for _, x := range *ext {
			xi := getExtensionFieldInfo(x.Type())
			if fullyLazyExtensions(opts) {
				// Don't expand the extension if it's still in wire format, instead use the buffer content.
				if lb := x.lazyBuffer(); lb != nil {
					b = append(b, lb...)
					continue
				}
			}
			b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)
		}
		return b, err
	default:
		// Sort the keys to provide a deterministic encoding.
		// Not sure this is required, but the old code does it.
		keys := make([]int, 0, len(*ext))
		for k := range *ext {
			keys = append(keys, int(k))
		}
		sort.Ints(keys)
		var err error
		for _, k := range keys {
			x := (*ext)[int32(k)]
			xi := getExtensionFieldInfo(x.Type())
			if fullyLazyExtensions(opts) {
				// Don't expand the extension if it's still in wire format, instead use the buffer content.
				if lb := x.lazyBuffer(); lb != nil {
					b = append(b, lb...)
					continue
				}
			}
			b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)
			if err != nil {
				return b, err
			}
		}
		return b, nil
	}
}