codec_messageset.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "sort"
  7. "google.golang.org/protobuf/encoding/protowire"
  8. "google.golang.org/protobuf/internal/encoding/messageset"
  9. "google.golang.org/protobuf/internal/errors"
  10. "google.golang.org/protobuf/internal/flags"
  11. )
  12. func sizeMessageSet(mi *MessageInfo, p pointer, opts marshalOptions) (size int) {
  13. if !flags.ProtoLegacy {
  14. return 0
  15. }
  16. ext := *p.Apply(mi.extensionOffset).Extensions()
  17. for _, x := range ext {
  18. xi := getExtensionFieldInfo(x.Type())
  19. if xi.funcs.size == nil {
  20. continue
  21. }
  22. num, _ := protowire.DecodeTag(xi.wiretag)
  23. size += messageset.SizeField(num)
  24. size += xi.funcs.size(x.Value(), protowire.SizeTag(messageset.FieldMessage), opts)
  25. }
  26. if u := mi.getUnknownBytes(p); u != nil {
  27. size += messageset.SizeUnknown(*u)
  28. }
  29. return size
  30. }
  31. func marshalMessageSet(mi *MessageInfo, b []byte, p pointer, opts marshalOptions) ([]byte, error) {
  32. if !flags.ProtoLegacy {
  33. return b, errors.New("no support for message_set_wire_format")
  34. }
  35. ext := *p.Apply(mi.extensionOffset).Extensions()
  36. switch len(ext) {
  37. case 0:
  38. case 1:
  39. // Fast-path for one extension: Don't bother sorting the keys.
  40. for _, x := range ext {
  41. var err error
  42. b, err = marshalMessageSetField(mi, b, x, opts)
  43. if err != nil {
  44. return b, err
  45. }
  46. }
  47. default:
  48. // Sort the keys to provide a deterministic encoding.
  49. // Not sure this is required, but the old code does it.
  50. keys := make([]int, 0, len(ext))
  51. for k := range ext {
  52. keys = append(keys, int(k))
  53. }
  54. sort.Ints(keys)
  55. for _, k := range keys {
  56. var err error
  57. b, err = marshalMessageSetField(mi, b, ext[int32(k)], opts)
  58. if err != nil {
  59. return b, err
  60. }
  61. }
  62. }
  63. if u := mi.getUnknownBytes(p); u != nil {
  64. var err error
  65. b, err = messageset.AppendUnknown(b, *u)
  66. if err != nil {
  67. return b, err
  68. }
  69. }
  70. return b, nil
  71. }
  72. func marshalMessageSetField(mi *MessageInfo, b []byte, x ExtensionField, opts marshalOptions) ([]byte, error) {
  73. xi := getExtensionFieldInfo(x.Type())
  74. num, _ := protowire.DecodeTag(xi.wiretag)
  75. b = messageset.AppendFieldStart(b, num)
  76. b, err := xi.funcs.marshal(b, x.Value(), protowire.EncodeTag(messageset.FieldMessage, protowire.BytesType), opts)
  77. if err != nil {
  78. return b, err
  79. }
  80. b = messageset.AppendFieldEnd(b)
  81. return b, nil
  82. }
  83. func unmarshalMessageSet(mi *MessageInfo, b []byte, p pointer, opts unmarshalOptions) (out unmarshalOutput, err error) {
  84. if !flags.ProtoLegacy {
  85. return out, errors.New("no support for message_set_wire_format")
  86. }
  87. ep := p.Apply(mi.extensionOffset).Extensions()
  88. if *ep == nil {
  89. *ep = make(map[int32]ExtensionField)
  90. }
  91. ext := *ep
  92. initialized := true
  93. err = messageset.Unmarshal(b, true, func(num protowire.Number, v []byte) error {
  94. o, err := mi.unmarshalExtension(v, num, protowire.BytesType, ext, opts)
  95. if err == errUnknown {
  96. u := mi.mutableUnknownBytes(p)
  97. *u = protowire.AppendTag(*u, num, protowire.BytesType)
  98. *u = append(*u, v...)
  99. return nil
  100. }
  101. if !o.initialized {
  102. initialized = false
  103. }
  104. return err
  105. })
  106. out.n = len(b)
  107. out.initialized = initialized
  108. return out, err
  109. }