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