Skip to content

Commit

Permalink
pff2: Enforce glyphs to be in sorted order
Browse files Browse the repository at this point in the history
  • Loading branch information
max-ishere committed Feb 5, 2024
1 parent 98fe5da commit 5a319f7
Showing 1 changed file with 31 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/parser/pff2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,18 +175,26 @@ impl Parser {
const ENTRY_SIZE: usize = 4 + 1 + 4;

if input.len() % ENTRY_SIZE != 0 {
return Err(ParserError::InvalidCharacterIndex);
return Err(ParserError::EmptyCharacterIndex);
}

let mut last_codepoint = 0;

input
.chunks(ENTRY_SIZE)
.map(|chunk| {
let codepoint = u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);

if codepoint > last_codepoint {
last_codepoint = codepoint;
} else {
return Err(ParserError::CharacterIndexNotSortedAscending);
}

Ok::<_, ParserError>(CharIndex {
code: char::from_u32(codepoint).ok_or(ParserError::InvalidCodepoint(codepoint))?,
//code: u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]),
// skipp [4], it's a `storage_flags`, and GRUB never uses that field anyway
// skip [4], it's a `storage_flags`, and GRUB never uses that field anyway
offset: u32::from_be_bytes([chunk[5], chunk[6], chunk[7], chunk[8]]).to_usize(),
})
})
Expand Down Expand Up @@ -264,6 +272,18 @@ impl Parser {
return Err(NoGlyphs);
}

let mut last_codepoint = self.glyphs[0].code as u32;

for Glyph { code, .. } in &self.glyphs[1..] {
let code = *code as u32;

if code > last_codepoint {
last_codepoint = code;
} else {
return Err(GlyphsNotSortedAscending);
}
}

Ok(Font {
name: self.name,
family: self.family,
Expand Down Expand Up @@ -327,7 +347,11 @@ pub enum ParserError {

/// The size of the character index section doesnt divide evenly by the size of the individual elements
#[error("Invalid data in the character index")]
InvalidCharacterIndex,
EmptyCharacterIndex,

/// The character index section is required to contain codepoints in ascending order (`char as u32`)
#[error("Character index is not sorted in ascending order")]
CharacterIndexNotSortedAscending,

/// The codepoint for a glyph entry was not valid UTF-8
#[error("Invalid unicode codepoint encountered: {0}")]
Expand Down Expand Up @@ -360,6 +384,10 @@ pub enum FontValidationError {
/// Font contains no glyphs or they could not be read
#[error("Font contains no glyphs")]
NoGlyphs,

/// Font must store glyphs in sorted ascending order
#[error("Glyphs are not sorted in ascending order")]
GlyphsNotSortedAscending,
}

impl TryFrom<[u8; 4]> for SectionName {
Expand Down

0 comments on commit 5a319f7

Please sign in to comment.