internal static int ReadReverseUnQuoted(string data, int index, bool permitUnicode, bool expectCommaDelimiter)
 {
     do
     {
         index = WhitespaceReader.ReadFwsReverse(data, index);
         if (index < 0)
         {
             return(index);
         }
         int num = QuotedPairReader.CountQuotedChars(data, index, permitUnicode);
         if (num > 0)
         {
             index -= num;
         }
         else
         {
             if (expectCommaDelimiter && (data[index] == MailBnfHelper.Comma))
             {
                 return(index);
             }
             if (!IsValidQtext(permitUnicode, data[index]))
             {
                 throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[] { data[index] }));
             }
             index--;
         }
     }while (index >= 0);
     return(index);
 }
示例#2
0
 internal static int ReadReverse(string data, int index)
 {
     index--;
     do
     {
         index = WhitespaceReader.ReadFwsReverse(data, index);
         if (index < 0)
         {
             break;
         }
         int num = QuotedPairReader.CountQuotedChars(data, index, false);
         if (num > 0)
         {
             index -= num;
         }
         else
         {
             if (data[index] == MailBnfHelper.StartSquareBracket)
             {
                 return(index - 1);
             }
             if ((data[index] > MailBnfHelper.Ascii7bitMaxValue) || !MailBnfHelper.Dtext[data[index]])
             {
                 throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[] { data[index] }));
             }
             index--;
         }
     }while (index >= 0);
     throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[] { MailBnfHelper.EndSquareBracket }));
 }
        //
        // This method reads a standard quoted string. Departing from the RFC, Unicode is permitted for display-name.
        //
        // Preconditions:
        //  - Index must be within the bounds of the data string.
        //  - The char at the given index is the initial quote. (data[index] == Quote)
        //
        // Return value: The next index past the terminating-quote (data[index + 1] == Quote).
        //   e.g. In (bob "user name"@domain), starting at index=14 (") returns index=3 (space).
        //
        // A FormatException will be thrown if:
        // - A non-escaped character is encountered that is not valid in a quoted string.
        // - A Unicode character is encountered and Unicode has not been allowed.
        // - The final double quote is not found.
        //
        internal static int ReadReverseQuoted(string data, int index, bool permitUnicode)
        {
            Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length);
            // Check for the first bounding quote
            Debug.Assert(data[index] == MailBnfHelper.Quote, "Initial char at index " + index + " was not a quote.");

            // Skip the bounding quote
            index--;

            do
            {
                // Check for valid whitespace
                index = WhitespaceReader.ReadFwsReverse(data, index);
                if (index < 0)
                {
                    break;
                }

                // Check for escaped characters
                int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, permitUnicode);
                if (quotedCharCount > 0)
                {
                    // Skip quoted pairs
                    index = index - quotedCharCount;
                }
                // Check for the terminating quote
                else if (data[index] == MailBnfHelper.Quote)
                {
                    // Skip the final bounding quote
                    return(index - 1);
                }
                // Check invalid characters
                else if (!IsValidQtext(permitUnicode, data[index]))
                {
                    throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                }
                // Valid char
                else
                {
                    index--;
                }
            }while (index >= 0);

            // We started with a quote, but did not end with one
            throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Quote));
        }
示例#4
0
        //
        // Reads a domain literal in reverse
        //
        // Preconditions:
        //  - Index must be within the bounds of the data string.
        //  - The char at the given index is the initial bracket. (data[index] == EndSquareBracket)
        //
        // Return value:
        // - The next index past the terminating bracket (data[index + 1] == StartSquareBracket).
        //   e.g. In (user@[domain]), starting at index=12 (]) returns index=4 (@).
        //
        // A FormatException will be thrown if:
        // - A non-escaped character is encountered that is not valid in a domain literal, including Unicode.
        // - The final bracket is not found.
        //
        internal static int ReadReverse(string data, int index)
        {
            Debug.Assert(0 <= index && index < data.Length, "index was outside the bounds of the string: " + index);
            Debug.Assert(data[index] == MailBnfHelper.EndSquareBracket, "data did not end with a square bracket");

            // Skip the end bracket
            index--;

            do
            {
                // Check for valid whitespace
                index = WhitespaceReader.ReadFwsReverse(data, index);
                if (index < 0)
                {
                    break;
                }
                // Check for escaped characters
                int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, false);
                if (quotedCharCount > 0)
                {
                    // Skip quoted pairs
                    index = index - quotedCharCount;
                }
                // Check for the terminating bracket
                else if (data[index] == MailBnfHelper.StartSquareBracket)
                {
                    // We're done parsing
                    return(index - 1);
                }
                // Check for invalid characters
                else if (data[index] > MailBnfHelper.Ascii7bitMaxValue || !MailBnfHelper.Dtext[data[index]])
                {
                    throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                }
                // Valid char
                else
                {
                    index--;
                }
            }while (index >= 0);

            // We didn't find a matching '[', throw.
            throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
                                                MailBnfHelper.EndSquareBracket));
        }
