Skip to main content

dvb_si/descriptors/
private_data_indicator.rs

1//! Private Data Indicator Descriptor — ISO/IEC 13818-1 §2.6.22 (tag 0x0F).
2//!
3//! Carries a 4-byte private data specifier that identifies the organization
4//! or entity that defined the private data carried in the associated stream.
5
6use crate::error::{Error, Result};
7use crate::traits::Descriptor;
8use dvb_common::{Parse, Serialize};
9
10/// Descriptor tag for private_data_indicator_descriptor.
11pub const TAG: u8 = 0x0F;
12const HEADER_LEN: usize = 2;
13const BODY_LEN: u8 = 4;
14
15/// Private Data Indicator Descriptor.
16///
17/// The `private_data_specifier` is a 4-byte value assigned by a standards body
18/// or industry consortium to identify the organization that defined the private
19/// data format used in the associated elementary stream.
20#[derive(Debug, Clone, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct PrivateDataIndicatorDescriptor {
23    /// 4-byte private data specifier identifier.
24    pub private_data_specifier: [u8; 4],
25}
26
27impl<'a> Parse<'a> for PrivateDataIndicatorDescriptor {
28    type Error = crate::error::Error;
29
30    fn parse(bytes: &'a [u8]) -> Result<Self> {
31        if bytes.len() < HEADER_LEN {
32            return Err(Error::BufferTooShort {
33                need: HEADER_LEN,
34                have: bytes.len(),
35                what: "PrivateDataIndicatorDescriptor header",
36            });
37        }
38        if bytes[0] != TAG {
39            return Err(Error::InvalidDescriptor {
40                tag: bytes[0],
41                reason: "unexpected tag for private_data_indicator_descriptor",
42            });
43        }
44        let length = bytes[1] as usize;
45        let total = HEADER_LEN + length;
46        if bytes.len() < total {
47            return Err(Error::BufferTooShort {
48                need: total,
49                have: bytes.len(),
50                what: "PrivateDataIndicatorDescriptor body",
51            });
52        }
53        if length != BODY_LEN as usize {
54            return Err(Error::InvalidDescriptor {
55                tag: TAG,
56                reason: "private_data_indicator_descriptor length must equal 4",
57            });
58        }
59        let mut private_data_specifier = [0u8; 4];
60        private_data_specifier.copy_from_slice(&bytes[HEADER_LEN..HEADER_LEN + 4]);
61        Ok(Self {
62            private_data_specifier,
63        })
64    }
65}
66
67impl Serialize for PrivateDataIndicatorDescriptor {
68    type Error = crate::error::Error;
69
70    fn serialized_len(&self) -> usize {
71        HEADER_LEN + BODY_LEN as usize
72    }
73
74    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
75        let len = self.serialized_len();
76        if buf.len() < len {
77            return Err(Error::OutputBufferTooSmall {
78                need: len,
79                have: buf.len(),
80            });
81        }
82        buf[0] = TAG;
83        buf[1] = BODY_LEN;
84        buf[HEADER_LEN..HEADER_LEN + 4].copy_from_slice(&self.private_data_specifier);
85        Ok(len)
86    }
87}
88
89impl<'a> Descriptor<'a> for PrivateDataIndicatorDescriptor {
90    const TAG: u8 = TAG;
91
92    fn descriptor_length(&self) -> u8 {
93        BODY_LEN
94    }
95}
96
97impl<'a> crate::traits::DescriptorDef<'a> for PrivateDataIndicatorDescriptor {
98    const TAG: u8 = TAG;
99    const NAME: &'static str = "PRIVATE_DATA_INDICATOR";
100}
101
102#[cfg(test)]
103mod tests {
104    use super::*;
105
106    #[test]
107    fn parse_private_data_specifier() {
108        let bytes = [TAG, 4, 0x00, 0x00, 0x00, 0x01];
109        let d = PrivateDataIndicatorDescriptor::parse(&bytes).unwrap();
110        assert_eq!(d.private_data_specifier, [0x00, 0x00, 0x00, 0x01]);
111    }
112
113    #[test]
114    fn parse_rejects_wrong_tag() {
115        let err = PrivateDataIndicatorDescriptor::parse(&[0x10, 4, 0, 0, 0, 0]).unwrap_err();
116        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x10, .. }));
117    }
118
119    #[test]
120    fn parse_rejects_wrong_length() {
121        // Length says 3 but valid is 4 — buffer is large enough to read header+3
122        let bytes = [TAG, 3, 0, 0, 0, 0];
123        let err = PrivateDataIndicatorDescriptor::parse(&bytes).unwrap_err();
124        assert!(matches!(err, Error::InvalidDescriptor { .. }));
125    }
126
127    #[test]
128    fn parse_rejects_short_buffer() {
129        let err = PrivateDataIndicatorDescriptor::parse(&[TAG]).unwrap_err();
130        assert!(matches!(err, Error::BufferTooShort { .. }));
131    }
132
133    #[test]
134    fn serialize_round_trip() {
135        let d = PrivateDataIndicatorDescriptor {
136            private_data_specifier: [0xAA, 0xBB, 0xCC, 0xDD],
137        };
138        let mut buf = vec![0u8; d.serialized_len()];
139        d.serialize_into(&mut buf).unwrap();
140        let reparsed = PrivateDataIndicatorDescriptor::parse(&buf).unwrap();
141        assert_eq!(d, reparsed);
142    }
143
144    #[test]
145    fn descriptor_length_matches_payload() {
146        let d = PrivateDataIndicatorDescriptor {
147            private_data_specifier: [0x00, 0x00, 0x00, 0x01],
148        };
149        assert_eq!(d.descriptor_length(), 4);
150    }
151}