Skip to content

Commit a0ab7b7

Browse files
committed
Properly mark CTRL characters as non-dtext
1 parent 12263ad commit a0ab7b7

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

MimeKit/Utils/ByteExtensions.cs

+17-35
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ static class ByteExtensions
5555
const string AtomSafeCharacters = "!#$%&'*+-/=?^_`{|}~";
5656
const string AttributeSpecials = "*'%"; // attribute specials from rfc2184/rfc2231
5757
const string CommentSpecials = "()\\\r"; // not allowed in comments
58-
const string DomainSpecials = "[]\\\r \t"; // not allowed in domains
5958
const string EncodedWordSpecials = "()<>@,;:\"/[]?.=_"; // rfc2047 5.1
6059
const string EncodedPhraseSpecials = "!*+-/=_"; // rfc2047 5.3
6160
const string Specials = "()<>[]:;@\\,.\""; // rfc5322 3.2.3
@@ -70,33 +69,15 @@ static void RemoveFlags (string values, CharType bit)
7069
table[(byte) values[i]] &= ~bit;
7170
}
7271

73-
static void SetFlags (string values, CharType bit, CharType bitcopy, bool remove)
72+
static void SetFlags (string values, CharType bit, CharType bitcopy)
7473
{
75-
int i;
76-
77-
if (remove) {
78-
for (i = 0; i < 128; i++)
79-
table[i] |= bit;
80-
81-
for (i = 0; i < values.Length; i++)
82-
table[values[i]] &= ~bit;
83-
84-
// Note: not actually used...
85-
//if (bitcopy != CharType.None) {
86-
// for (i = 0; i < 256; i++) {
87-
// if ((table[i] & bitcopy) != 0)
88-
// table[i] &= ~bit;
89-
// }
90-
//}
91-
} else {
92-
for (i = 0; i < values.Length; i++)
93-
table[values[i]] |= bit;
94-
95-
if (bitcopy != CharType.None) {
96-
for (i = 0; i < 256; i++) {
97-
if ((table[i] & bitcopy) != 0)
98-
table[i] |= bit;
99-
}
74+
for (int i = 0; i < values.Length; i++)
75+
table[values[i]] |= bit;
76+
77+
if (bitcopy != CharType.None) {
78+
for (int i = 0; i < 256; i++) {
79+
if ((table[i] & bitcopy) != 0)
80+
table[i] |= bit;
10081
}
10182
}
10283
}
@@ -109,14 +90,16 @@ static ByteExtensions ()
10990
table[i] |= CharType.IsControl;
11091
if (i > 32)
11192
table[i] |= CharType.IsAttrChar;
112-
if ((i >= 33 && i <= 60) || (i >= 62 && i <= 126) || i == 32)
93+
if ((i >= 32 && i <= 60) || i >= 62) // space + all printable characters *except* '='
11394
table[i] |= (CharType.IsQuotedPrintableSafe | CharType.IsEncodedWordSafe);
11495
if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'z') || (i >= 'A' && i <= 'Z'))
11596
table[i] |= CharType.IsEncodedPhraseSafe | CharType.IsAtom | CharType.IsPhraseAtom;
11697
if ((i >= '0' && i <= '9') || (i >= 'a' && i <= 'f') || (i >= 'A' && i <= 'F'))
11798
table[i] |= CharType.IsXDigit;
118-
if ((i >= 33 && i <= 57) || i >= 59)
99+
if ((i >= 33 && i <= 57) || i >= 59) // all printable characters *except* ':'
119100
table[i] |= CharType.IsFieldText;
101+
if ((i >= 33 && i <= 90) || i >= 94) // all printable characters *except* '[', '\\', and ']'
102+
table[i] |= CharType.IsDomainSafe;
120103

121104
table[i] |= CharType.IsAscii;
122105
} else {
@@ -132,15 +115,14 @@ static ByteExtensions ()
132115
table['\t'] |= CharType.IsQuotedPrintableSafe | CharType.IsBlank;
133116
table[' '] |= CharType.IsSpace | CharType.IsBlank;
134117

135-
SetFlags (Whitespace, CharType.IsWhitespace, CharType.None, false);
136-
SetFlags (AtomSafeCharacters, CharType.IsAtom | CharType.IsPhraseAtom, CharType.None, false);
137-
SetFlags (TokenSpecials, CharType.IsTokenSpecial, CharType.IsControl, false);
138-
SetFlags (Specials, CharType.IsSpecial, CharType.None, false);
139-
SetFlags (DomainSpecials, CharType.IsDomainSafe, CharType.None, true);
118+
SetFlags (Whitespace, CharType.IsWhitespace, CharType.None);
119+
SetFlags (AtomSafeCharacters, CharType.IsAtom | CharType.IsPhraseAtom, CharType.None);
120+
SetFlags (TokenSpecials, CharType.IsTokenSpecial, CharType.IsControl);
121+
SetFlags (Specials, CharType.IsSpecial, CharType.None);
140122
RemoveFlags (Specials, CharType.IsAtom | CharType.IsPhraseAtom);
141123
RemoveFlags (EncodedWordSpecials, CharType.IsEncodedWordSafe);
142124
RemoveFlags (AttributeSpecials + TokenSpecials, CharType.IsAttrChar);
143-
SetFlags (EncodedPhraseSpecials, CharType.IsEncodedPhraseSafe, CharType.None, false);
125+
SetFlags (EncodedPhraseSpecials, CharType.IsEncodedPhraseSafe, CharType.None);
144126

145127
// Note: Allow '[' and ']' in the display-name of a mailbox address
146128
table['['] |= CharType.IsPhraseAtom;

0 commit comments

Comments
 (0)