rpki 0.19.3

A library for validating and creating RPKI data.
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
//! Parsing and processing of RRDP responses.
//!
//! This module provides the scaffolding for client-side processing of the
//! RPKI Repository Delta Protocol (RRDP) as defined in [RFC 8182].
//!
//! Processing is done in two parts. The RRDP notification file is parsed into
//! a value of type [`NotificationFile`]. Processing of snapshot and delta
//! files is done incrementally via the [`ProcessSnapshot`] and
//! [`ProcessDelta`] traits since these files can become rather big.
//!
//! The module does not provide an HTTP client. Rather, it relies on the
//! `std::io::Read` trait for processing. As such, it is also not compatible
//! with async processing.
//!
//! A note on terminology: to avoid confusion, the term ‘file’ refers to the
//! RRDP data itself, i.e., the notification, snapshot, and delta files. The
//! repository’s content synchronized using RRDP also consists of a set of
//! files, which we will refer to as ‘objects.’
//!
//! [RFC 8182]: https://tools.ietf.org/html/rfc8182

#![cfg(feature = "rrdp")]

use std::{error, fmt, hash, io, str};
use std::io::Read;
use std::ops::Deref;
use bytes::Bytes;
use log::info;
use ring::digest;
use uuid::Uuid;
use crate::{uri, xml};
use crate::util::base64;
use crate::xml::decode::{Content, Error as XmlError, Reader, Name};

#[cfg(feature = "serde")] use std::str::FromStr;
#[cfg(feature = "serde")] use serde::{
    Deserialize, Deserializer, Serialize, Serializer
};


//------------ Maximum XML sizes ---------------------------------------------

const MAX_FILE_SIZE: u64 = 100_000_000;
const MAX_HEADER_SIZE: u64 = 1_000_000;

//------------ NotificationFile ----------------------------------------------

/// The RRDP Update Notification File.
///
/// This type represents the decoded content of the RRDP Update Notification
/// File. It can be read from a reader via the [`parse`][Self::parse]
/// function. All elements are accessible as attributes.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NotificationFile {
    /// The identifier of the current session of the server.
    session_id: Uuid,

    /// The serial number of the most recent update.
    serial: u64,

    /// Information about the most recent snapshot.
    snapshot: SnapshotInfo,

    /// The list of available delta updates.
    ///
    /// If parsing the list fails for a “soft” reason, this is set to the
    /// error variant.
    deltas: Result<Vec<DeltaInfo>, DeltaListError>,
}

/// # Data Access
///
impl NotificationFile {
    /// Creates a new notification file from the given components.
    pub fn new(
        session_id: Uuid,
        serial: u64,
        snapshot: UriAndHash,
        deltas: Vec<DeltaInfo>,
    ) -> Self {
        NotificationFile {
            session_id,
            serial,
            snapshot,
            deltas: Ok(deltas),
        }
    }

    /// Returns the identifier of the current session of the server.
    ///
    /// Delta updates can only be used if the session ID of the last processed
    /// update matches this value.
    pub fn session_id(&self) -> Uuid {
        self.session_id
    }

    /// Returns the serial number of the most recent update.
    ///
    /// Serial numbers increase by one between each update.
    pub fn serial(&self) -> u64 {
        self.serial
    }

    /// Returns information about the most recent snapshot.
    ///
    /// The snapshot contains a complete set of all data published via the
    /// repository. It can be processed using the [`ProcessSnapshot`] trait.
    pub fn snapshot(&self) -> &SnapshotInfo {
        &self.snapshot
    }

    /// Returns the list of available delta updates.
    ///
    /// Deltas can be processed using the [`ProcessDelta`] trait.
    ///
    /// If `delta_status` returns an error, this list will be empty.
    pub fn deltas(&self) -> &[DeltaInfo] {
        match self.deltas {
            Ok(ref deltas) => deltas.as_slice(),
            Err(_) => &[]
        }
    }

    /// Returns the status of the delta list.
    pub fn delta_status(&self) -> Result<(), DeltaListError> {
        match self.deltas {
            Ok(_) => Ok(()),
            Err(err) => Err(err)
        }
    }

    /// Sorts the deltas by increasing serial numbers.
    ///
    /// In other words, the delta with the smallest serial number will
    /// appear at the beginning of the sequence.
    pub fn sort_deltas(&mut self) {
        if let Ok(ref mut deltas) = self.deltas {
            deltas.sort_by_key(|delta| delta.serial());
        }
    }

    /// Sorts the deltas by decreasing serial numbers.
    ///
    /// In other words, the delta with the largest serial number will
    /// appear at the beginning of the sequence.
    pub fn reverse_sort_deltas(&mut self) {
        if let Ok(ref mut deltas) = self.deltas {
            deltas.sort_by_key(|b| std::cmp::Reverse(b.serial));
        }
    }

    /// Sorts, verifies, and optionally limits the list of deltas.
    ///
    /// Sorts the deltas by increasing serial number. If `limit` is given,
    /// it then retains at most that many of the newest deltas.
    ///
    /// Returns whether there are no gaps in the retained deltas.
    pub fn sort_and_verify_deltas(&mut self, limit: Option<usize>) -> bool {
        if let Ok(ref mut deltas) = self.deltas {
            if !deltas.is_empty() {
                deltas.sort_by_key(|delta| delta.serial());

                if let Some(limit) = limit {
                    if limit < deltas.len() {
                        let offset = deltas.len() - limit;
                        deltas.drain(..offset);
                    }
                }

                let mut last_seen = deltas[0].serial();
                for delta in &deltas[1..] {
                    if last_seen + 1 != delta.serial() {
                        return false;
                    } else {
                        last_seen = delta.serial()
                    }
                }
            }
        }

        true
    }

    /// Returns whether all URIs have the same origin as the given URI.
    pub fn has_matching_origins(&self, base: &uri::Https) -> bool {
        if !base.eq_authority(self.snapshot().uri()) {
            return false
        }
        if let Ok(ref deltas) = self.deltas {
            if deltas.iter().any(|delta| !base.eq_authority(delta.uri())) {
                return false
            }
        }
        true
    }
}

/// # XML support
///
impl NotificationFile {
    /// Parses the notification file from its XML representation.
    pub fn parse<R: io::BufRead>(
        reader: R,
    ) -> Result<Self, XmlError> {
        Self::_parse(reader, None)
    }

    /// Parses the notification file with a limit on the delta_list.
    ///
    /// If there are more delta entries than the given limit, the list of the
    /// returned value will be empty and [`Self::delta_status`] will return
    /// an error.
    pub fn parse_limited<R: io::BufRead>(
        reader: R,
        delta_limit: usize,
    ) -> Result<Self, XmlError> {
        Self::_parse(reader, Some(delta_limit))
    }

