-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype_decoder.go
578 lines (483 loc) · 11.7 KB
/
type_decoder.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
package rosbag
import (
"math"
"reflect"
"time"
"unsafe"
)
type fieldDecodeFunc func(raw []byte, length int) (v interface{}, off int, ok bool)
var fieldDecodeBasicHelper = map[MessageFieldType]fieldDecodeFunc{
MessageFieldTypeBool: fieldDecodeBool,
MessageFieldTypeInt8: fieldDecodeInt8,
MessageFieldTypeUint8: fieldDecodeUint8,
MessageFieldTypeInt16: fieldDecodeInt16,
MessageFieldTypeUint16: fieldDecodeUint16,
MessageFieldTypeInt32: fieldDecodeInt32,
MessageFieldTypeUint32: fieldDecodeUint32,
MessageFieldTypeInt64: fieldDecodeInt64,
MessageFieldTypeUint64: fieldDecodeUint64,
MessageFieldTypeFloat32: fieldDecodeFloat32,
MessageFieldTypeFloat64: fieldDecodeFloat64,
MessageFieldTypeString: fieldDecodeString,
MessageFieldTypeTime: fieldDecodeTime,
MessageFieldTypeDuration: fieldDecodeDuration,
}
var fieldDecodeSliceHelper map[MessageFieldType]fieldDecodeFunc
func initFieldSliceDecoder(fastMode bool) {
if fastMode {
fieldDecodeSliceHelper = map[MessageFieldType]fieldDecodeFunc{
MessageFieldTypeBool: fieldDecodeBoolSlice,
MessageFieldTypeInt8: fieldDecodeInt8Slice,
MessageFieldTypeUint8: fieldDecodeUint8Slice,
MessageFieldTypeInt16: fieldDecodeInt16Slice,
MessageFieldTypeUint16: fieldDecodeUint16Slice,
MessageFieldTypeInt32: fieldDecodeInt32Slice,
MessageFieldTypeUint32: fieldDecodeUint32Slice,
MessageFieldTypeInt64: fieldDecodeInt64Slice,
MessageFieldTypeUint64: fieldDecodeUint64Slice,
MessageFieldTypeFloat32: fieldDecodeFloat32Slice,
MessageFieldTypeFloat64: fieldDecodeFloat64Slice,
MessageFieldTypeString: fieldDecodeStringSlice,
MessageFieldTypeTime: fieldDecodeTimeSlice,
MessageFieldTypeDuration: fieldDecodeDurationSlice,
}
} else {
fieldDecodeSliceHelper = map[MessageFieldType]fieldDecodeFunc{
MessageFieldTypeBool: fieldDecodeBoolSlice,
MessageFieldTypeInt8: fieldDecodeInt8Slice,
MessageFieldTypeUint8: fieldDecodeUint8Slice,
MessageFieldTypeInt16: fieldDecodeInt16SliceSlow,
MessageFieldTypeUint16: fieldDecodeUint16SliceSlow,
MessageFieldTypeInt32: fieldDecodeInt32SliceSlow,
MessageFieldTypeUint32: fieldDecodeUint32SliceSlow,
MessageFieldTypeInt64: fieldDecodeInt64SliceSlow,
MessageFieldTypeUint64: fieldDecodeUint64SliceSlow,
MessageFieldTypeFloat32: fieldDecodeFloat32SliceSlow,
MessageFieldTypeFloat64: fieldDecodeFloat64SliceSlow,
MessageFieldTypeString: fieldDecodeStringSlice,
MessageFieldTypeTime: fieldDecodeTimeSlice,
MessageFieldTypeDuration: fieldDecodeDurationSlice,
}
}
}
func fieldDecodeLength(raw []byte, fixedLength int) (length int, off int, ok bool) {
if fixedLength >= 0 {
ok = true
length = fixedLength
return
}
if len(raw) < lenInBytes {
return
}
length = int(endian.Uint32(raw))
if len(raw) < lenInBytes+length {
return
}
ok = true
off = lenInBytes
return
}
func fieldDecodeBool(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 1
if len(raw) < off {
return
}
v = raw[0] != 0
ok = true
return
}
func fieldDecodeInt8(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 1
if len(raw) < off {
return
}
v = int8(raw[0])
ok = true
return
}
func fieldDecodeUint8(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 1
if len(raw) < off {
return
}
v = uint8(raw[0])
ok = true
return
}
func fieldDecodeInt16(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 2
if len(raw) < off {
return
}
v = int16(endian.Uint16(raw))
ok = true
return
}
func fieldDecodeUint16(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 2
if len(raw) < off {
return
}
v = endian.Uint16(raw)
ok = true
return
}
func fieldDecodeInt32(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 4
if len(raw) < off {
return
}
v = int32(endian.Uint32(raw))
ok = true
return
}
func fieldDecodeUint32(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 4
if len(raw) < off {
return
}
v = endian.Uint32(raw)
ok = true
return
}
func fieldDecodeInt64(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 8
if len(raw) < off {
return
}
v = int64(endian.Uint64(raw))
ok = true
return
}
func fieldDecodeUint64(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 8
if len(raw) < off {
return
}
v = endian.Uint64(raw)
ok = true
return
}
func fieldDecodeFloat32(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 4
if len(raw) < off {
return
}
u := endian.Uint32(raw)
v = math.Float32frombits(u)
ok = true
return
}
func fieldDecodeFloat64(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 8
if len(raw) < off {
return
}
u := endian.Uint64(raw)
v = math.Float64frombits(u)
ok = true
return
}
func fieldDecodeString(raw []byte, length int) (v interface{}, off int, ok bool) {
var s string
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
if length == 0 {
v = ""
ok = true
return
}
raw = raw[off:]
if len(raw) < length {
ok = false
return
}
sl := (*reflect.StringHeader)(unsafe.Pointer(&s))
sl.Data = uintptr(unsafe.Pointer(&raw[0]))
sl.Len = length
off += length
ok = true
v = s
return
}
func fieldDecodeTime(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 8
if len(raw) < off {
return
}
v = extractTime(raw)
ok = true
return
}
func fieldDecodeDuration(raw []byte, length int) (v interface{}, off int, ok bool) {
off = 8
if len(raw) < off {
return
}
v = extractDuration(raw)
ok = true
return
}
func fieldDecodeBasicSlice(raw []byte, ptr unsafe.Pointer, length int, size int) (off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
if length == 0 {
ok = true
return
}
raw = raw[off:]
if len(raw) < length*size {
ok = false
return
}
s := (*reflect.SliceHeader)(ptr)
s.Data = uintptr(unsafe.Pointer(&raw[0]))
s.Len = length
s.Cap = length
off += length * size
ok = true
return
}
func fieldDecodeBoolSlice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []bool
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 1)
v = b
return
}
func fieldDecodeInt8Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []int8
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 1)
v = b
return
}
func fieldDecodeUint8Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []uint8
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 1)
v = b
return
}
func fieldDecodeInt16Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []int16
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 2)
v = b
return
}
func fieldDecodeInt16SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]int16, length)
for i := range arr {
arr[i] = int16(endian.Uint16(raw[off:]))
off += 2
}
v = arr
return
}
func fieldDecodeUint16Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []uint16
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 2)
v = b
return
}
func fieldDecodeUint16SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]uint16, length)
for i := range arr {
arr[i] = endian.Uint16(raw[off:])
off += 2
}
v = arr
return
}
func fieldDecodeInt32Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []int32
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 4)
v = b
return
}
func fieldDecodeInt32SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]int32, length)
for i := range arr {
arr[i] = int32(endian.Uint32(raw[off:]))
off += 4
}
v = arr
return
}
func fieldDecodeUint32Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []uint32
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 4)
v = b
return
}
func fieldDecodeUint32SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]uint32, length)
for i := range arr {
arr[i] = endian.Uint32(raw[off:])
off += 4
}
v = arr
return
}
func fieldDecodeInt64Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []int64
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 8)
v = b
return
}
func fieldDecodeInt64SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]int64, length)
for i := range arr {
arr[i] = int64(endian.Uint64(raw[off:]))
off += 8
}
v = arr
return
}
func fieldDecodeUint64Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []uint64
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 8)
v = b
return
}
func fieldDecodeUint64SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]uint64, length)
for i := range arr {
arr[i] = endian.Uint64(raw[off:])
off += 8
}
v = arr
return
}
func fieldDecodeFloat32Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []float32
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 4)
v = b
return
}
func fieldDecodeFloat32SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]float32, length)
for i := range arr {
arr[i] = math.Float32frombits(endian.Uint32(raw[off:]))
off += 4
}
v = arr
return
}
func fieldDecodeFloat64Slice(raw []byte, length int) (v interface{}, off int, ok bool) {
var b []float64
off, ok = fieldDecodeBasicSlice(raw, unsafe.Pointer(&b), length, 8)
v = b
return
}
func fieldDecodeFloat64SliceSlow(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
arr := make([]float64, length)
for i := range arr {
arr[i] = math.Float64frombits(endian.Uint64(raw[off:]))
off += 8
}
v = arr
return
}
func fieldDecodeStringSlice(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
if length == 0 {
var s []string
v = s
ok = true
return
}
s := make([]string, length)
totalOff := off
for i := 0; i < length; i++ {
v, off, ok = fieldDecodeString(raw[totalOff:], -1)
if !ok {
off = 0
return
}
s[i] = v.(string)
totalOff += off
}
v = s
off = totalOff
ok = true
return
}
func fieldDecodeTimeSlice(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
s := make([]time.Time, length)
totalOff := off
for i := 0; i < length; i++ {
v, off, ok = fieldDecodeTime(raw[totalOff:], 0)
if !ok {
off = 0
return
}
s[i] = v.(time.Time)
totalOff += off
}
v = s
off = totalOff
ok = true
return
}
func fieldDecodeDurationSlice(raw []byte, length int) (v interface{}, off int, ok bool) {
length, off, ok = fieldDecodeLength(raw, length)
if !ok {
return
}
s := make([]time.Duration, length)
totalOff := off
for i := 0; i < length; i++ {
v, off, ok = fieldDecodeDuration(raw[totalOff:], 0)
if !ok {
off = 0
return
}
s[i] = v.(time.Duration)
totalOff += off
}
v = s
off = totalOff
ok = true
return
}