DerBoolean.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using Org.BouncyCastle.Utilities;
  4. namespace Org.BouncyCastle.Asn1
  5. {
  6. public class DerBoolean
  7. : Asn1Object
  8. {
  9. private readonly byte value;
  10. public static readonly DerBoolean False = new DerBoolean(false);
  11. public static readonly DerBoolean True = new DerBoolean(true);
  12. /**
  13. * return a bool from the passed in object.
  14. *
  15. * @exception ArgumentException if the object cannot be converted.
  16. */
  17. public static DerBoolean GetInstance(
  18. object obj)
  19. {
  20. if (obj == null || obj is DerBoolean)
  21. {
  22. return (DerBoolean) obj;
  23. }
  24. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  25. }
  26. /**
  27. * return a DerBoolean from the passed in bool.
  28. */
  29. public static DerBoolean GetInstance(
  30. bool value)
  31. {
  32. return value ? True : False;
  33. }
  34. /**
  35. * return a Boolean from a tagged object.
  36. *
  37. * @param obj the tagged object holding the object we want
  38. * @param explicitly true if the object is meant to be explicitly
  39. * tagged false otherwise.
  40. * @exception ArgumentException if the tagged object cannot
  41. * be converted.
  42. */
  43. public static DerBoolean GetInstance(
  44. Asn1TaggedObject obj,
  45. bool isExplicit)
  46. {
  47. Asn1Object o = obj.GetObject();
  48. if (isExplicit || o is DerBoolean)
  49. {
  50. return GetInstance(o);
  51. }
  52. return FromOctetString(((Asn1OctetString)o).GetOctets());
  53. }
  54. public DerBoolean(
  55. byte[] val)
  56. {
  57. if (val.Length != 1)
  58. throw new ArgumentException("byte value should have 1 byte in it", "val");
  59. // TODO Are there any constraints on the possible byte values?
  60. this.value = val[0];
  61. }
  62. private DerBoolean(
  63. bool value)
  64. {
  65. this.value = value ? (byte)0xff : (byte)0;
  66. }
  67. public bool IsTrue
  68. {
  69. get { return value != 0; }
  70. }
  71. internal override void Encode(
  72. DerOutputStream derOut)
  73. {
  74. // TODO Should we make sure the byte value is one of '0' or '0xff' here?
  75. derOut.WriteEncoded(Asn1Tags.Boolean, new byte[]{ value });
  76. }
  77. protected override bool Asn1Equals(
  78. Asn1Object asn1Object)
  79. {
  80. DerBoolean other = asn1Object as DerBoolean;
  81. if (other == null)
  82. return false;
  83. return IsTrue == other.IsTrue;
  84. }
  85. protected override int Asn1GetHashCode()
  86. {
  87. return IsTrue.GetHashCode();
  88. }
  89. public override string ToString()
  90. {
  91. return IsTrue ? "TRUE" : "FALSE";
  92. }
  93. internal static DerBoolean FromOctetString(byte[] value)
  94. {
  95. if (value.Length != 1)
  96. {
  97. throw new ArgumentException("BOOLEAN value should have 1 byte in it", "value");
  98. }
  99. byte b = value[0];
  100. return b == 0 ? False : b == 0xFF ? True : new DerBoolean(value);
  101. }
  102. }
  103. }
  104. #endif