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