Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 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. |
| 4 | |
| 5 | package proto |
| 6 | |
| 7 | import ( |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 8 | "sort" |
| 9 | |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 10 | "google.golang.org/protobuf/internal/encoding/messageset" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 11 | "google.golang.org/protobuf/internal/encoding/wire" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 12 | "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 Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | // MarshalOptions configures the marshaler. |
| 19 | // |
| 20 | // Example usage: |
| 21 | // b, err := MarshalOptions{Deterministic: true}.Marshal(m) |
| 22 | type MarshalOptions struct { |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 23 | pragma.NoUnkeyedLiterals |
| 24 | |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 25 | // 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 Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 30 | // 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 Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 37 | // It has no effect on the resulting size of the encoded message compared |
| 38 | // to a non-deterministic marshal. |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 39 | // |
| 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 Neil | 03e7486 | 2019-04-07 18:18:31 -0700 | [diff] [blame] | 53 | // 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 Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 74 | var _ = protoiface.MarshalOptions(MarshalOptions{}) |
| 75 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 76 | // Marshal returns the wire-format encoding of m. |
| 77 | func Marshal(m Message) ([]byte, error) { |
| 78 | return MarshalOptions{}.MarshalAppend(nil, m) |
| 79 | } |
| 80 | |
| 81 | // Marshal returns the wire-format encoding of m. |
| 82 | func (o MarshalOptions) Marshal(m Message) ([]byte, error) { |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 83 | return o.MarshalAppend(nil, m) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | // MarshalAppend appends the wire-format encoding of m to b, |
| 87 | // returning the result. |
| 88 | func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) { |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 89 | out, err := o.marshalMessage(b, m.ProtoReflect()) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 90 | if err != nil { |
Damien Neil | 37ef691 | 2019-09-25 16:51:15 -0700 | [diff] [blame] | 91 | return out, err |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 92 | } |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 93 | if o.AllowPartial { |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 94 | return out, nil |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 95 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 96 | return out, IsInitialized(m) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 99 | func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) { |
| 100 | if methods := protoMethods(m); methods != nil && methods.MarshalAppend != nil && |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 101 | !(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) { |
| 102 | sz := methods.Size(m, protoiface.MarshalOptions(o)) |
| 103 | if cap(b) < len(b)+sz { |
Damien Neil | 1e5516a | 2019-09-27 14:31:10 -0700 | [diff] [blame^] | 104 | x := make([]byte, len(b), growcap(cap(b), len(b)+sz)) |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 105 | copy(x, b) |
| 106 | b = x |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 107 | } |
Joe Tsai | f8b855d | 2019-07-12 13:37:59 -0700 | [diff] [blame] | 108 | o.UseCachedSize = true |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 109 | return methods.MarshalAppend(b, m, protoiface.MarshalOptions(o)) |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 110 | } |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 111 | return o.marshalMessageSlow(b, m) |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Damien Neil | 1e5516a | 2019-09-27 14:31:10 -0700 | [diff] [blame^] | 114 | // growcap scales up the capacity of a slice. |
| 115 | // |
| 116 | // Given a slice with a current capacity of oldcap and a desired |
| 117 | // capacity of wantcap, growcap returns a new capacity >= wantcap. |
| 118 | // |
| 119 | // The algorithm is mostly identical to the one used by append as of Go 1.14. |
| 120 | func growcap(oldcap, wantcap int) (newcap int) { |
| 121 | if wantcap > oldcap*2 { |
| 122 | newcap = wantcap |
| 123 | } else if oldcap < 1024 { |
| 124 | // The Go 1.14 runtime takes this case when len(s) < 1024, |
| 125 | // not when cap(s) < 1024. The difference doesn't seem |
| 126 | // significant here. |
| 127 | newcap = oldcap * 2 |
| 128 | } else { |
| 129 | newcap = oldcap |
| 130 | for 0 < newcap && newcap < wantcap { |
| 131 | newcap += newcap / 4 |
| 132 | } |
| 133 | if newcap <= 0 { |
| 134 | newcap = wantcap |
| 135 | } |
| 136 | } |
| 137 | return newcap |
| 138 | } |
| 139 | |
Joe Tsai | 0f81b38 | 2019-07-10 23:14:31 -0700 | [diff] [blame] | 140 | func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) { |
Damien Neil | 302cb32 | 2019-06-19 15:22:13 -0700 | [diff] [blame] | 141 | if messageset.IsMessageSet(m.Descriptor()) { |
| 142 | return marshalMessageSet(b, m, o) |
| 143 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 144 | // There are many choices for what order we visit fields in. The default one here |
| 145 | // is chosen for reasonable efficiency and simplicity given the protoreflect API. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 146 | // It is not deterministic, since Message.Range does not return fields in any |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 147 | // defined order. |
| 148 | // |
| 149 | // When using deterministic serialization, we sort the known fields by field number. |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 150 | var err error |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 151 | o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { |
| 152 | b, err = o.marshalField(b, fd, v) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 153 | return err == nil |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 154 | }) |
| 155 | if err != nil { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 156 | return b, err |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 157 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 158 | b = append(b, m.GetUnknown()...) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 159 | return b, nil |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 162 | // rangeFields visits fields in field number order when deterministic |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 163 | // serialization is enabled. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 164 | func (o MarshalOptions) rangeFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 165 | if !o.Deterministic { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 166 | m.Range(f) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 167 | return |
| 168 | } |
Damien Neil | a994082 | 2019-06-24 12:58:17 -0700 | [diff] [blame] | 169 | var fds []protoreflect.FieldDescriptor |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 170 | m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool { |
| 171 | fds = append(fds, fd) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 172 | return true |
| 173 | }) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 174 | sort.Slice(fds, func(a, b int) bool { |
| 175 | return fds[a].Number() < fds[b].Number() |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 176 | }) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 177 | for _, fd := range fds { |
| 178 | if !f(fd, m.Get(fd)) { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 179 | break |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 184 | func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 185 | switch { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 186 | case fd.IsList(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 187 | return o.marshalList(b, fd, value.List()) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 188 | case fd.IsMap(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 189 | return o.marshalMap(b, fd, value.Map()) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 190 | default: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 191 | b = wire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()]) |
| 192 | return o.marshalSingular(b, fd, value) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 196 | func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) { |
| 197 | if fd.IsPacked() && list.Len() > 0 { |
| 198 | b = wire.AppendTag(b, fd.Number(), wire.BytesType) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 199 | b, pos := appendSpeculativeLength(b) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 200 | for i, llen := 0, list.Len(); i < llen; i++ { |
| 201 | var err error |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 202 | b, err = o.marshalSingular(b, fd, list.Get(i)) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 203 | if err != nil { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 204 | return b, err |
| 205 | } |
| 206 | } |
| 207 | b = finishSpeculativeLength(b, pos) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 208 | return b, nil |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | kind := fd.Kind() |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 212 | for i, llen := 0, list.Len(); i < llen; i++ { |
| 213 | var err error |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 214 | b = wire.AppendTag(b, fd.Number(), wireTypes[kind]) |
| 215 | b, err = o.marshalSingular(b, fd, list.Get(i)) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 216 | if err != nil { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 217 | return b, err |
| 218 | } |
| 219 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 220 | return b, nil |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 221 | } |
| 222 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 223 | func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 224 | keyf := fd.MapKey() |
| 225 | valf := fd.MapValue() |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 226 | var err error |
| 227 | o.rangeMap(mapv, keyf.Kind(), func(key protoreflect.MapKey, value protoreflect.Value) bool { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 228 | b = wire.AppendTag(b, fd.Number(), wire.BytesType) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 229 | var pos int |
| 230 | b, pos = appendSpeculativeLength(b) |
| 231 | |
| 232 | b, err = o.marshalField(b, keyf, key.Value()) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 233 | if err != nil { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 234 | return false |
| 235 | } |
| 236 | b, err = o.marshalField(b, valf, value) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 237 | if err != nil { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 238 | return false |
| 239 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 240 | b = finishSpeculativeLength(b, pos) |
| 241 | return true |
| 242 | }) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 243 | return b, err |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | func (o MarshalOptions) rangeMap(mapv protoreflect.Map, kind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) { |
| 247 | if !o.Deterministic { |
| 248 | mapv.Range(f) |
| 249 | return |
| 250 | } |
| 251 | mapsort.Range(mapv, kind, f) |
| 252 | } |
| 253 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 254 | // When encoding length-prefixed fields, we speculatively set aside some number of bytes |
| 255 | // for the length, encode the data, and then encode the length (shifting the data if necessary |
| 256 | // to make room). |
| 257 | const speculativeLength = 1 |
| 258 | |
| 259 | func appendSpeculativeLength(b []byte) ([]byte, int) { |
| 260 | pos := len(b) |
| 261 | b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...) |
| 262 | return b, pos |
| 263 | } |
| 264 | |
| 265 | func finishSpeculativeLength(b []byte, pos int) []byte { |
| 266 | mlen := len(b) - pos - speculativeLength |
| 267 | msiz := wire.SizeVarint(uint64(mlen)) |
| 268 | if msiz != speculativeLength { |
| 269 | for i := 0; i < msiz-speculativeLength; i++ { |
| 270 | b = append(b, 0) |
| 271 | } |
| 272 | copy(b[pos+msiz:], b[pos+speculativeLength:]) |
| 273 | b = b[:pos+msiz+mlen] |
| 274 | } |
| 275 | wire.AppendVarint(b[:pos], uint64(mlen)) |
| 276 | return b |
| 277 | } |