Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 87 additions & 56 deletions src/ansi_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
//! either reading finds counts.
//!
//! Used by the two such readers:
//! - `permissions::rules` — `split_into`, `substitution`, `words` and
//! `unquote`: which commands a line holds, for the allow / ask / deny rules.
//! - `guards::shell` — `lex` and `Lexer::substitution`: which files a command
//! writes, for the protected-path guard.
//! - `permissions::rules` — `split::Splitter::dollar`,
//! `split::ParenScan::unquoted`, `peel::words` and `peel::unquote`: which
//! commands a line holds, for the allow / ask / deny rules.
//! - `guards::shell` — `lex::Lexer::dollar` and `lex::Lexer::skipped`: which
//! files a command writes, for the protected-path guard.
//!
//! `$"…"` (a double-quoted string translated by the locale) needs nothing
//! here: past its `$` it is read exactly as `"…"`.
Expand Down Expand Up @@ -55,65 +56,91 @@ fn decode(body: &[char]) -> String {
break;
};
i += 1;
let code = match escaped {
'a' => 0x07,
'b' => 0x08,
'e' | 'E' => 0x1b,
'f' => 0x0c,
'n' => u32::from('\n'),
'r' => u32::from('\r'),
't' => u32::from('\t'),
'v' => 0x0b,
'\\' | '\'' | '"' | '?' => u32::from(escaped),
// `\nnn`: one to three octal digits, the first already read.
'0'..='7' => {
let (rest, used) = digits(body, i, 8, 2);
i += used;
let first = escaped.to_digit(8).unwrap_or(0);
(first * 8u32.pow(used as u32) + rest) & 0xff
}
'x' | 'u' | 'U' => {
let most = match escaped {
'x' => 2,
'u' => 4,
_ => 8,
};
let (value, used) = digits(body, i, 16, most);
if used == 0 {
// No digits: bash keeps the escape as written.
out.push('\\');
out.push(escaped);
continue;
}
i += used;
value
}
// `\cx`: the control character of the next character, whatever
// it is (`\c\` is 0x1c).
'c' => match body.get(i) {
Some(&x) => {
i += 1;
u32::from(x) & 0x1f
}
None => {
out.push_str("\\c");
continue;
}
},
other => {
let (decoded, used) = escape(escaped, body, i);
i += used;
match decoded {
Escape::Kept => {
out.push('\\');
out.push(other);
continue;
out.push(escaped);
}
Escape::Code(0) => break,
Escape::Code(code) => {
out.push(char::from_u32(code).unwrap_or(char::REPLACEMENT_CHARACTER));
}
};
if code == 0 {
break;
}
out.push(char::from_u32(code).unwrap_or(char::REPLACEMENT_CHARACTER));
}
out
}

/// What one escape decodes to.
enum Escape {
/// A character code; 0 ends the text.
Code(u32),
/// Not an escape bash decodes: the backslash and the character stay.
Kept,
}

/// The escape `\<escaped>` whose operand, if it takes one, starts at
/// `at`: what it decodes to and how many characters of operand it used.
///
/// Called by [`decode`] for every backslash with a character after it.
fn escape(escaped: char, body: &[char], at: usize) -> (Escape, usize) {
match escaped {
// `\nnn`: one to three octal digits, the first already read.
'0'..='7' => {
let (rest, used) = digits(body, at, 8, 2);
let first = escaped.to_digit(8).unwrap_or(0);
(
Escape::Code((first * 8u32.pow(used as u32) + rest) & 0xff),
used,
)
}
'x' | 'u' | 'U' => hex(escaped, body, at),
// `\cx`: the control character of the next character, whatever
// it is (`\c\` is 0x1c).
'c' => match body.get(at) {
Some(&x) => (Escape::Code(u32::from(x) & 0x1f), 1),
None => (Escape::Kept, 0),
},
_ => (named(escaped).map_or(Escape::Kept, Escape::Code), 0),
}
}

/// `\xHH`, `\uHHHH` or `\UHHHHHHHH`: up to that many hex digits. With none,
/// bash keeps the escape as written.
///
/// Called by [`escape`].
fn hex(escaped: char, body: &[char], at: usize) -> (Escape, usize) {
let most = match escaped {
'x' => 2,
'u' => 4,
_ => 8,
};
match digits(body, at, 16, most) {
(_, 0) => (Escape::Kept, 0),
(value, used) => (Escape::Code(value), used),
}
}

