aria2-core 0.3.10

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
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
use crate::error::{Aria2Error, Result};
use sha2::{Digest, Sha256};
use std::path::{Path, PathBuf};
use tokio::io::AsyncWriteExt;

// A2CF was the pre-release Rust-only write format. Keep its reader for
// migration compatibility, but do not add new fields to it: it will be
// removed after old checkpoints have aged out.
const CONTROL_MAGIC: &[u8; 4] = b"A2CF";
const CONTROL_VERSION: u16 = 1;
const FLAG_HAS_CHECKSUM: u8 = 0x01;
const FLAG_TORRENT_CHECKPOINT: u8 = 0x02;
const FLAG_TORRENT_INFO_HASH: u8 = 0x04;
const FLAG_TORRENT_PIECE_LENGTH: u8 = 0x08;
const CONTROL_HEADER_LEN: usize = 39;

// The prefix of every newly-written file is the official aria2 v1 format.
// A2RX is only the project's extension trailer; aria2's reader stops after
// the official in-flight records and therefore remains compatible with it.
const PROJECT_EXTENSION_MAGIC: &[u8; 4] = b"A2RX";
const PROJECT_EXTENSION_VERSION: u16 = 1;
const PROJECT_EXTENSION_HEADER_LEN: usize = 12;
const PROJECT_EXTENSION_DIGEST_LEN: usize = 32;
const EXT_HAS_COMPLETED_LENGTH: u16 = 0x0001;
const EXT_HAS_CHECKSUM: u16 = 0x0002;
const EXT_TORRENT_CHECKPOINT: u16 = 0x0004;
const EXT_HAS_INFO_HASH: u16 = 0x0008;
const EXT_HAS_PIECE_LENGTH: u16 = 0x0010;
const EXT_KNOWN_FLAGS: u16 = EXT_HAS_COMPLETED_LENGTH
    | EXT_HAS_CHECKSUM
    | EXT_TORRENT_CHECKPOINT
    | EXT_HAS_INFO_HASH
    | EXT_HAS_PIECE_LENGTH;

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ControlFileInFlightPiece {
    pub index: u32,
    pub length: u32,
    pub bitfield: Vec<u8>,
}

#[derive(Debug, Clone)]
pub struct ControlFile {
    path: PathBuf,
    total_length: u64,
    completed_length: u64,
    upload_length: u64,
    bitfield: Vec<u8>,
    in_flight_pieces: Vec<ControlFileInFlightPiece>,
    num_pieces: usize,
    checksum_algo: u8,
    checksum_value: Vec<u8>,
    torrent_checkpoint: bool,
    torrent_info_hash: Option<[u8; 20]>,
    torrent_piece_length: Option<u32>,
    native_piece_length: Option<u32>,
    native_layout: bool,
}

impl ControlFile {
    pub fn path(&self) -> &Path {
        &self.path
    }
    pub fn total_length(&self) -> u64 {
        self.total_length
    }

    /// Piece length explicitly represented by this native checkpoint layout.
    ///
    /// Legacy A2CF checkpoints did not store a generic piece length, so they
    /// return `None` and must not be interpreted using a new segment layout.
    pub fn piece_length(&self) -> Option<u32> {
        self.native_piece_length.or(self.torrent_piece_length)
    }
    pub fn completed_length(&self) -> u64 {
        self.completed_length
    }
    pub fn bitfield(&self) -> &[u8] {
        &self.bitfield
    }

    pub fn in_flight_pieces(&self) -> &[ControlFileInFlightPiece] {
        &self.in_flight_pieces
    }

    pub fn set_in_flight_pieces(&mut self, pieces: Vec<ControlFileInFlightPiece>) {
        self.in_flight_pieces = pieces;
    }
    pub fn set_checksum(&mut self, algo: u8, value: Vec<u8>) {
        self.checksum_algo = algo;
        self.checksum_value = value;
    }
    pub fn checksum_algo(&self) -> u8 {
        self.checksum_algo
    }

    pub fn checksum_value(&self) -> &[u8] {
        &self.checksum_value
    }

    pub fn upload_length(&self) -> u64 {
        self.upload_length
    }

    /// Whether this value was decoded from aria2's native v0/v1 layout.
    pub fn uses_native_layout(&self) -> bool {
        self.native_layout
    }

    /// Mark this Rust-owned checkpoint as BitTorrent state.
    ///
    /// The marker prevents a generic HTTP/FTP checkpoint with the same output
    /// path and shape from being mistaken for verified torrent pieces.
    pub fn mark_torrent_checkpoint(&mut self) {
        self.torrent_checkpoint = true;
    }

    pub fn is_torrent_checkpoint(&self) -> bool {
        self.torrent_checkpoint
    }

    /// Store the torrent identity for a Rust-owned BitTorrent checkpoint.
    pub fn set_torrent_info_hash(&mut self, info_hash: [u8; 20]) {
        self.torrent_info_hash = Some(info_hash);
    }

    pub fn torrent_info_hash(&self) -> Option<[u8; 20]> {
        self.torrent_info_hash
    }

    /// Store the piece length used by a Rust-owned BitTorrent checkpoint.
    pub fn set_torrent_piece_length(&mut self, piece_length: u32) {
        self.torrent_piece_length = Some(piece_length);
    }

    pub fn torrent_piece_length(&self) -> Option<u32> {
        self.torrent_piece_length
    }

    /// Replace the persisted piece bitfield with a complete snapshot.
    pub fn set_bitfield(&mut self, bitfield: Vec<u8>) {
        self.bitfield = bitfield;
    }

    pub(crate) fn normalize_legacy_piece_layout(&mut self, num_pieces: usize) -> Result<()> {
        if !self.native_layout {
            self.normalize_bitfield(num_pieces)?;
        }
        Ok(())
    }

    pub async fn open_or_create(
        ctrl_path: &Path,
        total_length: u64,
        num_pieces: usize,
    ) -> Result<Self> {
        if ctrl_path.exists() {
            let mut control_file = Self::load(ctrl_path).await?.ok_or_else(|| {
                Aria2Error::FileIo(format!(
                    "Failed to load control file: {}",
                    ctrl_path.display()
                ))
            })?;
            // Legacy A2CF has no official piece-length field, so restore its
            // logical layout at the typed open seam. Native files retain the
            // piece layout encoded by aria2 itself.
            if !control_file.native_layout {
                control_file.normalize_bitfield(num_pieces)?;
            }
            Ok(control_file)
        } else {
            let piece_length = piece_length_for(total_length, num_pieces)?;
            Self::new_with_piece_length(ctrl_path, total_length, piece_length)
        }
    }

