Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
            }
        }
Exemplo n.º 3
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);
        }
        //
        // 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);
            }
        }