From 6bbb593d82970eda2ed87fc9cf6bd4e154f607fe Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 13:22:23 -0500 Subject: [PATCH 1/2] Use shared Base64 encoding for command payloads --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + crates/engine/Cargo.toml | 1 + crates/engine/src/cmd.rs | 31 +++++++++++++++++-------------- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a34345398..6b4a4acd7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -853,6 +853,11 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "craft-codepage" +version = "0.1.0" +source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725" + [[package]] name = "craft-fonts-build" version = "0.1.0" @@ -900,6 +905,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" @@ -1110,6 +1125,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 73c7e84aa..bc6f15c9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -49,6 +49,7 @@ 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"] } # craft-media, craft-audio and craft-video share types and stay on one rev. +craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" } craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false } craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] } # H.264, HEVC, VP9 and AV1 decoders. 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..628571edd 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,22 @@ 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); + } + 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 [ From 97423d80fdc6945f8a542eca75caa99786250c75 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 06:46:59 +0100 Subject: [PATCH 2/2] test(engine): pin the standard Base64 alphabet in the encoder test The lenient decoder accepts both alphabets, so the round trip alone would not catch a switch to the URL-safe encoder. --- crates/engine/src/cmd.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/engine/src/cmd.rs b/crates/engine/src/cmd.rs index 628571edd..f257d3282 100644 --- a/crates/engine/src/cmd.rs +++ b/crates/engine/src/cmd.rs @@ -656,6 +656,8 @@ mod tests { { 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]);