ironrdp_rdpdr/pdu/
efs.rs

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
//! PDUs for [\[MS-RDPEFS\]: Remote Desktop Protocol: File System Virtual Channel Extension]
//!
//! [\[MS-RDPEFS\]: Remote Desktop Protocol: File System Virtual Channel Extension]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/34d9de58-b2b5-40b6-b970-f82d4603bdb5

use core::fmt;
use core::fmt::{Debug, Display};
use core::mem::size_of;

use bitflags::bitflags;
use ironrdp_core::{
    cast_length, ensure_fixed_part_size, ensure_size, invalid_field_err, invalid_field_err_with_source,
    unsupported_value_err, DecodeError, DecodeResult, EncodeResult, ReadCursor, WriteCursor,
};
use ironrdp_pdu::utils::{decode_string, encoded_str_len, from_utf16_bytes, write_string_to_cursor, CharacterSet};
use ironrdp_pdu::{read_padding, write_padding, PduError};

use super::esc::rpce;
use super::{PacketId, SharedHeader};

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum VersionAndIdPduKind {
    /// [2.2.2.2] Server Announce Request (DR_CORE_SERVER_ANNOUNCE_REQ)
    ///
    /// [2.2.2.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/046047aa-62d8-49f9-bf16-7fe41880aaf4
    ServerAnnounceRequest,
    /// [2.2.2.3] Client Announce Reply (DR_CORE_CLIENT_ANNOUNCE_RSP)
    ///
    /// [2.2.2.3]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/d6fe6d1b-c145-4a6f-99aa-4fe3cdcea398
    ClientAnnounceReply,
    /// [2.2.2.6] Server Client ID Confirm (DR_CORE_SERVER_CLIENTID_CONFIRM)
    ///
    /// [2.2.2.6]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/bbbb9666-6994-4cf6-8e65-0d46eb319c6e
    ServerClientIdConfirm,
}

impl VersionAndIdPduKind {
    fn name(&self) -> &'static str {
        match self {
            VersionAndIdPduKind::ServerAnnounceRequest => "DR_CORE_SERVER_ANNOUNCE_REQ",
            VersionAndIdPduKind::ClientAnnounceReply => "DR_CORE_CLIENT_ANNOUNCE_RSP",
            VersionAndIdPduKind::ServerClientIdConfirm => "DR_CORE_SERVER_CLIENTID_CONFIRM",
        }
    }
}

/// VersionAndIdPDU is a fixed size structure representing multiple PDUs.
///
/// The kind field is used to determine the actual PDU type, see [`VersionAndIdPduKind`].
#[derive(Debug, PartialEq, Clone)]
pub struct VersionAndIdPdu {
    /// This field MUST be set to 0x0001 ([`VERSION_MAJOR`]).
    pub version_major: u16,
    pub version_minor: u16,
    pub client_id: u32,
    pub kind: VersionAndIdPduKind,
}

impl VersionAndIdPdu {
    const FIXED_PART_SIZE: usize = (size_of::<u16>() * 2) + size_of::<u32>();

    pub fn new_client_announce_reply(req: VersionAndIdPdu) -> DecodeResult<Self> {
        if req.kind != VersionAndIdPduKind::ServerAnnounceRequest {
            return Err(invalid_field_err!(
                "VersionAndIdPdu::new_client_announce_reply",
                "VersionAndIdPduKind",
                "invalid value"
            ));
        }

        Ok(Self {
            version_major: VERSION_MAJOR,
            version_minor: VERSION_MINOR_12,
            client_id: req.client_id,
            kind: VersionAndIdPduKind::ClientAnnounceReply,
        })
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(ctx: self.name(), in: dst, size: Self::FIXED_PART_SIZE);
        dst.write_u16(self.version_major);
        dst.write_u16(self.version_minor);
        dst.write_u32(self.client_id);
        Ok(())
    }

    pub fn decode(header: SharedHeader, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        let kind = match header.packet_id {
            PacketId::CoreServerAnnounce => VersionAndIdPduKind::ServerAnnounceRequest,
            PacketId::CoreClientidConfirm => VersionAndIdPduKind::ServerClientIdConfirm,
            _ => {
                return Err(invalid_field_err!(
                    "VersionAndIdPdu::decode",
                    "PacketId",
                    "invalid value"
                ))
            }
        };

        ensure_size!(ctx: kind.name(), in: src, size: Self::FIXED_PART_SIZE);
        let version_major = src.read_u16();
        let version_minor = src.read_u16();
        let client_id = src.read_u32();

        Ok(Self {
            version_major,
            version_minor,
            client_id,
            kind,
        })
    }

    pub fn name(&self) -> &'static str {
        self.kind.name()
    }

    pub fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

/// [2.2.2.4] Client Name Request (DR_CORE_CLIENT_NAME_REQ)
///
/// [2.2.2.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/902497f1-3b1c-4aee-95f8-1668f9b7b7d2
#[derive(Debug, PartialEq, Clone)]
pub enum ClientNameRequest {
    Ascii(String),
    Unicode(String),
}

impl ClientNameRequest {
    const NAME: &'static str = "DR_CORE_CLIENT_NAME_REQ";
    const FIXED_PART_SIZE: usize = size_of::<u32>() * 3; // unicode_flag + CodePage + ComputerNameLen

    pub fn new(computer_name: String, kind: ClientNameRequestUnicodeFlag) -> Self {
        match kind {
            ClientNameRequestUnicodeFlag::Ascii => ClientNameRequest::Ascii(computer_name),
            ClientNameRequestUnicodeFlag::Unicode => ClientNameRequest::Unicode(computer_name),
        }
    }

    fn unicode_flag(&self) -> ClientNameRequestUnicodeFlag {
        match self {
            ClientNameRequest::Ascii(_) => ClientNameRequestUnicodeFlag::Ascii,
            ClientNameRequest::Unicode(_) => ClientNameRequestUnicodeFlag::Unicode,
        }
    }

    fn computer_name(&self) -> &str {
        match self {
            ClientNameRequest::Ascii(name) => name,
            ClientNameRequest::Unicode(name) => name,
        }
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.unicode_flag().into());
        dst.write_u32(0); // // CodePage (4 bytes): it MUST be set to 0
        dst.write_u32(encoded_str_len(self.computer_name(), self.unicode_flag().into(), true) as u32);
        write_string_to_cursor(dst, self.computer_name(), self.unicode_flag().into(), true)
    }

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + encoded_str_len(self.computer_name(), self.unicode_flag().into(), true)
    }
}

#[repr(u32)]
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum ClientNameRequestUnicodeFlag {
    Ascii = 0x0,
    Unicode = 0x1,
}

impl From<ClientNameRequestUnicodeFlag> for CharacterSet {
    fn from(val: ClientNameRequestUnicodeFlag) -> Self {
        match val {
            ClientNameRequestUnicodeFlag::Ascii => CharacterSet::Ansi,
            ClientNameRequestUnicodeFlag::Unicode => CharacterSet::Unicode,
        }
    }
}

impl From<ClientNameRequestUnicodeFlag> for u32 {
    fn from(val: ClientNameRequestUnicodeFlag) -> Self {
        val as u32
    }
}

/// [2.2.2.7] Server Core Capability Request (DR_CORE_CAPABILITY_REQ)
/// and [2.2.2.8] Client Core Capability Response (DR_CORE_CAPABILITY_RSP)
///
/// [2.2.2.7]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/702789c3-b924-4bc2-9280-3221bc7d6797
/// [2.2.2.8]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/f513bf87-cca0-488a-ac5c-18cf18f4a7e1
#[derive(Debug, PartialEq, Clone)]
pub struct CoreCapability {
    pub capabilities: Vec<CapabilityMessage>,
    pub kind: CoreCapabilityKind,
}

impl CoreCapability {
    const FIXED_PART_SIZE: usize = size_of::<u16>() * 2;

    /// Creates a new [`DR_CORE_CAPABILITY_RSP`] with the given `capabilities`.
    ///
    /// [`DR_CORE_CAPABILITY_RSP`]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/f513bf87-cca0-488a-ac5c-18cf18f4a7e1
    pub fn new_response(capabilities: Vec<CapabilityMessage>) -> Self {
        Self {
            capabilities,
            kind: CoreCapabilityKind::ClientCoreCapabilityResponse,
        }
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(ctx: self.name(), in: dst, size: self.size());
        dst.write_u16(cast_length!(
            "CoreCapability",
            "numCapabilities",
            self.capabilities.len()
        )?);
        write_padding!(dst, 2); // 2-bytes padding
        for cap in self.capabilities.iter() {
            cap.encode(dst)?;
        }
        Ok(())
    }

    pub fn decode(header: SharedHeader, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        let kind = match header.packet_id {
            PacketId::CoreServerCapability => CoreCapabilityKind::ServerCoreCapabilityRequest,
            PacketId::CoreClientCapability => CoreCapabilityKind::ClientCoreCapabilityResponse,
            _ => {
                return Err(invalid_field_err!(
                    "CoreCapability::decode",
                    "PacketId",
                    "invalid value"
                ))
            }
        };

        ensure_size!(ctx: kind.name(), in: src, size: Self::FIXED_PART_SIZE);

        let num_capabilities = src.read_u16();
        read_padding!(src, 2); // 2-bytes padding
        let mut capabilities = Vec::new();
        for _ in 0..num_capabilities {
            capabilities.push(CapabilityMessage::decode(src)?);
        }

        Ok(Self { capabilities, kind })
    }

    pub fn name(&self) -> &'static str {
        self.kind.name()
    }

    pub fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + self.capabilities.iter().map(|c| c.size()).sum::<usize>()
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum CoreCapabilityKind {
    /// [2.2.2.7] Server Core Capability Request (DR_CORE_CAPABILITY_REQ)
    ///
    /// [2.2.2.7]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/702789c3-b924-4bc2-9280-3221bc7d6797
    ServerCoreCapabilityRequest,
    /// [2.2.2.8] Client Core Capability Response (DR_CORE_CAPABILITY_RSP)
    ///
    /// [2.2.2.8]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/f513bf87-cca0-488a-ac5c-18cf18f4a7e1
    ClientCoreCapabilityResponse,
}

impl CoreCapabilityKind {
    fn name(&self) -> &'static str {
        match self {
            CoreCapabilityKind::ServerCoreCapabilityRequest => "DR_CORE_CAPABILITY_REQ",
            CoreCapabilityKind::ClientCoreCapabilityResponse => "DR_CORE_CAPABILITY_RSP",
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct Capabilities(Vec<CapabilityMessage>);

impl Capabilities {
    pub fn new() -> Self {
        let mut this = Self(Vec::new());
        this.add_general(0);
        this
    }

    pub fn clone_inner(&mut self) -> Vec<CapabilityMessage> {
        self.0.clone()
    }

    pub fn add_smartcard(&mut self) {
        self.push(CapabilityMessage::new_smartcard());
        self.increment_special_devices();
    }

    pub fn add_drive(&mut self) {
        self.push(CapabilityMessage::new_drive());
    }

    fn add_general(&mut self, special_type_device_cap: u32) {
        self.push(CapabilityMessage::new_general(special_type_device_cap));
    }

    fn push(&mut self, capability: CapabilityMessage) {
        self.0.push(capability);
    }

    fn increment_special_devices(&mut self) {
        let capabilities = &mut self.0;
        for capability in capabilities.iter_mut() {
            match &mut capability.capability_data {
                CapabilityData::General(general_capability) => {
                    general_capability.special_type_device_cap += 1;
                    break;
                }
                _ => continue,
            }
        }
    }
}

impl Default for Capabilities {
    fn default() -> Self {
        Self::new()
    }
}

/// [2.2.1.2.1] Capability Message (CAPABILITY_SET)
///
/// [2.2.1.2.1]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/f1b9dd1d-2c37-4aac-9836-4b0df02369ba
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct CapabilityMessage {
    header: CapabilityHeader,
    capability_data: CapabilityData,
}

impl CapabilityMessage {
    /// Creates a new [`GENERAL_CAPS_SET`].
    ///
    /// `special_type_device_cap`: A 32-bit unsigned integer that
    /// specifies the number of special devices to be redirected
    /// before the user is logged on. Special devices are those
    /// that are safe and/or required to be redirected before a
    /// user logs on (such as smart cards and serial ports).
    ///
    /// [`GENERAL_CAPS_SET`]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/06c7cb30-303d-4fa2-b396-806df8ac1501
    pub fn new_general(special_type_device_cap: u32) -> Self {
        Self {
            header: CapabilityHeader::new_general(),
            capability_data: CapabilityData::General(GeneralCapabilitySet {
                os_type: 0,
                os_version: 0,
                protocol_major_version: 1,
                protocol_minor_version: VERSION_MINOR_12,
                io_code_1: IoCode1::REQUIRED,
                io_code_2: 0,
                extended_pdu: ExtendedPdu::RDPDR_DEVICE_REMOVE_PDUS | ExtendedPdu::RDPDR_CLIENT_DISPLAY_NAME_PDU,
                extra_flags_1: ExtraFlags1::empty(),
                extra_flags_2: 0,
                special_type_device_cap,
            }),
        }
    }

    /// Creates a new [`SMARTCARD_CAPS_SET`].
    ///
    /// [`SMARTCARD_CAPS_SET`]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/e02de60a-4d32-4dc7-ab17-9d591129eb93
    pub fn new_smartcard() -> Self {
        Self {
            header: CapabilityHeader::new_smartcard(),
            capability_data: CapabilityData::Smartcard,
        }
    }

    /// Creates a new [`DRIVE_CAPS_SET`].
    ///
    /// [`DRIVE_CAPS_SET`]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/4f018cd2-60ba-4c7b-adcf-55bd05cea6f8
    pub fn new_drive() -> Self {
        Self {
            header: CapabilityHeader::new_drive(),
            capability_data: CapabilityData::Drive,
        }
    }

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.header.encode(dst)?;
        self.capability_data.encode(dst)
    }

    fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        let header = CapabilityHeader::decode(src)?;
        let capability_data = CapabilityData::decode(src, &header)?;

        Ok(Self {
            header,
            capability_data,
        })
    }

    fn size(&self) -> usize {
        CapabilityHeader::SIZE + self.capability_data.size()
    }
}

/// [2.2.1.2] Capability Header (CAPABILITY_HEADER)
///
/// [2.2.1.2]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/b3c3304a-2e1b-4667-97e9-3bce49544907
#[derive(Debug, PartialEq, Clone, Copy)]
struct CapabilityHeader {
    cap_type: CapabilityType,
    length: u16,
    version: u32,
}

impl CapabilityHeader {
    const SIZE: usize = size_of::<u16>() * 2 + size_of::<u32>();

    fn new_general() -> Self {
        Self {
            cap_type: CapabilityType::General,
            length: (Self::SIZE + GeneralCapabilitySet::SIZE) as u16,
            version: GENERAL_CAPABILITY_VERSION_02,
        }
    }

    fn new_smartcard() -> Self {
        Self {
            cap_type: CapabilityType::Smartcard,
            length: Self::SIZE as u16,
            version: SMARTCARD_CAPABILITY_VERSION_01,
        }
    }

    fn new_drive() -> Self {
        Self {
            cap_type: CapabilityType::Drive,
            length: Self::SIZE as u16,
            version: DRIVE_CAPABILITY_VERSION_02,
        }
    }

    fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(in: src, size: Self::SIZE);
        let cap_type: CapabilityType = src.read_u16().try_into()?;
        let length = src.read_u16();
        let version = src.read_u32();