    pub fn _parse<R: io::BufRead>(
        reader: R,
        delta_limit: Option<usize>,
    ) -> Result<Self, XmlError> {
        let mut reader = Reader::new(reader);
        let mut session_id = None;
        let mut serial = None;
        let mut outer = reader.start_with_limit(
            |element| {
                if element.name() != NOTIFICATION {
                    return Err(XmlError::Malformed)
                }

                element.attributes(|name, value| {
                    match name {
                        b"version" => {
                            if value.ascii_into::<u8>()? != 1 {
                                return Err(XmlError::Malformed)
                            }
                            Ok(())
                        }
                        b"session_id" => {
                            session_id = Some(value.ascii_into()?);
                            Ok(())
                        }
                        b"serial" => {
                            serial = Some(value.ascii_into()?);
                            Ok(())
                        }
                        _ => Err(XmlError::Malformed)
                    }
                })
            },
            MAX_HEADER_SIZE
        )?;

        let mut snapshot = None;
        let mut deltas = Ok(vec![]);

        while let Some(mut content) = outer.take_opt_element_with_limit(
            &mut reader,
            |element| {
                match element.name() {
                    SNAPSHOT => {
                        if snapshot.is_some() {
                            return Err(XmlError::Malformed)
                        }
                        let mut uri = None;
                        let mut hash = None;
                        element.attributes(|name, value| match name {
                            b"uri" => {
                                uri = Some(value.ascii_into()?);
                                Ok(())
                            }
                            b"hash" => {
                                hash = Some(value.ascii_into()?);
                                Ok(())
                            }
                            _ => Err(XmlError::Malformed)
                        })?;
                        match (uri, hash) {
                            (Some(uri), Some(hash)) => {
                                snapshot = Some(UriAndHash::new(uri, hash));
                                Ok(())
                            }
                            _ => Err(XmlError::Malformed)
                        }
                    }
                    DELTA => {
                        let mut serial = None;
                        let mut uri = None;
                        let mut hash = None;
                        element.attributes(|name, value| match name {
                            b"serial" => {
                                serial = Some(value.ascii_into()?);
                                Ok(())
                            }
                            b"uri" => {
                                uri = Some(value.ascii_into()?);
                                Ok(())
                            }
                            b"hash" => {
                                hash = Some(value.ascii_into()?);
                                Ok(())
                            }
                            _ => Err(XmlError::Malformed)
                        })?;
                        let (serial, uri, hash) = match (serial, uri, hash) {
                            (Some(serial), Some(uri), Some(hash)) =>  {
                                (serial, uri, hash)
                            }
                            _ => return Err(XmlError::Malformed)
                        };
                        if let Some(limit) = delta_limit {
                            let len = deltas.as_ref().map(|deltas| {
                                deltas.len()
                            }).unwrap_or(0);
                            if len >= limit {
                                deltas = Err(DeltaListError::Oversized);
                            }
                        }
                        if let Ok(ref mut deltas) = deltas {
                            deltas.push(DeltaInfo::new(serial, uri, hash))
                        }
                        Ok(())
                    }
                    _ => Err(XmlError::Malformed)
                }
            },
            MAX_HEADER_SIZE
        )? {
            content.take_end(&mut reader)?;
        }

        outer.take_end(&mut reader)?;
        reader.end()?;

        match (session_id, serial, snapshot) {
            (Some(session_id), Some(serial), Some(snapshot)) => {
                Ok(NotificationFile { session_id, serial, snapshot, deltas })
            }
            _ => Err(XmlError::Malformed)
        }
    }

    /// Writes the notification file as RFC 8182 XML.
    pub fn write_xml(
        &self, writer: &mut impl io::Write
    ) -> Result<(), io::Error> {
        let mut writer = xml::encode::Writer::new(writer);
        writer.element(NOTIFICATION.into_unqualified())?
            .attr("xmlns", NS)?
            .attr("version", "1")?
            .attr("session_id", &self.session_id)?
            .attr("serial", &self.serial)?
            .content(|content| {
                // add snapshot
                content.element(SNAPSHOT.into_unqualified())?
                    .attr("uri", self.snapshot.uri())?
                    .attr("hash", &self.snapshot.hash())?
                ;

                // add deltas
                for delta in self.deltas() {
                    content.element(DELTA.into_unqualified())?
                        .attr("serial", &delta.serial())?
                        .attr("uri", delta.uri())?
                        .attr("hash", &delta.hash())?
                    ;
                }

                Ok(())
            })?;
        writer.done()
    }
}


//------------ PublishElement ------------------------------------------------

/// Am RPKI object to be published for the first time.
///
/// This type defines an RRDP publish element as found in RRDP Snapshots and
/// Deltas. See [`UpdateElement`] for the related element that replaces a
/// previous element for the same uri.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PublishElement {
    /// The URI of the object to be published.
    uri: uri::Rsync,

    /// The content of the object to be published.
    ///
    /// This is the raw content. It is _not_ Base64 encoded.
    data: Bytes,
}

impl PublishElement {
    /// Creates a new publish element from the object URI and content.
    ///
    /// The content provided via `data` is the raw content and must not yet
    /// be Base64 encoded.
    pub fn new(
        uri: uri::Rsync,
        data: Bytes,
    ) -> Self {
        PublishElement { uri, data }
    }
    
    /// Returns the published object’s URI.
    pub fn uri(&self) -> &uri::Rsync {
        &self.uri
    }

    /// Returns the published object’s content.
    pub fn data(&self) -> &Bytes {
        &self.data
    }

    /// Converts `self` into the object’s URI and content.
    pub fn unpack(self) -> (uri::Rsync, Bytes) {
        (self.uri, self.data)
    }

    /// Writes the publish element’s XML.
    fn write_xml(
        &self,
        content: &mut xml::encode::Content<impl io::Write>
    ) -> Result<(), io::Error> {
        content.element(PUBLISH.into_unqualified())?
            .attr("uri", &self.uri)?
            .content(|content| {
                content.base64(self.data.as_ref())
            })?;
        Ok(())
    }
}


//------------ UpdateElement -------------------------------------------------

/// An RPKI object to be updated with new content.
///
/// This type defines an RRDP update element as found in RRDP deltas. It is
/// like a [`PublishElement`] except that it replaces an existing object for
/// a URI.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UpdateElement {
    /// The URI of the object to be updated.
    uri: uri::Rsync,

    /// The SHA-256 hash of the previous content of the object.
    hash: Hash,

    /// The new content of the object.
    ///
    /// This is the raw content. It is _not_ Base64 encoded.
    data: Bytes,
}

