decode.go 8.5 KB

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