encode.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. "errors"
  7. "fmt"
  8. "google.golang.org/protobuf/encoding/protowire"
  9. "google.golang.org/protobuf/internal/encoding/messageset"
  10. "google.golang.org/protobuf/internal/order"
  11. "google.golang.org/protobuf/internal/pragma"
  12. "google.golang.org/protobuf/reflect/protoreflect"
  13. "google.golang.org/protobuf/runtime/protoiface"
  14. protoerrors "google.golang.org/protobuf/internal/errors"
  15. )
  16. // MarshalOptions configures the marshaler.
  17. //
  18. // Example usage:
  19. //
  20. // b, err := MarshalOptions{Deterministic: true}.Marshal(m)
  21. type MarshalOptions struct {
  22. pragma.NoUnkeyedLiterals
  23. // AllowPartial allows messages that have missing required fields to marshal
  24. // without returning an error. If AllowPartial is false (the default),
  25. // Marshal will return an error if there are any missing required fields.
  26. AllowPartial bool
  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. // It has no effect on the resulting size of the encoded message compared
  35. // to a non-deterministic marshal.
  36. //
  37. // Note that the deterministic serialization is NOT canonical across
  38. // languages. It is not guaranteed to remain stable over time. It is
  39. // unstable across different builds with schema changes due to unknown
  40. // fields. Users who need canonical serialization (e.g., persistent
  41. // storage in a canonical form, fingerprinting, etc.) must define
  42. // their own canonicalization specification and implement their own
  43. // serializer rather than relying on this API.
  44. //
  45. // If deterministic serialization is requested, map entries will be
  46. // sorted by keys in lexographical order. This is an implementation
  47. // detail and subject to change.
  48. Deterministic bool
  49. // UseCachedSize indicates that the result of a previous Size call
  50. // may be reused.
  51. //
  52. // Setting this option asserts that:
  53. //
  54. // 1. Size has previously been called on this message with identical
  55. // options (except for UseCachedSize itself).
  56. //
  57. // 2. The message and all its submessages have not changed in any
  58. // way since the Size call. For lazily decoded messages, accessing
  59. // a message results in decoding the message, which is a change.
  60. //
  61. // If either of these invariants is violated,
  62. // the results are undefined and may include panics or corrupted output.
  63. //
  64. // Implementations MAY take this option into account to provide
  65. // better performance, but there is no guarantee that they will do so.
  66. // There is absolutely no guarantee that Size followed by Marshal with
  67. // UseCachedSize set will perform equivalently to Marshal alone.
  68. UseCachedSize bool
  69. }
  70. // flags turns the specified MarshalOptions (user-facing) into
  71. // protoiface.MarshalInputFlags (used internally by the marshaler).
  72. //
  73. // See impl.marshalOptions.Options for the inverse operation.
  74. func (o MarshalOptions) flags() protoiface.MarshalInputFlags {
  75. var flags protoiface.MarshalInputFlags
  76. // Note: o.AllowPartial is always forced to true by MarshalOptions.marshal,
  77. // which is why it is not a part of MarshalInputFlags.
  78. if o.Deterministic {
  79. flags |= protoiface.MarshalDeterministic
  80. }
  81. if o.UseCachedSize {
  82. flags |= protoiface.MarshalUseCachedSize
  83. }
  84. return flags
  85. }
  86. // Marshal returns the wire-format encoding of m.
  87. //
  88. // This is the most common entry point for encoding a Protobuf message.
  89. //
  90. // See the [MarshalOptions] type if you need more control.
  91. func Marshal(m Message) ([]byte, error) {
  92. // Treat nil message interface as an empty message; nothing to output.
  93. if m == nil {
  94. return nil, nil
  95. }
  96. out, err := MarshalOptions{}.marshal(nil, m.ProtoReflect())
  97. if len(out.Buf) == 0 && err == nil {
  98. out.Buf = emptyBytesForMessage(m)
  99. }
  100. return out.Buf, err
  101. }
  102. // Marshal returns the wire-format encoding of m.
  103. func (o MarshalOptions) Marshal(m Message) ([]byte, error) {
  104. // Treat nil message interface as an empty message; nothing to output.
  105. if m == nil {
  106. return nil, nil
  107. }
  108. out, err := o.marshal(nil, m.ProtoReflect())
  109. if len(out.Buf) == 0 && err == nil {
  110. out.Buf = emptyBytesForMessage(m)
  111. }
  112. return out.Buf, err
  113. }
  114. // emptyBytesForMessage returns a nil buffer if and only if m is invalid,
  115. // otherwise it returns a non-nil empty buffer.
  116. //
  117. // This is to assist the edge-case where user-code does the following:
  118. //
  119. // m1.OptionalBytes, _ = proto.Marshal(m2)
  120. //
  121. // where they expect the proto2 "optional_bytes" field to be populated
  122. // if any only if m2 is a valid message.
  123. func emptyBytesForMessage(m Message) []byte {
  124. if m == nil || !m.ProtoReflect().IsValid() {
  125. return nil
  126. }
  127. return emptyBuf[:]
  128. }
  129. // MarshalAppend appends the wire-format encoding of m to b,
  130. // returning the result.
  131. //
  132. // This is a less common entry point than [Marshal], which is only needed if you
  133. // need to supply your own buffers for performance reasons.
  134. func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
  135. // Treat nil message interface as an empty message; nothing to append.
  136. if m == nil {
  137. return b, nil
  138. }
  139. out, err := o.marshal(b, m.ProtoReflect())
  140. return out.Buf, err
  141. }
  142. // MarshalState returns the wire-format encoding of a message.
  143. //
  144. // This method permits fine-grained control over the marshaler.
  145. // Most users should use [Marshal] instead.
  146. func (o MarshalOptions) MarshalState(in protoiface.MarshalInput) (protoiface.MarshalOutput, error) {
  147. return o.marshal(in.Buf, in.Message)
  148. }
  149. // marshal is a centralized function that all marshal operations go through.
  150. // For profiling purposes, avoid changing the name of this function or
  151. // introducing other code paths for marshal that do not go through this.
  152. func (o MarshalOptions) marshal(b []byte, m protoreflect.Message) (out protoiface.MarshalOutput, err error) {
  153. allowPartial := o.AllowPartial
  154. o.AllowPartial = true
  155. if methods := protoMethods(m); methods != nil && methods.Marshal != nil &&
  156. !(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) {
  157. in := protoiface.MarshalInput{
  158. Message: m,
  159. Buf: b,
  160. Flags: o.flags(),
  161. }
  162. if methods.Size != nil {
  163. sout := methods.Size(protoiface.SizeInput{
  164. Message: m,
  165. Flags: in.Flags,
  166. })
  167. if cap(b) < len(b)+sout.Size {
  168. in.Buf = make([]byte, len(b), growcap(cap(b), len(b)+sout.Size))
  169. copy(in.Buf, b)
  170. }
  171. in.Flags |= protoiface.MarshalUseCachedSize
  172. }
  173. out, err = methods.Marshal(in)
  174. } else {
  175. out.Buf, err = o.marshalMessageSlow(b, m)
  176. }
  177. if err != nil {
  178. var mismatch *protoerrors.SizeMismatchError
  179. if errors.As(err, &mismatch) {
  180. return out, fmt.Errorf("marshaling %s: %v", string(m.Descriptor().FullName()), err)
  181. }
  182. return out, err
  183. }
  184. if allowPartial {
  185. return out, nil
  186. }
  187. return out, checkInitialized(m)
  188. }
  189. func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
  190. out, err := o.marshal(b, m)
  191. return out.Buf, err
  192. }
  193. // growcap scales up the capacity of a slice.
  194. //
  195. // Given a slice with a current capacity of oldcap and a desired
  196. // capacity of wantcap, growcap returns a new capacity >= wantcap.
  197. //
  198. // The algorithm is mostly identical to the one used by append as of Go 1.14.
  199. func growcap(oldcap, wantcap int) (newcap int) {
  200. if wantcap > oldcap*2 {
  201. newcap = wantcap
  202. } else if oldcap < 1024 {
  203. // The Go 1.14 runtime takes this case when len(s) < 1024,
  204. // not when cap(s) < 1024. The difference doesn't seem
  205. // significant here.
  206. newcap = oldcap * 2
  207. } else {
  208. newcap = oldcap
  209. for 0 < newcap && newcap < wantcap {
  210. newcap += newcap / 4
  211. }
  212. if newcap <= 0 {
  213. newcap = wantcap
  214. }
  215. }
  216. return newcap
  217. }
  218. func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
  219. if messageset.IsMessageSet(m.Descriptor()) {
  220. return o.marshalMessageSet(b, m)
  221. }
  222. fieldOrder := order.AnyFieldOrder
  223. if o.Deterministic {
  224. // TODO: This should use a more natural ordering like NumberFieldOrder,
  225. // but doing so breaks golden tests that make invalid assumption about
  226. // output stability of this implementation.
  227. fieldOrder = order.LegacyFieldOrder
  228. }
  229. var err error
  230. order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  231. b, err = o.marshalField(b, fd, v)
  232. return err == nil
  233. })
  234. if err != nil {
  235. return b, err
  236. }
  237. b = append(b, m.GetUnknown()...)
  238. return b, nil
  239. }
  240. func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
  241. switch {
  242. case fd.IsList():
  243. return o.marshalList(b, fd, value.List())
  244. case fd.IsMap():
  245. return o.marshalMap(b, fd, value.Map())
  246. default:
  247. b = protowire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()])
  248. return o.marshalSingular(b, fd, value)
  249. }
  250. }
  251. func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
  252. if fd.IsPacked() && list.Len() > 0 {
  253. b = protowire.AppendTag(b, fd.Number(), protowire.BytesType)
  254. b, pos := appendSpeculativeLength(b)
  255. for i, llen := 0, list.Len(); i < llen; i++ {
  256. var err error
  257. b, err = o.marshalSingular(b, fd, list.Get(i))
  258. if err != nil {
  259. return b, err
  260. }
  261. }
  262. b = finishSpeculativeLength(b, pos)
  263. return b, nil
  264. }
  265. kind := fd.Kind()
  266. for i, llen := 0, list.Len(); i < llen; i++ {
  267. var err error
  268. b = protowire.AppendTag(b, fd.Number(), wireTypes[kind])
  269. b, err = o.marshalSingular(b, fd, list.Get(i))
  270. if err != nil {
  271. return b, err
  272. }
  273. }
  274. return b, nil
  275. }
  276. func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
  277. keyf := fd.MapKey()
  278. valf := fd.MapValue()
  279. keyOrder := order.AnyKeyOrder
  280. if o.Deterministic {
  281. keyOrder = order.GenericKeyOrder
  282. }
  283. var err error
  284. order.RangeEntries(mapv, keyOrder, func(key protoreflect.MapKey, value protoreflect.Value) bool {
  285. b = protowire.AppendTag(b, fd.Number(), protowire.BytesType)
  286. var pos int
  287. b, pos = appendSpeculativeLength(b)
  288. b, err = o.marshalField(b, keyf, key.Value())
  289. if err != nil {
  290. return false
  291. }
  292. b, err = o.marshalField(b, valf, value)
  293. if err != nil {
  294. return false
  295. }
  296. b = finishSpeculativeLength(b, pos)
  297. return true
  298. })
  299. return b, err
  300. }
  301. // When encoding length-prefixed fields, we speculatively set aside some number of bytes
  302. // for the length, encode the data, and then encode the length (shifting the data if necessary
  303. // to make room).
  304. const speculativeLength = 1
  305. func appendSpeculativeLength(b []byte) ([]byte, int) {
  306. pos := len(b)
  307. b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
  308. return b, pos
  309. }
  310. func finishSpeculativeLength(b []byte, pos int) []byte {
  311. mlen := len(b) - pos - speculativeLength
  312. msiz := protowire.SizeVarint(uint64(mlen))
  313. if msiz != speculativeLength {
  314. for i := 0; i < msiz-speculativeLength; i++ {
  315. b = append(b, 0)
  316. }
  317. copy(b[pos+msiz:], b[pos+speculativeLength:])
  318. b = b[:pos+msiz+mlen]
  319. }
  320. protowire.AppendVarint(b[:pos], uint64(mlen))
  321. return b
  322. }