From b447adbe373d3940d2023d8e7a6ec21c7bfb8a48 Mon Sep 17 00:00:00 2001 From: Jan Engelhardt Date: Tue, 21 May 2024 15:47:05 +0200 Subject: Fixes/comments for guessBestEncoding (#304) * tests: add case for getRecommendedEncoding * vmime: avoid integer multiply wraparound in wordEncoder::guessBestEncoding If the input string is 42949673 characters long or larger, there will be integer overflow on 32-bit platforms when multiplying by 100. Switch that one computation to floating point. * vmime: update comment in wordEncoder::guessBestEncoding --- src/vmime/wordEncoder.cpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/vmime/wordEncoder.cpp b/src/vmime/wordEncoder.cpp index 4f47d047..d1632022 100644 --- a/src/vmime/wordEncoder.cpp +++ b/src/vmime/wordEncoder.cpp @@ -302,14 +302,16 @@ wordEncoder::Encoding wordEncoder::guessBestEncoding( } } - // Use Base64 if more than 40% non-ASCII, or Quoted-Printable else (default) + // Base64 would be more space-efficient when the ASCII content is + // below 83.33%, but QP has a legibility arugment going for it, so we + // picked 60%. const size_t asciiCount = utility::stringUtils::countASCIIchars(buffer.begin(), buffer.end()); - const size_t asciiPercent = - buffer.length() == 0 ? 100 : (100 * asciiCount) / buffer.length(); + const double asciiPercent = + buffer.length() == 0 ? 100 : static_cast(asciiCount) / buffer.length(); - if (asciiPercent < 60) { + if (asciiPercent < 0.60) { return ENCODING_B64; } else { return ENCODING_QP; -- cgit v1.2.3