Skip to main content

dvb_si/tables/
rnt.rs

1//! Resolution provider Notification Table — ETSI TS 102 323 v1.4.1 §5.2.2.
2//!
3//! Carries the locations of CRI (Content Referencing Information) and metadata
4//! for CRID authorities. Carried on PID 0x0016 with table_id 0x79.
5//!
6//! The resolution-provider loop internals (name bytes, per-provider descriptors,
7//! CRID authority sub-loops) are kept as raw bytes — callers that need them
8//! can walk `resolution_providers` directly.
9
10use crate::descriptors::DescriptorLoop;
11use crate::error::{Error, Result};
12use crate::traits::Table;
13use dvb_common::{Parse, Serialize};
14
15/// table_id for the Resolution provider Notification Table.
16pub const TABLE_ID: u8 = 0x79;
17/// Well-known PID on which RNT sections are carried.
18pub const PID: u16 = 0x0016;
19
20/// Byte offset of the 3-byte outer header (table_id + section_length word).
21const HEADER_LEN: usize = 3;
22/// Bytes in the extension header: context_id(2) + version/cni byte(1)
23/// + section_number(1) + last_section_number(1) + context_id_type(1) = 6.
24const EXTENSION_HEADER_LEN: usize = 6;
25/// Bytes for the common_descriptors_length field (2, carries a reserved nibble + 12-bit length).
26const COMMON_DESC_LEN_FIELD: usize = 2;
27/// Bytes consumed by the CRC-32 trailer.
28const CRC_LEN: usize = 4;
29/// Minimum total bytes required to attempt parsing.
30const MIN_LEN: usize = HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_FIELD + CRC_LEN;
31
32/// Resolution provider Notification Table (ETSI TS 102 323 v1.4.1 §5.2.2).
33///
34/// Variable-length fields are kept as borrowed byte slices so no allocation is
35/// required. The `resolution_providers` slice contains everything between the
36/// end of the common-descriptor loop and the CRC-32 trailer; the internal
37/// sub-structure (provider names, per-provider descriptors, CRID authority
38/// loops) is not parsed further.
39#[derive(Debug, Clone, PartialEq, Eq)]
40#[cfg_attr(feature = "serde", derive(serde::Serialize))]
41pub struct Rnt<'a> {
42    /// 16-bit context identifier (table_id_extension at bytes 3–4).
43    pub context_id: u16,
44    /// 5-bit version_number.
45    pub version_number: u8,
46    /// current_next_indicator bit.
47    pub current_next_indicator: bool,
48    /// section_number in the sub-table sequence.
49    pub section_number: u8,
50    /// last_section_number in the sub-table sequence.
51    pub last_section_number: u8,
52    /// context_id_type byte (0x00 = bouquet_id, 0x01 = original_network_id,
53    /// 0x02 = network_id, 0x03–0x7F DVB reserved, 0x80–0xFF user defined).
54    pub context_id_type: u8,
55    /// Common descriptor loop (`common_descriptors_length` bytes). Serializes
56    /// as the typed descriptor sequence; `.raw()` yields the wire bytes.
57    pub common_descriptors: DescriptorLoop<'a>,
58    /// Raw bytes of the resolution-provider loop (everything after the common
59    /// descriptors up to, but not including, the CRC-32 trailer).
60    #[cfg_attr(feature = "serde", serde(borrow))]
61    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        // bytes[1] = section_syntax_indicator(1) | reserved(1) | reserved(2) | section_length[11:8](4)
85        // bytes[2] = section_length[7:0]
86        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        // Extension header (bytes 3–8):
96        //   bytes[3..5]  = context_id (table_id_extension)
97        //   bytes[5]     = reserved(2) | version_number(5) | current_next_indicator(1)
98        //   bytes[6]     = section_number
99        //   bytes[7]     = last_section_number
100        //   bytes[8]     = context_id_type
101        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        // bytes[9..11] = reserved(4) | common_descriptors_length(12)
109        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        // Everything from the end of the common descriptor loop up to (but not
126        // including) the 4-byte CRC trailer is the resolution-provider loop.
127        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        // Outer header.
166        let section_length = (len - HEADER_LEN) as u16;
167        buf[0] = TABLE_ID;
168        // section_syntax_indicator=1, reserved=1, reserved=2, section_length top nibble.
169        buf[1] = 0xB0 | ((section_length >> 8) as u8 & 0x0F);
170        buf[2] = (section_length & 0xFF) as u8;
171
172        // Extension header.
173        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        // common_descriptors_length field (reserved nibble = 0xF).
180        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        // Common descriptors.
186        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        // Resolution-provider loop (opaque bytes).
191        let rp_end = cd_end + self.resolution_providers.len();
192        buf[cd_end..rp_end].copy_from_slice(self.resolution_providers);
193
194        // CRC-32: compute over everything up to (but not including) the CRC slot.
195        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    /// Build a complete RNT section byte vector with the given field values.
218    /// `common_desc` and `resolution_providers` are pasted in verbatim; the
219    /// CRC slot is zeroed (matching the serialize contract).
220    #[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        // context_id 0x0042, version 3, CNI=true, section 0/0,
249        // context_id_type 0x01 (original_network_id),
250        // one dummy common descriptor (tag=0x83, length=2, data=[0xAB, 0xCD]),
251        // one minimal resolution-provider stub (6 opaque bytes).
252        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; // not 0x79
279        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        // Minimal resolution-provider entry: 2-byte length header (0 bytes length),
296        // 1-byte name length (0), 2-byte provider descriptors length (0).
297        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}