コード例 #1
0
        /// <summary>
        /// Sets item decoded value. Value will be encoded as needed and stored to item.Value property.
        /// </summary>
        /// <param name="value"></param>
        public void SetDecodedValue(string value)
        {
            /* RFC 2426 vCrad 5. Differences From vCard v2.1
             *  The QUOTED-PRINTABLE inline encoding has been eliminated.
             *  Only the "B" encoding of [RFC 2047] is an allowed value for
             *  the ENCODING parameter.
             *
             *  The CRLF character sequence in a text type value is specified
             *  with the backslash character sequence "\n" or "\N".
             *
             *  Any COMMA or SEMICOLON in a text type value must be backslash escaped.
             */

            // Remove encoding and charset parameters
            string newParmString = "";

            string[] parameters = m_Parameters.ToLower().Split(';');
            foreach (string parameter in parameters)
            {
                string[] name_value = parameter.Split('=');
                if (name_value[0] == "encoding" || name_value[0] == "charset")
                {
                }
                else if (parameter.Length > 0)
                {
                    newParmString += parameter + ";";
                }
            }

            if (m_pCard.Version.StartsWith("3"))
            {
                // Add encoding parameter
                if (!Net_Utils.IsAscii(value))
                {
                    newParmString += "CHARSET=utf-8";
                }

                this.ParametersString = newParmString;
                this.Value            = vCard_Utils.Encode(m_pCard.Version, m_pCard.Charset, value);
            }
            else
            {
                if (NeedEncode(value))
                {
                    // Add encoding parameter
                    newParmString += "ENCODING=QUOTED-PRINTABLE;CHARSET=" + m_pCard.Charset.WebName;

                    this.ParametersString = newParmString;
                    this.Value            = vCard_Utils.Encode(m_pCard.Version, m_pCard.Charset, value);
                }
                else
                {
                    this.ParametersString = newParmString;
                    this.Value            = value;
                }
            }
        }
コード例 #2
0
ファイル: Item.cs プロジェクト: ivkrivanov/datadesign
        /// <summary>
        /// Sets item decoded value. Value will be encoded as needed and stored to item.Value property.
        /// Also property item.ParametersString is updated to reflect right encoding(always base64, required by rfc) and charset (utf-8).
        /// </summary>
        /// <param name="value"></param>
        public void SetDecodedValue(string value)
        {
            /* RFC 2426 vCrad 5. Differences From vCard v2.1
             *  The QUOTED-PRINTABLE inline encoding has been eliminated.
             *  Only the "B" encoding of [RFC 2047] is an allowed value for
             *  the ENCODING parameter.
             *
             *  The CRLF character sequence in a text type value is specified
             *  with the backslash character sequence "\n" or "\N".
             *
             *  Any COMMA or SEMICOLON in a text type value must be backslash escaped.
             */

            // FIX ME: this must be done with structured fields
            //value = value.Replace("\r\n","\n").Replace("\n","\\n");
            //value = TextUtils.EscapeString(value,new char[]{',',';'});

            bool needEncode = false;

            if (!Net_Utils.IsAscii(value))
            {
                needEncode = true;
            }

            if (needEncode)
            {
                // Remove encoding and charset parameters
                string   newParmString = "";
                string[] parameters    = m_Parameters.ToLower().Split(';');
                foreach (string parameter in parameters)
                {
                    string[] name_value = parameter.Split('=');
                    if (name_value[0] == "encoding" || name_value[0] == "charset")
                    {
                    }
                    else if (parameter.Length > 0)
                    {
                        newParmString += parameter + ";";
                    }
                }
                // Add encoding parameter
                newParmString += "ENCODING=b;CHARSET=utf-8";

                this.ParametersString = newParmString;
                this.Value            = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(value));
            }
            else
            {
                this.Value = value;
            }
        }
