Beispiel #1
0
        public sealed override MimeNode Clone()
        {
            DateHeader dateHeader = new DateHeader(base.Name, base.HeaderId);

            this.CopyTo(dateHeader);
            return(dateHeader);
        }
Beispiel #2
0
        public void WriteHeaderValue(DateTime value)
        {
            this.AssertOpen();
            if (this.headerValueWritten)
            {
                throw new InvalidOperationException(Strings.CannotWriteHeaderValueMoreThanOnce);
            }
            if (this.lastHeader == null)
            {
                throw new InvalidOperationException(Strings.CannotWriteHeaderValueHere);
            }
            this.headerValueWritten = true;
            TimeSpan timeZoneOffset = TimeSpan.Zero;
            DateTime utcDateTime    = value.ToUniversalTime();

            if (value.Kind != DateTimeKind.Utc)
            {
                timeZoneOffset = TimeZoneInfo.Local.GetUtcOffset(value);
            }
            Header.WriteName(this.shimStream, this.lastHeader.Name, ref this.scratchBuffer);
            MimeStringLength mimeStringLength = new MimeStringLength(0);

            DateHeader.WriteDateHeaderValue(this.shimStream, utcDateTime, timeZoneOffset, ref mimeStringLength);
            this.lastHeader = null;
        }
Beispiel #3
0
        internal static Header Create(string name, HeaderId headerId)
        {
            if (headerId < HeaderId.Unknown || headerId > (HeaderId)MimeData.nameIndex.Length)
            {
                throw new ArgumentException(Strings.InvalidHeaderId, "headerId");
            }
            if (headerId == HeaderId.Unknown)
            {
                throw new ArgumentException(Strings.CannotDetermineHeaderNameFromId, "headerId");
            }
            Header header;

            switch (MimeData.headerNames[(int)MimeData.nameIndex[(int)headerId]].headerType)
            {
            case HeaderType.AsciiText:
                header = new AsciiTextHeader(MimeData.headerNames[(int)MimeData.nameIndex[(int)headerId]].name, headerId);
                break;

            case HeaderType.Date:
                header = new DateHeader(MimeData.headerNames[(int)MimeData.nameIndex[(int)headerId]].name, headerId);
                break;

            case HeaderType.Received:
                header = new ReceivedHeader();
                break;

            case HeaderType.ContentType:
                header = new ContentTypeHeader();
                break;

            case HeaderType.ContentDisposition:
                header = new ContentDispositionHeader();
                break;

            case HeaderType.Address:
                header = new AddressHeader(MimeData.headerNames[(int)MimeData.nameIndex[(int)headerId]].name, headerId);
                break;

            default:
                header = new TextHeader(MimeData.headerNames[(int)MimeData.nameIndex[(int)headerId]].name, headerId);
                break;
            }
            if (!string.IsNullOrEmpty(name) && !header.MatchName(name))
            {
                throw new ArgumentException("name");
            }
            return(header);
        }
Beispiel #4
0
        internal static DateTime ParseDateHeaderValue(string value)
        {
            byte[] array;
            if (value != null)
            {
                array = ByteString.StringToBytes(value, false);
            }
            else
            {
                array = MimeString.EmptyByteArray;
            }
            MimeStringList list = new MimeStringList(array, 0, array.Length);
            DateTime       result;
            TimeSpan       timeSpan;

            DateHeader.ParseValue(list, out result, out timeSpan);
            return(result);
        }
Beispiel #5
0
        public DateTime ReadValueAsDateTime()
        {
            this.AssertGood(true);
            if (this.reader.ReaderState == MimeReaderState.HeaderStart)
            {
                this.reader.TryCompleteCurrentHeader(true);
            }
            if (this.reader.CurrentHeaderObject == null)
            {
                return(DateTime.SpecifyKind(DateTime.MinValue, DateTimeKind.Utc));
            }
            DateHeader dateHeader = this.reader.CurrentHeaderObject as DateHeader;

            if (dateHeader != null)
            {
                return(dateHeader.DateTime);
            }
            return(DateHeader.ParseDateHeaderValue(this.reader.CurrentHeaderObject.Value));
        }
