﻿// Decompiled with JetBrains decompiler
// Type: System.Net.Mail.MailAddressParser
// Assembly: System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 5ABD58FD-DF31-44FD-A492-63F2B47CC9AF
// Assembly location: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.dll

using System;
using System.Collections.Generic;
using System.Net.Mime;
using System.Text;

namespace System.Net.Mail
{
  internal static class MailAddressParser
  {
    internal static MailAddress ParseAddress(string data)
    {
      int index = data.Length - 1;
      return MailAddressParser.ParseAddress(data, false, ref index);
    }

    internal static IList<MailAddress> ParseMultipleAddresses(string data)
    {
      IList<MailAddress> list = (IList<MailAddress>) new List<MailAddress>();
      for (int index = data.Length - 1; index >= 0; --index)
        list.Insert(0, MailAddressParser.ParseAddress(data, true, ref index));
      return list;
    }

    internal static string NormalizeOrThrow(string input)
    {
      try
      {
        return input.Normalize(NormalizationForm.FormC);
      }
      catch (ArgumentException ex)
      {
        throw new FormatException(SR.GetString("MailAddressInvalidFormat"), (Exception) ex);
      }
    }

    private static MailAddress ParseAddress(string data, bool expectMultipleAddresses, ref int index)
    {
      index = MailAddressParser.ReadCfwsAndThrowIfIncomplete(data, index);
      bool expectAngleBracket = false;
      if ((int) data[index] == (int) MailBnfHelper.EndAngleBracket)
      {
        expectAngleBracket = true;
        --index;
      }
      string domain = MailAddressParser.ParseDomain(data, ref index);
      if ((int) data[index] != (int) MailBnfHelper.At)
        throw new FormatException(SR.GetString("MailAddressInvalidFormat"));
      --index;
      string userName = MailAddressParser.ParseLocalPart(data, ref index, expectAngleBracket, expectMultipleAddresses);
      if (expectAngleBracket)
      {
        if (index >= 0 && (int) data[index] == (int) MailBnfHelper.StartAngleBracket)
        {
          --index;
          index = WhitespaceReader.ReadFwsReverse(data, index);
        }
        else
          throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[1]
          {
            (object) (char) (index >= 0 ? (int) data[index] : (int) MailBnfHelper.EndAngleBracket)
          }));
      }
      return new MailAddress(index < 0 || expectMultipleAddresses && (int) data[index] == (int) MailBnfHelper.Comma ? string.Empty : MailAddressParser.ParseDisplayName(data, ref index, expectMultipleAddresses), userName, domain);
    }

    private static int ReadCfwsAndThrowIfIncomplete(string data, int index)
    {
      index = WhitespaceReader.ReadCfwsReverse(data, index);
      if (index < 0)
        throw new FormatException(SR.GetString("MailAddressInvalidFormat"));
      return index;
    }

    private static string ParseDomain(string data, ref int index)
    {
      index = MailAddressParser.ReadCfwsAndThrowIfIncomplete(data, index);
      int num = index;
      index = (int) data[index] != (int) MailBnfHelper.EndSquareBracket ? DotAtomReader.ReadReverse(data, index) : DomainLiteralReader.ReadReverse(data, index);
      string input = data.Substring(index + 1, num - index);
      index = MailAddressParser.ReadCfwsAndThrowIfIncomplete(data, index);
      return MailAddressParser.NormalizeOrThrow(input);
    }

    private static string ParseLocalPart(string data, ref int index, bool expectAngleBracket, bool expectMultipleAddresses)
    {
      index = MailAddressParser.ReadCfwsAndThrowIfIncomplete(data, index);
      int num = index;
      if ((int) data[index] == (int) MailBnfHelper.Quote)
      {
        index = QuotedStringFormatReader.ReadReverseQuoted(data, index, true);
      }
      else
      {
        index = DotAtomReader.ReadReverse(data, index);
        if (index >= 0 && !MailBnfHelper.Whitespace.Contains(data[index]) && (int) data[index] != (int) MailBnfHelper.EndComment && ((!expectAngleBracket || (int) data[index] != (int) MailBnfHelper.StartAngleBracket) && (!expectMultipleAddresses || (int) data[index] != (int) MailBnfHelper.Comma)) && (int) data[index] != (int) MailBnfHelper.Quote)
          throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[1]
          {
            (object) data[index]
          }));
      }
      string input = data.Substring(index + 1, num - index);
      index = WhitespaceReader.ReadCfwsReverse(data, index);
      return MailAddressParser.NormalizeOrThrow(input);
    }

    private static string ParseDisplayName(string data, ref int index, bool expectMultipleAddresses)
    {
      int index1 = WhitespaceReader.ReadCfwsReverse(data, index);
      string input;
      if (index1 >= 0 && (int) data[index1] == (int) MailBnfHelper.Quote)
      {
        index = QuotedStringFormatReader.ReadReverseQuoted(data, index1, true);
        int startIndex = index + 2;
        input = data.Substring(startIndex, index1 - startIndex);
        index = WhitespaceReader.ReadCfwsReverse(data, index);
        if (index >= 0 && (!expectMultipleAddresses || (int) data[index] != (int) MailBnfHelper.Comma))
          throw new FormatException(SR.GetString("MailHeaderFieldInvalidCharacter", new object[1]
          {
            (object) data[index]
          }));
      }
      else
      {
        int num = index;
        index = QuotedStringFormatReader.ReadReverseUnQuoted(data, index, true, expectMultipleAddresses);
        input = data.Substring(index + 1, num - index).Trim();
      }
      return MailAddressParser.NormalizeOrThrow(input);
    }
  }
}
