DerBMPString.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /**
  7. * Der BMPString object.
  8. */
  9. public class DerBmpString
  10. : DerStringBase
  11. {
  12. private readonly string str;
  13. /**
  14. * return a BMP string from the given object.
  15. *
  16. * @param obj the object we want converted.
  17. * @exception ArgumentException if the object cannot be converted.
  18. */
  19. public static DerBmpString GetInstance(
  20. object obj)
  21. {
  22. if (obj == null || obj is DerBmpString)
  23. {
  24. return (DerBmpString)obj;
  25. }
  26. throw new ArgumentException("illegal object in GetInstance: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj));
  27. }
  28. /**
  29. * return a BMP string from a tagged object.
  30. *
  31. * @param obj the tagged object holding the object we want
  32. * @param explicitly true if the object is meant to be explicitly
  33. * tagged false otherwise.
  34. * @exception ArgumentException if the tagged object cannot
  35. * be converted.
  36. */
  37. public static DerBmpString GetInstance(
  38. Asn1TaggedObject obj,
  39. bool isExplicit)
  40. {
  41. Asn1Object o = obj.GetObject();
  42. if (isExplicit || o is DerBmpString)
  43. {
  44. return GetInstance(o);
  45. }
  46. return new DerBmpString(Asn1OctetString.GetInstance(o).GetOctets());
  47. }
  48. /**
  49. * basic constructor - byte encoded string.
  50. */
  51. public DerBmpString(
  52. byte[] str)
  53. {
  54. if (str == null)
  55. throw new ArgumentNullException("str");
  56. char[] cs = new char[str.Length / 2];
  57. for (int i = 0; i != cs.Length; i++)
  58. {
  59. cs[i] = (char)((str[2 * i] << 8) | (str[2 * i + 1] & 0xff));
  60. }
  61. this.str = new string(cs);
  62. }
  63. /**
  64. * basic constructor
  65. */
  66. public DerBmpString(
  67. string str)
  68. {
  69. if (str == null)
  70. throw new ArgumentNullException("str");
  71. this.str = str;
  72. }
  73. public override string GetString()
  74. {
  75. return str;
  76. }
  77. protected override bool Asn1Equals(
  78. Asn1Object asn1Object)
  79. {
  80. DerBmpString other = asn1Object as DerBmpString;
  81. if (other == null)
  82. return false;
  83. return this.str.Equals(other.str);
  84. }
  85. internal override void Encode(
  86. DerOutputStream derOut)
  87. {
  88. char[] c = str.ToCharArray();
  89. byte[] b = new byte[c.Length * 2];
  90. for (int i = 0; i != c.Length; i++)
  91. {
  92. b[2 * i] = (byte)(c[i] >> 8);
  93. b[2 * i + 1] = (byte)c[i];
  94. }
  95. derOut.WriteEncoded(Asn1Tags.BmpString, b);
  96. }
  97. }
  98. }
  99. #endif