    /// Open an existing checkpoint only when it uses this exact fixed-piece
    /// layout, or create a new checkpoint with the supplied piece length.
    pub async fn open_or_create_with_piece_length(
        ctrl_path: &Path,
        total_length: u64,
        piece_length: u32,
    ) -> Result<Self> {
        if piece_length == 0 {
            return Err(Aria2Error::InvalidArgument(
                "Control file piece length must not be 0".to_string(),
            ));
        }
        if ctrl_path.exists() {
            let control_file = Self::load(ctrl_path).await?.ok_or_else(|| {
                Aria2Error::FileIo(format!(
                    "Failed to load control file: {}",
                    ctrl_path.display()
                ))
            })?;
            let expected_pieces = piece_count(total_length, piece_length)?;
            if control_file.total_length() == total_length
                && control_file.piece_length() == Some(piece_length)
                && control_file.bitfield().len() == expected_pieces.div_ceil(8)
            {
                return Ok(control_file);
            }
            return Err(Aria2Error::InvalidArgument(format!(
                "Control file layout does not match piece length {}: {}",
                piece_length,
                ctrl_path.display()
            )));
        }
        Self::new_with_piece_length(ctrl_path, total_length, piece_length)
    }

    fn new_with_piece_length(
        ctrl_path: &Path,
        total_length: u64,
        piece_length: u32,
    ) -> Result<Self> {
        let actual_num_pieces = piece_count(total_length, piece_length)?;
        let bitfield_len = actual_num_pieces.div_ceil(8);
        Ok(Self {
            path: ctrl_path.to_path_buf(),
            total_length,
            completed_length: 0,
            upload_length: 0,
            bitfield: vec![0u8; bitfield_len],
            in_flight_pieces: Vec::new(),
            num_pieces: actual_num_pieces,
            checksum_algo: 0,
            checksum_value: Vec::new(),
            torrent_checkpoint: false,
            torrent_info_hash: None,
            torrent_piece_length: None,
            native_piece_length: Some(piece_length),
            native_layout: false,
        })
    }

    pub async fn load(path: &Path) -> Result<Option<Self>> {
        if !path.exists() {
            return Ok(None);
        }

        let data = tokio::fs::read(path)
            .await
            .map_err(|e| Aria2Error::FileIo(format!("{}: {e}", path.display())))?;

        if data.len() < 2 {
            return Err(Aria2Error::FileIo(format!(
                "Truncated control file header: {} bytes (expected at least 2)",
                data.len(),
            )));
        }

        // Native aria2 control files start with version 0000 or 0001. Read
        // both versions so a download can be resumed after switching
        // clients; all future writes use the native v1 prefix below.
        if data[0] == 0 && (data[1] == 0 || data[1] == 1) {
            return parse_native_control_file(path, &data);
        }

        if data.len() < CONTROL_HEADER_LEN {
            return Err(Aria2Error::FileIo(format!(
                "Truncated control file header: {} bytes (expected at least {})",
                data.len(),
                CONTROL_HEADER_LEN
            )));
        }

        if &data[0..4] != CONTROL_MAGIC {
            return Err(Aria2Error::FileIo("Invalid control file magic".to_string()));
        }

        let version = u16_from_le(&data[4..6]);
        if version > CONTROL_VERSION {
            return Err(Aria2Error::FileIo(format!(
                "Unsupported version: {}",
                version
            )));
        }

        // TODO: Remove the legacy A2CF reader after the migration window.
        let flags = data[6];
        let known_flags = FLAG_HAS_CHECKSUM
            | FLAG_TORRENT_CHECKPOINT
            | FLAG_TORRENT_INFO_HASH
            | FLAG_TORRENT_PIECE_LENGTH;
        if flags & !known_flags != 0 {
            return Err(Aria2Error::FileIo(format!(
                "Unknown control file flags: 0x{:02x}",
                flags & !known_flags
            )));
        }
        let total_length = u64_from_le(&data[7..15]);
        let completed_length = u64_from_le(&data[15..23]);
        let upload_length = u64_from_le(&data[23..31]);
        let bitfield_length = usize::try_from(u64_from_le(&data[31..39])).map_err(|_| {
            Aria2Error::FileIo("Control file bitfield length exceeds platform limits".to_string())
        })?;

        let mut offset = CONTROL_HEADER_LEN;
        let checksum_algo = if flags & FLAG_HAS_CHECKSUM != 0 {
            let algo = *data.get(offset).ok_or_else(|| {
                Aria2Error::FileIo("Truncated control file checksum algorithm".to_string())
            })?;
            offset += 1;
            algo
        } else {
            0
        };

        let checksum_value = if flags & FLAG_HAS_CHECKSUM != 0 {
            let len = checksum_length(checksum_algo).ok_or_else(|| {
                Aria2Error::FileIo(format!(
                    "Unsupported control file checksum algorithm: {}",
                    checksum_algo
                ))
            })?;
            let end = offset.checked_add(len).ok_or_else(|| {
                Aria2Error::FileIo("Control file checksum length overflow".to_string())
            })?;
            if end > data.len() {
                return Err(Aria2Error::FileIo(
                    "Truncated control file checksum".to_string(),
                ));
            }
            let val = data[offset..end].to_vec();
            offset = end;
            val
        } else {
            Vec::new()
        };

        let torrent_info_hash = if flags & FLAG_TORRENT_INFO_HASH != 0 {
            let end = offset.checked_add(20).ok_or_else(|| {
                Aria2Error::FileIo("Control file torrent info hash length overflow".to_string())
            })?;
            if end > data.len() {
                return Err(Aria2Error::FileIo(
                    "Truncated control file torrent info hash".to_string(),
                ));
            }
            let mut info_hash = [0u8; 20];
            info_hash.copy_from_slice(&data[offset..end]);
            offset = end;
            Some(info_hash)
        } else {
            None
        };

        let torrent_piece_length = if flags & FLAG_TORRENT_PIECE_LENGTH != 0 {
            let end = offset.checked_add(4).ok_or_else(|| {
                Aria2Error::FileIo("Control file torrent piece length overflow".to_string())
            })?;
            if end > data.len() {
                return Err(Aria2Error::FileIo(
                    "Truncated control file torrent piece length".to_string(),
                ));
            }
            let piece_length = u32_from_le(&data[offset..end]);
            if piece_length == 0 {
                return Err(Aria2Error::FileIo(
                    "Control file torrent piece length must not be 0".to_string(),
                ));
            }
            offset = end;
            Some(piece_length)
        } else {
            None
        };

        let bitfield_end = offset.checked_add(bitfield_length).ok_or_else(|| {
            Aria2Error::FileIo("Control file bitfield length overflow".to_string())
        })?;
        if bitfield_end != data.len() {
            return Err(Aria2Error::FileIo(format!(
                "Control file bitfield length mismatch: declared {}, available {}",
                bitfield_length,
                data.len().saturating_sub(offset)
            )));
        }
        let bitfield = data[offset..bitfield_end].to_vec();
        let num_pieces = bitfield.len() * 8;
        if completed_length > total_length {
            return Err(Aria2Error::FileIo(
                "Control file completed length exceeds total length".to_string(),
            ));
        }

        Ok(Some(Self {
            path: path.to_path_buf(),
            total_length,
            completed_length,
            upload_length,
            bitfield,
            in_flight_pieces: Vec::new(),
            num_pieces,
            checksum_algo,
            checksum_value,
            torrent_checkpoint: flags & FLAG_TORRENT_CHECKPOINT != 0,
            torrent_info_hash,
            torrent_piece_length,
            native_piece_length: None,
            native_layout: false,
        }))
    }