impl UpdateElement {
    /// Creates a new update element from its components.
    pub fn new(uri: uri::Rsync, hash: Hash, data: Bytes) -> Self {
        UpdateElement { uri, hash, data }
    }

    /// Returns the URI of the object to update.
    pub fn uri(&self) -> &uri::Rsync {
        &self.uri
    }

    /// Returns the hash of the previous content.
    pub fn hash(&self) -> &Hash {
        &self.hash
    }

    /// Returns the new content of the object.
    pub fn data(&self) -> &Bytes {
        &self.data
    }

    /// Unpacks the update element into its components.
    pub fn unpack(self) -> (uri::Rsync, Hash, Bytes) {
        (self.uri, self.hash, self.data)
    }
}

impl UpdateElement {
    /// Writes the update element’s XML.
    fn write_xml(
        &self,
        content: &mut xml::encode::Content<impl io::Write>
    ) -> Result<(), io::Error> {
        content.element(PUBLISH.into_unqualified())?
            .attr("uri", &self.uri)?
            .attr("hash", &self.hash)?
            .content(|content| {
                content.base64(self.data.as_ref())
            })?;
        Ok(())
    }
}


//------------ WithdrawElement -----------------------------------------------

/// An RPKI object is to be delete.
///
/// This type defines an RRDP update element as found in RRDP deltas.  It is
/// like a [`PublishElement`] except that it removes an existing object.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct WithdrawElement {
    /// The URI of the object to be deleted.
    uri: uri::Rsync,

    /// The SHA-256 hash of the content of the object to be deleted.
    hash: Hash,
}

impl WithdrawElement {
    /// Creates a new withdraw element from a URI and content hash.
    pub fn new(uri: uri::Rsync, hash: Hash) -> Self {
        WithdrawElement { uri, hash }
    }

    /// Returns the URI of the object to be deleted.
    pub fn uri(&self) -> &uri::Rsync {
        &self.uri
    }
    
    /// Returns the hash over the content of the object to be deleted.
    pub fn hash(&self) -> &Hash {
        &self.hash
    }

    /// Converts the withdraw element into its URI and hash.
    pub fn unpack(self) -> (uri::Rsync, Hash) {
        (self.uri, self.hash)
    }
}

impl WithdrawElement {
    /// Writes the withdraw element’s XML.
    fn write_xml(
        &self,
        content: &mut xml::encode::Content<impl io::Write>
    ) -> Result<(), io::Error> {
        content.element(WITHDRAW.into_unqualified())?
            .attr("uri", &self.uri)?
            .attr("hash", &self.hash)?;
        Ok(())
    }
}


//------------ DeltaElement --------------------------------------------------

/// A single element of a RRDP delta.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DeltaElement {
    /// The element publishes a new object.
    Publish(PublishElement),

    /// The element updates an existing object.
    Update(UpdateElement),

    /// The element deletes an existing object.
    Withdraw(WithdrawElement)
}

impl DeltaElement {
    /// Writes the element’s XML.
    fn write_xml(
        &self,
        content: &mut xml::encode::Content<impl io::Write>
    ) -> Result<(), io::Error> {
        match self {
            DeltaElement::Publish(p) => p.write_xml(content),
            DeltaElement::Update(u) => u.write_xml(content),
            DeltaElement::Withdraw(w) => w.write_xml(content)
        }
    }
}

//--- From

impl From<PublishElement> for DeltaElement {
    fn from(src: PublishElement) -> Self {
        DeltaElement::Publish(src)
    }
}

impl From<UpdateElement> for DeltaElement {
    fn from(src: UpdateElement) -> Self {
        DeltaElement::Update(src)
    }
}

impl From<WithdrawElement> for DeltaElement {
    fn from(src: WithdrawElement) -> Self {
        DeltaElement::Withdraw(src)
    }
}


//------------ Snapshot ------------------------------------------------------

/// An RRDP snapshot.
///
/// This type represents an owned RRDP snapshot containing the RRDP session
/// ID, serial number and all published elements.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Snapshot {
    /// The RRDP session of this snapshot.
    session_id: Uuid,

    /// The serial number of the update of this snapshot.
    serial: u64,

    /// The objects published through this snapshot.
    elements: Vec<PublishElement>,
}

impl Snapshot {
    /// Creates a new snapshot from its components.
    pub fn new(
        session_id: Uuid,
        serial: u64,
        elements: Vec<PublishElement>,
    ) -> Self {
        Snapshot { session_id, serial, elements }
    }

    /// Returns the session ID of this snapshot.
    pub fn session_id(&self) -> Uuid {
        self.session_id
    }

    /// Returns the serial number of the update represented by this snapshot.
    pub fn serial(&self) -> u64 {
        self.serial
    }

    /// Returns the list of objects published by the snapshot.
    pub fn elements(&self) -> &[PublishElement] {
        &self.elements
    }

    /// Converts the snapshots into its elements.
    pub fn into_elements(self) -> Vec<PublishElement> {
        self.elements
    }
}

/// # XML Support
///
impl Snapshot {
    /// Parses the snapshot from its XML representation.
    pub fn parse<R: io::BufRead>(
        reader: R
    ) -> Result<Self, ProcessError> {
        let mut builder = SnapshotBuilder {
            session_id: None,
            serial: None,
            elements: vec![]
        };

        builder.process(reader)?;
        builder.try_into()
    }

    /// Writes the snapshot’s XML representation.
    pub fn write_xml(
        &self, writer: &mut impl io::Write
    ) -> Result<(), io::Error> {
        let mut writer = xml::encode::Writer::new(writer);
        writer.element(SNAPSHOT.into_unqualified())?
            .attr("xmlns", NS)?
            .attr("version", "1")?
            .attr("session_id", &self.session_id)?
            .attr("serial", &self.serial)?
            .content(|content| {
                for el in &self.elements {
                    el.write_xml(content)?;
                }
                Ok(())
            })?;
        writer.done()
    }
}


//------------ SnapshotBuilder -----------------------------------------------

struct SnapshotBuilder {
    session_id: Option<Uuid>,
    serial: Option<u64>,
    elements: Vec<PublishElement>,
}

impl ProcessSnapshot for SnapshotBuilder {
    type Err = ProcessError;

    fn meta(&mut self, session_id: Uuid, serial: u64) -> Result<(), Self::Err> {
        self.session_id = Some(session_id);
        self.serial = Some(serial);
        Ok(())
    }

    fn publish(&mut self, uri: uri::Rsync, data: &mut ObjectReader) -> Result<(), Self::Err> {
        let mut buf = Vec::new();
        data.read_to_end(&mut buf)?;
        let data = Bytes::from(buf);
        let element = PublishElement { uri, data };
        self.elements.push(element);
        Ok(())
    }
}

