yara-x 1.15.0

A pure Rust implementation of YARA.
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
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
use std::collections::HashSet;

use bstr::{BStr, ByteSlice};
use der_parser::asn1_rs::{FromBer, OptTaggedParser, ParseResult};
use der_parser::ber::{
    parse_ber_integer, parse_ber_oid, parse_ber_sequence,
    parse_ber_sequence_defined_g, parse_ber_set_of_v,
    parse_ber_tagged_explicit_g,
};
use der_parser::error::BerResult;
use der_parser::error::Error::BerValueError;
use itertools::Itertools;
#[cfg(feature = "logging")]
use log::error;
use nom::bytes::complete::{tag, take, take_till};
use nom::combinator::{cond, map, verify};
use nom::error::ErrorKind;
use nom::multi::{count, length_count};
use nom::number::complete::{be_u32, le_u32, u16, u32, u64, u8};
use nom::number::Endianness;
use nom::{Err, IResult, Parser};
use protobuf::MessageField;
use x509_parser::x509::AlgorithmIdentifier;

use crate::modules::protos;
use crate::modules::utils::asn1::SignedData;
use crate::modules::utils::leb128::{sleb128, uleb128};

type NomError<'a> = nom::error::Error<&'a [u8]>;

/// Mach-O magic constants
const MH_MAGIC: u32 = 0xfeedface;
const MH_CIGAM: u32 = 0xcefaedfe;
const MH_MAGIC_64: u32 = 0xfeedfacf;
const MH_CIGAM_64: u32 = 0xcffaedfe;

/// Mach-O FAT magic constants
const FAT_MAGIC: u32 = 0xcafebabe;
const FAT_CIGAM: u32 = 0xbebafeca;
const FAT_MAGIC_64: u32 = 0xcafebabf;
const FAT_CIGAM_64: u32 = 0xbfbafeca;

/// Mach-O code signature constants
const _CS_MAGIC_REQUIREMENT: u32 = 0xfade0c00;
const _CS_MAGIC_REQUIREMENTS: u32 = 0xfade0c01;
const _CS_MAGIC_CODEDIRECTORY: u32 = 0xfade0c02;
const _CS_MAGIC_EMBEDDED_SIGNATURE: u32 = 0xfade0cc0;
const _CS_MAGIC_DETACHED_SIGNATURE: u32 = 0xfade0cc1;
const CS_MAGIC_BLOBWRAPPER: u32 = 0xfade0b01;
const CS_MAGIC_EMBEDDED_ENTITLEMENTS: u32 = 0xfade7171;

/// Mach-O symtol table flag constants
pub const N_STAB: u8 = 0xe0; /* if any of these bits set, a symbolic debugging entry */
const _N_PEXT: u8 = 0x10; /* private external symbol bit */
pub const N_TYPE: u8 = 0x0e; /* mask for the type bits */
pub const N_EXT: u8 = 0x01; /* external symbol bit, set for external symbols */

/// Mach-o value flags for N_TYPE bits of the n_type field.
const N_UNDF: u8 = 0x0; /* undefined, n_sect == NO_SECT */
const N_ABS: u8 = 0x2; /* absolute, n_sect == NO_SECT */
const N_SECT: u8 = 0xe; /* defined in section number n_sect */
const _N_PBUD: u8 = 0xc; /* prebound undefined (defined in a dylib) */
const N_INDR: u8 = 0xa; /* indirect */

/// Mach-O export flag constants
const _EXPORT_SYMBOL_FLAGS_KIND_REGULAR: u64 = 0x00000000;
const _EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION: u64 = 0x00000004;
const EXPORT_SYMBOL_FLAGS_REEXPORT: u64 = 0x00000008;
const EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER: u64 = 0x00000010;

/// Mach-O import opcode constants
const BIND_OPCODE_MASK: u8 = 0xF0;
const BIND_IMMEDIATE_MASK: u8 = 0x0F;
const _BIND_OPCODE_DONE: u8 = 0x00;
const _BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: u8 = 0x10;
const BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: u8 = 0x20;
const _BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: u8 = 0x30;
const BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: u8 = 0x40;
const _BIND_OPCODE_SET_TYPE_IMM: u8 = 0x50;
const BIND_OPCODE_SET_ADDEND_SLEB: u8 = 0x60;
const BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: u8 = 0x70;
const BIND_OPCODE_ADD_ADDR_ULEB: u8 = 0x80;
const _BIND_OPCODE_DO_BIND: u8 = 0x90;
const BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB: u8 = 0xA0;
const _BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED: u8 = 0xB0;
const BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB: u8 = 0xC0;

/// Mach-O dynamic linker constant
const LC_REQ_DYLD: u32 = 0x80000000;

/// Mach-O load commands
const LC_SEGMENT: u32 = 0x00000001;
const LC_SYMTAB: u32 = 0x00000002;
const LC_UNIXTHREAD: u32 = 0x00000005;
const LC_DYSYMTAB: u32 = 0x0000000b;
const LC_LOAD_DYLIB: u32 = 0x0000000c;
const LC_ID_DYLIB: u32 = 0x0000000d;
const LC_LOAD_DYLINKER: u32 = 0x0000000e;
const LC_ID_DYLINKER: u32 = 0x0000000f;
const LC_LOAD_WEAK_DYLIB: u32 = 0x18 | LC_REQ_DYLD;
const LC_SEGMENT_64: u32 = 0x00000019;
const LC_UUID: u32 = 0x00000001b;
const LC_RPATH: u32 = 0x1c | LC_REQ_DYLD;
const LC_CODE_SIGNATURE: u32 = 0x0000001d;
const LC_REEXPORT_DYLIB: u32 = 0x1f | LC_REQ_DYLD;
const LC_LAZY_LOAD_DYLIB: u32 = 0x00000020;
const LC_DYLD_INFO: u32 = 0x00000022;
const LC_DYLD_INFO_ONLY: u32 = 0x22 | LC_REQ_DYLD;
const LC_LOAD_UPWARD_DYLIB: u32 = 0x23 | LC_REQ_DYLD;
const LC_VERSION_MIN_MACOSX: u32 = 0x00000024;
const LC_VERSION_MIN_IPHONEOS: u32 = 0x00000025;
const LC_DYLD_ENVIRONMENT: u32 = 0x00000027;
const LC_MAIN: u32 = 0x28 | LC_REQ_DYLD;
const LC_SOURCE_VERSION: u32 = 0x0000002a;
const LC_LINKER_OPTION: u32 = 0x0000002d;
const LC_VERSION_MIN_TVOS: u32 = 0x0000002f;
const LC_VERSION_MIN_WATCHOS: u32 = 0x00000030;
const LC_BUILD_VERSION: u32 = 0x00000032;
const LC_DYLD_EXPORTS_TRIE: u32 = 0x00000033 | LC_REQ_DYLD;
const LC_DYLD_CHAINED_FIXUPS: u32 = 0x00000034 | LC_REQ_DYLD;

/// Mach-O CPU types
const CPU_TYPE_MC680X0: u32 = 0x00000006;
const CPU_TYPE_X86: u32 = 0x00000007;
const CPU_TYPE_X86_64: u32 = 0x01000007;
const CPU_TYPE_ARM: u32 = 0x0000000c;
const CPU_TYPE_ARM64: u32 = 0x0100000c;
const CPU_TYPE_MC88000: u32 = 0x0000000d;
const CPU_TYPE_SPARC: u32 = 0x0000000e;
const CPU_TYPE_POWERPC: u32 = 0x00000012;
const CPU_TYPE_POWERPC64: u32 = 0x01000012;

/// Mach-O Import Fixup Formats
const DYLD_CHAINED_IMPORT: u32 = 1;
const DYLD_CHAINED_IMPORT_ADDEND: u32 = 2;
const DYLD_CHAINED_IMPORT_ADDEND64: u32 = 3;

/// Represents a Mach-O file. It can represent both a multi-architecture
/// binary (a.k.a. FAT binary) or a single-architecture binary.
pub struct MachO<'a> {
    /// When representing a FAT binary, this contains the file magic. It's
    /// `None` when the Mach-O file is a single-architecture binary.
    fat_magic: Option<u32>,
    /// When representing a FAT binary, this array contains one entry per
    /// architecture supported by the FAT binary. In such case the number of
    /// entries in this array should be equal to the number of entries in the
    /// `files` array. When representing a single-architecture Mach-O, this
    /// array is empty.
    archs: Vec<FatArch>,
    /// This array contains an entry per architecture included in the Mach-O
    /// file. For single-architecture binaries the array contains a single
    /// entry.
    files: Vec<MachOFile<'a>>,
}

impl<'a> MachO<'a> {
    /// Given the content of Macho-O file, parses it and returns a [`MachO`]
    /// object representing the file.
    pub fn parse(data: &'a [u8]) -> Result<Self, Err<NomError<'a>>> {
        let (_, magic) = le_u32(data)?;

        if matches!(magic, FAT_MAGIC | FAT_CIGAM | FAT_MAGIC_64 | FAT_CIGAM_64)
        {
            Self::parse_fat_macho_file(data)
        } else {
            Ok(Self {
                fat_magic: None,
                archs: Vec::new(),
                files: vec![Self::parse_macho_file(data)?],
            })
        }
    }
}

impl<'a> MachO<'a> {
    /// Parses a FAT Mach-O file.
    fn parse_fat_macho_file(
        data: &'a [u8],
    ) -> Result<Self, Err<NomError<'a>>> {
        // Parse the magic number and make sure it's valid for a FAT
        // Mach-O file.
        let (remainder, magic) = verify(be_u32, |magic| {
            matches!(
                *magic,
                FAT_MAGIC | FAT_CIGAM | FAT_MAGIC_64 | FAT_CIGAM_64
            )
        })
        .parse(data)?;

