android-doctor 0.1.0

Extract and audit Android OTA and firmware images (payload.bin, super.img, ext4, erofs) without running them
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
//! A/B OTA payloads (`payload.bin`, "CrAU" version 2), full OTAs only.
//!
//! The format is the AOSP `update_engine` payload (`update_metadata.proto`, Apache-2.0); this is
//! written from that description and checked against ssut/payload-dumper-go (Apache-2.0).
use crate::extract::{ExtractOptions, create_part};
use anyhow::{Context, Result, anyhow, bail, ensure};
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use prost::Message;
use sha2::{Digest, Sha256};
use std::fs::File;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

/// The protobuf messages, hand-written from `update_metadata.proto` (only the fields used here).
/// Unknown fields are skipped when decoding.
pub mod pb {
    #[derive(Clone, PartialEq, prost::Message)]
    pub struct Extent {
        #[prost(uint64, optional, tag = "1")]
        pub start_block: Option<u64>,
        #[prost(uint64, optional, tag = "2")]
        pub num_blocks: Option<u64>,
    }

    #[derive(Clone, PartialEq, prost::Message)]
    pub struct PartitionInfo {
        #[prost(uint64, optional, tag = "1")]
        pub size: Option<u64>,
        #[prost(bytes = "vec", optional, tag = "2")]
        pub hash: Option<Vec<u8>>,
    }

    #[derive(Clone, PartialEq, prost::Message)]
    pub struct InstallOperation {
        /// `InstallOperation.Type` as its integer value (kept raw so unknown types survive).
        #[prost(int32, required, tag = "1")]
        pub r#type: i32,
        #[prost(uint64, optional, tag = "2")]
        pub data_offset: Option<u64>,
        #[prost(uint64, optional, tag = "3")]
        pub data_length: Option<u64>,
        #[prost(message, repeated, tag = "4")]
        pub src_extents: Vec<Extent>,
        #[prost(message, repeated, tag = "6")]
        pub dst_extents: Vec<Extent>,
        #[prost(bytes = "vec", optional, tag = "8")]
        pub data_sha256_hash: Option<Vec<u8>>,
    }

    #[derive(Clone, PartialEq, prost::Message)]
    pub struct PartitionUpdate {
        #[prost(string, required, tag = "1")]
        pub partition_name: String,
        #[prost(message, optional, tag = "6")]
        pub old_partition_info: Option<PartitionInfo>,
        #[prost(message, optional, tag = "7")]
        pub new_partition_info: Option<PartitionInfo>,
        #[prost(message, repeated, tag = "8")]
        pub operations: Vec<InstallOperation>,
        #[prost(message, optional, tag = "10")]
        pub hash_tree_data_extent: Option<Extent>,
        #[prost(message, optional, tag = "11")]
        pub hash_tree_extent: Option<Extent>,
        #[prost(message, optional, tag = "14")]
        pub fec_data_extent: Option<Extent>,
        #[prost(message, optional, tag = "15")]
        pub fec_extent: Option<Extent>,
    }

    #[derive(Clone, PartialEq, prost::Message)]
    pub struct DeltaArchiveManifest {
        #[prost(uint32, optional, tag = "3")]
        pub block_size: Option<u32>,
        #[prost(uint32, optional, tag = "12")]
        pub minor_version: Option<u32>,
        #[prost(message, repeated, tag = "13")]
        pub partitions: Vec<PartitionUpdate>,
    }
}

const MAGIC: &[u8; 4] = b"CrAU";
const HEADER_LEN: u64 = 24;
const PAYLOAD_NAME: &str = "payload.bin";
/// A manifest is tens of KiB to a few MiB; this stops a hostile header from asking for gigabytes.
const MAX_MANIFEST_BYTES: u64 = 64 << 20;
/// Real payloads list a few dozen partitions.
const MAX_PARTITIONS: usize = 1024;
/// One operation's compressed data (update_engine writes 2 MiB chunks; this leaves a wide margin).
const MAX_BLOB_BYTES: u64 = 512 << 20;
/// Largest partition accepted. Real ones are at most about 10 GB; the cap also bounds the time
/// spent hashing zeros for a tiny payload that declares a huge partition (about 40 s at 64 GiB).
const MAX_PARTITION_BYTES: u64 = 64 << 30;
const COPY_BUF: usize = 1 << 20;

// InstallOperation.Type values (update_metadata.proto; 14 is ZSTD in current AOSP).
const OP_REPLACE: i32 = 0;
const OP_REPLACE_BZ: i32 = 1;
const OP_ZERO: i32 = 6;
const OP_DISCARD: i32 = 7;
const OP_REPLACE_XZ: i32 = 8;
const OP_ZSTD: i32 = 14;
// Incremental operations that read from the base partition image supplied by --base.
const OP_SOURCE_COPY: i32 = 4;
const OP_SOURCE_BSDIFF: i32 = 5;

fn op_name(t: i32) -> String {
    match t {
        0 => "REPLACE".into(),
        1 => "REPLACE_BZ".into(),
        2 => "MOVE".into(),
        3 => "BSDIFF".into(),
        4 => "SOURCE_COPY".into(),
        5 => "SOURCE_BSDIFF".into(),
        6 => "ZERO".into(),
        7 => "DISCARD".into(),
        8 => "REPLACE_XZ".into(),
        9 => "PUFFDIFF".into(),
        10 => "BROTLI_BSDIFF".into(),
        11 => "ZUCCHINI".into(),
        12 => "LZ4DIFF_BSDIFF".into(),
        13 => "LZ4DIFF_PUFFDIFF".into(),
        14 => "ZSTD".into(),
        other => format!("type {other}"),
    }
}

/// Where the payload bytes live: `len` bytes starting at `base` in the file at `path`
/// (`base` is 0 for a plain `payload.bin`, the data offset of a stored zip entry otherwise).
#[derive(Debug, Clone)]
pub struct Located {
    pub path: PathBuf,
    pub base: u64,
    pub len: u64,
    /// Directory holding the base partition images for an incremental update, if supplied.
    pub base_dir: Option<PathBuf>,
}

/// Find a payload in `input`: a directory holding `payload.bin`, a zip with a stored
/// `payload.bin`, or a file that starts with the payload magic. `None` means "not an A/B OTA".
pub fn locate(input: &Path, base_dir: Option<&Path>) -> Result<Option<Located>> {
    if input.is_dir() {
        let path = input.join(PAYLOAD_NAME);
        return match std::fs::metadata(&path) {
            Ok(m) if m.is_file() => Ok(Some(Located {
                base_dir: base_dir.map(Path::to_path_buf),
                path,
                base: 0,
                len: m.len(),
            })),
            _ => Ok(None),
        };
    }
    let mut file = File::open(input).with_context(|| format!("opening {}", input.display()))?;
    let mut magic = [0u8; 4];
    let got = file.read(&mut magic)?;
    if got == 4 && &magic == MAGIC {
        return Ok(Some(Located {
            base_dir: base_dir.map(Path::to_path_buf),
            path: input.to_path_buf(),
            base: 0,
            len: file.metadata()?.len(),
        }));
    }
    if got == 4 && &magic[..2] == b"PK" {
        file.seek(SeekFrom::Start(0))?;
        let Ok(mut zip) = zip::ZipArchive::new(file) else {
            return Ok(None);
        };
        let Ok(entry) = zip.by_name(PAYLOAD_NAME) else {
            return Ok(None);
        };
        ensure!(
            entry.compression() == zip::CompressionMethod::Stored,
            "{PAYLOAD_NAME} is compressed inside the zip; unzip it first (OTA zips normally store it uncompressed)"
        );
        let base = entry
            .data_start()
            .context("payload.bin has no data offset in the zip")?;
        return Ok(Some(Located {
            base_dir: base_dir.map(Path::to_path_buf),
            path: input.to_path_buf(),
            base,
            len: entry.size(),
        }));
    }
    Ok(None)
}

/// A parsed payload header and manifest.
pub struct Payload {
    pub manifest: pb::DeltaArchiveManifest,
    /// Offset of the data blobs, relative to the start of the payload.
    pub data_base: u64,
}

fn be64(b: &[u8]) -> u64 {
    u64::from_be_bytes(b[..8].try_into().expect("8 bytes"))
}

/// Read the header and manifest from the start of a payload of `len` bytes.
pub fn read_payload<R: Read>(r: &mut R, len: u64) -> Result<Payload> {
    ensure!(
        len >= HEADER_LEN,
        "payload is only {len} bytes, too short for a header"
    );
    let mut h = [0u8; HEADER_LEN as usize];
    r.read_exact(&mut h).context("reading the payload header")?;
    ensure!(&h[..4] == MAGIC, "not an A/B payload: bad magic");
    let version = be64(&h[4..12]);
    ensure!(
        version == 2,
        "unsupported payload version {version} (only version 2 is supported)"
    );
    let manifest_size = be64(&h[12..20]);
    let sig_size = u32::from_be_bytes(h[20..24].try_into().expect("4 bytes")) as u64;
    ensure!(
        manifest_size <= MAX_MANIFEST_BYTES && HEADER_LEN + manifest_size + sig_size <= len,
        "payload header claims a {manifest_size}-byte manifest and a {sig_size}-byte signature, which do not fit in {len} bytes"
    );
    let mut raw = vec![0u8; manifest_size as usize];
    r.read_exact(&mut raw).context("reading the manifest")?;
    let manifest = pb::DeltaArchiveManifest::decode(&raw[..]).context("decoding the manifest")?;
    Ok(Payload {
        manifest,
        data_base: HEADER_LEN + manifest_size + sig_size,
    })
}

