decode.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 impl
  5. import (
  6. "math/bits"
  7. "google.golang.org/protobuf/encoding/protowire"
  8. "google.golang.org/protobuf/internal/errors"
  9. "google.golang.org/protobuf/internal/flags"
  10. "google.golang.org/protobuf/proto"
  11. "google.golang.org/protobuf/reflect/protoreflect"
  12. "google.golang.org/protobuf/reflect/protoregistry"
  13. "google.golang.org/protobuf/runtime/protoiface"
  14. )
  15. var errDecode = errors.New("cannot parse invalid wire-format data")
  16. var errRecursionDepth = errors.New("exceeded maximum recursion depth")
  17. type unmarshalOptions struct {
  18. flags protoiface.UnmarshalInputFlags
  19. resolver interface {
  20. FindExtensionByName(field protoreflect.FullName) (protoreflect.ExtensionType, error)
  21. FindExtensionByNumber(message protoreflect.FullName, field protoreflect.FieldNumber) (protoreflect.ExtensionType, error)
  22. }
  23. depth int
  24. }
  25. func (o unmarshalOptions) Options() proto.UnmarshalOptions {
  26. return proto.UnmarshalOptions{
  27. Merge: true,
  28. AllowPartial: true,
  29. DiscardUnknown: o.DiscardUnknown(),
  30. Resolver: o.resolver,
  31. NoLazyDecoding: o.NoLazyDecoding(),
  32. }
  33. }
  34. func (o unmarshalOptions) DiscardUnknown() bool {
  35. return o.flags&protoiface.UnmarshalDiscardUnknown != 0
  36. }
  37. func (o unmarshalOptions) AliasBuffer() bool { return o.flags&protoiface.UnmarshalAliasBuffer != 0 }
  38. func (o unmarshalOptions) Validated() bool { return o.flags&protoiface.UnmarshalValidated != 0 }
  39. func (o unmarshalOptions) NoLazyDecoding() bool {
  40. return o.flags&protoiface.UnmarshalNoLazyDecoding != 0
  41. }
  42. func (o unmarshalOptions) CanBeLazy() bool {
  43. if o.resolver != protoregistry.GlobalTypes {
  44. return false
  45. }
  46. // We ignore the UnmarshalInvalidateSizeCache even though it's not in the default set
  47. return (o.flags & ^(protoiface.UnmarshalAliasBuffer | protoiface.UnmarshalValidated | protoiface.UnmarshalCheckRequired)) == 0
  48. }
  49. var lazyUnmarshalOptions = unmarshalOptions{
  50. resolver: protoregistry.GlobalTypes,
  51. flags: protoiface.UnmarshalAliasBuffer | protoiface.UnmarshalValidated,
  52. depth: protowire.DefaultRecursionLimit,
  53. }
  54. type unmarshalOutput struct {
  55. n int // number of bytes consumed
  56. initialized bool
  57. }
  58. // unmarshal is protoreflect.Methods.Unmarshal.
  59. func (mi *MessageInfo) unmarshal(in protoiface.UnmarshalInput) (protoiface.UnmarshalOutput, error) {
  60. var p pointer
  61. if ms, ok := in.Message.(*messageState); ok {
  62. p = ms.pointer()
  63. } else {
  64. p = in.Message.(*messageReflectWrapper).pointer()
  65. }
  66. out, err := mi.unmarshalPointer(in.Buf, p, 0, unmarshalOptions{
  67. flags: in.Flags,
  68. resolver: in.Resolver,
  69. depth: in.Depth,
  70. })
  71. var flags protoiface.UnmarshalOutputFlags
  72. if out.initialized {
  73. flags |= protoiface.UnmarshalInitialized
  74. }
  75. return protoiface.UnmarshalOutput{
  76. Flags: flags,
  77. }, err
  78. }
  79. // errUnknown is returned during unmarshaling to indicate a parse error that
  80. // should result in a field being placed in the unknown fields section (for example,
  81. // when the wire type doesn't match) as opposed to the entire unmarshal operation
  82. // failing (for example, when a field extends past the available input).
  83. //
  84. // This is a sentinel error which should never be visible to the user.
  85. var errUnknown = errors.New("unknown")
  86. func (mi *MessageInfo) unmarshalPointer(b []byte, p pointer, groupTag protowire.Number, opts unmarshalOptions) (out unmarshalOutput, err error) {
  87. mi.init()
  88. if opts.depth--; opts.depth < 0 {
  89. return out, errRecursionDepth
  90. }
  91. if flags.ProtoLegacy && mi.isMessageSet {
  92. return unmarshalMessageSet(mi, b, p, opts)
  93. }
  94. lazyDecoding := LazyEnabled() // default
  95. if opts.NoLazyDecoding() {
  96. lazyDecoding = false // explicitly disabled
  97. }
  98. if mi.lazyOffset.IsValid() && lazyDecoding {
  99. return mi.unmarshalPointerLazy(b, p, groupTag, opts)
  100. }
  101. return mi.unmarshalPointerEager(b, p, groupTag, opts)
  102. }
  103. // unmarshalPointerEager is the message unmarshalling function for all messages that are not lazy.
  104. // The corresponding function for Lazy is in google_lazy.go.
  105. func (mi *MessageInfo) unmarshalPointerEager(b []byte, p pointer, groupTag protowire.Number, opts unmarshalOptions) (out unmarshalOutput, err error) {
  106. initialized := true
  107. var requiredMask uint64
  108. var exts *map[int32]ExtensionField
  109. var presence presence
  110. if mi.presenceOffset.IsValid() {
  111. presence = p.Apply(mi.presenceOffset).PresenceInfo()
  112. }
  113. start := len(b)
  114. for len(b) > 0 {
  115. // Parse the tag (field number and wire type).
  116. var tag uint64
  117. if b[0] < 0x80 {
  118. tag = uint64(b[0])
  119. b = b[1:]
  120. } else if len(b) >= 2 && b[1] < 128 {
  121. tag = uint64(b[0]&0x7f) + uint64(b[1])<<7
  122. b = b[2:]
  123. } else {
  124. var n int
  125. tag, n = protowire.ConsumeVarint(b)
  126. if n < 0 {
  127. return out, errDecode
  128. }
  129. b = b[n:]
  130. }
  131. var num protowire.Number
  132. if n := tag >> 3; n < uint64(protowire.MinValidNumber) || n > uint64(protowire.MaxValidNumber) {
  133. return out, errDecode
  134. } else {
  135. num = protowire.Number(n)
  136. }
  137. wtyp := protowire.Type(tag & 7)
  138. if wtyp == protowire.EndGroupType {
  139. if num != groupTag {
  140. return out, errDecode
  141. }
  142. groupTag = 0
  143. break
  144. }
  145. var f *coderFieldInfo
  146. if int(num) < len(mi.denseCoderFields) {
  147. f = mi.denseCoderFields[num]
  148. } else {
  149. f = mi.coderFields[num]
  150. }
  151. var n int
  152. err := errUnknown
  153. switch {
  154. case f != nil:
  155. if f.funcs.unmarshal == nil {
  156. break
  157. }
  158. var o unmarshalOutput
  159. o, err = f.funcs.unmarshal(b, p.Apply(f.offset), wtyp, f, opts)
  160. n = o.n
  161. if err != nil {
  162. break
  163. }
  164. requiredMask |= f.validation.requiredBit
  165. if f.funcs.isInit != nil && !o.initialized {
  166. initialized = false
  167. }
  168. if f.presenceIndex != noPresence {
  169. presence.SetPresentUnatomic(f.presenceIndex, mi.presenceSize)
  170. }
  171. default:
  172. // Possible extension.
  173. if exts == nil && mi.extensionOffset.IsValid() {
  174. exts = p.Apply(mi.extensionOffset).Extensions()
  175. if *exts == nil {
  176. *exts = make(map[int32]ExtensionField)
  177. }
  178. }
  179. if exts == nil {
  180. break
  181. }
  182. var o unmarshalOutput
  183. o, err = mi.unmarshalExtension(b, num, wtyp, *exts, opts)
  184. if err != nil {
  185. break
  186. }
  187. n = o.n
  188. if !o.initialized {
  189. initialized = false
  190. }
  191. }
  192. if err != nil {
  193. if err != errUnknown {
  194. return out, err
  195. }
  196. n = protowire.ConsumeFieldValue(num, wtyp, b)
  197. if n < 0 {
  198. return out, errDecode
  199. }
  200. if !opts.DiscardUnknown() && mi.unknownOffset.IsValid() {
  201. u := mi.mutableUnknownBytes(p)
  202. *u = protowire.AppendTag(*u, num, wtyp)
  203. *u = append(*u, b[:n]...)
  204. }
  205. }
  206. b = b[n:]
  207. }
  208. if groupTag != 0 {
  209. return out, errDecode
  210. }
  211. if mi.numRequiredFields > 0 && bits.OnesCount64(requiredMask) != int(mi.numRequiredFields) {
  212. initialized = false
  213. }
  214. if initialized {
  215. out.initialized = true
  216. }
  217. out.n = start - len(b)
  218. return out, nil
  219. }
  220. func (mi *MessageInfo) unmarshalExtension(b []byte, num protowire.Number, wtyp protowire.Type, exts map[int32]ExtensionField, opts unmarshalOptions) (out unmarshalOutput, err error) {
  221. x := exts[int32(num)]
  222. xt := x.Type()
  223. if xt == nil {
  224. var err error
  225. xt, err = opts.resolver.FindExtensionByNumber(mi.Desc.FullName(), num)
  226. if err != nil {
  227. if err == protoregistry.NotFound {
  228. return out, errUnknown
  229. }
  230. return out, errors.New("%v: unable to resolve extension %v: %v", mi.Desc.FullName(), num, err)
  231. }
  232. }
  233. xi := getExtensionFieldInfo(xt)
  234. if xi.funcs.unmarshal == nil {
  235. return out, errUnknown
  236. }
  237. if flags.LazyUnmarshalExtensions {
  238. if opts.CanBeLazy() && x.canLazy(xt) {
  239. out, valid := skipExtension(b, xi, num, wtyp, opts)
  240. switch valid {
  241. case ValidationValid:
  242. if out.initialized {
  243. x.appendLazyBytes(xt, xi, num, wtyp, b[:out.n])
  244. exts[int32(num)] = x
  245. return out, nil
  246. }
  247. case ValidationInvalid:
  248. return out, errDecode
  249. case ValidationUnknown:
  250. }
  251. }
  252. }
  253. ival := x.Value()
  254. if !ival.IsValid() && xi.unmarshalNeedsValue {
  255. // Create a new message, list, or map value to fill in.
  256. // For enums, create a prototype value to let the unmarshal func know the
  257. // concrete type.
  258. ival = xt.New()
  259. }
  260. v, out, err := xi.funcs.unmarshal(b, ival, num, wtyp, opts)
  261. if err != nil {
  262. return out, err
  263. }
  264. if xi.funcs.isInit == nil {
  265. out.initialized = true
  266. }
  267. x.Set(xt, v)
  268. exts[int32(num)] = x
  269. return out, nil
  270. }
  271. func skipExtension(b []byte, xi *extensionFieldInfo, num protowire.Number, wtyp protowire.Type, opts unmarshalOptions) (out unmarshalOutput, _ ValidationStatus) {
  272. if xi.validation.mi == nil {
  273. return out, ValidationUnknown
  274. }
  275. xi.validation.mi.init()
  276. switch xi.validation.typ {
  277. case validationTypeMessage:
  278. if wtyp != protowire.BytesType {
  279. return out, ValidationUnknown
  280. }
  281. v, n := protowire.ConsumeBytes(b)
  282. if n < 0 {
  283. return out, ValidationUnknown
  284. }
  285. if opts.Validated() {
  286. out.initialized = true
  287. out.n = n
  288. return out, ValidationValid
  289. }
  290. out, st := xi.validation.mi.validate(v, 0, opts)
  291. out.n = n
  292. return out, st
  293. case validationTypeGroup:
  294. if wtyp != protowire.StartGroupType {
  295. return out, ValidationUnknown
  296. }
  297. out, st := xi.validation.mi.validate(b, num, opts)
  298. return out, st
  299. default:
  300. return out, ValidationUnknown
  301. }
  302. }