    pub async fn save(&self) -> Result<()> {
        let piece_length = self.effective_piece_length()?;
        let num_pieces = piece_count(self.total_length, piece_length)?;
        if self.num_pieces != num_pieces {
            return Err(Aria2Error::InvalidArgument(format!(
                "Control file piece count mismatch: expected {}, actual {}",
                num_pieces, self.num_pieces
            )));
        }
        let bitfield_length = num_pieces.div_ceil(8);
        if self.bitfield.len() != bitfield_length {
            return Err(Aria2Error::InvalidArgument(format!(
                "Control file bitfield length mismatch: expected {}, actual {}",
                bitfield_length,
                self.bitfield.len()
            )));
        }
        if let Some(last_byte) = self.bitfield.last()
            && !num_pieces.is_multiple_of(8)
            && last_byte & (u8::MAX >> (num_pieces % 8)) != 0
        {
            return Err(Aria2Error::InvalidArgument(
                "Control file bitfield has set bits outside the piece count".to_string(),
            ));
        }
        validate_in_flight_pieces(
            &self.in_flight_pieces,
            self.total_length,
            piece_length,
            num_pieces,
            &self.bitfield,
        )?;

        let is_torrent = self.torrent_checkpoint
            || self.torrent_info_hash.is_some()
            || self.torrent_piece_length.is_some();
        if is_torrent && self.torrent_info_hash.is_none() {
            return Err(Aria2Error::InvalidArgument(
                "Torrent control files require an info hash".to_string(),
            ));
        }
        let bitfield_length = u32::try_from(bitfield_length).map_err(|_| {
            Aria2Error::InvalidArgument("Control file bitfield is too large".to_string())
        })?;
        let extension = serialize_project_extension(self)?;
        let tmp_path = self.path.with_extension("aria2.tmp");
        let mut file = tokio::fs::File::create(&tmp_path)
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        file.write_all(&1u16.to_be_bytes())
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        file.write_all(&(if is_torrent { 1u32 } else { 0 }).to_be_bytes())
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        if let Some(info_hash) = self.torrent_info_hash {
            file.write_all(&20u32.to_be_bytes())
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
            file.write_all(&info_hash)
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
        } else {
            file.write_all(&0u32.to_be_bytes())
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
        }
        file.write_all(&piece_length.to_be_bytes())
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        file.write_all(&self.total_length.to_be_bytes())
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        file.write_all(&self.upload_length.to_be_bytes())
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        file.write_all(&bitfield_length.to_be_bytes())
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        file.write_all(&self.bitfield)
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        let in_flight_count = u32::try_from(self.in_flight_pieces.len()).map_err(|_| {
            Aria2Error::InvalidArgument("Too many in-flight torrent pieces".to_string())
        })?;
        file.write_all(&in_flight_count.to_be_bytes())
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        for piece in &self.in_flight_pieces {
            file.write_all(&piece.index.to_be_bytes())
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
            file.write_all(&piece.length.to_be_bytes())
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
            let bitfield_length = u32::try_from(piece.bitfield.len()).map_err(|_| {
                Aria2Error::InvalidArgument("In-flight block bitfield is too large".to_string())
            })?;
            file.write_all(&bitfield_length.to_be_bytes())
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
            file.write_all(&piece.bitfield)
                .await
                .map_err(|e| Aria2Error::Io(e.to_string()))?;
        }
        file.write_all(&extension)
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        file.sync_all()
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        drop(file);
        tokio::fs::rename(&tmp_path, &self.path)
            .await
            .map_err(|e| Aria2Error::Io(e.to_string()))?;
        Ok(())
    }

    pub fn mark_piece_done(&mut self, index: usize) {
        let byte_index = index / 8;
        let bit_index = index % 8;
        if index < self.num_pieces && byte_index < self.bitfield.len() {
            self.bitfield[byte_index] |= 1 << (7 - bit_index);
            self.in_flight_pieces
                .retain(|piece| piece.index as usize != index);
            self.completed_length = self.calculate_completed();
        }
    }

    fn normalize_bitfield(&mut self, num_pieces: usize) -> Result<()> {
        let piece_length = piece_length_for(self.total_length, num_pieces)?;
        self.native_piece_length = Some(piece_length);
        self.num_pieces = piece_count(self.total_length, piece_length)?;
        self.bitfield.resize(self.num_pieces.div_ceil(8), 0);

        if let Some(last_byte) = self.bitfield.last_mut() {
            let valid_bits = self.num_pieces % 8;
            if valid_bits != 0 {
                *last_byte &= u8::MAX << (8 - valid_bits);
            }
        }

        self.completed_length = self.calculate_completed();
        Ok(())
    }

    pub fn is_piece_done(&self, index: usize) -> bool {
        let byte_index = index / 8;
        let bit_index = index % 8;
        if byte_index < self.bitfield.len() {
            (self.bitfield[byte_index] & (1 << (7 - bit_index))) != 0
        } else {
            false
        }
    }

    pub fn completed_pieces(&self) -> usize {
        self.bitfield.iter().map(|b| b.count_ones() as usize).sum()
    }

    fn calculate_completed(&self) -> u64 {
        if self.total_length == 0 || self.num_pieces == 0 {
            return 0;
        }
        let piece_size = self
            .torrent_piece_length
            .or(self.native_piece_length)
            .map(u64::from)
            .unwrap_or_else(|| self.total_length.div_ceil(self.num_pieces as u64));
        (0..self.num_pieces)
            .filter(|&index| self.is_piece_done(index))
            .map(|index| {
                let offset = index as u64 * piece_size;
                self.total_length.saturating_sub(offset).min(piece_size)
            })
            .sum()
    }

    pub fn update_completed_length(&mut self, length: u64) {
        self.completed_length = length.min(self.total_length);
    }

    fn effective_piece_length(&self) -> Result<u32> {
        self.torrent_piece_length
            .or(self.native_piece_length)
            .or_else(|| piece_length_for(self.total_length, self.num_pieces).ok())
            .filter(|piece_length| *piece_length != 0)
            .ok_or_else(|| {
                Aria2Error::InvalidArgument(
                    "Control file piece length cannot be represented".to_string(),
                )
            })
    }

    pub fn control_path_for(output_path: &Path) -> PathBuf {
        // aria2 appends the suffix.  Replacing the extension would turn
        // `file.tar.gz` into `file.tar.aria2`, while the documented sidecar
        // is `file.tar.gz.aria2`.  The same rule applies to a multi-file
        // torrent's top directory (`top-directory.aria2`).
        if output_path == Path::new(".") || output_path == Path::new("..") {
            return output_path.join(".aria2");
        }

        let Some(file_name) = output_path.file_name() else {
            return output_path.join(".aria2");
        };
        let mut control_name = file_name.to_os_string();
        control_name.push(".aria2");
        output_path.with_file_name(control_name)
    }
}

fn checksum_length(algo: u8) -> Option<usize> {
    match algo {
        1 => Some(16), // MD5
        2 => Some(20), // SHA-1
        3 => Some(32), // SHA-256
        4 => Some(8),  // CRC-64
        _ => None,
    }
}

