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 throwOnInvalid)
63 : base ((bigEndian ? BIG_UTF32_CODE_PAGE : UTF32_CODE_PAGE))
65 this.bigEndian = bigEndian;
66 this.byteOrderMark = byteOrderMark;
69 SetFallbackInternal (EncoderFallback.ExceptionFallback,
70 DecoderFallback.ExceptionFallback);
72 SetFallbackInternal (EncoderFallback.ReplacementFallback,
73 DecoderFallback.ReplacementFallback);
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";
83 encoding_name = "UTF-32";
84 header_name = "utf-32";
85 is_browser_save = true;
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;
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)
99 throw new ArgumentNullException ("chars");
101 if (index < 0 || index > chars.Length) {
102 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
104 if (count < 0 || count > (chars.Length - index)) {
105 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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]))
113 // FIXME: handle fallback
114 // ret += DecoderFallback.MaxCharCount;
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)
129 throw new ArgumentNullException ("chars");
132 throw new ArgumentNullException ("bytes");
134 if (charIndex < 0 || charIndex > chars.Length) {
135 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
137 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
138 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
140 if (byteIndex < 0 || byteIndex > bytes.Length) {
141 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
143 if ((bytes.Length - byteIndex) < (charCount * 4)) {
144 throw new ArgumentException (_("Arg_InsufficientSpace"));
146 int posn = byteIndex;
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;
155 for (int i = 0; i < 4; i++) {
156 bytes [posn + 3 - i] = (byte) (value % 0x100);
161 for (int i = 0; i < 4; i++) {
162 bytes [posn++] = (byte) (value % 0x100);
168 // FIXME: use fallback
173 bytes[posn++] = (byte) '?';
175 bytes[posn++] = (byte) '?';
185 bytes[posn++] = (byte)(ch >> 8);
186 bytes[posn++] = (byte)ch;
188 bytes[posn++] = (byte)ch;
189 bytes[posn++] = (byte)(ch >> 8);
196 return posn - byteIndex;
199 // Get the number of characters needed to decode a byte buffer.
200 public override int GetCharCount (byte[] bytes, int index, int count)
203 throw new ArgumentNullException ("bytes");
205 if (index < 0 || index > bytes.Length) {
206 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
208 if (count < 0 || count > (bytes.Length - index)) {
209 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
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)
219 throw new ArgumentNullException ("bytes");
222 throw new ArgumentNullException ("chars");
224 if (byteIndex < 0 || byteIndex > bytes.Length) {
225 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
227 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
228 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
230 if (charIndex < 0 || charIndex > chars.Length) {
231 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
235 // Determine the byte order in the incoming buffer.
237 if (byteCount >= 2) {
238 if (bytes[byteIndex] == (byte)0xFE && bytes[byteIndex + 1] == (byte)0xFF) {
240 } else if (bytes[byteIndex] == (byte)0xFF && bytes[byteIndex + 1] == (byte)0xFE) {
243 isBigEndian = bigEndian;
246 isBigEndian = bigEndian;
250 // Validate that we have sufficient space in "chars".
251 if ((chars.Length - charIndex) < (byteCount / 4)) {
252 throw new ArgumentException (_("Arg_InsufficientSpace"));
255 // Convert the characters.
256 int posn = charIndex;
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]);
268 while (byteCount >= 4) {
269 chars[posn++] = (char) (
271 bytes[byteIndex + 1] << 8 |
272 bytes[byteIndex + 2] << 16 |
273 bytes[byteIndex + 3] << 24);
278 return posn - charIndex;
281 // Get the maximum number of bytes needed to encode a
282 // specified number of characters.
283 public override int GetMaxByteCount (int charCount)
286 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
288 return charCount * 4;
291 // Get the maximum number of characters needed to decode a
292 // specified number of bytes.
293 public override int GetMaxCharCount (int byteCount)
296 throw new ArgumentOutOfRangeException
297 ("byteCount", _("ArgRange_NonNegative"));
299 return byteCount / 4;
302 // Get a UTF32-specific decoder that is attached to this instance.
303 public override Decoder GetDecoder ()
305 return new UTF32Decoder (bigEndian);
308 // Get the UTF32 preamble.
309 public override byte[] GetPreamble ()
312 byte[] preamble = new byte[2];
314 preamble[0] = (byte)0xFE;
315 preamble[1] = (byte)0xFF;
317 preamble[0] = (byte)0xFF;
318 preamble[1] = (byte)0xFE;
326 // Determine if this object is equal to another.
327 public override bool Equals (Object value)
329 UTF32Encoding enc = (value as UTF32Encoding);
331 return (codePage == enc.codePage &&
332 bigEndian == enc.bigEndian &&
333 byteOrderMark == enc.byteOrderMark &&
334 base.Equals (value));
340 // Get the hash code for this object.
341 public override int GetHashCode ()
343 return base.GetHashCode ();
346 // UTF32 decoder implementation.
347 private sealed class UTF32Decoder : Decoder
349 private bool bigEndian;
350 private int leftOverByte;
351 private int leftOverLength;
354 public UTF32Decoder (bool bigEndian)
356 this.bigEndian = bigEndian;
360 // Override inherited methods.
361 public override int GetCharCount (byte[] bytes, int index, int count)
364 throw new ArgumentNullException ("bytes");
366 if (index < 0 || index > bytes.Length) {
367 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
369 if (count < 0 || count > (bytes.Length - index)) {
370 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
372 if (leftOverByte != -1) {
373 return (count + 1) / 4;
379 public override int GetChars (byte[] bytes, int byteIndex,
380 int byteCount, char[] chars,
384 throw new ArgumentNullException ("bytes");
387 throw new ArgumentNullException ("chars");
389 if (byteIndex < 0 || byteIndex > bytes.Length) {
390 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
392 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
393 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
395 if (charIndex < 0 || charIndex > chars.Length) {
396 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
399 // Convert the characters.
400 int posn = charIndex;
401 int leftOver = leftOverByte;
402 int length = chars.Length;
405 int remain = 4 - leftOverLength;
406 if (leftOverLength > 0 && byteCount > remain) {
408 for (int i = 0; i < remain; i++)
409 leftOver += bytes [byteIndex++] << (4 - byteCount--);
411 for (int i = 0; i < remain; i++)
412 leftOver += bytes [byteIndex++] << byteCount--;
414 if (leftOver > char.MaxValue && posn + 1 < length
416 throw new ArgumentException (_("Arg_InsufficientSpace"));
417 if (leftOver > char.MaxValue) {
418 chars [posn++] = (char) ((leftOver - 10000) / 0x400 + 0xD800);
419 chars [posn++] = (char) ((leftOver - 10000) % 0x400 + 0xDC00);
422 chars [posn++] = (char) leftOver;
428 while (byteCount > 3) {
431 bytes[byteIndex++] << 24 |
432 bytes[byteIndex++] << 16 |
433 bytes[byteIndex++] << 8 |
438 bytes[byteIndex++] << 8 |
439 bytes[byteIndex++] << 16 |
440 bytes[byteIndex++] << 24);
447 throw new ArgumentException (_("Arg_InsufficientSpace"));
451 leftOverLength = byteCount;
454 for (int i = 0; i < byteCount; i++)
455 leftOver += bytes [byteIndex++] << (4 - byteCount--);
457 for (int i = 0; i < byteCount; i++)
458 leftOver += bytes [byteIndex++] << byteCount--;
460 leftOverByte = leftOver;
463 // Finished - return the converted length.
464 return posn - charIndex;
467 } // class UTF32Decoder
470 [CLSCompliantAttribute(false)]
471 public unsafe override int GetByteCount (char *chars, int count)
476 public override byte [] GetBytes (String s)
478 return base.GetBytes (s);
483 }; // class UTF32Encoding
485 }; // namespace System.Text