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.
42 public sealed class UTF32Encoding : Encoding
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;
49 private bool bigEndian;
50 private bool byteOrderMark;
53 public UTF32Encoding () : this (false, true, false)
57 public UTF32Encoding (bool bigEndian, bool byteOrderMark)
58 : this (bigEndian, byteOrderMark, false)
62 public UTF32Encoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidCharacters)
63 : base ((bigEndian ? BIG_UTF32_CODE_PAGE : UTF32_CODE_PAGE))
65 this.bigEndian = bigEndian;
66 this.byteOrderMark = byteOrderMark;
68 if (throwOnInvalidCharacters)
69 SetFallbackInternal (EncoderFallback.ExceptionFallback,
70 DecoderFallback.ExceptionFallback);
72 SetFallbackInternal (new EncoderReplacementFallback ("\uFFFD"),
73 new DecoderReplacementFallback ("\uFFFD"));
76 body_name = "utf-32BE";
77 encoding_name = "UTF-32 (Big-Endian)";
78 header_name = "utf-32BE";
79 web_name = "utf-32BE";
82 encoding_name = "UTF-32";
83 header_name = "utf-32";
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;
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)
97 throw new ArgumentNullException ("chars");
99 if (index < 0 || index > chars.Length) {
100 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
102 if (count < 0 || count > (chars.Length - index)) {
103 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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]))
111 // FIXME: handle fallback
112 // ret += DecoderFallback.MaxCharCount;
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)
127 throw new ArgumentNullException ("chars");
130 throw new ArgumentNullException ("bytes");
132 if (charIndex < 0 || charIndex > chars.Length) {
133 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
135 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
136 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
138 if (byteIndex < 0 || byteIndex > bytes.Length) {
139 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
141 if ((bytes.Length - byteIndex) < (charCount * 4)) {
142 throw new ArgumentException (_("Arg_InsufficientSpace"));
144 int posn = byteIndex;
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;
153 for (int i = 0; i < 4; i++) {
154 bytes [posn + 3 - i] = (byte) (value % 0x100);
159 for (int i = 0; i < 4; i++) {
160 bytes [posn++] = (byte) (value % 0x100);
166 // FIXME: use fallback
171 bytes[posn++] = (byte) '?';
173 bytes[posn++] = (byte) '?';
183 bytes[posn++] = (byte)(ch >> 8);
184 bytes[posn++] = (byte)ch;
186 bytes[posn++] = (byte)ch;
187 bytes[posn++] = (byte)(ch >> 8);
194 return posn - byteIndex;
197 // Get the number of characters needed to decode a byte buffer.
198 public override int GetCharCount (byte[] bytes, int index, int count)
201 throw new ArgumentNullException ("bytes");
203 if (index < 0 || index > bytes.Length) {
204 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
206 if (count < 0 || count > (bytes.Length - index)) {
207 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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)
217 throw new ArgumentNullException ("bytes");
220 throw new ArgumentNullException ("chars");
222 if (byteIndex < 0 || byteIndex > bytes.Length) {
223 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
225 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
226 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
228 if (charIndex < 0 || charIndex > chars.Length) {
229 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
233 // Determine the byte order in the incoming buffer.
235 if (byteCount >= 2) {
236 if (bytes[byteIndex] == (byte)0xFE && bytes[byteIndex + 1] == (byte)0xFF) {
238 } else if (bytes[byteIndex] == (byte)0xFF && bytes[byteIndex + 1] == (byte)0xFE) {
241 isBigEndian = bigEndian;
244 isBigEndian = bigEndian;
248 // Validate that we have sufficient space in "chars".
249 if ((chars.Length - charIndex) < (byteCount / 4)) {
250 throw new ArgumentException (_("Arg_InsufficientSpace"));
253 // Convert the characters.
254 int posn = charIndex;
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]);
266 while (byteCount >= 4) {
267 chars[posn++] = (char) (
269 bytes[byteIndex + 1] << 8 |
270 bytes[byteIndex + 2] << 16 |
271 bytes[byteIndex + 3] << 24);
276 return posn - charIndex;
279 // Get the maximum number of bytes needed to encode a
280 // specified number of characters.
281 public override int GetMaxByteCount (int charCount)
284 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
286 return charCount * 4;
289 // Get the maximum number of characters needed to decode a
290 // specified number of bytes.
291 public override int GetMaxCharCount (int byteCount)
294 throw new ArgumentOutOfRangeException
295 ("byteCount", _("ArgRange_NonNegative"));
297 return byteCount / 4;
300 // Get a UTF32-specific decoder that is attached to this instance.
301 public override Decoder GetDecoder ()
303 return new UTF32Decoder (bigEndian);
306 // Get the UTF32 preamble.
307 public override byte[] GetPreamble ()
310 byte[] preamble = new byte[4];
312 preamble[2] = (byte)0xFE;
313 preamble[3] = (byte)0xFF;
315 preamble[0] = (byte)0xFF;
316 preamble[1] = (byte)0xFE;
324 // Determine if this object is equal to another.
325 public override bool Equals (Object value)
327 UTF32Encoding enc = (value as UTF32Encoding);
329 return (codePage == enc.codePage &&
330 bigEndian == enc.bigEndian &&
331 byteOrderMark == enc.byteOrderMark &&
332 base.Equals (value));
338 // Get the hash code for this object.
339 public override int GetHashCode ()
341 int basis = base.GetHashCode ();
349 // UTF32 decoder implementation.
350 private sealed class UTF32Decoder : Decoder
352 private bool bigEndian;
353 private int leftOverByte;
354 private int leftOverLength;
357 public UTF32Decoder (bool bigEndian)
359 this.bigEndian = bigEndian;
363 // Override inherited methods.
364 public override int GetCharCount (byte[] bytes, int index, int count)
367 throw new ArgumentNullException ("bytes");
369 if (index < 0 || index > bytes.Length) {
370 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
372 if (count < 0 || count > (bytes.Length - index)) {
373 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
375 if (leftOverByte != -1) {
376 return (count + 1) / 4;
382 public override int GetChars (byte[] bytes, int byteIndex,
383 int byteCount, char[] chars,
387 throw new ArgumentNullException ("bytes");
390 throw new ArgumentNullException ("chars");
392 if (byteIndex < 0 || byteIndex > bytes.Length) {
393 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
395 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
396 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
398 if (charIndex < 0 || charIndex > chars.Length) {
399 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
402 // Convert the characters.
403 int posn = charIndex;
404 int leftOver = leftOverByte;
405 int length = chars.Length;
408 int remain = 4 - leftOverLength;
409 if (leftOverLength > 0 && byteCount > remain) {
411 for (int i = 0; i < remain; i++)
412 leftOver += bytes [byteIndex++] << (4 - byteCount--);
414 for (int i = 0; i < remain; i++)
415 leftOver += bytes [byteIndex++] << byteCount--;
417 if (leftOver > char.MaxValue && posn + 1 < 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);
425 chars [posn++] = (char) leftOver;
431 while (byteCount > 3) {
434 bytes[byteIndex++] << 24 |
435 bytes[byteIndex++] << 16 |
436 bytes[byteIndex++] << 8 |
441 bytes[byteIndex++] << 8 |
442 bytes[byteIndex++] << 16 |
443 bytes[byteIndex++] << 24);
450 throw new ArgumentException (_("Arg_InsufficientSpace"));
454 leftOverLength = byteCount;
457 for (int i = 0; i < byteCount; i++)
458 leftOver += bytes [byteIndex++] << (4 - byteCount--);
460 for (int i = 0; i < byteCount; i++)
461 leftOver += bytes [byteIndex++] << byteCount--;
463 leftOverByte = leftOver;
466 // Finished - return the converted length.
467 return posn - charIndex;
470 } // class UTF32Decoder
473 [CLSCompliantAttribute(false)]
474 public unsafe override int GetByteCount (char *chars, int count)
477 throw new ArgumentNullException ("chars");
481 public override byte [] GetBytes (String s)
483 return base.GetBytes (s);
488 // a bunch of practically missing implementations (but should just work)
490 public override int GetByteCount (string s)
492 return base.GetByteCount (s);
495 [CLSCompliantAttribute (false)]
496 public override unsafe int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
498 return base.GetBytes (chars, charCount, bytes, byteCount);
501 public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
503 return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
506 [CLSCompliantAttribute (false)]
507 public override unsafe int GetCharCount (byte *bytes, int count)
509 return base.GetCharCount (bytes, count);
512 [CLSCompliantAttribute (false)]
513 public override unsafe int GetChars (byte *bytes, int byteCount, char* chars, int charCount)
515 return base.GetChars (bytes, byteCount, chars, charCount);
518 public override string GetString (byte [] bytes, int index, int count)
520 return base.GetString (bytes, index, count);
523 public override Encoder GetEncoder ()
525 return base.GetEncoder ();
529 }; // class UTF32Encoding
531 }; // namespace System.Text