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.
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;
47 private bool bigEndian
;
48 private bool byteOrderMark
;
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
);
70 SetFallbackInternal (new EncoderReplacementFallback ("\uFFFD"),
71 new DecoderReplacementFallback ("\uFFFD"));
74 body_name
= "utf-32BE";
75 encoding_name
= "UTF-32 (Big-Endian)";
76 header_name
= "utf-32BE";
77 web_name
= "utf-32BE";
80 encoding_name
= "UTF-32";
81 header_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
)
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"));
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]))
109 // FIXME: handle fallback
110 // ret += DecoderFallback.MaxCharCount;
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
)
125 throw new ArgumentNullException ("chars");
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
;
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;
151 for (int i
= 0; i
< 4; i
++) {
152 bytes
[posn
+ 3 - i
] = (byte) (value % 0x100);
157 for (int i
= 0; i
< 4; i
++) {
158 bytes
[posn
++] = (byte) (value % 0x100);
164 // FIXME: use fallback
169 bytes
[posn
++] = (byte) '?';
171 bytes
[posn
++] = (byte) '?';
181 bytes
[posn
++] = (byte)(ch
>> 8);
182 bytes
[posn
++] = (byte)ch
;
184 bytes
[posn
++] = (byte)ch
;
185 bytes
[posn
++] = (byte)(ch
>> 8);
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
)
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"));
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
)
215 throw new ArgumentNullException ("bytes");
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.
233 if (byteCount >= 2) {
234 if (bytes[byteIndex] == (byte)0xFE && bytes[byteIndex + 1] == (byte)0xFF) {
236 } else if (bytes[byteIndex] == (byte)0xFF && bytes[byteIndex + 1] == (byte)0xFE) {
239 isBigEndian = bigEndian;
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
;
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]);
264 while (byteCount
>= 4) {
265 chars
[posn
++] = (char) (
267 bytes
[byteIndex
+ 1] << 8 |
268 bytes
[byteIndex
+ 2] << 16 |
269 bytes
[byteIndex
+ 3] << 24);
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
)
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
)
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 ()
308 byte[] preamble
= new byte[4];
310 preamble
[2] = (byte)0xFE;
311 preamble
[3] = (byte)0xFF;
313 preamble
[0] = (byte)0xFF;
314 preamble
[1] = (byte)0xFE;
322 // Determine if this object is equal to another.
323 public override bool Equals (Object
value)
325 UTF32Encoding enc
= (value as UTF32Encoding
);
327 return (codePage
== enc
.codePage
&&
328 bigEndian
== enc
.bigEndian
&&
329 byteOrderMark
== enc
.byteOrderMark
&&
330 base.Equals (value));
336 // Get the hash code for this object.
337 public override int GetHashCode ()
339 int basis
= base.GetHashCode ();
347 // UTF32 decoder implementation.
348 private sealed class UTF32Decoder
: Decoder
350 private bool bigEndian
;
351 private int leftOverByte
;
352 private int leftOverLength
;
355 public UTF32Decoder (bool bigEndian
)
357 this.bigEndian
= bigEndian
;
361 // Override inherited methods.
362 public override int GetCharCount (byte[] bytes
, int index
, int count
)
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;
380 public override int GetChars (byte[] bytes
, int byteIndex
,
381 int byteCount
, char[] chars
,
385 throw new ArgumentNullException ("bytes");
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
;
406 int remain
= 4 - leftOverLength
;
407 if (leftOverLength
> 0 && byteCount
> remain
) {
409 for (int i
= 0; i
< remain
; i
++)
410 leftOver
+= bytes
[byteIndex
++] << (4 - byteCount
--);
412 for (int i
= 0; i
< remain
; i
++)
413 leftOver
+= bytes
[byteIndex
++] << byteCount
--;
415 if (leftOver
> char.MaxValue
&& posn
+ 1 < 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);
423 chars
[posn
++] = (char) leftOver
;
429 while (byteCount
> 3) {
432 bytes
[byteIndex
++] << 24 |
433 bytes
[byteIndex
++] << 16 |
434 bytes
[byteIndex
++] << 8 |
439 bytes
[byteIndex
++] << 8 |
440 bytes
[byteIndex
++] << 16 |
441 bytes
[byteIndex
++] << 24);
448 throw new ArgumentException (_("Arg_InsufficientSpace"));
452 leftOverLength
= byteCount
;
455 for (int i
= 0; i
< byteCount
; i
++)
456 leftOver
+= bytes
[byteIndex
++] << (4 - byteCount
--);
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
)
474 throw new ArgumentNullException ("chars");
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