blob: bc7da0ca5e6af66f399451eb1f2809ad1bdb2d7a [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 Neile89e6242019-05-13 23:55:40 -070010 "google.golang.org/protobuf/internal/encoding/wire"
Damien Neile89e6242019-05-13 23:55:40 -070011 "google.golang.org/protobuf/internal/mapsort"
12 "google.golang.org/protobuf/internal/pragma"
13 "google.golang.org/protobuf/reflect/protoreflect"
14 "google.golang.org/protobuf/runtime/protoiface"
Damien Neil99f24c32019-03-13 17:06:42 -070015)
16
17// MarshalOptions configures the marshaler.
18//
19// Example usage:
20// b, err := MarshalOptions{Deterministic: true}.Marshal(m)
21type MarshalOptions struct {
Joe Tsaif8b855d2019-07-12 13:37:59 -070022 pragma.NoUnkeyedLiterals
23
Damien Neil96c229a2019-04-03 12:17:24 -070024 // AllowPartial allows messages that have missing required fields to marshal
25 // without returning an error. If AllowPartial is false (the default),
26 // Marshal will return an error if there are any missing required fields.
27 AllowPartial bool
28
Damien Neil99f24c32019-03-13 17:06:42 -070029 // Deterministic controls whether the same message will always be
30 // serialized to the same bytes within the same binary.
31 //
32 // Setting this option guarantees that repeated serialization of
33 // the same message will return the same bytes, and that different
34 // processes of the same binary (which may be executing on different
35 // machines) will serialize equal messages to the same bytes.
Joe Tsaif8b855d2019-07-12 13:37:59 -070036 // It has no effect on the resulting size of the encoded message compared
37 // to a non-deterministic marshal.
Damien Neil99f24c32019-03-13 17:06:42 -070038 //
39 // Note that the deterministic serialization is NOT canonical across
40 // languages. It is not guaranteed to remain stable over time. It is
41 // unstable across different builds with schema changes due to unknown
42 // fields. Users who need canonical serialization (e.g., persistent
43 // storage in a canonical form, fingerprinting, etc.) must define
44 // their own canonicalization specification and implement their own
45 // serializer rather than relying on this API.
46 //
47 // If deterministic serialization is requested, map entries will be
48 // sorted by keys in lexographical order. This is an implementation
49 // detail and subject to change.
50 Deterministic bool
51
Damien Neil03e74862019-04-07 18:18:31 -070052 // UseCachedSize indicates that the result of a previous Size call
53 // may be reused.
54 //
55 // Setting this option asserts that:
56 //
57 // 1. Size has previously been called on this message with identical
58 // options (except for UseCachedSize itself).
59 //
60 // 2. The message and all its submessages have not changed in any
61 // way since the Size call.
62 //
63 // If either of these invariants is broken, the results are undefined
64 // but may include panics or invalid output.
65 //
66 // Implementations MAY take this option into account to provide
67 // better performance, but there is no guarantee that they will do so.
68 // There is absolutely no guarantee that Size followed by Marshal with
69 // UseCachedSize set will perform equivalently to Marshal alone.
70 UseCachedSize bool
Damien Neil99f24c32019-03-13 17:06:42 -070071}
72
Damien Neil0d3e8cc2019-04-01 13:31:55 -070073var _ = protoiface.MarshalOptions(MarshalOptions{})
74
Damien Neil99f24c32019-03-13 17:06:42 -070075// Marshal returns the wire-format encoding of m.
76func Marshal(m Message) ([]byte, error) {
77 return MarshalOptions{}.MarshalAppend(nil, m)
78}
79
80// Marshal returns the wire-format encoding of m.
81func (o MarshalOptions) Marshal(m Message) ([]byte, error) {
Damien Neil0d3e8cc2019-04-01 13:31:55 -070082 return o.MarshalAppend(nil, m)
Damien Neil99f24c32019-03-13 17:06:42 -070083}
84
85// MarshalAppend appends the wire-format encoding of m to b,
86// returning the result.
87func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
Joe Tsai0f81b382019-07-10 23:14:31 -070088 out, err := o.marshalMessage(b, m.ProtoReflect())
Damien Neil8c86fc52019-06-19 09:28:29 -070089 if err != nil {
90 return nil, err
Damien Neil0d3e8cc2019-04-01 13:31:55 -070091 }
Joe Tsaif8b855d2019-07-12 13:37:59 -070092 if o.AllowPartial {
Damien Neil8c86fc52019-06-19 09:28:29 -070093 return out, nil
Damien Neil4686e232019-04-05 13:31:40 -070094 }
Damien Neil8c86fc52019-06-19 09:28:29 -070095 return out, IsInitialized(m)
Damien Neil99f24c32019-03-13 17:06:42 -070096}
97
Joe Tsai0f81b382019-07-10 23:14:31 -070098func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
99 if methods := protoMethods(m); methods != nil && methods.MarshalAppend != nil &&
Joe Tsaif8b855d2019-07-12 13:37:59 -0700100 !(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) {
101 sz := methods.Size(m, protoiface.MarshalOptions(o))
102 if cap(b) < len(b)+sz {
103 x := make([]byte, len(b), len(b)+sz)
104 copy(x, b)
105 b = x
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700106 }
Joe Tsaif8b855d2019-07-12 13:37:59 -0700107 o.UseCachedSize = true
Joe Tsai0f81b382019-07-10 23:14:31 -0700108 return methods.MarshalAppend(b, m, protoiface.MarshalOptions(o))
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700109 }
Joe Tsai0f81b382019-07-10 23:14:31 -0700110 return o.marshalMessageSlow(b, m)
Damien Neil0d3e8cc2019-04-01 13:31:55 -0700111}
112
Joe Tsai0f81b382019-07-10 23:14:31 -0700113func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
Damien Neil99f24c32019-03-13 17:06:42 -0700114 // There are many choices for what order we visit fields in. The default one here
115 // is chosen for reasonable efficiency and simplicity given the protoreflect API.
Joe Tsai378c1322019-04-25 23:48:08 -0700116 // It is not deterministic, since Message.Range does not return fields in any
Damien Neil99f24c32019-03-13 17:06:42 -0700117 // defined order.
118 //
119 // When using deterministic serialization, we sort the known fields by field number.
Damien Neil99f24c32019-03-13 17:06:42 -0700120 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700121 o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
122 b, err = o.marshalField(b, fd, v)
Damien Neil8c86fc52019-06-19 09:28:29 -0700123 return err == nil
Damien Neil99f24c32019-03-13 17:06:42 -0700124 })
125 if err != nil {
Damien Neil96c229a2019-04-03 12:17:24 -0700126 return b, err
Damien Neil99f24c32019-03-13 17:06:42 -0700127 }
Joe Tsai378c1322019-04-25 23:48:08 -0700128 b = append(b, m.GetUnknown()...)
Damien Neil8c86fc52019-06-19 09:28:29 -0700129 return b, nil
Damien Neil99f24c32019-03-13 17:06:42 -0700130}
131
Joe Tsai378c1322019-04-25 23:48:08 -0700132// rangeFields visits fields in field number order when deterministic
Damien Neil99f24c32019-03-13 17:06:42 -0700133// serialization is enabled.
Joe Tsai378c1322019-04-25 23:48:08 -0700134func (o MarshalOptions) rangeFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
Damien Neil99f24c32019-03-13 17:06:42 -0700135 if !o.Deterministic {
Joe Tsai378c1322019-04-25 23:48:08 -0700136 m.Range(f)
Damien Neil99f24c32019-03-13 17:06:42 -0700137 return
138 }
Damien Neila9940822019-06-24 12:58:17 -0700139 var fds []protoreflect.FieldDescriptor
Joe Tsai378c1322019-04-25 23:48:08 -0700140 m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
141 fds = append(fds, fd)
Damien Neil99f24c32019-03-13 17:06:42 -0700142 return true
143 })
Joe Tsai378c1322019-04-25 23:48:08 -0700144 sort.Slice(fds, func(a, b int) bool {
145 return fds[a].Number() < fds[b].Number()
Damien Neil99f24c32019-03-13 17:06:42 -0700146 })
Joe Tsai378c1322019-04-25 23:48:08 -0700147 for _, fd := range fds {
148 if !f(fd, m.Get(fd)) {
Damien Neil99f24c32019-03-13 17:06:42 -0700149 break
150 }
151 }
152}
153
Joe Tsaiac31a352019-05-13 14:32:56 -0700154func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
Damien Neil99f24c32019-03-13 17:06:42 -0700155 switch {
Joe Tsaiac31a352019-05-13 14:32:56 -0700156 case fd.IsList():
Joe Tsai378c1322019-04-25 23:48:08 -0700157 return o.marshalList(b, fd, value.List())
Joe Tsaiac31a352019-05-13 14:32:56 -0700158 case fd.IsMap():
Joe Tsai378c1322019-04-25 23:48:08 -0700159 return o.marshalMap(b, fd, value.Map())
Damien Neil99f24c32019-03-13 17:06:42 -0700160 default:
Joe Tsai378c1322019-04-25 23:48:08 -0700161 b = wire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()])
162 return o.marshalSingular(b, fd, value)
Damien Neil99f24c32019-03-13 17:06:42 -0700163 }
164}
165
Joe Tsai378c1322019-04-25 23:48:08 -0700166func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
167 if fd.IsPacked() && list.Len() > 0 {
168 b = wire.AppendTag(b, fd.Number(), wire.BytesType)
Joe Tsaiac31a352019-05-13 14:32:56 -0700169 b, pos := appendSpeculativeLength(b)
Joe Tsaiac31a352019-05-13 14:32:56 -0700170 for i, llen := 0, list.Len(); i < llen; i++ {
171 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700172 b, err = o.marshalSingular(b, fd, list.Get(i))
Damien Neil8c86fc52019-06-19 09:28:29 -0700173 if err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700174 return b, err
175 }
176 }
177 b = finishSpeculativeLength(b, pos)
Damien Neil8c86fc52019-06-19 09:28:29 -0700178 return b, nil
Joe Tsaiac31a352019-05-13 14:32:56 -0700179 }
180
181 kind := fd.Kind()
Joe Tsaiac31a352019-05-13 14:32:56 -0700182 for i, llen := 0, list.Len(); i < llen; i++ {
183 var err error
Joe Tsai378c1322019-04-25 23:48:08 -0700184 b = wire.AppendTag(b, fd.Number(), wireTypes[kind])
185 b, err = o.marshalSingular(b, fd, list.Get(i))
Damien Neil8c86fc52019-06-19 09:28:29 -0700186 if err != nil {
Joe Tsaiac31a352019-05-13 14:32:56 -0700187 return b, err
188 }
189 }
Damien Neil8c86fc52019-06-19 09:28:29 -0700190 return b, nil
Joe Tsaiac31a352019-05-13 14:32:56 -0700191}
192
Joe Tsai378c1322019-04-25 23:48:08 -0700193func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
Joe Tsaiac31a352019-05-13 14:32:56 -0700194 keyf := fd.MapKey()
195 valf := fd.MapValue()
Damien Neil99f24c32019-03-13 17:06:42 -0700196 var err error
197 o.rangeMap(mapv, keyf.Kind(), func(key protoreflect.MapKey, value protoreflect.Value) bool {
Joe Tsai378c1322019-04-25 23:48:08 -0700198 b = wire.AppendTag(b, fd.Number(), wire.BytesType)
Damien Neil99f24c32019-03-13 17:06:42 -0700199 var pos int
200 b, pos = appendSpeculativeLength(b)
201
202 b, err = o.marshalField(b, keyf, key.Value())
Damien Neil8c86fc52019-06-19 09:28:29 -0700203 if err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -0700204 return false
205 }
206 b, err = o.marshalField(b, valf, value)
Damien Neil8c86fc52019-06-19 09:28:29 -0700207 if err != nil {
Damien Neil99f24c32019-03-13 17:06:42 -0700208 return false
209 }
Damien Neil99f24c32019-03-13 17:06:42 -0700210 b = finishSpeculativeLength(b, pos)
211 return true
212 })
Damien Neil8c86fc52019-06-19 09:28:29 -0700213 return b, err
Damien Neil99f24c32019-03-13 17:06:42 -0700214}
215
216func (o MarshalOptions) rangeMap(mapv protoreflect.Map, kind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) {
217 if !o.Deterministic {
218 mapv.Range(f)
219 return
220 }
221 mapsort.Range(mapv, kind, f)
222}
223
Damien Neil99f24c32019-03-13 17:06:42 -0700224// When encoding length-prefixed fields, we speculatively set aside some number of bytes
225// for the length, encode the data, and then encode the length (shifting the data if necessary
226// to make room).
227const speculativeLength = 1
228
229func appendSpeculativeLength(b []byte) ([]byte, int) {
230 pos := len(b)
231 b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
232 return b, pos
233}
234
235func finishSpeculativeLength(b []byte, pos int) []byte {
236 mlen := len(b) - pos - speculativeLength
237 msiz := wire.SizeVarint(uint64(mlen))
238 if msiz != speculativeLength {
239 for i := 0; i < msiz-speculativeLength; i++ {
240 b = append(b, 0)
241 }
242 copy(b[pos+msiz:], b[pos+speculativeLength:])
243 b = b[:pos+msiz+mlen]
244 }
245 wire.AppendVarint(b[:pos], uint64(mlen))
246 return b
247}