        Ok(Self {
            cap_type,
            length,
            version,
        })
    }

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: Self::SIZE);
        dst.write_u16(self.cap_type.into());
        dst.write_u16(self.length);
        dst.write_u32(self.version);
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Copy)]
#[repr(u16)]
enum CapabilityType {
    /// CAP_GENERAL_TYPE
    General = 0x0001,
    /// CAP_PRINTER_TYPE
    Printer = 0x0002,
    /// CAP_PORT_TYPE
    Port = 0x0003,
    /// CAP_DRIVE_TYPE
    Drive = 0x0004,
    /// CAP_SMARTCARD_TYPE
    Smartcard = 0x0005,
}

impl From<CapabilityType> for u16 {
    fn from(cap_type: CapabilityType) -> Self {
        cap_type as u16
    }
}

/// GENERAL_CAPABILITY_VERSION_02
pub const GENERAL_CAPABILITY_VERSION_02: u32 = 0x0000_0002;
/// SMARTCARD_CAPABILITY_VERSION_01
pub const SMARTCARD_CAPABILITY_VERSION_01: u32 = 0x0000_0001;
/// DRIVE_CAPABILITY_VERSION_02
pub const DRIVE_CAPABILITY_VERSION_02: u32 = 0x0000_0002;

impl TryFrom<u16> for CapabilityType {
    type Error = DecodeError;

    fn try_from(value: u16) -> Result<Self, Self::Error> {
        match value {
            0x0001 => Ok(CapabilityType::General),
            0x0002 => Ok(CapabilityType::Printer),
            0x0003 => Ok(CapabilityType::Port),
            0x0004 => Ok(CapabilityType::Drive),
            0x0005 => Ok(CapabilityType::Smartcard),
            _ => Err(invalid_field_err!("try_from", "CapabilityType", "invalid value")),
        }
    }
}

#[derive(Debug, PartialEq, Clone, Copy)]
enum CapabilityData {
    General(GeneralCapabilitySet),
    Printer,
    Port,
    Drive,
    Smartcard,
}

impl CapabilityData {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        match self {
            CapabilityData::General(general) => general.encode(dst),
            _ => Ok(()),
        }
    }

    fn decode(src: &mut ReadCursor<'_>, header: &CapabilityHeader) -> DecodeResult<Self> {
        match header.cap_type {
            CapabilityType::General => Ok(CapabilityData::General(GeneralCapabilitySet::decode(
                src,
                header.version,
            )?)),
            CapabilityType::Printer => Ok(CapabilityData::Printer),
            CapabilityType::Port => Ok(CapabilityData::Port),
            CapabilityType::Drive => Ok(CapabilityData::Drive),
            CapabilityType::Smartcard => Ok(CapabilityData::Smartcard),
        }
    }

    fn size(&self) -> usize {
        match self {
            CapabilityData::General(_) => GeneralCapabilitySet::SIZE,
            CapabilityData::Printer => 0,
            CapabilityData::Port => 0,
            CapabilityData::Drive => 0,
            CapabilityData::Smartcard => 0,
        }
    }
}

/// [2.2.2.7.1] General Capability Set (GENERAL_CAPS_SET)
///
/// [2.2.2.7.1]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/06c7cb30-303d-4fa2-b396-806df8ac1501
#[derive(Debug, PartialEq, Clone, Copy)]
struct GeneralCapabilitySet {
    /// MUST be ignored.
    os_type: u32,
    /// SHOULD be ignored.
    os_version: u32,
    /// MUST be set to 1.
    protocol_major_version: u16,
    /// MUST be set to one of the values described by the VersionMinor field
    /// of the [Server Client ID Confirm (section 2.2.2.6)] packet.
    ///
    /// [Server Client ID Confirm (section 2.2.2.6)]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/bbbb9666-6994-4cf6-8e65-0d46eb319c6e
    protocol_minor_version: u16,
    /// See [`IoCode1`].
    io_code_1: IoCode1,
    /// MUST be set to 0.
    io_code_2: u32,
    /// See [`ExtendedPdu`].
    extended_pdu: ExtendedPdu,
    /// See [`ExtraFlags1`].
    extra_flags_1: ExtraFlags1,
    /// MUST be set to 0.
    extra_flags_2: u32,
    /// A 32-bit unsigned integer that specifies the number
    /// of special devices to be redirected before the user
    /// is logged on. Special devices are those that are safe
    /// and/or required to be redirected before a user logs
    /// on (such as smart cards and serial ports).
    special_type_device_cap: u32,
}

impl GeneralCapabilitySet {
    #[allow(clippy::manual_bits)]
    const SIZE: usize = size_of::<u32>() * 8 + size_of::<u16>() * 2;

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: Self::SIZE);
        dst.write_u32(self.os_type);
        dst.write_u32(self.os_version);
        dst.write_u16(self.protocol_major_version);
        dst.write_u16(self.protocol_minor_version);
        dst.write_u32(self.io_code_1.bits());
        dst.write_u32(self.io_code_2);
        dst.write_u32(self.extended_pdu.bits());
        dst.write_u32(self.extra_flags_1.bits());
        dst.write_u32(self.extra_flags_2);
        dst.write_u32(self.special_type_device_cap);
        Ok(())
    }

    fn decode(src: &mut ReadCursor<'_>, version: u32) -> DecodeResult<Self> {
        ensure_size!(in: src, size: Self::SIZE);
        let os_type = src.read_u32();
        let os_version = src.read_u32();
        let protocol_major_version = src.read_u16();
        let protocol_minor_version = src.read_u16();
        let io_code_1 = IoCode1::from_bits_retain(src.read_u32());
        let io_code_2 = src.read_u32();
        let extended_pdu = ExtendedPdu::from_bits_retain(src.read_u32());
        let extra_flags_1 = ExtraFlags1::from_bits_retain(src.read_u32());
        let extra_flags_2 = src.read_u32();
        let special_type_device_cap = if version == GENERAL_CAPABILITY_VERSION_02 {
            src.read_u32()
        } else {
            0
        };

        Ok(Self {
            os_type,
            os_version,
            protocol_major_version,
            protocol_minor_version,
            io_code_1,
            io_code_2,
            extended_pdu,
            extra_flags_1,
            extra_flags_2,
            special_type_device_cap,
        })
    }
}

bitflags! {
    /// A 32-bit unsigned integer that identifies a bitmask of the supported I/O requests for the given device.
    /// If the bit is set, the I/O request is allowed. The requests are identified by the MajorFunction field
    /// in the Device I/O Request (section 2.2.1.4) header. This field MUST be set to a valid combination of
    /// the following values.
    #[derive(Debug, PartialEq, Clone, Copy)]
    struct IoCode1: u32 {
        /// Unused, always set.
        const RDPDR_IRP_MJ_CREATE = 0x0000_0001;
        /// Unused, always set.
        const RDPDR_IRP_MJ_CLEANUP = 0x0000_0002;
        /// Unused, always set.
        const RDPDR_IRP_MJ_CLOSE = 0x0000_0004;
        /// Unused, always set.
        const RDPDR_IRP_MJ_READ = 0x0000_0008;
        /// Unused, always set.
        const RDPDR_IRP_MJ_WRITE = 0x0000_0010;
        /// Unused, always set.
        const RDPDR_IRP_MJ_FLUSH_BUFFERS = 0x0000_0020;
        /// Unused, always set.
        const RDPDR_IRP_MJ_SHUTDOWN = 0x0000_0040;
        /// Unused, always set.
        const RDPDR_IRP_MJ_DEVICE_CONTROL = 0x0000_0080;
        /// Unused, always set.
        const RDPDR_IRP_MJ_QUERY_VOLUME_INFORMATION = 0x0000_0100;
        /// Unused, always set.
        const RDPDR_IRP_MJ_SET_VOLUME_INFORMATION = 0x0000_0200;
        /// Unused, always set.
        const RDPDR_IRP_MJ_QUERY_INFORMATION = 0x0000_0400;
        /// Unused, always set.
        const RDPDR_IRP_MJ_SET_INFORMATION = 0x0000_0800;
        /// Unused, always set.
        const RDPDR_IRP_MJ_DIRECTORY_CONTROL = 0x0000_1000;
        /// Unused, always set.
        const RDPDR_IRP_MJ_LOCK_CONTROL = 0x0000_2000;
        /// Enable Query Security requests (IRP_MJ_QUERY_SECURITY).
        const RDPDR_IRP_MJ_QUERY_SECURITY = 0x0000_4000;
        /// Enable Set Security requests (IRP_MJ_SET_SECURITY).
        const RDPDR_IRP_MJ_SET_SECURITY = 0x0000_8000;

        /// Combination of all the required bits.
        const REQUIRED = Self::RDPDR_IRP_MJ_CREATE.bits()
            | Self::RDPDR_IRP_MJ_CLEANUP.bits()
            | Self::RDPDR_IRP_MJ_CLOSE.bits()
            | Self::RDPDR_IRP_MJ_READ.bits()
            | Self::RDPDR_IRP_MJ_WRITE.bits()
            | Self::RDPDR_IRP_MJ_FLUSH_BUFFERS.bits()
            | Self::RDPDR_IRP_MJ_SHUTDOWN.bits()
            | Self::RDPDR_IRP_MJ_DEVICE_CONTROL.bits()
            | Self::RDPDR_IRP_MJ_QUERY_VOLUME_INFORMATION.bits()
            | Self::RDPDR_IRP_MJ_SET_VOLUME_INFORMATION.bits()
            | Self::RDPDR_IRP_MJ_QUERY_INFORMATION.bits()
            | Self::RDPDR_IRP_MJ_SET_INFORMATION.bits()
            | Self::RDPDR_IRP_MJ_DIRECTORY_CONTROL.bits()
            | Self::RDPDR_IRP_MJ_LOCK_CONTROL.bits();

    }
}

bitflags! {
    /// A 32-bit unsigned integer that specifies extended PDU flags.
    /// This field MUST be set as a bitmask of the following values.
    #[derive(Debug, PartialEq, Clone, Copy)]
    struct ExtendedPdu: u32 {
        /// Allow the client to send Client Drive Device List Remove packets.
        const RDPDR_DEVICE_REMOVE_PDUS = 0x0000_0001;
        /// Unused, always set.
        const RDPDR_CLIENT_DISPLAY_NAME_PDU = 0x0000_0002;
        /// Allow the server to send a Server User Logged On packet.
        const RDPDR_USER_LOGGEDON_PDU = 0x0000_0004;
    }
}

bitflags! {
    /// A 32-bit unsigned integer that specifies extended flags.
    /// The extraFlags1 field MUST be set as a bitmask of the following value.
    #[derive(Debug, PartialEq, Clone, Copy)]
    struct ExtraFlags1: u32 {
        /// Optionally present only in the Client Core Capability Response.
        /// Allows the server to send multiple simultaneous read or write requests
        /// on the same file from a redirected file system.
        const ENABLE_ASYNCIO = 0x0000_0001;
    }
}

/// From VersionMinor in [Server Client ID Confirm (section 2.2.2.6)], [2.2.2.3 Client Announce Reply (DR_CORE_CLIENT_ANNOUNCE_RSP)]
///
/// VERSION_MINOR_12 is what Teleport has successfully been using.
/// There is a version 13 as well, but it's not clear to me what
/// the difference is.
///
/// [Server Client ID Confirm (section 2.2.2.6)]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/bbbb9666-6994-4cf6-8e65-0d46eb319c6e
/// [2.2.2.3]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/d6fe6d1b-c145-4a6f-99aa-4fe3cdcea398
pub const VERSION_MINOR_12: u16 = 0x000C;
pub const VERSION_MAJOR: u16 = 0x0001;

/// [2.2.2.9] Client Device List Announce Request (DR_CORE_DEVICELIST_ANNOUNCE_REQ)
/// and [2.2.3.1] Client Device List Announce (DR_DEVICELIST_ANNOUNCE)
///
/// [2.2.2.9]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/10ef9ada-cba2-4384-ab60-7b6290ed4a9a
/// [2.2.3.1]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/d8b2bc1c-0207-4c15-abe3-62eaa2afcaf1
#[derive(Debug, PartialEq, Clone)]
pub struct ClientDeviceListAnnounce {
    pub device_list: Vec<DeviceAnnounceHeader>,
}

impl ClientDeviceListAnnounce {
    const FIXED_PART_SIZE: usize = size_of::<u32>(); // DeviceCount

    /// Library users should not typically call this directly, use [`Rdpdr::add_drive`] instead.
    pub(crate) fn new_drive(device_id: u32, name: String) -> Self {
        Self {
            device_list: vec![DeviceAnnounceHeader::new_drive(device_id, name)],
        }
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        dst.write_u32(cast_length!(
            "ClientDeviceListAnnounce",
            "DeviceCount",
            self.device_list.len()
        )?);

        for dev in self.device_list.iter() {
            dev.encode(dst)?;
        }

        Ok(())
    }

    pub fn name(&self) -> &'static str {
        "DR_CORE_DEVICELIST_ANNOUNCE_REQ"
    }

    pub fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + self.device_list.iter().map(|d| d.size()).sum::<usize>()
    }
}

#[derive(Debug, PartialEq, Clone)]
pub struct Devices(Vec<DeviceAnnounceHeader>);

impl Devices {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn add_smartcard(&mut self, device_id: u32) {
        self.push(DeviceAnnounceHeader::new_smartcard(device_id));
    }

    pub fn add_drive(&mut self, device_id: u32, name: String) {
        self.push(DeviceAnnounceHeader::new_drive(device_id, name));
    }

    /// Returns the [`DeviceType`] for the given device ID.
    pub fn for_device_type(&self, device_id: u32) -> DecodeResult<DeviceType> {
        if let Some(device_type) = self.0.iter().find(|d| d.device_id == device_id).map(|d| d.device_type) {
            Ok(device_type)
        } else {
            Err(invalid_field_err!(
                "Devices::for_device_type",
                "device_id",
                "no device with that ID"
            ))
        }
    }

    fn push(&mut self, device: DeviceAnnounceHeader) {
        self.0.push(device);
    }

    pub fn clone_inner(&mut self) -> Vec<DeviceAnnounceHeader> {
        self.0.clone()
    }
}

impl Default for Devices {
    fn default() -> Self {
        Self::new()
    }
}

/// [2.2.1.3] Device Announce Header (DEVICE_ANNOUNCE)
///
/// [2.2.1.3]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/32e34332-774b-4ead-8c9d-5d64720d6bf9
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceAnnounceHeader {
    device_type: DeviceType,
    device_id: u32,
    preferred_dos_name: PreferredDosName,
    device_data: Vec<u8>,
}

impl DeviceAnnounceHeader {
    const FIXED_PART_SIZE: usize = size_of::<u32>() * 3 + 8; // DeviceType, DeviceId, DeviceDataLength, PreferredDosName

    pub fn new_smartcard(device_id: u32) -> Self {
        Self {
            device_type: DeviceType::Smartcard,
            device_id,
            // This name is a constant defined by the spec.
            preferred_dos_name: PreferredDosName("SCARD".to_owned()),
            device_data: Vec::new(),
        }
    }

