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.
12
"google.golang.org/protobuf/internal/flags"
13
proto "google.golang.org/protobuf/proto"
14
piface "google.golang.org/protobuf/runtime/protoiface"
17
type marshalOptions struct {
18
flags piface.MarshalInputFlags
21
func (o marshalOptions) Options() proto.MarshalOptions {
22
return proto.MarshalOptions{
24
Deterministic: o.Deterministic(),
25
UseCachedSize: o.UseCachedSize(),
29
func (o marshalOptions) Deterministic() bool { return o.flags&piface.MarshalDeterministic != 0 }
30
func (o marshalOptions) UseCachedSize() bool { return o.flags&piface.MarshalUseCachedSize != 0 }
32
// size is protoreflect.Methods.Size.
33
func (mi *MessageInfo) size(in piface.SizeInput) piface.SizeOutput {
35
if ms, ok := in.Message.(*messageState); ok {
38
p = in.Message.(*messageReflectWrapper).pointer()
40
size := mi.sizePointer(p, marshalOptions{
43
return piface.SizeOutput{Size: size}
46
func (mi *MessageInfo) sizePointer(p pointer, opts marshalOptions) (size int) {
51
if opts.UseCachedSize() && mi.sizecacheOffset.IsValid() {
52
if size := atomic.LoadInt32(p.Apply(mi.sizecacheOffset).Int32()); size >= 0 {
56
return mi.sizePointerSlow(p, opts)
59
func (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))
67
if mi.extensionOffset.IsValid() {
68
e := p.Apply(mi.extensionOffset).Extensions()
69
size += mi.sizeExtensions(e, opts)
71
for _, f := range mi.orderedCoderFields {
72
if f.funcs.size == nil {
75
fptr := p.Apply(f.offset)
76
if f.isPointer && fptr.Elem().IsNil() {
79
size += f.funcs.size(fptr, f, opts)
81
if mi.unknownOffset.IsValid() {
82
if u := mi.getUnknownBytes(p); u != nil {
86
if mi.sizecacheOffset.IsValid() {
87
if size > math.MaxInt32 {
88
// The size is too large for the int32 sizecache field.
89
// We will need to recompute the size when encoding;
90
// unfortunately expensive, but better than invalid output.
91
atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), -1)
93
atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size))
99
// marshal is protoreflect.Methods.Marshal.
100
func (mi *MessageInfo) marshal(in piface.MarshalInput) (out piface.MarshalOutput, err error) {
102
if ms, ok := in.Message.(*messageState); ok {
105
p = in.Message.(*messageReflectWrapper).pointer()
107
b, err := mi.marshalAppendPointer(in.Buf, p, marshalOptions{
110
return piface.MarshalOutput{Buf: b}, err
113
func (mi *MessageInfo) marshalAppendPointer(b []byte, p pointer, opts marshalOptions) ([]byte, error) {
118
if flags.ProtoLegacy && mi.isMessageSet {
119
return marshalMessageSet(mi, b, p, opts)
122
// The old marshaler encodes extensions at beginning.
123
if mi.extensionOffset.IsValid() {
124
e := p.Apply(mi.extensionOffset).Extensions()
125
// TODO: Special handling for MessageSet?
126
b, err = mi.appendExtensions(b, e, opts)
131
for _, f := range mi.orderedCoderFields {
132
if f.funcs.marshal == nil {
135
fptr := p.Apply(f.offset)
136
if f.isPointer && fptr.Elem().IsNil() {
139
b, err = f.funcs.marshal(b, fptr, f, opts)
144
if mi.unknownOffset.IsValid() && !mi.isMessageSet {
145
if u := mi.getUnknownBytes(p); u != nil {
146
b = append(b, (*u)...)
152
func (mi *MessageInfo) sizeExtensions(ext *map[int32]ExtensionField, opts marshalOptions) (n int) {
156
for _, x := range *ext {
157
xi := getExtensionFieldInfo(x.Type())
158
if xi.funcs.size == nil {
161
n += xi.funcs.size(x.Value(), xi.tagsize, opts)
166
func (mi *MessageInfo) appendExtensions(b []byte, ext *map[int32]ExtensionField, opts marshalOptions) ([]byte, error) {
175
// Fast-path for one extension: Don't bother sorting the keys.
177
for _, x := range *ext {
178
xi := getExtensionFieldInfo(x.Type())
179
b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)
183
// Sort the keys to provide a deterministic encoding.
184
// Not sure this is required, but the old code does it.
185
keys := make([]int, 0, len(*ext))
186
for k := range *ext {
187
keys = append(keys, int(k))
191
for _, k := range keys {
192
x := (*ext)[int32(k)]
193
xi := getExtensionFieldInfo(x.Type())
194
b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)