Skip to main content

scan_packs

Function scan_packs 

Source
pub fn scan_packs(b: &[u8]) -> PackScan<'_>
Expand description

Walk a Program Stream like parse_all_packs, but resynchronise after a pack that fails to parse instead of abandoning the whole stream.

On a parse failure the failed span is recorded in PackScan::skipped (with the error, never swallowed) and scanning resumes at the next pack_start_code (0x000001BA) after the failed pack’s first byte. That byte search is deliberately confined to this recovery path: a well-formed stream is still walked purely by PES_packet_length (see parse_pack), so start-code emulation inside a payload cannot truncate a good pack — only a stream that is already corrupt can resync onto an emulated code (audit r14-MPS-W3, #1119).

// A garbage prefix, then a valid empty pack (pack header only).
let mut data = vec![0x00, 0x00, 0x01, 0xBA, 0xFF, 0xFF, 0xFF, 0xFF];
data.extend_from_slice(&[
    0x00, 0x00, 0x01, 0xBA, 0x44, 0x00, 0x04, 0x00, 0x04, 0x01,
    0x40, 0x00, 0x03, 0x00,
]);
let scan = mpeg_ps::program_stream::scan_packs(&data);
assert_eq!(scan.skipped.len(), 1);
assert_eq!(scan.skipped[0].offset, 0);
assert_eq!(scan.packs.len(), 1);