    fn new_drive(device_id: u32, name: String) -> Self {
        // The spec says Unicode but empirically this wants null terminated UTF-8.
        let mut device_data = name.into_bytes();
        device_data.push(0u8);

        Self {
            device_type: DeviceType::Filesystem,
            device_id,
            // "The drive name MUST be specified in the PreferredDosName field; however, if the drive name is larger than the allocated size of the PreferredDosName field,
            // then the drive name MUST be truncated to fit. If the client supports DRIVE_CAPABILITY_VERSION_02 in the Drive Capability Set, then the full name MUST also
            // be specified in the DeviceData field, as a null-terminated Unicode string. If the DeviceDataLength field is nonzero, the content of the PreferredDosName field
            // is ignored."
            //
            // Since we do support DRIVE_CAPABILITY_VERSION_02, we'll put the full name in the DeviceData field.
            preferred_dos_name: PreferredDosName("ignored".to_owned()),
            device_data,
        }
    }

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        dst.write_u32(self.device_type.into());
        dst.write_u32(self.device_id);
        self.preferred_dos_name.encode(dst)?;
        dst.write_u32(cast_length!(
            "DeviceAnnounceHeader",
            "DeviceDataLength",
            self.device_data.len()
        )?);
        dst.write_slice(&self.device_data);
        Ok(())
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + self.device_data.len()
    }
}

/// From ["PreferredDosName"](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/32e34332-774b-4ead-8c9d-5d64720d6bf9):
///
/// PreferredDosName (8 bytes): A string of ASCII characters (with a maximum length of eight characters) that represents the name of the device as it appears on the client. This field MUST be null-terminated, so the maximum device name is 7 characters long. The following characters are considered invalid for the PreferredDosName field:
///
/// <, >, ", /, \, |
///
/// If any of these characters are present, the DR_CORE_DEVICE_ANNOUNC_RSP packet for this device (section 2.2.2.1) will be sent with STATUS_ACCESS_DENIED set in the ResultCode field.
///
/// If DeviceType is set to RDPDR_DTYP_SMARTCARD, the PreferredDosName MUST be set to "SCARD".
///
/// Note A column character, ":", is valid only when present at the end of the PreferredDosName field, otherwise it is also considered invalid.
#[derive(Debug, PartialEq, Clone)]
struct PreferredDosName(String);

impl PreferredDosName {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        write_string_to_cursor(dst, &self.format(), CharacterSet::Ansi, false)
    }

    /// Returns the underlying String with a maximum length of 7 characters plus a null terminator.
    fn format(&self) -> String {
        let mut name: &str = &self.0;
        if name.len() > 7 {
            name = &name[..7];
        }
        format!("{name:\x00<8}")
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u32)]
pub enum DeviceType {
    /// RDPDR_DTYP_SERIAL
    Serial = 0x0000_0001,
    /// RDPDR_DTYP_PARALLEL
    Parallel = 0x0000_0002,
    /// RDPDR_DTYP_PRINT
    Print = 0x0000_0004,
    /// RDPDR_DTYP_FILESYSTEM
    Filesystem = 0x0000_0008,
    /// RDPDR_DTYP_SMARTCARD
    Smartcard = 0x0000_0020,
}

impl From<DeviceType> for u32 {
    fn from(device_type: DeviceType) -> Self {
        device_type as u32
    }
}

impl TryFrom<u32> for DeviceType {
    type Error = DecodeError;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            0x0000_0001 => Ok(DeviceType::Serial),
            0x0000_0002 => Ok(DeviceType::Parallel),
            0x0000_0004 => Ok(DeviceType::Print),
            0x0000_0008 => Ok(DeviceType::Filesystem),
            0x0000_0020 => Ok(DeviceType::Smartcard),
            _ => Err(invalid_field_err!("try_from", "DeviceType", "invalid value")),
        }
    }
}

/// [2.2.2.1] Server Device Announce Response (DR_CORE_DEVICE_ANNOUNCE_RSP)
///
/// [2.2.2.1]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/a4c0b619-6e87-4721-bdc4-5d2db7f485f3
#[derive(Debug, PartialEq, Clone)]
pub struct ServerDeviceAnnounceResponse {
    pub device_id: u32,
    pub result_code: NtStatus,
}

impl ServerDeviceAnnounceResponse {
    const NAME: &'static str = "DR_CORE_DEVICE_ANNOUNCE_RSP";
    const FIXED_PART_SIZE: usize = size_of::<u32>() * 2; // DeviceId, ResultCode

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.device_id);
        dst.write_u32(self.result_code.into());
        Ok(())
    }

    pub fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(ctx: Self::NAME, in: src, size: Self::FIXED_PART_SIZE);
        let device_id = src.read_u32();
        let result_code = NtStatus::from(src.read_u32());

        Ok(Self { device_id, result_code })
    }

    pub fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

/// [2.3.1] NTSTATUS Values
///
/// Windows defines an absolutely massive list of potential NTSTATUS values.
/// This enum includes some basic ones for communicating with the RDP server.
///
/// [2.3.1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct NtStatus(u32);

impl NtStatus {
    /// STATUS_SUCCESS
    pub const SUCCESS: Self = Self(0x0000_0000);
    /// STATUS_UNSUCCESSFUL
    pub const UNSUCCESSFUL: Self = Self(0xC000_0001);
    /// STATUS_NOT_IMPLEMENTED
    pub const NOT_IMPLEMENTED: Self = Self(0xC000_0002);
    /// STATUS_NO_MORE_FILES
    pub const NO_MORE_FILES: Self = Self(0x8000_0006);
    /// STATUS_OBJECT_NAME_COLLISION
    pub const OBJECT_NAME_COLLISION: Self = Self(0xC000_0035);
    /// STATUS_ACCESS_DENIED
    pub const ACCESS_DENIED: Self = Self(0xC000_0022);
    /// STATUS_NOT_A_DIRECTORY
    pub const NOT_A_DIRECTORY: Self = Self(0xC000_0103);
    /// STATUS_NO_SUCH_FILE
    pub const NO_SUCH_FILE: Self = Self(0xC000_000F);
    /// STATUS_NOT_SUPPORTED
    pub const NOT_SUPPORTED: Self = Self(0xC000_00BB);
    /// STATUS_DIRECTORY_NOT_EMPTY
    pub const DIRECTORY_NOT_EMPTY: Self = Self(0xC000_0101);
}

impl Debug for NtStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            NtStatus::SUCCESS => write!(f, "STATUS_SUCCESS"),
            NtStatus::UNSUCCESSFUL => write!(f, "STATUS_UNSUCCESSFUL"),
            NtStatus::NOT_IMPLEMENTED => write!(f, "STATUS_NOT_IMPLEMENTED"),
            NtStatus::NO_MORE_FILES => write!(f, "STATUS_NO_MORE_FILES"),
            NtStatus::OBJECT_NAME_COLLISION => write!(f, "STATUS_OBJECT_NAME_COLLISION"),
            NtStatus::ACCESS_DENIED => write!(f, "STATUS_ACCESS_DENIED"),
            NtStatus::NOT_A_DIRECTORY => write!(f, "STATUS_NOT_A_DIRECTORY"),
            NtStatus::NO_SUCH_FILE => write!(f, "STATUS_NO_SUCH_FILE"),
            NtStatus::NOT_SUPPORTED => write!(f, "STATUS_NOT_SUPPORTED"),
            NtStatus::DIRECTORY_NOT_EMPTY => write!(f, "STATUS_DIRECTORY_NOT_EMPTY"),
            _ => write!(f, "NtStatus({:#010X})", self.0),
        }
    }
}

impl From<u32> for NtStatus {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl From<NtStatus> for u32 {
    fn from(status: NtStatus) -> Self {
        status.0
    }
}

/// [2.2.1.4] Device I/O Request (DR_DEVICE_IOREQUEST)
///
/// [2.2.1.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/a087ffa8-d0d5-4874-ac7b-0494f63e2d5d
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceIoRequest {
    pub device_id: u32,
    pub file_id: u32,
    pub completion_id: u32,
    pub major_function: MajorFunction,
    pub minor_function: MinorFunction,
}

impl DeviceIoRequest {
    const NAME: &'static str = "DR_DEVICE_IOREQUEST";
    const FIXED_PART_SIZE: usize = size_of::<u32>() * 5; // DeviceId, FileId, CompletionId, MajorFunction, MinorFunction

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.device_id);
        dst.write_u32(self.file_id);
        dst.write_u32(self.completion_id);
        dst.write_u32(self.major_function.into());
        dst.write_u32(self.minor_function.into());
        Ok(())
    }

    pub fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(ctx: Self::NAME, in: src, size: Self::FIXED_PART_SIZE);
        let device_id = src.read_u32();
        let file_id = src.read_u32();
        let completion_id = src.read_u32();
        let major_function = MajorFunction::try_from(src.read_u32())?;
        let minor_function = MinorFunction::from(src.read_u32());

        Ok(Self {
            device_id,
            file_id,
            completion_id,
            major_function,
            minor_function,
        })
    }

    pub fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

/// See [`DeviceIoRequest`].
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(u32)]
pub enum MajorFunction {
    /// IRP_MJ_CREATE
    Create = 0x0000_0000,
    /// IRP_MJ_CLOSE
    Close = 0x0000_0002,
    /// IRP_MJ_READ
    Read = 0x0000_0003,
    /// IRP_MJ_WRITE
    Write = 0x0000_0004,
    /// IRP_MJ_DEVICE_CONTROL
    DeviceControl = 0x0000_000e,
    /// IRP_MJ_QUERY_VOLUME_INFORMATION
    QueryVolumeInformation = 0x0000_000a,
    /// IRP_MJ_SET_VOLUME_INFORMATION
    SetVolumeInformation = 0x0000_000b,
    /// IRP_MJ_QUERY_INFORMATION
    QueryInformation = 0x0000_0005,
    /// IRP_MJ_SET_INFORMATION
    SetInformation = 0x0000_0006,
    /// IRP_MJ_DIRECTORY_CONTROL
    DirectoryControl = 0x0000_000c,
    /// IRP_MJ_LOCK_CONTROL
    LockControl = 0x0000_0011,
}

impl TryFrom<u32> for MajorFunction {
    type Error = DecodeError;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        match value {
            0x0000_0000 => Ok(MajorFunction::Create),
            0x0000_0002 => Ok(MajorFunction::Close),
            0x0000_0003 => Ok(MajorFunction::Read),
            0x0000_0004 => Ok(MajorFunction::Write),
            0x0000_000e => Ok(MajorFunction::DeviceControl),
            0x0000_000a => Ok(MajorFunction::QueryVolumeInformation),
            0x0000_000b => Ok(MajorFunction::SetVolumeInformation),
            0x0000_0005 => Ok(MajorFunction::QueryInformation),
            0x0000_0006 => Ok(MajorFunction::SetInformation),
            0x0000_000c => Ok(MajorFunction::DirectoryControl),
            0x0000_0011 => Ok(MajorFunction::LockControl),
            _ => Err(invalid_field_err!("try_from", "MajorFunction", "unsupported value")),
        }
    }
}

impl From<MajorFunction> for u32 {
    fn from(major_function: MajorFunction) -> Self {
        major_function as u32
    }
}

#[derive(Clone, Copy, PartialEq, Eq)]
/// A 32-bit unsigned integer. This field is valid only when the MajorFunction field is
/// set to IRP_MJ_DIRECTORY_CONTROL. If the MajorFunction field is set to another value,
/// the MinorFunction field value SHOULD be 0x00000000; otherwise, the MinorFunction
/// field MUST have one of the following values:
///
/// 1. [`MinorFunction::IRP_MN_QUERY_DIRECTORY`]
/// 2. [`MinorFunction::IRP_MN_NOTIFY_CHANGE_DIRECTORY`]
pub struct MinorFunction(u32);

impl MinorFunction {
    pub const IRP_MN_QUERY_DIRECTORY: Self = Self(0x00000001);
    pub const IRP_MN_NOTIFY_CHANGE_DIRECTORY: Self = Self(0x00000002);
}

impl Debug for MinorFunction {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            MinorFunction::IRP_MN_QUERY_DIRECTORY => write!(f, "IRP_MN_QUERY_DIRECTORY"),
            MinorFunction::IRP_MN_NOTIFY_CHANGE_DIRECTORY => write!(f, "IRP_MN_NOTIFY_CHANGE_DIRECTORY"),
            _ => write!(f, "MinorFunction({:#010X})", self.0),
        }
    }
}

impl From<u32> for MinorFunction {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl From<MinorFunction> for u32 {
    fn from(minor_function: MinorFunction) -> Self {
        minor_function.0
    }
}

impl From<MinorFunction> for u8 {
    fn from(minor_function: MinorFunction) -> Self {
        minor_function.0 as u8
    }
}

/// [2.2.1.4.5] Device Control Request (DR_CONTROL_REQ)
///
/// [2.2.1.4.5]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/30662c80-ec6e-4ed1-9004-2e6e367bb59f
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceControlRequest<T: IoCtlCode> {
    pub header: DeviceIoRequest,
    pub output_buffer_length: u32,
    pub input_buffer_length: u32,
    pub io_control_code: T,
}

impl<T: IoCtlCode> DeviceControlRequest<T>
where
    T::Error: ironrdp_error::Source,
{
    const HEADERLESS_SIZE: usize = 4 // OutputBufferLength
        + 4 // InputBufferLength
        + 4 // IoControlCode
        + 20; // Additional 20 bytes for padding

    pub fn decode(header: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(ctx: "DeviceControlRequest", in: src, size: Self::HEADERLESS_SIZE);
        let output_buffer_length = src.read_u32();
        let input_buffer_length = src.read_u32();
        let io_control_code = T::try_from(src.read_u32()).map_err(|e| {
            error!("Failed to parse IoCtlCode");
            invalid_field_err_with_source("DeviceControlRequest", "IoCtlCode", "invalid IoCtlCode", e)
        })?;

        // Padding (20 bytes): An array of 20 bytes. Reserved. This field can be set to any value and MUST be ignored.
        read_padding!(src, 20);

        Ok(Self {
            header,
            output_buffer_length,
            input_buffer_length,
            io_control_code,
        })
    }
}

/// A 32-bit unsigned integer. This field is specific to the redirected device.
pub trait IoCtlCode: TryFrom<u32> {}

/// An IoCtlCode that can be used when the IoCtlCode is not known
/// or not important.
#[derive(Debug, PartialEq, Clone)]
pub struct AnyIoCtlCode(pub u32);

impl TryFrom<u32> for AnyIoCtlCode {
    type Error = PduError;

    fn try_from(value: u32) -> Result<Self, Self::Error> {
        Ok(Self(value))
    }
}

impl IoCtlCode for AnyIoCtlCode {}

/// [2.2.1.5.5] Device Control Response (DR_CONTROL_RSP)
///
/// [2.2.1.5.5]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/a00fbce4-95bb-4e15-8182-be2b5ef9076a
#[derive(Debug)]
pub struct DeviceControlResponse {
    pub device_io_reply: DeviceIoResponse,
    /// A value of `None` represents an empty buffer,
    /// such as can be seen in FreeRDP [here].
    ///
    /// [here]: https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_main.c#L677-L684
    pub output_buffer: Option<Box<dyn rpce::Encode>>,
}

impl PartialEq for DeviceControlResponse {
    fn eq(&self, other: &Self) -> bool {
        if (self.device_io_reply != other.device_io_reply)
            || (self.output_buffer.is_some() != other.output_buffer.is_some())
        {
            return false;
        }

        // If both are `None`, they are equal.
        if self.output_buffer.is_none() && other.output_buffer.is_none() {
            return true;
        }

        // device_io_reply is equal and both output_buffers are Some

        // If the sizes are different, the buffers are not equal.
        let self_size = self.output_buffer.as_ref().unwrap().size();
        let other_size = other.output_buffer.as_ref().unwrap().size();
        if self_size != other_size {
            return false;
        }

        // Sizes are the same. Last check is to encode the output buffers and compare the encoded bytes directly.
        let mut self_buf = vec![0u8; self_size];
        let mut other_buf = vec![0u8; other_size];
        self.output_buffer
            .as_ref()
            .unwrap()
            .encode(&mut WriteCursor::new(self_buf.as_mut_slice()))
            .unwrap();
        other
            .output_buffer
            .as_ref()
            .unwrap()
            .encode(&mut WriteCursor::new(other_buf.as_mut_slice()))
            .unwrap();

        self_buf == other_buf
    }
}

impl DeviceControlResponse {
    const NAME: &'static str = "DR_CONTROL_RSP";

