ASCIIEncoding.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
  3. *
  4. * Copyright (c) 2001 Southern Storm Software, Pty Ltd
  5. * Copyright (C) 2003 Novell, Inc.
  6. * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. namespace System.Text
  27. {
  28. using System;
  29. [Serializable]
  30. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  31. public class ASCIIEncoding : Encoding
  32. {
  33. // Magic number used by Windows for "ASCII".
  34. internal const int ASCII_CODE_PAGE = 20127;
  35. // Constructor.
  36. public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
  37. body_name = header_name = web_name= "us-ascii";
  38. encoding_name = "US-ASCII";
  39. is_mail_news_display = true;
  40. is_mail_news_save = true;
  41. }
  42. #if NET_2_0
  43. public override bool IsSingleByte {
  44. get { return true; }
  45. }
  46. #endif
  47. // Get the number of bytes needed to encode a character buffer.
  48. public override int GetByteCount (char[] chars, int index, int count)
  49. {
  50. if (chars == null) {
  51. throw new ArgumentNullException ("chars");
  52. }
  53. if (index < 0 || index > chars.Length) {
  54. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  55. }
  56. if (count < 0 || count > (chars.Length - index)) {
  57. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  58. }
  59. return count;
  60. }
  61. // Convenience wrappers for "GetByteCount".
  62. public override int GetByteCount (String s)
  63. {
  64. if (s == null) {
  65. throw new ArgumentNullException ("s");
  66. }
  67. return s.Length;
  68. }
  69. // Get the bytes that result from encoding a character buffer.
  70. public override int GetBytes (char[] chars, int charIndex, int charCount,
  71. byte[] bytes, int byteIndex)
  72. {
  73. #if NET_2_0
  74. // well, yes, I know this #if is ugly, but I think it is the simplest switch.
  75. EncoderFallbackBuffer buffer = null;
  76. char [] fallback_chars = null;
  77. return GetBytes (chars, charIndex, charCount, bytes,
  78. byteIndex, ref buffer, ref fallback_chars);
  79. }
  80. int GetBytes (char[] chars, int charIndex, int charCount,
  81. byte[] bytes, int byteIndex,
  82. ref EncoderFallbackBuffer buffer,
  83. ref char [] fallback_chars)
  84. {
  85. #endif
  86. if (chars == null) {
  87. throw new ArgumentNullException ("chars");
  88. }
  89. if (bytes == null) {
  90. throw new ArgumentNullException ("bytes");
  91. }
  92. if (charIndex < 0 || charIndex > chars.Length) {
  93. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  94. }
  95. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  96. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  97. }
  98. if (byteIndex < 0 || byteIndex > bytes.Length) {
  99. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  100. }
  101. if ((bytes.Length - byteIndex) < charCount) {
  102. throw new ArgumentException (_("Arg_InsufficientSpace"));
  103. }
  104. int count = charCount;
  105. char ch;
  106. while (count-- > 0) {
  107. ch = chars [charIndex++];
  108. if (ch < (char)0x80) {
  109. bytes [byteIndex++] = (byte)ch;
  110. } else {
  111. #if NET_2_0
  112. if (buffer == null)
  113. buffer = EncoderFallback.CreateFallbackBuffer ();
  114. if (Char.IsSurrogate (ch) && count > 1 &&
  115. Char.IsSurrogate (chars [charIndex]))
  116. buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
  117. else
  118. buffer.Fallback (ch, charIndex - 1);
  119. if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
  120. fallback_chars = new char [buffer.Remaining];
  121. for (int i = 0; i < fallback_chars.Length; i++)
  122. fallback_chars [i] = buffer.GetNextChar ();
  123. byteIndex += GetBytes (fallback_chars, 0,
  124. fallback_chars.Length, bytes, byteIndex,
  125. ref buffer, ref fallback_chars);
  126. #else
  127. bytes [byteIndex++] = (byte)'?';
  128. #endif
  129. }
  130. }
  131. return charCount;
  132. }
  133. // Convenience wrappers for "GetBytes".
  134. public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
  135. {
  136. #if NET_2_0
  137. // I know this #if is ugly, but I think it is the simplest switch.
  138. EncoderFallbackBuffer buffer = null;
  139. char [] fallback_chars = null;
  140. return GetBytes (s, charIndex, charCount, bytes, byteIndex,
  141. ref buffer, ref fallback_chars);
  142. }
  143. int GetBytes (String s, int charIndex, int charCount,
  144. byte[] bytes, int byteIndex,
  145. ref EncoderFallbackBuffer buffer,
  146. ref char [] fallback_chars)
  147. {
  148. #endif
  149. if (s == null) {
  150. throw new ArgumentNullException ("s");
  151. }
  152. if (bytes == null) {
  153. throw new ArgumentNullException ("bytes");
  154. }
  155. if (charIndex < 0 || charIndex > s.Length) {
  156. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  157. }
  158. if (charCount < 0 || charCount > (s.Length - charIndex)) {
  159. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  160. }
  161. if (byteIndex < 0 || byteIndex > bytes.Length) {
  162. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  163. }
  164. if ((bytes.Length - byteIndex) < charCount) {
  165. throw new ArgumentException (_("Arg_InsufficientSpace"));
  166. }
  167. int count = charCount;
  168. char ch;
  169. while (count-- > 0) {
  170. ch = s [charIndex++];
  171. if (ch < (char)0x80) {
  172. bytes [byteIndex++] = (byte)ch;
  173. } else {
  174. #if NET_2_0
  175. if (buffer == null)
  176. buffer = EncoderFallback.CreateFallbackBuffer ();
  177. if (Char.IsSurrogate (ch) && count > 1 &&
  178. Char.IsSurrogate (s [charIndex]))
  179. buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
  180. else
  181. buffer.Fallback (ch, charIndex - 1);
  182. if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
  183. fallback_chars = new char [buffer.Remaining];
  184. for (int i = 0; i < fallback_chars.Length; i++)
  185. fallback_chars [i] = buffer.GetNextChar ();
  186. byteIndex += GetBytes (fallback_chars, 0,
  187. fallback_chars.Length, bytes, byteIndex,
  188. ref buffer, ref fallback_chars);
  189. #else
  190. bytes [byteIndex++] = (byte)'?';
  191. #endif
  192. }
  193. }
  194. return charCount;
  195. }
  196. // Get the number of characters needed to decode a byte buffer.
  197. public override int GetCharCount (byte[] bytes, int index, int count)
  198. {
  199. if (bytes == null) {
  200. throw new ArgumentNullException ("bytes");
  201. }
  202. if (index < 0 || index > bytes.Length) {
  203. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  204. }
  205. if (count < 0 || count > (bytes.Length - index)) {
  206. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  207. }
  208. return count;
  209. }
  210. // Get the characters that result from decoding a byte buffer.
  211. public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  212. {
  213. if (bytes == null)
  214. throw new ArgumentNullException ("bytes");
  215. if (chars == null)
  216. throw new ArgumentNullException ("chars");
  217. if (byteIndex < 0 || byteIndex > bytes.Length)
  218. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  219. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
  220. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  221. if (charIndex < 0 || charIndex > chars.Length)
  222. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  223. if ((chars.Length - charIndex) < byteCount)
  224. throw new ArgumentException (_("Arg_InsufficientSpace"));
  225. int count = byteCount;
  226. while (count-- > 0) {
  227. char c = (char)(bytes [byteIndex++]);
  228. if (c > 127)
  229. c = '?';
  230. chars [charIndex++] = c;
  231. }
  232. return byteCount;
  233. }
  234. // Get the maximum number of bytes needed to encode a
  235. // specified number of characters.
  236. public override int GetMaxByteCount (int charCount)
  237. {
  238. if (charCount < 0) {
  239. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  240. }
  241. return charCount;
  242. }
  243. // Get the maximum number of characters needed to decode a
  244. // specified number of bytes.
  245. public override int GetMaxCharCount (int byteCount)
  246. {
  247. if (byteCount < 0) {
  248. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  249. }
  250. return byteCount;
  251. }
  252. // Decode a buffer of bytes into a string.
  253. public override String GetString (byte[] bytes, int index, int count)
  254. {
  255. if (bytes == null) {
  256. throw new ArgumentNullException ("bytes");
  257. }
  258. if (index < 0 || index > bytes.Length) {
  259. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  260. }
  261. if (count < 0 || count > (bytes.Length - index)) {
  262. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  263. }
  264. if (count == 0)
  265. return String.Empty;
  266. unsafe {
  267. fixed (byte *ss = &bytes [0]) {
  268. return new String ((sbyte*)ss, index, count);
  269. }
  270. }
  271. }
  272. #if NET_2_0
  273. [CLSCompliantAttribute (false)]
  274. public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  275. {
  276. if (chars == null)
  277. throw new ArgumentNullException ("chars");
  278. if (bytes == null)
  279. throw new ArgumentNullException ("bytes");
  280. if (charCount < 0)
  281. throw new ArgumentOutOfRangeException ("charCount");
  282. if (byteCount < 0)
  283. throw new ArgumentOutOfRangeException ("byteCount");
  284. if (byteCount < charCount)
  285. throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
  286. for (int i = 0; i < charCount; i++){
  287. char c = chars [i];
  288. bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
  289. }
  290. return charCount;
  291. }
  292. [CLSCompliantAttribute(false)]
  293. public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  294. {
  295. if (bytes == null)
  296. throw new ArgumentNullException ("bytes");
  297. if (chars == null)
  298. throw new ArgumentNullException ("chars");
  299. if (charCount < 0)
  300. throw new ArgumentOutOfRangeException ("charCount");
  301. if (byteCount < 0)
  302. throw new ArgumentOutOfRangeException ("byteCount");
  303. if (charCount < byteCount)
  304. throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
  305. for (int i = 0; i < byteCount; i++){
  306. byte b = bytes [i];
  307. chars [i] = b > 127 ? '?' : (char) b;
  308. }
  309. return byteCount;
  310. }
  311. [CLSCompliantAttribute(false)]
  312. public unsafe override int GetCharCount (byte *bytes, int count)
  313. {
  314. return count;
  315. }
  316. [CLSCompliantAttribute(false)]
  317. public unsafe override int GetByteCount (char *chars, int count)
  318. {
  319. return count;
  320. }
  321. #else
  322. // This routine is gone in 2.x
  323. public override String GetString (byte[] bytes)
  324. {
  325. if (bytes == null) {
  326. throw new ArgumentNullException ("bytes");
  327. }
  328. int count = bytes.Length;
  329. if (count == 0)
  330. return String.Empty;
  331. unsafe {
  332. fixed (byte *ss = &bytes [0]) {
  333. return new String ((sbyte*)ss, 0, count);
  334. }
  335. }
  336. }
  337. #endif
  338. }; // class ASCIIEncoding
  339. }; // namespace System.Text