ASCIIEncoding.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. using System.Runtime.InteropServices;
  30. [Serializable]
  31. #if NET_2_0
  32. [ComVisible (true)]
  33. #endif
  34. [MonoTODO ("Serialization format not compatible with .NET")]
  35. public class ASCIIEncoding : Encoding
  36. {
  37. // Magic number used by Windows for "ASCII".
  38. internal const int ASCII_CODE_PAGE = 20127;
  39. // Constructor.
  40. public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
  41. body_name = header_name = web_name= "us-ascii";
  42. encoding_name = "US-ASCII";
  43. is_mail_news_display = true;
  44. is_mail_news_save = true;
  45. }
  46. #if NET_2_0
  47. [ComVisible (false)]
  48. public override bool IsSingleByte {
  49. get { return true; }
  50. }
  51. #endif
  52. // Get the number of bytes needed to encode a character buffer.
  53. public override int GetByteCount (char[] chars, int index, int count)
  54. {
  55. if (chars == null) {
  56. throw new ArgumentNullException ("chars");
  57. }
  58. if (index < 0 || index > chars.Length) {
  59. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  60. }
  61. if (count < 0 || count > (chars.Length - index)) {
  62. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  63. }
  64. return count;
  65. }
  66. // Convenience wrappers for "GetByteCount".
  67. public override int GetByteCount (String s)
  68. {
  69. if (s == null) {
  70. throw new ArgumentNullException ("s");
  71. }
  72. return s.Length;
  73. }
  74. // Get the bytes that result from encoding a character buffer.
  75. public override int GetBytes (char[] chars, int charIndex, int charCount,
  76. byte[] bytes, int byteIndex)
  77. {
  78. #if NET_2_0
  79. // well, yes, I know this #if is ugly, but I think it is the simplest switch.
  80. EncoderFallbackBuffer buffer = null;
  81. char [] fallback_chars = null;
  82. return GetBytes (chars, charIndex, charCount, bytes,
  83. byteIndex, ref buffer, ref fallback_chars);
  84. }
  85. int GetBytes (char[] chars, int charIndex, int charCount,
  86. byte[] bytes, int byteIndex,
  87. ref EncoderFallbackBuffer buffer,
  88. ref char [] fallback_chars)
  89. {
  90. #endif
  91. if (chars == null) {
  92. throw new ArgumentNullException ("chars");
  93. }
  94. if (bytes == null) {
  95. throw new ArgumentNullException ("bytes");
  96. }
  97. if (charIndex < 0 || charIndex > chars.Length) {
  98. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  99. }
  100. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  101. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  102. }
  103. if (byteIndex < 0 || byteIndex > bytes.Length) {
  104. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  105. }
  106. if ((bytes.Length - byteIndex) < charCount) {
  107. throw new ArgumentException (_("Arg_InsufficientSpace"));
  108. }
  109. int count = charCount;
  110. char ch;
  111. while (count-- > 0) {
  112. ch = chars [charIndex++];
  113. if (ch < (char)0x80) {
  114. bytes [byteIndex++] = (byte)ch;
  115. } else {
  116. #if NET_2_0
  117. if (buffer == null)
  118. buffer = EncoderFallback.CreateFallbackBuffer ();
  119. if (Char.IsSurrogate (ch) && count > 1 &&
  120. Char.IsSurrogate (chars [charIndex]))
  121. buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
  122. else
  123. buffer.Fallback (ch, charIndex - 1);
  124. if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
  125. fallback_chars = new char [buffer.Remaining];
  126. for (int i = 0; i < fallback_chars.Length; i++)
  127. fallback_chars [i] = buffer.GetNextChar ();
  128. byteIndex += GetBytes (fallback_chars, 0,
  129. fallback_chars.Length, bytes, byteIndex,
  130. ref buffer, ref fallback_chars);
  131. #else
  132. bytes [byteIndex++] = (byte)'?';
  133. #endif
  134. }
  135. }
  136. return charCount;
  137. }
  138. // Convenience wrappers for "GetBytes".
  139. public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
  140. {
  141. #if NET_2_0
  142. // I know this #if is ugly, but I think it is the simplest switch.
  143. EncoderFallbackBuffer buffer = null;
  144. char [] fallback_chars = null;
  145. return GetBytes (s, charIndex, charCount, bytes, byteIndex,
  146. ref buffer, ref fallback_chars);
  147. }
  148. int GetBytes (String s, int charIndex, int charCount,
  149. byte[] bytes, int byteIndex,
  150. ref EncoderFallbackBuffer buffer,
  151. ref char [] fallback_chars)
  152. {
  153. #endif
  154. if (s == null) {
  155. throw new ArgumentNullException ("s");
  156. }
  157. if (bytes == null) {
  158. throw new ArgumentNullException ("bytes");
  159. }
  160. if (charIndex < 0 || charIndex > s.Length) {
  161. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  162. }
  163. if (charCount < 0 || charCount > (s.Length - charIndex)) {
  164. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  165. }
  166. if (byteIndex < 0 || byteIndex > bytes.Length) {
  167. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  168. }
  169. if ((bytes.Length - byteIndex) < charCount) {
  170. throw new ArgumentException (_("Arg_InsufficientSpace"));
  171. }
  172. int count = charCount;
  173. char ch;
  174. while (count-- > 0) {
  175. ch = s [charIndex++];
  176. if (ch < (char)0x80) {
  177. bytes [byteIndex++] = (byte)ch;
  178. } else {
  179. #if NET_2_0
  180. if (buffer == null)
  181. buffer = EncoderFallback.CreateFallbackBuffer ();
  182. if (Char.IsSurrogate (ch) && count > 1 &&
  183. Char.IsSurrogate (s [charIndex]))
  184. buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
  185. else
  186. buffer.Fallback (ch, charIndex - 1);
  187. if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
  188. fallback_chars = new char [buffer.Remaining];
  189. for (int i = 0; i < fallback_chars.Length; i++)
  190. fallback_chars [i] = buffer.GetNextChar ();
  191. byteIndex += GetBytes (fallback_chars, 0,
  192. fallback_chars.Length, bytes, byteIndex,
  193. ref buffer, ref fallback_chars);
  194. #else
  195. bytes [byteIndex++] = (byte)'?';
  196. #endif
  197. }
  198. }
  199. return charCount;
  200. }
  201. // Get the number of characters needed to decode a byte buffer.
  202. public override int GetCharCount (byte[] bytes, int index, int count)
  203. {
  204. if (bytes == null) {
  205. throw new ArgumentNullException ("bytes");
  206. }
  207. if (index < 0 || index > bytes.Length) {
  208. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  209. }
  210. if (count < 0 || count > (bytes.Length - index)) {
  211. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  212. }
  213. return count;
  214. }
  215. // Get the characters that result from decoding a byte buffer.
  216. public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  217. {
  218. #if NET_2_0
  219. // well, yes, I know this #if is ugly, but I think it is the simplest switch.
  220. DecoderFallbackBuffer buffer = null;
  221. return GetChars (bytes, byteIndex, byteCount, chars,
  222. charIndex, ref buffer);
  223. }
  224. int GetChars (byte[] bytes, int byteIndex, int byteCount,
  225. char[] chars, int charIndex,
  226. ref DecoderFallbackBuffer buffer)
  227. {
  228. #endif
  229. if (bytes == null)
  230. throw new ArgumentNullException ("bytes");
  231. if (chars == null)
  232. throw new ArgumentNullException ("chars");
  233. if (byteIndex < 0 || byteIndex > bytes.Length)
  234. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  235. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
  236. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  237. if (charIndex < 0 || charIndex > chars.Length)
  238. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  239. if ((chars.Length - charIndex) < byteCount)
  240. throw new ArgumentException (_("Arg_InsufficientSpace"));
  241. int count = byteCount;
  242. while (count-- > 0) {
  243. char c = (char) bytes [byteIndex++];
  244. if (c < '\x80')
  245. chars [charIndex++] = c;
  246. else {
  247. #if NET_2_0
  248. if (buffer == null)
  249. buffer = DecoderFallback.CreateFallbackBuffer ();
  250. buffer.Fallback (bytes, byteIndex);
  251. while (buffer.Remaining > 0)
  252. chars [charIndex++] = buffer.GetNextChar ();
  253. #else
  254. chars [charIndex++] = '?';
  255. #endif
  256. }
  257. }
  258. return byteCount;
  259. }
  260. // Get the maximum number of bytes needed to encode a
  261. // specified number of characters.
  262. public override int GetMaxByteCount (int charCount)
  263. {
  264. if (charCount < 0) {
  265. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  266. }
  267. return charCount;
  268. }
  269. // Get the maximum number of characters needed to decode a
  270. // specified number of bytes.
  271. public override int GetMaxCharCount (int byteCount)
  272. {
  273. if (byteCount < 0) {
  274. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  275. }
  276. return byteCount;
  277. }
  278. // Decode a buffer of bytes into a string.
  279. public override String GetString (byte[] bytes, int index, int count)
  280. {
  281. if (bytes == null) {
  282. throw new ArgumentNullException ("bytes");
  283. }
  284. if (index < 0 || index > bytes.Length) {
  285. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  286. }
  287. if (count < 0 || count > (bytes.Length - index)) {
  288. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  289. }
  290. if (count == 0)
  291. return String.Empty;
  292. unsafe {
  293. fixed (byte* bytePtr = bytes) {
  294. string s = string.InternalAllocateStr (count);
  295. fixed (char* charPtr = s) {
  296. byte* currByte = bytePtr + index;
  297. byte* lastByte = currByte + count;
  298. char* currChar = charPtr;
  299. while (currByte < lastByte) {
  300. #if NET_2_0
  301. byte b = currByte++ [0];
  302. currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
  303. #else
  304. // GetString is incompatible with GetChars
  305. currChar++ [0] = (char) (currByte++ [0] & 0x7F);
  306. #endif
  307. }
  308. }
  309. return s;
  310. }
  311. }
  312. }
  313. #if NET_2_0
  314. [CLSCompliantAttribute (false)]
  315. [ComVisible (false)]
  316. public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  317. {
  318. if (chars == null)
  319. throw new ArgumentNullException ("chars");
  320. if (bytes == null)
  321. throw new ArgumentNullException ("bytes");
  322. if (charCount < 0)
  323. throw new ArgumentOutOfRangeException ("charCount");
  324. if (byteCount < 0)
  325. throw new ArgumentOutOfRangeException ("byteCount");
  326. if (byteCount < charCount)
  327. throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
  328. for (int i = 0; i < charCount; i++){
  329. char c = chars [i];
  330. bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
  331. }
  332. return charCount;
  333. }
  334. [CLSCompliantAttribute(false)]
  335. [ComVisible (false)]
  336. public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  337. {
  338. if (bytes == null)
  339. throw new ArgumentNullException ("bytes");
  340. if (chars == null)
  341. throw new ArgumentNullException ("chars");
  342. if (charCount < 0)
  343. throw new ArgumentOutOfRangeException ("charCount");
  344. if (byteCount < 0)
  345. throw new ArgumentOutOfRangeException ("byteCount");
  346. if (charCount < byteCount)
  347. throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
  348. for (int i = 0; i < byteCount; i++){
  349. byte b = bytes [i];
  350. chars [i] = b > 127 ? '?' : (char) b;
  351. }
  352. return byteCount;
  353. }
  354. [CLSCompliantAttribute(false)]
  355. [ComVisible (false)]
  356. public unsafe override int GetCharCount (byte *bytes, int count)
  357. {
  358. return count;
  359. }
  360. [CLSCompliantAttribute(false)]
  361. [ComVisible (false)]
  362. public unsafe override int GetByteCount (char *chars, int count)
  363. {
  364. return count;
  365. }
  366. #else
  367. // This routine is gone in 2.x
  368. public override String GetString (byte[] bytes)
  369. {
  370. if (bytes == null) {
  371. throw new ArgumentNullException ("bytes");
  372. }
  373. return GetString (bytes, 0, bytes.Length);
  374. }
  375. #endif
  376. #if NET_2_0
  377. [MonoTODO ("we have simple override to match method signature.")]
  378. [ComVisible (false)]
  379. public override Decoder GetDecoder ()
  380. {
  381. return base.GetDecoder ();
  382. }
  383. [MonoTODO ("we have simple override to match method signature.")]
  384. [ComVisible (false)]
  385. public override Encoder GetEncoder ()
  386. {
  387. return base.GetEncoder ();
  388. }
  389. #endif
  390. }; // class ASCIIEncoding
  391. }; // namespace System.Text