        // The magic number indicates the endianness.
        let endianness = match magic {
            FAT_MAGIC | FAT_MAGIC_64 => Endianness::Big,
            FAT_CIGAM | FAT_CIGAM_64 => Endianness::Little,
            _ => unreachable!(),
        };

        // The magic number also indicates whether this is a 32-bits or
        // 64-bits binary.
        let is_32_bits = match magic {
            FAT_MAGIC | FAT_CIGAM => true,
            FAT_MAGIC_64 | FAT_CIGAM_64 => false,
            _ => unreachable!(),
        };

        // After the magic comes an u32 with the number of `fat_arch`
        // structures that follow (`fat_arch64` for 64-bits binaries). Each
        // structure describes an individual Mach-O file included in the FAT
        // binary.
        let (_, archs) = length_count(
            // number of architectures.
            u32(endianness),
            // fat_arch/fat_arch64 structure.
            map(
                (
                    u32(endianness),                    // cputype
                    u32(endianness),                    // cpusubtype
                    uint(endianness, is_32_bits),       // offset
                    uint(endianness, is_32_bits),       // size
                    u32(endianness),                    // align
                    cond(!is_32_bits, u32(endianness)), // reserved
                ),
                |(cputype, cpusubtype, offset, size, align, reserved)| {
                    FatArch {
                        cputype,
                        cpusubtype,
                        offset,
                        size,
                        align,
                        reserved: reserved.unwrap_or_default(),
                    }
                },
            ),
        )
        .parse(remainder)?;

        let mut files = Vec::new();

        // Parse each of the individual Mach-O files contained in the FAT
        // binary. Errors that occur while parsing individual Mach-O files are
        // not propagated. If the FAT file is truncated for example, we may be
        // able to parse some of the Mach-O files while the rest can't be
        // parsed, but we still consider that case a success.
        for arch in &archs {
            let start = arch.offset as usize;
            let end = start.saturating_add(arch.size as usize);

            if let Some(macho) = data.get(start..end) {
                match Self::parse_macho_file(macho) {
                    Ok(macho) => files.push(macho),
                    #[cfg(feature = "logging")]
                    Err(err) => {
                        error!("Error parsing Mach-O file: {:?}", err);
                    }
                    #[cfg(not(feature = "logging"))]
                    Err(_) => {}
                }
            };
        }

        Ok(MachO { fat_magic: Some(magic), archs, files })
    }

    /// Parses a single-architecture Mach-O file.
    fn parse_macho_file(
        data: &'a [u8],
    ) -> Result<MachOFile<'a>, Err<NomError<'a>>> {
        let (remainder, magic) = verify(be_u32, |magic| {
            matches!(*magic, MH_MAGIC | MH_CIGAM | MH_MAGIC_64 | MH_CIGAM_64)
        })
        .parse(data)?;

        let endianness = match magic {
            MH_MAGIC | MH_MAGIC_64 => Endianness::Big,
            MH_CIGAM | MH_CIGAM_64 => Endianness::Little,
            _ => unreachable!(),
        };

        let is_32_bits = match magic {
            MH_MAGIC | MH_CIGAM => true,
            MH_MAGIC_64 | MH_CIGAM_64 => false,
            _ => unreachable!(),
        };

        let (mut commands, header) = map(
            (
                u32(endianness),                    // cputype
                u32(endianness),                    // cpusubtype
                u32(endianness),                    // filetype
                u32(endianness),                    // ncmds
                u32(endianness),                    // sizeofcmds,
                u32(endianness),                    // flags,
                cond(!is_32_bits, u32(endianness)), // reserved, only in 64-bits
            ),
            |(
                cputype,
                cpusubtype,
                filetype,
                ncmds,
                sizeofcmds,
                flags,
                reserved,
            )| {
                MachOHeader {
                    magic,
                    cputype,
                    cpusubtype,
                    filetype,
                    ncmds,
                    sizeofcmds,
                    flags,
                    reserved,
                }
            },
        )
        .parse(remainder)?;

        let mut macho = MachOFile {
            endianness,
            is_32_bits,
            header,
            segments: Vec::new(),
            dylibs: Vec::new(),
            rpaths: Vec::new(),
            symtab: None,
            dysymtab: None,
            dynamic_linker: None,
            linker_options: Vec::new(),
            dyld_info: None,
            dyld_export_trie: None,
            dyld_chain_fixups: None,
            source_version: None,
            entry_point_offset: None,
            entry_point_rva: None,
            stack_size: None,
            code_signature_data: None,
            entitlements: Vec::new(),
            certificates: Vec::new(),
            uuid: None,
            build_version: None,
            min_version: None,
            exports: Vec::new(),
            imports: Vec::new(),
        };

        for _ in 0..macho.header.ncmds as usize {
            match macho.command().parse(commands) {
                Ok((c, _)) => commands = c,
                Err(err) => {
                    #[cfg(feature = "logging")]
                    error!("Error parsing Mach-O file: {:?}", err);
                    // Break the loop when the end of file has been reached.
                    // With other types of errors we keep trying to parse more
                    // commands as one individual command structure could be
                    // corrupted while the rest are ok. But when the end of
                    // the file is reached there are no more commands that can
                    // be parsed.
                    if let Err::Error(e) = err
                        && e.code == ErrorKind::Eof {
                            break;
                        }
                }
            }
        }

        if let Some(ref symtab) = macho.symtab {
            let str_offset = symtab.stroff as usize;
            let str_end = symtab.strsize as usize;
            let sym_offset = symtab.symoff as usize;
            let nsyms = symtab.nsyms;

            // We don't want the dyld_shared_cache ones for now
            if let Some(string_table) =
                data.get(str_offset..str_offset.saturating_add(str_end))
                && let Some(symbol_table) = data.get(sym_offset..)
                    && let Err(_err) =
                        macho.parse_symtab(string_table, symbol_table, nsyms)
                    {
                        #[cfg(feature = "logging")]
                        error!("Error parsing Mach-O file: {:?}", _err);
                        // fail silently if it fails, data was not formatted
                        // correctly but parsing should still proceed for
                        // everything else
                    };
        }

        if let Some(entry_point_rva) = macho.entry_point_rva {
            macho.entry_point_offset = macho.rva_to_offset(entry_point_rva);
        }

        if let Some(ref code_signature_data) = macho.code_signature_data {
            let offset = code_signature_data.dataoff as usize;
            let size = code_signature_data.datasize as usize;
            if let Some(super_data) =
                data.get(offset..offset.saturating_add(size))
                && let Err(_err) = macho.cs_superblob().parse(super_data) {
                    #[cfg(feature = "logging")]
                    error!("Error parsing Mach-O file: {:?}", _err);
                    // fail silently if it fails, data was not formatted
                    // correctly but parsing should still proceed for
                    // everything else
                };
        }

        for (offset, size) in [
            macho
                .dyld_export_trie
                .as_ref()
                .map(|t| (t.data_off as usize, t.data_size as usize)),
            macho
                .dyld_info
                .as_ref()
                .map(|i| (i.export_off as usize, i.export_size as usize)),
        ]
        .into_iter()
        .flatten()
        {
            if let Some(export_data) =
                data.get(offset..offset.saturating_add(size))
                && let Err(_err) = macho.parse_exports(export_data) {
                    #[cfg(feature = "logging")]
                    error!("Error parsing Mach-O file: {:?}", _err);
                    // fail silently if it fails, data was not formatted
                    // correctly but parsing should still proceed for
                    // everything else
                };
        }

        // imports defined at `weak_offset` can be duplicative, meaning
        // they can exist in `bind_offset` _and_ `weak_offset` or one of them.
        // To avoid duplicates in the imports list, keep track of seen imports
        let mut seen: HashSet<_> = HashSet::new();
        for (offset, size) in macho
            .dyld_info
            .as_ref()
            .map(|i| {
                [
                    (i.bind_off as usize, i.bind_size as usize),
                    (i.lazy_bind_off as usize, i.lazy_bind_size as usize),
                    (i.weak_bind_off as usize, i.weak_bind_size as usize),
                ]
            })
            .into_iter()
            .flatten()
        {
            if let Some(import_data) =
                data.get(offset..offset.saturating_add(size))
                && let Err(_err) = macho.parse_imports(import_data, &mut seen)
                {
                    #[cfg(feature = "logging")]
                    error!("Error parsing Mach-O file: {:?}", _err);
                    // fail silently if it fails, data was not formatted
                    // correctly but parsing should still proceed for
                    // everything else
                };
        }

        if let Some(ref chained_fixups) = macho.dyld_chain_fixups {
            let offset = chained_fixups.data_off as usize;
            let size = chained_fixups.data_size as usize;
            if let Some(fixup_data) =
                data.get(offset..offset.saturating_add(size))
                && let Err(_err) = macho.parse_chained_fixups(fixup_data) {
                    #[cfg(feature = "logging")]
                    error!("Error parsing Mach-O file: {:?}", _err);
                    // fail silently if it fails, data was not formatted
                    // correctly but parsing should still proceed for
                    // everything else
                };
        }

        Ok(macho)
    }
}

pub struct MachOFile<'a> {
    endianness: Endianness,
    is_32_bits: bool,
    entry_point_offset: Option<u64>,
    entry_point_rva: Option<u64>,
    stack_size: Option<u64>,
    header: MachOHeader,
    segments: Vec<Segment<'a>>,
    dylibs: Vec<Dylib<'a>>,
    symtab: Option<Symtab<'a>>,
    dysymtab: Option<Dysymtab>,
    dyld_info: Option<DyldInfo>,
    dyld_export_trie: Option<DyldExportTrie>,
    dyld_chain_fixups: Option<DyldChainFixups>,
    dynamic_linker: Option<&'a [u8]>,
    linker_options: Vec<&'a [u8]>,
    source_version: Option<String>,
    rpaths: Vec<&'a [u8]>,
    uuid: Option<&'a [u8]>,
    code_signature_data: Option<LinkedItData>,
    entitlements: Vec<String>,
    certificates: Vec<Certificate>,
    build_version: Option<BuildVersionCommand>,
    min_version: Option<MinVersion>,
    exports: Vec<String>,
    imports: Vec<String>,
}

