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
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn parse_private_data_specifier() {
103        let bytes = [TAG, 4, 0x00, 0x00, 0x00, 0x01];
104        let d = PrivateDataIndicatorDescriptor::parse(&bytes).unwrap();
105        assert_eq!(d.private_data_specifier, [0x00, 0x00, 0x00, 0x01]);
106    }
107
108    #[test]
109    fn parse_rejects_wrong_tag() {
110        let err = PrivateDataIndicatorDescriptor::parse(&[0x10, 4, 0, 0, 0, 0]).unwrap_err();
111        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x10, .. }));
112    }
113
114    #[test]
115    fn parse_rejects_wrong_length() {
116        // Length says 3 but valid is 4 — buffer is large enough to read header+3
117        let bytes = [TAG, 3, 0, 0, 0, 0];
118        let err = PrivateDataIndicatorDescriptor::parse(&bytes).unwrap_err();
119        assert!(matches!(err, Error::InvalidDescriptor { .. }));
120    }
121
122    #[test]
123    fn parse_rejects_short_buffer() {
124        let err = PrivateDataIndicatorDescriptor::parse(&[TAG]).unwrap_err();
125        assert!(matches!(err, Error::BufferTooShort { .. }));
126    }
127
128    #[test]
129    fn serialize_round_trip() {
130        let d = PrivateDataIndicatorDescriptor {
131            private_data_specifier: [0xAA, 0xBB, 0xCC, 0xDD],
132        };
133        let mut buf = vec![0u8; d.serialized_len()];
134        d.serialize_into(&mut buf).unwrap();
135        let reparsed = PrivateDataIndicatorDescriptor::parse(&buf).unwrap();
136        assert_eq!(d, reparsed);
137    }
138
139    #[test]
140    fn descriptor_length_matches_payload() {
141        let d = PrivateDataIndicatorDescriptor {
142            private_data_specifier: [0x00, 0x00, 0x00, 0x01],
143        };
144        assert_eq!(d.descriptor_length(), 4);
145    }
146}