コード例 #3
0
        /// <summary>
        /// CHecks if specified value must be encoded.
        /// </summary>
        /// <param name="value">String value.</param>
        /// <returns>Returns true if value must be encoded, otherwise false.</returns>
        private bool NeedEncode(string value)
        {
            // We have 8-bit chars.
            if (!Net_Utils.IsAscii(value))
            {
                return(true);
            }

            // Allow only printable chars and whitespaces.
            foreach (char c in value)
            {
                if (char.IsControl(c))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #4
0
        /// <summary>
        /// CHecks if specified value must be encoded.
        /// </summary>
        /// <param name="value">String value.</param>
        /// <returns>Returns true if value must be encoded, otherwise false.</returns>
        private bool NeedEncode(string value)
        {
            // We have 8-bit chars.
            if (!Net_Utils.IsAscii(value))
            {
                return(true);
            }

            // Allow only prontable chars and whitespaces.
            foreach (char c in value)
            {
                if (!(char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #5
0
        /// <summary>
        /// Returns header field parameters as string.
        /// </summary>
        /// <param name="charset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param>
        /// <returns>Returns header field parameters as string.</returns>
        public string ToString(Encoding charset)
        {
            /* RFC 2231.
             *      If parameter conatins 8-bit byte, we need to encode parameter value
             *      If parameter value length bigger than MIME maximum allowed line length,
             *      we need split value.
             */

            if (charset == null)
            {
                charset = Encoding.Default;
            }

            StringBuilder retVal = new StringBuilder();

            foreach (MIME_h_Parameter parameter in this.ToArray())
            {
                if (string.IsNullOrEmpty(parameter.Value))
                {
                    retVal.Append(";\r\n\t" + parameter.Name);
                }
                // We don't need to encode or split value.
                else if ((charset == null || Net_Utils.IsAscii(parameter.Value)) && parameter.Value.Length < 76)
                {
                    retVal.Append(";\r\n\t" + parameter.Name + "=" + TextUtils.QuoteString(parameter.Value));
                }
                // Use RFC 2047 to encode parameter.
                else if (m_EncodeRfc2047)
                {
                    retVal.Append(";\r\n\t" + parameter.Name + "=" + TextUtils.QuoteString(MIME_Encoding_EncodedWord.EncodeS(MIME_EncodedWordEncoding.B, Encoding.UTF8, false, parameter.Value)));
                }
                // We need to RFC 2231 encode/split value.
                else
                {
                    byte[] byteValue = charset.GetBytes(parameter.Value);

                    List <string> values = new List <string>();
                    // Do encoding/splitting.
                    int    offset    = 0;
                    char[] valueBuff = new char[50];
                    foreach (byte b in byteValue)
                    {
                        // We need split value as RFC 2231 says.
                        if (offset >= (50 - 3))
                        {
                            values.Add(new string(valueBuff, 0, offset));
                            offset = 0;
                        }

                        // Normal char, we don't need to encode.
                        if (MIME_Reader.IsAttributeChar((char)b))
                        {
                            valueBuff[offset++] = (char)b;
                        }
                        // We need to encode byte as %X2.
                        else
                        {
                            valueBuff[offset++] = '%';
                            valueBuff[offset++] = (b >> 4).ToString("X")[0];
                            valueBuff[offset++] = (b & 0xF).ToString("X")[0];
                        }
                    }
                    // Add pending buffer value.
                    if (offset > 0)
                    {
                        values.Add(new string(valueBuff, 0, offset));
                    }

                    for (int i = 0; i < values.Count; i++)
                    {
                        // Only fist value entry has charset and language info.
                        if (charset != null && i == 0)
                        {
                            retVal.Append(";\r\n\t" + parameter.Name + "*" + i.ToString() + "*=" + charset.WebName + "''" + values[i]);
                        }
                        else
                        {
                            retVal.Append(";\r\n\t" + parameter.Name + "*" + i.ToString() + "*=" + values[i]);
                        }
                    }
                }
            }

            return(retVal.ToString());
        }