Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix check in parse_charset #148

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tables/cff/cff1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ impl<'a> Table<'a> {
Some(charset_id::EXPERT_SUBSET) => Charset::ExpertSubset,
Some(offset) => {
let mut s = Stream::new_at(data, offset)?;
parse_charset(number_of_glyphs.get(), &mut s)?
parse_charset(number_of_glyphs, &mut s)?
}
None => Charset::ISOAdobe, // default
};
Expand Down
16 changes: 8 additions & 8 deletions src/tables/cff/charset.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::StringId;
use crate::parser::{FromData, LazyArray16, Stream};
use crate::GlyphId;
use core::num::NonZeroU16;

/// The Expert Encoding conversion as defined in the Adobe Technical Note #5176 Appendix C.
#[rustfmt::skip]
Expand Down Expand Up @@ -189,24 +190,23 @@ impl Charset<'_> {
}
}

pub(crate) fn parse_charset<'a>(number_of_glyphs: u16, s: &mut Stream<'a>) -> Option<Charset<'a>> {
if number_of_glyphs < 2 {
return None;
}

pub(crate) fn parse_charset<'a>(
number_of_glyphs: NonZeroU16,
s: &mut Stream<'a>,
) -> Option<Charset<'a>> {
// -1 everywhere, since `.notdef` is omitted.
let format = s.read::<u8>()?;
match format {
0 => Some(Charset::Format0(
s.read_array16::<StringId>(number_of_glyphs - 1)?,
s.read_array16::<StringId>(number_of_glyphs.get() - 1)?,
)),
1 => {
// The number of ranges is not defined, so we have to
// read until no glyphs are left.
let mut count = 0;
{
let mut s = s.clone();
let mut total_left = number_of_glyphs - 1;
let mut total_left = number_of_glyphs.get() - 1;
while total_left > 0 {
s.skip::<StringId>(); // first
let left = s.read::<u8>()?;
Expand All @@ -222,7 +222,7 @@ pub(crate) fn parse_charset<'a>(number_of_glyphs: u16, s: &mut Stream<'a>) -> Op
let mut count = 0;
{
let mut s = s.clone();
let mut total_left = number_of_glyphs - 1;
let mut total_left = number_of_glyphs.get() - 1;
while total_left > 0 {
s.skip::<StringId>(); // first
let left = s.read::<u16>()?.checked_add(1)?;
Expand Down
Loading