use std::{fs::File, io::Read, path::Path};
use base64::{
Engine, alphabet,
engine::{DecodePaddingMode, GeneralPurpose, GeneralPurposeConfig, general_purpose::STANDARD},
};
use crate::scope;
use super::Content;
pub const IMAGE_LIMIT: usize = 4 * 1024 * 1024;
const PNG_SIGNATURE: &[u8] = b"\x89PNG\r\n\x1a\n";
const PNG_IHDR_HEADER: &[u8] = b"\0\0\0\rIHDR";
const PNG_CHUNK_HEADER_BYTES: usize = 8;
const PNG_LENGTH_BYTES: usize = std::mem::size_of::<u32>();
const BASE64_INPUT_BYTES: usize = 3;
const BASE64_OUTPUT_BYTES: usize = 4;
const PNG_CHUNK_OVERHEAD_BYTES: usize = 12;
const PNG_ANIMATION_CONTROL: &[u8] = b"acTL";
const PNG_END: &[u8] = b"IEND";
const JPEG_START: &[u8] = b"\xff\xd8\xff";
const JPEG_LS_MARKER: u8 = 0xf7;
const RIFF_SIGNATURE: &[u8] = b"RIFF";
const WEBP_FORM: &[u8] = b"WEBP";
const RIFF_FORM_TYPE: std::ops::Range<usize> = 8..12;
pub fn from_base64(data: &str, mime_type: &str) -> Result<Content, String> {
if data.len() > IMAGE_LIMIT.div_ceil(BASE64_INPUT_BYTES) * BASE64_OUTPUT_BYTES {
return Err("Image exceeds the 4 MiB limit.".into());
}
let decoder = GeneralPurpose::new(
&alphabet::STANDARD,
GeneralPurposeConfig::new().with_decode_padding_mode(DecodePaddingMode::Indifferent),
);
let bytes = decoder
.decode(data)
.map_err(|_| "Image requires valid base64 data.")?;
normalize(&bytes, Some(mime_type))
}
pub fn from_output(root: &Path, path: &str, mime_type: &str) -> Result<Content, String> {
let relative = crate::files::logical_below(root, path)
.ok_or("Image path must be a UTF-8 path below the tool output directory.")?;
let file = scope::open_scoped(root, &relative).map_err(|e| format!("Image {relative}: {e}"))?;
normalize(&read(file)?, Some(mime_type))
}
pub fn read(file: File) -> Result<Vec<u8>, String> {
let metadata = file.metadata().map_err(|_| "Cannot inspect image file.")?;
if !metadata.is_file() {
return Err("Viewing an image requires a regular file.".into());
}
if metadata.len() == 0 || metadata.len() > IMAGE_LIMIT as u64 {
return Err("Image exceeds the 4 MiB limit or contains no data.".into());
}
let mut bytes = Vec::new();
file.take((IMAGE_LIMIT + 1) as u64)
.read_to_end(&mut bytes)
.map_err(|_| "Cannot read image file.")?;
Ok(bytes)
}
pub fn normalize(bytes: &[u8], declared: Option<&str>) -> Result<Content, String> {
if bytes.is_empty() || bytes.len() > IMAGE_LIMIT {
return Err("Image exceeds the 4 MiB limit or contains no data.".into());
}
let mime_type = mime_type(bytes)
.ok_or("Image must be PNG, JPEG or WebP; GIF, BMP and animated PNG are not supported.")?;
if declared.is_some_and(|declared| declared != mime_type) {
return Err("Image bytes do not match declared MIME type.".into());
}
Ok(Content::Image {
data: STANDARD.encode(bytes),
mime_type: mime_type.into(),
})
}
fn mime_type(bytes: &[u8]) -> Option<&'static str> {
if bytes.starts_with(JPEG_START) && bytes.get(JPEG_START.len()) != Some(&JPEG_LS_MARKER) {
Some("image/jpeg")
} else if bytes.starts_with(PNG_SIGNATURE) {
let ihdr = PNG_SIGNATURE.len()..PNG_SIGNATURE.len() + PNG_IHDR_HEADER.len();
if bytes.get(ihdr) != Some(PNG_IHDR_HEADER) {
return None;
}
let mut offset = PNG_SIGNATURE.len();
while offset < bytes.len() {
let header = bytes.get(offset..offset + PNG_CHUNK_HEADER_BYTES)?;
let (length, kind) = header.split_at(PNG_LENGTH_BYTES);
let length = u32::from_be_bytes(length.try_into().ok()?) as usize;
let end = offset
.checked_add(PNG_CHUNK_OVERHEAD_BYTES)?
.checked_add(length)?;
if end > bytes.len() || kind == PNG_ANIMATION_CONTROL {
return None;
}
if kind == PNG_END {
return Some("image/png");
}
offset = end;
}
None
} else if bytes.starts_with(RIFF_SIGNATURE) && bytes.get(RIFF_FORM_TYPE) == Some(WEBP_FORM) {
Some("image/webp")
} else {
None
}
}