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