decode.go 9.1 KB

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