Beispiel #6
0
        internal override long WriteTo(Stream stream, EncodingOptions encodingOptions, MimeOutputFilter filter, ref MimeStringLength currentLineLength, ref byte[] scratchBuffer)
        {
            long num = base.WriteName(stream, ref scratchBuffer);

            currentLineLength.IncrementBy((int)num);
            if (!base.IsDirty && base.RawLength != 0 && base.IsProtected)
            {
                long num2 = Header.WriteLines(base.Lines, stream);
                num += num2;
                currentLineLength.SetAs(0);
                return(num);
            }
            if (!this.parsed)
            {
                this.Parse();
            }
            num += DateHeader.WriteDateHeaderValue(stream, this.utcDateTime, this.timeZoneOffset, ref currentLineLength);
            currentLineLength.SetAs(0);
            return(num);
        }
Beispiel #7
0
        public sealed override void CopyTo(object destination)
        {
            if (destination == null)
            {
                throw new ArgumentNullException("destination");
            }
            if (destination == this)
            {
                return;
            }
            DateHeader dateHeader = destination as DateHeader;

            if (dateHeader == null)
            {
                throw new ArgumentException(Strings.CantCopyToDifferentObjectType);
            }
            base.CopyTo(destination);
            dateHeader.parsed         = this.parsed;
            dateHeader.utcDateTime    = this.utcDateTime;
            dateHeader.timeZoneOffset = this.timeZoneOffset;
        }
Beispiel #8
0
        internal static long WriteDateHeaderValue(Stream stream, DateTime utcDateTime, TimeSpan timeZoneOffset, ref MimeStringLength currentLineLength)
        {
            long num          = 0L;
            int  countInChars = 0;

            byte[] array = DateHeader.FormatValue(utcDateTime, timeZoneOffset, out countInChars);
            stream.Write(MimeString.Space, 0, MimeString.Space.Length);
            num += (long)MimeString.Space.Length;
            currentLineLength.IncrementBy(MimeString.Space.Length);
            if (array != null)
            {
                stream.Write(array, 0, array.Length);
                num += (long)array.Length;
                currentLineLength.IncrementBy(countInChars, array.Length);
            }
            else
            {
                stream.Write(MimeString.CommentInvalidDate, 0, MimeString.CommentInvalidDate.Length);
                num += (long)MimeString.CommentInvalidDate.Length;
                currentLineLength.IncrementBy(MimeString.CommentInvalidDate.Length);
            }
            return(num + Header.WriteLineEnd(stream, ref currentLineLength));
        }
Beispiel #9
0
 private void Parse()
 {
     this.parsed = true;
     DateHeader.ParseValue(base.Lines, out this.utcDateTime, out this.timeZoneOffset);
 }
