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
| public String Decode(String text)
{
String r = @"(?<prefixe>.*?)=\?(?<encoding>.*?)\?(?<base>B|Q|b|q)\?(?<val>.*?)\?=";
Encoding code;
MimeCodeQP aCode = new MimeCodeQP();
Base64Decoder base64;
String strDecoded = "";
byte[] temp;
foreach(Match m in Regex.Matches(text,r))
{
strDecoded += m.Groups["prefixe"].ToString();
if (m.Groups["base"].ToString().ToUpper().Equals("B"))
{
//décoder val de base 64
code = Encoding.GetEncoding(m.Groups["encoding"].ToString());
base64 = new Base64Decoder(m.Groups["val"].ToString().ToCharArray());
temp = base64.GetDecoded();
strDecoded += code.GetString(temp).Replace("_", " ");
}
else
{
aCode.Charset = m.Groups["encoding"].ToString();
strDecoded += aCode.DecodeToString(m.Groups["val"].ToString()).Replace("_", " ");
}
}
if ((text.LastIndexOf("?=")+2) < text.Length)
{
strDecoded += text.Substring(text.LastIndexOf("?=") + 2);
}
if (strDecoded.Equals(String.Empty))
return text;
else
return strDecoded;
} |