1use super::descriptor_body;
7use crate::error::{Error, Result};
8use dvb_common::{Parse, Serialize};
9
10pub const TAG: u8 = 0x09;
12const HEADER_LEN: usize = 2;
13const MIN_BODY_LEN: usize = 4; #[must_use]
19pub fn ca_system_name(ca_system_id: u16) -> Option<&'static str> {
20 crate::registry_names::ca_system_name_generated(ca_system_id)
21}
22
23#[derive(Debug, Clone, PartialEq, Eq)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize))]
30#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
31pub struct CaDescriptor<'a> {
32 pub ca_system_id: u16,
37
38 pub ca_pid: u16,
41
42 pub private_data: &'a [u8],
44}
45
46impl<'a> Parse<'a> for CaDescriptor<'a> {
47 type Error = crate::error::Error;
48
49 fn parse(bytes: &'a [u8]) -> Result<Self> {
50 let body = descriptor_body(
51 bytes,
52 TAG,
53 "CaDescriptor",
54 "unexpected tag for CA_descriptor",
55 )?;
56 let (fixed, private_data) =
57 body.split_first_chunk::<MIN_BODY_LEN>()
58 .ok_or(Error::InvalidDescriptor {
59 tag: TAG,
60 reason: "CA_descriptor length too short for mandatory fields",
61 })?;
62 let ca_system_id = u16::from_be_bytes([fixed[0], fixed[1]]);
63 let ca_pid = ((u16::from(fixed[2]) & 0x1F) << 8) | u16::from(fixed[3]);
65 Ok(Self {
66 ca_system_id,
67 ca_pid,
68 private_data,
69 })
70 }
71}
72
73impl Serialize for CaDescriptor<'_> {
74 type Error = crate::error::Error;
75
76 fn serialized_len(&self) -> usize {
77 HEADER_LEN + MIN_BODY_LEN + self.private_data.len()
78 }
79
80 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
81 let len = self.serialized_len();
82 if buf.len() < len {
83 return Err(Error::OutputBufferTooSmall {
84 need: len,
85 have: buf.len(),
86 });
87 }
88 buf[0] = TAG;
89 buf[1] = (len - HEADER_LEN) as u8;
90 buf[2] = (self.ca_system_id >> 8) as u8;
91 buf[3] = (self.ca_system_id & 0xFF) as u8;
92 buf[4] = 0xE0 | ((self.ca_pid >> 8) as u8);
94 buf[5] = (self.ca_pid & 0xFF) as u8;
95 if !self.private_data.is_empty() {
96 buf[HEADER_LEN + MIN_BODY_LEN..len].copy_from_slice(self.private_data);
97 }
98 Ok(len)
99 }
100}
101impl<'a> crate::traits::DescriptorDef<'a> for CaDescriptor<'a> {
102 const TAG: u8 = TAG;
103 const NAME: &'static str = "CA";
104}
105
106#[cfg(test)]
107mod tests {
108 use super::*;
109
110 #[test]
111 fn parse_viaccess_ecm_pid() {
112 let bytes = [TAG, 4, 0x05, 0x00, 0xE1, 0x01];
114 let d = CaDescriptor::parse(&bytes).unwrap();
115 assert_eq!(d.ca_system_id, 0x0500);
116 assert_eq!(d.ca_pid, 0x0101);
117 assert!(d.private_data.is_empty());
118 }
119
120 #[test]
121 fn parse_with_private_data() {
122 let bytes = [TAG, 6, 0x05, 0x00, 0xE1, 0x01, 0xAA, 0xBB];
124 let d = CaDescriptor::parse(&bytes).unwrap();
125 assert_eq!(d.ca_system_id, 0x0500);
126 assert_eq!(d.ca_pid, 0x0101);
127 assert_eq!(d.private_data, &[0xAA, 0xBB]);
128 }
129
130 #[test]
131 fn parse_rejects_wrong_tag() {
132 let err = CaDescriptor::parse(&[0x0A, 4, 0x05, 0x00, 0xE1, 0x01]).unwrap_err();
133 assert!(matches!(err, Error::InvalidDescriptor { tag: 0x0A, .. }));
134 }
135
136 #[test]
137 fn parse_rejects_short_header() {
138 let err = CaDescriptor::parse(&[TAG]).unwrap_err();
139 assert!(matches!(err, Error::BufferTooShort { .. }));
140 }
141
142 #[test]
143 fn parse_rejects_length_too_short() {
144 let bytes = [TAG, 3, 0x05, 0x00, 0xE1];
145 let err = CaDescriptor::parse(&bytes).unwrap_err();
146 assert!(matches!(err, Error::InvalidDescriptor { tag: TAG, .. }));
147 }
148
149 #[test]
150 fn parse_rejects_length_overflow() {
151 let bytes = [TAG, 10, 0x05, 0x00, 0xE1, 0x01];
152 let err = CaDescriptor::parse(&bytes).unwrap_err();
153 assert!(matches!(err, Error::BufferTooShort { .. }));
154 }
155
156 #[test]
157 fn serialize_round_trip() {
158 let d = CaDescriptor {
159 ca_system_id: 0x1800,
160 ca_pid: 0x0200,
161 private_data: &[0xDE, 0xAD],
162 };
163 let mut buf = vec![0u8; d.serialized_len()];
164 d.serialize_into(&mut buf).unwrap();
165 let reparsed = CaDescriptor::parse(&buf).unwrap();
166 assert_eq!(d, reparsed);
167 }
168
169 #[test]
170 fn serialize_round_trip_no_private_data() {
171 let d = CaDescriptor {
172 ca_system_id: 0x0500,
173 ca_pid: 0x0101,
174 private_data: &[],
175 };
176 let mut buf = vec![0u8; d.serialized_len()];
177 d.serialize_into(&mut buf).unwrap();
178 let reparsed = CaDescriptor::parse(&buf).unwrap();
179 assert_eq!(d, reparsed);
180 }
181
182 #[test]
183 fn serialize_rejects_small_buffer() {
184 let d = CaDescriptor {
185 ca_system_id: 0x0500,
186 ca_pid: 0x0101,
187 private_data: &[],
188 };
189 let mut tiny = vec![0u8; 3];
190 let err = d.serialize_into(&mut tiny).unwrap_err();
191 assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
192 }
193
194 #[test]
195 fn descriptor_length_matches_payload() {
196 let d = CaDescriptor {
197 ca_system_id: 0x0500,
198 ca_pid: 0x0101,
199 private_data: &[0xAA],
200 };
201 assert_eq!(d.serialized_len() - 2, 5);
202 }
203
204 #[test]
205 fn ca_system_name_exact_entry() {
206 assert_eq!(
208 ca_system_name(0x0001),
209 Some("IPDC SPP Open Security Framework Generic Roaming")
210 );
211 assert_eq!(ca_system_name(0x004E), None);
212 }
213
214 #[test]
215 fn ca_system_name_range_entry() {
216 assert_eq!(ca_system_name(0x0100), Some("MediaGuard"));
218 assert_eq!(ca_system_name(0x01FF), Some("MediaGuard"));
219 }
220
221 #[test]
222 fn ca_system_name_unknown() {
223 assert_eq!(ca_system_name(0x0000), None);
224 assert_eq!(ca_system_name(0x0003), None);
225 assert_eq!(ca_system_name(0xDEAD), None);
226 }
227}