forked from microsoft/msquic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.c
1543 lines (1351 loc) · 45.9 KB
/
frame.c
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
/*++
Copyright (c) Microsoft Corporation.
Licensed under the MIT License.
Abstract:
Functions for encoding and decoding frames.
--*/
#include "precomp.h"
#ifdef QUIC_LOGS_WPP
#include "frame.tmh"
#endif
_Post_equal_to_(Buffer + sizeof(uint8_t))
uint8_t*
QuicUint8Encode(
_In_ uint8_t Value,
_Out_ uint8_t* Buffer
)
{
*Buffer = Value;
return Buffer + sizeof(uint8_t);
}
_Post_equal_to_(Buffer + sizeof(uint16_t))
uint8_t*
QuicUint16Encode(
_In_ uint16_t Value,
_Out_writes_bytes_all_(sizeof(uint16_t))
uint8_t* Buffer
)
{
*(uint16_t*)Buffer = QuicByteSwapUint16(Value);
return Buffer + sizeof(uint16_t);
}
_Success_(return != FALSE)
BOOLEAN
QuicUint16Decode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_
_Deref_in_range_(0, BufferLength)
_Deref_out_range_(0, BufferLength)
uint16_t* Offset,
_Out_ uint16_t* Value
)
{
if (*Offset + sizeof(uint16_t) > BufferLength) {
return FALSE;
}
*Value = QuicByteSwapUint16(*(const uint16_t * const)(Buffer + *Offset));
*Offset += sizeof(uint16_t);
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicAckHeaderEncode(
_In_ const QUIC_ACK_EX * const Frame,
_In_opt_ PQUIC_ACK_ECN_EX Ecn,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->LargestAcknowledged) +
QuicVarIntSize(Frame->AckDelay) +
QuicVarIntSize(Frame->AdditionalAckBlockCount) +
QuicVarIntSize(Frame->FirstAckBlock);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(Ecn == NULL ? QUIC_FRAME_ACK : QUIC_FRAME_ACK + 1, Buffer);
Buffer = QuicVarIntEncode(Frame->LargestAcknowledged, Buffer);
Buffer = QuicVarIntEncode(Frame->AckDelay, Buffer);
Buffer = QuicVarIntEncode(Frame->AdditionalAckBlockCount, Buffer);
Buffer = QuicVarIntEncode(Frame->FirstAckBlock, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicAckHeaderDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_ACK_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->LargestAcknowledged) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->AckDelay) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->AdditionalAckBlockCount) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->FirstAckBlock)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicAckBlockEncode(
_In_ const QUIC_ACK_BLOCK_EX * const Block,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
QuicVarIntSize(Block->Gap) +
QuicVarIntSize(Block->AckBlock);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicVarIntEncode(Block->Gap, Buffer);
Buffer = QuicVarIntEncode(Block->AckBlock, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicAckBlockDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_ACK_BLOCK_EX Block
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Block->Gap) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Block->AckBlock)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicAckEcnEncode(
_In_ const QUIC_ACK_ECN_EX * const Ecn,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
QuicVarIntSize(Ecn->ECT_0_Count) +
QuicVarIntSize(Ecn->ECT_0_Count) +
QuicVarIntSize(Ecn->CE_Count);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicVarIntEncode(Ecn->ECT_0_Count, Buffer);
Buffer = QuicVarIntEncode(Ecn->ECT_0_Count, Buffer);
Buffer = QuicVarIntEncode(Ecn->CE_Count, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicAckEcnDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_ACK_ECN_EX Ecn
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Ecn->ECT_0_Count) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Ecn->ECT_1_Count) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Ecn->CE_Count)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicAckFrameEncode(
_In_ const QUIC_RANGE * const AckBlocks,
_In_ uint64_t AckDelay,
_In_opt_ PQUIC_ACK_ECN_EX Ecn,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint32_t i = QuicRangeSize(AckBlocks) - 1;
PQUIC_SUBRANGE LastSub = QuicRangeGet(AckBlocks, i);
uint64_t Largest = QuicRangeGetHigh(LastSub);
uint64_t Count = LastSub->Count;
//
// Write the ACK Frame Header
//
QUIC_ACK_EX Frame = {
Largest, // LargestAcknowledged
AckDelay, // AckDelay
i, // AdditionalAckBlockCount
Count - 1 // FirstAckBlock
};
if (!QuicAckHeaderEncode(&Frame, Ecn, Offset, BufferLength, Buffer)) {
return FALSE;
}
//
// Write any additional ACK Blocks
//
while (i != 0) {
QUIC_DBG_ASSERT(Largest >= Count);
Largest -= Count;
PQUIC_SUBRANGE Next = QuicRangeGet(AckBlocks, i - 1);
uint64_t NextLargest = QuicRangeGetHigh(Next);
Count = Next->Count;
QUIC_DBG_ASSERT(Largest > NextLargest);
QUIC_DBG_ASSERT(Count > 0);
QUIC_ACK_BLOCK_EX Block = {
(Largest - NextLargest) - 1, // Gap
Count - 1 // AckBlock
};
if (!QuicAckBlockEncode(&Block, Offset, BufferLength, Buffer)) {
QUIC_TEL_ASSERT(FALSE); // TODO - Support partial ACK array encoding by updating the 'AdditionalAckBlockCount' field.
return FALSE;
}
Largest = NextLargest;
i--;
}
if (Ecn != NULL) {
if (!QuicAckEcnEncode(Ecn, Offset, BufferLength, Buffer)) {
return FALSE;
}
}
return TRUE;
}
//
// Given that the max UDP packet is 64k, this is a reasonable upper bound for
// the number of ACK blocks possible.
//
#define QUIC_MAX_NUMBER_ACK_BLOCKS 0x10000
//
// Decodes the ACK_FRAME (has packet numbers from largest to smallest) to a
// QUIC_RANGE format (smallest to largest).
// TODO - Continue to work with QUICWG to try to get this reversed.
//
_Success_(return != FALSE)
BOOLEAN
QuicAckFrameDecode(
_In_ QUIC_FRAME_TYPE FrameType,
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ BOOLEAN* InvalidFrame,
_Inout_ PQUIC_RANGE AckRanges, // Pre-Initialized by caller
_When_(FrameType == QUIC_FRAME_ACK_1, _Out_)
PQUIC_ACK_ECN_EX Ecn,
_Out_ uint64_t* AckDelay
)
{
*InvalidFrame = FALSE;
QUIC_DBG_ASSERT(AckRanges->SubRanges); // Should be pre-initialized.
//
// Decode the ACK frame header.
//
QUIC_ACK_EX Frame;
if (!QuicAckHeaderDecode(BufferLength, Buffer, Offset, &Frame)) {
*InvalidFrame = TRUE;
return FALSE;
}
//
// Insert the largest/first block into the range.
//
uint64_t Largest = Frame.LargestAcknowledged;
uint64_t Count = Frame.FirstAckBlock + 1;
BOOLEAN DontCare;
if (!QuicRangeAddRange(AckRanges, Largest - Count + 1, Count, &DontCare)) {
return FALSE;
}
if (Frame.AdditionalAckBlockCount >= QUIC_MAX_NUMBER_ACK_BLOCKS) {
*InvalidFrame = TRUE;
return FALSE;
}
//
// Insert all the rest of the blocks (if any) into the range.
//
for (uint32_t i = 0; i < (uint32_t)Frame.AdditionalAckBlockCount; i++) {
if (Count > Largest) {
*InvalidFrame = TRUE;
return FALSE;
}
Largest -= Count;
QUIC_ACK_BLOCK_EX Block;
if (!QuicAckBlockDecode(BufferLength, Buffer, Offset, &Block)) {
*InvalidFrame = TRUE;
return FALSE;
}
if (Block.Gap + 1 > Largest) {
*InvalidFrame = TRUE;
return FALSE;
}
Largest -= (Block.Gap + 1);
Count = Block.AckBlock + 1;
//
// N.B. The efficiency here isn't great because we are always inserting
// values less than the current minimum, which requires a complete
// memmove of the current array. A circular buffer that allows both
// forward and backward growth would fix this.
//
if (!QuicRangeAddRange(AckRanges, Largest - Count + 1, Count, &DontCare)) {
return FALSE;
}
}
*AckDelay = Frame.AckDelay;
if (FrameType == QUIC_FRAME_ACK_1) {
//
// The ECN section was provided. Decode it as well.
//
if (!QuicAckEcnDecode(BufferLength, Buffer, Offset, Ecn)) {
return FALSE;
}
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicResetStreamFrameEncode(
_In_ const QUIC_RESET_STREAM_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->ErrorCode) +
QuicVarIntSize(Frame->StreamID) +
QuicVarIntSize(Frame->FinalSize);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_RESET_STREAM, Buffer);
Buffer = QuicVarIntEncode(Frame->StreamID, Buffer);
Buffer = QuicVarIntEncode(Frame->ErrorCode, Buffer);
Buffer = QuicVarIntEncode(Frame->FinalSize, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicResetStreamFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_RESET_STREAM_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->StreamID) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->ErrorCode) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->FinalSize)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStopSendingFrameEncode(
_In_ const QUIC_STOP_SENDING_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->StreamID) +
QuicVarIntSize(Frame->ErrorCode);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_STOP_SENDING, Buffer);
Buffer = QuicVarIntEncode(Frame->StreamID, Buffer);
Buffer = QuicVarIntEncode(Frame->ErrorCode, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStopSendingFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_STOP_SENDING_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->StreamID) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->ErrorCode)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicCryptoFrameEncode(
_In_ const QUIC_CRYPTO_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
QUIC_DBG_ASSERT(Frame->Length < UINT16_MAX);
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->Offset) +
QuicVarIntSize(Frame->Length) +
(uint16_t)Frame->Length;
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_CRYPTO, Buffer);
Buffer = QuicVarIntEncode(Frame->Offset, Buffer);
Buffer = QuicVarIntEncode(Frame->Length, Buffer);
QuicCopyMemory(Buffer, Frame->Data, (uint16_t)Frame->Length);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicCryptoFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_CRYPTO_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->Offset) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->Length) ||
BufferLength - *Offset < Frame->Length) {
return FALSE;
}
Frame->Data = Buffer + *Offset;
*Offset += (uint16_t)Frame->Length;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicNewTokenFrameEncode(
_In_ const QUIC_NEW_TOKEN_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->TokenLength) +
(uint16_t)Frame->TokenLength;
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_NEW_TOKEN, Buffer);
Buffer = QuicVarIntEncode(Frame->TokenLength, Buffer);
QuicCopyMemory(Buffer, Frame->Token, (uint16_t)Frame->TokenLength);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicNewTokenFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_NEW_TOKEN_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->TokenLength) ||
BufferLength - *Offset < Frame->TokenLength) {
return FALSE;
}
Frame->Token = Buffer + *Offset;
*Offset += (uint16_t)Frame->TokenLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStreamFrameEncode(
_In_ const QUIC_STREAM_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
__analysis_assume(Frame->Length < 0x10000);
QUIC_DBG_ASSERT(Frame->Length < 0x10000);
uint16_t RequiredLength =
QuicStreamFrameHeaderSize(Frame) +
(uint16_t)Frame->Length;
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
QUIC_STREAM_FRAME_TYPE Type = {{{
Frame->Fin,
Frame->ExplicitLength,
Frame->Offset != 0 ? TRUE : FALSE,
0b00001
}}};
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(Type.Type, Buffer);
Buffer = QuicVarIntEncode(Frame->StreamID, Buffer);
if (Type.OFF) {
Buffer = QuicVarIntEncode(Frame->Offset, Buffer);
}
if (Type.LEN) {
Buffer = QuicVarIntEncode2Bytes(Frame->Length, Buffer); // We always use two bytes for the explicit length.
}
QUIC_DBG_ASSERT(Frame->Length == 0 || Buffer == Frame->Data); // Caller already set the data.
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStreamFrameDecode(
_In_ QUIC_FRAME_TYPE FrameType,
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_
_Deref_in_range_(0, BufferLength)
_Deref_out_range_(0, BufferLength)
uint16_t* Offset,
_Out_ PQUIC_STREAM_EX Frame
)
{
QUIC_STREAM_FRAME_TYPE Type = { .Type = FrameType };
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->StreamID)) {
return FALSE;
}
if (Type.OFF) {
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->Offset)) {
return FALSE;
}
} else {
Frame->Offset = 0;
}
if (Type.LEN) {
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->Length) ||
BufferLength - *Offset < Frame->Length) {
return FALSE;
}
} else {
QUIC_ANALYSIS_ASSERT(BufferLength >= *Offset);
Frame->Length = BufferLength - *Offset;
}
Frame->Fin = Type.FIN;
Frame->Data = Buffer + *Offset;
*Offset += (uint16_t)Frame->Length;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicMaxDataFrameEncode(
_In_ const QUIC_MAX_DATA_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->MaximumData);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_MAX_DATA, Buffer);
Buffer = QuicVarIntEncode(Frame->MaximumData, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicMaxDataFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_MAX_DATA_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->MaximumData)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicMaxStreamDataFrameEncode(
_In_ const QUIC_MAX_STREAM_DATA_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->StreamID) +
QuicVarIntSize(Frame->MaximumData);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_MAX_STREAM_DATA, Buffer);
Buffer = QuicVarIntEncode(Frame->StreamID, Buffer);
Buffer = QuicVarIntEncode(Frame->MaximumData, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicMaxStreamDataFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_MAX_STREAM_DATA_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->StreamID) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->MaximumData)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicMaxStreamsFrameEncode(
_In_ const QUIC_MAX_STREAMS_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->MaximumStreams);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer =
QuicUint8Encode(
Frame->BidirectionalStreams ?
QUIC_FRAME_MAX_STREAMS :
QUIC_FRAME_MAX_STREAMS_1,
Buffer);
Buffer = QuicVarIntEncode(Frame->MaximumStreams, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicMaxStreamsFrameDecode(
_In_ QUIC_FRAME_TYPE FrameType,
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_MAX_STREAMS_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->MaximumStreams)) {
return FALSE;
}
Frame->BidirectionalStreams = FrameType == QUIC_FRAME_MAX_STREAMS;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicDataBlockedFrameEncode(
_In_ const QUIC_DATA_BLOCKED_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->DataLimit);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_DATA_BLOCKED, Buffer);
Buffer = QuicVarIntEncode(Frame->DataLimit, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicDataBlockedFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_DATA_BLOCKED_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->DataLimit)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStreamDataBlockedFrameEncode(
_In_ const QUIC_STREAM_DATA_BLOCKED_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->StreamID) +
QuicVarIntSize(Frame->StreamDataLimit);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_STREAM_DATA_BLOCKED, Buffer);
Buffer = QuicVarIntEncode(Frame->StreamID, Buffer);
Buffer = QuicVarIntEncode(Frame->StreamDataLimit, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStreamDataBlockedFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_STREAM_DATA_BLOCKED_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->StreamID) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->StreamDataLimit)) {
return FALSE;
}
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStreamsBlockedFrameEncode(
_In_ const QUIC_STREAMS_BLOCKED_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->StreamLimit);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer =
QuicUint8Encode(
Frame->BidirectionalStreams ?
QUIC_FRAME_STREAMS_BLOCKED :
QUIC_FRAME_STREAMS_BLOCKED_1,
Buffer);
Buffer = QuicVarIntEncode(Frame->StreamLimit, Buffer);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicStreamsBlockedFrameDecode(
_In_ QUIC_FRAME_TYPE FrameType,
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_STREAMS_BLOCKED_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->StreamLimit)) {
return FALSE;
}
Frame->BidirectionalStreams = FrameType == QUIC_FRAME_STREAMS_BLOCKED;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicNewConnectionIDFrameEncode(
_In_ const QUIC_NEW_CONNECTION_ID_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->Sequence) +
QuicVarIntSize(Frame->RetirePriorTo) +
sizeof(uint8_t) + // Length
Frame->Length +
QUIC_STATELESS_RESET_TOKEN_LENGTH;
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_NEW_CONNECTION_ID, Buffer);
Buffer = QuicVarIntEncode(Frame->Sequence, Buffer);
Buffer = QuicVarIntEncode(Frame->RetirePriorTo, Buffer);
Buffer = QuicUint8Encode(Frame->Length, Buffer);
QuicCopyMemory(Buffer, Frame->Buffer, Frame->Length + QUIC_STATELESS_RESET_TOKEN_LENGTH);
*Offset += RequiredLength;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicNewConnectionIDFrameDecode(
_In_ uint16_t BufferLength,
_In_reads_bytes_(BufferLength)
const uint8_t * const Buffer,
_Inout_ uint16_t* Offset,
_Out_ PQUIC_NEW_CONNECTION_ID_EX Frame
)
{
if (!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->Sequence) ||
!QuicVarIntDecode(BufferLength, Buffer, Offset, &Frame->RetirePriorTo) ||
Frame->RetirePriorTo > Frame->Sequence ||
BufferLength < *Offset + 1) {
return FALSE;
}
Frame->Length = Buffer[(*Offset)++];
if (Frame->Length < 1 || Frame->Length > QUIC_MAX_CONNECTION_ID_LENGTH_V1 ||
BufferLength < *Offset + Frame->Length + QUIC_STATELESS_RESET_TOKEN_LENGTH) {
return FALSE;
}
QuicCopyMemory(Frame->Buffer, Buffer + *Offset, Frame->Length + QUIC_STATELESS_RESET_TOKEN_LENGTH);
*Offset += Frame->Length + QUIC_STATELESS_RESET_TOKEN_LENGTH;
return TRUE;
}
_Success_(return != FALSE)
BOOLEAN
QuicRetireConnectionIDFrameEncode(
_In_ const QUIC_RETIRE_CONNECTION_ID_EX * const Frame,
_Inout_ uint16_t* Offset,
_In_ uint16_t BufferLength,
_Out_writes_to_(BufferLength, *Offset) uint8_t* Buffer
)
{
uint16_t RequiredLength =
sizeof(uint8_t) + // Type
QuicVarIntSize(Frame->Sequence);
if (BufferLength < *Offset + RequiredLength) {
return FALSE;
}
Buffer = Buffer + *Offset;
Buffer = QuicUint8Encode(QUIC_FRAME_RETIRE_CONNECTION_ID, Buffer);
Buffer = QuicVarIntEncode(Frame->Sequence, Buffer);
*Offset += RequiredLength;
return TRUE;
}