fn piece_length_for(total_length: u64, num_pieces: usize) -> Result<u32> {
    let divisor = num_pieces.max(1) as u64;
    let piece_length = if total_length == 0 {
        1
    } else {
        total_length.div_ceil(divisor)
    };
    u32::try_from(piece_length).map_err(|_| {
        Aria2Error::InvalidArgument(format!(
            "Control file piece length exceeds native aria2 limit: {}",
            piece_length
        ))
    })
}

fn piece_count(total_length: u64, piece_length: u32) -> Result<usize> {
    if piece_length == 0 {
        return Err(Aria2Error::InvalidArgument(
            "Control file piece length must not be 0".to_string(),
        ));
    }
    usize::try_from(total_length.div_ceil(piece_length as u64))
        .map_err(|_| Aria2Error::InvalidArgument("Control file has too many pieces".to_string()))
}

fn validate_in_flight_pieces(
    pieces: &[ControlFileInFlightPiece],
    total_length: u64,
    piece_length: u32,
    num_pieces: usize,
    _completed_bitfield: &[u8],
) -> Result<()> {
    let mut seen = std::collections::HashSet::with_capacity(pieces.len());
    for piece in pieces {
        let index = piece.index as usize;
        if index >= num_pieces {
            return Err(Aria2Error::FileIo(format!(
                "In-flight torrent piece index is out of range: {}",
                piece.index
            )));
        }
        if !seen.insert(piece.index) {
            return Err(Aria2Error::FileIo(format!(
                "Duplicate in-flight torrent piece index: {}",
                piece.index
            )));
        }
        if piece.length == 0 || piece.length > piece_length {
            return Err(Aria2Error::FileIo(format!(
                "In-flight torrent piece length is out of range: {}",
                piece.length
            )));
        }
        let block_count = (piece.length as usize).div_ceil(16 * 1024);
        let expected_bitfield_length = block_count.div_ceil(8);
        if piece.bitfield.len() != expected_bitfield_length {
            return Err(Aria2Error::FileIo(format!(
                "In-flight block bitfield length mismatch: expected {}, actual {}",
                expected_bitfield_length,
                piece.bitfield.len()
            )));
        }
        let unused_bits = (8 - block_count % 8) % 8;
        if unused_bits != 0
            && piece
                .bitfield
                .last()
                .is_some_and(|byte| byte & ((1u8 << unused_bits) - 1) != 0)
        {
            return Err(Aria2Error::FileIo(
                "In-flight block bitfield has set trailing bits".to_string(),
            ));
        }
        let piece_offset = u64::from(piece.index) * u64::from(piece_length);
        if piece_offset >= total_length {
            return Err(Aria2Error::FileIo(format!(
                "In-flight torrent piece offset is out of range: {}",
                piece.index
            )));
        }
    }
    Ok(())
}

#[derive(Debug, Default)]
struct ProjectExtension {
    completed_length: Option<u64>,
    checksum_algo: u8,
    checksum_value: Vec<u8>,
    torrent_checkpoint: bool,
    torrent_info_hash: Option<[u8; 20]>,
    torrent_piece_length: Option<u32>,
}

fn serialize_project_extension(control_file: &ControlFile) -> Result<Vec<u8>> {
    if control_file.completed_length > control_file.total_length {
        return Err(Aria2Error::InvalidArgument(
            "Control file completed length exceeds total length".to_string(),
        ));
    }
    if (control_file.checksum_algo == 0 && !control_file.checksum_value.is_empty())
        || (control_file.checksum_algo != 0
            && checksum_length(control_file.checksum_algo)
                != Some(control_file.checksum_value.len()))
    {
        return Err(Aria2Error::InvalidArgument(format!(
            "Checksum length does not match algorithm {}",
            control_file.checksum_algo
        )));
    }
    if control_file
        .torrent_piece_length
        .is_some_and(|piece_length| piece_length == 0)
    {
        return Err(Aria2Error::InvalidArgument(
            "Control file torrent piece length must not be 0".to_string(),
        ));
    }

    let mut flags = EXT_HAS_COMPLETED_LENGTH;
    if control_file.checksum_algo != 0 {
        flags |= EXT_HAS_CHECKSUM;
    }
    if control_file.torrent_checkpoint {
        flags |= EXT_TORRENT_CHECKPOINT;
    }
    if control_file.torrent_info_hash.is_some() {
        flags |= EXT_HAS_INFO_HASH;
    }
    if control_file.torrent_piece_length.is_some() {
        flags |= EXT_HAS_PIECE_LENGTH;
    }

    let mut payload = Vec::with_capacity(8 + 1 + 4 + 32 + 20 + 4);
    payload.extend_from_slice(&control_file.completed_length.to_be_bytes());
    if flags & EXT_HAS_CHECKSUM != 0 {
        payload.push(control_file.checksum_algo);
        payload.extend_from_slice(&(control_file.checksum_value.len() as u32).to_be_bytes());
        payload.extend_from_slice(&control_file.checksum_value);
    }
    if let Some(info_hash) = control_file.torrent_info_hash {
        payload.extend_from_slice(&info_hash);
    }
    if let Some(piece_length) = control_file.torrent_piece_length {
        payload.extend_from_slice(&piece_length.to_be_bytes());
    }

    let payload_length = u32::try_from(payload.len()).map_err(|_| {
        Aria2Error::InvalidArgument("Control file extension payload is too large".to_string())
    })?;
    let mut extension = Vec::with_capacity(
        PROJECT_EXTENSION_HEADER_LEN + payload.len() + PROJECT_EXTENSION_DIGEST_LEN,
    );
    extension.extend_from_slice(PROJECT_EXTENSION_MAGIC);
    extension.extend_from_slice(&PROJECT_EXTENSION_VERSION.to_be_bytes());
    extension.extend_from_slice(&flags.to_be_bytes());
    extension.extend_from_slice(&payload_length.to_be_bytes());
    extension.extend_from_slice(&payload);
    let digest = Sha256::digest(&extension);
    extension.extend_from_slice(&digest);
    Ok(extension)
}

