use crate::error::{ParseError, RawTag};
use crate::reader::{be_u16_at, be_u32_at};
use alloc::vec::Vec;
const TAG: RawTag = *b"wOFF";
const WOFF_SIGNATURE: u32 = 0x774F_4646;
const HEADER: usize = 44;
const ENTRY: usize = 20;
pub fn decode(data: &[u8]) -> Result<crate::OwnedFont, ParseError> {
if data.len() < HEADER {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: HEADER,
got: data.len(),
});
}
if be_u32_at(data, 0, TAG)? != WOFF_SIGNATURE {
return Err(ParseError::InvalidSfnt);
}
let flavor = be_u32_at(data, 4, TAG)?;
let declared_len = be_u32_at(data, 8, TAG)?;
let num_tables = usize::from(be_u16_at(data, 12, TAG)?);
if num_tables == 0 {
return Err(ParseError::InvalidSfnt);
}
let dir_len = HEADER + num_tables * ENTRY;
if data.len() < dir_len {
return Err(ParseError::TruncatedTable {
tag: TAG,
expected: dir_len,
got: data.len(),
});
}
if usize::try_from(declared_len)
.map(|l| l > data.len())
.unwrap_or(true)
{
return Err(ParseError::InvalidSfnt);
}
let mut tables: Vec<(RawTag, Vec<u8>)> = Vec::with_capacity(num_tables);
for i in 0..num_tables {
let at = HEADER + i * ENTRY;
let tag_bytes = crate::reader::slice_at(data, at, 4, TAG)?;
let mut tag: [u8; 4] = *b"????";
tag.copy_from_slice(tag_bytes);
let offset = be_u32_at(data, at + 4, TAG)?;
let comp_length = be_u32_at(data, at + 8, TAG)?;
let orig_length = be_u32_at(data, at + 12, TAG)?;
let comp_length = usize::try_from(comp_length).map_err(|_| ParseError::InvalidSfnt)?;
let orig_length = usize::try_from(orig_length).map_err(|_| ParseError::InvalidSfnt)?;
let offset = usize::try_from(offset).map_err(|_| ParseError::InvalidSfnt)?;
let end = offset
.checked_add(comp_length)
.ok_or(ParseError::InvalidSfnt)?;
if end > data.len() {
return Err(ParseError::TruncatedTable {
tag,
expected: end,
got: data.len(),
});
}
if comp_length > orig_length {
return Err(ParseError::InvalidSfnt);
}
let compressed = crate::reader::slice_at(data, offset, comp_length, TAG)?;
let bytes = if comp_length < orig_length {
let limit = orig_length.saturating_add(1);
let out = miniz_oxide::inflate::decompress_to_vec_zlib_with_limit(compressed, limit)
.map_err(|_| ParseError::InvalidSfnt)?;
if out.len() != orig_length {
return Err(ParseError::InvalidSfnt);
}
out
} else {
compressed.to_vec()
};
tables.push((tag, bytes));
}
let sfnt = crate::font::build_sfnt(flavor, &mut tables)?;
crate::OwnedFont::from_sfnt(sfnt)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::indexing_slicing)]
use super::decode;
use crate::error::ParseError;
use alloc::vec::Vec;
const FIXTURE: &[u8] = include_bytes!("../tests/fixtures/minimal.woff");
#[test]
fn decodes_reference_fixture() {
let font = decode(FIXTURE).unwrap();
let f = font.as_font_ref().unwrap();
assert_eq!(f.head().unwrap().units_per_em().unwrap(), 1000);
assert_eq!(f.num_glyphs(), 4);
let cmap = f.cmap().unwrap();
assert_eq!(cmap.codepoint('A').map(crate::GlyphId::to_u32), Some(1));
}
#[test]
fn rejects_bad_containers() {
assert_eq!(
decode(b"nope").unwrap_err(),
ParseError::TruncatedTable {
tag: *b"wOFF",
expected: 44,
got: 4
}
);
assert_eq!(
decode(&FIXTURE[..40]).unwrap_err(),
ParseError::TruncatedTable {
tag: *b"wOFF",
expected: 44,
got: 40
}
);
let mut short: Vec<u8> = FIXTURE[..46].to_vec();
short[12..14].copy_from_slice(&16u16.to_be_bytes()); assert!(matches!(
decode(&short),
Err(ParseError::TruncatedTable { .. })
));
let mut bad_sig: Vec<u8> = FIXTURE.to_vec();
bad_sig[3] = b'X';
assert_eq!(decode(&bad_sig).unwrap_err(), ParseError::InvalidSfnt);
}
}