Latin1Encoding.cs 11 KB

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