Fix the 1.1 build.
[mono.git] / mcs / class / corlib / System.Text / UTF8Encoding.cs
index 58b1f8c0712aeb0c139b4eb8915117143e36262b..c1c7973196add26f387fa2b7d88e8a268b209c37 100644 (file)
@@ -27,11 +27,13 @@ namespace System.Text
 {
 
 using System;
+using System.Runtime.InteropServices;
 
 [Serializable]
-[MonoTODO ("Fix serialization compatibility with MS.NET")]
+[MonoTODO ("Serialization format not compatible with .NET")]
 #if NET_2_0
 [MonoTODO ("EncoderFallback is not handled")]
+[ComVisible (true)]
 #endif
 public class UTF8Encoding : Encoding
 {
@@ -57,7 +59,7 @@ public class UTF8Encoding : Encoding
                if (throwOnInvalidBytes)
                        SetFallbackInternal (null, new DecoderExceptionFallback ());
                else
-                       SetFallbackInternal (null, new DecoderReplacementFallback (String.Empty));
+                       SetFallbackInternal (null, new DecoderReplacementFallback ("\uFFFD"));
 #else
                throwOnInvalid = throwOnInvalidBytes;
 #endif
@@ -67,6 +69,7 @@ public class UTF8Encoding : Encoding
                is_browser_save = true;
                is_browser_display = true;
                is_mail_news_display = true;
+               is_mail_news_save = true;
                windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
        }
 
@@ -103,73 +106,62 @@ public class UTF8Encoding : Encoding
                }
        }
 
