-
Notifications
You must be signed in to change notification settings - Fork 15.3k
/
ArrayDecoders.java
1102 lines (1049 loc) · 40.7 KB
/
ArrayDecoders.java
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
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// 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 com.google.protobuf;
import static com.google.protobuf.MessageSchema.getMutableUnknownFields;
import com.google.protobuf.Internal.ProtobufList;
import java.io.IOException;
/**
* Helper functions to decode protobuf wire format from a byte array.
*
* <p>Note that these functions don't do boundary check on the byte array but instead rely on Java
* VM to check it. That means parsing routines utilizing these functions must catch
* IndexOutOfBoundsException and convert it to protobuf's InvalidProtocolBufferException when
* crossing protobuf public API boundaries.
*/
final class ArrayDecoders {
/**
* A helper used to return multiple values in a Java function. Java doesn't natively support
* returning multiple values in a function. Creating a new Object to hold the return values will
* be too expensive. Instead, we pass a Registers instance to functions that want to return
* multiple values and let the function set the return value in this Registers instance instead.
*
* <p>TODO(xiaofeng): This could be merged into CodedInputStream or CodedInputStreamReader which
* is already being passed through all the parsing routines.
*/
static final class Registers {
public int int1;
public long long1;
public Object object1;
public final ExtensionRegistryLite extensionRegistry;
Registers() {
this.extensionRegistry = ExtensionRegistryLite.getEmptyRegistry();
}
Registers(ExtensionRegistryLite extensionRegistry) {
if (extensionRegistry == null) {
throw new NullPointerException();
}
this.extensionRegistry = extensionRegistry;
}
}
/**
* Decodes a varint. Returns the position after the varint. The decoded varint is stored in
* registers.int1.
*/
static int decodeVarint32(byte[] data, int position, Registers registers) {
int value = data[position++];
if (value >= 0) {
registers.int1 = value;
return position;
}
return decodeVarint32(value, data, position, registers);
}
/** Like decodeVarint32 except that the first byte is already read. */
static int decodeVarint32(int firstByte, byte[] data, int position, Registers registers) {
int value = firstByte & 0x7F;
final byte b2 = data[position++];
if (b2 >= 0) {
registers.int1 = value | ((int) b2 << 7);
return position;
}
value |= (b2 & 0x7F) << 7;
final byte b3 = data[position++];
if (b3 >= 0) {
registers.int1 = value | ((int) b3 << 14);
return position;
}
value |= (b3 & 0x7F) << 14;
final byte b4 = data[position++];
if (b4 >= 0) {
registers.int1 = value | ((int) b4 << 21);
return position;
}
value |= (b4 & 0x7F) << 21;
final byte b5 = data[position++];
if (b5 >= 0) {
registers.int1 = value | ((int) b5 << 28);
return position;
}
value |= (b5 & 0x7F) << 28;
while (data[position++] < 0) {}
registers.int1 = value;
return position;
}
/**
* Decodes a varint. Returns the position after the varint. The decoded varint is stored in
* registers.long1.
*/
static int decodeVarint64(byte[] data, int position, Registers registers) {
long value = data[position++];
if (value >= 0) {
registers.long1 = value;
return position;
} else {
return decodeVarint64(value, data, position, registers);
}
}
/** Like decodeVarint64 except that the first byte is already read. */
static int decodeVarint64(long firstByte, byte[] data, int position, Registers registers) {
long value = firstByte & 0x7F;
byte next = data[position++];
int shift = 7;
value |= (long) (next & 0x7F) << 7;
while (next < 0) {
next = data[position++];
shift += 7;
value |= (long) (next & 0x7F) << shift;
}
registers.long1 = value;
return position;
}
/** Decodes and returns a fixed32 value. */
static int decodeFixed32(byte[] data, int position) {
return (data[position] & 0xff)
| ((data[position + 1] & 0xff) << 8)
| ((data[position + 2] & 0xff) << 16)
| ((data[position + 3] & 0xff) << 24);
}
/** Decodes and returns a fixed64 value. */
static long decodeFixed64(byte[] data, int position) {
return (data[position] & 0xffL)
| ((data[position + 1] & 0xffL) << 8)
| ((data[position + 2] & 0xffL) << 16)
| ((data[position + 3] & 0xffL) << 24)
| ((data[position + 4] & 0xffL) << 32)
| ((data[position + 5] & 0xffL) << 40)
| ((data[position + 6] & 0xffL) << 48)
| ((data[position + 7] & 0xffL) << 56);
}
/** Decodes and returns a double value. */
static double decodeDouble(byte[] data, int position) {
return Double.longBitsToDouble(decodeFixed64(data, position));
}
/** Decodes and returns a float value. */
static float decodeFloat(byte[] data, int position) {
return Float.intBitsToFloat(decodeFixed32(data, position));
}
/** Decodes a string value. */
static int decodeString(byte[] data, int position, Registers registers)
throws InvalidProtocolBufferException {
position = decodeVarint32(data, position, registers);
final int length = registers.int1;
if (length < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (length == 0) {
registers.object1 = "";
return position;
} else {
registers.object1 = new String(data, position, length, Internal.UTF_8);
return position + length;
}
}
/** Decodes a string value with utf8 check. */
static int decodeStringRequireUtf8(byte[] data, int position, Registers registers)
throws InvalidProtocolBufferException {
position = decodeVarint32(data, position, registers);
final int length = registers.int1;
if (length < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (length == 0) {
registers.object1 = "";
return position;
} else {
registers.object1 = Utf8.decodeUtf8(data, position, length);
return position + length;
}
}
/** Decodes a bytes value. */
static int decodeBytes(byte[] data, int position, Registers registers)
throws InvalidProtocolBufferException {
position = decodeVarint32(data, position, registers);
final int length = registers.int1;
if (length < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (length > data.length - position) {
throw InvalidProtocolBufferException.truncatedMessage();
} else if (length == 0) {
registers.object1 = ByteString.EMPTY;
return position;
} else {
registers.object1 = ByteString.copyFrom(data, position, length);
return position + length;
}
}
/** Decodes a message value. */
@SuppressWarnings({"unchecked", "rawtypes"})
static int decodeMessageField(
Schema schema, byte[] data, int position, int limit, Registers registers) throws IOException {
Object msg = schema.newInstance();
int offset = mergeMessageField(msg, schema, data, position, limit, registers);
schema.makeImmutable(msg);
registers.object1 = msg;
return offset;
}
/** Decodes a group value. */
@SuppressWarnings({"unchecked", "rawtypes"})
static int decodeGroupField(
Schema schema, byte[] data, int position, int limit, int endGroup, Registers registers)
throws IOException {
Object msg = schema.newInstance();
int offset = mergeGroupField(msg, schema, data, position, limit, endGroup, registers);
schema.makeImmutable(msg);
registers.object1 = msg;
return offset;
}
@SuppressWarnings({"unchecked", "rawtypes"})
static int mergeMessageField(
Object msg, Schema schema, byte[] data, int position, int limit, Registers registers)
throws IOException {
int length = data[position++];
if (length < 0) {
position = decodeVarint32(length, data, position, registers);
length = registers.int1;
}
if (length < 0 || length > limit - position) {
throw InvalidProtocolBufferException.truncatedMessage();
}
schema.mergeFrom(msg, data, position, position + length, registers);
registers.object1 = msg;
return position + length;
}
@SuppressWarnings({"unchecked", "rawtypes"})
static int mergeGroupField(
Object msg,
Schema schema,
byte[] data,
int position,
int limit,
int endGroup,
Registers registers)
throws IOException {
// A group field must has a MessageSchema (the only other subclass of Schema is MessageSetSchema
// and it can't be used in group fields).
final MessageSchema messageSchema = (MessageSchema) schema;
// It's OK to directly use parseProto2Message since proto3 doesn't have group.
final int endPosition =
messageSchema.parseProto2Message(msg, data, position, limit, endGroup, registers);
registers.object1 = msg;
return endPosition;
}
/** Decodes a repeated 32-bit varint field. Returns the position after all read values. */
static int decodeVarint32List(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final IntArrayList output = (IntArrayList) list;
position = decodeVarint32(data, position, registers);
output.addInt(registers.int1);
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint32(data, nextPosition, registers);
output.addInt(registers.int1);
}
return position;
}
/** Decodes a repeated 64-bit varint field. Returns the position after all read values. */
static int decodeVarint64List(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final LongArrayList output = (LongArrayList) list;
position = decodeVarint64(data, position, registers);
output.addLong(registers.long1);
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint64(data, nextPosition, registers);
output.addLong(registers.long1);
}
return position;
}
/** Decodes a repeated fixed32 field. Returns the position after all read values. */
static int decodeFixed32List(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final IntArrayList output = (IntArrayList) list;
output.addInt(decodeFixed32(data, position));
position += 4;
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
output.addInt(decodeFixed32(data, nextPosition));
position = nextPosition + 4;
}
return position;
}
/** Decodes a repeated fixed64 field. Returns the position after all read values. */
static int decodeFixed64List(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final LongArrayList output = (LongArrayList) list;
output.addLong(decodeFixed64(data, position));
position += 8;
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
output.addLong(decodeFixed64(data, nextPosition));
position = nextPosition + 8;
}
return position;
}
/** Decodes a repeated float field. Returns the position after all read values. */
static int decodeFloatList(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final FloatArrayList output = (FloatArrayList) list;
output.addFloat(decodeFloat(data, position));
position += 4;
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
output.addFloat(decodeFloat(data, nextPosition));
position = nextPosition + 4;
}
return position;
}
/** Decodes a repeated double field. Returns the position after all read values. */
static int decodeDoubleList(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final DoubleArrayList output = (DoubleArrayList) list;
output.addDouble(decodeDouble(data, position));
position += 8;
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
output.addDouble(decodeDouble(data, nextPosition));
position = nextPosition + 8;
}
return position;
}
/** Decodes a repeated boolean field. Returns the position after all read values. */
static int decodeBoolList(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final BooleanArrayList output = (BooleanArrayList) list;
position = decodeVarint64(data, position, registers);
output.addBoolean(registers.long1 != 0);
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint64(data, nextPosition, registers);
output.addBoolean(registers.long1 != 0);
}
return position;
}
/** Decodes a repeated sint32 field. Returns the position after all read values. */
static int decodeSInt32List(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final IntArrayList output = (IntArrayList) list;
position = decodeVarint32(data, position, registers);
output.addInt(CodedInputStream.decodeZigZag32(registers.int1));
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint32(data, nextPosition, registers);
output.addInt(CodedInputStream.decodeZigZag32(registers.int1));
}
return position;
}
/** Decodes a repeated sint64 field. Returns the position after all read values. */
static int decodeSInt64List(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers) {
final LongArrayList output = (LongArrayList) list;
position = decodeVarint64(data, position, registers);
output.addLong(CodedInputStream.decodeZigZag64(registers.long1));
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint64(data, nextPosition, registers);
output.addLong(CodedInputStream.decodeZigZag64(registers.long1));
}
return position;
}
/** Decodes a packed 32-bit varint field. Returns the position after all read values. */
static int decodePackedVarint32List(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final IntArrayList output = (IntArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
position = decodeVarint32(data, position, registers);
output.addInt(registers.int1);
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed 64-bit varint field. Returns the position after all read values. */
static int decodePackedVarint64List(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final LongArrayList output = (LongArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
position = decodeVarint64(data, position, registers);
output.addLong(registers.long1);
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed fixed32 field. Returns the position after all read values. */
static int decodePackedFixed32List(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final IntArrayList output = (IntArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
output.addInt(decodeFixed32(data, position));
position += 4;
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed fixed64 field. Returns the position after all read values. */
static int decodePackedFixed64List(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final LongArrayList output = (LongArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
output.addLong(decodeFixed64(data, position));
position += 8;
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed float field. Returns the position after all read values. */
static int decodePackedFloatList(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final FloatArrayList output = (FloatArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
output.addFloat(decodeFloat(data, position));
position += 4;
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed double field. Returns the position after all read values. */
static int decodePackedDoubleList(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final DoubleArrayList output = (DoubleArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
output.addDouble(decodeDouble(data, position));
position += 8;
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed boolean field. Returns the position after all read values. */
static int decodePackedBoolList(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final BooleanArrayList output = (BooleanArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
position = decodeVarint64(data, position, registers);
output.addBoolean(registers.long1 != 0);
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed sint32 field. Returns the position after all read values. */
static int decodePackedSInt32List(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final IntArrayList output = (IntArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
position = decodeVarint32(data, position, registers);
output.addInt(CodedInputStream.decodeZigZag32(registers.int1));
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a packed sint64 field. Returns the position after all read values. */
@SuppressWarnings("unchecked")
static int decodePackedSInt64List(
byte[] data, int position, ProtobufList<?> list, Registers registers) throws IOException {
final LongArrayList output = (LongArrayList) list;
position = decodeVarint32(data, position, registers);
final int fieldLimit = position + registers.int1;
while (position < fieldLimit) {
position = decodeVarint64(data, position, registers);
output.addLong(CodedInputStream.decodeZigZag64(registers.long1));
}
if (position != fieldLimit) {
throw InvalidProtocolBufferException.truncatedMessage();
}
return position;
}
/** Decodes a repeated string field. Returns the position after all read values. */
@SuppressWarnings("unchecked")
static int decodeStringList(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers)
throws InvalidProtocolBufferException {
final ProtobufList<String> output = (ProtobufList<String>) list;
position = decodeVarint32(data, position, registers);
final int length = registers.int1;
if (length < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (length == 0) {
output.add("");
} else {
String value = new String(data, position, length, Internal.UTF_8);
output.add(value);
position += length;
}
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint32(data, nextPosition, registers);
final int nextLength = registers.int1;
if (nextLength < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (nextLength == 0) {
output.add("");
} else {
String value = new String(data, position, nextLength, Internal.UTF_8);
output.add(value);
position += nextLength;
}
}
return position;
}
/**
* Decodes a repeated string field with utf8 check. Returns the position after all read values.
*/
@SuppressWarnings("unchecked")
static int decodeStringListRequireUtf8(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers)
throws InvalidProtocolBufferException {
final ProtobufList<String> output = (ProtobufList<String>) list;
position = decodeVarint32(data, position, registers);
final int length = registers.int1;
if (length < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (length == 0) {
output.add("");
} else {
if (!Utf8.isValidUtf8(data, position, position + length)) {
throw InvalidProtocolBufferException.invalidUtf8();
}
String value = new String(data, position, length, Internal.UTF_8);
output.add(value);
position += length;
}
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint32(data, nextPosition, registers);
final int nextLength = registers.int1;
if (nextLength < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (nextLength == 0) {
output.add("");
} else {
if (!Utf8.isValidUtf8(data, position, position + nextLength)) {
throw InvalidProtocolBufferException.invalidUtf8();
}
String value = new String(data, position, nextLength, Internal.UTF_8);
output.add(value);
position += nextLength;
}
}
return position;
}
/** Decodes a repeated bytes field. Returns the position after all read values. */
@SuppressWarnings("unchecked")
static int decodeBytesList(
int tag, byte[] data, int position, int limit, ProtobufList<?> list, Registers registers)
throws InvalidProtocolBufferException {
final ProtobufList<ByteString> output = (ProtobufList<ByteString>) list;
position = decodeVarint32(data, position, registers);
final int length = registers.int1;
if (length < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (length > data.length - position) {
throw InvalidProtocolBufferException.truncatedMessage();
} else if (length == 0) {
output.add(ByteString.EMPTY);
} else {
output.add(ByteString.copyFrom(data, position, length));
position += length;
}
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeVarint32(data, nextPosition, registers);
final int nextLength = registers.int1;
if (nextLength < 0) {
throw InvalidProtocolBufferException.negativeSize();
} else if (nextLength > data.length - position) {
throw InvalidProtocolBufferException.truncatedMessage();
} else if (nextLength == 0) {
output.add(ByteString.EMPTY);
} else {
output.add(ByteString.copyFrom(data, position, nextLength));
position += nextLength;
}
}
return position;
}
/**
* Decodes a repeated message field
*
* @return The position of after read all messages
*/
@SuppressWarnings({"unchecked"})
static int decodeMessageList(
Schema<?> schema,
int tag,
byte[] data,
int position,
int limit,
ProtobufList<?> list,
Registers registers)
throws IOException {
final ProtobufList<Object> output = (ProtobufList<Object>) list;
position = decodeMessageField(schema, data, position, limit, registers);
output.add(registers.object1);
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeMessageField(schema, data, nextPosition, limit, registers);
output.add(registers.object1);
}
return position;
}
/**
* Decodes a repeated group field
*
* @return The position of after read all groups
*/
@SuppressWarnings({"unchecked", "rawtypes"})
static int decodeGroupList(
Schema schema,
int tag,
byte[] data,
int position,
int limit,
ProtobufList<?> list,
Registers registers)
throws IOException {
final ProtobufList<Object> output = (ProtobufList<Object>) list;
final int endgroup = (tag & ~0x7) | WireFormat.WIRETYPE_END_GROUP;
position = decodeGroupField(schema, data, position, limit, endgroup, registers);
output.add(registers.object1);
while (position < limit) {
int nextPosition = decodeVarint32(data, position, registers);
if (tag != registers.int1) {
break;
}
position = decodeGroupField(schema, data, nextPosition, limit, endgroup, registers);
output.add(registers.object1);
}
return position;
}
static int decodeExtensionOrUnknownField(
int tag, byte[] data, int position, int limit,
Object message,
MessageLite defaultInstance,
UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite> unknownFieldSchema,
Registers registers)
throws IOException {
final int number = tag >>> 3;
GeneratedMessageLite.GeneratedExtension extension =
registers.extensionRegistry.findLiteExtensionByNumber(defaultInstance, number);
if (extension == null) {
return decodeUnknownField(
tag, data, position, limit, getMutableUnknownFields(message), registers);
} else {
((GeneratedMessageLite.ExtendableMessage<?, ?>) message).ensureExtensionsAreMutable();
return decodeExtension(
tag, data, position, limit, (GeneratedMessageLite.ExtendableMessage) message,
extension, unknownFieldSchema, registers);
}
}
static int decodeExtension(
int tag,
byte[] data,
int position,
int limit,
GeneratedMessageLite.ExtendableMessage<?, ?> message,
GeneratedMessageLite.GeneratedExtension<?, ?> extension,
UnknownFieldSchema<UnknownFieldSetLite, UnknownFieldSetLite> unknownFieldSchema,
Registers registers)
throws IOException {
final FieldSet<GeneratedMessageLite.ExtensionDescriptor> extensions = message.extensions;
final int fieldNumber = tag >>> 3;
if (extension.descriptor.isRepeated() && extension.descriptor.isPacked()) {
switch (extension.getLiteType()) {
case DOUBLE:
{
DoubleArrayList list = new DoubleArrayList();
position = decodePackedDoubleList(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case FLOAT:
{
FloatArrayList list = new FloatArrayList();
position = decodePackedFloatList(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case INT64:
case UINT64:
{
LongArrayList list = new LongArrayList();
position = decodePackedVarint64List(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case INT32:
case UINT32:
{
IntArrayList list = new IntArrayList();
position = decodePackedVarint32List(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case FIXED64:
case SFIXED64:
{
LongArrayList list = new LongArrayList();
position = decodePackedFixed64List(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case FIXED32:
case SFIXED32:
{
IntArrayList list = new IntArrayList();
position = decodePackedFixed32List(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case BOOL:
{
BooleanArrayList list = new BooleanArrayList();
position = decodePackedBoolList(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case SINT32:
{
IntArrayList list = new IntArrayList();
position = decodePackedSInt32List(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case SINT64:
{
LongArrayList list = new LongArrayList();
position = decodePackedSInt64List(data, position, list, registers);
extensions.setField(extension.descriptor, list);
break;
}
case ENUM:
{
IntArrayList list = new IntArrayList();
position = decodePackedVarint32List(data, position, list, registers);
SchemaUtil.filterUnknownEnumList(
message,
fieldNumber,
list,
extension.descriptor.getEnumType(),
null,
unknownFieldSchema);
extensions.setField(extension.descriptor, list);
break;
}
default:
throw new IllegalStateException(
"Type cannot be packed: " + extension.descriptor.getLiteType());
}
} else {
Object value = null;
// Enum is a special case because unknown enum values will be put into UnknownFieldSetLite.
if (extension.getLiteType() == WireFormat.FieldType.ENUM) {
position = decodeVarint32(data, position, registers);
Object enumValue = extension.descriptor.getEnumType().findValueByNumber(registers.int1);
if (enumValue == null) {
SchemaUtil.storeUnknownEnum(
message, fieldNumber, registers.int1, null, unknownFieldSchema);
return position;
}
// Note, we store the integer value instead of the actual enum object in FieldSet.
// This is also different from full-runtime where we store EnumValueDescriptor.
value = registers.int1;
} else {
switch (extension.getLiteType()) {
case DOUBLE:
value = decodeDouble(data, position);
position += 8;
break;
case FLOAT:
value = decodeFloat(data, position);
position += 4;
break;
case INT64:
case UINT64:
position = decodeVarint64(data, position, registers);
value = registers.long1;
break;
case INT32:
case UINT32:
position = decodeVarint32(data, position, registers);
value = registers.int1;
break;
case FIXED64:
case SFIXED64:
value = decodeFixed64(data, position);
position += 8;
break;
case FIXED32:
case SFIXED32:
value = decodeFixed32(data, position);
position += 4;
break;
case BOOL:
position = decodeVarint64(data, position, registers);
value = (registers.long1 != 0);
break;
case BYTES:
position = decodeBytes(data, position, registers);
value = registers.object1;
break;
case SINT32:
position = decodeVarint32(data, position, registers);
value = CodedInputStream.decodeZigZag32(registers.int1);
break;
case SINT64:
position = decodeVarint64(data, position, registers);
value = CodedInputStream.decodeZigZag64(registers.long1);
break;
case STRING:
position = decodeString(data, position, registers);
value = registers.object1;
break;
case GROUP:
{
final int endTag = (fieldNumber << 3) | WireFormat.WIRETYPE_END_GROUP;
final Schema fieldSchema =
Protobuf.getInstance()
.schemaFor(extension.getMessageDefaultInstance().getClass());
if (extension.isRepeated()) {
position = decodeGroupField(fieldSchema, data, position, limit, endTag, registers);
extensions.addRepeatedField(extension.descriptor, registers.object1);
} else {
Object oldValue = extensions.getField(extension.descriptor);
if (oldValue == null) {
oldValue = fieldSchema.newInstance();
extensions.setField(extension.descriptor, oldValue);
}
position =
mergeGroupField(
oldValue, fieldSchema, data, position, limit, endTag, registers);
}
return position;
}
case MESSAGE:
{
final Schema fieldSchema =
Protobuf.getInstance()
.schemaFor(extension.getMessageDefaultInstance().getClass());
if (extension.isRepeated()) {
position = decodeMessageField(fieldSchema, data, position, limit, registers);
extensions.addRepeatedField(extension.descriptor, registers.object1);
} else {
Object oldValue = extensions.getField(extension.descriptor);
if (oldValue == null) {
oldValue = fieldSchema.newInstance();
extensions.setField(extension.descriptor, oldValue);
}
position =
mergeMessageField(oldValue, fieldSchema, data, position, limit, registers);
}
return position;
}
case ENUM:
throw new IllegalStateException("Shouldn't reach here.");
}
}
if (extension.isRepeated()) {
extensions.addRepeatedField(extension.descriptor, value);