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