예제 #1
0
 private static void ParseValue(MimeStringList list, out System.DateTime utcDateTime, out System.TimeSpan timeZoneOffset)
 {
     var phrase = new MimeStringList();
     var valueParser = new ValueParser(list, false);
     var parseStage = ParseStage.DayOfWeek;
     var numArray = new int[8];
     byte num1 = 32;
     while (parseStage != ParseStage.Count) {
         valueParser.ParseCFWS(false, ref phrase, true);
         var ch = valueParser.ParseGet();
         if (ch != 0) {
             if (!MimeScan.IsToken(ch) || ch == 45 || ch == 43) {
                 num1 = ch;
                 valueParser.ParseCFWS(false, ref phrase, true);
             } else {
                 if (MimeScan.IsDigit(ch)) {
                     if (parseStage == ParseStage.DayOfWeek)
                         parseStage = ParseStage.DayOfMonth;
                     if (parseStage == ParseStage.Second && num1 != 58)
                         parseStage = ParseStage.Zone;
                     var num2 = 0;
                     do {
                         ++num2;
                         numArray[(int) parseStage] *= 10;
                         numArray[(int) parseStage] += ch - 48;
                         ch = valueParser.ParseGet();
                     } while (ch != 0 && MimeScan.IsDigit(ch));
                     if (ch != 0)
                         valueParser.ParseUnget();
                     if (parseStage == ParseStage.Year && num2 <= 2)
                         numArray[(int) parseStage] += numArray[(int) parseStage] < 30 ? 2000 : 1900;
                     if (parseStage == ParseStage.Zone && num2 <= 2)
                         numArray[(int) parseStage] *= 100;
                     if (parseStage == ParseStage.Zone && num1 == 45)
                         numArray[(int) parseStage] = -numArray[(int) parseStage];
                     ++parseStage;
                 } else if (MimeScan.IsAlpha(ch)) {
                     valueParser.ParseUnget();
                     var mimeString = valueParser.ParseToken(MimeScan.Token.Alpha);
                     if (parseStage == ParseStage.DayOfWeek)
                         parseStage = ParseStage.DayOfMonth;
                     else if (parseStage == ParseStage.Month) {
                         numArray[(int) parseStage] = DateHeader.MapMonthToInt(mimeString.Data, mimeString.Offset, mimeString.Length);
                         parseStage = ParseStage.Year;
                     } else if (parseStage >= ParseStage.Second) {
                         if (mimeString.Length == 2 && 65 == DateHeader.MakeUpper(mimeString[0]) && 77 == DateHeader.MakeUpper(mimeString[1])) {
                             if (numArray[4] == 12)
                                 numArray[4] = 0;
                             parseStage = ParseStage.Zone;
                         } else if (mimeString.Length == 2 && 80 == DateHeader.MakeUpper(mimeString[0]) && 77 == DateHeader.MakeUpper(mimeString[1])) {
                             if (numArray[4] < 12)
                                 numArray[4] += 12;
                             parseStage = ParseStage.Zone;
                         } else {
                             numArray[7] = (int) DateHeader.MapZoneToInt(mimeString.Data, mimeString.Offset, mimeString.Length);
                             parseStage = ParseStage.Count;
                         }
                     }
                 }
                 num1 = 32;
             }
         } else
             break;
     }
     if (parseStage > ParseStage.Year) {
         var year = numArray[3];
         var month = numArray[2];
         var day = numArray[1];
         var hour = numArray[4];
         var minute = numArray[5];
         var second = numArray[6];
         if (hour == 23 && minute == 59 && second == 60)
             second = 59;
         if (year >= 1900 && year <= 9999 && (month >= 1 && month <= 12) && (day >= 1 && day <= System.DateTime.DaysInMonth(year, month) && (hour >= 0 && hour < 24)) && (minute >= 0 && minute < 60 && second >= 0)) {
             if (second < 60) {
                 try {
                     utcDateTime = new System.DateTime(year, month, day, hour, minute, second, 0, System.DateTimeKind.Utc);
                     goto label_46;
                 } catch (System.ArgumentException ex) {
                     utcDateTime = minDateTime;
                     goto label_46;
                 }
             }
         }
         utcDateTime = minDateTime;
     } else
         utcDateTime = minDateTime;
     label_46:
     if (parseStage == ParseStage.Count && utcDateTime > minDateTime) {
         var hours = numArray[7]/100;
         var minutes = numArray[7]%100;
         if (hours > 99 || hours < -99) {
             hours = 0;
             minutes = 0;
         }
         if (minutes > 59 || minutes < -59)
             minutes = 0;
         timeZoneOffset = new System.TimeSpan(hours, minutes, 0);
         if (utcDateTime.Ticks >= timeZoneOffset.Ticks && System.DateTime.MaxValue.Ticks >= utcDateTime.Ticks - timeZoneOffset.Ticks)
             utcDateTime -= timeZoneOffset;
         else {
             utcDateTime = minDateTime;
             timeZoneOffset = System.TimeSpan.Zero;
         }
     } else
         timeZoneOffset = System.TimeSpan.Zero;
 }
