X509NameTokenizer.cs 2.4 KB

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