fn parse_project_extension(data: &[u8]) -> Result<Option<ProjectExtension>> {
    if data.is_empty() {
        return Ok(None);
    }
    if data.len() < PROJECT_EXTENSION_HEADER_LEN + PROJECT_EXTENSION_DIGEST_LEN
        || &data[..4] != PROJECT_EXTENSION_MAGIC
    {
        return Err(Aria2Error::FileIo(
            "Unexpected trailing bytes in native control file".to_string(),
        ));
    }

    let version = u16::from_be_bytes([data[4], data[5]]);
    if version != PROJECT_EXTENSION_VERSION {
        return Err(Aria2Error::FileIo(format!(
            "Unsupported control file extension version: {}",
            version
        )));
    }
    let flags = u16::from_be_bytes([data[6], data[7]]);
    if flags & !EXT_KNOWN_FLAGS != 0 {
        return Err(Aria2Error::FileIo(format!(
            "Unknown control file extension flags: 0x{:04x}",
            flags & !EXT_KNOWN_FLAGS
        )));
    }
    let payload_length =
        usize::try_from(u32::from_be_bytes([data[8], data[9], data[10], data[11]])).map_err(
            |_| Aria2Error::FileIo("Control file extension payload is too large".to_string()),
        )?;
    let payload_end = PROJECT_EXTENSION_HEADER_LEN
        .checked_add(payload_length)
        .ok_or_else(|| Aria2Error::FileIo("Control file extension length overflow".to_string()))?;
    let expected_length = payload_end
        .checked_add(PROJECT_EXTENSION_DIGEST_LEN)
        .ok_or_else(|| Aria2Error::FileIo("Control file extension length overflow".to_string()))?;
    if expected_length != data.len() {
        return Err(Aria2Error::FileIo(format!(
            "Control file extension length mismatch: declared {}, available {}",
            payload_length,
            data.len().saturating_sub(PROJECT_EXTENSION_HEADER_LEN)
        )));
    }
    let digest = Sha256::digest(&data[..payload_end]);
    if digest.as_slice() != &data[payload_end..] {
        return Err(Aria2Error::FileIo(
            "Control file extension checksum mismatch".to_string(),
        ));
    }

    let mut reader = NativeReader::payload(&data[PROJECT_EXTENSION_HEADER_LEN..payload_end]);
    let completed_length = if flags & EXT_HAS_COMPLETED_LENGTH != 0 {
        Some(reader.u64("extension completed length")?)
    } else {
        None
    };
    let (checksum_algo, checksum_value) = if flags & EXT_HAS_CHECKSUM != 0 {
        let checksum_algo = reader.bytes(1, "extension checksum algorithm")?[0];
        let value_length = usize::try_from(reader.u32("extension checksum length")?)
            .map_err(|_| Aria2Error::FileIo("Control file checksum is too large".to_string()))?;
        let expected_length = checksum_length(checksum_algo).ok_or_else(|| {
            Aria2Error::FileIo(format!(
                "Unsupported control file checksum algorithm: {}",
                checksum_algo
            ))
        })?;
        if value_length != expected_length {
            return Err(Aria2Error::FileIo(format!(
                "Control file checksum length mismatch: expected {}, actual {}",
                expected_length, value_length
            )));
        }
        (
            checksum_algo,
            reader
                .bytes(value_length, "extension checksum value")?
                .to_vec(),
        )
    } else {
        (0, Vec::new())
    };
    let torrent_checkpoint = flags & EXT_TORRENT_CHECKPOINT != 0;
    let torrent_info_hash = if flags & EXT_HAS_INFO_HASH != 0 {
        let mut info_hash = [0u8; 20];
        info_hash.copy_from_slice(reader.bytes(20, "extension info hash")?);
        Some(info_hash)
    } else {
        None
    };
    let torrent_piece_length = if flags & EXT_HAS_PIECE_LENGTH != 0 {
        let piece_length = reader.u32("extension piece length")?;
        if piece_length == 0 {
            return Err(Aria2Error::FileIo(
                "Control file extension piece length must not be 0".to_string(),
            ));
        }
        Some(piece_length)
    } else {
        None
    };
    if !reader.remaining().is_empty() {
        return Err(Aria2Error::FileIo(
            "Control file extension contains unexpected payload bytes".to_string(),
        ));
    }

    Ok(Some(ProjectExtension {
        completed_length,
        checksum_algo,
        checksum_value,
        torrent_checkpoint,
        torrent_info_hash,
        torrent_piece_length,
    }))
}

fn parse_native_control_file(path: &Path, data: &[u8]) -> Result<Option<ControlFile>> {
    let version = u16::from_be_bytes([data[0], data[1]]);
    let native = NativeReader::new(data, version == 1);
    let mut reader = native;
    // aria2 stores extension flags as a network-order byte mask even in v0;
    // only the following numeric fields use the host byte order in that version.
    let extension = reader.u32_be("extension")?;
    let is_torrent = extension & 1 != 0;
    let info_hash_length = usize::try_from(reader.u32("info hash length")?).map_err(|_| {
        Aria2Error::FileIo("Native control file info hash length is too large".to_string())
    })?;
    if info_hash_length > 20 || (is_torrent && info_hash_length != 20) {
        return Err(Aria2Error::FileIo(format!(
            "Invalid native control file info hash length: {}",
            info_hash_length
        )));
    }

    let torrent_info_hash = if info_hash_length == 20 {
        let bytes = reader.bytes(20, "info hash")?;
        let mut info_hash = [0u8; 20];
        info_hash.copy_from_slice(bytes);
        Some(info_hash)
    } else if info_hash_length != 0 {
        let _ = reader.bytes(info_hash_length, "info hash")?;
        None
    } else {
        None
    };

    let piece_length = reader.u32("piece length")?;
    if piece_length == 0 {
        return Err(Aria2Error::FileIo(
            "Native control file piece length must not be 0".to_string(),
        ));
    }
    let total_length = reader.u64("total length")?;
    let upload_length = reader.u64("upload length")?;
    let bitfield_length = usize::try_from(reader.u32("bitfield length")?).map_err(|_| {
        Aria2Error::FileIo("Native control file bitfield length is too large".to_string())
    })?;
    let num_pieces_u64 = if total_length == 0 {
        0
    } else {
        total_length.div_ceil(piece_length as u64)
    };
    let num_pieces = usize::try_from(num_pieces_u64)
        .map_err(|_| Aria2Error::FileIo("Native control file has too many pieces".to_string()))?;
    let expected_bitfield_length = num_pieces.div_ceil(8);
    if bitfield_length != expected_bitfield_length {
        return Err(Aria2Error::FileIo(format!(
            "Native control file bitfield length mismatch: expected {}, actual {}",
            expected_bitfield_length, bitfield_length
        )));
    }
    let bitfield = reader.bytes(bitfield_length, "bitfield")?.to_vec();
    if let Some(last_byte) = bitfield.last()
        && !num_pieces.is_multiple_of(8)
        && last_byte & (u8::MAX >> (num_pieces % 8)) != 0
    {
        return Err(Aria2Error::FileIo(
            "Native control file bitfield has set trailing bits".to_string(),
        ));
    }

    // Validate the in-flight-piece records even though the generic Rust
    // checkpoint does not currently restore block-level progress.  This
    // prevents malformed native files from being accepted as resumable state.
    let in_flight_count = usize::try_from(reader.u32("in-flight piece count")?).map_err(|_| {
        Aria2Error::FileIo("Native control file in-flight count is too large".to_string())
    })?;
    let mut in_flight_pieces = Vec::with_capacity(in_flight_count);
    for _ in 0..in_flight_count {
        let index = reader.u32("in-flight piece index")?;
        if index as usize >= num_pieces {
            return Err(Aria2Error::FileIo(format!(
                "Native control file piece index out of range: {}",
                index
            )));
        }
        let length = reader.u32("in-flight piece length")?;
        if length == 0 || length > piece_length {
            return Err(Aria2Error::FileIo(format!(
                "Native control file in-flight piece length out of range: {}",
                length
            )));
        }
        let block_bitfield_length = usize::try_from(reader.u32("in-flight bitfield length")?)
            .map_err(|_| {
                Aria2Error::FileIo(
                    "Native control file in-flight bitfield length is too large".to_string(),
                )
            })?;
        let expected_block_bitfield_length = (length as usize).div_ceil(16 * 1024).div_ceil(8);
        if block_bitfield_length != expected_block_bitfield_length {
            return Err(Aria2Error::FileIo(format!(
                "Native control file in-flight bitfield length mismatch: expected {}, actual {}",
                expected_block_bitfield_length, block_bitfield_length
            )));
        }
        let bitfield = reader
            .bytes(block_bitfield_length, "in-flight bitfield")?
            .to_vec();
        in_flight_pieces.push(ControlFileInFlightPiece {
            index,
            length,
            bitfield,
        });
    }
    validate_in_flight_pieces(
        &in_flight_pieces,
        total_length,
        piece_length,
        num_pieces,
        &bitfield,
    )?;

    let project_extension = parse_project_extension(reader.remaining())?;
    let mut completed_length =
        completed_length_from_bitfield(total_length, piece_length, &bitfield);
    let mut checksum_algo = 0;
    let mut checksum_value = Vec::new();
    let mut torrent_checkpoint = is_torrent;
    let mut final_info_hash = torrent_info_hash;
    let mut final_piece_length = is_torrent.then_some(piece_length);
    if let Some(extension) = project_extension {
        if let Some(value) = extension.completed_length {
            if value > total_length {
                return Err(Aria2Error::FileIo(
                    "Control file extension completed length exceeds total length".to_string(),
                ));
            }
            completed_length = value;
        }
        checksum_algo = extension.checksum_algo;
        checksum_value = extension.checksum_value;
        if extension.torrent_checkpoint && !is_torrent {
            return Err(Aria2Error::FileIo(
                "Control file extension marks a non-torrent native file as torrent state"
                    .to_string(),
            ));
        }
        if (extension.torrent_info_hash.is_some() || extension.torrent_piece_length.is_some())
            && !is_torrent
        {
            return Err(Aria2Error::FileIo(
                "Control file extension has torrent metadata without native torrent state"
                    .to_string(),
            ));
        }
        if let (Some(native), Some(extended)) = (final_info_hash, extension.torrent_info_hash)
            && native != extended
        {
            return Err(Aria2Error::FileIo(
                "Native and project torrent info hashes differ".to_string(),
            ));
        }
        if let (Some(native), Some(extended)) = (final_piece_length, extension.torrent_piece_length)
            && native != extended
        {
            return Err(Aria2Error::FileIo(
                "Native and project torrent piece lengths differ".to_string(),
            ));
        }
        torrent_checkpoint |= extension.torrent_checkpoint;
        final_info_hash = final_info_hash.or(extension.torrent_info_hash);
        final_piece_length = final_piece_length.or(extension.torrent_piece_length);
    }

    Ok(Some(ControlFile {
        path: path.to_path_buf(),
        total_length,
        completed_length,
        upload_length,
        bitfield,
        in_flight_pieces,
        num_pieces,
        checksum_algo,
        checksum_value,
        torrent_checkpoint,
        torrent_info_hash: final_info_hash,
        torrent_piece_length: final_piece_length,
        native_piece_length: Some(piece_length),
        native_layout: true,
    }))
}