示例#5
0
        private static MailAddress ParseAddress(string data, bool expectMultipleAddresses, ref int index)
        {
            string domain      = null;
            string userName    = null;
            string displayName = null;

            index = ReadCfwsAndThrowIfIncomplete(data, index);
            bool expectAngleBracket = false;

            if (data[index] == MailBnfHelper.EndAngleBracket)
            {
                expectAngleBracket = true;
                index--;
            }
            domain = ParseDomain(data, ref index);
            if (data[index] != MailBnfHelper.At)
            {
                throw new FormatException(SR.GetString("MailAddressInvalidFormat"));
            }
            index--;
            userName = ParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses);
            if (expectAngleBracket)
            {
                if ((index < 0) || (data[index] != MailBnfHelper.StartAngleBracket))
                {
                    throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[] { (index >= 0) ? data[index] : MailBnfHelper.EndAngleBracket }));
                }
                index--;
                index = WhitespaceReader.ReadFwsReverse(data, index);
            }
            if ((index >= 0) && (!expectMultipleAddresses || (data[index] != MailBnfHelper.Comma)))
            {
                displayName = ParseDisplayName(data, ref index, expectMultipleAddresses);
            }
            else
            {
                displayName = string.Empty;
            }
            return(new MailAddress(displayName, userName, domain));
        }
        //
        // This method attempts reading quoted-string formatted data when the bounding quotes were omitted.
        // This is common for e-mail display-names.
        //
        // Precondition: The index must be within the bounds of the data string.
        //
        // Return value:
        // - The index of the special delimiter provided.
        //   e.g. In ([email protected], billy box [email protected]), starting at index=19 (x) returns index=9 (,).
        // - -1 if the terminating character was not found.
        //   e.g. In (my name username@domain), starting at index=5 (e) returns index=-1.
        //
        // A FormatException will be thrown if:
        // - A non-escaped character is encountered that is not valid in a quoted string.  This includes double quotes.
        // - A Unicode character is encountered and Unicode has not been allowed.
        //
        internal static int ReadReverseUnQuoted(string data, int index, bool permitUnicode, bool expectCommaDelimiter)
        {
            Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length);

            do
            {
                // Check for valid whitespace
                index = WhitespaceReader.ReadFwsReverse(data, index);
                if (index < 0)
                {
                    break;
                }
                // Check for escaped characters
                int quotedCharCount = QuotedPairReader.CountQuotedChars(data, index, permitUnicode);
                if (quotedCharCount > 0)
                {
                    index = index - quotedCharCount;
                }
                // Check for the terminating char
                else if (expectCommaDelimiter && data[index] == MailBnfHelper.Comma)
                {
                    break;
                }
                // Check invalid characters
                else if (!IsValidQtext(permitUnicode, data[index]))
                {
                    throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                }
                // Valid char
                else
                {
                    index--;
                }
            }while (index >= 0);

            return(index);
        }
示例#7
0
        //
        // Parse a single MailAddress, potentially from a list.
        //
        // Preconditions:
        //  - Index must be within the bounds of the data string.
        //  - The data string must not be null or empty
        //
        // Postconditions:
        // - Returns a valid MailAddress object parsed from the string
        // - For a single MailAddress index is set to -1
        // - For a list data[index] is the comma separator or -1 if the end of the data string was reached.
        //
        // Throws a FormatException if any part of the MailAddress is invalid.
        private static MailAddress ParseAddress(string data, bool expectMultipleAddresses, ref int index)
        {
            Debug.Assert(!String.IsNullOrEmpty(data));
            Debug.Assert(index >= 0 && index < data.Length, "Index out of range: " + index + ", " + data.Length);

            // Parsed components to be assembled as a MailAddress later
            string domain      = null;
            string localPart   = null;
            string displayName = null;

            // Skip comments and whitespace
            index = ReadCfwsAndThrowIfIncomplete(data, index);

            // Do we expect angle brackets around the address?
            // e.g. ("display name" <user@domain>)
            bool expectAngleBracket = false;

            if (data[index] == MailBnfHelper.EndAngleBracket)
            {
                expectAngleBracket = true;
                index--;
            }

            domain = ParseDomain(data, ref index);

            // The next character after the domain must be the '@' symbol
            if (data[index] != MailBnfHelper.At)
            {
                throw new FormatException(SR.MailAddressInvalidFormat);
            }

            // Skip the '@' symbol
            index--;

            localPart = ParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses);

            // Check for a matching angle bracket around the address
            if (expectAngleBracket)
            {
                if (index >= 0 && data[index] == MailBnfHelper.StartAngleBracket)
                {
                    index--; // Skip the angle bracket
                    // Skip white spaces, but leave comments, as they may be part of the display name.
                    index = WhitespaceReader.ReadFwsReverse(data, index);
                }
                else
                {
                    // Mismatched angle brackets, throw
                    throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
                                                        (index >= 0 ? data[index] : MailBnfHelper.EndAngleBracket)));
                }
            }

            // Is there anything left to parse?
            // There could still be a display name or another address
            if (index >= 0 && !(expectMultipleAddresses && data[index] == MailBnfHelper.Comma))
            {
                displayName = ParseDisplayName(data, ref index, expectMultipleAddresses);
            }
            else
            {
                displayName = String.Empty;
            }

            return(new MailAddress(displayName, localPart, domain));
        }