email_encoding/body/chooser.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
use std::mem;
use super::{Encoding, StrOrBytes};
enum InputKind {
Ascii,
Utf8,
Binary,
}
impl<'a> StrOrBytes<'a> {
fn kind(&self) -> InputKind {
if self.is_ascii() {
InputKind::Ascii
} else {
match self {
Self::Str(_) => InputKind::Utf8,
Self::Bytes(_) => InputKind::Binary,
}
}
}
}
impl Encoding {
/// Choose the most efficient `Encoding` for `input`
///
/// Look into `input` and decide what encoding format could best
/// be used to represent it.
///
/// If the SMTP server supports the `SMTPUTF8` extension
/// `supports_utf8` _may_ me set to `true`, otherwise `false`
/// is the safest option.
///
/// Possible return values based on `supports_utf8`
///
/// | `Encoding` | `false` | `true` |
/// | ------------------ | ------- | ------ |
/// | `7bit` | ✅ | ✅ |
/// | `8bit` | ❌ | ✅ |
/// | `quoted-printable` | ✅ | ✅ |
/// | `base64` | ✅ | ✅ |
///
/// # Examples
///
/// ```rust
/// # use email_encoding::body::Encoding;
/// // Ascii
/// {
/// let input = "Hello, World!";
/// assert_eq!(Encoding::choose(input, false), Encoding::SevenBit);
/// assert_eq!(Encoding::choose(input, true), Encoding::SevenBit);
/// }
///
/// // Mostly ascii + utf-8
/// {
/// let input = "Hello, World! 📬";
/// assert_eq!(Encoding::choose(input, false), Encoding::QuotedPrintable);
/// assert_eq!(Encoding::choose(input, true), Encoding::EightBit);
/// }
///
/// // Mostly utf-8
/// {
/// let input = "Hello! 📬📬📬📬📬📬📬📬📬📬";
/// assert_eq!(Encoding::choose(input, false), Encoding::Base64);
/// assert_eq!(Encoding::choose(input, true), Encoding::EightBit);
/// }
///
/// // Non utf-8 bytes
/// {
/// let input = &[255, 35, 123, 190];
/// assert_eq!(Encoding::choose(input, false), Encoding::Base64);
/// assert_eq!(Encoding::choose(input, true), Encoding::Base64);
/// }
/// ```
pub fn choose<'a>(input: impl Into<StrOrBytes<'a>>, supports_utf8: bool) -> Self {
let input = input.into();
Self::choose_impl(input, supports_utf8)
}
fn choose_impl(input: StrOrBytes<'_>, supports_utf8: bool) -> Self {
let line_too_long = line_too_long(&input);
match (input.kind(), line_too_long, supports_utf8) {
(InputKind::Ascii, false, _) => {
// Input is ascii and fits the maximum line length
Self::SevenBit
}
(InputKind::Ascii, true, _) => {
// Input is ascii but doesn't fix the maximum line length
quoted_printable_or_base64(&input)
}
(InputKind::Utf8, false, true) => {
// Input is utf-8, line fits, the server supports it
Self::EightBit
}
(InputKind::Utf8, true, true) => {
// Input is utf-8, line doesn't fit, the server supports it
quoted_printable_or_base64(&input)
}
(InputKind::Utf8, _, false) => {
// Input is utf-8, the server doesn't support it
quoted_printable_or_base64(&input)
}
(InputKind::Binary, _, _) => {
// Input is binary
Self::Base64
}
}
}
}
fn line_too_long(b: &[u8]) -> bool {
let mut last = 0;
memchr::memchr_iter(b'\n', b).any(|i| {
let last_ = mem::replace(&mut last, i);
(i - last_) >= 76
}) || (b.len() - last) >= 76
}
fn quoted_printable_or_base64(b: &[u8]) -> Encoding {
if quoted_printable_efficient(b) {
Encoding::QuotedPrintable
} else {
Encoding::Base64
}
}
fn quoted_printable_efficient(b: &[u8]) -> bool {
let requiring_escaping = b
.iter()
.filter(|&b| !matches!(b, b'\t' | b' '..=b'~'))
.count();
requiring_escaping <= (b.len() / 3) // 33.33% or less
}
#[cfg(test)]
mod tests {
use super::{line_too_long, Encoding};
#[test]
fn ascii_short_str() {
let input = "0123";
assert_eq!(Encoding::choose(input, false), Encoding::SevenBit);
}
#[test]
fn ascii_long_str() {
let input = concat!(
"0123\n",
"01234567899876543210012345678998765432100123456789987654321001234567899876543210\n",
"4567"
);
assert_eq!(Encoding::choose(input, false), Encoding::QuotedPrintable);
}
#[test]
fn ascii_short_binary() {
let input = b"0123";
assert_eq!(Encoding::choose(input, false), Encoding::SevenBit);
}
#[test]
fn ascii_long_binary() {
let input = concat!(
"0123\n",
"01234567899876543210012345678998765432100123456789987654321001234567899876543210\n",
"4567"
)
.as_bytes();
assert_eq!(Encoding::choose(input, false), Encoding::QuotedPrintable);
}
#[test]
fn utf8_short_str_supported() {
let input = "0123 📬";
assert_eq!(Encoding::choose(input, true), Encoding::EightBit);
}
#[test]
fn utf8_short_str_unsupported_efficient() {
let input = "01234567899876543210 📬";
assert_eq!(Encoding::choose(input, false), Encoding::QuotedPrintable);
}
#[test]
fn utf8_short_str_unsupported_inefficient() {
let input = "0123 📬";
assert_eq!(Encoding::choose(input, false), Encoding::Base64);
}
#[test]
fn utf8_long_str_efficient() {
let input =
"01234567899876543210012345678998765432100123456789987654321001234567899876543210";
assert_eq!(Encoding::choose(input, true), Encoding::QuotedPrintable);
}
#[test]
fn utf8_long_str_inefficient() {
let input = "0123 📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬📬";
assert_eq!(Encoding::choose(input, true), Encoding::Base64);
}
#[test]
fn binary() {
let input = &[255, 234, b'A', b'C', 210];
assert_eq!(Encoding::choose(input, false), Encoding::Base64);
}
#[test]
fn not_too_long_oneline() {
let input = b"0123";
assert!(!line_too_long(input));
}
#[test]
fn not_too_long_multiline() {
let input = concat!("0123\n", "4567").as_bytes();
assert!(!line_too_long(input));
}
#[test]
fn too_long_oneline() {
let input =
b"01234567899876543210012345678998765432100123456789987654321001234567899876543210";
assert!(line_too_long(input));
}
#[test]
fn too_long_multiline() {
let input = concat!(
"0123\n",
"01234567899876543210012345678998765432100123456789987654321001234567899876543210\n",
"4567"
)
.as_bytes();
assert!(line_too_long(input));
}
}