struct NativeReader<'a> {
    data: &'a [u8],
    offset: usize,
    network_order: bool,
}

impl<'a> NativeReader<'a> {
    fn new(data: &'a [u8], network_order: bool) -> Self {
        Self {
            data,
            offset: 2,
            network_order,
        }
    }

    fn payload(data: &'a [u8]) -> Self {
        Self {
            data,
            offset: 0,
            network_order: true,
        }
    }

    fn remaining(&self) -> &'a [u8] {
        &self.data[self.offset..]
    }

    fn bytes(&mut self, length: usize, field: &str) -> Result<&'a [u8]> {
        let end = self.offset.checked_add(length).ok_or_else(|| {
            Aria2Error::FileIo(format!("Native control file {field} length overflow"))
        })?;
        if end > self.data.len() {
            return Err(Aria2Error::FileIo(format!(
                "Truncated native control file {field}"
            )));
        }
        let bytes = &self.data[self.offset..end];
        self.offset = end;
        Ok(bytes)
    }

    fn u32(&mut self, field: &str) -> Result<u32> {
        let bytes = self.bytes(4, field)?;
        Ok(if self.network_order {
            u32::from_be_bytes(bytes.try_into().expect("four-byte native field"))
        } else {
            u32::from_ne_bytes(bytes.try_into().expect("four-byte native field"))
        })
    }

    fn u32_be(&mut self, field: &str) -> Result<u32> {
        let bytes = self.bytes(4, field)?;
        Ok(u32::from_be_bytes(
            bytes.try_into().expect("four-byte native field"),
        ))
    }

    fn u64(&mut self, field: &str) -> Result<u64> {
        let bytes = self.bytes(8, field)?;
        Ok(if self.network_order {
            u64::from_be_bytes(bytes.try_into().expect("eight-byte native field"))
        } else {
            u64::from_ne_bytes(bytes.try_into().expect("eight-byte native field"))
        })
    }
}

fn completed_length_from_bitfield(total_length: u64, piece_length: u32, bitfield: &[u8]) -> u64 {
    if total_length == 0 || piece_length == 0 {
        return 0;
    }
    let num_pieces = total_length.div_ceil(piece_length as u64) as usize;
    (0..num_pieces)
        .filter(|index| {
            bitfield
                .get(index / 8)
                .is_some_and(|byte| byte & (1 << (7 - index % 8)) != 0)
        })
        .map(|index| {
            let offset = index as u64 * piece_length as u64;
            total_length.saturating_sub(offset).min(piece_length as u64)
        })
        .sum()
}

fn u16_from_le(b: &[u8]) -> u16 {
    u16::from_le_bytes([b[0], b[1]])
}

fn u32_from_le(b: &[u8]) -> u32 {
    u32::from_le_bytes([b[0], b[1], b[2], b[3]])
}

