Skip to main content

dvb_si/descriptors/
copyright.rs

1//! Copyright Descriptor — ISO/IEC 13818-1 §2.6.24 (tag 0x0D).
2//!
3//! Carries a 32-bit copyright identifier and optional additional
4//! copyright information bytes.
5
6use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10/// Descriptor tag for copyright_descriptor.
11pub const TAG: u8 = 0x0D;
12const HEADER_LEN: usize = 2;
13const COPYRIGHT_ID_LEN: usize = 4;
14
15/// Copyright Descriptor.
16#[derive(Debug, Clone, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(serde::Serialize))]
18#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
19pub struct CopyrightDescriptor<'a> {
20    /// 32-bit copyright identifier.
21    pub copyright_identifier: u32,
22    /// Additional copyright info bytes.
23    #[cfg_attr(feature = "serde", serde(borrow))]
24    pub additional_copyright_info: &'a [u8],
25}
26
27impl<'a> Parse<'a> for CopyrightDescriptor<'a> {
28    type Error = crate::error::Error;
29
30    fn parse(bytes: &'a [u8]) -> Result<Self> {
31        let body = descriptor_body(
32            bytes,
33            TAG,
34            "CopyrightDescriptor",
35            "unexpected tag for copyright_descriptor",
36        )?;
37        if body.len() < COPYRIGHT_ID_LEN {
38            return Err(Error::InvalidDescriptor {
39                tag: TAG,
40                reason: "copyright_descriptor length too short for copyright_identifier",
41            });
42        }
43        let copyright_identifier = u32::from_be_bytes([body[0], body[1], body[2], body[3]]);
44        let additional_copyright_info = &body[COPYRIGHT_ID_LEN..];
45        Ok(Self {
46            copyright_identifier,
47            additional_copyright_info,
48        })
49    }
50}
51
52impl Serialize for CopyrightDescriptor<'_> {
53    type Error = crate::error::Error;
54
55    fn serialized_len(&self) -> usize {
56        HEADER_LEN + COPYRIGHT_ID_LEN + self.additional_copyright_info.len()
57    }
58
59    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
60        let len = self.serialized_len();
61        if buf.len() < len {
62            return Err(Error::OutputBufferTooSmall {
63                need: len,
64                have: buf.len(),
65            });
66        }
67        buf[0] = TAG;
68        buf[1] = (len - HEADER_LEN) as u8;
69        buf[HEADER_LEN..HEADER_LEN + COPYRIGHT_ID_LEN]
70            .copy_from_slice(&self.copyright_identifier.to_be_bytes());
71        buf[HEADER_LEN + COPYRIGHT_ID_LEN..len].copy_from_slice(self.additional_copyright_info);
72        Ok(len)
73    }
74}
75impl<'a> crate::traits::DescriptorDef<'a> for CopyrightDescriptor<'a> {
76    const TAG: u8 = TAG;
77    const NAME: &'static str = "COPYRIGHT";
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn parse_id_only() {
86        let bytes = [TAG, 4, 0x12, 0x34, 0x56, 0x78];
87        let d = CopyrightDescriptor::parse(&bytes).unwrap();
88        assert_eq!(d.copyright_identifier, 0x12345678);
89        assert!(d.additional_copyright_info.is_empty());
90    }
91
92    #[test]
93    fn parse_with_additional_info() {
94        let bytes = [TAG, 6, 0xDE, 0xAD, 0xBE, 0xEF, 0xAA, 0xBB];
95        let d = CopyrightDescriptor::parse(&bytes).unwrap();
96        assert_eq!(d.copyright_identifier, 0xDEADBEEF);
97        assert_eq!(d.additional_copyright_info, &[0xAA, 0xBB]);
98    }
99
100    #[test]
101    fn serialize_round_trip() {
102        let d = CopyrightDescriptor {
103            copyright_identifier: 0xCAFEBABE,
104            additional_copyright_info: &[0x01, 0x02, 0x03],
105        };
106        let mut buf = vec![0u8; d.serialized_len()];
107        d.serialize_into(&mut buf).unwrap();
108        let reparsed = CopyrightDescriptor::parse(&buf).unwrap();
109        assert_eq!(d, reparsed);
110    }
111
112    #[test]
113    fn parse_rejects_wrong_tag() {
114        let err = CopyrightDescriptor::parse(&[0x0E, 4, 0, 0, 0, 0]).unwrap_err();
115        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x0E, .. }));
116    }
117
118    #[test]
119    fn parse_rejects_too_short_for_id() {
120        let err = CopyrightDescriptor::parse(&[TAG, 3, 0, 0, 0]).unwrap_err();
121        assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
122    }
123
124    #[test]
125    fn serialize_rejects_small_buffer() {
126        let d = CopyrightDescriptor {
127            copyright_identifier: 0,
128            additional_copyright_info: &[],
129        };
130        let mut tiny = vec![0u8; 3];
131        let err = d.serialize_into(&mut tiny).unwrap_err();
132        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
133    }
134}