Promote verified native PDF and box capacity support

This commit is contained in:
gloamxun committed 2026-11-19 14:39:39 +08:00
1 parent 3706afa483
commit 72ed779749
31 files changed
+1042 -78

No files matched your search

@@ -0,0 +1,150 @@
//! JPEG frame and density metadata. The encoded image stays in its original stream.
pub(super) const SIGNATURE: &[u8] = b"\xff\xd8";
pub(super) struct Header {
pub size: [u16; 2],
pub precision: u8,
pub components: u8,
pub density: [u32; 2],
pub progressive: bool,
}
pub(super) fn read(bytes: &[u8]) -> Result<Header, &'static str> {
let mut rest = bytes
.strip_prefix(SIGNATURE)
.ok_or("missing JPEG signature")?;
let mut density = [0, 0];
let mut first = true;
let truncated = "premature end of JPEG header";
loop {
rest = rest.strip_prefix(&[0xff]).ok_or("expected JPEG marker")?;
while let Some(following) = rest.strip_prefix(&[0xff]) {
rest = following;
}
let (&marker, remaining) = rest.split_first().ok_or(truncated)?;
rest = remaining;
match marker {
0xd9 | 0xda => return Err("JPEG has no frame header"),
0x01 | 0xd0..=0xd8 => {
first = false;
continue;
}
_ => {}
}
let length = usize::from(u16::from_be_bytes(
rest.get(..2)
.ok_or(truncated)?
.try_into()
.expect("two-byte JPEG length"),
));
let segment = rest.get(2..length).ok_or(truncated)?;
rest = &rest[length..];
match marker {
0xc0..=0xc3 => {
let &[precision, h0, h1, w0, w1, components, ref table @ ..] = segment else {
return Err(truncated);
};
if !matches!(components, 1 | 3 | 4) {
return Err("unsupported JPEG color space");
}
if table.len() != 3 * usize::from(components) {
return Err("invalid JPEG component table");
}
let size = [u16::from_be_bytes([w0, w1]), u16::from_be_bytes([h0, h1])];
if size.contains(&0) || precision == 0 {
return Err("invalid JPEG frame dimensions or precision");
}
return Ok(Header {
size,
precision,
components,
density,
progressive: marker == 0xc2,
});
}
0xc5..=0xc7 | 0xc9..=0xcb | 0xcd..=0xcf => {
return Err("unsupported JPEG compression");
}
0xe0 if first => {
if let Some(&[_, _, unit, x0, x1, y0, y1, ..]) = segment.strip_prefix(b"JFIF\0") {
let x = u32::from(u16::from_be_bytes([x0, x1]));
let y = u32::from(u16::from_be_bytes([y0, y1]));
density = match unit {
1 => [x, y],
2 => [x * 254 / 100, y * 254 / 100],
_ => [0, 0],
};
if density.contains(&0) {
density = [density[0].max(density[1]); 2];
}
}
}
0xe1 if first => {
if let Some(tiff) = segment.strip_prefix(b"Exif\0") {
let start = tiff
.iter()
.position(|&byte| byte != 0)
.unwrap_or(tiff.len());
density = exif_density(&tiff[start..]).unwrap_or([0, 0]);
}
}
_ => {}
}
first = false;
}
}
fn exif_density(tiff: &[u8]) -> Option<[u32; 2]> {
let little = match tiff.get(..2)? {
b"II" => true,
b"MM" => false,
_ => return None,
};
let short = |at: usize| {
let bytes = tiff.get(at..at.checked_add(2)?)?.try_into().ok()?;
Some(if little {
u16::from_le_bytes(bytes)
} else {
u16::from_be_bytes(bytes)
})
};
let long = |at: usize| {
let bytes = tiff.get(at..at.checked_add(4)?)?.try_into().ok()?;
Some(if little {
u32::from_le_bytes(bytes)
} else {
u32::from_be_bytes(bytes)
})
};
if short(2)? != 42 {
return None;
}
let start = usize::try_from(long(4)?).ok()?;
let entries = usize::from(short(start)?);
let start = start.checked_add(2)?;
let end = start.checked_add(12 * entries)?;
tiff.get(start..end)?;
let mut density = [72, 72];
let mut cm = false;
for at in (start..end).step_by(12) {
match (short(at)?, short(at + 2)?, long(at + 4)?) {
(tag @ (282 | 283), 5, 1) => {
let offset = usize::try_from(long(at + 8)?).ok()?;
let numerator = long(offset)?;
let denominator = long(offset.checked_add(4)?)?;
if denominator != 0 {
density[usize::from(tag == 283)] = numerator / denominator;
}
}
(296, 3, 1) => cm = short(at + 8)? == 3,
_ => {}
}
}
if cm {
density =
density.map(|value| u32::try_from(u64::from(value) * 254 / 100).unwrap_or(u32::MAX));
}
Some(density)
}