impl TryFrom<SnapshotBuilder> for Snapshot {
    type Error = ProcessError;

    fn try_from(builder: SnapshotBuilder) -> Result<Self, Self::Error> {
        let session_id = builder.session_id.ok_or(
            ProcessError::Xml(XmlError::Malformed)
        )?;

        let serial = builder.serial.ok_or(
            ProcessError::Xml(XmlError::Malformed)
        )?;

        Ok(Snapshot { session_id, serial, elements: builder.elements })
    }
}


//------------ ProcessSnapshot -----------------------------------------------

/// A type that can process an RRDP snapshot.
///
/// The trait contains two required methods: [`meta`][Self::meta] is called
/// once at the beginning of the snapshot and gives the processor a chance to
/// check if the session ID and serial number are as expected. Then,
/// [`publish`][Self::publish] is called for each published object.
/// The processor can abort at any time by returning an error.
///
/// The provided method [`process`][Self::process] drives the actual
/// processing and should thus be called when using a type that implements
/// this trait.
pub trait ProcessSnapshot {
    /// The error type returned by the processor.
    type Err: From<ProcessError>;

    /// Processes the snapshot meta data.
    ///
    /// The method is called before any other method and is passed the
    /// session ID and serial number encountered in the outermost tag of the
    /// snapshot file’s XML. If they don’t match the expected values, the
    /// processor should abort processing by returning an error.
    fn meta(
        &mut self,
        session_id: Uuid,
        serial: u64,
    ) -> Result<(), Self::Err>;

    /// Processes a published object.
    ///
    /// The object is identified by the provided rsync URI. The object’s data
    /// is provided via a reader.
    fn publish(
        &mut self,
        uri: uri::Rsync,
        data: &mut ObjectReader,
    ) -> Result<(), Self::Err>;

    /// Processes a snapshot file.
    ///
    /// The file’s content is read from `reader`. The two required methods
    /// are called as appropriate. If the reader fails, parsing fails, or the
    /// methods return an error, processing is aborted and an error is
    /// returned.
    fn process<R: io::BufRead>(
        &mut self,
        reader: R
    ) -> Result<(), Self::Err> {
        let mut reader = Reader::new(reader);
        
        let mut session_id = None;
        let mut serial = None;
        let mut outer = reader.start_with_limit(
            |element| {
                if element.name() != SNAPSHOT {
                    info!("Bad outer: not snapshot, but {:?}", element.name());
                    return Err(XmlError::Malformed)
                }
                element.attributes(|name, value| match name {
                    b"version" => {
                        if value.ascii_into::<u8>()? != 1 {
                            info!("Bad version");
                            return Err(XmlError::Malformed)
                        }
                        Ok(())
                    }
                    b"session_id" => {
                        session_id = Some(value.ascii_into()?);
                        Ok(())
                    }
                    b"serial" => {
                        serial = Some(value.ascii_into()?);
                        Ok(())
                    }
                    _ => {
                        info!("Bad attribute on snapshot.");
                        Err(XmlError::Malformed)
                    }
                })
            },
            MAX_HEADER_SIZE,
        ).map_err(Into::into)?;

        match (session_id, serial) {
            (Some(session_id), Some(serial)) => {
                self.meta(session_id, serial)?;
            }
            _ => {
                info!("Missing session or serial");
                return Err(ProcessError::malformed().into())
            }
        }

        loop {
            let mut uri = None;
            let inner = outer.take_opt_element_with_limit(
                &mut reader,
                |element| {
                    if element.name() != PUBLISH {
                    info!("Bad inner: not publish");
                        return Err(ProcessError::malformed())
                    }
                    element.attributes(|name, value| match name {
                        b"uri" => {
                            uri = Some(value.ascii_into()?);
                            Ok(())
                        }
                        _ => {
                            info!("Bad attribute on publish.");
                            Err(ProcessError::malformed())
                        }
                    })
                },
                MAX_FILE_SIZE,
            )?;
            let mut inner = match inner {
                Some(inner) => inner,
                None => break
            };
            let uri = match uri {
                Some(uri) => uri,
                None => return Err(ProcessError::malformed().into())
            };
            ObjectReader::process(&mut inner, &mut reader, |reader| {
                self.publish(uri, reader)
            })?;
        }

        outer.take_end(&mut reader).map_err(Into::into)?;
        reader.end().map_err(Into::into)?;
        Ok(())
    }
}


//------------ Delta ---------------------------------------------------------

/// An RRDP delta.
///
/// This type represents an owned RRDP snapshot containing the RRDP session
/// ID, serial number and all its elements.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Delta {
    /// The RRDP session ID of the delta.
    session_id: Uuid,

    /// The serial number of this delta.
    serial: u64,

    /// The objects changed by this delta.
    elements: Vec<DeltaElement>
}

/// # Data Access
///
impl Delta {
    /// Creates a new delta from session ID, serial number, and elements.
    pub fn new(
        session_id: Uuid,
        serial: u64,
        elements: Vec<DeltaElement>
    ) -> Self {
        Delta { session_id, serial, elements }
    }

    /// Returns the session ID of the RRDP session this delta is part of.
    pub fn session_id(&self) -> Uuid {
        self.session_id
    }

    /// Returns the serial number of this delta.
    ///
    /// The serial number is identical to that of the snapshot this delta
    /// updates _to._
    pub fn serial(&self) -> u64 {
        self.serial
    }

    /// The list of objects changed by this delta.
    pub fn elements(&self) -> &[DeltaElement] {
        &self.elements
    }

    /// Converts the delta into its elements.
    pub fn into_elements(self) -> Vec<DeltaElement> {
        self.elements
    }
}

/// # Decoding and Encoding XML
///
impl Delta {
    /// Parses the delta from its XML representation.
    pub fn parse<R: io::BufRead>(
        reader: R
    ) -> Result<Self, ProcessError> {
        let mut builder = DeltaBuilder {
            session_id: None,
            serial: None,
            elements: vec![]
        };

        builder.process(reader)?;
        builder.try_into()
    }

    /// Write the delta’s XML representation.
    pub fn write_xml(
        &self, writer: &mut impl io::Write
    ) -> Result<(), io::Error> {
        let mut writer = xml::encode::Writer::new(writer);
        writer.element(DELTA.into_unqualified())?
            .attr("xmlns", NS)?
            .attr("version", "1")?
            .attr("session_id", &self.session_id)?
            .attr("serial", &self.serial)?
            .content(|content| {
                for el in &self.elements {
                    el.write_xml(content)?;
                }
                Ok(())
            })?;
        writer.done()
    }
}


//------------ DeltaBuilder --------------------------------------------------

struct DeltaBuilder {
    session_id: Option<Uuid>,
    serial: Option<u64>,
    elements: Vec<DeltaElement>
}

