2 * UTF8Encoding.cs - Implementation of the "System.Text.UTF8Encoding" class.
4 * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
5 * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
30 using System.Runtime.InteropServices;
33 [MonoLimitation ("Serialization format not compatible with .NET")]
35 public class UTF8Encoding : Encoding
37 // Magic number used by Windows for UTF-8.
38 internal const int UTF8_CODE_PAGE = 65001;
41 private bool emitIdentifier;
44 public UTF8Encoding () : this (false, false) {}
45 public UTF8Encoding (bool encoderShouldEmitUTF8Identifier)
46 : this (encoderShouldEmitUTF8Identifier, false) {}
48 public UTF8Encoding (bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes)
49 : base (UTF8_CODE_PAGE)
51 emitIdentifier = encoderShouldEmitUTF8Identifier;
52 if (throwOnInvalidBytes)
53 SetFallbackInternal (EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
55 SetFallbackInternal (EncoderFallback.StandardSafeFallback, DecoderFallback.StandardSafeFallback);
57 web_name = body_name = header_name = "utf-8";
58 encoding_name = "Unicode (UTF-8)";
59 is_browser_save = true;
60 is_browser_display = true;
61 is_mail_news_display = true;
62 is_mail_news_save = true;
63 windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
66 #region GetByteCount()
68 // Internal version of "GetByteCount" which can handle a rolling
69 // state between multiple calls to this method.
70 private static int InternalGetByteCount (char[] chars, int index, int count, EncoderFallback fallback, ref char leftOver, bool flush)
72 // Validate the parameters.
74 throw new ArgumentNullException ("chars");
76 if (index < 0 || index > chars.Length) {
77 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
79 if (count < 0 || count > (chars.Length - index)) {
80 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
83 if (index == chars.Length) {
84 if (flush && leftOver != '\0') {
85 // Flush the left-over surrogate pair start.
93 fixed (char* cptr = chars) {
94 return InternalGetByteCount (cptr + index, count, fallback, ref leftOver, flush);
99 private unsafe static int InternalGetByteCount (char* chars, int count, EncoderFallback fallback, ref char leftOver, bool flush)
102 char* end = chars + count;
104 EncoderFallbackBuffer buffer = null;
105 while (chars < end) {
107 for (; chars < end; chars++) {
108 if (*chars < '\x80') {
110 } else if (*chars < '\x800') {
112 } else if (*chars < '\uD800' || *chars > '\uDFFF') {
114 } else if (*chars <= '\uDBFF') {
115 // This is a surrogate start char, exit the inner loop only
116 // if we don't find the complete surrogate pair.
117 if (chars + 1 < end && chars [1] >= '\uDC00' && chars [1] <= '\uDFFF') {
126 // We have a surrogate tail without
127 // leading surrogate.
128 char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
129 fixed (char *fb_chars = fallback_chars) {
131 length += InternalGetByteCount (fb_chars, fallback_chars.Length, fallback, ref dummy, true);
138 if (*chars >= '\uDC00' && *chars <= '\uDFFF') {
139 // We have a correct surrogate pair.
143 // We have a surrogate start followed by a
144 // regular character. Technically, this is
145 // invalid, but we have to do something.
146 // We write out the surrogate start and then
147 // re-visit the current character again.
148 char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
149 fixed (char *fb_chars = fallback_chars) {
151 length += InternalGetByteCount (fb_chars, fallback_chars.Length, fallback, ref dummy, true);
158 // Flush the left-over surrogate pair start.
159 if (leftOver != '\0') {
167 unsafe static char [] GetFallbackChars (char *chars, char *start, EncoderFallback fallback, ref EncoderFallbackBuffer buffer)
170 buffer = fallback.CreateFallbackBuffer ();
172 buffer.Fallback (*chars, (int) (chars - start));
174 char [] fallback_chars = new char [buffer.Remaining];
175 for (int i = 0; i < fallback_chars.Length; i++)
176 fallback_chars [i] = buffer.GetNextChar ();
180 return fallback_chars;
183 // Get the number of bytes needed to encode a character buffer.
184 public override int GetByteCount (char[] chars, int index, int count)
187 return InternalGetByteCount (chars, index, count, EncoderFallback, ref dummy, true);
191 [CLSCompliant (false)]
193 public unsafe override int GetByteCount (char* chars, int count)
196 throw new ArgumentNullException ("chars");
200 return InternalGetByteCount (chars, count, EncoderFallback, ref dummy, true);
207 // Internal version of "GetBytes" which can handle a rolling
208 // state between multiple calls to this method.
209 private static int InternalGetBytes (char[] chars, int charIndex,
210 int charCount, byte[] bytes,
212 EncoderFallback fallback, ref EncoderFallbackBuffer buffer,
213 ref char leftOver, bool flush)
215 // Validate the parameters.
217 throw new ArgumentNullException ("chars");
220 throw new ArgumentNullException ("bytes");
222 if (charIndex < 0 || charIndex > chars.Length) {
223 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
225 if (charCount < 0 || charCount > (chars.Length - charIndex)) {
226 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
228 if (byteIndex < 0 || byteIndex > bytes.Length) {
229 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
232 if (charIndex == chars.Length) {
233 if (flush && leftOver != '\0') {
234 // FIXME: use EncoderFallback.
236 // By default it is empty, so I do nothing for now.
243 fixed (char* cptr = chars) {
244 if (bytes.Length == byteIndex)
245 return InternalGetBytes (
246 cptr + charIndex, charCount,
247 null, 0, fallback, ref buffer, ref leftOver, flush);
248 fixed (byte *bptr = bytes) {
249 return InternalGetBytes (
250 cptr + charIndex, charCount,
251 bptr + byteIndex, bytes.Length - byteIndex,
252 fallback, ref buffer,
253 ref leftOver, flush);
259 private unsafe static int InternalGetBytes (char* chars, int count, byte* bytes, int bcount, EncoderFallback fallback, ref EncoderFallbackBuffer buffer, ref char leftOver, bool flush)
261 char* end = chars + count;
263 byte* start_bytes = bytes;
264 byte* end_bytes = bytes + bcount;
265 while (chars < end) {
267 for (; chars < end; chars++) {
270 if (bytes >= end_bytes)
273 } else if (ch < '\x800') {
274 if (bytes + 1 >= end_bytes)
276 bytes [0] = (byte) (0xC0 | (ch >> 6));
277 bytes [1] = (byte) (0x80 | (ch & 0x3F));
279 } else if (ch < '\uD800' || ch > '\uDFFF') {
280 if (bytes + 2 >= end_bytes)
282 bytes [0] = (byte) (0xE0 | (ch >> 12));
283 bytes [1] = (byte) (0x80 | ((ch >> 6) & 0x3F));
284 bytes [2] = (byte) (0x80 | (ch & 0x3F));
286 } else if (ch <= '\uDBFF') {
287 // This is a surrogate char, exit the inner loop.
292 // We have a surrogate tail without
293 // leading surrogate.
294 char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
296 if (bytes + InternalGetByteCount (fallback_chars, 0, fallback_chars.Length, fallback, ref dummy, true) > end_bytes)
298 fixed (char *fb_chars = fallback_chars) {
299 bytes += InternalGetBytes (fb_chars, fallback_chars.Length, bytes, bcount - (int) (bytes - start_bytes), fallback, ref buffer, ref dummy, true);
306 if (*chars >= '\uDC00' && *chars <= '\uDFFF') {
307 // We have a correct surrogate pair.
308 int ch = 0x10000 + (int) *chars - 0xDC00 + (((int) leftOver - 0xD800) << 10);
309 if (bytes + 3 >= end_bytes)
311 bytes [0] = (byte) (0xF0 | (ch >> 18));
312 bytes [1] = (byte) (0x80 | ((ch >> 12) & 0x3F));
313 bytes [2] = (byte) (0x80 | ((ch >> 6) & 0x3F));
314 bytes [3] = (byte) (0x80 | (ch & 0x3F));
318 // We have a surrogate start followed by a
319 // regular character. Technically, this is
320 // invalid, but we have to do something.
321 // We write out the surrogate start and then
322 // re-visit the current character again.
323 char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
325 if (bytes + InternalGetByteCount (fallback_chars, 0, fallback_chars.Length, fallback, ref dummy, true) > end_bytes)
327 fixed (char *fb_chars = fallback_chars) {
328 InternalGetBytes (fb_chars, fallback_chars.Length, bytes, bcount - (int) (bytes - start_bytes), fallback, ref buffer, ref dummy, true);
337 // Flush the left-over surrogate pair start.
338 if (leftOver != '\0') {
340 if (bytes + 2 < end_bytes) {
341 bytes [0] = (byte) (0xE0 | (ch >> 12));
342 bytes [1] = (byte) (0x80 | ((ch >> 6) & 0x3F));
343 bytes [2] = (byte) (0x80 | (ch & 0x3F));
351 return (int)(bytes - (end_bytes - bcount));
353 throw new ArgumentException ("Insufficient Space", "bytes");
356 // Get the bytes that result from encoding a character buffer.
357 public override int GetBytes (char[] chars, int charIndex, int charCount,
358 byte[] bytes, int byteIndex)
360 char leftOver = '\0';
361 EncoderFallbackBuffer buffer = null;
362 return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, EncoderFallback, ref buffer, ref leftOver, true);
365 // Convenience wrappers for "GetBytes".
366 public override int GetBytes (String s, int charIndex, int charCount,
367 byte[] bytes, int byteIndex)
369 // Validate the parameters.
371 throw new ArgumentNullException ("s");
374 throw new ArgumentNullException ("bytes");
376 if (charIndex < 0 || charIndex > s.Length) {
377 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
379 if (charCount < 0 || charCount > (s.Length - charIndex)) {
380 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
382 if (byteIndex < 0 || byteIndex > bytes.Length) {
383 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
386 if (charIndex == s.Length)
390 fixed (char* cptr = s) {
392 EncoderFallbackBuffer buffer = null;
393 if (bytes.Length == byteIndex)
394 return InternalGetBytes (
395 cptr + charIndex, charCount,
396 null, 0, EncoderFallback, ref buffer, ref dummy, true);
397 fixed (byte *bptr = bytes) {
398 return InternalGetBytes (
399 cptr + charIndex, charCount,
400 bptr + byteIndex, bytes.Length - byteIndex,
401 EncoderFallback, ref buffer,
408 [CLSCompliant (false)]
410 public unsafe override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount)
413 throw new ArgumentNullException ("chars");
415 throw new IndexOutOfRangeException ("charCount");
417 throw new ArgumentNullException ("bytes");
419 throw new IndexOutOfRangeException ("charCount");
425 EncoderFallbackBuffer buffer = null;
427 return InternalGetBytes (chars, charCount, null, 0, EncoderFallback, ref buffer, ref dummy, true);
429 return InternalGetBytes (chars, charCount, bytes, byteCount, EncoderFallback, ref buffer, ref dummy, true);
434 // Internal version of "GetCharCount" which can handle a rolling
435 // state between multiple calls to this method.
436 private unsafe static int InternalGetCharCount (
437 byte[] bytes, int index, int count, uint leftOverBits,
438 uint leftOverCount, object provider,
439 ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
441 // Validate the parameters.
443 throw new ArgumentNullException ("bytes");
445 if (index < 0 || index > bytes.Length) {
446 throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
448 if (count < 0 || count > (bytes.Length - index)) {
449 throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
454 fixed (byte *bptr = bytes)
455 return InternalGetCharCount (bptr + index, count,
456 leftOverBits, leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
459 private unsafe static int InternalGetCharCount (
460 byte* bytes, int count, uint leftOverBits,
461 uint leftOverCount, object provider,
462 ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
468 if (leftOverCount == 0) {
469 int end = index + count;
470 for (; index < end; index++, count--) {
471 if (bytes [index] < 0x80)
478 // Determine the number of characters that we have.
480 uint leftBits = leftOverBits;
481 uint leftSoFar = (leftOverCount & (uint)0x0F);
482 uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
484 ch = (uint)(bytes[index++]);
487 // Process a UTF-8 start character.
488 if (ch < (uint)0x0080) {
489 // Single-byte UTF-8 character.
491 } else if ((ch & (uint)0xE0) == (uint)0xC0) {
492 // Double-byte UTF-8 character.
493 leftBits = (ch & (uint)0x1F);
496 } else if ((ch & (uint)0xF0) == (uint)0xE0) {
497 // Three-byte UTF-8 character.
498 leftBits = (ch & (uint)0x0F);
501 } else if ((ch & (uint)0xF8) == (uint)0xF0) {
502 // Four-byte UTF-8 character.
503 leftBits = (ch & (uint)0x07);
506 } else if ((ch & (uint)0xFC) == (uint)0xF8) {
507 // Five-byte UTF-8 character.
508 leftBits = (ch & (uint)0x03);
511 } else if ((ch & (uint)0xFE) == (uint)0xFC) {
512 // Six-byte UTF-8 character.
513 leftBits = (ch & (uint)0x03);
517 // Invalid UTF-8 start character.
518 length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - 1, 1);
521 // Process an extra byte in a multi-byte sequence.
522 if ((ch & (uint)0xC0) == (uint)0x80) {
523 leftBits = ((leftBits << 6) | (ch & (uint)0x3F));
524 if (++leftSoFar >= leftSize) {
525 // We have a complete character now.
526 if (leftBits < (uint)0x10000) {
527 // is it an overlong ?
528 bool overlong = false;
531 overlong = (leftBits <= 0x7F);
534 overlong = (leftBits <= 0x07FF);
537 overlong = (leftBits <= 0xFFFF);
540 overlong = (leftBits <= 0x1FFFFF);
543 overlong = (leftBits <= 0x03FFFFFF);
547 length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
549 else if ((leftBits & 0xF800) == 0xD800) {
550 // UTF-8 doesn't use surrogate characters
551 length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
555 } else if (leftBits < (uint)0x110000) {
558 length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
563 // Invalid UTF-8 sequence: clear and restart.
564 length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
571 if (flush && leftSize != 0) {
572 // We had left-over bytes that didn't make up
573 // a complete UTF-8 character sequence.
574 length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
577 // Return the final length to the caller.
581 // for GetCharCount()
582 static unsafe int Fallback (object provider, ref DecoderFallbackBuffer buffer, ref byte [] bufferArg, byte* bytes, long index, uint size)
584 if (buffer == null) {
585 DecoderFallback fb = provider as DecoderFallback;
587 buffer = fb.CreateFallbackBuffer ();
589 buffer = ((Decoder) provider).FallbackBuffer;
591 if (bufferArg == null)
592 bufferArg = new byte [1];
594 for (int i = 0; i < size; i++) {
595 bufferArg [0] = bytes [(int) index + i];
596 buffer.Fallback (bufferArg, 0);
597 ret += buffer.Remaining;
604 static unsafe void Fallback (object provider, ref DecoderFallbackBuffer buffer, ref byte [] bufferArg, byte* bytes, long byteIndex, uint size,
605 char* chars, ref int charIndex)
607 if (buffer == null) {
608 DecoderFallback fb = provider as DecoderFallback;
610 buffer = fb.CreateFallbackBuffer ();
612 buffer = ((Decoder) provider).FallbackBuffer;
614 if (bufferArg == null)
615 bufferArg = new byte [1];
616 for (int i = 0; i < size; i++) {
617 bufferArg [0] = bytes [byteIndex + i];
618 buffer.Fallback (bufferArg, 0);
619 while (buffer.Remaining > 0)
620 chars [charIndex++] = buffer.GetNextChar ();
625 // Get the number of characters needed to decode a byte buffer.
626 public override int GetCharCount (byte[] bytes, int index, int count)
628 DecoderFallbackBuffer buf = null;
629 byte [] bufferArg = null;
630 return InternalGetCharCount (bytes, index, count, 0, 0, DecoderFallback, ref buf, ref bufferArg, true);
633 [CLSCompliant (false)]
635 public unsafe override int GetCharCount (byte* bytes, int count)
637 DecoderFallbackBuffer buf = null;
638 byte [] bufferArg = null;
639 return InternalGetCharCount (bytes, count, 0, 0, DecoderFallback, ref buf, ref bufferArg, true);
642 // Get the characters that result from decoding a byte buffer.
643 private unsafe static int InternalGetChars (
644 byte[] bytes, int byteIndex, int byteCount, char[] chars,
645 int charIndex, ref uint leftOverBits, ref uint leftOverCount,
647 ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
649 // Validate the parameters.
651 throw new ArgumentNullException ("bytes");
654 throw new ArgumentNullException ("chars");
656 if (byteIndex < 0 || byteIndex > bytes.Length) {
657 throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
659 if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
660 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
662 if (charIndex < 0 || charIndex > chars.Length) {
663 throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
666 if (charIndex == chars.Length)
669 fixed (char* cptr = chars) {
670 if (byteCount == 0 || byteIndex == bytes.Length)
671 return InternalGetChars (null, 0, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
673 fixed (byte* bptr = bytes)
674 return InternalGetChars (bptr + byteIndex, byteCount, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
678 private unsafe static int InternalGetChars (
679 byte* bytes, int byteCount, char* chars, int charCount,
680 ref uint leftOverBits, ref uint leftOverCount,
682 ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
684 int charIndex = 0, byteIndex = 0;
685 int length = charCount;
686 int posn = charIndex;
688 if (leftOverCount == 0) {
689 int end = byteIndex + byteCount;
690 for (; byteIndex < end; posn++, byteIndex++, byteCount--) {
691 if (bytes [byteIndex] < 0x80)
692 chars [posn] = (char) bytes [byteIndex];
698 // Convert the bytes into the output buffer.
700 uint leftBits = leftOverBits;
701 uint leftSoFar = (leftOverCount & (uint)0x0F);
702 uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
704 int byteEnd = byteIndex + byteCount;
705 for(; byteIndex < byteEnd; byteIndex++) {
706 // Fetch the next character from the byte buffer.
707 ch = (uint)(bytes[byteIndex]);
709 // Process a UTF-8 start character.
710 if (ch < (uint)0x0080) {
711 // Single-byte UTF-8 character.
712 if (posn >= length) {
713 throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
715 chars[posn++] = (char)ch;
716 } else if ((ch & (uint)0xE0) == (uint)0xC0) {
717 // Double-byte UTF-8 character.
718 leftBits = (ch & (uint)0x1F);
721 } else if ((ch & (uint)0xF0) == (uint)0xE0) {
722 // Three-byte UTF-8 character.
723 leftBits = (ch & (uint)0x0F);
726 } else if ((ch & (uint)0xF8) == (uint)0xF0) {
727 // Four-byte UTF-8 character.
728 leftBits = (ch & (uint)0x07);
731 } else if ((ch & (uint)0xFC) == (uint)0xF8) {
732 // Five-byte UTF-8 character.
733 leftBits = (ch & (uint)0x03);
736 } else if ((ch & (uint)0xFE) == (uint)0xFC) {
737 // Six-byte UTF-8 character.
738 leftBits = (ch & (uint)0x03);
742 // Invalid UTF-8 start character.
743 Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex, 1, chars, ref posn);
746 // Process an extra byte in a multi-byte sequence.
747 if ((ch & (uint)0xC0) == (uint)0x80) {
748 leftBits = ((leftBits << 6) | (ch & (uint)0x3F));
749 if (++leftSoFar >= leftSize) {
750 // We have a complete character now.
751 if (leftBits < (uint)0x10000) {
752 // is it an overlong ?
753 bool overlong = false;
756 overlong = (leftBits <= 0x7F);
759 overlong = (leftBits <= 0x07FF);
762 overlong = (leftBits <= 0xFFFF);
765 overlong = (leftBits <= 0x1FFFFF);
768 overlong = (leftBits <= 0x03FFFFFF);
772 Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
774 else if ((leftBits & 0xF800) == 0xD800) {
775 // UTF-8 doesn't use surrogate characters
776 Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
779 if (posn >= length) {
780 throw new ArgumentException
781 (_("Arg_InsufficientSpace"), "chars");
783 chars[posn++] = (char)leftBits;
785 } else if (leftBits < (uint)0x110000) {
786 if ((posn + 2) > length) {
787 throw new ArgumentException
788 (_("Arg_InsufficientSpace"), "chars");
790 leftBits -= (uint)0x10000;
791 chars[posn++] = (char)((leftBits >> 10) +
794 (char)((leftBits & (uint)0x3FF) + (uint)0xDC00);
796 Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
801 // Invalid UTF-8 sequence: clear and restart.
802 Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
808 if (flush && leftSize != 0) {
809 // We had left-over bytes that didn't make up
810 // a complete UTF-8 character sequence.
811 Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
813 leftOverBits = leftBits;
814 leftOverCount = (leftSoFar | (leftSize << 4));
816 // Return the final length to the caller.
817 return posn - charIndex;
820 // Get the characters that result from decoding a byte buffer.
821 public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
822 char[] chars, int charIndex)
824 uint leftOverBits = 0;
825 uint leftOverCount = 0;
826 DecoderFallbackBuffer buf = null;
827 byte [] bufferArg = null;
828 return InternalGetChars (bytes, byteIndex, byteCount, chars,
829 charIndex, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, ref bufferArg, true);
832 [CLSCompliant (false)]
834 public unsafe override int GetChars (byte* bytes, int byteCount, char* chars, int charCount)
836 DecoderFallbackBuffer buf = null;
837 byte [] bufferArg = null;
838 uint leftOverBits = 0;
839 uint leftOverCount = 0;
840 return InternalGetChars (bytes, byteCount, chars,
841 charCount, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, ref bufferArg, true);
844 // Get the maximum number of bytes needed to encode a
845 // specified number of characters.
846 public override int GetMaxByteCount (int charCount)
849 throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
851 return charCount * 4;
854 // Get the maximum number of characters needed to decode a
855 // specified number of bytes.
856 public override int GetMaxCharCount (int byteCount)
859 throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
864 // Get a UTF8-specific decoder that is attached to this instance.
865 public override Decoder GetDecoder ()
867 return new UTF8Decoder (DecoderFallback);
870 // Get a UTF8-specific encoder that is attached to this instance.
871 public override Encoder GetEncoder ()
873 return new UTF8Encoder (EncoderFallback, emitIdentifier);
876 // Get the UTF8 preamble.
877 public override byte[] GetPreamble ()
880 return new byte [] { 0xEF, 0xBB, 0xBF };
882 return EmptyArray<byte>.Value;
885 // Determine if this object is equal to another.
886 public override bool Equals (Object value)
888 UTF8Encoding enc = (value as UTF8Encoding);
890 return (codePage == enc.codePage &&
891 emitIdentifier == enc.emitIdentifier &&
892 DecoderFallback.Equals (enc.DecoderFallback) &&
893 EncoderFallback.Equals (enc.EncoderFallback));
899 // Get the hash code for this object.
900 public override int GetHashCode ()
902 return base.GetHashCode ();
905 public override int GetByteCount (string chars)
907 // hmm, does this override make any sense?
908 return base.GetByteCount (chars);
912 public override string GetString (byte [] bytes, int index, int count)
914 // hmm, does this override make any sense?
915 return base.GetString (bytes, index, count);
918 // UTF-8 decoder implementation.
920 private class UTF8Decoder : Decoder
922 private uint leftOverBits;
923 private uint leftOverCount;
926 public UTF8Decoder (DecoderFallback fallback)
933 // Override inherited methods.
934 public override int GetCharCount (byte[] bytes, int index, int count)
936 DecoderFallbackBuffer buf = null;
937 byte [] bufferArg = null;
938 return InternalGetCharCount (bytes, index, count,
939 leftOverBits, leftOverCount, this, ref buf, ref bufferArg, false);
941 public override int GetChars (byte[] bytes, int byteIndex,
942 int byteCount, char[] chars, int charIndex)
944 DecoderFallbackBuffer buf = null;
945 byte [] bufferArg = null;
946 return InternalGetChars (bytes, byteIndex, byteCount,
947 chars, charIndex, ref leftOverBits, ref leftOverCount, this, ref buf, ref bufferArg, false);
950 } // class UTF8Decoder
952 // UTF-8 encoder implementation.
954 private class UTF8Encoder : Encoder
956 // private bool emitIdentifier;
957 private char leftOverForCount;
958 private char leftOverForConv;
961 public UTF8Encoder (EncoderFallback fallback, bool emitIdentifier)
964 // this.emitIdentifier = emitIdentifier;
965 leftOverForCount = '\0';
966 leftOverForConv = '\0';
969 // Override inherited methods.
970 public override int GetByteCount (char[] chars, int index,
971 int count, bool flush)
973 return InternalGetByteCount (chars, index, count, Fallback, ref leftOverForCount, flush);
975 public override int GetBytes (char[] chars, int charIndex,
976 int charCount, byte[] bytes, int byteIndex, bool flush)
979 EncoderFallbackBuffer buffer = null;
980 result = InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, Fallback, ref buffer, ref leftOverForConv, flush);
981 // emitIdentifier = false;
985 public unsafe override int GetByteCount (char* chars, int count, bool flush)
987 return InternalGetByteCount (chars, count, Fallback, ref leftOverForCount, flush);
990 public unsafe override int GetBytes (char* chars, int charCount,
991 byte* bytes, int byteCount, bool flush)
994 EncoderFallbackBuffer buffer = null;
995 result = InternalGetBytes (chars, charCount, bytes, byteCount, Fallback, ref buffer, ref leftOverForConv, flush);
996 // emitIdentifier = false;
999 } // class UTF8Encoder
1001 }; // class UTF8Encoding
1003 }; // namespace System.Text