-
        private unsafe static int InternalGetByteCount (char* chars, int count, ref char leftOver, bool flush)
        {
-               int index = 0;
-
-               // Determine the lengths of all characters.
-               char ch;
                int length = 0;
-               char pair = leftOver;
-               while (count > 0) {
-                       ch = chars[index];
-                       if (pair == 0) {
-                               if (ch < '\u0080') {
-                                       // fast path optimization
-                                       int end = index + count;
-                                       for (; index < end; index++, count--) {
-                                               if (chars [index] < '\x80')
-                                                       ++length;
-                                               else
-                                                       break;
+               char* end = chars + count;
+               while (chars < end) {
+                       if (leftOver == 0) {
+                               for (; chars < end; chars++) {
+                                       if (*chars < '\x80') {
+                                               ++length;
+                                       } else if (*chars < '\x800') {
+                                               length += 2;
+                                       } else if (*chars < '\uD800' || *chars > '\uDFFF') {
+                                               length += 3;
+                                       } else if (*chars <= '\uDBFF') {
+                                               // This is a surrogate start char, exit the inner loop only
+                                               // if we don't find the complete surrogate pair.
+                                               if (chars + 1 < end && chars [1] >= '\uDC00' && chars [1] <= '\uDFFF') {
+                                                       length += 4;
+                                                       chars++;
+                                                       continue;
+                                               }
+                                               leftOver = *chars;
+                                               chars++;
+                                               break;
+                                       } else {
+                                               // We have a surrogate tail without 
+                                               // leading surrogate. In NET_2_0 it
+                                               // uses fallback. In NET_1_1 we output
+                                               // wrong surrogate.
+                                               length += 3;
+                                               leftOver = '\0';
                                        }
-                                       continue;
-                                       //length++;
-                               } else if (ch < '\u0800') {
-                                       length += 2;
-                               } else if (ch >= '\uD800' && ch <= '\uDBFF') {
-                                       // This is the start of a surrogate pair.
-                                       pair = ch;
-                               } else {
-                                       length += 3;
                                }
-                       } else if (ch >= '\uDC00' && ch <= '\uDFFF') {
-                               if (pair != 0) {
-                                       // We have a surrogate pair.
+                       } else {
+                               if (*chars >= '\uDC00' && *chars <= '\uDFFF') {
+                                       // We have a correct surrogate pair.
                                        length += 4;
-                                       pair = '\0';
+                                       chars++;
                                } else {
-                                       // We have a surrogate tail without 
-                                       // leading surrogate. In NET_2_0 it
-                                       // uses fallback. In NET_1_1 we output
-                                       // wrong surrogate.
+                                       // We have a surrogate start followed by a
+                                       // regular character.  Technically, this is
+                                       // invalid, but we have to do something.
+                                       // We write out the surrogate start and then
+                                       // re-visit the current character again.
                                        length += 3;
-                                       pair = '\0';
                                }
-                       } else {
-                               // We have a surrogate start followed by a
-                               // regular character.  Technically, this is
-                               // invalid, but we have to do something.
-                               // We write out the surrogate start and then
-                               // re-visit the current character again.
-                               length += 3;
-                               pair = '\0';
-                               continue;
+                               leftOver = '\0';
                        }
-                       ++index;
-                       --count;
                }
                if (flush) {
-                       if (pair != '\0')
-                               // Flush the left-over surrogate pair start.
+                       // Flush the left-over surrogate pair start.
+                       if (leftOver != '\0') {
                                length += 3;
-                       leftOver = '\0';
+                               leftOver = '\0';
+                       }
                }
-               else
-                       leftOver = pair;
-
-               // Return the final length to the caller.
                return length;
        }
 
@@ -180,21 +172,37 @@ public class UTF8Encoding : Encoding
                return InternalGetByteCount (chars, index, count, ref dummy, true);
        }
 
+#if !NET_2_0
        // Convenience wrappers for "GetByteCount".
-       public override int GetByteCount (String s)
+       public override int GetByteCount (String chars)
        {
                // Validate the parameters.
-               if (s == null) {
-                       throw new ArgumentNullException ("s");
+               if (chars == null) {
+                       throw new ArgumentNullException ("chars");
                }
 
                unsafe {
-                       fixed (char* cptr = s) {
+                       fixed (char* cptr = chars) {
                                char dummy = '\0';
-                               return InternalGetByteCount (cptr, s.Length, ref dummy, true);
+                               return InternalGetByteCount (cptr, chars.Length, ref dummy, true);
                        }
                }
        }
+#endif
+
+#if NET_2_0
+       [CLSCompliant (false)]
+       [ComVisible (false)]
+       public unsafe override int GetByteCount (char* chars, int count)
+       {
+               if (chars == null)
+                       throw new ArgumentNullException ("chars");
+               if (count == 0)
+                       return 0;
+               char dummy = '\0';
+               return InternalGetByteCount (chars, count, ref dummy, true);
+       }
+#endif
 
        #endregion
 
@@ -233,7 +241,7 @@ public class UTF8Encoding : Encoding
                                leftOver = '\0';
 #else
                                // Flush the left-over surrogate pair start.
-                               if (byteIndex + 3 >= bytes.Length)
+                               if (byteIndex >= bytes.Length - 3)
                                        throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
                                bytes [byteIndex++] = 0xEF;
                                bytes [byteIndex++] = 0xBB;
@@ -261,131 +269,97 @@ public class UTF8Encoding : Encoding
                }
        }
 
-       private unsafe static int InternalGetBytes (char* chars, int charCount,
-                                            byte* bytes, int byteCount,
-                                            ref char leftOver, bool flush)
+       private unsafe static int InternalGetBytes (char* chars, int count, byte* bytes, int bcount, ref char leftOver, bool flush)
        {
-               int charIndex = 0;
-               int byteIndex = 0;
-
-               // Convert the characters into bytes.
-               // Convert the characters into bytes.
-               char ch;
-               int length = byteCount;
-               char pair = leftOver;
-               int posn = byteIndex;
-               int code = 0;
-
-               while (charCount > 0) {
-                       // Fetch the next UTF-16 character pair value.
-                       ch = chars [charIndex];
-                       if (pair == '\0') {
-                               if (ch < '\uD800' || ch >= '\uE000') {
-                                       if (ch < '\x80') { // fast path optimization
-                                               int end = charIndex + charCount;
-                                               for (; charIndex < end; posn++, charIndex++, charCount--) {
-                                                       if (chars [charIndex] < '\x80')
-                                                               bytes [posn] = (byte) chars [charIndex];
-                                                       else
-                                                               break;
-                                               }
-                                               continue;
+               char* end = chars + count;
+               byte* end_bytes = bytes + bcount;
+               while (chars < end) {
+                       if (leftOver == 0) {
+                               for (; chars < end; chars++) {
+                                       int ch = *chars;
+                                       if (ch < '\x80') {
+                                               if (bytes >= end_bytes)
+                                                       goto fail_no_space;
+                                               *bytes++ = (byte)ch;
+                                       } else if (ch < '\x800') {
+                                               if (bytes + 1 >= end_bytes)
+                                                       goto fail_no_space;
+                                               bytes [0] = (byte) (0xC0 | (ch >> 6));
+                                               bytes [1] = (byte) (0x80 | (ch & 0x3F));
+                                               bytes += 2;
+                                       } else if (ch < '\uD800' || ch > '\uDFFF') {
+                                               if (bytes + 2 >= end_bytes)
+                                                       goto fail_no_space;
+                                               bytes [0] = (byte) (0xE0 | (ch >> 12));
+                                               bytes [1] = (byte) (0x80 | ((ch >> 6) & 0x3F));
+                                               bytes [2] = (byte) (0x80 | (ch & 0x3F));
+                                               bytes += 3;
+                                       } else if (ch <= '\uDBFF') {
+                                               // This is a surrogate char, exit the inner loop.
+                                               leftOver = *chars;
+                                               chars++;
+                                               break;
+                                       } else {
+                                               // We have a surrogate tail without 
+                                               // leading surrogate. In NET_2_0 it
+                                               // uses fallback. In NET_1_1 we output
+                                               // wrong surrogate.
+                                               if (bytes + 2 >= end_bytes)
+                                                       goto fail_no_space;
+                                               bytes [0] = (byte) (0xE0 | (ch >> 12));
+                                               bytes [1] = (byte) (0x80 | ((ch >> 6) & 0x3F));
+                                               bytes [2] = (byte) (0x80 | (ch & 0x3F));
+                                               bytes += 3;
+                                               leftOver = '\0';
                                        }
-                                       code = ch;
-                               }
-                               else if (ch < '\uDC00') {
-                                       // surrogate start
-                                       pair = ch;
-                                       ++charIndex;
-                                       --charCount;
-                                       continue;
-                               } else { // ch <= '\uDFFF'
-                                       // We have a surrogate tail without leading 
-                                       // surrogate. In NET_2_0 it uses fallback.
-                                       // In NET_1_1 we output wrong surrogate.
-                                       if ((posn + 3) > length) {
-                                               throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                                       }
-                                       bytes [posn++] = (byte) (0xE0 | (ch >> 12));
-                                       bytes [posn++] = (byte) (0x80 | ((ch >> 6) & 0x3F));
-                                       bytes [posn++] = (byte) (0x80 | (ch & 0x3F));
-                                       ++charIndex;
-                                       --charCount;
-                                       continue;
                                }
                        } else {
-                               if ('\uDC00' <= ch && ch <= '\uDFFF')
-                                       code =  0x10000 + (int) ch - 0xDC00 +
-                                               (((int) pair - 0xD800) << 10);
-                               else {
+                               if (*chars >= '\uDC00' && *chars <= '\uDFFF') {
+                                       // We have a correct surrogate pair.
+                                       int ch = 0x10000 + (int) *chars - 0xDC00 + (((int) leftOver - 0xD800) << 10);
+                                       if (bytes + 3 >= end_bytes)
+                                               goto fail_no_space;
+                                       bytes [0] = (byte) (0xF0 | (ch >> 18));
+                                       bytes [1] = (byte) (0x80 | ((ch >> 12) & 0x3F));
+                                       bytes [2] = (byte) (0x80 | ((ch >> 6) & 0x3F));
+                                       bytes [3] = (byte) (0x80 | (ch & 0x3F));
+                                       bytes += 4;
+                                       chars++;
+                               } else {
                                        // We have a surrogate start followed by a
                                        // regular character.  Technically, this is
                                        // invalid, but we have to do something.
                                        // We write out the surrogate start and then
                                        // re-visit the current character again.
-                                       if ((posn + 3) > length) {
-                                               throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                                       }
-                                       bytes [posn++] = (byte) (0xE0 | (pair >> 12));
-                                       bytes [posn++] = (byte) (0x80 | ((pair >> 6) & 0x3F));
-                                       bytes [posn++] = (byte) (0x80 | (pair & 0x3F));
-                                       pair = '\0';
-                                       continue;
+                                       int ch = leftOver;
+                                       if (bytes + 2 >= end_bytes)
+                                               goto fail_no_space;
+                                       bytes [0] = (byte) (0xE0 | (ch >> 12));
+                                       bytes [1] = (byte) (0x80 | ((ch >> 6) & 0x3F));
+                                       bytes [2] = (byte) (0x80 | (ch & 0x3F));
+                                       bytes += 3;
                                }
-                               pair = '\0';
-                       }
-                       ++charIndex;
-                       --charCount;
-
-                       // Encode the character pair value.
-                       if (code < 0x0080) {
-                               if (posn >= length)
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               bytes [posn++] = (byte)code;
-                       } else if (code < 0x0800) {
-                               if ((posn + 2) > length)
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               bytes [posn++] = (byte) (0xC0 | (code >> 6));
-                               bytes [posn++] = (byte) (0x80 | (code & 0x3F));
-                       } else if (code < 0x10000) {
-                               if ((posn + 3) > length)
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               bytes [posn++] = (byte) (0xE0 | (code >> 12));
-                               bytes [posn++] = (byte) (0x80 | ((code >> 6) & 0x3F));
-                               bytes [posn++] = (byte) (0x80 | (code & 0x3F));
-                       } else {
-                               if ((posn + 4) > length)
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
-                               bytes [posn++] = (byte) (0xF0 | (code >> 18));
-                               bytes [posn++] = (byte) (0x80 | ((code >> 12) & 0x3F));
-                               bytes [posn++] = (byte) (0x80 | ((code >> 6) & 0x3F));
-                               bytes [posn++] = (byte) (0x80 | (code & 0x3F));
+                               leftOver = '\0';
                        }
                }
-
                if (flush) {
-                       if (pair != '\0') {
-                               // Flush the left-over incomplete surrogate.
-                               if ((posn + 3) > length) {
-                                       throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
+                       // Flush the left-over surrogate pair start.
+                       if (leftOver != '\0') {
+                               int ch = leftOver;
+                               if (bytes + 2 < end_bytes) {
+                                       bytes [0] = (byte) (0xE0 | (ch >> 12));
+                                       bytes [1] = (byte) (0x80 | ((ch >> 6) & 0x3F));
+                                       bytes [2] = (byte) (0x80 | (ch & 0x3F));
+                                       bytes += 3;
+                               } else {
+                                       goto fail_no_space;
                                }
-                               bytes [posn++] = (byte) (0xE0 | (pair >> 12));
-                               bytes [posn++] = (byte) (0x80 | ((pair >> 6) & 0x3F));
-                               bytes [posn++] = (byte) (0x80 | (pair & 0x3F));
+                               leftOver = '\0';
                        }
-                       leftOver = '\0';
                }
-               else
-                       leftOver = pair;
-Char.IsLetterOrDigit (pair);
-
-               // Return the final count to the caller.
-               return posn - byteIndex;
-       }
-
-       private unsafe int Fallback (byte* bytes, int byteCount, char lead, char tail)
-       {
-               throw new NotImplementedException ();
+               return (int)(bytes - (end_bytes - bcount));
+fail_no_space:
+               throw new ArgumentException ("Insufficient Space", "bytes");
        }
 
        // Get the bytes that result from encoding a character buffer.
@@ -437,19 +411,42 @@ Char.IsLetterOrDigit (pair);
                }
        }
 
+#if NET_2_0
+       [CLSCompliant (false)]
+       [ComVisible (false)]
+       public unsafe override int GetBytes (char* chars, int charCount, byte* bytes, int byteCount)
+       {
+               if (chars == null)
+                       throw new ArgumentNullException ("chars");
+               if (charCount < 0)
+                       throw new IndexOutOfRangeException ("charCount");
+               if (bytes == null)
+                       throw new ArgumentNullException ("bytes");
+               if (byteCount < 0)
+                       throw new IndexOutOfRangeException ("charCount");
+
+               if (charCount == 0)
+                       return 0;
+
+               char dummy = '\0';
+               if (byteCount == 0)
+                       return InternalGetBytes (chars, charCount, null, 0, ref dummy, true);
+               else
+                       return InternalGetBytes (chars, charCount, bytes, byteCount, ref dummy, true);
+       }
+#endif
+
        #endregion
 
        // Internal version of "GetCharCount" which can handle a rolling
        // state between multiple calls to this method.
 #if NET_2_0
-       // Internal version of "GetCharCount" which can handle a rolling
-       // state between multiple calls to this method.
-       private static int InternalGetCharCount (
+       private unsafe static int InternalGetCharCount (
                byte[] bytes, int index, int count, uint leftOverBits,
                uint leftOverCount, object provider,
-               ref DecoderFallbackBuffer fallbackBuffer, bool flush)
+               ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
 #else
-       private static int InternalGetCharCount (
+       private unsafe static int InternalGetCharCount (
                byte[] bytes, int index, int count, uint leftOverBits,
                uint leftOverCount, bool throwOnInvalid, bool flush)
 #endif
@@ -465,6 +462,31 @@ Char.IsLetterOrDigit (pair);
                        throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
                }
 
+               if (count == 0)
+                       return 0;
+               fixed (byte *bptr = bytes)
+#if NET_2_0
+                       return InternalGetCharCount (bptr + index, count,
+                               leftOverBits, leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
+#else
+                       return InternalGetCharCount (bptr + index, count,
+                               leftOverBits, leftOverCount, throwOnInvalid, flush);
+#endif
+       }
+
+#if NET_2_0
+       private unsafe static int InternalGetCharCount (
+               byte* bytes, int count, uint leftOverBits,
+               uint leftOverCount, object provider,
+               ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
+#else
+       private unsafe static int InternalGetCharCount (
+               byte* bytes, int count, uint leftOverBits,
+               uint leftOverCount, bool throwOnInvalid, bool flush)
+#endif
+       {
+               int index = 0;
+
                int length = 0;
 
                if (leftOverCount == 0) {
@@ -518,7 +540,7 @@ Char.IsLetterOrDigit (pair);
                                } else {
                                        // Invalid UTF-8 start character.
 #if NET_2_0
-                                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                       length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - 1, 1);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -552,7 +574,7 @@ Char.IsLetterOrDigit (pair);
                                                        }
                                                        if (overlong) {
 #if NET_2_0
-                                                               length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                                               length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
 #else
                                                                if (throwOnInvalid)
                                                                        throw new ArgumentException (_("Overlong"), leftBits.ToString ());
@@ -564,7 +586,7 @@ Char.IsLetterOrDigit (pair);
                                                        length += 2;
                                                } else {
 #if NET_2_0
-                                                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                                       length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
 #else
                                                        if (throwOnInvalid)
                                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -575,7 +597,7 @@ Char.IsLetterOrDigit (pair);
                                } else {
                                        // Invalid UTF-8 sequence: clear and restart.
 #if NET_2_0
-                                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                                       length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -590,7 +612,7 @@ Char.IsLetterOrDigit (pair);
                        // We had left-over bytes that didn't make up
                        // a complete UTF-8 character sequence.
 #if NET_2_0
-                       length += Fallback (provider, ref fallbackBuffer, bytes, index - 1);
+                       length += Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, index - leftSoFar, leftSoFar);
 #else
                        if (throwOnInvalid)
                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -603,7 +625,7 @@ Char.IsLetterOrDigit (pair);
 
 #if NET_2_0
        // for GetCharCount()
-       static int Fallback (object provider, ref DecoderFallbackBuffer buffer, byte [] bytes, int index)
+       static unsafe int Fallback (object provider, ref DecoderFallbackBuffer buffer, ref byte [] bufferArg, byte* bytes, long index, uint size)
        {
                if (buffer == null) {
                        DecoderFallback fb = provider as DecoderFallback;
@@ -612,13 +634,21 @@ Char.IsLetterOrDigit (pair);
                        else
                                buffer = ((Decoder) provider).FallbackBuffer;
                }
-               buffer.Fallback (bytes, index - 1);
-               return buffer.Remaining;
+               if (bufferArg == null)
+                       bufferArg = new byte [1];
+               int ret = 0;
+               for (int i = 0; i < size; i++) {
+                       bufferArg [0] = bytes [(int) index + i];
+                       buffer.Fallback (bufferArg, 0);
+                       ret += buffer.Remaining;
+                       buffer.Reset ();
+               }
+               return ret;
        }
 
        // for GetChars()
-       static void Fallback (object provider, ref DecoderFallbackBuffer buffer, byte [] bytes, int byteIndex,
-               char [] chars, ref int charIndex)
+       static unsafe void Fallback (object provider, ref DecoderFallbackBuffer buffer, ref byte [] bufferArg, byte* bytes, long byteIndex, uint size,
+               char* chars, ref int charIndex)
        {
                if (buffer == null) {
                        DecoderFallback fb = provider as DecoderFallback;
@@ -627,9 +657,15 @@ Char.IsLetterOrDigit (pair);
                        else
                                buffer = ((Decoder) provider).FallbackBuffer;
                }
-               buffer.Fallback (bytes, byteIndex - 1);
-               while (buffer.Remaining > 0)
-                       chars [charIndex++] = buffer.GetNextChar ();
+               if (bufferArg == null)
+                       bufferArg = new byte [1];
+               for (int i = 0; i < size; i++) {
+                       bufferArg [0] = bytes [byteIndex + i];
+                       buffer.Fallback (bufferArg, 0);
+                       while (buffer.Remaining > 0)
+                               chars [charIndex++] = buffer.GetNextChar ();
+                       buffer.Reset ();
+               }
        }
 #endif
 
@@ -638,21 +674,33 @@ Char.IsLetterOrDigit (pair);
        {
 #if NET_2_0
                DecoderFallbackBuffer buf = null;
-               return InternalGetCharCount (bytes, index, count, 0, 0, DecoderFallback, ref buf, true);
+               byte [] bufferArg = null;
+               return InternalGetCharCount (bytes, index, count, 0, 0, DecoderFallback, ref buf, ref bufferArg, true);
 #else
                return InternalGetCharCount (bytes, index, count, 0, 0, throwOnInvalid, true);
 #endif
        }
 
+#if NET_2_0
+       [CLSCompliant (false)]
+       [ComVisible (false)]
+       public unsafe override int GetCharCount (byte* bytes, int count)
+       {
+               DecoderFallbackBuffer buf = null;
+               byte [] bufferArg = null;
+               return InternalGetCharCount (bytes, count, 0, 0, DecoderFallback, ref buf, ref bufferArg, true);
+       }
+#endif
+
        // Get the characters that result from decoding a byte buffer.
 #if NET_2_0
-       private static int InternalGetChars (
+       private unsafe static int InternalGetChars (
                byte[] bytes, int byteIndex, int byteCount, char[] chars,
                int charIndex, ref uint leftOverBits, ref uint leftOverCount,
                object provider,
-               ref DecoderFallbackBuffer fallbackBuffer, bool flush)
+               ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
 #else
-       private static int InternalGetChars (
+       private unsafe static int InternalGetChars (
                byte[] bytes, int byteIndex, int byteCount, char[] chars,
                int charIndex, ref uint leftOverBits, ref uint leftOverCount,
                bool throwOnInvalid, bool flush)
@@ -678,6 +726,38 @@ Char.IsLetterOrDigit (pair);
                if (charIndex == chars.Length)
                        return 0;
 
+               fixed (char* cptr = chars) {
+#if NET_2_0
+                       if (byteCount == 0 || byteIndex == bytes.Length)
+                               return InternalGetChars (null, 0, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
+                       // otherwise...
+                       fixed (byte* bptr = bytes)
+                               return InternalGetChars (bptr + byteIndex, byteCount, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, ref bufferArg, flush);
+#else
+                       if (byteCount == 0 || byteIndex == bytes.Length)
+                               return InternalGetChars (null, 0, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, flush);
+                       // otherwise...
+                       fixed (byte* bptr = bytes)
+                               return InternalGetChars (bptr + byteIndex, byteCount, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, flush);
+#endif
+               }
+       }
+
+#if NET_2_0
+       private unsafe static int InternalGetChars (
+               byte* bytes, int byteCount, char* chars, int charCount,
+               ref uint leftOverBits, ref uint leftOverCount,
+               object provider,
+               ref DecoderFallbackBuffer fallbackBuffer, ref byte [] bufferArg, bool flush)
+#else
+       private unsafe static int InternalGetChars (
+               byte* bytes, int byteCount, char* chars, int charCount,
+               ref uint leftOverBits, ref uint leftOverCount,
+               bool throwOnInvalid, bool flush)
+#endif
+       {
+               int charIndex = 0, byteIndex = 0;
+               int length = charCount;
                int posn = charIndex;
 
                if (leftOverCount == 0) {
@@ -692,15 +772,11 @@ Char.IsLetterOrDigit (pair);
 
                // Convert the bytes into the output buffer.
                uint ch;
-               int length = chars.Length;
                uint leftBits = leftOverBits;
                uint leftSoFar = (leftOverCount & (uint)0x0F);
                uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
 
                int byteEnd = byteIndex + byteCount;
-               if (byteEnd < 0 || byteEnd > bytes.Length)
-                       throw new SystemException (String.Format ("INTERNAL ERROR: should not happen: {0} {1} {2}", byteIndex, byteCount, byteEnd));
-
                for(; byteIndex < byteEnd; byteIndex++) {
                        // Fetch the next character from the byte buffer.
                        ch = (uint)(bytes[byteIndex]);
@@ -740,7 +816,7 @@ Char.IsLetterOrDigit (pair);
                                } else {
                                        // Invalid UTF-8 start character.
 #if NET_2_0
-                                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                       Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex, 1, chars, ref posn);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -774,7 +850,7 @@ Char.IsLetterOrDigit (pair);
                                                        }
                                                        if (overlong) {
 #if NET_2_0
-                                                               Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                                               Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
 #else
                                                                if (throwOnInvalid)
                                                                        throw new ArgumentException (_("Overlong"), leftBits.ToString ());
@@ -783,7 +859,7 @@ Char.IsLetterOrDigit (pair);
                                                        else if ((leftBits & 0xF800) == 0xD800) {
                                                                // UTF-8 doesn't use surrogate characters
 #if NET_2_0
-                                                               Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                                               Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
 #else
                                                                if (throwOnInvalid)
                                                                        throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -808,7 +884,7 @@ Char.IsLetterOrDigit (pair);
                                                                (char)((leftBits & (uint)0x3FF) + (uint)0xDC00);
                                                } else {
 #if NET_2_0
-                                                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                                       Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
 #else
                                                        if (throwOnInvalid)
                                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -819,7 +895,7 @@ Char.IsLetterOrDigit (pair);
                                } else {
                                        // Invalid UTF-8 sequence: clear and restart.
 #if NET_2_0
-                                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                                       Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
 #else
                                        if (throwOnInvalid)
                                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -833,7 +909,7 @@ Char.IsLetterOrDigit (pair);
                        // We had left-over bytes that didn't make up
                        // a complete UTF-8 character sequence.
 #if NET_2_0
-                       Fallback (provider, ref fallbackBuffer, bytes, byteIndex, chars, ref posn);
+                       Fallback (provider, ref fallbackBuffer, ref bufferArg, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
 #else
                        if (throwOnInvalid)
                                throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
@@ -854,14 +930,29 @@ Char.IsLetterOrDigit (pair);
                uint leftOverCount = 0;
 #if NET_2_0
                DecoderFallbackBuffer buf = null;
+               byte [] bufferArg = null;
                return InternalGetChars (bytes, byteIndex, byteCount, chars, 
-                               charIndex, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, true);
+                               charIndex, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, ref bufferArg, true);
 #else
                return InternalGetChars (bytes, byteIndex, byteCount, chars, 
                                charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, true);
 #endif
        }
 
+#if NET_2_0
+       [CLSCompliant (false)]
+       [ComVisible (false)]
+       public unsafe override int GetChars (byte* bytes, int byteCount, char* chars, int charCount)
+       {
+               DecoderFallbackBuffer buf = null;
+               byte [] bufferArg = null;
+               uint leftOverBits = 0;
+               uint leftOverCount = 0;
+               return InternalGetChars (bytes, byteCount, chars, 
+                               charCount, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, ref bufferArg, true);
+       }
+#endif
+
        // Get the maximum number of bytes needed to encode a
        // specified number of characters.
        public override int GetMaxByteCount (int charCount)
@@ -937,7 +1028,23 @@ Char.IsLetterOrDigit (pair);
        {
                return base.GetHashCode ();
        }
-       
+
+#if NET_2_0
+       public override int GetByteCount (string chars)
+       {
+               // hmm, does this override make any sense?
+               return base.GetByteCount (chars);
+       }
+
+       [ComVisible (false)]
+       public override string GetString (byte [] bytes, int index, int count)
+       {
+               // hmm, does this override make any sense?
+               return base.GetString (bytes, index, count);
+       }
+#endif
+
+#if !NET_2_0
        public override byte [] GetBytes (String s)
        {
                if (s == null)
@@ -948,6 +1055,7 @@ Char.IsLetterOrDigit (pair);
                GetBytes (s, 0, s.Length, bytes, 0);
                return bytes;
        }
+#endif
 
        // UTF-8 decoder implementation.
        [Serializable]
@@ -980,8 +1088,9 @@ Char.IsLetterOrDigit (pair);
                {
 #if NET_2_0
                        DecoderFallbackBuffer buf = null;
+                       byte [] bufferArg = null;
                        return InternalGetCharCount (bytes, index, count,
-                               leftOverBits, leftOverCount, this, ref buf, false);
+                               leftOverBits, leftOverCount, this, ref buf, ref bufferArg, false);
 #else
                        return InternalGetCharCount (bytes, index, count,
                                        leftOverBits, leftOverCount, throwOnInvalid, false);
@@ -992,8 +1101,9 @@ Char.IsLetterOrDigit (pair);
                {
 #if NET_2_0
                        DecoderFallbackBuffer buf = null;
+                       byte [] bufferArg = null;
                        return InternalGetChars (bytes, byteIndex, byteCount,
-                               chars, charIndex, ref leftOverBits, ref leftOverCount, this, ref buf, false);
+                               chars, charIndex, ref leftOverBits, ref leftOverCount, this, ref buf, ref bufferArg, false);
 #else
                        return InternalGetChars (bytes, byteIndex, byteCount,
                                chars, charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, false);
@@ -1006,14 +1116,14 @@ Char.IsLetterOrDigit (pair);
        [Serializable]
        private class UTF8Encoder : Encoder
        {
-               private bool emitIdentifier;
+//             private bool emitIdentifier;
                private char leftOverForCount;
                private char leftOverForConv;
 
                // Constructor.
                public UTF8Encoder (bool emitIdentifier)
                {
-                       this.emitIdentifier = emitIdentifier;
+//                     this.emitIdentifier = emitIdentifier;
                        leftOverForCount = '\0';
                        leftOverForConv = '\0';
                }
@@ -1029,7 +1139,7 @@ Char.IsLetterOrDigit (pair);
                {
                        int result;
                        result = InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, ref leftOverForConv, flush);
-                       emitIdentifier = false;
+//                     emitIdentifier = false;
                        return result;
                }
 
@@ -1044,7 +1154,7 @@ Char.IsLetterOrDigit (pair);
                {
                        int result;
                        result = InternalGetBytes (chars, charCount, bytes, byteCount, ref leftOverForConv, flush);
-                       emitIdentifier = false;
+//                     emitIdentifier = false;
                        return result;
                }
 #endif