impl ProcessDelta for DeltaBuilder {
    type Err = ProcessError;

    fn meta(
        &mut self,
        session_id: Uuid,
        serial: u64,
    ) -> Result<(), Self::Err> {
        self.session_id = Some(session_id);
        self.serial = Some(serial);
        Ok(())
    }

    fn publish(
        &mut self,
        uri: uri::Rsync,
        hash_opt: Option<Hash>,
        data: &mut ObjectReader,
    ) -> Result<(), Self::Err> {
        let mut buf = Vec::new();
        data.read_to_end(&mut buf)?;
        let data = Bytes::from(buf);
        match hash_opt {
            Some(hash) => {
                let update = UpdateElement { uri, hash, data};
                self.elements.push(DeltaElement::Update(update));
            },
            None => {
                let publish = PublishElement { uri, data};
                self.elements.push(DeltaElement::Publish(publish));
            }
        }
        Ok(())
    }

    fn withdraw(
        &mut self,
        uri: uri::Rsync,
        hash: Hash,
    ) -> Result<(), Self::Err> {
        let withdraw = WithdrawElement { uri, hash };
        self.elements.push(DeltaElement::Withdraw(withdraw));
        Ok(())
    }
}

impl TryFrom<DeltaBuilder> for Delta {
    type Error = ProcessError;

    fn try_from(builder: DeltaBuilder) -> Result<Self, Self::Error> {
        let session_id = builder.session_id.ok_or(
            ProcessError::Xml(XmlError::Malformed)
        )?;

        let serial = builder.serial.ok_or(
            ProcessError::Xml(XmlError::Malformed)
        )?;

        Ok(Delta { session_id, serial, elements: builder.elements })

    }
}

//------------ ProcessDelta --------------------------------------------------

/// A type that can process an RRDP delta.
///
/// The trait contains three required methods: [`meta`][Self::meta] is called
/// once at the beginning of the snapshot and gives the processor a chance to
/// check if the session ID and serial number are as expected. Then,
/// [`publish`][Self::publish] is called for each newly published or
/// updated object and [`withdraw`][Self::withdraw] is called for each
/// deleted object. The processor can abort at any time by returning an error.
///
/// The provided method [`process`][Self::process] drives the actual
/// processing and should thus be called when using a type that implements
/// this trait.
pub trait ProcessDelta {
    /// The error type returned by the processor.
    type Err: From<ProcessError>;

    /// Processes the delta meta data.
    ///
    /// The method is called before any other method and is passed the
    /// session ID and serial number encountered in the outermost tag of the
    /// delta file’s XML. If they don’t match the expected values, the
    /// processor should abort processing by returning an error.
    fn meta(
        &mut self,
        session_id: Uuid,
        serial: u64,
    ) -> Result<(), Self::Err>;

    /// Processes a published object.
    ///
    /// The object is identified by the rsync URI provided in `uri`. If the
    /// object is updated, the hash over the previous content of the object
    /// is given in `hash`. If the object is newly published, `hash` will be
    /// `None`. The (new) content of the object is provided via the reader in
    /// `data`.
    fn publish(
        &mut self,
        uri: uri::Rsync,
        hash: Option<Hash>,
        data: &mut ObjectReader,
    ) -> Result<(), Self::Err>;

    /// Processes a withdrawn object.
    ///
    /// The object is identified by the rsync URI provided in `uri`. The hash
    /// over the expected content of the object to be deleted is given in
    /// `hash`.
    fn withdraw(
        &mut self,
        uri: uri::Rsync,
        hash: Hash,
    ) -> Result<(), Self::Err>;


    /// Processes a delta file.
    ///
    /// The file’s content is taken from `reader`. The content is parsed and
    /// the three required methods are called as required.
    ///
    /// If the reader fails, parsing fails, or the methods return an error,
    /// processing is aborted and an error is returned.
    fn process<R: io::BufRead>(
        &mut self,
        reader: R
    ) -> Result<(), Self::Err> {
        let mut reader = Reader::new(reader);
        
        let mut session_id = None;
        let mut serial = None;
        let mut outer = reader.start_with_limit(
            |element| {
                if element.name() != DELTA {
                    return Err(ProcessError::malformed())
                }
                element.attributes(|name, value| match name {
                    b"version" => {
                        if value.ascii_into::<u8>()? != 1 {
                            return Err(ProcessError::malformed())
                        }
                        Ok(())
                    }
                    b"session_id" => {
                        session_id = Some(value.ascii_into()?);
                        Ok(())
                    }
                    b"serial" => {
                        serial = Some(value.ascii_into()?);
                        Ok(())
                    }
                    _ => Err(ProcessError::malformed())
                })
            },
            MAX_HEADER_SIZE,
        )?;

        match (session_id, serial) {
            (Some(session_id), Some(serial)) => {
                self.meta(session_id, serial)?;
            }
            _ => return Err(ProcessError::malformed().into())
        }

        loop {
            let mut action = None;
            let mut uri = None;
            let mut hash = None;
            let inner = outer.take_opt_element_with_limit(
                &mut reader,
                |element| {
                    match element.name() {
                        PUBLISH => action = Some(Action::Publish),
                        WITHDRAW => action = Some(Action::Withdraw),
                        _ => return Err(ProcessError::malformed()),
                    };
                    element.attributes(|name, value| match name {
                        b"uri" => {
                            uri = Some(value.ascii_into()?);
                            Ok(())
                        }
                        b"hash" => {
                            hash = Some(value.ascii_into()?);
                            Ok(())
                        }
                        _ => Err(ProcessError::malformed())
                    })
                },
                MAX_FILE_SIZE,
            )?;
            let mut inner = match inner {
                Some(inner) => inner,
                None => break
            };
            let uri = match uri {
                Some(uri) => uri,
                None => return Err(ProcessError::malformed().into())
            };
            match action.unwrap() { // Or we'd have exited already.
                Action::Publish => {
                    ObjectReader::process(
                        &mut inner, &mut reader,
                        |reader| self.publish(uri, hash, reader)
                    )?;
                }
                Action::Withdraw => {
                    let hash = match hash {
                        Some(hash) => hash,
                        None => return Err(ProcessError::malformed().into())
                    };
                    self.withdraw(uri, hash)?;
                    inner.take_end(&mut reader).map_err(Into::into)?;
                }
            }
        }
        outer.take_end(&mut reader).map_err(Into::into)?;
        reader.end().map_err(Into::into)?;
        Ok(())
    }

}


//------------ SnapshotInfo --------------------------------------------------

/// The URI and HASH of the current snapshot for a [`NotificationFile`].
pub type SnapshotInfo = UriAndHash;


//------------ DeltaInfo -----------------------------------------------------

