|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package protoenc |
| 6 | + |
| 7 | +import ( |
| 8 | + "errors" |
| 9 | + "math" |
| 10 | + "reflect" |
| 11 | + "unsafe" |
| 12 | + |
| 13 | + "google.golang.org/protobuf/encoding/protowire" |
| 14 | +) |
| 15 | + |
| 16 | +var predefiniedDecoders = map[reflect.Type]func(buf []byte, dst reflect.Value) (bool, error){ |
| 17 | + typeOf[[]int](): decodeIntSlice[int], |
| 18 | + typeOf[[]int32](): decodeIntSlice[int32], |
| 19 | + typeOf[[]int64](): decodeIntSlice[int64], |
| 20 | + typeOf[[]uint](): decodeIntSlice[uint], |
| 21 | + typeOf[[]uint32](): decodeIntSlice[uint32], |
| 22 | + typeOf[[]uint64](): decodeIntSlice[uint64], |
| 23 | + typeOf[[]FixedU32](): decodeFixed32[FixedU32], |
| 24 | + typeOf[[]FixedS32](): decodeFixed32[FixedS32], |
| 25 | + typeOf[[]FixedU64](): decodeFixed64[FixedU64], |
| 26 | + typeOf[[]FixedS64](): decodeFixed64[FixedS64], |
| 27 | + typeOf[[]float32](): decodeFloat32, |
| 28 | + typeOf[[]float64](): decodeFloat64, |
| 29 | +} |
| 30 | + |
| 31 | +func tryDecodePredefinedSlice(wiretype protowire.Type, buf []byte, dst reflect.Value) (bool, error) { |
| 32 | + switch wiretype { //nolint:exhaustive |
| 33 | + case protowire.VarintType, protowire.Fixed32Type, protowire.Fixed64Type: |
| 34 | + fn, ok := predefiniedDecoders[dst.Type()] |
| 35 | + if !ok { |
| 36 | + return false, nil |
| 37 | + } |
| 38 | + |
| 39 | + return fn(buf, dst) |
| 40 | + default: |
| 41 | + return false, nil |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +type integer interface { |
| 46 | + int32 | int64 | uint32 | uint64 | int | uint |
| 47 | +} |
| 48 | + |
| 49 | +func decodeIntSlice[I integer](buf []byte, dst reflect.Value) (bool, error) { |
| 50 | + var result []I |
| 51 | + |
| 52 | + for len(buf) > 0 { |
| 53 | + v, n := protowire.ConsumeVarint(buf) |
| 54 | + if n < 0 { |
| 55 | + return false, errors.New("bad protobuf varint value") |
| 56 | + } |
| 57 | + |
| 58 | + buf = buf[n:] |
| 59 | + |
| 60 | + result = append(result, I(v)) |
| 61 | + } |
| 62 | + |
| 63 | + *(*[]I)(unsafe.Pointer(dst.UnsafeAddr())) = result |
| 64 | + |
| 65 | + return true, nil |
| 66 | +} |
| 67 | + |
| 68 | +func decodeFixed32[F FixedU32 | FixedS32](buf []byte, dst reflect.Value) (bool, error) { |
| 69 | + var result []F |
| 70 | + |
| 71 | + for len(buf) > 0 { |
| 72 | + v, n := protowire.ConsumeFixed32(buf) |
| 73 | + if n < 0 { |
| 74 | + return false, errors.New("bad protobuf 32-bit value") |
| 75 | + } |
| 76 | + |
| 77 | + buf = buf[n:] |
| 78 | + |
| 79 | + result = append(result, F(v)) |
| 80 | + } |
| 81 | + |
| 82 | + *(*[]F)(unsafe.Pointer(dst.UnsafeAddr())) = result |
| 83 | + |
| 84 | + return true, nil |
| 85 | +} |
| 86 | + |
| 87 | +func decodeFixed64[F FixedU64 | FixedS64](buf []byte, dst reflect.Value) (bool, error) { |
| 88 | + var result []F |
| 89 | + |
| 90 | + for len(buf) > 0 { |
| 91 | + v, n := protowire.ConsumeFixed64(buf) |
| 92 | + if n < 0 { |
| 93 | + return false, errors.New("bad protobuf 64-bit value") |
| 94 | + } |
| 95 | + |
| 96 | + buf = buf[n:] |
| 97 | + |
| 98 | + result = append(result, F(v)) |
| 99 | + } |
| 100 | + |
| 101 | + *(*[]F)(unsafe.Pointer(dst.UnsafeAddr())) = result |
| 102 | + |
| 103 | + return true, nil |
| 104 | +} |
| 105 | + |
| 106 | +func decodeFloat32(buf []byte, dst reflect.Value) (bool, error) { |
| 107 | + var result []float32 |
| 108 | + |
| 109 | + for len(buf) > 0 { |
| 110 | + v, n := protowire.ConsumeFixed32(buf) |
| 111 | + if n < 0 { |
| 112 | + return false, errors.New("bad protobuf 32-bit value") |
| 113 | + } |
| 114 | + |
| 115 | + buf = buf[n:] |
| 116 | + |
| 117 | + result = append(result, math.Float32frombits(v)) |
| 118 | + } |
| 119 | + |
| 120 | + *(*[]float32)(unsafe.Pointer(dst.UnsafeAddr())) = result |
| 121 | + |
| 122 | + return true, nil |
| 123 | +} |
| 124 | + |
| 125 | +func decodeFloat64(buf []byte, dst reflect.Value) (bool, error) { |
| 126 | + var result []float64 |
| 127 | + |
| 128 | + for len(buf) > 0 { |
| 129 | + v, n := protowire.ConsumeFixed64(buf) |
| 130 | + if n < 0 { |
| 131 | + return false, errors.New("bad protobuf 64-bit value") |
| 132 | + } |
| 133 | + |
| 134 | + buf = buf[n:] |
| 135 | + |
| 136 | + result = append(result, math.Float64frombits(v)) |
| 137 | + } |
| 138 | + |
| 139 | + *(*[]float64)(unsafe.Pointer(dst.UnsafeAddr())) = result |
| 140 | + |
| 141 | + return true, nil |
| 142 | +} |
0 commit comments