1use crate::error::{Error, Result};
13use crate::traits::Table;
14use dvb_common::{Parse, Serialize};
15
16pub const TABLE_ID_FIRST: u8 = 0x3A;
18pub const TABLE_ID_LAST: u8 = 0x3F;
20pub const PID: u16 = 0x0000;
22
23const MIN_HEADER_LEN: usize = 3;
24const EXTENSION_HEADER_LEN: usize = 5;
25const CRC_LEN: usize = 4;
26
27#[derive(Debug, Clone, PartialEq, Eq)]
30#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
31pub struct DsmccSection<'a> {
32 pub table_id: u8,
34 pub extension_id: u16,
36 pub version_number: u8,
38 pub current_next_indicator: bool,
40 pub section_number: u8,
42 pub last_section_number: u8,
44 #[cfg_attr(feature = "serde", serde(borrow))]
46 pub payload: &'a [u8],
47}
48
49impl<'a> Parse<'a> for DsmccSection<'a> {
50 type Error = crate::error::Error;
51 fn parse(bytes: &'a [u8]) -> Result<Self> {
52 let min_len = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + CRC_LEN;
53 if bytes.len() < min_len {
54 return Err(Error::BufferTooShort {
55 need: min_len,
56 have: bytes.len(),
57 what: "DsmccSection",
58 });
59 }
60
61 let table_id = bytes[0];
62 if !(TABLE_ID_FIRST..=TABLE_ID_LAST).contains(&table_id) {
63 return Err(Error::UnexpectedTableId {
64 table_id,
65 what: "DsmccSection",
66 expected: &[TABLE_ID_FIRST, TABLE_ID_LAST],
67 });
68 }
69
70 let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
71 let total = MIN_HEADER_LEN + section_length as usize;
72 if bytes.len() < total {
73 return Err(Error::SectionLengthOverflow {
74 declared: section_length as usize,
75 available: bytes.len() - MIN_HEADER_LEN,
76 });
77 }
78
79 let extension_id = u16::from_be_bytes([bytes[3], bytes[4]]);
80 let version_number = (bytes[5] >> 1) & 0x1F;
81 let current_next_indicator = (bytes[5] & 0x01) != 0;
82 let section_number = bytes[6];
83 let last_section_number = bytes[7];
84
85 let payload_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN;
86 let payload_end = total - CRC_LEN;
87 let payload = &bytes[payload_start..payload_end];
88
89 Ok(DsmccSection {
90 table_id,
91 extension_id,
92 version_number,
93 current_next_indicator,
94 section_number,
95 last_section_number,
96 payload,
97 })
98 }
99}
100
101impl Serialize for DsmccSection<'_> {
102 type Error = crate::error::Error;
103 fn serialized_len(&self) -> usize {
104 MIN_HEADER_LEN + EXTENSION_HEADER_LEN + self.payload.len() + CRC_LEN
105 }
106
107 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
108 let len = self.serialized_len();
109 if buf.len() < len {
110 return Err(Error::OutputBufferTooSmall {
111 need: len,
112 have: buf.len(),
113 });
114 }
115
116 let section_length: u16 = (len - MIN_HEADER_LEN) as u16;
117 buf[0] = self.table_id;
118 buf[1] = 0xB0 | ((section_length >> 8) as u8 & 0x0F);
119 buf[2] = (section_length & 0xFF) as u8;
120 buf[3..5].copy_from_slice(&self.extension_id.to_be_bytes());
121 buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
122 buf[6] = self.section_number;
123 buf[7] = self.last_section_number;
124
125 let payload_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN;
126 let payload_end = payload_start + self.payload.len();
127 buf[payload_start..payload_end].copy_from_slice(self.payload);
128
129 let crc_pos = len - CRC_LEN;
130 let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
131 buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
132 Ok(len)
133 }
134}
135
136impl<'a> Table<'a> for DsmccSection<'a> {
137 const TABLE_ID: u8 = TABLE_ID_FIRST;
138 const PID: u16 = PID;
139}
140
141impl<'a> crate::traits::TableDef<'a> for DsmccSection<'a> {
142 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID_FIRST, TABLE_ID_LAST)];
147 const NAME: &'static str = "DSM_CC_SECTION";
148}
149
150#[cfg(test)]
151mod tests {
152 use super::*;
153
154 fn build_dsmcc(table_id: u8, extension_id: u16, version: u8, payload: &[u8]) -> Vec<u8> {
155 let section_length: u16 = (EXTENSION_HEADER_LEN + payload.len() + CRC_LEN) as u16;
156 let mut v = Vec::new();
157 v.push(table_id);
158 v.push(0xB0 | ((section_length >> 8) as u8 & 0x0F));
159 v.push((section_length & 0xFF) as u8);
160 v.extend_from_slice(&extension_id.to_be_bytes());
161 v.push(0xC0 | ((version & 0x1F) << 1) | 0x01);
162 v.push(0);
163 v.push(0);
164 v.extend_from_slice(payload);
165 v.extend_from_slice(&[0, 0, 0, 0]);
166 v
167 }
168
169 #[test]
170 fn parse_rejects_wrong_table_id() {
171 let mut bytes = build_dsmcc(0x3B, 0x0001, 0, &[]);
172 bytes[0] = 0x00;
173 let err = DsmccSection::parse(&bytes).unwrap_err();
174 assert!(matches!(
175 err,
176 Error::UnexpectedTableId { table_id: 0x00, .. }
177 ));
178 }
179
180 #[test]
181 fn parse_rejects_table_id_below_range() {
182 let bytes = build_dsmcc(0x39, 0x0001, 0, &[]);
183 let err = DsmccSection::parse(&bytes).unwrap_err();
184 assert!(matches!(
185 err,
186 Error::UnexpectedTableId { table_id: 0x39, .. }
187 ));
188 }
189
190 #[test]
191 fn parse_rejects_table_id_above_range() {
192 let bytes = build_dsmcc(0x40, 0x0001, 0, &[]);
193 let err = DsmccSection::parse(&bytes).unwrap_err();
194 assert!(matches!(
195 err,
196 Error::UnexpectedTableId { table_id: 0x40, .. }
197 ));
198 }
199
200 #[test]
201 fn parse_rejects_short_buffer() {
202 let err = DsmccSection::parse(&[0x3B, 0x00]).unwrap_err();
203 assert!(matches!(err, Error::BufferTooShort { .. }));
204 }
205
206 #[test]
207 fn parse_empty_payload() {
208 let bytes = build_dsmcc(0x3B, 0x1234, 5, &[]);
209 let sec = DsmccSection::parse(&bytes).expect("parse");
210 assert_eq!(sec.table_id, 0x3B);
211 assert_eq!(sec.extension_id, 0x1234);
212 assert_eq!(sec.version_number, 5);
213 assert!(sec.current_next_indicator);
214 assert_eq!(sec.section_number, 0);
215 assert_eq!(sec.last_section_number, 0);
216 assert_eq!(sec.payload.len(), 0);
217 }
218
219 #[test]
220 fn parse_0x3c_table_id_accepted() {
221 let bytes = build_dsmcc(0x3C, 0x0001, 0, &[0xAA, 0xBB]);
222 let sec = DsmccSection::parse(&bytes).unwrap();
223 assert_eq!(sec.table_id, 0x3C);
224 assert_eq!(sec.payload, &[0xAA, 0xBB]);
225 }
226
227 #[test]
228 fn parse_payload_preserved() {
229 let payload = vec![0x01, 0x02, 0x03, 0x04, 0x05];
230 let bytes = build_dsmcc(0x3B, 0x0001, 0, &payload);
231 let sec = DsmccSection::parse(&bytes).unwrap();
232 assert_eq!(sec.payload, &payload[..]);
233 }
234
235 #[test]
236 fn serialize_round_trip_empty() {
237 let sec = DsmccSection {
238 table_id: 0x3B,
239 extension_id: 0x0001,
240 version_number: 0,
241 current_next_indicator: true,
242 section_number: 0,
243 last_section_number: 0,
244 payload: &[],
245 };
246 let mut buf = vec![0u8; sec.serialized_len()];
247 sec.serialize_into(&mut buf).unwrap();
248 let reparsed = DsmccSection::parse(&buf).unwrap();
249 assert_eq!(sec, reparsed);
250 }
251
252 #[test]
253 fn serialize_round_trip_with_payload() {
254 let payload: [u8; 5] = [0xDE, 0xAD, 0xBE, 0xEF, 0x00];
255 let sec = DsmccSection {
256 table_id: 0x3C,
257 extension_id: 0xABCD,
258 version_number: 3,
259 current_next_indicator: true,
260 section_number: 1,
261 last_section_number: 2,
262 payload: &payload,
263 };
264 let mut buf = vec![0u8; sec.serialized_len()];
265 sec.serialize_into(&mut buf).unwrap();
266 let reparsed = DsmccSection::parse(&buf).unwrap();
267 assert_eq!(sec, reparsed);
268 }
269}