encode.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. "github.com/golang/protobuf/v2/internal/encoding/wire"
  9. "github.com/golang/protobuf/v2/internal/errors"
  10. "github.com/golang/protobuf/v2/internal/mapsort"
  11. "github.com/golang/protobuf/v2/internal/pragma"
  12. "github.com/golang/protobuf/v2/reflect/protoreflect"
  13. "github.com/golang/protobuf/v2/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. fields := m.Type().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 := fields.ByNumber(num)
  121. if field == nil {
  122. field = knownFields.ExtensionTypes().ByNumber(num)
  123. if field == nil {
  124. panic(fmt.Errorf("no descriptor for field %d in %q", num, m.Type().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, field protoreflect.FieldDescriptor, value protoreflect.Value) ([]byte, error) {
  165. num := field.Number()
  166. kind := field.Kind()
  167. switch {
  168. case field.Cardinality() != protoreflect.Repeated:
  169. b = wire.AppendTag(b, num, wireTypes[kind])
  170. return o.marshalSingular(b, num, field, value)
  171. case field.IsMap():
  172. return o.marshalMap(b, num, kind, field.Message(), value.Map())
  173. case field.IsPacked():
  174. return o.marshalPacked(b, num, field, value.List())
  175. default:
  176. return o.marshalList(b, num, field, value.List())
  177. }
  178. }
  179. func (o MarshalOptions) marshalMap(b []byte, num wire.Number, kind protoreflect.Kind, mdesc protoreflect.MessageDescriptor, mapv protoreflect.Map) ([]byte, error) {
  180. keyf := mdesc.Fields().ByNumber(1)
  181. valf := mdesc.Fields().ByNumber(2)
  182. var nerr errors.NonFatal
  183. var err error
  184. o.rangeMap(mapv, keyf.Kind(), func(key protoreflect.MapKey, value protoreflect.Value) bool {
  185. b = wire.AppendTag(b, num, wire.BytesType)
  186. var pos int
  187. b, pos = appendSpeculativeLength(b)
  188. b, err = o.marshalField(b, keyf, key.Value())
  189. if !nerr.Merge(err) {
  190. return false
  191. }
  192. b, err = o.marshalField(b, valf, value)
  193. if !nerr.Merge(err) {
  194. return false
  195. }
  196. err = nil
  197. b = finishSpeculativeLength(b, pos)
  198. return true
  199. })
  200. if err != nil {
  201. return b, err
  202. }
  203. return b, nerr.E
  204. }
  205. func (o MarshalOptions) rangeMap(mapv protoreflect.Map, kind protoreflect.Kind, f func(protoreflect.MapKey, protoreflect.Value) bool) {
  206. if !o.Deterministic {
  207. mapv.Range(f)
  208. return
  209. }
  210. mapsort.Range(mapv, kind, f)
  211. }
  212. func (o MarshalOptions) marshalPacked(b []byte, num wire.Number, field protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
  213. b = wire.AppendTag(b, num, wire.BytesType)
  214. b, pos := appendSpeculativeLength(b)
  215. var nerr errors.NonFatal
  216. for i, llen := 0, list.Len(); i < llen; i++ {
  217. var err error
  218. b, err = o.marshalSingular(b, num, field, list.Get(i))
  219. if !nerr.Merge(err) {
  220. return b, err
  221. }
  222. }
  223. b = finishSpeculativeLength(b, pos)
  224. return b, nerr.E
  225. }
  226. func (o MarshalOptions) marshalList(b []byte, num wire.Number, field protoreflect.FieldDescriptor, list protoreflect.List) ([]byte, error) {
  227. kind := field.Kind()
  228. var nerr errors.NonFatal
  229. for i, llen := 0, list.Len(); i < llen; i++ {
  230. var err error
  231. b = wire.AppendTag(b, num, wireTypes[kind])
  232. b, err = o.marshalSingular(b, num, field, list.Get(i))
  233. if !nerr.Merge(err) {
  234. return b, err
  235. }
  236. }
  237. return b, nerr.E
  238. }
  239. // When encoding length-prefixed fields, we speculatively set aside some number of bytes
  240. // for the length, encode the data, and then encode the length (shifting the data if necessary
  241. // to make room).
  242. const speculativeLength = 1
  243. func appendSpeculativeLength(b []byte) ([]byte, int) {
  244. pos := len(b)
  245. b = append(b, "\x00\x00\x00\x00"[:speculativeLength]...)
  246. return b, pos
  247. }
  248. func finishSpeculativeLength(b []byte, pos int) []byte {
  249. mlen := len(b) - pos - speculativeLength
  250. msiz := wire.SizeVarint(uint64(mlen))
  251. if msiz != speculativeLength {
  252. for i := 0; i < msiz-speculativeLength; i++ {
  253. b = append(b, 0)
  254. }
  255. copy(b[pos+msiz:], b[pos+speculativeLength:])
  256. b = b[:pos+msiz+mlen]
  257. }
  258. wire.AppendVarint(b[:pos], uint64(mlen))
  259. return b
  260. }