forked from golang/protobuf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
decode.go
1656 lines (1445 loc) · 42.2 KB
/
decode.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Go support for Protocol Buffers - Google's data interchange format
//
// Copyright 2016 Mist Systems. All rights reserved.
//
// This code is derived from earlier code which was itself:
//
// Copyright 2010 The Go Authors. All rights reserved.
// https://github.com/golang/protobuf
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package protobuf3
/*
* Routines for decoding protocol buffer data to construct in-memory representations.
*/
import (
"errors"
"fmt"
"io"
"os"
"reflect"
"time"
"unsafe"
"github.com/nsd20463/cpuendian"
)
// errOverflow is returned when an integer is too large to be represented.
var errOverflow = errors.New("protobuf3: integer overflow")
// The fundamental decoders that interpret bytes on the wire.
// Those that take integer types all return uint64 and are
// therefore of type valueDecoder.
// DecodeVarint reads a varint-encoded integer from the slice.
// It returns the integer and the number of bytes consumed, or
// zero if there is not enough.
// This is the format for the
// int32, int64, uint32, uint64, bool, and enum
// protocol buffer types.
func DecodeVarint(buf []byte) (x uint64, n int) {
// x, n already 0
for shift := uint(0); shift < 64 && n < len(buf); shift += 7 {
b := uint64(buf[n])
n++
x |= (b & 0x7F) << shift
if (b & 0x80) == 0 {
return x, n
}
}
// The number is truncated in some way
return 0, 0
}
// DecodeVarint reads a varint-encoded integer from the Buffer.
// This is the format for the
// int32, int64, uint32, uint64, bool, and enum
// protocol buffer types.
func (p *Buffer) decodeVarintSlow() (x uint64, err error) {
// x, err already 0
i := p.index
l := ulen(p.buf)
for shift := uint(0); shift < 64; shift += 7 {
if i >= l {
err = io.ErrUnexpectedEOF
return
}
b := p.buf[i]
i++
x |= (uint64(b) & 0x7F) << shift
if b < 0x80 {
p.index = i
if shift == 9*7 {
// the 10th byte can't have any non-zero unused bits
if b&0xfe != 0 {
err = errOverflow
}
}
return
}
}
// The number is too large to represent in a 64-bit value.
err = errOverflow
return
}
// DecodeVarint reads a varint-encoded integer from the Buffer.
// This is the format for the int32, int64, uint32, uint64, bool,
// and enum protocol buffer types, as well as the tags.
func (p *Buffer) DecodeVarint() (x uint64, err error) {
i := p.index
buf := p.buf
n := ulen(buf)
var b uint8
if i >= n {
return 0, io.ErrUnexpectedEOF
}
// most varints are 1 byte (because they are the protobuf tag, and most of those are 1 byte)
// so it pays to have a special case for those
x = uint64(buf[i])
i++
if x < 0x80 {
goto done
}
// the longest varint we'll successfully decode is 10 bytes. so if there are more than 9 bytes
// (since we've already read one) of buffer left we can decode it with fewer bounds checks
if i+9 > n {
// there are fewer than 9 bytes left; use the slower, bounds-checking code
return p.decodeVarintSlow()
}
x &^= 0x80
// note: the only way I've found to get go 1.8.1 to do bounds-check-elimination is to use constant indexes, which
// means paying the cost of slicing buf (which is two bounds checks). That, however, ends up costing more, and
// especially it impacts the performance of the most important 1 and 2-byte cases. So instead we leave the bounds
// checks and index by `i`
//_ = buf[i+8] // doesn't help (makes the code slower) in go 1.8.1 (still true with go 1.14.4)
b = buf[i]
i++
x |= uint64(b) << 7
if b < 0x80 {
goto done
}
x &^= 0x80 << 7
b = buf[i]
i++
x |= uint64(b) << 14
if b < 0x80 {
goto done
}
x &^= 0x80 << 14
b = buf[i]
i++
x |= uint64(b) << 21
if b < 0x80 {
goto done
}
x &^= 0x80 << 21
b = buf[i]
i++
x |= uint64(b) << 28
if b < 0x80 {
goto done
}
x &^= 0x80 << 28
b = buf[i]
i++
x |= uint64(b) << 35
if b < 0x80 {
goto done
}
x &^= 0x80 << 35
b = buf[i]
i++
x |= uint64(b) << 42
if b < 0x80 {
goto done
}
x &^= 0x80 << 42
b = buf[i]
i++
x |= uint64(b) << 49
if b < 0x80 {
goto done
}
x &^= 0x80 << 49
b = buf[i]
i++
x |= uint64(b) << 56
if b < 0x80 {
goto done
}
x &^= 0x80 << 56
b = buf[i]
i++
x |= uint64(b) << 63
if b < 2 {
goto done
}
// x &^= 0x80 << 63 // Always zero.
return 0, errOverflow
done:
p.index = i
return x, nil
}
func le64tocpu(x uint64) uint64 {
if cpuendian.Big {
x = ((x & 0xff) << 56) | ((x & 0xff00) << 40) | ((x & 0xff0000) << 24) | ((x & 0xff000000) << 8) |
((x & 0xff00000000) >> 8) | ((x & 0xff0000000000) >> 24) | ((x & 0xff000000000000) >> 40) | ((x & 0xff00000000000000) >> 56)
}
return x
}
func le32tocpu(x uint32) uint32 {
if cpuendian.Big {
x = ((x & 0xff) << 24) | ((x & 0xff00) << 8) | ((x & 0xff0000) >> 8) | ((x & 0xff000000) >> 24)
}
return x
}
// DecodeFixed64 reads a 64-bit integer from the Buffer.
// This is the format for the
// fixed64, sfixed64, and double protocol buffer types.
func (p *Buffer) DecodeFixed64() (uint64, error) {
// x, err already 0
i := p.index + 8
if i < 8 || i > ulen(p.buf) {
return 0, io.ErrUnexpectedEOF
}
p.index = i
return le64tocpu(*(*uint64)(unsafe.Pointer(&p.buf[i-8]))), nil
}
// DecodeFixed32 reads a 32-bit integer from the Buffer.
// This is the format for the
// fixed32, sfixed32, and float protocol buffer types.
func (p *Buffer) DecodeFixed32() (uint64, error) {
// x, err already 0
i := p.index + 4
if i < 4 || i > ulen(p.buf) {
return 0, io.ErrUnexpectedEOF
}
p.index = i
return uint64(le32tocpu(*(*uint32)(unsafe.Pointer(&p.buf[i-4])))), nil
}
// DecodeZigzag64 reads a zigzag-encoded 64-bit integer
// from the Buffer.
// This is the format used for the sint64 protocol buffer type.
func (p *Buffer) DecodeZigzag64() (x uint64, err error) {
x, err = p.DecodeVarint()
if err != nil {
return
}
x = (x >> 1) ^ uint64((int64(x&1)<<63)>>63)
return
}
// DecodeZigzag32 reads a zigzag-encoded 32-bit integer
// from the Buffer.
// This is the format used for the sint32 protocol buffer type.
// Since I might cast the result to 'int', I want this to return a signed
// 64-bit value, rather than a signed 32-bit value embedded in an
// unsigned 64-bit value. Hence the cast to int32 before extending
// to uint64 which does not appear in the proto package.
func (p *Buffer) DecodeZigzag32() (x uint64, err error) {
x, err = p.DecodeVarint()
if err != nil {
return
}
x32 := int32((uint32(x) >> 1) ^ uint32((int32(x&1)<<31)>>31))
x = uint64(x32)
return
}
// These are not ValueDecoders: they produce an array of bytes or a string.
// bytes, embedded messages
// DecodeRawBytes reads a count-delimited byte buffer from the Buffer.
// This is the format used for the bytes protocol buffer
// type and for embedded messages.
// The returned slice points to shared memory. Treat as read-only.
func (p *Buffer) DecodeRawBytes() ([]byte, error) {
// many strings and structs are short. it pays to have a special case for these
i := p.index
n := ulen(p.buf)
if i >= n {
return nil, io.ErrUnexpectedEOF
}
c := uint(p.buf[i])
i++
if c < 0x80 {
// 1-byte count
} else if i < n && p.buf[i] < 0x80 {
// 2-byte count
c &^= 0x80
c += uint(p.buf[i]) << 7
i++
} else {
c64, err := p.DecodeVarint()
if err != nil {
return nil, err
}
c = uint(c64)
if uint64(c) != c64 {
return nil, fmt.Errorf("protobuf3: bad byte length %d", c64)
}
i = p.index
}
end := i + c
if end < i || end > n {
return nil, io.ErrUnexpectedEOF
}
buf := p.buf[i:end:end]
p.index = end
return buf, nil
}
// DecodeStringBytes reads an encoded string from the Buffer.
// This is the format used for the proto3 string type.
func (p *Buffer) DecodeStringBytes() (string, error) {
buf, err := p.DecodeRawBytes()
if err != nil {
return "", err
}
return string(buf), nil
}
// SkipVarint skips over a varint-encoded integer from the Buffer.
// Functionally it is similar to calling DecodeVarint and ignoring the
// value returned, except that it doesn't worry about 64-bit overflow
// of the varint value, and it runs much faster than DecodeVarint.
func (p *Buffer) SkipVarint() error {
i := p.index
n := ulen(p.buf)
for {
if i >= n {
return io.ErrUnexpectedEOF
}
b := p.buf[i]
i++
if b < 0x80 {
p.index = i
return nil
}
}
}
// SkipFixed skips over n bytes. Useful for skipping over Fixed32 and Fixed64 with proper arguments,
// but also used to skip over arbitrary lengths.
func (p *Buffer) SkipFixed(n uint64) error {
nb := uint(n)
if uint64(nb) != n {
return fmt.Errorf("protobuf3: bad skip length %d", n)
}
i := p.index + nb
if i < p.index || i > ulen(p.buf) {
return io.ErrUnexpectedEOF
}
p.index = i
return nil
}
// SkipRawBytes skips over a count-delimited byte buffer from the Buffer.
// Functionally it is identical to calling DecodeRawBytes() and ignoring
// the value returned.
func (p *Buffer) SkipRawBytes() error {
// many strings and structs are short. it pays to have a special case for these
i := p.index
n := ulen(p.buf)
if i >= n {
return io.ErrUnexpectedEOF
}
c := uint(p.buf[i])
i++
if c < 0x80 {
// 1-byte count
} else if i < n && p.buf[i] < 0x80 {
// 2-byte count
c &^= 0x80
c += uint(p.buf[i]) << 7
i++
} else {
c64, err := p.DecodeVarint()
if err != nil {
return err
}
c = uint(c64)
if uint64(c) != c64 {
return fmt.Errorf("protobuf3: bad byte length %d", c64)
}
i = p.index
}
end := i + c
if end < i || end > n {
return io.ErrUnexpectedEOF
}
p.index = end
return nil
}
// Unmarshal parses the protocol buffer representation in buf and
// writes the decoded result to pb. If the struct underlying pb does not match
// the data in buf, the results can be unpredictable.
//
// Unmarshal merges into existing data in pb. If that's not what you wanted then
// you ought to zero pb before calling Unmarshal. NOTE WELL this differs from the
// behavior of the golang/proto.Unmarshal(), but matches the standard go encoding/json.Unmarshal()
// Since we're used to json, and since having the caller do the zeroing is more efficient
// (both because they know the type (making it more efficient for the CPU), and it avoids forcing
// everyone to define a Reset() method for the Message interface (making it more efficient for
// the developer, me!)), our Unmarshal() matches the behavior of encoding/json.Unmarshal()
func Unmarshal(bytes []byte, pb Message) error {
buf := newBuffer(bytes)
err := buf.Unmarshal(pb)
buf.release()
return err
}
// Unmarshal parses the protocol buffer representation in the
// Buffer and places the decoded result in pb. If the struct
// underlying pb does not match the data in the buffer, the results can be
// unpredictable.
func (p *Buffer) Unmarshal(pb Message) error {
if pb == nil { // we need a non-nil interface or this won't work
return ErrNil // NOTE this could almost qualify for a panic(), because the calling code is clearly quite confused
}
// If the object can unmarshal itself, let it.
if m, ok := pb.(Marshaler); ok {
err := m.UnmarshalProtobuf3(p.buf[p.index:])
p.index = ulen(p.buf)
return err
}
// pb must be a pointer to a struct
t := reflect.TypeOf(pb)
if t.Kind() != reflect.Ptr {
return ErrNotPointerToStruct
}
t = t.Elem()
if t.Kind() != reflect.Struct {
return ErrNotPointerToStruct
}
// the caller already checked that pb is a pointer-to-struct type
base := unsafe.Pointer(reflect.ValueOf(pb).Pointer())
prop, err := GetProperties(t)
if err != nil {
return err
}
return p.unmarshal_struct(t, prop, base)
}
// unmarshal_struct does the work of unmarshaling a structure.
func (o *Buffer) unmarshal_struct(st reflect.Type, prop *StructProperties, base unsafe.Pointer) error {
var err error
var pidx = 0 // index into prop.props[] where we should start searching for the next tag
var ptag = -1 // -1, or the previous tag (matched or not, depending on whether p is nil or not)
var p *Properties // nil, or the p where p.Tag == ptag
for err == nil && o.index < ulen(o.buf) {
start := o.index
var wire WireType
var tag int
// most tags are one byte varints, so make that a special case and don't call DecodeVarint() and avoid error checks too
b := uint64(o.buf[start])
if b < 0x80 {
o.index++
wire = WireType(b & 0x7)
tag = int(b >> 3)
} else if start+1 < ulen(o.buf) && o.buf[start+1] < 0x80 {
u := uint32(b&^0x80) + uint32(o.buf[start+1])<<7
wire = WireType(u & 0x7)
tag = int(u >> 3)
o.index += 2
} else {
var u uint64
u, err = o.DecodeVarint()
if err != nil {
break
}
wire = WireType(u & 0x7)
tag = int(u >> 3)
if tag <= 0 {
return fmt.Errorf("protobuf3: %s: illegal tag %d (wiretype %v) at index %d of %d", st, tag, wire, start, len(o.buf))
}
}
if tag != ptag {
if tag < ptag {
// the order on the wire has jumped around. this is legal in protobuf, but unusual. in any case we need to
// reset the search back to the start
pidx = 0
}
p = nil
for ; pidx < len(prop.props); pidx++ {
q := &prop.props[pidx]
if q.Tag >= uint32(tag) { // props[] is sorted by Tag
if q.Tag == uint32(tag) {
p = q
}
break
}
}
ptag = tag
} // else re-use previous search result `p`
if p == nil {
err = o.skip(st, wire)
continue
}
if p.dec == nil {
fmt.Fprintf(os.Stderr, "protobuf3: no protobuf decoder for %s.%s\n", st, p.Name)
continue
}
if wire != p.WireType && wire != WireBytes { // packed encoding, which is used in protobuf v3, wraps repeated numeric types in WireBytes
err = fmt.Errorf("protobuf3: bad wiretype for field %s.%s: got wiretype %v, want %v", st, p.Name, wire, p.WireType)
break
}
err = p.dec(o, p, base)
}
return err
}
// Skip the next item in the buffer. Its wire type is decoded and presented as an argument.
// t can be nil
func (o *Buffer) skip(t reflect.Type, wire WireType) error {
switch wire {
case WireVarint:
return o.SkipVarint()
case WireBytes:
return o.SkipRawBytes()
case WireFixed64:
return o.SkipFixed(8)
case WireFixed32:
return o.SkipFixed(4)
default:
return fmt.Errorf("protobuf3: can't skip unknown wiretype %v for %v", wire, t)
}
}
// Get the value of the next item in the buffer. Similar to skip() but also returns the value.
// t can be nil
func (o *Buffer) get(t reflect.Type, wire WireType) ([]byte, error) {
var err error
start := o.index
switch wire {
case WireVarint:
err = o.SkipVarint()
case WireBytes:
var n uint64
n, err = o.DecodeVarint()
start = o.index // reset the starting index to where the byte payload starts
if err == nil {
err = o.SkipFixed(n)
}
case WireFixed64:
err = o.SkipFixed(8)
case WireFixed32:
err = o.SkipFixed(4)
default:
err = fmt.Errorf("protobuf3: can't get unknown wiretype %v for %v", wire, t)
}
if err != nil {
return nil, err
}
return o.buf[start:o.index:o.index], nil // set slice cap out of paranoid, should someone ever append()
}
// Individual type decoders
// For each,
// u is the decoded value,
// v is a pointer to the field (pointer) in the struct
// Decode a *bool.
func (o *Buffer) dec_ptr_bool(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
x := u != 0
*(**bool)(unsafe.Pointer(uintptr(base) + p.offset)) = &x
return nil
}
// Decode a bool.
func (o *Buffer) dec_bool(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
*(*bool)(unsafe.Pointer(uintptr(base) + p.offset)) = u != 0
return nil
}
// Decode an *int8.
func (o *Buffer) dec_ptr_int8(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
x := uint8(u)
*(**uint8)(unsafe.Pointer(uintptr(base) + p.offset)) = &x
return nil
}
// Decode an int8.
func (o *Buffer) dec_int8(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
*(*uint8)(unsafe.Pointer(uintptr(base) + p.offset)) = uint8(u)
return nil
}
// Decode an *int16.
func (o *Buffer) dec_ptr_int16(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
x := uint16(u)
*(**uint16)(unsafe.Pointer(uintptr(base) + p.offset)) = &x
return nil
}
// Decode an int16.
func (o *Buffer) dec_int16(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
*(*uint16)(unsafe.Pointer(uintptr(base) + p.offset)) = uint16(u)
return nil
}
// Decode an *int32.
func (o *Buffer) dec_ptr_int32(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
x := uint32(u)
*(**uint32)(unsafe.Pointer(uintptr(base) + p.offset)) = &x
return nil
}
// Decode an int32.
func (o *Buffer) dec_int32(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
*(*uint32)(unsafe.Pointer(uintptr(base) + p.offset)) = uint32(u)
return nil
}
// Decode an *int.
func (o *Buffer) dec_ptr_int(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
x := uint(u)
*(**uint)(unsafe.Pointer(uintptr(base) + p.offset)) = &x
return nil
}
// Decode an int.
func (o *Buffer) dec_int(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
*(*uint)(unsafe.Pointer(uintptr(base) + p.offset)) = uint(u)
return nil
}
// Decode an *int64.
func (o *Buffer) dec_ptr_int64(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
*(**uint64)(unsafe.Pointer(uintptr(base) + p.offset)) = &u
return nil
}
// Decode an int64.
func (o *Buffer) dec_int64(p *Properties, base unsafe.Pointer) error {
u, err := p.valDec(o)
if err != nil {
return err
}
*(*uint64)(unsafe.Pointer(uintptr(base) + p.offset)) = u
return nil
}
// Decode a *string.
func (o *Buffer) dec_ptr_string(p *Properties, base unsafe.Pointer) error {
s, err := o.DecodeStringBytes()
if err != nil {
return err
}
*(**string)(unsafe.Pointer(uintptr(base) + p.offset)) = &s
return nil
}
// Decode a string.
func (o *Buffer) dec_string(p *Properties, base unsafe.Pointer) error {
s, err := o.DecodeStringBytes()
if err != nil {
return err
}
*(*string)(unsafe.Pointer(uintptr(base) + p.offset)) = s
return nil
}
// Decode a slice of bytes ([]byte).
func (o *Buffer) dec_slice_byte(p *Properties, base unsafe.Pointer) error {
raw, err := o.DecodeRawBytes()
if err != nil {
return err
}
if !o.Immutable {
copied := make([]byte, len(raw))
copy(copied, raw)
raw = copied
}
*(*[]byte)(unsafe.Pointer(uintptr(base) + p.offset)) = raw
return nil
}
// Decode an array of bytes ([N]byte).
func (o *Buffer) dec_array_byte(p *Properties, base unsafe.Pointer) error {
raw, err := o.DecodeRawBytes()
if err != nil {
return err
}
n := p.length
// NOTE WELL we assume packed bytes are encoded in one block. Thus we restart the decoding
// at index 0 in the array. Should this not be the case then we ought to restart at an
// index saved in a map of array->index in Buffer. However for all use cases we have that
// is useless extra work. Should we want to decode such a field someday we can either do
// the work, or decode into a slice, which is always variable length.
s := ((*[maxLen]byte)(unsafe.Pointer(uintptr(base) + p.offset)))[0:n:n]
copy(s, raw)
return nil
}
// Decode a slice of bools ([]bool).
func (o *Buffer) dec_slice_packed_bool(p *Properties, base unsafe.Pointer) error {
v := (*[]bool)(unsafe.Pointer(uintptr(base) + p.offset))
nn, err := o.DecodeVarint()
if err != nil {
return err
}
nb := uint(nn) // number of bytes of encoded bools
fin := o.index + nb
if fin < o.index {
return errOverflow
}
y := *v
for o.index < fin {
u, err := p.valDec(o)
if err != nil {
return err
}
y = append(y, u != 0)
}
*v = y
return nil
}
// Decode an array of bools ([N]bool).
func (o *Buffer) dec_array_packed_bool(p *Properties, base unsafe.Pointer) error {
n := p.length
// NOTE WELL we assume packed integers are encoded in one block. Thus we restart the decoding
// at index 0 in the array. Should this not be the case then we ought to restart at an
// index saved in a map of array->index in Buffer. However for all use cases we have that
// is useless extra work. Should we want to decode such a field someday we can either do
// the work, or decode into a slice, which is always variable length.
s := ((*[maxLen]bool)(unsafe.Pointer(uintptr(base) + p.offset)))[0:0:n]
nn, err := o.DecodeVarint()
if err != nil {
return err
}
nb := uint(nn) // number of bytes of encoded bools
fin := o.index + nb
if fin < o.index {
return errOverflow
}
for o.index < fin {
u, err := p.valDec(o)
if err != nil {
return err
}
s = append(s, u != 0)
}
return nil
}
// Decode a slice of int8s ([]int8) in packed format.
func (o *Buffer) dec_slice_packed_int8(p *Properties, base unsafe.Pointer) error {
v := (*[]int8)(unsafe.Pointer(uintptr(base) + p.offset))
nn, err := o.DecodeVarint()
if err != nil {
return err
}
nb := uint(nn) // number of bytes of encoded int8s
fin := o.index + nb
if fin < o.index {
return errOverflow
}
y := *v
for o.index < fin {
u, err := p.valDec(o)
if err != nil {
return err
}
y = append(y, int8(u))
}
*v = y
return nil
}
// Decode an array of int8s ([N]int8).
func (o *Buffer) dec_array_packed_int8(p *Properties, base unsafe.Pointer) error {
n := p.length
// NOTE WELL we assume packed integers are encoded in one block. Thus we restart the decoding
// at index 0 in the array. Should this not be the case then we ought to restart at an
// index saved in a map of array->index in Buffer. However for all use cases we have that
// is useless extra work. Should we want to decode such a field someday we can either do
// the work, or decode into a slice, which is always variable length.
s := ((*[maxLen]int8)(unsafe.Pointer(uintptr(base) + p.offset)))[0:0:n]
nn, err := o.DecodeVarint()
if err != nil {
return err
}
nb := uint(nn) // number of bytes of encoded bools
fin := o.index + nb
if fin < o.index {
return errOverflow
}
for o.index < fin {
u, err := p.valDec(o)
if err != nil {
return err
}
if uint(len(s)) < n {
s = append(s, int8(u))
}
}
return nil
}
// Decode a slice of int16s ([]int16) in packed format.
func (o *Buffer) dec_slice_packed_int16(p *Properties, base unsafe.Pointer) error {
v := (*[]uint16)(unsafe.Pointer(uintptr(base) + p.offset))
nn, err := o.DecodeVarint()
if err != nil {
return err
}
nb := uint(nn) // number of bytes of encoded int16s
fin := o.index + nb
if fin < o.index {
return errOverflow
}
y := *v
for o.index < fin {
u, err := p.valDec(o)
if err != nil {
return err
}
y = append(y, uint16(u))
}
*v = y
return nil
}
// Decode an array of int16s ([N]int16).
func (o *Buffer) dec_array_packed_int16(p *Properties, base unsafe.Pointer) error {
n := p.length
// NOTE WELL we assume packed integers are encoded in one block. Thus we restart the decoding
// at index 0 in the array. Should this not be the case then we ought to restart at an
// index saved in a map of array->index in Buffer. However for all use cases we have that
// is useless extra work. Should we want to decode such a field someday we can either do
// the work, or decode into a slice, which is always variable length.
s := ((*[maxLen / 2]int16)(unsafe.Pointer(uintptr(base) + p.offset)))[0:0:n]
nn, err := o.DecodeVarint()
if err != nil {
return err
}
nb := uint(nn) // number of bytes of encoded bools
fin := o.index + nb
if fin < o.index {
return errOverflow
}
for o.index < fin {
u, err := p.valDec(o)
if err != nil {
return err
}
if uint(len(s)) < n {
s = append(s, int16(u))
}
}
return nil
}
// Decode a slice of int32s ([]int32) in packed format.
func (o *Buffer) dec_slice_packed_int32(p *Properties, base unsafe.Pointer) error {
v := (*[]uint32)(unsafe.Pointer(uintptr(base) + p.offset))
nn, err := o.DecodeVarint()
if err != nil {
return err
}
nb := uint(nn) // number of bytes of encoded int32s
fin := o.index + nb
if fin < o.index {
return errOverflow
}
y := *v
for o.index < fin {
u, err := p.valDec(o)
if err != nil {
return err
}
y = append(y, uint32(u))
}
*v = y
return nil
}
// Decode an array of int32s ([N]int32).