encode.go 8.2 KB

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