impl MachOFile<'_> {
    /// Converts a relative virtual address (RVA) to file object.
    pub fn rva_to_offset(&self, rva: u64) -> Option<u64> {
        for segment in &self.segments {
            let start = segment.vmaddr;
            let end = segment.vmaddr.checked_add(segment.vmsize)?;
            if rva >= start && rva < end {
                return segment.fileoff.checked_add(rva.checked_sub(start)?);
            }
        }
        None
    }
}

impl<'a> MachOFile<'a> {
    /// Parser that parses a Mach-O section.
    fn section(
        &self,
    ) -> impl Parser<&'a [u8], Output = Section<'a>, Error = NomError<'a>> + '_
    {
        map(
            (
                // sectname
                map(take(16_usize), |name| {
                    BStr::new(name).trim_end_with(|c| c == '\0')
                }),
                // segname
                map(take(16_usize), |name| {
                    BStr::new(name).trim_end_with(|c| c == '\0')
                }),
                uint(self.endianness, self.is_32_bits), // addr
                uint(self.endianness, self.is_32_bits), // size
                u32(self.endianness),                   // offset
                u32(self.endianness),                   // align
                u32(self.endianness),                   // reloff
                u32(self.endianness),                   // nreloc
                u32(self.endianness),                   // flags
                u32(self.endianness),                   // reserved1
                u32(self.endianness),                   // reserved2
                cond(!self.is_32_bits, u32(self.endianness)), // reserved3
            ),
            |(
                sectname,
                segname,
                addr,
                size,
                offset,
                align,
                reloff,
                nreloc,
                flags,
                reserved1,
                reserved2,
                reserved3,
            )| {
                Section {
                    sectname,
                    segname,
                    addr,
                    size,
                    offset,
                    align,
                    reloff,
                    nreloc,
                    flags,
                    reserved1,
                    reserved2,
                    reserved3,
                }
            },
        )
    }

    /// Parser that parses a Mach-O command.
    fn command(
        &mut self,
    ) -> impl Parser<&'a [u8], Output = (), Error = NomError<'a>> + '_ {
        move |input: &'a [u8]| {
            // The first two u32 in the command are the value that indicates
            // the command type, and the size of the command's data.
            let (remainder, (command, command_size)) = (
                u32(self.endianness), // command
                u32(self.endianness), // command_size
            )
                .parse(input)?;

            // Take the command's data.
            let (remainder, command_data) = take(
                // `command_size` includes the sizes of `command` and
                // `command_size` itself, which is 8 bytes in total. So,
                // the size of the command's data is actually `command_size`
                // minus 8.
                command_size.saturating_sub(8),
            )(remainder)?;
            // Parse the command's data. Parsers for individual commands must
            // consume all `command_data`.
            match command {
                LC_MAIN => {
                    let (_, (entry_point_offset, stack_size)) =
                        self.main_command().parse(command_data)?;
                    self.entry_point_offset = Some(entry_point_offset);
                    self.stack_size = Some(stack_size);
                }
                LC_UNIXTHREAD => {
                    let (_, eip) =
                        self.thread_command().parse(command_data)?;
                    self.entry_point_rva = Some(eip);
                }
                LC_SEGMENT | LC_SEGMENT_64 => {
                    let (_, segment) =
                        self.segment_command().parse(command_data)?;
                    self.segments.push(segment);
                }
                LC_RPATH => {
                    let (_, rpath) =
                        self.rpath_command().parse(command_data)?;
                    self.rpaths.push(rpath);
                }
                LC_LOAD_DYLIB | LC_ID_DYLIB | LC_LOAD_WEAK_DYLIB
                | LC_REEXPORT_DYLIB | LC_LAZY_LOAD_DYLIB
                | LC_LOAD_UPWARD_DYLIB => {
                    let (_, dylib) =
                        self.dylib_command().parse(command_data)?;
                    self.dylibs.push(dylib);
                }
                LC_SOURCE_VERSION => {
                    let (_, ver) =
                        self.source_version_command().parse(command_data)?;
                    self.source_version =
                        Some(convert_to_source_version_string(ver));
                }
                LC_ID_DYLINKER | LC_LOAD_DYLINKER | LC_DYLD_ENVIRONMENT => {
                    let (_, dylinker) =
                        self.dylinker_command().parse(command_data)?;
                    self.dynamic_linker = Some(dylinker);
                }
                LC_SYMTAB => {
                    let (_, symtab) =
                        self.symtab_command().parse(command_data)?;
                    self.symtab = Some(symtab);
                }
                LC_DYSYMTAB => {
                    let (_, dysymtab) =
                        self.dysymtab_command().parse(command_data)?;
                    self.dysymtab = Some(dysymtab);
                }
                LC_CODE_SIGNATURE => {
                    let (_, lid) =
                        self.linkeditdata_command().parse(command_data)?;
                    self.code_signature_data = Some(lid);
                }
                LC_DYLD_EXPORTS_TRIE => {
                    let (_, exports_data) =
                        self.linkeditdata_command().parse(command_data)?;
                    self.dyld_export_trie = Some(DyldExportTrie {
                        data_off: exports_data.dataoff,
                        data_size: exports_data.datasize,
                    });
                }
                LC_DYLD_CHAINED_FIXUPS => {
                    let (_, imports_data) =
                        self.linkeditdata_command().parse(command_data)?;
                    self.dyld_chain_fixups = Some(DyldChainFixups {
                        data_off: imports_data.dataoff,
                        data_size: imports_data.datasize,
                    });
                }
                LC_DYLD_INFO | LC_DYLD_INFO_ONLY => {
                    let (_, dyld_info) =
                        self.dyld_info_command().parse(command_data)?;
                    self.dyld_info = Some(dyld_info);
                }
                LC_UUID => {
                    let (_, uuid) = self.uuid_command().parse(command_data)?;
                    self.uuid = Some(uuid);
                }
                LC_BUILD_VERSION => {
                    let (_, bv) =
                        self.build_version_command().parse(command_data)?;
                    self.build_version = Some(bv);
                }
                LC_VERSION_MIN_MACOSX
                | LC_VERSION_MIN_IPHONEOS
                | LC_VERSION_MIN_TVOS
                | LC_VERSION_MIN_WATCHOS => {
                    let (_, mut mv) =
                        self.min_version_command().parse(command_data)?;
                    mv.device = command;
                    self.min_version = Some(mv);
                }
                LC_LINKER_OPTION => {
                    let (_, linker_options) =
                        self.linker_options_command().parse(command_data)?;
                    self.linker_options.extend(linker_options);
                }
                _ => {}
            }

