14#include "charfreq_p.h"
15#include "kmime_debug.h"
16#include "headerparsing.h"
20#include <QCoreApplication>
32QList<QByteArray> c_harsetCache;
34QByteArray cachedCharset(
const QByteArray &name)
36 for (
const QByteArray &charset : std::as_const(c_harsetCache)) {
37 if (qstricmp(
name.
data(), charset.data()) == 0) {
44 return c_harsetCache.last();
47bool isUsAscii(QStringView s)
49 return std::all_of(s.begin(), s.end(), [](QChar c) { return c.unicode() < 128; });
61 default:
return QStringLiteral(
"unknown");
65QList<Headers::contentEncoding> encodingsForData(QByteArrayView data) {
66 QList<Headers::contentEncoding> allowed;
70 case CharFreq::SevenBitText:
73 case CharFreq::EightBitText:
76 case CharFreq::SevenBitData:
77 if (cf.printableRatio() > 5.0 / 6.0) {
88 case CharFreq::EightBitData:
100const uchar aTextMap[16] = {
101 0x00, 0x00, 0x00, 0x00,
102 0x5F, 0x35, 0xFF, 0xC5,
103 0x7F, 0xFF, 0xFF, 0xE3,
104 0xFF, 0xFF, 0xFF, 0xFE
108const uchar tTextMap[16] = {
109 0x00, 0x00, 0x00, 0x00,
110 0x5F, 0x36, 0xFF, 0xC0,
111 0x7F, 0xFF, 0xFF, 0xE3,
112 0xFF, 0xFF, 0xFF, 0xFE
115QByteArray uniqueString()
117 static const char chars[] =
"0123456789abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
121 unsigned int timeval;
125 ran = 1 + (int)(1000.0 * rand() / (RAND_MAX + 1.0));
128 for (
int i = 0; i < 10; i++) {
129 int pos = (int)(61.0 * rand() / (RAND_MAX + 1.0));
142QByteArray multiPartBoundary()
144 return "nextPart" + uniqueString();
147QByteArray CRLFtoLF(
const QByteArray &s)
149 if (!s.contains(
"\r\n")) {
158QByteArray LFtoCRLF(
const QByteArray &s)
160 const auto firstNewline = s.indexOf(
'\n');
161 if (firstNewline == -1) {
164 if (firstNewline > 0 && s.at(firstNewline - 1) ==
'\r') {
176bool isCryptoPart(
const Content *content)
178 const auto ct = content->contentType();
179 if (!ct || !ct->isMediatype(
"application")) {
183 const QByteArray lowerSubType = ct->subType().toLower();
184 if (lowerSubType ==
"pgp-encrypted" ||
185 lowerSubType ==
"pgp-signature" ||
186 lowerSubType ==
"pkcs7-mime" ||
187 lowerSubType ==
"x-pkcs7-mime" ||
188 lowerSubType ==
"pkcs7-signature" ||
189 lowerSubType ==
"x-pkcs7-signature") {
193 if (lowerSubType ==
"octet-stream") {
194 const auto cd = content->contentDisposition();
198 const auto fileName = cd->filename().toLower();
199 return fileName == QLatin1StringView(
"msg.asc") ||
200 fileName == QLatin1StringView(
"encrypted.asc");
206bool isAttachment(
const Content* content)
212 const auto contentType = content->contentType();
215 if (contentType->isMultipart()) {
218 if (contentType->isMimeType(
"message/rfc822")) {
224 if (content->parent()) {
225 const auto top = content->topLevel();
226 if (content == top->textContent()) {
232 if (isCryptoPart(content)) {
242 if (contentType && !contentType->name().isEmpty()) {
254bool hasAttachment(
const Content *content)
260 if (isAttachment(content)) {
265 const auto ct = content->contentType();
266 if (ct && ct->isMultipart() && !ct->isSubtype(
"related")) {
267 const auto contents = content->contents();
268 for (
auto child : contents) {
269 if (hasAttachment(child)) {
277bool hasInvitation(
const Content *content)
283 if (isInvitation(content)) {
288 if (
auto ct = content->contentType(); ct && ct->isMultipart()) {
289 const auto contents = content->contents();
290 for (
auto child : contents) {
291 if (hasInvitation(child)) {
299bool isSigned(
const Message *message)
305 const KMime::Headers::ContentType *
const contentType = message->
contentType();
310 contentType->
isSubtype(
"pgp-signature") ||
311 contentType->
isSubtype(
"pkcs7-signature") ||
312 contentType->
isSubtype(
"x-pkcs7-signature") ||
316 message->
mainBodyPart(
"application/x-pkcs7-signature")) {
322bool isEncrypted(
const Message *message)
328 const KMime::Headers::ContentType *
const contentType = message->
contentType();
332 if (contentType->
isSubtype(
"encrypted") ||
333 contentType->
isSubtype(
"pgp-encrypted") ||
335 contentType->
isSubtype(
"x-pkcs7-mime") ||
346bool isInvitation(
const Content *content)
352 const KMime::Headers::ContentType *
const contentType = content->contentType();
A class that encapsulates MIME encoded Content.
Headers::ContentType * contentType(bool create=true)
Returns the Content-Type header.
Represents a (email) message.
Content * mainBodyPart(const QByteArray &type=QByteArray())
Returns the first main body part of a given type, taking multipart/mixed and multipart/alternative no...
QString name(StandardAction id)
QByteArray & replace(QByteArrayView before, QByteArrayView after)
QByteArray & setNum(double n, char format, int precision)
QString toUpper() const const