ASCIIEncoding.cs 10 KB

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