Beispiel #1
0
        internal static string DecodeHeaderValue(string value)
        {
            if ((value == null) || (value.Length == 0))
            {
                return(string.Empty);
            }
            string str = string.Empty;

            foreach (string str2 in value.Split(new char[] { '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries))
            {
                string[] strArray2 = str2.Split(new char[] { '?' });
                if (((strArray2.Length != 5) || (strArray2[0] != "=")) || (strArray2[4] != "="))
                {
                    return(value);
                }
                string name  = strArray2[1];
                bool   flag  = strArray2[2] == "B";
                byte[] bytes = Encoding.ASCII.GetBytes(strArray2[3]);
                EncodedStreamFactory factory = new EncodedStreamFactory();
                int      count    = factory.GetEncoderForHeader(Encoding.GetEncoding(name), flag, 0).DecodeBytes(bytes, 0, bytes.Length);
                Encoding encoding = Encoding.GetEncoding(name);
                str = str + encoding.GetString(bytes, 0, count);
            }
            return(str);
        }
Beispiel #2
0
 internal static string EncodeHeaderValue(string value, Encoding encoding, bool base64Encoding, int headerLength)
 {
     new StringBuilder();
     if (IsAscii(value, false))
         return value;
     if (encoding == null)
         encoding = Encoding.GetEncoding("utf-8");
     var stream = new EncodedStreamFactory().GetEncoderForHeader(encoding, base64Encoding, headerLength);
     var bytes = encoding.GetBytes(value);
     stream.EncodeBytes(bytes, 0, bytes.Length);
     return stream.GetEncodedString();
 }
Beispiel #3
0
        //used when the length of the header name itself is known (i.e. Subject : )
        internal static string EncodeHeaderValue(string value, Encoding?encoding, bool base64Encoding, int headerLength)
        {
            //no need to encode if it's pure ascii
            if (IsAscii(value, false))
            {
                return(value);
            }

            encoding ??= Encoding.GetEncoding(DefaultCharSet);

            IEncodableStream stream = EncodedStreamFactory.GetEncoderForHeader(encoding, base64Encoding, headerLength);

            stream.EncodeString(value, encoding);
            return(stream.GetEncodedString());
        }
Beispiel #4
0
        internal static string EncodeHeaderValue(string value, Encoding encoding, bool base64Encoding, int headerLength)
        {
            new StringBuilder();
            if (IsAscii(value, false))
            {
                return(value);
            }
            if (encoding == null)
            {
                encoding = Encoding.GetEncoding("utf-8");
            }
            IEncodableStream stream = new EncodedStreamFactory().GetEncoderForHeader(encoding, base64Encoding, headerLength);

            byte[] bytes = encoding.GetBytes(value);
            stream.EncodeBytes(bytes, 0, bytes.Length);
            return(stream.GetEncodedString());
        }
Beispiel #5
0
        //used when the length of the header name itself is known (i.e. Subject : )
        internal static string EncodeHeaderValue(string value, Encoding?encoding, bool base64Encoding, int headerLength)
        {
            //no need to encode if it's pure ascii
            if (IsAscii(value, false))
            {
                return(value);
            }

            encoding ??= Encoding.GetEncoding(DefaultCharSet);

            EncodedStreamFactory factory = new EncodedStreamFactory();
            IEncodableStream     stream  = factory.GetEncoderForHeader(encoding, base64Encoding, headerLength);

            byte[] buffer = encoding.GetBytes(value);
            stream.EncodeBytes(buffer, 0, buffer.Length);
            return(stream.GetEncodedString());
        }
        //used when the length of the header name itself is known (i.e. Subject : )
        internal static string EncodeHeaderValue(string value, Encoding encoding, bool base64Encoding, int headerLength) {
            StringBuilder newString = new StringBuilder();
            
            //no need to encode if it's pure ascii
            if (IsAscii(value, false)) {
                return value;
            }

            if (encoding == null) {
                encoding = Encoding.GetEncoding(MimeBasePart.defaultCharSet);
            }

            EncodedStreamFactory factory = new EncodedStreamFactory();
            IEncodableStream stream = factory.GetEncoderForHeader(encoding, base64Encoding, headerLength);
            
            byte[] buffer = encoding.GetBytes(value);
            stream.EncodeBytes(buffer, 0, buffer.Length);
            return stream.GetEncodedString();
        }
Beispiel #7
0
 internal static string DecodeHeaderValue(string value)
 {
     if (value == null || value.Length == 0)
         return string.Empty;
     var str = string.Empty;
     foreach (var str2 in value.Split(new char[] { '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries))
     {
         var strArray2 = str2.Split(new char[] { '?' });
         if (strArray2.Length != 5 || strArray2[0] != "=" || strArray2[4] != "=")
             return value;
         var name = strArray2[1];
         var flag = strArray2[2] == "B";
         var bytes = Encoding.ASCII.GetBytes(strArray2[3]);
         var factory = new EncodedStreamFactory();
         var count = factory.GetEncoderForHeader(Encoding.GetEncoding(name), flag, 0).DecodeBytes(bytes, 0, bytes.Length);
         var encoding = Encoding.GetEncoding(name);
         str = str + encoding.GetString(bytes, 0, count);
     }
     return str;
 }
Beispiel #8
0
        internal static string DecodeHeaderValue(string?value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return(string.Empty);
            }

            string newValue = string.Empty;

            //split strings, they may be folded.  If they are, decode one at a time and append the results
            string[] substringsToDecode = value.Split(s_headerValueSplitChars, StringSplitOptions.RemoveEmptyEntries);

            foreach (string foldedSubString in substringsToDecode)
            {
                //an encoded string has as specific format in that it must start and end with an
                //'=' char and contains five parts, separated by '?' chars.
                //the first and last part are therefore '=', the second part is the byte encoding (B or Q)
                //the third is the unicode encoding type, and the fourth is encoded message itself.  '?' is not valid inside of
                //an encoded string other than as a separator for these five parts.
                //If this check fails, the string is either not encoded or cannot be decoded by this method
                string[] subStrings = foldedSubString.Split(s_questionMarkSplitChars);
                if ((subStrings.Length != 5 || subStrings[0] != "=" || subStrings[4] != "="))
                {
                    return(value);
                }

                string charSet        = subStrings[1];
                bool   base64Encoding = (subStrings[2] == "B");
                byte[] buffer         = Encoding.ASCII.GetBytes(subStrings[3]);
                int    newLength;

                EncodedStreamFactory encoderFactory = new EncodedStreamFactory();
                IEncodableStream     s = encoderFactory.GetEncoderForHeader(Encoding.GetEncoding(charSet), base64Encoding, 0);

                newLength = s.DecodeBytes(buffer, 0, buffer.Length);

                Encoding encoding = Encoding.GetEncoding(charSet);
                newValue += encoding.GetString(buffer, 0, newLength);
            }
            return(newValue);
        }
Beispiel #9
0
        internal static string DecodeHeaderValue(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            string newValue = string.Empty;

            //split strings, they may be folded.  If they are, decode one at a time and append the results
            string[] substringsToDecode = value.Split(s_headerValueSplitChars, StringSplitOptions.RemoveEmptyEntries);

            foreach (string foldedSubString in substringsToDecode)
            {
                //an encoded string has as specific format in that it must start and end with an
                //'=' char and contains five parts, separated by '?' chars.
                //the first and last part are therefore '=', the second part is the byte encoding (B or Q)
                //the third is the unicode encoding type, and the fourth is encoded message itself.  '?' is not valid inside of
                //an encoded string other than as a separator for these five parts.
                //If this check fails, the string is either not encoded or cannot be decoded by this method
                string[] subStrings = foldedSubString.Split(s_questionMarkSplitChars);
                if ((subStrings.Length != 5 || subStrings[0] != "=" || subStrings[4] != "="))
                {
                    return value;
                }

                string charSet = subStrings[1];
                bool base64Encoding = (subStrings[2] == "B");
                byte[] buffer = Encoding.ASCII.GetBytes(subStrings[3]);
                int newLength;

                EncodedStreamFactory encoderFactory = new EncodedStreamFactory();
                IEncodableStream s = encoderFactory.GetEncoderForHeader(Encoding.GetEncoding(charSet), base64Encoding, 0);

                newLength = s.DecodeBytes(buffer, 0, buffer.Length);

                Encoding encoding = Encoding.GetEncoding(charSet);
                newValue += encoding.GetString(buffer, 0, newLength);
            }
            return newValue;
        }
        //used when the length of the header name itself is known (i.e. Subject : )
        internal static string EncodeHeaderValue(string value, Encoding encoding, bool base64Encoding, int headerLength)
        {
            StringBuilder newString = new StringBuilder();

            //no need to encode if it's pure ascii
            if (IsAscii(value, false))
            {
                return(value);
            }

            if (encoding == null)
            {
                encoding = Encoding.GetEncoding(MimeBasePart.defaultCharSet);
            }

            EncodedStreamFactory factory = new EncodedStreamFactory();
            IEncodableStream     stream  = factory.GetEncoderForHeader(encoding, base64Encoding, headerLength);

            byte[] buffer = encoding.GetBytes(value);
            stream.EncodeBytes(buffer, 0, buffer.Length);
            return(stream.GetEncodedString());
        }
 internal static string DecodeHeaderValue(string value)
 {
     if ((value == null) || (value.Length == 0))
     {
         return string.Empty;
     }
     string str = string.Empty;
     foreach (string str2 in value.Split(new char[] { '\r', '\n', ' ' }, StringSplitOptions.RemoveEmptyEntries))
     {
         string[] strArray2 = str2.Split(new char[] { '?' });
         if (((strArray2.Length != 5) || (strArray2[0] != "=")) || (strArray2[4] != "="))
         {
             return value;
         }
         string name = strArray2[1];
         bool flag = strArray2[2] == "B";
         byte[] bytes = Encoding.ASCII.GetBytes(strArray2[3]);
         EncodedStreamFactory factory = new EncodedStreamFactory();
         int count = factory.GetEncoderForHeader(Encoding.GetEncoding(name), flag, 0).DecodeBytes(bytes, 0, bytes.Length);
         Encoding encoding = Encoding.GetEncoding(name);
         str = str + encoding.GetString(bytes, 0, count);
     }
     return str;
 }