    /// A value of `None` for `output_buffer` represents an empty buffer,
    /// such as can be seen in FreeRDP [here].
    ///
    /// [here]: https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_main.c#L677-L684
    pub fn new<T: IoCtlCode>(
        req: DeviceControlRequest<T>,
        io_status: NtStatus,
        output_buffer: Option<Box<dyn rpce::Encode>>,
    ) -> Self {
        Self {
            device_io_reply: DeviceIoResponse {
                device_id: req.header.device_id,
                completion_id: req.header.completion_id,
                io_status,
            },
            output_buffer,
        }
    }

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_reply.encode(dst)?;
        if let Some(output_buffer) = &self.output_buffer {
            dst.write_u32(cast_length!(
                "DeviceControlResponse",
                "OutputBufferLength",
                output_buffer.size()
            )?);
            output_buffer.encode(dst)?;
        } else {
            dst.write_u32(0); // OutputBufferLength
        }

        Ok(())
    }

    pub fn size(&self) -> usize {
        self.device_io_reply.size() // DeviceIoResponse
            + 4 // OutputBufferLength
            + if let Some(output_buffer) = &self.output_buffer {
                output_buffer.size() // OutputBuffer
            } else {
                0 // OutputBuffer
            }
    }
}

/// [2.2.1.5] Device I/O Response (DR_DEVICE_IOCOMPLETION)
///
/// [2.2.1.5]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/1c412a84-0776-4984-b35c-3f0445fcae65
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceIoResponse {
    pub device_id: u32,
    pub completion_id: u32,
    pub io_status: NtStatus,
}

impl DeviceIoResponse {
    const FIXED_PART_SIZE: usize = size_of::<u32>() * 3; // DeviceId, CompletionId, IoStatus

    pub fn new(device_io_request: DeviceIoRequest, io_status: NtStatus) -> Self {
        Self {
            device_id: device_io_request.device_id,
            completion_id: device_io_request.completion_id,
            io_status,
        }
    }

    pub fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(ctx: "DeviceIoResponse", in: src, size: Self::FIXED_PART_SIZE);
        let device_id = src.read_u32();
        let completion_id = src.read_u32();
        let io_status = NtStatus::from(src.read_u32());

        Ok(Self {
            device_id,
            completion_id,
            io_status,
        })
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.device_id);
        dst.write_u32(self.completion_id);
        dst.write_u32(self.io_status.into());
        Ok(())
    }

    pub fn size(&self) -> usize {
        Self::FIXED_PART_SIZE
    }
}

/// [2.2.3.3] Server Drive I/O Request (DR_DRIVE_CORE_DEVICE_IOREQUEST)
///
/// [2.2.3.3]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/89bb51af-c54d-40fb-81c1-d1bb353c4536
#[derive(Debug, PartialEq, Clone)]
pub enum ServerDriveIoRequest {
    ServerCreateDriveRequest(DeviceCreateRequest),
    ServerDriveQueryInformationRequest(ServerDriveQueryInformationRequest),
    DeviceCloseRequest(DeviceCloseRequest),
    ServerDriveQueryDirectoryRequest(ServerDriveQueryDirectoryRequest),
    ServerDriveNotifyChangeDirectoryRequest(ServerDriveNotifyChangeDirectoryRequest),
    ServerDriveQueryVolumeInformationRequest(ServerDriveQueryVolumeInformationRequest),
    DeviceControlRequest(DeviceControlRequest<AnyIoCtlCode>),
    DeviceReadRequest(DeviceReadRequest),
    DeviceWriteRequest(DeviceWriteRequest),
    ServerDriveSetInformationRequest(ServerDriveSetInformationRequest),
    ServerDriveLockControlRequest(ServerDriveLockControlRequest),
}

impl ServerDriveIoRequest {
    pub fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        match dev_io_req.major_function {
            MajorFunction::Create => Ok(DeviceCreateRequest::decode(dev_io_req, src)?.into()),
            MajorFunction::Close => Ok(DeviceCloseRequest::decode(dev_io_req).into()),
            MajorFunction::Read => Ok(DeviceReadRequest::decode(dev_io_req, src)?.into()),
            MajorFunction::Write => Ok(DeviceWriteRequest::decode(dev_io_req, src)?.into()),
            MajorFunction::DeviceControl => Ok(DeviceControlRequest::<AnyIoCtlCode>::decode(dev_io_req, src)?.into()),
            MajorFunction::QueryVolumeInformation => {
                Ok(ServerDriveQueryVolumeInformationRequest::decode(dev_io_req, src)?.into())
            }
            MajorFunction::SetVolumeInformation => Err(unsupported_value_err!(
                "ServerDriveIoRequest::decode",
                "MajorFunction",
                "SetVolumeInformation".to_owned()
            )), // FreeRDP doesn't implement this
            MajorFunction::QueryInformation => Ok(ServerDriveQueryInformationRequest::decode(dev_io_req, src)?.into()),
            MajorFunction::SetInformation => Ok(ServerDriveSetInformationRequest::decode(dev_io_req, src)?.into()),
            MajorFunction::DirectoryControl => match dev_io_req.minor_function {
                MinorFunction::IRP_MN_QUERY_DIRECTORY => {
                    Ok(ServerDriveQueryDirectoryRequest::decode(dev_io_req, src)?.into())
                }
                MinorFunction::IRP_MN_NOTIFY_CHANGE_DIRECTORY => {
                    Ok(ServerDriveNotifyChangeDirectoryRequest::decode(dev_io_req, src)?.into())
                }
                // If MajorFunction is set to IRP_MJ_DIRECTORY_CONTROL and MinorFunction is set to any other value, we've encountered a server bug.
                _ => Err(invalid_field_err!(
                    "ServerDriveIoRequest::decode",
                    "MinorFunction",
                    "invalid value"
                )),
            },
            MajorFunction::LockControl => Ok(ServerDriveLockControlRequest::decode(dev_io_req, src)?.into()),
        }
    }
}

impl From<DeviceCreateRequest> for ServerDriveIoRequest {
    fn from(req: DeviceCreateRequest) -> Self {
        Self::ServerCreateDriveRequest(req)
    }
}

impl From<ServerDriveQueryInformationRequest> for ServerDriveIoRequest {
    fn from(req: ServerDriveQueryInformationRequest) -> Self {
        Self::ServerDriveQueryInformationRequest(req)
    }
}

impl From<DeviceCloseRequest> for ServerDriveIoRequest {
    fn from(req: DeviceCloseRequest) -> Self {
        Self::DeviceCloseRequest(req)
    }
}

impl From<ServerDriveQueryDirectoryRequest> for ServerDriveIoRequest {
    fn from(req: ServerDriveQueryDirectoryRequest) -> Self {
        Self::ServerDriveQueryDirectoryRequest(req)
    }
}

impl From<ServerDriveNotifyChangeDirectoryRequest> for ServerDriveIoRequest {
    fn from(req: ServerDriveNotifyChangeDirectoryRequest) -> Self {
        Self::ServerDriveNotifyChangeDirectoryRequest(req)
    }
}

impl From<ServerDriveQueryVolumeInformationRequest> for ServerDriveIoRequest {
    fn from(req: ServerDriveQueryVolumeInformationRequest) -> Self {
        Self::ServerDriveQueryVolumeInformationRequest(req)
    }
}

impl From<DeviceControlRequest<AnyIoCtlCode>> for ServerDriveIoRequest {
    fn from(req: DeviceControlRequest<AnyIoCtlCode>) -> Self {
        Self::DeviceControlRequest(req)
    }
}

impl From<DeviceReadRequest> for ServerDriveIoRequest {
    fn from(req: DeviceReadRequest) -> Self {
        Self::DeviceReadRequest(req)
    }
}

impl From<DeviceWriteRequest> for ServerDriveIoRequest {
    fn from(req: DeviceWriteRequest) -> Self {
        Self::DeviceWriteRequest(req)
    }
}

impl From<ServerDriveSetInformationRequest> for ServerDriveIoRequest {
    fn from(req: ServerDriveSetInformationRequest) -> Self {
        Self::ServerDriveSetInformationRequest(req)
    }
}

impl From<ServerDriveLockControlRequest> for ServerDriveIoRequest {
    fn from(req: ServerDriveLockControlRequest) -> Self {
        Self::ServerDriveLockControlRequest(req)
    }
}

/// [2.2.3.3.1] Server Create Drive Request (DR_DRIVE_CREATE_REQ)
/// and [2.2.1.4.1] Device Create Request (DR_CREATE_REQ)
///
/// [2.2.3.3.1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/95b16fd0-d530-407c-a310-adedc85e9897
/// [2.2.1.4.1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/5f71f6d2-d9ff-40c2-bdb5-a739447d3c3e
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceCreateRequest {
    /// The MajorFunction field in this header MUST be set to IRP_MJ_CREATE.
    pub device_io_request: DeviceIoRequest,
    pub desired_access: DesiredAccess,
    pub allocation_size: u64,
    pub file_attributes: FileAttributes,
    pub shared_access: SharedAccess,
    pub create_disposition: CreateDisposition,
    pub create_options: CreateOptions,
    pub path: String,
}

impl DeviceCreateRequest {
    const FIXED_PART_SIZE: usize = 4  // DesiredAccess
                                 + 8  // AllocationSize
                                 + 4  // FileAttributes
                                 + 4  // SharedAccess
                                 + 4  // CreateDisposition
                                 + 4  // CreateOptions
                                 + 4; // PathLength

    fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(ctx: "DeviceCreateRequest", in: src, size: Self::FIXED_PART_SIZE);
        let desired_access = DesiredAccess::from_bits_retain(src.read_u32());
        let allocation_size = src.read_u64();
        let file_attributes = FileAttributes::from_bits_retain(src.read_u32());
        let shared_access = SharedAccess::from_bits_retain(src.read_u32());
        let create_disposition = CreateDisposition::from_bits_retain(src.read_u32());
        let create_options = CreateOptions::from_bits_retain(src.read_u32());
        let path_length: usize = cast_length!("DeviceCreateRequest", "path_length", src.read_u32())?;

        ensure_size!(ctx: "DeviceCreateRequest", in: src, size: path_length);
        let path = from_utf16_bytes(src.read_slice(path_length))
            .trim_end_matches('\0')
            .into();

        Ok(Self {
            device_io_request: dev_io_req,
            desired_access,
            allocation_size,
            file_attributes,
            shared_access,
            create_disposition,
            create_options,
            path,
        })
    }
}

bitflags! {
    /// DesiredAccess can be interpreted as either
    /// [2.2.13.1.1] File_Pipe_Printer_Access_Mask \[MS-SMB2\] or [2.2.13.1.2] Directory_Access_Mask \[MS-SMB2\]
    ///
    /// This implements the combination of the two. For flags where the names and/or functions are distinct between the two,
    /// the names are appended with an "_OR_", and the File_Pipe_Printer_Access_Mask functionality is described on the top line comment,
    /// and the Directory_Access_Mask functionality is described on the bottom (2nd) line comment.
    ///
    /// [2.2.13.1.1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/77b36d0f-6016-458a-a7a0-0f4a72ae1534
    /// [2.2.13.1.2]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/0a5934b1-80f1-4da0-b1bf-5e021c309b71
    #[derive(Debug, PartialEq, Clone)]
    pub struct DesiredAccess: u32 {
        /// This value indicates the right to read data from the file or named pipe.
        ///
        /// This value indicates the right to enumerate the contents of the directory.
        const FILE_READ_DATA_OR_FILE_LIST_DIRECTORY = 0x00000001;
        /// This value indicates the right to write data into the file or named pipe beyond the end of the file.
        ///
        /// This value indicates the right to create a file under the directory.
        const FILE_WRITE_DATA_OR_FILE_ADD_FILE = 0x00000002;
        /// This value indicates the right to append data into the file or named pipe.
        ///
        /// This value indicates the right to add a sub-directory under the directory.
        const FILE_APPEND_DATA_OR_FILE_ADD_SUBDIRECTORY = 0x00000004;
        /// This value indicates the right to read the extended attributes of the file or named pipe.
        const FILE_READ_EA = 0x00000008;
        /// This value indicates the right to write or change the extended attributes to the file or named pipe.
        const FILE_WRITE_EA = 0x00000010;
        /// This value indicates the right to traverse this directory if the server enforces traversal checking.
        const FILE_TRAVERSE = 0x00000020;
        /// This value indicates the right to delete entries within a directory.
        const FILE_DELETE_CHILD = 0x00000040;
        /// This value indicates the right to execute the file/directory.
        const FILE_EXECUTE = 0x00000020;
        /// This value indicates the right to read the attributes of the file/directory.
        const FILE_READ_ATTRIBUTES = 0x00000080;
        /// This value indicates the right to change the attributes of the file/directory.
        const FILE_WRITE_ATTRIBUTES = 0x00000100;
        /// This value indicates the right to delete the file/directory.
        const DELETE = 0x00010000;
        /// This value indicates the right to read the security descriptor for the file/directory or named pipe.
        const READ_CONTROL = 0x00020000;
        /// This value indicates the right to change the discretionary access control list (DACL) in the security descriptor for the file/directory or named pipe. For the DACL data pub structure, see ACL in [MS-DTYP].
        const WRITE_DAC = 0x00040000;
        /// This value indicates the right to change the owner in the security descriptor for the file/directory or named pipe.
        const WRITE_OWNER = 0x00080000;
        /// SMB2 clients set this flag to any value. SMB2 servers SHOULD ignore this flag.
        const SYNCHRONIZE = 0x00100000;
        /// This value indicates the right to read or change the system access control list (SACL) in the security descriptor for the file/directory or named pipe. For the SACL data pub structure, see ACL in [MS-DTYP].
        const ACCESS_SYSTEM_SECURITY = 0x01000000;
        /// This value indicates that the client is requesting an open to the file with the highest level of access the client has on this file. If no access is granted for the client on this file, the server MUST fail the open with STATUS_ACCESS_DENIED.
        const MAXIMUM_ALLOWED = 0x02000000;
        /// This value indicates a request for all the access flags that are previously listed except MAXIMUM_ALLOWED and ACCESS_SYSTEM_SECURITY.
        const GENERIC_ALL = 0x10000000;
        /// This value indicates a request for the following combination of access flags listed above: FILE_READ_ATTRIBUTES| FILE_EXECUTE| SYNCHRONIZE| READ_CONTROL.
        const GENERIC_EXECUTE = 0x20000000;
        /// This value indicates a request for the following combination of access flags listed above: FILE_WRITE_DATA| FILE_APPEND_DATA| FILE_WRITE_ATTRIBUTES| FILE_WRITE_EA| SYNCHRONIZE| READ_CONTROL.
        const GENERIC_WRITE = 0x40000000;
        /// This value indicates a request for the following combination of access flags listed above: FILE_READ_DATA| FILE_READ_ATTRIBUTES| FILE_READ_EA| SYNCHRONIZE| READ_CONTROL.
        const GENERIC_READ = 0x80000000;
    }
}