            Ok((remainder, ()))
        }
    }

    /// Parser that parses a LC_MAIN command.
    fn main_command(
        &mut self,
    ) -> impl Parser<&'a [u8], Output = (u64, u64), Error = NomError<'a>> + '_
    {
        (
            u64(self.endianness), // entryoff,
            u64(self.endianness), // stacksize,
        )
    }

    /// Parser that parses a LC_UNIXTHREAD command.
    fn thread_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        move |input: &'a [u8]| {
            let (remainder, (_flavor, _count)) = (
                u32(self.endianness), // flavor
                u32(self.endianness), // count
            )
                .parse(input)?;

            match self.header.cputype {
                CPU_TYPE_X86 => self.x86_thread_state().parse(remainder),
                CPU_TYPE_X86_64 => self.x86_64_thread_state().parse(remainder),
                CPU_TYPE_ARM => self.arm_thread_state().parse(remainder),
                CPU_TYPE_ARM64 => self.arm64_thread_state().parse(remainder),
                CPU_TYPE_POWERPC => self.ppc_thread_state().parse(remainder),
                CPU_TYPE_POWERPC64 => {
                    self.ppc64_thread_state().parse(remainder)
                }
                CPU_TYPE_MC680X0 => self.m68k_thread_state().parse(remainder),
                CPU_TYPE_MC88000 => self.m88k_thread_state().parse(remainder),
                CPU_TYPE_SPARC => self.sparc_thread_state().parse(remainder),
                _ => Ok((remainder, 0)),
            }
        }
    }

    /// Parser that parses a LC_SEGMENT or LC_SEGMENT_64 command.
    fn segment_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = Segment<'a>, Error = NomError<'a>> + '_
    {
        move |input: &'a [u8]| {
            let (
                remainder,
                (
                    segname,
                    vmaddr,
                    vmsize,
                    fileoff,
                    filesize,
                    maxprot,
                    initprot,
                    nsects,
                    flags,
                ),
            ) = (
                // name
                map(take(16_usize), |name| {
                    BStr::new(name).trim_end_with(|c| c == '\0')
                }),
                uint(self.endianness, self.is_32_bits), // vmaddr
                uint(self.endianness, self.is_32_bits), // vmsize
                uint(self.endianness, self.is_32_bits), // fileoff
                uint(self.endianness, self.is_32_bits), // filesize,
                u32(self.endianness),                   // maxprot,
                u32(self.endianness),                   // initprot,
                u32(self.endianness),                   // nsects,
                u32(self.endianness),                   // flags,
            )
                .parse(input)?;

            let (remainder, sections) =
                count(self.section(), nsects as usize).parse(remainder)?;

            Ok((
                remainder,
                Segment {
                    segname,
                    vmaddr,
                    vmsize,
                    fileoff,
                    filesize,
                    maxprot,
                    initprot,
                    nsects,
                    flags,
                    sections,
                },
            ))
        }
    }

    /// Parser that parses a LC_LOAD_DYLIB, LC_ID_DYLIB, LC_LOAD_WEAK_DYLIB
    /// or LC_REEXPORT_DYLIB command.
    fn dylib_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = Dylib<'a>, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness),        // offset,
                u32(self.endianness),        // timestamp,
                u32(self.endianness),        // current_version,
                u32(self.endianness),        // compatibility_version,
                take_till(|b| b == b'\x00'), // name
            ),
            |(
                _offset,
                timestamp,
                current_version,
                compatibility_version,
                name,
            )| {
                Dylib {
                    name: BStr::new(name),
                    timestamp,
                    current_version,
                    compatibility_version,
                }
            },
        )
    }

    /// Parser that parses a LC_DYSYMTAB command.
    fn symtab_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = Symtab<'a>, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness), //  symoff
                u32(self.endianness), //  nsyms
                u32(self.endianness), //  stroff
                u32(self.endianness), //  strsize
            ),
            |(symoff, nsyms, stroff, strsize)| Symtab {
                symoff,
                nsyms,
                stroff,
                strsize,
                entries: Vec::new(),
                nlists: Vec::new(),
            },
        )
    }

    /// Parser that parses a LC_DYSYMTAB command.
    fn dysymtab_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = Dysymtab, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness), //  ilocalsym
                u32(self.endianness), //  nlocalsym
                u32(self.endianness), //  iextdefsym
                u32(self.endianness), //  nextdefsym
                u32(self.endianness), //  tocoff
                u32(self.endianness), //  ntoc
                u32(self.endianness), //  modtaboff
                u32(self.endianness), //  nmodtab
                u32(self.endianness), //  extrefsymoff
                u32(self.endianness), //  nextrefsyms
                u32(self.endianness), //  indirectsymoff
                u32(self.endianness), //  nindirectsyms =
                u32(self.endianness), //  extreloff
                u32(self.endianness), //  nextrel
                u32(self.endianness), //  locreloff
                u32(self.endianness), //  nlocrel
            ),
            |(
                ilocalsym,
                nlocalsym,
                iextdefsym,
                nextdefsym,
                tocoff,
                ntoc,
                modtaboff,
                nmodtab,
                extrefsymoff,
                nextrefsyms,
                indirectsymoff,
                nindirectsyms,
                extreloff,
                nextrel,
                locreloff,
                nlocrel,
            )| {
                Dysymtab {
                    ilocalsym,
                    nlocalsym,
                    iextdefsym,
                    nextdefsym,
                    tocoff,
                    ntoc,
                    modtaboff,
                    nmodtab,
                    extrefsymoff,
                    nextrefsyms,
                    indirectsymoff,
                    nindirectsyms,
                    extreloff,
                    nextrel,
                    locreloff,
                    nlocrel,
                }
            },
        )
    }

    /// Parser that parses a LC_CODESIGNATURE command
    fn linkeditdata_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = LinkedItData, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness), //  dataoff
                u32(self.endianness), //  datasize
            ),
            |(dataoff, datasize)| LinkedItData { dataoff, datasize },
        )
    }

    fn cs_blob(
        &self,
    ) -> impl Parser<&'a [u8], Output = CSBlob, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(Endianness::Big), // magic
                u32(Endianness::Big), // length,
            ),
            |(magic, length)| CSBlob { magic, length },
        )
    }

    fn cs_index(
        &self,
    ) -> impl Parser<&'a [u8], Output = CSBlobIndex, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(Endianness::Big), // blobtype
                u32(Endianness::Big), // offset,
            ),
            |(_blobtype, offset)| CSBlobIndex {
                _blobtype,
                offset,
                blob: None,
            },
        )
    }

    fn cs_superblob(
        &mut self,
    ) -> impl Parser<&'a [u8], Output = CSSuperBlob, Error = NomError<'a>> + '_
    {
        move |input: &'a [u8]| {
            let (mut remainder, (_magic, _length, count)) = (
                u32(Endianness::Big), // magic
                u32(Endianness::Big), // offset,
                u32(Endianness::Big), // count,
            )
                .parse(input)?;

            let mut super_blob =
                CSSuperBlob { _magic, _length, count, index: Vec::new() };

            let mut cs_index: CSBlobIndex;

            for _ in 0..super_blob.count {
                (remainder, cs_index) = self.cs_index().parse(remainder)?;

                cs_index.blob = input
                    .get(cs_index.offset as usize..)
                    .and_then(|blob_data| self.cs_blob().parse(blob_data).ok())
                    .map(|(_, blob)| blob);

                super_blob.index.push(cs_index);
            }

            let super_data = input;

            // Iterator over the `CSBlobIndex` entries that have some blob.
            let blobs = super_blob.index.iter().filter_map(|blob_index| {
                blob_index
                    .blob
                    .as_ref()
                    .map(|blob| (blob_index.offset as usize, blob))
            });

            for (offset, blob) in blobs {
                let length = blob.length as usize;
                let size_of_blob = std::mem::size_of::<CSBlob>();
                match blob.magic {
                    CS_MAGIC_EMBEDDED_ENTITLEMENTS => {
                        let xml_data = match super_data.get(
                            offset.saturating_add(size_of_blob)
                                ..offset.saturating_add(length),
                        ) {
                            Some(data) => data,
                            None => continue,
                        };

                        let xml_string =
                            std::str::from_utf8(xml_data).unwrap_or_default();

                        let opt = roxmltree::ParsingOptions {
                            allow_dtd: true,
                            ..roxmltree::ParsingOptions::default()
                        };

                        if let Ok(parsed_xml) =
                            roxmltree::Document::parse_with_options(
                                xml_string, opt,
                            )
                        {
                            for node in parsed_xml.descendants().filter(|n| {
                                n.has_tag_name("key")
                                    || n.has_tag_name("array")
                            }) {
                                if let Some(entitlement) = node.text() {
                                    if node.has_tag_name("array") {
                                        node.descendants()
                                            .filter_map(|n| n.text())
                                            .filter(|t| !t.trim().is_empty())
                                            .unique()
                                            .map(|t| t.to_string())
                                            .for_each(|array_entitlement| {
                                                self.entitlements
                                                    .push(array_entitlement)
                                            });
                                    } else {
                                        self.entitlements
                                            .push(entitlement.to_string());
                                    }
                                }
                            }
                        }
                    }
                    CS_MAGIC_BLOBWRAPPER => {
                        if let Some(ber_blob) = super_data.get(
                            offset.saturating_add(size_of_blob)
                                ..offset.saturating_add(length),
                        )
                            && let Ok((_remainder, certs)) =
                                parse_certificates(ber_blob)
                            {
                                self.certificates.extend(certs);
                            }
                    }
                    _ => {}
                }
            }

            Ok((remainder, super_blob))
        }
    }

    /// Parser that parses LC_DYLD_INFO_ONLY and LC_DYLD_INFO commands
    fn dyld_info_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = DyldInfo, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness), //  rebase_off
                u32(self.endianness), //  rebase_size
                u32(self.endianness), //  bind_off
                u32(self.endianness), //  bind_size
                u32(self.endianness), //  weak_bind_off
                u32(self.endianness), //  weak_bind_size
                u32(self.endianness), //  lazy_bind_off
                u32(self.endianness), //  lazy_bind_size
                u32(self.endianness), //  export_off
                u32(self.endianness), //  export_size
            ),
            |(
                rebase_off,
                rebase_size,
                bind_off,
                bind_size,
                weak_bind_off,
                weak_bind_size,
                lazy_bind_off,
                lazy_bind_size,
                export_off,
                export_size,
            )| {
                DyldInfo {
                    rebase_off,
                    rebase_size,
                    bind_off,
                    bind_size,
                    weak_bind_off,
                    weak_bind_size,
                    lazy_bind_off,
                    lazy_bind_size,
                    export_off,
                    export_size,
                }
            },
        )
    }

    /// Parser that parses the nlist structures that are defined by the offsets
    /// in LC_SYMTAB.
    fn nlist(
        &self,
    ) -> impl Parser<&'a [u8], Output = Nlist, Error = NomError<'a>> + '_ {
        map(
            (
                u32(self.endianness),                   // n_strx
                u8,                                     // n_type
                u8,                                     // n_sect
                u16(self.endianness),                   // n_desc
                uint(self.endianness, self.is_32_bits), // n_value
            ),
            |(n_strx, n_type, n_sect, n_desc, n_value)| Nlist {
                n_strx,
                n_type,
                n_sect,
                n_desc,
                n_value,
            },
        )
    }

    /// Parser that parses the symbol table which includes the nlist structure
    /// and the relevant string from the string table as defined in LC_SYMTAB.
    fn parse_symtab(
        &mut self,
        string_table: &'a [u8],
        symbol_table: &'a [u8],
        count: u32,
    ) -> IResult<&'a [u8], ()> {
        let mut data = symbol_table;
        let mut n;

        for _ in 0..count {
            (data, n) = self.nlist().parse(data)?;
            if let Some(symtab) = self.symtab.as_mut()
                && let Some(string_data) =
                    string_table.get(n.n_strx as usize..)
                {
                    let (_, string_value) = map(
                        (take_till(|b| b == b'\x00'), tag("\x00")),
                        |(s, _)| s,
                    )
                    .parse(string_data)?;

                    if !string_value.is_empty() {
                        symtab.entries.push(string_value);
                        symtab.nlists.push(n);
                    }
                }
        }

        Ok((data, ()))
    }

    /// Parser that parses the exports at the offsets defined within
    /// LC_DYLD_INFO, LC_DYLD_INFO_ONLY, and LC_DYLD_EXPORTS_TRIE.
    fn parse_exports(&mut self, data: &'a [u8]) -> IResult<&'a [u8], ()> {
        let mut stack = Vec::<ExportNode>::new();
        let mut visited = HashSet::<usize>::new();

        stack.push(ExportNode { offset: 0, prefix: "".to_string() });

        while !stack.is_empty() && !data.is_empty() {
            let export_node = stack.pop().unwrap();

            // If node was already visited, continue without processing it.
            if !visited.insert(export_node.offset) {
                continue;
            }

            let node_data = match data.get(export_node.offset..) {
                Some(data) => data,
                None => continue,
            };

            let (mut remaining_data, length) = uleb128(node_data)?;

            if length != 0 {
                let (remainder, flags) = uleb128(remaining_data)?;
                match flags {
                    EXPORT_SYMBOL_FLAGS_STUB_AND_RESOLVER => {
                        let (remainder, (_stub_offset, _resolver_offset)) =
                            (uleb128, uleb128).parse(remainder)?;
                        remaining_data = remainder;
                    }
                    EXPORT_SYMBOL_FLAGS_REEXPORT => {
                        let (remainder, _ordinal) = uleb128(remainder)?;
                        let (remainder, _label) = map(
                            (take_till(|b| b == b'\x00'), tag("\x00")),
                            |(s, _)| s,
                        )
                        .parse(remainder)?;

                        remaining_data = remainder;
                    }
                    _ => {
                        let (remainder, _offset) = uleb128(remainder)?;
                        remaining_data = remainder;
                    }
                }
            }

            let (mut edge_remainder, edges) = u8(remaining_data)?;

            for _ in 0..edges {
                let (remainder, edge_label) = map(
                    (take_till(|b| b == b'\x00'), tag("\x00")),
                    |(s, _)| BStr::new(s),
                )
                .parse(edge_remainder)?;

                let (remainder, edge_offset) = uleb128(remainder)?;

                if let Ok(edge_label_str) = edge_label.to_str() {
                    stack.push(ExportNode {
                        offset: edge_offset as usize,
                        prefix: format!(
                            "{}{}",
                            export_node.prefix, edge_label_str
                        ),
                    });
                }

                edge_remainder = remainder;
            }

            if length != 0 {
                self.exports.push(export_node.prefix)
            }
        }

        Ok((&[], ()))
    }

    /// Parser that parses the imports at the offsets defined within LC_DYLD_INFO and LC_DYLD_INFO_ONLY
    fn parse_imports(
        &mut self,
        data: &'a [u8],
        seen: &mut HashSet<&'a str>,
    ) -> IResult<&'a [u8], ()> {
        let mut remainder: &[u8] = data;
        let mut entry: u8;

        while !remainder.is_empty() {
            (remainder, entry) = u8(remainder)?;
            let opcode = entry & BIND_OPCODE_MASK;
            let _immediate = entry & BIND_IMMEDIATE_MASK;
            match opcode {
                BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB
                | BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB
                | BIND_OPCODE_ADD_ADDR_ULEB
                | BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB => {
                    (remainder, _) = uleb128(remainder)?;
                }
                BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB => {
                    (remainder, _) = uleb128(remainder)?;
                    (remainder, _) = uleb128(remainder)?;
                }
                BIND_OPCODE_SET_ADDEND_SLEB => {
                    (remainder, _) = sleb128(remainder)?;
                }

                BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM => {
                    let (import_remainder, strr) = map(
                        (take_till(|b| b == b'\x00'), tag("\x00")),
                        |(s, _)| BStr::new(s),
                    )
                    .parse(remainder)?;
                    remainder = import_remainder;
                    if let Ok(import) = strr.to_str()
                        && !seen.contains(import) {
                            self.imports.push(import.to_string());
                            seen.insert(import);
                        }
                }
                _ => {}
            }
        }

        Ok((remainder, ()))
    }

    /// Parser that parses the header for the chained fixups designated by LC_DYLD_CHAINED_FIXUPS.
    fn chained_fixup_header(
        &self,
    ) -> impl Parser<&'a [u8], Output = ChainedFixupsHeader, Error = NomError<'a>>
           + '_ {
        map(
            (
                u32(self.endianness), //  fixups_version
                u32(self.endianness), //  starts_offset
                u32(self.endianness), //  imports_offset
                u32(self.endianness), //  symbols_offset
                u32(self.endianness), //  imports_count
                u32(self.endianness), //  imports_format
                u32(self.endianness), //  symbols_format
            ),
            |(
                fixups_version,
                starts_offset,
                imports_offset,
                symbols_offset,
                imports_count,
                imports_format,
                symbols_format,
            )| {
                ChainedFixupsHeader {
                    _fixups_version: fixups_version,
                    _starts_offset: starts_offset,
                    imports_offset,
                    symbols_offset,
                    imports_count,
                    imports_format,
                    _symbols_format: symbols_format,
                }
            },
        )
    }

    /// Parser that parses the chained fixup imports designated by LC_DYLD_CHAINED_FIXUPS.
    fn parse_chained_fixups(
        &mut self,
        data: &'a [u8],
    ) -> IResult<&'a [u8], ()> {
        let (_, header) = self.chained_fixup_header().parse(data)?;

        if let Some(import_data) = data.get(header.imports_offset as usize..) {
            let entry_size = match header.imports_format {
                DYLD_CHAINED_IMPORT => 4,
                DYLD_CHAINED_IMPORT_ADDEND => 8,
                DYLD_CHAINED_IMPORT_ADDEND64 => 16,
                _ => 4, // fallback
            };

            let is_addend64 =
                header.imports_format == DYLD_CHAINED_IMPORT_ADDEND64;

            let (shift_symbol, symbol_mask, ordinal_mask, shift_kind) =
                if is_addend64 {
                    (32, 0xFFFFFFFF, 0xFFFF, 16)
                } else {
                    (9, 0x7FFFFF, 0xFF, 8)
                };

            let imports_size = (header.imports_count as usize) * entry_size;
            if let Some(raw_imports_blob) = import_data.get(..imports_size) {
                for chunk in raw_imports_blob.chunks_exact(entry_size) {
                    // this is u64 if DYLD_CHAINED_IMPORT_ADDEND64 else u32
                    let chained_import_value = if is_addend64 {
                        let (_, val) = u64(self.endianness)(chunk)?;
                        val
                    } else {
                        let (_, val) = u32(self.endianness)(chunk)?;
                        val as u64
                    };

                    let _lib_ordinal = chained_import_value & ordinal_mask;
                    let _import_kind =
                        (chained_import_value >> shift_kind) & 0x1;

                    let name_offset =
                        (chained_import_value >> shift_symbol) & symbol_mask;

                    if let Some(name_buffer) = data.get(
                        header
                            .symbols_offset
                            // name_offset is always _at most_ 32 bits, so this is ok to cast down
                            .saturating_add(name_offset as u32)
                            as usize..,
                    ) {
                        let (_remainder, import_str) = map(
                            (take_till(|b| b == b'\x00'), tag("\x00")),
                            |(s, _)| s,
                        )
                        .parse(name_buffer)?;
                        if let Ok(import) = import_str.to_str() {
                            self.imports.push(import.to_string());
                        }
                    }
                }
            }
        }

        Ok((&[], ()))
    }

    /// Parser that parses a LC_ID_DYLINKER, LC_LOAD_DYLINKER or
    /// LC_DYLD_ENVIRONMENT  command.
    fn dylinker_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = &'a [u8], Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness),        // offset,
                take_till(|b| b == b'\x00'), // command
            ),
            |(_offset, command)| command,
        )
    }

    /// Parser that parses a LC_LINKER_OPTION command.
    fn linker_options_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = Vec<&'a [u8]>, Error = NomError<'a>> + '_
    {
        length_count(
            u32(self.endianness), // count
            map((take_till(|b| b == b'\x00'), tag("\x00")), |(s, _)| s),
        )
    }

    /// Parser that parses a LC_UUID command.
    fn uuid_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = &'a [u8], Error = NomError<'a>> + '_
    {
        map(take(16usize), |uuid| BStr::new(uuid).trim_end_with(|c| c == '\0'))
    }

    /// Parser that parses a LC_SOURCE_VERSION command.
    fn source_version_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        u64(self.endianness)
    }

    /// Parser that parses a LC_RPATH command.
    fn rpath_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = &'a [u8], Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness),
                take_till(|b| b == b'\x00'), // rpath
            ),
            |(_, rpath)| rpath,
        )
    }

    /// Parser that parses a LC_BUILD_VERSION command.
    fn build_version_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = BuildVersionCommand, Error = NomError<'a>>
           + '_ {
        map(
            (
                u32(self.endianness), // platform,
                u32(self.endianness), // minos,
                u32(self.endianness), // sdk,
                length_count(
                    u32(self.endianness), // ntools,
                    map(
                        (
                            u32(self.endianness), // tool,
                            u32(self.endianness), // version,
                        ),
                        |(tool, version)| BuildToolObject { tool, version },
                    ),
                ),
            ),
            |(platform, minos, sdk, tools)| BuildVersionCommand {
                platform,
                minos,
                sdk,
                tools,
            },
        )
    }

    fn min_version_command(
        &self,
    ) -> impl Parser<&'a [u8], Output = MinVersion, Error = NomError<'a>> + '_
    {
        map(
            (
                u32(self.endianness), // version
                u32(self.endianness), // sdk,
            ),
            |(version, sdk)| MinVersion { device: 0, version, sdk },
        )
    }

    fn x86_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                u32(self.endianness), // eax
                u32(self.endianness), // ebx
                u32(self.endianness), // ecx
                u32(self.endianness), // edx
                u32(self.endianness), // edi
                u32(self.endianness), // esi
                u32(self.endianness), // ebp
                u32(self.endianness), // esp
                u32(self.endianness), // ss
                u32(self.endianness), // eflags
                u32(self.endianness), // eip
                u32(self.endianness), // cs
                u32(self.endianness), // ds
                u32(self.endianness), // es
                u32(self.endianness), // fs
                u32(self.endianness), // gs
            ),
            |reg| reg.10 as u64, // eip,
        )
    }

    fn x86_64_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                u64(self.endianness), // rax
                u64(self.endianness), // rbx
                u64(self.endianness), // rcx
                u64(self.endianness), // rdx
                u64(self.endianness), // rdi
                u64(self.endianness), // rsi
                u64(self.endianness), // rbp
                u64(self.endianness), // rsp
                u64(self.endianness), // r8
                u64(self.endianness), // r9
                u64(self.endianness), // r10
                u64(self.endianness), // r11
                u64(self.endianness), // r12
                u64(self.endianness), // r13
                u64(self.endianness), // r14
                u64(self.endianness), // r15
                u64(self.endianness), // rip
                u64(self.endianness), // rflags
                u64(self.endianness), // cs
                u64(self.endianness), // fs
                u64(self.endianness), // gs
            ),
            |reg| reg.16, // eip,
        )
    }

    fn arm_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                count(u32(self.endianness), 13), // r
                u32(self.endianness),            // sp
                u32(self.endianness),            // lr
                u32(self.endianness),            // pc
                u32(self.endianness),            // cpsr
            ),
            |(_, _, _, pc, _)| pc as u64,
        )
    }

    fn arm64_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                count(u64(self.endianness), 29), // r
                u64(self.endianness),            // fp
                u64(self.endianness),            // lr
                u64(self.endianness),            // sp
                u64(self.endianness),            // pc
                u32(self.endianness),            // cpsr
            ),
            |(_, _, _, _, pc, _)| pc,
        )
    }

    fn ppc_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                uint(self.endianness, true),            // srr0
                uint(self.endianness, true),            // srr1
                count(uint(self.endianness, true), 32), // r
                uint(self.endianness, true),            // cr
                uint(self.endianness, true),            // xer
                uint(self.endianness, true),            // lr
                uint(self.endianness, true),            // ctr
                uint(self.endianness, true),            // mq
                uint(self.endianness, true),            // vrsavead
            ),
            |(srr0, _, _, _, _, _, _, _, _)| srr0,
        )
    }

    fn ppc64_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                uint(self.endianness, false),            // srr0
                uint(self.endianness, false),            // srr1
                count(uint(self.endianness, false), 32), // r
                uint(self.endianness, true),             // cr
                uint(self.endianness, false),            // xer
                uint(self.endianness, false),            // lr
                uint(self.endianness, false),            // ctr
                uint(self.endianness, false),            // vrsave
            ),
            |(srr0, _, _, _, _, _, _, _)| srr0,
        )
    }

    fn sparc_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                u32(self.endianness),           // psr
                u32(self.endianness),           // pc
                u32(self.endianness),           // npc
                u32(self.endianness),           // y
                count(u32(self.endianness), 7), // g
                count(u32(self.endianness), 7), // o
            ),
            |(_, pc, _, _, _, _)| pc as u64,
        )
    }

    fn m68k_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                count(u32(self.endianness), 8), // dreg
                count(u32(self.endianness), 8), // areg
                u16(self.endianness),           // pad
                u16(self.endianness),           // sr
                u32(self.endianness),           // pc
            ),
            |(_, _, _, _, pc)| pc as u64,
        )
    }

    fn m88k_thread_state(
        &self,
    ) -> impl Parser<&'a [u8], Output = u64, Error = NomError<'a>> + '_ {
        map(
            (
                count(u32(self.endianness), 31), // r
                u32(self.endianness),            // xip
                u32(self.endianness),            // xip_in_bd
                u32(self.endianness),            // nip
            ),
            |(_, xip, _, _)| xip as u64,
        )
    }
}

