forked from smiley22/S22.Mail
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUtil.cs
More file actions
222 lines (215 loc) · 6.67 KB
/
Util.cs
File metadata and controls
222 lines (215 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace S22.Mail
{
/// <summary>
/// A static utility class containing methods for decoding encoded
/// non-ASCII data as is often used in mail messages as well as
/// extension methods for some existing classes.
/// </summary>
internal static class Util
{
/// <summary>
/// Returns true if the string contains only ASCII characters.
/// </summary>
/// <param name="s">Extension method for the String class.</param>
/// <returns>
/// Returns true if the string contains only ASCII characters,
/// otherwise false is returned.
/// </returns>
internal static bool IsASCII(this string s)
{
return s.ToCharArray().All(c => c < 127);
}
/// <summary>
/// Decodes a string composed of one or several MIME 'encoded-words'.
/// </summary>
/// <param name="words">
/// A string to composed of one or several MIME
/// 'encoded-words'
/// </param>
/// <exception cref="FormatException">
/// Thrown when an unknown encoding
/// (other than Q-Encoding or Base64) is encountered.
/// </exception>
/// <returns>
/// A concatenation of all enconded-words in the passed
/// string
/// </returns>
public static string DecodeWords(string words)
{
var matches = Regex.Matches(words,
@"(=\?[A-Za-z0-9\-]+\?[BbQq]\?[^\?]+\?=)");
return matches.Cast<Match>().Aggregate(string.Empty, (current, m) => current + DecodeWord(m.ToString()));
}
/// <summary>
/// Decodes a MIME 'encoded-word' string.
/// </summary>
/// <param name="word">The encoded word to decode</param>
/// <exception cref="FormatException">
/// Thrown when an unknown encoding
/// (other than Q-Encoding or Base64) is encountered.
/// </exception>
/// <returns>A decoded string</returns>
/// <remarks>
/// MIME encoded-word syntax is a way to encode strings that
/// contain non-ASCII data. Commonly used encodings for the encoded-word
/// sytax are Q-Encoding and Base64. For an in-depth description, refer
/// to RFC 2047
/// </remarks>
internal static string DecodeWord(string word)
{
var m = Regex.Match(word,
@"=\?([A-Za-z0-9\-]+)\?([BbQq])\?(.+)\?=");
if (!m.Success)
return word;
var encoding = GetEncoding(m.Groups[1].Value);
var type = m.Groups[2].Value.ToUpper();
var text = m.Groups[3].Value;
switch (type)
{
case "Q":
return QDecode(text, encoding);
case "B":
return encoding.GetString(Base64Decode(text));
default:
throw new FormatException("Encoding not recognized " +
"in encoded word: " + word);
}
}
/// <summary>
/// Takes a Q-encoded string and decodes it using the specified
/// encoding.
/// </summary>
/// <param name="value">The Q-encoded string to decode</param>
/// <param name="encoding">
/// The encoding to use for encoding the
/// returned string
/// </param>
/// <exception cref="FormatException">
/// Thrown if the string is
/// not a valid Q-encoded string.
/// </exception>
/// <returns>A Q-decoded string</returns>
internal static string QDecode(string value, Encoding encoding)
{
try
{
using (var m = new MemoryStream())
{
for (var i = 0; i < value.Length; i++)
{
if (value[i] == '=')
{
var hex = value.Substring(i + 1, 2);
m.WriteByte(Convert.ToByte(hex, 16));
i = i + 2;
}
else if (value[i] == '_')
{
m.WriteByte(Convert.ToByte(' '));
}
else
{
m.WriteByte(Convert.ToByte(value[i]));
}
}
return encoding.GetString(m.ToArray());
}
}
catch
{
throw new FormatException("value is not a valid Q-encoded " +
"string");
}
}
/// <summary>
/// Takes a quoted-printable encoded string and decodes it using
/// the specified encoding.
/// </summary>
/// <param name="value">
/// The quoted-printable-encoded string to
/// decode
/// </param>
/// <param name="encoding">
/// The encoding to use for encoding the
/// returned string
/// </param>
/// <exception cref="FormatException">
/// Thrown if the string is
/// not a valid quoted-printable encoded string.
/// </exception>
/// <returns>A quoted-printable decoded string</returns>
internal static string QPDecode(string value, Encoding encoding)
{
try
{
using (var m = new MemoryStream())
{
for (var i = 0; i < value.Length; i++)
{
if (value[i] == '=')
{
var hex = value.Substring(i + 1, 2);
m.WriteByte(Convert.ToByte(hex, 16));
i = i + 2;
}
else
{
m.WriteByte(Convert.ToByte(value[i]));
}
}
return encoding.GetString(m.ToArray());
}
}
catch
{
throw new FormatException("value is not a valid quoted-printable " +
"encoded string");
}
}
/// <summary>
/// Takes a Base64-encoded string and decodes it.
/// </summary>
/// <param name="value">The Base64-encoded string to decode</param>
/// <returns>
/// A byte array containing the Base64-decoded bytes
/// of the input string.
/// </returns>
internal static byte[] Base64Decode(string value)
{
return Convert.FromBase64String(value);
}
/// <summary>
/// This just wraps Encoding.GetEncoding in a try-catch block to
/// ensure it never fails. If the encoding can not be determined
/// ASCII is returned as a default.
/// </summary>
/// <param name="name">
/// The code page name of the preferred encoding.
/// Any value returned by System.Text.Encoding.WebName is a valid
/// input.
/// </param>
/// <returns>
/// The System.Text.Encoding associated with the specified
/// code page or Encoding.ASCII if the specified code page could not
/// be resolved.
/// </returns>
internal static Encoding GetEncoding(string name)
{
Encoding encoding;
try
{
encoding = Encoding.GetEncoding(name);
}
catch
{
encoding = Encoding.ASCII;
}
return encoding;
}
}
}