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