示例#1
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 }));
 }
 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);
 }
        // Read through a section of CFWS.  If we reach the end of the data string then throw because not enough of the
        // MailAddress components were found.
        private static bool TryReadCfwsAndThrowIfIncomplete(string data, int index, out int outIndex, bool throwExceptionIfFail)
        {
            if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail))
            {
                outIndex = default;
                return(false);
            }

            if (index < 0)
            {
                // More components were expected.  Incomplete address, invalid
                if (throwExceptionIfFail)
                {
                    throw new FormatException(SR.MailAddressInvalidFormat);
                }
                else
                {
                    outIndex = default;
                    return(false);
                }
            }

            outIndex = index;
            return(true);
        }
示例#4
0
 private static int ReadCfwsAndThrowIfIncomplete(string data, int index)
 {
     index = WhitespaceReader.ReadCfwsReverse(data, index);
     if (index < 0)
     {
         throw new FormatException(SR.GetString("MailAddressInvalidFormat"));
     }
     return(index);
 }
示例#5
0
        //
        // 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 or false is returned 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 bool TryReadReverseUnQuoted(string data, int index, bool permitUnicode, bool expectCommaDelimiter, out int outIndex, bool throwExceptionIfFail)
        {
            Debug.Assert(0 <= index && index < data.Length, "Index out of range: " + index + ", " + data.Length);

            do
            {
                // Check for valid whitespace
                if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
                {
                    outIndex = default;
                    return(false);
                }

                if (index < 0)
                {
                    break;
                }
                // Check for escaped characters
                if (!QuotedPairReader.TryCountQuotedChars(data, index, permitUnicode, out int quotedCharCount, throwExceptionIfFail))
                {
                    outIndex = default;
                    return(false);
                }

                if (quotedCharCount > 0)
                {
                    index -= quotedCharCount;
                }
                // Check for the terminating char
                else if (expectCommaDelimiter && data[index] == MailBnfHelper.Comma)
                {
                    break;
                }
                // Check invalid characters
                else if (!IsValidQtext(permitUnicode, data[index]))
                {
                    if (throwExceptionIfFail)
                    {
                        throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                    }
                    else
                    {
                        outIndex = default;
                        return(false);
                    }
                }
                // Valid char
                else
                {
                    index--;
                }
            }while (index >= 0);

            outIndex = index;
            return(true);
        }
示例#6
0
 // Read through a section of CFWS.  If we reach the end of the data string then throw because not enough of the
 // MailAddress components were found.
 private static int ReadCfwsAndThrowIfIncomplete(string data, int index)
 {
     index = WhitespaceReader.ReadCfwsReverse(data, index);
     if (index < 0)
     {
         // More components were expected.  Incomplete address, invalid
         throw new FormatException(SR.MailAddressInvalidFormat);
     }
     return(index);
 }
示例#7
0
        // Parses the display-name section of an address.  In departure from the RFC, we attempt to read data in the
        // quoted-string format even if the bounding quotes are omitted.  We also permit Unicode, which the RFC does
        // not allow for.
        // e.g. ("display name" <user@domain>) or (display name <user@domain>)
        //
        // Preconditions:
        //
        // Postconditions:
        // - data[index] should refer to the comma ',' separator, if any
        // - index == -1 if the beginning of the data string has been reached.
        // - returns the parsed display-name, excluding any bounding quotes around quoted-strings
        //
        // Throws a FormatException:
        // - For invalid un-escaped chars, except Unicode
        // - If the postconditions cannot be met.
        private static string ParseDisplayName(string data, ref int index, bool expectMultipleAddresses)
        {
            string displayName;

            // Whatever is left over must be the display name. The display name should be a single word/atom or a
            // quoted string, but for robustness we allow the quotes to be omitted, so long as we can find the comma
            // separator before the next address.

            // Read the comment (if any).  If the display name is contained in quotes, the surrounding comments are
            // omitted. Otherwise, mark this end of the comment so we can include it as part of the display name.
            int firstNonCommentIndex = WhitespaceReader.ReadCfwsReverse(data, index);

            // Check to see if there's a quoted-string display name
            if (firstNonCommentIndex >= 0 && data[firstNonCommentIndex] == MailBnfHelper.Quote)
            {
                // The preceding comment was not part of the display name.  Read just the quoted string.
                index = QuotedStringFormatReader.ReadReverseQuoted(data, firstNonCommentIndex, true);

                Debug.Assert(data[index + 1] == MailBnfHelper.Quote, "Mis-alligned index: " + index);

                // Do not include the bounding quotes on the display name
                int leftIndex = index + 2;
                displayName = data.Substring(leftIndex, firstNonCommentIndex - leftIndex);

                // Skip any CFWS after the display name
                index = WhitespaceReader.ReadCfwsReverse(data, index);

                // Check for completion. We are valid if we hit the end of the data string or if the rest of the data
                // belongs to another address.
                if (index >= 0 && !(expectMultipleAddresses && data[index] == MailBnfHelper.Comma))
                {
                    // If there was still data, only a comma could have been the next valid character
                    throw new FormatException(SR.GetString(SR.MailHeaderFieldInvalidCharacter, data[index]));
                }
            }
            else
            {
                // The comment (if any) should be part of the display name.
                int startingIndex = index;

                // Read until the dividing comma or the end of the line.
                index = QuotedStringFormatReader.ReadReverseUnQuoted(data, index, true, expectMultipleAddresses);

                Debug.Assert(index < 0 || data[index] == MailBnfHelper.Comma, "Mis-alligned index: " + index);

                // Do not include the Comma (if any)
                displayName = data.Substring(index + 1, startingIndex - index);

                // Because there were no bounding quotes, trim extra whitespace
                displayName = displayName.Trim();
            }
            return(NormalizeOrThrow(displayName));
        }
        //
        // 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));
        }
