X509NameTokenizer.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System.Text;
  4. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Asn1.X509
  5. {
  6. /**
  7. * class for breaking up an X500 Name into it's component tokens, ala
  8. * java.util.StringTokenizer. We need this class as some of the
  9. * lightweight Java environment don't support classes like
  10. * StringTokenizer.
  11. */
  12. public class X509NameTokenizer
  13. {
  14. private string value;
  15. private int index;
  16. private char separator;
  17. private StringBuilder buffer = new StringBuilder();
  18. public X509NameTokenizer(
  19. string oid)
  20. : this(oid, ',')
  21. {
  22. }
  23. public X509NameTokenizer(
  24. string oid,
  25. char separator)
  26. {
  27. this.value = oid;
  28. this.index = -1;
  29. this.separator = separator;
  30. }
  31. public bool HasMoreTokens()
  32. {
  33. return index != value.Length;
  34. }
  35. public string NextToken()
  36. {
  37. if (index == value.Length)
  38. {
  39. return null;
  40. }
  41. int end = index + 1;
  42. bool quoted = false;
  43. bool escaped = false;
  44. buffer.Remove(0, buffer.Length);
  45. while (end != value.Length)
  46. {
  47. char c = value[end];
  48. if (c == '"')
  49. {
  50. if (!escaped)
  51. {
  52. quoted = !quoted;
  53. }
  54. else
  55. {
  56. buffer.Append(c);
  57. escaped = false;
  58. }
  59. }
  60. else
  61. {
  62. if (escaped || quoted)
  63. {
  64. if (c == '#' && buffer[buffer.Length - 1] == '=')
  65. {
  66. buffer.Append('\\');
  67. }
  68. else if (c == '+' && separator != '+')
  69. {
  70. buffer.Append('\\');
  71. }
  72. buffer.Append(c);
  73. escaped = false;
  74. }
  75. else if (c == '\\')
  76. {
  77. escaped = true;
  78. }
  79. else if (c == separator)
  80. {
  81. break;
  82. }
  83. else
  84. {
  85. buffer.Append(c);
  86. }
  87. }
  88. end++;
  89. }
  90. index = end;
  91. return buffer.ToString().Trim();
  92. }
  93. }
  94. }
  95. #pragma warning restore
  96. #endif