2 * UnicodeEncoding.cs - Implementation of the
3 * "System.Text.UnicodeEncoding" class.
5 * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
6 * Copyright (C) 2003, 2004 Novell, Inc.
7 * Copyright (C) 2006 Kornél Pál <http://www.kornelpal.hu/>
9 * Permission is hereby granted, free of charge, to any person obtaining
10 * a copy of this software and associated documentation files (the "Software"),
11 * to deal in the Software without restriction, including without limitation
12 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 * and/or sell copies of the Software, and to permit persons to whom the
14 * Software is furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included
17 * in all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
32 using System
.Runtime
.InteropServices
;
36 [MonoLimitation ("Serialization format not compatible with .NET")]
37 public class UnicodeEncoding
: Encoding
39 // Magic numbers used by Windows for Unicode.
40 internal const int UNICODE_CODE_PAGE
= 1200;
41 internal const int BIG_UNICODE_CODE_PAGE
= 1201;
44 // Size of characters in this encoding.
45 public const int CharSize
= 2;
49 private bool bigEndian
;
50 private bool byteOrderMark
;
53 public UnicodeEncoding () : this (false, true)
58 public UnicodeEncoding (bool bigEndian
, bool byteOrderMark
)
59 : this (bigEndian
, byteOrderMark
, false)
63 public UnicodeEncoding (bool bigEndian
, bool byteOrderMark
, bool throwOnInvalidBytes
)
64 : base ((bigEndian
? BIG_UNICODE_CODE_PAGE
: UNICODE_CODE_PAGE
))
66 if (throwOnInvalidBytes
)
67 SetFallbackInternal (null, new DecoderExceptionFallback ());
69 SetFallbackInternal (null, new DecoderReplacementFallback ("\uFFFD"));
71 this.bigEndian
= bigEndian
;
72 this.byteOrderMark
= byteOrderMark
;
75 body_name
= "unicodeFFFE";
76 encoding_name
= "Unicode (Big-Endian)";
77 header_name
= "unicodeFFFE";
78 is_browser_save
= false;
79 web_name
= "unicodeFFFE";
82 encoding_name
= "Unicode";
83 header_name
= "utf-16";
84 is_browser_save
= true;
88 // Windows reports the same code page number for
89 // both the little-endian and big-endian forms.
90 windows_code_page
= UNICODE_CODE_PAGE
;
93 // Get the number of bytes needed to encode a character buffer.
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"));
108 public override int GetByteCount (String s
)
111 throw new ArgumentNullException ("s");
116 [CLSCompliantAttribute (false)]
118 public unsafe override int GetByteCount (char* chars
, int count
)
121 throw new ArgumentNullException ("chars");
123 throw new ArgumentOutOfRangeException ("count");
128 // Get the bytes that result from encoding a character buffer.
129 public unsafe override int GetBytes (char [] chars
, int charIndex
, int charCount
,
130 byte [] bytes
, int byteIndex
)
133 throw new ArgumentNullException ("chars");
136 throw new ArgumentNullException ("bytes");
138 if (charIndex
< 0 || charIndex
> chars
.Length
) {
139 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
141 if (charCount
< 0 || charCount
> (chars
.Length
- charIndex
)) {
142 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
144 if (byteIndex
< 0 || byteIndex
> bytes
.Length
) {
145 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
151 int byteCount
= bytes
.Length
- byteIndex
;
152 if (bytes
.Length
== 0)
153 bytes
= new byte [1];
155 fixed (char* charPtr
= chars
)
156 fixed (byte* bytePtr
= bytes
)
157 return GetBytesInternal (charPtr
+ charIndex
, charCount
, bytePtr
+ byteIndex
, byteCount
);
160 public unsafe override int GetBytes (String s
, int charIndex
, int charCount
,
161 byte [] bytes
, int byteIndex
)
164 throw new ArgumentNullException ("s");
167 throw new ArgumentNullException ("bytes");
169 if (charIndex
< 0 || charIndex
> s
.Length
) {
170 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
172 if (charCount
< 0 || charCount
> (s
.Length
- charIndex
)) {
173 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
175 if (byteIndex
< 0 || byteIndex
> bytes
.Length
) {
176 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
183 int byteCount
= bytes
.Length
- byteIndex
;
184 if (bytes
.Length
== 0)
185 bytes
= new byte [1];
187 fixed (char* charPtr
= s
)
188 fixed (byte* bytePtr
= bytes
)
189 return GetBytesInternal (charPtr
+ charIndex
, charCount
, bytePtr
+ byteIndex
, byteCount
);
192 [CLSCompliantAttribute (false)]
194 public unsafe override int GetBytes (char* chars
, int charCount
,
195 byte* bytes
, int byteCount
)
198 throw new ArgumentNullException ("bytes");
200 throw new ArgumentNullException ("chars");
202 throw new ArgumentOutOfRangeException ("charCount");
204 throw new ArgumentOutOfRangeException ("byteCount");
206 return GetBytesInternal (chars
, charCount
, bytes
, byteCount
);
209 private unsafe int GetBytesInternal (char* chars
, int charCount
,
210 byte* bytes
, int byteCount
)
212 int count
= charCount
* 2;
214 if (byteCount
< count
)
215 throw new ArgumentException (_("Arg_InsufficientSpace"));
217 CopyChars ((byte*) chars
, bytes
, count
, bigEndian
);
221 // Get the number of characters needed to decode a byte buffer.
222 public override int GetCharCount (byte[] bytes
, int index
, int count
)
225 throw new ArgumentNullException ("bytes");
227 if (index
< 0 || index
> bytes
.Length
) {
228 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
230 if (count
< 0 || count
> (bytes
.Length
- index
)) {
231 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
236 [CLSCompliantAttribute (false)]
238 public unsafe override int GetCharCount (byte* bytes
, int count
)
241 throw new ArgumentNullException ("bytes");
243 throw new ArgumentOutOfRangeException ("count");
248 // Get the characters that result from decoding a byte buffer.
249 public unsafe override int GetChars (byte [] bytes
, int byteIndex
, int byteCount
,
250 char [] chars
, int charIndex
)
253 throw new ArgumentNullException ("bytes");
256 throw new ArgumentNullException ("chars");
258 if (byteIndex
< 0 || byteIndex
> bytes
.Length
) {
259 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
261 if (byteCount
< 0 || byteCount
> (bytes
.Length
- byteIndex
)) {
262 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
264 if (charIndex
< 0 || charIndex
> chars
.Length
) {
265 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
271 int charCount
= chars
.Length
- charIndex
;
272 if (chars
.Length
== 0)
273 chars
= new char [1];
275 fixed (byte* bytePtr
= bytes
)
276 fixed (char* charPtr
= chars
)
277 return GetCharsInternal (bytePtr
+ byteIndex
, byteCount
, charPtr
+ charIndex
, charCount
);
280 [CLSCompliantAttribute (false)]
282 public unsafe override int GetChars (byte* bytes
, int byteCount
,
283 char* chars
, int charCount
)
286 throw new ArgumentNullException ("bytes");
288 throw new ArgumentNullException ("chars");
290 throw new ArgumentOutOfRangeException ("charCount");
292 throw new ArgumentOutOfRangeException ("byteCount");
294 return GetCharsInternal (bytes
, byteCount
, chars
, charCount
);
297 // Decode a buffer of bytes into a string.
299 public unsafe override String
GetString (byte [] bytes
, int index
, int count
)
302 throw new ArgumentNullException ("bytes");
303 if (index
< 0 || index
> bytes
.Length
)
304 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
305 if (count
< 0 || count
> (bytes
.Length
- index
))
306 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
311 // GetCharCountInternal
312 int charCount
= count
/ 2;
313 string s
= string.InternalAllocateStr (charCount
);
315 fixed (byte* bytePtr
= bytes
)
316 fixed (char* charPtr
= s
)
317 GetCharsInternal (bytePtr
+ index
, count
, charPtr
, charCount
);
322 private unsafe int GetCharsInternal (byte* bytes
, int byteCount
,
323 char* chars
, int charCount
)
325 int count
= byteCount
/ 2;
327 // Validate that we have sufficient space in "chars".
328 if (charCount
< count
)
329 throw new ArgumentException (_("Arg_InsufficientSpace"));
331 CopyChars (bytes
, (byte*) chars
, byteCount
, bigEndian
);
336 public override Encoder
GetEncoder ()
338 return(base.GetEncoder ());
341 // Get the maximum number of bytes needed to encode a
342 // specified number of characters.
343 public override int GetMaxByteCount (int charCount
)
346 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
348 return charCount
* 2;
351 // Get the maximum number of characters needed to decode a
352 // specified number of bytes.
353 public override int GetMaxCharCount (int byteCount
)
356 throw new ArgumentOutOfRangeException
357 ("byteCount", _("ArgRange_NonNegative"));
359 return byteCount
/ 2;
362 // Get a Unicode-specific decoder that is attached to this instance.
363 public override Decoder
GetDecoder ()
365 return new UnicodeDecoder (bigEndian
);
368 // Get the Unicode preamble.
369 public override byte[] GetPreamble ()
372 byte[] preamble
= new byte[2];
374 preamble
[0] = (byte)0xFE;
375 preamble
[1] = (byte)0xFF;
377 preamble
[0] = (byte)0xFF;
378 preamble
[1] = (byte)0xFE;
386 // Determine if this object is equal to another.
387 public override bool Equals (Object
value)
389 UnicodeEncoding enc
= (value as UnicodeEncoding
);
391 return (codePage
== enc
.codePage
&&
392 bigEndian
== enc
.bigEndian
&&
393 byteOrderMark
== enc
.byteOrderMark
);
399 // Get the hash code for this object.
400 public override int GetHashCode ()
402 return base.GetHashCode ();
405 private unsafe static void CopyChars (byte* src
, byte* dest
, int count
, bool bigEndian
)
407 if (BitConverter
.IsLittleEndian
!= bigEndian
) {
408 string.memcpy (dest
, src
, count
& unchecked ((int) 0xFFFFFFFE));
458 dest
[10] = src
[11];
459 dest
[11] = src
[10];
460 dest
[12] = src
[13];
461 dest
[13] = src
[12];
462 dest
[14] = src
[15];
463 dest
[15] = src
[14];
467 } while ((count
& unchecked ((int) 0xFFFFFFF0)) != 0);
500 if ((count
& 4) == 0)
511 if ((count
& 2) == 0)
518 // Unicode decoder implementation.
519 private sealed class UnicodeDecoder
: Decoder
521 private bool bigEndian
;
522 private int leftOverByte
;
525 public UnicodeDecoder (bool bigEndian
)
527 this.bigEndian
= bigEndian
;
531 // Override inherited methods.
532 public override int GetCharCount (byte[] bytes
, int index
, int count
)
535 throw new ArgumentNullException ("bytes");
537 if (index
< 0 || index
> bytes
.Length
) {
538 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
540 if (count
< 0 || count
> (bytes
.Length
- index
)) {
541 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
543 if (leftOverByte
!= -1) {
544 return (count
+ 1) / 2;
550 public unsafe override int GetChars (byte [] bytes
, int byteIndex
,
551 int byteCount
, char [] chars
,
555 throw new ArgumentNullException ("bytes");
558 throw new ArgumentNullException ("chars");
560 if (byteIndex
< 0 || byteIndex
> bytes
.Length
) {
561 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
563 if (byteCount
< 0 || byteCount
> (bytes
.Length
- byteIndex
)) {
564 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
566 if (charIndex
< 0 || charIndex
> chars
.Length
) {
567 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
573 int leftOver
= leftOverByte
;
577 count
= (byteCount
+ 1) / 2;
579 count
= byteCount
/ 2;
581 if (chars
.Length
- charIndex
< count
)
582 throw new ArgumentException (_("Arg_InsufficientSpace"));
584 if (leftOver
!= -1) {
586 chars
[charIndex
] = unchecked ((char) ((leftOver
<< 8) | (int) bytes
[byteIndex
]));
588 chars
[charIndex
] = unchecked ((char) (((int) bytes
[byteIndex
] << 8) | leftOver
));
594 if ((byteCount
& unchecked ((int) 0xFFFFFFFE)) != 0)
595 fixed (byte* bytePtr
= bytes
)
596 fixed (char* charPtr
= chars
)
597 CopyChars (bytePtr
+ byteIndex
, (byte*) (charPtr
+ charIndex
), byteCount
, bigEndian
);
599 if ((byteCount
& 1) == 0)
602 leftOverByte
= bytes
[byteCount
+ byteIndex
- 1];
607 } // class UnicodeDecoder
609 }; // class UnicodeEncoding
611 }; // namespace System.Text