DefaultEncoding.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * DefaultEncoding.cs - Implementation of the
  3. * "System.Text.DefaultEncoding" class.
  4. *
  5. * Copyright (c) 2001 Southern Storm Software, Pty Ltd
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining
  8. * a copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  21. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  22. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  23. * OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. namespace System.Text
  26. {
  27. using System;
  28. using System.Runtime.CompilerServices;
  29. internal sealed class DefaultEncoding : Encoding
  30. {
  31. // Constructor.
  32. public DefaultEncoding() : base(0) {}
  33. // Get the number of bytes needed to encode a character buffer.
  34. unsafe public override int GetByteCount(char[] chars, int index, int count)
  35. {
  36. if(chars == null)
  37. {
  38. throw new ArgumentNullException("chars");
  39. }
  40. if(index < 0 || index > chars.Length)
  41. {
  42. throw new ArgumentOutOfRangeException
  43. ("index", _("ArgRange_Array"));
  44. }
  45. if(count < 0 || count > (chars.Length - index))
  46. {
  47. throw new ArgumentOutOfRangeException
  48. ("count", _("ArgRange_Array"));
  49. }
  50. return InternalGetByteCount(chars, index, count);
  51. }
  52. // Convenience wrappers for "GetByteCount".
  53. public override int GetByteCount(String s)
  54. {
  55. if(s == null)
  56. {
  57. throw new ArgumentNullException("s");
  58. }
  59. return InternalGetByteCount(s, 0, s.Length);
  60. }
  61. // Get the bytes that result from encoding a character buffer.
  62. public override int GetBytes(char[] chars, int charIndex, int charCount,
  63. byte[] bytes, int byteIndex)
  64. {
  65. if(chars == null)
  66. {
  67. throw new ArgumentNullException("chars");
  68. }
  69. if(bytes == null)
  70. {
  71. throw new ArgumentNullException("bytes");
  72. }
  73. if(charIndex < 0 || charIndex > chars.Length)
  74. {
  75. throw new ArgumentOutOfRangeException
  76. ("charIndex", _("ArgRange_Array"));
  77. }
  78. if(charCount < 0 || charCount > (chars.Length - charIndex))
  79. {
  80. throw new ArgumentOutOfRangeException
  81. ("charCount", _("ArgRange_Array"));
  82. }
  83. if(byteIndex < 0 || byteIndex > bytes.Length)
  84. {
  85. throw new ArgumentOutOfRangeException
  86. ("byteIndex", _("ArgRange_Array"));
  87. }
  88. int result = InternalGetBytes(chars, charIndex, charCount,
  89. bytes, byteIndex);
  90. if(result != -1)
  91. {
  92. return result;
  93. }
  94. throw new ArgumentException(_("Arg_InsufficientSpace"));
  95. }
  96. // Convenience wrappers for "GetBytes".
  97. public override int GetBytes(String s, int charIndex, int charCount,
  98. byte[] bytes, int byteIndex)
  99. {
  100. if(s == null)
  101. {
  102. throw new ArgumentNullException("s");
  103. }
  104. if(bytes == null)
  105. {
  106. throw new ArgumentNullException("bytes");
  107. }
  108. if(charIndex < 0 || charIndex > s.Length)
  109. {
  110. throw new ArgumentOutOfRangeException
  111. ("charIndex", _("ArgRange_StringIndex"));
  112. }
  113. if(charCount < 0 || charCount > (s.Length - charIndex))
  114. {
  115. throw new ArgumentOutOfRangeException
  116. ("charCount", _("ArgRange_StringRange"));
  117. }
  118. if(byteIndex < 0 || byteIndex > bytes.Length)
  119. {
  120. throw new ArgumentOutOfRangeException
  121. ("byteIndex", _("ArgRange_Array"));
  122. }
  123. int result = InternalGetBytes(s, charIndex, charCount,
  124. bytes, byteIndex);
  125. if(result != -1)
  126. {
  127. return result;
  128. }
  129. throw new ArgumentException(_("Arg_InsufficientSpace"));
  130. }
  131. public override byte[] GetBytes(String s)
  132. {
  133. if(s == null)
  134. {
  135. throw new ArgumentNullException("s");
  136. }
  137. byte[] bytes = new byte [InternalGetByteCount(s, 0, s.Length)];
  138. InternalGetBytes(s, 0, s.Length, bytes, 0);
  139. return bytes;
  140. }
  141. // Get the number of characters needed to decode a byte buffer.
  142. public override int GetCharCount(byte[] bytes, int index, int count)
  143. {
  144. if(bytes == null)
  145. {
  146. throw new ArgumentNullException("bytes");
  147. }
  148. if(index < 0 || index > bytes.Length)
  149. {
  150. throw new ArgumentOutOfRangeException
  151. ("index", _("ArgRange_Array"));
  152. }
  153. if(count < 0 || count > (bytes.Length - index))
  154. {
  155. throw new ArgumentOutOfRangeException
  156. ("count", _("ArgRange_Array"));
  157. }
  158. return InternalGetCharCount(bytes, index, count);
  159. }
  160. // Get the characters that result from decoding a byte buffer.
  161. public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
  162. char[] chars, int charIndex)
  163. {
  164. if(bytes == null)
  165. {
  166. throw new ArgumentNullException("bytes");
  167. }
  168. if(chars == null)
  169. {
  170. throw new ArgumentNullException("chars");
  171. }
  172. if(byteIndex < 0 || byteIndex > bytes.Length)
  173. {
  174. throw new ArgumentOutOfRangeException
  175. ("byteIndex", _("ArgRange_Array"));
  176. }
  177. if(byteCount < 0 || byteCount > (bytes.Length - byteIndex))
  178. {
  179. throw new ArgumentOutOfRangeException
  180. ("byteCount", _("ArgRange_Array"));
  181. }
  182. if(charIndex < 0 || charIndex > chars.Length)
  183. {
  184. throw new ArgumentOutOfRangeException
  185. ("charIndex", _("ArgRange_Array"));
  186. }
  187. int result = InternalGetChars(bytes, byteIndex, byteCount,
  188. chars, charIndex);
  189. if(result != -1)
  190. {
  191. return result;
  192. }
  193. throw new ArgumentException(_("Arg_InsufficientSpace"));
  194. }
  195. // Get the maximum number of bytes needed to encode a
  196. // specified number of characters.
  197. public override int GetMaxByteCount(int charCount)
  198. {
  199. if(charCount < 0)
  200. {
  201. throw new ArgumentOutOfRangeException
  202. ("charCount", _("ArgRange_NonNegative"));
  203. }
  204. return InternalGetMaxByteCount(charCount);
  205. }
  206. // Get the maximum number of characters needed to decode a
  207. // specified number of bytes.
  208. public override int GetMaxCharCount(int byteCount)
  209. {
  210. if(byteCount < 0)
  211. {
  212. throw new ArgumentOutOfRangeException
  213. ("byteCount", _("ArgRange_NonNegative"));
  214. }
  215. return InternalGetMaxCharCount(byteCount);
  216. }
  217. // Decode a buffer of bytes into a string.
  218. public override String GetString(byte[] bytes, int index, int count)
  219. {
  220. if(bytes == null)
  221. {
  222. throw new ArgumentNullException("bytes");
  223. }
  224. if(index < 0 || index > bytes.Length)
  225. {
  226. throw new ArgumentOutOfRangeException
  227. ("index", _("ArgRange_Array"));
  228. }
  229. if(count < 0 || count > (bytes.Length - index))
  230. {
  231. throw new ArgumentOutOfRangeException
  232. ("count", _("ArgRange_Array"));
  233. }
  234. return InternalGetString(bytes, index, count);
  235. }
  236. public override String GetString(byte[] bytes)
  237. {
  238. if(bytes == null)
  239. {
  240. throw new ArgumentNullException("bytes");
  241. }
  242. return InternalGetString(bytes, 0, bytes.Length);
  243. }
  244. // Internal methods that are used by the runtime engine to
  245. // provide the default encoding. These may assume that their
  246. // arguments have been fully validated.
  247. [MethodImpl(MethodImplOptions.InternalCall)]
  248. extern private static int InternalGetByteCount
  249. (char[] chars, int index, int count);
  250. [MethodImpl(MethodImplOptions.InternalCall)]
  251. extern private static int InternalGetByteCount
  252. (String s, int index, int count);
  253. // Returns -1 if insufficient space in "bytes".
  254. [MethodImpl(MethodImplOptions.InternalCall)]
  255. extern private static int InternalGetBytes
  256. (char[] chars, int charIndex, int charCount,
  257. byte[] bytes, int byteIndex);
  258. // Returns -1 if insufficient space in "bytes".
  259. [MethodImpl(MethodImplOptions.InternalCall)]
  260. extern private static int InternalGetBytes
  261. (String s, int charIndex, int charCount,
  262. byte[] bytes, int byteIndex);
  263. [MethodImpl(MethodImplOptions.InternalCall)]
  264. extern private static int InternalGetCharCount
  265. (byte[] bytes, int index, int count);
  266. // Returns -1 if insufficient space in "chars".
  267. [MethodImpl(MethodImplOptions.InternalCall)]
  268. extern private static int InternalGetChars
  269. (byte[] bytes, int byteIndex, int byteCount,
  270. char[] chars, int charIndex);
  271. [MethodImpl(MethodImplOptions.InternalCall)]
  272. extern private static int InternalGetMaxByteCount(int charCount);
  273. [MethodImpl(MethodImplOptions.InternalCall)]
  274. extern private static int InternalGetMaxCharCount(int byteCount);
  275. [MethodImpl(MethodImplOptions.InternalCall)]
  276. extern private static String InternalGetString
  277. (byte[] bytes, int index, int count);
  278. // Get the default code page number. Zero if unknown.
  279. [MethodImpl(MethodImplOptions.InternalCall)]
  280. extern internal static int InternalCodePage();
  281. }; // class DefaultEncoding
  282. }; // namespace System.Text