Latin1Encoding.cs 8.3 KB

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