struct FatArch {
    cputype: u32,
    cpusubtype: u32,
    offset: u64,
    size: u64,
    align: u32,
    reserved: u32,
}

struct MachOHeader {
    magic: u32,
    cputype: u32,
    cpusubtype: u32,
    filetype: u32,
    ncmds: u32,
    sizeofcmds: u32,
    flags: u32,
    reserved: Option<u32>, // Only set in 64-bits binary.
}

struct Segment<'a> {
    segname: &'a [u8],
    vmaddr: u64,
    vmsize: u64,
    fileoff: u64,
    filesize: u64,
    maxprot: u32,
    initprot: u32,
    nsects: u32,
    flags: u32,
    sections: Vec<Section<'a>>,
}

struct Section<'a> {
    sectname: &'a [u8],
    segname: &'a [u8],
    addr: u64,
    size: u64,
    offset: u32,
    align: u32,
    reloff: u32,
    nreloc: u32,
    flags: u32,
    reserved1: u32,
    reserved2: u32,
    reserved3: Option<u32>, // Only set in 64-bits binaries
}

struct Dylib<'a> {
    name: &'a [u8],
    timestamp: u32,
    current_version: u32,
    compatibility_version: u32,
}

#[derive(Default)]
struct Certificate {
    issuer: String,
    subject: String,
    is_self_signed: bool,
}

