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 int basis = base.GetHashCode ();
351 // UTF32 decoder implementation.
352 private sealed class UTF32Decoder : Decoder
354 private bool bigEndian;
355 private int leftOverByte;
356 private int leftOverLength;
359 public UTF32Decoder (bool bigEndian)
361 this.bigEndian = bigEndian;
365 // Override inherited methods.
366 public override int GetCharCount (byte[] bytes, int index, int count)
369 throw new ArgumentNullException ("bytes");
371 if (index < 0 || index > bytes.Length) {
372 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
374 if (count < 0 || count > (bytes.Length - index)) {
375 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
377 if (leftOverByte != -1) {
378 return (count + 1) / 4;
384 public override int GetChars (byte[] bytes, int byteIndex,
385 int byteCount, char[] chars,
389 throw new ArgumentNullException ("bytes");
392 throw new ArgumentNullException ("chars");
394 if (byteIndex < 0 || byteIndex > bytes.Length) {
395 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
397 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
398 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
400 if (charIndex < 0 || charIndex > chars.Length) {
401 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
404 // Convert the characters.
405 int posn = charIndex;
406 int leftOver = leftOverByte;
407 int length = chars.Length;
410 int remain = 4 - leftOverLength;
411 if (leftOverLength > 0 && byteCount > remain) {
413 for (int i = 0; i < remain; i++)
414 leftOver += bytes [byteIndex++] << (4 - byteCount--);
416 for (int i = 0; i < remain; i++)
417 leftOver += bytes [byteIndex++] << byteCount--;
419 if (leftOver > char.MaxValue && posn + 1 < length
421 throw new ArgumentException (_("Arg_InsufficientSpace"));
422 if (leftOver > char.MaxValue) {
423 chars [posn++] = (char) ((leftOver - 10000) / 0x400 + 0xD800);
424 chars [posn++] = (char) ((leftOver - 10000) % 0x400 + 0xDC00);
427 chars [posn++] = (char) leftOver;
433 while (byteCount > 3) {
436 bytes[byteIndex++] << 24 |
437 bytes[byteIndex++] << 16 |
438 bytes[byteIndex++] << 8 |
443 bytes[byteIndex++] << 8 |
444 bytes[byteIndex++] << 16 |
445 bytes[byteIndex++] << 24);
452 throw new ArgumentException (_("Arg_InsufficientSpace"));
456 leftOverLength = byteCount;
459 for (int i = 0; i < byteCount; i++)
460 leftOver += bytes [byteIndex++] << (4 - byteCount--);
462 for (int i = 0; i < byteCount; i++)
463 leftOver += bytes [byteIndex++] << byteCount--;
465 leftOverByte = leftOver;
468 // Finished - return the converted length.
469 return posn - charIndex;
472 } // class UTF32Decoder
475 [CLSCompliantAttribute(false)]
476 public unsafe override int GetByteCount (char *chars, int count)
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