mirror of
https://github.com/storytold/deckcraft.git
synced 2026-11-15 18:30:42 -06:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
349d3d1c24 | ||
|
|
d842a0cc2a |
Generated
+8
-3
@@ -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=0325b2d73676d736e94097dbcef01219cf25c7db#0325b2d73676d736e94097dbcef01219cf25c7db"
|
||||
|
||||
[[package]]
|
||||
name = "craft-codepage"
|
||||
version = "0.1.0"
|
||||
@@ -908,10 +913,10 @@ dependencies = [
|
||||
[[package]]
|
||||
name = "craft-mime"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db#0325b2d73676d736e94097dbcef01219cf25c7db"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"craft-codepage",
|
||||
"craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db)",
|
||||
"encoding_rs",
|
||||
]
|
||||
|
||||
@@ -929,7 +934,7 @@ name = "craft-rtf"
|
||||
version = "0.1.0"
|
||||
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
|
||||
dependencies = [
|
||||
"craft-codepage",
|
||||
"craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725)",
|
||||
"encoding_rs",
|
||||
]
|
||||
|
||||
|
||||
+1
-1
@@ -49,7 +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-mime = { git = "https://github.com/storytold/craft-libs", rev = "0325b2d73676d736e94097dbcef01219cf25c7db", version = "=0.1.0" }
|
||||
craft-rtf = { 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"] }
|
||||
|
||||
+65
-26
@@ -612,43 +612,82 @@ pub fn base64_encode(data: &[u8]) -> String {
|
||||
|
||||
pub fn base64_decode(s: &str) -> Option<Vec<u8>> {
|
||||
let s = s.split_once("base64,").map(|(_, b)| b).unwrap_or(s);
|
||||
let mut out = Vec::with_capacity(s.len() / 4 * 3);
|
||||
let mut acc = 0u32;
|
||||
let mut bits = 0;
|
||||
let mut padded = false;
|
||||
// Preserve the command transport's admission rules before the shared legacy decoder:
|
||||
// padding ends the payload and one trailing symbol cannot encode a byte.
|
||||
let (mut symbols_mod_four, mut padded) = (0u8, false);
|
||||
for c in s.bytes() {
|
||||
let v = match c {
|
||||
b'A'..=b'Z' => c - b'A',
|
||||
b'a'..=b'z' => c - b'a' + 26,
|
||||
b'0'..=b'9' => c - b'0' + 52,
|
||||
b'+' | b'-' => 62,
|
||||
b'/' | b'_' => 63,
|
||||
b'=' => {
|
||||
padded = true;
|
||||
continue;
|
||||
match c {
|
||||
b'=' => padded = true,
|
||||
b'\n' | b'\r' | b' ' => {}
|
||||
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'+' | b'-' | b'/' | b'_' if !padded => {
|
||||
symbols_mod_four = (symbols_mod_four + 1) % 4;
|
||||
}
|
||||
b'\n' | b'\r' | b' ' => continue,
|
||||
_ => return None,
|
||||
} as u32;
|
||||
// Padding ends the payload; no further data may be decoded.
|
||||
if padded {
|
||||
return None;
|
||||
}
|
||||
acc = (acc << 6) | v;
|
||||
bits += 6;
|
||||
if bits >= 8 {
|
||||
bits -= 8;
|
||||
out.push(((acc >> bits) & 0xff) as u8);
|
||||
}
|
||||
}
|
||||
// A trailing lone Base64 character cannot encode another byte.
|
||||
(bits != 6).then_some(out)
|
||||
if symbols_mod_four == 1 {
|
||||
return None;
|
||||
}
|
||||
craft_mime::base64_compat::decode(s).ok()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
// Frozen pre-cutover implementation (Brandon Thomas, with Mex's padding/tail fixes).
|
||||
fn previous_base64_decode(s: &str) -> Option<Vec<u8>> {
|
||||
let s = s.split_once("base64,").map(|(_, b)| b).unwrap_or(s);
|
||||
let mut out = Vec::with_capacity(s.len() / 4 * 3);
|
||||
let mut acc = 0u32;
|
||||
let mut bits = 0;
|
||||
let mut padded = false;
|
||||
for c in s.bytes() {
|
||||
let v = match c {
|
||||
b'A'..=b'Z' => c - b'A',
|
||||
b'a'..=b'z' => c - b'a' + 26,
|
||||
b'0'..=b'9' => c - b'0' + 52,
|
||||
b'+' | b'-' => 62,
|
||||
b'/' | b'_' => 63,
|
||||
b'=' => {
|
||||
padded = true;
|
||||
continue;
|
||||
}
|
||||
b'\n' | b'\r' | b' ' => continue,
|
||||
_ => return None,
|
||||
} as u32;
|
||||
// Padding ends the payload; no further data may be decoded.
|
||||
if padded {
|
||||
return None;
|
||||
}
|
||||
acc = (acc << 6) | v;
|
||||
bits += 6;
|
||||
if bits >= 8 {
|
||||
bits -= 8;
|
||||
out.push(((acc >> bits) & 0xff) as u8);
|
||||
}
|
||||
}
|
||||
// A trailing lone Base64 character cannot encode another byte.
|
||||
(bits != 6).then_some(out)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_decoder_preserves_command_transport_admission() {
|
||||
for text in ["", "=", "Zg== \r\n==", "Zg==A", "A", "AAAAA", "AB", "+_8=", "Z=m9v", "Zm9v\t", "Zm9v!", "data:text/plain;base64,Zm9v"] {
|
||||
assert_eq!(base64_decode(text), previous_base64_decode(text), "{text:?}");
|
||||
}
|
||||
let alphabet = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/-_= \r\n!\t";
|
||||
let mut seed = 0x1234_5678u32;
|
||||
for n in 0..50_000 {
|
||||
let mut text = String::new();
|
||||
for _ in 0..n % 65 {
|
||||
seed = seed.wrapping_mul(1_664_525).wrapping_add(1_013_904_223);
|
||||
text.push(char::from(alphabet[(seed as usize) % alphabet.len()]));
|
||||
}
|
||||
assert_eq!(base64_decode(&text), previous_base64_decode(&text), "{text:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shared_encoder_preserves_rfc4648_and_all_byte_remainders() {
|
||||
for (input, expected) in
|
||||
|
||||
Reference in New Issue
Block a user