bitflags! {
    /// [2.6] File Attributes \[MS-FSCC\]
    ///
    /// [2.6]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/ca28ec38-f155-4768-81d6-4bfeb8586fc9
    #[derive(Debug, PartialEq, Clone)]
    pub struct FileAttributes: u32 {
        const FILE_ATTRIBUTE_READONLY = 0x00000001;
        const FILE_ATTRIBUTE_HIDDEN = 0x00000002;
        const FILE_ATTRIBUTE_SYSTEM = 0x00000004;
        const FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
        const FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
        const FILE_ATTRIBUTE_NORMAL = 0x00000080;
        const FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
        const FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
        const FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;
        const FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
        const FILE_ATTRIBUTE_OFFLINE = 0x00001000;
        const FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
        const FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
        const FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000;
        const FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000;
        const FILE_ATTRIBUTE_RECALL_ON_OPEN = 0x00040000;
        const FILE_ATTRIBUTE_PINNED = 0x00080000;
        const FILE_ATTRIBUTE_UNPINNED = 0x00100000;
        const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS = 0x00400000;

        const _ = !0;
    }
}

bitflags! {
    /// Specified in [2.2.13] SMB2 CREATE Request
    ///
    /// [2.2.13]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/e8fb45c1-a03d-44ca-b7ae-47385cfd7997
    #[derive(Debug, PartialEq, Clone)]
    pub struct SharedAccess: u32 {
        const FILE_SHARE_READ = 0x00000001;
        const FILE_SHARE_WRITE = 0x00000002;
        const FILE_SHARE_DELETE = 0x00000004;
    }
}

bitflags! {
    /// Defined in [2.2.13] SMB2 CREATE Request
    ///
    /// See FreeRDP's [drive_file.c] for context about how these should be interpreted.
    ///
    /// [2.2.13]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/e8fb45c1-a03d-44ca-b7ae-47385cfd7997
    /// [drive_file.c]: https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L207
    #[derive(PartialEq, Eq, Debug, Clone)]
    pub struct CreateDisposition: u32 {
        const FILE_SUPERSEDE = 0x00000000;
        const FILE_OPEN = 0x00000001;
        const FILE_CREATE = 0x00000002;
        const FILE_OPEN_IF = 0x00000003;
        const FILE_OVERWRITE = 0x00000004;
        const FILE_OVERWRITE_IF = 0x00000005;
    }
}

bitflags! {
    /// Defined in [2.2.13] SMB2 CREATE Request
    ///
    /// [2.2.13]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-smb2/e8fb45c1-a03d-44ca-b7ae-47385cfd7997
    #[derive(Debug, PartialEq, Clone)]
    pub struct CreateOptions: u32 {
        const FILE_DIRECTORY_FILE = 0x00000001;
        const FILE_WRITE_THROUGH = 0x00000002;
        const FILE_SEQUENTIAL_ONLY = 0x00000004;
        const FILE_NO_INTERMEDIATE_BUFFERING = 0x00000008;
        const FILE_SYNCHRONOUS_IO_ALERT = 0x00000010;
        const FILE_SYNCHRONOUS_IO_NONALERT = 0x00000020;
        const FILE_NON_DIRECTORY_FILE = 0x00000040;
        const FILE_COMPLETE_IF_OPLOCKED = 0x00000100;
        const FILE_NO_EA_KNOWLEDGE = 0x00000200;
        const FILE_RANDOM_ACCESS = 0x00000800;
        const FILE_DELETE_ON_CLOSE = 0x00001000;
        const FILE_OPEN_BY_FILE_ID = 0x00002000;
        const FILE_OPEN_FOR_BACKUP_INTENT = 0x00004000;
        const FILE_NO_COMPRESSION = 0x00008000;
        const FILE_OPEN_REMOTE_INSTANCE = 0x00000400;
        const FILE_OPEN_REQUIRING_OPLOCK = 0x00010000;
        const FILE_DISALLOW_EXCLUSIVE = 0x00020000;
        const FILE_RESERVE_OPFILTER = 0x00100000;
        const FILE_OPEN_REPARSE_POINT = 0x00200000;
        const FILE_OPEN_NO_RECALL = 0x00400000;
        const FILE_OPEN_FOR_FREE_SPACE_QUERY = 0x00800000;
    }
}

/// [2.2.1.5.1] Device Create Response (DR_CREATE_RSP)
///
/// [2.2.1.5.1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/99e5fca5-b37a-41e4-bc69-8d7da7860f76
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceCreateResponse {
    pub device_io_reply: DeviceIoResponse,
    pub file_id: u32,
    pub information: Information,
}

impl DeviceCreateResponse {
    const NAME: &'static str = "DR_CREATE_RSP";

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_reply.encode(dst)?;
        dst.write_u32(self.file_id);
        dst.write_u8(self.information.bits());
        Ok(())
    }

    pub fn size(&self) -> usize {
        self.device_io_reply.size() // DeviceIoReply
        + 4 // FileId
        + 1 // Information
    }
}

bitflags! {
    /// Defined in [2.2.1.5.1] Device Create Response (DR_CREATE_RSP)
    ///
    /// [2.2.1.5.1]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/99e5fca5-b37a-41e4-bc69-8d7da7860f76
    #[derive(Debug, PartialEq, Clone)]
    pub struct Information: u8 {
        /// A new file was created.
        const FILE_SUPERSEDED = 0x00000000;
        /// An existing file was opened.
        const FILE_OPENED = 0x00000001;
        /// An existing file was overwritten.
        const FILE_OVERWRITTEN = 0x00000003;
    }
}

/// [2.2.3.3.8] Server Drive Query Information Request (DR_DRIVE_QUERY_INFORMATION_REQ)
///
/// Note that Length, Padding, and QueryBuffer fields are all ignored in keeping with the [analogous code in FreeRDP].
///
/// [2.2.3.3.8]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/e43dcd68-2980-40a9-9238-344b6cf94946
/// [analogous code in FreeRDP]: https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_main.c#L384
#[derive(Debug, PartialEq, Clone)]
pub struct ServerDriveQueryInformationRequest {
    pub device_io_request: DeviceIoRequest,
    pub file_info_class_lvl: FileInformationClassLevel,
}

impl ServerDriveQueryInformationRequest {
    pub fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(ctx: "ServerDriveQueryInformationRequest", in: src, size: 4);
        let file_info_class_lvl = FileInformationClassLevel::from(src.read_u32());

        Ok(Self {
            device_io_request: dev_io_req,
            file_info_class_lvl,
        })
    }
}

/// [2.4] File Information Classes \[MS-FSCC\]
///
/// [2.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/4718fc40-e539-4014-8e33-b675af74e3e1
#[derive(PartialEq, Eq, Clone)]
pub struct FileInformationClassLevel(u32);

impl FileInformationClassLevel {
    /// FileBasicInformation
    pub const FILE_BASIC_INFORMATION: Self = Self(4);
    /// FileStandardInformation
    pub const FILE_STANDARD_INFORMATION: Self = Self(5);
    /// FileAttributeTagInformation
    pub const FILE_ATTRIBUTE_TAG_INFORMATION: Self = Self(35);
    /// FileDirectoryInformation
    pub const FILE_DIRECTORY_INFORMATION: Self = Self(1);
    /// FileFullDirectoryInformation
    pub const FILE_FULL_DIRECTORY_INFORMATION: Self = Self(2);
    /// FileBothDirectoryInformation
    pub const FILE_BOTH_DIRECTORY_INFORMATION: Self = Self(3);
    /// FileNamesInformation
    pub const FILE_NAMES_INFORMATION: Self = Self(12);
    /// FileEndOfFileInformation
    pub const FILE_END_OF_FILE_INFORMATION: Self = Self(20);
    /// FileDispositionInformation
    pub const FILE_DISPOSITION_INFORMATION: Self = Self(13);
    /// FileRenameInformation
    pub const FILE_RENAME_INFORMATION: Self = Self(10);
    /// FileAllocationInformation
    pub const FILE_ALLOCATION_INFORMATION: Self = Self(19);
}

impl Display for FileInformationClassLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            FileInformationClassLevel::FILE_BASIC_INFORMATION => write!(f, "FileBasicInformation"),
            FileInformationClassLevel::FILE_STANDARD_INFORMATION => write!(f, "FileStandardInformation"),
            FileInformationClassLevel::FILE_ATTRIBUTE_TAG_INFORMATION => write!(f, "FileAttributeTagInformation"),
            FileInformationClassLevel::FILE_DIRECTORY_INFORMATION => write!(f, "FileDirectoryInformation"),
            FileInformationClassLevel::FILE_FULL_DIRECTORY_INFORMATION => write!(f, "FileFullDirectoryInformation"),
            FileInformationClassLevel::FILE_BOTH_DIRECTORY_INFORMATION => write!(f, "FileBothDirectoryInformation"),
            FileInformationClassLevel::FILE_NAMES_INFORMATION => write!(f, "FileNamesInformation"),
            FileInformationClassLevel::FILE_END_OF_FILE_INFORMATION => write!(f, "FileEndOfFileInformation"),
            FileInformationClassLevel::FILE_DISPOSITION_INFORMATION => write!(f, "FileDispositionInformation"),
            FileInformationClassLevel::FILE_RENAME_INFORMATION => write!(f, "FileRenameInformation"),
            FileInformationClassLevel::FILE_ALLOCATION_INFORMATION => write!(f, "FileAllocationInformation"),
            _ => write!(f, "FileInformationClassLevel({})", self.0),
        }
    }
}

impl Debug for FileInformationClassLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            FileInformationClassLevel::FILE_BASIC_INFORMATION => write!(f, "FileBasicInformation"),
            FileInformationClassLevel::FILE_STANDARD_INFORMATION => write!(f, "FileStandardInformation"),
            FileInformationClassLevel::FILE_ATTRIBUTE_TAG_INFORMATION => write!(f, "FileAttributeTagInformation"),
            FileInformationClassLevel::FILE_DIRECTORY_INFORMATION => write!(f, "FileDirectoryInformation"),
            FileInformationClassLevel::FILE_FULL_DIRECTORY_INFORMATION => write!(f, "FileFullDirectoryInformation"),
            FileInformationClassLevel::FILE_BOTH_DIRECTORY_INFORMATION => write!(f, "FileBothDirectoryInformation"),
            FileInformationClassLevel::FILE_NAMES_INFORMATION => write!(f, "FileNamesInformation"),
            _ => write!(f, "FileInformationClassLevel({})", self.0),
        }
    }
}

impl From<u32> for FileInformationClassLevel {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl From<FileInformationClassLevel> for u32 {
    fn from(file_info_class_lvl: FileInformationClassLevel) -> Self {
        file_info_class_lvl.0
    }
}

/// [2.2.3.4.8] Client Drive Query Information Response (DR_DRIVE_QUERY_INFORMATION_RSP)
///
/// [2.2.3.4.8]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/37ef4fb1-6a95-4200-9fbf-515464f034a4
#[derive(Debug, PartialEq, Clone)]
pub struct ClientDriveQueryInformationResponse {
    pub device_io_response: DeviceIoResponse,
    /// If [`Self::device_io_response`] has an `io_status` besides [`NtStatus::SUCCESS`],
    /// this field can be omitted (set to `None`).
    pub buffer: Option<FileInformationClass>,
}

impl ClientDriveQueryInformationResponse {
    const NAME: &'static str = "DR_DRIVE_QUERY_INFORMATION_RSP";

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_response.encode(dst)?;
        if let Some(buffer) = &self.buffer {
            dst.write_u32(cast_length!(
                "ClientDriveQueryInformationResponse",
                "buffer.size()",
                buffer.size()
            )?);
            buffer.encode(dst)?;
        } else {
            dst.write_u32(0); // Length = 0
        }
        Ok(())
    }

    pub fn size(&self) -> usize {
        self.device_io_response.size() // DeviceIoResponse
        + 4 // Length
        + if let Some(buffer) = &self.buffer {
            buffer.size() // Buffer
        } else {
            0
        }
    }
}

/// [2.4] File Information Classes \[MS-FSCC\]
///
/// [2.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/4718fc40-e539-4014-8e33-b675af74e3e1
#[derive(Debug, PartialEq, Clone)]
pub enum FileInformationClass {
    Basic(FileBasicInformation),
    Standard(FileStandardInformation),
    AttributeTag(FileAttributeTagInformation),
    BothDirectory(FileBothDirectoryInformation),
    FullDirectory(FileFullDirectoryInformation),
    Names(FileNamesInformation),
    Directory(FileDirectoryInformation),
    EndOfFile(FileEndOfFileInformation),
    Disposition(FileDispositionInformation),
    Rename(FileRenameInformation),
    Allocation(FileAllocationInformation),
}

impl FileInformationClass {
    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        match self {
            Self::Basic(f) => f.encode(dst),
            Self::Standard(f) => f.encode(dst),
            Self::AttributeTag(f) => f.encode(dst),
            Self::BothDirectory(f) => f.encode(dst),
            Self::FullDirectory(f) => f.encode(dst),
            Self::Names(f) => f.encode(dst),
            Self::Directory(f) => f.encode(dst),
            _ => Err(unsupported_value_err!(
                "FileInformationClass::encode",
                "FileInformationClass",
                self.to_string()
            )),
        }
    }

    pub fn decode(
        file_info_class_level: FileInformationClassLevel,
        length: usize,
        src: &mut ReadCursor<'_>,
    ) -> DecodeResult<Self> {
        match file_info_class_level {
            FileInformationClassLevel::FILE_BASIC_INFORMATION => Ok(FileBasicInformation::decode(src)?.into()),
            FileInformationClassLevel::FILE_END_OF_FILE_INFORMATION => {
                Ok(FileEndOfFileInformation::decode(src)?.into())
            }
            FileInformationClassLevel::FILE_DISPOSITION_INFORMATION => {
                Ok(FileDispositionInformation::decode(src, length)?.into())
            }
            FileInformationClassLevel::FILE_RENAME_INFORMATION => Ok(FileRenameInformation::decode(src)?.into()),
            FileInformationClassLevel::FILE_ALLOCATION_INFORMATION => {
                Ok(FileAllocationInformation::decode(src)?.into())
            }
            _ => Err(unsupported_value_err!(
                "FileInformationClass::decode",
                "FileInformationClassLevel",
                file_info_class_level.to_string()
            )),
        }
    }

    pub fn size(&self) -> usize {
        match self {
            Self::Basic(_) => FileBasicInformation::size(),
            Self::Standard(_) => FileStandardInformation::size(),
            Self::AttributeTag(_) => FileAttributeTagInformation::size(),
            Self::BothDirectory(f) => f.size(),
            Self::FullDirectory(f) => f.size(),
            Self::Names(f) => f.size(),
            Self::Directory(f) => f.size(),
            Self::EndOfFile(_) => FileEndOfFileInformation::size(),
            Self::Disposition(_) => FileDispositionInformation::size(),
            Self::Rename(f) => f.size(),
            Self::Allocation(_) => FileAllocationInformation::size(),
        }
    }
}

impl Display for FileInformationClass {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Basic(_) => write!(f, "FileBasicInformation"),
            Self::Standard(_) => write!(f, "FileStandardInformation"),
            Self::AttributeTag(_) => write!(f, "FileAttributeTagInformation"),
            Self::BothDirectory(_) => write!(f, "FileBothDirectoryInformation"),
            Self::FullDirectory(_) => write!(f, "FileFullDirectoryInformation"),
            Self::Names(_) => write!(f, "FileNamesInformation"),
            Self::Directory(_) => write!(f, "FileDirectoryInformation"),
            Self::EndOfFile(_) => write!(f, "FileEndOfFileInformation"),
            Self::Disposition(_) => write!(f, "FileDispositionInformation"),
            Self::Rename(_) => write!(f, "FileRenameInformation"),
            Self::Allocation(_) => write!(f, "FileAllocationInformation"),
        }
    }
}

impl From<FileBasicInformation> for FileInformationClass {
    fn from(f: FileBasicInformation) -> Self {
        Self::Basic(f)
    }
}