/// The serial, URI and HASH of a delta in a [`NotificationFile`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct DeltaInfo {
    serial: u64,
    uri_and_hash: UriAndHash
}

impl DeltaInfo {
    /// Creates a new info from its components.
    pub fn new(serial: u64, uri: uri::Https, hash: Hash) -> Self {
        DeltaInfo {
            serial,
            uri_and_hash: UriAndHash::new(uri, hash)
        }
    }

    /// Returns the serial number of this delta.
    pub fn serial(&self) -> u64 {
        self.serial
    }
}

impl Deref for DeltaInfo {
    type Target = UriAndHash;

    fn deref(&self) -> &Self::Target {
        &self.uri_and_hash
    }
}

//------------ UriAndHash ----------------------------------------------------

/// The URI of an RRDP file and a SHA-256 hash over its content.
///
/// In order to detect accidental or malicious modifications of the data
/// all references to RRDP files are given with a SHA-256 hash over the
/// expected content of that file, allowing a client to verify they got the
/// right file.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct UriAndHash {
    /// The URI of the RRDP file.
    uri: uri::Https,

    /// The expected SHA-256 hash over the file’s content.
    hash: Hash,
}

impl UriAndHash {
    /// Creates a new URI-and-hash pair.
    pub fn new(uri: uri::Https, hash: Hash) -> Self {
        UriAndHash { uri, hash }
    }

    /// Returns a reference to the URI.
    pub fn uri(&self) -> &uri::Https {
        &self.uri
    }

    /// Returns the expected SHA-256 hash.
    pub fn hash(&self) -> Hash {
        self.hash
    }

    /// Converts the pair into just the URI.
    pub fn into_uri(self) -> uri::Https {
        self.uri
    }

    /// Converts `self` into a pair of URI and hash.
    pub fn into_pair(self) -> (uri::Https, Hash) {
        (self.uri, self.hash)
    }
}


//------------ Hash ----------------------------------------------------------

/// A hash over RRDP data.
///
/// This hash is used both for verifying the correctness of RRDP files as well
/// as update or deletion of the right objects.
///
/// RRDP exclusively uses SHA-256 and provides no means of choosing a different
/// algorithm. Consequently, this type is a wrapper around a 32 byte array
/// holding SHA-256 output.
#[derive(Clone, Copy, Eq, hash::Hash, PartialEq)]
#[repr(transparent)] // ensure that size_of::<Hash>() == 32.
pub struct Hash([u8; 32]);

impl Hash {
    /// Returns a reference to the octets as a slice.
    pub fn as_slice(&self) -> &[u8] {
        self.0.as_ref()
    }

    /// Returns a new Hash from the provided data
    pub fn from_data(data: &[u8]) -> Self {
        let digest = digest::digest(&digest::SHA256, data);
        Self::try_from(digest.as_ref()).unwrap()
    }

    /// Returns whether this hash matches the provided data
    pub fn matches(&self, data: &[u8]) -> bool {
        let data_hash = Self::from_data(data);
        *self == data_hash
    }
}


//--- From, TryFrom, and FromStr

impl From<[u8;32]> for Hash {
    fn from(value: [u8;32]) -> Hash {
        Hash(value)
    }
}

impl From<Hash> for [u8; 32] {
    fn from(src: Hash) -> Self {
        src.0
    }
}

impl<'a> TryFrom<&'a [u8]> for Hash {
    type Error = std::array::TryFromSliceError;

    fn try_from(src: &'a [u8]) -> Result<Self, Self::Error> {
        TryFrom::try_from(src).map(Hash)
    }
}

impl TryFrom<digest::Digest> for Hash {
    type Error = AlgorithmError;

    fn try_from(digest: digest::Digest) -> Result<Self, Self::Error> {
        // XXX This doesn’t properly check the algorithm.
        TryFrom::try_from(
            digest.as_ref()
        ).map(Hash).map_err(|_| AlgorithmError(()))
    }
}

impl str::FromStr for Hash {
    type Err = ParseHashError;

    /// Parses a string into a hash.
    ///
    /// The string must consist of exactly 64 hexadecimal digits and nothing
    /// else.
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.len() != 64 {
            return Err(ParseHashError::BAD_LENGTH)
        }
        let mut res = [0u8; 32];
        let mut s = s.chars();
        for octet in &mut res {
            let first = s.next().ok_or(
                ParseHashError::BAD_LENGTH
            )?.to_digit(16).ok_or(
                ParseHashError::BAD_CHARS
            )?;
            let second = s.next().ok_or(
                ParseHashError::BAD_LENGTH
            )?.to_digit(16).ok_or(
                ParseHashError::BAD_CHARS
            )?;
            *octet = ((first << 4) | second) as u8;
        }
        Ok(Hash(res))
    }
}


//--- AsRef

impl AsRef<[u8]> for Hash {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}


//--- PartialEq
//
// PartialEq<Self> and Eq are derived.

impl PartialEq<digest::Digest> for Hash {
    fn eq(&self, other: &digest::Digest) -> bool {
        // XXX This doesn’t properly check the algorithm.
        self.0.as_ref() == other.as_ref()
    }
}


//--- Display and Debug

impl fmt::Display for Hash {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for &ch in self.as_slice() {
            write!(f, "{ch:02x}")?;
        }
        Ok(())
    }
}

impl fmt::Debug for Hash {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Hash({self})")
    }
}

//--- Serialize and Deserialize

#[cfg(feature = "serde")]
impl Serialize for Hash {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where S: Serializer {
        self.to_string().serialize(serializer)
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for Hash {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where D: Deserializer<'de> {
        let hex_str = String::deserialize(deserializer)?;
        Hash::from_str(&hex_str).map_err(serde::de::Error::custom)
    }
}


//------------ Action --------------------------------------------------------

/// The choice of actions in a delta file.
enum Action {
    /// An object is to be inserted or updated.
    ///
    /// The object is to be updated, if a hash is given. Otherwise it is
    /// to be inserted.
    Publish,

    /// An object is to be deleted.
    Withdraw,
}


//------------ ObjectReader --------------------------------------------------

/// A reader providing the content of an object.
///
/// The content is included in base64 encoding in the RRDP’s XML. This reader
/// provides access to the decoded data via the standard `Read` trait.
pub struct ObjectReader<'a>(
    /// The base64 encoded data.
    base64::XmlDecoderReader<'a>
);

