encoding: reuse shared Base64 writers across four consumers

This commit is contained in:
Dylan M. Taylor
2026-11-15 13:19:05 -05:00
parent d6c9ad6643
commit ac1c1d9202
10 changed files with 40 additions and 48 deletions
Generated
+19
View File
@@ -1103,6 +1103,11 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-codepage"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9"
[[package]]
name = "craft-doc"
version = "0.1.0"
@@ -1220,6 +1225,16 @@ dependencies = [
"serde_json",
]
[[package]]
name = "craft-mime"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=47107f660191de2f0b425569506f4daa4faadac9#47107f660191de2f0b425569506f4daa4faadac9"
dependencies = [
"chrono",
"craft-codepage",
"encoding_rs",
]
[[package]]
name = "craft-pcraft-format"
version = "0.1.0"
@@ -1604,6 +1619,7 @@ name = "effectcraft-aep"
version = "0.24.1"
dependencies = [
"craft-aep",
"craft-mime",
"effectcraft-color",
"effectcraft-effects",
"effectcraft-keyframe",
@@ -1619,6 +1635,7 @@ dependencies = [
name = "effectcraft-automation"
version = "0.24.1"
dependencies = [
"craft-mime",
"effectcraft-engine",
"effectcraft-host",
"effectcraft-model",
@@ -2116,6 +2133,7 @@ dependencies = [
name = "effectcraft-track"
version = "0.24.1"
dependencies = [
"craft-mime",
"effectcraft-raster",
"effectcraft-segment",
"rayon",
@@ -2130,6 +2148,7 @@ version = "0.24.1"
dependencies = [
"bytemuck",
"chrono",
"craft-mime",
"craft-quit",
"craft-ui",
"craft-update",
+1
View File
@@ -11,6 +11,7 @@ rust-version = "1.95"
repository = "https://github.com/storytold/effectcraft"
[workspace.dependencies]
craft-mime = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-pdf = { git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9", version = "=0.1.0", default-features = false, features = ["read"] }
craft-docpath = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
craft-quit = { version = "0.1.0", git = "https://github.com/storytold/craft-libs", rev = "47107f660191de2f0b425569506f4daa4faadac9" }
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
craft-aep = { workspace = true }
effectcraft-time = { workspace = true }
effectcraft-color = { workspace = true }
+1 -13
View File
@@ -224,19 +224,7 @@ pub(crate) fn profile_json(icc: &[u8], name: &str) -> String {
/// Standard base64 with padding.
pub(crate) fn base64(data: &[u8]) -> String {
const T: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let n = (u32::from(c[0]) << 16) | (u32::from(c.get(1).copied().unwrap_or(0)) << 8) | u32::from(c.get(2).copied().unwrap_or(0));
for (i, shift) in [18u32, 12, 6, 0].iter().enumerate() {
if i <= c.len() {
out.push(char::from(T[((n >> shift) & 63) as usize]));
} else {
out.push('=');
}
}
}
out
craft_mime::base64::STANDARD.encode(data)
}
#[cfg(test)]
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
effectcraft-engine = { workspace = true }
serde_json = { workspace = true }
thiserror = { workspace = true }
+13 -10
View File
@@ -1,16 +1,7 @@
//! Standard base64 (RFC 4648 §4, padded): MCP image content and the control channel's inline PNGs.
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn encode(data: &[u8]) -> String {
let mut s = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32;
for i in 0..4 {
s.push(if i <= c.len() { A[(n >> (18 - 6 * i) & 63) as usize] as char } else { '=' });
}
}
s
craft_mime::base64::STANDARD.encode(data)
}
/// Decode, ignoring ASCII whitespace; `None` on any other invalid input.
@@ -48,6 +39,18 @@ pub fn decode(s: &str) -> Option<Vec<u8>> {
#[cfg(test)]
mod tests {
#[test]
fn shared_encoder_round_trips_every_byte_and_remainder() {
let all: Vec<u8> = (0..=255).cycle().take(513).collect();
for length in 0..=all.len() {
let input = &all[..length];
let encoded = super::encode(input);
assert_eq!(encoded.len(), length.div_ceil(3) * 4);
assert_eq!(super::decode(&encoded).as_deref(), Some(input));
assert_eq!(encoded.bytes().rev().take_while(|b| *b == b'=').count(), (3 - length % 3) % 3);
}
}
#[test]
fn rfc4648_vectors() {
for (i, o) in [("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")] {
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
effectcraft-raster = { workspace = true }
effectcraft-segment = { workspace = true }
rayon = { workspace = true }
+1 -12
View File
@@ -1,19 +1,8 @@
//! Compact text encodings of mattes for project storage: run-length coding (LEB128 varints)
//! wrapped in standard base64 (RFC 4648 §4).
const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
pub fn base64_encode(data: &[u8]) -> String {
let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let b = [c[0], *c.get(1).unwrap_or(&0), *c.get(2).unwrap_or(&0)];
let n = (b[0] as u32) << 16 | (b[1] as u32) << 8 | b[2] as u32;
out.push(B64[(n >> 18) as usize & 63] as char);
out.push(B64[(n >> 12) as usize & 63] as char);
out.push(if c.len() > 1 { B64[(n >> 6) as usize & 63] as char } else { '=' });
out.push(if c.len() > 2 { B64[n as usize & 63] as char } else { '=' });
}
out
craft_mime::base64::STANDARD.encode(data)
}
pub fn base64_decode(s: &str) -> Option<Vec<u8>> {
+1
View File
@@ -10,6 +10,7 @@ rust-version.workspace = true
workspace = true
[dependencies]
craft-mime = { workspace = true }
craft-ui = { workspace = true }
craft-quit = { workspace = true, features = ["egui"] }
effectcraft-time = { workspace = true }
+1 -13
View File
@@ -803,19 +803,7 @@ fn render_frame(app: &EffectcraftApp, p: &Value) -> Outcome {
/// Standard base64 (RFC 4648, padded).
pub fn base64(data: &[u8]) -> String {
const A: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
let mut s = String::with_capacity(data.len().div_ceil(3) * 4);
for c in data.chunks(3) {
let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32;
for i in 0..4 {
if i <= c.len() {
s.push(A[(n >> (18 - 6 * i) & 63) as usize] as char);
} else {
s.push('=');
}
}
}
s
craft_mime::base64::STANDARD.encode(data)
}
/// Builds the `ui.inspect` reply: window size and pixels per point, fps, UI state with the