示例#9
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));
        }
示例#10
0
        // Parses the local-part section of an address.  The local-part may be in dot-atom format or
        // quoted-string format. e.g. <user.name@domain> or <"user name"@domain>
        // We do not support the obsolete formats of user."name"@domain, "user".name@domain, or "user"."name"@domain.
        //
        // Preconditions:
        // - data[index + 1] is the '@' symbol
        //
        // Postconditions:
        // - data[index] should refer to the '<', if any, otherwise the next non-CFWS char.
        // - index == -1 if the beginning of the data string has been reached.
        // - returns the parsed local-part, including any bounding quotes around quoted-strings
        //
        // Throws a FormatException:
        // - For invalid un-escaped chars, including Unicode
        // - If the final value of data[index] is not a valid character to precede the local-part
        private static string ParseLocalPart(string data, ref int index, bool expectAngleBracket,
                                             bool expectMultipleAddresses)
        {
            // Skip comments and whitespace
            index = ReadCfwsAndThrowIfIncomplete(data, index);

            // Mark the start of the local-part
            int startingIndex = index;

            // Is the local-part component in quoted-string format or dot-atom format?
            if (data[index] == MailBnfHelper.Quote)
            {
                index = QuotedStringFormatReader.ReadReverseQuoted(data, index, true);
            }
            else
            {
                index = DotAtomReader.ReadReverse(data, index);

                // Check that the local-part is properly separated from the next component. It may be separated by a
                // comment, white space, an expected angle bracket, a quote for the display-name, or an expected comma
                // before the next address.
                if (index >= 0 &&
                    !(
                        MailBnfHelper.Whitespace.Contains(data[index]) ||  // < local@domain >
                        data[index] == MailBnfHelper.EndComment ||     // <(comment)local@domain>
                        (expectAngleBracket && data[index] == MailBnfHelper.StartAngleBracket) ||     // <local@domain>
                        (expectMultipleAddresses && data[index] == MailBnfHelper.Comma) // local@dom,local@dom
                                                                                        // Note: The following condition is more lax than the RFC.  This is done so we could support
                                                                                        // a common invalid formats as shown below.
                        || data[index] == MailBnfHelper.Quote                           // "display"local@domain
                        )
                    )
                {
                    throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                }
            }

            string localPart = data.Substring(index + 1, startingIndex - index);

            index = WhitespaceReader.ReadCfwsReverse(data, index);

            return(NormalizeOrThrow(localPart));
        }
示例#11
0
        private static string ParseDisplayName(string data, ref int index, bool expectMultipleAddresses)
        {
            int num = WhitespaceReader.ReadCfwsReverse(data, index);

            if ((num >= 0) && (data[num] == MailBnfHelper.Quote))
            {
                index = QuotedStringFormatReader.ReadReverseQuoted(data, num, true);
                int    startIndex = index + 2;
                string str        = data.Substring(startIndex, num - startIndex);
                index = WhitespaceReader.ReadCfwsReverse(data, index);
                if ((index >= 0) && (!expectMultipleAddresses || (data[index] != MailBnfHelper.Comma)))
                {
                    throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[] { data[index] }));
                }
                return(str);
            }
            int num3 = index;

            index = QuotedStringFormatReader.ReadReverseUnQuoted(data, index, true, expectMultipleAddresses);
            return(data.Substring(index + 1, num3 - index).Trim());
        }
