Latin1Encoding.cs 9.3 KB

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