codec_messageset.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. if fullyLazyExtensions(opts) {
  25. // Don't expand the extension, instead use the buffer to calculate size
  26. if lb := x.lazyBuffer(); lb != nil {
  27. // We got hold of the buffer, so it's still lazy.
  28. // Don't count the tag size in the extension buffer, it's already added.
  29. size += protowire.SizeTag(messageset.FieldMessage) + len(lb) - xi.tagsize
  30. continue
  31. }
  32. }
  33. size += xi.funcs.size(x.Value(), protowire.SizeTag(messageset.FieldMessage), opts)
  34. }
  35. if u := mi.getUnknownBytes(p); u != nil {
  36. size += messageset.SizeUnknown(*u)
  37. }
  38. return size
  39. }
  40. func marshalMessageSet(mi *MessageInfo, b []byte, p pointer, opts marshalOptions) ([]byte, error) {
  41. if !flags.ProtoLegacy {
  42. return b, errors.New("no support for message_set_wire_format")
  43. }
  44. ext := *p.Apply(mi.extensionOffset).Extensions()
  45. switch len(ext) {
  46. case 0:
  47. case 1:
  48. // Fast-path for one extension: Don't bother sorting the keys.
  49. for _, x := range ext {
  50. var err error
  51. b, err = marshalMessageSetField(mi, b, x, opts)
  52. if err != nil {
  53. return b, err
  54. }
  55. }
  56. default:
  57. // Sort the keys to provide a deterministic encoding.
  58. // Not sure this is required, but the old code does it.
  59. keys := make([]int, 0, len(ext))
  60. for k := range ext {
  61. keys = append(keys, int(k))
  62. }
  63. sort.Ints(keys)
  64. for _, k := range keys {
  65. var err error
  66. b, err = marshalMessageSetField(mi, b, ext[int32(k)], opts)
  67. if err != nil {
  68. return b, err
  69. }
  70. }
  71. }
  72. if u := mi.getUnknownBytes(p); u != nil {
  73. var err error
  74. b, err = messageset.AppendUnknown(b, *u)
  75. if err != nil {
  76. return b, err
  77. }
  78. }
  79. return b, nil
  80. }
  81. func marshalMessageSetField(mi *MessageInfo, b []byte, x ExtensionField, opts marshalOptions) ([]byte, error) {
  82. xi := getExtensionFieldInfo(x.Type())
  83. num, _ := protowire.DecodeTag(xi.wiretag)
  84. b = messageset.AppendFieldStart(b, num)
  85. if fullyLazyExtensions(opts) {
  86. // Don't expand the extension if it's still in wire format, instead use the buffer content.
  87. if lb := x.lazyBuffer(); lb != nil {
  88. // The tag inside the lazy buffer is a different tag (the extension
  89. // number), but what we need here is the tag for FieldMessage:
  90. b = protowire.AppendVarint(b, protowire.EncodeTag(messageset.FieldMessage, protowire.BytesType))
  91. b = append(b, lb[xi.tagsize:]...)
  92. b = messageset.AppendFieldEnd(b)
  93. return b, nil
  94. }
  95. }
  96. b, err := xi.funcs.marshal(b, x.Value(), protowire.EncodeTag(messageset.FieldMessage, protowire.BytesType), opts)
  97. if err != nil {
  98. return b, err
  99. }
  100. b = messageset.AppendFieldEnd(b)
  101. return b, nil
  102. }
  103. func unmarshalMessageSet(mi *MessageInfo, b []byte, p pointer, opts unmarshalOptions) (out unmarshalOutput, err error) {
  104. if !flags.ProtoLegacy {
  105. return out, errors.New("no support for message_set_wire_format")
  106. }
  107. ep := p.Apply(mi.extensionOffset).Extensions()
  108. if *ep == nil {
  109. *ep = make(map[int32]ExtensionField)
  110. }
  111. ext := *ep
  112. initialized := true
  113. err = messageset.Unmarshal(b, true, func(num protowire.Number, v []byte) error {
  114. o, err := mi.unmarshalExtension(v, num, protowire.BytesType, ext, opts)
  115. if err == errUnknown {
  116. u := mi.mutableUnknownBytes(p)
  117. *u = protowire.AppendTag(*u, num, protowire.BytesType)
  118. *u = append(*u, v...)
  119. return nil
  120. }
  121. if !o.initialized {
  122. initialized = false
  123. }
  124. return err
  125. })
  126. out.n = len(b)
  127. out.initialized = initialized
  128. return out, err
  129. }