impl ObjectReader<'_> {
    /// Processes an element with optional XML PCDATA as object content.
    ///
    /// An object reader is created and passed to the closure `op` for
    /// actual processing.
    ///
    /// This method expects the next XML event to either be text or the end
    /// of an element. It will process both.
    fn process<R, T, E, F> (
        content: &mut Content,
        reader: &mut Reader<R>,
        op: F
    ) -> Result<T, E>
    where
        R: io::BufRead,
        E: From<ProcessError>,
        F: FnOnce(&mut ObjectReader) -> Result<T, E>
    {
        // We need this extra error type to fulfil the trait bounds of the
        // error type of the closure passed to take_opt_final_text. Or, as
        // the German saying goes: „Von hinten durch die Brust ins Auge.“
        enum Error<E> {
            Xml(XmlError),
            User(E),
        }

        impl<E> From<XmlError> for Error<E> {
            fn from(err: XmlError) -> Self {
                Error::Xml(err)
            }
        }

        content.take_opt_final_text(reader, |text| {
            let b64 = match text.as_ref() {
                Some(text) => text.to_ascii()?,
                None => Default::default(),
            };
            op(
                &mut ObjectReader(base64::Xml.decode_reader(b64.as_ref()))
            ).map_err(Error::User)
        }).map_err(|err| match err {
            Error::Xml(err) => ProcessError::Xml(err).into(),
            Error::User(err) => err
        })
    }
}

impl io::Read for ObjectReader<'_> {
    fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
        self.0.read(buf)
    }
}


//------------ XML Names -----------------------------------------------------

const NS: &[u8] = b"http://www.ripe.net/rpki/rrdp";
const NOTIFICATION: Name = Name::qualified(NS, b"notification");
const SNAPSHOT: Name = Name::qualified(NS, b"snapshot");
const DELTA: Name = Name::qualified(NS, b"delta");
const PUBLISH: Name = Name::qualified(NS, b"publish");
const WITHDRAW: Name = Name::qualified(NS, b"withdraw");


//============ Errors ========================================================

//------------ AlgorithmError ------------------------------------------------

/// A digest was of the wrong algorithm.
#[derive(Clone, Copy, Debug)]
pub struct AlgorithmError(());

impl fmt::Display for AlgorithmError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("algorithm mismatch")
    }
}

impl error::Error for AlgorithmError { }


//------------ ParseHashError ------------------------------------------------

/// An error happened while parsing a hash.
#[derive(Clone, Copy, Debug)]
pub struct ParseHashError(&'static str);

impl ParseHashError {
    /// The error when the hash value was of the wrong length.
    const BAD_LENGTH: Self = ParseHashError("invalid length");

    /// The error when the hash value contained illegal characters.
    const BAD_CHARS: Self = ParseHashError("invalid characters");
}

impl fmt::Display for ParseHashError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.0)
    }
}

impl error::Error for ParseHashError { }


//------------ DeltaListError ------------------------------------------------

/// An error happened when parsing the delta list of a notification file.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DeltaListError {
    /// The delta list was larger than a given limit.
    Oversized,
}

impl fmt::Display for DeltaListError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Oversized => write!(f, "excessively large delta list")
        }
    }
}

impl error::Error for DeltaListError { }


//------------ ProcessError --------------------------------------------------

/// An error occurred while processing RRDP data.
#[derive(Debug)]
pub enum ProcessError {
    /// An IO error happened.
    Io(io::Error),

    /// The XML was not correctly formed.
    Xml(XmlError),
}

impl ProcessError {
    /// Creates an error when the XML was malformed.
    fn malformed() -> Self {
        ProcessError::Xml(XmlError::Malformed)
    }
}

impl From<io::Error> for ProcessError {
    fn from(err: io::Error) -> Self {
        ProcessError::Io(err)
    }
}

impl From<XmlError> for ProcessError {
    fn from(err: XmlError) -> Self {
        ProcessError::Xml(err)
    }
}

impl fmt::Display for ProcessError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ProcessError::Io(ref inner) => inner.fmt(f),
            ProcessError::Xml(ref inner) => inner.fmt(f),
        }
    }
}

impl error::Error for ProcessError { 
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            ProcessError::Io(error) => Some(error),
            ProcessError::Xml(error) => Some(error),
        }
    }
}


//============ Tests =========================================================

#[cfg(test)]
mod test {
    use std::str::from_utf8_unchecked;
    use std::str::FromStr;

    use super::*;

    pub struct Test;

    impl ProcessSnapshot for Test {
        type Err = ProcessError;

        fn meta(
            &mut self,
            _session_id: Uuid,
            _serial: u64,
        ) -> Result<(), Self::Err> {
            Ok(())
        }

        fn publish(
            &mut self,
            _uri: uri::Rsync,
            _data: &mut ObjectReader,
        ) -> Result<(), Self::Err> {
            Ok(())
        }
    }

    impl ProcessDelta for Test {
        type Err = ProcessError;

        fn meta(
            &mut self,
            _session_id: Uuid,
            _serial: u64,
        ) -> Result<(), Self::Err> {
            Ok(())
        }

        fn publish(
            &mut self,
            _uri: uri::Rsync,
            _hash: Option<Hash>,
            _data: &mut ObjectReader,
        ) -> Result<(), Self::Err> {
            Ok(())
        }

        fn withdraw(
            &mut self,
            _uri: uri::Rsync,
            _hash: Hash,
        ) -> Result<(), Self::Err> {
            Ok(())
        }
    }

    #[test]
    fn ripe_notification() {
        NotificationFile::parse(
            include_bytes!(
                "../test-data/rrdp/ripe-notification.xml"
            ).as_ref()
        ).unwrap();
    }

    #[test]
    fn lolz_notification() {
        assert!(
            NotificationFile::parse(
                include_bytes!(
                    "../test-data/rrdp/lolz-notification.xml"
                ).as_ref()
            ).is_err()
        );
    }

    #[test]
    fn gaps_notification() {
        let mut notification_without_gaps =  NotificationFile::parse(
            include_bytes!("../test-data/rrdp/ripe-notification.xml").as_ref()
        ).unwrap();
        assert!(notification_without_gaps.sort_and_verify_deltas(None));

        let mut notification_with_gaps =  NotificationFile::parse(
            include_bytes!(
                "../test-data/rrdp/ripe-notification-with-gaps.xml"
            ).as_ref()
        ).unwrap();
        assert!(!notification_with_gaps.sort_and_verify_deltas(None));
    }

    #[test]
    fn limit_notification_deltas() {
        let mut notification_without_gaps =  NotificationFile::parse(
            include_bytes!("../test-data/rrdp/ripe-notification.xml").as_ref()
        ).unwrap();
        assert!(notification_without_gaps.sort_and_verify_deltas(Some(2)));

        assert_eq!(2, notification_without_gaps.deltas().len());
        assert_eq!(
            notification_without_gaps.deltas().first().unwrap().serial(),
            notification_without_gaps.serial() - 1
        );
        assert_eq!(
            notification_without_gaps.deltas().last().unwrap().serial(),
            notification_without_gaps.serial()
        );
    }

