UTF32Encoding.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * UTF32Encoding.cs - Implementation of the
  3. * "System.Text.UTF32Encoding" class.
  4. *
  5. * Author: Atsushi Enomoto <[email protected]>
  6. *
  7. * Copyright (C) 2005 Novell, Inc. http://www.novell.com
  8. *
  9. * The basic part (now almost nothing) is copied from UnicodeEncoding.cs.
  10. * Original copyrights goes here:
  11. *
  12. * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
  13. * Copyright (C) 2003, 2004 Novell, Inc.
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining
  16. * a copy of this software and associated documentation files (the "Software"),
  17. * to deal in the Software without restriction, including without limitation
  18. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. * and/or sell copies of the Software, and to permit persons to whom the
  20. * Software is furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included
  23. * in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  26. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  28. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  29. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  30. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  31. * OTHER DEALINGS IN THE SOFTWARE.
  32. */
  33. #if NET_2_0
  34. namespace System.Text
  35. {
  36. using System;
  37. [Serializable]
  38. public sealed class UTF32Encoding : Encoding
  39. {
  40. // Magic numbers used by Windows for UTF32.
  41. internal const int UTF32_CODE_PAGE = 12000;
  42. internal const int BIG_UTF32_CODE_PAGE = 12001;
  43. // Internal state.
  44. private bool bigEndian;
  45. private bool byteOrderMark;
  46. // Constructors.
  47. public UTF32Encoding () : this (false, true, false)
  48. {
  49. }
  50. public UTF32Encoding (bool bigEndian, bool byteOrderMark)
  51. : this (bigEndian, byteOrderMark, false)
  52. {
  53. }
  54. public UTF32Encoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalid)
  55. : base ((bigEndian ? BIG_UTF32_CODE_PAGE : UTF32_CODE_PAGE))
  56. {
  57. this.bigEndian = bigEndian;
  58. this.byteOrderMark = byteOrderMark;
  59. if (throwOnInvalid)
  60. SetFallbackInternal (EncoderFallback.ExceptionFallback,
  61. DecoderFallback.ExceptionFallback);
  62. else
  63. SetFallbackInternal (EncoderFallback.ReplacementFallback,
  64. DecoderFallback.ReplacementFallback);
  65. if (bigEndian){
  66. body_name = "utf-32BE";
  67. encoding_name = "UTF-32 (Big-Endian)";
  68. header_name = "utf-32BE";
  69. is_browser_save = false;
  70. web_name = "utf-32BE";
  71. } else {
  72. body_name = "utf-32";
  73. encoding_name = "UTF-32";
  74. header_name = "utf-32";
  75. is_browser_save = true;
  76. web_name = "utf-32";
  77. }
  78. // Windows reports the same code page number for
  79. // both the little-endian and big-endian forms.
  80. windows_code_page = UTF32_CODE_PAGE;
  81. }
  82. // Get the number of bytes needed to encode a character buffer.
  83. [MonoTODO ("handle fallback")]
  84. public override int GetByteCount (char[] chars, int index, int count)
  85. {
  86. if (chars == null) {
  87. throw new ArgumentNullException ("chars");
  88. }
  89. if (index < 0 || index > chars.Length) {
  90. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  91. }
  92. if (count < 0 || count > (chars.Length - index)) {
  93. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  94. }
  95. int ret = 0;
  96. for (int i = index; i < count; i++) {
  97. if (Char.IsSurrogate (chars [i])) {
  98. if (i + 1 < chars.Length && Char.IsSurrogate (chars [i + 1]))
  99. ret += 4;
  100. else
  101. // FIXME: handle fallback
  102. // ret += DecoderFallback.MaxCharCount;
  103. ret += 4;
  104. }
  105. else
  106. ret += 4;
  107. }
  108. return ret;
  109. }
  110. // Get the bytes that result from encoding a character buffer.
  111. [MonoTODO ("handle fallback")]
  112. public override int GetBytes (char[] chars, int charIndex, int charCount,
  113. byte[] bytes, int byteIndex)
  114. {
  115. if (chars == null) {
  116. throw new ArgumentNullException ("chars");
  117. }
  118. if (bytes == null) {
  119. throw new ArgumentNullException ("bytes");
  120. }
  121. if (charIndex < 0 || charIndex > chars.Length) {
  122. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  123. }
  124. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  125. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  126. }
  127. if (byteIndex < 0 || byteIndex > bytes.Length) {
  128. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  129. }
  130. if ((bytes.Length - byteIndex) < (charCount * 4)) {
  131. throw new ArgumentException (_("Arg_InsufficientSpace"));
  132. }
  133. int posn = byteIndex;
  134. char ch;
  135. while (charCount-- > 0) {
  136. ch = chars[charIndex++];
  137. if (Char.IsSurrogate (ch)) {
  138. if (charCount-- > 0) {
  139. int value = 0x400 * (ch - 0xD800) + 0x10000 + chars [charIndex++] - 0xDC00;
  140. if (bigEndian) {
  141. for (int i = 0; i < 4; i++) {
  142. bytes [posn + 3 - i] = (byte) (value % 0x100);
  143. value >>= 8;
  144. }
  145. posn += 4;
  146. } else {
  147. for (int i = 0; i < 4; i++) {
  148. bytes [posn++] = (byte) (value % 0x100);
  149. value >>= 8;
  150. }
  151. }
  152. } else {
  153. // Illegal surrogate
  154. // FIXME: use fallback
  155. if (bigEndian) {
  156. bytes[posn++] = 0;
  157. bytes[posn++] = 0;
  158. bytes[posn++] = 0;
  159. bytes[posn++] = (byte) '?';
  160. } else {
  161. bytes[posn++] = (byte) '?';
  162. bytes[posn++] = 0;
  163. bytes[posn++] = 0;
  164. bytes[posn++] = 0;
  165. }
  166. }
  167. } else {
  168. if (bigEndian) {
  169. bytes[posn++] = 0;
  170. bytes[posn++] = 0;
  171. bytes[posn++] = (byte)(ch >> 8);
  172. bytes[posn++] = (byte)ch;
  173. } else {
  174. bytes[posn++] = (byte)ch;
  175. bytes[posn++] = (byte)(ch >> 8);
  176. bytes[posn++] = 0;
  177. bytes[posn++] = 0;
  178. }
  179. }
  180. }
  181. return posn - byteIndex;
  182. }
  183. // Get the number of characters needed to decode a byte buffer.
  184. public override int GetCharCount (byte[] bytes, int index, int count)
  185. {
  186. if (bytes == null) {
  187. throw new ArgumentNullException ("bytes");
  188. }
  189. if (index < 0 || index > bytes.Length) {
  190. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  191. }
  192. if (count < 0 || count > (bytes.Length - index)) {
  193. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  194. }
  195. return count / 4;
  196. }
  197. // Get the characters that result from decoding a byte buffer.
  198. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  199. char[] chars, int charIndex)
  200. {
  201. if (bytes == null) {
  202. throw new ArgumentNullException ("bytes");
  203. }
  204. if (chars == null) {
  205. throw new ArgumentNullException ("chars");
  206. }
  207. if (byteIndex < 0 || byteIndex > bytes.Length) {
  208. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  209. }
  210. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  211. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  212. }
  213. if (charIndex < 0 || charIndex > chars.Length) {
  214. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  215. }
  216. /*
  217. // Determine the byte order in the incoming buffer.
  218. bool isBigEndian;
  219. if (byteCount >= 2) {
  220. if (bytes[byteIndex] == (byte)0xFE && bytes[byteIndex + 1] == (byte)0xFF) {
  221. isBigEndian = true;
  222. } else if (bytes[byteIndex] == (byte)0xFF && bytes[byteIndex + 1] == (byte)0xFE) {
  223. isBigEndian = false;
  224. } else {
  225. isBigEndian = bigEndian;
  226. }
  227. } else {
  228. isBigEndian = bigEndian;
  229. }
  230. */
  231. // Validate that we have sufficient space in "chars".
  232. if ((chars.Length - charIndex) < (byteCount / 4)) {
  233. throw new ArgumentException (_("Arg_InsufficientSpace"));
  234. }
  235. // Convert the characters.
  236. int posn = charIndex;
  237. if (bigEndian) {
  238. while (byteCount >= 4) {
  239. chars[posn++] = (char) (
  240. bytes[byteIndex] << 24 |
  241. bytes[byteIndex + 1] << 16 |
  242. bytes[byteIndex + 2] << 8 |
  243. bytes[byteIndex + 3]);
  244. byteIndex += 4;
  245. byteCount -= 4;
  246. }
  247. } else {
  248. while (byteCount >= 4) {
  249. chars[posn++] = (char) (
  250. bytes[byteIndex] |
  251. bytes[byteIndex + 1] << 8 |
  252. bytes[byteIndex + 2] << 16 |
  253. bytes[byteIndex + 3] << 24);
  254. byteIndex += 4;
  255. byteCount -= 4;
  256. }
  257. }
  258. return posn - charIndex;
  259. }
  260. // Get the maximum number of bytes needed to encode a
  261. // specified number of characters.
  262. public override int GetMaxByteCount (int charCount)
  263. {
  264. if (charCount < 0) {
  265. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  266. }
  267. return charCount * 4;
  268. }
  269. // Get the maximum number of characters needed to decode a
  270. // specified number of bytes.
  271. public override int GetMaxCharCount (int byteCount)
  272. {
  273. if (byteCount < 0) {
  274. throw new ArgumentOutOfRangeException
  275. ("byteCount", _("ArgRange_NonNegative"));
  276. }
  277. return byteCount / 4;
  278. }
  279. // Get a UTF32-specific decoder that is attached to this instance.
  280. public override Decoder GetDecoder ()
  281. {
  282. return new UTF32Decoder (bigEndian);
  283. }
  284. // Get the UTF32 preamble.
  285. public override byte[] GetPreamble ()
  286. {
  287. if (byteOrderMark) {
  288. byte[] preamble = new byte[2];
  289. if (bigEndian) {
  290. preamble[0] = (byte)0xFE;
  291. preamble[1] = (byte)0xFF;
  292. } else {
  293. preamble[0] = (byte)0xFF;
  294. preamble[1] = (byte)0xFE;
  295. }
  296. return preamble;
  297. } else {
  298. return new byte [0];
  299. }
  300. }
  301. // Determine if this object is equal to another.
  302. public override bool Equals (Object value)
  303. {
  304. UTF32Encoding enc = (value as UTF32Encoding);
  305. if (enc != null) {
  306. return (codePage == enc.codePage &&
  307. bigEndian == enc.bigEndian &&
  308. byteOrderMark == enc.byteOrderMark &&
  309. base.Equals (value));
  310. } else {
  311. return false;
  312. }
  313. }
  314. // Get the hash code for this object.
  315. public override int GetHashCode ()
  316. {
  317. return base.GetHashCode ();
  318. }
  319. // UTF32 decoder implementation.
  320. private sealed class UTF32Decoder : Decoder
  321. {
  322. private bool bigEndian;
  323. private int leftOverByte;
  324. private int leftOverLength;
  325. // Constructor.
  326. public UTF32Decoder (bool bigEndian)
  327. {
  328. this.bigEndian = bigEndian;
  329. leftOverByte = -1;
  330. }
  331. // Override inherited methods.
  332. public override int GetCharCount (byte[] bytes, int index, int count)
  333. {
  334. if (bytes == null) {
  335. throw new ArgumentNullException ("bytes");
  336. }
  337. if (index < 0 || index > bytes.Length) {
  338. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  339. }
  340. if (count < 0 || count > (bytes.Length - index)) {
  341. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  342. }
  343. if (leftOverByte != -1) {
  344. return (count + 1) / 4;
  345. } else {
  346. return count / 4;
  347. }
  348. }
  349. public override int GetChars (byte[] bytes, int byteIndex,
  350. int byteCount, char[] chars,
  351. int charIndex)
  352. {
  353. if (bytes == null) {
  354. throw new ArgumentNullException ("bytes");
  355. }
  356. if (chars == null) {
  357. throw new ArgumentNullException ("chars");
  358. }
  359. if (byteIndex < 0 || byteIndex > bytes.Length) {
  360. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  361. }
  362. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  363. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  364. }
  365. if (charIndex < 0 || charIndex > chars.Length) {
  366. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  367. }
  368. // Convert the characters.
  369. int posn = charIndex;
  370. int leftOver = leftOverByte;
  371. int length = chars.Length;
  372. char ch;
  373. int remain = 4 - leftOverLength;
  374. if (leftOverLength > 0 && byteCount > remain) {
  375. if (bigEndian) {
  376. for (int i = 0; i < remain; i++)
  377. leftOver += bytes [byteIndex++] << (4 - byteCount--);
  378. } else {
  379. for (int i = 0; i < remain; i++)
  380. leftOver += bytes [byteIndex++] << byteCount--;
  381. }
  382. if (leftOver > char.MaxValue && posn + 1 < length
  383. || posn < length)
  384. throw new ArgumentException (_("Arg_InsufficientSpace"));
  385. if (leftOver > char.MaxValue) {
  386. chars [posn++] = (char) ((leftOver - 10000) / 0x400 + 0xD800);
  387. chars [posn++] = (char) ((leftOver - 10000) % 0x400 + 0xDC00);
  388. }
  389. else
  390. chars [posn++] = (char) leftOver;
  391. leftOver = -1;
  392. leftOverLength = 0;
  393. }
  394. while (byteCount > 3) {
  395. if (bigEndian) {
  396. ch = (char) (
  397. bytes[byteIndex++] << 24 |
  398. bytes[byteIndex++] << 16 |
  399. bytes[byteIndex++] << 8 |
  400. bytes[byteIndex++]);
  401. } else {
  402. ch = (char) (
  403. bytes[byteIndex++] |
  404. bytes[byteIndex++] << 8 |
  405. bytes[byteIndex++] << 16 |
  406. bytes[byteIndex++] << 24);
  407. }
  408. byteCount -= 4;
  409. if (posn < length) {
  410. chars[posn++] = ch;
  411. } else {
  412. throw new ArgumentException (_("Arg_InsufficientSpace"));
  413. }
  414. }
  415. if (byteCount > 0) {
  416. leftOverLength = byteCount;
  417. leftOver = 0;
  418. if (bigEndian) {
  419. for (int i = 0; i < byteCount; i++)
  420. leftOver += bytes [byteIndex++] << (4 - byteCount--);
  421. } else {
  422. for (int i = 0; i < byteCount; i++)
  423. leftOver += bytes [byteIndex++] << byteCount--;
  424. }
  425. leftOverByte = leftOver;
  426. }
  427. // Finished - return the converted length.
  428. return posn - charIndex;
  429. }
  430. } // class UTF32Decoder
  431. #if NET_2_0
  432. [CLSCompliantAttribute(false)]
  433. public unsafe override int GetByteCount (char *chars, int count)
  434. {
  435. return count * 4;
  436. }
  437. #else
  438. public override byte [] GetBytes (String s)
  439. {
  440. return base.GetBytes (s);
  441. }
  442. #endif
  443. }; // class UTF32Encoding
  444. }; // namespace System.Text
  445. #endif