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