aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJan Engelhardt <[email protected]>2024-05-21 13:47:05 +0000
committerGitHub <[email protected]>2024-05-21 13:47:05 +0000
commitb447adbe373d3940d2023d8e7a6ec21c7bfb8a48 (patch)
tree5c8092e7215d83400ec5ad3a377f3da9db7678c0 /src
parentvmime: avoid changing SEVEN_BIT when encoding::decideImpl sees U+007F (#303) (diff)
downloadvmime-b447adbe373d3940d2023d8e7a6ec21c7bfb8a48.tar.gz
vmime-b447adbe373d3940d2023d8e7a6ec21c7bfb8a48.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/vmime/wordEncoder.cpp10
1 files changed, 6 insertions, 4 deletions
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<double>(asciiCount) / buffer.length();
- if (asciiPercent < 60) {
+ if (asciiPercent < 0.60) {
return ENCODING_B64;
} else {
return ENCODING_QP;