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 header compressed size check by checking against an aligned size #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 15 additions & 5 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
},
};

#[derive(Error, Debug)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Error)]
pub enum DecodeError {
#[error("Invalid Woff2 File {0}")]
Invalid(String),
Expand Down Expand Up @@ -68,6 +68,8 @@ pub fn is_woff2(input_buffer: &[u8]) -> bool {

/// Converts a WOFF2 font in `input_buffer` into a TTF format font.
pub fn convert_woff2_to_ttf(input_buffer: &mut impl Buf) -> Result<Vec<u8>, DecodeError> {
let input_size = input_buffer.remaining();

let header = Woff2Header::from_buf(input_buffer)?;
header.is_valid_header()?;

Expand Down Expand Up @@ -95,10 +97,15 @@ pub fn convert_woff2_to_ttf(input_buffer: &mut impl Buf) -> Result<Vec<u8>, Deco
brotli::BrotliDecompress(&mut input_buffer.reader(), &mut decompressed_tables)?;

let compressed_size = stream_start_remaining - input_buffer.remaining();

if compressed_size != usize::try_from(header.total_compressed_size).unwrap() + 1 {
let size_until_stream_start = input_size - stream_start_remaining;
// Header provided compressed size with 4 bytes alignment.
let header_compressed_size =
((size_until_stream_start + usize::try_from(header.total_compressed_size).unwrap() + 0b11)
& !0b11)
- size_until_stream_start;
if compressed_size != header_compressed_size {
Err(DecodeError::Invalid(
"Compressed stream size does not match header".to_string(),
format!("Compressed stream size does not match header, expected {header_compressed_size} including padding, got {compressed_size}"),
))?;
}

Expand Down Expand Up @@ -153,7 +160,10 @@ mod tests {
}
#[test]
// Spec: https://www.w3.org/TR/WOFF2/#table_order
// The loca table MUST follow the glyf table in the table directory. When WOFF2 file contains individually encoded font file, the table directory MAY contain other tables inserted between glyf and loca tables; however when WOFF2 contains a font collection file each loca table MUST immediately follow its corresponding glyf table. For example, the following order of tables: 'cmap', 'glyf', 'hhea', 'hmtx', 'loca', 'maxp' ... is acceptable for individually encoded font files;
// The loca table MUST follow the glyf table in the table directory. When WOFF2 file contains individually encoded font file,
// the table directory MAY contain other tables inserted between glyf and loca tables; however when WOFF2 contains a font collection file
// each loca table MUST immediately follow its corresponding glyf table. For example, the following order of tables:
// 'cmap', 'glyf', 'hhea', 'hmtx', 'loca', 'maxp' ... is acceptable for individually encoded font files;
fn read_loca_is_not_after_glyf_font() {
// In this test font, the loca table does not follow the glyf table.
let buffer = FONTAWESOME_REGULAR_400;
Expand Down