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 ( |
| 8 | "fmt" |
| 9 | "reflect" |
| 10 | "unicode/utf8" |
| 11 | |
| 12 | "google.golang.org/protobuf/internal/encoding/wire" |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 13 | "google.golang.org/protobuf/proto" |
| 14 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 15 | ) |
| 16 | |
| 17 | type errInvalidUTF8 struct{} |
| 18 | |
| 19 | func (errInvalidUTF8) Error() string { return "string field contains invalid UTF-8" } |
| 20 | func (errInvalidUTF8) InvalidUTF8() bool { return true } |
| 21 | |
| 22 | func makeOneofFieldCoder(fs reflect.StructField, od pref.OneofDescriptor, structFields map[pref.FieldNumber]reflect.StructField, otypes map[pref.FieldNumber]reflect.Type) pointerCoderFuncs { |
| 23 | type oneofFieldInfo struct { |
| 24 | wiretag uint64 |
| 25 | tagsize int |
| 26 | funcs pointerCoderFuncs |
| 27 | } |
| 28 | |
| 29 | oneofFieldInfos := make(map[reflect.Type]oneofFieldInfo) |
| 30 | for i, fields := 0, od.Fields(); i < fields.Len(); i++ { |
| 31 | fd := fields.Get(i) |
| 32 | ot := otypes[fd.Number()] |
| 33 | wiretag := wire.EncodeTag(fd.Number(), wireTypes[fd.Kind()]) |
| 34 | oneofFieldInfos[ot] = oneofFieldInfo{ |
| 35 | wiretag: wiretag, |
| 36 | tagsize: wire.SizeVarint(wiretag), |
| 37 | funcs: fieldCoder(fd, ot.Field(0).Type), |
| 38 | } |
| 39 | } |
| 40 | ft := fs.Type |
| 41 | return pointerCoderFuncs{ |
| 42 | size: func(p pointer, _ int, opts marshalOptions) int { |
| 43 | v := p.AsValueOf(ft).Elem() |
| 44 | if v.IsNil() { |
| 45 | return 0 |
| 46 | } |
| 47 | v = v.Elem() // interface -> *struct |
| 48 | telem := v.Elem().Type() |
| 49 | info, ok := oneofFieldInfos[telem] |
| 50 | if !ok { |
| 51 | panic(fmt.Errorf("invalid oneof type %v", telem)) |
| 52 | } |
| 53 | return info.funcs.size(pointerOfValue(v).Apply(zeroOffset), info.tagsize, opts) |
| 54 | }, |
| 55 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 56 | v := p.AsValueOf(ft).Elem() |
| 57 | if v.IsNil() { |
| 58 | return b, nil |
| 59 | } |
| 60 | v = v.Elem() // interface -> *struct |
| 61 | telem := v.Elem().Type() |
| 62 | info, ok := oneofFieldInfos[telem] |
| 63 | if !ok { |
| 64 | panic(fmt.Errorf("invalid oneof type %v", telem)) |
| 65 | } |
| 66 | return info.funcs.marshal(b, pointerOfValue(v).Apply(zeroOffset), info.wiretag, opts) |
| 67 | }, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func makeMessageFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 72 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 73 | return pointerCoderFuncs{ |
| 74 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 75 | return sizeMessageInfo(p, fi, tagsize, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 76 | }, |
| 77 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 78 | return appendMessageInfo(b, p, wiretag, fi, opts) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 79 | }, |
| 80 | } |
| 81 | } else { |
| 82 | return pointerCoderFuncs{ |
| 83 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 84 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 85 | return sizeMessage(m, tagsize, opts) |
| 86 | }, |
| 87 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 88 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 89 | return appendMessage(b, m, wiretag, opts) |
| 90 | }, |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 95 | func sizeMessageInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 96 | return wire.SizeBytes(mi.sizePointer(p.Elem(), opts)) + tagsize |
| 97 | } |
| 98 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 99 | 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] | 100 | b = wire.AppendVarint(b, wiretag) |
| 101 | b = wire.AppendVarint(b, uint64(mi.sizePointer(p.Elem(), opts))) |
| 102 | return mi.marshalAppendPointer(b, p.Elem(), opts) |
| 103 | } |
| 104 | |
| 105 | func sizeMessage(m proto.Message, tagsize int, _ marshalOptions) int { |
| 106 | return wire.SizeBytes(proto.Size(m)) + tagsize |
| 107 | } |
| 108 | |
| 109 | func appendMessage(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 110 | b = wire.AppendVarint(b, wiretag) |
| 111 | b = wire.AppendVarint(b, uint64(proto.Size(m))) |
| 112 | return opts.Options().MarshalAppend(b, m) |
| 113 | } |
| 114 | |
| 115 | func sizeMessageIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 116 | m := Export{}.MessageOf(ival).Interface() |
| 117 | return sizeMessage(m, tagsize, opts) |
| 118 | } |
| 119 | |
| 120 | func appendMessageIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 121 | m := Export{}.MessageOf(ival).Interface() |
| 122 | return appendMessage(b, m, wiretag, opts) |
| 123 | } |
| 124 | |
| 125 | var coderMessageIface = ifaceCoderFuncs{ |
| 126 | size: sizeMessageIface, |
| 127 | marshal: appendMessageIface, |
| 128 | } |
| 129 | |
| 130 | func makeGroupFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 131 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 132 | return pointerCoderFuncs{ |
| 133 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 134 | return sizeGroupType(p, fi, tagsize, opts) |
| 135 | }, |
| 136 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 137 | return appendGroupType(b, p, wiretag, fi, opts) |
| 138 | }, |
| 139 | } |
| 140 | } else { |
| 141 | return pointerCoderFuncs{ |
| 142 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 143 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 144 | return sizeGroup(m, tagsize, opts) |
| 145 | }, |
| 146 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 147 | m := asMessage(p.AsValueOf(ft).Elem()) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 148 | return appendGroup(b, m, wiretag, opts) |
| 149 | }, |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 154 | func sizeGroupType(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 155 | return 2*tagsize + mi.sizePointer(p.Elem(), opts) |
| 156 | } |
| 157 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 158 | 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] | 159 | b = wire.AppendVarint(b, wiretag) // start group |
| 160 | b, err := mi.marshalAppendPointer(b, p.Elem(), opts) |
| 161 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 162 | return b, err |
| 163 | } |
| 164 | |
| 165 | func sizeGroup(m proto.Message, tagsize int, _ marshalOptions) int { |
| 166 | return 2*tagsize + proto.Size(m) |
| 167 | } |
| 168 | |
| 169 | func appendGroup(b []byte, m proto.Message, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 170 | b = wire.AppendVarint(b, wiretag) // start group |
| 171 | b, err := opts.Options().MarshalAppend(b, m) |
| 172 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 173 | return b, err |
| 174 | } |
| 175 | |
| 176 | func sizeGroupIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 177 | m := Export{}.MessageOf(ival).Interface() |
| 178 | return sizeGroup(m, tagsize, opts) |
| 179 | } |
| 180 | |
| 181 | func appendGroupIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 182 | m := Export{}.MessageOf(ival).Interface() |
| 183 | return appendGroup(b, m, wiretag, opts) |
| 184 | } |
| 185 | |
| 186 | var coderGroupIface = ifaceCoderFuncs{ |
| 187 | size: sizeGroupIface, |
| 188 | marshal: appendGroupIface, |
| 189 | } |
| 190 | |
| 191 | func makeMessageSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 192 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 193 | return pointerCoderFuncs{ |
| 194 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 195 | return appendMessageSliceInfo(b, p, wiretag, fi, opts) |
| 196 | }, |
| 197 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 198 | return sizeMessageSliceInfo(p, fi, tagsize, opts) |
| 199 | }, |
| 200 | } |
| 201 | } |
| 202 | return pointerCoderFuncs{ |
| 203 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 204 | return sizeMessageSlice(p, ft, tagsize, opts) |
| 205 | }, |
| 206 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 207 | return appendMessageSlice(b, p, wiretag, ft, opts) |
| 208 | }, |
| 209 | } |
| 210 | } |
| 211 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 212 | func sizeMessageSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 213 | s := p.PointerSlice() |
| 214 | n := 0 |
| 215 | for _, v := range s { |
| 216 | n += wire.SizeBytes(mi.sizePointer(v, opts)) + tagsize |
| 217 | } |
| 218 | return n |
| 219 | } |
| 220 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 221 | 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] | 222 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 223 | var err error |
| 224 | for _, v := range s { |
| 225 | b = wire.AppendVarint(b, wiretag) |
| 226 | siz := mi.sizePointer(v, opts) |
| 227 | b = wire.AppendVarint(b, uint64(siz)) |
| 228 | b, err = mi.marshalAppendPointer(b, v, opts) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 229 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 230 | return b, err |
| 231 | } |
| 232 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 233 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | func sizeMessageSlice(p pointer, goType reflect.Type, tagsize int, _ marshalOptions) int { |
| 237 | s := p.PointerSlice() |
| 238 | n := 0 |
| 239 | for _, v := range s { |
| 240 | m := Export{}.MessageOf(v.AsValueOf(goType.Elem()).Interface()).Interface() |
| 241 | n += wire.SizeBytes(proto.Size(m)) + tagsize |
| 242 | } |
| 243 | return n |
| 244 | } |
| 245 | |
| 246 | func appendMessageSlice(b []byte, p pointer, wiretag uint64, goType reflect.Type, opts marshalOptions) ([]byte, error) { |
| 247 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 248 | var err error |
| 249 | for _, v := range s { |
| 250 | m := Export{}.MessageOf(v.AsValueOf(goType.Elem()).Interface()).Interface() |
| 251 | b = wire.AppendVarint(b, wiretag) |
| 252 | siz := proto.Size(m) |
| 253 | b = wire.AppendVarint(b, uint64(siz)) |
| 254 | b, err = opts.Options().MarshalAppend(b, m) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 255 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 256 | return b, err |
| 257 | } |
| 258 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 259 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // Slices of messages |
| 263 | |
| 264 | func sizeMessageSliceIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 265 | p := pointerOfIface(ival) |
| 266 | return sizeMessageSlice(p, reflect.TypeOf(ival).Elem().Elem(), tagsize, opts) |
| 267 | } |
| 268 | |
| 269 | func appendMessageSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 270 | p := pointerOfIface(ival) |
| 271 | return appendMessageSlice(b, p, wiretag, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 272 | } |
| 273 | |
| 274 | var coderMessageSliceIface = ifaceCoderFuncs{ |
| 275 | size: sizeMessageSliceIface, |
| 276 | marshal: appendMessageSliceIface, |
| 277 | } |
| 278 | |
| 279 | func makeGroupSliceFieldCoder(fd pref.FieldDescriptor, ft reflect.Type) pointerCoderFuncs { |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 280 | if fi, ok := getMessageInfo(ft); ok { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 281 | return pointerCoderFuncs{ |
| 282 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 283 | return sizeGroupSliceInfo(p, fi, tagsize, opts) |
| 284 | }, |
| 285 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 286 | return appendGroupSliceInfo(b, p, wiretag, fi, opts) |
| 287 | }, |
| 288 | } |
| 289 | } |
| 290 | return pointerCoderFuncs{ |
| 291 | size: func(p pointer, tagsize int, opts marshalOptions) int { |
| 292 | return sizeGroupSlice(p, ft, tagsize, opts) |
| 293 | }, |
| 294 | marshal: func(b []byte, p pointer, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 295 | return appendGroupSlice(b, p, wiretag, ft, opts) |
| 296 | }, |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | func sizeGroupSlice(p pointer, messageType reflect.Type, tagsize int, _ marshalOptions) int { |
| 301 | s := p.PointerSlice() |
| 302 | n := 0 |
| 303 | for _, v := range s { |
| 304 | m := Export{}.MessageOf(v.AsValueOf(messageType.Elem()).Interface()).Interface() |
| 305 | n += 2*tagsize + proto.Size(m) |
| 306 | } |
| 307 | return n |
| 308 | } |
| 309 | |
| 310 | func appendGroupSlice(b []byte, p pointer, wiretag uint64, messageType reflect.Type, opts marshalOptions) ([]byte, error) { |
| 311 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 312 | var err error |
| 313 | for _, v := range s { |
| 314 | m := Export{}.MessageOf(v.AsValueOf(messageType.Elem()).Interface()).Interface() |
| 315 | b = wire.AppendVarint(b, wiretag) // start group |
| 316 | b, err = opts.Options().MarshalAppend(b, m) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 317 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 318 | return b, err |
| 319 | } |
| 320 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 321 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 322 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 325 | func sizeGroupSliceInfo(p pointer, mi *MessageInfo, tagsize int, opts marshalOptions) int { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 326 | s := p.PointerSlice() |
| 327 | n := 0 |
| 328 | for _, v := range s { |
| 329 | n += 2*tagsize + mi.sizePointer(v, opts) |
| 330 | } |
| 331 | return n |
| 332 | } |
| 333 | |
Joe Tsai | 4fe9663 | 2019-05-22 05:12:36 -0400 | [diff] [blame] | 334 | 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] | 335 | s := p.PointerSlice() |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 336 | var err error |
| 337 | for _, v := range s { |
| 338 | b = wire.AppendVarint(b, wiretag) // start group |
| 339 | b, err = mi.marshalAppendPointer(b, v, opts) |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 340 | if err != nil { |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 341 | return b, err |
| 342 | } |
| 343 | b = wire.AppendVarint(b, wiretag+1) // end group |
| 344 | } |
Damien Neil | 8c86fc5 | 2019-06-19 09:28:29 -0700 | [diff] [blame] | 345 | return b, nil |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | func sizeGroupSliceIface(ival interface{}, tagsize int, opts marshalOptions) int { |
| 349 | p := pointerOfIface(ival) |
| 350 | return sizeGroupSlice(p, reflect.TypeOf(ival).Elem().Elem(), tagsize, opts) |
| 351 | } |
| 352 | |
| 353 | func appendGroupSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 354 | p := pointerOfIface(ival) |
| 355 | return appendGroupSlice(b, p, wiretag, reflect.TypeOf(ival).Elem().Elem(), opts) |
| 356 | } |
| 357 | |
| 358 | var coderGroupSliceIface = ifaceCoderFuncs{ |
| 359 | size: sizeGroupSliceIface, |
| 360 | marshal: appendGroupSliceIface, |
| 361 | } |
| 362 | |
| 363 | // Enums |
| 364 | |
| 365 | func sizeEnumIface(ival interface{}, tagsize int, _ marshalOptions) (n int) { |
| 366 | v := reflect.ValueOf(ival).Int() |
| 367 | return wire.SizeVarint(uint64(v)) + tagsize |
| 368 | } |
| 369 | |
| 370 | func appendEnumIface(b []byte, ival interface{}, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 371 | v := reflect.ValueOf(ival).Int() |
| 372 | b = wire.AppendVarint(b, wiretag) |
| 373 | b = wire.AppendVarint(b, uint64(v)) |
| 374 | return b, nil |
| 375 | } |
| 376 | |
| 377 | var coderEnumIface = ifaceCoderFuncs{ |
| 378 | size: sizeEnumIface, |
| 379 | marshal: appendEnumIface, |
| 380 | } |
| 381 | |
| 382 | func sizeEnumSliceIface(ival interface{}, tagsize int, opts marshalOptions) (size int) { |
| 383 | return sizeEnumSliceReflect(reflect.ValueOf(ival).Elem(), tagsize, opts) |
| 384 | } |
| 385 | |
| 386 | func sizeEnumSliceReflect(s reflect.Value, tagsize int, _ marshalOptions) (size int) { |
| 387 | for i, llen := 0, s.Len(); i < llen; i++ { |
| 388 | size += wire.SizeVarint(uint64(s.Index(i).Int())) + tagsize |
| 389 | } |
| 390 | return size |
| 391 | } |
| 392 | |
| 393 | func appendEnumSliceIface(b []byte, ival interface{}, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 394 | return appendEnumSliceReflect(b, reflect.ValueOf(ival).Elem(), wiretag, opts) |
| 395 | } |
| 396 | |
| 397 | func appendEnumSliceReflect(b []byte, s reflect.Value, wiretag uint64, opts marshalOptions) ([]byte, error) { |
| 398 | for i, llen := 0, s.Len(); i < llen; i++ { |
| 399 | b = wire.AppendVarint(b, wiretag) |
| 400 | b = wire.AppendVarint(b, uint64(s.Index(i).Int())) |
| 401 | } |
| 402 | return b, nil |
| 403 | } |
| 404 | |
| 405 | var coderEnumSliceIface = ifaceCoderFuncs{ |
| 406 | size: sizeEnumSliceIface, |
| 407 | marshal: appendEnumSliceIface, |
| 408 | } |
| 409 | |
| 410 | // Strings with UTF8 validation. |
| 411 | |
| 412 | func appendStringValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 413 | v := *p.String() |
| 414 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame^] | 415 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 416 | if !utf8.ValidString(v) { |
| 417 | return b, errInvalidUTF8{} |
| 418 | } |
| 419 | return b, nil |
| 420 | } |
| 421 | |
| 422 | var coderStringValidateUTF8 = pointerCoderFuncs{ |
| 423 | size: sizeString, |
| 424 | marshal: appendStringValidateUTF8, |
| 425 | } |
| 426 | |
| 427 | func appendStringNoZeroValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 428 | v := *p.String() |
| 429 | if len(v) == 0 { |
| 430 | return b, nil |
| 431 | } |
| 432 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame^] | 433 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 434 | if !utf8.ValidString(v) { |
| 435 | return b, errInvalidUTF8{} |
| 436 | } |
| 437 | return b, nil |
| 438 | } |
| 439 | |
| 440 | var coderStringNoZeroValidateUTF8 = pointerCoderFuncs{ |
| 441 | size: sizeStringNoZero, |
| 442 | marshal: appendStringNoZeroValidateUTF8, |
| 443 | } |
| 444 | |
| 445 | func sizeStringSliceValidateUTF8(p pointer, tagsize int, _ marshalOptions) (size int) { |
| 446 | s := *p.StringSlice() |
| 447 | for _, v := range s { |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame^] | 448 | size += tagsize + wire.SizeBytes(len(v)) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 449 | } |
| 450 | return size |
| 451 | } |
| 452 | |
| 453 | func appendStringSliceValidateUTF8(b []byte, p pointer, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 454 | s := *p.StringSlice() |
| 455 | var err error |
| 456 | for _, v := range s { |
| 457 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame^] | 458 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 459 | if !utf8.ValidString(v) { |
| 460 | err = errInvalidUTF8{} |
| 461 | } |
| 462 | } |
| 463 | return b, err |
| 464 | } |
| 465 | |
| 466 | var coderStringSliceValidateUTF8 = pointerCoderFuncs{ |
| 467 | size: sizeStringSliceValidateUTF8, |
| 468 | marshal: appendStringSliceValidateUTF8, |
| 469 | } |
| 470 | |
| 471 | func sizeStringIfaceValidateUTF8(ival interface{}, tagsize int, _ marshalOptions) int { |
| 472 | v := ival.(string) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame^] | 473 | return tagsize + wire.SizeBytes(len(v)) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | func appendStringIfaceValidateUTF8(b []byte, ival interface{}, wiretag uint64, _ marshalOptions) ([]byte, error) { |
| 477 | v := ival.(string) |
| 478 | b = wire.AppendVarint(b, wiretag) |
Damien Neil | cedb595 | 2019-06-21 12:04:07 -0700 | [diff] [blame^] | 479 | b = wire.AppendString(b, v) |
Damien Neil | c37adef | 2019-04-01 13:49:56 -0700 | [diff] [blame] | 480 | if !utf8.ValidString(v) { |
| 481 | return b, errInvalidUTF8{} |
| 482 | } |
| 483 | return b, nil |
| 484 | } |
| 485 | |
| 486 | var coderStringIfaceValidateUTF8 = ifaceCoderFuncs{ |
| 487 | size: sizeStringIfaceValidateUTF8, |
| 488 | marshal: appendStringIfaceValidateUTF8, |
| 489 | } |
Damien Neil | 5b6d047 | 2019-06-14 11:54:07 -0700 | [diff] [blame] | 490 | |
| 491 | func asMessage(v reflect.Value) pref.ProtoMessage { |
| 492 | if m, ok := v.Interface().(pref.ProtoMessage); ok { |
| 493 | return m |
| 494 | } |
| 495 | return legacyWrapMessage(v) |
| 496 | } |