BasicAlphabetMapper.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Collections.Generic;
  5. namespace Best.HTTP.SecureProtocol.Org.BouncyCastle.Crypto.Utilities
  6. {
  7. /**
  8. * A basic alphabet mapper that just creates a mapper based on the
  9. * passed in array of characters.
  10. */
  11. public class BasicAlphabetMapper
  12. : IAlphabetMapper
  13. {
  14. private readonly IDictionary<char, int> m_indexMap = new Dictionary<char, int>();
  15. private readonly IList<char> m_charMap = new List<char>();
  16. /**
  17. * Base constructor.
  18. *
  19. * @param alphabet a string of characters making up the alphabet.
  20. */
  21. public BasicAlphabetMapper(string alphabet) :
  22. this(alphabet.ToCharArray())
  23. {
  24. }
  25. /**
  26. * Base constructor.
  27. *
  28. * @param alphabet an array of characters making up the alphabet.
  29. */
  30. public BasicAlphabetMapper(char[] alphabet)
  31. {
  32. for (int i = 0; i != alphabet.Length; i++)
  33. {
  34. if (m_indexMap.ContainsKey(alphabet[i]))
  35. throw new ArgumentException("duplicate key detected in alphabet: " + alphabet[i]);
  36. m_indexMap.Add(alphabet[i], i);
  37. m_charMap.Add(alphabet[i]);
  38. }
  39. }
  40. public int Radix
  41. {
  42. get { return m_charMap.Count; }
  43. }
  44. public byte[] ConvertToIndexes(char[] input)
  45. {
  46. byte[] outBuf;
  47. if (m_charMap.Count <= 256)
  48. {
  49. outBuf = new byte[input.Length];
  50. for (int i = 0; i != input.Length; i++)
  51. {
  52. if (!m_indexMap.TryGetValue(input[i], out var idx))
  53. throw new InvalidOperationException();
  54. outBuf[i] = (byte)idx;
  55. }
  56. }
  57. else
  58. {
  59. outBuf = new byte[input.Length * 2];
  60. for (int i = 0; i != input.Length; i++)
  61. {
  62. if (!m_indexMap.TryGetValue(input[i], out var idx))
  63. throw new InvalidOperationException();
  64. outBuf[i * 2 + 0] = (byte)(idx >> 8);
  65. outBuf[i * 2 + 1] = (byte)idx;
  66. }
  67. }
  68. return outBuf;
  69. }
  70. public char[] ConvertToChars(byte[] input)
  71. {
  72. char[] outBuf;
  73. if (m_charMap.Count <= 256)
  74. {
  75. outBuf = new char[input.Length];
  76. for (int i = 0; i != input.Length; i++)
  77. {
  78. outBuf[i] = m_charMap[input[i]];
  79. }
  80. }
  81. else
  82. {
  83. if ((input.Length & 0x1) != 0)
  84. {
  85. throw new ArgumentException("two byte radix and input string odd.Length");
  86. }
  87. outBuf = new char[input.Length / 2];
  88. for (int i = 0; i != input.Length; i += 2)
  89. {
  90. outBuf[i / 2] = m_charMap[(input[i] << 8) | input[i + 1]];
  91. }
  92. }
  93. return outBuf;
  94. }
  95. }
  96. }
  97. #pragma warning restore
  98. #endif