UTF32Encoding.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. int basis = base.GetHashCode ();
  318. if (bigEndian)
  319. basis ^= 0x1F;
  320. if (byteOrderMark)
  321. basis ^= 0x3F;
  322. return basis;
  323. }
  324. // UTF32 decoder implementation.
  325. private sealed class UTF32Decoder : Decoder
  326. {
  327. private bool bigEndian;
  328. private int leftOverByte;
  329. private int leftOverLength;
  330. // Constructor.
  331. public UTF32Decoder (bool bigEndian)
  332. {
  333. this.bigEndian = bigEndian;
  334. leftOverByte = -1;
  335. }
  336. // Override inherited methods.
  337. public override int GetCharCount (byte[] bytes, int index, int count)
  338. {
  339. if (bytes == null) {
  340. throw new ArgumentNullException ("bytes");
  341. }
  342. if (index < 0 || index > bytes.Length) {
  343. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  344. }
  345. if (count < 0 || count > (bytes.Length - index)) {
  346. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  347. }
  348. if (leftOverByte != -1) {
  349. return (count + 1) / 4;
  350. } else {
  351. return count / 4;
  352. }
  353. }
  354. public override int GetChars (byte[] bytes, int byteIndex,
  355. int byteCount, char[] chars,
  356. int charIndex)
  357. {
  358. if (bytes == null) {
  359. throw new ArgumentNullException ("bytes");
  360. }
  361. if (chars == null) {
  362. throw new ArgumentNullException ("chars");
  363. }
  364. if (byteIndex < 0 || byteIndex > bytes.Length) {
  365. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  366. }
  367. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  368. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  369. }
  370. if (charIndex < 0 || charIndex > chars.Length) {
  371. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  372. }
  373. // Convert the characters.
  374. int posn = charIndex;
  375. int leftOver = leftOverByte;
  376. int length = chars.Length;
  377. char ch;
  378. int remain = 4 - leftOverLength;
  379. if (leftOverLength > 0 && byteCount > remain) {
  380. if (bigEndian) {
  381. for (int i = 0; i < remain; i++)
  382. leftOver += bytes [byteIndex++] << (4 - byteCount--);
  383. } else {
  384. for (int i = 0; i < remain; i++)
  385. leftOver += bytes [byteIndex++] << byteCount--;
  386. }
  387. if (leftOver > char.MaxValue && posn + 1 < length
  388. || posn < length)
  389. throw new ArgumentException (_("Arg_InsufficientSpace"));
  390. if (leftOver > char.MaxValue) {
  391. chars [posn++] = (char) ((leftOver - 10000) / 0x400 + 0xD800);
  392. chars [posn++] = (char) ((leftOver - 10000) % 0x400 + 0xDC00);
  393. }
  394. else
  395. chars [posn++] = (char) leftOver;
  396. leftOver = -1;
  397. leftOverLength = 0;
  398. }
  399. while (byteCount > 3) {
  400. if (bigEndian) {
  401. ch = (char) (
  402. bytes[byteIndex++] << 24 |
  403. bytes[byteIndex++] << 16 |
  404. bytes[byteIndex++] << 8 |
  405. bytes[byteIndex++]);
  406. } else {
  407. ch = (char) (
  408. bytes[byteIndex++] |
  409. bytes[byteIndex++] << 8 |
  410. bytes[byteIndex++] << 16 |
  411. bytes[byteIndex++] << 24);
  412. }
  413. byteCount -= 4;
  414. if (posn < length) {
  415. chars[posn++] = ch;
  416. } else {
  417. throw new ArgumentException (_("Arg_InsufficientSpace"));
  418. }
  419. }
  420. if (byteCount > 0) {
  421. leftOverLength = byteCount;
  422. leftOver = 0;
  423. if (bigEndian) {
  424. for (int i = 0; i < byteCount; i++)
  425. leftOver += bytes [byteIndex++] << (4 - byteCount--);
  426. } else {
  427. for (int i = 0; i < byteCount; i++)
  428. leftOver += bytes [byteIndex++] << byteCount--;
  429. }
  430. leftOverByte = leftOver;
  431. }
  432. // Finished - return the converted length.
  433. return posn - charIndex;
  434. }
  435. } // class UTF32Decoder
  436. #if NET_2_0
  437. [CLSCompliantAttribute(false)]
  438. public unsafe override int GetByteCount (char *chars, int count)
  439. {
  440. return count * 4;
  441. }
  442. #else
  443. public override byte [] GetBytes (String s)
  444. {
  445. return base.GetBytes (s);
  446. }
  447. #endif
  448. #if NET_2_0
  449. // a bunch of practically missing implementations (but should just work)
  450. public override int GetByteCount (string s)
  451. {
  452. return base.GetByteCount (s);
  453. }
  454. [CLSCompliantAttribute (false)]
  455. public override unsafe int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  456. {
  457. return base.GetBytes (chars, charCount, bytes, byteCount);
  458. }
  459. public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
  460. {
  461. return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
  462. }
  463. [CLSCompliantAttribute (false)]
  464. public override unsafe int GetCharCount (byte *bytes, int count)
  465. {
  466. return base.GetCharCount (bytes, count);
  467. }
  468. [CLSCompliantAttribute (false)]
  469. public override unsafe int GetChars (byte *bytes, int byteCount, char* chars, int charCount)
  470. {
  471. return base.GetChars (bytes, byteCount, chars, charCount);
  472. }
  473. public override string GetString (byte [] bytes, int index, int count)
  474. {
  475. return base.GetString (bytes, index, count);
  476. }
  477. public override Encoder GetEncoder ()
  478. {
  479. return base.GetEncoder ();
  480. }
  481. #endif
  482. }; // class UTF32Encoding
  483. }; // namespace System.Text
  484. #endif