예제 #1
0
        /// <summary>
        /// Enumerates the message-id references such as those that can be found in the In-Reply-To or References header.
        /// </summary>
        /// <returns>The references.</returns>
        /// <param name="buffer">The raw byte buffer to parse.</param>
        /// <param name="startIndex">The index into the buffer to start parsing.</param>
        /// <param name="length">The length of the buffer to parse.</param>
        /// <exception cref="System.ArgumentNullException">
        /// <paramref name="buffer"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> and <paramref name="length"/> do not specify
        /// a valid range in the byte array.
        /// </exception>
        public static IEnumerable <string> EnumerateReferences(byte[] buffer, int startIndex, int length)
        {
            int             endIndex = startIndex + length;
            int             index    = startIndex;
            InternetAddress addr;

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            if (startIndex < 0 || startIndex > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("startIndex");
            }

            if (length < 0 || startIndex + length > buffer.Length)
            {
                throw new ArgumentOutOfRangeException("length");
            }

            do
            {
                if (!ParseUtils.SkipCommentsAndWhiteSpace(buffer, ref index, endIndex, false))
                {
                    break;
                }

                if (index >= endIndex)
                {
                    break;
                }

                if (buffer[index] == '<')
                {
                    if (!InternetAddress.TryParseMailbox(buffer, startIndex, ref index, endIndex, "", 65001, false, out addr))
                    {
                        break;
                    }

                    yield return("<" + ((MailboxAddress)addr).Address + ">");
                }
                else if (!ParseUtils.Skip8bitWord(buffer, ref index, endIndex, false))
                {
                    index++;
                }
            } while (index < endIndex);

            yield break;
        }