Latin1Encoding.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. // Get the number of bytes needed to encode a character buffer.
  39. public override int GetByteCount (char[] chars, int index, int count)
  40. {
  41. if (chars == null) {
  42. throw new ArgumentNullException ("chars");
  43. }
  44. if (index < 0 || index > chars.Length) {
  45. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  46. }
  47. if (count < 0 || count > (chars.Length - index)) {
  48. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  49. }
  50. return count;
  51. }
  52. // Convenience wrappers for "GetByteCount".
  53. public override int GetByteCount (String s)
  54. {
  55. if (s == null) {
  56. throw new ArgumentNullException ("s");
  57. }
  58. return s.Length;
  59. }
  60. // Get the bytes that result from encoding a character buffer.
  61. public override int GetBytes (char[] chars, int charIndex, int charCount,
  62. byte[] bytes, int byteIndex)
  63. {
  64. if (chars == null) {
  65. throw new ArgumentNullException ("chars");
  66. }
  67. if (bytes == null) {
  68. throw new ArgumentNullException ("bytes");
  69. }
  70. if (charIndex < 0 || charIndex > chars.Length) {
  71. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  72. }
  73. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  74. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  75. }
  76. if (byteIndex < 0 || byteIndex > bytes.Length) {
  77. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  78. }
  79. if ((bytes.Length - byteIndex) < charCount) {
  80. throw new ArgumentException (_("Arg_InsufficientSpace"));
  81. }
  82. int count = charCount;
  83. char ch;
  84. while (count-- > 0) {
  85. ch = chars [charIndex++];
  86. if (ch < (char)0x0100) {
  87. bytes [byteIndex++] = (byte)ch;
  88. } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
  89. bytes [byteIndex++] = (byte)(ch - 0xFEE0);
  90. } else {
  91. bytes [byteIndex++] = (byte)'?';
  92. }
  93. }
  94. return charCount;
  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. throw new ArgumentNullException ("s");
  102. }
  103. if (bytes == null) {
  104. throw new ArgumentNullException ("bytes");
  105. }
  106. if (charIndex < 0 || charIndex > s.Length) {
  107. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  108. }
  109. if (charCount < 0 || charCount > (s.Length - charIndex)) {
  110. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  111. }
  112. if (byteIndex < 0 || byteIndex > bytes.Length) {
  113. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  114. }
  115. if ((bytes.Length - byteIndex) < charCount) {
  116. throw new ArgumentException (_("Arg_InsufficientSpace"));
  117. }
  118. int count = charCount;
  119. char ch;
  120. while (count-- > 0) {
  121. ch = s [charIndex++];
  122. if (ch < (char)0x0100) {
  123. bytes [byteIndex++] = (byte)ch;
  124. } else if (ch >= '\uFF01' && ch <= '\uFF5E') {
  125. bytes [byteIndex++] = (byte)(ch - 0xFEE0);
  126. } else {
  127. bytes [byteIndex++] = (byte)'?';
  128. }
  129. }
  130. return charCount;
  131. }
  132. // Get the number of characters needed to decode a byte buffer.
  133. public override int GetCharCount (byte[] bytes, int index, int count)
  134. {
  135. if (bytes == null) {
  136. throw new ArgumentNullException ("bytes");
  137. }
  138. if (index < 0 || index > bytes.Length) {
  139. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  140. }
  141. if (count < 0 || count > (bytes.Length - index)) {
  142. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  143. }
  144. return count;
  145. }
  146. // Get the characters that result from decoding a byte buffer.
  147. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  148. char[] chars, int charIndex)
  149. {
  150. if (bytes == null) {
  151. throw new ArgumentNullException ("bytes");
  152. }
  153. if (chars == null) {
  154. throw new ArgumentNullException ("chars");
  155. }
  156. if (byteIndex < 0 || byteIndex > bytes.Length) {
  157. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  158. }
  159. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  160. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  161. }
  162. if (charIndex < 0 || charIndex > chars.Length) {
  163. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  164. }
  165. if ((chars.Length - charIndex) < byteCount) {
  166. throw new ArgumentException (_("Arg_InsufficientSpace"));
  167. }
  168. int count = byteCount;
  169. while (count-- > 0) {
  170. chars [charIndex++] = (char)(bytes [byteIndex++]);
  171. }
  172. return byteCount;
  173. }
  174. // Get the maximum number of bytes needed to encode a
  175. // specified number of characters.
  176. public override int GetMaxByteCount (int charCount)
  177. {
  178. if (charCount < 0) {
  179. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  180. }
  181. return charCount;
  182. }
  183. // Get the maximum number of characters needed to decode a
  184. // specified number of bytes.
  185. public override int GetMaxCharCount (int byteCount)
  186. {
  187. if (byteCount < 0) {
  188. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  189. }
  190. return byteCount;
  191. }
  192. // Decode a buffer of bytes into a string.
  193. public override String GetString (byte[] bytes, int index, int count)
  194. {
  195. if (bytes == null) {
  196. throw new ArgumentNullException ("bytes");
  197. }
  198. if (index < 0 || index > bytes.Length) {
  199. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  200. }
  201. if (count < 0 || count > (bytes.Length - index)) {
  202. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  203. }
  204. if (count == 0)
  205. return String.Empty;
  206. unsafe {
  207. fixed (byte *ss = &bytes [0]) {
  208. return new String ((sbyte*)ss, index, count);
  209. }
  210. }
  211. }
  212. public override String GetString (byte[] bytes)
  213. {
  214. if (bytes == null) {
  215. throw new ArgumentNullException ("bytes");
  216. }
  217. int count = bytes.Length;
  218. if (count == 0)
  219. return String.Empty;
  220. unsafe {
  221. fixed (byte *ss = &bytes [0]) {
  222. return new String ((sbyte*)ss, 0, count);
  223. }
  224. }
  225. }
  226. #if !ECMA_COMPAT
  227. // Get the mail body name for this encoding.
  228. public override String BodyName
  229. {
  230. get {
  231. return "iso-8859-1";
  232. }
  233. }
  234. // Get the human-readable name for this encoding.
  235. public override String EncodingName
  236. {
  237. get {
  238. return "Western European (ISO)";
  239. }
  240. }
  241. // Get the mail agent header name for this encoding.
  242. public override String HeaderName
  243. {
  244. get {
  245. return "iso-8859-1";
  246. }
  247. }
  248. // Determine if this encoding can be displayed in a Web browser.
  249. public override bool IsBrowserDisplay
  250. {
  251. get {
  252. return true;
  253. }
  254. }
  255. // Determine if this encoding can be saved from a Web browser.
  256. public override bool IsBrowserSave
  257. {
  258. get {
  259. return true;
  260. }
  261. }
  262. // Determine if this encoding can be displayed in a mail/news agent.
  263. public override bool IsMailNewsDisplay
  264. {
  265. get {
  266. return true;
  267. }
  268. }
  269. // Determine if this encoding can be saved from a mail/news agent.
  270. public override bool IsMailNewsSave
  271. {
  272. get {
  273. return true;
  274. }
  275. }
  276. // Get the IANA-preferred Web name for this encoding.
  277. public override String WebName
  278. {
  279. get {
  280. return "iso-8859-1";
  281. }
  282. }
  283. #endif // !ECMA_COMPAT
  284. }; // class Latin1Encoding
  285. }; // namespace System.Text