Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches

◆ ParseGeneralizedTime()

static DateTimeOffset System.Formats.Asn1.AsnDecoder.ParseGeneralizedTime ( AsnEncodingRules ruleSet,
ReadOnlySpan< byte > contentOctets )
inlinestaticprivate

Definition at line 742 of file AsnDecoder.cs.

743 {
744 bool flag = ruleSet == AsnEncodingRules.DER || ruleSet == AsnEncodingRules.CER;
745 if (flag && contentOctets.Length < 15)
746 {
747 throw new AsnContentException(System.SR.ContentException_InvalidUnderCerOrDer_TryBer);
748 }
749 if (contentOctets.Length < 10)
750 {
751 throw new AsnContentException();
752 }
754 int year = ParseNonNegativeIntAndSlice(ref data, 4);
755 int month = ParseNonNegativeIntAndSlice(ref data, 2);
756 int day = ParseNonNegativeIntAndSlice(ref data, 2);
757 int hour = ParseNonNegativeIntAndSlice(ref data, 2);
758 int? num = null;
759 int? num2 = null;
760 ulong value = 0uL;
761 ulong num3 = 1uL;
762 byte b = byte.MaxValue;
763 TimeSpan? timeSpan = null;
764 bool flag2 = false;
765 byte b2 = 0;
766 while (b2 == 0 && data.Length != 0)
767 {
768 byte? b3 = GetNextState(data[0]);
769 if (!b3.HasValue)
770 {
771 if (!num.HasValue)
772 {
773 num = ParseNonNegativeIntAndSlice(ref data, 2);
774 continue;
775 }
776 if (num2.HasValue)
777 {
778 throw new AsnContentException();
779 }
781 }
782 else
783 {
784 b2 = b3.Value;
785 }
786 }
787 if (b2 == 1)
788 {
789 switch (data[0])
790 {
791 case 44:
792 if (flag)
793 {
794 throw new AsnContentException(System.SR.ContentException_InvalidUnderCerOrDer_TryBer);
795 }
796 break;
797 default:
798 throw new AsnContentException();
799 case 46:
800 break;
801 }
802 data = data.Slice(1);
803 if (data.IsEmpty)
804 {
805 throw new AsnContentException();
806 }
807 if (!Utf8Parser.TryParse(SliceAtMost(data, 12), out value, out int bytesConsumed, '\0') || bytesConsumed == 0)
808 {
809 throw new AsnContentException();
810 }
811 b = (byte)(value % 10);
812 for (int i = 0; i < bytesConsumed; i++)
813 {
814 num3 *= 10;
815 }
816 data = data.Slice(bytesConsumed);
817 uint value2;
818 while (Utf8Parser.TryParse(SliceAtMost(data, 9), out value2, out bytesConsumed, '\0'))
819 {
820 data = data.Slice(bytesConsumed);
821 b = (byte)(value2 % 10);
822 }
823 if (data.Length != 0)
824 {
825 byte? b4 = GetNextState(data[0]);
826 if (!b4.HasValue)
827 {
828 throw new AsnContentException();
829 }
830 b2 = b4.Value;
831 }
832 }
833 if (b2 == 2)
834 {
835 byte b5 = data[0];
836 data = data.Slice(1);
837 if (b5 == 90)
838 {
839 timeSpan = TimeSpan.Zero;
840 flag2 = true;
841 }
842 else
843 {
844 bool flag3 = b5 switch
845 {
846 43 => false,
847 45 => true,
848 _ => throw new AsnContentException(),
849 };
850 if (data.IsEmpty)
851 {
852 throw new AsnContentException();
853 }
855 int num4 = 0;
856 if (data.Length != 0)
857 {
859 }
860 if (num4 > 59)
861 {
862 throw new AsnContentException();
863 }
865 if (flag3)
866 {
868 }
870 }
871 }
872 if (!data.IsEmpty)
873 {
874 throw new AsnContentException();
875 }
876 if (flag)
877 {
878 if (!flag2 || !num2.HasValue)
879 {
880 throw new AsnContentException(System.SR.ContentException_InvalidUnderCerOrDer_TryBer);
881 }
882 if (b == 0)
883 {
884 throw new AsnContentException(System.SR.ContentException_InvalidUnderCerOrDer_TryBer);
885 }
886 }
887 double num5 = (double)value / (double)num3;
889 if (!num.HasValue)
890 {
891 num = 0;
892 num2 = 0;
893 if (value != 0L)
894 {
895 timeSpan3 = new TimeSpan((long)(num5 * 36000000000.0));
896 }
897 }
898 else if (!num2.HasValue)
899 {
900 num2 = 0;
901 if (value != 0L)
902 {
903 timeSpan3 = new TimeSpan((long)(num5 * 600000000.0));
904 }
905 }
906 else if (value != 0L)
907 {
908 timeSpan3 = new TimeSpan((long)(num5 * 10000000.0));
909 }
910 try
911 {
912 DateTimeOffset dateTimeOffset = (timeSpan.HasValue ? new DateTimeOffset(year, month, day, hour, num.Value, num2.Value, timeSpan.Value) : new DateTimeOffset(new DateTime(year, month, day, hour, num.Value, num2.Value)));
913 return dateTimeOffset + timeSpan3;
914 }
915 catch (Exception inner)
916 {
917 throw new AsnContentException(System.SR.ContentException_DefaultMessage, inner);
918 }
919 static byte? GetNextState(byte octet)
920 {
921 switch (octet)
922 {
923 case 43:
924 case 45:
925 case 90:
926 return 2;
927 case 44:
928 case 46:
929 return 1;
930 default:
931 return null;
932 }
933 }
934 }
static bool TryParse(ReadOnlySpan< byte > source, out bool value, out int bytesConsumed, char standardFormat='\0')
static ReadOnlySpan< byte > SliceAtMost(ReadOnlySpan< byte > source, int longestPermitted)
static int ParseNonNegativeIntAndSlice(ref ReadOnlySpan< byte > data, int bytesToRead)
static string ContentException_InvalidUnderCerOrDer_TryBer
Definition SR.cs:56
static string ContentException_DefaultMessage
Definition SR.cs:50
Definition SR.cs:7

References System.SR.ContentException_DefaultMessage, System.SR.ContentException_InvalidUnderCerOrDer_TryBer, System.Formats.Asn1.DateTime, System.L, System.Formats.Asn1.AsnDecoder.ParseNonNegativeIntAndSlice(), System.Formats.Asn1.AsnDecoder.SliceAtMost(), System.Buffers.Text.Utf8Parser.TryParse(), System.value, System.year, and System.TimeSpan.Zero.

Referenced by System.Formats.Asn1.AsnDecoder.ReadGeneralizedTime().