Skip to main content

dvb_si/descriptors/
scrambling.rs

1//! Scrambling Descriptor — ETSI EN 300 468 §6.2.32 (tag 0x65).
2//!
3//! A single byte identifying the scrambling mode in use (Table 86 syntax /
4//! Table 87 coding, PDF pp. 98-99): 0x01 = DVB-CSA1, 0x02 = DVB-CSA2,
5//! 0x03 = DVB-CSA3, 0x10 = DVB-CISSA v1, etc.
6
7use crate::error::{Error, Result};
8use crate::traits::Descriptor;
9use dvb_common::{Parse, Serialize};
10
11/// Descriptor tag for scrambling_descriptor.
12pub const TAG: u8 = 0x65;
13const HEADER_LEN: usize = 2;
14/// Fixed payload length: a single scrambling_mode byte (EN 300 468 Table 86).
15const BODY_LEN: u8 = 1;
16
17/// Scrambling Descriptor (tag 0x65).
18#[derive(Debug, Clone, PartialEq, Eq)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize))]
20pub struct ScramblingDescriptor {
21    /// 8-bit scrambling_mode (ETSI Table 87, PDF p. 99).
22    pub scrambling_mode: u8,
23}
24
25impl<'a> Parse<'a> for ScramblingDescriptor {
26    type Error = crate::error::Error;
27    fn parse(bytes: &'a [u8]) -> Result<Self> {
28        if bytes.len() < HEADER_LEN {
29            return Err(Error::BufferTooShort {
30                need: HEADER_LEN,
31                have: bytes.len(),
32                what: "ScramblingDescriptor header",
33            });
34        }
35        if bytes[0] != TAG {
36            return Err(Error::InvalidDescriptor {
37                tag: bytes[0],
38                reason: "unexpected tag for scrambling_descriptor",
39            });
40        }
41        let length = bytes[1] as usize;
42        let total = HEADER_LEN + length;
43        if bytes.len() < total {
44            return Err(Error::BufferTooShort {
45                need: total,
46                have: bytes.len(),
47                what: "ScramblingDescriptor body",
48            });
49        }
50        if length != BODY_LEN as usize {
51            return Err(Error::InvalidDescriptor {
52                tag: TAG,
53                reason: "scrambling_descriptor length must equal 1",
54            });
55        }
56        Ok(Self {
57            scrambling_mode: bytes[HEADER_LEN],
58        })
59    }
60}
61
62impl Serialize for ScramblingDescriptor {
63    type Error = crate::error::Error;
64    fn serialized_len(&self) -> usize {
65        HEADER_LEN + BODY_LEN as usize
66    }
67
68    fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
69        let len = self.serialized_len();
70        if buf.len() < len {
71            return Err(Error::OutputBufferTooSmall {
72                need: len,
73                have: buf.len(),
74            });
75        }
76        buf[0] = TAG;
77        buf[1] = BODY_LEN;
78        buf[HEADER_LEN] = self.scrambling_mode;
79        Ok(len)
80    }
81}
82
83impl<'a> Descriptor<'a> for ScramblingDescriptor {
84    const TAG: u8 = TAG;
85    fn descriptor_length(&self) -> u8 {
86        BODY_LEN
87    }
88}
89
90impl<'a> crate::traits::DescriptorDef<'a> for ScramblingDescriptor {
91    const TAG: u8 = TAG;
92    const NAME: &'static str = "SCRAMBLING";
93}
94
95#[cfg(test)]
96mod tests {
97    use super::*;
98
99    #[test]
100    fn parse_extracts_scrambling_mode() {
101        let bytes = [TAG, 1, 0x02];
102        let d = ScramblingDescriptor::parse(&bytes).unwrap();
103        assert_eq!(d.scrambling_mode, 0x02);
104    }
105
106    #[test]
107    fn parse_rejects_wrong_tag() {
108        let err = ScramblingDescriptor::parse(&[0x66, 1, 0x02]).unwrap_err();
109        assert!(matches!(err, Error::InvalidDescriptor { tag: 0x66, .. }));
110    }
111
112    #[test]
113    fn parse_rejects_short_buffer() {
114        let err = ScramblingDescriptor::parse(&[TAG]).unwrap_err();
115        assert!(matches!(err, Error::BufferTooShort { .. }));
116    }
117
118    #[test]
119    fn parse_rejects_truncated_body() {
120        // length=1 but no payload byte present.
121        let err = ScramblingDescriptor::parse(&[TAG, 1]).unwrap_err();
122        assert!(matches!(err, Error::BufferTooShort { .. }));
123    }
124
125    #[test]
126    fn parse_rejects_wrong_length() {
127        let err = ScramblingDescriptor::parse(&[TAG, 2, 0x02, 0x03]).unwrap_err();
128        assert!(matches!(err, Error::InvalidDescriptor { .. }));
129    }
130
131    #[test]
132    fn serialize_round_trip() {
133        let d = ScramblingDescriptor {
134            scrambling_mode: 0x10,
135        };
136        let mut buf = vec![0u8; d.serialized_len()];
137        d.serialize_into(&mut buf).unwrap();
138        let re = ScramblingDescriptor::parse(&buf).unwrap();
139        assert_eq!(d, re);
140    }
141
142    #[test]
143    fn serialize_rejects_too_small_buffer() {
144        let d = ScramblingDescriptor {
145            scrambling_mode: 0x03,
146        };
147        let mut tiny = [0u8; 1];
148        let err = d.serialize_into(&mut tiny).unwrap_err();
149        assert!(matches!(err, Error::OutputBufferTooSmall { .. }));
150    }
151
152    #[test]
153    fn descriptor_length_matches_payload() {
154        let d = ScramblingDescriptor {
155            scrambling_mode: 0x01,
156        };
157        assert_eq!(d.descriptor_length(), 1);
158    }
159
160    #[cfg(feature = "serde")]
161    #[test]
162    fn serde_round_trip() {
163        let d = ScramblingDescriptor {
164            scrambling_mode: 0x02,
165        };
166        let json = serde_json::to_string(&d).unwrap();
167        // Serialize-only: assert the emitted JSON re-parses (serialize-stable).
168        let _v: serde_json::Value = serde_json::from_str(&json).unwrap();
169    }
170}