1use crate::descriptors::{AitDescriptorLoop, DescriptorLoop};
7use crate::error::{Error, Result};
8use alloc::vec::Vec;
9use broadcast_common::{Parse, Serialize};
10
11pub const TABLE_ID: u8 = 0x74;
13pub const PID: u16 = 0x0000;
15
16const MIN_HEADER_LEN: usize = 3;
17const EXTENSION_HEADER_LEN: usize = 5;
18const COMMON_DESC_LEN_BYTES: usize = 2;
19const APP_LOOP_LEN_BYTES: usize = 2;
20const CRC_LEN: usize = 4;
21const APP_HEADER_LEN: usize = 9;
22const MIN_SECTION_LEN: usize =
23 MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES + APP_LOOP_LEN_BYTES + CRC_LEN;
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
27#[cfg_attr(feature = "serde", derive(serde::Serialize))]
28#[non_exhaustive]
29pub enum ControlCode {
30 Reserved,
32 Autostart,
34 Present,
36 Destroy,
38 Kill,
40 Prefetch,
42 Remote,
44 Disabled,
46 PlaybackAutostart,
48 Unallocated(u8),
50}
51
52impl ControlCode {
53 #[must_use]
54 pub fn from_u8(v: u8) -> Self {
56 match v {
57 0x00 => Self::Reserved,
58 0x01 => Self::Autostart,
59 0x02 => Self::Present,
60 0x03 => Self::Destroy,
61 0x04 => Self::Kill,
62 0x05 => Self::Prefetch,
63 0x06 => Self::Remote,
64 0x07 => Self::Disabled,
65 0x08 => Self::PlaybackAutostart,
66 _ => Self::Unallocated(v),
67 }
68 }
69
70 #[must_use]
71 pub const fn to_u8(self) -> u8 {
73 match self {
74 Self::Reserved => 0x00,
75 Self::Autostart => 0x01,
76 Self::Present => 0x02,
77 Self::Destroy => 0x03,
78 Self::Kill => 0x04,
79 Self::Prefetch => 0x05,
80 Self::Remote => 0x06,
81 Self::Disabled => 0x07,
82 Self::PlaybackAutostart => 0x08,
83 Self::Unallocated(v) => v,
84 }
85 }
86
87 #[must_use]
88 pub fn name(self) -> &'static str {
90 match self {
91 Self::Reserved => "Reserved",
92 Self::Autostart => "AUTOSTART",
93 Self::Present => "PRESENT",
94 Self::Destroy => "DESTROY",
95 Self::Kill => "KILL",
96 Self::Prefetch => "PREFETCH",
97 Self::Remote => "REMOTE",
98 Self::Disabled => "DISABLED",
99 Self::PlaybackAutostart => "PLAYBACK_AUTOSTART",
100 Self::Unallocated(_) => "Unallocated",
101 }
102 }
103}
104broadcast_common::impl_spec_display!(ControlCode, Unallocated);
105
106#[derive(Debug, Clone, Copy, PartialEq, Eq)]
111#[cfg_attr(feature = "serde", derive(serde::Serialize))]
112#[non_exhaustive]
113pub enum ApplicationType {
114 DvbJ,
116 DvbHtml,
118 HbbTv,
120 OipfDae,
122 Reserved(u16),
124 UserDefined(u16),
126}
127
128impl ApplicationType {
129 #[must_use]
130 pub fn from_u16(v: u16) -> Self {
132 match v {
133 0x0001 => Self::DvbJ,
134 0x0002 => Self::DvbHtml,
135 0x0010 => Self::HbbTv,
136 0x0011 => Self::OipfDae,
137 v @ 0x0000..0x8000 => Self::Reserved(v),
138 _ => Self::UserDefined(v),
139 }
140 }
141
142 #[must_use]
143 pub const fn to_u16(self) -> u16 {
145 match self {
146 Self::DvbJ => 0x0001,
147 Self::DvbHtml => 0x0002,
148 Self::HbbTv => 0x0010,
149 Self::OipfDae => 0x0011,
150 Self::Reserved(v) | Self::UserDefined(v) => v,
151 }
152 }
153
154 #[must_use]
155 pub fn name(self) -> &'static str {
157 match self {
158 Self::DvbJ => "DVB-J",
159 Self::DvbHtml => "DVB-HTML",
160 Self::HbbTv => "HbbTV",
161 Self::OipfDae => "OIPF DAE",
162 Self::Reserved(_) => "Reserved",
163 Self::UserDefined(_) => "User Defined",
164 }
165 }
166}
167broadcast_common::impl_spec_display!(ApplicationType, Reserved, UserDefined);
168
169#[derive(Debug, Clone, PartialEq, Eq)]
171#[cfg_attr(feature = "serde", derive(serde::Serialize))]
172pub struct ApplicationIdentifier {
173 pub organisation_id: u32,
175 pub application_id: u16,
177}
178
179#[derive(Debug, Clone, PartialEq, Eq)]
181#[cfg_attr(feature = "serde", derive(serde::Serialize))]
182#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
183pub struct AitApplication<'a> {
184 pub identifier: ApplicationIdentifier,
186 pub control_code: ControlCode,
188 pub descriptors: DescriptorLoop<'a>,
192}
193
194impl<'a> AitApplication<'a> {
195 #[must_use]
200 pub fn ait_descriptors(&self) -> AitDescriptorLoop<'a> {
201 AitDescriptorLoop::new(self.descriptors.raw())
202 }
203}
204
205#[derive(Debug, Clone, PartialEq, Eq)]
207#[cfg_attr(feature = "serde", derive(serde::Serialize))]
208#[cfg_attr(feature = "yoke", derive(yoke::Yokeable))]
209pub struct AitSection<'a> {
210 pub application_type: ApplicationType,
212 pub test_application_flag: bool,
214 pub version_number: u8,
216 pub current_next_indicator: bool,
218 pub section_number: u8,
220 pub last_section_number: u8,
222 pub common_descriptors: DescriptorLoop<'a>,
226 pub applications: Vec<AitApplication<'a>>,
228}
229
230impl<'a> AitSection<'a> {
231 #[must_use]
236 pub fn common_ait_descriptors(&self) -> AitDescriptorLoop<'a> {
237 AitDescriptorLoop::new(self.common_descriptors.raw())
238 }
239}
240
241impl<'a> Parse<'a> for AitSection<'a> {
242 type Error = crate::error::Error;
243 fn parse(bytes: &'a [u8]) -> Result<Self> {
244 let min_len = MIN_HEADER_LEN
245 + EXTENSION_HEADER_LEN
246 + COMMON_DESC_LEN_BYTES
247 + APP_LOOP_LEN_BYTES
248 + CRC_LEN;
249 if bytes.len() < min_len {
250 return Err(Error::BufferTooShort {
251 need: min_len,
252 have: bytes.len(),
253 what: "AitSection",
254 });
255 }
256
257 if bytes[0] != TABLE_ID {
258 return Err(Error::UnexpectedTableId {
259 table_id: bytes[0],
260 what: "AitSection",
261 expected: &[TABLE_ID],
262 });
263 }
264
265 let section_length = ((bytes[1] & 0x0F) as u16) << 8 | bytes[2] as u16;
266 let total = super::check_section_length(
267 bytes.len(),
268 MIN_HEADER_LEN,
269 section_length as usize,
270 MIN_SECTION_LEN,
271 )?;
272
273 let test_application_flag = (bytes[3] & 0x80) != 0;
274 let application_type_raw = (((bytes[3] & 0x7F) as u16) << 8) | (bytes[4] as u16);
275 let application_type = ApplicationType::from_u16(application_type_raw);
276 let version_number = (bytes[5] >> 1) & 0x1F;
277 let current_next_indicator = (bytes[5] & 0x01) != 0;
278 let section_number = bytes[6];
279 let last_section_number = bytes[7];
280
281 let common_descriptors_length = (((bytes[8] & 0x0F) as usize) << 8) | bytes[9] as usize;
282 let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
283 let common_desc_end = common_desc_start + common_descriptors_length;
284 let app_loop_end = total - CRC_LEN;
285 if common_desc_end > app_loop_end {
286 return Err(Error::SectionLengthOverflow {
287 declared: common_descriptors_length,
288 available: app_loop_end.saturating_sub(common_desc_start),
289 });
290 }
291 let common_descriptors = DescriptorLoop::new(&bytes[common_desc_start..common_desc_end]);
292
293 let app_loop_length =
294 (((bytes[common_desc_end] & 0x0F) as usize) << 8) | bytes[common_desc_end + 1] as usize;
295 let app_loop_start = common_desc_end + APP_LOOP_LEN_BYTES;
296 let app_loop_actual_end = app_loop_start + app_loop_length;
297 if app_loop_actual_end > app_loop_end {
298 return Err(Error::SectionLengthOverflow {
299 declared: app_loop_length,
300 available: app_loop_end.saturating_sub(app_loop_start),
301 });
302 }
303 if app_loop_actual_end != app_loop_end {
310 return Err(Error::BufferTooShort {
311 need: app_loop_end - app_loop_actual_end,
312 have: 0,
313 what: "AitSection trailing bytes after application loop",
314 });
315 }
316
317 let mut applications = Vec::new();
318 let mut pos = app_loop_start;
319 while pos + APP_HEADER_LEN <= app_loop_actual_end {
320 let organisation_id = ((bytes[pos] as u32) << 24)
321 | ((bytes[pos + 1] as u32) << 16)
322 | ((bytes[pos + 2] as u32) << 8)
323 | (bytes[pos + 3] as u32);
324 let application_id = u16::from_be_bytes(*bytes[pos + 4..].first_chunk::<2>().unwrap());
325 let control_code = ControlCode::from_u8(bytes[pos + 6]);
326 let app_desc_length =
327 (((bytes[pos + 7] & 0x0F) as usize) << 8) | bytes[pos + 8] as usize;
328 let app_desc_start = pos + APP_HEADER_LEN;
329 let app_desc_end = app_desc_start + app_desc_length;
330 if app_desc_end > app_loop_actual_end {
331 return Err(Error::SectionLengthOverflow {
332 declared: app_desc_length,
333 available: app_loop_actual_end.saturating_sub(app_desc_start),
334 });
335 }
336 applications.push(AitApplication {
337 identifier: ApplicationIdentifier {
338 organisation_id,
339 application_id,
340 },
341 control_code,
342 descriptors: DescriptorLoop::new(&bytes[app_desc_start..app_desc_end]),
343 });
344 pos = app_desc_end;
345 }
346
347 if pos != app_loop_actual_end {
348 return Err(Error::BufferTooShort {
349 need: app_loop_actual_end - pos,
350 have: 0,
351 what: "AitSection trailing application bytes",
352 });
353 }
354
355 Ok(AitSection {
356 application_type,
357 test_application_flag,
358 version_number,
359 current_next_indicator,
360 section_number,
361 last_section_number,
362 common_descriptors,
363 applications,
364 })
365 }
366}
367
368impl Serialize for AitSection<'_> {
369 type Error = crate::error::Error;
370 fn serialized_len(&self) -> usize {
371 let app_bytes: usize = self
372 .applications
373 .iter()
374 .map(|a| APP_HEADER_LEN + a.descriptors.len())
375 .sum();
376 MIN_HEADER_LEN
377 + EXTENSION_HEADER_LEN
378 + COMMON_DESC_LEN_BYTES
379 + self.common_descriptors.len()
380 + APP_LOOP_LEN_BYTES
381 + app_bytes
382 + CRC_LEN
383 }
384
385 fn serialize_into(&self, buf: &mut [u8]) -> Result<usize> {
386 let len = self.serialized_len();
387 if buf.len() < len {
388 return Err(Error::OutputBufferTooSmall {
389 need: len,
390 have: buf.len(),
391 });
392 }
393
394 let section_length: u16 = (len - MIN_HEADER_LEN) as u16;
395 let app_type_raw = self.application_type.to_u16();
396 buf[0] = TABLE_ID;
397 buf[1] = super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F);
398 buf[2] = (section_length & 0xFF) as u8;
399 buf[3] = (u8::from(self.test_application_flag) << 7) | ((app_type_raw >> 8) as u8 & 0x7F);
400 buf[4] = (app_type_raw & 0xFF) as u8;
401 buf[5] = 0xC0 | ((self.version_number & 0x1F) << 1) | u8::from(self.current_next_indicator);
402 buf[6] = self.section_number;
403 buf[7] = self.last_section_number;
404
405 let cdl = self.common_descriptors.len() as u16;
406 buf[8] = 0xF0 | ((cdl >> 8) as u8 & 0x0F);
407 buf[9] = (cdl & 0xFF) as u8;
408
409 let common_desc_start = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
410 buf[common_desc_start..common_desc_start + self.common_descriptors.len()]
411 .copy_from_slice(self.common_descriptors.raw());
412
413 let app_loop_start = common_desc_start + self.common_descriptors.len();
414 let app_bytes: usize = self
415 .applications
416 .iter()
417 .map(|a| APP_HEADER_LEN + a.descriptors.len())
418 .sum();
419 let apl = app_bytes as u16;
420 buf[app_loop_start] = 0xF0 | ((apl >> 8) as u8 & 0x0F);
421 buf[app_loop_start + 1] = (apl & 0xFF) as u8;
422
423 let mut pos = app_loop_start + APP_LOOP_LEN_BYTES;
424 for app in &self.applications {
425 buf[pos..pos + 4].copy_from_slice(&app.identifier.organisation_id.to_be_bytes());
426 buf[pos + 4..pos + 6].copy_from_slice(&app.identifier.application_id.to_be_bytes());
427 buf[pos + 6] = app.control_code.to_u8();
428 let adl = app.descriptors.len() as u16;
429 buf[pos + 7] = 0xF0 | ((adl >> 8) as u8 & 0x0F);
430 buf[pos + 8] = (adl & 0xFF) as u8;
431 let desc_start = pos + APP_HEADER_LEN;
432 buf[desc_start..desc_start + app.descriptors.len()]
433 .copy_from_slice(app.descriptors.raw());
434 pos = desc_start + app.descriptors.len();
435 }
436
437 let crc_pos = len - CRC_LEN;
438 let crc = broadcast_common::crc32_mpeg2::compute(&buf[..crc_pos]);
439 buf[crc_pos..len].copy_from_slice(&crc.to_be_bytes());
440 Ok(len)
441 }
442}
443impl<'a> crate::traits::TableDef<'a> for AitSection<'a> {
444 const TABLE_ID_RANGES: &'static [(u8, u8)] = &[(TABLE_ID, TABLE_ID)];
445 const NAME: &'static str = "APPLICATION_INFORMATION";
446}
447
448#[cfg(test)]
449mod tests {
450 use super::*;
451
452 fn build_ait(
453 application_type: u16,
454 test_flag: bool,
455 version: u8,
456 common_descriptors: &[u8],
457 applications: &[(u32, u16, u8, Vec<u8>)],
458 ) -> Vec<u8> {
459 let app_bytes: usize = applications
460 .iter()
461 .map(|(_, _, _, d)| APP_HEADER_LEN + d.len())
462 .sum();
463 let section_length: u16 = (EXTENSION_HEADER_LEN
464 + COMMON_DESC_LEN_BYTES
465 + common_descriptors.len()
466 + APP_LOOP_LEN_BYTES
467 + app_bytes
468 + CRC_LEN) as u16;
469 let mut v = vec![
470 TABLE_ID,
471 super::super::SECTION_B1_FLAGS_DVB | ((section_length >> 8) as u8 & 0x0F),
472 (section_length & 0xFF) as u8,
473 (u8::from(test_flag) << 7) | ((application_type >> 8) as u8 & 0x7F),
474 (application_type & 0xFF) as u8,
475 0xC0 | ((version & 0x1F) << 1) | 0x01,
476 0,
477 0,
478 ];
479 let cdl = common_descriptors.len() as u16;
480 v.push(0xF0 | ((cdl >> 8) as u8 & 0x0F));
481 v.push((cdl & 0xFF) as u8);
482 v.extend_from_slice(common_descriptors);
483 let apl = app_bytes as u16;
484 v.push(0xF0 | ((apl >> 8) as u8 & 0x0F));
485 v.push((apl & 0xFF) as u8);
486 for &(org_id, app_id, cc, ref desc) in applications {
487 v.extend_from_slice(&org_id.to_be_bytes());
488 v.extend_from_slice(&app_id.to_be_bytes());
489 v.push(cc);
490 let adl = desc.len() as u16;
491 v.push(0xF0 | ((adl >> 8) as u8 & 0x0F));
492 v.push((adl & 0xFF) as u8);
493 v.extend_from_slice(desc);
494 }
495 v.extend_from_slice(&[0, 0, 0, 0]);
496 v
497 }
498
499 #[test]
500 fn parse_rejects_wrong_table_id() {
501 let mut bytes = build_ait(0x0010, false, 0, &[], &[]);
502 bytes[0] = 0x00;
503 let err = AitSection::parse(&bytes).unwrap_err();
504 assert!(matches!(
505 err,
506 Error::UnexpectedTableId { table_id: 0x00, .. }
507 ));
508 }
509
510 #[test]
511 fn parse_rejects_short_buffer() {
512 let err = AitSection::parse(&[0x74, 0x00]).unwrap_err();
513 assert!(matches!(err, Error::BufferTooShort { .. }));
514 }
515
516 #[test]
517 fn parse_empty_ait_no_applications() {
518 let bytes = build_ait(0x0010, false, 5, &[], &[]);
519 let ait = AitSection::parse(&bytes).expect("parse");
520 assert_eq!(ait.application_type, ApplicationType::HbbTv);
521 assert!(!ait.test_application_flag);
522 assert_eq!(ait.version_number, 5);
523 assert!(ait.current_next_indicator);
524 assert_eq!(ait.section_number, 0);
525 assert_eq!(ait.last_section_number, 0);
526 assert_eq!(ait.common_descriptors.len(), 0);
527 assert_eq!(ait.applications.len(), 0);
528 }
529
530 #[test]
531 fn parse_test_application_flag_extracted() {
532 let bytes = build_ait(0x0010, true, 0, &[], &[]);
533 let ait = AitSection::parse(&bytes).unwrap();
534 assert!(ait.test_application_flag);
535 }
536
537 #[test]
538 fn parse_common_descriptors_preserved() {
539 let desc = vec![0x00, 0x02, 0xAA, 0xBB];
540 let bytes = build_ait(0x0010, false, 0, &desc, &[]);
541 let ait = AitSection::parse(&bytes).unwrap();
542 assert_eq!(ait.common_descriptors.raw(), &desc[..]);
543 }
544
545 #[test]
546 fn parse_single_application() {
547 let desc = vec![0x02, 0x03, 0xCC, 0xDD, 0xEE];
548 let bytes = build_ait(
549 0x0010,
550 false,
551 0,
552 &[],
553 &[(0x12345678, 0xABCD, 0x01, desc.clone())],
554 );
555 let ait = AitSection::parse(&bytes).unwrap();
556 assert_eq!(ait.applications.len(), 1);
557 assert_eq!(ait.applications[0].identifier.organisation_id, 0x12345678);
558 assert_eq!(ait.applications[0].identifier.application_id, 0xABCD);
559 assert_eq!(ait.applications[0].control_code, ControlCode::Autostart);
560 assert_eq!(ait.applications[0].descriptors.raw(), &desc[..]);
561 }
562
563 #[test]
564 fn parse_multiple_applications_preserve_order() {
565 let bytes = build_ait(
566 0x0010,
567 false,
568 0,
569 &[],
570 &[
571 (0x00000001, 0x0001, 0x01, vec![]),
572 (0x00000002, 0x0002, 0x02, vec![0x01]),
573 (0x00000003, 0x0003, 0x03, vec![0x02, 0x03]),
574 ],
575 );
576 let ait = AitSection::parse(&bytes).unwrap();
577 assert_eq!(ait.applications.len(), 3);
578 assert_eq!(ait.applications[0].identifier.organisation_id, 1);
579 assert_eq!(ait.applications[1].identifier.organisation_id, 2);
580 assert_eq!(ait.applications[2].identifier.organisation_id, 3);
581 }
582
583 #[test]
584 fn serialize_round_trip_empty() {
585 let ait = AitSection {
586 application_type: ApplicationType::HbbTv,
587 test_application_flag: false,
588 version_number: 3,
589 current_next_indicator: true,
590 section_number: 0,
591 last_section_number: 0,
592 common_descriptors: DescriptorLoop::new(&[]),
593 applications: vec![],
594 };
595 let mut buf = vec![0u8; ait.serialized_len()];
596 ait.serialize_into(&mut buf).unwrap();
597 let reparsed = AitSection::parse(&buf).unwrap();
598 assert_eq!(ait, reparsed);
599 }
600
601 #[test]
602 fn serialize_round_trip_with_applications() {
603 let desc1: [u8; 2] = [0xAA, 0xBB];
604 let ait = AitSection {
605 application_type: ApplicationType::HbbTv,
606 test_application_flag: true,
607 version_number: 7,
608 current_next_indicator: true,
609 section_number: 1,
610 last_section_number: 2,
611 common_descriptors: DescriptorLoop::new(&[0x01, 0x00]),
612 applications: vec![
613 AitApplication {
614 identifier: ApplicationIdentifier {
615 organisation_id: 0x12345678,
616 application_id: 0xABCD,
617 },
618 control_code: ControlCode::Autostart,
619 descriptors: DescriptorLoop::new(&desc1),
620 },
621 AitApplication {
622 identifier: ApplicationIdentifier {
623 organisation_id: 0x87654321,
624 application_id: 0x00EF,
625 },
626 control_code: ControlCode::Present,
627 descriptors: DescriptorLoop::new(&[]),
628 },
629 ],
630 };
631 let mut buf = vec![0u8; ait.serialized_len()];
632 ait.serialize_into(&mut buf).unwrap();
633 let reparsed = AitSection::parse(&buf).unwrap();
634 assert_eq!(ait, reparsed);
635 }
636
637 #[test]
638 fn parse_rejects_zero_section_length() {
639 let mut buf = vec![0u8; 64];
640 buf[0] = TABLE_ID;
641 buf[1] = 0xF0;
642 buf[2] = 0x00;
643 for b in &mut buf[3..] {
644 *b = 0xFF;
645 }
646 assert!(matches!(
647 AitSection::parse(&buf).unwrap_err(),
648 Error::SectionLengthOverflow { .. }
649 ));
650 }
651
652 #[test]
653 fn control_code_full_range_round_trip() {
654 for byte in 0u8..=0xFF {
655 let cc = ControlCode::from_u8(byte);
656 assert_eq!(
657 cc.to_u8(),
658 byte,
659 "ControlCode round-trip failed for {byte:#04x}"
660 );
661 }
662 }
663
664 #[test]
665 fn control_code_named_values() {
666 assert_eq!(ControlCode::Autostart.to_u8(), 0x01);
667 assert_eq!(ControlCode::Kill.to_u8(), 0x04);
668 assert_eq!(ControlCode::Prefetch.to_u8(), 0x05);
669 assert_eq!(ControlCode::PlaybackAutostart.to_u8(), 0x08);
670 }
671
672 #[test]
673 fn control_code_wire_to_name() {
674 assert_eq!(ControlCode::from_u8(0x01).name(), "AUTOSTART");
675 assert_eq!(ControlCode::from_u8(0x04).name(), "KILL");
676 assert_eq!(ControlCode::from_u8(0x08).name(), "PLAYBACK_AUTOSTART");
677 assert_eq!(ControlCode::from_u8(0x00).name(), "Reserved");
678 }
679
680 #[test]
681 fn application_type_full_range_round_trip() {
682 for at in 0u16..=0xFFFF {
683 let app = ApplicationType::from_u16(at);
684 assert_eq!(
685 app.to_u16(),
686 at,
687 "ApplicationType round-trip failed for {at:#06x}"
688 );
689 }
690 }
691
692 #[test]
693 fn ait_descriptors_accessor_decodes_ait_namespace() {
694 let app_desc: Vec<u8> = vec![
697 0x01, 0x09, b'e', b'n', b'g', 0x05, b'H', b'b', b'b', b'T', b'V', ];
702 let buf = build_ait(
703 0x0010,
704 false,
705 0,
706 &[],
707 &[(0x12345678, 0xABCD, 0x01, app_desc)],
708 );
709 let ait = AitSection::parse(&buf).unwrap();
710 assert_eq!(ait.applications.len(), 1);
711
712 let app = &ait.applications[0];
713 let items: Vec<_> = app.ait_descriptors().iter().collect();
714 assert_eq!(items.len(), 1, "expected one AIT descriptor");
715
716 match items[0].as_ref().unwrap() {
717 crate::descriptors::ait::AnyAitDescriptor::ApplicationName(name) => {
718 assert_eq!(name.entries.len(), 1);
719 assert_eq!(
720 name.entries[0].language_code,
721 crate::text::LangCode(*b"eng")
722 );
723 assert_eq!(&*name.entries[0].application_name.decode(), "HbbTV");
724 }
725 other => panic!("expected ApplicationName, got {other:?}"),
726 }
727
728 let common_items: Vec<_> = ait.common_ait_descriptors().iter().collect();
730 assert_eq!(common_items.len(), 0);
731 }
732
733 #[test]
734 fn parse_rejects_trailing_bytes_after_undersized_app_loop_length() {
735 let mut bytes = build_ait(0x0010, false, 0, &[], &[(0x12345678, 0xABCD, 0x01, vec![])]);
739 let sl = (bytes.len() - MIN_HEADER_LEN) as u16 + 2;
740 bytes[1] = (bytes[1] & 0xF0) | ((sl >> 8) as u8 & 0x0F);
741 bytes[2] = (sl & 0xFF) as u8;
742 let crc_pos = bytes.len() - CRC_LEN;
743 bytes.splice(crc_pos..crc_pos, [0xFF, 0xFF]);
744 let err = AitSection::parse(&bytes).unwrap_err();
745 assert!(matches!(
746 err,
747 Error::BufferTooShort {
748 what: "AitSection trailing bytes after application loop",
749 ..
750 }
751 ));
752 }
753
754 #[test]
755 fn parse_rejects_trailing_slack_bytes() {
756 let mut bytes = build_ait(0x0010, false, 0, &[], &[(0x12345678, 0xABCD, 0x01, vec![])]);
764 let common_desc_end = MIN_HEADER_LEN + EXTENSION_HEADER_LEN + COMMON_DESC_LEN_BYTES;
765 let old_apl =
766 (((bytes[common_desc_end] & 0x0F) as usize) << 8) | bytes[common_desc_end + 1] as usize;
767 let new_apl = old_apl + 2;
768 bytes[common_desc_end] = 0xF0 | ((new_apl >> 8) as u8 & 0x0F);
769 bytes[common_desc_end + 1] = (new_apl & 0xFF) as u8;
770 let sl = (bytes.len() - MIN_HEADER_LEN) as u16 + 2;
771 bytes[1] = (bytes[1] & 0xF0) | ((sl >> 8) as u8 & 0x0F);
772 bytes[2] = (sl & 0xFF) as u8;
773 let crc_pos = bytes.len() - CRC_LEN;
774 bytes.splice(crc_pos..crc_pos, [0xFF, 0xFF]);
775 let err = AitSection::parse(&bytes).unwrap_err();
776 assert!(matches!(
777 err,
778 Error::BufferTooShort {
779 what: "AitSection trailing application bytes",
780 ..
781 }
782 ));
783 }
784
785 #[test]
786 fn parse_accepts_well_formed_app_loop_with_no_slack() {
787 let bytes = build_ait(
788 0x0010,
789 false,
790 0,
791 &[],
792 &[
793 (0x12345678, 0xABCD, 0x01, vec![]),
794 (0x87654321, 0x00EF, 0x04, vec![0x01, 0x02, 0x61, 0x62]),
795 ],
796 );
797 let ait = AitSection::parse(&bytes).unwrap();
798 assert_eq!(ait.applications.len(), 2);
799 }
800}