Merge pull request #1937 from KenM76/fix/mesh-data-uri

craft-mesh: decode a glTF data URI's percent escapes to bytes
This commit is contained in:
Dylan M. Taylor authored and GitHub committed 2026-11-16 19:21:22 -05:00
commit 7780b4f58e
2 files changed
+38 -3

No files matched your search

+10 -3
View File
@@ -115,6 +115,11 @@ pub use crate::base64_decode;
/// Percent-decode a URI. A `%` that two hex digits do not follow stays as it is.
pub fn uri_decode(s: &str) -> String {
String::from_utf8_lossy(&percent_decode(s)).into_owned()
}
/// [`uri_decode`] as bytes, so escaped bytes that are not UTF-8 survive.
fn percent_decode(s: &str) -> Vec<u8> {
let b = s.as_bytes();
let mut out = Vec::with_capacity(b.len());
let mut i = 0;
@@ -128,7 +133,7 @@ pub fn uri_decode(s: &str) -> String {
i += 1;
}
}
String::from_utf8_lossy(&out).into_owned()
out
}
/// The media type and bytes of a `data:` URI.
@@ -137,10 +142,12 @@ fn data_uri(uri: &str) -> Option<Result<(String, Vec<u8>)>> {
Some((|| {
let (meta, data) = rest.split_once(',').ok_or_else(|| malformed(KIND, "a data URI without a comma"))?;
let mime = meta.split(';').next().unwrap_or("").to_string();
// Any character may be escaped (RFC 3986 2.1), base64's `+`, `/` and `=` included.
let bytes = percent_decode(data);
let bytes = if meta.ends_with(";base64") {
base64_decode(data).ok_or_else(|| malformed(KIND, "bad base64 in a data URI"))?
std::str::from_utf8(&bytes).ok().and_then(base64_decode).ok_or_else(|| malformed(KIND, "bad base64 in a data URI"))?
} else {
uri_decode(data).into_bytes()
bytes
};
Ok((mime, bytes))
})())
+28
View File
@@ -152,6 +152,34 @@ fn gltf_text(buffer: &str, image: &str) -> (Vec<u8>, Vec<u8>) {
(serde_json::to_vec(&doc).unwrap(), b.bin)
}
/// What a `.gltf` file holding `buffer` and `image` as URIs reads as: the image's bytes, or
/// the error.
fn embedded(buffer: &str, image: &str) -> std::result::Result<Option<Vec<u8>>, Error> {
let (doc, _) = gltf_text(buffer, image);
read(&doc, &Limits::default(), &mut |_| None).map(|s| s.images[0].data.clone())
}
/// A data URI may escape any character (RFC 3986 §2.1), base64's `+`, `/` and `=` included.
#[test]
fn percent_escapes_in_a_data_uri_are_decoded_to_bytes() {
let bin = gltf_text("", "").1;
let buffer = format!("data:application/octet-stream;base64,{}", base64(&bin));
// 0xFB 0xFF 0xBF: "+/+/" in base64.
let image = [0xFB, 0xFF, 0xBF, 0xFB, 0xFF, 0xBF];
assert_eq!(base64(&image), "+/+/+/+/");
let escaped = |s: &str| s.replace('+', "%2B").replace('/', "%2f").replace('=', "%3D");
let want = Some(image.to_vec());
let plain_image = format!("data:image/png;base64,{}", base64(&image));
let (head, body) = buffer.split_once(',').unwrap();
let every = format!("{head},{}", body.bytes().map(|c| format!("%{c:02X}")).collect::<String>());
assert_eq!(embedded(&every, &plain_image).unwrap(), want, "a buffer escaped throughout");
assert_eq!(embedded(&buffer, &escaped(&plain_image)).unwrap(), want, "an escaped image");
// Not base64: the escaped bytes as they are, high bytes too.
assert_eq!(embedded(&buffer, "data:image/png,%FB%FF%BF%FB%ff%bf").unwrap(), want);
assert_eq!(embedded(&buffer, "data:,a%20b%").unwrap(), Some(b"a b%".to_vec()));
assert_eq!(embedded(&buffer, "data:image/png;base64,%25").unwrap(), None, "an escaped % is not base64");
}
#[test]
fn embedded_and_external_resources() {
// Embedded: data URIs for the buffer and the image.