//! Format authority shared by complete-buffer and bounded resumable decoding. use super::*; pub(super) fn glyph_layout( entry: &AssetEntry, ) -> Result<(TileSize, usize, usize, usize, usize, usize), String> { let meta = entry.metadata_as_glyph_bank()?; let tile_size = match meta.tile_size { 8 => TileSize::Size8, 16 => TileSize::Size16, 32 => TileSize::Size32, _ => return Err(format!("Invalid tile_size: {}", meta.tile_size)), }; let palette_count = meta.palette_count as usize; if !(1..=GLYPH_BANK_MAX_PALETTE_COUNT_V1).contains(&palette_count) { return Err(format!("Invalid palette_count: {}", meta.palette_count)); } let width = meta.width as usize; let height = meta.height as usize; let logical_pixels = width.checked_mul(height).ok_or("GlyphBank dimensions overflow")?; let serialized_pixel_bytes = logical_pixels.div_ceil(2); let palette_bytes = palette_count .checked_mul(GLYPH_BANK_PALETTE_BYTES_PER_PALETTE) .ok_or("GlyphBank palette size overflow")?; let serialized_size = serialized_pixel_bytes .checked_add(palette_bytes) .ok_or("GlyphBank serialized size overflow")?; let decoded_size = logical_pixels.checked_add(palette_bytes).ok_or("GlyphBank decoded size overflow")?; if entry.size != serialized_size as u64 { return Err(format!( "Invalid GLYPHBANK serialized size: expected {}, got {}", serialized_size, entry.size )); } if entry.decoded_size != decoded_size as u64 { return Err(format!( "Invalid GLYPHBANK decoded_size: expected {}, got {}", decoded_size, entry.decoded_size )); } Ok((tile_size, width, height, serialized_pixel_bytes, palette_count, palette_bytes)) } pub(super) fn append_pixels(pixels: &mut Vec, bytes: &[u8], logical: usize) { for &packed in bytes { if pixels.len() < logical { pixels.push(packed >> 4); } if pixels.len() < logical { pixels.push(packed & 15); } } } pub(super) fn write_palettes( palettes: &mut [[Color; GLYPH_BANK_COLORS_PER_PALETTE]], position: usize, bytes: &[u8], ) { for (i, rgba) in bytes.as_chunks::<4>().0.iter().enumerate() { let at = position / 4 + i; palettes[at / 16][at % 16] = Color::rgba(rgba[0], rgba[1], rgba[2], rgba[3]); } } pub(super) fn sound_rate(entry: &AssetEntry) -> Result { Ok(entry.metadata_as_sound_bank()?.sample_rate) } // Callers split on even chunk boundaries. Only the final odd byte is ignored. pub(super) fn append_pcm(data: &mut Vec, bytes: &[u8]) { data.extend(bytes.as_chunks::<2>().0.iter().map(|p| i16::from_le_bytes(*p))); } pub(super) fn payload_size(entry: &AssetEntry, actual: usize) -> Result<(), String> { if actual as u64 != entry.size { return Err("Invalid asset payload size".into()); } Ok(()) } pub(super) fn scene_metadata(entry: &AssetEntry) -> Result<(), String> { entry.metadata_as_scene_bank().map(|_| ()) } pub(super) fn empty_layers() -> [SceneLayer; SCENE_LAYER_COUNT_V1] { std::array::from_fn(|_| SceneLayer { active: false, glyph_asset_id: 0, tile_size: TileSize::Size8, parallax_factor: ParallaxFactor { x: 1.0, y: 1.0 }, tilemap: TileMap { width: 0, height: 0, tiles: Vec::new() }, }) } pub(super) fn scene_header(bytes: &[u8]) -> Result<(), String> { if bytes.len() < SCENE_HEADER_BYTES_V1 { return Err("Buffer too small for SCENE".into()); } if bytes[..4] != SCENE_PAYLOAD_MAGIC_V1 { return Err("Invalid SCENE magic".into()); } let version = u16::from_le_bytes([bytes[4], bytes[5]]); if version != SCENE_PAYLOAD_VERSION_V1 { return Err(format!("Unsupported SCENE version: {version}")); } let count = u16::from_le_bytes([bytes[6], bytes[7]]) as usize; if count != SCENE_LAYER_COUNT_V1 { return Err(format!("Invalid SCENE layer count: {count}")); } Ok(()) } pub(super) fn scene_layer( bytes: &[u8], position: usize, payload_len: u64, decoded: &mut usize, ) -> Result { if bytes.len() < SCENE_LAYER_HEADER_BYTES_V1 { return Err("Buffer too small for SCENE layer header".into()); } let u32at = |at| u32::from_le_bytes(bytes[at..at + 4].try_into().unwrap()); let tile_size = match bytes[5] { 8 => TileSize::Size8, 16 => TileSize::Size16, 32 => TileSize::Size32, other => return Err(format!("Invalid SCENE tile size: {other}")), }; let x = f32::from_le_bytes(bytes[8..12].try_into().unwrap()); let y = f32::from_le_bytes(bytes[12..16].try_into().unwrap()); if !x.is_finite() || !y.is_finite() { return Err("Invalid SCENE parallax_factor".into()); } let width = u32at(16) as usize; let height = u32at(20) as usize; let count = u32at(24) as usize; let expected = width.checked_mul(height).ok_or("SCENE tile count overflow")?; if count != expected { return Err(format!( "Invalid SCENE tile count for layer: expected {expected}, got {count}" )); } let end = count .checked_mul(SCENE_TILE_RECORD_BYTES_V1) .and_then(|n| position.checked_add(n)) .ok_or("SCENE payload overflow")?; if end as u64 > payload_len { return Err("Buffer too small for SCENE tile data".into()); } *decoded = decoded .checked_add(SCENE_DECODED_LAYER_OVERHEAD_BYTES_V1) .and_then(|n| count.checked_mul(size_of::()).and_then(|b| n.checked_add(b))) .ok_or("SCENE decoded_size overflow")?; Ok(SceneLayer { active: bytes[0] & 1 != 0, glyph_asset_id: i32::from_le_bytes(bytes[1..5].try_into().unwrap()), tile_size, parallax_factor: ParallaxFactor { x, y }, tilemap: TileMap { width, height, tiles: Vec::with_capacity(count) }, }) } pub(super) fn append_tiles(tiles: &mut Vec, bytes: &[u8]) { tiles.extend(bytes.as_chunks::().0.iter().map(|b| Tile { active: b[0] & 1 != 0, glyph: Glyph { glyph_id: u16::from_le_bytes([b[2], b[3]]), palette_id: b[1] }, flip_x: b[0] & 2 != 0, flip_y: b[0] & 4 != 0, })); } pub(super) fn scene_finish( entry: &AssetEntry, position: usize, decoded: usize, ) -> Result<(), String> { payload_size(entry, position)?; if decoded as u64 != entry.decoded_size { return Err(format!( "Invalid SCENE decoded_size: expected {decoded}, got {}", entry.decoded_size )); } Ok(()) } #[cfg(test)] pub(super) mod tests;