Compare commits

...
Author SHA1 Message Date
Dylan M. Taylor 349d3d1c24 Integrate refreshed encoder and RTF cutovers 2026-11-15 15:02:44 -05:00
autocraft-bot[bot] f33d22001c Merge main into cutover/shared-base64 2026-11-15 19:59:20 +00:00
Dylan M. Taylor d842a0cc2a Delegate Base64 decoding while preserving command input guards 2026-11-15 14:54:50 -05:00
Dylan M. Taylor e51ff1a6a8 Merge pull request #2159 from storytold/cutover/shared-rtf-text
Reuse shared RTF text escaping and code-page decoding
2026-11-15 14:39:46 -05:00
Dylan M. Taylor 6bbb593d82 Use shared Base64 encoding for command payloads 2026-11-15 13:22:23 -05:00
Dylan M. TaylorandNikita Rakitin 187afbd4e5 Reuse shared RTF text escaping and Windows-1252 decoding
Keep outline parser and formatting policy local. Preserve exact escaping with explicit compatibility policy and exhaustive scalar/byte oracles. Original text writer and preserved oracle by Nikita Rakitin (beb883d0).

Co-authored-by: Nikita Rakitin <nnnrrr93@gmail.com>
2026-11-15 13:01:05 -05:00
6 changed files with 199 additions and 69 deletions
Generated
+31
View File
@@ -853,6 +853,16 @@ 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"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
[[package]]
name = "craft-fonts-build"
version = "0.1.0"
@@ -900,6 +910,16 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-mime"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db#0325b2d73676d736e94097dbcef01219cf25c7db"
dependencies = [
"chrono",
"craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=0325b2d73676d736e94097dbcef01219cf25c7db)",
"encoding_rs",
]
[[package]]
name = "craft-pdf"
version = "0.1.0"
@@ -909,6 +929,15 @@ dependencies = [
"thiserror 2.0.21",
]
[[package]]
name = "craft-rtf"
version = "0.1.0"
source = "git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725#63196e66f3033a6623eb629af95a104d26986725"
dependencies = [
"craft-codepage 0.1.0 (git+https://github.com/storytold/craft-libs?rev=63196e66f3033a6623eb629af95a104d26986725)",
"encoding_rs",
]
[[package]]
name = "craft-ui"
version = "0.1.0"
@@ -1110,6 +1139,7 @@ name = "deckcraft-engine"
version = "0.19.0"
dependencies = [
"chrono",
"craft-mime",
"deckcraft-anim",
"deckcraft-color",
"deckcraft-fonts",
@@ -1147,6 +1177,7 @@ dependencies = [
name = "deckcraft-format"
version = "0.19.0"
dependencies = [
"craft-rtf",
"deckcraft-model",
"serde",
"serde_json",
+2
View File
@@ -49,6 +49,8 @@ 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 = "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"] }
# H.264, HEVC, VP9 and AV1 decoders.
+1
View File
@@ -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 }
+82 -40
View File
@@ -607,61 +607,103 @@ fn param_optional(alt: &str) -> Option<bool> {
/// 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<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
[("", ""), ("f", "Zg=="), ("fo", "Zm8="), ("foo", "Zm9v"), ("foob", "Zm9vYg=="), ("fooba", "Zm9vYmE="), ("foobar", "Zm9vYmFy")]
{
assert_eq!(base64_encode(input.as_bytes()), expected);
}
let all: Vec<u8> = (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 [
+1
View File
@@ -9,6 +9,7 @@ description = "DeckCraft native file format (.deckcraft: zip of a JSON manifest
workspace = true
[dependencies]
craft-rtf = { workspace = true }
deckcraft-model = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
+82 -29
View File
@@ -10,24 +10,24 @@ use deckcraft_model::text::{Paragraph, RunKind};
/// RTF-escape `s` (non-ASCII as `\uN?`, UTF-16, signed as RTF wants).
fn escape(out: &mut String, s: &str) {
for c in s.chars() {
match c {
'\\' | '{' | '}' => {
out.push('\\');
out.push(c);
}
'\t' => out.push_str("\\tab "),
'\u{b}' | '\n' => out.push_str("\\line "),
c if c.is_ascii() && !c.is_ascii_control() => out.push(c),
c if c.is_ascii() => {}
c => {
let mut buf = [0u16; 2];
for u in c.encode_utf16(&mut buf) {
out.push_str(&format!("\\u{}?", *u as i16));
}
}
}
// Keep outline text literal: the shared writer also supports page/column breaks and
// shorthand controls that this app's deliberately small outline reader does not consume.
// Delegate ordinary spans, including UTF-16 escaping, without allocating a second string.
let mut start = 0;
for (i, c) in s.char_indices() {
let replacement = match c {
'\u{000C}' | '\u{000E}' => "",
'\u{00A0}' => "\\u160?",
'\u{00AD}' => "\\u173?",
'\u{2011}' => "\\u8209?",
'\u{2028}' => "\\u8232?",
_ => continue,
};
craft_rtf::push_esc(out, &s[start..i]);
out.push_str(replacement);
start = i + c.len_utf8();
}
craft_rtf::push_esc(out, &s[start..]);
}
fn runs(out: &mut String, p: &Paragraph) {
@@ -297,7 +297,7 @@ impl Outline<'_> {
&& let Some(h) = hex
&& h != 0
{
self.line.push(cp1252(h));
self.line.push(craft_rtf::codepage::cp1252(h));
}
}
@@ -384,22 +384,75 @@ fn finish_surrogates(s: String) -> String {
out
}
fn cp1252(b: u8) -> char {
const HIGH: [char; 32] = [
'€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–', '—',
'˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ',
];
match b {
0x80..=0x9f => HIGH[(b - 0x80) as usize],
_ => b as char,
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
// Frozen pre-cutover implementations are test oracles, not production parsers.
fn legacy_escape(out: &mut String, s: &str) {
for c in s.chars() {
match c {
'\\' | '{' | '}' => {
out.push('\\');
out.push(c);
}
'\t' => out.push_str("\\tab "),
'\u{b}' | '\n' => out.push_str("\\line "),
c if c.is_ascii() && !c.is_ascii_control() => out.push(c),
c if c.is_ascii() => {}
c => {
let mut buf = [0u16; 2];
for u in c.encode_utf16(&mut buf) {
out.push_str(&format!("\\u{}?", *u as i16));
}
}
}
}
}
fn legacy_cp1252(b: u8) -> char {
const HIGH: [char; 32] = [
'€', '\u{81}', '‚', 'ƒ', '„', '…', '†', '‡', 'ˆ', '‰', 'Š', '‹', 'Œ', '\u{8d}', 'Ž', '\u{8f}', '\u{90}', '‘', '’', '“', '”', '•', '–',
'—', '˜', '™', 'š', '›', 'œ', '\u{9d}', 'ž', 'Ÿ',
];
match b {
0x80..=0x9f => HIGH[(b - 0x80) as usize],
_ => b as char,
}
}
#[test]
fn shared_escape_preserves_every_unicode_scalar() {
for value in 0..=0x10ffff {
let Some(c) = char::from_u32(value) else { continue };
let mut buf = [0; 4];
let text = c.encode_utf8(&mut buf);
let mut expected = String::new();
let mut actual = String::new();
legacy_escape(&mut expected, text);
escape(&mut actual, text);
assert_eq!(actual, expected, "U+{value:04X}");
}
}
#[test]
fn shared_escape_preserves_mixed_spans_and_append() {
let text = "a\t中\u{a0}b\u{ad}c\u{2011}d\u{2028}e\u{c}f\u{e}g\n😀{}\\";
let mut expected = String::from("prefix:");
let mut actual = expected.clone();
legacy_escape(&mut expected, text);
escape(&mut actual, text);
assert_eq!(actual, expected);
}
#[test]
fn shared_cp1252_preserves_all_bytes() {
for b in 0..=u8::MAX {
assert_eq!(craft_rtf::codepage::cp1252(b), legacy_cp1252(b));
}
}
/// A sign in a `\'hh` escape is not a hex digit and must be ignored.
#[test]
fn hex_escape_rejects_sign() {