/// <summary> /// Decode rfc 2047 definition of quoted-printable /// </summary> /// <param name="charset">charset to use when decoding</param> /// <param name="orig"><c>string</c> to decode</param> /// <returns>the decoded <see cref="System.String" /></returns> public static System.String QuotedPrintable2Unicode(System.String charset, System.String orig) { System.Text.Encoding enc = MimeTools.parseCharSet(charset); if (enc == null || orig == null) { return(orig); } MimeTools.QuotedPrintable2Unicode(enc, ref orig); return(orig); }
/// <summary> /// rfc 2047 header body decoding /// </summary> /// <param name="word"><c>string</c> to decode</param> /// <returns>the decoded <see cref="System.String" /></returns> public static System.String rfc2047decode(System.String word) { System.String[] words; System.String[] wordetails; System.Text.RegularExpressions.Regex rfc2047format = new System.Text.RegularExpressions.Regex(@"(=\?[\-a-zA-Z0-9]+\?[qQbB]\?[a-zA-Z0-9=_\-\.$%&/\'\\!:;{}\+\*\|@#~`^]+\?=)\s*", System.Text.RegularExpressions.RegexOptions.ECMAScript); // No rfc2047 format if (!rfc2047format.IsMatch(word)) { #if LOG if (log.IsDebugEnabled) { log.Debug("Not a RFC 2047 string: " + word); } #endif return(word); } #if LOG if (log.IsDebugEnabled) { log.Debug("Decoding 2047 string: " + word); } #endif words = rfc2047format.Split(word); word = System.String.Empty; rfc2047format = new System.Text.RegularExpressions.Regex(@"=\?([\-a-zA-Z0-9]+)\?([qQbB])\?([a-zA-Z0-9=_\-\.$%&/\'\\!:;{}\+\*\|@#~`^]+)\?=", System.Text.RegularExpressions.RegexOptions.ECMAScript); for (int i = 0; i < words.GetLength(0); i++) { if (!rfc2047format.IsMatch(words[i])) { word += words[i]; continue; } wordetails = rfc2047format.Split(words[i]); switch (wordetails[2]) { case "q": case "Q": word += MimeTools.QuotedPrintable2Unicode(wordetails[1], wordetails[3]).Replace('_', ' ');; break; case "b": case "B": try { System.Text.Encoding enc = System.Text.Encoding.GetEncoding(wordetails[1]); System.Byte[] ch = System.Convert.FromBase64String(wordetails[3]); word += enc.GetString(ch); } catch (System.Exception) { } break; } } #if LOG if (log.IsDebugEnabled) { log.Debug("Decoded 2047 string: " + word); } #endif return(word); }