fn u64_from_le(b: &[u8]) -> u64 {
    u64::from_le_bytes([b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]])
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn original_aria2_bt_control_file_fixtures_restore_verified_progress() {
        const INFO_HASH: [u8; 20] = [
            0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee,
            0xff, 0x00, 0xff, 0xff, 0xff, 0xff,
        ];
        // Keep these upstream aria2 test fixtures in-tree so tests do not
        // depend on the locally ignored aria2_original checkout.
        const V0000_FIXTURE: [u8; 94] = [
            0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x14, 0x00, 0x00, 0x00, 0x11, 0x22, 0x33, 0x44,
            0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0xff,
            0xff, 0xff, 0x00, 0x04, 0x00, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
            0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x00, 0xff, 0xff,
            0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00,
            0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
            0x00, 0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
        ];
        const V0001_FIXTURE: [u8; 94] = [
            0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x11, 0x22, 0x33, 0x44,
            0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x00, 0xff, 0xff,
            0xff, 0xff, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x40, 0x00,
            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xff, 0xff,
            0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
            0x00, 0x01, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
            0x02, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
        ];
        let fixtures: [(&str, &[u8]); 2] = [("v0000", &V0000_FIXTURE), ("v0001", &V0001_FIXTURE)];
        let dir = tempfile::tempdir().unwrap();

        for (version, bytes) in fixtures {
            let path = dir.path().join(format!("original-{version}.aria2"));
            tokio::fs::write(&path, bytes).await.unwrap();
            let loaded = ControlFile::load(&path).await.unwrap().unwrap();

            assert!(loaded.uses_native_layout(), "version {version}");
            assert!(loaded.is_torrent_checkpoint(), "version {version}");
            assert_eq!(
                loaded.torrent_info_hash(),
                Some(INFO_HASH),
                "version {version}"
            );
            assert_eq!(loaded.piece_length(), Some(1024), "version {version}");
            assert_eq!(loaded.total_length(), 80 * 1024, "version {version}");
            assert_eq!(loaded.upload_length(), 1024, "version {version}");
            assert_eq!(loaded.completed_length(), 79 * 1024, "version {version}");
            assert_eq!(
                loaded.bitfield(),
                &[0xff; 9].into_iter().chain([0xfe]).collect::<Vec<_>>()
            );
            assert_eq!(
                loaded.in_flight_pieces(),
                &[
                    ControlFileInFlightPiece {
                        index: 1,
                        length: 1024,
                        bitfield: vec![0],
                    },
                    ControlFileInFlightPiece {
                        index: 2,
                        length: 512,
                        bitfield: vec![0],
                    },
                ],
                "version {version}"
            );
        }
    }

    #[tokio::test]
    async fn native_control_file_roundtrips_in_flight_block_bitfields() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("partial-piece.bin.aria2");
        let mut control =
            ControlFile::open_or_create_with_piece_length(&path, 64 * 1024, 64 * 1024)
                .await
                .unwrap();
        control.mark_torrent_checkpoint();
        control.set_torrent_info_hash([0x31; 20]);
        control.set_in_flight_pieces(vec![ControlFileInFlightPiece {
            index: 0,
            length: 64 * 1024,
            bitfield: vec![0b1010_0000],
        }]);
        control.save().await.unwrap();

        let restored = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(restored.in_flight_pieces(), control.in_flight_pieces());
        assert_eq!(restored.bitfield(), &[0]);
    }

    #[test]
    fn test_control_path_uses_aria2_suffix() {
        assert_eq!(
            ControlFile::control_path_for(Path::new("payload.bin")),
            PathBuf::from("payload.bin.aria2")
        );
    }

    #[test]
    fn test_control_path_for_current_directory_is_a_sidecar_file() {
        let path = ControlFile::control_path_for(Path::new("."));
        assert_ne!(path, Path::new("."));
        assert_eq!(path.file_name(), Some(std::ffi::OsStr::new(".aria2")));
    }

    #[test]
    fn test_control_path_appends_after_multiple_extensions() {
        assert_eq!(
            ControlFile::control_path_for(Path::new("archive.tar.gz")),
            PathBuf::from("archive.tar.gz.aria2")
        );
    }

    #[test]
    fn test_control_path_for_multi_file_top_directory_is_a_sibling() {
        let path = ControlFile::control_path_for(Path::new("downloads/torrent"));
        assert_eq!(path, PathBuf::from("downloads/torrent.aria2"));
    }

    #[tokio::test]
    async fn test_control_file_new_and_save() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.aria2");

        let cf = ControlFile::open_or_create(&path, 10000, 10).await.unwrap();
        assert_eq!(cf.total_length(), 10000);
        assert_eq!(cf.completed_length(), 0);
        assert!(!cf.is_piece_done(0));

        cf.save().await.unwrap();

        assert!(path.exists());
        let data = tokio::fs::read(&path).await.unwrap();
        assert_eq!(&data[0..2], &1u16.to_be_bytes());
        assert_ne!(&data[0..4], CONTROL_MAGIC);
        assert!(
            data.windows(PROJECT_EXTENSION_MAGIC.len())
                .any(|window| { window == PROJECT_EXTENSION_MAGIC })
        );
    }

    #[tokio::test]
    async fn fixed_piece_checkpoint_persists_the_exact_piece_layout() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("fixed-piece.bin.aria2");
        let total_length: u64 = 101 * 1024 * 1024;
        let piece_length = 2 * 1024 * 1024;
        let pieces = total_length.div_ceil(piece_length as u64) as usize;

        let checkpoint =
            ControlFile::open_or_create_with_piece_length(&path, total_length, piece_length)
                .await
                .unwrap();
        assert_eq!(checkpoint.piece_length(), Some(piece_length));
        assert_eq!(checkpoint.bitfield().len(), pieces.div_ceil(8));
        checkpoint.save().await.unwrap();

        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.piece_length(), Some(piece_length));
        assert_eq!(loaded.bitfield().len(), pieces.div_ceil(8));
        assert!(
            ControlFile::open_or_create_with_piece_length(&path, total_length, piece_length / 2,)
                .await
                .is_err()
        );
    }

    #[tokio::test]
    async fn test_loads_native_aria2_v1_control_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("native.bin.aria2");
        let mut data = Vec::new();
        data.extend_from_slice(&1u16.to_be_bytes());
        data.extend_from_slice(&0u32.to_be_bytes());
        data.extend_from_slice(&0u32.to_be_bytes());
        data.extend_from_slice(&2u32.to_be_bytes());
        data.extend_from_slice(&3u64.to_be_bytes());
        data.extend_from_slice(&7u64.to_be_bytes());
        data.extend_from_slice(&1u32.to_be_bytes());
        data.push(0b1100_0000);
        data.extend_from_slice(&0u32.to_be_bytes());
        tokio::fs::write(&path, data).await.unwrap();

        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.total_length(), 3);
        assert_eq!(loaded.upload_length(), 7);
        assert_eq!(loaded.completed_length(), 3);
        assert_eq!(loaded.bitfield(), &[0b1100_0000]);
        assert!(!loaded.is_torrent_checkpoint());
    }

    #[tokio::test]
    async fn test_loads_native_aria2_v0_control_file() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("native-v0.bin.aria2");
        let mut data = Vec::new();
        data.extend_from_slice(&0u16.to_ne_bytes());
        data.extend_from_slice(&0u32.to_ne_bytes());
        data.extend_from_slice(&0u32.to_ne_bytes());
        data.extend_from_slice(&4u32.to_ne_bytes());
        data.extend_from_slice(&4u64.to_ne_bytes());
        data.extend_from_slice(&0u64.to_ne_bytes());
        data.extend_from_slice(&1u32.to_ne_bytes());
        data.push(0b1000_0000);
        data.extend_from_slice(&0u32.to_ne_bytes());
        tokio::fs::write(&path, data).await.unwrap();

        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.total_length(), 4);
        assert_eq!(loaded.completed_length(), 4);
    }

    #[tokio::test]
    async fn test_control_file_mark_and_check_pieces() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test.aria2");

        let mut cf = ControlFile::open_or_create(&path, 1000, 8).await.unwrap();

        cf.mark_piece_done(0);
        cf.mark_piece_done(3);
        cf.mark_piece_done(7);

        assert!(cf.is_piece_done(0));
        assert!(!cf.is_piece_done(1));
        assert!(cf.is_piece_done(3));
        assert!(!cf.is_piece_done(5));
        assert!(cf.is_piece_done(7));
        assert_eq!(cf.completed_pieces(), 3);

        cf.save().await.unwrap();

        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.completed_pieces(), 3);
        assert!(loaded.is_piece_done(0));
        assert!(loaded.is_piece_done(7));
    }

    #[tokio::test]
    async fn test_control_file_piece_completion_handles_short_final_piece() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("short_final.aria2");

        let mut cf = ControlFile::open_or_create(&path, 10, 3).await.unwrap();
        cf.mark_piece_done(0);
        cf.mark_piece_done(2);

        assert_eq!(cf.completed_length(), 6);
        assert!(!cf.is_piece_done(3));
    }

    #[tokio::test]
    async fn test_control_file_reload_restores_logical_piece_count() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("reload_piece_count.aria2");

        let mut cf = ControlFile::open_or_create(&path, 10, 3).await.unwrap();
        cf.mark_piece_done(0);
        cf.save().await.unwrap();

        let mut loaded = ControlFile::open_or_create(&path, 10, 3).await.unwrap();
        loaded.mark_piece_done(2);

        assert_eq!(loaded.completed_length(), 6);
        assert!(!loaded.is_piece_done(3));
    }

    #[tokio::test]
    async fn test_control_file_reload_normalizes_legacy_piece_count() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("normalize_piece_count.aria2");

        // This is the retired A2CF layout. It intentionally exercises the
        // compatibility reader instead of the current official writer.
        let mut legacy = Vec::new();
        legacy.extend_from_slice(CONTROL_MAGIC);
        legacy.extend_from_slice(&CONTROL_VERSION.to_le_bytes());
        legacy.push(0);
        legacy.extend_from_slice(&10u64.to_le_bytes());
        legacy.extend_from_slice(&6u64.to_le_bytes());
        legacy.extend_from_slice(&0u64.to_le_bytes());
        legacy.extend_from_slice(&1u64.to_le_bytes());
        legacy.push(0b1100_1000);
        tokio::fs::write(&path, legacy).await.unwrap();

        let loaded = ControlFile::open_or_create(&path, 10, 3).await.unwrap();

        assert_eq!(loaded.bitfield(), &[0b1100_0000]);
        assert_eq!(loaded.completed_pieces(), 2);
        assert!(loaded.is_piece_done(0));
        assert!(loaded.is_piece_done(1));
        assert!(!loaded.is_piece_done(2));
        assert!(!loaded.is_piece_done(3));
        assert_eq!(loaded.completed_length(), 8);
    }

    #[tokio::test]
    async fn test_control_file_roundtrip_with_checksum() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test_hash.aria2");

        let mut cf = ControlFile::open_or_create(&path, 5000, 5).await.unwrap();
        cf.checksum_algo = 2;
        cf.checksum_value = vec![0xAB; 20];
        cf.mark_piece_done(0);
        cf.mark_piece_done(2);
        cf.save().await.unwrap();

        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.total_length(), 5000);
        assert_eq!(loaded.checksum_algo, 2);
        assert_eq!(loaded.completed_pieces(), 2);
    }

    #[tokio::test]
    async fn test_control_file_rejects_corrupt_project_extension() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("corrupt_extension.aria2");
        let control = ControlFile::open_or_create(&path, 100, 4).await.unwrap();
        control.save().await.unwrap();

        let mut data = tokio::fs::read(&path).await.unwrap();
        *data.last_mut().unwrap() ^= 0x01;
        tokio::fs::write(&path, data).await.unwrap();

        assert!(ControlFile::load(&path).await.is_err());
    }

    #[tokio::test]
    async fn test_control_file_atomic_save() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test_atomic.aria2");

        let mut cf = ControlFile::open_or_create(&path, 999, 4).await.unwrap();
        cf.mark_piece_done(1);
        cf.save().await.unwrap();

        let tmp_path = path.with_extension("aria2.tmp");
        assert!(!tmp_path.exists());
        assert!(path.exists());
    }

    #[tokio::test]
    async fn test_control_file_load_nonexistent() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("nonexistent.aria2");
        let result = ControlFile::load(&path).await.unwrap();
        assert!(result.is_none());
    }

    #[tokio::test]
    async fn test_control_file_load_invalid_magic() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("bad.aria2");
        tokio::fs::write(&path, b"NOT_A2CF_DATA").await.unwrap();

        let result = ControlFile::load(&path).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_control_file_load_truncated_header() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("truncated.aria2");
        for length in [0usize, 7, 8, 38] {
            let mut data = vec![0u8; length];
            if length >= 4 {
                data[..4].copy_from_slice(CONTROL_MAGIC);
            }
            tokio::fs::write(&path, &data).await.unwrap();
            assert!(ControlFile::load(&path).await.is_err(), "length={length}");
        }
    }

    #[tokio::test]
    async fn test_control_file_rejects_invalid_checksum_and_bitfield_lengths() {
        let dir = tempfile::tempdir().expect("failed to create temporary directory");
        let path = dir.path().join("malformed.aria2");
        let mut data = vec![0u8; CONTROL_HEADER_LEN];
        data[..4].copy_from_slice(CONTROL_MAGIC);
        data[6] = FLAG_HAS_CHECKSUM;
        data[31..39].copy_from_slice(&0u64.to_le_bytes());
        data.push(2);
        tokio::fs::write(&path, &data).await.unwrap();
        assert!(ControlFile::load(&path).await.is_err());

        let mut data = vec![0u8; CONTROL_HEADER_LEN];
        data[..4].copy_from_slice(CONTROL_MAGIC);
        data[31..39].copy_from_slice(&1u64.to_le_bytes());
        tokio::fs::write(&path, &data).await.unwrap();
        assert!(ControlFile::load(&path).await.is_err());
    }

    #[tokio::test]
    async fn test_control_path_for_output() {
        let out = Path::new("/downloads/file.iso");
        let ctrl = ControlFile::control_path_for(out);
        assert_eq!(ctrl.extension().unwrap().to_str().unwrap(), "aria2");
        assert_eq!(ctrl, PathBuf::from("/downloads/file.iso.aria2"));
    }

    #[tokio::test]
    async fn test_control_file_update_completed_length() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("test_len.aria2");

        let mut cf = ControlFile::open_or_create(&path, 8000, 8).await.unwrap();
        cf.update_completed_length(3500);
        assert_eq!(cf.completed_length(), 3500);

        cf.update_completed_length(9000);
        assert_eq!(cf.completed_length(), 8000);

        cf.update_completed_length(3500);
        cf.save().await.unwrap();
        let loaded = ControlFile::load(&path).await.unwrap().unwrap();
        assert_eq!(loaded.completed_length(), 3500);
    }
}