Accuracy.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.Tsp
  6. {
  7. public class Accuracy
  8. : Asn1Encodable
  9. {
  10. private readonly DerInteger seconds;
  11. private readonly DerInteger millis;
  12. private readonly DerInteger micros;
  13. // constants
  14. protected const int MinMillis = 1;
  15. protected const int MaxMillis = 999;
  16. protected const int MinMicros = 1;
  17. protected const int MaxMicros = 999;
  18. public Accuracy(
  19. DerInteger seconds,
  20. DerInteger millis,
  21. DerInteger micros)
  22. {
  23. if (null != millis)
  24. {
  25. int millisValue = millis.IntValueExact;
  26. if (millisValue < MinMillis || millisValue > MaxMillis)
  27. throw new ArgumentException("Invalid millis field : not in (1..999)");
  28. }
  29. if (null != micros)
  30. {
  31. int microsValue = micros.IntValueExact;
  32. if (microsValue < MinMicros || microsValue > MaxMicros)
  33. throw new ArgumentException("Invalid micros field : not in (1..999)");
  34. }
  35. this.seconds = seconds;
  36. this.millis = millis;
  37. this.micros = micros;
  38. }
  39. private Accuracy(
  40. Asn1Sequence seq)
  41. {
  42. for (int i = 0; i < seq.Count; ++i)
  43. {
  44. // seconds
  45. if (seq[i] is DerInteger)
  46. {
  47. seconds = (DerInteger) seq[i];
  48. }
  49. else if (seq[i] is Asn1TaggedObject)
  50. {
  51. Asn1TaggedObject extra = (Asn1TaggedObject)seq[i];
  52. switch (extra.TagNo)
  53. {
  54. case 0:
  55. millis = DerInteger.GetInstance(extra, false);
  56. int millisValue = millis.IntValueExact;
  57. if (millisValue < MinMillis || millisValue > MaxMillis)
  58. throw new ArgumentException("Invalid millis field : not in (1..999)");
  59. break;
  60. case 1:
  61. micros = DerInteger.GetInstance(extra, false);
  62. int microsValue = micros.IntValueExact;
  63. if (microsValue < MinMicros || microsValue > MaxMicros)
  64. throw new ArgumentException("Invalid micros field : not in (1..999)");
  65. break;
  66. default:
  67. throw new ArgumentException("Invalid tag number");
  68. }
  69. }
  70. }
  71. }
  72. public static Accuracy GetInstance(object obj)
  73. {
  74. if (obj is Accuracy)
  75. return (Accuracy)obj;
  76. if (obj == null)
  77. return null;
  78. return new Accuracy(Asn1Sequence.GetInstance(obj));
  79. }
  80. public DerInteger Seconds
  81. {
  82. get { return seconds; }
  83. }
  84. public DerInteger Millis
  85. {
  86. get { return millis; }
  87. }
  88. public DerInteger Micros
  89. {
  90. get { return micros; }
  91. }
  92. /**
  93. * <pre>
  94. * Accuracy ::= SEQUENCE {
  95. * seconds INTEGER OPTIONAL,
  96. * millis [0] INTEGER (1..999) OPTIONAL,
  97. * micros [1] INTEGER (1..999) OPTIONAL
  98. * }
  99. * </pre>
  100. */
  101. public override Asn1Object ToAsn1Object()
  102. {
  103. Asn1EncodableVector v = new Asn1EncodableVector();
  104. v.AddOptional(seconds);
  105. v.AddOptionalTagged(false, 0, millis);
  106. v.AddOptionalTagged(false, 1, micros);
  107. return new DerSequence(v);
  108. }
  109. }
  110. }
  111. #pragma warning restore
  112. #endif