예제 #2
0
 private void Parse()
 {
     this.Reset();
     parsed = true;
     var headerDecodingOptions = this.GetHeaderDecodingOptions();
     var valueParser = new ValueParser(this.Lines, headerDecodingOptions.AllowUTF8);
     var mimeStringList1 = new MimeStringList();
     var mimeStringList2 = new MimeStringList();
     var mimeString1 = new MimeString();
     var mimeString2 = MimeString.Empty;
     var parseState1 = ParseState.None;
     while (true) {
         valueParser.ParseWhitespace(true, ref mimeStringList1);
         var num1 = valueParser.ParseGet();
         if (num1 != 0) {
             if (59 == num1)
                 parseState1 = ParseState.Date;
             else if (40 == num1 && parseState1 == ParseState.DomainValue)
                 parseState1 = ParseState.DomainAddress;
             else {
                 valueParser.ParseUnget();
                 mimeString1 = valueParser.ParseToken();
                 if (mimeString1.Length == 0) {
                     int num2 = valueParser.ParseGet();
                     mimeStringList2.TakeOverAppend(ref mimeStringList1);
                     valueParser.ParseAppendLastByte(ref mimeStringList2);
                     continue;
                 }
                 var parseState2 = this.StateFromToken(mimeString1);
                 if (ParseState.None != parseState2)
                     parseState1 = parseState2;
             }
             switch (parseState1) {
                 case ParseState.Domain:
                 case ParseState.OptInfo:
                     if (mimeString2.Length != 0)
                         this.FinishClause(ref mimeString2, ref mimeStringList2, headerDecodingOptions.AllowUTF8);
                     else
                         mimeStringList2.Reset();
                     mimeString2 = mimeString1;
                     valueParser.ParseWhitespace(false, ref mimeStringList1);
                     mimeStringList1.Reset();
                     ++parseState1;
                     continue;
                 case ParseState.DomainValue:
                     mimeStringList2.TakeOverAppend(ref mimeStringList1);
                     mimeStringList2.AppendFragment(mimeString1);
                     continue;
                 case ParseState.DomainAddress:
                     var flag = mimeString2.CompareEqI(ParamFrom);
                     this.FinishClause(ref mimeString2, ref mimeStringList2, headerDecodingOptions.AllowUTF8);
                     mimeStringList1.Reset();
                     valueParser.ParseUnget();
                     valueParser.ParseComment(true, false, ref mimeStringList2, true);
                     var sz1 = mimeStringList2.GetSz();
                     var str = sz1 == null ? null : Internal.ByteString.BytesToString(sz1, headerDecodingOptions.AllowUTF8);
                     if (flag)
                         fromTcpInfoValue = str;
                     else
                         byTcpInfoValue = str;
                     mimeStringList2.Reset();
                     parseState1 = ParseState.None;
                     continue;
                 case ParseState.OptInfoValue:
                     mimeStringList2.TakeOverAppend(ref mimeStringList1);
                     mimeStringList2.AppendFragment(mimeString1);
                     continue;
                 case ParseState.Date:
                     this.FinishClause(ref mimeString2, ref mimeStringList2, headerDecodingOptions.AllowUTF8);
                     mimeStringList1.Reset();
                     valueParser.ParseWhitespace(false, ref mimeStringList1);
                     valueParser.ParseToEnd(ref mimeStringList2);
                     var sz2 = mimeStringList2.GetSz();
                     dateValue = sz2 == null ? null : Internal.ByteString.BytesToString(sz2, false);
                     continue;
                 case ParseState.None:
                     mimeStringList2.Reset();
                     continue;
                 default:
                     continue;
             }
         }
         break;
     }
     this.FinishClause(ref mimeString2, ref mimeStringList2, headerDecodingOptions.AllowUTF8);
 }