diff --git a/Cargo.lock b/Cargo.lock index d36fab678..cbe7c4c14 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -917,6 +917,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-mime" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" +dependencies = [ + "chrono", + "craft-codepage", + "encoding_rs", +] + [[package]] name = "craft-pdf" version = "0.1.0" @@ -1155,6 +1165,7 @@ name = "deckcraft-engine" version = "0.19.0" dependencies = [ "chrono", + "craft-mime", "deckcraft-anim", "deckcraft-color", "deckcraft-fonts", diff --git a/Cargo.toml b/Cargo.toml index adc2d9414..09bb17e0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -50,6 +50,8 @@ harfrust = "0.13" vello_cpu = "0.2" vello_common = { version = "0.2", default-features = false } image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] } +# Base64 for command payloads (engine). +craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" } # craft-media, craft-audio and craft-video share types and stay on one rev. # The .deckcraft package layer (identity, manifest, member names, bounded reads); crates/format maps the presentation onto it. craft-deckcraft-format = { git = "https://github.com/storytold/craft-libs", rev = "254e9a409b410dc93c0e06ff66cd1e767893ac32", version = "=0.1.0" } diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index cb1f4c29f..40569d1d6 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -9,6 +9,7 @@ description = "DeckCraft engine: sessions, undo history, selection, the command workspace = true [dependencies] +craft-mime = { workspace = true } deckcraft-geom = { workspace = true } deckcraft-color = { workspace = true } deckcraft-model = { workspace = true } diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 59160df14..f257d3282 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -607,20 +607,7 @@ fn param_optional(alt: &str) -> Option { /// Base64 (standard alphabet) for binary payloads in JSON params and results. pub fn base64_encode(data: &[u8]) -> String { - const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - let mut out = String::with_capacity(data.len().div_ceil(3) * 4); - for chunk in data.chunks(3) { - let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)]; - let n = ((b[0] as u32) << 16) | ((b[1] as u32) << 8) | b[2] as u32; - for (i, shift) in [18, 12, 6, 0].iter().enumerate() { - if i <= chunk.len() { - out.push(A.get(((n >> shift) & 63) as usize).copied().unwrap_or(b'A') as char); - } else { - out.push('='); - } - } - } - out + craft_mime::base64::STANDARD.encode(data) } pub fn base64_decode(s: &str) -> Option> { @@ -662,6 +649,24 @@ pub fn base64_decode(s: &str) -> Option> { mod tests { use super::*; + #[test] + fn shared_encoder_preserves_rfc4648_and_all_byte_remainders() { + for (input, expected) in + [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] + { + assert_eq!(base64_encode(input.as_bytes()), expected); + } + // The decoder accepts both alphabets, so pin the standard one explicitly. + assert_eq!(base64_encode(&[0xfb, 0xff]), "+/8="); + let all: Vec = (0..=255).cycle().take(513).collect(); + for len in 0..=all.len() { + let encoded = base64_encode(&all[..len]); + assert_eq!(encoded.len(), len.div_ceil(3) * 4); + assert_eq!(encoded.bytes().rev().take_while(|b| *b == b'=').count(), (3 - len % 3) % 3); + assert_eq!(base64_decode(&encoded).as_deref(), Some(&all[..len])); + } + } + #[test] fn base64_rejects_a_single_character_tail() { for (encoded, decoded) in [