Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [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 ( |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 8 | "reflect" |
| 9 | "unicode/utf8" |
| 10 | |
| 11 | "google.golang.org/protobuf/internal/encoding/wire" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 12 | "google.golang.org/protobuf/proto" |
| 13 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 14 | ) |
| 15 | |
| 16 | type errInvalidUTF8 struct{} |
| 17 | |
| 18 | func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" } |
| 19 | func (errInvalidUTF8) InvalidUTF8() bool { return true } |
| 20 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 21 | func makeOneofFieldCoder(si structInfo, fd pref.FieldDescriptor) pointerCoderFuncs { |
| 22 | ot := si.oneofWrappersByNumber[fd.Number()] |
| 23 | funcs := fieldCoder(fd, ot.Field(0).Type) |
| 24 | fs := si.oneofsByName[fd.ContainingOneof().Name()] |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 25 | ft := fs.Type |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 26 | wiretag := wire.EncodeTag(fd.Number(), wireTypes[fd.Kind()]) |
| 27 | tagsize := wire.SizeVarint(wiretag) |
| 28 | getInfo := func(p pointer) (pointer, bool) { |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 29 | v := p.AsValueOf(ft).Elem() |
| 30 | if v.IsNil() { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 31 | return pointer{}, false |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 32 | } |
| 33 | v = v.Elem() // interface -> *struct |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 34 | if v.Elem().Type() != ot { |
| 35 | return pointer{}, false |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 36 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 37 | return pointerOfValue(v).Apply(zeroOffset), true |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 38 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 39 | pcf := pointerCoderFuncs{ |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 40 | size: func(p pointer, _ int, opts marshalOptions) int { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 41 | v, ok := getInfo(p) |
| 42 | if !ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 43 | return 0 |
| 44 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 45 | return funcs.size(v, tagsize, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 46 | }, |
| 47 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 48 | v, ok := getInfo(p) |
| 49 | if !ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 50 | return b, nil |
| 51 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 52 | return funcs.marshal(b, v, wiretag, opts) |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 53 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 54 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 55 | v := reflect.New(ot) |
| 56 | n, err := funcs.unmarshal(b, pointerOfValue(v).Apply(zeroOffset), wtyp, opts) |
| 57 | if err != nil { |
| 58 | return 0, err |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 59 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 60 | p.AsValueOf(ft).Elem().Set(v) |
| 61 | return n, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 62 | }, |
| 63 | } |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 64 | if funcs.isInit != nil { |
| 65 | pcf.isInit = func(p pointer) error { |
| 66 | v, ok := getInfo(p) |
| 67 | if !ok { |
| 68 | return nil |
| 69 | } |
| 70 | return funcs.isInit(v) |
| 71 | } |
| 72 | } |
| 73 | return pcf |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | func makeMessageFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 77 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 78 | return pointerCoderFuncs{ |
| 79 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 80 | return sizeMessageInfo(p, fi, tagsize, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 81 | }, |
| 82 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 83 | return appendMessageInfo(b, p, wiretag, fi, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 84 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 85 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 86 | return consumeMessageInfo(b, p, fi, wtyp, opts) |
| 87 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 88 | isInit: func(p pointer) error { |
| 89 | return fi.isInitializedPointer(p.Elem()) |
| 90 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 91 | } |
| 92 | } else { |
| 93 | return pointerCoderFuncs{ |
| 94 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 95 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 96 | return sizeMessage(m, tagsize, opts) |
| 97 | }, |
| 98 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 99 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 100 | return appendMessage(b, m, wiretag, opts) |
| 101 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 102 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 103 | mp := p.AsValueOf(ft).Elem() |
| 104 | if mp.IsNil() { |
| 105 | mp.Set(reflect.New(ft.Elem())) |
| 106 | } |
| 107 | return consumeMessage(b, asMessage(mp), wtyp, opts) |
| 108 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 109 | isInit: func(p pointer) error { |
| 110 | m := asMessage(p.AsValueOf(ft).Elem()) |
| 111 | return proto.IsInitialized(m) |
| 112 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 117 | func sizeMessageInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 118 | return wire.SizeBytes(mi.sizePointer(p.Elem(), opts)) + tagsize |
| 119 | } |
| 120 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 121 | func appendMessageInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 122 | b = wire.AppendVarint(b, wiretag) |
| 123 | b = wire.AppendVarint(b, uint64(mi.sizePointer(p.Elem(), opts))) |
| 124 | return mi.marshalAppendPointer(b, p.Elem(), opts) |
| 125 | } |
| 126 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 127 | func consumeMessageInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 128 | if wtyp != wire.BytesType { |
| 129 | return 0, errUnknown |
| 130 | } |
| 131 | v, n := wire.ConsumeBytes(b) |
| 132 | if n < 0 { |
| 133 | return 0, wire.ParseError(n) |
| 134 | } |
| 135 | if p.Elem().IsNil() { |
| 136 | p.SetPointer(pointerOfValue(reflect.New(mi.GoType.Elem()))) |
| 137 | } |
| 138 | if _, err := mi.unmarshalPointer(v, p.Elem(), 0, opts); err != nil { |
| 139 | return 0, err |
| 140 | } |
| 141 | return n, nil |
| 142 | } |
| 143 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 144 | func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int { |
| 145 | return wire.SizeBytes(proto.Size(m)) + tagsize |
| 146 | } |
| 147 | |
| 148 | func appendMessage(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 149 | b = wire.AppendVarint(b, wiretag) |
| 150 | b = wire.AppendVarint(b, uint64(proto.Size(m))) |
| 151 | return opts.Options().MarshalAppend(b, m) |
| 152 | } |
| 153 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 154 | func consumeMessage(b []byte, m proto.Message, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 155 | if wtyp != wire.BytesType { |
| 156 | return 0, errUnknown |
| 157 | } |
| 158 | v, n := wire.ConsumeBytes(b) |
| 159 | if n < 0 { |
| 160 | return 0, wire.ParseError(n) |
| 161 | } |
| 162 | if err := opts.Options().Unmarshal(v, m); err != nil { |
| 163 | return 0, err |
| 164 | } |
| 165 | return n, nil |
| 166 | } |
| 167 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 168 | func sizeMessageIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 169 | m := Export{}.MessageOf(ival).Interface() |
| 170 | return sizeMessage(m, tagsize, opts) |
| 171 | } |
| 172 | |
| 173 | func appendMessageIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 174 | m := Export{}.MessageOf(ival).Interface() |
| 175 | return appendMessage(b, m, wiretag, opts) |
| 176 | } |
| 177 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 178 | func consumeMessageIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 179 | m := Export{}.MessageOf(ival).Interface() |
| 180 | n, err := consumeMessage(b, m, wtyp, opts) |
| 181 | return ival, n, err |
| 182 | } |
| 183 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 184 | func isInitMessageIface(ival interface{}) error { |
| 185 | m := Export{}.MessageOf(ival).Interface() |
| 186 | return proto.IsInitialized(m) |
| 187 | } |
| 188 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 189 | var coderMessageIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 190 | size: sizeMessageIface, |
| 191 | marshal: appendMessageIface, |
| 192 | unmarshal: consumeMessageIface, |
| 193 | isInit: isInitMessageIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 197 | num := fd.Number() |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 198 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 199 | return pointerCoderFuncs{ |
| 200 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 201 | return sizeGroupType(p, fi, tagsize, opts) |
| 202 | }, |
| 203 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 204 | return appendGroupType(b, p, wiretag, fi, opts) |
| 205 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 206 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 207 | return consumeGroupType(b, p, fi, num, wtyp, opts) |
| 208 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 209 | isInit: func(p pointer) error { |
| 210 | return fi.isInitializedPointer(p.Elem()) |
| 211 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 212 | } |
| 213 | } else { |
| 214 | return pointerCoderFuncs{ |
| 215 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 216 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 217 | return sizeGroup(m, tagsize, opts) |
| 218 | }, |
| 219 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 220 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 221 | return appendGroup(b, m, wiretag, opts) |
| 222 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 223 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 224 | mp := p.AsValueOf(ft).Elem() |
| 225 | if mp.IsNil() { |
| 226 | mp.Set(reflect.New(ft.Elem())) |
| 227 | } |
| 228 | return consumeGroup(b, asMessage(mp), num, wtyp, opts) |
| 229 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 230 | isInit: func(p pointer) error { |
| 231 | m := asMessage(p.AsValueOf(ft).Elem()) |
| 232 | return proto.IsInitialized(m) |
| 233 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 234 | } |
| 235 | } |
| 236 | } |
| 237 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 238 | func sizeGroupType(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 239 | return 2*tagsize + mi.sizePointer(p.Elem(), opts) |
| 240 | } |
| 241 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 242 | func appendGroupType(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 243 | b = wire.AppendVarint(b, wiretag) // start group |
| 244 | b, err := mi.marshalAppendPointer(b, p.Elem(), opts) |
| 245 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 246 | return b, err |
| 247 | } |
| 248 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 249 | func consumeGroupType(b []byte, p pointer, mi *MessageInfo, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 250 | if wtyp != wire.StartGroupType { |
| 251 | return 0, errUnknown |
| 252 | } |
| 253 | if p.Elem().IsNil() { |
| 254 | p.SetPointer(pointerOfValue(reflect.New(mi.GoType.Elem()))) |
| 255 | } |
| 256 | return mi.unmarshalPointer(b, p.Elem(), num, opts) |
| 257 | } |
| 258 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 259 | func sizeGroup(m proto.Message, tagsize int, _ marshalOptions) int { |
| 260 | return 2*tagsize + proto.Size(m) |
| 261 | } |
| 262 | |
| 263 | func appendGroup(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 264 | b = wire.AppendVarint(b, wiretag) // start group |
| 265 | b, err := opts.Options().MarshalAppend(b, m) |
| 266 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 267 | return b, err |
| 268 | } |
| 269 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 270 | func consumeGroup(b []byte, m proto.Message, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 271 | if wtyp != wire.StartGroupType { |
| 272 | return 0, errUnknown |
| 273 | } |
| 274 | b, n := wire.ConsumeGroup(num, b) |
| 275 | if n < 0 { |
| 276 | return 0, wire.ParseError(n) |
| 277 | } |
| 278 | return n, opts.Options().Unmarshal(b, m) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 281 | func makeGroupValueCoder(fd pref.FieldDescriptor, ft reflect.Type) ifaceCoderFuncs { |
| 282 | return ifaceCoderFuncs{ |
| 283 | size: func(ival interface{}, tagsize int, opts marshalOptions) int { |
| 284 | m := Export{}.MessageOf(ival).Interface() |
| 285 | return sizeGroup(m, tagsize, opts) |
| 286 | }, |
| 287 | marshal: func(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 288 | m := Export{}.MessageOf(ival).Interface() |
| 289 | return appendGroup(b, m, wiretag, opts) |
| 290 | }, |
| 291 | unmarshal: func(b []byte, ival interface{}, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 292 | m := Export{}.MessageOf(ival).Interface() |
| 293 | n, err := consumeGroup(b, m, num, wtyp, opts) |
| 294 | return ival, n, err |
| 295 | }, |
| 296 | isInit: isInitMessageIface, |
| 297 | } |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | func makeMessageSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 301 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 302 | return pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 303 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 304 | return sizeMessageSliceInfo(p, fi, tagsize, opts) |
| 305 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 306 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 307 | return appendMessageSliceInfo(b, p, wiretag, fi, opts) |
| 308 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 309 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 310 | return consumeMessageSliceInfo(b, p, fi, wtyp, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 311 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 312 | isInit: func(p pointer) error { |
| 313 | return isInitMessageSliceInfo(p, fi) |
| 314 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | return pointerCoderFuncs{ |
| 318 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 319 | return sizeMessageSlice(p, ft, tagsize, opts) |
| 320 | }, |
| 321 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 322 | return appendMessageSlice(b, p, wiretag, ft, opts) |
| 323 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 324 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 325 | return consumeMessageSlice(b, p, ft, wtyp, opts) |
| 326 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 327 | isInit: func(p pointer) error { |
| 328 | return isInitMessageSlice(p, ft) |
| 329 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 333 | func sizeMessageSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 334 | s := p.PointerSlice() |
| 335 | n := 0 |
| 336 | for _, v := range s { |
| 337 | n += wire.SizeBytes(mi.sizePointer(v, opts)) + tagsize |
| 338 | } |
| 339 | return n |
| 340 | } |
| 341 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 342 | func appendMessageSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 343 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 344 | var err error |
| 345 | for _, v := range s { |
| 346 | b = wire.AppendVarint(b, wiretag) |
| 347 | siz := mi.sizePointer(v, opts) |
| 348 | b = wire.AppendVarint(b, uint64(siz)) |
| 349 | b, err = mi.marshalAppendPointer(b, v, opts) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 350 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 351 | return b, err |
| 352 | } |
| 353 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 354 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 355 | } |
| 356 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 357 | func consumeMessageSliceInfo(b []byte, p pointer, mi *MessageInfo, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 358 | if wtyp != wire.BytesType { |
| 359 | return 0, errUnknown |
| 360 | } |
| 361 | v, n := wire.ConsumeBytes(b) |
| 362 | if n < 0 { |
| 363 | return 0, wire.ParseError(n) |
| 364 | } |
| 365 | m := reflect.New(mi.GoType.Elem()).Interface() |
| 366 | mp := pointerOfIface(m) |
| 367 | if _, err := mi.unmarshalPointer(v, mp, 0, opts); err != nil { |
| 368 | return 0, err |
| 369 | } |
| 370 | p.AppendPointerSlice(mp) |
| 371 | return n, nil |
| 372 | } |
| 373 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 374 | func isInitMessageSliceInfo(p pointer, mi *MessageInfo) error { |
| 375 | s := p.PointerSlice() |
| 376 | for _, v := range s { |
| 377 | if err := mi.isInitializedPointer(v); err != nil { |
| 378 | return err |
| 379 | } |
| 380 | } |
| 381 | return nil |
| 382 | } |
| 383 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 384 | func sizeMessageSlice(p pointer, goType reflect.Type, tagsize int, _ marshalOptions) int { |
| 385 | s := p.PointerSlice() |
| 386 | n := 0 |
| 387 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 388 | m := asMessage(v.AsValueOf(goType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 389 | n += wire.SizeBytes(proto.Size(m)) + tagsize |
| 390 | } |
| 391 | return n |
| 392 | } |
| 393 | |
| 394 | func appendMessageSlice(b []byte, p pointer, wiretag uint64, goType reflect.Type, opts marshalOptions) ([]byte, error) { |
| 395 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 396 | var err error |
| 397 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 398 | m := asMessage(v.AsValueOf(goType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 399 | b = wire.AppendVarint(b, wiretag) |
| 400 | siz := proto.Size(m) |
| 401 | b = wire.AppendVarint(b, uint64(siz)) |
| 402 | b, err = opts.Options().MarshalAppend(b, m) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 403 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 404 | return b, err |
| 405 | } |
| 406 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 407 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 408 | } |
| 409 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 410 | func consumeMessageSlice(b []byte, p pointer, goType reflect.Type, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 411 | if wtyp != wire.BytesType { |
| 412 | return 0, errUnknown |
| 413 | } |
| 414 | v, n := wire.ConsumeBytes(b) |
| 415 | if n < 0 { |
| 416 | return 0, wire.ParseError(n) |
| 417 | } |
| 418 | mp := reflect.New(goType.Elem()) |
| 419 | if err := opts.Options().Unmarshal(v, asMessage(mp)); err != nil { |
| 420 | return 0, err |
| 421 | } |
| 422 | p.AppendPointerSlice(pointerOfValue(mp)) |
| 423 | return n, nil |
| 424 | } |
| 425 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 426 | func isInitMessageSlice(p pointer, goType reflect.Type) error { |
| 427 | s := p.PointerSlice() |
| 428 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 429 | m := asMessage(v.AsValueOf(goType.Elem())) |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 430 | if err := proto.IsInitialized(m); err != nil { |
| 431 | return err |
| 432 | } |
| 433 | } |
| 434 | return nil |
| 435 | } |
| 436 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 437 | // Slices of messages |
| 438 | |
| 439 | func sizeMessageSliceIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 440 | p := pointerOfIface(ival) |
| 441 | return sizeMessageSlice(p, reflect.TypeOf(ival).Elem().Elem(), tagsize, opts) |
| 442 | } |
| 443 | |
| 444 | func appendMessageSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 445 | p := pointerOfIface(ival) |
| 446 | return appendMessageSlice(b, p, wiretag, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 447 | } |
| 448 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 449 | func consumeMessageSliceIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 450 | p := pointerOfIface(ival) |
| 451 | n, err := consumeMessageSlice(b, p, reflect.TypeOf(ival).Elem().Elem(), wtyp, opts) |
| 452 | return ival, n, err |
| 453 | } |
| 454 | |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 455 | func isInitMessageSliceIface(ival interface{}) error { |
| 456 | p := pointerOfIface(ival) |
| 457 | return isInitMessageSlice(p, reflect.TypeOf(ival).Elem().Elem()) |
| 458 | } |
| 459 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 460 | var coderMessageSliceIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 461 | size: sizeMessageSliceIface, |
| 462 | marshal: appendMessageSliceIface, |
| 463 | unmarshal: consumeMessageSliceIface, |
| 464 | isInit: isInitMessageSliceIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | func makeGroupSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 468 | num := fd.Number() |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 469 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 470 | return pointerCoderFuncs{ |
| 471 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 472 | return sizeGroupSliceInfo(p, fi, tagsize, opts) |
| 473 | }, |
| 474 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 475 | return appendGroupSliceInfo(b, p, wiretag, fi, opts) |
| 476 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 477 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 478 | return consumeGroupSliceInfo(b, p, num, wtyp, fi, opts) |
| 479 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 480 | isInit: func(p pointer) error { |
| 481 | return isInitMessageSliceInfo(p, fi) |
| 482 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | return pointerCoderFuncs{ |
| 486 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 487 | return sizeGroupSlice(p, ft, tagsize, opts) |
| 488 | }, |
| 489 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 490 | return appendGroupSlice(b, p, wiretag, ft, opts) |
| 491 | }, |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 492 | unmarshal: func(b []byte, p pointer, wtyp wire.Type, opts unmarshalOptions) (int, error) { |
| 493 | return consumeGroupSlice(b, p, num, wtyp, ft, opts) |
| 494 | }, |
Damien Neil | 5322bdb | 2019-04-09 15:57:05 -0700 | [diff] [blame] | 495 | isInit: func(p pointer) error { |
| 496 | return isInitMessageSlice(p, ft) |
| 497 | }, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | |
| 501 | func sizeGroupSlice(p pointer, messageType reflect.Type, tagsize int, _ marshalOptions) int { |
| 502 | s := p.PointerSlice() |
| 503 | n := 0 |
| 504 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 505 | m := asMessage(v.AsValueOf(messageType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 506 | n += 2*tagsize + proto.Size(m) |
| 507 | } |
| 508 | return n |
| 509 | } |
| 510 | |
| 511 | func appendGroupSlice(b []byte, p pointer, wiretag uint64, messageType reflect.Type, opts marshalOptions) ([]byte, error) { |
| 512 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 513 | var err error |
| 514 | for _, v := range s { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 515 | m := asMessage(v.AsValueOf(messageType.Elem())) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 516 | b = wire.AppendVarint(b, wiretag) // start group |
| 517 | b, err = opts.Options().MarshalAppend(b, m) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 518 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 519 | return b, err |
| 520 | } |
| 521 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 522 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 523 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 526 | func consumeGroupSlice(b []byte, p pointer, num wire.Number, wtyp wire.Type, goType reflect.Type, opts unmarshalOptions) (int, error) { |
| 527 | if wtyp != wire.StartGroupType { |
| 528 | return 0, errUnknown |
| 529 | } |
| 530 | b, n := wire.ConsumeGroup(num, b) |
| 531 | if n < 0 { |
| 532 | return 0, wire.ParseError(n) |
| 533 | } |
| 534 | mp := reflect.New(goType.Elem()) |
| 535 | if err := opts.Options().Unmarshal(b, asMessage(mp)); err != nil { |
| 536 | return 0, err |
| 537 | } |
| 538 | p.AppendPointerSlice(pointerOfValue(mp)) |
| 539 | return n, nil |
| 540 | } |
| 541 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 542 | func sizeGroupSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 543 | s := p.PointerSlice() |
| 544 | n := 0 |
| 545 | for _, v := range s { |
| 546 | n += 2*tagsize + mi.sizePointer(v, opts) |
| 547 | } |
| 548 | return n |
| 549 | } |
| 550 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 551 | func appendGroupSliceInfo(b []byte, p pointer, wiretag uint64, mi *MessageInfo, opts marshalOptions) ([]byte, error) { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 552 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 553 | var err error |
| 554 | for _, v := range s { |
| 555 | b = wire.AppendVarint(b, wiretag) // start group |
| 556 | b, err = mi.marshalAppendPointer(b, v, opts) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 557 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 558 | return b, err |
| 559 | } |
| 560 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 561 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 562 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 563 | } |
| 564 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 565 | func consumeGroupSliceInfo(b []byte, p pointer, num wire.Number, wtyp wire.Type, mi *MessageInfo, opts unmarshalOptions) (int, error) { |
| 566 | if wtyp != wire.StartGroupType { |
| 567 | return 0, errUnknown |
| 568 | } |
| 569 | m := reflect.New(mi.GoType.Elem()).Interface() |
| 570 | mp := pointerOfIface(m) |
| 571 | n, err := mi.unmarshalPointer(b, mp, num, opts) |
| 572 | if err != nil { |
| 573 | return 0, err |
| 574 | } |
| 575 | p.AppendPointerSlice(mp) |
| 576 | return n, nil |
| 577 | } |
| 578 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 579 | func sizeGroupSliceIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 580 | p := pointerOfIface(ival) |
| 581 | return sizeGroupSlice(p, reflect.TypeOf(ival).Elem().Elem(), tagsize, opts) |
| 582 | } |
| 583 | |
| 584 | func appendGroupSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 585 | p := pointerOfIface(ival) |
| 586 | return appendGroupSlice(b, p, wiretag, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 587 | } |
| 588 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 589 | func consumeGroupSliceIface(b []byte, ival interface{}, num wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 590 | p := pointerOfIface(ival) |
| 591 | n, err := consumeGroupSlice(b, p, num, wtyp, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 592 | return ival, n, err |
| 593 | } |
| 594 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 595 | var coderGroupSliceIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 596 | size: sizeGroupSliceIface, |
| 597 | marshal: appendGroupSliceIface, |
| 598 | unmarshal: consumeGroupSliceIface, |
| 599 | isInit: isInitMessageSliceIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | // Enums |
| 603 | |
| 604 | func sizeEnumIface(ival interface{}, tagsize int, _ marshalOptions) (n int) { |
| 605 | v := reflect.ValueOf(ival).Int() |
| 606 | return wire.SizeVarint(uint64(v)) + tagsize |
| 607 | } |
| 608 | |
| 609 | func appendEnumIface(b []byte, ival interface{}, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 610 | v := reflect.ValueOf(ival).Int() |
| 611 | b = wire.AppendVarint(b, wiretag) |
| 612 | b = wire.AppendVarint(b, uint64(v)) |
| 613 | return b, nil |
| 614 | } |
| 615 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 616 | func consumeEnumIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, _ unmarshalOptions) (interface{}, int, error) { |
| 617 | if wtyp != wire.VarintType { |
| 618 | return nil, 0, errUnknown |
| 619 | } |
| 620 | v, n := wire.ConsumeVarint(b) |
| 621 | if n < 0 { |
| 622 | return nil, 0, wire.ParseError(n) |
| 623 | } |
| 624 | rv := reflect.New(reflect.TypeOf(ival)).Elem() |
| 625 | rv.SetInt(int64(v)) |
| 626 | return rv.Interface(), n, nil |
| 627 | } |
| 628 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 629 | var coderEnumIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 630 | size: sizeEnumIface, |
| 631 | marshal: appendEnumIface, |
| 632 | unmarshal: consumeEnumIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | func sizeEnumSliceIface(ival interface{}, tagsize int, opts marshalOptions) (size int) { |
| 636 | return sizeEnumSliceReflect(reflect.ValueOf(ival).Elem(), tagsize, opts) |
| 637 | } |
| 638 | |
| 639 | func sizeEnumSliceReflect(s reflect.Value, tagsize int, _ marshalOptions) (size int) { |
| 640 | for i, llen := 0, s.Len(); i < llen; i++ { |
| 641 | size += wire.SizeVarint(uint64(s.Index(i).Int())) + tagsize |
| 642 | } |
| 643 | return size |
| 644 | } |
| 645 | |
| 646 | func appendEnumSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 647 | return appendEnumSliceReflect(b, reflect.ValueOf(ival).Elem(), wiretag, opts) |
| 648 | } |
| 649 | |
| 650 | func appendEnumSliceReflect(b []byte, s reflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 651 | for i, llen := 0, s.Len(); i < llen; i++ { |
| 652 | b = wire.AppendVarint(b, wiretag) |
| 653 | b = wire.AppendVarint(b, uint64(s.Index(i).Int())) |
| 654 | } |
| 655 | return b, nil |
| 656 | } |
| 657 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 658 | func consumeEnumSliceIface(b []byte, ival interface{}, _ wire.Number, wtyp wire.Type, opts unmarshalOptions) (interface{}, int, error) { |
| 659 | n, err := consumeEnumSliceReflect(b, reflect.ValueOf(ival), wtyp, opts) |
| 660 | return ival, n, err |
| 661 | } |
| 662 | |
| 663 | func consumeEnumSliceReflect(b []byte, s reflect.Value, wtyp wire.Type, _ unmarshalOptions) (n int, err error) { |
| 664 | s = s.Elem() // *[]E -> []E |
| 665 | if wtyp == wire.BytesType { |
| 666 | b, n = wire.ConsumeBytes(b) |
| 667 | if n < 0 { |
| 668 | return 0, wire.ParseError(n) |
| 669 | } |
| 670 | for len(b) > 0 { |
| 671 | v, n := wire.ConsumeVarint(b) |
| 672 | if n < 0 { |
| 673 | return 0, wire.ParseError(n) |
| 674 | } |
| 675 | rv := reflect.New(s.Type().Elem()).Elem() |
| 676 | rv.SetInt(int64(v)) |
| 677 | s.Set(reflect.Append(s, rv)) |
| 678 | b = b[n:] |
| 679 | } |
| 680 | return n, nil |
| 681 | } |
| 682 | if wtyp != wire.VarintType { |
| 683 | return 0, errUnknown |
| 684 | } |
| 685 | v, n := wire.ConsumeVarint(b) |
| 686 | if n < 0 { |
| 687 | return 0, wire.ParseError(n) |
| 688 | } |
| 689 | rv := reflect.New(s.Type().Elem()).Elem() |
| 690 | rv.SetInt(int64(v)) |
| 691 | s.Set(reflect.Append(s, rv)) |
| 692 | return n, nil |
| 693 | } |
| 694 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 695 | var coderEnumSliceIface = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 696 | size: sizeEnumSliceIface, |
| 697 | marshal: appendEnumSliceIface, |
| 698 | unmarshal: consumeEnumSliceIface, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 699 | } |
| 700 | |
Damien Neil | 7492a09 | 2019-07-10 15:23:29 -0700 | [diff] [blame^] | 701 | func sizeEnumPackedSliceIface(ival interface{}, tagsize int, opts marshalOptions) (size int) { |
| 702 | return sizeEnumPackedSliceReflect(reflect.ValueOf(ival).Elem(), tagsize, opts) |
| 703 | } |
| 704 | |
| 705 | func sizeEnumPackedSliceReflect(s reflect.Value, tagsize int, _ marshalOptions) (size int) { |
| 706 | llen := s.Len() |
| 707 | if llen == 0 { |
| 708 | return 0 |
| 709 | } |
| 710 | n := 0 |
| 711 | for i := 0; i < llen; i++ { |
| 712 | n += wire.SizeVarint(uint64(s.Index(i).Int())) |
| 713 | } |
| 714 | return tagsize + wire.SizeBytes(n) |
| 715 | } |
| 716 | |
| 717 | func appendEnumPackedSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 718 | return appendEnumPackedSliceReflect(b, reflect.ValueOf(ival).Elem(), wiretag, opts) |
| 719 | } |
| 720 | |
| 721 | func appendEnumPackedSliceReflect(b []byte, s reflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 722 | llen := s.Len() |
| 723 | if llen == 0 { |
| 724 | return b, nil |
| 725 | } |
| 726 | b = wire.AppendVarint(b, wiretag) |
| 727 | n := 0 |
| 728 | for i := 0; i < llen; i++ { |
| 729 | n += wire.SizeVarint(uint64(s.Index(i).Int())) |
| 730 | } |
| 731 | b = wire.AppendVarint(b, uint64(n)) |
| 732 | for i := 0; i < llen; i++ { |
| 733 | b = wire.AppendVarint(b, uint64(s.Index(i).Int())) |
| 734 | } |
| 735 | return b, nil |
| 736 | } |
| 737 | |
| 738 | var coderEnumPackedSliceIface = ifaceCoderFuncs{ |
| 739 | size: sizeEnumPackedSliceIface, |
| 740 | marshal: appendEnumPackedSliceIface, |
| 741 | unmarshal: consumeEnumSliceIface, |
| 742 | } |
| 743 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 744 | // Strings with UTF8 validation. |
| 745 | |
| 746 | func appendStringValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 747 | v := *p.String() |
| 748 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 749 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 750 | if !utf8.ValidString(v) { |
| 751 | return b, errInvalidUTF8{} |
| 752 | } |
| 753 | return b, nil |
| 754 | } |
| 755 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 756 | func consumeStringValidateUTF8(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (n int, err error) { |
| 757 | if wtyp != wire.BytesType { |
| 758 | return 0, errUnknown |
| 759 | } |
| 760 | v, n := wire.ConsumeString(b) |
| 761 | if n < 0 { |
| 762 | return 0, wire.ParseError(n) |
| 763 | } |
| 764 | if !utf8.ValidString(v) { |
| 765 | return 0, errInvalidUTF8{} |
| 766 | } |
| 767 | *p.String() = v |
| 768 | return n, nil |
| 769 | } |
| 770 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 771 | var coderStringValidateUTF8 = pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 772 | size: sizeString, |
| 773 | marshal: appendStringValidateUTF8, |
| 774 | unmarshal: consumeStringValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | func appendStringNoZeroValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 778 | v := *p.String() |
| 779 | if len(v) == 0 { |
| 780 | return b, nil |
| 781 | } |
| 782 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 783 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 784 | if !utf8.ValidString(v) { |
| 785 | return b, errInvalidUTF8{} |
| 786 | } |
| 787 | return b, nil |
| 788 | } |
| 789 | |
| 790 | var coderStringNoZeroValidateUTF8 = pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 791 | size: sizeStringNoZero, |
| 792 | marshal: appendStringNoZeroValidateUTF8, |
| 793 | unmarshal: consumeStringValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | func sizeStringSliceValidateUTF8(p pointer, tagsize int, _ marshalOptions) (size int) { |
| 797 | s := *p.StringSlice() |
| 798 | for _, v := range s { |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 799 | size += tagsize + wire.SizeBytes(len(v)) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 800 | } |
| 801 | return size |
| 802 | } |
| 803 | |
| 804 | func appendStringSliceValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 805 | s := *p.StringSlice() |
| 806 | var err error |
| 807 | for _, v := range s { |
| 808 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 809 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 810 | if !utf8.ValidString(v) { |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 811 | return b, errInvalidUTF8{} |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 812 | } |
| 813 | } |
| 814 | return b, err |
| 815 | } |
| 816 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 817 | func consumeStringSliceValidateUTF8(b []byte, p pointer, wtyp wire.Type, _ unmarshalOptions) (n int, err error) { |
| 818 | if wtyp != wire.BytesType { |
| 819 | return 0, errUnknown |
| 820 | } |
| 821 | sp := p.StringSlice() |
| 822 | v, n := wire.ConsumeString(b) |
| 823 | if n < 0 { |
| 824 | return 0, wire.ParseError(n) |
| 825 | } |
| 826 | if !utf8.ValidString(v) { |
| 827 | return 0, errInvalidUTF8{} |
| 828 | } |
| 829 | *sp = append(*sp, v) |
| 830 | return n, nil |
| 831 | } |
| 832 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 833 | var coderStringSliceValidateUTF8 = pointerCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 834 | size: sizeStringSliceValidateUTF8, |
| 835 | marshal: appendStringSliceValidateUTF8, |
| 836 | unmarshal: consumeStringSliceValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | func sizeStringIfaceValidateUTF8(ival interface{}, tagsize int, _ marshalOptions) int { |
| 840 | v := ival.(string) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 841 | return tagsize + wire.SizeBytes(len(v)) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | func appendStringIfaceValidateUTF8(b []byte, ival interface{}, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 845 | v := ival.(string) |
| 846 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame] | 847 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 848 | if !utf8.ValidString(v) { |
| 849 | return b, errInvalidUTF8{} |
| 850 | } |
| 851 | return b, nil |
| 852 | } |
| 853 | |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 854 | func consumeStringIfaceValidateUTF8(b []byte, _ interface{}, _ wire.Number, wtyp wire.Type, _ unmarshalOptions) (interface{}, int, error) { |
| 855 | if wtyp != wire.BytesType { |
| 856 | return nil, 0, errUnknown |
| 857 | } |
| 858 | v, n := wire.ConsumeString(b) |
| 859 | if n < 0 { |
| 860 | return nil, 0, wire.ParseError(n) |
| 861 | } |
| 862 | if !utf8.ValidString(v) { |
| 863 | return nil, 0, errInvalidUTF8{} |
| 864 | } |
| 865 | return v, n, nil |
| 866 | } |
| 867 | |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 868 | var coderStringIfaceValidateUTF8 = ifaceCoderFuncs{ |
Damien Neil | e91877d | 2019-06-27 10:54:42 -0700 | [diff] [blame] | 869 | size: sizeStringIfaceValidateUTF8, |
| 870 | marshal: appendStringIfaceValidateUTF8, |
| 871 | unmarshal: consumeStringIfaceValidateUTF8, |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 872 | } |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 873 | |
| 874 | func asMessage(v reflect.Value) pref.ProtoMessage { |
| 875 | if m, ok := v.Interface().(pref.ProtoMessage); ok { |
| 876 | return m |
| 877 | } |
| 878 | return legacyWrapMessage(v) |
| 879 | } |