encode.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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"
  7. "sort"
  8. "sync/atomic"
  9. "google.golang.org/protobuf/internal/flags"
  10. proto "google.golang.org/protobuf/proto"
  11. piface "google.golang.org/protobuf/runtime/protoiface"
  12. )
  13. type marshalOptions struct {
  14. flags piface.MarshalInputFlags
  15. }
  16. func (o marshalOptions) Options() proto.MarshalOptions {
  17. return proto.MarshalOptions{
  18. AllowPartial: true,
  19. Deterministic: o.Deterministic(),
  20. UseCachedSize: o.UseCachedSize(),
  21. }
  22. }
  23. func (o marshalOptions) Deterministic() bool { return o.flags&piface.MarshalDeterministic != 0 }
  24. func (o marshalOptions) UseCachedSize() bool { return o.flags&piface.MarshalUseCachedSize != 0 }
  25. // size is protoreflect.Methods.Size.
  26. func (mi *MessageInfo) size(in piface.SizeInput) piface.SizeOutput {
  27. var p pointer
  28. if ms, ok := in.Message.(*messageState); ok {
  29. p = ms.pointer()
  30. } else {
  31. p = in.Message.(*messageReflectWrapper).pointer()
  32. }
  33. size := mi.sizePointer(p, marshalOptions{
  34. flags: in.Flags,
  35. })
  36. return piface.SizeOutput{Size: size}
  37. }
  38. func (mi *MessageInfo) sizePointer(p pointer, opts marshalOptions) (size int) {
  39. mi.init()
  40. if p.IsNil() {
  41. return 0
  42. }
  43. if opts.UseCachedSize() && mi.sizecacheOffset.IsValid() {
  44. // The size cache contains the size + 1, to allow the
  45. // zero value to be invalid, while also allowing for a
  46. // 0 size to be cached.
  47. if size := atomic.LoadInt32(p.Apply(mi.sizecacheOffset).Int32()); size > 0 {
  48. return int(size - 1)
  49. }
  50. }
  51. return mi.sizePointerSlow(p, opts)
  52. }
  53. func (mi *MessageInfo) sizePointerSlow(p pointer, opts marshalOptions) (size int) {
  54. if flags.ProtoLegacy && mi.isMessageSet {
  55. size = sizeMessageSet(mi, p, opts)
  56. if mi.sizecacheOffset.IsValid() {
  57. atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size+1))
  58. }
  59. return size
  60. }
  61. if mi.extensionOffset.IsValid() {
  62. e := p.Apply(mi.extensionOffset).Extensions()
  63. size += mi.sizeExtensions(e, opts)
  64. }
  65. for _, f := range mi.orderedCoderFields {
  66. if f.funcs.size == nil {
  67. continue
  68. }
  69. fptr := p.Apply(f.offset)
  70. if f.isPointer && fptr.Elem().IsNil() {
  71. continue
  72. }
  73. size += f.funcs.size(fptr, f, opts)
  74. }
  75. if mi.unknownOffset.IsValid() {
  76. if u := mi.getUnknownBytes(p); u != nil {
  77. size += len(*u)
  78. }
  79. }
  80. if mi.sizecacheOffset.IsValid() {
  81. if size > (math.MaxInt32 - 1) {
  82. // The size is too large for the int32 sizecache field.
  83. // We will need to recompute the size when encoding;
  84. // unfortunately expensive, but better than invalid output.
  85. atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), 0)
  86. } else {
  87. // The size cache contains the size + 1, to allow the
  88. // zero value to be invalid, while also allowing for a
  89. // 0 size to be cached.
  90. atomic.StoreInt32(p.Apply(mi.sizecacheOffset).Int32(), int32(size+1))
  91. }
  92. }
  93. return size
  94. }
  95. // marshal is protoreflect.Methods.Marshal.
  96. func (mi *MessageInfo) marshal(in piface.MarshalInput) (out piface.MarshalOutput, err error) {
  97. var p pointer
  98. if ms, ok := in.Message.(*messageState); ok {
  99. p = ms.pointer()
  100. } else {
  101. p = in.Message.(*messageReflectWrapper).pointer()
  102. }
  103. b, err := mi.marshalAppendPointer(in.Buf, p, marshalOptions{
  104. flags: in.Flags,
  105. })
  106. return piface.MarshalOutput{Buf: b}, err
  107. }
  108. func (mi *MessageInfo) marshalAppendPointer(b []byte, p pointer, opts marshalOptions) ([]byte, error) {
  109. mi.init()
  110. if p.IsNil() {
  111. return b, nil
  112. }
  113. if flags.ProtoLegacy && mi.isMessageSet {
  114. return marshalMessageSet(mi, b, p, opts)
  115. }
  116. var err error
  117. // The old marshaler encodes extensions at beginning.
  118. if mi.extensionOffset.IsValid() {
  119. e := p.Apply(mi.extensionOffset).Extensions()
  120. // TODO: Special handling for MessageSet?
  121. b, err = mi.appendExtensions(b, e, opts)
  122. if err != nil {
  123. return b, err
  124. }
  125. }
  126. for _, f := range mi.orderedCoderFields {
  127. if f.funcs.marshal == nil {
  128. continue
  129. }
  130. fptr := p.Apply(f.offset)
  131. if f.isPointer && fptr.Elem().IsNil() {
  132. continue
  133. }
  134. b, err = f.funcs.marshal(b, fptr, f, opts)
  135. if err != nil {
  136. return b, err
  137. }
  138. }
  139. if mi.unknownOffset.IsValid() && !mi.isMessageSet {
  140. if u := mi.getUnknownBytes(p); u != nil {
  141. b = append(b, (*u)...)
  142. }
  143. }
  144. return b, nil
  145. }
  146. // fullyLazyExtensions returns true if we should attempt to keep extensions lazy over size and marshal.
  147. func fullyLazyExtensions(opts marshalOptions) bool {
  148. // When deterministic marshaling is requested, force an unmarshal for lazy
  149. // extensions to produce a deterministic result, instead of passing through
  150. // bytes lazily that may or may not match what Go Protobuf would produce.
  151. return opts.flags&piface.MarshalDeterministic == 0
  152. }
  153. func (mi *MessageInfo) sizeExtensions(ext *map[int32]ExtensionField, opts marshalOptions) (n int) {
  154. if ext == nil {
  155. return 0
  156. }
  157. for _, x := range *ext {
  158. xi := getExtensionFieldInfo(x.Type())
  159. if xi.funcs.size == nil {
  160. continue
  161. }
  162. if fullyLazyExtensions(opts) {
  163. // Don't expand the extension, instead use the buffer to calculate size
  164. if lb := x.lazyBuffer(); lb != nil {
  165. // We got hold of the buffer, so it's still lazy.
  166. n += len(lb)
  167. continue
  168. }
  169. }
  170. n += xi.funcs.size(x.Value(), xi.tagsize, opts)
  171. }
  172. return n
  173. }
  174. func (mi *MessageInfo) appendExtensions(b []byte, ext *map[int32]ExtensionField, opts marshalOptions) ([]byte, error) {
  175. if ext == nil {
  176. return b, nil
  177. }
  178. switch len(*ext) {
  179. case 0:
  180. return b, nil
  181. case 1:
  182. // Fast-path for one extension: Don't bother sorting the keys.
  183. var err error
  184. for _, x := range *ext {
  185. xi := getExtensionFieldInfo(x.Type())
  186. if fullyLazyExtensions(opts) {
  187. // Don't expand the extension if it's still in wire format, instead use the buffer content.
  188. if lb := x.lazyBuffer(); lb != nil {
  189. b = append(b, lb...)
  190. continue
  191. }
  192. }
  193. b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)
  194. }
  195. return b, err
  196. default:
  197. // Sort the keys to provide a deterministic encoding.
  198. // Not sure this is required, but the old code does it.
  199. keys := make([]int, 0, len(*ext))
  200. for k := range *ext {
  201. keys = append(keys, int(k))
  202. }
  203. sort.Ints(keys)
  204. var err error
  205. for _, k := range keys {
  206. x := (*ext)[int32(k)]
  207. xi := getExtensionFieldInfo(x.Type())
  208. if fullyLazyExtensions(opts) {
  209. // Don't expand the extension if it's still in wire format, instead use the buffer content.
  210. if lb := x.lazyBuffer(); lb != nil {
  211. b = append(b, lb...)
  212. continue
  213. }
  214. }
  215. b, err = xi.funcs.marshal(b, x.Value(), xi.wiretag, opts)
  216. if err != nil {
  217. return b, err
  218. }
  219. }
  220. return b, nil
  221. }
  222. }