decode.go 8.6 KB

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