You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

323 rivejä
9.9 KiB

  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. "google.golang.org/protobuf/encoding/protowire"
  7. "google.golang.org/protobuf/internal/encoding/messageset"
  8. "google.golang.org/protobuf/internal/order"
  9. "google.golang.org/protobuf/internal/pragma"
  10. "google.golang.org/protobuf/reflect/protoreflect"
  11. "google.golang.org/protobuf/runtime/protoiface"
  12. )
  13. // MarshalOptions configures the marshaler.
  14. //
  15. // Example usage:
  16. //
  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 violated,
  58. // the results are undefined and may include panics or corrupted 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. // Marshal returns the wire-format encoding of m.
  67. func Marshal(m Message) ([]byte, error) {
  68. // Treat nil message interface as an empty message; nothing to output.
  69. if m == nil {
  70. return nil, nil
  71. }
  72. out, err := MarshalOptions{}.marshal(nil, m.ProtoReflect())
  73. if len(out.Buf) == 0 && err == nil {
  74. out.Buf = emptyBytesForMessage(m)
  75. }
  76. return out.Buf, err
  77. }
  78. // Marshal returns the wire-format encoding of m.
  79. func (o MarshalOptions) Marshal(m Message) ([]byte, error) {
  80. // Treat nil message interface as an empty message; nothing to output.
  81. if m == nil {
  82. return nil, nil
  83. }
  84. out, err := o.marshal(nil, m.ProtoReflect())
  85. if len(out.Buf) == 0 && err == nil {
  86. out.Buf = emptyBytesForMessage(m)
  87. }
  88. return out.Buf, err
  89. }
  90. // emptyBytesForMessage returns a nil buffer if and only if m is invalid,
  91. // otherwise it returns a non-nil empty buffer.
  92. //
  93. // This is to assist the edge-case where user-code does the following:
  94. //
  95. // m1.OptionalBytes, _ = proto.Marshal(m2)
  96. //
  97. // where they expect the proto2 "optional_bytes" field to be populated
  98. // if any only if m2 is a valid message.
  99. func emptyBytesForMessage(m Message) []byte {
  100. if m == nil || !m.ProtoReflect().IsValid() {
  101. return nil
  102. }
  103. return emptyBuf[:]
  104. }
  105. // MarshalAppend appends the wire-format encoding of m to b,
  106. // returning the result.
  107. func (o MarshalOptions) MarshalAppend(b []byte, m Message) ([]byte, error) {
  108. // Treat nil message interface as an empty message; nothing to append.
  109. if m == nil {
  110. return b, nil
  111. }
  112. out, err := o.marshal(b, m.ProtoReflect())
  113. return out.Buf, err
  114. }
  115. // MarshalState returns the wire-format encoding of a message.
  116. //
  117. // This method permits fine-grained control over the marshaler.
  118. // Most users should use Marshal instead.
  119. func (o MarshalOptions) MarshalState(in protoiface.MarshalInput) (protoiface.MarshalOutput, error) {
  120. return o.marshal(in.Buf, in.Message)
  121. }
  122. // marshal is a centralized function that all marshal operations go through.
  123. // For profiling purposes, avoid changing the name of this function or
  124. // introducing other code paths for marshal that do not go through this.
  125. func (o MarshalOptions) marshal(b []byte, m protoreflect.Message) (out protoiface.MarshalOutput, err error) {
  126. allowPartial := o.AllowPartial
  127. o.AllowPartial = true
  128. if methods := protoMethods(m); methods != nil && methods.Marshal != nil &&
  129. !(o.Deterministic && methods.Flags&protoiface.SupportMarshalDeterministic == 0) {
  130. in := protoiface.MarshalInput{
  131. Message: m,
  132. Buf: b,
  133. }
  134. if o.Deterministic {
  135. in.Flags |= protoiface.MarshalDeterministic
  136. }
  137. if o.UseCachedSize {
  138. in.Flags |= protoiface.MarshalUseCachedSize
  139. }
  140. if methods.Size != nil {
  141. sout := methods.Size(protoiface.SizeInput{
  142. Message: m,
  143. Flags: in.Flags,
  144. })
  145. if cap(b) < len(b)+sout.Size {
  146. in.Buf = make([]byte, len(b), growcap(cap(b), len(b)+sout.Size))
  147. copy(in.Buf, b)
  148. }
  149. in.Flags |= protoiface.MarshalUseCachedSize
  150. }
  151. out, err = methods.Marshal(in)
  152. } else {
  153. out.Buf, err = o.marshalMessageSlow(b, m)
  154. }
  155. if err != nil {
  156. return out, err
  157. }
  158. if allowPartial {
  159. return out, nil
  160. }
  161. return out, checkInitialized(m)
  162. }
  163. func (o MarshalOptions) marshalMessage(b []byte, m protoreflect.Message) ([]byte, error) {
  164. out, err := o.marshal(b, m)
  165. return out.Buf, err
  166. }
  167. // growcap scales up the capacity of a slice.
  168. //
  169. // Given a slice with a current capacity of oldcap and a desired
  170. // capacity of wantcap, growcap returns a new capacity >= wantcap.
  171. //
  172. // The algorithm is mostly identical to the one used by append as of Go 1.14.
  173. func growcap(oldcap, wantcap int) (newcap int) {
  174. if wantcap > oldcap*2 {
  175. newcap = wantcap
  176. } else if oldcap < 1024 {
  177. // The Go 1.14 runtime takes this case when len(s) < 1024,
  178. // not when cap(s) < 1024. The difference doesn't seem
  179. // significant here.
  180. newcap = oldcap * 2
  181. } else {
  182. newcap = oldcap
  183. for 0 < newcap && newcap < wantcap {
  184. newcap += newcap / 4
  185. }
  186. if newcap <= 0 {
  187. newcap = wantcap
  188. }
  189. }
  190. return newcap
  191. }
  192. func (o MarshalOptions) marshalMessageSlow(b []byte, m protoreflect.Message) ([]byte, error) {
  193. if messageset.IsMessageSet(m.Descriptor()) {
  194. return o.marshalMessageSet(b, m)
  195. }
  196. fieldOrder := order.AnyFieldOrder
  197. if o.Deterministic {
  198. // TODO: This should use a more natural ordering like NumberFieldOrder,
  199. // but doing so breaks golden tests that make invalid assumption about
  200. // output stability of this implementation.
  201. fieldOrder = order.LegacyFieldOrder
  202. }
  203. var err error
  204. order.RangeFields(m, fieldOrder, func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
  205. b, err = o.marshalField(b, fd, v)
  206. return err == nil
  207. })
  208. if err != nil {
  209. return b, err
  210. }
  211. b = append(b, m.GetUnknown()...)
  212. return b, nil
  213. }
  214. func (o MarshalOptions) marshalField(b []byte, fd protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
  215. switch {
  216. case fd.IsList():
  217. return o.marshalList(b, fd, value.List())
  218. case fd.IsMap():
  219. return o.marshalMap(b, fd, value.Map())
  220. default:
  221. b = protowire.AppendTag(b, fd.Number(), wireTypes[fd.Kind()])
  222. return o.marshalSingular(b, fd, value)
  223. }
  224. }
  225. func (o MarshalOptions) marshalList(b []byte, fd protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
  226. if fd.IsPacked() && list.Len() > 0 {
  227. b = protowire.AppendTag(b, fd.Number(), protowire.BytesType)
  228. b, pos := appendSpeculativeLength(b)
  229. for i, llen := 0, list.Len(); i < llen; i++ {
  230. var err error
  231. b, err = o.marshalSingular(b, fd, list.Get(i))
  232. if err != nil {
  233. return b, err
  234. }
  235. }
  236. b = finishSpeculativeLength(b, pos)
  237. return b, nil
  238. }
  239. kind := fd.Kind()
  240. for i, llen := 0, list.Len(); i < llen; i++ {
  241. var err error
  242. b = protowire.AppendTag(b, fd.Number(), wireTypes[kind])
  243. b, err = o.marshalSingular(b, fd, list.Get(i))
  244. if err != nil {
  245. return b, err
  246. }
  247. }
  248. return b, nil
  249. }
  250. func (o MarshalOptions) marshalMap(b []byte, fd protoreflect.FieldDescriptor, mapv protoreflect.Map) ([]byte, error) {
  251. keyf := fd.MapKey()
  252. valf := fd.MapValue()
  253. keyOrder := order.AnyKeyOrder
  254. if o.Deterministic {
  255. keyOrder = order.GenericKeyOrder
  256. }
  257. var err error
  258. order.RangeEntries(mapv, keyOrder, func(key protoreflect.MapKey, value protoreflect.Value) bool {
  259. b = protowire.AppendTag(b, fd.Number(), protowire.BytesType)
  260. var pos int
  261. b, pos = appendSpeculativeLength(b)
  262. b, err = o.marshalField(b, keyf, key.Value())
  263. if err != nil {
  264. return false
  265. }
  266. b, err = o.marshalField(b, valf, value)
  267. if err != nil {
  268. return false
  269. }
  270. b = finishSpeculativeLength(b, pos)
  271. return true
  272. })
  273. return b, err
  274. }
  275. // When encoding length-prefixed fields, we speculatively set aside some number of bytes
  276. // for the length, encode the data, and then encode the length (shifting the data if necessary
  277. // to make room).
  278. const speculativeLength = 1
  279. func appendSpeculativeLength(b []byte) ([]byte, int) {
  280. pos := len(b)
  281. b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
  282. return b, pos
  283. }
  284. func finishSpeculativeLength(b []byte, pos int) []byte {
  285. mlen := len(b) - pos - speculativeLength
  286. msiz := protowire.SizeVarint(uint64(mlen))
  287. if msiz != speculativeLength {
  288. for i := 0; i < msiz-speculativeLength; i++ {
  289. b = append(b, 0)
  290. }
  291. copy(b[pos+msiz:], b[pos+speculativeLength:])
  292. b = b[:pos+msiz+mlen]
  293. }
  294. protowire.AppendVarint(b[:pos], uint64(mlen))
  295. return b
  296. }