-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathProtoBufRuntime.sol
3082 lines (2774 loc) · 70.9 KB
/
ProtoBufRuntime.sol
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
/**
* @title Runtime library for ProtoBuf serialization and/or deserialization.
* All ProtoBuf generated code will use this library.
*/
library ProtoBufRuntime {
// Types defined in ProtoBuf
enum WireType { Varint, Fixed64, LengthDelim, StartGroup, EndGroup, Fixed32 }
// Constants for bytes calculation
uint256 constant WORD_LENGTH = 32;
uint256 constant HEADER_SIZE_LENGTH_IN_BYTES = 4;
uint256 constant BYTE_SIZE = 8;
uint256 constant REMAINING_LENGTH = WORD_LENGTH - HEADER_SIZE_LENGTH_IN_BYTES;
string constant OVERFLOW_MESSAGE = "length overflow";
//Storages
/**
* @dev Encode to storage location using assembly to save storage space.
* @param location The location of storage
* @param encoded The encoded ProtoBuf bytes
*/
function encodeStorage(bytes storage location, bytes memory encoded)
internal
{
/**
* This code use the first four bytes as size,
* and then put the rest of `encoded` bytes.
*/
uint256 length = encoded.length;
uint256 firstWord;
uint256 wordLength = WORD_LENGTH;
uint256 remainingLength = REMAINING_LENGTH;
assembly {
firstWord := mload(add(encoded, wordLength))
}
firstWord =
(firstWord >> (BYTE_SIZE * HEADER_SIZE_LENGTH_IN_BYTES)) |
(length << (BYTE_SIZE * REMAINING_LENGTH));
assembly {
sstore(location.slot, firstWord)
}
if (length > REMAINING_LENGTH) {
length -= REMAINING_LENGTH;
for (uint256 i = 0; i < ceil(length, WORD_LENGTH); i++) {
assembly {
let offset := add(mul(i, wordLength), remainingLength)
let slotIndex := add(i, 1)
sstore(
add(location.slot, slotIndex),
mload(add(add(encoded, wordLength), offset))
)
}
}
}
}
/**
* @dev Decode storage location using assembly using the format in `encodeStorage`.
* @param location The location of storage
* @return The encoded bytes
*/
function decodeStorage(bytes storage location)
internal
view
returns (bytes memory)
{
/**
* This code is to decode the first four bytes as size,
* and then decode the rest using the decoded size.
*/
uint256 firstWord;
uint256 remainingLength = REMAINING_LENGTH;
uint256 wordLength = WORD_LENGTH;
assembly {
firstWord := sload(location.slot)
}
uint256 length = firstWord >> (BYTE_SIZE * REMAINING_LENGTH);
bytes memory encoded = new bytes(length);
assembly {
mstore(add(encoded, remainingLength), firstWord)
}
if (length > REMAINING_LENGTH) {
length -= REMAINING_LENGTH;
for (uint256 i = 0; i < ceil(length, WORD_LENGTH); i++) {
assembly {
let offset := add(mul(i, wordLength), remainingLength)
let slotIndex := add(i, 1)
mstore(
add(add(encoded, wordLength), offset),
sload(add(location.slot, slotIndex))
)
}
}
}
return encoded;
}
/**
* @dev Fast memory copy of bytes using assembly.
* @param src The source memory address
* @param dest The destination memory address
* @param len The length of bytes to copy
*/
function copyBytes(uint256 src, uint256 dest, uint256 len) internal pure {
if (len == 0) {
return;
}
// Copy word-length chunks while possible
for (; len > WORD_LENGTH; len -= WORD_LENGTH) {
assembly {
mstore(dest, mload(src))
}
dest += WORD_LENGTH;
src += WORD_LENGTH;
}
// Copy remaining bytes
uint256 mask = 256**(WORD_LENGTH - len) - 1;
assembly {
let srcpart := and(mload(src), not(mask))
let destpart := and(mload(dest), mask)
mstore(dest, or(destpart, srcpart))
}
}
/**
* @dev Use assembly to get memory address.
* @param r The in-memory bytes array
* @return The memory address of `r`
*/
function getMemoryAddress(bytes memory r) internal pure returns (uint256) {
uint256 addr;
assembly {
addr := r
}
return addr;
}
/**
* @dev Implement Math function of ceil
* @param a The denominator
* @param m The numerator
* @return r The result of ceil(a/m)
*/
function ceil(uint256 a, uint256 m) internal pure returns (uint256 r) {
return (a + m - 1) / m;
}
// Decoders
/**
* This section of code `_decode_(u)int(32|64)`, `_decode_enum` and `_decode_bool`
* is to decode ProtoBuf native integers,
* using the `varint` encoding.
*/
/**
* @dev Decode integers
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded integer
* @return The length of `bs` used to get decoded
*/
function _decode_uint32(uint256 p, bytes memory bs)
internal
pure
returns (uint32, uint256)
{
(uint256 varint, uint256 sz) = _decode_varint(p, bs);
return (uint32(varint), sz);
}
/**
* @dev Decode integers
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded integer
* @return The length of `bs` used to get decoded
*/
function _decode_uint64(uint256 p, bytes memory bs)
internal
pure
returns (uint64, uint256)
{
(uint256 varint, uint256 sz) = _decode_varint(p, bs);
return (uint64(varint), sz);
}
/**
* @dev Decode integers
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded integer
* @return The length of `bs` used to get decoded
*/
function _decode_int32(uint256 p, bytes memory bs)
internal
pure
returns (int32, uint256)
{
(uint256 varint, uint256 sz) = _decode_varint(p, bs);
int32 r;
assembly {
r := varint
}
return (r, sz);
}
/**
* @dev Decode integers
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded integer
* @return The length of `bs` used to get decoded
*/
function _decode_int64(uint256 p, bytes memory bs)
internal
pure
returns (int64, uint256)
{
(uint256 varint, uint256 sz) = _decode_varint(p, bs);
int64 r;
assembly {
r := varint
}
return (r, sz);
}
/**
* @dev Decode enum
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded enum's integer
* @return The length of `bs` used to get decoded
*/
function _decode_enum(uint256 p, bytes memory bs)
internal
pure
returns (int64, uint256)
{
return _decode_int64(p, bs);
}
/**
* @dev Decode enum
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded boolean
* @return The length of `bs` used to get decoded
*/
function _decode_bool(uint256 p, bytes memory bs)
internal
pure
returns (bool, uint256)
{
(uint256 varint, uint256 sz) = _decode_varint(p, bs);
if (varint == 0) {
return (false, sz);
}
return (true, sz);
}
/**
* This section of code `_decode_sint(32|64)`
* is to decode ProtoBuf native signed integers,
* using the `zig-zag` encoding.
*/
/**
* @dev Decode signed integers
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded integer
* @return The length of `bs` used to get decoded
*/
function _decode_sint32(uint256 p, bytes memory bs)
internal
pure
returns (int32, uint256)
{
(int256 varint, uint256 sz) = _decode_varints(p, bs);
return (int32(varint), sz);
}
/**
* @dev Decode signed integers
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded integer
* @return The length of `bs` used to get decoded
*/
function _decode_sint64(uint256 p, bytes memory bs)
internal
pure
returns (int64, uint256)
{
(int256 varint, uint256 sz) = _decode_varints(p, bs);
return (int64(varint), sz);
}
/**
* @dev Decode string
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded string
* @return The length of `bs` used to get decoded
*/
function _decode_string(uint256 p, bytes memory bs)
internal
pure
returns (string memory, uint256)
{
(bytes memory x, uint256 sz) = _decode_lendelim(p, bs);
return (string(x), sz);
}
/**
* @dev Decode bytes array
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded bytes array
* @return The length of `bs` used to get decoded
*/
function _decode_bytes(uint256 p, bytes memory bs)
internal
pure
returns (bytes memory, uint256)
{
return _decode_lendelim(p, bs);
}
/**
* @dev Decode ProtoBuf key
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded field ID
* @return The decoded WireType specified in ProtoBuf
* @return The length of `bs` used to get decoded
*/
function _decode_key(uint256 p, bytes memory bs)
internal
pure
returns (uint256, WireType, uint256)
{
(uint256 x, uint256 n) = _decode_varint(p, bs);
WireType typeId = WireType(x & 7);
uint256 fieldId = x / 8;
return (fieldId, typeId, n);
}
/**
* @dev Decode ProtoBuf varint
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded unsigned integer
* @return The length of `bs` used to get decoded
*/
function _decode_varint(uint256 p, bytes memory bs)
internal
pure
returns (uint256, uint256)
{
/**
* Read a byte.
* Use the lower 7 bits and shift it to the left,
* until the most significant bit is 0.
* Refer to https://developers.google.com/protocol-buffers/docs/encoding
*/
uint256 x = 0;
uint256 sz = 0;
uint256 length = bs.length + WORD_LENGTH;
assembly {
let b := 0x80
p := add(bs, p)
for {
} eq(0x80, and(b, 0x80)) {
} {
if eq(lt(sub(p, bs), length), 0) {
mstore(
0,
0x08c379a000000000000000000000000000000000000000000000000000000000
) //error function selector
mstore(4, 32)
mstore(36, 15)
mstore(
68,
0x6c656e677468206f766572666c6f770000000000000000000000000000000000
) // length overflow in hex
revert(0, 83)
}
let tmp := mload(p)
let pos := 0
for {
} and(eq(0x80, and(b, 0x80)), lt(pos, 32)) {
} {
if eq(lt(sub(p, bs), length), 0) {
mstore(
0,
0x08c379a000000000000000000000000000000000000000000000000000000000
) //error function selector
mstore(4, 32)
mstore(36, 15)
mstore(
68,
0x6c656e677468206f766572666c6f770000000000000000000000000000000000
) // length overflow in hex
revert(0, 83)
}
b := byte(pos, tmp)
x := or(x, shl(mul(7, sz), and(0x7f, b)))
sz := add(sz, 1)
pos := add(pos, 1)
p := add(p, 0x01)
}
}
}
return (x, sz);
}
/**
* @dev Decode ProtoBuf zig-zag encoding
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded signed integer
* @return The length of `bs` used to get decoded
*/
function _decode_varints(uint256 p, bytes memory bs)
internal
pure
returns (int256, uint256)
{
/**
* Refer to https://developers.google.com/protocol-buffers/docs/encoding
*/
(uint256 u, uint256 sz) = _decode_varint(p, bs);
int256 s;
assembly {
s := xor(shr(1, u), add(not(and(u, 1)), 1))
}
return (s, sz);
}
/**
* @dev Decode ProtoBuf fixed-length encoding
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded unsigned integer
* @return The length of `bs` used to get decoded
*/
function _decode_uintf(uint256 p, bytes memory bs, uint256 sz)
internal
pure
returns (uint256, uint256)
{
/**
* Refer to https://developers.google.com/protocol-buffers/docs/encoding
*/
uint256 x = 0;
uint256 length = bs.length + WORD_LENGTH;
assert(p + sz <= length);
assembly {
let i := 0
p := add(bs, p)
let tmp := mload(p)
for {
} lt(i, sz) {
} {
x := or(x, shl(mul(8, i), byte(i, tmp)))
p := add(p, 0x01)
i := add(i, 1)
}
}
return (x, sz);
}
/**
* `_decode_(s)fixed(32|64)` is the concrete implementation of `_decode_uintf`
*/
function _decode_fixed32(uint256 p, bytes memory bs)
internal
pure
returns (uint32, uint256)
{
(uint256 x, uint256 sz) = _decode_uintf(p, bs, 4);
return (uint32(x), sz);
}
function _decode_fixed64(uint256 p, bytes memory bs)
internal
pure
returns (uint64, uint256)
{
(uint256 x, uint256 sz) = _decode_uintf(p, bs, 8);
return (uint64(x), sz);
}
function _decode_sfixed32(uint256 p, bytes memory bs)
internal
pure
returns (int32, uint256)
{
(uint256 x, uint256 sz) = _decode_uintf(p, bs, 4);
int256 r;
assembly {
r := x
}
return (int32(r), sz);
}
function _decode_sfixed64(uint256 p, bytes memory bs)
internal
pure
returns (int64, uint256)
{
(uint256 x, uint256 sz) = _decode_uintf(p, bs, 8);
int256 r;
assembly {
r := x
}
return (int64(r), sz);
}
/**
* @dev Decode bytes array
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The decoded bytes array
* @return The length of `bs` used to get decoded
*/
function _decode_lendelim(uint256 p, bytes memory bs)
internal
pure
returns (bytes memory, uint256)
{
/**
* First read the size encoded in `varint`, then use the size to read bytes.
*/
(uint256 len, uint256 sz) = _decode_varint(p, bs);
bytes memory b = new bytes(len);
uint256 length = bs.length + WORD_LENGTH;
assert(p + sz + len <= length);
uint256 sourcePtr;
uint256 destPtr;
assembly {
destPtr := add(b, 32)
sourcePtr := add(add(bs, p), sz)
}
copyBytes(sourcePtr, destPtr, len);
return (b, sz + len);
}
/**
* @dev Skip the decoding of a single field
* @param wt The WireType of the field
* @param p The memory offset of `bs`
* @param bs The bytes array to be decoded
* @return The length of `bs` to skipped
*/
function _skip_field_decode(WireType wt, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
if (wt == ProtoBufRuntime.WireType.Fixed64) {
return 8;
} else if (wt == ProtoBufRuntime.WireType.Fixed32) {
return 4;
} else if (wt == ProtoBufRuntime.WireType.Varint) {
(, uint256 size) = ProtoBufRuntime._decode_varint(p, bs);
return size;
} else {
require(wt == ProtoBufRuntime.WireType.LengthDelim);
(uint256 len, uint256 size) = ProtoBufRuntime._decode_varint(p, bs);
return size + len;
}
}
// Encoders
/**
* @dev Encode ProtoBuf key
* @param x The field ID
* @param wt The WireType specified in ProtoBuf
* @param p The offset of bytes array `bs`
* @param bs The bytes array to encode
* @return The length of encoded bytes
*/
function _encode_key(uint256 x, WireType wt, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
uint256 i;
assembly {
i := or(mul(x, 8), mod(wt, 8))
}
return _encode_varint(i, p, bs);
}
/**
* @dev Encode ProtoBuf varint
* @param x The unsigned integer to be encoded
* @param p The offset of bytes array `bs`
* @param bs The bytes array to encode
* @return The length of encoded bytes
*/
function _encode_varint(uint256 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
/**
* Refer to https://developers.google.com/protocol-buffers/docs/encoding
*/
uint256 sz = 0;
assembly {
let bsptr := add(bs, p)
let byt := and(x, 0x7f)
for {
} gt(shr(7, x), 0) {
} {
mstore8(bsptr, or(0x80, byt))
bsptr := add(bsptr, 1)
sz := add(sz, 1)
x := shr(7, x)
byt := and(x, 0x7f)
}
mstore8(bsptr, byt)
sz := add(sz, 1)
}
return sz;
}
/**
* @dev Encode ProtoBuf zig-zag encoding
* @param x The signed integer to be encoded
* @param p The offset of bytes array `bs`
* @param bs The bytes array to encode
* @return The length of encoded bytes
*/
function _encode_varints(int256 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
/**
* Refer to https://developers.google.com/protocol-buffers/docs/encoding
*/
uint256 encodedInt = _encode_zigzag(x);
return _encode_varint(encodedInt, p, bs);
}
/**
* @dev Encode ProtoBuf bytes
* @param xs The bytes array to be encoded
* @param p The offset of bytes array `bs`
* @param bs The bytes array to encode
* @return The length of encoded bytes
*/
function _encode_bytes(bytes memory xs, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
uint256 xsLength = xs.length;
uint256 sz = _encode_varint(xsLength, p, bs);
uint256 count = 0;
assembly {
let bsptr := add(bs, add(p, sz))
let xsptr := add(xs, 32)
for {
} lt(count, xsLength) {
} {
mstore8(bsptr, byte(0, mload(xsptr)))
bsptr := add(bsptr, 1)
xsptr := add(xsptr, 1)
count := add(count, 1)
}
}
return sz + count;
}
/**
* @dev Encode ProtoBuf string
* @param xs The string to be encoded
* @param p The offset of bytes array `bs`
* @param bs The bytes array to encode
* @return The length of encoded bytes
*/
function _encode_string(string memory xs, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_bytes(bytes(xs), p, bs);
}
/**
* `_encode_(u)int(32|64)`, `_encode_enum` and `_encode_bool`
* are concrete implementation of `_encode_varint`
*/
function _encode_uint32(uint32 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_varint(x, p, bs);
}
function _encode_uint64(uint64 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_varint(x, p, bs);
}
function _encode_int32(int32 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
uint64 twosComplement;
assembly {
twosComplement := x
}
return _encode_varint(twosComplement, p, bs);
}
function _encode_int64(int64 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
uint64 twosComplement;
assembly {
twosComplement := x
}
return _encode_varint(twosComplement, p, bs);
}
function _encode_enum(int32 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_int32(x, p, bs);
}
function _encode_bool(bool x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
if (x) {
return _encode_varint(1, p, bs);
} else return _encode_varint(0, p, bs);
}
/**
* `_encode_sint(32|64)`, `_encode_enum` and `_encode_bool`
* are the concrete implementation of `_encode_varints`
*/
function _encode_sint32(int32 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_varints(x, p, bs);
}
function _encode_sint64(int64 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_varints(x, p, bs);
}
/**
* `_encode_(s)fixed(32|64)` is the concrete implementation of `_encode_uintf`
*/
function _encode_fixed32(uint32 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_uintf(x, p, bs, 4);
}
function _encode_fixed64(uint64 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
return _encode_uintf(x, p, bs, 8);
}
function _encode_sfixed32(int32 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
uint32 twosComplement;
assembly {
twosComplement := x
}
return _encode_uintf(twosComplement, p, bs, 4);
}
function _encode_sfixed64(int64 x, uint256 p, bytes memory bs)
internal
pure
returns (uint256)
{
uint64 twosComplement;
assembly {
twosComplement := x
}
return _encode_uintf(twosComplement, p, bs, 8);
}
/**
* @dev Encode ProtoBuf fixed-length integer
* @param x The unsigned integer to be encoded
* @param p The offset of bytes array `bs`
* @param bs The bytes array to encode
* @return The length of encoded bytes
*/
function _encode_uintf(uint256 x, uint256 p, bytes memory bs, uint256 sz)
internal
pure
returns (uint256)
{
assembly {
let bsptr := add(sz, add(bs, p))
let count := sz
for {
} gt(count, 0) {
} {
bsptr := sub(bsptr, 1)
mstore8(bsptr, byte(sub(32, count), x))
count := sub(count, 1)
}
}
return sz;
}
/**
* @dev Encode ProtoBuf zig-zag signed integer
* @param i The unsigned integer to be encoded
* @return The encoded unsigned integer
*/
function _encode_zigzag(int256 i) internal pure returns (uint256) {
if (i >= 0) {
return uint256(i) * 2;
} else return uint256(i * -2) - 1;
}
// Estimators
/**
* @dev Estimate the length of encoded LengthDelim
* @param i The length of LengthDelim
* @return The estimated encoded length
*/
function _sz_lendelim(uint256 i) internal pure returns (uint256) {
return i + _sz_varint(i);
}
/**
* @dev Estimate the length of encoded ProtoBuf field ID
* @param i The field ID
* @return The estimated encoded length
*/
function _sz_key(uint256 i) internal pure returns (uint256) {
if (i < 16) {
return 1;
} else if (i < 2048) {
return 2;
} else if (i < 262144) {
return 3;
} else {
revert("not supported");
}
}
/**
* @dev Estimate the length of encoded ProtoBuf varint
* @param i The unsigned integer
* @return The estimated encoded length
*/
function _sz_varint(uint256 i) internal pure returns (uint256) {
uint256 count = 1;
assembly {
i := shr(7, i)
for {
} gt(i, 0) {
} {
i := shr(7, i)
count := add(count, 1)
}
}
return count;
}
/**
* `_sz_(u)int(32|64)` and `_sz_enum` are the concrete implementation of `_sz_varint`
*/
function _sz_uint32(uint32 i) internal pure returns (uint256) {
return _sz_varint(i);
}
function _sz_uint64(uint64 i) internal pure returns (uint256) {
return _sz_varint(i);
}
function _sz_int32(int32 i) internal pure returns (uint256) {
if (i < 0) {
return 10;
} else return _sz_varint(uint32(i));
}
function _sz_int64(int64 i) internal pure returns (uint256) {
if (i < 0) {
return 10;
} else return _sz_varint(uint64(i));
}
function _sz_enum(int64 i) internal pure returns (uint256) {
if (i < 0) {
return 10;
} else return _sz_varint(uint64(i));
}
/**
* `_sz_sint(32|64)` and `_sz_enum` are the concrete implementation of zig-zag encoding
*/
function _sz_sint32(int32 i) internal pure returns (uint256) {
return _sz_varint(_encode_zigzag(i));
}
function _sz_sint64(int64 i) internal pure returns (uint256) {
return _sz_varint(_encode_zigzag(i));
}
/**
* `_estimate_packed_repeated_(uint32|uint64|int32|int64|sint32|sint64)`
*/
function _estimate_packed_repeated_uint32(uint32[] memory a) internal pure returns (uint256) {
uint256 e = 0;
for (uint i = 0; i < a.length; i++) {
e += _sz_uint32(a[i]);
}
return e;
}
function _estimate_packed_repeated_uint64(uint64[] memory a) internal pure returns (uint256) {
uint256 e = 0;
for (uint i = 0; i < a.length; i++) {
e += _sz_uint64(a[i]);
}
return e;
}
function _estimate_packed_repeated_int32(int32[] memory a) internal pure returns (uint256) {
uint256 e = 0;
for (uint i = 0; i < a.length; i++) {
e += _sz_int32(a[i]);
}
return e;
}
function _estimate_packed_repeated_int64(int64[] memory a) internal pure returns (uint256) {
uint256 e = 0;
for (uint i = 0; i < a.length; i++) {
e += _sz_int64(a[i]);
}
return e;
}