fn read_box_header()

in mp4parse/src/lib.rs [2209:2258]


fn read_box_header<T: ReadBytesExt>(src: &mut T) -> Result<BoxHeader> {
    let size32 = be_u32(src)?;
    let name = BoxType::from(be_u32(src)?);
    let size = match size32 {
        // valid only for top-level box and indicates it's the last box in the file.  usually mdat.
        0 => {
            if name == BoxType::MediaDataBox {
                0
            } else {
                return Err(Error::Unsupported("unknown sized box"));
            }
        }
        1 => be_u64(src)?,
        _ => u64::from(size32),
    };
    trace!("read_box_header: name: {:?}, size: {}", name, size);
    let mut offset = match size32 {
        1 => BoxHeader::MIN_LARGE_SIZE,
        _ => BoxHeader::MIN_SIZE,
    };
    let uuid = if name == BoxType::UuidBox {
        if size >= offset + 16 {
            let mut buffer = [0u8; 16];
            let count = src.read(&mut buffer)?;
            offset += count.to_u64();
            if count == 16 {
                Some(buffer)
            } else {
                debug!("malformed uuid (short read)");
                return Err(Error::UnexpectedEOF);
            }
        } else {
            None
        }
    } else {
        None
    };
    match size32 {
        0 => (),
        1 if offset > size => return Err(Error::from(Status::BoxBadWideSize)),
        _ if offset > size => return Err(Error::from(Status::BoxBadSize)),
        _ => (),
    }
    Ok(BoxHeader {
        name,
        size,
        offset,
        uuid,
    })
}