fn open_payload(l: &Located) -> Result<Payload> {
    let mut f = File::open(&l.path).with_context(|| format!("opening {}", l.path.display()))?;
    f.seek(SeekFrom::Start(l.base))?;
    read_payload(&mut f, l.len)
}

fn block_size(m: &pb::DeltaArchiveManifest) -> Result<u64> {
    let bs = m.block_size.unwrap_or(4096) as u64;
    ensure!(
        (512..=(1 << 20)).contains(&bs) && bs.is_multiple_of(512),
        "unsupported block size {bs}"
    );
    Ok(bs)
}

fn is_data_op(t: i32) -> bool {
    matches!(t, OP_REPLACE | OP_REPLACE_BZ | OP_REPLACE_XZ | OP_ZSTD)
}

/// Incremental operations that read from the base partition image (--base).
fn is_source_op(t: i32) -> bool {
    matches!(t, OP_SOURCE_COPY | OP_SOURCE_BSDIFF)
}

fn has_verity(p: &pb::PartitionUpdate) -> bool {
    [
        &p.hash_tree_data_extent,
        &p.hash_tree_extent,
        &p.fec_data_extent,
        &p.fec_extent,
    ]
    .iter()
    .any(|e| e.as_ref().is_some_and(|e| e.num_blocks.unwrap_or(0) > 0))
}

/// Byte extents `(offset, length)` of an operation's destination, validated.
fn byte_extents(op: &pb::InstallOperation, bs: u64) -> Result<Vec<(u64, u64)>> {
    ensure!(
        !op.dst_extents.is_empty(),
        "operation has no destination extents"
    );
    let mut out = Vec::with_capacity(op.dst_extents.len());
    for e in &op.dst_extents {
        let (start, num) = (e.start_block.unwrap_or(0), e.num_blocks.unwrap_or(0));
        ensure!(start != u64::MAX, "sparse-hole extents are not supported");
        let end = start.checked_add(num).context("extent overflows")?;
        // `end` is at least `start` and `num`, so if `end * bs` fits, so do `start * bs` and `num * bs`
        ensure!(end.checked_mul(bs).is_some(), "extent end overflows");
        if num > 0 {
            out.push((start * bs, num * bs));
        }
    }
    Ok(out)
}

/// One data operation, validated: where its blob is and where its output goes.
struct DataOp<'a> {
    op: &'a pb::InstallOperation,
    /// Destination byte extents `(offset, length)`.
    extents: Vec<(u64, u64)>,
    /// Offset of the blob in the payload file (relative to the payload start) and its length.
    start: u64,
    len: u64,
}

/// Everything about one partition that is checked before any file is created.
struct Plan<'a> {
    name: &'a str,
    update: &'a pb::PartitionUpdate,
    size: u64,
    data_ops: Vec<DataOp<'a>>,
    blob_bytes: u64,
}

fn plan_partition<'a>(
    p: &'a Payload,
    update: &'a pb::PartitionUpdate,
    file_len: u64,
    bs: u64,
) -> Result<Plan<'a>> {
    let name = update.partition_name.as_str();
    let mut ranges: Vec<(u64, u64)> = Vec::new();
    let mut data_ops = Vec::new();
    let mut blob_bytes = 0u64;
    for (i, op) in update.operations.iter().enumerate() {
        let ctx = || format!("partition {name}, operation {i} ({})", op_name(op.r#type));
        let extents = byte_extents(op, bs).with_context(ctx)?;
        ranges.extend(extents.iter().copied());
        if is_data_op(op.r#type) || is_source_op(op.r#type) {
            let (off, len) = (op.data_offset.unwrap_or(0), op.data_length.unwrap_or(0));
            // SOURCE_COPY has no blob data; SOURCE_BSDIFF has a bsdiff patch as its data.
            if is_data_op(op.r#type) {
                ensure!(len > 0, "{}: no data", ctx());
            }
            ensure!(
                len <= MAX_BLOB_BYTES,
                "{}: {len} bytes of data is over the {MAX_BLOB_BYTES}-byte limit",
                ctx()
            );
            let start = p
                .data_base
                .checked_add(off)
                .context("blob offset overflows")
                .with_context(ctx)?;
            let end = start
                .checked_add(len)
                .context("blob end overflows")
                .with_context(ctx)?;
            ensure!(
                end <= file_len,
                "{}: data ends at {end} but the payload is {file_len} bytes",
                ctx()
            );
            // Source ops also need src_extents that reference the old partition.
            if is_source_op(op.r#type) {
                ensure!(
                    !op.src_extents.is_empty(),
                    "{}: source operation has no source extents",
                    ctx()
                );
            }
            data_ops.push(DataOp {
                op,
                extents,
                start,
                len,
            });
            blob_bytes += len;
        } else {
            ensure!(
                op.r#type == OP_ZERO || op.r#type == OP_DISCARD,
                "{}: unsupported",
                ctx()
            );
        }
    }
    // destination extents must not overlap: operations run in parallel and in any order
    ranges.sort_unstable();
    if let Some(w) = ranges.windows(2).find(|w| w[0].0 + w[0].1 > w[1].0) {
        bail!(
            "partition {name}: overlapping destination extents at byte {}",
            w[1].0
        );
    }
    let furthest = ranges.last().map_or(0, |r| r.0 + r.1);
    let declared = update.new_partition_info.as_ref().and_then(|i| i.size);
    let size = declared.unwrap_or(furthest);
    ensure!(
        size <= MAX_PARTITION_BYTES,
        "partition {name}: {size} bytes is over the {MAX_PARTITION_BYTES}-byte limit"
    );
    ensure!(
        furthest <= size,
        "partition {name}: extents reach byte {furthest} but the partition is {size} bytes"
    );
    data_ops.sort_by_key(|o| o.start); // read the payload front to back
    Ok(Plan {
        name,
        update,
        size,
        data_ops,
        blob_bytes,
    })
}

/// Reject incremental payloads when no base image is supplied.
///
/// When `opts.base` is `Some`, SOURCE_COPY and SOURCE_BSDIFF are allowed (the caller has
/// provided the previous partition image to read from). Operations like MOVE, BSDIFF, PUFFDIFF
/// and anything else that is not a data op, source op, or zero/discard is still rejected.
fn check_full(
    payload: &Payload,
    selected: &[&pb::PartitionUpdate],
    opts: &ExtractOptions,
) -> Result<()> {
    // When a base directory is supplied, SOURCE_COPY and SOURCE_BSDIFF are supported.
    let allowed_inc = opts.base.is_some();
    let mut needs_source: Vec<String> = Vec::new();
    for part in selected {
        let mut kinds: Vec<String> = part
            .operations
            .iter()
            .map(|o| o.r#type)
            .filter(|t| !is_data_op(*t) && *t != OP_ZERO && *t != OP_DISCARD && !is_source_op(*t))
            .map(op_name)
            .collect();
        if !allowed_inc {
            for t in part.operations.iter().map(|o| o.r#type) {
                if is_source_op(t) {
                    kinds.push(op_name(t));
                }
            }
        }
        kinds.sort();
        kinds.dedup();
        if !kinds.is_empty() {
            needs_source.push(format!("{} ({})", part.partition_name, kinds.join(", ")));
        } else if !allowed_inc && part.old_partition_info.is_some() {
            needs_source.push(format!(
                "{} (built against an older partition)",
                part.partition_name
            ));
        }
    }
    if !needs_source.is_empty() {
        bail!(
            "this is an incremental OTA (minor version {}); it needs the previous build, which is not supported: {}",
            payload.manifest.minor_version.unwrap_or(0),
            needs_source.join("; ")
        );
    }
    Ok(())
}

fn is_safe_name(p: &str) -> bool {
    !p.is_empty()
        && p.chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
}

/// Validate names, apply `--only`, and return the selected partitions sorted by name.
fn select<'a>(
    m: &'a pb::DeltaArchiveManifest,
    only: &Option<Vec<String>>,
) -> Result<Vec<&'a pb::PartitionUpdate>> {
    ensure!(!m.partitions.is_empty(), "the payload lists no partitions");
    ensure!(
        m.partitions.len() <= MAX_PARTITIONS,
        "the payload lists {} partitions, over the limit of {MAX_PARTITIONS}",
        m.partitions.len()
    );
    let mut all: Vec<&pb::PartitionUpdate> = m.partitions.iter().collect();
    all.sort_by(|a, b| a.partition_name.cmp(&b.partition_name));
    if let Some(bad) = all.iter().find(|p| !is_safe_name(&p.partition_name)) {
        bail!("unsafe partition name {:?}", bad.partition_name);
    }
    if let Some(w) = all.windows(2).find(|w| {
        w[0].partition_name
            .eq_ignore_ascii_case(&w[1].partition_name)
    }) {
        if w[0].partition_name == w[1].partition_name {
            bail!(
                "partition {} is listed more than once and would overwrite itself",
                w[0].partition_name
            );
        }
        bail!(
            "{} and {} differ only by case and would overwrite each other",
            w[0].partition_name,
            w[1].partition_name
        );
    }
    if let Some(only) = only {
        let names: Vec<&str> = all.iter().map(|p| p.partition_name.as_str()).collect();
        if let Some(bad) = only.iter().find(|o| !names.contains(&o.as_str())) {
            bail!("unknown name {bad:?}; available: {}", names.join(", "));
        }
        all.retain(|p| only.contains(&p.partition_name));
    }
    Ok(all)
}

