Exemplo n.º 1
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 or false is returned:
        // - 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 bool TryParseLocalPart(string data, ref int index, bool expectAngleBracket, bool expectMultipleAddresses, out string localPart, bool throwExceptionIfFail)
        {
            // Skip comments and whitespace
            if (!TryReadCfwsAndThrowIfIncomplete(data, index, out index, throwExceptionIfFail))
            {
                localPart = default;
                return(false);
            }

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

            // Is the local-part component in quoted-string format or dot-atom format?
            if (data[index] == dotNetTips.Utility.Standard.Common.ControlChars.Quote)
            {
                if (!QuotedStringFormatReader.TryReadReverseQuoted(data, index, true, out index, throwExceptionIfFail))
                {
                    localPart = default;
                    return(false);
                }
            }
            else
            {
                if (!DotAtomReader.TryReadReverse(data, index, out index, throwExceptionIfFail))
                {
                    localPart = default;
                    return(false);
                }

                // Check that the local-part is properly separated from the next component. It may be separated by a
                // comment, whitespace, an expected angle bracket, a quote for the display-name, or an expected comma
                // before the next address.
                if (index >= 0 &&
                    !(
                        MailBnfHelper.IsAllowedWhiteSpace(data[index]) ||  // < local@domain >
                        data[index] == dotNetTips.Utility.Standard.Common.ControlChars.EndComment ||     // <(comment)local@domain>
                        (expectAngleBracket && data[index] == dotNetTips.Utility.Standard.Common.ControlChars.StartAngleBracket) ||       // <local@domain>
                        (expectMultipleAddresses && data[index] == dotNetTips.Utility.Standard.Common.ControlChars.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] == dotNetTips.Utility.Standard.Common.ControlChars.Quote     // "display"local@domain
                        )
                    )
                {
                    if (throwExceptionIfFail)
                    {
                        throw new FormatException($"Invalid character: {data[index]}.");
                    }
                    else
                    {
                        localPart = default;
                        return(false);
                    }
                }
            }

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

            if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail))
            {
                return(false);
            }

            if (!TryNormalizeOrThrow(localPart, out localPart, throwExceptionIfFail))
            {
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
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 or false is returned:
        // - For invalid un-escaped chars, except Unicode
        // - If the postconditions cannot be met.
        private static bool TryParseDisplayName(string data, ref int index, bool expectMultipleAddresses, out string displayName, bool throwExceptionIfFail)
        {
            // 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.
            if (!WhitespaceReader.TryReadCfwsReverse(data, index, out int firstNonCommentIndex, throwExceptionIfFail))
            {
                displayName = default;
                return(false);
            }

            // Check to see if there's a quoted-string display name
            if (firstNonCommentIndex >= 0 && data[firstNonCommentIndex] == dotNetTips.Utility.Standard.Common.ControlChars.Quote)
            {
                // The preceding comment was not part of the display name.  Read just the quoted string.
                if (!QuotedStringFormatReader.TryReadReverseQuoted(data, firstNonCommentIndex, true, out index, throwExceptionIfFail))
                {
                    displayName = default;
                    return(false);
                }

                Debug.Assert(data[index + 1] == dotNetTips.Utility.Standard.Common.ControlChars.Quote, "Mis-aligned 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
                if (!WhitespaceReader.TryReadCfwsReverse(data, index, out index, throwExceptionIfFail))
                {
                    return(false);
                }

                // 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] == dotNetTips.Utility.Standard.Common.ControlChars.Comma))
                {
                    // If there was still data, only a comma could have been the next valid character
                    return(throwExceptionIfFail ? throw new FormatException($"Invalid character: {data[index]}.") : false);
                }
            }
            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.
                if (!QuotedStringFormatReader.TryReadReverseUnQuoted(data, index, true, expectMultipleAddresses, out index, throwExceptionIfFail))
                {
                    displayName = default;
                    return(false);
                }

                Debug.Assert(index < 0 || data[index] == dotNetTips.Utility.Standard.Common.ControlChars.Comma, "Mis-aligned index: " + index);

                // Do not include the Comma (if any), and because there were no bounding quotes,
                // trim extra whitespace.
                displayName = data.SubstringTrim(index + 1, startingIndex - index);
            }

            if (!TryNormalizeOrThrow(displayName, out displayName, throwExceptionIfFail))
            {
                return(false);
            }

            return(true);
        }