struct CSBlob {
    magic: u32,
    length: u32,
}

struct CSBlobIndex {
    _blobtype: u32,
    offset: u32,
    blob: Option<CSBlob>,
}

struct CSSuperBlob {
    _magic: u32,
    _length: u32,
    count: u32,
    index: Vec<CSBlobIndex>,
}

struct LinkedItData {
    dataoff: u32,
    datasize: u32,
}

struct Symtab<'a> {
    symoff: u32,
    nsyms: u32,
    stroff: u32,
    strsize: u32,
    entries: Vec<&'a [u8]>,
    nlists: Vec<Nlist>,
}

struct Nlist {
    n_strx: u32,
    n_type: u8,
    n_sect: u8,
    n_desc: u16,
    n_value: u64,
}

struct Dysymtab {
    ilocalsym: u32,
    nlocalsym: u32,
    iextdefsym: u32,
    nextdefsym: u32,
    tocoff: u32,
    ntoc: u32,
    modtaboff: u32,
    nmodtab: u32,
    extrefsymoff: u32,
    nextrefsyms: u32,
    indirectsymoff: u32,
    nindirectsyms: u32,
    extreloff: u32,
    nextrel: u32,
    locreloff: u32,
    nlocrel: u32,
}

struct DyldInfo {
    rebase_off: u32,
    rebase_size: u32,
    bind_off: u32,
    bind_size: u32,
    weak_bind_off: u32,
    weak_bind_size: u32,
    lazy_bind_off: u32,
    lazy_bind_size: u32,
    export_off: u32,
    export_size: u32,
}

struct DyldExportTrie {
    data_off: u32,
    data_size: u32,
}

struct DyldChainFixups {
    data_off: u32,
    data_size: u32,
}

struct ChainedFixupsHeader {
    _fixups_version: u32,
    _starts_offset: u32,
    imports_offset: u32,
    symbols_offset: u32,
    imports_count: u32,
    imports_format: u32,
    _symbols_format: u32,
}

struct BuildVersionCommand {
    platform: u32,
    minos: u32,
    sdk: u32,
    tools: Vec<BuildToolObject>,
}

struct BuildToolObject {
    tool: u32,
    version: u32,
}

struct MinVersion {
    device: u32,
    version: u32,
    sdk: u32,
}

struct ExportNode {
    offset: usize,
    prefix: String,
}

/// Parser that reads a 32-bits or 64-bits
fn uint(
    endianness: Endianness,
    _32bits: bool,
) -> impl FnMut(&[u8]) -> IResult<&[u8], u64> {
    move |input: &[u8]| {
        if _32bits {
            let (remainder, i) = u32(endianness)(input)?;
            Ok((remainder, i as u64))
        } else {
            u64(endianness)(input)
        }
    }
}

/// Convert a decimal number representation to a version string representation.
fn convert_to_version_string(decimal_number: u32) -> String {
    let major = decimal_number >> 16;
    let minor = (decimal_number >> 8) & 0xFF;
    let patch = decimal_number & 0xFF;
    format!("{major}.{minor}.{patch}")
}

/// Convert a decimal number representation to a build version string representation.
fn convert_to_build_tool_version(decimal_number: u32) -> String {
    let a = decimal_number >> 16;
    let b = (decimal_number >> 8) & 0xff;
    format!("{a}.{b}")
}

/// Convert a decimal number representation to a source version string
/// representation.
fn convert_to_source_version_string(decimal_number: u64) -> String {
    let mask = 0x3f;
    let a = decimal_number >> 40;
    let b = (decimal_number >> 30) & mask;
    let c = (decimal_number >> 20) & mask;
    let d = (decimal_number >> 10) & mask;
    let e = decimal_number & mask;
    format!("{a}.{b}.{c}.{d}.{e}")
}

/// Parses CMS certificates from a BER-encoded blob that are embedded in the
/// Mach-O binary.
fn parse_certificates(ber_blob: &[u8]) -> BerResult<'_, Vec<Certificate>> {
    parse_ber_sequence_defined_g(|ber_blob: &[u8], _| {
        let (remainder, _content_type) = parse_ber_oid(ber_blob)?;

        parse_ber_tagged_explicit_g(0, |content, _| {
            parse_ber_sequence_defined_g(|content: &[u8], _| {
                let (remainder, _cms_version) = parse_ber_integer(content)?;

                let (remainder, _digest_algorithms) =
                    parse_digest_algorithms(remainder)?;

                let (remainder, _content_info) =
                    parse_content_info(remainder)?;

                let (remainder, certificates) = OptTaggedParser::from(0)
                    .parse_ber(
                        remainder,
                        |_, raw_certs| -> ParseResult<'_, Vec<_>> {
                            Ok(SignedData::parse_certificates(raw_certs))
                        },
                    )
                    .map_err(|_| BerValueError)?;

                let certificates: Vec<Certificate> = certificates
                    .iter()
                    .flatten()
                    .map(|c| Certificate {
                        issuer: c.x509.issuer.to_string(),
                        subject: c.x509.subject.to_string(),
                        is_self_signed: c.x509.issuer == c.x509.subject,
                    })
                    .collect();

                Ok((remainder, certificates))
            })(content)
        })(remainder)
    })(ber_blob)
}

