disclady 0.4.0

Pure Rust library and CLI utility for optical and storage disc image heuristics, inspection, and format detection (CD, DVD, GDI, Hard Disk, LaserDisc, CHD).
Documentation
//! Raw CD sector heuristics and synchronization pattern detection.

use crate::types::TrackType;
use std::io::{Read, Seek, SeekFrom};

/// The standard 12-byte Red Book CD synchronization pattern.
pub const CD_SYNC_PATTERN: [u8; 12] = [
    0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
];

/// Tests candidate sector sizes (2352, 2448) by checking for repeating sync patterns and valid BCD timecodes.
pub(crate) fn detect_raw_cd_sector_size<R: Read + Seek>(
    reader: &mut R,
    file_size: u64,
) -> Option<u32> {
    for &sector_size in &[2352u32, 2448u32] {
        let size_u64 = sector_size as u64;

        if file_size < size_u64 || !file_size.is_multiple_of(size_u64) {
            continue;
        }

        let total_sectors = file_size / size_u64;
        let sectors_to_check = (total_sectors.min(16) as usize).max(1);
        let mut sync_matches = 0;
        let mut valid_msf_count = 0;
        let mut last_msf_lba: Option<u64> = None;
        let mut msf_progressed_correctly = true;

        let mut header_buf = [0u8; 16];

        for i in 0..sectors_to_check {
            let offset = (i as u64) * size_u64;
            if reader.seek(SeekFrom::Start(offset)).is_err() {
                break;
            }
            if reader.read_exact(&mut header_buf).is_err() {
                break;
            }

            if header_buf[..12] == CD_SYNC_PATTERN {
                sync_matches += 1;

                if let Some(lba) = parse_bcd_msf(&header_buf[12..15]) {
                    valid_msf_count += 1;
                    if let Some(prev_lba) = last_msf_lba {
                        if lba != prev_lba + 1 {
                            msf_progressed_correctly = false;
                        }
                    }
                    last_msf_lba = Some(lba);
                }
            }
        }

        if sync_matches == sectors_to_check && (valid_msf_count <= 1 || msf_progressed_correctly) {
            return Some(sector_size);
        }
    }

    None
}

/// Inspects the header of a raw 2352-byte CD sector to determine track mode.
pub(crate) fn inspect_raw_sector_mode(sector: &[u8]) -> Option<TrackType> {
    if sector.len() < 16 || sector[..12] != CD_SYNC_PATTERN {
        return None;
    }

    match sector[15] {
        0x01 => Some(TrackType::Mode1_2352),
        0x02 => Some(TrackType::Mode2_2352),
        _ => None,
    }
}

/// Determines the user data byte offset within a CD sector (0 for cooked 2048, 16 for Mode 1 2352/2448, 24 for Mode 2 Form 1 CD-ROM XA).
pub(crate) fn cd_user_data_offset(sector: &[u8]) -> usize {
    if sector.len() >= 2352 && sector[..12] == CD_SYNC_PATTERN {
        if sector.len() >= 2072 && (sector[25..30] == crate::iso::ISO9660_ID || sector[15] == 0x02)
        {
            24
        } else {
            16
        }
    } else {
        0
    }
}

/// Extracts the 2,048-byte user data slice from a CD sector.
pub(crate) fn extract_cd_user_data(sector: &[u8]) -> Option<&[u8]> {
    let offset = cd_user_data_offset(sector);
    if sector.len() >= offset + 2048 {
        Some(&sector[offset..offset + 2048])
    } else if sector.len() >= 2048 {
        Some(&sector[0..2048])
    } else {
        None
    }
}

/// Converts a 3-byte BCD (Minute, Second, Frame) slice into an LBA frame count.
fn parse_bcd_msf(bytes: &[u8]) -> Option<u64> {
    if bytes.len() < 3 {
        return None;
    }

    let min = bcd_to_u8(bytes[0])?;
    let sec = bcd_to_u8(bytes[1])?;
    let frame = bcd_to_u8(bytes[2])?;

    if sec >= 60 || frame >= 75 {
        return None;
    }

    Some(((min as u64) * 60 + (sec as u64)) * 75 + (frame as u64))
}

/// Decodes an 8-bit Binary Coded Decimal (BCD) number.
fn bcd_to_u8(b: u8) -> Option<u8> {
    let hi = b >> 4;
    let lo = b & 0x0F;
    if hi < 10 && lo < 10 {
        Some(hi * 10 + lo)
    } else {
        None
    }
}