2
0

ASCIIEncoding.cs 12 KB

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