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