Beispiel #10
0
        private static void ParseValue(MimeStringList list, out DateTime utcDateTime, out TimeSpan timeZoneOffset)
        {
            MimeStringList mimeStringList = default(MimeStringList);
            ValueParser    valueParser    = new ValueParser(list, false);

            DateHeader.ParseStage parseStage = DateHeader.ParseStage.DayOfWeek;
            int[] array = new int[8];
            byte  b     = 32;

            while (parseStage != DateHeader.ParseStage.Count)
            {
                valueParser.ParseCFWS(false, ref mimeStringList, true);
                byte b2 = valueParser.ParseGet();
                if (b2 == 0)
                {
                    break;
                }
                if (!MimeScan.IsToken(b2) || b2 == 45 || b2 == 43)
                {
                    b = b2;
                    valueParser.ParseCFWS(false, ref mimeStringList, true);
                }
                else
                {
                    if (MimeScan.IsDigit(b2))
                    {
                        if (parseStage == DateHeader.ParseStage.DayOfWeek)
                        {
                            parseStage = DateHeader.ParseStage.DayOfMonth;
                        }
                        if (parseStage == DateHeader.ParseStage.Second && b != 58)
                        {
                            parseStage = DateHeader.ParseStage.Zone;
                        }
                        int num = 0;
                        do
                        {
                            num++;
                            array[(int)parseStage] *= 10;
                            array[(int)parseStage] += (int)(b2 - 48);
                            b2 = valueParser.ParseGet();
                        }while (b2 != 0 && MimeScan.IsDigit(b2));
                        if (b2 != 0)
                        {
                            valueParser.ParseUnget();
                        }
                        if (parseStage == DateHeader.ParseStage.Year && num <= 2)
                        {
                            array[(int)parseStage] += ((array[(int)parseStage] < 30) ? 2000 : 1900);
                        }
                        if (parseStage == DateHeader.ParseStage.Zone && num <= 2)
                        {
                            array[(int)parseStage] *= 100;
                        }
                        if (parseStage == DateHeader.ParseStage.Zone && b == 45)
                        {
                            array[(int)parseStage] = -array[(int)parseStage];
                        }
                        parseStage++;
                    }
                    else if (MimeScan.IsAlpha(b2))
                    {
                        valueParser.ParseUnget();
                        MimeString mimeString = valueParser.ParseToken(MimeScan.Token.Alpha);
                        if (parseStage == DateHeader.ParseStage.DayOfWeek)
                        {
                            parseStage = DateHeader.ParseStage.DayOfMonth;
                        }
                        else if (parseStage == DateHeader.ParseStage.Month)
                        {
                            array[(int)parseStage] = DateHeader.MapMonthToInt(mimeString.Data, mimeString.Offset, mimeString.Length);
                            parseStage             = DateHeader.ParseStage.Year;
                        }
                        else if (parseStage >= DateHeader.ParseStage.Second)
                        {
                            if (mimeString.Length == 2 && 65 == DateHeader.MakeUpper(mimeString[0]) && 77 == DateHeader.MakeUpper(mimeString[1]))
                            {
                                if (array[4] == 12)
                                {
                                    array[4] = 0;
                                }
                                parseStage = DateHeader.ParseStage.Zone;
                            }
                            else if (mimeString.Length == 2 && 80 == DateHeader.MakeUpper(mimeString[0]) && 77 == DateHeader.MakeUpper(mimeString[1]))
                            {
                                if (array[4] < 12)
                                {
                                    array[4] += 12;
                                }
                                parseStage = DateHeader.ParseStage.Zone;
                            }
                            else
                            {
                                array[7]   = (int)DateHeader.MapZoneToInt(mimeString.Data, mimeString.Offset, mimeString.Length);
                                parseStage = DateHeader.ParseStage.Count;
                            }
                        }
                    }
                    b = 32;
                }
            }
            if (parseStage > DateHeader.ParseStage.Year)
            {
                int num2 = array[3];
                int num3 = array[2];
                int num4 = array[1];
                int num5 = array[4];
                int num6 = array[5];
                int num7 = array[6];
                if (num5 == 23 && num6 == 59 && num7 == 60)
                {
                    num7 = 59;
                }
                if (num2 >= 1900 && num2 <= 9999 && num3 >= 1 && num3 <= 12 && num4 >= 1 && num4 <= DateTime.DaysInMonth(num2, num3) && num5 >= 0 && num5 < 24 && num6 >= 0 && num6 < 60 && num7 >= 0 && num7 < 60)
                {
                    try
                    {
                        utcDateTime = new DateTime(num2, num3, num4, num5, num6, num7, 0, DateTimeKind.Utc);
                        goto IL_319;
                    }
                    catch (ArgumentException)
                    {
                        utcDateTime = DateHeader.minDateTime;
                        goto IL_319;
                    }
                }
                utcDateTime = DateHeader.minDateTime;
            }
            else
            {
                utcDateTime = DateHeader.minDateTime;
            }
IL_319:
            if (parseStage != DateHeader.ParseStage.Count || !(utcDateTime > DateHeader.minDateTime))
            {
                timeZoneOffset = TimeSpan.Zero;
                return;
            }
            int num8 = array[7] / 100;
            int num9 = array[7] % 100;

            if (num8 > 99 || num8 < -99)
            {
                num8 = 0;
                num9 = 0;
            }
            if (num9 > 59 || num9 < -59)
            {
                num9 = 0;
            }
            timeZoneOffset = new TimeSpan(num8, num9, 0);
            if (utcDateTime.Ticks >= timeZoneOffset.Ticks && DateTime.MaxValue.Ticks >= utcDateTime.Ticks - timeZoneOffset.Ticks)
            {
                utcDateTime -= timeZoneOffset;
                return;
            }
            utcDateTime    = DateHeader.minDateTime;
            timeZoneOffset = TimeSpan.Zero;
        }
