1use crate::descriptors::DescriptorLoop;
11use crate::error::{Error, Result};
12use crate::traits::Table;
13use dvb_common::{Parse, Serialize};
14
15pub const TABLE_ID: u8 = 0x79;
17pub const PID: u16 = 0x0016;
19
20const HEADER_LEN: usize = 3;
22const EXTENSION_HEADER_LEN: usize = 6;
25const COMMON_DESC_LEN_FIELD: usize = 2;
27const CRC_LEN: usize = 4;
29const MIN_LEN: usize = HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_FIELD + CRC_LEN;
31
32#[derive(Debug, Clone, PartialEq, Eq)]
40#[cfg_attr(feature = "serde", derive(serde::Serialize))]
41#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
42pub struct Rnt<'a> {
43 pub context_id: u16,
45 pub version_number: u8,
47 pub current_next_indicator: bool,
49 pub section_number: u8,
51 pub last_section_number: u8,
53 pub context_id_type: u8,
56 pub common_descriptors: DescriptorLoop<'a>,
59 pub resolution_providers: &'a [u8],
62}
63
64impl<'a> Parse<'a> for Rnt<'a> {
65 type Error = crate::error::Error;
66
67 fn parse(bytes: &'a [u8]) -> Result<Self> {
68 if bytes.len() < MIN_LEN {
69 return Err(Error::BufferTooShort {
70 need: MIN_LEN,
71 have: bytes.len(),
72 what: "Rnt",
73 });
74 }
75
76 if bytes[0] != TABLE_ID {
77 return Err(Error::UnexpectedTableId {
78 table_id: bytes[0],
79 what: "Rnt",
80 expected: &[TABLE_ID],
81 });
82 }
83
84 let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
87 let total = HEADER_LEN + section_length as usize;
88 if bytes.len() < total {
89 return Err(Error::SectionLengthOverflow {
90 declared: section_length as usize,
91 available: bytes.len() - HEADER_LEN,
92 });
93 }
94
95 let context_id = u16::from_be_bytes([bytes[3], bytes[4]]);
102 let version_number = (bytes[5] >> 1) & 0x1F;
103 let current_next_indicator = (bytes[5] & 0x01) != 0;
104 let section_number = bytes[6];
105 let last_section_number = bytes[7];
106 let context_id_type = bytes[8];
107
108 let common_desc_len_pos = HEADER_LEN + EXTENSION_HEADER_LEN;
110 let common_descriptors_length = (((bytes[common_desc_len_pos] & 0x0F) as usize) << 8)
111 | bytes[common_desc_len_pos + 1] as usize;
112
113 let common_desc_start = common_desc_len_pos + COMMON_DESC_LEN_FIELD;
114 let common_desc_end = common_desc_start + common_descriptors_length;
115
116 if common_desc_end > total - CRC_LEN {
117 return Err(Error::SectionLengthOverflow {
118 declared: common_descriptors_length,
119 available: (total - CRC_LEN).saturating_sub(common_desc_start),
120 });
121 }
122
123 let common_descriptors = DescriptorLoop::new(&bytes[common_desc_start..common_desc_end]);
124
125 let rp_start = common_desc_end;
128 let rp_end = total - CRC_LEN;
129 let resolution_providers = &bytes[rp_start..rp_end];
130
131 Ok(Rnt {
132 context_id,
133 version_number,
134 current_next_indicator,
135 section_number,
136 last_section_number,
137 context_id_type,
138 common_descriptors,
139 resolution_providers,
140 })
141 }
142}
143
144impl Serialize for Rnt<'_> {
145 type Error = crate::error::Error;
146
147 fn serialized_len(&self) -> usize {
148 HEADER_LEN
149 + EXTENSION_HEADER_LEN
150 + COMMON_DESC_LEN_FIELD
151 + self.common_descriptors.len()
152 + self.resolution_providers.len()
153 + CRC_LEN
154 }
155
156 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
157 let len = self.serialized_len();
158 if buf.len() < len {
159 return Err(Error::OutputBufferTooSmall {
160 need: len,
161 have: buf.len(),
162 });
163 }
164
165 let section_length = (len - HEADER_LEN) as u16;
167 buf[0] = TABLE_ID;
168 buf[1] = 0xB0 | ((section_length >> 8) as u8 & 0x0F);
170 buf[2] = (section_length & 0xFF) as u8;
171
172 buf[3..5].copy_from_slice(&self.context_id.to_be_bytes());
174 buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
175 buf[6] = self.section_number;
176 buf[7] = self.last_section_number;
177 buf[8] = self.context_id_type;
178
179 let cdl = self.common_descriptors.len() as u16;
181 let cdl_pos = HEADER_LEN + EXTENSION_HEADER_LEN;
182 buf[cdl_pos] = 0xF0 | ((cdl >> 8) as u8 & 0x0F);
183 buf[cdl_pos + 1] = (cdl & 0xFF) as u8;
184
185 let cd_start = cdl_pos + COMMON_DESC_LEN_FIELD;
187 let cd_end = cd_start + self.common_descriptors.len();
188 buf[cd_start..cd_end].copy_from_slice(self.common_descriptors.raw());
189
190 let rp_end = cd_end + self.resolution_providers.len();
192 buf[cd_end..rp_end].copy_from_slice(self.resolution_providers);
193
194 let crc_pos = len - CRC_LEN;
196 let crc = dvb_common::crc32_mpeg2::compute(&buf[..crc_pos]);
197 buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
198
199 Ok(len)
200 }
201}
202
203impl<'a> Table<'a> for Rnt<'a> {
204 const TABLE_ID: u8 = TABLE_ID;
205 const PID: u16 = PID;
206}
207
208impl<'a> crate::traits::TableDef<'a> for Rnt<'a> {
209 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
210 const NAME: &'static str = "RELATED_AND_NEIGHBOURING";
211}
212
213#[cfg(test)]
214mod tests {
215 use super::*;
216
217 #[allow(clippy::too_many_arguments)]
221 fn build_rnt(
222 context_id: u16,
223 version: u8,
224 current_next: bool,
225 section_number: u8,
226 last_section_number: u8,
227 context_id_type: u8,
228 common_desc: &[u8],
229 resolution_providers: &[u8],
230 ) -> Vec<u8> {
231 let rnt = Rnt {
232 context_id,
233 version_number: version,
234 current_next_indicator: current_next,
235 section_number,
236 last_section_number,
237 context_id_type,
238 common_descriptors: DescriptorLoop::new(common_desc),
239 resolution_providers,
240 };
241 let mut buf = vec![0u8; rnt.serialized_len()];
242 rnt.serialize_into(&mut buf).unwrap();
243 buf
244 }
245
246 #[test]
247 fn parse_happy_path() {
248 let common_desc = [0x83u8, 0x02, 0xAB, 0xCD];
253 let rp_bytes = [0xF0u8, 0x00, 0x02, b'b', b'b', 0xF0, 0x00];
254 let bytes = build_rnt(0x0042, 3, true, 0, 0, 0x01, &common_desc, &rp_bytes);
255
256 let rnt = Rnt::parse(&bytes).unwrap();
257 assert_eq!(rnt.context_id, 0x0042);
258 assert_eq!(rnt.version_number, 3);
259 assert!(rnt.current_next_indicator);
260 assert_eq!(rnt.section_number, 0);
261 assert_eq!(rnt.last_section_number, 0);
262 assert_eq!(rnt.context_id_type, 0x01);
263 assert_eq!(rnt.common_descriptors.raw(), &common_desc[..]);
264 assert_eq!(rnt.resolution_providers, &rp_bytes[..]);
265 }
266
267 #[test]
268 fn parse_no_descriptors_no_providers() {
269 let bytes = build_rnt(0x0000, 0, false, 0, 0, 0x00, &[], &[]);
270 let rnt = Rnt::parse(&bytes).unwrap();
271 assert_eq!(rnt.common_descriptors.len(), 0);
272 assert_eq!(rnt.resolution_providers.len(), 0);
273 }
274
275 #[test]
276 fn parse_rejects_wrong_table_id() {
277 let mut bytes = build_rnt(0x0001, 0, true, 0, 0, 0x00, &[], &[]);
278 bytes[0] = 0x70; let err = Rnt::parse(&bytes).unwrap_err();
280 assert!(matches!(
281 err,
282 Error::UnexpectedTableId { table_id: 0x70, .. }
283 ));
284 }
285
286 #[test]
287 fn parse_rejects_short_buffer() {
288 let err = Rnt::parse(&[0x79, 0x00]).unwrap_err();
289 assert!(matches!(err, Error::BufferTooShort { .. }));
290 }
291
292 #[test]
293 fn serialize_round_trip() {
294 let common_desc = [0x40u8, 0x03, b'R', b'N', b'T'];
295 let rp_bytes = [0xF0u8, 0x03, 0x00, 0xF0, 0x00];
298 let rnt = Rnt {
299 context_id: 0xABCD,
300 version_number: 15,
301 current_next_indicator: true,
302 section_number: 1,
303 last_section_number: 2,
304 context_id_type: 0x02,
305 common_descriptors: DescriptorLoop::new(&common_desc),
306 resolution_providers: &rp_bytes,
307 };
308
309 let mut buf = vec![0u8; rnt.serialized_len()];
310 rnt.serialize_into(&mut buf).unwrap();
311 let parsed = Rnt::parse(&buf).unwrap();
312 assert_eq!(rnt, parsed);
313 }
314
315 #[test]
316 fn serialize_rejects_too_small_buffer() {
317 let rnt = Rnt {
318 context_id: 0x0001,
319 version_number: 0,
320 current_next_indicator: true,
321 section_number: 0,
322 last_section_number: 0,
323 context_id_type: 0x00,
324 common_descriptors: DescriptorLoop::new(&[]),
325 resolution_providers: &[],
326 };
327 let mut buf = vec![0u8; 2];
328 let err = rnt.serialize_into(&mut buf).unwrap_err();
329 assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
330 }
331}