    #[test]
    fn unsorted_notification() {
        let mut from_sorted = NotificationFile::parse(
            include_bytes!("../test-data/rrdp/ripe-notification.xml").as_ref()
        ).unwrap();

        let mut from_unsorted = NotificationFile::parse(
            include_bytes!(
                "../test-data/rrdp/ripe-notification-unsorted.xml"
            ).as_ref()
        ).unwrap();
        
        assert_ne!(from_sorted, from_unsorted);

        from_unsorted.reverse_sort_deltas();
        assert_eq!(from_sorted, from_unsorted);
        
        from_unsorted.sort_deltas();
        assert_ne!(from_sorted, from_unsorted);
                
        from_sorted.sort_deltas();
        assert_eq!(from_sorted, from_unsorted);
    }

    #[test]
    fn notification_parse_limited() {
        let bytes = include_bytes!(
            "../test-data/rrdp/ripe-notification.xml"
        ).as_ref();
        let full = NotificationFile::parse(bytes).unwrap();
        assert!(!full.deltas().is_empty());
        
        let limited = NotificationFile::parse_limited(
            bytes, full.deltas().len()
        ).unwrap();
        assert_eq!(limited.deltas().len(), full.deltas().len());
        assert!(limited.delta_status().is_ok());

        let limited = NotificationFile::parse_limited(
            bytes, full.deltas().len() - 1
        ).unwrap();
        assert_eq!(limited.deltas().len(), 0);
        assert!(limited.delta_status().is_err());
    }

    #[test]
    fn ripe_snapshot() {
        <Test as ProcessSnapshot>::process(
            &mut Test,
            include_bytes!("../test-data/rrdp/ripe-snapshot.xml").as_ref()
        ).unwrap();
    }

    #[test]
    fn ripe_delta() {
        <Test as ProcessDelta>::process(
            &mut Test,
            include_bytes!("../test-data/rrdp/ripe-delta.xml").as_ref()
        ).unwrap();
    }

    #[test]
    fn hash_to_hash() {
        use std::str::FromStr;

        let string = "this is a test";
        let sha256 =
            "2e99758548972a8e8822ad47fa1017ff72f06f3ff6a016851f45c398732bc50c";
        let hash = Hash::from_str(sha256).unwrap();
        let hash_from_data = Hash::from_data(string.as_bytes());
        assert_eq!(hash, hash_from_data);
        assert!(hash.matches(string.as_bytes()));
    }

    #[test]
    fn notification_from_to_xml() {
        let notification = NotificationFile::parse(
            include_bytes!("../test-data/rrdp/ripe-notification.xml").as_ref()
        ).unwrap();

        let mut vec = vec![];
        notification.write_xml(&mut vec).unwrap();

        let xml = unsafe {
            from_utf8_unchecked(vec.as_ref())
        };

        let notification_parsed = NotificationFile::parse(xml.as_bytes()).unwrap();

        assert_eq!(notification, notification_parsed);
    }

    #[test]
    fn snapshot_from_to_xml() {
        let data = include_bytes!("../test-data/rrdp/ripe-snapshot.xml");
        let snapshot = Snapshot::parse(data.as_ref()).unwrap();

        let mut vec = vec![];
        snapshot.write_xml(&mut vec).unwrap();

        let xml = unsafe {
            from_utf8_unchecked(vec.as_ref())
        };

        let snapshot_parsed = Snapshot::parse(xml.as_bytes()).unwrap();

        assert_eq!(snapshot, snapshot_parsed);
    }

    #[test]
    fn delta_from_to_xml() {
        let data = include_bytes!("../test-data/rrdp/ripe-delta.xml");
        let delta = Delta::parse(data.as_ref()).unwrap();

        let mut vec = vec![];
        delta.write_xml(&mut vec).unwrap();

        let xml = unsafe {
            from_utf8_unchecked(vec.as_ref())
        };

        let delta_parsed = Delta::parse(xml.as_bytes()).unwrap();

        assert_eq!(delta, delta_parsed);
    }

    #[test]
    fn snapshot_content() {
        const CONTENT: &[u8] = b"foo bar\n";
        let snapshot = br#"
            <snapshot version="1"
                session_id="a2d845c4-5b91-4015-a2b7-988c03ce232a"
                serial="1742"
                xmlns="http://www.ripe.net/rpki/rrdp"
            >
                <publish
                    uri="rsync://example.com/some/path"
                >
                    Zm9vIGJhcgo=
                </publish>
                <publish
                    uri="rsync://example.com/some/other"
                />
                <publish
                    uri="rsync://example.com/some/third"
                ></publish>
            </snapshot>
        "#;

        let snapshot = Snapshot::parse(&mut snapshot.as_ref()).unwrap();
        assert_eq!(snapshot.elements.len(), 3);
        assert_eq!(
            snapshot.elements[0],
            PublishElement::new(
                uri::Rsync::from_str("rsync://example.com/some/path").unwrap(),
                Bytes::copy_from_slice(CONTENT)
            )
        );
        assert_eq!(
            snapshot.elements[1],
            PublishElement::new(
                uri::Rsync::from_str("rsync://example.com/some/other").unwrap(),
                Bytes::new()
            )
        );
        assert_eq!(
            snapshot.elements[2],
            PublishElement::new(
                uri::Rsync::from_str("rsync://example.com/some/third").unwrap(),
                Bytes::new()
            )
        );
    }

    #[test]
    fn has_matching_origins() {
        fn check<const N: usize>(
            snapshot: &str, deltas: [&str; N]
        ) -> bool {
            let hash = Hash::from_data(b"12");
            NotificationFile::new(
                Uuid::nil(), 0,
                SnapshotInfo::new(
                    uri::Https::from_str(snapshot).unwrap(), hash
                ),
                deltas.iter().map(|uri| {
                    DeltaInfo::new(
                        0, uri::Https::from_str(uri).unwrap(), hash
                    )
                }).collect()
            ).has_matching_origins(
                &uri::Https::from_str("https://foo.bar/n/o").unwrap()
            )
        }

        assert!(check(
            "https://foo.bar/1/2/3",
            ["https://foo.bar/", "https://foo.bar/4", "https://foo.bar/7/8"],
        ));

        assert!(!check(
            "https://foo.bar/1/2/3",
            ["https://foo.bar/", "https://foo.local/4"],
        ));
    }

    /// Regression test for an panic in quick-xml 0.39.0.
    #[test]
    fn doctype_panic_regression() {
        let reader = std::io::BufReader::with_capacity(
            8, "<!d[<>23456789".as_bytes()
        );
        let _  = NotificationFile::parse(reader);
    }
}