/// `Some(names)` when `input` is an A/B OTA, `None` otherwise (used by `report`).
pub fn partition_names(input: &Path) -> Result<Option<Vec<String>>> {
    // Only the manifest is read here, so no base image is needed.
    let Some(loc) = locate(input, None)? else {
        return Ok(None);
    };
    let payload = open_payload(&loc)?;
    let mut names: Vec<String> = payload
        .manifest
        .partitions
        .iter()
        .map(|p| p.partition_name.clone())
        .collect();
    names.sort();
    Ok(Some(names))
}

/// What `extract` would write, with sizes in bytes; `None` when `input` is not an A/B OTA.
pub fn list(input: &Path, opts: &ExtractOptions) -> Result<Option<Vec<(String, u64)>>> {
    let Some(loc) = locate(input, opts.base.as_deref())? else {
        return Ok(None);
    };
    let payload = open_payload(&loc)?;
    let bs = block_size(&payload.manifest)?;
    let mut out = Vec::new();
    for part in select(&payload.manifest, &opts.only)? {
        let plan = plan_partition(&payload, part, loc.len, bs)?;
        out.push((format!("{}.img", plan.name), plan.size));
    }
    Ok(Some(out))
}

/// Writes a stream into a list of byte extents of an output file.
struct ExtentWriter<'a> {
    file: File,
    extents: &'a [(u64, u64)],
    idx: usize,
    within: u64,
}

impl Write for ExtentWriter<'_> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let Some(&(off, len)) = self.extents.get(self.idx) else {
            return Err(std::io::Error::other(
                "more data than the destination extents hold",
            ));
        };
        let n = buf.len().min((len - self.within) as usize);
        self.file.seek(SeekFrom::Start(off + self.within))?;
        self.file.write_all(&buf[..n])?;
        self.within += n as u64;
        if self.within == len {
            self.idx += 1;
            self.within = 0;
        }
        Ok(n)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

/// Copy exactly `expected` bytes from `r` to `w`; fewer or more is an error.
fn copy_exact<R: Read, W: Write>(mut r: R, w: &mut W, expected: u64) -> Result<()> {
    let mut buf = vec![0u8; COPY_BUF];
    let mut left = expected;
    while left > 0 {
        let n = r.read(&mut buf[..left.min(COPY_BUF as u64) as usize])?;
        ensure!(
            n > 0,
            "data ends {left} bytes before the destination extents are full"
        );
        w.write_all(&buf[..n])?;
        left -= n as u64;
    }
    ensure!(
        r.read(&mut [0u8; 1])? == 0,
        "data holds more than the {expected} bytes the destination extents hold"
    );
    Ok(())
}

/// Read the source bytes referenced by `src_extents` from the base partition image at
/// `base_dir/<partition>.img`.
/// The total bytes read must match `expected` (the destination extent size).
fn read_source(
    op: &pb::InstallOperation,
    src_path: &Path,
    bs: u64,
    expected: u64,
) -> Result<Vec<u8>> {
    let total: u64 = op
        .src_extents
        .iter()
        .map(|e| e.num_blocks.unwrap_or(0) * bs)
        .sum();
    ensure!(
        total == expected,
        "source extents ({total} bytes) do not match destination extents ({expected} bytes)"
    );
    let mut src = File::open(src_path)
        .with_context(|| format!("opening base image {}", src_path.display()))?;
    let mut out = Vec::with_capacity(expected as usize);
    for e in &op.src_extents {
        let (s, n) = (e.start_block.unwrap_or(0), e.num_blocks.unwrap_or(0));
        let off = s.checked_mul(bs).context("source start overflows")?;
        let len = n.checked_mul(bs).context("source length overflows")?;
        src.seek(SeekFrom::Start(off))?;
        let mut buf = vec![0u8; len as usize];
        src.read_exact(&mut buf).context("reading source data")?;
        out.extend_from_slice(&buf);
    }
    Ok(out)
}

/// For SOURCE_COPY: read source bytes from the base image and write them to the destination.
fn copy_source(
    op: &pb::InstallOperation,
    _blob: &[u8],
    src_path: &Path,
    bs: u64,
    expected: &u64,
    w: &mut ExtentWriter,
) -> Result<()> {
    let src = read_source(op, src_path, bs, *expected)?;
    copy_exact(&src[..], w, *expected)
}

/// Where an operation's bytes live, and where its source range is read from.
struct OpContext<'a> {
    payload_base: u64,
    /// Base image directory for an incremental update; empty when none was supplied.
    src_base: &'a Path,
    block_size: u64,
}

fn run_data_op(
    payload: &mut File,
    op: &pb::InstallOperation,
    extents: &[(u64, u64)],
    start: u64,
    len: u64,
    out_path: &Path,
    ctx_src: &OpContext<'_>,
) -> Result<()> {
    let OpContext {
        payload_base,
        src_base,
        block_size: bs,
    } = *ctx_src;
    payload.seek(SeekFrom::Start(payload_base + start))?;
    let mut blob = vec![0u8; len as usize];
    payload
        .read_exact(&mut blob)
        .context("reading the operation data")?;
    if let Some(want) = op.data_sha256_hash.as_deref().filter(|h| !h.is_empty()) {
        let got = Sha256::digest(&blob);
        ensure!(
            got.as_slice() == want,
            "operation data does not match its sha256"
        );
    }
    let expected: u64 = extents.iter().map(|e| e.1).sum();
    // each operation opens the file itself (cheap): no shared handle, no lock, no open-file limit
    let file = File::options()
        .write(true)
        .open(out_path)
        .with_context(|| format!("opening {}", out_path.display()))?;
    let mut w = ExtentWriter {
        file,
        extents,
        idx: 0,
        within: 0,
    };
    match op.r#type {
        OP_REPLACE => copy_exact(&blob[..], &mut w, expected),
        OP_REPLACE_BZ => copy_exact(bzip2::read::BzDecoder::new(&blob[..]), &mut w, expected),
        OP_REPLACE_XZ => copy_exact(
            lzma_rust2::XzReader::new(&blob[..], false),
            &mut w,
            expected,
        ),
        OP_ZSTD => {
            let dec = ruzstd::decoding::StreamingDecoder::new(&blob[..])
                .map_err(|e| anyhow!("zstd: {e}"))?;
            copy_exact(dec, &mut w, expected)
        }
        OP_SOURCE_COPY => {
            // Source is the old partition image; copy bytes from src_extents to dst_extents.
            copy_source(op, &blob, src_base, bs, &expected, &mut w)?;
            Ok(())
        }
        OP_SOURCE_BSDIFF => {
            // Blob is a BSDIFF4 patch; apply it to the source data read from src_extents.
            let src = read_source(op, src_base, bs, expected)?;
            let patched = crate::bsdiff::apply(&blob, &src)?;
            copy_exact(&patched[..], &mut w, expected)?;
            Ok(())
        }
        other => bail!("unexpected operation type {}", op_name(other)),
    }
}

fn hash_file(path: &Path, size: u64) -> Result<Vec<u8>> {
    let mut f = File::open(path)?;
    let mut hasher = Sha256::new();
    let mut buf = vec![0u8; COPY_BUF];
    let mut left = size;
    while left > 0 {
        let n = f.read(&mut buf[..left.min(COPY_BUF as u64) as usize])?;
        ensure!(n > 0, "image is shorter than {size} bytes");
        hasher.update(&buf[..n]);
        left -= n as u64;
    }
    Ok(hasher.finalize().to_vec())
}

fn hex(b: &[u8]) -> String {
    b.iter().map(|x| format!("{x:02x}")).collect()
}

fn part_path(out_dir: &Path, name: &str) -> PathBuf {
    out_dir.join(format!("{name}.img.part"))
}

/// Extract every (selected) partition of an A/B OTA into `out_dir`. Returns `None` when `input`
/// is not an A/B OTA. All-or-nothing: on any error every `.part` file is removed and no image is
/// created. Each result carries a note on how far the image was verified.
pub fn extract(
    input: &Path,
    out_dir: &Path,
    opts: &ExtractOptions,
) -> Result<Option<Vec<(PathBuf, String)>>> {
    let Some(loc) = locate(input, opts.base.as_deref())? else {
        return Ok(None);
    };
    let payload = open_payload(&loc)?;
    let bs = block_size(&payload.manifest)?;
    let selected = select(&payload.manifest, &opts.only)?;
    check_full(&payload, &selected, opts)?;
    let plans = selected
        .iter()
        .map(|u| plan_partition(&payload, u, loc.len, bs))
        .collect::<Result<Vec<_>>>()?;
    if !opts.force {
        let existing: Vec<String> = plans
            .iter()
            .map(|p| format!("{}.img", p.name))
            .filter(|n| out_dir.join(n).symlink_metadata().is_ok())
            .collect();
        ensure!(
            existing.is_empty(),
            "{} already exist in {}; pass --force to overwrite",
            existing.join(", "),
            out_dir.display()
        );
    }
    std::fs::create_dir_all(out_dir)?;

    // every output file is created and sized up front, then closed again
    let result = (|| -> Result<Vec<(PathBuf, String)>> {
        for plan in &plans {
            create_part(&part_path(out_dir, plan.name))?.set_len(plan.size)?;
        }
        run_operations(&loc, &plans, out_dir)?;
        verify_and_publish(&plans, out_dir)
    })();
    if result.is_err() {
        for plan in &plans {
            let _ = std::fs::remove_file(part_path(out_dir, plan.name));
        }
    }
    result.map(Some)
}

