GenericParameter.cs: override Module properly
[mcs.git] / class / corlib / System.Text / UTF32Encoding.cs
blob9e97f6aefab42b81c3f2a581748b5db47d4d2b56
1 /*
2 * UTF32Encoding.cs - Implementation of the
3 * "System.Text.UTF32Encoding" class.
5 * Author: Atsushi Enomoto <atsushi@ximian.com>
7 * Copyright (C) 2005 Novell, Inc. http://www.novell.com
9 * The basic part (now almost nothing) is copied from UnicodeEncoding.cs.
10 * Original copyrights goes here:
12 * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
13 * Copyright (C) 2003, 2004 Novell, Inc.
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:
22 * The above copyright notice and this permission notice shall be included
23 * in all copies or substantial portions of the Software.
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.
34 namespace System.Text
37 using System;
39 [Serializable]
40 public sealed class UTF32Encoding : Encoding
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;
46 // Internal state.
47 private bool bigEndian;
48 private bool byteOrderMark;
50 // Constructors.
51 public UTF32Encoding () : this (false, true, false)
55 public UTF32Encoding (bool bigEndian, bool byteOrderMark)
56 : this (bigEndian, byteOrderMark, false)
60 public UTF32Encoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidCharacters)
61 : base ((bigEndian ? BIG_UTF32_CODE_PAGE : UTF32_CODE_PAGE))
63 this.bigEndian = bigEndian;
64 this.byteOrderMark = byteOrderMark;
66 if (throwOnInvalidCharacters)
67 SetFallbackInternal (EncoderFallback.ExceptionFallback,
68 DecoderFallback.ExceptionFallback);
69 else
70 SetFallbackInternal (new EncoderReplacementFallback ("\uFFFD"),
71 new DecoderReplacementFallback ("\uFFFD"));
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";
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;
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)
94 if (chars == null) {
95 throw new ArgumentNullException ("chars");
97 if (index < 0 || index > chars.Length) {
98 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
100 if (count < 0 || count > (chars.Length - index)) {
101 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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;
113 else
114 ret += 4;
116 return ret;
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)
124 if (chars == null) {
125 throw new ArgumentNullException ("chars");
127 if (bytes == null) {
128 throw new ArgumentNullException ("bytes");
130 if (charIndex < 0 || charIndex > chars.Length) {
131 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
133 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
134 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
136 if (byteIndex < 0 || byteIndex > bytes.Length) {
137 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
139 if ((bytes.Length - byteIndex) < (charCount * 4)) {
140 throw new ArgumentException (_("Arg_InsufficientSpace"));
142 int posn = byteIndex;
143 char ch;
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;
155 posn += 4;
156 } else {
157 for (int i = 0; i < 4; i++) {
158 bytes [posn++] = (byte) (value % 0x100);
159 value >>= 8;
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;
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;
192 return posn - byteIndex;
195 // Get the number of characters needed to decode a byte buffer.
196 public override int GetCharCount (byte[] bytes, int index, int count)
198 if (bytes == null) {
199 throw new ArgumentNullException ("bytes");
201 if (index < 0 || index > bytes.Length) {
202 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
204 if (count < 0 || count > (bytes.Length - index)) {
205 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
207 return count / 4;
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)
214 if (bytes == null) {
215 throw new ArgumentNullException ("bytes");
217 if (chars == null) {
218 throw new ArgumentNullException ("chars");
220 if (byteIndex < 0 || byteIndex > bytes.Length) {
221 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
223 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
224 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
226 if (charIndex < 0 || charIndex > chars.Length) {
227 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
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;
241 } else {
242 isBigEndian = bigEndian;
246 // Validate that we have sufficient space in "chars".
247 if ((chars.Length - charIndex) < (byteCount / 4)) {
248 throw new ArgumentException (_("Arg_InsufficientSpace"));
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;
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;
274 return posn - charIndex;
277 // Get the maximum number of bytes needed to encode a
278 // specified number of characters.
279 public override int GetMaxByteCount (int charCount)
281 if (charCount < 0) {
282 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
284 return charCount * 4;
287 // Get the maximum number of characters needed to decode a
288 // specified number of bytes.
289 public override int GetMaxCharCount (int byteCount)
291 if (byteCount < 0) {
292 throw new ArgumentOutOfRangeException
293 ("byteCount", _("ArgRange_NonNegative"));
295 return byteCount / 4;
298 // Get a UTF32-specific decoder that is attached to this instance.
299 public override Decoder GetDecoder ()
301 return new UTF32Decoder (bigEndian);
304 // Get the UTF32 preamble.
305 public override byte[] GetPreamble ()
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;
316 return preamble;
317 } else {
318 return new byte [0];
322 // Determine if this object is equal to another.
323 public override bool Equals (Object value)
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;
336 // Get the hash code for this object.
337 public override int GetHashCode ()
339 int basis = base.GetHashCode ();
340 if (bigEndian)
341 basis ^= 0x1F;
342 if (byteOrderMark)
343 basis ^= 0x3F;
344 return basis;
347 // UTF32 decoder implementation.
348 private sealed class UTF32Decoder : Decoder
350 private bool bigEndian;
351 private int leftOverByte;
352 private int leftOverLength;
354 // Constructor.
355 public UTF32Decoder (bool bigEndian)
357 this.bigEndian = bigEndian;
358 leftOverByte = -1;
361 // Override inherited methods.
362 public override int GetCharCount (byte[] bytes, int index, int count)
364 if (bytes == null) {
365 throw new ArgumentNullException ("bytes");
367 if (index < 0 || index > bytes.Length) {
368 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
370 if (count < 0 || count > (bytes.Length - index)) {
371 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
373 if (leftOverByte != -1) {
374 return (count + 1) / 4;
375 } else {
376 return count / 4;
380 public override int GetChars (byte[] bytes, int byteIndex,
381 int byteCount, char[] chars,
382 int charIndex)
384 if (bytes == null) {
385 throw new ArgumentNullException ("bytes");
387 if (chars == null) {
388 throw new ArgumentNullException ("chars");
390 if (byteIndex < 0 || byteIndex > bytes.Length) {
391 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
393 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
394 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
396 if (charIndex < 0 || charIndex > chars.Length) {
397 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
400 // Convert the characters.
401 int posn = charIndex;
402 int leftOver = leftOverByte;
403 int length = chars.Length;
404 char ch;
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--;
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);
422 else
423 chars [posn++] = (char) leftOver;
425 leftOver = -1;
426 leftOverLength = 0;
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);
443 byteCount -= 4;
445 if (posn < length) {
446 chars[posn++] = ch;
447 } else {
448 throw new ArgumentException (_("Arg_InsufficientSpace"));
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--;
461 leftOverByte = leftOver;
464 // Finished - return the converted length.
465 return posn - charIndex;
468 } // class UTF32Decoder
470 [CLSCompliantAttribute(false)]
471 public unsafe override int GetByteCount (char *chars, int count)
473 if (chars == null)
474 throw new ArgumentNullException ("chars");
475 return count * 4;
478 // a bunch of practically missing implementations (but should just work)
480 public override int GetByteCount (string s)
482 return base.GetByteCount (s);
485 [CLSCompliantAttribute (false)]
486 public override unsafe int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
488 return base.GetBytes (chars, charCount, bytes, byteCount);
491 public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
493 return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
496 [CLSCompliantAttribute (false)]
497 public override unsafe int GetCharCount (byte *bytes, int count)
499 return base.GetCharCount (bytes, count);
502 [CLSCompliantAttribute (false)]
503 public override unsafe int GetChars (byte *bytes, int byteCount, char* chars, int charCount)
505 return base.GetChars (bytes, byteCount, chars, charCount);
508 public override string GetString (byte [] bytes, int index, int count)
510 return base.GetString (bytes, index, count);
513 public override Encoder GetEncoder ()
515 return base.GetEncoder ();
517 }; // class UTF32Encoding
519 }; // namespace System.Text