blob: 65e951e7cfcad99c3a6b26ed7a4a88177821b5c0 [file] [log] [blame]
Damien Neil99f24c32019-03-13 17:06: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 proto
6
7import (
Damien Neil99f24c32019-03-13 17:06:42 -07008 "sort"
9
Damien Neil302cb322019-06-19 15:22:13 -070010 "google.golang.org/protobuf/internal/encoding/messageset"
Damien Neile89e6242019-05-13 23:55:40 -070011 "google.golang.org/protobuf/internal/encoding/wire"
Damien Neil01c0e8d2019-11-12 12:33:12 -080012 "google.golang.org/protobuf/internal/fieldsort"
Damien Neile89e6242019-05-13 23:55:40 -070013 "google.golang.org/protobuf/internal/mapsort"
14 "google.golang.org/protobuf/internal/pragma"
15 "google.golang.org/protobuf/reflect/protoreflect"
16 "google.golang.org/protobuf/runtime/protoiface"
Damien Neil99f24c32019-03-13 17:06:42 -070017)
18
19// MarshalOptions configures the marshaler.
20//
21// Example usage:
22// b, err := MarshalOptions{Deterministic: true}.Marshal(m)
23type MarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070024 pragma.NoUnkeyedLiterals
25
Damien Neil96c229a2019-04-03 12:17:24 -070026 // AllowPartial allows messages that have missing required fields to marshal
27 // without returning an error. If AllowPartial is false (the default),
28 // Marshal will return an error if there are any missing required fields.
29 AllowPartial bool
30
Damien Neil99f24c32019-03-13 17:06:42 -070031 // Deterministic controls whether the same message will always be
32 // serialized to the same bytes within the same binary.
33 //
34 // Setting this option guarantees that repeated serialization of
35 // the same message will return the same bytes, and that different
36 // processes of the same binary (which may be executing on different
37 // machines) will serialize equal messages to the same bytes.
Joe Tsaif8b855d2019-07-12 13:37:59 -070038 // It has no effect on the resulting size of the encoded message compared
39 // to a non-deterministic marshal.
Damien Neil99f24c32019-03-13 17:06:42 -070040 //
41 // Note that the deterministic serialization is NOT canonical across
42 // languages. It is not guaranteed to remain stable over time. It is
43 // unstable across different builds with schema changes due to unknown
44 // fields. Users who need canonical serialization (e.g., persistent
45 // storage in a canonical form, fingerprinting, etc.) must define
46 // their own canonicalization specification and implement their own
47 // serializer rather than relying on this API.
48 //
49 // If deterministic serialization is requested, map entries will be
50 // sorted by keys in lexographical order. This is an implementation
51 // detail and subject to change.
52 Deterministic bool
53
Damien Neil03e74862019-04-07 18:18:31 -070054 // UseCachedSize indicates that the result of a previous Size call
55 // may be reused.
56 //
57 // Setting this option asserts that:
58 //
59 // 1. Size has previously been called on this message with identical
60 // options (except for UseCachedSize itself).
61 //
62 // 2. The message and all its submessages have not changed in any
63 // way since the Size call.
64 //
65 // If either of these invariants is broken, the results are undefined
66 // but may include panics or invalid output.
67 //
68 // Implementations MAY take this option into account to provide
69 // better performance, but there is no guarantee that they will do so.
70 // There is absolutely no guarantee that Size followed by Marshal with
71 // UseCachedSize set will perform equivalently to Marshal alone.
72 UseCachedSize bool
Damien Neil99f24c32019-03-13 17:06:42 -070073}
74
75// Marshal returns the wire-format encoding of m.
76func Marshal(m Message) ([]byte, error) {
Damien Neild30e5612020-01-22 10:28:16 -080077 out, err := MarshalOptions{}.marshal(nil, m)
78 return out.Buf, err
Damien Neil99f24c32019-03-13 17:06:42 -070079}
80
81// Marshal returns the wire-format encoding of m.
82func (o MarshalOptions) Marshal(m Message) ([]byte, error) {
Damien Neild30e5612020-01-22 10:28:16 -080083 out, err := o.marshal(nil, m)
84 return out.Buf, err
Damien Neil99f24c32019-03-13 17:06:42 -070085}
86
87// MarshalAppend appends the wire-format encoding of m to b,
88// returning the result.
89func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
Damien Neild30e5612020-01-22 10:28:16 -080090 out, err := o.marshal(b, m)
91 return out.Buf, err
Damien Neil99f24c32019-03-13 17:06:42 -070092}
93
Damien Neild30e5612020-01-22 10:28:16 -080094// MarshalState returns the wire-format encoding of m.
95//
96// This method permits fine-grained control over the marshaler.
97// Most users should use Marshal instead.
98func (o MarshalOptions) MarshalState(m Message, in protoiface.MarshalInput) (protoiface.MarshalOutput, error) {
99 return o.marshal(in.Buf, m)
100}
101
102func (o MarshalOptions) marshal(b []byte, message Message) (out protoiface.MarshalOutput, err error) {
103 allowPartial := o.AllowPartial
104 o.AllowPartial = true
105 m := message.ProtoReflect()
Damien Neil61781dd2020-01-21 13:29:51 -0800106 if methods := protoMethods(m); methods != nil && methods.Marshal != nil &&
Joe Tsaif8b855d2019-07-12 13:37:59 -0700107 !(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) {
Damien Neil524c6062020-01-28 13:32:01 -0800108 opts := protoiface.MarshalOptions{}
109 if o.Deterministic {
110 opts.Flags |= protoiface.MarshalDeterministic
111 }
112 if o.UseCachedSize {
113 opts.Flags |= protoiface.MarshalUseCachedSize
114 }
Damien Neilb0d217f2020-01-06 11:17:07 -0800115 if methods.Size != nil {
Damien Neil524c6062020-01-28 13:32:01 -0800116 sz := methods.Size(m, opts)
Damien Neilb0d217f2020-01-06 11:17:07 -0800117 if cap(b) < len(b)+sz {
118 x := make([]byte, len(b), growcap(cap(b), len(b)+sz))
119 copy(x, b)
120 b = x
121 }
Damien Neil524c6062020-01-28 13:32:01 -0800122 opts.Flags |= protoiface.MarshalUseCachedSize
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700123 }
Damien Neild30e5612020-01-22 10:28:16 -0800124 out, err = methods.Marshal(m, protoiface.MarshalInput{
125 Buf: b,
Damien Neil524c6062020-01-28 13:32:01 -0800126 }, opts)
Damien Neild30e5612020-01-22 10:28:16 -0800127 } else {
128 out.Buf, err = o.marshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700129 }
Damien Neild30e5612020-01-22 10:28:16 -0800130 if err != nil {
131 return out, err
132 }
133 if allowPartial {
134 return out, nil
135 }
136 return out, isInitialized(m)
137}
138
139func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
140 out, err := o.marshal(b, m.Interface())
141 return out.Buf, err
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700142}
143
Damien Neil1e5516a2019-09-27 14:31:10 -0700144// growcap scales up the capacity of a slice.
145//
146// Given a slice with a current capacity of oldcap and a desired
147// capacity of wantcap, growcap returns a new capacity >= wantcap.
148//
149// The algorithm is mostly identical to the one used by append as of Go 1.14.
150func growcap(oldcap, wantcap int) (newcap int) {
151 if wantcap > oldcap*2 {
152 newcap = wantcap
153 } else if oldcap < 1024 {
154 // The Go 1.14 runtime takes this case when len(s) < 1024,
155 // not when cap(s) < 1024. The difference doesn't seem
156 // significant here.
157 newcap = oldcap * 2
158 } else {
159 newcap = oldcap
160 for 0 < newcap && newcap < wantcap {
161 newcap += newcap / 4
162 }
163 if newcap <= 0 {
164 newcap = wantcap
165 }
166 }
167 return newcap
168}
169
Joe Tsai0f81b382019-07-10 23:14:31 -0700170func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
Damien Neil302cb322019-06-19 15:22:13 -0700171 if messageset.IsMessageSet(m.Descriptor()) {
172 return marshalMessageSet(b, m, o)
173 }
Damien Neil99f24c32019-03-13 17:06:42 -0700174 // There are many choices for what order we visit fields in. The default one here
175 // is chosen for reasonable efficiency and simplicity given the protoreflect API.
Joe Tsai378c1322019-04-25 23:48:08 -0700176 // It is not deterministic, since Message.Range does not return fields in any
Damien Neil99f24c32019-03-13 17:06:42 -0700177 // defined order.
178 //
Damien Neil01c0e8d2019-11-12 12:33:12 -0800179 // When using deterministic serialization, we sort the known fields.
Damien Neil99f24c32019-03-13 17:06:42 -0700180 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700181 o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
182 b, err = o.marshalField(b, fd, v)
Damien Neil8c86fc52019-06-19 09:28:29 -0700183 return err == nil
Damien Neil99f24c32019-03-13 17:06:42 -0700184 })
185 if err != nil {
Damien Neil96c229a2019-04-03 12:17:24 -0700186 return b, err
Damien Neil99f24c32019-03-13 17:06:42 -0700187 }
Joe Tsai378c1322019-04-25 23:48:08 -0700188 b = append(b, m.GetUnknown()...)
Damien Neil8c86fc52019-06-19 09:28:29 -0700189 return b, nil
Damien Neil99f24c32019-03-13 17:06:42 -0700190}
191
Damien Neil01c0e8d2019-11-12 12:33:12 -0800192// rangeFields visits fields in a defined order when deterministic serialization is enabled.
Joe Tsai378c1322019-04-25 23:48:08 -0700193func (o MarshalOptions) rangeFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
Damien Neil99f24c32019-03-13 17:06:42 -0700194 if !o.Deterministic {
Joe Tsai378c1322019-04-25 23:48:08 -0700195 m.Range(f)
Damien Neil99f24c32019-03-13 17:06:42 -0700196 return
197 }
Damien Neila9940822019-06-24 12:58:17 -0700198 var fds []protoreflect.FieldDescriptor
Joe Tsai378c1322019-04-25 23:48:08 -0700199 m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
200 fds = append(fds, fd)
Damien Neil99f24c32019-03-13 17:06:42 -0700201 return true
202 })
Joe Tsai378c1322019-04-25 23:48:08 -0700203 sort.Slice(fds, func(a, b int) bool {
Damien Neil01c0e8d2019-11-12 12:33:12 -0800204 return fieldsort.Less(fds[a], fds[b])
Damien Neil99f24c32019-03-13 17:06:42 -0700205 })
Joe Tsai378c1322019-04-25 23:48:08 -0700206 for _, fd := range fds {
207 if !f(fd, m.Get(fd)) {
Damien Neil99f24c32019-03-13 17:06:42 -0700208 break
209 }
210 }
211}
212
Joe Tsaiac31a352019-05-13 14:32:56 -0700213func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
Damien Neil99f24c32019-03-13 17:06:42 -0700214 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700215 case fd.IsList():
Joe Tsai378c1322019-04-25 23:48:08 -0700216 return o.marshalList(b, fd, value.List())
Joe Tsaiac31a352019-05-13 14:32:56 -0700217 case fd.IsMap():
Joe Tsai378c1322019-04-25 23:48:08 -0700218 return o.marshalMap(b, fd, value.Map())
Damien Neil99f24c32019-03-13 17:06:42 -0700219 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700220 b = wire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()])
221 return o.marshalSingular(b, fd, value)
Damien Neil99f24c32019-03-13 17:06:42 -0700222 }
223}
224
Joe Tsai378c1322019-04-25 23:48:08 -0700225func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
226 if fd.IsPacked() && list.Len() > 0 {
227 b = wire.AppendTag(b, fd.Number(), wire.BytesType)
Joe Tsaiac31a352019-05-13 14:32:56 -0700228 b, pos := appendSpeculativeLength(b)
Joe Tsaiac31a352019-05-13 14:32:56 -0700229 for i, llen := 0, list.Len(); i < llen; i++ {
230 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700231 b, err = o.marshalSingular(b, fd, list.Get(i))
Damien Neil8c86fc52019-06-19 09:28:29 -0700232 if err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700233 return b, err
234 }
235 }
236 b = finishSpeculativeLength(b, pos)
Damien Neil8c86fc52019-06-19 09:28:29 -0700237 return b, nil
Joe Tsaiac31a352019-05-13 14:32:56 -0700238 }
239
240 kind := fd.Kind()
Joe Tsaiac31a352019-05-13 14:32:56 -0700241 for i, llen := 0, list.Len(); i < llen; i++ {
242 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700243 b = wire.AppendTag(b, fd.Number(), wireTypes[kind])
244 b, err = o.marshalSingular(b, fd, list.Get(i))
Damien Neil8c86fc52019-06-19 09:28:29 -0700245 if err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700246 return b, err
247 }
248 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700249 return b, nil
Joe Tsaiac31a352019-05-13 14:32:56 -0700250}
251
Joe Tsai378c1322019-04-25 23:48:08 -0700252func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
Joe Tsaiac31a352019-05-13 14:32:56 -0700253 keyf := fd.MapKey()
254 valf := fd.MapValue()
Damien Neil99f24c32019-03-13 17:06:42 -0700255 var err error
256 o.rangeMap(mapv, keyf.Kind(), func(key protoreflect.MapKey, value protoreflect.Value) bool {
Joe Tsai378c1322019-04-25 23:48:08 -0700257 b = wire.AppendTag(b, fd.Number(), wire.BytesType)
Damien Neil99f24c32019-03-13 17:06:42 -0700258 var pos int
259 b, pos = appendSpeculativeLength(b)
260
261 b, err = o.marshalField(b, keyf, key.Value())
Damien Neil8c86fc52019-06-19 09:28:29 -0700262 if err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -0700263 return false
264 }
265 b, err = o.marshalField(b, valf, value)
Damien Neil8c86fc52019-06-19 09:28:29 -0700266 if err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -0700267 return false
268 }
Damien Neil99f24c32019-03-13 17:06:42 -0700269 b = finishSpeculativeLength(b, pos)
270 return true
271 })
Damien Neil8c86fc52019-06-19 09:28:29 -0700272 return b, err
Damien Neil99f24c32019-03-13 17:06:42 -0700273}
274
275func (o MarshalOptions) rangeMap(mapv protoreflect.Map, kind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) {
276 if !o.Deterministic {
277 mapv.Range(f)
278 return
279 }
280 mapsort.Range(mapv, kind, f)
281}
282
Damien Neil99f24c32019-03-13 17:06:42 -0700283// When encoding length-prefixed fields, we speculatively set aside some number of bytes
284// for the length, encode the data, and then encode the length (shifting the data if necessary
285// to make room).
286const speculativeLength = 1
287
288func appendSpeculativeLength(b []byte) ([]byte, int) {
289 pos := len(b)
290 b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
291 return b, pos
292}
293
294func finishSpeculativeLength(b []byte, pos int) []byte {
295 mlen := len(b) - pos - speculativeLength
296 msiz := wire.SizeVarint(uint64(mlen))
297 if msiz != speculativeLength {
298 for i := 0; i < msiz-speculativeLength; i++ {
299 b = append(b, 0)
300 }
301 copy(b[pos+msiz:], b[pos+speculativeLength:])
302 b = b[:pos+msiz+mlen]
303 }
304 wire.AppendVarint(b[:pos], uint64(mlen))
305 return b
306}