12#include "kmime_debug.h"
14#include <QStringEncoder>
18static const char reservedCharacters[] =
"\"()<>@,.;:\\[]=";
20QByteArray encodeRFC2047String(QStringView src,
const QByteArray &charset,
26 bool nonAscii =
false;
27 bool useQEncoding =
false;
30 QStringEncoder codec(charset.
constData());
33 if (!codec.isValid()) {
36 usedCS = codec.name();
39 usedCS = codec.name();
45 QByteArray encoded8Bit = codec.encode(src);
46 if (codec.hasError()) {
48 codec = QStringEncoder(usedCS.
constData());
49 encoded8Bit = codec.encode(src);
56 const auto encoded8BitLength = encoded8Bit.
size();
57 for (
int i = 0; i < encoded8BitLength; i++) {
58 if (encoded8Bit[i] ==
' ') {
63 if (((
signed char)encoded8Bit[i] < 0) || (encoded8Bit[i] ==
'\033') ||
64 (addressHeader && (strchr(
"\"()<>@,.;:\\[]=", encoded8Bit[i]) !=
nullptr))) {
72 while ((end < encoded8Bit.
length()) && (encoded8Bit[end] !=
' ')) {
77 for (
int x = end; x < encoded8Bit.
length(); x++) {
78 if (((
signed char)encoded8Bit[x] < 0) || (encoded8Bit[x] ==
'\033') ||
79 (addressHeader && (strchr(reservedCharacters, encoded8Bit[x]) !=
nullptr))) {
82 while ((end < encoded8Bit.
length()) && (encoded8Bit[end] !=
' ')) {
89 result = encoded8Bit.
left(
start) +
"=?" + usedCS;
96 char c = encoded8Bit[i];
100 if (((c >=
'a') && (c <=
'z')) ||
101 ((c >=
'A') && (c <=
'Z')) ||
102 ((c >=
'0') && (c <=
'9'))) {
106 hexcode = ((c & 0xF0) >> 4) + 48;
111 hexcode = (c & 0x0F) + 48;
124 result += encoded8Bit.
right(encoded8Bit.
length() - end);
126 result = encoded8Bit;
132QByteArray encodeRFC2047Sentence(QStringView src,
const QByteArray &charset)
136 const auto length = src.
size();
138 qsizetype wordStart = 0;
143 while (pos < length) {
145 const bool isAscii = ch->
unicode() < 127;
146 const bool isReserved = (strchr(reservedCharacters, ch->
toLatin1()) !=
nullptr);
147 if (isAscii && isReserved) {
148 const auto wordSize = pos - wordStart;
150 const auto word = src.
mid(wordStart, wordSize);
151 result += encodeRFC2047String(word, charset);
162 const auto wordSize = pos - wordStart;
164 const auto word = src.
mid(wordStart, pos - wordStart);
165 result += encodeRFC2047String(word, charset);
172QByteArray encodeRFC2231String(QStringView str,
const QByteArray &charset)
178 QStringEncoder codec(charset.
constData());
180 if (charset ==
"us-ascii") {
182 }
else if (codec.isValid()) {
183 latin = codec.encode(str);
189 for (l = latin.
data(); *l; ++l) {
190 if (((*l & 0xE0) == 0) || (*l & 0x80)) {
199 QByteArray result = charset +
"''";
200 for (l = latin.
data(); *l; ++l) {
201 bool needsQuoting = (*l & 0x80) || (*l ==
'%');
203 constexpr const char especials[] =
"()<>@,;:\"/[]?.= \033";
204 for (
const auto especial :especials) {
205 if (*l == especial) {
213 unsigned char hexcode;
214 hexcode = ((*l & 0xF0) >> 4) + 48;
219 hexcode = (*l & 0x0F) + 48;
Q_SCRIPTABLE Q_NOREPLY void start()
const QList< QKeySequence > & end()
const char * constData() const const
bool contains(QByteArrayView bv) const const
bool isEmpty() const const
QByteArray left(qsizetype len) const const
qsizetype length() const const
QByteArray mid(qsizetype pos, qsizetype len) const const
QByteArray right(qsizetype len) const const
qsizetype size() const const
QByteArray toBase64(Base64Options options) const const
char toLatin1() const const
QStringView mid(qsizetype start, qsizetype length) const const
const_pointer constData() const const
bool isEmpty() const const
qsizetype size() const const
QByteArray toLatin1() const const
QByteArray toLocal8Bit() const const