示例#12
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));
        }
示例#13
0
        private static string ParseLocalPart(string data, ref int index, bool expectAngleBracket, bool expectMultipleAddresses)
        {
            index = ReadCfwsAndThrowIfIncomplete(data, index);
            int num = index;

            if (data[index] == MailBnfHelper.Quote)
            {
                index = QuotedStringFormatReader.ReadReverseQuoted(data, index, false);
            }
            else
            {
                index = DotAtomReader.ReadReverse(data, index);
                if ((((((index >= 0) && !MailBnfHelper.Whitespace.Contains(data[index])) && (data[index] != MailBnfHelper.EndComment)) && (!expectAngleBracket || (data[index] != MailBnfHelper.StartAngleBracket))) && (!expectMultipleAddresses || (data[index] != MailBnfHelper.Comma))) && (data[index] != MailBnfHelper.Quote))
                {
                    throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[] { data[index] }));
                }
            }
            string str = data.Substring(index + 1, num - index);

            index = WhitespaceReader.ReadCfwsReverse(data, index);
            return(str);
        }
        //
        // 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);
        }
示例#15
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 or false is returned if any part of the MailAddress is invalid.
        private static bool TryParseAddress(string data, bool expectMultipleAddresses, ref int index, out ParseAddressInfo parseAddressInfo, bool throwExceptionIfFail)
        {
            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?displayName;

            // Skip comments and whitespace
            if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
            {
                parseAddressInfo = default;
                return(false);
            }

            // 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--;
            }

            if (!TryParseDomain(data, ref index, out string?domain, throwExceptionIfFail))
            {
                parseAddressInfo = default;
                return(false);
            }

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

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

            if (!TryParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses, out string?localPart, throwExceptionIfFail))
            {
                parseAddressInfo = default;
                return(false);
            }

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

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

            parseAddressInfo = new ParseAddressInfo(displayName, localPart, domain);
            return(true);
        }
示例#16
0
        //
        // 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 or false is returned 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 bool TryReadReverseQuoted(string data, int index, bool permitUnicode, out int outIndex, bool throwExceptionIfFail)
        {
            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
                if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
                {
                    outIndex = default;
                    return(false);
                }

                if (index < 0)
                {
                    break;
                }

                // Check for escaped characters
                if (!QuotedPairReader.TryCountQuotedChars(data, index, permitUnicode, out int quotedCharCount, throwExceptionIfFail))
                {
                    outIndex = default;
                    return(false);
                }

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

            if (throwExceptionIfFail)
            {
                // We started with a quote, but did not end with one
                throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, MailBnfHelper.Quote));
            }
            else
            {
                outIndex = default;
                return(false);
            }
        }
示例#17
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 or false is returned 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 bool TryReadReverse(string data, int index, out int outIndex, bool throwExceptionIfFail)
        {
            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
                if (!WhitespaceReader.TryReadFwsReverse(data, index, out index, throwExceptionIfFail))
                {
                    outIndex = default;
                    return(false);
                }

                if (index < 0)
                {
                    break;
                }

                // Check for escaped characters
                if (!QuotedPairReader.TryCountQuotedChars(data, index, false, out int quotedCharCount, throwExceptionIfFail))
                {
                    outIndex = default;
                    return(false);
                }

                if (quotedCharCount > 0)
                {
                    // Skip quoted pairs
                    index -= quotedCharCount;
                }
                // Check for the terminating bracket
                else if (data[index] == MailBnfHelper.StartSquareBracket)
                {
                    // We're done parsing
                    outIndex = index - 1;
                    return(true);
                }
                // Check for invalid characters
                else if (data[index] > MailBnfHelper.Ascii7bitMaxValue || !MailBnfHelper.Dtext[data[index]])
                {
                    if (throwExceptionIfFail)
                    {
                        throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter, data[index]));
                    }
                    else
                    {
                        outIndex = default;
                        return(false);
                    }
                }
                // Valid char
                else
                {
                    index--;
                }
            }while (index >= 0);

            if (throwExceptionIfFail)
            {
                // We didn't find a matching '[', throw.
                throw new FormatException(SR.Format(SR.MailHeaderFieldInvalidCharacter,
                                                    MailBnfHelper.EndSquareBracket));
            }
            else
            {
                outIndex = default;
                return(false);
            }
        }
示例#18
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));
        }