decode.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. // Copyright 2018 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/errors"
  9. "google.golang.org/protobuf/internal/flags"
  10. "google.golang.org/protobuf/internal/genid"
  11. "google.golang.org/protobuf/internal/pragma"
  12. "google.golang.org/protobuf/reflect/protoreflect"
  13. "google.golang.org/protobuf/reflect/protoregistry"
  14. "google.golang.org/protobuf/runtime/protoiface"
  15. )
  16. // UnmarshalOptions configures the unmarshaler.
  17. //
  18. // Example usage:
  19. // err := UnmarshalOptions{DiscardUnknown: true}.Unmarshal(b, m)
  20. type UnmarshalOptions struct {
  21. pragma.NoUnkeyedLiterals
  22. // Merge merges the input into the destination message.
  23. // The default behavior is to always reset the message before unmarshaling,
  24. // unless Merge is specified.
  25. Merge bool
  26. // AllowPartial accepts input for messages that will result in missing
  27. // required fields. If AllowPartial is false (the default), Unmarshal will
  28. // return an error if there are any missing required fields.
  29. AllowPartial bool
  30. // If DiscardUnknown is set, unknown fields are ignored.
  31. DiscardUnknown bool
  32. // Resolver is used for looking up types when unmarshaling extension fields.
  33. // If nil, this defaults to using protoregistry.GlobalTypes.
  34. Resolver interface {
  35. FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
  36. FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
  37. }
  38. }
  39. // Unmarshal parses the wire-format message in b and places the result in m.
  40. func Unmarshal(b []byte, m Message) error {
  41. _, err := UnmarshalOptions{}.unmarshal(b, m.ProtoReflect())
  42. return err
  43. }
  44. // Unmarshal parses the wire-format message in b and places the result in m.
  45. func (o UnmarshalOptions) Unmarshal(b []byte, m Message) error {
  46. _, err := o.unmarshal(b, m.ProtoReflect())
  47. return err
  48. }
  49. // UnmarshalState parses a wire-format message and places the result in m.
  50. //
  51. // This method permits fine-grained control over the unmarshaler.
  52. // Most users should use Unmarshal instead.
  53. func (o UnmarshalOptions) UnmarshalState(in protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) {
  54. return o.unmarshal(in.Buf, in.Message)
  55. }
  56. // unmarshal is a centralized function that all unmarshal operations go through.
  57. // For profiling purposes, avoid changing the name of this function or
  58. // introducing other code paths for unmarshal that do not go through this.
  59. func (o UnmarshalOptions) unmarshal(b []byte, m protoreflect.Message) (out protoiface.UnmarshalOutput, err error) {
  60. if o.Resolver == nil {
  61. o.Resolver = protoregistry.GlobalTypes
  62. }
  63. if !o.Merge {
  64. Reset(m.Interface())
  65. }
  66. allowPartial := o.AllowPartial
  67. o.Merge = true
  68. o.AllowPartial = true
  69. methods := protoMethods(m)
  70. if methods != nil && methods.Unmarshal != nil &&
  71. !(o.DiscardUnknown && methods.Flags&protoiface.SupportUnmarshalDiscardUnknown == 0) {
  72. in := protoiface.UnmarshalInput{
  73. Message: m,
  74. Buf: b,
  75. Resolver: o.Resolver,
  76. }
  77. if o.DiscardUnknown {
  78. in.Flags |= protoiface.UnmarshalDiscardUnknown
  79. }
  80. out, err = methods.Unmarshal(in)
  81. } else {
  82. err = o.unmarshalMessageSlow(b, m)
  83. }
  84. if err != nil {
  85. return out, err
  86. }
  87. if allowPartial || (out.Flags&protoiface.UnmarshalInitialized != 0) {
  88. return out, nil
  89. }
  90. return out, checkInitialized(m)
  91. }
  92. func (o UnmarshalOptions) unmarshalMessage(b []byte, m protoreflect.Message) error {
  93. _, err := o.unmarshal(b, m)
  94. return err
  95. }
  96. func (o UnmarshalOptions) unmarshalMessageSlow(b []byte, m protoreflect.Message) error {
  97. md := m.Descriptor()
  98. if messageset.IsMessageSet(md) {
  99. return o.unmarshalMessageSet(b, m)
  100. }
  101. fields := md.Fields()
  102. for len(b) > 0 {
  103. // Parse the tag (field number and wire type).
  104. num, wtyp, tagLen := protowire.ConsumeTag(b)
  105. if tagLen < 0 {
  106. return protowire.ParseError(tagLen)
  107. }
  108. if num > protowire.MaxValidNumber {
  109. return errors.New("invalid field number")
  110. }
  111. // Find the field descriptor for this field number.
  112. fd := fields.ByNumber(num)
  113. if fd == nil && md.ExtensionRanges().Has(num) {
  114. extType, err := o.Resolver.FindExtensionByNumber(md.FullName(), num)
  115. if err != nil && err != protoregistry.NotFound {
  116. return errors.New("%v: unable to resolve extension %v: %v", md.FullName(), num, err)
  117. }
  118. if extType != nil {
  119. fd = extType.TypeDescriptor()
  120. }
  121. }
  122. var err error
  123. if fd == nil {
  124. err = errUnknown
  125. } else if flags.ProtoLegacy {
  126. if fd.IsWeak() && fd.Message().IsPlaceholder() {
  127. err = errUnknown // weak referent is not linked in
  128. }
  129. }
  130. // Parse the field value.
  131. var valLen int
  132. switch {
  133. case err != nil:
  134. case fd.IsList():
  135. valLen, err = o.unmarshalList(b[tagLen:], wtyp, m.Mutable(fd).List(), fd)
  136. case fd.IsMap():
  137. valLen, err = o.unmarshalMap(b[tagLen:], wtyp, m.Mutable(fd).Map(), fd)
  138. default:
  139. valLen, err = o.unmarshalSingular(b[tagLen:], wtyp, m, fd)
  140. }
  141. if err != nil {
  142. if err != errUnknown {
  143. return err
  144. }
  145. valLen = protowire.ConsumeFieldValue(num, wtyp, b[tagLen:])
  146. if valLen < 0 {
  147. return protowire.ParseError(valLen)
  148. }
  149. if !o.DiscardUnknown {
  150. m.SetUnknown(append(m.GetUnknown(), b[:tagLen+valLen]...))
  151. }
  152. }
  153. b = b[tagLen+valLen:]
  154. }
  155. return nil
  156. }
  157. func (o UnmarshalOptions) unmarshalSingular(b []byte, wtyp protowire.Type, m protoreflect.Message, fd protoreflect.FieldDescriptor) (n int, err error) {
  158. v, n, err := o.unmarshalScalar(b, wtyp, fd)
  159. if err != nil {
  160. return 0, err
  161. }
  162. switch fd.Kind() {
  163. case protoreflect.GroupKind, protoreflect.MessageKind:
  164. m2 := m.Mutable(fd).Message()
  165. if err := o.unmarshalMessage(v.Bytes(), m2); err != nil {
  166. return n, err
  167. }
  168. default:
  169. // Non-message scalars replace the previous value.
  170. m.Set(fd, v)
  171. }
  172. return n, nil
  173. }
  174. func (o UnmarshalOptions) unmarshalMap(b []byte, wtyp protowire.Type, mapv protoreflect.Map, fd protoreflect.FieldDescriptor) (n int, err error) {
  175. if wtyp != protowire.BytesType {
  176. return 0, errUnknown
  177. }
  178. b, n = protowire.ConsumeBytes(b)
  179. if n < 0 {
  180. return 0, protowire.ParseError(n)
  181. }
  182. var (
  183. keyField = fd.MapKey()
  184. valField = fd.MapValue()
  185. key protoreflect.Value
  186. val protoreflect.Value
  187. haveKey bool
  188. haveVal bool
  189. )
  190. switch valField.Kind() {
  191. case protoreflect.GroupKind, protoreflect.MessageKind:
  192. val = mapv.NewValue()
  193. }
  194. // Map entries are represented as a two-element message with fields
  195. // containing the key and value.
  196. for len(b) > 0 {
  197. num, wtyp, n := protowire.ConsumeTag(b)
  198. if n < 0 {
  199. return 0, protowire.ParseError(n)
  200. }
  201. if num > protowire.MaxValidNumber {
  202. return 0, errors.New("invalid field number")
  203. }
  204. b = b[n:]
  205. err = errUnknown
  206. switch num {
  207. case genid.MapEntry_Key_field_number:
  208. key, n, err = o.unmarshalScalar(b, wtyp, keyField)
  209. if err != nil {
  210. break
  211. }
  212. haveKey = true
  213. case genid.MapEntry_Value_field_number:
  214. var v protoreflect.Value
  215. v, n, err = o.unmarshalScalar(b, wtyp, valField)
  216. if err != nil {
  217. break
  218. }
  219. switch valField.Kind() {
  220. case protoreflect.GroupKind, protoreflect.MessageKind:
  221. if err := o.unmarshalMessage(v.Bytes(), val.Message()); err != nil {
  222. return 0, err
  223. }
  224. default:
  225. val = v
  226. }
  227. haveVal = true
  228. }
  229. if err == errUnknown {
  230. n = protowire.ConsumeFieldValue(num, wtyp, b)
  231. if n < 0 {
  232. return 0, protowire.ParseError(n)
  233. }
  234. } else if err != nil {
  235. return 0, err
  236. }
  237. b = b[n:]
  238. }
  239. // Every map entry should have entries for key and value, but this is not strictly required.
  240. if !haveKey {
  241. key = keyField.Default()
  242. }
  243. if !haveVal {
  244. switch valField.Kind() {
  245. case protoreflect.GroupKind, protoreflect.MessageKind:
  246. default:
  247. val = valField.Default()
  248. }
  249. }
  250. mapv.Set(key.MapKey(), val)
  251. return n, nil
  252. }
  253. // errUnknown is used internally to indicate fields which should be added
  254. // to the unknown field set of a message. It is never returned from an exported
  255. // function.
  256. var errUnknown = errors.New("BUG: internal error (unknown)")