Latin1Encoding.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * Latin1Encoding.cs - Implementation of the
  3. * "System.Text.Latin1Encoding" class.
  4. *
  5. * Copyright (c) 2002 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. [Serializable]
  29. internal class Latin1Encoding : Encoding
  30. {
  31. // Magic number used by Windows for the ISO Latin1 code page.
  32. internal const int ISOLATIN_CODE_PAGE = 28591;
  33. // Constructor.
  34. public Latin1Encoding () : base (ISOLATIN_CODE_PAGE)
  35. {
  36. // Nothing to do here.
  37. }
  38. #if NET_2_0
  39. public override bool IsSingleByte {
  40. get { return true; }
  41. }
  42. #endif
  43. // Get the number of bytes needed to encode a character buffer.
  44. public override int GetByteCount (char[] chars, int index, int count)
  45. {
  46. if (chars == null) {
  47. throw new ArgumentNullException ("chars");
  48. }
  49. if (index < 0 || index > chars.Length) {
  50. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  51. }
  52. if (count < 0 || count > (chars.Length - index)) {
  53. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  54. }
  55. return count;
  56. }
  57. // Convenience wrappers for "GetByteCount".
  58. public override int GetByteCount (String s)
  59. {
  60. if (s == null) {
  61. throw new ArgumentNullException ("s");
  62. }
  63. return s.Length;
  64. }
  65. // Get the bytes that result from encoding a character buffer.
  66. public override int GetBytes (char[] chars, int charIndex, int charCount,
  67. byte[] bytes, int byteIndex)
  68. {
  69. #if NET_2_0
  70. // well, yes, I know this #if is ugly, but I think it is the simplest switch.
  71. EncoderFallbackBuffer buffer = null;
  72. char [] fallback_chars = null;
  73. return GetBytes (chars, charIndex, charCount, bytes,
  74. byteIndex, ref buffer, ref fallback_chars);
  75. }
  76. int GetBytes (char[] chars, int charIndex, int charCount,
  77. byte[] bytes, int byteIndex,
  78. ref EncoderFallbackBuffer buffer,
  79. ref char [] fallback_chars)
  80. {
  81. #endif
  82. if (chars == null) {
  83. throw new ArgumentNullException ("chars");
  84. }
  85. if (bytes == null) {
  86. throw new ArgumentNullException ("bytes");
  87. }
  88. if (charIndex < 0 || charIndex > chars.Length) {
  89. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  90. }
  91. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  92. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  93. }
  94. if (byteIndex < 0 || byteIndex > bytes.Length) {
  95. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  96. }
  97. if ((bytes.Length - byteIndex) < charCount) {
  98. throw new ArgumentException (_("Arg_InsufficientSpace"));
  99. }
  100. int count = charCount;
  101. char ch;
  102. while (count-- > 0) {
  103. ch = chars [charIndex++];
  104. if (ch < (char)0x0100) {
  105. bytes [byteIndex++] = (byte)ch;
  106. } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
  107. bytes [byteIndex++] = (byte)(ch - 0xFEE0);
  108. } else {
  109. #if NET_2_0
  110. if (buffer == null)
  111. buffer = EncoderFallback.CreateFallbackBuffer ();
  112. if (Char.IsSurrogate (ch) && count > 1 &&
  113. Char.IsSurrogate (chars [charIndex]))
  114. buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
  115. else
  116. buffer.Fallback (ch, charIndex - 1);
  117. if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
  118. fallback_chars = new char [buffer.Remaining];
  119. for (int i = 0; i < fallback_chars.Length; i++)
  120. fallback_chars [i] = buffer.GetNextChar ();
  121. byteIndex += GetBytes (fallback_chars, 0,
  122. fallback_chars.Length, bytes, byteIndex,
  123. ref buffer, ref fallback_chars);
  124. #else
  125. bytes [byteIndex++] = (byte)'?';
  126. #endif
  127. }
  128. }
  129. return charCount;
  130. }
  131. // Convenience wrappers for "GetBytes".
  132. public override int GetBytes (String s, int charIndex, int charCount,
  133. byte[] bytes, int byteIndex)
  134. {
  135. #if NET_2_0
  136. // I know this #if is ugly, but I think it is the simplest switch.
  137. EncoderFallbackBuffer buffer = null;
  138. char [] fallback_chars = null;
  139. return GetBytes (s, charIndex, charCount, bytes, byteIndex,
  140. ref buffer, ref fallback_chars);
  141. }
  142. int GetBytes (String s, int charIndex, int charCount,
  143. byte[] bytes, int byteIndex,
  144. ref EncoderFallbackBuffer buffer,
  145. ref char [] fallback_chars)
  146. {
  147. #endif
  148. if (s == null) {
  149. throw new ArgumentNullException ("s");
  150. }
  151. if (bytes == null) {
  152. throw new ArgumentNullException ("bytes");
  153. }
  154. if (charIndex < 0 || charIndex > s.Length) {
  155. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  156. }
  157. if (charCount < 0 || charCount > (s.Length - charIndex)) {
  158. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  159. }
  160. if (byteIndex < 0 || byteIndex > bytes.Length) {
  161. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  162. }
  163. if ((bytes.Length - byteIndex) < charCount) {
  164. throw new ArgumentException (_("Arg_InsufficientSpace"));
  165. }
  166. int count = charCount;
  167. char ch;
  168. while (count-- > 0) {
  169. ch = s [charIndex++];
  170. if (ch < (char)0x0100) {
  171. bytes [byteIndex++] = (byte)ch;
  172. } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
  173. bytes [byteIndex++] = (byte)(ch - 0xFEE0);
  174. } else {
  175. #if NET_2_0
  176. if (buffer == null)
  177. buffer = EncoderFallback.CreateFallbackBuffer ();
  178. if (Char.IsSurrogate (ch) && count > 1 &&
  179. Char.IsSurrogate (s [charIndex]))
  180. buffer.Fallback (ch, s [charIndex], charIndex++ - 1);
  181. else
  182. buffer.Fallback (ch, charIndex - 1);
  183. if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
  184. fallback_chars = new char [buffer.Remaining];
  185. for (int i = 0; i < fallback_chars.Length; i++)
  186. fallback_chars [i] = buffer.GetNextChar ();
  187. byteIndex += GetBytes (fallback_chars, 0,
  188. fallback_chars.Length, bytes, byteIndex,
  189. ref buffer, ref fallback_chars);
  190. #else
  191. bytes [byteIndex++] = (byte)'?';
  192. #endif
  193. }
  194. }
  195. return charCount;
  196. }
  197. // Get the number of characters needed to decode a byte buffer.
  198. public override int GetCharCount (byte[] bytes, int index, int count)
  199. {
  200. if (bytes == null) {
  201. throw new ArgumentNullException ("bytes");
  202. }
  203. if (index < 0 || index > bytes.Length) {
  204. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  205. }
  206. if (count < 0 || count > (bytes.Length - index)) {
  207. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  208. }
  209. return count;
  210. }
  211. // Get the characters that result from decoding a byte buffer.
  212. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  213. char[] chars, int charIndex)
  214. {
  215. if (bytes == null) {
  216. throw new ArgumentNullException ("bytes");
  217. }
  218. if (chars == null) {
  219. throw new ArgumentNullException ("chars");
  220. }
  221. if (byteIndex < 0 || byteIndex > bytes.Length) {
  222. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  223. }
  224. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  225. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  226. }
  227. if (charIndex < 0 || charIndex > chars.Length) {
  228. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  229. }
  230. if ((chars.Length - charIndex) < byteCount) {
  231. throw new ArgumentException (_("Arg_InsufficientSpace"));
  232. }
  233. int count = byteCount;
  234. while (count-- > 0) {
  235. chars [charIndex++] = (char)(bytes [byteIndex++]);
  236. }
  237. return byteCount;
  238. }
  239. // Get the maximum number of bytes needed to encode a
  240. // specified number of characters.
  241. public override int GetMaxByteCount (int charCount)
  242. {
  243. if (charCount < 0) {
  244. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  245. }
  246. return charCount;
  247. }
  248. // Get the maximum number of characters needed to decode a
  249. // specified number of bytes.
  250. public override int GetMaxCharCount (int byteCount)
  251. {
  252. if (byteCount < 0) {
  253. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  254. }
  255. return byteCount;
  256. }
  257. // Decode a buffer of bytes into a string.
  258. public override String GetString (byte[] bytes, int index, int count)
  259. {
  260. if (bytes == null) {
  261. throw new ArgumentNullException ("bytes");
  262. }
  263. if (index < 0 || index > bytes.Length) {
  264. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  265. }
  266. if (count < 0 || count > (bytes.Length - index)) {
  267. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  268. }
  269. if (count == 0)
  270. return String.Empty;
  271. unsafe {
  272. fixed (byte *ss = &bytes [0]) {
  273. return new String ((sbyte*)ss, index, count);
  274. }
  275. }
  276. }
  277. public override String GetString (byte[] bytes)
  278. {
  279. if (bytes == null) {
  280. throw new ArgumentNullException ("bytes");
  281. }
  282. int count = bytes.Length;
  283. if (count == 0)
  284. return String.Empty;
  285. unsafe {
  286. fixed (byte *ss = &bytes [0]) {
  287. return new String ((sbyte*)ss, 0, count);
  288. }
  289. }
  290. }
  291. #if !ECMA_COMPAT
  292. // Get the mail body name for this encoding.
  293. public override String BodyName
  294. {
  295. get {
  296. return "iso-8859-1";
  297. }
  298. }
  299. // Get the human-readable name for this encoding.
  300. public override String EncodingName
  301. {
  302. get {
  303. return "Western European (ISO)";
  304. }
  305. }
  306. // Get the mail agent header name for this encoding.
  307. public override String HeaderName
  308. {
  309. get {
  310. return "iso-8859-1";
  311. }
  312. }
  313. // Determine if this encoding can be displayed in a Web browser.
  314. public override bool IsBrowserDisplay
  315. {
  316. get {
  317. return true;
  318. }
  319. }
  320. // Determine if this encoding can be saved from a Web browser.
  321. public override bool IsBrowserSave
  322. {
  323. get {
  324. return true;
  325. }
  326. }
  327. // Determine if this encoding can be displayed in a mail/news agent.
  328. public override bool IsMailNewsDisplay
  329. {
  330. get {
  331. return true;
  332. }
  333. }
  334. // Determine if this encoding can be saved from a mail/news agent.
  335. public override bool IsMailNewsSave
  336. {
  337. get {
  338. return true;
  339. }
  340. }
  341. // Get the IANA-preferred Web name for this encoding.
  342. public override String WebName
  343. {
  344. get {
  345. return "iso-8859-1";
  346. }
  347. }
  348. #endif // !ECMA_COMPAT
  349. }; // class Latin1Encoding
  350. }; // namespace System.Text