ASCIIEncoding.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. // Get the number of bytes needed to encode a character buffer.
  43. public override int GetByteCount (char[] chars, int index, int count)
  44. {
  45. if (chars == null) {
  46. throw new ArgumentNullException ("chars");
  47. }
  48. if (index < 0 || index > chars.Length) {
  49. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  50. }
  51. if (count < 0 || count > (chars.Length - index)) {
  52. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  53. }
  54. return count;
  55. }
  56. // Convenience wrappers for "GetByteCount".
  57. public override int GetByteCount (String s)
  58. {
  59. if (s == null) {
  60. throw new ArgumentNullException ("s");
  61. }
  62. return s.Length;
  63. }
  64. // Get the bytes that result from encoding a character buffer.
  65. public override int GetBytes (char[] chars, int charIndex, int charCount,
  66. byte[] bytes, int byteIndex)
  67. {
  68. if (chars == null) {
  69. throw new ArgumentNullException ("chars");
  70. }
  71. if (bytes == null) {
  72. throw new ArgumentNullException ("bytes");
  73. }
  74. if (charIndex < 0 || charIndex > chars.Length) {
  75. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  76. }
  77. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  78. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  79. }
  80. if (byteIndex < 0 || byteIndex > bytes.Length) {
  81. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  82. }
  83. if ((bytes.Length - byteIndex) < charCount) {
  84. throw new ArgumentException (_("Arg_InsufficientSpace"));
  85. }
  86. int count = charCount;
  87. char ch;
  88. while (count-- > 0) {
  89. ch = chars [charIndex++];
  90. if (ch < (char)0x80) {
  91. bytes [byteIndex++] = (byte)ch;
  92. } else {
  93. bytes [byteIndex++] = (byte)'?';
  94. }
  95. }
  96. return charCount;
  97. }
  98. // Convenience wrappers for "GetBytes".
  99. public override int GetBytes (String s, int charIndex, int charCount, byte[] bytes, int byteIndex)
  100. {
  101. if (s == null) {
  102. throw new ArgumentNullException ("s");
  103. }
  104. if (bytes == null) {
  105. throw new ArgumentNullException ("bytes");
  106. }
  107. if (charIndex < 0 || charIndex > s.Length) {
  108. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  109. }
  110. if (charCount < 0 || charCount > (s.Length - charIndex)) {
  111. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  112. }
  113. if (byteIndex < 0 || byteIndex > bytes.Length) {
  114. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  115. }
  116. if ((bytes.Length - byteIndex) < charCount) {
  117. throw new ArgumentException (_("Arg_InsufficientSpace"));
  118. }
  119. int count = charCount;
  120. char ch;
  121. while (count-- > 0) {
  122. ch = s [charIndex++];
  123. if (ch < (char)0x80) {
  124. bytes [byteIndex++] = (byte)ch;
  125. } else {
  126. bytes [byteIndex++] = (byte)'?';
  127. }
  128. }
  129. return charCount;
  130. }
  131. // Get the number of characters needed to decode a byte buffer.
  132. public override int GetCharCount (byte[] bytes, int index, int count)
  133. {
  134. if (bytes == null) {
  135. throw new ArgumentNullException ("bytes");
  136. }
  137. if (index < 0 || index > bytes.Length) {
  138. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  139. }
  140. if (count < 0 || count > (bytes.Length - index)) {
  141. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  142. }
  143. return count;
  144. }
  145. // Get the characters that result from decoding a byte buffer.
  146. public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
  147. {
  148. if (bytes == null)
  149. throw new ArgumentNullException ("bytes");
  150. if (chars == null)
  151. throw new ArgumentNullException ("chars");
  152. if (byteIndex < 0 || byteIndex > bytes.Length)
  153. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  154. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
  155. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  156. if (charIndex < 0 || charIndex > chars.Length)
  157. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  158. if ((chars.Length - charIndex) < byteCount)
  159. throw new ArgumentException (_("Arg_InsufficientSpace"));
  160. int count = byteCount;
  161. while (count-- > 0) {
  162. char c = (char)(bytes [byteIndex++]);
  163. if (c > 127)
  164. c = '?';
  165. chars [charIndex++] = c;
  166. }
  167. return byteCount;
  168. }
  169. // Get the maximum number of bytes needed to encode a
  170. // specified number of characters.
  171. public override int GetMaxByteCount (int charCount)
  172. {
  173. if (charCount < 0) {
  174. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  175. }
  176. return charCount;
  177. }
  178. // Get the maximum number of characters needed to decode a
  179. // specified number of bytes.
  180. public override int GetMaxCharCount (int byteCount)
  181. {
  182. if (byteCount < 0) {
  183. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  184. }
  185. return byteCount;
  186. }
  187. // Decode a buffer of bytes into a string.
  188. public override String GetString (byte[] bytes, int index, int count)
  189. {
  190. if (bytes == null) {
  191. throw new ArgumentNullException ("bytes");
  192. }
  193. if (index < 0 || index > bytes.Length) {
  194. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  195. }
  196. if (count < 0 || count > (bytes.Length - index)) {
  197. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  198. }
  199. if (count == 0)
  200. return String.Empty;
  201. unsafe {
  202. fixed (byte *ss = &bytes [0]) {
  203. return new String ((sbyte*)ss, index, count);
  204. }
  205. }
  206. }
  207. #if NET_2_0
  208. [CLSCompliantAttribute (false)]
  209. public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  210. {
  211. if (chars == null)
  212. throw new ArgumentNullException ("chars");
  213. if (bytes == null)
  214. throw new ArgumentNullException ("bytes");
  215. if (charCount < 0)
  216. throw new ArgumentOutOfRangeException ("charCount");
  217. if (byteCount < 0)
  218. throw new ArgumentOutOfRangeException ("byteCount");
  219. if (byteCount < charCount)
  220. throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
  221. for (int i = 0; i < charCount; i++){
  222. char c = chars [i];
  223. bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
  224. }
  225. return charCount;
  226. }
  227. [CLSCompliantAttribute(false)]
  228. public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
  229. {
  230. if (bytes == null)
  231. throw new ArgumentNullException ("bytes");
  232. if (chars == null)
  233. throw new ArgumentNullException ("chars");
  234. if (charCount < 0)
  235. throw new ArgumentOutOfRangeException ("charCount");
  236. if (byteCount < 0)
  237. throw new ArgumentOutOfRangeException ("byteCount");
  238. if (charCount < byteCount)
  239. throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
  240. for (int i = 0; i < byteCount; i++){
  241. byte b = bytes [i];
  242. chars [i] = b > 127 ? '?' : (char) b;
  243. }
  244. return byteCount;
  245. }
  246. [CLSCompliantAttribute(false)]
  247. public unsafe override int GetCharCount (byte *bytes, int count)
  248. {
  249. return count;
  250. }
  251. [CLSCompliantAttribute(false)]
  252. public unsafe override int GetByteCount (char *chars, int count)
  253. {
  254. return count;
  255. }
  256. #else
  257. // This routine is gone in 2.x
  258. public override String GetString (byte[] bytes)
  259. {
  260. if (bytes == null) {
  261. throw new ArgumentNullException ("bytes");
  262. }
  263. int count = bytes.Length;
  264. if (count == 0)
  265. return String.Empty;
  266. unsafe {
  267. fixed (byte *ss = &bytes [0]) {
  268. return new String ((sbyte*)ss, 0, count);
  269. }
  270. }
  271. }
  272. #endif
  273. }; // class ASCIIEncoding
  274. }; // namespace System.Text