ASCIIEncoding.cs 9.8 KB

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