Beispiel #11
0
        private static int MapMonthToInt(byte[] ptr, int offset, int len)
        {
            byte b = DateHeader.MakeUpper(ptr[offset++]);

            if (b <= 70)
            {
                if (b != 65)
                {
                    switch (b)
                    {
                    case 68:
                        return(12);

                    case 70:
                        return(2);
                    }
                }
                else
                {
                    if (--len == 0 || 85 != DateHeader.MakeUpper(ptr[offset++]))
                    {
                        return(4);
                    }
                    return(8);
                }
            }
            else
            {
                switch (b)
                {
                case 74:
                    if (--len == 0 || 85 != DateHeader.MakeUpper(ptr[offset++]))
                    {
                        return(1);
                    }
                    if (--len == 0 || 76 != DateHeader.MakeUpper(ptr[offset++]))
                    {
                        return(6);
                    }
                    return(7);

                case 75:
                case 76:
                    break;

                case 77:
                    if (--len == 0 || 65 != DateHeader.MakeUpper(ptr[offset++]))
                    {
                        return(3);
                    }
                    if (--len == 0 || 89 != DateHeader.MakeUpper(ptr[offset++]))
                    {
                        return(3);
                    }
                    return(5);

                case 78:
                    return(11);

                case 79:
                    return(10);

                default:
                    if (b == 83)
                    {
                        return(9);
                    }
                    break;
                }
            }
            return(1);
        }
Beispiel #12
0
        private static DateHeader.TimeZoneToken MapZoneToInt(byte[] ptr, int offset, int len)
        {
            byte b = DateHeader.MakeUpper(ptr[offset++]);

            switch (b)
            {
            case 67:
                if (--len == 0 || 68 != DateHeader.MakeUpper(ptr[offset++]))
                {
                    return(DateHeader.TimeZoneToken.CST);
                }
                return(DateHeader.TimeZoneToken.EST);

            case 68:
            case 70:
            case 72:
            case 73:
            case 76:
                break;

            case 69:
                if (--len == 0 || 68 != DateHeader.MakeUpper(ptr[offset++]))
                {
                    return(DateHeader.TimeZoneToken.EST);
                }
                return(DateHeader.TimeZoneToken.EDT);

            case 71:
                return(DateHeader.TimeZoneToken.GMT);

            case 74:
                return(DateHeader.TimeZoneToken.KST);

            case 75:
                return(DateHeader.TimeZoneToken.KST);

            case 77:
                if (--len == 0 || 68 != DateHeader.MakeUpper(ptr[offset++]))
                {
                    return(DateHeader.TimeZoneToken.MST);
                }
                return(DateHeader.TimeZoneToken.CST);

            default:
                if (b != 80)
                {
                    if (b == 85)
                    {
                        return(DateHeader.TimeZoneToken.GMT);
                    }
                }
                else
                {
                    if (--len == 0 || 68 != DateHeader.MakeUpper(ptr[offset++]))
                    {
                        return(DateHeader.TimeZoneToken.PST);
                    }
                    return(DateHeader.TimeZoneToken.MST);
                }
                break;
            }
            return(DateHeader.TimeZoneToken.GMT);
        }