QDecode() public static method

"Q" decoder. This is same as quoted-printable, except '_' is converted to ' '.
public static QDecode ( System encoding, string data ) : string
encoding System Input string encoding.
data string String which to encode.
return string
Ejemplo n.º 1
0
        /// <summary>
        /// Canonical decoding. Decodes all canonical encoding occurences in specified text.
        /// Usually mime message header unicode/8bit values are encoded as Canonical.
        /// Format: =?charSet?type[Q or B]?encoded string?= .
        /// </summary>
        /// <param name="text">Text to decode.</param>
        /// <returns>Returns decoded text.</returns>
        public static string CanonicalDecode(string text)
        {
            // =?charSet?type[Q or B]?encoded string?=

            Regex regex = new Regex(@"\=\?(?<charSet>[\w\-]*)\?(?<type>[qQbB])\?(?<text>[\w\W]*)\?\=");

            MatchCollection m = regex.Matches(text);

            foreach (Match match in m)
            {
                try{
                    System.Text.Encoding enc = System.Text.Encoding.GetEncoding(match.Groups["charSet"].Value);
                    // QDecode
                    if (match.Groups["type"].Value.ToLower() == "q")
                    {
                        text = text.Replace(match.Value, Core.QDecode(enc, match.Groups["text"].Value));
                    }
                    // Base64
                    else
                    {
                        text = text.Replace(match.Value, enc.GetString(Convert.FromBase64String(match.Groups["text"].Value)));
                    }
                }
                catch {
                    // If parsing fails, just leave this string as is
                }
            }

            return(text);
        }