fn run_operations(loc: &Located, plans: &[Plan], out_dir: &Path) -> Result<()> {
    let base_dir: Option<&Path> = loc.base_dir.as_deref();
    // flat task list in payload order: (partition index, operation index within the plan)
    let mut tasks: Vec<(usize, usize, u64)> = Vec::new();
    for (pi, plan) in plans.iter().enumerate() {
        for (oi, op) in plan.data_ops.iter().enumerate() {
            tasks.push((pi, oi, op.start));
        }
    }
    tasks.sort_by_key(|t| t.2);

    let multi = MultiProgress::new(); // hidden automatically when stderr is not a terminal
    let style = ProgressStyle::with_template("{prefix:>12} [{bar:30}] {bytes}/{total_bytes}")
        .expect("valid progress template")
        .progress_chars("=> ");
    let bars: Vec<ProgressBar> = plans
        .iter()
        .map(|p| {
            let bar = multi.add(ProgressBar::new(p.blob_bytes).with_style(style.clone()));
            bar.set_prefix(p.name.to_string());
            bar
        })
        .collect();

    let next = AtomicUsize::new(0);
    let stop = AtomicBool::new(false);
    let first_error: Mutex<Option<anyhow::Error>> = Mutex::new(None);
    let workers = std::thread::available_parallelism()
        .map_or(4, |n| n.get())
        .min(tasks.len().max(1));
    std::thread::scope(|scope| {
        for _ in 0..workers {
            scope.spawn(|| {
                let fail = |e: anyhow::Error| {
                    stop.store(true, Ordering::SeqCst);
                    let mut slot = first_error.lock().expect("error slot");
                    if slot.is_none() {
                        *slot = Some(e);
                    }
                };
                let mut file = match File::open(&loc.path) {
                    Ok(f) => f,
                    Err(e) => {
                        return fail(anyhow!(e).context(format!("opening {}", loc.path.display())));
                    }
                };
                while !stop.load(Ordering::SeqCst) {
                    let i = next.fetch_add(1, Ordering::SeqCst);
                    let Some(&(pi, oi, _)) = tasks.get(i) else {
                        break;
                    };
                    let plan = &plans[pi];
                    let d = &plan.data_ops[oi];
                    let (start, len) = (d.start, d.len);
                    let out_path = part_path(out_dir, plan.name);
                    let ctx = OpContext {
                        payload_base: loc.base,
                        src_base: base_dir.unwrap_or(Path::new("")),
                        block_size: 4096,
                    };
                    let ran = run_data_op(&mut file, d.op, &d.extents, start, len, &out_path, &ctx);
                    match ran {
                        Ok(()) => bars[pi].inc(len),
                        Err(e) => {
                            return fail(e.context(format!(
                                "partition {}, operation at payload byte {}",
                                plan.name, start
                            )));
                        }
                    }
                }
            });
        }
    });
    for bar in &bars {
        bar.finish_and_clear();
    }
    match first_error.into_inner().expect("error slot") {
        Some(e) => Err(e),
        None => Ok(()),
    }
}