impl From<FileStandardInformation> for FileInformationClass {
    fn from(f: FileStandardInformation) -> Self {
        Self::Standard(f)
    }
}

impl From<FileAttributeTagInformation> for FileInformationClass {
    fn from(f: FileAttributeTagInformation) -> Self {
        Self::AttributeTag(f)
    }
}

impl From<FileBothDirectoryInformation> for FileInformationClass {
    fn from(f: FileBothDirectoryInformation) -> Self {
        Self::BothDirectory(f)
    }
}

impl From<FileFullDirectoryInformation> for FileInformationClass {
    fn from(f: FileFullDirectoryInformation) -> Self {
        Self::FullDirectory(f)
    }
}

impl From<FileNamesInformation> for FileInformationClass {
    fn from(f: FileNamesInformation) -> Self {
        Self::Names(f)
    }
}

impl From<FileDirectoryInformation> for FileInformationClass {
    fn from(f: FileDirectoryInformation) -> Self {
        Self::Directory(f)
    }
}

impl From<FileEndOfFileInformation> for FileInformationClass {
    fn from(f: FileEndOfFileInformation) -> Self {
        Self::EndOfFile(f)
    }
}

impl From<FileDispositionInformation> for FileInformationClass {
    fn from(f: FileDispositionInformation) -> Self {
        Self::Disposition(f)
    }
}

impl From<FileRenameInformation> for FileInformationClass {
    fn from(f: FileRenameInformation) -> Self {
        Self::Rename(f)
    }
}

impl From<FileAllocationInformation> for FileInformationClass {
    fn from(f: FileAllocationInformation) -> Self {
        Self::Allocation(f)
    }
}

/// [2.4.7] FileBasicInformation \[MS-FSCC\]
///
/// [2.4.7]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/16023025-8a78-492f-8b96-c873b042ac50
#[derive(Debug, PartialEq, Clone)]
pub struct FileBasicInformation {
    pub creation_time: i64,
    pub last_access_time: i64,
    pub last_write_time: i64,
    pub change_time: i64,
    pub file_attributes: FileAttributes,
    // NOTE: The `reserved` field in the spec MUST not be serialized and sent over RDP, or it will break the server implementation.
    // FreeRDP does the same: https://github.com/FreeRDP/FreeRDP/blob/1adb263813ca2e76a893ef729a04db8f94b5d757/channels/drive/client/drive_file.c#L508
}

impl FileBasicInformation {
    fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_size!(ctx: "FileBasicInformation", in: src, size: Self::size());
        let creation_time = src.read_i64();
        let last_access_time = src.read_i64();
        let last_write_time = src.read_i64();
        let change_time = src.read_i64();
        let file_attributes = FileAttributes::from_bits_retain(src.read_u32());
        Ok(Self {
            creation_time,
            last_access_time,
            last_write_time,
            change_time,
            file_attributes,
        })
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: Self::size());
        dst.write_i64(self.creation_time);
        dst.write_i64(self.last_access_time);
        dst.write_i64(self.last_write_time);
        dst.write_i64(self.change_time);
        dst.write_u32(self.file_attributes.bits());
        Ok(())
    }

    pub fn size() -> usize {
        8 // CreationTime
        + 8 // LastAccessTime
        + 8 // LastWriteTime
        + 8 // ChangeTime
        + 4 // FileAttributes
    }
}

/// [2.4.41] FileStandardInformation \[MS-FSCC\]
///
/// [2.4.41]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/5afa7f66-619c-48f3-955f-68c4ece704ae
#[derive(Debug, PartialEq, Clone)]
pub struct FileStandardInformation {
    pub allocation_size: i64,
    pub end_of_file: i64,
    pub number_of_links: u32,
    /// Set to TRUE to indicate that a file deletion has been requested; set to FALSE
    /// otherwise.
    pub delete_pending: Boolean,
    /// Set to TRUE to indicate that the file is a directory; set to FALSE otherwise.
    pub directory: Boolean,
    // NOTE: `reserved` field omitted.
}

impl FileStandardInformation {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: Self::size());
        dst.write_i64(self.allocation_size);
        dst.write_i64(self.end_of_file);
        dst.write_u32(self.number_of_links);
        dst.write_u8(self.delete_pending.into());
        dst.write_u8(self.directory.into());
        Ok(())
    }

    pub fn size() -> usize {
        8 // AllocationSize
        + 8 // EndOfFile
        + 4 // NumberOfLinks
        + 1 // DeletePending
        + 1 // Directory
    }
}

/// [2.1.8] Boolean
///
/// [2.1.8]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/8ce7b38c-d3cc-415d-ab39-944000ea77ff
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum Boolean {
    True = 1,
    False = 0,
}

impl From<Boolean> for u8 {
    fn from(boolean: Boolean) -> Self {
        match boolean {
            Boolean::True => 1,
            Boolean::False => 0,
        }
    }
}

impl From<u8> for Boolean {
    fn from(value: u8) -> Self {
        match value {
            1 => Boolean::True,
            _ => Boolean::False,
        }
    }
}

/// [2.4.6] FileAttributeTagInformation \[MS-FSCC\]
///
/// [2.4.6]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/d295752f-ce89-4b98-8553-266d37c84f0e?redirectedfrom=MSDN
#[derive(Debug, PartialEq, Clone)]
pub struct FileAttributeTagInformation {
    pub file_attributes: FileAttributes,
    pub reparse_tag: u32,
}

impl FileAttributeTagInformation {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: Self::size());
        dst.write_u32(self.file_attributes.bits());
        dst.write_u32(self.reparse_tag);
        Ok(())
    }

    fn size() -> usize {
        4 // FileAttributes
        + 4 // ReparseTag
    }
}

/// [2.4.8] FileBothDirectoryInformation \[MS-FSCC\]
///
/// [2.4.8]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/270df317-9ba5-4ccb-ba00-8d22be139bc5
#[derive(Debug, PartialEq, Clone)]
pub struct FileBothDirectoryInformation {
    pub next_entry_offset: u32,
    pub file_index: u32,
    pub creation_time: i64,
    pub last_access_time: i64,
    pub last_write_time: i64,
    pub change_time: i64,
    pub end_of_file: i64,
    pub allocation_size: i64,
    pub file_attributes: FileAttributes,
    pub ea_size: u32,
    pub short_name_length: i8,
    // reserved: u8: MUST NOT be added,
    // see https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L907
    pub short_name: [u8; 24], // 24 bytes
    pub file_name: String,
}

impl FileBothDirectoryInformation {
    pub fn new(
        creation_time: i64,
        last_access_time: i64,
        last_write_time: i64,
        change_time: i64,
        file_size: i64,
        file_attributes: FileAttributes,
        file_name: String,
    ) -> Self {
        // Default field values taken from
        // https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L871
        Self {
            next_entry_offset: 0,
            file_index: 0,
            creation_time,
            last_access_time,
            last_write_time,
            change_time,
            end_of_file: file_size,
            allocation_size: file_size,
            file_attributes,
            ea_size: 0,
            short_name_length: 0,
            short_name: [0; 24],
            file_name,
        }
    }

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.next_entry_offset);
        dst.write_u32(self.file_index);
        dst.write_i64(self.creation_time);
        dst.write_i64(self.last_access_time);
        dst.write_i64(self.last_write_time);
        dst.write_i64(self.change_time);
        dst.write_i64(self.end_of_file);
        dst.write_i64(self.allocation_size);
        dst.write_u32(self.file_attributes.bits());
        dst.write_u32(cast_length!(
            "FileBothDirectoryInformation::encode",
            "file_name_length",
            encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
        )?);
        dst.write_u32(self.ea_size);
        dst.write_i8(self.short_name_length);
        // reserved u8 MUST NOT be added,
        // see https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L907
        dst.write_slice(&self.short_name);
        write_string_to_cursor(dst, &self.file_name, CharacterSet::Unicode, false)?;
        Ok(())
    }

    fn size(&self) -> usize {
        4 // NextEntryOffset
        + 4 // FileIndex
        + 8 // CreationTime
        + 8 // LastAccessTime
        + 8 // LastWriteTime
        + 8 // ChangeTime
        + 8 // EndOfFile
        + 8 // AllocationSize
        + 4 // FileAttributes
        + 4 // FileNameLength
        + 4 // EaSize
        + 1 // ShortNameLength
        + 24 // ShortName
        + encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
    }
}

/// [2.4.14] FileFullDirectoryInformation \[MS-FSCC\]
///
/// [2.4.14]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/e8d926d1-3a22-4654-be9c-58317a85540b
#[derive(Debug, PartialEq, Clone)]
pub struct FileFullDirectoryInformation {
    pub next_entry_offset: u32,
    pub file_index: u32,
    pub creation_time: i64,
    pub last_access_time: i64,
    pub last_write_time: i64,
    pub change_time: i64,
    pub end_of_file: i64,
    pub allocation_size: i64,
    pub file_attributes: FileAttributes,
    pub ea_size: u32,
    pub file_name: String,
}

impl FileFullDirectoryInformation {
    pub fn new(
        creation_time: i64,
        last_access_time: i64,
        last_write_time: i64,
        change_time: i64,
        file_size: i64,
        file_attributes: FileAttributes,
        file_name: String,
    ) -> Self {
        // Default field values taken from
        // https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L871
        Self {
            next_entry_offset: 0,
            file_index: 0,
            creation_time,
            last_access_time,
            last_write_time,
            change_time,
            end_of_file: file_size,
            allocation_size: file_size,
            file_attributes,
            ea_size: 0,
            file_name,
        }
    }

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.next_entry_offset);
        dst.write_u32(self.file_index);
        dst.write_i64(self.creation_time);
        dst.write_i64(self.last_access_time);
        dst.write_i64(self.last_write_time);
        dst.write_i64(self.change_time);
        dst.write_i64(self.end_of_file);
        dst.write_i64(self.allocation_size);
        dst.write_u32(self.file_attributes.bits());
        dst.write_u32(cast_length!(
            "FileFullDirectoryInformation::encode",
            "file_name_length",
            encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
        )?);
        dst.write_u32(self.ea_size);
        write_string_to_cursor(dst, &self.file_name, CharacterSet::Unicode, false)?;
        Ok(())
    }

    fn size(&self) -> usize {
        4 // NextEntryOffset
        + 4 // FileIndex
        + 8 // CreationTime
        + 8 // LastAccessTime
        + 8 // LastWriteTime
        + 8 // ChangeTime
        + 8 // EndOfFile
        + 8 // AllocationSize
        + 4 // FileAttributes
        + 4 // FileNameLength
        + 4 // EaSize
        + encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
    }
}

/// [2.4.28] FileNamesInformation \[MS-FSCC\]
///
/// [2.4.28]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/a289f7a8-83d2-4927-8c88-b2d328dde5a5?redirectedfrom=MSDN
#[derive(Debug, PartialEq, Clone)]
pub struct FileNamesInformation {
    pub next_entry_offset: u32,
    pub file_index: u32,
    pub file_name: String,
}

impl FileNamesInformation {
    pub fn new(file_name: String) -> Self {
        // Default field values taken from
        // https://github.com/FreeRDP/FreeRDP/blob/dfa231c0a55b005af775b833f92f6bcd30363d77/channels/drive/client/drive_file.c#L912
        Self {
            next_entry_offset: 0,
            file_index: 0,
            file_name,
        }
    }

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.next_entry_offset);
        dst.write_u32(self.file_index);
        dst.write_u32(cast_length!(
            "FileNamesInformation::encode",
            "file_name_length",
            encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
        )?);
        write_string_to_cursor(dst, &self.file_name, CharacterSet::Unicode, false)?;
        Ok(())
    }

    fn size(&self) -> usize {
        4 // NextEntryOffset
        + 4 // FileIndex
        + 4 // FileNameLength
        + encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
    }
}

/// [2.4.10] FileDirectoryInformation \[MS-FSCC\]
///
/// [2.4.10]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/b38bf518-9057-4c88-9ddd-5e2d3976a64b
#[derive(Debug, PartialEq, Clone)]
pub struct FileDirectoryInformation {
    pub next_entry_offset: u32,
    pub file_index: u32,
    pub creation_time: i64,
    pub last_access_time: i64,
    pub last_write_time: i64,
    pub change_time: i64,
    pub end_of_file: i64,
    pub allocation_size: i64,
    pub file_attributes: FileAttributes,
    pub file_name: String,
}

impl FileDirectoryInformation {
    pub fn new(
        creation_time: i64,
        last_access_time: i64,
        last_write_time: i64,
        change_time: i64,
        file_size: i64,
        file_attributes: FileAttributes,
        file_name: String,
    ) -> Self {
        // Default field values taken from
        // https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L796
        Self {
            next_entry_offset: 0,
            file_index: 0,
            creation_time,
            last_access_time,
            last_write_time,
            change_time,
            end_of_file: file_size,
            allocation_size: file_size,
            file_attributes,
            file_name,
        }
    }

    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.next_entry_offset);
        dst.write_u32(self.file_index);
        dst.write_i64(self.creation_time);
        dst.write_i64(self.last_access_time);
        dst.write_i64(self.last_write_time);
        dst.write_i64(self.change_time);
        dst.write_i64(self.end_of_file);
        dst.write_i64(self.allocation_size);
        dst.write_u32(self.file_attributes.bits());
        dst.write_u32(cast_length!(
            "FileDirectoryInformation::encode",
            "file_name_length",
            encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
        )?);
        write_string_to_cursor(dst, &self.file_name, CharacterSet::Unicode, false)?;
        Ok(())
    }

    fn size(&self) -> usize {
        4 // NextEntryOffset
        + 4 // FileIndex
        + 8 // CreationTime
        + 8 // LastAccessTime
        + 8 // LastWriteTime
        + 8 // ChangeTime
        + 8 // EndOfFile
        + 8 // AllocationSize
        + 4 // FileAttributes
        + 4 // FileNameLength
        + encoded_str_len(&self.file_name, CharacterSet::Unicode, false)
    }
}

/// [2.2.1.4.2] Device Close Request (DR_CLOSE_REQ)
///
/// [2.2.1.4.2]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/3ec6627f-9e0f-4941-a828-3fc6ed63d9e7
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceCloseRequest {
    pub device_io_request: DeviceIoRequest,
    // Padding (32 bytes): ignored as per FreeRDP:
    // https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_main.c#L236
}

impl DeviceCloseRequest {
    pub fn decode(dev_io_req: DeviceIoRequest) -> Self {
        Self {
            device_io_request: dev_io_req,
        }
    }
}

/// [2.2.1.5.2] Device Close Response (DR_CLOSE_RSP)
///
/// [2.2.1.5.2]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/0dae7031-cfd8-4f14-908c-ec06e14997b5
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceCloseResponse {
    pub device_io_response: DeviceIoResponse,
    // Padding (4 bytes):  An array of 4 bytes. Reserved. This field can be set to any value and MUST be ignored.
}

impl DeviceCloseResponse {
    const NAME: &'static str = "DR_CLOSE_RSP";

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_response.encode(dst)?;
        dst.write_u32(0); // Padding
        Ok(())
    }

    pub fn size(&self) -> usize {
        self.device_io_response.size() // DeviceIoResponse
        + 4 // Padding
    }
}

/// [2.2.3.3.10] Server Drive Query Directory Request (DR_DRIVE_QUERY_DIRECTORY_REQ)
///
/// [2.2.3.3.10]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/458019d2-5d5a-4fd4-92ef-8c05f8d7acb1
#[derive(Debug, PartialEq, Clone)]
pub struct ServerDriveQueryDirectoryRequest {
    pub device_io_request: DeviceIoRequest,
    pub file_info_class_lvl: FileInformationClassLevel,
    pub initial_query: u8,
    pub path: String,
}

