wuff/
lib.rs

1//! Pure Rust WOFF2 decoder
2
3#![cfg_attr(docsrs, feature(doc_cfg))]
4#![allow(non_snake_case)]
5#![allow(non_upper_case_globals)]
6#![allow(unused_imports)]
7#![allow(dead_code)]
8#![allow(clippy::needless_range_loop)]
9#![allow(clippy::collapsible_if)]
10
11mod decompress;
12mod error;
13mod table_tags;
14mod variable_length;
15mod woff;
16
17pub use decompress::{decompress_woff2_with_custom_brotli};
18
19#[cfg(feature = "brotli")]
20#[cfg_attr(docsrs, doc(cfg(feature = "brotli")))]
21pub use decompress::{decompress_woff2};
22
23#[derive(Copy, Clone)]
24pub(crate) struct Point {
25    pub x: i32,
26    pub y: i32,
27    pub on_curve: bool,
28}
29
30// Round a value up to the nearest multiple of 4. Don't round the value in the
31// case that rounding up overflows.
32//
33// Implemented as a macro to make it generic over the type without horrible type bounds
34macro_rules! Round4 {
35    ($value:expr) => {
36        match $value.checked_add(3) {
37            Some(value_plus_3) => value_plus_3 & !3,
38            None => $value,
39        }
40    };
41}
42use Round4;
43
44/// Compute checksum over size bytes of buf
45pub(crate) fn compute_checksum(buf: &[u8]) -> u32 {
46    let mut checksum: u32 = 0;
47    let mut iter = buf.chunks_exact(4);
48    for chunk in &mut iter {
49        let bytes: [u8; 4] = chunk.try_into().unwrap();
50        checksum = checksum.wrapping_add(u32::from_be_bytes(bytes));
51    }
52
53    // Treat sizes not aligned on 4 as if it were padded to 4 with 0's.
54    checksum = checksum.wrapping_add(match iter.remainder() {
55        &[a, b, c] => u32::from_be_bytes([a, b, c, 0]),
56        &[a, b] => u32::from_be_bytes([a, b, 0, 0]),
57        &[a] => u32::from_be_bytes([a, 0, 0, 0]),
58        [] => 0,
59        _ => unreachable!("chunk size was 4 so remainder will be a slice of length 3 or smaller"),
60    });
61
62    checksum
63}