ASCIIEncoding.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. EncoderFallbackBuffer buffer = null;
  75. char [] fallback_chars = null;
  76. return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
  77. ref buffer, ref fallback_chars);
  78. }
  79. int GetBytes (char[] chars, int charIndex, int charCount,
  80. byte[] bytes, int byteIndex,
  81. ref EncoderFallbackBuffer buffer,
  82. ref char [] fallback_chars)
  83. {
  84. if (chars == null)
  85. throw new ArgumentNullException ("chars");
  86. unsafe {
  87. fixed (char *cptr = chars) {
  88. return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
  89. }
  90. }
  91. }
  92. // Convenience wrappers for "GetBytes".
  93. public override int GetBytes (String chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
  94. {
  95. EncoderFallbackBuffer buffer = null;
  96. char [] fallback_chars = null;
  97. return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
  98. ref buffer, ref fallback_chars);
  99. }
  100. int GetBytes (String chars, int charIndex, int charCount,
  101. byte[] bytes, int byteIndex,
  102. ref EncoderFallbackBuffer buffer,
  103. ref char [] fallback_chars)
  104. {
  105. if (chars == null)
  106. throw new ArgumentNullException ("chars");
  107. unsafe {
  108. fixed (char *cptr = chars) {
  109. return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
  110. }
  111. }
  112. }
  113. unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
  114. byte[] bytes, int byteIndex,
  115. ref EncoderFallbackBuffer buffer,
  116. ref char [] fallback_chars)
  117. {
  118. if (bytes == null)
  119. throw new ArgumentNullException ("bytes");
  120. if (charIndex < 0 || charIndex > charLength)
  121. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  122. if (charCount < 0 || charCount > (charLength - charIndex))
  123. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  124. if (byteIndex < 0 || byteIndex > bytes.Length)
  125. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  126. if ((bytes.Length - byteIndex) < charCount)
  127. throw new ArgumentException (_("Arg_InsufficientSpace"));
  128. int count = charCount;
  129. char ch;
  130. while (count-- > 0) {
  131. ch = chars [charIndex++];
  132. if (ch < (char)0x80) {
  133. bytes [byteIndex++] = (byte)ch;
  134. } else {
  135. if (buffer == null)
  136. buffer = EncoderFallback.CreateFallbackBuffer ();
  137. if (Char.IsSurrogate (ch) && count > 1 &&
  138. Char.IsSurrogate (chars [charIndex]))
  139. buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
  140. else
  141. buffer.Fallback (ch, charIndex - 1);
  142. if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
  143. fallback_chars = new char [buffer.Remaining];
  144. for (int i = 0; i < fallback_chars.Length; i++)
  145. fallback_chars [i] = buffer.GetNextChar ();
  146. byteIndex += GetBytes (fallback_chars, 0,
  147. fallback_chars.Length, bytes, byteIndex,
  148. ref buffer, ref fallback_chars);
  149. }
  150. }
  151. return charCount;
  152. }
  153. // Get the number of characters needed to decode a byte buffer.
  154. public override int GetCharCount (byte[] bytes, int index, int count)
  155. {
  156. if (bytes == null) {
  157. throw new ArgumentNullException ("bytes");
  158. }
  159. if (index < 0 || index > bytes.Length) {
  160. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  161. }
  162. if (count < 0 || count > (bytes.Length - index)) {
  163. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  164. }
  165. return count;
  166. }
  167. // Get the characters that result from decoding a byte buffer.
  168. public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  169. {
  170. DecoderFallbackBuffer buffer = null;
  171. return GetChars (bytes, byteIndex, byteCount, chars,
  172. charIndex, ref buffer);
  173. }
  174. int GetChars (byte[] bytes, int byteIndex, int byteCount,
  175. char[] chars, int charIndex,
  176. ref DecoderFallbackBuffer buffer)
  177. {
  178. if (bytes == null)
  179. throw new ArgumentNullException ("bytes");
  180. if (chars == null)
  181. throw new ArgumentNullException ("chars");
  182. if (byteIndex < 0 || byteIndex > bytes.Length)
  183. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  184. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
  185. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  186. if (charIndex < 0 || charIndex > chars.Length)
  187. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  188. if ((chars.Length - charIndex) < byteCount)
  189. throw new ArgumentException (_("Arg_InsufficientSpace"));
  190. int count = byteCount;
  191. while (count-- > 0) {
  192. char c = (char) bytes [byteIndex++];
  193. if (c < '\x80')
  194. chars [charIndex++] = c;
  195. else {
  196. if (buffer == null)
  197. buffer = DecoderFallback.CreateFallbackBuffer ();
  198. buffer.Fallback (bytes, byteIndex);
  199. while (buffer.Remaining > 0)
  200. chars [charIndex++] = buffer.GetNextChar ();
  201. }
  202. }
  203. return byteCount;
  204. }
  205. // Get the maximum number of bytes needed to encode a
  206. // specified number of characters.
  207. public override int GetMaxByteCount (int charCount)
  208. {
  209. if (charCount < 0) {
  210. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  211. }
  212. return charCount;
  213. }
  214. // Get the maximum number of characters needed to decode a
  215. // specified number of bytes.
  216. public override int GetMaxCharCount (int byteCount)
  217. {
  218. if (byteCount < 0) {
  219. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  220. }
  221. return byteCount;
  222. }
  223. // Decode a buffer of bytes into a string.
  224. public override String GetString (byte[] bytes, int byteIndex, int byteCount)
  225. {
  226. if (bytes == null) {
  227. throw new ArgumentNullException ("bytes");
  228. }
  229. if (byteIndex < 0 || byteIndex > bytes.Length) {
  230. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  231. }
  232. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  233. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  234. }
  235. if (byteCount == 0)
  236. return String.Empty;
  237. unsafe {
  238. fixed (byte* bytePtr = bytes) {
  239. string s = string.InternalAllocateStr (byteCount);
  240. fixed (char* charPtr = s) {
  241. byte* currByte = bytePtr + byteIndex;
  242. byte* lastByte = currByte + byteCount;
  243. char* currChar = charPtr;
  244. while (currByte < lastByte) {
  245. byte b = currByte++ [0];
  246. currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
  247. }
  248. }
  249. return s;
  250. }
  251. }
  252. }
  253. [CLSCompliantAttribute (false)]
  254. [ComVisible (false)]
  255. public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  256. {
  257. if (chars == null)
  258. throw new ArgumentNullException ("chars");
  259. if (bytes == null)
  260. throw new ArgumentNullException ("bytes");
  261. if (charCount < 0)
  262. throw new ArgumentOutOfRangeException ("charCount");
  263. if (byteCount < 0)
  264. throw new ArgumentOutOfRangeException ("byteCount");
  265. if (byteCount < charCount)
  266. throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
  267. for (int i = 0; i < charCount; i++){
  268. char c = chars [i];
  269. bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
  270. }
  271. return charCount;
  272. }
  273. [CLSCompliantAttribute(false)]
  274. [ComVisible (false)]
  275. public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  276. {
  277. if (bytes == null)
  278. throw new ArgumentNullException ("bytes");
  279. if (chars == null)
  280. throw new ArgumentNullException ("chars");
  281. if (charCount < 0)
  282. throw new ArgumentOutOfRangeException ("charCount");
  283. if (byteCount < 0)
  284. throw new ArgumentOutOfRangeException ("byteCount");
  285. if (charCount < byteCount)
  286. throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
  287. for (int i = 0; i < byteCount; i++){
  288. byte b = bytes [i];
  289. chars [i] = b > 127 ? '?' : (char) b;
  290. }
  291. return byteCount;
  292. }
  293. [CLSCompliantAttribute(false)]
  294. [ComVisible (false)]
  295. public unsafe override int GetCharCount (byte *bytes, int count)
  296. {
  297. return count;
  298. }
  299. [CLSCompliantAttribute(false)]
  300. [ComVisible (false)]
  301. public unsafe override int GetByteCount (char *chars, int count)
  302. {
  303. return count;
  304. }
  305. [ComVisible (false)]
  306. public override Decoder GetDecoder ()
  307. {
  308. return base.GetDecoder ();
  309. }
  310. [ComVisible (false)]
  311. public override Encoder GetEncoder ()
  312. {
  313. return base.GetEncoder ();
  314. }
  315. }; // class ASCIIEncoding
  316. }; // namespace System.Text