dvb_si/descriptors/
ca_identifier.rs1pub use super::ca::ca_system_name;
8use super::descriptor_body;
9use crate::error::{Error, Result};
10use dvb_common::{Parse, Serialize};
11
12pub const TAG: u8 = 0x53;
14const HEADER_LEN: usize = 2;
15const ENTRY_LEN: usize = 2;
16const MAX_BODY_LEN: usize = u8::MAX as usize;
18
19#[derive(Debug, Clone, PartialEq, Eq)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize))]
22pub struct CaIdentifierDescriptor {
23 pub ca_system_ids: Vec<u16>,
25}
26
27impl<'a> Parse<'a> for CaIdentifierDescriptor {
28 type Error = crate::error::Error;
29 fn parse(bytes: &'a [u8]) -> Result<Self> {
30 let body = descriptor_body(
31 bytes,
32 TAG,
33 "CaIdentifierDescriptor",
34 "unexpected tag for CA_identifier_descriptor",
35 )?;
36 if body.len() % ENTRY_LEN != 0 {
37 return Err(Error::InvalidDescriptor {
38 tag: TAG,
39 reason: "descriptor_length must be a multiple of 2",
40 });
41 }
42 let mut ca_system_ids = Vec::with_capacity(body.len() / ENTRY_LEN);
43 for chunk in body.chunks_exact(ENTRY_LEN) {
44 ca_system_ids.push(u16::from_be_bytes([chunk[0], chunk[1]]));
45 }
46 Ok(Self { ca_system_ids })
47 }
48}
49
50impl Serialize for CaIdentifierDescriptor {
51 type Error = crate::error::Error;
52 fn serialized_len(&self) -> usize {
53 HEADER_LEN + ENTRY_LEN * self.ca_system_ids.len()
54 }
55
56 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
57 let len = self.serialized_len();
58 if buf.len() < len {
59 return Err(Error::OutputBufferTooSmall {
60 need: len,
61 have: buf.len(),
62 });
63 }
64 let body_len = ENTRY_LEN * self.ca_system_ids.len();
65 if body_len > MAX_BODY_LEN {
67 return Err(Error::InvalidDescriptor {
68 tag: TAG,
69 reason: "CA_identifier_descriptor body exceeds 255 bytes",
70 });
71 }
72 buf[0] = TAG;
73 buf[1] = body_len as u8;
74 let mut pos = HEADER_LEN;
75 for caid in &self.ca_system_ids {
76 buf[pos..pos + ENTRY_LEN].copy_from_slice(&caid.to_be_bytes());
77 pos += ENTRY_LEN;
78 }
79 Ok(len)
80 }
81}
82impl<'a> crate::traits::DescriptorDef<'a> for CaIdentifierDescriptor {
83 const TAG: u8 = TAG;
84 const NAME: &'static str = "CA_IDENTIFIER";
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90
91 #[test]
92 fn parse_single_caid() {
93 let bytes = [TAG, 2, 0x06, 0x50];
94 let d = CaIdentifierDescriptor::parse(&bytes).unwrap();
95 assert_eq!(d.ca_system_ids, vec![0x0650]);
96 }
97
98 #[test]
99 fn parse_multiple_caids_preserves_order() {
100 let bytes = [TAG, 6, 0x05, 0x00, 0x06, 0x50, 0x0B, 0x00];
101 let d = CaIdentifierDescriptor::parse(&bytes).unwrap();
102 assert_eq!(d.ca_system_ids, vec![0x0500, 0x0650, 0x0B00]);
103 }
104
105 #[test]
106 fn parse_rejects_wrong_tag() {
107 assert!(matches!(
108 CaIdentifierDescriptor::parse(&[0x54, 2, 0x06, 0x50]).unwrap_err(),
109 Error::InvalidDescriptor { tag: 0x54, .. }
110 ));
111 }
112
113 #[test]
114 fn parse_rejects_short_buffer() {
115 let bytes = [TAG, 4, 0x06, 0x50];
117 assert!(matches!(
118 CaIdentifierDescriptor::parse(&bytes).unwrap_err(),
119 Error::BufferTooShort { .. }
120 ));
121 }
122
123 #[test]
124 fn parse_rejects_odd_length() {
125 let bytes = [TAG, 3, 0x06, 0x50, 0x00];
126 assert!(matches!(
127 CaIdentifierDescriptor::parse(&bytes).unwrap_err(),
128 Error::InvalidDescriptor { tag: TAG, .. }
129 ));
130 }
131
132 #[test]
133 fn empty_descriptor_valid() {
134 let d = CaIdentifierDescriptor::parse(&[TAG, 0]).unwrap();
135 assert!(d.ca_system_ids.is_empty());
136 }
137
138 #[test]
139 fn serialize_round_trip() {
140 let d = CaIdentifierDescriptor {
141 ca_system_ids: vec![0x0100, 0x1800, 0x2600],
142 };
143 let mut buf = vec![0u8; d.serialized_len()];
144 d.serialize_into(&mut buf).unwrap();
145 assert_eq!(CaIdentifierDescriptor::parse(&buf).unwrap(), d);
146 }
147
148 #[test]
149 fn serialize_rejects_over_range_body() {
150 let d = CaIdentifierDescriptor {
152 ca_system_ids: vec![0x0500; 128],
153 };
154 let mut buf = vec![0u8; d.serialized_len()];
155 assert!(matches!(
156 d.serialize_into(&mut buf).unwrap_err(),
157 Error::InvalidDescriptor { tag: TAG, .. }
158 ));
159 }
160
161 #[cfg(feature = "serde")]
162 #[test]
163 fn serde_round_trip() {
164 let d = CaIdentifierDescriptor {
165 ca_system_ids: vec![0x0500, 0x0650],
166 };
167 let json = serde_json::to_string(&d).unwrap();
168 let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
170 }
171}