/// The code of a one-letter escape (`\n`, `\t`, `\\`, …), if it is one.
///
/// Called by [`escape`].
fn named(escaped: char) -> Option<u32> {
let code = match escaped {
'a' => 0x07,
'b' => 0x08,
'e' | 'E' => 0x1b,
'f' => 0x0c,
'n' => u32::from('\n'),
'r' => u32::from('\r'),
't' => u32::from('\t'),
'v' => 0x0b,
'\\' | '\'' | '"' | '?' => u32::from(escaped),
_ => return None,
};
Some(code)
}

/// Up to `most` digits in `radix` from `start`: their value and how many.
fn digits(chars: &[char], start: usize, radix: u32, most: usize) -> (u32, usize) {
let mut value = 0u32;
Expand Down Expand Up @@ -202,3 +229,7 @@ mod tests {
assert_eq!(decode("abc"), ("abc".to_string(), 3));
}
}

#[cfg(test)]
#[path = "ansi_c_pinned_tests.rs"]
mod pinned_tests;
126 changes: 126 additions & 0 deletions src/ansi_c_pinned_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
//! What [`super::quoted`] makes of every `$'…'` body below, as it was recorded
//! before the decoder was restructured: the decoded text and where the string
//! ends.

use super::quoted;
use crate::parser_pins::{assert_pinned, esc};

const RECORDING: &str = include_str!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/pinned/ansi_c.txt"
));

/// Bodies read whole: the module's own tests, and the edges of each escape.
const BODIES: &[&str] = &[
r"\'' ; rm",
r"it\'s' tail",
r"\c' ; rm",
r"\c\'' ; rm",
r"\x' ; rm",
r"\1' ; rm",
r"rm\0x' tail",
r"rm\x00x' tail",
r"rm\000x' tail",
r"rm\c@x' tail",
concat!("rm", "\\", "u0000x' tail"),
r"a\tb'",
r"\x72m'",
r"\x7'",
r"\162m'",
r"\0'",
concat!("\\", "u0072m'"),
r"\U00000072m'",
r"\cA'",
r"\\'",
r#"\"\?'"#,
r"\e\E'",
r"\z'",
r"\xg'",
r"abc\",
"abc",
"",
"'",
r"\",
r"\\",
r"\377'",
r"\777'",
r"\400'",
r"\08'",
r"\1234'",
r"\x123'",
r"\xFF'",
r"\xfF'",
concat!("\\", "ud800'"),
concat!("\\", "udfff'"),
concat!("\\", "uFFFF'"),
concat!("\\", "u12345'"),
r"\U00110000'",
r"\UFFFFFFFF'",
r"\U0001F600'",
r"\U1'",
r"\c\\'",
r"\c'",
r"\cé'",
r"\c?'",
r"\c[x'",
r"\x'",
r"\u'",
r"\U'",
r"\u'tail",
"é日本\\x41'",
"\\é'",
"a\nb'",
"\\\n'",
r"\a\b\f\n\r\t\v'",
r"a'b'c",
r"\'\'\''",
];

/// Escapes and what follows them, every pair of which is read.
const ESCAPES: &[&str] = &[
"a", "b", "e", "E", "f", "n", "r", "t", "v", "\\", "'", "\"", "?", "0", "1", "3", "4", "7",
"8", "x", "u", "U", "c", "z", "é",
];
const TAILS: &[&str] = &[
"",
"'",
"1",
"41'",
"7f'",
"377'",
"0000'",
"00000041'",
"zz'",
"\\'",
"\\",
"'tail",
"@x'",
"\\\\'",
"日'",
"0'",
];

fn reading(body: &str, start: usize) -> String {
let chars: Vec<char> = body.chars().collect();
let (text, end) = quoted(&chars, start);
format!("{} @{start} => {} {end}", esc(body), esc(&text))
}

/// **NO RESTRUCTURING CHANGES A DECODED STRING OR WHERE IT ENDS.**
#[test]
fn every_pinned_body_decodes_as_recorded() {
let mut lines = Vec::new();
for body in BODIES {
let length = body.chars().count();
for start in [0, 1, length, length + 1] {
lines.push(reading(body, start));
}
}
for escape in ESCAPES {
for tail in TAILS {
lines.push(reading(&format!("\\{escape}{tail}"), 0));
lines.push(reading(&format!("x{escape}{tail}"), 0));
}
}
assert_pinned(RECORDING, &lines);
}
Loading
Loading