/// Check each image's SHA-256 against the manifest, then rename `.part` files into place.
fn verify_and_publish(plans: &[Plan], out_dir: &Path) -> Result<Vec<(PathBuf, String)>> {
    let notes: Vec<Result<String>> = std::thread::scope(|scope| {
        let handles: Vec<_> = plans
            .iter()
            .map(|plan| {
                scope.spawn(move || -> Result<String> {
                    let want = plan.update.new_partition_info.as_ref().and_then(|i| i.hash.as_deref()).filter(|h| !h.is_empty());
                    let Some(want) = want else { return Ok("no hash in the payload to check".to_string()) };
                    if has_verity(plan.update) {
                        return Ok("sha256 not checked: the payload leaves out the dm-verity/FEC data the device adds".to_string());
                    }
                    let got = hash_file(&part_path(out_dir, plan.name), plan.size)?;
                    ensure!(
                        got == want,
                        "partition {}: sha256 mismatch (manifest {}, image {})",
                        plan.name,
                        hex(want),
                        hex(&got)
                    );
                    Ok("sha256 verified".to_string())
                })
            })
            .collect();
        handles
            .into_iter()
            .map(|h| {
                h.join()
                    .unwrap_or_else(|_| Err(anyhow!("verification thread panicked")))
            })
            .collect()
    });
    let notes = notes.into_iter().collect::<Result<Vec<_>>>()?;
    let mut out = Vec::new();
    for (plan, note) in plans.iter().zip(notes) {
        let final_path = out_dir.join(format!("{}.img", plan.name));
        std::fs::rename(part_path(out_dir, plan.name), &final_path)?;
        out.push((final_path, note));
    }
    Ok(out)
}

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

    const BS: u64 = 512; // small blocks keep the fixtures tiny; 512 is the smallest accepted size

    #[derive(Clone, Copy, PartialEq, Debug)]
    enum Kind {
        Raw,
        Bz,
        Xz,
        Zstd,
        Zero,
        Discard,
    }

    #[derive(Clone)]
    struct OpSpec {
        kind: Kind,
        extents: Vec<(u64, u64)>, // (start_block, num_blocks)
        data: Vec<u8>,            // what the op writes (uncompressed); empty for zero/discard
        bad_op_hash: bool,
        raw_type: Option<i32>, // override the encoded type (incremental / unknown types)
    }

    #[derive(Clone, Copy, PartialEq)]
    enum Hash {
        Good,
        Bad,
        Absent,
    }

    #[derive(Clone)]
    struct PartSpec {
        name: String,
        size: Option<u64>,
        ops: Vec<OpSpec>,
        hash: Hash,
        verity: bool,
        old_info: bool,
    }

    /// Deterministic bytes: half repetitive (compressible), half pseudo-random.
    fn bytes(seed: u8, n: usize) -> Vec<u8> {
        let mut x = 0x9E37_79B9u32 ^ seed as u32;
        (0..n)
            .map(|i| {
                x ^= x << 13;
                x ^= x >> 17;
                x ^= x << 5;
                if i % 2 == 0 { seed } else { x as u8 }
            })
            .collect()
    }

    fn op(kind: Kind, extents: &[(u64, u64)], seed: u8) -> OpSpec {
        let blocks: u64 = extents.iter().map(|e| e.1).sum();
        let data = match kind {
            Kind::Zero | Kind::Discard => vec![],
            _ => bytes(seed, (blocks * BS) as usize),
        };
        OpSpec {
            kind,
            extents: extents.to_vec(),
            data,
            bad_op_hash: false,
            raw_type: None,
        }
    }

    fn part(name: &str, ops: Vec<OpSpec>) -> PartSpec {
        PartSpec {
            name: name.into(),
            size: None,
            ops,
            hash: Hash::Good,
            verity: false,
            old_info: false,
        }
    }

    fn compress(kind: Kind, data: &[u8]) -> Vec<u8> {
        match kind {
            Kind::Raw => data.to_vec(),
            Kind::Bz => {
                let mut e = bzip2::write::BzEncoder::new(Vec::new(), bzip2::Compression::default());
                e.write_all(data).unwrap();
                e.finish().unwrap()
            }
            Kind::Xz => {
                let mut w =
                    lzma_rust2::XzWriter::new(Vec::new(), lzma_rust2::XzOptions::with_preset(1))
                        .unwrap();
                w.write_all(data).unwrap();
                w.finish().unwrap()
            }
            Kind::Zstd => {
                ruzstd::encoding::compress_to_vec(data, ruzstd::encoding::CompressionLevel::Fastest)
            }
            Kind::Zero | Kind::Discard => vec![],
        }
    }

    fn type_code(k: Kind) -> i32 {
        match k {
            Kind::Raw => OP_REPLACE,
            Kind::Bz => OP_REPLACE_BZ,
            Kind::Xz => OP_REPLACE_XZ,
            Kind::Zstd => OP_ZSTD,
            Kind::Zero => OP_ZERO,
            Kind::Discard => OP_DISCARD,
        }
    }

    /// Build a payload and the images it must produce.
    fn build(parts: &[PartSpec]) -> (Vec<u8>, Vec<(String, Vec<u8>)>) {
        let mut blobs: Vec<u8> = Vec::new();
        let mut updates = Vec::new();
        let mut expected = Vec::new();
        for p in parts {
            let furthest = p
                .ops
                .iter()
                .flat_map(|o| o.extents.iter())
                .map(|e| (e.0 + e.1) * BS)
                .max()
                .unwrap_or(0);
            let size = p.size.unwrap_or(furthest);
            let mut image = vec![0u8; size as usize];
            let mut ops = Vec::new();
            for o in &p.ops {
                let mut at = 0usize;
                for &(start, num) in &o.extents {
                    let len = (num * BS) as usize;
                    let n = len.min(o.data.len().saturating_sub(at)); // data may be deliberately short
                    if n > 0 && (start * BS) as usize + n <= image.len() {
                        image[(start * BS) as usize..][..n].copy_from_slice(&o.data[at..at + n]);
                    }
                    at += len;
                }
                let blob = compress(o.kind, &o.data);
                let (data_offset, data_length, hash) = if blob.is_empty() {
                    (None, None, None)
                } else {
                    let off = blobs.len() as u64;
                    blobs.extend(&blob);
                    let mut h = Sha256::digest(&blob).to_vec();
                    if o.bad_op_hash {
                        h[0] ^= 0xff;
                    }
                    (Some(off), Some(blob.len() as u64), Some(h))
                };
                ops.push(pb::InstallOperation {
                    r#type: o.raw_type.unwrap_or(type_code(o.kind)),
                    data_offset,
                    data_length,
                    src_extents: vec![],
                    dst_extents: o
                        .extents
                        .iter()
                        .map(|&(s, n)| pb::Extent {
                            start_block: Some(s),
                            num_blocks: Some(n),
                        })
                        .collect(),
                    data_sha256_hash: hash,
                });
            }
            let digest = Sha256::digest(&image).to_vec();
            let hash = match p.hash {
                Hash::Good => Some(digest),
                Hash::Bad => Some(vec![0xAB; 32]),
                Hash::Absent => None,
            };
            let ext = pb::Extent {
                start_block: Some(1),
                num_blocks: Some(1),
            };
            updates.push(pb::PartitionUpdate {
                partition_name: p.name.clone(),
                old_partition_info: p.old_info.then_some(pb::PartitionInfo {
                    size: Some(size),
                    hash: None,
                }),
                new_partition_info: Some(pb::PartitionInfo {
                    size: p.size.or(Some(furthest)),
                    hash,
                }),
                operations: ops,
                hash_tree_data_extent: None,
                hash_tree_extent: p.verity.then(|| ext.clone()),
                fec_data_extent: None,
                fec_extent: None,
            });
            expected.push((p.name.clone(), image));
        }
        let manifest = pb::DeltaArchiveManifest {
            block_size: Some(BS as u32),
            minor_version: Some(0),
            partitions: updates,
        };
        let m = manifest.encode_to_vec();
        let mut out = Vec::new();
        out.extend(MAGIC);
        out.extend(2u64.to_be_bytes());
        out.extend((m.len() as u64).to_be_bytes());
        out.extend(5u32.to_be_bytes());
        out.extend(&m);
        out.extend([0xEE; 5]); // metadata signature
        out.extend(&blobs);
        (out, expected)
    }

    fn scratch(tag: &str) -> crate::testutil::Scratch {
        crate::testutil::Scratch::new(&format!("payload-{tag}"))
    }

    fn as_dir(tag: &str, payload: &[u8]) -> (crate::testutil::Scratch, PathBuf) {
        let g = scratch(tag);
        let d = g.join("ota");
        std::fs::create_dir_all(&d).unwrap();
        std::fs::write(d.join("payload.bin"), payload).unwrap();
        (g, d)
    }

    fn as_file(tag: &str, payload: &[u8]) -> (crate::testutil::Scratch, PathBuf) {
        let g = scratch(tag);
        let f = g.join("whatever.bin");
        std::fs::write(&f, payload).unwrap();
        (g, f)
    }

    fn as_zip(
        tag: &str,
        payload: &[u8],
        method: zip::CompressionMethod,
    ) -> (crate::testutil::Scratch, PathBuf) {
        let g = scratch(tag);
        let z = g.join("ota.zip");
        let mut w = zip::ZipWriter::new(File::create(&z).unwrap());
        let opts = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        w.start_file("META-INF/com/android/metadata", opts).unwrap();
        w.write_all(b"ota-type=AB\n").unwrap();
        w.start_file(
            "payload.bin",
            zip::write::SimpleFileOptions::default().compression_method(method),
        )
        .unwrap();
        w.write_all(payload).unwrap();
        w.start_file("payload_properties.txt", opts).unwrap();
        w.write_all(b"FILE_HASH=x\n").unwrap();
        w.finish().unwrap();
        (g, z)
    }

    /// Run an extraction on a thread with a watchdog, so a hang fails the test instead of the run.
    fn run(input: &Path, out: &Path, opts: &ExtractOptions) -> Result<Vec<(PathBuf, String)>> {
        let (input, out, opts) = (input.to_path_buf(), out.to_path_buf(), opts.clone());
        let (tx, rx) = std::sync::mpsc::channel();
        std::thread::spawn(move || {
            let r = extract(&input, &out, &opts)
                .map(|o| o.expect("recognised as an A/B OTA"))
                .map_err(|e| format!("{e:#}"));
            let _ = tx.send(r);
        });
        rx.recv_timeout(std::time::Duration::from_secs(120))
            .expect("extraction hung")
            .map_err(|e| anyhow!(e))
    }

    fn go(input: &Path, tag: &str) -> Result<(crate::testutil::Scratch, Vec<(PathBuf, String)>)> {
        let out = scratch(&format!("{tag}-out"));
        let r = run(input, &out, &ExtractOptions::default())?;
        Ok((out, r))
    }

    fn images_match(out: &Path, expected: &[(String, Vec<u8>)]) {
        for (name, image) in expected {
            let got = std::fs::read(out.join(format!("{name}.img")))
                .unwrap_or_else(|e| panic!("{name}.img: {e}"));
            assert!(
                got == *image,
                "{name}.img differs ({} vs {} bytes)",
                got.len(),
                image.len()
            );
        }
    }

    fn files_in(dir: &Path) -> Vec<String> {
        let mut v: Vec<String> = std::fs::read_dir(dir)
            .map(|d| {
                d.map(|e| e.unwrap().file_name().to_string_lossy().into_owned())
                    .collect()
            })
            .unwrap_or_default();
        v.sort();
        v
    }

    fn mixed() -> Vec<PartSpec> {
        vec![
            part(
                "system",
                vec![
                    op(Kind::Xz, &[(0, 6)], 1),
                    op(Kind::Bz, &[(6, 3)], 2),
                    op(Kind::Raw, &[(9, 2)], 3),
                    op(Kind::Zstd, &[(11, 4)], 4),
                    op(Kind::Zero, &[(15, 3)], 0),
                    op(Kind::Discard, &[(18, 2)], 0),
                    op(Kind::Xz, &[(20, 1)], 5),
                ],
            ),
            part("boot", vec![op(Kind::Raw, &[(0, 4)], 9)]),
        ]
    }

    #[test]
    fn every_operation_type_rebuilds_the_image_and_is_verified() {
        let (payload, expected) = build(&mixed());
        for (tag, (_g, input)) in [
            ("dir", as_dir("t1d", &payload)),
            ("file", as_file("t1f", &payload)),
            (
                "zip",
                as_zip("t1z", &payload, zip::CompressionMethod::Stored),
            ),
        ] {
            let (out, res) = go(&input, tag).unwrap();
            images_match(&out, &expected);
            assert_eq!(res.len(), 2, "{tag}");
            assert!(
                res.iter().all(|(_, note)| note == "sha256 verified"),
                "{tag}: {res:?}"
            );
            assert_eq!(
                files_in(&out),
                ["boot.img", "system.img"],
                "{tag}: no .part files remain"
            );
            assert_eq!(
                res[0].0.file_name().unwrap(),
                "boot.img",
                "results come back sorted"
            );
        }
    }

    #[test]
    fn blob_order_in_the_payload_does_not_matter() {
        let mut parts = mixed();
        parts[0].ops.reverse(); // operations listed back to front, blobs laid out in that order
        let (payload, expected) = build(&parts);
        let (out, _) = go(&as_dir("t2", &payload).1, "t2").unwrap();
        images_match(&out, &expected);
    }

    #[test]
    fn an_operation_can_scatter_over_several_extents() {
        let parts = vec![part(
            "p",
            vec![
                op(Kind::Xz, &[(10, 2), (0, 3), (20, 1)], 1),
                op(Kind::Raw, &[(3, 7)], 2),
                op(Kind::Bz, &[(12, 8)], 3),
            ],
        )];
        let (payload, expected) = build(&parts);
        let (out, _) = go(&as_dir("t3", &payload).1, "t3").unwrap();
        images_match(&out, &expected);
    }

    #[test]
    fn a_partition_with_many_operations_is_decoded_in_parallel_correctly() {
        let ops: Vec<OpSpec> = (0..300u64)
            .map(|i| {
                op(
                    [Kind::Xz, Kind::Bz, Kind::Raw, Kind::Zstd][i as usize % 4],
                    &[(i * 2, 2)],
                    i as u8,
                )
            })
            .collect();
        let (payload, expected) = build(&[part("big", ops)]);
        let (out, res) = go(&as_dir("t4", &payload).1, "t4").unwrap();
        images_match(&out, &expected);
        assert_eq!(res[0].1, "sha256 verified");
    }

    #[test]
    fn declared_size_larger_than_the_extents_leaves_zero_padding() {
        let mut p = part("p", vec![op(Kind::Raw, &[(0, 2)], 1)]);
        p.size = Some(10 * BS);
        let (payload, expected) = build(&[p]);
        let (out, _) = go(&as_dir("t5", &payload).1, "t5").unwrap();
        images_match(&out, &expected);
        assert_eq!(std::fs::metadata(out.join("p.img")).unwrap().len(), 10 * BS);
    }

    #[test]
    fn only_and_list_select_partitions_and_report_sizes() {
        let (payload, expected) = build(&mixed());
        let (_g, input) = as_dir("t6", &payload);
        let list = list(&input, &ExtractOptions::default()).unwrap().unwrap();
        assert_eq!(
            list,
            vec![
                ("boot.img".to_string(), 4 * BS),
                ("system.img".to_string(), 21 * BS)
            ]
        );
        let only = |n: &[&str]| ExtractOptions {
            only: Some(n.iter().map(|s| s.to_string()).collect()),
            ..Default::default()
        };
        let out = scratch("t6-out");
        let res = run(&input, &out, &only(&["boot"])).unwrap();
        assert_eq!(res.len(), 1);
        assert_eq!(files_in(&out), ["boot.img"]);
        assert_eq!(std::fs::read(out.join("boot.img")).unwrap(), expected[1].1);
        let e = run(&input, &scratch("t6-out2"), &only(&["boot", "sysem"]))
            .unwrap_err()
            .to_string();
        assert!(
            e.contains("unknown name \"sysem\"") && e.contains("available: boot, system"),
            "{e}"
        );
        assert_eq!(
            super::list(&input, &only(&["system"]))
                .unwrap()
                .unwrap()
                .len(),
            1
        );
    }

    #[test]
    fn headers_that_are_wrong_are_rejected_with_a_reason() {
        let (payload, _) = build(&mixed());
        let patch = |at: usize, bytes: &[u8]| {
            let mut p = payload.clone();
            p[at..at + bytes.len()].copy_from_slice(bytes);
            p
        };
        let read = |p: &[u8]| {
            read_payload(&mut &p[..], p.len() as u64)
                .map(|_| ())
                .unwrap_err()
                .to_string()
        };
        assert!(read(&patch(0, b"CrAV")).contains("bad magic"));
        assert!(read(&patch(4, &1u64.to_be_bytes())).contains("version 1"));
        assert!(read(&payload[..10]).contains("too short"));
        assert!(read(&patch(12, &u64::MAX.to_be_bytes())).contains("do not fit"));
        assert!(read(&patch(12, &(MAX_MANIFEST_BYTES + 1).to_be_bytes())).contains("do not fit"));
        assert!(read(&patch(20, &u32::MAX.to_be_bytes())).contains("do not fit"));
        assert!(
            read(&payload[..40]).contains("manifest"),
            "truncated inside the manifest"
        );
        let mut garbage = payload.clone();
        garbage[24..40].copy_from_slice(&[0xFF; 16]);
        assert!(read(&garbage).contains("decoding the manifest"));
    }

    #[test]
    fn every_truncation_of_a_payload_is_an_error_or_a_clean_failure() {
        let (payload, _) = build(&[part(
            "p",
            vec![op(Kind::Xz, &[(0, 2)], 1), op(Kind::Raw, &[(2, 2)], 2)],
        )]);
        let dir = scratch("t8");
        for cut in (0..payload.len()).step_by(7).chain([payload.len() - 1]) {
            let f = dir.join("payload.bin");
            std::fs::write(&f, &payload[..cut]).unwrap();
            let out = dir.join(format!("out{cut}"));
            let r = run(&dir, &out, &ExtractOptions::default());
            assert!(r.is_err(), "cut at {cut} of {} must fail", payload.len());
            assert!(
                files_in(&out)
                    .iter()
                    .all(|n| !n.ends_with(".part") && !n.ends_with(".img")),
                "cut {cut} left files: {:?}",
                files_in(&out)
            );
        }
    }

    #[test]
    fn incremental_operations_are_refused_before_anything_is_written() {
        for (what, edit) in [
            ("SOURCE_COPY", Some(4)),
            ("SOURCE_BSDIFF", Some(5)),
            ("BROTLI_BSDIFF", Some(10)),
            ("PUFFDIFF", Some(9)),
            ("type 77", Some(77)),
            ("older partition", None),
        ] {
            let mut parts = mixed();
            match edit {
                Some(t) => parts[0].ops[2].raw_type = Some(t),
                None => parts[1].old_info = true,
            }
            let (payload, _) = build(&parts);
            let out = scratch(&format!("t9-{}", what.replace(' ', "")));
            let e = run(
                &as_dir(&format!("t9{}", what.replace(' ', "")), &payload).1,
                &out,
                &ExtractOptions::default(),
            )
            .unwrap_err()
            .to_string();
            assert!(
                e.contains("incremental OTA") && e.contains(what),
                "{what}: {e}"
            );
            assert!(
                files_in(&out).is_empty(),
                "{what}: nothing may be created, found {:?}",
                files_in(&out)
            );
        }
    }

    #[test]
    fn incremental_partitions_are_fine_when_not_selected() {
        let mut parts = mixed();
        parts[0].ops[2].raw_type = Some(OP_REPLACE + 4); // SOURCE_COPY in system only
        let (payload, expected) = build(&parts);
        let out = scratch("t9b-out");
        let only = ExtractOptions {
            only: Some(vec!["boot".into()]),
            ..Default::default()
        };
        run(&as_dir("t9b", &payload).1, &out, &only).unwrap();
        assert_eq!(std::fs::read(out.join("boot.img")).unwrap(), expected[1].1);
    }

    #[test]
    fn a_corrupt_operation_hash_fails_everything_and_leaves_nothing() {
        let mut parts = mixed();
        parts[0].ops[1].bad_op_hash = true;
        let (payload, _) = build(&parts);
        let out = scratch("t10-out");
        let e = run(&as_dir("t10", &payload).1, &out, &ExtractOptions::default())
            .unwrap_err()
            .to_string();
        assert!(
            e.contains("does not match its sha256") && e.contains("partition system"),
            "{e}"
        );
        assert!(
            files_in(&out).is_empty(),
            "all-or-nothing: {:?}",
            files_in(&out)
        );
    }

    #[test]
    fn a_wrong_partition_hash_is_an_error_naming_both_hashes() {
        let mut parts = mixed();
        parts[1].hash = Hash::Bad;
        let (payload, _) = build(&parts);
        let out = scratch("t11-out");
        let e = run(&as_dir("t11", &payload).1, &out, &ExtractOptions::default())
            .unwrap_err()
            .to_string();
        assert!(
            e.contains("partition boot: sha256 mismatch") && e.contains(&"ab".repeat(32)),
            "{e}"
        );
        assert!(
            files_in(&out).is_empty(),
            "the good partition is not published either: {:?}",
            files_in(&out)
        );
    }

    #[test]
    fn missing_hashes_and_verity_data_are_reported_honestly() {
        let mut parts = mixed();
        parts[0].hash = Hash::Absent;
        parts[1].verity = true;
        parts[1].hash = Hash::Bad; // would fail if it were checked
        let (payload, expected) = build(&parts);
        let (out, res) = go(&as_dir("t12", &payload).1, "t12").unwrap();
        images_match(&out, &expected);
        assert_eq!(
            res[0].1,
            "sha256 not checked: the payload leaves out the dm-verity/FEC data the device adds"
        );
        assert_eq!(res[1].1, "no hash in the payload to check");
    }

    #[test]
    fn data_that_does_not_fill_its_extents_exactly_is_an_error() {
        for kind in [Kind::Raw, Kind::Bz, Kind::Xz, Kind::Zstd] {
            for (data_blocks, extent_blocks, what) in [(2u64, 3u64, "ends"), (3, 2, "more than")] {
                let mut o = op(kind, &[(0, extent_blocks)], 1);
                o.data = bytes(1, (data_blocks * BS) as usize);
                let mut p = part("p", vec![o]);
                p.hash = Hash::Absent;
                let (payload, _) = build(&[p]);
                let out = scratch(&format!("t13-{kind:?}-{data_blocks}"));
                let e = run(
                    &as_dir(&format!("t13{kind:?}{data_blocks}"), &payload).1,
                    &out,
                    &ExtractOptions::default(),
                )
                .unwrap_err()
                .to_string();
                assert!(
                    e.contains(what),
                    "{kind:?} {data_blocks}/{extent_blocks}: {e}"
                );
                assert!(files_in(&out).is_empty());
            }
        }
    }

    #[test]
    fn corrupt_compressed_data_is_an_error_not_a_panic() {
        for kind in [Kind::Bz, Kind::Xz, Kind::Zstd] {
            let mut spec = part("p", vec![op(kind, &[(0, 4)], 3)]);
            spec.hash = Hash::Absent;
            let (mut payload, _) = build(&[spec]);
            let at = payload.len() - 12;
            payload[at..].fill(0xFF); // damage the end of the only blob
            // drop the per-operation hash so the decoder, not the hash check, has to notice
            let payload = with_manifest(payload, |m| {
                m.partitions[0].operations[0].data_sha256_hash = None
            });
            let out = scratch(&format!("t14-{kind:?}-out"));
            let e = run(
                &as_dir(&format!("t14{kind:?}"), &payload).1,
                &out,
                &ExtractOptions::default(),
            );
            assert!(e.is_err(), "{kind:?}: corrupt data must fail");
            assert!(files_in(&out).is_empty(), "{kind:?}: {:?}", files_in(&out));
        }
    }

    fn with_manifest(
        mut payload: Vec<u8>,
        edit: impl Fn(&mut pb::DeltaArchiveManifest),
    ) -> Vec<u8> {
        let msize = be64(&payload[12..20]) as usize;
        let mut manifest = pb::DeltaArchiveManifest::decode(&payload[24..24 + msize]).unwrap();
        edit(&mut manifest);
        let m = manifest.encode_to_vec();
        let tail = payload.split_off(24 + msize + 5);
        let mut out = Vec::new();
        out.extend(MAGIC);
        out.extend(2u64.to_be_bytes());
        out.extend((m.len() as u64).to_be_bytes());
        out.extend(5u32.to_be_bytes());
        out.extend(&m);
        out.extend([0xEE; 5]);
        out.extend(tail);
        out
    }

    fn rejects(tag: &str, payload: Vec<u8>, want: &str) {
        let out = scratch(&format!("{tag}-out"));
        let e = run(&as_dir(tag, &payload).1, &out, &ExtractOptions::default())
            .unwrap_err()
            .to_string();
        assert!(e.contains(want), "{tag}: expected {want:?} in {e}");
        assert!(files_in(&out).is_empty(), "{tag}: {:?}", files_in(&out));
    }

    #[test]
    fn hostile_manifests_are_rejected_before_any_file_is_made() {
        let (base, _) = build(&mixed());
        rejects(
            "h1",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].dst_extents[0].start_block = Some(u64::MAX / 2)
            }),
            "overflows",
        );
        rejects(
            "h2",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].dst_extents[0].start_block = Some(u64::MAX)
            }),
            "sparse-hole",
        );
        rejects(
            "h3",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].data_offset = Some(1 << 50)
            }),
            "data ends",
        );
        rejects(
            "h4",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].data_length = Some(MAX_BLOB_BYTES + 1)
            }),
            "limit",
        );
        rejects(
            "h5",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].data_length = Some(0)
            }),
            "no data",
        );
        rejects(
            "h6",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].dst_extents.clear()
            }),
            "no destination extents",
        );
        rejects(
            "h7",
            with_manifest(base.clone(), |m| {
                m.partitions[0].new_partition_info.as_mut().unwrap().size =
                    Some(MAX_PARTITION_BYTES + 1)
            }),
            "limit",
        );
        rejects(
            "h8",
            with_manifest(base.clone(), |m| {
                m.partitions[0].new_partition_info.as_mut().unwrap().size = Some(BS)
            }),
            "extents reach",
        );
        rejects(
            "h9",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[1].dst_extents[0].start_block = Some(1)
            }),
            "overlapping",
        );
        rejects(
            "h10",
            with_manifest(base.clone(), |m| m.block_size = Some(100)),
            "block size",
        );
        rejects(
            "h11",
            with_manifest(base.clone(), |m| m.block_size = Some(2 << 20)),
            "block size",
        );
        rejects(
            "h12",
            with_manifest(base.clone(), |m| m.partitions.clear()),
            "no partitions",
        );
        rejects(
            "h13",
            with_manifest(base.clone(), |m| {
                m.partitions[0].partition_name = "../evil".into()
            }),
            "unsafe partition name",
        );
        rejects(
            "h14",
            with_manifest(base.clone(), |m| {
                m.partitions[1].partition_name = "SYSTEM".into()
            }),
            "differ only by case",
        );
        rejects(
            "h15",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].dst_extents[0].num_blocks = Some(u64::MAX / 100)
            }),
            "overflows",
        );
    }

    #[test]
    fn a_partition_listed_twice_is_refused_so_two_writers_never_share_a_file() {
        let (base, _) = build(&mixed());
        rejects(
            "d1",
            with_manifest(base.clone(), |m| {
                m.partitions[1].partition_name = m.partitions[0].partition_name.clone()
            }),
            "listed more than once",
        );
        // three entries, two of them equal and not adjacent in the manifest order
        rejects(
            "d2",
            with_manifest(base, |m| {
                let mut extra = m.partitions[0].clone();
                extra.partition_name = "zzz".into();
                m.partitions.push(extra);
                m.partitions[1].partition_name = "zzz".into();
            }),
            "listed more than once",
        );
    }

    #[test]
    fn the_failed_run_removes_part_files_even_when_workers_stop_midway() {
        let mut parts = mixed();
        parts[0].ops[4].raw_type = None;
        let mut bad = op(Kind::Xz, &[(30, 2)], 7);
        bad.bad_op_hash = true;
        parts[0].ops.push(bad);
        parts[0]
            .ops
            .extend((0..40u64).map(|i| op(Kind::Xz, &[(40 + i, 1)], i as u8)));
        let (payload, _) = build(&parts);
        let out = scratch("t16-out");
        assert!(run(&as_dir("t16", &payload).1, &out, &ExtractOptions::default()).is_err());
        assert!(files_in(&out).is_empty(), "{:?}", files_in(&out));
    }

    #[test]
    fn existing_output_is_refused_without_force_and_replaced_with_it() {
        let (payload, expected) = build(&mixed());
        let (_g, input) = as_dir("t17", &payload);
        let out = scratch("t17-out");
        std::fs::write(out.join("boot.img"), b"precious").unwrap();
        let e = run(&input, &out, &ExtractOptions::default())
            .unwrap_err()
            .to_string();
        assert!(
            e.contains("boot.img") && !e.contains("system.img") && e.contains("--force"),
            "{e}"
        );
        assert_eq!(files_in(&out), ["boot.img"], "a refused run writes nothing");
        assert_eq!(std::fs::read(out.join("boot.img")).unwrap(), b"precious");
        run(
            &input,
            &out,
            &ExtractOptions {
                force: true,
                ..Default::default()
            },
        )
        .unwrap();
        images_match(&out, &expected);
    }

    #[cfg(unix)]
    #[test]
    fn planted_symlinks_are_never_written_through() {
        use std::os::unix::fs::symlink;
        let (payload, expected) = build(&mixed());
        let (_g, input) = as_dir("t18", &payload);
        let out = scratch("t18-out");
        let gv = scratch("t18-victim");
        let victim = gv.join("v");
        symlink(&victim, out.join("boot.img.part")).unwrap();
        let gv2 = scratch("t18-victim2");
        symlink(gv2.join("w"), out.join("system.img")).unwrap(); // dangling
        let e = run(&input, &out, &ExtractOptions::default())
            .unwrap_err()
            .to_string();
        assert!(e.contains("system.img") && e.contains("--force"), "{e}");
        assert!(!victim.exists());
        std::fs::remove_file(out.join("system.img")).unwrap();
        run(&input, &out, &ExtractOptions::default()).unwrap();
        assert!(!victim.exists(), "the .part symlink must not be followed");
        images_match(&out, &expected);
    }

    #[test]
    fn a_deflated_payload_in_a_zip_gets_a_clear_message() {
        let (payload, _) = build(&mixed());
        let (_g, z) = as_zip("t19", &payload, zip::CompressionMethod::Deflated);
        let e = format!("{:#}", locate(&z, None).unwrap_err());
        assert!(
            e.contains("compressed inside the zip") && e.contains("unzip"),
            "{e}"
        );
    }

    #[test]
    fn inputs_that_are_not_ab_otas_are_left_alone() {
        let d = scratch("t20");
        assert!(
            locate(&d, None).unwrap().is_none(),
            "directory without payload.bin"
        );
        std::fs::write(d.join("x.txt"), b"hello").unwrap();
        assert!(locate(&d.join("x.txt"), None).unwrap().is_none());
        std::fs::write(d.join("tiny"), b"CrA").unwrap();
        assert!(locate(&d.join("tiny"), None).unwrap().is_none());
        std::fs::write(d.join("empty"), b"").unwrap();
        assert!(locate(&d.join("empty"), None).unwrap().is_none());
        let z = d.join("plain.zip");
        let mut w = zip::ZipWriter::new(File::create(&z).unwrap());
        w.start_file("a.txt", zip::write::SimpleFileOptions::default())
            .unwrap();
        w.write_all(b"x").unwrap();
        w.finish().unwrap();
        assert!(
            locate(&z, None).unwrap().is_none(),
            "a zip without payload.bin"
        );
        std::fs::write(d.join("PKbad.zip"), b"PK\x03\x04garbage").unwrap();
        assert!(
            locate(&d.join("PKbad.zip"), None).unwrap().is_none(),
            "a damaged zip is not a payload"
        );
        assert!(locate(&d.join("missing"), None).is_err());
    }

    #[test]
    fn locate_finds_the_payload_inside_a_stored_zip_entry() {
        let (payload, _) = build(&mixed());
        let (_g, z) = as_zip("t21", &payload, zip::CompressionMethod::Stored);
        let loc = locate(&z, None).unwrap().unwrap();
        assert_eq!(loc.len, payload.len() as u64);
        let raw = std::fs::read(&z).unwrap();
        assert_eq!(&raw[loc.base as usize..loc.base as usize + 4], b"CrAU");
        assert_eq!(&raw[loc.base as usize..][..payload.len()], &payload[..]);
        assert_eq!(partition_names(&z).unwrap().unwrap(), ["boot", "system"]);
        assert!(partition_names(&scratch("t21b")).unwrap().is_none());
    }

    #[test]
    fn operation_type_numbers_are_the_literal_values_from_the_proto() {
        // not derived from the constants, so a wrong constant cannot hide behind the test builder
        assert_eq!(
            [
                OP_REPLACE,
                OP_REPLACE_BZ,
                OP_ZERO,
                OP_DISCARD,
                OP_REPLACE_XZ,
                OP_ZSTD
            ],
            [0, 1, 6, 7, 8, 14]
        );
        for (n, data_op) in [
            (0, true),
            (1, true),
            (8, true),
            (14, true),
            (6, false),
            (7, false),
            (4, false),
            (5, false),
            (2, false),
            (3, false),
            (9, false),
            (10, false),
            (11, false),
            (12, false),
            (13, false),
            (15, false),
            (-1, false),
        ] {
            assert_eq!(is_data_op(n), data_op, "type {n}");
        }
    }

    #[test]
    fn a_zero_length_verity_extent_does_not_count_as_verity_data() {
        let (payload, expected) = build(&mixed());
        let payload = with_manifest(payload, |m| {
            m.partitions[0].hash_tree_extent = Some(pb::Extent {
                start_block: Some(3),
                num_blocks: Some(0),
            });
            m.partitions[0].fec_extent = Some(pb::Extent {
                start_block: None,
                num_blocks: None,
            });
        });
        let (out, res) = go(&as_dir("t24", &payload).1, "t24").unwrap();
        images_match(&out, &expected);
        assert!(res.iter().all(|(_, n)| n == "sha256 verified"), "{res:?}");
        let with_data = with_manifest(build(&mixed()).0, |m| {
            m.partitions[1].fec_data_extent = Some(pb::Extent {
                start_block: Some(0),
                num_blocks: Some(1),
            })
        });
        let (_, res) = go(&as_dir("t24b", &with_data).1, "t24b").unwrap();
        assert!(
            res[0].1.starts_with("sha256 not checked"),
            "boot has fec data: {res:?}"
        );
        let with_fec = with_manifest(build(&mixed()).0, |m| {
            m.partitions[1].fec_extent = Some(pb::Extent {
                start_block: Some(0),
                num_blocks: Some(1),
            })
        });
        assert!(
            go(&as_dir("t24c", &with_fec).1, "t24c").unwrap().1[0]
                .1
                .starts_with("sha256 not checked")
        );
        let with_tree_data = with_manifest(build(&mixed()).0, |m| {
            m.partitions[1].hash_tree_data_extent = Some(pb::Extent {
                start_block: Some(0),
                num_blocks: Some(1),
            })
        });
        assert!(
            go(&as_dir("t24d", &with_tree_data).1, "t24d").unwrap().1[0]
                .1
                .starts_with("sha256 not checked")
        );
    }

    #[test]
    fn zero_block_extents_inside_an_operation_are_skipped() {
        let mut o = op(Kind::Xz, &[(0, 2), (9, 0), (6, 1), (0, 0)], 4);
        o.data = bytes(4, (3 * BS) as usize);
        let mut p = part("p", vec![o, op(Kind::Zero, &[(3, 0)], 0)]);
        p.size = Some(8 * BS);
        let (payload, expected) = build(&[p]);
        let (out, res) = go(&as_dir("t27", &payload).1, "t27").unwrap();
        images_match(&out, &expected);
        assert_eq!(res[0].1, "sha256 verified");
    }

    #[test]
    fn the_extent_writer_places_bytes_correctly_whatever_the_write_sizes() {
        let dir = scratch("t25");
        let path = dir.join("img");
        let extents = [(30u64, 4u64), (0, 6), (10, 1)];
        let data: Vec<u8> = (1..=11).collect();
        for pieces in [
            vec![11usize],
            vec![1; 11],
            vec![3, 3, 3, 2],
            vec![4, 6, 1],
            vec![5, 5, 1],
            vec![2, 8, 1],
            vec![10, 1],
        ] {
            File::create(&path).unwrap().set_len(40).unwrap();
            let mut w = ExtentWriter {
                file: File::options().write(true).open(&path).unwrap(),
                extents: &extents,
                idx: 0,
                within: 0,
            };
            let mut at = 0;
            for n in pieces.iter().copied() {
                w.write_all(&data[at..at + n]).unwrap();
                at += n;
            }
            let got = std::fs::read(&path).unwrap();
            let mut want = vec![0u8; 40];
            want[30..34].copy_from_slice(&data[0..4]);
            want[0..6].copy_from_slice(&data[4..10]);
            want[10] = data[10];
            assert_eq!(got, want, "writes of {pieces:?}");
            let e = w.write(&[1]).unwrap_err().to_string();
            assert!(e.contains("more data than"), "{e}");
        }
    }

    #[test]
    fn a_partition_count_over_the_limit_is_refused_before_anything_is_made() {
        let (base, _) = build(&mixed());
        let many = with_manifest(base.clone(), |m| {
            let one = m.partitions[1].clone();
            m.partitions = (0..=MAX_PARTITIONS)
                .map(|i| pb::PartitionUpdate {
                    partition_name: format!("p{i}"),
                    ..one.clone()
                })
                .collect();
        });
        rejects("c1", many, "over the limit of 1024");
        // exactly at the limit is fine, and does not need 1024 open files at once
        let at_limit = with_manifest(base, |m| {
            let mut one = m.partitions[1].clone();
            one.operations.truncate(1);
            m.partitions = (0..MAX_PARTITIONS)
                .map(|i| pb::PartitionUpdate {
                    partition_name: format!("p{i}"),
                    ..one.clone()
                })
                .collect();
        });
        let out = scratch("c2-out");
        let only = ExtractOptions {
            only: Some(vec!["p0".into(), "p1023".into()]),
            ..Default::default()
        };
        assert_eq!(
            run(&as_dir("c2", &at_limit).1, &out, &only).unwrap().len(),
            2
        );
    }

    #[test]
    fn the_limits_are_the_documented_values() {
        assert_eq!(MAX_PARTITION_BYTES, 64 << 30);
        assert_eq!(MAX_MANIFEST_BYTES, 64 << 20);
        assert_eq!(MAX_PARTITIONS, 1024);
        let (payload, _) = build(&mixed());
        let big = with_manifest(payload, |m| {
            m.partitions[1].new_partition_info.as_mut().unwrap().size =
                Some(MAX_PARTITION_BYTES + 1)
        });
        rejects("c3", big, "limit");
    }

    #[test]
    fn hashing_a_file_shorter_than_expected_is_an_error() {
        let dir = scratch("t26");
        std::fs::write(dir.join("short"), [7u8; 10]).unwrap();
        assert!(
            hash_file(&dir.join("short"), 20)
                .unwrap_err()
                .to_string()
                .contains("shorter")
        );
        assert_eq!(
            hash_file(&dir.join("short"), 10).unwrap(),
            Sha256::digest([7u8; 10]).to_vec()
        );
        assert_eq!(
            hash_file(&dir.join("short"), 4).unwrap(),
            Sha256::digest([7u8; 4]).to_vec(),
            "only the first `size` bytes"
        );
        assert_eq!(
            hash_file(&dir.join("short"), 0).unwrap(),
            Sha256::digest([]).to_vec()
        );
    }

    #[test]
    fn extent_overflow_edges_are_rejected_on_their_own() {
        let (base, _) = build(&mixed());
        // start and num each fit when multiplied by the block size, their sum does not
        let half = u64::MAX / BS;
        rejects(
            "o1",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].dst_extents[0] = pb::Extent {
                    start_block: Some(half),
                    num_blocks: Some(half),
                };
            }),
            "extent end overflows",
        );
        // start + num itself overflows
        rejects(
            "o2",
            with_manifest(base.clone(), |m| {
                m.partitions[0].operations[0].dst_extents[0] = pb::Extent {
                    start_block: Some(u64::MAX - 1),
                    num_blocks: Some(5),
                };
            }),
            "extent overflows",
        );
        // a huge but representable extent is a size error, not an overflow
        rejects(
            "o3",
            with_manifest(base, |m| {
                m.partitions[0].operations[0].dst_extents[0] = pb::Extent {
                    start_block: Some(1 << 40),
                    num_blocks: Some(1),
                };
            }),
            "extents reach",
        );
    }

    #[test]
    fn the_operation_type_table_matches_the_proto() {
        for (n, name) in [
            (0, "REPLACE"),
            (1, "REPLACE_BZ"),
            (2, "MOVE"),
            (3, "BSDIFF"),
            (4, "SOURCE_COPY"),
            (5, "SOURCE_BSDIFF"),
            (6, "ZERO"),
            (7, "DISCARD"),
            (8, "REPLACE_XZ"),
            (9, "PUFFDIFF"),
            (10, "BROTLI_BSDIFF"),
            (11, "ZUCCHINI"),
            (12, "LZ4DIFF_BSDIFF"),
            (13, "LZ4DIFF_PUFFDIFF"),
            (14, "ZSTD"),
        ] {
            assert_eq!(op_name(n), name);
        }
        assert_eq!(op_name(99), "type 99");
        assert_eq!(hex(&[0x00, 0xab, 0x0f]), "00ab0f");
    }

    #[test]
    fn the_command_prints_a_note_per_image() {
        let (payload, _) = build(&mixed());
        let (_g, input) = as_dir("t23", &payload);
        let out = scratch("t23-out");
        let done =
            crate::extract::extract_all_noted(&input, &out, &ExtractOptions::default()).unwrap();
        assert_eq!(done.done.len(), 2);
        assert!(done.done.iter().all(|(_, n)| n == "sha256 verified"));
        assert_eq!(
            crate::extract::list_images(&input, &ExtractOptions::default())
                .unwrap()
                .len(),
            2
        );
        assert_eq!(
            crate::extract::partition_names(&input).unwrap(),
            ["boot", "system"]
        );
    }
}