ASCIIEncoding.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Renci.SshNet.Common
  6. {
  7. /// <summary>
  8. ///
  9. /// </summary>
  10. public class ASCIIEncoding : Encoding
  11. {
  12. private readonly char _fallbackChar;
  13. private static char[] _byteToChar;
  14. static ASCIIEncoding()
  15. {
  16. if (_byteToChar == null)
  17. {
  18. _byteToChar = new char[128];
  19. var ch = '\0';
  20. for (byte i = 0; i < 128; i++)
  21. {
  22. _byteToChar[i] = ch++;
  23. }
  24. }
  25. }
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="ASCIIEncoding"/> class.
  28. /// </summary>
  29. public ASCIIEncoding()
  30. {
  31. this._fallbackChar = '?';
  32. }
  33. /// <summary>
  34. /// Calculates the number of bytes produced by encoding a set of characters from the specified character array.
  35. /// </summary>
  36. /// <param name="chars">The character array containing the set of characters to encode.</param>
  37. /// <param name="index">The index of the first character to encode.</param>
  38. /// <param name="count">The number of characters to encode.</param>
  39. /// <returns>
  40. /// The number of bytes produced by encoding the specified characters.
  41. /// </returns>
  42. /// <exception cref="T:System.ArgumentNullException">
  43. /// <paramref name="chars"/> is null. </exception>
  44. ///
  45. /// <exception cref="T:System.ArgumentOutOfRangeException">
  46. /// <paramref name="index"/> or <paramref name="count"/> is less than zero.-or- <paramref name="index"/> and <paramref name="count"/> do not denote a valid range in <paramref name="chars"/>. </exception>
  47. ///
  48. /// <exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>.</exception>
  49. public override int GetByteCount(char[] chars, int index, int count)
  50. {
  51. return count;
  52. }
  53. /// <summary>
  54. /// Encodes a set of characters from the specified character array into the specified byte array.
  55. /// </summary>
  56. /// <param name="chars">The character array containing the set of characters to encode.</param>
  57. /// <param name="charIndex">The index of the first character to encode.</param>
  58. /// <param name="charCount">The number of characters to encode.</param>
  59. /// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
  60. /// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
  61. /// <returns>
  62. /// The actual number of bytes written into <paramref name="bytes"/>.
  63. /// </returns>
  64. /// <exception cref="T:System.ArgumentNullException">
  65. /// <paramref name="chars"/> is null.-or- <paramref name="bytes"/> is null. </exception>
  66. ///
  67. /// <exception cref="T:System.ArgumentOutOfRangeException">
  68. /// <paramref name="charIndex"/> or <paramref name="charCount"/> or <paramref name="byteIndex"/> is less than zero.-or- <paramref name="charIndex"/> and <paramref name="charCount"/> do not denote a valid range in <paramref name="chars"/>.-or- <paramref name="byteIndex"/> is not a valid index in <paramref name="bytes"/>. </exception>
  69. ///
  70. /// <exception cref="T:System.ArgumentException">
  71. /// <paramref name="bytes"/> does not have enough capacity from <paramref name="byteIndex"/> to the end of the array to accommodate the resulting bytes. </exception>
  72. ///
  73. /// <exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>.</exception>
  74. public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
  75. {
  76. for (int i = 0; i < charCount; i++)
  77. {
  78. var b = (byte)chars[i + charIndex];
  79. if (b > 127)
  80. b = (byte)this._fallbackChar;
  81. bytes[i + byteIndex] = b;
  82. }
  83. return charCount;
  84. }
  85. /// <summary>
  86. /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
  87. /// </summary>
  88. /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
  89. /// <param name="index">The index of the first byte to decode.</param>
  90. /// <param name="count">The number of bytes to decode.</param>
  91. /// <returns>
  92. /// The number of characters produced by decoding the specified sequence of bytes.
  93. /// </returns>
  94. /// <exception cref="T:System.ArgumentNullException">
  95. /// <paramref name="bytes"/> is null. </exception>
  96. ///
  97. /// <exception cref="T:System.ArgumentOutOfRangeException">
  98. /// <paramref name="index"/> or <paramref name="count"/> is less than zero.-or- <paramref name="index"/> and <paramref name="count"/> do not denote a valid range in <paramref name="bytes"/>. </exception>
  99. ///
  100. /// <exception cref="T:System.Text.DecoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.DecoderFallback"/> is set to <see cref="T:System.Text.DecoderExceptionFallback"/>.</exception>
  101. public override int GetCharCount(byte[] bytes, int index, int count)
  102. {
  103. return count;
  104. }
  105. /// <summary>
  106. /// Decodes a sequence of bytes from the specified byte array into the specified character array.
  107. /// </summary>
  108. /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
  109. /// <param name="byteIndex">The index of the first byte to decode.</param>
  110. /// <param name="byteCount">The number of bytes to decode.</param>
  111. /// <param name="chars">The character array to contain the resulting set of characters.</param>
  112. /// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
  113. /// <returns>
  114. /// The actual number of characters written into <paramref name="chars"/>.
  115. /// </returns>
  116. /// <exception cref="T:System.ArgumentNullException">
  117. /// <paramref name="bytes"/> is null.-or- <paramref name="chars"/> is null. </exception>
  118. ///
  119. /// <exception cref="T:System.ArgumentOutOfRangeException">
  120. /// <paramref name="byteIndex"/> or <paramref name="byteCount"/> or <paramref name="charIndex"/> is less than zero.-or- <paramref name="byteIndex"/> and <paramref name="byteCount"/> do not denote a valid range in <paramref name="bytes"/>.-or- <paramref name="charIndex"/> is not a valid index in <paramref name="chars"/>. </exception>
  121. ///
  122. /// <exception cref="T:System.ArgumentException">
  123. /// <paramref name="chars"/> does not have enough capacity from <paramref name="charIndex"/> to the end of the array to accommodate the resulting characters. </exception>
  124. ///
  125. /// <exception cref="T:System.Text.DecoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.DecoderFallback"/> is set to <see cref="T:System.Text.DecoderExceptionFallback"/>.</exception>
  126. public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  127. {
  128. for (int i = 0; i < byteCount; i++)
  129. {
  130. var b = bytes[i + byteIndex];
  131. char ch;
  132. if (b > 127)
  133. {
  134. ch = this._fallbackChar;
  135. }
  136. else
  137. {
  138. ch = _byteToChar[b];
  139. }
  140. chars[i + charIndex] = ch;
  141. }
  142. return byteCount;
  143. }
  144. /// <summary>
  145. /// Calculates the maximum number of bytes produced by encoding the specified number of characters.
  146. /// </summary>
  147. /// <param name="charCount">The number of characters to encode.</param>
  148. /// <returns>
  149. /// The maximum number of bytes produced by encoding the specified number of characters.
  150. /// </returns>
  151. /// <exception cref="T:System.ArgumentOutOfRangeException">
  152. /// <paramref name="charCount"/> is less than zero. </exception>
  153. ///
  154. /// <exception cref="T:System.Text.EncoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.EncoderFallback"/> is set to <see cref="T:System.Text.EncoderExceptionFallback"/>.</exception>
  155. public override int GetMaxByteCount(int charCount)
  156. {
  157. return charCount;
  158. }
  159. /// <summary>
  160. /// Calculates the maximum number of characters produced by decoding the specified number of bytes.
  161. /// </summary>
  162. /// <param name="byteCount">The number of bytes to decode.</param>
  163. /// <returns>
  164. /// The maximum number of characters produced by decoding the specified number of bytes.
  165. /// </returns>
  166. /// <exception cref="T:System.ArgumentOutOfRangeException">
  167. /// <paramref name="byteCount"/> is less than zero. </exception>
  168. ///
  169. /// <exception cref="T:System.Text.DecoderFallbackException">A fallback occurred (see Understanding Encodings for complete explanation)-and-<see cref="P:System.Text.Encoding.DecoderFallback"/> is set to <see cref="T:System.Text.DecoderExceptionFallback"/>.</exception>
  170. public override int GetMaxCharCount(int byteCount)
  171. {
  172. return byteCount;
  173. }
  174. }
  175. }