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