Skip to content

Commit 6bbb593

Browse files
committed
Use shared Base64 encoding for command payloads
1 parent 1af5712 commit 6bbb593

4 files changed

Lines changed: 35 additions & 14 deletions

File tree

‎Cargo.lock‎

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ vello_cpu = "0.2"
4949
vello_common = { version = "0.2", default-features = false }
5050
image = { version = "0.25", default-features = false, features = ["png", "jpeg", "webp", "gif", "tiff", "bmp"] }
5151
# craft-media, craft-audio and craft-video share types and stay on one rev.
52+
craft-mime = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0" }
5253
craft-media = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false }
5354
craft-audio = { git = "https://github.com/storytold/craft-libs", rev = "63196e66f3033a6623eb629af95a104d26986725", version = "=0.1.0", default-features = false, features = ["opus-decode"] }
5455
# H.264, HEVC, VP9 and AV1 decoders.

‎crates/engine/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ description = "DeckCraft engine: sessions, undo history, selection, the command
99
workspace = true
1010

1111
[dependencies]
12+
craft-mime = { workspace = true }
1213
deckcraft-geom = { workspace = true }
1314
deckcraft-color = { workspace = true }
1415
deckcraft-model = { workspace = true }

‎crates/engine/src/cmd.rs‎

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -607,20 +607,7 @@ fn param_optional(alt: &str) -> Option<bool> {
607607

608608
/// Base64 (standard alphabet) for binary payloads in JSON params and results.
609609
pub fn base64_encode(data: &[u8]) -> String {
610-
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
611-
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
612-
for chunk in data.chunks(3) {
613-
let b = [chunk.first().copied().unwrap_or(0), chunk.get(1).copied().unwrap_or(0), chunk.get(2).copied().unwrap_or(0)];
614-
let n = ((b[0] as u32) << 16) | ((b[1] as u32) << 8) | b[2] as u32;
615-
for (i, shift) in [18, 12, 6, 0].iter().enumerate() {
616-
if i <= chunk.len() {
617-
out.push(A.get(((n >> shift) & 63) as usize).copied().unwrap_or(b'A') as char);
618-
} else {
619-
out.push('=');
620-
}
621-
}
622-
}
623-
out
610+
craft_mime::base64::STANDARD.encode(data)
624611
}
625612

626613
pub fn base64_decode(s: &str) -> Option<Vec<u8>> {
@@ -662,6 +649,22 @@ pub fn base64_decode(s: &str) -> Option<Vec<u8>> {
662649
mod tests {
663650
use super::*;
664651

652+
#[test]
653+
fn shared_encoder_preserves_rfc4648_and_all_byte_remainders() {
654+
for (input, expected) in
655+
[("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")]
656+
{
657+
assert_eq!(base64_encode(input.as_bytes()), expected);
658+
}
659+
let all: Vec<u8> = (0..=255).cycle().take(513).collect();
660+
for len in 0..=all.len() {
661+
let encoded = base64_encode(&all[..len]);
662+
assert_eq!(encoded.len(), len.div_ceil(3) * 4);
663+
assert_eq!(encoded.bytes().rev().take_while(|b| *b == b'=').count(), (3 - len % 3) % 3);
664+
assert_eq!(base64_decode(&encoded).as_deref(), Some(&all[..len]));
665+
}
666+
}
667+
665668
#[test]
666669
fn base64_rejects_a_single_character_tail() {
667670
for (encoded, decoded) in [

0 commit comments

Comments
 (0)