UTF32Encoding.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 (new EncoderReplacementFallback ("\uFFFD"),
  64. new DecoderReplacementFallback ("\uFFFD"));
  65. if (bigEndian){
  66. body_name = "utf-32BE";
  67. encoding_name = "UTF-32 (Big-Endian)";
  68. header_name = "utf-32BE";
  69. web_name = "utf-32BE";
  70. } else {
  71. body_name = "utf-32";
  72. encoding_name = "UTF-32";
  73. header_name = "utf-32";
  74. web_name = "utf-32";
  75. }
  76. // Windows reports the same code page number for
  77. // both the little-endian and big-endian forms.
  78. windows_code_page = UTF32_CODE_PAGE;
  79. }
  80. // Get the number of bytes needed to encode a character buffer.
  81. [MonoTODO ("handle fallback")]
  82. public override int GetByteCount (char[] chars, int index, int count)
  83. {
  84. if (chars == null) {
  85. throw new ArgumentNullException ("chars");
  86. }
  87. if (index < 0 || index > chars.Length) {
  88. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  89. }
  90. if (count < 0 || count > (chars.Length - index)) {
  91. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  92. }
  93. int ret = 0;
  94. for (int i = index; i < count; i++) {
  95. if (Char.IsSurrogate (chars [i])) {
  96. if (i + 1 < chars.Length && Char.IsSurrogate (chars [i + 1]))
  97. ret += 4;
  98. else
  99. // FIXME: handle fallback
  100. // ret += DecoderFallback.MaxCharCount;
  101. ret += 4;
  102. }
  103. else
  104. ret += 4;
  105. }
  106. return ret;
  107. }
  108. // Get the bytes that result from encoding a character buffer.
  109. [MonoTODO ("handle fallback")]
  110. public override int GetBytes (char[] chars, int charIndex, int charCount,
  111. byte[] bytes, int byteIndex)
  112. {
  113. if (chars == null) {
  114. throw new ArgumentNullException ("chars");
  115. }
  116. if (bytes == null) {
  117. throw new ArgumentNullException ("bytes");
  118. }
  119. if (charIndex < 0 || charIndex > chars.Length) {
  120. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  121. }
  122. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  123. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  124. }
  125. if (byteIndex < 0 || byteIndex > bytes.Length) {
  126. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  127. }
  128. if ((bytes.Length - byteIndex) < (charCount * 4)) {
  129. throw new ArgumentException (_("Arg_InsufficientSpace"));
  130. }
  131. int posn = byteIndex;
  132. char ch;
  133. while (charCount-- > 0) {
  134. ch = chars[charIndex++];
  135. if (Char.IsSurrogate (ch)) {
  136. if (charCount-- > 0) {
  137. int value = 0x400 * (ch - 0xD800) + 0x10000 + chars [charIndex++] - 0xDC00;
  138. if (bigEndian) {
  139. for (int i = 0; i < 4; i++) {
  140. bytes [posn + 3 - i] = (byte) (value % 0x100);
  141. value >>= 8;
  142. }
  143. posn += 4;
  144. } else {
  145. for (int i = 0; i < 4; i++) {
  146. bytes [posn++] = (byte) (value % 0x100);
  147. value >>= 8;
  148. }
  149. }
  150. } else {
  151. // Illegal surrogate
  152. // FIXME: use fallback
  153. if (bigEndian) {
  154. bytes[posn++] = 0;
  155. bytes[posn++] = 0;
  156. bytes[posn++] = 0;
  157. bytes[posn++] = (byte) '?';
  158. } else {
  159. bytes[posn++] = (byte) '?';
  160. bytes[posn++] = 0;
  161. bytes[posn++] = 0;
  162. bytes[posn++] = 0;
  163. }
  164. }
  165. } else {
  166. if (bigEndian) {
  167. bytes[posn++] = 0;
  168. bytes[posn++] = 0;
  169. bytes[posn++] = (byte)(ch >> 8);
  170. bytes[posn++] = (byte)ch;
  171. } else {
  172. bytes[posn++] = (byte)ch;
  173. bytes[posn++] = (byte)(ch >> 8);
  174. bytes[posn++] = 0;
  175. bytes[posn++] = 0;
  176. }
  177. }
  178. }
  179. return posn - byteIndex;
  180. }
  181. // Get the number of characters needed to decode a byte buffer.
  182. public override int GetCharCount (byte[] bytes, int index, int count)
  183. {
  184. if (bytes == null) {
  185. throw new ArgumentNullException ("bytes");
  186. }
  187. if (index < 0 || index > bytes.Length) {
  188. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  189. }
  190. if (count < 0 || count > (bytes.Length - index)) {
  191. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  192. }
  193. return count / 4;
  194. }
  195. // Get the characters that result from decoding a byte buffer.
  196. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  197. char[] chars, int charIndex)
  198. {
  199. if (bytes == null) {
  200. throw new ArgumentNullException ("bytes");
  201. }
  202. if (chars == null) {
  203. throw new ArgumentNullException ("chars");
  204. }
  205. if (byteIndex < 0 || byteIndex > bytes.Length) {
  206. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  207. }
  208. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  209. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  210. }
  211. if (charIndex < 0 || charIndex > chars.Length) {
  212. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  213. }
  214. /*
  215. // Determine the byte order in the incoming buffer.
  216. bool isBigEndian;
  217. if (byteCount >= 2) {
  218. if (bytes[byteIndex] == (byte)0xFE && bytes[byteIndex + 1] == (byte)0xFF) {
  219. isBigEndian = true;
  220. } else if (bytes[byteIndex] == (byte)0xFF && bytes[byteIndex + 1] == (byte)0xFE) {
  221. isBigEndian = false;
  222. } else {
  223. isBigEndian = bigEndian;
  224. }
  225. } else {
  226. isBigEndian = bigEndian;
  227. }
  228. */
  229. // Validate that we have sufficient space in "chars".
  230. if ((chars.Length - charIndex) < (byteCount / 4)) {
  231. throw new ArgumentException (_("Arg_InsufficientSpace"));
  232. }
  233. // Convert the characters.
  234. int posn = charIndex;
  235. if (bigEndian) {
  236. while (byteCount >= 4) {
  237. chars[posn++] = (char) (
  238. bytes[byteIndex] << 24 |
  239. bytes[byteIndex + 1] << 16 |
  240. bytes[byteIndex + 2] << 8 |
  241. bytes[byteIndex + 3]);
  242. byteIndex += 4;
  243. byteCount -= 4;
  244. }
  245. } else {
  246. while (byteCount >= 4) {
  247. chars[posn++] = (char) (
  248. bytes[byteIndex] |
  249. bytes[byteIndex + 1] << 8 |
  250. bytes[byteIndex + 2] << 16 |
  251. bytes[byteIndex + 3] << 24);
  252. byteIndex += 4;
  253. byteCount -= 4;
  254. }
  255. }
  256. return posn - charIndex;
  257. }
  258. // Get the maximum number of bytes needed to encode a
  259. // specified number of characters.
  260. public override int GetMaxByteCount (int charCount)
  261. {
  262. if (charCount < 0) {
  263. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  264. }
  265. return charCount * 4;
  266. }
  267. // Get the maximum number of characters needed to decode a
  268. // specified number of bytes.
  269. public override int GetMaxCharCount (int byteCount)
  270. {
  271. if (byteCount < 0) {
  272. throw new ArgumentOutOfRangeException
  273. ("byteCount", _("ArgRange_NonNegative"));
  274. }
  275. return byteCount / 4;
  276. }
  277. // Get a UTF32-specific decoder that is attached to this instance.
  278. public override Decoder GetDecoder ()
  279. {
  280. return new UTF32Decoder (bigEndian);
  281. }
  282. // Get the UTF32 preamble.
  283. public override byte[] GetPreamble ()
  284. {
  285. if (byteOrderMark) {
  286. byte[] preamble = new byte[4];
  287. if (bigEndian) {
  288. preamble[2] = (byte)0xFE;
  289. preamble[3] = (byte)0xFF;
  290. } else {
  291. preamble[0] = (byte)0xFF;
  292. preamble[1] = (byte)0xFE;
  293. }
  294. return preamble;
  295. } else {
  296. return new byte [0];
  297. }
  298. }
  299. // Determine if this object is equal to another.
  300. public override bool Equals (Object value)
  301. {
  302. UTF32Encoding enc = (value as UTF32Encoding);
  303. if (enc != null) {
  304. return (codePage == enc.codePage &&
  305. bigEndian == enc.bigEndian &&
  306. byteOrderMark == enc.byteOrderMark &&
  307. base.Equals (value));
  308. } else {
  309. return false;
  310. }
  311. }
  312. // Get the hash code for this object.
  313. public override int GetHashCode ()
  314. {
  315. int basis = base.GetHashCode ();
  316. if (bigEndian)
  317. basis ^= 0x1F;
  318. if (byteOrderMark)
  319. basis ^= 0x3F;
  320. return basis;
  321. }
  322. // UTF32 decoder implementation.
  323. private sealed class UTF32Decoder : Decoder
  324. {
  325. private bool bigEndian;
  326. private int leftOverByte;
  327. private int leftOverLength;
  328. // Constructor.
  329. public UTF32Decoder (bool bigEndian)
  330. {
  331. this.bigEndian = bigEndian;
  332. leftOverByte = -1;
  333. }
  334. // Override inherited methods.
  335. public override int GetCharCount (byte[] bytes, int index, int count)
  336. {
  337. if (bytes == null) {
  338. throw new ArgumentNullException ("bytes");
  339. }
  340. if (index < 0 || index > bytes.Length) {
  341. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  342. }
  343. if (count < 0 || count > (bytes.Length - index)) {
  344. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  345. }
  346. if (leftOverByte != -1) {
  347. return (count + 1) / 4;
  348. } else {
  349. return count / 4;
  350. }
  351. }
  352. public override int GetChars (byte[] bytes, int byteIndex,
  353. int byteCount, char[] chars,
  354. int charIndex)
  355. {
  356. if (bytes == null) {
  357. throw new ArgumentNullException ("bytes");
  358. }
  359. if (chars == null) {
  360. throw new ArgumentNullException ("chars");
  361. }
  362. if (byteIndex < 0 || byteIndex > bytes.Length) {
  363. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  364. }
  365. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  366. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  367. }
  368. if (charIndex < 0 || charIndex > chars.Length) {
  369. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  370. }
  371. // Convert the characters.
  372. int posn = charIndex;
  373. int leftOver = leftOverByte;
  374. int length = chars.Length;
  375. char ch;
  376. int remain = 4 - leftOverLength;
  377. if (leftOverLength > 0 && byteCount > remain) {
  378. if (bigEndian) {
  379. for (int i = 0; i < remain; i++)
  380. leftOver += bytes [byteIndex++] << (4 - byteCount--);
  381. } else {
  382. for (int i = 0; i < remain; i++)
  383. leftOver += bytes [byteIndex++] << byteCount--;
  384. }
  385. if (leftOver > char.MaxValue && posn + 1 < length
  386. || posn < length)
  387. throw new ArgumentException (_("Arg_InsufficientSpace"));
  388. if (leftOver > char.MaxValue) {
  389. chars [posn++] = (char) ((leftOver - 10000) / 0x400 + 0xD800);
  390. chars [posn++] = (char) ((leftOver - 10000) % 0x400 + 0xDC00);
  391. }
  392. else
  393. chars [posn++] = (char) leftOver;
  394. leftOver = -1;
  395. leftOverLength = 0;
  396. }
  397. while (byteCount > 3) {
  398. if (bigEndian) {
  399. ch = (char) (
  400. bytes[byteIndex++] << 24 |
  401. bytes[byteIndex++] << 16 |
  402. bytes[byteIndex++] << 8 |
  403. bytes[byteIndex++]);
  404. } else {
  405. ch = (char) (
  406. bytes[byteIndex++] |
  407. bytes[byteIndex++] << 8 |
  408. bytes[byteIndex++] << 16 |
  409. bytes[byteIndex++] << 24);
  410. }
  411. byteCount -= 4;
  412. if (posn < length) {
  413. chars[posn++] = ch;
  414. } else {
  415. throw new ArgumentException (_("Arg_InsufficientSpace"));
  416. }
  417. }
  418. if (byteCount > 0) {
  419. leftOverLength = byteCount;
  420. leftOver = 0;
  421. if (bigEndian) {
  422. for (int i = 0; i < byteCount; i++)
  423. leftOver += bytes [byteIndex++] << (4 - byteCount--);
  424. } else {
  425. for (int i = 0; i < byteCount; i++)
  426. leftOver += bytes [byteIndex++] << byteCount--;
  427. }
  428. leftOverByte = leftOver;
  429. }
  430. // Finished - return the converted length.
  431. return posn - charIndex;
  432. }
  433. } // class UTF32Decoder
  434. #if NET_2_0
  435. [CLSCompliantAttribute(false)]
  436. public unsafe override int GetByteCount (char *chars, int count)
  437. {
  438. return count * 4;
  439. }
  440. #else
  441. public override byte [] GetBytes (String s)
  442. {
  443. return base.GetBytes (s);
  444. }
  445. #endif
  446. #if NET_2_0
  447. // a bunch of practically missing implementations (but should just work)
  448. public override int GetByteCount (string s)
  449. {
  450. return base.GetByteCount (s);
  451. }
  452. [CLSCompliantAttribute (false)]
  453. public override unsafe int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
  454. {
  455. return base.GetBytes (chars, charCount, bytes, byteCount);
  456. }
  457. public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
  458. {
  459. return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
  460. }
  461. [CLSCompliantAttribute (false)]
  462. public override unsafe int GetCharCount (byte *bytes, int count)
  463. {
  464. return base.GetCharCount (bytes, count);
  465. }
  466. [CLSCompliantAttribute (false)]
  467. public override unsafe int GetChars (byte *bytes, int byteCount, char* chars, int charCount)
  468. {
  469. return base.GetChars (bytes, byteCount, chars, charCount);
  470. }
  471. public override string GetString (byte [] bytes, int index, int count)
  472. {
  473. return base.GetString (bytes, index, count);
  474. }
  475. public override Encoder GetEncoder ()
  476. {
  477. return base.GetEncoder ();
  478. }
  479. #endif
  480. }; // class UTF32Encoding
  481. }; // namespace System.Text
  482. #endif