Latin1Encoding.cs 9.6 KB

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