/// Parses a BER-encoded sequence of AlgorithmIdentifiers.
fn parse_digest_algorithms(
    input: &[u8],
) -> BerResult<'_, Vec<AlgorithmIdentifier<'_>>> {
    Ok(parse_ber_set_of_v(AlgorithmIdentifier::from_ber)(input)
        .map_err(|_| BerValueError)?)
}

/// Parses a BER-encoded sequence of ContentInfo objects.
fn parse_content_info(input: &[u8]) -> BerResult<'_> {
    parse_ber_sequence(input)
}

impl From<MachO<'_>> for protos::macho::Macho {
    fn from(macho: MachO<'_>) -> Self {
        let mut result = protos::macho::Macho::new();
        // If the Mach-O file is a single-architecture binary, fill the fields
        // at the top level of `protos::macho::Macho` structure. If it is a
        // multi-architecture binary (FAT binary) then fill the `fat_arch`
        // and `file` arrays.
        if macho.files.len() == 1 {
            let m = macho.files.first().unwrap();
            result.set_magic(m.header.magic);
            result.set_ncmds(m.header.ncmds);
            result.set_cputype(m.header.cputype);
            result.set_cpusubtype(m.header.cpusubtype);
            result.set_filetype(m.header.filetype);
            result.set_flags(m.header.flags);
            result.set_sizeofcmds(m.header.sizeofcmds);
            result.reserved = m.header.reserved;
            result.entry_point = m.entry_point_offset;
            result.stack_size = m.stack_size;
            m.source_version.clone_into(&mut result.source_version);
            result.dynamic_linker = m.dynamic_linker.map(|dl| dl.into());

            if let Some(symtab) = &m.symtab {
                result.symtab = MessageField::some(symtab.into());
            }

            if let Some(dysymtab) = &m.dysymtab {
                result.dysymtab = MessageField::some(dysymtab.into());
            }

            if let Some(cs_data) = &m.code_signature_data {
                result.code_signature_data =
                    MessageField::some(cs_data.into());
            }

            if let Some(dyld_info) = &m.dyld_info {
                result.dyld_info = MessageField::some(dyld_info.into());
            };

            if let Some(uuid) = &m.uuid {
                let mut uuid_str = String::new();

                for (idx, c) in uuid.iter().enumerate() {
                    match idx {
                        3 | 5 | 7 | 9 => {
                            uuid_str.push_str(format!("{c:02X}").as_str());
                            uuid_str.push('-');
                        }
                        _ => {
                            uuid_str.push_str(format!("{c:02X}").as_str());
                        }
                    }
                }

                result.uuid = Some(uuid_str.clone());
            }

            if let Some(bv) = &m.build_version {
                result.build_version = MessageField::some(bv.into());
            }

            if let Some(mv) = &m.min_version {
                result.min_version = MessageField::some(mv.into());
            }

            result.segments.extend(m.segments.iter().map(|seg| seg.into()));
            result.dylibs.extend(m.dylibs.iter().map(|dylib| dylib.into()));
            result
                .rpaths
                .extend(m.rpaths.iter().map(|rpath: &&[u8]| rpath.to_vec()));
            result.entitlements.extend(m.entitlements.clone());
            result.exports.extend(m.exports.clone());
            result.imports.extend(m.imports.clone());

            // If the exports are empty, iterate the symbol table entries to
            // check like dyld_info does:
            // https://github.com/apple-oss-distributions/dyld/blob/main/other-tools/dyld_info.cpp#L560-L617
            if m.dyld_export_trie.is_none() && m.dyld_info.is_none()
                && let Some(symtab) = &m.symtab {
                    result.exports.extend(
                        symtab
                            .nlists
                            .iter()
                            .enumerate()
                            .filter_map(|(idx, nlist)| {
                                let t = nlist.n_type & N_TYPE;
                                if (nlist.n_type & N_EXT != 0)
                                    && ((t == N_SECT)
                                        || (t == N_ABS)
                                        || (t == N_INDR))
                                    && ((nlist.n_type & N_STAB) == 0)
                                {
                                    symtab.entries.get(idx)
                                } else {
                                    None
                                }
                            })
                            .map(|sym| BStr::new(sym).to_string()),
                    )
                }

            // If the imports are empty, iterate the symbol table entries to
            // check for undefined symbols like dyld_info does:
            // https://github.com/apple-oss-distributions/dyld/blob/main/other-tools/dyld_info.cpp#L372-L398
            if m.dyld_chain_fixups.is_none() && m.dyld_info.is_none()
                && let Some(symtab) = &m.symtab {
                    result.imports.extend(
                        symtab
                            .nlists
                            .iter()
                            .enumerate()
                            .filter_map(|(idx, nlist)| {
                                let t = nlist.n_type & N_TYPE;
                                if (t == N_UNDF)
                                    && (nlist.n_type & N_STAB == 0)
                                {
                                    symtab.entries.get(idx)
                                } else {
                                    None
                                }
                            })
                            .map(|sym| BStr::new(sym).to_string()),
                    );
                }

            result
                .certificates
                .extend(m.certificates.iter().map(|cert| cert.into()));

            result
                .set_number_of_segments(m.segments.len().try_into().unwrap());

            result
                .linker_options
                .extend(m.linker_options.iter().map(|lo| lo.to_vec()));
        } else {
            result.fat_magic = macho.fat_magic;
            result.set_nfat_arch(macho.archs.len().try_into().unwrap());
            result.fat_arch.extend(macho.archs.iter().map(|arch| arch.into()));
            result.file.extend(macho.files.iter().map(|file| file.into()));
        }
        result
    }
}

impl From<&MachOFile<'_>> for protos::macho::File {
    fn from(macho: &MachOFile<'_>) -> Self {
        let mut result = protos::macho::File::new();
        result.set_magic(macho.header.magic);
        result.set_ncmds(macho.header.ncmds);
        result.set_cputype(macho.header.cputype);
        result.set_cpusubtype(macho.header.cpusubtype);
        result.set_filetype(macho.header.filetype);
        result.set_flags(macho.header.flags);
        result.set_sizeofcmds(macho.header.sizeofcmds);
        result.reserved = macho.header.reserved;
        result.entry_point = macho.entry_point_offset;
        result.stack_size = macho.stack_size;
        macho.source_version.clone_into(&mut result.source_version);
        result.dynamic_linker = macho.dynamic_linker.map(|dl| dl.into());

        if let Some(symtab) = &macho.symtab {
            result.symtab = MessageField::some(symtab.into());
        }

        if let Some(dysymtab) = &macho.dysymtab {
            result.dysymtab = MessageField::some(dysymtab.into());
        }

        if let Some(cs_data) = &macho.code_signature_data {
            result.code_signature_data = MessageField::some(cs_data.into());
        }

        if let Some(dyld_info) = &macho.dyld_info {
            result.dyld_info = MessageField::some(dyld_info.into());
        };

        if let Some(uuid) = &macho.uuid {
            let mut uuid_str = String::new();

            for (idx, c) in uuid.iter().enumerate() {
                match idx {
                    3 | 5 | 7 | 9 => {
                        uuid_str.push_str(format!("{c:02X}").as_str());
                        uuid_str.push('-');
                    }
                    _ => {
                        uuid_str.push_str(format!("{c:02X}").as_str());
                    }
                }
            }

            result.uuid = Some(uuid_str.clone());
        }

        if let Some(bv) = &macho.build_version {
            result.build_version = MessageField::some(bv.into());
        }

        if let Some(mv) = &macho.min_version {
            result.min_version = MessageField::some(mv.into());
        }

        result.segments.extend(macho.segments.iter().map(|seg| seg.into()));
        result.dylibs.extend(macho.dylibs.iter().map(|dylib| dylib.into()));
        result.rpaths.extend(macho.rpaths.iter().map(|rpath| rpath.to_vec()));
        result.entitlements.extend(macho.entitlements.clone());
        result.exports.extend(macho.exports.clone());
        result.imports.extend(macho.imports.clone());

        // If the exports are empty, iterate the symbol table entries to check
        // like dyld_info does:
        // https://github.com/apple-oss-distributions/dyld/blob/main/other-tools/dyld_info.cpp#L560-L617
        if macho.dyld_export_trie.is_none() && macho.dyld_info.is_none()
            && let Some(symtab) = &macho.symtab {
                result.exports.extend(
                    symtab
                        .nlists
                        .iter()
                        .enumerate()
                        .filter_map(|(idx, nlist)| {
                            let t = nlist.n_type & N_TYPE;
                            if (nlist.n_type & N_EXT != 0)
                                && ((t == N_SECT)
                                    || (t == N_ABS)
                                    || (t == N_INDR))
                                && ((nlist.n_type & N_STAB) == 0)
                            {
                                symtab.entries.get(idx)
                            } else {
                                None
                            }
                        })
                        .map(|sym| BStr::new(sym).to_string()),
                )
            }

        // If the imports are empty, iterate the symbol table entries to
        // check for undefined symbols like dyld_info does:
        // https://github.com/apple-oss-distributions/dyld/blob/main/other-tools/dyld_info.cpp#L372-L398
        if macho.dyld_chain_fixups.is_none() && macho.dyld_info.is_none()
            && let Some(symtab) = &macho.symtab {
                result.imports.extend(
                    symtab
                        .nlists
                        .iter()
                        .enumerate()
                        .filter_map(|(idx, nlist)| {
                            let t = nlist.n_type & N_TYPE;
                            if (t == N_UNDF) && (nlist.n_type & N_STAB == 0) {
                                symtab.entries.get(idx)
                            } else {
                                None
                            }
                        })
                        .map(|sym| BStr::new(sym).to_string()),
                );
            }
        result
            .certificates
            .extend(macho.certificates.iter().map(|cert| cert.into()));

        result
            .linker_options
            .extend(macho.linker_options.iter().map(|lo| lo.to_vec()));

        result
            .set_number_of_segments(result.segments.len().try_into().unwrap());

        result
    }
}

