use crate::cursor::range;
use crate::image::layout;
use crate::{Error, Header, Result};
const DICTIONARY_OFFSET: usize = 80; const ENTRY_SIZE: usize = 8; const NO_DATA_CHUNK: u32 = 0x02 << 24;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct ResourceTag(pub u32);
impl ResourceTag {
pub const THUMBNAIL_DATA: Self = Self(0x01);
pub const IMAGE_DATA: Self = Self(0x30);
pub const PARTICLE_SHEET_DATA: Self = Self(0x10);
pub const EXTENDED_FLAGS: Self = Self::from_bytes(*b"TS0");
pub const CRC: Self = Self::from_bytes(*b"CRC");
pub const LOD_CONTROL_INFO: Self = Self::from_bytes(*b"LOD");
pub const STREAM_SETTINGS: Self = Self::from_bytes(*b"STR");
pub const KEY_VALUES_DATA: Self = Self::from_bytes(*b"KVD");
pub const fn from_bytes(bytes: [u8; 3]) -> Self {
Self(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], 0]))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ResourceLocation {
Inline([u8; 4]),
Data { offset: usize, length: usize },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Resource {
pub tag: ResourceTag,
pub location: ResourceLocation,
}
pub(super) fn parse_dictionary(bytes: &[u8], header: &Header, count: usize) -> Result<Vec<Resource>> {
let table = range(bytes, DICTIONARY_OFFSET, count.saturating_mul(ENTRY_SIZE), "resource table")?;
let mut resources = Vec::with_capacity(count);
for entry in table.chunks_exact(ENTRY_SIZE) {
let kind = u32::from_le_bytes(entry[..4].try_into().unwrap());
let data: [u8; 4] = entry[4..].try_into().unwrap();
let tag = ResourceTag(kind & 0x00ff_ffff);
if kind & NO_DATA_CHUNK != 0 {
resources.push(Resource { tag, location: ResourceLocation::Inline(data) });
continue;
}
let offset = u32::from_le_bytes(data) as usize;
let (offset, length) = match tag {
ResourceTag::IMAGE_DATA => (offset, layout::image_len(header)),
ResourceTag::THUMBNAIL_DATA => (offset, layout::thumbnail_len(header)),
_ => {
let size = range(bytes, offset, 4, "resource size")?;
(offset + 4, u32::from_le_bytes(size.try_into().unwrap()) as usize)
}
};
range(bytes, offset, length, "resource data")?;
resources.push(Resource { tag, location: ResourceLocation::Data { offset, length } });
}
require(&resources, ResourceTag::IMAGE_DATA)?;
if layout::thumbnail_len(header) != 0 {
require(&resources, ResourceTag::THUMBNAIL_DATA)?;
}
Ok(resources)
}
pub(super) fn legacy_resources(bytes: &[u8], header: &Header, header_size: usize) -> Result<Vec<Resource>> {
let thumbnail_length = layout::thumbnail_len(header);
let image_offset = header_size + thumbnail_length;
let image_length = layout::image_len(header);
range(bytes, image_offset, image_length, "image data")?;
let mut resources = Vec::with_capacity(2);
if thumbnail_length != 0 {
resources.push(Resource {
tag: ResourceTag::THUMBNAIL_DATA,
location: ResourceLocation::Data { offset: header_size, length: thumbnail_length },
});
}
resources.push(Resource {
tag: ResourceTag::IMAGE_DATA,
location: ResourceLocation::Data { offset: image_offset, length: image_length },
});
Ok(resources)
}
fn require(resources: &[Resource], tag: ResourceTag) -> Result<()> {
match resources
.iter()
.find(|resource| resource.tag == tag)
{
Some(Resource { location: ResourceLocation::Data { .. }, .. }) => Ok(()),
_ => Err(Error::MissingResource(tag)),
}
}