ASCIIEncoding.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. var thisByte = new byte[] { bytes [byteIndex-1] };
  199. buffer.Fallback (thisByte, 0);
  200. while (buffer.Remaining > 0) {
  201. if (charIndex < chars.Length) {
  202. chars [charIndex++] = buffer.GetNextChar ();
  203. continue;
  204. }
  205. throw new ArgumentException (
  206. "The output char buffer is too small to contain the " +
  207. "decoded characters.");
  208. }
  209. }
  210. }
  211. return byteCount;
  212. }
  213. // Get the maximum number of bytes needed to encode a
  214. // specified number of characters.
  215. public override int GetMaxByteCount (int charCount)
  216. {
  217. if (charCount < 0) {
  218. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  219. }
  220. return charCount;
  221. }
  222. // Get the maximum number of characters needed to decode a
  223. // specified number of bytes.
  224. public override int GetMaxCharCount (int byteCount)
  225. {
  226. if (byteCount < 0) {
  227. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  228. }
  229. return byteCount;
  230. }
  231. // Decode a buffer of bytes into a string.
  232. public override String GetString (byte[] bytes, int byteIndex, int byteCount)
  233. {
  234. if (bytes == null) {
  235. throw new ArgumentNullException ("bytes");
  236. }
  237. if (byteIndex < 0 || byteIndex > bytes.Length) {
  238. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  239. }
  240. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  241. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  242. }
  243. if (byteCount == 0)
  244. return String.Empty;
  245. unsafe {
  246. fixed (byte* bytePtr = bytes) {
  247. string s = string.InternalAllocateStr (byteCount);
  248. fixed (char* charPtr = s) {
  249. byte* currByte = bytePtr + byteIndex;
  250. byte* lastByte = currByte + byteCount;
  251. char* currChar = charPtr;
  252. while (currByte < lastByte) {
  253. byte b = currByte++ [0];
  254. currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
  255. }
  256. }
  257. return s;
  258. }
  259. }
  260. }
  261. [CLSCompliantAttribute (false)]
  262. [ComVisible (false)]
  263. public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  264. {
  265. if (chars == null)
  266. throw new ArgumentNullException ("chars");
  267. if (bytes == null)
  268. throw new ArgumentNullException ("bytes");
  269. if (charCount < 0)
  270. throw new ArgumentOutOfRangeException ("charCount");
  271. if (byteCount < 0)
  272. throw new ArgumentOutOfRangeException ("byteCount");
  273. if (byteCount < charCount)
  274. throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
  275. for (int i = 0; i < charCount; i++){
  276. char c = chars [i];
  277. bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
  278. }
  279. return charCount;
  280. }
  281. [CLSCompliantAttribute(false)]
  282. [ComVisible (false)]
  283. public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  284. {
  285. if (bytes == null)
  286. throw new ArgumentNullException ("bytes");
  287. if (chars == null)
  288. throw new ArgumentNullException ("chars");
  289. if (charCount < 0)
  290. throw new ArgumentOutOfRangeException ("charCount");
  291. if (byteCount < 0)
  292. throw new ArgumentOutOfRangeException ("byteCount");
  293. if (charCount < byteCount)
  294. throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
  295. for (int i = 0; i < byteCount; i++){
  296. byte b = bytes [i];
  297. chars [i] = b > 127 ? '?' : (char) b;
  298. }
  299. return byteCount;
  300. }
  301. [CLSCompliantAttribute(false)]
  302. [ComVisible (false)]
  303. public unsafe override int GetCharCount (byte *bytes, int count)
  304. {
  305. return count;
  306. }
  307. [CLSCompliantAttribute(false)]
  308. [ComVisible (false)]
  309. public unsafe override int GetByteCount (char *chars, int count)
  310. {
  311. return count;
  312. }
  313. [ComVisible (false)]
  314. public override Decoder GetDecoder ()
  315. {
  316. return base.GetDecoder ();
  317. }
  318. [ComVisible (false)]
  319. public override Encoder GetEncoder ()
  320. {
  321. return base.GetEncoder ();
  322. }
  323. }; // class ASCIIEncoding
  324. }; // namespace System.Text