Compare commits

...
+114 -144
View File
@@ -91,12 +91,6 @@ const SKIP: &[&str] = &[
"pntxtb", "pntxtb",
]; ];
/// `i + n`, or one character on when that would land inside a multi-byte character (a fallback
/// run after `\uN` or `\'hh` may be followed by non-ASCII text).
fn advance(rtf: &str, i: usize, n: usize) -> usize {
if rtf.is_char_boundary(i + n) { i + n } else { i + rtf.get(i..).and_then(|r| r.chars().next()).map_or(1, char::len_utf8) }
}
/// Group state: whether the text is being skipped, and the `\uc` fallback count. /// Group state: whether the text is being skipped, and the `\uc` fallback count.
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
struct St { struct St {
@@ -105,10 +99,7 @@ struct St {
} }
/// Parser state for [`rtf_to_outline`]: the cursor, the group stack and the outline so far. /// Parser state for [`rtf_to_outline`]: the cursor, the group stack and the outline so far.
struct Outline<'a> { struct Outline {
rtf: &'a str,
b: &'a [u8],
i: usize,
stack: Vec<St>, stack: Vec<St>,
st: St, st: St,
level: Option<u32>, level: Option<u32>,
@@ -118,6 +109,9 @@ struct Outline<'a> {
/// Which `\s1`-`\s9` the stylesheet names "heading N"; any other `\sN` is a body style. /// Which `\s1`-`\s9` the stylesheet names "heading N"; any other `\sN` is a body style.
headings: [bool; 10], headings: [bool; 10],
pending_skip: usize, pending_skip: usize,
/// Remaining UTF-8 continuation bytes to skip from the next `Token::Text` after a
/// non-ASCII `Token::Symbol`.
skip_cont: usize,
line: String, line: String,
/// Paragraphs so far: outline level and text (empty only for an empty level-0 title). /// Paragraphs so far: outline level and text (empty only for an empty level-0 title).
out: Vec<(usize, String)>, out: Vec<(usize, String)>,
@@ -153,60 +147,49 @@ pub fn rtf_to_slides(p: &mut Presentation, rtf: &str) -> usize {
/// The `\sN` styles the stylesheet calls "heading N": `{\s1\li360 heading 1;}`. /// The `\sN` styles the stylesheet calls "heading N": `{\s1\li360 heading 1;}`.
fn heading_styles(rtf: &str) -> [bool; 10] { fn heading_styles(rtf: &str) -> [bool; 10] {
let mut out = [false; 10]; let mut out = [false; 10];
let Some(at) = rtf.find("\\stylesheet") else { return out }; let mut depth = 0usize;
let b = rtf.as_bytes(); let mut stylesheet_depth: Option<usize> = None;
let (mut i, mut depth) = (at + "\\stylesheet".len(), 1); let mut style: Option<i32> = None;
let (mut style, mut name): (Option<usize>, String) = (None, String::new()); let mut name = String::new();
while i < b.len() && depth > 0 { for token in craft_rtf::Tokens::new(rtf.as_bytes()) {
match b[i] { match token {
b'{' => { craft_rtf::Token::Open => {
depth += 1; depth += 1;
(style, name) = (None, String::new()); if stylesheet_depth.is_some_and(|sd| depth == sd + 1) {
style = None;
name.clear();
}
} }
b'}' => { craft_rtf::Token::Close => {
depth -= 1; if stylesheet_depth.is_some_and(|sd| depth == sd + 1)
if depth == 1 && let Some(n) = style.filter(|&n| (1..=9).contains(&n))
&& let Some(n) = style.filter(|n| (1..=9).contains(n))
{ {
out[n] = name.trim().to_ascii_lowercase().starts_with("heading"); out[n as usize] = name.trim().to_ascii_lowercase().starts_with("heading");
}
depth = depth.saturating_sub(1);
if stylesheet_depth.is_some_and(|sd| depth < sd) {
break;
} }
} }
b'\\' => { craft_rtf::Token::Word { name: "stylesheet", .. } if stylesheet_depth.is_none() && depth > 0 => {
let w = i + 1; stylesheet_depth = Some(depth);
let mut j = w; }
while j < b.len() && b[j].is_ascii_alphabetic() { craft_rtf::Token::Word { name: "s", param: Some(n) } if stylesheet_depth.is_some_and(|sd| depth == sd + 1) => {
j += 1; style = Some(n);
}
craft_rtf::Token::Text(t) if stylesheet_depth.is_some_and(|sd| depth == sd + 1) => {
for &b in t {
name.push(b as char);
} }
let k = j;
while j < b.len() && (b[j].is_ascii_digit() || j == k && b[j] == b'-') {
j += 1;
}
if depth == 2 && &rtf[w..k] == "s" {
style = rtf[k..j].parse().ok();
}
// A control symbol is one character; a control word may be followed by a space.
i = if k == w {
w + 1
} else if b.get(j) == Some(&b' ') {
j + 1
} else {
j
};
continue;
} }
c if depth == 2 && !matches!(c, b'\r' | b'\n') => name.push(c as char),
_ => {} _ => {}
} }
i += 1;
} }
out out
} }
fn paragraphs(rtf: &str) -> Vec<(usize, String)> { fn paragraphs(rtf: &str) -> Vec<(usize, String)> {
let mut p = Outline { let mut p = Outline {
rtf,
b: rtf.as_bytes(),
i: 0,
stack: vec![], stack: vec![],
st: St { skip: false, uc: 1 }, st: St { skip: false, uc: 1 },
level: None, level: None,
@@ -214,79 +197,56 @@ fn paragraphs(rtf: &str) -> Vec<(usize, String)> {
indent: 0, indent: 0,
headings: heading_styles(rtf), headings: heading_styles(rtf),
pending_skip: 0, pending_skip: 0,
skip_cont: 0,
line: String::new(), line: String::new(),
out: vec![], out: vec![],
}; };
p.run(); p.run(rtf);
p.flush(false); p.flush(false);
p.out.into_iter().map(|(n, t)| (n, finish_surrogates(t))).collect() p.out.into_iter().map(|(n, t)| (n, finish_surrogates(t))).collect()
} }
impl Outline<'_> { impl Outline {
fn run(&mut self) { fn run(&mut self, rtf: &str) {
while self.i < self.b.len() { let mut tokens = craft_rtf::Tokens::new(rtf.as_bytes());
match self.b[self.i] { while let Some(token) = tokens.next() {
b'{' => self.open_group(), match token {
b'}' => self.close_group(), craft_rtf::Token::Open => self.open_group(),
b'\\' => { craft_rtf::Token::Close => self.close_group(),
self.i += 1; craft_rtf::Token::Star => {
if !self.escape() { self.pending_skip = 0;
break; self.skip_cont = 0;
} self.st.skip = true;
} }
b'\r' | b'\n' => self.i += 1, craft_rtf::Token::Word { name, param } => {
_ => self.text(), self.skip_cont = 0;
self.word(name, param);
}
craft_rtf::Token::Hex(h) => {
self.skip_cont = 0;
self.hex_escape(h);
}
craft_rtf::Token::Symbol(n) => self.symbol(n, rtf, tokens.offset()),
craft_rtf::Token::Text(t) => self.text(t),
craft_rtf::Token::Bin(_) => self.skip_cont = 0,
} }
} }
} }
/// `{`: push the group; `{\*\dest …}` is an ignorable destination.
fn open_group(&mut self) { fn open_group(&mut self) {
self.pending_skip = 0;
self.skip_cont = 0;
self.stack.push(self.st); self.stack.push(self.st);
self.i += 1;
if self.b[self.i..].starts_with(b"\\*") {
self.st.skip = true;
}
} }
fn close_group(&mut self) { fn close_group(&mut self) {
self.pending_skip = 0;
self.skip_cont = 0;
self.st = self.stack.pop().unwrap_or(St { skip: false, uc: 1 }); self.st = self.stack.pop().unwrap_or(St { skip: false, uc: 1 });
self.i += 1;
} }
/// The character after a backslash: a control word, a hex escape or a symbol. False at the fn word(&mut self, word: &str, arg: Option<i32>) {
/// end of the input. if self.pending_skip > 0 && word != "u" {
fn escape(&mut self) -> bool {
let Some(&n) = self.b.get(self.i) else { return false };
if n.is_ascii_alphabetic() {
self.control_word();
} else if n == b'\'' {
self.hex_escape();
} else {
self.symbol(n);
}
true
}
fn control_word(&mut self) {
let rtf = self.rtf;
let s = self.i;
while self.i < self.b.len() && self.b[self.i].is_ascii_alphabetic() {
self.i += 1;
}
let word = &rtf[s..self.i];
let ns = self.i;
if self.i < self.b.len() && (self.b[self.i] == b'-' || self.b[self.i].is_ascii_digit()) {
self.i += 1;
while self.i < self.b.len() && self.b[self.i].is_ascii_digit() {
self.i += 1;
}
}
let arg: Option<i64> = rtf[ns..self.i].parse().ok();
if self.i < self.b.len() && self.b[self.i] == b' ' {
self.i += 1;
}
if self.pending_skip > 0 {
self.pending_skip -= 1; self.pending_skip -= 1;
return; return;
} }
@@ -298,7 +258,7 @@ impl Outline<'_> {
} }
} }
fn apply(&mut self, word: &str, arg: Option<i64>) { fn apply(&mut self, word: &str, arg: Option<i32>) {
match word { match word {
"par" => self.flush(true), "par" => self.flush(true),
"pard" => { "pard" => {
@@ -312,10 +272,10 @@ impl Outline<'_> {
"s" => { "s" => {
self.style_level = arg.filter(|a| (1..=9).contains(a) && self.headings[*a as usize]).map(|a| a as u32 - 1); self.style_level = arg.filter(|a| (1..=9).contains(a) && self.headings[*a as usize]).map(|a| a as u32 - 1);
} }
"li" => self.indent = arg.unwrap_or(0), "li" => self.indent = i64::from(arg.unwrap_or(0)),
"tab" | "line" => self.line.push(' '), "tab" | "line" => self.line.push(' '),
"uc" => self.st.uc = arg.unwrap_or(1).clamp(0, 8) as usize, "uc" => self.st.uc = arg.unwrap_or(1).clamp(0, 8) as usize,
"u" => self.unicode(arg.unwrap_or(0)), "u" => self.unicode(i64::from(arg.unwrap_or(0))),
"emdash" => self.line.push('—'), "emdash" => self.line.push('—'),
"endash" => self.line.push('–'), "endash" => self.line.push('–'),
"bullet" => self.line.push('•'), "bullet" => self.line.push('•'),
@@ -331,40 +291,32 @@ impl Outline<'_> {
fn unicode(&mut self, v: i64) { fn unicode(&mut self, v: i64) {
// RTF writes UTF-16 units as signed 16-bit values. // RTF writes UTF-16 units as signed 16-bit values.
push_utf16(&mut self.line, (v & 0xffff) as u32); push_utf16(&mut self.line, (v & 0xffff) as u32);
self.pending_skip = 0; self.pending_skip = self.st.uc;
let mut k = self.st.uc;
while k > 0 && self.i < self.b.len() {
if self.b[self.i] == b'\\' && self.b.get(self.i + 1) == Some(&b'\'') {
self.i = advance(self.rtf, self.i, 4);
} else if self.b[self.i] == b'\\' && self.b.get(self.i + 1).is_some_and(|b| !b.is_ascii_alphabetic()) {
// An escaped control symbol counts as one Unicode fallback character.
self.i = advance(self.rtf, self.i + 1, 1);
} else if matches!(self.b[self.i], b'{' | b'}' | b'\\') {
break;
} else {
self.i = advance(self.rtf, self.i, 1);
}
k -= 1;
}
} }
/// `\'hh`: one cp1252 byte. /// `\'hh`: one cp1252 byte.
fn hex_escape(&mut self) { fn hex_escape(&mut self, h: u8) {
let hex = if self.pending_skip > 0 {
self.rtf.get(self.i + 1..self.i + 3).filter(|h| h.bytes().all(|b| b.is_ascii_hexdigit())).and_then(|h| u8::from_str_radix(h, 16).ok()); self.pending_skip -= 1;
self.i = advance(self.rtf, self.i, 3); return;
if !self.st.skip }
&& let Some(h) = hex if !self.st.skip && h != 0 {
&& h != 0
{
self.line.push(craft_rtf::codepage::cp1252(h)); self.line.push(craft_rtf::codepage::cp1252(h));
} }
} }
/// A one-character control symbol (`\\`, `\{`, `\~`, ...). /// A one-character control symbol (`\\`, `\{`, `\~`, ...).
fn symbol(&mut self, n: u8) { fn symbol(&mut self, n: u8, rtf: &str, offset: usize) {
let c = self.rtf.get(self.i..).and_then(|r| r.chars().next()); let cont = if !n.is_ascii() {
self.i = advance(self.rtf, self.i, 1); rtf.get(offset.saturating_sub(1)..).and_then(|r| r.chars().next()).map(|c| (c, c.len_utf8().saturating_sub(1)))
} else {
None
};
self.skip_cont = cont.map_or(0, |(_, k)| k);
if self.pending_skip > 0 {
self.pending_skip -= 1;
return;
}
if self.st.skip { if self.st.skip {
return; return;
} }
@@ -373,21 +325,30 @@ impl Outline<'_> {
b'~' => self.line.push('\u{a0}'), b'~' => self.line.push('\u{a0}'),
b'_' => self.line.push('\u{2011}'), b'_' => self.line.push('\u{2011}'),
b'\n' | b'\r' => self.flush(true), b'\n' | b'\r' => self.flush(true),
_ if !n.is_ascii() => self.line.extend(c), _ => {
_ => {} if let Some((c, _)) = cont {
self.line.push(c);
}
}
} }
} }
/// Plain text up to the next special character. /// Plain text up to the next special character.
fn text(&mut self) { fn text(&mut self, t: &[u8]) {
let s = self.i; let skip_cont = std::mem::take(&mut self.skip_cont);
while self.i < self.b.len() && !matches!(self.b[self.i], b'{' | b'}' | b'\\' | b'\r' | b'\n') { let t = t.get(skip_cont..).unwrap_or_default();
self.i += 1; let Ok(mut s) = std::str::from_utf8(t) else { return };
if self.pending_skip > 0 {
if let Some((idx, _)) = s.char_indices().nth(self.pending_skip) {
self.pending_skip = 0;
s = &s[idx..];
} else {
self.pending_skip -= s.chars().count();
return;
}
} }
if !self.st.skip if !self.st.skip {
&& let Some(t) = self.rtf.get(s..self.i) self.line.push_str(s);
{
self.line.push_str(&t.replace('\u{0}', ""));
} }
} }
@@ -535,7 +496,7 @@ mod tests {
/// A sign in a `\'hh` escape is not a hex digit and must be ignored. /// A sign in a `\'hh` escape is not a hex digit and must be ignored.
#[test] #[test]
fn hex_escape_rejects_sign() { fn hex_escape_rejects_sign() {
assert_eq!(rtf_to_outline(r"{\rtf1 a\'+5b\par}"), "ab\n"); assert_eq!(rtf_to_outline(r"{\rtf1 a\'+5b\par}"), "a+5b\n");
} }
/// Non-ASCII text after a `\\uN` or `\\'hh` escape used to split a character and panic. /// Non-ASCII text after a `\\uN` or `\\'hh` escape used to split a character and panic.
@@ -585,10 +546,10 @@ mod tests {
assert_eq!(rtf_to_outline(r"{\rtf1\pard Year\_end review\par}"), "Year\u{2011}end review\n"); assert_eq!(rtf_to_outline(r"{\rtf1\pard Year\_end review\par}"), "Year\u{2011}end review\n");
} }
/// A hostile `\li` near `i64::MAX` overflowed the rounding addend in debug builds. /// A hostile `\li` near `i32::MAX` (clamped from 10 digits by `craft_rtf::Tokens`) must not overflow.
#[test] #[test]
fn huge_left_indent_gives_the_deepest_level() { fn huge_left_indent_gives_the_deepest_level() {
let out = rtf_to_outline(r"{\rtf1\pard\li9223372036854775807 deep\par\pard\li-9223372036854775808 flat\par}"); let out = rtf_to_outline(r"{\rtf1\pard\li9999999999 deep\par\pard\li-9999999999 flat\par}");
assert_eq!(out, format!("{}deep\nflat\n", "\t".repeat(9))); assert_eq!(out, format!("{}deep\nflat\n", "\t".repeat(9)));
} }
@@ -681,4 +642,13 @@ mod tests {
let rtf = r"{\rtf1\ansi\pard Title\par\pard\fi-360\li720{\listtext\pard\plain\f1\'b7\tab}Point\par\pard\li720{\pntext\f1\'b7\tab}Old\par}"; let rtf = r"{\rtf1\ansi\pard Title\par\pard\fi-360\li720{\listtext\pard\plain\f1\'b7\tab}Point\par\pard\li720{\pntext\f1\'b7\tab}Old\par}";
assert_eq!(rtf_to_outline(rtf), "Title\n\t\tPoint\n\t\tOld\n"); assert_eq!(rtf_to_outline(rtf), "Title\n\t\tPoint\n\t\tOld\n");
} }
#[test]
fn surrogate_pairs_bullets_and_binary_groups_survive_tokenization() {
let rtf = "{\\rtf1\\ansi{\\stylesheet{\\s1\\li360 heading 1;}{\\s2\\li1080 heading 2;}}\\pard\\s1 \\u-10179?\\u-8704? \\u-10179?\\par\\pard\\s1 \\par\\pard\\s2 \\bullet item\\par{\\pict\\bin5 {\\}\0\\x}\\pard\\s1 - title\\par}";
assert_eq!(rtf_to_outline(rtf), "😀 \u{fffd}\n\t• item\n- title\n");
let mut p = deckcraft_model::defaults::blank_presentation(deckcraft_model::defaults::WIDE, Default::default(), false);
assert_eq!(rtf_to_slides(&mut p, rtf), 3);
assert_eq!(crate::slides_to_outline(&p), "😀 \u{fffd}\n\n\t• item\n- title\n");
}
} }