ASCIIEncoding.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
  3. *
  4. * Copyright (C) 2001 Southern Storm Software, Pty Ltd.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. using System;
  21. using System.Text;
  22. namespace Renci.SshNet.Common
  23. {
  24. /// <summary>
  25. /// Represents an ASCII character encoding of Unicode characters.
  26. /// </summary>
  27. public partial class ASCIIEncoding : Encoding
  28. {
  29. // Magic number used by Windows for "ASCII".
  30. internal const int ASCII_CODE_PAGE = 20127;
  31. private static ASCIIEncoding _current = new ASCIIEncoding();
  32. /// <summary>
  33. /// Gets the current encoding.
  34. /// </summary>
  35. public static ASCIIEncoding Current
  36. {
  37. get
  38. {
  39. return _current;
  40. }
  41. }
  42. /// <summary>
  43. /// Calculates the number of bytes produced by encoding a set of characters from the specified character array.
  44. /// </summary>
  45. /// <param name="chars">The character array containing the set of characters to encode.</param>
  46. /// <param name="index">The index of the first character to encode.</param>
  47. /// <param name="count">The number of characters to encode.</param>
  48. /// <returns>
  49. /// The number of bytes produced by encoding the specified characters.
  50. /// </returns>
  51. /// <exception cref="T:System.ArgumentNullException">
  52. /// <paramref name="chars"/> is null. </exception>
  53. ///
  54. /// <exception cref="T:System.ArgumentOutOfRangeException">
  55. /// <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>
  56. ///
  57. /// <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>
  58. public override int GetByteCount(char[] chars, int index, int count)
  59. {
  60. if (chars == null)
  61. {
  62. throw new ArgumentNullException("chars");
  63. }
  64. if (index < 0 || index > chars.Length)
  65. {
  66. throw new ArgumentOutOfRangeException("index");
  67. }
  68. if (count < 0 || count > (chars.Length - index))
  69. {
  70. throw new ArgumentOutOfRangeException("count");
  71. }
  72. return count;
  73. }
  74. /// <summary>
  75. /// Gets the byte count.
  76. /// </summary>
  77. /// <param name="chars">The chars.</param>
  78. /// <returns></returns>
  79. public override int GetByteCount(string chars)
  80. {
  81. if (chars == null)
  82. {
  83. throw new ArgumentNullException("chars");
  84. }
  85. return chars.Length;
  86. }
  87. /// <summary>
  88. /// Encodes a set of characters from the specified character array into the specified byte array.
  89. /// </summary>
  90. /// <param name="chars">The character array containing the set of characters to encode.</param>
  91. /// <param name="charIndex">The index of the first character to encode.</param>
  92. /// <param name="charCount">The number of characters to encode.</param>
  93. /// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
  94. /// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
  95. /// <returns>
  96. /// The actual number of bytes written into <paramref name="bytes"/>.
  97. /// </returns>
  98. /// <exception cref="T:System.ArgumentNullException">
  99. /// <paramref name="chars"/> is null.-or- <paramref name="bytes"/> is null. </exception>
  100. ///
  101. /// <exception cref="T:System.ArgumentOutOfRangeException">
  102. /// <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>
  103. ///
  104. /// <exception cref="T:System.ArgumentException">
  105. /// <paramref name="bytes"/> does not have enough capacity from <paramref name="byteIndex"/> to the end of the array to accommodate the resulting bytes. </exception>
  106. ///
  107. /// <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>
  108. public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
  109. {
  110. if (chars == null)
  111. {
  112. throw new ArgumentNullException("chars");
  113. }
  114. if (bytes == null)
  115. {
  116. throw new ArgumentNullException("bytes");
  117. }
  118. if (charIndex < 0 || charIndex > chars.Length)
  119. {
  120. throw new ArgumentOutOfRangeException("charIndex");
  121. }
  122. if (charCount < 0 || charCount > (chars.Length - charIndex))
  123. {
  124. throw new ArgumentOutOfRangeException("charCount");
  125. }
  126. if (byteIndex < 0 || byteIndex > bytes.Length)
  127. {
  128. throw new ArgumentOutOfRangeException("byteIndex");
  129. }
  130. if ((bytes.Length - byteIndex) < charCount)
  131. {
  132. throw new ArgumentException();
  133. }
  134. int count = charCount;
  135. char ch;
  136. while (count-- > 0)
  137. {
  138. ch = chars[charIndex++];
  139. if (ch < (char)0x80)
  140. {
  141. bytes[byteIndex++] = (byte)ch;
  142. }
  143. else
  144. {
  145. bytes[byteIndex++] = (byte)'?';
  146. }
  147. }
  148. return charCount;
  149. }
  150. /// <summary>
  151. /// Encodes a set of characters from the specified <see cref="T:System.String"/> into the specified byte array.
  152. /// </summary>
  153. /// <param name="s">The <see cref="T:System.String"/> containing the set of characters to encode.</param>
  154. /// <param name="charIndex">The index of the first character to encode.</param>
  155. /// <param name="charCount">The number of characters to encode.</param>
  156. /// <param name="bytes">The byte array to contain the resulting sequence of bytes.</param>
  157. /// <param name="byteIndex">The index at which to start writing the resulting sequence of bytes.</param>
  158. /// <returns>
  159. /// The actual number of bytes written into <paramref name="bytes"/>.
  160. /// </returns>
  161. /// <exception cref="T:System.ArgumentNullException">
  162. /// <paramref name="s"/> is null.-or- <paramref name="bytes"/> is null. </exception>
  163. ///
  164. /// <exception cref="T:System.ArgumentOutOfRangeException">
  165. /// <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="bytes"/>.-or- <paramref name="byteIndex"/> is not a valid index in <paramref name="bytes"/>. </exception>
  166. ///
  167. /// <exception cref="T:System.ArgumentException">
  168. /// <paramref name="bytes"/> does not have enough capacity from <paramref name="byteIndex"/> to the end of the array to accommodate the resulting bytes. </exception>
  169. ///
  170. /// <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>
  171. public override int GetBytes(String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
  172. {
  173. if (s == null)
  174. {
  175. throw new ArgumentNullException("s");
  176. }
  177. if (bytes == null)
  178. {
  179. throw new ArgumentNullException("bytes");
  180. }
  181. if (charIndex < 0 || charIndex > s.Length)
  182. {
  183. throw new ArgumentOutOfRangeException("charIndex");
  184. }
  185. if (charCount < 0 || charCount > (s.Length - charIndex))
  186. {
  187. throw new ArgumentOutOfRangeException("charCount");
  188. }
  189. if (byteIndex < 0 || byteIndex > bytes.Length)
  190. {
  191. throw new ArgumentOutOfRangeException("byteIndex");
  192. }
  193. if ((bytes.Length - byteIndex) < charCount)
  194. {
  195. throw new ArgumentException();
  196. }
  197. int count = charCount;
  198. char ch;
  199. while (count-- > 0)
  200. {
  201. ch = s[charIndex++];
  202. if (ch < (char)0x80)
  203. {
  204. bytes[byteIndex++] = (byte)ch;
  205. }
  206. else
  207. {
  208. bytes[byteIndex++] = (byte)'?';
  209. }
  210. }
  211. return charCount;
  212. }
  213. /// <summary>
  214. /// Calculates the number of characters produced by decoding a sequence of bytes from the specified byte array.
  215. /// </summary>
  216. /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
  217. /// <param name="index">The index of the first byte to decode.</param>
  218. /// <param name="count">The number of bytes to decode.</param>
  219. /// <returns>
  220. /// The number of characters produced by decoding the specified sequence of bytes.
  221. /// </returns>
  222. /// <exception cref="T:System.ArgumentNullException">
  223. /// <paramref name="bytes"/> is null. </exception>
  224. ///
  225. /// <exception cref="T:System.ArgumentOutOfRangeException">
  226. /// <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>
  227. ///
  228. /// <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>
  229. public override int GetCharCount(byte[] bytes, int index, int count)
  230. {
  231. if (bytes == null)
  232. {
  233. throw new ArgumentNullException("bytes");
  234. }
  235. if (index < 0 || index > bytes.Length)
  236. {
  237. throw new ArgumentOutOfRangeException("index");
  238. }
  239. if (count < 0 || count > (bytes.Length - index))
  240. {
  241. throw new ArgumentOutOfRangeException("count");
  242. }
  243. return count;
  244. }
  245. /// <summary>
  246. /// Decodes a sequence of bytes from the specified byte array into the specified character array.
  247. /// </summary>
  248. /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
  249. /// <param name="byteIndex">The index of the first byte to decode.</param>
  250. /// <param name="byteCount">The number of bytes to decode.</param>
  251. /// <param name="chars">The character array to contain the resulting set of characters.</param>
  252. /// <param name="charIndex">The index at which to start writing the resulting set of characters.</param>
  253. /// <returns>
  254. /// The actual number of characters written into <paramref name="chars"/>.
  255. /// </returns>
  256. /// <exception cref="T:System.ArgumentNullException">
  257. /// <paramref name="bytes"/> is null.-or- <paramref name="chars"/> is null. </exception>
  258. ///
  259. /// <exception cref="T:System.ArgumentOutOfRangeException">
  260. /// <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>
  261. ///
  262. /// <exception cref="T:System.ArgumentException">
  263. /// <paramref name="chars"/> does not have enough capacity from <paramref name="charIndex"/> to the end of the array to accommodate the resulting characters. </exception>
  264. ///
  265. /// <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>
  266. public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  267. {
  268. if (bytes == null)
  269. {
  270. throw new ArgumentNullException("bytes");
  271. }
  272. if (chars == null)
  273. {
  274. throw new ArgumentNullException("chars");
  275. }
  276. if (byteIndex < 0 || byteIndex > bytes.Length)
  277. {
  278. throw new ArgumentOutOfRangeException("byteIndex");
  279. }
  280. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
  281. {
  282. throw new ArgumentOutOfRangeException("byteCount");
  283. }
  284. if (charIndex < 0 || charIndex > chars.Length)
  285. {
  286. throw new ArgumentOutOfRangeException("charIndex");
  287. }
  288. if ((chars.Length - charIndex) < byteCount)
  289. {
  290. throw new ArgumentException();
  291. }
  292. int count = byteCount;
  293. byte ch;
  294. while (count-- > 0)
  295. {
  296. ch = bytes[byteIndex++];
  297. if (ch < 0x80)
  298. {
  299. chars[charIndex++] = (char)ch;
  300. }
  301. else
  302. {
  303. chars[charIndex++] = '?';
  304. }
  305. }
  306. return byteCount;
  307. }
  308. /// <summary>
  309. /// Calculates the maximum number of bytes produced by encoding the specified number of characters.
  310. /// </summary>
  311. /// <param name="charCount">The number of characters to encode.</param>
  312. /// <returns>
  313. /// The maximum number of bytes produced by encoding the specified number of characters.
  314. /// </returns>
  315. /// <exception cref="T:System.ArgumentOutOfRangeException">
  316. /// <paramref name="charCount"/> is less than zero. </exception>
  317. ///
  318. /// <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>
  319. public override int GetMaxByteCount(int charCount)
  320. {
  321. if (charCount < 0)
  322. {
  323. throw new ArgumentOutOfRangeException("charCount");
  324. }
  325. return charCount;
  326. }
  327. /// <summary>
  328. /// Calculates the maximum number of characters produced by decoding the specified number of bytes.
  329. /// </summary>
  330. /// <param name="byteCount">The number of bytes to decode.</param>
  331. /// <returns>
  332. /// The maximum number of characters produced by decoding the specified number of bytes.
  333. /// </returns>
  334. /// <exception cref="T:System.ArgumentOutOfRangeException">
  335. /// <paramref name="byteCount"/> is less than zero. </exception>
  336. ///
  337. /// <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>
  338. public override int GetMaxCharCount(int byteCount)
  339. {
  340. if (byteCount < 0)
  341. {
  342. throw new ArgumentOutOfRangeException("byteCount");
  343. }
  344. return byteCount;
  345. }
  346. /// <summary>
  347. /// Decodes a sequence of bytes from the specified byte array into a string.
  348. /// </summary>
  349. /// <param name="bytes">The byte array containing the sequence of bytes to decode.</param>
  350. /// <param name="index">The index of the first byte to decode.</param>
  351. /// <param name="count">The number of bytes to decode.</param>
  352. /// <returns>
  353. /// A <see cref="T:System.String"/> containing the results of decoding the specified sequence of bytes.
  354. /// </returns>
  355. /// <exception cref="T:System.ArgumentException">The byte array contains invalid Unicode code points.</exception>
  356. ///
  357. /// <exception cref="T:System.ArgumentNullException">
  358. /// <paramref name="bytes"/> is null. </exception>
  359. ///
  360. /// <exception cref="T:System.ArgumentOutOfRangeException">
  361. /// <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>
  362. ///
  363. /// <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>
  364. public override String GetString(byte[] bytes, int index, int count)
  365. {
  366. if (bytes == null)
  367. {
  368. throw new ArgumentNullException("bytes");
  369. }
  370. if (index < 0 || index > bytes.Length)
  371. {
  372. throw new ArgumentOutOfRangeException("index");
  373. }
  374. if (count < 0 || count > (bytes.Length - index))
  375. {
  376. throw new ArgumentOutOfRangeException("count");
  377. }
  378. if (count == 0)
  379. {
  380. return String.Empty;
  381. }
  382. char[] chars = new char[count];
  383. int posn = 0;
  384. while (count-- > 0)
  385. {
  386. chars[posn++] = (char)(bytes[index++]);
  387. }
  388. return new string(chars);
  389. }
  390. };
  391. }