Latin1Encoding.cs 11 KB

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