impl From<&FatArch> for protos::macho::FatArch {
    fn from(arch: &FatArch) -> Self {
        let mut result = protos::macho::FatArch::new();
        result.set_cputype(arch.cputype);
        result.set_cpusubtype(arch.cpusubtype);
        result.set_offset(arch.offset);
        result.set_size(arch.size);
        result.set_align(arch.align);
        result.set_reserved(arch.reserved);
        result
    }
}

impl From<&Segment<'_>> for protos::macho::Segment {
    fn from(seg: &Segment<'_>) -> Self {
        let mut result = protos::macho::Segment::new();
        result.set_segname(seg.segname.into());
        result.set_vmaddr(seg.vmaddr);
        result.set_vmsize(seg.vmsize);
        result.set_fileoff(seg.fileoff);
        result.set_filesize(seg.filesize);
        result.set_maxprot(seg.maxprot);
        result.set_initprot(seg.initprot);
        result.set_nsects(seg.nsects);
        result.set_flags(seg.flags);
        result.sections.extend(seg.sections.iter().map(|sec| sec.into()));
        result
    }
}

impl From<&Section<'_>> for protos::macho::Section {
    fn from(sec: &Section<'_>) -> Self {
        let mut result = protos::macho::Section::new();
        result.set_segname(sec.segname.into());
        result.set_sectname(sec.sectname.into());
        result.set_addr(sec.addr);
        result.set_size(sec.size);
        result.set_offset(sec.offset);
        result.set_align(sec.align);
        result.set_reloff(sec.reloff);
        result.set_nreloc(sec.nreloc);
        result.set_flags(sec.flags);
        result.set_reserved1(sec.reserved1);
        result.set_reserved2(sec.reserved2);
        result.reserved3 = sec.reserved3;
        result
    }
}

impl From<&Dylib<'_>> for protos::macho::Dylib {
    fn from(dylib: &Dylib<'_>) -> Self {
        let mut result = protos::macho::Dylib::new();
        result.set_name(dylib.name.into());
        result.set_timestamp(dylib.timestamp);
        result.set_compatibility_version(convert_to_version_string(
            dylib.compatibility_version,
        ));
        result.set_current_version(convert_to_version_string(
            dylib.current_version,
        ));
        result
    }
}

impl From<&Symtab<'_>> for protos::macho::Symtab {
    fn from(symtab: &Symtab<'_>) -> Self {
        let mut result = protos::macho::Symtab::new();
        result.set_symoff(symtab.symoff);
        result.set_nsyms(symtab.nsyms);
        result.set_stroff(symtab.stroff);
        result.set_strsize(symtab.strsize);
        // populate the entries
        result
            .entries
            .extend(symtab.entries.iter().map(|entry| entry.to_vec()));
        result.nlists.extend(symtab.nlists.iter().map(|n| n.into()));
        result
    }
}

impl From<&Nlist> for protos::macho::Nlist {
    fn from(nlist: &Nlist) -> Self {
        let mut result = protos::macho::Nlist::new();
        result.set_n_strx(nlist.n_strx);
        result.set_n_type(nlist.n_type as u32);
        result.set_n_sect(nlist.n_sect as u32);
        result.set_n_desc(nlist.n_desc as u32);
        result.set_n_value(nlist.n_value);
        result
    }
}

impl From<&Dysymtab> for protos::macho::Dysymtab {
    fn from(dysymtab: &Dysymtab) -> Self {
        let mut result = protos::macho::Dysymtab::new();
        result.set_ilocalsym(dysymtab.ilocalsym);
        result.set_nlocalsym(dysymtab.nlocalsym);
        result.set_iextdefsym(dysymtab.iextdefsym);
        result.set_nextdefsym(dysymtab.nextdefsym);
        result.set_tocoff(dysymtab.tocoff);
        result.set_ntoc(dysymtab.ntoc);
        result.set_modtaboff(dysymtab.modtaboff);
        result.set_nmodtab(dysymtab.nmodtab);
        result.set_extrefsymoff(dysymtab.extrefsymoff);
        result.set_nextrefsyms(dysymtab.nextrefsyms);
        result.set_indirectsymoff(dysymtab.indirectsymoff);
        result.set_nindirectsyms(dysymtab.nindirectsyms);
        result.set_extreloff(dysymtab.extreloff);
        result.set_nextrel(dysymtab.nextrel);
        result.set_locreloff(dysymtab.locreloff);
        result.set_nlocrel(dysymtab.nlocrel);
        result
    }
}

impl From<&LinkedItData> for protos::macho::LinkedItData {
    fn from(lid: &LinkedItData) -> Self {
        let mut result = protos::macho::LinkedItData::new();
        result.set_dataoff(lid.dataoff);
        result.set_datasize(lid.datasize);
        result
    }
}

impl From<&Certificate> for protos::macho::Certificate {
    fn from(cert: &Certificate) -> Self {
        let mut result = protos::macho::Certificate::new();
        result.set_issuer(cert.issuer.clone());
        result.set_subject(cert.subject.clone());
        result.set_is_self_signed(cert.is_self_signed);
        result
    }
}

impl From<&DyldInfo> for protos::macho::DyldInfo {
    fn from(dyld_info: &DyldInfo) -> Self {
        let mut result = protos::macho::DyldInfo::new();
        result.set_rebase_off(dyld_info.rebase_off);
        result.set_rebase_size(dyld_info.rebase_size);
        result.set_bind_off(dyld_info.bind_off);
        result.set_bind_size(dyld_info.bind_size);
        result.set_weak_bind_off(dyld_info.weak_bind_off);
        result.set_weak_bind_size(dyld_info.weak_bind_size);
        result.set_lazy_bind_off(dyld_info.lazy_bind_off);
        result.set_lazy_bind_size(dyld_info.lazy_bind_size);
        result.set_export_off(dyld_info.export_off);
        result.set_export_size(dyld_info.export_size);
        result
    }
}

impl From<&BuildVersionCommand> for protos::macho::BuildVersion {
    fn from(bv: &BuildVersionCommand) -> Self {
        let mut result = protos::macho::BuildVersion::new();
        result.set_platform(bv.platform);
        result.set_ntools(bv.tools.len() as u32);
        result.set_minos(convert_to_version_string(bv.minos));
        result.set_sdk(convert_to_version_string(bv.sdk));
        result.tools.extend(bv.tools.iter().map(|tool| tool.into()));
        result
    }
}

impl From<&BuildToolObject> for protos::macho::BuildTool {
    fn from(bt: &BuildToolObject) -> Self {
        let mut result = protos::macho::BuildTool::new();
        result.set_tool(bt.tool);
        result.set_version(convert_to_build_tool_version(bt.version));
        result
    }
}

impl From<&MinVersion> for protos::macho::MinVersion {
    fn from(mv: &MinVersion) -> Self {
        let mut result = protos::macho::MinVersion::new();

        result.set_device(
            protobuf::EnumOrUnknown::<protos::macho::DeviceType>::from_i32(
                mv.device as i32,
            )
            .unwrap(),
        );
        result.set_version(convert_to_version_string(mv.version));
        result.set_sdk(convert_to_version_string(mv.sdk));
        result
    }
}

#[test]
fn test_uleb_parsing() {
    let (_, n) = uleb128(&[0b1000_0001, 0b000_0001]).unwrap();
    assert_eq!(129, n);

    let (_, n) = uleb128(&[0b1000_0000, 0b0000_0001]).unwrap();
    assert_eq!(128, n);

    let (_, n) = uleb128(&[0b111_1111]).unwrap();
    assert_eq!(127, n);

    let (_, n) = uleb128(&[0b111_1110]).unwrap();
    assert_eq!(126, n);

    let (_, n) = uleb128(&[0b000_0000]).unwrap();
    assert_eq!(0, n);

    let (_, n) = uleb128(&[0b1010_0000, 0b0000_0001]).unwrap();
    assert_eq!(160, n);

    let (_, n) = uleb128(&[0b1001_0110, 0b0000_0101]).unwrap();
    assert_eq!(662, n);

    let (_, n) = uleb128(&[0b1110_0101, 0b1000_1110, 0b0010_0110]).unwrap();
    assert_eq!(624485, n);

    let (_, n) = uleb128(&[0x80, 0x80, 0x80, 0x00]).unwrap();
    assert_eq!(0, n);

    let (_, n) =
        uleb128(&[0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00]).unwrap();
    assert_eq!(0, n);

    let (_, n) =
        uleb128(&[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f]).unwrap();
    assert_eq!(72057594037927935, n);

    assert!(uleb128(&[
        0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,
    ])
    .is_err());
}

#[test]
fn test_sleb_parsing() {
    let sleb_128_in = vec![0b1100_0111, 0b1001_1111, 0b111_1111];
    let (_remainder, result) = sleb128(&sleb_128_in).unwrap();
    assert_eq!(-12345, result);

    let sleb_128_in = vec![0b1001_1100, 0b111_1111];
    let (_remainder, result) = sleb128(&sleb_128_in).unwrap();
    assert_eq!(-100, result);

    let sleb_128_in = vec![0b1111_1111, 0b0];
    let (_remainder, result) = sleb128(&sleb_128_in).unwrap();
    assert_eq!(127, result);

    let sleb_128_in = vec![0b111_1111];
    let (_remainder, result) = sleb128(&sleb_128_in).unwrap();
    assert_eq!(-1, result);

    let sleb_128_in = vec![0b1111_1110, 0b0];
    let (_remainder, result) = sleb128(&sleb_128_in).unwrap();
    assert_eq!(126, result);

    let sleb_128_in = vec![0b000_0000];
    let (_remainder, result) = sleb128(&sleb_128_in).unwrap();
    assert_eq!(0, result);

    assert!(sleb128(&[
        0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,
    ])
    .is_err());
}