encode.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/messageset"
  8. "google.golang.org/protobuf/internal/encoding/wire"
  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. pragma.NoUnkeyedLiterals
  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. // It has no effect on the resulting size of the encoded message compared
  32. // to a non-deterministic marshal.
  33. //
  34. // Note that the deterministic serialization is NOT canonical across
  35. // languages. It is not guaranteed to remain stable over time. It is
  36. // unstable across different builds with schema changes due to unknown
  37. // fields. Users who need canonical serialization (e.g., persistent
  38. // storage in a canonical form, fingerprinting, etc.) must define
  39. // their own canonicalization specification and implement their own
  40. // serializer rather than relying on this API.
  41. //
  42. // If deterministic serialization is requested, map entries will be
  43. // sorted by keys in lexographical order. This is an implementation
  44. // detail and subject to change.
  45. Deterministic bool
  46. // UseCachedSize indicates that the result of a previous Size call
  47. // may be reused.
  48. //
  49. // Setting this option asserts that:
  50. //
  51. // 1. Size has previously been called on this message with identical
  52. // options (except for UseCachedSize itself).
  53. //
  54. // 2. The message and all its submessages have not changed in any
  55. // way since the Size call.
  56. //
  57. // If either of these invariants is broken, the results are undefined
  58. // but may include panics or invalid output.
  59. //
  60. // Implementations MAY take this option into account to provide
  61. // better performance, but there is no guarantee that they will do so.
  62. // There is absolutely no guarantee that Size followed by Marshal with
  63. // UseCachedSize set will perform equivalently to Marshal alone.
  64. UseCachedSize bool
  65. }
  66. var _ = protoiface.MarshalOptions(MarshalOptions{})
  67. // Marshal returns the wire-format encoding of m.
  68. func Marshal(m Message) ([]byte, error) {
  69. return MarshalOptions{}.MarshalAppend(nil, m)
  70. }
  71. // Marshal returns the wire-format encoding of m.
  72. func (o MarshalOptions) Marshal(m Message) ([]byte, error) {
  73. return o.MarshalAppend(nil, m)
  74. }
  75. // MarshalAppend appends the wire-format encoding of m to b,
  76. // returning the result.
  77. func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
  78. out, err := o.marshalMessage(b, m.ProtoReflect())
  79. if err != nil {
  80. return out, err
  81. }
  82. if o.AllowPartial {
  83. return out, nil
  84. }
  85. return out, IsInitialized(m)
  86. }
  87. func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
  88. if methods := protoMethods(m); methods != nil && methods.MarshalAppend != nil &&
  89. !(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) {
  90. sz := methods.Size(m, protoiface.MarshalOptions(o))
  91. if cap(b) < len(b)+sz {
  92. x := make([]byte, len(b), growcap(cap(b), len(b)+sz))
  93. copy(x, b)
  94. b = x
  95. }
  96. o.UseCachedSize = true
  97. return methods.MarshalAppend(b, m, protoiface.MarshalOptions(o))
  98. }
  99. return o.marshalMessageSlow(b, m)
  100. }
  101. // growcap scales up the capacity of a slice.
  102. //
  103. // Given a slice with a current capacity of oldcap and a desired
  104. // capacity of wantcap, growcap returns a new capacity >= wantcap.
  105. //
  106. // The algorithm is mostly identical to the one used by append as of Go 1.14.
  107. func growcap(oldcap, wantcap int) (newcap int) {
  108. if wantcap > oldcap*2 {
  109. newcap = wantcap
  110. } else if oldcap < 1024 {
  111. // The Go 1.14 runtime takes this case when len(s) < 1024,
  112. // not when cap(s) < 1024. The difference doesn't seem
  113. // significant here.
  114. newcap = oldcap * 2
  115. } else {
  116. newcap = oldcap
  117. for 0 < newcap && newcap < wantcap {
  118. newcap += newcap / 4
  119. }
  120. if newcap <= 0 {
  121. newcap = wantcap
  122. }
  123. }
  124. return newcap
  125. }
  126. func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
  127. if messageset.IsMessageSet(m.Descriptor()) {
  128. return marshalMessageSet(b, m, o)
  129. }
  130. // There are many choices for what order we visit fields in. The default one here
  131. // is chosen for reasonable efficiency and simplicity given the protoreflect API.
  132. // It is not deterministic, since Message.Range does not return fields in any
  133. // defined order.
  134. //
  135. // When using deterministic serialization, we sort the known fields by field number.
  136. var err error
  137. o.rangeFields(m, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  138. b, err = o.marshalField(b, fd, v)
  139. return err == nil
  140. })
  141. if err != nil {
  142. return b, err
  143. }
  144. b = append(b, m.GetUnknown()...)
  145. return b, nil
  146. }
  147. // rangeFields visits fields in field number order when deterministic
  148. // serialization is enabled.
  149. func (o MarshalOptions) rangeFields(m protoreflect.Message, f func(protoreflect.FieldDescriptor, protoreflect.Value) bool) {
  150. if !o.Deterministic {
  151. m.Range(f)
  152. return
  153. }
  154. var fds []protoreflect.FieldDescriptor
  155. m.Range(func(fd protoreflect.FieldDescriptor, _ protoreflect.Value) bool {
  156. fds = append(fds, fd)
  157. return true
  158. })
  159. sort.Slice(fds, func(a, b int) bool {
  160. return fds[a].Number() < fds[b].Number()
  161. })
  162. for _, fd := range fds {
  163. if !f(fd, m.Get(fd)) {
  164. break
  165. }
  166. }
  167. }
  168. func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
  169. switch {
  170. case fd.IsList():
  171. return o.marshalList(b, fd, value.List())
  172. case fd.IsMap():
  173. return o.marshalMap(b, fd, value.Map())
  174. default:
  175. b = wire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()])
  176. return o.marshalSingular(b, fd, value)
  177. }
  178. }
  179. func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
  180. if fd.IsPacked() && list.Len() > 0 {
  181. b = wire.AppendTag(b, fd.Number(), wire.BytesType)
  182. b, pos := appendSpeculativeLength(b)
  183. for i, llen := 0, list.Len(); i < llen; i++ {
  184. var err error
  185. b, err = o.marshalSingular(b, fd, list.Get(i))
  186. if err != nil {
  187. return b, err
  188. }
  189. }
  190. b = finishSpeculativeLength(b, pos)
  191. return b, nil
  192. }
  193. kind := fd.Kind()
  194. for i, llen := 0, list.Len(); i < llen; i++ {
  195. var err error
  196. b = wire.AppendTag(b, fd.Number(), wireTypes[kind])
  197. b, err = o.marshalSingular(b, fd, list.Get(i))
  198. if err != nil {
  199. return b, err
  200. }
  201. }
  202. return b, nil
  203. }
  204. func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
  205. keyf := fd.MapKey()
  206. valf := fd.MapValue()
  207. var err error
  208. o.rangeMap(mapv, keyf.Kind(), func(key protoreflect.MapKey, value protoreflect.Value) bool {
  209. b = wire.AppendTag(b, fd.Number(), wire.BytesType)
  210. var pos int
  211. b, pos = appendSpeculativeLength(b)
  212. b, err = o.marshalField(b, keyf, key.Value())
  213. if err != nil {
  214. return false
  215. }
  216. b, err = o.marshalField(b, valf, value)
  217. if err != nil {
  218. return false
  219. }
  220. b = finishSpeculativeLength(b, pos)
  221. return true
  222. })
  223. return b, err
  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. // When encoding length-prefixed fields, we speculatively set aside some number of bytes
  233. // for the length, encode the data, and then encode the length (shifting the data if necessary
  234. // to make room).
  235. const speculativeLength = 1
  236. func appendSpeculativeLength(b []byte) ([]byte, int) {
  237. pos := len(b)
  238. b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
  239. return b, pos
  240. }
  241. func finishSpeculativeLength(b []byte, pos int) []byte {
  242. mlen := len(b) - pos - speculativeLength
  243. msiz := wire.SizeVarint(uint64(mlen))
  244. if msiz != speculativeLength {
  245. for i := 0; i < msiz-speculativeLength; i++ {
  246. b = append(b, 0)
  247. }
  248. copy(b[pos+msiz:], b[pos+speculativeLength:])
  249. b = b[:pos+msiz+mlen]
  250. }
  251. wire.AppendVarint(b[:pos], uint64(mlen))
  252. return b
  253. }