encode.go 8.6 KB

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