impl ServerDriveQueryDirectoryRequest {
    const FIXED_PART_SIZE: usize = 4 /* FsInformationClass */ + 1 /* InitialQuery */ + 4 /* PathLength */ + 23 /* Padding */;

    fn decode(device_io_request: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let file_info_class_lvl = FileInformationClassLevel::from(src.read_u32());

        // This field MUST contain one of the following values
        match file_info_class_lvl {
            FileInformationClassLevel::FILE_DIRECTORY_INFORMATION
            | FileInformationClassLevel::FILE_FULL_DIRECTORY_INFORMATION
            | FileInformationClassLevel::FILE_BOTH_DIRECTORY_INFORMATION
            | FileInformationClassLevel::FILE_NAMES_INFORMATION => {}
            _ => {
                return Err(invalid_field_err!(
                    "ServerDriveQueryDirectoryRequest::decode",
                    "file_info_class_lvl",
                    "received invalid level"
                ))
            }
        }

        let initial_query = src.read_u8();
        let path_length = cast_length!("ServerDriveQueryDirectoryRequest", "path_length", src.read_u32())?;
        // Padding (23 bytes): An array of 23 bytes. This field is unused and MUST be ignored.
        read_padding!(src, 23);

        ensure_size!(in: src, size: path_length);
        let path = decode_string(src.read_slice(path_length), CharacterSet::Unicode, true)?;

        Ok(Self {
            device_io_request,
            file_info_class_lvl,
            initial_query,
            path,
        })
    }
}

/// 2.2.3.3.11 Server Drive NotifyChange Directory Request (DR_DRIVE_NOTIFY_CHANGE_DIRECTORY_REQ)
///
/// [2.2.3.3.11]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/ed05e73d-e53e-4261-a1e1-365a70ba6512
#[derive(Debug, PartialEq, Clone)]
pub struct ServerDriveNotifyChangeDirectoryRequest {
    pub device_io_request: DeviceIoRequest,
    pub watch_tree: u8,
    pub completion_filter: u32,
}

impl ServerDriveNotifyChangeDirectoryRequest {
    const FIXED_PART_SIZE: usize = 1 /* WatchTree */ + 4 /* CompletionFilter */ + 27 /* Padding */;

    fn decode(device_io_request: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let watch_tree = src.read_u8();
        let completion_filter = src.read_u32();
        // Padding (27 bytes): An array of 27 bytes. This field is unused and MUST be ignored.
        read_padding!(src, 27);

        Ok(Self {
            device_io_request,
            watch_tree,
            completion_filter,
        })
    }
}

/// [2.2.3.4.10] Client Drive Query Directory Response (DR_DRIVE_QUERY_DIRECTORY_RSP)
///
/// [2.2.3.4.10]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/9c929407-a833-4893-8f20-90c984756140
#[derive(Debug, PartialEq, Clone)]
pub struct ClientDriveQueryDirectoryResponse {
    pub device_io_reply: DeviceIoResponse,
    pub buffer: Option<FileInformationClass>,
}

impl ClientDriveQueryDirectoryResponse {
    const NAME: &'static str = "DR_DRIVE_QUERY_DIRECTORY_RSP";

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_reply.encode(dst)?;
        dst.write_u32(cast_length!(
            "ClientDriveQueryDirectoryResponse",
            "length",
            if self.buffer.is_some() {
                self.buffer.as_ref().unwrap().size()
            } else {
                0
            }
        )?);
        if let Some(buffer) = &self.buffer {
            buffer.encode(dst)?;
        } else {
            write_padding!(dst, 1) // Padding: https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L937
        }
        Ok(())
    }

    pub fn size(&self) -> usize {
        self.device_io_reply.size() // DeviceIoResponse
        + 4 // Length
        + if let Some(buffer) = &self.buffer {
            buffer.size() // Buffer
        } else {
            1 // Padding: https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_file.c#L937
        }
    }
}

/// [2.2.3.3.6] Server Drive Query Volume Information Request
///
/// We only need to read the buffer up to the FileInformationClass to get the job done, so the rest of the fields in
/// this structure are discarded. See FreeRDP:
/// <https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_main.c#L464>
///
/// [2.2.3.3.6]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/484e622d-0e2b-423c-8461-7de38878effb
#[derive(Debug, PartialEq, Clone)]
pub struct ServerDriveQueryVolumeInformationRequest {
    pub device_io_request: DeviceIoRequest,
    pub fs_info_class_lvl: FileSystemInformationClassLevel,
}

impl ServerDriveQueryVolumeInformationRequest {
    const FIXED_PART_SIZE: usize = 4 /* FsInformationClass */ + 4 /* Length */ + 24 /* Padding */;

    pub fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let fs_info_class_lvl = FileSystemInformationClassLevel::from(src.read_u32());

        // This field MUST contain one of the following values.
        match fs_info_class_lvl {
            FileSystemInformationClassLevel::FILE_FS_VOLUME_INFORMATION
            | FileSystemInformationClassLevel::FILE_FS_SIZE_INFORMATION
            | FileSystemInformationClassLevel::FILE_FS_ATTRIBUTE_INFORMATION
            | FileSystemInformationClassLevel::FILE_FS_FULL_SIZE_INFORMATION
            | FileSystemInformationClassLevel::FILE_FS_DEVICE_INFORMATION => {}
            _ => {
                return Err(invalid_field_err!(
                    "ServerDriveQueryVolumeInformationRequest::decode",
                    "fs_info_class_lvl",
                    "received invalid level"
                ))
            }
        }

        // We only need to read the buffer up to the FileInformationClass to get the job done, so the rest of the fields in
        // this structure are discarded. See FreeRDP:
        // https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_main.c#L464
        let length = cast_length!("ServerDriveQueryVolumeInformationRequest", "length", src.read_u32())?; // Length
        read_padding!(src, 24); // Padding
        ensure_size!(in: src, size: length);
        read_padding!(src, length); // QueryVolumeBuffer

        Ok(Self {
            device_io_request: dev_io_req,
            fs_info_class_lvl,
        })
    }
}

/// [2.5] File System Information Classes [MS-FSCC]
///
/// [2.5] <https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/ee12042a-9352-46e3-9f67-c094b75fe6c3>
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct FileSystemInformationClassLevel(u32);

impl FileSystemInformationClassLevel {
    /// FileFsVolumeInformation
    pub const FILE_FS_VOLUME_INFORMATION: Self = Self(1);
    /// FileFsLabelInformation
    pub const FILE_FS_LABEL_INFORMATION: Self = Self(2);
    /// FileFsSizeInformation
    pub const FILE_FS_SIZE_INFORMATION: Self = Self(3);
    /// FileFsDeviceInformation
    pub const FILE_FS_DEVICE_INFORMATION: Self = Self(4);
    /// FileFsAttributeInformation
    pub const FILE_FS_ATTRIBUTE_INFORMATION: Self = Self(5);
    /// FileFsControlInformation
    pub const FILE_FS_CONTROL_INFORMATION: Self = Self(6);
    /// FileFsFullSizeInformation
    pub const FILE_FS_FULL_SIZE_INFORMATION: Self = Self(7);
    /// FileFsObjectIdInformation
    pub const FILE_FS_OBJECT_ID_INFORMATION: Self = Self(8);
    /// FileFsDriverPathInformation
    pub const FILE_FS_DRIVER_PATH_INFORMATION: Self = Self(9);
    /// FileFsVolumeFlagsInformation
    pub const FILE_FS_VOLUME_FLAGS_INFORMATION: Self = Self(10);
    /// FileFsSectorSizeInformation
    pub const FILE_FS_SECTOR_SIZE_INFORMATION: Self = Self(11);
}

impl From<u32> for FileSystemInformationClassLevel {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

/// [2.5] File System Information Classes
///
/// [2.5]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/ee12042a-9352-46e3-9f67-c094b75fe6c3
#[derive(Debug, PartialEq, Clone)]
pub enum FileSystemInformationClass {
    FileFsVolumeInformation(FileFsVolumeInformation),
    FileFsSizeInformation(FileFsSizeInformation),
    FileFsAttributeInformation(FileFsAttributeInformation),
    FileFsFullSizeInformation(FileFsFullSizeInformation),
    FileFsDeviceInformation(FileFsDeviceInformation),
}

impl FileSystemInformationClass {
    fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        match self {
            Self::FileFsVolumeInformation(f) => f.encode(dst),
            Self::FileFsSizeInformation(f) => f.encode(dst),
            Self::FileFsAttributeInformation(f) => f.encode(dst),
            Self::FileFsFullSizeInformation(f) => f.encode(dst),
            Self::FileFsDeviceInformation(f) => f.encode(dst),
        }
    }

    fn size(&self) -> usize {
        match self {
            Self::FileFsVolumeInformation(f) => f.size(),
            Self::FileFsSizeInformation(f) => f.size(),
            Self::FileFsAttributeInformation(f) => f.size(),
            Self::FileFsFullSizeInformation(_) => FileFsFullSizeInformation::size(),
            Self::FileFsDeviceInformation(_) => FileFsDeviceInformation::size(),
        }
    }
}

impl From<FileFsVolumeInformation> for FileSystemInformationClass {
    fn from(file_fs_vol_info: FileFsVolumeInformation) -> Self {
        Self::FileFsVolumeInformation(file_fs_vol_info)
    }
}

impl From<FileFsSizeInformation> for FileSystemInformationClass {
    fn from(file_fs_vol_info: FileFsSizeInformation) -> Self {
        Self::FileFsSizeInformation(file_fs_vol_info)
    }
}

impl From<FileFsAttributeInformation> for FileSystemInformationClass {
    fn from(file_fs_vol_info: FileFsAttributeInformation) -> Self {
        Self::FileFsAttributeInformation(file_fs_vol_info)
    }
}

impl From<FileFsFullSizeInformation> for FileSystemInformationClass {
    fn from(file_fs_vol_info: FileFsFullSizeInformation) -> Self {
        Self::FileFsFullSizeInformation(file_fs_vol_info)
    }
}

impl From<FileFsDeviceInformation> for FileSystemInformationClass {
    fn from(file_fs_vol_info: FileFsDeviceInformation) -> Self {
        Self::FileFsDeviceInformation(file_fs_vol_info)
    }
}

/// [2.5.9] FileFsVolumeInformation
///
/// [2.5.9]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/bf691378-c34e-4a13-976e-404ea1a87738
#[derive(Debug, PartialEq, Clone)]
pub struct FileFsVolumeInformation {
    pub volume_creation_time: i64,
    pub volume_serial_number: u32,
    pub supports_objects: Boolean,
    // reserved is omitted
    // https://github.com/FreeRDP/FreeRDP/blob/511444a65e7aa2f537c5e531fa68157a50c1bd4d/channels/drive/client/drive_main.c#L495
    pub volume_label: String,
}

impl FileFsVolumeInformation {
    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_i64(self.volume_creation_time);
        dst.write_u32(self.volume_serial_number);
        dst.write_u32(cast_length!(
            "FileFsVolumeInformation::encode",
            "volume_label_length",
            encoded_str_len(&self.volume_label, CharacterSet::Unicode, true)
        )?);
        dst.write_u8(self.supports_objects.into());
        write_string_to_cursor(dst, &self.volume_label, CharacterSet::Unicode, true)?;
        Ok(())
    }

    pub fn size(&self) -> usize {
        8 // VolumeCreationTime
        + 4 // VolumeSerialNumber
        + 4 // VolumeLabelLength
        + 1 // SupportsObjects
        + encoded_str_len(&self.volume_label, CharacterSet::Unicode, true)
    }
}

/// [2.5.8] FileFsSizeInformation
///
/// [2.5.8]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/e13e068c-e3a7-4dd4-94fd-3892b492e6e7
#[derive(Debug, PartialEq, Clone)]
pub struct FileFsSizeInformation {
    pub total_alloc_units: i64,
    pub available_alloc_units: i64,
    pub sectors_per_alloc_unit: u32,
    pub bytes_per_sector: u32,
}

impl FileFsSizeInformation {
    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_i64(self.total_alloc_units);
        dst.write_i64(self.available_alloc_units);
        dst.write_u32(self.sectors_per_alloc_unit);
        dst.write_u32(self.bytes_per_sector);
        Ok(())
    }

    pub fn size(&self) -> usize {
        8 // TotalAllocationUnits
        + 8 // AvailableAllocationUnits
        + 4 // SectorsPerAllocationUnit
        + 4 // BytesPerSector
    }
}

/// [2.5.1] FileFsAttributeInformation
///
/// [2.5.1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/ebc7e6e5-4650-4e54-b17c-cf60f6fbeeaa
#[derive(Debug, PartialEq, Clone)]
pub struct FileFsAttributeInformation {
    pub file_system_attributes: FileSystemAttributes,
    pub max_component_name_len: u32,
    pub file_system_name: String,
}

impl FileFsAttributeInformation {
    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        dst.write_u32(self.file_system_attributes.bits());
        dst.write_u32(self.max_component_name_len);
        dst.write_u32(cast_length!(
            "FileFsAttributeInformation::encode",
            "file_system_name_length",
            encoded_str_len(&self.file_system_name, CharacterSet::Unicode, true)
        )?);
        write_string_to_cursor(dst, &self.file_system_name, CharacterSet::Unicode, true)?;
        Ok(())
    }

    pub fn size(&self) -> usize {
        4 // FileSystemAttributes
        + 4 // MaximumComponentNameLength
        + 4 // FileSystemNameLength
        + encoded_str_len(&self.file_system_name, CharacterSet::Unicode, true)
    }
}

/// [2.5.4] FileFsFullSizeInformation
///
/// [2.5.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/63768db7-9012-4209-8cca-00781e7322f5
#[derive(Debug, PartialEq, Clone)]
pub struct FileFsFullSizeInformation {
    pub total_alloc_units: i64,
    pub caller_available_alloc_units: i64,
    pub actual_available_alloc_units: i64,
    pub sectors_per_alloc_unit: u32,
    pub bytes_per_sector: u32,
}

impl FileFsFullSizeInformation {
    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: Self::size());
        dst.write_i64(self.total_alloc_units);
        dst.write_i64(self.caller_available_alloc_units);
        dst.write_i64(self.actual_available_alloc_units);
        dst.write_u32(self.sectors_per_alloc_unit);
        dst.write_u32(self.bytes_per_sector);
        Ok(())
    }

    pub fn size() -> usize {
        8 // TotalAllocationUnits
        + 8 // CallerAvailableAllocationUnits
        + 8 // ActualAvailableAllocationUnits
        + 4 // SectorsPerAllocationUnit
        + 4 // BytesPerSector
    }
}

/// [2.5.10] FileFsDeviceInformation
///
/// [2.5.10]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/616b66d5-b335-4e1c-8f87-b4a55e8d3e4a
#[derive(Debug, PartialEq, Clone)]
pub struct FileFsDeviceInformation {
    pub device_type: u32,
    pub characteristics: Characteristics,
}

impl FileFsDeviceInformation {
    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: Self::size());
        dst.write_u32(self.device_type);
        dst.write_u32(self.characteristics.bits());
        Ok(())
    }

    pub fn size() -> usize {
        4 // DeviceType
        + 4 // Characteristics
    }
}

