/
fielddata.go
601 lines (561 loc) · 21.9 KB
/
fielddata.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package lazyproto
import (
"encoding/binary"
"fmt"
"math"
"reflect"
"sort"
"strings"
"sync"
"github.com/CrowdStrike/csproto"
)
// FieldData is a wrapper around lazily-decoded Protobuf field data, with accessors for retrieving
// typed values.
//
// All methods potentially return an error because the Protobuf encoding is not self-describing. We
// must instead rely on the consumer having some prior knowledge of the message data and using that
// knowledge to access the fields appropriately.
//
// The XxxValue() methods convert the lazily decoded field data into a single value of the appropriate
// Go type. If the decoded message contained repeated data for the field, the last value is returned.
//
// Similarly, the XxxValues() methods convert the data into a slice of the appropriate Go type. If the
// decoded message contained only a single value, or the field is not defined as repeated, the methods
// return a one-element slice. We err on the side of determinism since it's not possible to distinguish
// between these two scenarios based on only the encoded data.
//
// For both XxxValue() and XxxValues(), if the source data was not of the correct Protobuf wire type
// for the target Go type a [WireTypeMismatchError] error is returned with relevant details.
//
// A zero-valued instance is the equivalent of a varint field with no data. All methods valid for varint
// data will return [ErrTagNotFound] and all others will return a [WireTypeMismatchError].
//
// To avoid panics, any method called on a nil instance returns a zero value and [ErrTagNotFound].
type FieldData struct {
// holds the Protobuf wire type from the source data
wt csproto.WireType
// holds either:
// . one or more []byte values containing the raw bytes from the decoded message for single or
// repeated scalar values
// . a map[int]*FieldData for nested values
data []any
}
// BoolValue converts the lazily-decoded field data into a bool.
//
// Since Protobuf encodes boolean values as integers, any varint-encoded integer value is valid. A value
// of zero is treated as false and any non-zero value is treated as true.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) BoolValue() (bool, error) {
return scalarValue(fd, csproto.WireTypeVarint, func(data []byte) (bool, error) {
value, _, err := csproto.DecodeVarint(data)
if err != nil {
return false, err
}
return value != 0, nil
})
}
// BoolValues converts the lazily-decoded field data into a []bool.
//
// Since Protobuf encodes boolean values as integers, any varint-encoded integer value is valid. A value
// of zero is treated as false and any non-zero value is treated as true.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) BoolValues() ([]bool, error) {
return sliceValue(fd, csproto.WireTypeVarint, func(data []byte) (bool, int, error) {
v, n, err := csproto.DecodeVarint(data)
if err != nil {
return false, 0, err
}
return v != 0, n, nil
})
}
// StringValue converts the lazily-decoded field data into a string.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) StringValue() (string, error) {
return scalarValue(fd, csproto.WireTypeLengthDelimited, func(data []byte) (string, error) {
return string(data), nil
})
}
// StringValues converts the lazily-decoded field data into a []string.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) StringValues() ([]string, error) {
return sliceValue(fd, csproto.WireTypeLengthDelimited, func(data []byte) (string, int, error) {
return string(data), len(data), nil
})
}
// BytesValue converts the lazily-decoded field data into a []byte.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) BytesValue() ([]byte, error) {
return scalarValue(fd, csproto.WireTypeLengthDelimited, func(data []byte) ([]byte, error) {
return data, nil
})
}
// BytesValues converts the lazily-decoded field data into a [][]byte.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) BytesValues() ([][]byte, error) {
return sliceValue(fd, csproto.WireTypeLengthDelimited, func(data []byte) ([]byte, int, error) {
return data, len(data), nil
})
}
// UInt32Value converts the lazily-decoded field data into a uint32.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) UInt32Value() (uint32, error) {
return scalarValue(fd, csproto.WireTypeVarint, func(data []byte) (uint32, error) {
value, _, err := csproto.DecodeVarint(data)
if err != nil {
return 0, err
}
if value > math.MaxUint32 {
return 0, csproto.ErrValueOverflow
}
return uint32(value), nil
})
}
// UInt32Values converts the lazily-decoded field data into a []uint32.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) UInt32Values() ([]uint32, error) {
return sliceValue(fd, csproto.WireTypeVarint, func(data []byte) (uint32, int, error) {
value, n, err := csproto.DecodeVarint(data)
if err != nil {
return 0, 0, err
}
if value > math.MaxUint32 {
return 0, 0, csproto.ErrValueOverflow
}
return uint32(value), n, nil
})
}
// Int32Value converts the lazily-decoded field data into an int32.
//
// Use this method to retreive values that are defined as int32 in the Protobuf message. Fields that
// are defined as sint32 (and so use the [Protobuf ZigZag encoding]) should be retrieved using
// SInt32Value() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf ZigZag encoding]: https://developers.google.com/protocol-buffers/docs/encoding#signed-ints
func (fd *FieldData) Int32Value() (int32, error) {
return scalarValue(fd, csproto.WireTypeVarint, func(data []byte) (int32, error) {
value, _, err := csproto.DecodeVarint(data)
if err != nil {
return 0, err
}
if int64(value) > math.MaxInt32 {
return 0, csproto.ErrValueOverflow
}
return int32(value), nil
})
}
// Int32Values converts the lazily-decoded field data into a []int32.
//
// Use this method to retreive values that are defined as int32 in the Protobuf message. Fields that
// are defined as sint32 (and so use the [Protobuf ZigZag encoding]) should be retrieved using
// SInt32Values() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf ZigZag encoding]: https://developers.google.com/protocol-buffers/docs/encoding#signed-ints
func (fd *FieldData) Int32Values() ([]int32, error) {
return sliceValue(fd, csproto.WireTypeVarint, func(data []byte) (int32, int, error) {
value, n, err := csproto.DecodeVarint(data)
if err != nil {
return 0, 0, err
}
if value > math.MaxUint32 {
return 0, 0, csproto.ErrValueOverflow
}
return int32(value), n, nil
})
}
// SInt32Value converts the lazily-decoded field data into an int32.
//
// Use this method to retreive values that are defined as sint32 in the Protobuf message. Fields that
// are defined as int32 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int32Value() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) SInt32Value() (int32, error) {
return scalarValue(fd, csproto.WireTypeVarint, func(data []byte) (int32, error) {
value, _, err := csproto.DecodeZigZag32(data)
if err != nil {
return 0, err
}
return value, nil
})
}
// SInt32Values converts the lazily-decoded field data into a []int32.
//
// Use this method to retreive values that are defined as sint32 in the Protobuf message. Fields that
// are defined as int32 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int32Values() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) SInt32Values() ([]int32, error) {
return sliceValue(fd, csproto.WireTypeVarint, func(data []byte) (int32, int, error) {
value, n, err := csproto.DecodeZigZag32(data)
if err != nil {
return 0, 0, err
}
return value, n, nil
})
}
// UInt64Value converts the lazily-decoded field data into a uint64.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) UInt64Value() (uint64, error) {
return scalarValue(fd, csproto.WireTypeVarint, func(data []byte) (uint64, error) {
value, _, err := csproto.DecodeVarint(data)
if err != nil {
return 0, err
}
return value, nil
})
}
// UInt64Values converts the lazily-decoded field data into a []uint64.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) UInt64Values() ([]uint64, error) {
return sliceValue(fd, csproto.WireTypeVarint, func(data []byte) (uint64, int, error) {
value, n, err := csproto.DecodeVarint(data)
if err != nil {
return 0, 0, err
}
return value, n, nil
})
}
// Int64Value converts the lazily-decoded field data into an int64.
//
// Use this method to retreive values that are defined as int64 in the Protobuf message. Fields that
// are defined as sint64 (and so use the [Protobuf ZigZag encoding]) should be retrieved using
// SInt64Value() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf ZigZag encoding]: https://developers.google.com/protocol-buffers/docs/encoding#signed-ints
func (fd *FieldData) Int64Value() (int64, error) {
return scalarValue(fd, csproto.WireTypeVarint, func(data []byte) (int64, error) {
value, _, err := csproto.DecodeVarint(data)
if err != nil {
return 0, err
}
return int64(value), nil
})
}
// Int64Values converts the lazily-decoded field data into a []int64.
//
// Use this method to retreive values that are defined as int64 in the Protobuf message. Fields that
// are defined as sint64 (and so use the [Protobuf ZigZag encoding]) should be retrieved using
// SInt64Values() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf ZigZag encoding]: https://developers.google.com/protocol-buffers/docs/encoding#signed-ints
func (fd *FieldData) Int64Values() ([]int64, error) {
return sliceValue(fd, csproto.WireTypeVarint, func(data []byte) (int64, int, error) {
value, n, err := csproto.DecodeVarint(data)
if err != nil {
return 0, 0, err
}
return int64(value), n, nil
})
}
// SInt64Value converts the lazily-decoded field data into an int64.
//
// Use this method to retreive values that are defined as sint64 in the Protobuf message. Fields that
// are defined as int64 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int64Value() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) SInt64Value() (int64, error) {
return scalarValue(fd, csproto.WireTypeVarint, func(data []byte) (int64, error) {
value, _, err := csproto.DecodeZigZag64(data)
if err != nil {
return 0, err
}
return value, nil
})
}
// SInt64Values converts the lazily-decoded field data into a []int64.
//
// Use this method to retreive values that are defined as sint64 in the Protobuf message. Fields that
// are defined as int64 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int64Values() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) SInt64Values() ([]int64, error) {
return sliceValue(fd, csproto.WireTypeVarint, func(data []byte) (int64, int, error) {
value, n, err := csproto.DecodeZigZag64(data)
if err != nil {
return 0, 0, err
}
return value, n, nil
})
}
// Fixed32Value converts the lazily-decoded field data into a uint32.
//
// Use this method to retreive values that are defined as fixed32 in the Protobuf message. Fields that
// are defined as uint32 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int32Value() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) Fixed32Value() (uint32, error) {
return scalarValue(fd, csproto.WireTypeFixed32, func(data []byte) (uint32, error) {
value, _, err := csproto.DecodeFixed32(data)
if err != nil {
return 0, err
}
return value, nil
})
}
// Fixed32Values converts the lazily-decoded field data into a []uint32.
//
// Use this method to retreive values that are defined as fixed32 in the Protobuf message. Fields that
// are defined as uint32 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int32Values() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) Fixed32Values() ([]uint32, error) {
return sliceValue(fd, csproto.WireTypeFixed32, func(data []byte) (uint32, int, error) {
value, n, err := csproto.DecodeFixed32(data)
if err != nil {
return 0, 0, err
}
return value, n, nil
})
}
// Fixed64Value converts the lazily-decoded field data into a uint64.
//
// Use this method to retreive values that are defined as fixed64 in the Protobuf message. Fields that
// are defined as uint64 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int64Value() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) Fixed64Value() (uint64, error) {
return scalarValue(fd, csproto.WireTypeFixed64, func(data []byte) (uint64, error) {
value, _, err := csproto.DecodeFixed64(data)
if err != nil {
return 0, err
}
return value, nil
})
}
// Fixed64Values converts the lazily-decoded field data into a []uint64.
//
// Use this method to retreive values that are defined as fixed64 in the Protobuf message. Fields that
// are defined as uint64 (and so use the [Protobuf base128 varint encoding]) should be retrieved using
// Int64Values() instead.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
//
// [Protobuf base128 varint encoding]: https://developers.google.com/protocol-buffers/docs/encoding#varints
func (fd *FieldData) Fixed64Values() ([]uint64, error) {
return sliceValue(fd, csproto.WireTypeFixed64, func(data []byte) (uint64, int, error) {
value, n, err := csproto.DecodeFixed64(data)
if err != nil {
return 0, 0, err
}
return value, n, nil
})
}
// Float32Value converts the lazily-decoded field data into a float32.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) Float32Value() (float32, error) {
return scalarValue(fd, csproto.WireTypeFixed32, func(data []byte) (float32, error) {
return math.Float32frombits(binary.LittleEndian.Uint32(data)), nil
})
}
// Float32Values converts the lazily-decoded field data into a []float32.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) Float32Values() ([]float32, error) {
return sliceValue(fd, csproto.WireTypeFixed32, func(data []byte) (float32, int, error) {
return math.Float32frombits(binary.LittleEndian.Uint32(data)), 4, nil
})
}
// Float64Value converts the lazily-decoded field data into a float64.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) Float64Value() (float64, error) {
return scalarValue(fd, csproto.WireTypeFixed64, func(data []byte) (float64, error) {
return math.Float64frombits(binary.LittleEndian.Uint64(data)), nil
})
}
// Float64Values converts the lazily-decoded field data into a []float64.
//
// See the [FieldData] docs for more specific details about interpreting lazily-decoded data.
func (fd *FieldData) Float64Values() ([]float64, error) {
return sliceValue(fd, csproto.WireTypeFixed64, func(data []byte) (float64, int, error) {
return math.Float64frombits(binary.LittleEndian.Uint64(data)), 8, nil
})
}
// close releases all internal resources held by fd.
//
// This is unexported because consumers should not call this method directly. It is called automatically
// by [DecodeResult.Close].
func (fd *FieldData) close() {
for i, d := range fd.data {
if sub, ok := d.(map[int]*FieldData); ok && sub != nil {
for k, v := range sub {
if v != nil {
v.close()
}
delete(sub, k)
}
fieldDataMapPool.Put(sub)
}
fd.data[i] = nil
}
fd.data = nil
}
// a sync.Pool of field data maps to cut down on repeated small allocations
var fieldDataMapPool = sync.Pool{
New: func() any {
return make(map[int]*FieldData)
},
}
// scalarProtoFieldGoType is a generic constraint that defines the Go types that can be created from
// encoded Protobuf data.
type scalarProtoFieldGoType interface {
bool | string | []byte | int32 | uint32 | int64 | uint64 | float32 | float64
}
// scalarValue is a helper to convert the lazily-decoded field data in fd to a scalar value of
// concrete type T by invoking the provided convertFn. The wt parameter contains the expected
// Protobuf wire type for a Go value of type T.
func scalarValue[T scalarProtoFieldGoType](fd *FieldData, wt csproto.WireType, convertFn func([]byte) (T, error)) (T, error) {
var zero T
if fd == nil || len(fd.data) == 0 {
return zero, ErrTagNotFound
}
if fd.wt != wt {
return zero, wireTypeMismatchError(fd.wt, wt)
}
switch data := fd.data[len(fd.data)-1].(type) {
case []byte:
value, err := convertFn(data)
if err != nil {
return zero, err
}
return value, nil
case map[int]*FieldData:
return zero, fmt.Errorf("cannot convert field data for a nested message into %T", zero)
default:
// TODO: should this be a panic?
// . elements of fd.data *SHOULD* always contain either []byte or map[int]*FieldData so this
// is a "just in case" path
return zero, rawValueConversionError[T](data)
}
}
// sliceValue is a helper to convert the lazily-decoded field data in fd to a slice of values of
// concrete type T by successively invoking the provided convertFn to produce each value. The wt parameter
// contains the expected Protobuf wire type for a Go value of type T.
func sliceValue[T scalarProtoFieldGoType](fd *FieldData, wt csproto.WireType, convertFn func([]byte) (T, int, error)) ([]T, error) {
if fd == nil {
return nil, ErrTagNotFound
}
switch fd.wt {
// wt is the wire type for values of type T
// packed repeated fields are always WireTypeLengthDelimited
case wt, csproto.WireTypeLengthDelimited:
if len(fd.data) == 0 {
return nil, ErrTagNotFound
}
var res []T
for _, rv := range fd.data {
switch data := rv.(type) {
case []byte:
// data contains 1 or more encoded values of type T
// . invoke convertFn at each successive offset to extract them
for offset := 0; offset < len(data); {
v, n, err := convertFn(data[offset:])
if err != nil {
return nil, err
}
if n == 0 {
return nil, csproto.ErrInvalidVarintData
}
res = append(res, v)
offset += n
}
case map[int]*FieldData:
var zero T
return nil, fmt.Errorf("cannot convert field data for a nested message into a %T", zero)
default:
// TODO: should this be a panic?
// . elements of fd.data *SHOULD* always contain either []byte or map[int]*FieldData so this
// is a "just in case" path
return nil, rawValueConversionError[T](data)
}
}
return res, nil
default:
if wt != csproto.WireTypeLengthDelimited {
return nil, wireTypeMismatchError(fd.wt, csproto.WireTypeLengthDelimited, wt)
}
return nil, wireTypeMismatchError(fd.wt, csproto.WireTypeLengthDelimited)
}
}
// wireTypeMismatchError constructs a new WireTypeMismatchError error
func wireTypeMismatchError(got csproto.WireType, supported ...csproto.WireType) *WireTypeMismatchError {
var want string
if len(supported) == 1 {
want = supported[0].String()
} else {
supportedTypes := make([]string, len(supported))
for i, wt := range supported {
supportedTypes[i] = wt.String()
}
sort.Strings(supportedTypes)
want = strings.Join(supportedTypes, ",")
}
err := WireTypeMismatchError(fmt.Sprintf("wire type %s must be one of: %s", got, want))
return &err
}
// WireTypeMismatchError is returned when the actual type of a lazily decoded Protobuf field does
// not match one of the supported types.
type WireTypeMismatchError string
// Error satisfies the error interface
func (e *WireTypeMismatchError) Error() string {
if e == nil {
return ""
}
return string(*e)
}
// rawValueConversionError constructs a new RawValueConversionError
func rawValueConversionError[T any](from any) *RawValueConversionError {
var target T
msg := fmt.Sprintf("unable to convert raw value (Kind = %s) to %T", reflect.ValueOf(from).Kind().String(), target)
err := RawValueConversionError(msg)
return &err
}
// RawValueConversionError is returned when the lazily-decoded value for a Protobuf field could not
// be converted to the requested Go type.
type RawValueConversionError string
// Error satisfies the error interface
func (e *RawValueConversionError) Error() string {
if e == nil {
return ""
}
return string(*e)
}