/
encoderUtil.go
282 lines (239 loc) · 6.91 KB
/
encoderUtil.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2018 Istio Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package dynamic
import (
"fmt"
"math"
"strings"
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"istio.io/istio/mixer/pkg/protobuf/yaml"
)
func makeField(fd *descriptor.FieldDescriptorProto) *fieldEncoder {
packed := fd.IsPacked() || fd.IsPacked3()
wireType := uint64(fd.WireType())
fieldNumber := int(fd.GetNumber())
if packed {
wireType = uint64(proto.WireBytes)
}
return &fieldEncoder{
protoKey: protoKey(fieldNumber, wireType),
number: fieldNumber,
name: fd.GetName(),
packed: packed,
}
}
// EncodeVarintZeroExtend encodes x as Varint in ba. Ensures that encoding is at least
// minBytes long.
func EncodeVarintZeroExtend(ba []byte, x uint64, minBytes int) []byte {
var bn int
ba, bn = EncodeVarint(ba, x)
diff := minBytes - bn
if diff <= 0 {
return ba
}
ba[len(ba)-1] = 0x80 | ba[len(ba)-1]
for ; diff > 1; diff-- {
ba = append(ba, 0x80)
}
// must end with 0x00
ba = append(ba, 0x00)
return ba
}
// The low level encoding functions are based on
// https://github.com/gogo/protobuf/blob/master/proto/encode.go#Buffer
// EncodeVarint -- encodeVarint no allocations
func EncodeVarint(buf []byte, x uint64) ([]byte, int) {
ol := len(buf)
for x > 127 {
buf = append(buf, 0x80|uint8(x&0x7F))
x >>= 7
}
buf = append(buf, uint8(x))
return buf, len(buf) - ol
}
func encodeFixed64(buf []byte, x uint64) []byte {
return append(buf,
uint8(x),
uint8(x>>8),
uint8(x>>16),
uint8(x>>24),
uint8(x>>32),
uint8(x>>40),
uint8(x>>48),
uint8(x>>56))
}
func encodeFixed32(buf []byte, x uint64) []byte {
return append(buf,
uint8(x),
uint8(x>>8),
uint8(x>>16),
uint8(x>>24))
}
func encodeZigzag32(buf []byte, x uint64) []byte {
// use signed number to get arithmetic right shift.
buf, _ = EncodeVarint(buf, uint64((uint32(x)<<1)^uint32((int32(x)>>31))))
return buf
}
func encodeZigzag64(buf []byte, x uint64) []byte {
// use signed number to get arithmetic right shift.
buf, _ = EncodeVarint(buf, (x<<1)^uint64((int64(x)>>63)))
return buf
}
// protoKey returns the key which is comprised of fieldEncoder number and wire type.
func protoKey(fieldNumber int, wireType uint64) []byte {
buf := make([]byte, 0, 2) // this is enough to encode protokey
buf, _ = EncodeVarint(buf, (uint64(fieldNumber)<<3)|wireType)
return buf
}
func badTypeError(v interface{}, wantType string) error {
return fmt.Errorf("badTypeError: value: '%v' is of type:%T, want:%s", v, v, wantType)
}
// Low level Encode Funcs
// EncodeDouble encode Double.
func EncodeDouble(v interface{}, ba []byte) ([]byte, error) {
c, ok := yaml.ToFloat(v)
if !ok {
return nil, badTypeError(v, "double")
}
return encodeFixed64(ba, math.Float64bits(c)), nil
}
// EncodeFloat encode Float.
func EncodeFloat(v interface{}, ba []byte) ([]byte, error) {
c, ok := yaml.ToFloat(v)
if !ok {
return nil, badTypeError(v, "float64")
}
return encodeFixed32(ba, uint64(math.Float32bits(float32(c)))), nil
}
// EncodeInt encodes (U)INT(32/64)
func EncodeInt(v interface{}, ba []byte) ([]byte, error) {
c, ok := yaml.ToInt64(v)
if !ok {
return nil, badTypeError(v, "int")
}
return encodeInt(c, ba)
}
func encodeInt(v int64, ba []byte) ([]byte, error) {
ba, _ = EncodeVarint(ba, uint64(v))
return ba, nil
}
// EncodeFixed64 encodes FIXED64, SFIXED64
func EncodeFixed64(v interface{}, ba []byte) ([]byte, error) {
c, ok := yaml.ToInt64(v)
if !ok {
return nil, badTypeError(v, "int")
}
return encodeFixed64(ba, uint64(c)), nil
}
// EncodeFixed32 encodes FIXED32, SFIXED32
func EncodeFixed32(v interface{}, ba []byte) ([]byte, error) {
c, ok := yaml.ToInt64(v)
if !ok {
return nil, badTypeError(v, "int")
}
return encodeFixed32(ba, uint64(c)), nil
}
// EncodeBool encodes input as boolean.
func EncodeBool(v interface{}, ba []byte) ([]byte, error) {
c, ok := v.(bool)
if !ok {
return nil, badTypeError(v, "bool")
}
return encodeBool(c, ba), nil
}
const trueByte = byte(0x1)
const falseByte = byte(0x0)
func encodeBool(c bool, ba []byte) []byte {
if c {
ba = append(ba, trueByte)
} else {
ba = append(ba, falseByte)
}
return ba
}
// EncodeString encodes string
func EncodeString(v interface{}, ba []byte) ([]byte, error) {
t, ok := v.(string)
if !ok {
return nil, badTypeError(v, "string")
}
return encodeString(t, ba)
}
func encodeString(s string, ba []byte) ([]byte, error) {
ba, _ = EncodeVarint(ba, uint64(len(s)))
ba = append(ba, s...)
return ba, nil
}
// EncodeSInt32 encodes sint32 as zigzag
func EncodeSInt32(v interface{}, ba []byte) ([]byte, error) {
c, ok := yaml.ToInt64(v)
if !ok {
return nil, badTypeError(v, "int")
}
return encodeZigzag32(ba, uint64(c)), nil
}
// EncodeSInt64 encodes sint64 as zigzag
func EncodeSInt64(v interface{}, ba []byte) ([]byte, error) {
c, ok := yaml.ToInt64(v)
if !ok {
return nil, badTypeError(v, "int")
}
return encodeZigzag64(ba, uint64(c)), nil
}
// EncodeEnum encodes enum from string or int inputs
func EncodeEnum(v interface{}, ba []byte, enumValues []*descriptor.EnumValueDescriptorProto) ([]byte, error) {
if vs, ok := v.(string); ok {
return encodeEnumString(vs, ba, enumValues)
}
if vi, ok := yaml.ToInt64(v); ok {
return encodeEnumInt(vi, ba, enumValues)
}
return nil, fmt.Errorf("unable to encode enum %v of type: %T", v, v)
}
func encodeEnumInt(v int64, ba []byte, enumValues []*descriptor.EnumValueDescriptorProto) ([]byte, error) {
for _, val := range enumValues {
if val.GetNumber() == int32(v) {
ba, _ = EncodeVarint(ba, uint64(val.GetNumber()))
return ba, nil
}
}
return nil, fmt.Errorf("unknown value: %v, enum:%v", v, enumValues)
}
func encodeEnumString(v string, ba []byte, enumValues []*descriptor.EnumValueDescriptorProto) ([]byte, error) {
for _, val := range enumValues {
if val.GetName() == v {
ba, _ = EncodeVarint(ba, uint64(val.GetNumber()))
return ba, nil
}
}
return nil, fmt.Errorf("unknown value: %v, enum:%v", v, enumValues)
}
// transformQuotedString removes quotes from strings and returns true
// if quotes were removed. constant strings are not expressions.
func transformQuotedString(v interface{}) (interface{}, bool) {
var ok bool
var s string
if s, ok = v.(string); !ok {
return v, false
}
if len(s) < 2 {
return v, false
}
if (strings.HasPrefix(s, "'") && strings.HasSuffix(s, "'")) ||
(strings.HasPrefix(s, "\"") && strings.HasSuffix(s, "\"")) {
return s[1 : len(s)-1], true
}
return s, false
}