bitflags! {
    /// See [2.5.1] FileFsAttributeInformation.
    ///
    /// [2.5.1]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/ebc7e6e5-4650-4e54-b17c-cf60f6fbeeaa
    #[derive(Debug, PartialEq, Clone)]
    pub struct FileSystemAttributes: u32 {
        const FILE_SUPPORTS_USN_JOURNAL = 0x02000000;
        const FILE_SUPPORTS_OPEN_BY_FILE_ID = 0x01000000;
        const FILE_SUPPORTS_EXTENDED_ATTRIBUTES = 0x00800000;
        const FILE_SUPPORTS_HARD_LINKS = 0x00400000;
        const FILE_SUPPORTS_TRANSACTIONS = 0x00200000;
        const FILE_SEQUENTIAL_WRITE_ONCE = 0x00100000;
        const FILE_READ_ONLY_VOLUME = 0x00080000;
        const FILE_NAMED_STREAMS = 0x00040000;
        const FILE_SUPPORTS_ENCRYPTION = 0x00020000;
        const FILE_SUPPORTS_OBJECT_IDS = 0x00010000;
        const FILE_VOLUME_IS_COMPRESSED = 0x00008000;
        const FILE_SUPPORTS_REMOTE_STORAGE = 0x00000100;
        const FILE_SUPPORTS_REPARSE_POINTS = 0x00000080;
        const FILE_SUPPORTS_SPARSE_FILES = 0x00000040;
        const FILE_VOLUME_QUOTAS = 0x00000020;
        const FILE_FILE_COMPRESSION = 0x00000010;
        const FILE_PERSISTENT_ACLS = 0x00000008;
        const FILE_UNICODE_ON_DISK = 0x00000004;
        const FILE_CASE_PRESERVED_NAMES = 0x00000002;
        const FILE_CASE_SENSITIVE_SEARCH = 0x00000001;
        const FILE_SUPPORT_INTEGRITY_STREAMS = 0x04000000;
        const FILE_SUPPORTS_BLOCK_REFCOUNTING = 0x08000000;
        const FILE_SUPPORTS_SPARSE_VDL = 0x10000000;
    }
}

bitflags! {
    /// See [2.5.10] FileFsDeviceInformation.
    ///
    /// [2.5.10]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/616b66d5-b335-4e1c-8f87-b4a55e8d3e4a
    #[derive(Debug, PartialEq, Clone)]
    pub struct Characteristics: u32 {
        const FILE_REMOVABLE_MEDIA = 0x00000001;
        const FILE_READ_ONLY_DEVICE = 0x00000002;
        const FILE_FLOPPY_DISKETTE = 0x00000004;
        const FILE_WRITE_ONCE_MEDIA = 0x00000008;
        const FILE_REMOTE_DEVICE = 0x00000010;
        const FILE_DEVICE_IS_MOUNTED = 0x00000020;
        const FILE_VIRTUAL_VOLUME = 0x00000040;
        const FILE_DEVICE_SECURE_OPEN = 0x00000100;
        const FILE_CHARACTERISTIC_TS_DEVICE = 0x00001000;
        const FILE_CHARACTERISTIC_WEBDAV_DEVICE = 0x00002000;
        const FILE_DEVICE_ALLOW_APPCONTAINER_TRAVERSAL = 0x00020000;
        const FILE_PORTABLE_DEVICE = 0x0004000;
    }
}

/// [2.2.3.4.6] Client Drive Query Volume Information Response (DR_DRIVE_QUERY_VOLUME_INFORMATION_RSP)
///
/// [2.2.3.4.6]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/fbdc7db8-a268-4420-8b5e-ce689ad1d4ac
#[derive(Debug, PartialEq, Clone)]
pub struct ClientDriveQueryVolumeInformationResponse {
    pub device_io_reply: DeviceIoResponse,
    pub buffer: Option<FileSystemInformationClass>,
}

impl ClientDriveQueryVolumeInformationResponse {
    const NAME: &'static str = "DR_DRIVE_QUERY_VOLUME_INFORMATION_RSP";

    pub fn new(
        device_io_request: DeviceIoRequest,
        io_status: NtStatus,
        buffer: Option<FileSystemInformationClass>,
    ) -> Self {
        Self {
            device_io_reply: DeviceIoResponse::new(device_io_request, io_status),
            buffer,
        }
    }

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_reply.encode(dst)?;
        dst.write_u32(cast_length!(
            "ClientDriveQueryVolumeInformationResponse",
            "length",
            if self.buffer.is_some() {
                self.buffer.as_ref().unwrap().size()
            } else {
                0
            }
        )?);
        if let Some(buffer) = &self.buffer {
            buffer.encode(dst)?;
        }

        Ok(())
    }

    pub fn size(&self) -> usize {
        self.device_io_reply.size() // DeviceIoResponse
        + 4 // Length
        + if let Some(buffer) = &self.buffer {
            buffer.size() // Buffer
        } else {
            0
        }
    }
}

/// [2.2.1.4.3] Device Read Request (DR_READ_REQ)
///
/// [2.2.1.4.3]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/3192516d-36a6-47c5-987a-55c214aa0441
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceReadRequest {
    pub device_io_request: DeviceIoRequest,
    pub length: u32,
    pub offset: u64,
}

impl DeviceReadRequest {
    const FIXED_PART_SIZE: usize = 4 /* Length */ + 8 /* Offset */ + 20 /* Padding */;

    pub fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let length = src.read_u32();
        let offset = src.read_u64();
        // Padding (20 bytes):  An array of 20 bytes. Reserved. This field can be set to any value and MUST be ignored.
        read_padding!(src, 20);

        Ok(Self {
            device_io_request: dev_io_req,
            length,
            offset,
        })
    }
}

/// [2.2.1.5.3] Device Read Response (DR_READ_RSP)
///
/// [2.2.1.5.3]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/d35d3f91-fc5b-492b-80be-47f483ad1dc9
pub struct DeviceReadResponse {
    pub device_io_reply: DeviceIoResponse,
    pub read_data: Vec<u8>,
}

impl DeviceReadResponse {
    const NAME: &'static str = "DR_READ_RSP";

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_reply.encode(dst)?;
        dst.write_u32(cast_length!("DeviceReadResponse", "length", self.read_data.len())?);
        dst.write_slice(&self.read_data);
        Ok(())
    }

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn size(&self) -> usize {
        self.device_io_reply.size() // DeviceIoResponse
        + 4 // Length
        + self.read_data.len() // ReadData
    }
}

impl Debug for DeviceReadResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DeviceReadResponse")
            .field("device_io_reply", &self.device_io_reply)
            .field("read_data", &format!("Vec<u8> of length {}", self.read_data.len()))
            .finish()
    }
}

/// [2.2.1.4.4] Device Write Request (DR_WRITE_REQ)
///
/// [2.2.1.4.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/2e25f0aa-a4ce-4ff3-ad62-ab6098280a3a
#[derive(PartialEq, Clone)]
pub struct DeviceWriteRequest {
    pub device_io_request: DeviceIoRequest,
    pub offset: u64,
    pub write_data: Vec<u8>,
}

impl DeviceWriteRequest {
    const FIXED_PART_SIZE: usize = 4 /* Length */ + 8 /* Offset */ + 20 /* Padding */;

    pub fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let length = cast_length!("DeviceWriteRequest", "length", src.read_u32())?;
        let offset = src.read_u64();
        // Padding (20 bytes):  An array of 20 bytes. Reserved. This field can be set to any value and MUST be ignored.
        read_padding!(src, 20);

        ensure_size!(in: src, size: length);
        let write_data = src.read_slice(length).to_vec();

        Ok(Self {
            device_io_request: dev_io_req,
            offset,
            write_data,
        })
    }
}

impl Debug for DeviceWriteRequest {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("DeviceWriteRequest")
            .field("device_io_request", &self.device_io_request)
            .field("offset", &self.offset)
            .field("write_data", &format!("Vec<u8> of length {}", self.write_data.len()))
            .finish()
    }
}

/// [2.2.1.5.4] Device Write Response (DR_WRITE_RSP)
///
/// [2.2.1.5.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/58160a47-2379-4c4a-a99d-24a1a666c02a
#[derive(Debug, PartialEq, Clone)]
pub struct DeviceWriteResponse {
    pub device_io_reply: DeviceIoResponse,
    pub length: u32,
}

impl DeviceWriteResponse {
    const NAME: &'static str = "DR_WRITE_RSP";

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_reply.encode(dst)?;
        dst.write_u32(self.length);
        write_padding!(dst, 1); // Padding
        Ok(())
    }

    pub fn size(&self) -> usize {
        self.device_io_reply.size() // DeviceIoResponse
        + 4 // Length
        + 1 // Padding
    }
}

/// [2.2.3.3.9] Server Drive Set Information Request (DR_DRIVE_SET_INFORMATION_REQ)
///
/// [2.2.3.3.9]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/b5d3104b-0e42-4cf8-9059-e9fe86615e5c
#[derive(Debug, PartialEq, Clone)]
pub struct ServerDriveSetInformationRequest {
    pub device_io_request: DeviceIoRequest,
    pub set_buffer: FileInformationClass,
}

impl ServerDriveSetInformationRequest {
    const FIXED_PART_SIZE: usize = 4 /* FileInformationClass */ + 4 /* Length */ + 24 /* Padding */;

    fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let file_information_class_level = FileInformationClassLevel::from(src.read_u32());

        // This field MUST contain one of the following values.
        match file_information_class_level {
            FileInformationClassLevel::FILE_BASIC_INFORMATION
            | FileInformationClassLevel::FILE_END_OF_FILE_INFORMATION
            | FileInformationClassLevel::FILE_DISPOSITION_INFORMATION
            | FileInformationClassLevel::FILE_RENAME_INFORMATION
            | FileInformationClassLevel::FILE_ALLOCATION_INFORMATION => {}
            _ => {
                return Err(invalid_field_err!(
                    "ServerDriveSetInformationRequest::decode",
                    "file_information_class_level",
                    "received invalid level"
                ))
            }
        };

        let length = cast_length!("ServerDriveSetInformationRequest", "length", src.read_u32())?;

        read_padding!(src, 24); // Padding

        let set_buffer = FileInformationClass::decode(file_information_class_level, length, src)?;

        Ok(Self {
            device_io_request: dev_io_req,
            set_buffer,
        })
    }
}

/// 2.4.13 FileEndOfFileInformation
///
/// [2.4.13]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/75241cca-3167-472f-8058-a52d77c6bb17
#[derive(Debug, PartialEq, Clone)]
pub struct FileEndOfFileInformation {
    pub end_of_file: i64,
}

impl FileEndOfFileInformation {
    const FIXED_PART_SIZE: usize = 8; // EndOfFile

    fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let end_of_file = src.read_i64();
        Ok(Self { end_of_file })
    }

    fn size() -> usize {
        Self::FIXED_PART_SIZE
    }
}

/// [2.4.11] FileDispositionInformation
///
/// [2.4.11]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/12c3dd1c-14f6-4229-9d29-75fb2cb392f6
#[derive(Debug, PartialEq, Clone)]
pub struct FileDispositionInformation {
    pub delete_pending: u8,
}

impl FileDispositionInformation {
    const FIXED_PART_SIZE: usize = 1; // DeletePending

    fn decode(src: &mut ReadCursor<'_>, length: usize) -> DecodeResult<Self> {
        // https://github.com/FreeRDP/FreeRDP/blob/dfa231c0a55b005af775b833f92f6bcd30363d77/channels/drive/client/drive_file.c#L684-L692
        let delete_pending = if length != 0 {
            ensure_fixed_part_size!(in: src);
            src.read_u8()
        } else {
            1
        };
        Ok(Self { delete_pending })
    }

    fn size() -> usize {
        Self::FIXED_PART_SIZE
    }
}

/// [2.4.37] FileRenameInformation
///
/// [2.4.37]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/1d2673a8-8fb9-4868-920a-775ccaa30cf8
#[derive(Debug, PartialEq, Clone)]
pub struct FileRenameInformation {
    pub replace_if_exists: Boolean,
    /// `file_name` is the relative path to the new location of the file
    pub file_name: String,
}

impl FileRenameInformation {
    const FIXED_PART_SIZE: usize = 1 /* ReplaceIfExists */ + 1 /* RootDirectory */ + 4 /* FileNameLength */;

    fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let replace_if_exists = Boolean::from(src.read_u8());
        let _ = src.read_u8(); // RootDirectory
        let file_name_length = cast_length!("FileRenameInformation", "file_name_length", src.read_u32())?;

        ensure_size!(in: src, size: file_name_length);
        let file_name = decode_string(src.read_slice(file_name_length), CharacterSet::Unicode, true)?;

        Ok(Self {
            replace_if_exists,
            file_name,
        })
    }

    fn size(&self) -> usize {
        Self::FIXED_PART_SIZE + encoded_str_len(&self.file_name, CharacterSet::Unicode, true)
    }
}

/// [2.4.4] FileAllocationInformation
///
/// [2.4.4]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-fscc/0201c69b-50db-412d-bab3-dd97aeede13b
#[derive(Debug, PartialEq, Clone)]
pub struct FileAllocationInformation {
    pub allocation_size: i64,
}

impl FileAllocationInformation {
    const FIXED_PART_SIZE: usize = 8; // AllocationSize

    fn decode(src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        ensure_fixed_part_size!(in: src);
        let allocation_size = src.read_i64();
        Ok(Self { allocation_size })
    }

    fn size() -> usize {
        Self::FIXED_PART_SIZE
    }
}

/// [2.2.3.4.9] Client Drive Set Information Response (DR_DRIVE_SET_INFORMATION_RSP)
///
/// [2.2.3.4.9]: https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/16b893d5-5d8b-49d1-8dcb-ee21e7612970
#[derive(Debug, PartialEq, Clone)]
pub struct ClientDriveSetInformationResponse {
    device_io_reply: DeviceIoResponse,
    /// This field MUST be equal to the Length field in the Server Drive Set Information Request (section 2.2.3.3.9).
    length: u32,
}

impl ClientDriveSetInformationResponse {
    const NAME: &'static str = "DR_DRIVE_SET_INFORMATION_RSP";

    pub fn new(req: &ServerDriveSetInformationRequest, io_status: NtStatus) -> EncodeResult<Self> {
        Ok(Self {
            device_io_reply: DeviceIoResponse::new(req.device_io_request.clone(), io_status),
            length: cast_length!("ClientDriveSetInformationResponse", "length", req.set_buffer.size())?,
        })
    }

    pub fn encode(&self, dst: &mut WriteCursor<'_>) -> EncodeResult<()> {
        ensure_size!(in: dst, size: self.size());
        self.device_io_reply.encode(dst)?;
        dst.write_u32(self.length);
        Ok(())
    }

    pub fn name(&self) -> &'static str {
        Self::NAME
    }

    pub fn size(&self) -> usize {
        self.device_io_reply.size() // DeviceIoResponse
        + 4 // Length
    }
}

/// 2.2.3.3.12 Server Drive Lock Control Request (DR_DRIVE_LOCK_REQ)
///
/// [2.2.3.3.12]: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-rdpefs/a96fe85c-620c-40ce-8858-a6bc38609b0a
#[derive(Debug, PartialEq, Clone)]
pub struct ServerDriveLockControlRequest {
    pub device_io_request: DeviceIoRequest,
}

impl ServerDriveLockControlRequest {
    fn decode(dev_io_req: DeviceIoRequest, src: &mut ReadCursor<'_>) -> DecodeResult<Self> {
        // It's not quite clear why this is done this way, but it's what FreeRDP does:
        // https://github.com/FreeRDP/FreeRDP/blob/dfa231c0a55b005af775b833f92f6bcd30363d77/channels/drive/client/drive_main.c#L600
        ensure_size!(in: src, size: 4);
        let _ = src.read_u32();
        Ok(Self {
            device_io_request: dev_io_req,
        })
    }
}