blob: 5511d16266644e77685e50aba34982bc558995dc [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 Neile89e6242019-05-13 23:55:40 -070012 "google.golang.org/protobuf/internal/mapsort"
13 "google.golang.org/protobuf/internal/pragma"
14 "google.golang.org/protobuf/reflect/protoreflect"
15 "google.golang.org/protobuf/runtime/protoiface"
Damien Neil99f24c32019-03-13 17:06:42 -070016)
17
18// MarshalOptions configures the marshaler.
19//
20// Example usage:
21// b, err := MarshalOptions{Deterministic: true}.Marshal(m)
22type MarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070023 pragma.NoUnkeyedLiterals
24
Damien Neil96c229a2019-04-03 12:17:24 -070025 // AllowPartial allows messages that have missing required fields to marshal
26 // without returning an error. If AllowPartial is false (the default),
27 // Marshal will return an error if there are any missing required fields.
28 AllowPartial bool
29
Damien Neil99f24c32019-03-13 17:06:42 -070030 // Deterministic controls whether the same message will always be
31 // serialized to the same bytes within the same binary.
32 //
33 // Setting this option guarantees that repeated serialization of
34 // the same message will return the same bytes, and that different
35 // processes of the same binary (which may be executing on different
36 // machines) will serialize equal messages to the same bytes.
Joe Tsaif8b855d2019-07-12 13:37:59 -070037 // It has no effect on the resulting size of the encoded message compared
38 // to a non-deterministic marshal.
Damien Neil99f24c32019-03-13 17:06:42 -070039 //
40 // Note that the deterministic serialization is NOT canonical across
41 // languages. It is not guaranteed to remain stable over time. It is
42 // unstable across different builds with schema changes due to unknown
43 // fields. Users who need canonical serialization (e.g., persistent
44 // storage in a canonical form, fingerprinting, etc.) must define
45 // their own canonicalization specification and implement their own
46 // serializer rather than relying on this API.
47 //
48 // If deterministic serialization is requested, map entries will be
49 // sorted by keys in lexographical order. This is an implementation
50 // detail and subject to change.
51 Deterministic bool
52
Damien Neil03e74862019-04-07 18:18:31 -070053 // UseCachedSize indicates that the result of a previous Size call
54 // may be reused.
55 //
56 // Setting this option asserts that:
57 //
58 // 1. Size has previously been called on this message with identical
59 // options (except for UseCachedSize itself).
60 //
61 // 2. The message and all its submessages have not changed in any
62 // way since the Size call.
63 //
64 // If either of these invariants is broken, the results are undefined
65 // but may include panics or invalid output.
66 //
67 // Implementations MAY take this option into account to provide
68 // better performance, but there is no guarantee that they will do so.
69 // There is absolutely no guarantee that Size followed by Marshal with
70 // UseCachedSize set will perform equivalently to Marshal alone.
71 UseCachedSize bool
Damien Neil99f24c32019-03-13 17:06:42 -070072}
73
Damien Neil0d3e8cc2019-04-01 13:31:55 -070074var _ = protoiface.MarshalOptions(MarshalOptions{})
75
Damien Neil99f24c32019-03-13 17:06:42 -070076// Marshal returns the wire-format encoding of m.
77func Marshal(m Message) ([]byte, error) {
78 return MarshalOptions{}.MarshalAppend(nil, m)
79}
80
81// Marshal returns the wire-format encoding of m.
82func (o MarshalOptions) Marshal(m Message) ([]byte, error) {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070083 return o.MarshalAppend(nil, m)
Damien Neil99f24c32019-03-13 17:06:42 -070084}
85
86// MarshalAppend appends the wire-format encoding of m to b,
87// returning the result.
88func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
Joe Tsai0f81b382019-07-10 23:14:31 -070089 out, err := o.marshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070090 if err != nil {
91 return nil, err
Damien Neil0d3e8cc2019-04-01 13:31:55 -070092 }
Joe Tsaif8b855d2019-07-12 13:37:59 -070093 if o.AllowPartial {
Damien Neil8c86fc52019-06-19 09:28:29 -070094 return out, nil
Damien Neil4686e232019-04-05 13:31:40 -070095 }
Damien Neil8c86fc52019-06-19 09:28:29 -070096 return out, IsInitialized(m)
Damien Neil99f24c32019-03-13 17:06:42 -070097}
98
Joe Tsai0f81b382019-07-10 23:14:31 -070099func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
100 if methods := protoMethods(m); methods != nil && methods.MarshalAppend != nil &&
Joe Tsaif8b855d2019-07-12 13:37:59 -0700101 !(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) {
102 sz := methods.Size(m, protoiface.MarshalOptions(o))
103 if cap(b) < len(b)+sz {
104 x := make([]byte, len(b), len(b)+sz)
105 copy(x, b)
106 b = x
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700107 }
Joe Tsaif8b855d2019-07-12 13:37:59 -0700108 o.UseCachedSize = true
Joe Tsai0f81b382019-07-10 23:14:31 -0700109 return methods.MarshalAppend(b, m, protoiface.MarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700110 }
Joe Tsai0f81b382019-07-10 23:14:31 -0700111 return o.marshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700112}
113
Joe Tsai0f81b382019-07-10 23:14:31 -0700114func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
Damien Neil302cb322019-06-19 15:22:13 -0700115 if messageset.IsMessageSet(m.Descriptor()) {
116 return marshalMessageSet(b, m, o)
117 }
Damien Neil99f24c32019-03-13 17:06:42 -0700118 // There are many choices for what order we visit fields in. The default one here
119 // is chosen for reasonable efficiency and simplicity given the protoreflect API.
Joe Tsai378c1322019-04-25 23:48:08 -0700120 // It is not deterministic, since Message.Range does not return fields in any
Damien Neil99f24c32019-03-13 17:06:42 -0700121 // defined order.
122 //
123 // When using deterministic serialization, we sort the known fields by field number.
Damien Neil99f24c32019-03-13 17:06:42 -0700124 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700125 o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
126 b, err = o.marshalField(b, fd, v)
Damien Neil8c86fc52019-06-19 09:28:29 -0700127 return err == nil
Damien Neil99f24c32019-03-13 17:06:42 -0700128 })
129 if err != nil {
Damien Neil96c229a2019-04-03 12:17:24 -0700130 return b, err
Damien Neil99f24c32019-03-13 17:06:42 -0700131 }
Joe Tsai378c1322019-04-25 23:48:08 -0700132 b = append(b, m.GetUnknown()...)
Damien Neil8c86fc52019-06-19 09:28:29 -0700133 return b, nil
Damien Neil99f24c32019-03-13 17:06:42 -0700134}
135
Joe Tsai378c1322019-04-25 23:48:08 -0700136// rangeFields visits fields in field number order when deterministic
Damien Neil99f24c32019-03-13 17:06:42 -0700137// serialization is enabled.
Joe Tsai378c1322019-04-25 23:48:08 -0700138func (o MarshalOptions) rangeFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
Damien Neil99f24c32019-03-13 17:06:42 -0700139 if !o.Deterministic {
Joe Tsai378c1322019-04-25 23:48:08 -0700140 m.Range(f)
Damien Neil99f24c32019-03-13 17:06:42 -0700141 return
142 }
Damien Neila9940822019-06-24 12:58:17 -0700143 var fds []protoreflect.FieldDescriptor
Joe Tsai378c1322019-04-25 23:48:08 -0700144 m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
145 fds = append(fds, fd)
Damien Neil99f24c32019-03-13 17:06:42 -0700146 return true
147 })
Joe Tsai378c1322019-04-25 23:48:08 -0700148 sort.Slice(fds, func(a, b int) bool {
149 return fds[a].Number() < fds[b].Number()
Damien Neil99f24c32019-03-13 17:06:42 -0700150 })
Joe Tsai378c1322019-04-25 23:48:08 -0700151 for _, fd := range fds {
152 if !f(fd, m.Get(fd)) {
Damien Neil99f24c32019-03-13 17:06:42 -0700153 break
154 }
155 }
156}
157
Joe Tsaiac31a352019-05-13 14:32:56 -0700158func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
Damien Neil99f24c32019-03-13 17:06:42 -0700159 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700160 case fd.IsList():
Joe Tsai378c1322019-04-25 23:48:08 -0700161 return o.marshalList(b, fd, value.List())
Joe Tsaiac31a352019-05-13 14:32:56 -0700162 case fd.IsMap():
Joe Tsai378c1322019-04-25 23:48:08 -0700163 return o.marshalMap(b, fd, value.Map())
Damien Neil99f24c32019-03-13 17:06:42 -0700164 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700165 b = wire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()])
166 return o.marshalSingular(b, fd, value)
Damien Neil99f24c32019-03-13 17:06:42 -0700167 }
168}
169
Joe Tsai378c1322019-04-25 23:48:08 -0700170func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
171 if fd.IsPacked() && list.Len() > 0 {
172 b = wire.AppendTag(b, fd.Number(), wire.BytesType)
Joe Tsaiac31a352019-05-13 14:32:56 -0700173 b, pos := appendSpeculativeLength(b)
Joe Tsaiac31a352019-05-13 14:32:56 -0700174 for i, llen := 0, list.Len(); i < llen; i++ {
175 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700176 b, err = o.marshalSingular(b, fd, list.Get(i))
Damien Neil8c86fc52019-06-19 09:28:29 -0700177 if err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700178 return b, err
179 }
180 }
181 b = finishSpeculativeLength(b, pos)
Damien Neil8c86fc52019-06-19 09:28:29 -0700182 return b, nil
Joe Tsaiac31a352019-05-13 14:32:56 -0700183 }
184
185 kind := fd.Kind()
Joe Tsaiac31a352019-05-13 14:32:56 -0700186 for i, llen := 0, list.Len(); i < llen; i++ {
187 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700188 b = wire.AppendTag(b, fd.Number(), wireTypes[kind])
189 b, err = o.marshalSingular(b, fd, list.Get(i))
Damien Neil8c86fc52019-06-19 09:28:29 -0700190 if err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700191 return b, err
192 }
193 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700194 return b, nil
Joe Tsaiac31a352019-05-13 14:32:56 -0700195}
196
Joe Tsai378c1322019-04-25 23:48:08 -0700197func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
Joe Tsaiac31a352019-05-13 14:32:56 -0700198 keyf := fd.MapKey()
199 valf := fd.MapValue()
Damien Neil99f24c32019-03-13 17:06:42 -0700200 var err error
201 o.rangeMap(mapv, keyf.Kind(), func(key protoreflect.MapKey, value protoreflect.Value) bool {
Joe Tsai378c1322019-04-25 23:48:08 -0700202 b = wire.AppendTag(b, fd.Number(), wire.BytesType)
Damien Neil99f24c32019-03-13 17:06:42 -0700203 var pos int
204 b, pos = appendSpeculativeLength(b)
205
206 b, err = o.marshalField(b, keyf, key.Value())
Damien Neil8c86fc52019-06-19 09:28:29 -0700207 if err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -0700208 return false
209 }
210 b, err = o.marshalField(b, valf, value)
Damien Neil8c86fc52019-06-19 09:28:29 -0700211 if err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -0700212 return false
213 }
Damien Neil99f24c32019-03-13 17:06:42 -0700214 b = finishSpeculativeLength(b, pos)
215 return true
216 })
Damien Neil8c86fc52019-06-19 09:28:29 -0700217 return b, err
Damien Neil99f24c32019-03-13 17:06:42 -0700218}
219
220func (o MarshalOptions) rangeMap(mapv protoreflect.Map, kind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) {
221 if !o.Deterministic {
222 mapv.Range(f)
223 return
224 }
225 mapsort.Range(mapv, kind, f)
226}
227
Damien Neil99f24c32019-03-13 17:06:42 -0700228// When encoding length-prefixed fields, we speculatively set aside some number of bytes
229// for the length, encode the data, and then encode the length (shifting the data if necessary
230// to make room).
231const speculativeLength = 1
232
233func appendSpeculativeLength(b []byte) ([]byte, int) {
234 pos := len(b)
235 b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
236 return b, pos
237}
238
239func finishSpeculativeLength(b []byte, pos int) []byte {
240 mlen := len(b) - pos - speculativeLength
241 msiz := wire.SizeVarint(uint64(mlen))
242 if msiz != speculativeLength {
243 for i := 0; i < msiz-speculativeLength; i++ {
244 b = append(b, 0)
245 }
246 copy(b[pos+msiz:], b[pos+speculativeLength:])
247 b = b[:pos+msiz+mlen]
248 }
249 wire.AppendVarint(b[:pos], uint64(mlen))
250 return b
251}