sequoia_openpgp/packet/
compressed_data.rs

1use std::fmt;
2
3#[cfg(test)]
4use quickcheck::{Arbitrary, Gen};
5
6use crate::packet;
7use crate::Packet;
8use crate::types::CompressionAlgorithm;
9
10/// Holds a compressed data packet.
11///
12/// A compressed data packet is a container.  See [Section 5.6 of RFC
13/// 9580] for details.
14///
15/// When the parser encounters a compressed data packet with an
16/// unknown compress algorithm, it returns an `Unknown` packet instead
17/// of a `CompressedData` packet.
18///
19/// [Section 5.6 of RFC 9580]: https://www.rfc-editor.org/rfc/rfc9580.html#section-5.6
20// IMPORTANT: If you add fields to this struct, you need to explicitly
21// IMPORTANT: implement PartialEq, Eq, and Hash.
22#[derive(Clone, PartialEq, Eq, Hash)]
23pub struct CompressedData {
24    /// CTB packet header fields.
25    pub(crate) common: packet::Common,
26    /// Algorithm used to compress the payload.
27    algo: CompressionAlgorithm,
28
29    /// This is a container packet.
30    container: packet::Container,
31}
32assert_send_and_sync!(CompressedData);
33
34impl fmt::Debug for CompressedData {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        f.debug_struct("CompressedData")
37            .field("algo", &self.algo)
38            .field("container", &self.container)
39            .finish()
40    }
41}
42
43impl CompressedData {
44    /// Returns a new `CompressedData` packet.
45    pub fn new(algo: CompressionAlgorithm) -> Self {
46        CompressedData {
47            common: Default::default(),
48            algo,
49            container: Default::default(),
50        }
51    }
52
53    /// Gets the compression algorithm.
54    pub fn algo(&self) -> CompressionAlgorithm {
55        self.algo
56    }
57
58    /// Sets the compression algorithm.
59    pub fn set_algo(&mut self, algo: CompressionAlgorithm) -> CompressionAlgorithm {
60        ::std::mem::replace(&mut self.algo, algo)
61    }
62
63    /// Adds a new packet to the container.
64    #[cfg(test)]
65    pub fn push(mut self, packet: Packet) -> Self {
66        self.container.children_mut().unwrap().push(packet);
67        self
68    }
69
70    /// Inserts a new packet to the container at a particular index.
71    /// If `i` is 0, the new packet is insert at the front of the
72    /// container.  If `i` is one, it is inserted after the first
73    /// packet, etc.
74    #[cfg(test)]
75    pub fn insert(mut self, i: usize, packet: Packet) -> Self {
76        self.container.children_mut().unwrap().insert(i, packet);
77        self
78    }
79}
80
81impl_processed_body_forwards!(CompressedData);
82
83impl From<CompressedData> for Packet {
84    fn from(s: CompressedData) -> Self {
85        Packet::CompressedData(s)
86    }
87}
88
89#[cfg(test)]
90impl Arbitrary for CompressedData {
91    fn arbitrary(g: &mut Gen) -> Self {
92        use crate::serialize::SerializeInto;
93        use crate::arbitrary_helper::gen_arbitrary_from_range;
94
95        loop {
96            let a =
97                CompressionAlgorithm::from(gen_arbitrary_from_range(0..4, g));
98            if a.is_supported() {
99                let mut c = CompressedData::new(a);
100                // We arbitrarily chose to create packets with
101                // processed bodies, so that
102                // Packet::from_bytes(c.to_vec()) will roundtrip them.
103                c.set_body(packet::Body::Processed(
104                    Packet::arbitrary(g).to_vec().unwrap()
105                ));
106                return c;
107            }
108        }
109    }
110}