UTF32Encoding.cs 14 KB

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