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 | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 10 | "google.golang.org/protobuf/internal/encoding/wire" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 11 | "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 Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | // MarshalOptions configures the marshaler. |
| 18 | // |
| 19 | // Example usage: |
| 20 | // b, err := MarshalOptions{Deterministic: true}.Marshal(m) |
| 21 | type MarshalOptions struct { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 22 | // AllowPartial allows messages that have missing required fields to marshal |
| 23 | // without returning an error. If AllowPartial is false (the default), |
| 24 | // Marshal will return an error if there are any missing required fields. |
| 25 | AllowPartial bool |
| 26 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 27 | // Deterministic controls whether the same message will always be |
| 28 | // serialized to the same bytes within the same binary. |
| 29 | // |
| 30 | // Setting this option guarantees that repeated serialization of |
| 31 | // the same message will return the same bytes, and that different |
| 32 | // processes of the same binary (which may be executing on different |
| 33 | // machines) will serialize equal messages to the same bytes. |
| 34 | // |
| 35 | // Note that the deterministic serialization is NOT canonical across |
| 36 | // languages. It is not guaranteed to remain stable over time. It is |
| 37 | // unstable across different builds with schema changes due to unknown |
| 38 | // fields. Users who need canonical serialization (e.g., persistent |
| 39 | // storage in a canonical form, fingerprinting, etc.) must define |
| 40 | // their own canonicalization specification and implement their own |
| 41 | // serializer rather than relying on this API. |
| 42 | // |
| 43 | // If deterministic serialization is requested, map entries will be |
| 44 | // sorted by keys in lexographical order. This is an implementation |
| 45 | // detail and subject to change. |
| 46 | Deterministic bool |
| 47 | |
Damien Neil | 03e7486 | 2019-04-07 18:18:31 -0700 | [diff] [blame] | 48 | // UseCachedSize indicates that the result of a previous Size call |
| 49 | // may be reused. |
| 50 | // |
| 51 | // Setting this option asserts that: |
| 52 | // |
| 53 | // 1. Size has previously been called on this message with identical |
| 54 | // options (except for UseCachedSize itself). |
| 55 | // |
| 56 | // 2. The message and all its submessages have not changed in any |
| 57 | // way since the Size call. |
| 58 | // |
| 59 | // If either of these invariants is broken, the results are undefined |
| 60 | // but may include panics or invalid output. |
| 61 | // |
| 62 | // Implementations MAY take this option into account to provide |
| 63 | // better performance, but there is no guarantee that they will do so. |
| 64 | // There is absolutely no guarantee that Size followed by Marshal with |
| 65 | // UseCachedSize set will perform equivalently to Marshal alone. |
| 66 | UseCachedSize bool |
| 67 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 68 | pragma.NoUnkeyedLiterals |
| 69 | } |
| 70 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 71 | var _ = protoiface.MarshalOptions(MarshalOptions{}) |
| 72 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 73 | // Marshal returns the wire-format encoding of m. |
| 74 | func Marshal(m Message) ([]byte, error) { |
| 75 | return MarshalOptions{}.MarshalAppend(nil, m) |
| 76 | } |
| 77 | |
| 78 | // Marshal returns the wire-format encoding of m. |
| 79 | func (o MarshalOptions) Marshal(m Message) ([]byte, error) { |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 80 | return o.MarshalAppend(nil, m) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | // MarshalAppend appends the wire-format encoding of m to b, |
| 84 | // returning the result. |
| 85 | func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 86 | // Set AllowPartial in recursive calls to marshal to avoid duplicating |
| 87 | // effort with the single initialization check below. |
| 88 | allowPartial := o.AllowPartial |
| 89 | o.AllowPartial = true |
Damien Neil | 3016b73 | 2019-04-07 12:43:10 -0700 | [diff] [blame] | 90 | out, err := o.marshalMessageFast(b, m) |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 91 | if err == errInternalNoFast { |
Damien Neil | 3016b73 | 2019-04-07 12:43:10 -0700 | [diff] [blame] | 92 | out, err = o.marshalMessage(b, m.ProtoReflect()) |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 93 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 94 | if err != nil { |
| 95 | return nil, err |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 96 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 97 | if allowPartial { |
| 98 | return out, nil |
Damien Neil | 4686e23 | 2019-04-05 13:31:40 -0700 | [diff] [blame] | 99 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 100 | return out, IsInitialized(m) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 103 | func (o MarshalOptions) marshalMessageFast(b []byte, m Message) ([]byte, error) { |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 104 | methods := protoMethods(m) |
| 105 | if methods == nil || |
| 106 | methods.MarshalAppend == nil || |
| 107 | (o.Deterministic && methods.Flags&protoiface.MethodFlagDeterministicMarshal == 0) { |
| 108 | return nil, errInternalNoFast |
| 109 | } |
| 110 | if methods.Size != nil { |
| 111 | sz := methods.Size(m) |
| 112 | if cap(b) < len(b)+sz { |
| 113 | x := make([]byte, len(b), len(b)+sz) |
| 114 | copy(x, b) |
| 115 | b = x |
| 116 | } |
Damien Neil | 03e7486 | 2019-04-07 18:18:31 -0700 | [diff] [blame] | 117 | o.UseCachedSize = true |
Damien Neil | 0d3e8cc | 2019-04-01 13:31:55 -0700 | [diff] [blame] | 118 | } |
| 119 | return methods.MarshalAppend(b, m, protoiface.MarshalOptions(o)) |
| 120 | } |
| 121 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 122 | func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) { |
| 123 | // There are many choices for what order we visit fields in. The default one here |
| 124 | // is chosen for reasonable efficiency and simplicity given the protoreflect API. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 125 | // 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] | 126 | // defined order. |
| 127 | // |
| 128 | // When using deterministic serialization, we sort the known fields by field number. |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 129 | var err error |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 130 | o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool { |
| 131 | b, err = o.marshalField(b, fd, v) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 132 | return err == nil |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 133 | }) |
| 134 | if err != nil { |
Damien Neil | 96c229a | 2019-04-03 12:17:24 -0700 | [diff] [blame] | 135 | return b, err |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 136 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 137 | b = append(b, m.GetUnknown()...) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 138 | return b, nil |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 141 | // rangeFields visits fields in field number order when deterministic |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 142 | // serialization is enabled. |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 143 | 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] | 144 | if !o.Deterministic { |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 145 | m.Range(f) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 146 | return |
| 147 | } |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 148 | fds := make([]protoreflect.FieldDescriptor, 0, m.Len()) |
| 149 | m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool { |
| 150 | fds = append(fds, fd) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 151 | return true |
| 152 | }) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 153 | sort.Slice(fds, func(a, b int) bool { |
| 154 | return fds[a].Number() < fds[b].Number() |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 155 | }) |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 156 | for _, fd := range fds { |
| 157 | if !f(fd, m.Get(fd)) { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 158 | break |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 163 | 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] | 164 | switch { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 165 | case fd.IsList(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 166 | return o.marshalList(b, fd, value.List()) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 167 | case fd.IsMap(): |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 168 | return o.marshalMap(b, fd, value.Map()) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 169 | default: |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 170 | b = wire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()]) |
| 171 | return o.marshalSingular(b, fd, value) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 175 | func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) { |
| 176 | if fd.IsPacked() && list.Len() > 0 { |
| 177 | b = wire.AppendTag(b, fd.Number(), wire.BytesType) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 178 | b, pos := appendSpeculativeLength(b) |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 179 | for i, llen := 0, list.Len(); i < llen; i++ { |
| 180 | var err error |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 181 | b, err = o.marshalSingular(b, fd, list.Get(i)) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 182 | if err != nil { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 183 | return b, err |
| 184 | } |
| 185 | } |
| 186 | b = finishSpeculativeLength(b, pos) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 187 | return b, nil |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | kind := fd.Kind() |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 191 | for i, llen := 0, list.Len(); i < llen; i++ { |
| 192 | var err error |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 193 | b = wire.AppendTag(b, fd.Number(), wireTypes[kind]) |
| 194 | b, err = o.marshalSingular(b, fd, list.Get(i)) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 195 | if err != nil { |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 196 | return b, err |
| 197 | } |
| 198 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 199 | return b, nil |
Joe Tsai | ac31a35 | 2019-05-13 14:32:56 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Joe Tsai | 378c132 | 2019-04-25 23:48:08 -0700 | [diff] [blame] | 202 | 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] | 203 | keyf := fd.MapKey() |
| 204 | valf := fd.MapValue() |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 205 | var err error |
| 206 | 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] | 207 | b = wire.AppendTag(b, fd.Number(), wire.BytesType) |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 208 | var pos int |
| 209 | b, pos = appendSpeculativeLength(b) |
| 210 | |
| 211 | b, err = o.marshalField(b, keyf, key.Value()) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 212 | if err != nil { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 213 | return false |
| 214 | } |
| 215 | b, err = o.marshalField(b, valf, value) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 216 | if err != nil { |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 217 | return false |
| 218 | } |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 219 | b = finishSpeculativeLength(b, pos) |
| 220 | return true |
| 221 | }) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame^] | 222 | return b, err |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | func (o MarshalOptions) rangeMap(mapv protoreflect.Map, kind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) { |
| 226 | if !o.Deterministic { |
| 227 | mapv.Range(f) |
| 228 | return |
| 229 | } |
| 230 | mapsort.Range(mapv, kind, f) |
| 231 | } |
| 232 | |
Damien Neil | 99f24c3 | 2019-03-13 17:06:42 -0700 | [diff] [blame] | 233 | // When encoding length-prefixed fields, we speculatively set aside some number of bytes |
| 234 | // for the length, encode the data, and then encode the length (shifting the data if necessary |
| 235 | // to make room). |
| 236 | const speculativeLength = 1 |
| 237 | |
| 238 | func appendSpeculativeLength(b []byte) ([]byte, int) { |
| 239 | pos := len(b) |
| 240 | b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...) |
| 241 | return b, pos |
| 242 | } |
| 243 | |
| 244 | func finishSpeculativeLength(b []byte, pos int) []byte { |
| 245 | mlen := len(b) - pos - speculativeLength |
| 246 | msiz := wire.SizeVarint(uint64(mlen)) |
| 247 | if msiz != speculativeLength { |
| 248 | for i := 0; i < msiz-speculativeLength; i++ { |
| 249 | b = append(b, 0) |
| 250 | } |
| 251 | copy(b[pos+msiz:], b[pos+speculativeLength:]) |
| 252 | b = b[:pos+msiz+mlen] |
| 253 | } |
| 254 | wire.AppendVarint(b[:pos], uint64(mlen)) |
| 255 | return b |
| 256 | } |