+++ /dev/null
-//\r
-// System.Globalization.CodePageDataItem.cs\r
-//\r
-// Author:\r
-// Kornél Pál <http://www.kornelpal.hu/>\r
-//\r
-// Copyright (C) 2006 Kornél Pál\r
-//\r
-\r
-//\r
-// Permission is hereby granted, free of charge, to any person obtaining\r
-// a copy of this software and associated documentation files (the\r
-// "Software"), to deal in the Software without restriction, including\r
-// without limitation the rights to use, copy, modify, merge, publish,\r
-// distribute, sublicense, and/or sell copies of the Software, and to\r
-// permit persons to whom the Software is furnished to do so, subject to\r
-// the following conditions:\r
-//\r
-// The above copyright notice and this permission notice shall be\r
-// included in all copies or substantial portions of the Software.\r
-//\r
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
-//\r
-\r
-//\r
-// .NET Framework 1.x serializes encoding properties using this class and\r
-// .NET Framework 2.0 can deserialize it but drops the properties.\r
-// This class supports serialization compatibility.\r
-//\r
-\r
-using System;\r
-\r
-namespace System.Globalization\r
-{\r
- [Serializable]\r
- internal sealed class CodePageDataItem\r
- {\r
-#pragma warning disable 169 \r
- private string m_bodyName;\r
- private int m_codePage;\r
- private int m_dataIndex;\r
- private string m_description;\r
- private uint m_flags;\r
- private string m_headerName;\r
- private int m_uiFamilyCodePage;\r
- private string m_webName;\r
-\r
- private CodePageDataItem ()\r
- {\r
- }\r
-#pragma warning disable 169 \r
- }\r
-}\r
+++ /dev/null
-/*
- * ASCIIEncoding.cs - Implementation of the "System.Text.ASCIIEncoding" class.
- *
- * Copyright (c) 2001 Southern Storm Software, Pty Ltd
- * Copyright (C) 2003 Novell, Inc.
- * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-using System.Runtime.InteropServices;
-
-[Serializable]
-[ComVisible (true)]
-[MonoLimitation ("Serialization format not compatible with .NET")]
-public class ASCIIEncoding : Encoding
-{
- // Magic number used by Windows for "ASCII".
- internal const int ASCII_CODE_PAGE = 20127;
-
- // Constructor.
- public ASCIIEncoding () : base(ASCII_CODE_PAGE) {
- body_name = header_name = web_name= "us-ascii";
- encoding_name = "US-ASCII";
- is_mail_news_display = true;
- is_mail_news_save = true;
- }
-
- [ComVisible (false)]
- public override bool IsSingleByte {
- get { return true; }
- }
-
- // Get the number of bytes needed to encode a character buffer.
- public override int GetByteCount (char[] chars, int index, int count)
- {
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (index < 0 || index > chars.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (chars.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- return count;
- }
-
- // Convenience wrappers for "GetByteCount".
- public override int GetByteCount (String chars)
- {
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- return chars.Length;
- }
-
- // Get the bytes that result from encoding a character buffer.
- public override int GetBytes (char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex)
- {
- EncoderFallbackBuffer buffer = null;
- char [] fallback_chars = null;
-
- return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
- ref buffer, ref fallback_chars);
- }
-
- int GetBytes (char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex,
- ref EncoderFallbackBuffer buffer,
- ref char [] fallback_chars)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
-
- unsafe {
- fixed (char *cptr = chars) {
- return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
- }
- }
- }
-
- // Convenience wrappers for "GetBytes".
- public override int GetBytes (String chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
- {
- EncoderFallbackBuffer buffer = null;
- char [] fallback_chars = null;
- return GetBytes (chars, charIndex, charCount, bytes, byteIndex,
- ref buffer, ref fallback_chars);
- }
-
- int GetBytes (String chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex,
- ref EncoderFallbackBuffer buffer,
- ref char [] fallback_chars)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
-
- unsafe {
- fixed (char *cptr = chars) {
- return InternalGetBytes (cptr, chars.Length, charIndex, charCount, bytes, byteIndex, ref buffer, ref fallback_chars);
- }
- }
- }
-
- unsafe int InternalGetBytes (char *chars, int charLength, int charIndex, int charCount,
- byte[] bytes, int byteIndex,
- ref EncoderFallbackBuffer buffer,
- ref char [] fallback_chars)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charIndex < 0 || charIndex > charLength)
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
- if (charCount < 0 || charCount > (charLength - charIndex))
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
- if (byteIndex < 0 || byteIndex > bytes.Length)
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- if ((bytes.Length - byteIndex) < charCount)
- throw new ArgumentException (_("Arg_InsufficientSpace"));
-
- int count = charCount;
- char ch;
- while (count-- > 0) {
- ch = chars [charIndex++];
- if (ch < (char)0x80) {
- bytes [byteIndex++] = (byte)ch;
- } else {
- if (buffer == null)
- buffer = EncoderFallback.CreateFallbackBuffer ();
- if (Char.IsSurrogate (ch) && count > 1 &&
- Char.IsSurrogate (chars [charIndex]))
- buffer.Fallback (ch, chars [charIndex], charIndex++ - 1);
- else
- buffer.Fallback (ch, charIndex - 1);
- if (fallback_chars == null || fallback_chars.Length < buffer.Remaining)
- fallback_chars = new char [buffer.Remaining];
- for (int i = 0; i < fallback_chars.Length; i++)
- fallback_chars [i] = buffer.GetNextChar ();
- byteIndex += GetBytes (fallback_chars, 0,
- fallback_chars.Length, bytes, byteIndex,
- ref buffer, ref fallback_chars);
- }
- }
- return charCount;
- }
-
- // Get the number of characters needed to decode a byte buffer.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (bytes.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- return count;
- }
-
- // Get the characters that result from decoding a byte buffer.
- public override int GetChars (byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
- {
- DecoderFallbackBuffer buffer = null;
- return GetChars (bytes, byteIndex, byteCount, chars,
- charIndex, ref buffer);
- }
-
- int GetChars (byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex,
- ref DecoderFallbackBuffer buffer)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (byteIndex < 0 || byteIndex > bytes.Length)
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex))
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- if (charIndex < 0 || charIndex > chars.Length)
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
-
- if ((chars.Length - charIndex) < byteCount)
- throw new ArgumentException (_("Arg_InsufficientSpace"));
-
- int count = byteCount;
- while (count-- > 0) {
- char c = (char) bytes [byteIndex++];
- if (c < '\x80')
- chars [charIndex++] = c;
- else {
- if (buffer == null)
- buffer = DecoderFallback.CreateFallbackBuffer ();
- var thisByte = new byte[] { bytes [byteIndex-1] };
- buffer.Fallback (thisByte, 0);
- while (buffer.Remaining > 0) {
- if (charIndex < chars.Length) {
- chars [charIndex++] = buffer.GetNextChar ();
- continue;
- }
- throw new ArgumentException (
- "The output char buffer is too small to contain the " +
- "decoded characters.");
- }
- }
- }
- return byteCount;
- }
-
- // Get the maximum number of bytes needed to encode a
- // specified number of characters.
- public override int GetMaxByteCount (int charCount)
- {
- if (charCount < 0) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
- }
- return charCount;
- }
-
- // Get the maximum number of characters needed to decode a
- // specified number of bytes.
- public override int GetMaxCharCount (int byteCount)
- {
- if (byteCount < 0) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
- }
- return byteCount;
- }
-
- // Decode a buffer of bytes into a string.
- public override String GetString (byte[] bytes, int byteIndex, int byteCount)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- }
- if (byteCount == 0)
- return String.Empty;
-
- unsafe {
- fixed (byte* bytePtr = bytes) {
- string s = string.InternalAllocateStr (byteCount);
-
- fixed (char* charPtr = s) {
- byte* currByte = bytePtr + byteIndex;
- byte* lastByte = currByte + byteCount;
- char* currChar = charPtr;
-
- while (currByte < lastByte) {
- byte b = currByte++ [0];
- currChar++ [0] = b <= 0x7F ? (char) b : (char) '?';
- }
- }
-
- return s;
- }
- }
- }
-
- [CLSCompliantAttribute (false)]
- [ComVisible (false)]
- public unsafe override int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- if (byteCount < charCount)
- throw new ArgumentException ("bytecount is less than the number of bytes required", "byteCount");
-
- for (int i = 0; i < charCount; i++){
- char c = chars [i];
- bytes [i] = (byte) ((c < (char) 0x80) ? c : '?');
- }
- return charCount;
- }
-
- [CLSCompliantAttribute(false)]
- [ComVisible (false)]
- public unsafe override int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
- if (charCount < byteCount)
- throw new ArgumentException ("charcount is less than the number of bytes required", "charCount");
-
- for (int i = 0; i < byteCount; i++){
- byte b = bytes [i];
- chars [i] = b > 127 ? '?' : (char) b;
- }
- return byteCount;
-
- }
-
- [CLSCompliantAttribute(false)]
- [ComVisible (false)]
- public unsafe override int GetCharCount (byte *bytes, int count)
- {
- return count;
- }
-
- [CLSCompliantAttribute(false)]
- [ComVisible (false)]
- public unsafe override int GetByteCount (char *chars, int count)
- {
- return count;
- }
-
- [ComVisible (false)]
- public override Decoder GetDecoder ()
- {
- return base.GetDecoder ();
- }
-
- [ComVisible (false)]
- public override Encoder GetEncoder ()
- {
- return base.GetEncoder ();
- }
-}; // class ASCIIEncoding
-
-}; // namespace System.Text
+++ /dev/null
-//
-// System.Text.CodePageEncoding.cs
-//
-// Author:
-// Kornél Pál <http://www.kornelpal.hu/>
-//
-// Copyright (C) 2006 Kornél Pál
-//
-
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-//
-// These proxy classes implement IObjectReference.GetRealObject() that returns
-// an instance of the appropriate Encoding, Encoder or Decoder class.
-// As a result serialized objects of these types will transparently be
-// deserialized to instances of the above described classes.
-//
-// Use SerializationInfo.SetType() in ISerializable.GetObjectData() method of
-// serializable classes to serialize their instances using a proxy class.
-//
-// All of these proxy classes are non-public thus they only have to be
-// serialization compatible with .NET Framework.
-//
-
-//
-// .NET Framework 1.x uses this class for internal encodings and
-// .NET Framework 2.0 serializes internal encodings using a proxy.
-// This class supports serialization compatibility.
-//
-
-using System;
-using System.Runtime.Serialization;
-
-namespace System.Text
-{
- [Serializable]
- internal sealed class CodePageEncoding : ISerializable, IObjectReference
- {
- //
- // .NET Framework 1.x uses this class for internal decoders and
- // .NET Framework 2.0 can deserialize them using a proxy.
- // This class supports serialization compatibility.
- //
-
- [Serializable]
- private sealed class Decoder : ISerializable, IObjectReference
- {
- private Encoding encoding;
- private System.Text.Decoder realObject;
-
- private Decoder (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
-
- this.encoding = (Encoding) info.GetValue ("encoding", typeof (Encoding));
- }
-
- public void GetObjectData (SerializationInfo info, StreamingContext context)
- {
- throw new ArgumentException ("This class cannot be serialized.");
- }
-
- public object GetRealObject (StreamingContext context)
- {
- if (this.realObject == null)
- this.realObject = this.encoding.GetDecoder ();
-
- return this.realObject;
- }
- }
-
- private int codePage;
- private bool isReadOnly;
- private EncoderFallback encoderFallback;
- private DecoderFallback decoderFallback;
- private Encoding realObject;
-
- private CodePageEncoding (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
-
- this.codePage = (int) info.GetValue ("m_codePage", typeof (int));
-
- try {
- this.isReadOnly = (bool) info.GetValue ("m_isReadOnly", typeof (bool));
- this.encoderFallback = (EncoderFallback) info.GetValue ("encoderFallback", typeof (EncoderFallback));
- this.decoderFallback = (DecoderFallback) info.GetValue ("decoderFallback", typeof (DecoderFallback));
- } catch (SerializationException) {
- // .NET Framework 1.x has no fallbacks
- this.isReadOnly = true;
- }
- }
-
- public void GetObjectData (SerializationInfo info, StreamingContext context)
- {
- throw new ArgumentException ("This class cannot be serialized.");
- }
-
- public object GetRealObject (StreamingContext context)
- {
- if (this.realObject == null) {
- Encoding encoding = Encoding.GetEncoding (this.codePage);
-
- if (!this.isReadOnly) {
- encoding = (Encoding) encoding.Clone ();
- encoding.EncoderFallback = this.encoderFallback;
- encoding.DecoderFallback = this.decoderFallback;
- }
-
- this.realObject = encoding;
- }
-
- return this.realObject;
- }
- }
-}
+++ /dev/null
-/*
- * Decoder.cs - Implementation of the "System.Text.Decoder" class.
- *
- * Copyright (c) 2001 Southern Storm Software, Pty Ltd
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-using System.Runtime.InteropServices;
-
-[Serializable]
-[ComVisible (true)]
-public abstract class Decoder
-{
-
- // Constructor.
- protected Decoder () {}
-
- DecoderFallback fallback = new DecoderReplacementFallback ();
- DecoderFallbackBuffer fallback_buffer;
-
- [ComVisible (false)]
- public DecoderFallback Fallback {
- get { return fallback; }
- set {
- if (value == null)
- throw new ArgumentNullException ();
- fallback = value;
- fallback_buffer = null;
- }
- }
-
- [ComVisible (false)]
- public DecoderFallbackBuffer FallbackBuffer {
- get {
- if (fallback_buffer == null)
- fallback_buffer = fallback.CreateFallbackBuffer ();
- return fallback_buffer;
- }
- }
-
- // Get the number of characters needed to decode a buffer.
- public abstract int GetCharCount (byte[] bytes, int index, int count);
-
- // Get the characters that result from decoding a buffer.
- public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex);
-
- [ComVisible (false)]
- public virtual int GetCharCount (byte [] bytes, int index, int count, bool flush)
- {
- if (flush)
- Reset ();
- return GetCharCount (bytes, index, count);
- }
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe virtual int GetCharCount (byte* bytes, int count, bool flush)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
-
- byte [] barr = new byte [count];
- Marshal.Copy ((IntPtr) bytes, barr, 0, count);
- return GetCharCount (barr, 0, count, flush);
- }
-
- public virtual int GetChars (
- byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex, bool flush)
- {
- CheckArguments (bytes, byteIndex, byteCount);
- CheckArguments (chars, charIndex);
-
- if (flush)
- Reset ();
- return GetChars (bytes, byteIndex, byteCount, chars, charIndex);
- }
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe virtual int GetChars (byte* bytes, int byteCount,
- char* chars, int charCount, bool flush)
- {
- CheckArguments (chars, charCount, bytes, byteCount);
-
- char [] carr = new char [charCount];
- byte [] barr = new byte [byteCount];
- Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
- int charsUsed = GetChars (barr, 0, byteCount, carr, 0, flush);
- Marshal.Copy (carr, 0, (IntPtr) chars, charsUsed);
- return charsUsed;
- }
-
- [ComVisible (false)]
- public virtual void Reset ()
- {
- if (fallback_buffer != null)
- fallback_buffer.Reset ();
- }
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe virtual void Convert (
- byte* bytes, int byteCount,
- char* chars, int charCount, bool flush,
- out int bytesUsed, out int charsUsed, out bool completed)
- {
- CheckArguments (chars, charCount, bytes, byteCount);
-
- bytesUsed = byteCount;
- while (true) {
- charsUsed = GetCharCount (bytes, bytesUsed, flush);
- if (charsUsed <= charCount)
- break;
- flush = false;
- bytesUsed >>= 1;
- }
- completed = bytesUsed == byteCount;
- charsUsed = GetChars (bytes, bytesUsed, chars, charCount, flush);
- }
-
- [ComVisible (false)]
- public virtual void Convert (
- byte [] bytes, int byteIndex, int byteCount,
- char [] chars, int charIndex, int charCount, bool flush,
- out int bytesUsed, out int charsUsed, out bool completed)
- {
- CheckArguments (bytes, byteIndex, byteCount);
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (charIndex < 0)
- throw new ArgumentOutOfRangeException ("charIndex");
- if (charCount < 0 || chars.Length < charIndex + charCount)
- throw new ArgumentOutOfRangeException ("charCount");
-
- bytesUsed = byteCount;
- while (true) {
- charsUsed = GetCharCount (bytes, byteIndex, bytesUsed, flush);
- if (charsUsed <= charCount)
- break;
- flush = false;
- bytesUsed >>= 1;
- }
- completed = bytesUsed == byteCount;
- charsUsed = GetChars (bytes, byteIndex, bytesUsed, chars, charIndex, flush);
- }
-
- void CheckArguments (char [] chars, int charIndex)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (charIndex < 0 || chars.Length < charIndex)
- throw new ArgumentOutOfRangeException ("charIndex");
- }
-
- void CheckArguments (byte [] bytes, int byteIndex, int byteCount)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (byteIndex < 0)
- throw new ArgumentOutOfRangeException ("byteIndex");
- if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
- throw new ArgumentOutOfRangeException ("byteCount");
- }
-
- unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
- }
-}; // class Decoder
-
-}; // namespace System.Text
+++ /dev/null
-//
-// DecoderExceptionFallback.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-namespace System.Text
-{
- [Serializable]
- public sealed class DecoderExceptionFallback : DecoderFallback
- {
- public DecoderExceptionFallback ()
- {
- }
-
- public override int MaxCharCount {
- get { return 0; }
- }
-
- public override DecoderFallbackBuffer CreateFallbackBuffer ()
- {
- return new DecoderExceptionFallbackBuffer ();
- }
-
- public override bool Equals (object value)
- {
- return (value is DecoderExceptionFallback);
- }
-
- public override int GetHashCode ()
- {
- return 0;
- }
- }
-}
+++ /dev/null
-//
-// DecoderExceptionFallbackBuffer.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- public sealed class DecoderExceptionFallbackBuffer
- : DecoderFallbackBuffer
- {
- public DecoderExceptionFallbackBuffer ()
- {
- }
-
- public override int Remaining {
- get { return 0; }
- }
-
- public override bool Fallback (byte [] bytesUnknown, int index)
- {
- throw new DecoderFallbackException (null, bytesUnknown, index);
- }
-
- public override char GetNextChar ()
- {
- return char.MinValue;
- }
-
- public override bool MovePrevious ()
- {
- return false;
- }
- }
-}
+++ /dev/null
-//
-// DecoderFallback.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- [Serializable]
- public abstract class DecoderFallback
- {
- static readonly DecoderFallback exception_fallback =
- new DecoderExceptionFallback ();
- static readonly DecoderFallback replacement_fallback =
- new DecoderReplacementFallback ();
- static readonly DecoderFallback standard_safe_fallback =
- new DecoderReplacementFallback ("\uFFFD");
-
- protected DecoderFallback ()
- {
- }
-
- public static DecoderFallback ExceptionFallback {
- get { return exception_fallback; }
- }
-
- public abstract int MaxCharCount { get; }
-
- public static DecoderFallback ReplacementFallback {
- get { return replacement_fallback; }
- }
-
- internal static DecoderFallback StandardSafeFallback {
- get { return standard_safe_fallback; }
- }
-
- public abstract DecoderFallbackBuffer CreateFallbackBuffer ();
- }
-}
+++ /dev/null
-//
-// DecoderFallbackBuffer.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- public abstract class DecoderFallbackBuffer
- {
- protected DecoderFallbackBuffer ()
- {
- }
-
- public abstract int Remaining { get; }
-
- public abstract bool Fallback (byte [] bytesUnknown, int index);
-
- public abstract char GetNextChar ();
-
- public abstract bool MovePrevious ();
-
- public virtual void Reset ()
- {
- }
- }
-}
-
+++ /dev/null
-//
-// DecoderFallbackException.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- [Serializable]
- public sealed class DecoderFallbackException : ArgumentException
- {
- public DecoderFallbackException ()
- : this (null)
- {
- }
-
- public DecoderFallbackException (string message)
- : base (message)
- {
- }
-
- public DecoderFallbackException (string message, Exception innerException)
- : base (message, innerException)
- {
- }
-
- public DecoderFallbackException (string message,
- byte [] bytesUnknown, int index)
- : base (message)
- {
- bytes_unknown = bytesUnknown;
- this.index = index;
- }
-
- byte [] bytes_unknown;
- int index = - 1;
-
- [MonoTODO]
- public byte [] BytesUnknown {
- get { return bytes_unknown; }
- }
-
- [MonoTODO]
- public int Index {
- get { return index; }
- }
- }
-}
+++ /dev/null
-//
-// DecoderReplacementFallback.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- [Serializable]
- public sealed class DecoderReplacementFallback : DecoderFallback
- {
- public DecoderReplacementFallback ()
- : this ("?")
- {
- }
-
- [MonoTODO]
- public DecoderReplacementFallback (string replacement)
- {
- if (replacement == null)
- throw new ArgumentNullException ();
- // FIXME: check replacement validity (invalid surrogate)
-
- this.replacement = replacement;
- }
-
- string replacement;
-
- public string DefaultString {
- get { return replacement; }
- }
-
- public override int MaxCharCount {
- get { return replacement.Length; }
- }
-
- public override DecoderFallbackBuffer CreateFallbackBuffer ()
- {
- return new DecoderReplacementFallbackBuffer (this);
- }
-
- public override bool Equals (object value)
- {
- DecoderReplacementFallback f = value as DecoderReplacementFallback;
- return f != null && replacement == f.replacement;
- }
-
- public override int GetHashCode ()
- {
- return replacement.GetHashCode ();
- }
- }
-}
+++ /dev/null
-//
-// DecoderReplacementFallbackBuffer.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-
-namespace System.Text
-{
- // This DecoderFallbackBuffer is simple. It ignores the input buffers.
- // DecoderFallbackBuffer users could implement their own complex
- // fallback buffers.
-
- public sealed class DecoderReplacementFallbackBuffer
- : DecoderFallbackBuffer
- {
- bool fallback_assigned;
- int current;
- string replacement;
-
- public DecoderReplacementFallbackBuffer (
- DecoderReplacementFallback fallback)
- {
- if (fallback == null)
- throw new ArgumentNullException ("fallback");
- replacement = fallback.DefaultString;
- current = 0;
- }
-
- public override int Remaining {
- get { return fallback_assigned ? replacement.Length - current : 0; }
- }
-
- public override bool Fallback (byte [] bytesUnknown, int index)
- {
- if (bytesUnknown == null)
- throw new ArgumentNullException ("bytesUnknown");
- if (fallback_assigned && Remaining != 0)
- throw new ArgumentException ("Reentrant Fallback method invocation occurred. It might be because either this FallbackBuffer is incorrectly shared by multiple threads, invoked inside Encoding recursively, or Reset invocation is forgotten.");
- if (index < 0 || bytesUnknown.Length < index)
- throw new ArgumentOutOfRangeException ("index");
- fallback_assigned = true;
- current = 0;
-
- return replacement.Length > 0;
- }
-
- public override char GetNextChar ()
- {
- if (!fallback_assigned)
- return '\0';
- if (current >= replacement.Length)
- return char.MinValue;
- return replacement [current++];
- }
-
- public override bool MovePrevious ()
- {
- if (current == 0)
- return false;
- current--;
- return true;
- }
-
- public override void Reset ()
- {
- fallback_assigned = false;
- current = 0;
- }
- }
-}
+++ /dev/null
-/*
- * Encoder.cs - Implementation of the "System.Text.Encoder" class.
- *
- * Copyright (c) 2001 Southern Storm Software, Pty Ltd
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-using System.Runtime.InteropServices;
-
-[Serializable]
-[ComVisible (true)]
-public abstract class Encoder
-{
-
- // Constructor.
- protected Encoder() {}
-
- EncoderFallback fallback = new EncoderReplacementFallback ();
- EncoderFallbackBuffer fallback_buffer;
-
- [ComVisible (false)]
- public EncoderFallback Fallback {
- get { return fallback; }
- set {
- if (value == null)
- throw new ArgumentNullException ();
- fallback = value;
- fallback_buffer = null;
- }
- }
-
- [ComVisible (false)]
- public EncoderFallbackBuffer FallbackBuffer {
- get {
- if (fallback_buffer == null)
- fallback_buffer = Fallback.CreateFallbackBuffer ();
- return fallback_buffer;
- }
- }
-
- // Get the number of bytes needed to encode a buffer.
- public abstract int GetByteCount(char[] chars, int index,
- int count, bool flush);
-
- // Get the bytes that result from decoding a buffer.
- public abstract int GetBytes(char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex, bool flush);
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe virtual int GetByteCount (char* chars, int count, bool flush)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
-
- char [] carr = new char [count];
- Marshal.Copy ((IntPtr) chars, carr, 0, count);
- return GetByteCount (carr, 0, count, flush);
- }
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe virtual int GetBytes (char* chars, int charCount,
- byte* bytes, int byteCount, bool flush)
- {
- CheckArguments (chars, charCount, bytes, byteCount);
-
- char [] carr = new char [charCount];
- Marshal.Copy ((IntPtr) chars, carr, 0, charCount);
- byte [] barr = new byte [byteCount];
- Marshal.Copy ((IntPtr) bytes, barr, 0, byteCount);
- return GetBytes (carr, 0, charCount, barr, 0, flush);
- }
-
- [ComVisible (false)]
- public virtual void Reset ()
- {
- if (fallback_buffer != null)
- fallback_buffer.Reset ();
- }
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe virtual void Convert (
- char* chars, int charCount,
- byte* bytes, int byteCount, bool flush,
- out int charsUsed, out int bytesUsed, out bool completed)
- {
- CheckArguments (chars, charCount, bytes, byteCount);
-
- charsUsed = charCount;
- while (true) {
- bytesUsed = GetByteCount (chars, charsUsed, flush);
- if (bytesUsed <= byteCount)
- break;
- flush = false;
- charsUsed >>= 1;
- }
- completed = charsUsed == charCount;
- bytesUsed = GetBytes (chars, charsUsed, bytes, byteCount, flush);
- }
-
- [ComVisible (false)]
- public virtual void Convert (
- char [] chars, int charIndex, int charCount,
- byte [] bytes, int byteIndex, int byteCount, bool flush,
- out int charsUsed, out int bytesUsed, out bool completed)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charIndex < 0)
- throw new ArgumentOutOfRangeException ("charIndex");
- if (charCount < 0 || chars.Length < charIndex + charCount)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteIndex < 0)
- throw new ArgumentOutOfRangeException ("byteIndex");
- if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- charsUsed = charCount;
- while (true) {
- bytesUsed = GetByteCount (chars, charIndex, charsUsed, flush);
- if (bytesUsed <= byteCount)
- break;
- flush = false;
- charsUsed >>= 1;
- }
- completed = charsUsed == charCount;
- bytesUsed = GetBytes (chars, charIndex, charsUsed, bytes, byteIndex, flush);
- }
-
- unsafe void CheckArguments (char* chars, int charCount, byte* bytes, int byteCount)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
- }
-}; // class Encoder
-
-}; // namespace System.Text
+++ /dev/null
-//
-// EncoderExceptionFallback.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-namespace System.Text
-{
- [Serializable]
- public sealed class EncoderExceptionFallback : EncoderFallback
- {
- public EncoderExceptionFallback ()
- {
- }
-
- public override int MaxCharCount {
- get { return 0; }
- }
-
- public override EncoderFallbackBuffer CreateFallbackBuffer ()
- {
- return new EncoderExceptionFallbackBuffer ();
- }
-
- public override bool Equals (object value)
- {
- return (value is EncoderExceptionFallback);
- }
-
- public override int GetHashCode ()
- {
- return 0;
- }
- }
-}
+++ /dev/null
-//
-// EncoderExceptionFallbackBuffer.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- public sealed class EncoderExceptionFallbackBuffer
- : EncoderFallbackBuffer
- {
- public EncoderExceptionFallbackBuffer ()
- {
- }
-
- public override int Remaining {
- get { return 0; }
- }
-
- public override bool Fallback (char charUnknown, int index)
- {
- throw new EncoderFallbackException (charUnknown, index);
- }
-
- public override bool Fallback (char charUnknownHigh, char charUnknownLow, int index)
- {
- throw new EncoderFallbackException (charUnknownHigh, charUnknownLow, index);
- }
-
- public override char GetNextChar ()
- {
- return char.MinValue;
- }
-
- public override bool MovePrevious ()
- {
- return false;
- }
- }
-}
+++ /dev/null
-//
-// EncoderFallback.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- [Serializable]
- public abstract class EncoderFallback
- {
- static readonly EncoderFallback exception_fallback =
- new EncoderExceptionFallback ();
- static readonly EncoderFallback replacement_fallback =
- new EncoderReplacementFallback ();
- static readonly EncoderFallback standard_safe_fallback =
- new EncoderReplacementFallback ("\uFFFD");
-
- protected EncoderFallback ()
- {
- }
-
- public static EncoderFallback ExceptionFallback {
- get { return exception_fallback; }
- }
-
- public abstract int MaxCharCount { get; }
-
- public static EncoderFallback ReplacementFallback {
- get { return replacement_fallback; }
- }
-
- internal static EncoderFallback StandardSafeFallback {
- get { return standard_safe_fallback; }
- }
-
- public abstract EncoderFallbackBuffer CreateFallbackBuffer ();
- }
-}
+++ /dev/null
-//
-// EncoderFallbackBuffer.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- public abstract class EncoderFallbackBuffer
- {
- protected EncoderFallbackBuffer ()
- {
- }
-
- public abstract int Remaining { get; }
-
- public abstract bool Fallback (char charUnknown, int index);
-
- public abstract bool Fallback (char charUnknownHigh, char charUnknownLow, int index);
-
- public abstract char GetNextChar ();
-
- public abstract bool MovePrevious ();
-
- public virtual void Reset ()
- {
- while (GetNextChar () != '\0')
- ;
- }
- }
-}
-
+++ /dev/null
-//
-// EncoderFallbackException.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- [Serializable]
- public sealed class EncoderFallbackException : ArgumentException
- {
- public EncoderFallbackException ()
- : this (null)
- {
- }
-
- public EncoderFallbackException (string message)
- : base (message)
- {
- }
-
- public EncoderFallbackException (string message, Exception innerException)
- : base (message, innerException)
- {
- }
-
- internal EncoderFallbackException (char charUnknown, int index)
- : base (null)
- {
- char_unknown = charUnknown;
- this.index = index;
- }
-
- internal EncoderFallbackException (char charUnknownHigh,
- char charUnknownLow, int index)
- : base (null)
- {
- char_unknown_high = charUnknownHigh;
- char_unknown_low = charUnknownLow;
- this.index = index;
- }
-
- char char_unknown, char_unknown_high, char_unknown_low;
- int index = - 1;
-
- public char CharUnknown {
- get { return char_unknown; }
- }
-
- public char CharUnknownHigh {
- get { return char_unknown_high; }
- }
-
- public char CharUnknownLow {
- get { return char_unknown_low; }
- }
-
- [MonoTODO]
- public int Index {
- get { return index; }
- }
-
- [MonoTODO]
- public bool IsUnknownSurrogate ()
- {
- throw new NotImplementedException ();
- }
- }
-}
+++ /dev/null
-//
-// EncoderReplacementFallback.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- [Serializable]
- public sealed class EncoderReplacementFallback : EncoderFallback
- {
- public EncoderReplacementFallback ()
- : this ("?")
- {
- }
-
- [MonoTODO]
- public EncoderReplacementFallback (string replacement)
- {
- if (replacement == null)
- throw new ArgumentNullException ();
- // FIXME: check replacement validity (invalid surrogate)
-
- this.replacement = replacement;
- }
-
- string replacement;
-
- public string DefaultString {
- get { return replacement; }
- }
-
- public override int MaxCharCount {
- get { return replacement.Length; }
- }
-
- public override EncoderFallbackBuffer CreateFallbackBuffer ()
- {
- return new EncoderReplacementFallbackBuffer (this);
- }
-
- public override bool Equals (object value)
- {
- EncoderReplacementFallback f = value as EncoderReplacementFallback;
- return f != null && replacement == f.replacement;
- }
-
- public override int GetHashCode ()
- {
- return replacement.GetHashCode ();
- }
- }
-}
+++ /dev/null
-//
-// EncoderReplacementFallbackBuffer.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
-// Copyright 2011 Xamarin Inc (http://www.xamarin.com).
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-namespace System.Text
-{
- // This EncoderFallbackBuffer is simple. It ignores the input buffers.
- // EncoderFallbackBuffer users could implement their own complex
- // fallback buffers.
-
- public sealed class EncoderReplacementFallbackBuffer
- : EncoderFallbackBuffer
- {
- string replacement;
- int current;
- bool fallback_assigned;
-
- public EncoderReplacementFallbackBuffer (
- EncoderReplacementFallback fallback)
- {
- if (fallback == null)
- throw new ArgumentNullException ("fallback");
- replacement = fallback.DefaultString;
- current = 0;
- }
-
- public override int Remaining {
- get { return replacement.Length - current; }
- }
-
- public override bool Fallback (char charUnknown, int index)
- {
- return Fallback (index);
- }
-
- public override bool Fallback (char charUnknownHigh, char charUnknownLow, int index)
- {
- return Fallback (index);
- }
-
- // hmm, what is this index for???
- private bool Fallback (int index)
- {
- if (fallback_assigned && Remaining != 0)
- throw new ArgumentException ("Reentrant Fallback method invocation occurred. It might be because either this FallbackBuffer is incorrectly shared by multiple threads, invoked inside Encoding recursively, or Reset invocation is forgotten.");
- if (index < 0)
- throw new ArgumentOutOfRangeException ("index");
- fallback_assigned = true;
- current = 0;
-
- return replacement.Length > 0;
- }
-
- public override char GetNextChar ()
- {
- if (current >= replacement.Length)
- return char.MinValue;
- return replacement [current++];
- }
-
- public override bool MovePrevious ()
- {
- if (current == 0)
- return false;
- current--;
- return true;
- }
-
- public override void Reset ()
- {
- fallback_assigned = false;
- current = 0;
- }
- }
-}
-
+++ /dev/null
-/*
- * Encoding.cs - Implementation of the "System.Text.Encoding" class.
- *
- * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
- * Copyright (c) 2002, Ximian, Inc.
- * Copyright (c) 2003, 2004 Novell, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-using System.Reflection;
-using System.Globalization;
-using System.Security;
-using System.Runtime.CompilerServices;
-using System.Runtime.InteropServices;
-
-[Serializable]
-[ComVisible (true)]
-public abstract class Encoding : ICloneable
-{
- // Code page used by this encoding.
- internal int codePage;
- internal int windows_code_page;
- bool is_readonly = true;
-
- // Constructor.
- protected Encoding ()
- {
- }
-
-#if ECMA_COMPAT
- protected internal
-#else
- protected
-#endif
- Encoding (int codePage)
- {
- this.codePage = windows_code_page = codePage;
-
- switch (codePage) {
- default:
- // MS has "InternalBestFit{Decoder|Encoder}Fallback
- // here, but we dunno what they are for.
- decoder_fallback = DecoderFallback.ReplacementFallback;
- encoder_fallback = EncoderFallback.ReplacementFallback;
- break;
- case 20127: // ASCII
- case 54936: // GB18030
- decoder_fallback = DecoderFallback.ReplacementFallback;
- encoder_fallback = EncoderFallback.ReplacementFallback;
- break;
- case 1200: // UTF16
- case 1201: // UTF16
- case 12000: // UTF32
- case 12001: // UTF32
- case 65000: // UTF7
- case 65001: // UTF8
- decoder_fallback = DecoderFallback.StandardSafeFallback;
- encoder_fallback = EncoderFallback.StandardSafeFallback;
- break;
- }
- }
-
- // until we change the callers:
- internal static string _ (string arg) {
- return arg;
- }
-
- DecoderFallback decoder_fallback;
- EncoderFallback encoder_fallback;
-
- [ComVisible (false)]
- public bool IsReadOnly {
- get { return is_readonly; }
- }
-
- [ComVisible (false)]
- public virtual bool IsSingleByte {
- get { return false; }
- }
-
- [ComVisible (false)]
- public DecoderFallback DecoderFallback {
- get { return decoder_fallback; }
- set {
- if (IsReadOnly)
- throw new InvalidOperationException ("This Encoding is readonly.");
- if (value == null)
- throw new ArgumentNullException ();
- decoder_fallback = value;
- }
- }
-
- [ComVisible (false)]
- public EncoderFallback EncoderFallback {
- get { return encoder_fallback; }
- set {
- if (IsReadOnly)
- throw new InvalidOperationException ("This Encoding is readonly.");
- if (value == null)
- throw new ArgumentNullException ();
- encoder_fallback = value;
- }
- }
-
- internal void SetFallbackInternal (EncoderFallback e, DecoderFallback d)
- {
- if (e != null)
- encoder_fallback = e;
- if (d != null)
- decoder_fallback = d;
- }
-
- // Convert between two encodings.
- public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
- byte[] bytes)
- {
- if (srcEncoding == null) {
- throw new ArgumentNullException ("srcEncoding");
- }
- if (dstEncoding == null) {
- throw new ArgumentNullException ("dstEncoding");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, 0, bytes.Length));
- }
- public static byte[] Convert (Encoding srcEncoding, Encoding dstEncoding,
- byte[] bytes, int index, int count)
- {
- if (srcEncoding == null) {
- throw new ArgumentNullException ("srcEncoding");
- }
- if (dstEncoding == null) {
- throw new ArgumentNullException ("dstEncoding");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException
- ("index", _("ArgRange_Array"));
- }
- if (count < 0 || (bytes.Length - index) < count) {
- throw new ArgumentOutOfRangeException
- ("count", _("ArgRange_Array"));
- }
- return dstEncoding.GetBytes (srcEncoding.GetChars (bytes, index, count));
- }
-
- // Determine if two Encoding objects are equal.
- public override bool Equals (Object value)
- {
- Encoding enc = (value as Encoding);
- if (enc != null) {
- return codePage == enc.codePage &&
- DecoderFallback.Equals (enc.DecoderFallback) &&
- EncoderFallback.Equals (enc.EncoderFallback);
- } else {
- return false;
- }
- }
-
- // Get the number of characters needed to encode a character buffer.
- public abstract int GetByteCount (char[] chars, int index, int count);
-
- // Convenience wrappers for "GetByteCount".
- public virtual int GetByteCount (String s)
- {
- if (s == null)
- throw new ArgumentNullException ("s");
-
- if (s.Length == 0)
- return 0;
- unsafe {
- fixed (char* cptr = s) {
- return GetByteCount (cptr, s.Length);
- }
- }
- }
- public virtual int GetByteCount (char[] chars)
- {
- if (chars != null) {
- return GetByteCount (chars, 0, chars.Length);
- } else {
- throw new ArgumentNullException ("chars");
- }
- }
-
- // Get the bytes that result from encoding a character buffer.
- public abstract int GetBytes (char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex);
-
- // Convenience wrappers for "GetBytes".
- public virtual int GetBytes (String s, int charIndex, int charCount,
- byte[] bytes, int byteIndex)
- {
- if (s == null)
- throw new ArgumentNullException ("s");
- if (charIndex < 0 || charIndex > s.Length)
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- if (charCount < 0 || charIndex > (s.Length - charCount))
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
- if (byteIndex < 0 || byteIndex > bytes.Length)
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
-
- if (charCount == 0 || bytes.Length == byteIndex)
- return 0;
- unsafe {
- fixed (char* cptr = s) {
- fixed (byte* bptr = bytes) {
- return GetBytes (cptr + charIndex,
- charCount,
- bptr + byteIndex,
- bytes.Length - byteIndex);
- }
- }
- }
- }
- public virtual byte[] GetBytes (String s)
- {
- if (s == null)
- throw new ArgumentNullException ("s");
-
- if (s.Length == 0)
- return EmptyArray<byte>.Value;
- int byteCount = GetByteCount (s);
- if (byteCount == 0)
- return EmptyArray<byte>.Value;
- unsafe {
- fixed (char* cptr = s) {
- byte [] bytes = new byte [byteCount];
- fixed (byte* bptr = bytes) {
- GetBytes (cptr, s.Length,
- bptr, byteCount);
- return bytes;
- }
- }
- }
- }
-
- public virtual byte[] GetBytes (char[] chars, int index, int count)
- {
- int numBytes = GetByteCount (chars, index, count);
- byte[] bytes = new byte [numBytes];
- GetBytes (chars, index, count, bytes, 0);
- return bytes;
- }
- public virtual byte[] GetBytes (char[] chars)
- {
- int numBytes = GetByteCount (chars, 0, chars.Length);
- byte[] bytes = new byte [numBytes];
- GetBytes (chars, 0, chars.Length, bytes, 0);
- return bytes;
- }
-
- // Get the number of characters needed to decode a byte buffer.
- public abstract int GetCharCount (byte[] bytes, int index, int count);
-
- // Convenience wrappers for "GetCharCount".
- public virtual int GetCharCount (byte[] bytes)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- return GetCharCount (bytes, 0, bytes.Length);
- }
-
- // Get the characters that result from decoding a byte buffer.
- public abstract int GetChars (byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex);
-
- // Convenience wrappers for "GetChars".
- public virtual char[] GetChars (byte[] bytes, int index, int count)
- {
- int numChars = GetCharCount (bytes, index, count);
- char[] chars = new char [numChars];
- GetChars (bytes, index, count, chars, 0);
- return chars;
- }
- public virtual char[] GetChars (byte[] bytes)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- int numChars = GetCharCount (bytes, 0, bytes.Length);
- char[] chars = new char [numChars];
- GetChars (bytes, 0, bytes.Length, chars, 0);
- return chars;
- }
-
- // Get a decoder that forwards requests to this object.
- public virtual Decoder GetDecoder ()
- {
- return new ForwardingDecoder (this);
- }
-
- // Get an encoder that forwards requests to this object.
- public virtual Encoder GetEncoder ()
- {
- return new ForwardingEncoder (this);
- }
-
- // Loaded copy of the "I18N" assembly. We need to move
- // this into a class in "System.Private" eventually.
- private static Assembly i18nAssembly;
- private static bool i18nDisabled;
-
- // Invoke a specific method on the "I18N" manager object.
- // Returns NULL if the method failed.
- private static Object InvokeI18N (String name, params Object[] args)
- {
- lock (lockobj) {
- // Bail out if we previously detected that there
- // is insufficent engine support for I18N handling.
- if (i18nDisabled) {
- return null;
- }
-
- // Find or load the "I18N" assembly.
- if (i18nAssembly == null) {
- try {
- try {
- i18nAssembly = Assembly.Load (Consts.AssemblyI18N);
- } catch (NotImplementedException) {
- // Assembly loading unsupported by the engine.
- i18nDisabled = true;
- return null;
- }
- if (i18nAssembly == null) {
- return null;
- }
- } catch (SystemException) {
- return null;
- }
- }
-
- // Find the "I18N.Common.Manager" class.
- Type managerClass;
- try {
- managerClass = i18nAssembly.GetType ("I18N.Common.Manager");
- } catch (NotImplementedException) {
- // "GetType" is not supported by the engine.
- i18nDisabled = true;
- return null;
- }
- if (managerClass == null) {
- return null;
- }
-
- // Get the value of the "PrimaryManager" property.
- Object manager;
- try {
- manager = managerClass.InvokeMember
- ("PrimaryManager",
- BindingFlags.GetProperty |
- BindingFlags.Static |
- BindingFlags.Public,
- null, null, null, null, null, null);
- if (manager == null) {
- return null;
- }
- } catch (MissingMethodException) {
- return null;
- } catch (SecurityException) {
- return null;
- } catch (NotImplementedException) {
- // "InvokeMember" is not supported by the engine.
- i18nDisabled = true;
- return null;
- }
-
- // Invoke the requested method on the manager.
- try {
- return managerClass.InvokeMember
- (name,
- BindingFlags.InvokeMethod |
- BindingFlags.Instance |
- BindingFlags.Public,
- null, manager, args, null, null, null);
- } catch (MissingMethodException) {
- return null;
- } catch (SecurityException) {
- return null;
- }
- }
- }
-
- // Get an encoder for a specific code page.
-#if ECMA_COMPAT
- private
-#else
- public
-#endif
- static Encoding GetEncoding (int codepage)
- {
- if (codepage < 0 || codepage > 0xffff)
- throw new ArgumentOutOfRangeException ("codepage",
- "Valid values are between 0 and 65535, inclusive.");
-
- // Check for the builtin code pages first.
- switch (codepage) {
- case 0: return Default;
-
- case ASCIIEncoding.ASCII_CODE_PAGE:
- return ASCII;
-
- case UTF7Encoding.UTF7_CODE_PAGE:
- return UTF7;
-
- case UTF8Encoding.UTF8_CODE_PAGE:
- return UTF8;
-
- case UTF32Encoding.UTF32_CODE_PAGE:
- return UTF32;
-
- case UTF32Encoding.BIG_UTF32_CODE_PAGE:
- return BigEndianUTF32;
-
- case UnicodeEncoding.UNICODE_CODE_PAGE:
- return Unicode;
-
- case UnicodeEncoding.BIG_UNICODE_CODE_PAGE:
- return BigEndianUnicode;
-
- case Latin1Encoding.ISOLATIN_CODE_PAGE:
- return ISOLatin1;
- default: break;
- }
- // Try to obtain a code page handler from the I18N handler.
- Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", codepage));
- if (enc != null) {
- enc.is_readonly = true;
- return enc;
- }
-
- // Build a code page class name.
- String cpName = "System.Text.CP" + codepage.ToString ();
-
- // Look for a code page converter in this assembly.
- Assembly assembly = Assembly.GetExecutingAssembly ();
- Type type = assembly.GetType (cpName);
- if (type != null) {
- enc = (Encoding)(Activator.CreateInstance (type));
- enc.is_readonly = true;
- return enc;
- }
-
- // Look in any assembly, in case the application
- // has provided its own code page handler.
- type = Type.GetType (cpName);
- if (type != null) {
- enc = (Encoding)(Activator.CreateInstance (type));
- enc.is_readonly = true;
- return enc;
- }
- // We have no idea how to handle this code page.
- throw new NotSupportedException
- (String.Format ("CodePage {0} not supported", codepage.ToString ()));
- }
-
-#if !ECMA_COMPAT
-
- [ComVisible (false)]
- public virtual object Clone ()
- {
- Encoding e = (Encoding) MemberwiseClone ();
- e.is_readonly = false;
- return e;
- }
-
- public static Encoding GetEncoding (int codepage,
- EncoderFallback encoderFallback, DecoderFallback decoderFallback)
- {
- if (encoderFallback == null)
- throw new ArgumentNullException ("encoderFallback");
- if (decoderFallback == null)
- throw new ArgumentNullException ("decoderFallback");
-
- Encoding e = GetEncoding (codepage).Clone () as Encoding;
- e.is_readonly = false;
- e.encoder_fallback = encoderFallback;
- e.decoder_fallback = decoderFallback;
- return e;
- }
-
- public static Encoding GetEncoding (string name,
- EncoderFallback encoderFallback, DecoderFallback decoderFallback)
- {
- if (encoderFallback == null)
- throw new ArgumentNullException ("encoderFallback");
- if (decoderFallback == null)
- throw new ArgumentNullException ("decoderFallback");
-
- Encoding e = GetEncoding (name).Clone () as Encoding;
- e.is_readonly = false;
- e.encoder_fallback = encoderFallback;
- e.decoder_fallback = decoderFallback;
- return e;
- }
-
- static EncodingInfo [] encoding_infos;
-
- // FIXME: As everyone would agree, this implementation is so *hacky*
- // and could be very easily broken. But since there is a test for
- // this method to make sure that this method always returns
- // the same number and content of encoding infos, this won't
- // matter practically.
- public static EncodingInfo[] GetEncodings ()
- {
- if (encoding_infos == null) {
- int [] codepages = new int [] {
- 37, 437, 500, 708,
- 850, 852, 855, 857, 858, 860, 861, 862, 863,
- 864, 865, 866, 869, 870, 874, 875,
- 932, 936, 949, 950,
- 1026, 1047, 1140, 1141, 1142, 1143, 1144,
- 1145, 1146, 1147, 1148, 1149,
- 1200, 1201, 1250, 1251, 1252, 1253, 1254,
- 1255, 1256, 1257, 1258,
- 10000, 10079, 12000, 12001,
- 20127, 20273, 20277, 20278, 20280, 20284,
- 20285, 20290, 20297, 20420, 20424, 20866,
- 20871, 21025, 21866, 28591, 28592, 28593,
- 28594, 28595, 28596, 28597, 28598, 28599,
- 28605, 38598,
- 50220, 50221, 50222, 51932, 51949, 54936,
- 57002, 57003, 57004, 57005, 57006, 57007,
- 57008, 57009, 57010, 57011,
- 65000, 65001};
-
- encoding_infos = new EncodingInfo [codepages.Length];
- for (int i = 0; i < codepages.Length; i++)
- encoding_infos [i] = new EncodingInfo (codepages [i]);
- }
- return encoding_infos;
- }
-
- [ComVisible (false)]
- public bool IsAlwaysNormalized ()
- {
- return IsAlwaysNormalized (NormalizationForm.FormC);
- }
-
- [ComVisible (false)]
- public virtual bool IsAlwaysNormalized (NormalizationForm form)
- {
- // umm, ASCIIEncoding should have overriden this method, no?
- return form == NormalizationForm.FormC && this is ASCIIEncoding;
- }
-
- // Get an encoding object for a specific web encoding name.
- public static Encoding GetEncoding (string name)
- {
- // Validate the parameters.
- if (name == null) {
- throw new ArgumentNullException ("name");
- }
-
- string converted = name.ToLowerInvariant ().Replace ('-', '_');
-
- // Builtin web encoding names and the corresponding code pages.
- switch (converted) {
- case "ascii":
- case "us_ascii":
- case "us":
- case "ansi_x3.4_1968":
- case "ansi_x3.4_1986":
- case "cp367":
- case "csascii":
- case "ibm367":
- case "iso_ir_6":
- case "iso646_us":
- case "iso_646.irv:1991":
- return GetEncoding (ASCIIEncoding.ASCII_CODE_PAGE);
-
- case "utf_7":
- case "csunicode11utf7":
- case "unicode_1_1_utf_7":
- case "unicode_2_0_utf_7":
- case "x_unicode_1_1_utf_7":
- case "x_unicode_2_0_utf_7":
- return GetEncoding (UTF7Encoding.UTF7_CODE_PAGE);
-
- case "utf_8":
- case "unicode_1_1_utf_8":
- case "unicode_2_0_utf_8":
- case "x_unicode_1_1_utf_8":
- case "x_unicode_2_0_utf_8":
- return GetEncoding (UTF8Encoding.UTF8_CODE_PAGE);
-
- case "utf_16":
- case "utf_16le":
- case "ucs_2":
- case "unicode":
- case "iso_10646_ucs2":
- return GetEncoding (UnicodeEncoding.UNICODE_CODE_PAGE);
-
- case "unicodefffe":
- case "utf_16be":
- return GetEncoding (UnicodeEncoding.BIG_UNICODE_CODE_PAGE);
-
- case "utf_32":
- case "utf_32le":
- case "ucs_4":
- return GetEncoding (UTF32Encoding.UTF32_CODE_PAGE);
-
- case "utf_32be":
- return GetEncoding (UTF32Encoding.BIG_UTF32_CODE_PAGE);
-
- case "iso_8859_1":
- case "latin1":
- return GetEncoding (Latin1Encoding.ISOLATIN_CODE_PAGE);
- }
-
- // Try to obtain a web encoding handler from the I18N handler.
- Encoding enc = (Encoding)(InvokeI18N ("GetEncoding", name));
- if (enc != null) {
- return enc;
- }
-
- // Build a web encoding class name.
- String encName = "System.Text.ENC" + converted;
-
-
- // Look for a code page converter in this assembly.
- Assembly assembly = Assembly.GetExecutingAssembly ();
- Type type = assembly.GetType (encName);
- if (type != null) {
- return (Encoding)(Activator.CreateInstance (type));
- }
-
- // Look in any assembly, in case the application
- // has provided its own code page handler.
- type = Type.GetType (encName);
- if (type != null) {
- return (Encoding)(Activator.CreateInstance (type));
- }
- // We have no idea how to handle this encoding name.
- throw new ArgumentException (String.Format ("Encoding name '{0}' not "
- + "supported", name), "name");
- }
-
-#endif // !ECMA_COMPAT
-
- // Get a hash code for this instance.
- public override int GetHashCode ()
- {
- return DecoderFallback.GetHashCode () << 24 + EncoderFallback.GetHashCode () << 16 + codePage;
- }
-
- // Get the maximum number of bytes needed to encode a
- // specified number of characters.
- public abstract int GetMaxByteCount (int charCount);
-
- // Get the maximum number of characters needed to decode a
- // specified number of bytes.
- public abstract int GetMaxCharCount (int byteCount);
-
- // Get the identifying preamble for this encoding.
- public virtual byte[] GetPreamble ()
- {
- return EmptyArray<byte>.Value;
- }
-
- // Decode a buffer of bytes into a string.
- public virtual String GetString (byte[] bytes, int index, int count)
- {
- return new String (GetChars(bytes, index, count));
- }
- public virtual String GetString (byte[] bytes)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
-
- return GetString (bytes, 0, bytes.Length);
- }
-
-#if !ECMA_COMPAT
-
- internal bool is_mail_news_display;
- internal bool is_mail_news_save;
- internal bool is_browser_save = false;
- internal bool is_browser_display = false;
- internal string body_name;
- internal string encoding_name;
- internal string header_name;
- internal string web_name;
-
- // Get the mail body name for this encoding.
- public virtual String BodyName
- {
- get {
- return body_name;
- }
- }
-
- // Get the code page represented by this object.
- public virtual int CodePage
- {
- get {
- return codePage;
- }
- }
-
- // Get the human-readable name for this encoding.
- public virtual String EncodingName
- {
- get {
- return encoding_name;
- }
- }
-
- // Get the mail agent header name for this encoding.
- public virtual String HeaderName
- {
- get {
- return header_name;
- }
- }
-
- // Determine if this encoding can be displayed in a Web browser.
- public virtual bool IsBrowserDisplay
- {
- get {
- return is_browser_display;
- }
- }
-
- // Determine if this encoding can be saved from a Web browser.
- public virtual bool IsBrowserSave
- {
- get {
- return is_browser_save;
- }
- }
-
- // Determine if this encoding can be displayed in a mail/news agent.
- public virtual bool IsMailNewsDisplay
- {
- get {
- return is_mail_news_display;
- }
- }
-
- // Determine if this encoding can be saved from a mail/news agent.
- public virtual bool IsMailNewsSave
- {
- get {
- return is_mail_news_save;
- }
- }
-
- // Get the IANA-preferred Web name for this encoding.
- public virtual String WebName
- {
- get {
- return web_name;
- }
- }
-
- // Get the Windows code page represented by this object.
- public virtual int WindowsCodePage
- {
- get {
- // We make no distinction between normal and
- // Windows code pages in this implementation.
- return windows_code_page;
- }
- }
-
-#endif // !ECMA_COMPAT
-
- // Storage for standard encoding objects.
- static volatile Encoding asciiEncoding;
- static volatile Encoding bigEndianEncoding;
- static volatile Encoding defaultEncoding;
- static volatile Encoding utf7Encoding;
- static volatile Encoding utf8EncodingWithMarkers;
- static volatile Encoding utf8EncodingWithoutMarkers;
- static volatile Encoding unicodeEncoding;
- static volatile Encoding isoLatin1Encoding;
- static volatile Encoding utf8EncodingUnsafe;
- static volatile Encoding utf32Encoding;
- static volatile Encoding bigEndianUTF32Encoding;
-
- static readonly object lockobj = new object ();
-
- // Get the standard ASCII encoding object.
- public static Encoding ASCII
- {
- get {
- if (asciiEncoding == null) {
- lock (lockobj) {
- if (asciiEncoding == null) {
- asciiEncoding = new ASCIIEncoding ();
-// asciiEncoding.is_readonly = true;
- }
- }
- }
-
- return asciiEncoding;
- }
- }
-
- // Get the standard big-endian Unicode encoding object.
- public static Encoding BigEndianUnicode
- {
- get {
- if (bigEndianEncoding == null) {
- lock (lockobj) {
- if (bigEndianEncoding == null) {
- bigEndianEncoding = new UnicodeEncoding (true, true);
-// bigEndianEncoding.is_readonly = true;
- }
- }
- }
-
- return bigEndianEncoding;
- }
- }
-
- [MethodImpl (MethodImplOptions.InternalCall)]
- extern internal static string InternalCodePage (ref int code_page);
-
- // Get the default encoding object.
- public static Encoding Default
- {
- get {
- if (defaultEncoding == null) {
- lock (lockobj) {
- if (defaultEncoding == null) {
-#if MOBILE
- defaultEncoding = UTF8;
-#else
- // See if the underlying system knows what
- // code page handler we should be using.
- int code_page = 1;
-
- string code_page_name = InternalCodePage (ref code_page);
- try {
- if (code_page == -1)
- defaultEncoding = GetEncoding (code_page_name);
- else {
- // map the codepage from internal to our numbers
- code_page = code_page & 0x0fffffff;
- switch (code_page){
- case 1: code_page = ASCIIEncoding.ASCII_CODE_PAGE; break;
- case 2: code_page = UTF7Encoding.UTF7_CODE_PAGE; break;
- case 3: code_page = UTF8Encoding.UTF8_CODE_PAGE; break;
- case 4: code_page = UnicodeEncoding.UNICODE_CODE_PAGE; break;
- case 5: code_page = UnicodeEncoding.BIG_UNICODE_CODE_PAGE; break;
- case 6: code_page = Latin1Encoding.ISOLATIN_CODE_PAGE; break;
- }
- defaultEncoding = GetEncoding (code_page);
- }
- } catch (NotSupportedException) {
- // code_page is not supported on underlying platform
- defaultEncoding = UTF8Unmarked;
- } catch (ArgumentException) {
- // code_page_name is not a valid code page, or is
- // not supported by underlying OS
- defaultEncoding = UTF8Unmarked;
- }
- defaultEncoding.is_readonly = true;
-#endif
- }
- }
- }
-
- return defaultEncoding;
- }
- }
-
-
- // Get the ISO Latin1 encoding object.
- private static Encoding ISOLatin1
- {
- get {
- if (isoLatin1Encoding == null) {
- lock (lockobj) {
- if (isoLatin1Encoding == null) {
- isoLatin1Encoding = new Latin1Encoding ();
-// isoLatin1Encoding.is_readonly = true;
- }
- }
- }
-
- return isoLatin1Encoding;
- }
- }
-
- // Get the standard UTF-7 encoding object.
-#if ECMA_COMPAT
- private
-#else
- public
-#endif
- static Encoding UTF7
- {
- get {
- if (utf7Encoding == null) {
- lock (lockobj) {
- if (utf7Encoding == null) {
- utf7Encoding = new UTF7Encoding ();
-// utf7Encoding.is_readonly = true;
- }
- }
- }
-
- return utf7Encoding;
- }
- }
-
- // Get the standard UTF-8 encoding object.
- public static Encoding UTF8
- {
- get {
- if (utf8EncodingWithMarkers == null) {
- lock (lockobj) {
- if (utf8EncodingWithMarkers == null) {
- utf8EncodingWithMarkers = new UTF8Encoding (true);
-// utf8EncodingWithMarkers.is_readonly = true;
- }
- }
- }
-
- return utf8EncodingWithMarkers;
- }
- }
-
- //
- // Only internal, to be used by the class libraries: Unmarked and non-input-validating
- //
- internal static Encoding UTF8Unmarked {
- get {
- if (utf8EncodingWithoutMarkers == null) {
- lock (lockobj){
- if (utf8EncodingWithoutMarkers == null){
- utf8EncodingWithoutMarkers = new UTF8Encoding (false, false);
-// utf8EncodingWithoutMarkers.is_readonly = true;
- }
- }
- }
-
- return utf8EncodingWithoutMarkers;
- }
- }
-
- //
- // Only internal, to be used by the class libraries: Unmarked and non-input-validating
- //
- internal static Encoding UTF8UnmarkedUnsafe {
- get {
- if (utf8EncodingUnsafe == null) {
- lock (lockobj){
- if (utf8EncodingUnsafe == null){
- utf8EncodingUnsafe = new UTF8Encoding (false, false);
- utf8EncodingUnsafe.is_readonly = false;
- utf8EncodingUnsafe.DecoderFallback = new DecoderReplacementFallback (String.Empty);
- utf8EncodingUnsafe.is_readonly = true;
- }
- }
- }
-
- return utf8EncodingUnsafe;
- }
- }
-
- // Get the standard little-endian Unicode encoding object.
- public static Encoding Unicode
- {
- get {
- if (unicodeEncoding == null) {
- lock (lockobj) {
- if (unicodeEncoding == null) {
- unicodeEncoding = new UnicodeEncoding (false, true);
-// unicodeEncoding.is_readonly = true;
- }
- }
- }
-
- return unicodeEncoding;
- }
- }
-
- // Get the standard little-endian UTF-32 encoding object.
- public static Encoding UTF32
- {
- get {
- if (utf32Encoding == null) {
- lock (lockobj) {
- if (utf32Encoding == null) {
- utf32Encoding = new UTF32Encoding (false, true);
-// utf32Encoding.is_readonly = true;
- }
- }
- }
-
- return utf32Encoding;
- }
- }
-
- // Get the standard big-endian UTF-32 encoding object.
- internal static Encoding BigEndianUTF32
- {
- get {
- if (bigEndianUTF32Encoding == null) {
- lock (lockobj) {
- if (bigEndianUTF32Encoding == null) {
- bigEndianUTF32Encoding = new UTF32Encoding (true, true);
-// bigEndianUTF32Encoding.is_readonly = true;
- }
- }
- }
-
- return bigEndianUTF32Encoding;
- }
- }
-
- // Forwarding decoder implementation.
- private sealed class ForwardingDecoder : EncodingDecoder
- {
- // Constructor.
- public ForwardingDecoder (Encoding enc)
- : base (enc)
- {
- }
-
- // Override inherited methods.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- return encoding.GetCharCount (bytes, index, count);
- }
- public override int GetChars (byte[] bytes, int byteIndex,
- int byteCount, char[] chars,
- int charIndex)
- {
- return encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
- }
-
- } // class ForwardingDecoder
-
- // Forwarding encoder implementation.
- private sealed class ForwardingEncoder : EncodingEncoder
- {
- // Constructor.
- public ForwardingEncoder (Encoding enc)
- : base (enc)
- {
- }
-
- // Override inherited methods.
- public override int GetByteCount (char[] chars, int index, int count, bool flush)
- {
- return encoding.GetByteCount (chars, index, count);
- }
- public override int GetBytes (char[] chars, int charIndex,
- int charCount, byte[] bytes,
- int byteCount, bool flush)
- {
- return encoding.GetBytes (chars, charIndex, charCount, bytes, byteCount);
- }
-
- } // class ForwardingEncoder
-
- [CLSCompliantAttribute(false)]
- [ComVisible (false)]
- public unsafe virtual int GetByteCount (char *chars, int count)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
- char [] c = new char [count];
-
- for (int p = 0; p < count; p++)
- c [p] = chars [p];
-
- return GetByteCount (c);
- }
-
- [CLSCompliantAttribute(false)]
- [ComVisible (false)]
- public unsafe virtual int GetCharCount (byte *bytes, int count)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
-
- byte [] ba = new byte [count];
- for (int i = 0; i < count; i++)
- ba [i] = bytes [i];
- return GetCharCount (ba, 0, count);
- }
-
- [CLSCompliantAttribute(false)]
- [ComVisible (false)]
- public unsafe virtual int GetChars (byte *bytes, int byteCount, char *chars, int charCount)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- byte [] ba = new byte [byteCount];
- for (int i = 0; i < byteCount; i++)
- ba [i] = bytes [i];
- char [] ret = GetChars (ba, 0, byteCount);
- int top = ret.Length;
-
- if (top > charCount)
- throw new ArgumentException ("charCount is less than the number of characters produced", "charCount");
-
- for (int i = 0; i < top; i++)
- chars [i] = ret [i];
- return top;
- }
-
- [CLSCompliantAttribute(false)]
- [ComVisible (false)]
- public unsafe virtual int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- char [] c = new char [charCount];
-
- for (int i = 0; i < charCount; i++)
- c [i] = chars [i];
-
- byte [] b = GetBytes (c, 0, charCount);
- int top = b.Length;
- if (top > byteCount)
- throw new ArgumentException ("byteCount is less that the number of bytes produced", "byteCount");
-
- for (int i = 0; i < top; i++)
- bytes [i] = b [i];
-
- return b.Length;
- }
-}; // class Encoding
-
-}; // namespace System.Text
+++ /dev/null
-//
-// System.Text.EncodingDecoder.cs
-//
-// Authors:
-// Marcos Henrich (marcos.henrich@xamarin.com)
-//
-// Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-using System;
-using System.Runtime.InteropServices;
-
-namespace System.Text {
-
-abstract class EncodingDecoder : Decoder {
- protected readonly Encoding encoding;
-
- // Constructor.
- protected EncodingDecoder (Encoding encoding)
- {
- this.encoding = encoding;
- var fallback = encoding.DecoderFallback;
- if (fallback != null)
- Fallback = fallback;
- }
-
- public unsafe override void Convert (
- byte* bytes, int byteCount,
- char* chars, int charCount, bool flush,
- out int bytesUsed, out int charsUsed, out bool completed)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- bytesUsed = encoding.GetByteCount(chars, charCount);
-
- if (bytesUsed > byteCount) {
- charsUsed = encoding.GetChars (bytes, byteCount, chars, charCount);
- bytesUsed = encoding.GetByteCount (chars, charsUsed);
- } else
- charsUsed = encoding.GetChars (bytes, bytesUsed, chars, charCount);
-
-
- completed = bytesUsed == byteCount;
- }
-
- public override void Convert (
- byte [] bytes, int byteIndex, int byteCount,
- char [] chars, int charIndex, int charCount, bool flush,
- out int bytesUsed, out int charsUsed, out bool completed)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charIndex < 0)
- throw new ArgumentOutOfRangeException ("charIndex");
- if (charCount < 0 || chars.Length < charIndex + charCount)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteIndex < 0)
- throw new ArgumentOutOfRangeException ("byteIndex");
- if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- bytesUsed = encoding.GetByteCount(chars, charIndex, charCount);
-
- if (bytesUsed > byteCount) {
- charsUsed = encoding.GetChars (bytes, byteIndex, byteCount, chars, charIndex);
- bytesUsed = encoding.GetByteCount (chars, charIndex, charsUsed);
- } else
- charsUsed = encoding.GetChars (bytes, byteIndex, bytesUsed, chars, charIndex);
-
- completed = bytesUsed == byteCount;
- }
-}; // class EncodingDecoder
-
-}; // namespace System.Text
+++ /dev/null
-//
-// System.Text.EncodingEncoder.cs
-//
-// Authors:
-// Marcos Henrich (marcos.henrich@xamarin.com)
-//
-// Copyright (C) 2014 Xamarin Inc (http://www.xamarin.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-using System;
-using System.Runtime.InteropServices;
-
-namespace System.Text {
-
-abstract class EncodingEncoder : Encoder {
- protected readonly Encoding encoding;
-
- // Constructor.
- protected EncodingEncoder (Encoding encoding)
- {
- this.encoding = encoding;
- var fallback = encoding.EncoderFallback;
- if (fallback != null)
- Fallback = fallback;
- }
-
- public unsafe override void Convert (
- char* chars, int charCount,
- byte* bytes, int byteCount, bool flush,
- out int charsUsed, out int bytesUsed, out bool completed)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- charsUsed = encoding.GetCharCount (bytes, byteCount);
-
- if (charsUsed > charCount)
- charsUsed = charCount;
-
- bytesUsed = encoding.GetBytes (chars, charsUsed, bytes, byteCount);
-
- completed = charsUsed == charCount;
- }
-
- public override void Convert (
- char [] chars, int charIndex, int charCount,
- byte [] bytes, int byteIndex, int byteCount, bool flush,
- out int charsUsed, out int bytesUsed, out bool completed)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (charIndex < 0)
- throw new ArgumentOutOfRangeException ("charIndex");
- if (charCount < 0 || chars.Length < charIndex + charCount)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteIndex < 0)
- throw new ArgumentOutOfRangeException ("byteIndex");
- if (byteCount < 0 || bytes.Length < byteIndex + byteCount)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- charsUsed = encoding.GetCharCount (bytes, byteIndex, byteCount);
-
- if (charsUsed > charCount)
- charsUsed = charCount;
-
- bytesUsed = encoding.GetBytes (chars, charIndex, charsUsed, bytes, byteIndex);
-
- completed = charsUsed == charCount;
- }
-}; // class EncodingEncoder
-
-}; // namespace System.Text
+++ /dev/null
-//
-// System.Text.EncodingInfo.cs
-//
-// Author:
-// Atsushi Enomoto <atsushi@ximian.com>
-//
-
-//
-// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-
-namespace System.Text
-{
- [Serializable]
- public sealed class EncodingInfo
- {
- readonly int codepage;
- Encoding encoding;
-
- internal EncodingInfo (int cp)
- {
- codepage = cp;
- }
-
- public int CodePage {
- get { return codepage; }
- }
-
- [MonoTODO]
- public string DisplayName {
- get { return Name; }
- }
-
- public string Name {
- get {
- if (encoding == null)
- encoding = GetEncoding ();
- return encoding.WebName;
- }
- }
-
- public override bool Equals (object value)
- {
- EncodingInfo ei = value as EncodingInfo;
- return ei != null &&
- ei.codepage == codepage;
- }
-
- public override int GetHashCode ()
- {
- return codepage;
- }
-
- public Encoding GetEncoding ()
- {
- return Encoding.GetEncoding (codepage);
- }
- }
-}
+++ /dev/null
-//
-// System.Text.MLangCodePageEncoding.cs
-//
-// Author:
-// Kornél Pál <http://www.kornelpal.hu/>
-//
-// Copyright (C) 2006 Kornél Pál
-//
-
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-//
-// These proxy classes implement IObjectReference.GetRealObject() that returns
-// an instance of the appropriate Encoding, Encoder or Decoder class.
-// As a result serialized objects of these types will transparently be
-// deserialized to instances of the above described classes.
-//
-// Use SerializationInfo.SetType() in ISerializable.GetObjectData() method of
-// serializable classes to serialize their instances using a proxy class.
-//
-// All of these proxy classes are non-public thus they only have to be
-// serialization compatible with .NET Framework.
-//
-
-//
-// .NET Framework 1.x uses this class for internal encodings and
-// .NET Framework 2.0 serializes internal encodings using a proxy.
-// This class supports serialization compatibility.
-//
-
-using System;
-using System.Runtime.Serialization;
-
-namespace System.Text
-{
- [Serializable]
- internal sealed class MLangCodePageEncoding : ISerializable, IObjectReference
- {
- //
- // .NET Framework 1.x uses this class for internal encoders.
- // .NET Framework 2.0 can deserialize them using a proxy although
- // this class is not serializable in .NET Framework 1.x.
- // This class supports serialization compatibility.
- //
-
- [Serializable]
- private sealed class MLangEncoder : ISerializable, IObjectReference
- {
- private Encoding encoding;
- private Encoder realObject;
-
- private MLangEncoder (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
-
- this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
- }
-
- public void GetObjectData (SerializationInfo info, StreamingContext context)
- {
- throw new ArgumentException ("This class cannot be serialized.");
- }
-
- public object GetRealObject (StreamingContext context)
- {
- if (this.realObject == null)
- this.realObject = this.encoding.GetEncoder ();
-
- return this.realObject;
- }
- }
-
- //
- // .NET Framework 1.x uses this class for internal decoders.
- // .NET Framework 2.0 can deserialize them using a proxy although
- // this class is not serializable in .NET Framework 1.x.
- // This class supports serialization compatibility.
- //
-
- [Serializable]
- private sealed class MLangDecoder : ISerializable, IObjectReference
- {
- private Encoding encoding;
- private Decoder realObject;
-
- private MLangDecoder (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
-
- this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
- }
-
- public void GetObjectData (SerializationInfo info, StreamingContext context)
- {
- throw new ArgumentException ("This class cannot be serialized.");
- }
-
- public object GetRealObject (StreamingContext context)
- {
- if (this.realObject == null)
- this.realObject = this.encoding.GetDecoder ();
-
- return this.realObject;
- }
- }
-
- private int codePage;
- private bool isReadOnly;
- private EncoderFallback encoderFallback;
- private DecoderFallback decoderFallback;
- private Encoding realObject;
-
- private MLangCodePageEncoding (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
-
- this.codePage = (int) info.GetValue ("m_codePage", typeof (int));
-
- try {
- this.isReadOnly = (bool) info.GetValue ("m_isReadOnly", typeof (bool));
- this.encoderFallback = (EncoderFallback) info.GetValue ("encoderFallback", typeof (EncoderFallback));
- this.decoderFallback = (DecoderFallback) info.GetValue ("decoderFallback", typeof (DecoderFallback));
- } catch (SerializationException) {
- // .NET Framework 1.x has no fallbacks
- this.isReadOnly = true;
- }
- }
-
- public void GetObjectData (SerializationInfo info, StreamingContext context)
- {
- throw new ArgumentException ("This class cannot be serialized.");
- }
-
- public object GetRealObject (StreamingContext context)
- {
- if (this.realObject == null) {
- Encoding encoding = Encoding.GetEncoding (this.codePage);
-
- if (!this.isReadOnly) {
- encoding = (Encoding) encoding.Clone ();
- encoding.EncoderFallback = this.encoderFallback;
- encoding.DecoderFallback = this.decoderFallback;
- }
- this.realObject = encoding;
- }
-
- return this.realObject;
- }
- }
-}
+++ /dev/null
-//
-// System.Text.SurrogateEncoder.cs
-//
-// Author:
-// Kornél Pál <http://www.kornelpal.hu/>
-//
-// Copyright (C) 2006 Kornél Pál
-//
-
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-//
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
-
-//
-// These proxy classes implement IObjectReference.GetRealObject() that returns
-// an instance of the appropriate Encoding, Encoder or Decoder class.
-// As a result serialized objects of these types will transparently be
-// deserialized to instances of the above described classes.
-//
-// Use SerializationInfo.SetType() in ISerializable.GetObjectData() method of
-// serializable classes to serialize their instances using a proxy class.
-//
-// All of these proxy classes are non-public thus they only have to be
-// serialization compatible with .NET Framework.
-//
-
-//
-// .NET Framework 1.x uses this class for GB18030 encoder and
-// .NET Framework 2.0 can deserialize it using a proxy.
-// This class supports serialization compatibility.
-//
-
-using System;
-using System.Runtime.Serialization;
-
-namespace System.Text
-{
- [Serializable]
- internal sealed class SurrogateEncoder : ISerializable, IObjectReference
- {
- private Encoding encoding;
- private Encoder realObject;
-
- private SurrogateEncoder (SerializationInfo info, StreamingContext context)
- {
- if (info == null)
- throw new ArgumentNullException ("info");
-
- this.encoding = (Encoding) info.GetValue ("m_encoding", typeof (Encoding));
- }
-
- public void GetObjectData (SerializationInfo info, StreamingContext context)
- {
- throw new ArgumentException ("This class cannot be serialized.");
- }
-
- public object GetRealObject (StreamingContext context)
- {
- if (this.realObject == null)
- this.realObject = this.encoding.GetEncoder ();
-
- return this.realObject;
- }
- }
-}
+++ /dev/null
-/*
- * UTF32Encoding.cs - Implementation of the
- * "System.Text.UTF32Encoding" class.
- *
- * Author: Atsushi Enomoto <atsushi@ximian.com>
- *
- * Copyright (C) 2005 Novell, Inc. http://www.novell.com
- *
- * The basic part (now almost nothing) is copied from UnicodeEncoding.cs.
- * Original copyrights goes here:
- *
- * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
- * Copyright (C) 2003, 2004 Novell, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-
-[Serializable]
-public sealed class UTF32Encoding : Encoding
-{
- // Magic numbers used by Windows for UTF32.
- internal const int UTF32_CODE_PAGE = 12000;
- internal const int BIG_UTF32_CODE_PAGE = 12001;
-
- // Internal state.
- private bool bigEndian;
- private bool byteOrderMark;
-
- // Constructors.
- public UTF32Encoding () : this (false, true, false)
- {
- }
-
- public UTF32Encoding (bool bigEndian, bool byteOrderMark)
- : this (bigEndian, byteOrderMark, false)
- {
- }
-
- public UTF32Encoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidCharacters)
- : base ((bigEndian ? BIG_UTF32_CODE_PAGE : UTF32_CODE_PAGE))
- {
- this.bigEndian = bigEndian;
- this.byteOrderMark = byteOrderMark;
-
- if (throwOnInvalidCharacters)
- SetFallbackInternal (EncoderFallback.ExceptionFallback,
- DecoderFallback.ExceptionFallback);
- else
- SetFallbackInternal (new EncoderReplacementFallback ("\uFFFD"),
- new DecoderReplacementFallback ("\uFFFD"));
-
- if (bigEndian){
- body_name = "utf-32BE";
- encoding_name = "UTF-32 (Big-Endian)";
- header_name = "utf-32BE";
- web_name = "utf-32BE";
- } else {
- body_name = "utf-32";
- encoding_name = "UTF-32";
- header_name = "utf-32";
- web_name = "utf-32";
- }
-
- // Windows reports the same code page number for
- // both the little-endian and big-endian forms.
- windows_code_page = UTF32_CODE_PAGE;
- }
-
- // Get the number of bytes needed to encode a character buffer.
- [MonoTODO ("handle fallback")]
- public override int GetByteCount (char[] chars, int index, int count)
- {
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (index < 0 || index > chars.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (chars.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- int ret = 0;
- for (int i = index; i < index + count; i++) {
- if (Char.IsSurrogate (chars [i])) {
- if (i + 1 < chars.Length && Char.IsSurrogate (chars [i + 1]))
- ret += 4;
- else
- // FIXME: handle fallback
-// ret += DecoderFallback.MaxCharCount;
- ret += 4;
- }
- else
- ret += 4;
- }
- return ret;
- }
-
- // Get the bytes that result from encoding a character buffer.
- [MonoTODO ("handle fallback")]
- public override int GetBytes (char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex)
- {
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
- if (charCount < 0 || charCount > (chars.Length - charIndex)) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if ((bytes.Length - byteIndex) < (charCount * 4)) {
- throw new ArgumentException (_("Arg_InsufficientSpace"));
- }
- int posn = byteIndex;
- char ch;
-
- while (charCount-- > 0) {
- ch = chars[charIndex++];
- if (Char.IsSurrogate (ch)) {
- if (charCount-- > 0) {
- int value = 0x400 * (ch - 0xD800) + 0x10000 + chars [charIndex++] - 0xDC00;
- if (bigEndian) {
- for (int i = 0; i < 4; i++) {
- bytes [posn + 3 - i] = (byte) (value % 0x100);
- value >>= 8;
- }
- posn += 4;
- } else {
- for (int i = 0; i < 4; i++) {
- bytes [posn++] = (byte) (value % 0x100);
- value >>= 8;
- }
- }
- } else {
- // Illegal surrogate
- // FIXME: use fallback
- if (bigEndian) {
- bytes[posn++] = 0;
- bytes[posn++] = 0;
- bytes[posn++] = 0;
- bytes[posn++] = (byte) '?';
- } else {
- bytes[posn++] = (byte) '?';
- bytes[posn++] = 0;
- bytes[posn++] = 0;
- bytes[posn++] = 0;
- }
- }
- } else {
- if (bigEndian) {
- bytes[posn++] = 0;
- bytes[posn++] = 0;
- bytes[posn++] = (byte)(ch >> 8);
- bytes[posn++] = (byte)ch;
- } else {
- bytes[posn++] = (byte)ch;
- bytes[posn++] = (byte)(ch >> 8);
- bytes[posn++] = 0;
- bytes[posn++] = 0;
- }
- }
- }
-
- return posn - byteIndex;
- }
-
- // Get the number of characters needed to decode a byte buffer.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (bytes.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- return count / 4;
- }
-
- // Get the characters that result from decoding a byte buffer.
- public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
-
-/*
- // Determine the byte order in the incoming buffer.
- bool isBigEndian;
- if (byteCount >= 2) {
- if (bytes[byteIndex] == (byte)0xFE && bytes[byteIndex + 1] == (byte)0xFF) {
- isBigEndian = true;
- } else if (bytes[byteIndex] == (byte)0xFF && bytes[byteIndex + 1] == (byte)0xFE) {
- isBigEndian = false;
- } else {
- isBigEndian = bigEndian;
- }
- } else {
- isBigEndian = bigEndian;
- }
-*/
-
- // Validate that we have sufficient space in "chars".
- if ((chars.Length - charIndex) < (byteCount / 4)) {
- throw new ArgumentException (_("Arg_InsufficientSpace"));
- }
-
- // Convert the characters.
- int posn = charIndex;
- if (bigEndian) {
- while (byteCount >= 4) {
- chars[posn++] = (char) (
- bytes[byteIndex] << 24 |
- bytes[byteIndex + 1] << 16 |
- bytes[byteIndex + 2] << 8 |
- bytes[byteIndex + 3]);
- byteIndex += 4;
- byteCount -= 4;
- }
- } else {
- while (byteCount >= 4) {
- chars[posn++] = (char) (
- bytes[byteIndex] |
- bytes[byteIndex + 1] << 8 |
- bytes[byteIndex + 2] << 16 |
- bytes[byteIndex + 3] << 24);
- byteIndex += 4;
- byteCount -= 4;
- }
- }
- return posn - charIndex;
- }
-
- // Get the maximum number of bytes needed to encode a
- // specified number of characters.
- public override int GetMaxByteCount (int charCount)
- {
- if (charCount < 0) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
- }
- return charCount * 4;
- }
-
- // Get the maximum number of characters needed to decode a
- // specified number of bytes.
- public override int GetMaxCharCount (int byteCount)
- {
- if (byteCount < 0) {
- throw new ArgumentOutOfRangeException
- ("byteCount", _("ArgRange_NonNegative"));
- }
- return byteCount / 4;
- }
-
- // Get a UTF32-specific decoder that is attached to this instance.
- public override Decoder GetDecoder ()
- {
- return new UTF32Decoder (bigEndian);
- }
-
- // Get the UTF32 preamble.
- public override byte[] GetPreamble ()
- {
- if (byteOrderMark) {
- byte[] preamble = new byte[4];
- if (bigEndian) {
- preamble[2] = (byte)0xFE;
- preamble[3] = (byte)0xFF;
- } else {
- preamble[0] = (byte)0xFF;
- preamble[1] = (byte)0xFE;
- }
- return preamble;
- }
-
- return EmptyArray<byte>.Value;
- }
-
- // Determine if this object is equal to another.
- public override bool Equals (Object value)
- {
- UTF32Encoding enc = (value as UTF32Encoding);
- if (enc != null) {
- return (codePage == enc.codePage &&
- bigEndian == enc.bigEndian &&
- byteOrderMark == enc.byteOrderMark &&
- base.Equals (value));
- } else {
- return false;
- }
- }
-
- // Get the hash code for this object.
- public override int GetHashCode ()
- {
- int basis = base.GetHashCode ();
- if (bigEndian)
- basis ^= 0x1F;
- if (byteOrderMark)
- basis ^= 0x3F;
- return basis;
- }
-
- // UTF32 decoder implementation.
- private sealed class UTF32Decoder : Decoder
- {
- private bool bigEndian;
- private int leftOverByte;
- private int leftOverLength;
-
- // Constructor.
- public UTF32Decoder (bool bigEndian)
- {
- this.bigEndian = bigEndian;
- leftOverByte = -1;
- }
-
- // Override inherited methods.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (bytes.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- if (leftOverByte != -1) {
- return (count + 1) / 4;
- } else {
- return count / 4;
- }
- }
-
- public override int GetChars (byte[] bytes, int byteIndex,
- int byteCount, char[] chars,
- int charIndex)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
-
- // Convert the characters.
- int posn = charIndex;
- int leftOver = leftOverByte;
- int length = chars.Length;
- char ch;
-
- int remain = 4 - leftOverLength;
- if (leftOverLength > 0 && byteCount > remain) {
- if (bigEndian) {
- for (int i = 0; i < remain; i++)
- leftOver += bytes [byteIndex++] << (4 - byteCount--);
- } else {
- for (int i = 0; i < remain; i++)
- leftOver += bytes [byteIndex++] << byteCount--;
- }
- if (leftOver > char.MaxValue && posn + 1 < length
- || posn < length)
- throw new ArgumentException (_("Arg_InsufficientSpace"));
- if (leftOver > char.MaxValue) {
- chars [posn++] = (char) ((leftOver - 10000) / 0x400 + 0xD800);
- chars [posn++] = (char) ((leftOver - 10000) % 0x400 + 0xDC00);
- }
- else
- chars [posn++] = (char) leftOver;
-
- leftOver = -1;
- leftOverLength = 0;
- }
-
- while (byteCount > 3) {
- if (bigEndian) {
- ch = (char) (
- bytes[byteIndex++] << 24 |
- bytes[byteIndex++] << 16 |
- bytes[byteIndex++] << 8 |
- bytes[byteIndex++]);
- } else {
- ch = (char) (
- bytes[byteIndex++] |
- bytes[byteIndex++] << 8 |
- bytes[byteIndex++] << 16 |
- bytes[byteIndex++] << 24);
- }
- byteCount -= 4;
-
- if (posn < length) {
- chars[posn++] = ch;
- } else {
- throw new ArgumentException (_("Arg_InsufficientSpace"));
- }
- }
- if (byteCount > 0) {
- leftOverLength = byteCount;
- leftOver = 0;
- if (bigEndian) {
- for (int i = 0; i < byteCount; i++)
- leftOver += bytes [byteIndex++] << (4 - byteCount--);
- } else {
- for (int i = 0; i < byteCount; i++)
- leftOver += bytes [byteIndex++] << byteCount--;
- }
- leftOverByte = leftOver;
- }
-
- // Finished - return the converted length.
- return posn - charIndex;
- }
-
- } // class UTF32Decoder
-
- [CLSCompliantAttribute(false)]
- public unsafe override int GetByteCount (char *chars, int count)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- return count * 4;
- }
-
- // a bunch of practically missing implementations (but should just work)
-
- public override int GetByteCount (string s)
- {
- return base.GetByteCount (s);
- }
-
- [CLSCompliantAttribute (false)]
- public override unsafe int GetBytes (char *chars, int charCount, byte *bytes, int byteCount)
- {
- return base.GetBytes (chars, charCount, bytes, byteCount);
- }
-
- public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
- {
- return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
- }
-
- [CLSCompliantAttribute (false)]
- public override unsafe int GetCharCount (byte *bytes, int count)
- {
- return base.GetCharCount (bytes, count);
- }
-
- [CLSCompliantAttribute (false)]
- public override unsafe int GetChars (byte *bytes, int byteCount, char* chars, int charCount)
- {
- return base.GetChars (bytes, byteCount, chars, charCount);
- }
-
- public override string GetString (byte [] bytes, int index, int count)
- {
- return base.GetString (bytes, index, count);
- }
-
- public override Encoder GetEncoder ()
- {
- return base.GetEncoder ();
- }
-}; // class UTF32Encoding
-
-}; // namespace System.Text
+++ /dev/null
-/*
- * UTF7Encoding.cs - Implementation of the
- * "System.Text.UTF7Encoding" class.
- *
- * Copyright (c) 2002 Southern Storm Software, Pty Ltd
- * Copyright (c) 2003, 2004, Novell, Inc.
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-using System.Runtime.InteropServices;
-
-[Serializable]
-[ComVisible (true)]
-[MonoLimitation ("Serialization format not compatible with .NET")]
-#if ECMA_COMPAT
-internal
-#else
-public
-#endif
-class UTF7Encoding : Encoding
-{
- // Magic number used by Windows for UTF-7.
- internal const int UTF7_CODE_PAGE = 65000;
-
- // Internal state.
- private bool allowOptionals;
-
- // Encoding rule table for 0x00-0x7F.
- // 0 - full encode, 1 - direct, 2 - optional, 3 - encode plus.
- private static readonly byte[] encodingRules = {
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, // 00
- 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
- 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 3, 1, 1, 1, 1, // 20
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, // 30
-
- 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 2, 2, 2, // 50
- 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 0, 0, // 70
- };
-
- // Characters to use to encode 6-bit values in base64.
- private const String base64Chars =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
-
- // Map bytes in base64 to 6-bit values.
- private static readonly sbyte[] base64Values = {
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, // 20
- 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 30
-
- -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40
- 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 50
- -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 60
- 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 70
-
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 90
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // A0
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // B0
-
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // C0
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // D0
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // E0
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // F0
- };
-
- // Constructors.
- public UTF7Encoding ()
- : this (false)
- {
- }
-
- public UTF7Encoding (bool allowOptionals)
- : base (UTF7_CODE_PAGE)
- {
- this.allowOptionals = allowOptionals;
-
- body_name = "utf-7";
- encoding_name = "Unicode (UTF-7)";
- header_name = "utf-7";
- is_mail_news_display = true;
- is_mail_news_save = true;
- web_name = "utf-7";
- windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
- }
-
- [ComVisible (false)]
- public override int GetHashCode ()
- {
- int basis = base.GetHashCode ();
- return allowOptionals ? -basis : basis;
- }
-
- [ComVisible(false)]
- public override bool Equals (object value)
- {
- UTF7Encoding e = value as UTF7Encoding;
- if (e == null)
- return false;
- return allowOptionals == e.allowOptionals &&
- EncoderFallback.Equals (e.EncoderFallback) &&
- DecoderFallback.Equals (e.DecoderFallback);
- }
-
- // Internal version of "GetByteCount" that can handle
- // a rolling state between calls.
- private static int InternalGetByteCount
- (char[] chars, int index, int count, bool flush,
- int leftOver, bool isInShifted, bool allowOptionals)
- {
- // Validate the parameters.
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (index < 0 || index > chars.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (chars.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
-
- // Determine the length of the output.
- int length = 0;
- int leftOverSize = (leftOver >> 8);
- byte[] rules = encodingRules;
- int ch, rule;
- while (count > 0) {
- ch = (int)(chars[index++]);
- --count;
- if (ch < 0x0080) {
- rule = rules[ch];
- } else {
- rule = 0;
- }
- switch (rule) {
- case 0:
- // Handle characters that must be fully encoded.
- if ( !isInShifted ) {
- ++length;
- leftOverSize = 0;
- isInShifted = true;
- }
- leftOverSize += 16;
- while (leftOverSize >= 6) {
- ++length;
- leftOverSize -= 6;
- }
- break;
- case 1:
- // The character is encoded as itself.
- if (isInShifted) {
- if (leftOverSize != 0) {
- // Flush the previous encoded sequence.
- ++length;
- leftOverSize = 0;
- }
- // Count the "-" (sequence terminator)
- ++length;
- isInShifted = false;
- }
- ++length;
- break;
- case 2:
- // The character may need to be encoded.
- if (allowOptionals) {
- goto case 1;
- } else {
- goto case 0;
- }
- // Not reached.
- case 3:
- // Encode the plus sign as "+-".
- if (isInShifted) {
- if (leftOverSize != 0) {
- // Flush the previous encoded sequence.
- ++length;
- leftOverSize = 0;
- }
- // Count the "-" (sequence terminator)
- ++length;
- isInShifted = false;
- }
- length += 2;
- break;
- }
- }
- if (isInShifted && flush) {
- if (leftOverSize != 0)
- {
- // Flush the previous encoded sequence.
- ++length;
- }
- // Count the "-" (sequence terminator)
- ++length;
- }
-
- // Return the length to the caller.
- return length;
- }
-
- // Get the number of bytes needed to encode a character buffer.
- public override int GetByteCount (char[] chars, int index, int count)
- {
- return InternalGetByteCount (chars, index, count, true, 0, false, allowOptionals);
- }
-
- // Internal version of "GetBytes" that can handle a
- // rolling state between calls.
- private static int InternalGetBytes
- (char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex, bool flush,
- ref int leftOver, ref bool isInShifted, bool allowOptionals)
- {
- // Validate the parameters.
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
- if (charCount < 0 || charCount > (chars.Length - charIndex)) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
-
- // Convert the characters.
- int posn = byteIndex;
- int byteLength = bytes.Length;
- int leftOverSize = (leftOver >> 8);
- int leftOverBits = (leftOver & 0xFF);
- byte[] rules = encodingRules;
- String base64 = base64Chars;
- int ch, rule;
- while (charCount > 0) {
- ch = (int)(chars[charIndex++]);
- --charCount;
- if (ch < 0x0080) {
- rule = rules[ch];
- } else {
- rule = 0;
- }
- switch (rule) {
- case 0:
- // Handle characters that must be fully encoded.
- if (!isInShifted) {
- if (posn >= byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- // Start the sequence
- bytes[posn++] = (byte)'+';
- isInShifted = true;
- leftOverSize = 0;
- }
- leftOverBits = ((leftOverBits << 16) | ch);
- leftOverSize += 16;
- while (leftOverSize >= 6) {
- if (posn >= byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- leftOverSize -= 6;
- bytes[posn++] = (byte)(base64 [leftOverBits >> leftOverSize]);
- leftOverBits &= ((1 << leftOverSize) - 1);
- }
- break;
- case 1:
- // The character is encoded as itself.
- if (isInShifted) {
- if (leftOverSize != 0) {
- // Flush the previous encoded sequence.
- if ((posn + 1) > byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
- }
- if ((posn + 1) > byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- // Terminate the sequence
- bytes[posn++] = (byte)'-';
- isInShifted = false;
- leftOverSize = 0;
- leftOverBits = 0;
- }
- if (posn >= byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)ch;
- break;
- case 2:
- // The character may need to be encoded.
- if (allowOptionals) {
- goto case 1;
- } else {
- goto case 0;
- }
- // Not reached.
- case 3:
- // Encode the plus sign as "+-".
- if (isInShifted) {
- if (leftOverSize != 0) {
- // Flush the previous encoded sequence.
- if ((posn + 1) > byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
- }
- if ((posn + 1) > byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- // Terminate the sequence
- bytes[posn++] = (byte)'-';
- isInShifted = false;
- leftOverSize = 0;
- leftOverBits = 0;
- }
- if ((posn + 2) > byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)'+';
- bytes[posn++] = (byte)'-';
- break;
- }
- }
- if (isInShifted && flush) {
- // Flush the previous encoded sequence.
- if (leftOverSize != 0) {
- if ((posn + 1) > byteLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
- }
- bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
- }
- // Terminate the sequence
- bytes[posn++] = (byte)'-';
- leftOverSize = 0;
- leftOverBits = 0;
- isInShifted = false;
- }
- leftOver = ((leftOverSize << 8) | leftOverBits);
-
- // Return the length to the caller.
- return posn - byteIndex;
- }
-
- // Get the bytes that result from encoding a character buffer.
- public override int GetBytes (char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex)
- {
- int leftOver = 0;
- bool isInShifted = false;
- return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, true,
- ref leftOver, ref isInShifted, allowOptionals);
- }
-
- // Internal version of "GetCharCount" that can handle
- // a rolling state between call.s
- private static int InternalGetCharCount
- (byte[] bytes, int index, int count, int leftOver)
- {
- // Validate the parameters.
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (bytes.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
-
- // Determine the length of the result.
- int length = 0;
- int byteval;
- bool normal = ((leftOver & 0x01000000) == 0);
- bool prevIsPlus = ((leftOver & 0x02000000) != 0);
- int leftOverSize = ((leftOver >> 16) & 0xFF);
- sbyte[] base64 = base64Values;
- while (count > 0) {
- byteval = (int)(bytes[index++]);
- --count;
- if (normal) {
- if (byteval != '+') {
- // Directly-encoded character.
- ++length;
- } else {
- // Start of a base64-encoded character.
- normal = false;
- prevIsPlus = true;
- }
- } else {
- // Process the next byte in a base64 sequence.
- if (byteval == (int)'-') {
- // End of a base64 sequence.
- if (prevIsPlus) {
- ++length;
- }
- leftOverSize = 0;
- normal = true;
- } else if (base64 [byteval] != -1) {
- // Extra character in a base64 sequence.
- leftOverSize += 6;
- if (leftOverSize >= 16) {
- ++length;
- leftOverSize -= 16;
- }
- } else {
- ++length;
- normal = true;
- leftOverSize = 0;
- }
- prevIsPlus = false;
- }
- }
-
- // Return the final length to the caller.
- return length;
- }
-
- // Get the number of characters needed to decode a byte buffer.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- return InternalGetCharCount (bytes, index, count, 0);
- }
-
- // Internal version of "GetChars" that can handle a
- // rolling state between calls.
- private static int InternalGetChars (byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex, ref int leftOver)
- {
- // Validate the parameters.
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
-
- // Convert the bytes into characters.
- int posn = charIndex;
- int charLength = chars.Length;
- int byteval, b64value;
- bool normal = ((leftOver & 0x01000000) == 0);
- bool prevIsPlus = ((leftOver & 0x02000000) != 0);
- bool afterHighSurrogate = ((leftOver & 0x04000000) != 0);
- int leftOverSize = ((leftOver >> 16) & 0xFF);
- int leftOverBits = (leftOver & 0xFFFF);
- sbyte[] base64 = base64Values;
- while (byteCount > 0) {
- byteval = (int)(bytes[byteIndex++]);
- --byteCount;
- if (normal) {
- if (byteval != '+') {
- // Directly-encoded character.
- if (posn >= charLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
- }
- if (afterHighSurrogate) {
- throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
- }
- chars[posn++] = (char)byteval;
- } else {
- // Start of a base64-encoded character.
- normal = false;
- prevIsPlus = true;
- }
- } else {
- // Process the next byte in a base64 sequence.
- if (byteval == (int)'-') {
- // End of a base64 sequence.
- if (prevIsPlus) {
- if (posn >= charLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
- }
- if (afterHighSurrogate) {
- throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
- }
- chars[posn++] = '+';
- }
- // RFC1642 Rule #2
- // When decoding, any bits at the end of the Modified Base64 sequence that
- // do not constitute a complete 16-bit Unicode character are discarded.
- // If such discarded bits are non-zero the sequence is ill-formed.
- normal = true;
- leftOverSize = 0;
- leftOverBits = 0;
- }
- else if ((b64value = base64[byteval]) != -1)
- {
- // Extra character in a base64 sequence.
- leftOverBits = (leftOverBits << 6) | b64value;
- leftOverSize += 6;
- if (leftOverSize >= 16) {
- if (posn >= charLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
- }
- leftOverSize -= 16;
- char nextChar = (char)(leftOverBits >> leftOverSize);
- if ((nextChar & 0xFC00) == 0xD800) {
- afterHighSurrogate = true;
- }
- else if ((nextChar & 0xFC00) == 0xDC00) {
- if (!afterHighSurrogate) {
- throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
- }
- afterHighSurrogate = false;
- }
- chars[posn++] = nextChar;
- leftOverBits &= ((1 << leftOverSize) - 1);
- }
- } else {
- if (posn >= charLength) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
- }
- if (afterHighSurrogate) {
- throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
- }
- chars[posn++] = (char)byteval;
- normal = true;
- leftOverSize = 0;
- leftOverBits = 0;
- }
- prevIsPlus = false;
- }
- }
- leftOver = (leftOverBits | (leftOverSize << 16) |
- (normal ? 0 : 0x01000000) |
- (prevIsPlus ? 0x02000000 : 0) |
- (afterHighSurrogate ? 0x04000000 : 0));
-
- // Return the final length to the caller.
- return posn - charIndex;
- }
-
- // Get the characters that result from decoding a byte buffer.
- public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex)
- {
- int leftOver = 0;
- int amount = InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
- if ((leftOver & 0x04000000) != 0) {
- throw new ArgumentException (_("Arg_InvalidUTF7"), "chars");
- }
- return amount;
- }
-
- // Get the maximum number of bytes needed to encode a
- // specified number of characters.
- public override int GetMaxByteCount (int charCount)
- {
- if (charCount < 0) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
- }
- if (charCount == 0)
- return 0;
- return 8 * (int) (charCount / 3) + (charCount % 3) * 3 + 2;
- }
-
- // Get the maximum number of characters needed to decode a
- // specified number of bytes.
- public override int GetMaxCharCount (int byteCount)
- {
- if (byteCount < 0) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
- }
- return byteCount;
- }
-
- // Get a UTF7-specific decoder that is attached to this instance.
- public override Decoder GetDecoder ()
- {
- return new UTF7Decoder (this);
- }
-
- // Get a UTF7-specific encoder that is attached to this instance.
- public override Encoder GetEncoder ()
- {
- return new UTF7Encoder (allowOptionals, this);
- }
-
- // UTF-7 decoder implementation.
- private sealed class UTF7Decoder : EncodingDecoder
- {
- // Internal state.
- private int leftOver;
-
- // Constructor.
- public UTF7Decoder (Encoding encoding)
- : base (encoding)
- {
- leftOver = 0;
- }
-
- // Override inherited methods.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- return InternalGetCharCount (bytes, index, count, leftOver);
- }
- public override int GetChars (byte[] bytes, int byteIndex,
- int byteCount, char[] chars,
- int charIndex)
- {
- return InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
- }
-
- } // class UTF7Decoder
-
- // UTF-7 encoder implementation.
- private sealed class UTF7Encoder : EncodingEncoder
- {
- private bool allowOptionals;
- private int leftOver = 0;
- private bool isInShifted = false;
-
- // Constructor.
- public UTF7Encoder (bool allowOptionals, UTF7Encoding encoding)
- : base (encoding)
- {
- this.allowOptionals = allowOptionals;
- }
-
- // Override inherited methods.
- public override int GetByteCount (char[] chars, int index,
- int count, bool flush)
- {
- return InternalGetByteCount
- (chars, index, count, flush, leftOver, isInShifted, allowOptionals);
- }
- public override int GetBytes (char[] chars, int charIndex,
- int charCount, byte[] bytes,
- int byteIndex, bool flush)
- {
- return InternalGetBytes (chars, charIndex, charCount,
- bytes, byteIndex, flush,
- ref leftOver, ref isInShifted, allowOptionals);
- }
-
- } // class UTF7Encoder
-
- // a bunch of practically missing implementations (but should just work)
-
- [CLSCompliantAttribute (false)]
- [ComVisible(false)]
- public override unsafe int GetByteCount (char *chars, int count)
- {
- return base.GetByteCount (chars, count);
- }
-
- [ComVisible(false)]
- public override int GetByteCount (string s)
- {
- return base.GetByteCount (s);
- }
-
- [ComVisible(false)]
- [CLSCompliantAttribute (false)]
- public override unsafe int GetBytes (char *chars, int charCount, byte* bytes, int byteCount)
- {
- return base.GetBytes (chars, charCount, bytes, byteCount);
- }
-
- [ComVisible(false)]
- public override int GetBytes (string s, int charIndex, int charCount, byte [] bytes, int byteIndex)
- {
- return base.GetBytes (s, charIndex, charCount, bytes, byteIndex);
- }
-
- [ComVisible(false)]
- [CLSCompliantAttribute (false)]
- public override unsafe int GetCharCount (byte *bytes, int count)
- {
- return base.GetCharCount (bytes, count);
- }
-
- [ComVisible(false)]
- [CLSCompliantAttribute (false)]
- public override unsafe int GetChars (byte* bytes, int byteCount, char* chars, int charCount)
- {
- return base.GetChars (bytes, byteCount, chars, charCount);
- }
-
- [ComVisible(false)]
- public override string GetString (byte [] bytes, int index, int count)
- {
- return base.GetString (bytes, index, count);
- }
-
-}; // class UTF7Encoding
-
-}; // namespace System.Text
+++ /dev/null
-/*
- * UTF8Encoding.cs - Implementation of the "System.Text.UTF8Encoding" class.
- *
- * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
- * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-using System.Runtime.InteropServices;
-
-[Serializable]
-[MonoLimitation ("Serialization format not compatible with .NET")]
-[ComVisible (true)]
-public class UTF8Encoding : Encoding
-{
- // Magic number used by Windows for UTF-8.
- internal const int UTF8_CODE_PAGE = 65001;
-
- // Internal state.
- private bool emitIdentifier;
-
- // Constructors.
- public UTF8Encoding () : this (false, false) {}
- public UTF8Encoding (bool encoderShouldEmitUTF8Identifier)
- : this (encoderShouldEmitUTF8Identifier, false) {}
-
- public UTF8Encoding (bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes)
- : base (UTF8_CODE_PAGE)
- {
- emitIdentifier = encoderShouldEmitUTF8Identifier;
- if (throwOnInvalidBytes)
- SetFallbackInternal (EncoderFallback.ExceptionFallback, DecoderFallback.ExceptionFallback);
- else
- SetFallbackInternal (EncoderFallback.StandardSafeFallback, DecoderFallback.StandardSafeFallback);
-
- web_name = body_name = header_name = "utf-8";
- encoding_name = "Unicode (UTF-8)";
- 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;
- }
-
- #region GetByteCount()
-
- // Internal version of "GetByteCount" which can handle a rolling
- // state between multiple calls to this method.
- private static int InternalGetByteCount (char[] chars, int index, int count, EncoderFallback fallback, ref char leftOver, bool flush)
- {
- // Validate the parameters.
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (index < 0 || index > chars.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (chars.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
-
- if (index == chars.Length) {
- if (flush && leftOver != '\0') {
- // Flush the left-over surrogate pair start.
- leftOver = '\0';
- return 3;
- }
- return 0;
- }
-
- unsafe {
- fixed (char* cptr = chars) {
- return InternalGetByteCount (cptr + index, count, fallback, ref leftOver, flush);
- }
- }
- }
-
- private unsafe static int InternalGetByteCount (char* chars, int count, EncoderFallback fallback, ref char leftOver, bool flush)
- {
- int length = 0;
- char* end = chars + count;
- char* start = chars;
- EncoderFallbackBuffer buffer = null;
- 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.
- char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
- fixed (char *fb_chars = fallback_chars) {
- char dummy = '\0';
- length += InternalGetByteCount (fb_chars, fallback_chars.Length, fallback, ref dummy, true);
- }
-
- leftOver = '\0';
- }
- }
- } else {
- if (*chars >= '\uDC00' && *chars <= '\uDFFF') {
- // We have a correct surrogate pair.
- length += 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.
- char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
- fixed (char *fb_chars = fallback_chars) {
- char dummy = '\0';
- length += InternalGetByteCount (fb_chars, fallback_chars.Length, fallback, ref dummy, true);
- }
- }
- leftOver = '\0';
- }
- }
- if (flush) {
- // Flush the left-over surrogate pair start.
- if (leftOver != '\0') {
- length += 3;
- leftOver = '\0';
- }
- }
- return length;
- }
-
- unsafe static char [] GetFallbackChars (char *chars, char *start, EncoderFallback fallback, ref EncoderFallbackBuffer buffer)
- {
- if (buffer == null)
- buffer = fallback.CreateFallbackBuffer ();
-
- buffer.Fallback (*chars, (int) (chars - start));
-
- char [] fallback_chars = new char [buffer.Remaining];
- for (int i = 0; i < fallback_chars.Length; i++)
- fallback_chars [i] = buffer.GetNextChar ();
-
- buffer.Reset ();
-
- return fallback_chars;
- }
-
- // Get the number of bytes needed to encode a character buffer.
- public override int GetByteCount (char[] chars, int index, int count)
- {
- char dummy = '\0';
- return InternalGetByteCount (chars, index, count, EncoderFallback, ref dummy, true);
- }
-
-
- [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, EncoderFallback, ref dummy, true);
- }
-
- #endregion
-
- #region GetBytes()
-
- // Internal version of "GetBytes" which can handle a rolling
- // state between multiple calls to this method.
- private static int InternalGetBytes (char[] chars, int charIndex,
- int charCount, byte[] bytes,
- int byteIndex,
- EncoderFallback fallback, ref EncoderFallbackBuffer buffer,
- ref char leftOver, bool flush)
- {
- // Validate the parameters.
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
- if (charCount < 0 || charCount > (chars.Length - charIndex)) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
-
- if (charIndex == chars.Length) {
- if (flush && leftOver != '\0') {
- // FIXME: use EncoderFallback.
- //
- // By default it is empty, so I do nothing for now.
- leftOver = '\0';
- }
- return 0;
- }
-
- unsafe {
- fixed (char* cptr = chars) {
- if (bytes.Length == byteIndex)
- return InternalGetBytes (
- cptr + charIndex, charCount,
- null, 0, fallback, ref buffer, ref leftOver, flush);
- fixed (byte *bptr = bytes) {
- return InternalGetBytes (
- cptr + charIndex, charCount,
- bptr + byteIndex, bytes.Length - byteIndex,
- fallback, ref buffer,
- ref leftOver, flush);
- }
- }
- }
- }
-
- private unsafe static int InternalGetBytes (char* chars, int count, byte* bytes, int bcount, EncoderFallback fallback, ref EncoderFallbackBuffer buffer, ref char leftOver, bool flush)
- {
- char* end = chars + count;
- char* start = chars;
- byte* start_bytes = bytes;
- 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.
- char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
- char dummy = '\0';
- if (bytes + InternalGetByteCount (fallback_chars, 0, fallback_chars.Length, fallback, ref dummy, true) > end_bytes)
- goto fail_no_space;
- fixed (char *fb_chars = fallback_chars) {
- bytes += InternalGetBytes (fb_chars, fallback_chars.Length, bytes, bcount - (int) (bytes - start_bytes), fallback, ref buffer, ref dummy, true);
- }
-
- leftOver = '\0';
- }
- }
- } 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.
- char [] fallback_chars = GetFallbackChars (chars, start, fallback, ref buffer);
- char dummy = '\0';
- if (bytes + InternalGetByteCount (fallback_chars, 0, fallback_chars.Length, fallback, ref dummy, true) > end_bytes)
- goto fail_no_space;
- fixed (char *fb_chars = fallback_chars) {
- InternalGetBytes (fb_chars, fallback_chars.Length, bytes, bcount - (int) (bytes - start_bytes), fallback, ref buffer, ref dummy, true);
- }
-
- leftOver = '\0';
- }
- leftOver = '\0';
- }
- }
- if (flush) {
- // 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;
- }
- leftOver = '\0';
- }
- }
- 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.
- public override int GetBytes (char[] chars, int charIndex, int charCount,
- byte[] bytes, int byteIndex)
- {
- char leftOver = '\0';
- EncoderFallbackBuffer buffer = null;
- return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, EncoderFallback, ref buffer, ref leftOver, true);
- }
-
- // Convenience wrappers for "GetBytes".
- public override int GetBytes (String s, int charIndex, int charCount,
- byte[] bytes, int byteIndex)
- {
- // Validate the parameters.
- if (s == null) {
- throw new ArgumentNullException ("s");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (charIndex < 0 || charIndex > s.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
- }
- if (charCount < 0 || charCount > (s.Length - charIndex)) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
-
- if (charIndex == s.Length)
- return 0;
-
- unsafe {
- fixed (char* cptr = s) {
- char dummy = '\0';
- EncoderFallbackBuffer buffer = null;
- if (bytes.Length == byteIndex)
- return InternalGetBytes (
- cptr + charIndex, charCount,
- null, 0, EncoderFallback, ref buffer, ref dummy, true);
- fixed (byte *bptr = bytes) {
- return InternalGetBytes (
- cptr + charIndex, charCount,
- bptr + byteIndex, bytes.Length - byteIndex,
- EncoderFallback, ref buffer,
- ref dummy, true);
- }
- }
- }
- }
-
- [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';
- EncoderFallbackBuffer buffer = null;
- if (byteCount == 0)
- return InternalGetBytes (chars, charCount, null, 0, EncoderFallback, ref buffer, ref dummy, true);
- else
- return InternalGetBytes (chars, charCount, bytes, byteCount, EncoderFallback, ref buffer, ref dummy, true);
- }
-
- #endregion
-
- // Internal version of "GetCharCount" which can handle a rolling
- // state between multiple calls to this method.
- private unsafe static int InternalGetCharCount (
- byte[] bytes, int index, int count, uint leftOverBits,
- uint leftOverCount, object provider,
- ref DecoderFallbackBuffer fallbackBuffer, bool flush)
- {
- // Validate the parameters.
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (bytes.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
-
- if (count == 0)
- return 0;
- fixed (byte *bptr = bytes)
- return InternalGetCharCount (bptr + index, count,
- leftOverBits, leftOverCount, provider, ref fallbackBuffer, flush);
- }
-
- private unsafe static int InternalGetCharCount (
- byte* bytes, int byteCount, uint leftOverBits,
- uint leftOverCount, object provider,
- ref DecoderFallbackBuffer fallbackBuffer, bool flush)
- {
- int byteIndex = 0;
-
- int length = 0;
-
- if (leftOverCount == 0) {
- int end = byteIndex + byteCount;
- for (; byteIndex < end; byteIndex++, byteCount--) {
- if (bytes [byteIndex] < 0x80)
- length++;
- else
- break;
- }
- }
-
- // Determine the number of characters that we have.
- uint ch;
- uint leftBits = leftOverBits;
- uint leftSoFar = (leftOverCount & (uint)0x0F);
- uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
-
- int byteEnd = byteIndex + byteCount;
- for(; byteIndex < byteEnd; byteIndex++) {
- // Fetch the next character from the byte buffer.
- ch = (uint)(bytes[byteIndex]);
- if (leftSize == 0) {
- // Process a UTF-8 start character.
- if (ch < (uint)0x0080) {
- // Single-byte UTF-8 character.
- ++length;
- } else if ((ch & (uint)0xE0) == (uint)0xC0) {
- // Double-byte UTF-8 character.
- leftBits = (ch & (uint)0x1F);
- leftSoFar = 1;
- leftSize = 2;
- } else if ((ch & (uint)0xF0) == (uint)0xE0) {
- // Three-byte UTF-8 character.
- leftBits = (ch & (uint)0x0F);
- leftSoFar = 1;
- leftSize = 3;
- } else if ((ch & (uint)0xF8) == (uint)0xF0) {
- // Four-byte UTF-8 character.
- leftBits = (ch & (uint)0x07);
- leftSoFar = 1;
- leftSize = 4;
- } else if ((ch & (uint)0xFC) == (uint)0xF8) {
- // Five-byte UTF-8 character.
- leftBits = (ch & (uint)0x03);
- leftSoFar = 1;
- leftSize = 5;
- } else if ((ch & (uint)0xFE) == (uint)0xFC) {
- // Six-byte UTF-8 character.
- leftBits = (ch & (uint)0x03);
- leftSoFar = 1;
- leftSize = 6;
- } else {
- // Invalid UTF-8 start character.
- length += Fallback (provider, ref fallbackBuffer, bytes, byteIndex, 1);
- }
- } else {
- // Process an extra byte in a multi-byte sequence.
- if ((ch & (uint)0xC0) == (uint)0x80) {
- leftBits = ((leftBits << 6) | (ch & (uint)0x3F));
- if (++leftSoFar >= leftSize) {
- // We have a complete character now.
- if (leftBits < (uint)0x10000) {
- // is it an overlong ?
- bool overlong = false;
- switch (leftSize) {
- case 2:
- overlong = (leftBits <= 0x7F);
- break;
- case 3:
- overlong = (leftBits <= 0x07FF);
- break;
- case 4:
- overlong = (leftBits <= 0xFFFF);
- break;
- case 5:
- overlong = (leftBits <= 0x1FFFFF);
- break;
- case 6:
- overlong = (leftBits <= 0x03FFFFFF);
- break;
- }
- if (overlong) {
- length += Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar);
- --byteIndex; //process byte again
- }
- else if ((leftBits & 0xF800) == 0xD800) {
- // UTF-8 doesn't use surrogate characters
- length += Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar);
- }
- else
- ++length;
- } else if (leftBits < (uint)0x110000) {
- length += 2;
- } else {
- length += Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar);
- }
- leftSize = 0;
- }
- } else {
- // Invalid UTF-8 sequence: clear and restart.
- length += Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar);
- leftSize = 0;
- --byteIndex;
- }
- }
- }
- if (flush && leftSize != 0) {
- // We had left-over bytes that didn't make up
- // a complete UTF-8 character sequence.
- length += Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar);
- }
-
- // Return the final length to the caller.
- return length;
- }
-
- // for GetCharCount()
- static unsafe int Fallback (object provider, ref DecoderFallbackBuffer buffer, byte* bytes, long index, uint size)
- {
- if (buffer == null) {
- DecoderFallback fb = provider as DecoderFallback;
- if (fb != null)
- buffer = fb.CreateFallbackBuffer ();
- else
- buffer = ((Decoder) provider).FallbackBuffer;
- }
-
- var bufferArg = new byte [size];
-
- for (int i = 0; i < size; i++)
- bufferArg [i] = bytes [(int) index + i];
-
- buffer.Fallback (bufferArg, 0);
- int ret = buffer.Remaining;
- buffer.Reset ();
-
- return ret;
- }
-
- // for GetChars()
- static unsafe void Fallback (object provider, ref DecoderFallbackBuffer buffer, byte* bytes, long byteIndex, uint size,
- char* chars, ref int charIndex)
- {
- if (buffer == null) {
- DecoderFallback fb = provider as DecoderFallback;
- if (fb != null)
- buffer = fb.CreateFallbackBuffer ();
- else
- buffer = ((Decoder) provider).FallbackBuffer;
- }
-
- var bufferArg = new byte [size];
-
- for (int i = 0; i < size; i++)
- bufferArg [i] = bytes [byteIndex + i];
-
- buffer.Fallback (bufferArg, 0);
- while (buffer.Remaining > 0)
- chars [charIndex++] = buffer.GetNextChar ();
- buffer.Reset ();
- }
-
- // Get the number of characters needed to decode a byte buffer.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- DecoderFallbackBuffer buf = null;
- return InternalGetCharCount (bytes, index, count, 0, 0, DecoderFallback, ref buf, true);
- }
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe override int GetCharCount (byte* bytes, int count)
- {
- DecoderFallbackBuffer buf = null;
- return InternalGetCharCount (bytes, count, 0, 0, DecoderFallback, ref buf, true);
- }
-
- // Get the characters that result from decoding a byte buffer.
- 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)
- {
- // Validate the parameters.
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
-
- if (charIndex == chars.Length && byteCount == 0)
- return 0;
-
- fixed (char* cptr = chars) {
- if (byteCount == 0 || byteIndex == bytes.Length)
- return InternalGetChars (null, 0, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, flush);
- // otherwise...
- fixed (byte* bptr = bytes)
- return InternalGetChars (bptr + byteIndex, byteCount, cptr + charIndex, chars.Length - charIndex, ref leftOverBits, ref leftOverCount, provider, ref fallbackBuffer, flush);
- }
- }
-
- private unsafe static int InternalGetChars (
- byte* bytes, int byteCount, char* chars, int charCount,
- ref uint leftOverBits, ref uint leftOverCount,
- object provider,
- ref DecoderFallbackBuffer fallbackBuffer, bool flush)
- {
- int charIndex = 0, byteIndex = 0;
- int length = charCount;
- int posn = charIndex;
-
- if (leftOverCount == 0) {
- int end = byteIndex + byteCount;
- for (; byteIndex < end; posn++, byteIndex++, byteCount--) {
- if (bytes [byteIndex] < 0x80) {
- if (posn >= length) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
- }
- chars [posn] = (char) bytes [byteIndex];
- } else {
- break;
- }
- }
- }
-
- // Convert the bytes into the output buffer.
- uint ch;
- uint leftBits = leftOverBits;
- uint leftSoFar = (leftOverCount & (uint)0x0F);
- uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
-
- int byteEnd = byteIndex + byteCount;
- for(; byteIndex < byteEnd; byteIndex++) {
- // Fetch the next character from the byte buffer.
- ch = (uint)(bytes[byteIndex]);
- if (leftSize == 0) {
- // Process a UTF-8 start character.
- if (ch < (uint)0x0080) {
- // Single-byte UTF-8 character.
- if (posn >= length) {
- throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
- }
- chars[posn++] = (char)ch;
- } else if ((ch & (uint)0xE0) == (uint)0xC0) {
- // Double-byte UTF-8 character.
- leftBits = (ch & (uint)0x1F);
- leftSoFar = 1;
- leftSize = 2;
- } else if ((ch & (uint)0xF0) == (uint)0xE0) {
- // Three-byte UTF-8 character.
- leftBits = (ch & (uint)0x0F);
- leftSoFar = 1;
- leftSize = 3;
- } else if ((ch & (uint)0xF8) == (uint)0xF0) {
- // Four-byte UTF-8 character.
- leftBits = (ch & (uint)0x07);
- leftSoFar = 1;
- leftSize = 4;
- } else if ((ch & (uint)0xFC) == (uint)0xF8) {
- // Five-byte UTF-8 character.
- leftBits = (ch & (uint)0x03);
- leftSoFar = 1;
- leftSize = 5;
- } else if ((ch & (uint)0xFE) == (uint)0xFC) {
- // Six-byte UTF-8 character.
- leftBits = (ch & (uint)0x03);
- leftSoFar = 1;
- leftSize = 6;
- } else {
- // Invalid UTF-8 start character.
- Fallback (provider, ref fallbackBuffer, bytes, byteIndex, 1, chars, ref posn);
- }
- } else {
- // Process an extra byte in a multi-byte sequence.
- if ((ch & (uint)0xC0) == (uint)0x80) {
- leftBits = ((leftBits << 6) | (ch & (uint)0x3F));
- if (++leftSoFar >= leftSize) {
- // We have a complete character now.
- if (leftBits < (uint)0x10000) {
- // is it an overlong ?
- bool overlong = false;
- switch (leftSize) {
- case 2:
- overlong = (leftBits <= 0x7F);
- break;
- case 3:
- overlong = (leftBits <= 0x07FF);
- break;
- case 4:
- overlong = (leftBits <= 0xFFFF);
- break;
- case 5:
- overlong = (leftBits <= 0x1FFFFF);
- break;
- case 6:
- overlong = (leftBits <= 0x03FFFFFF);
- break;
- }
- if (overlong) {
- Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
- --byteIndex; //process byte again
- }
- else if ((leftBits & 0xF800) == 0xD800) {
- // UTF-8 doesn't use surrogate characters
- Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
- }
- else {
- if (posn >= length) {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "chars");
- }
- chars[posn++] = (char)leftBits;
- }
- } else if (leftBits < (uint)0x110000) {
- if ((posn + 2) > length) {
- throw new ArgumentException
- (_("Arg_InsufficientSpace"), "chars");
- }
- leftBits -= (uint)0x10000;
- chars[posn++] = (char)((leftBits >> 10) +
- (uint)0xD800);
- chars[posn++] =
- (char)((leftBits & (uint)0x3FF) + (uint)0xDC00);
- } else {
- Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
- }
- leftSize = 0;
- }
- } else {
- // Invalid UTF-8 sequence: clear and restart.
- Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
- leftSize = 0;
- --byteIndex;
- }
- }
- }
- if (flush && leftSize != 0) {
- // We had left-over bytes that didn't make up
- // a complete UTF-8 character sequence.
- Fallback (provider, ref fallbackBuffer, bytes, byteIndex - leftSoFar, leftSoFar, chars, ref posn);
- }
- leftOverBits = leftBits;
- leftOverCount = (leftSoFar | (leftSize << 4));
-
- // Return the final length to the caller.
- return posn - charIndex;
- }
-
- // Get the characters that result from decoding a byte buffer.
- public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
- char[] chars, int charIndex)
- {
- uint leftOverBits = 0;
- uint leftOverCount = 0;
- DecoderFallbackBuffer buf = null;
- return InternalGetChars (bytes, byteIndex, byteCount, chars,
- charIndex, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, true);
- }
-
- [CLSCompliant (false)]
- [ComVisible (false)]
- public unsafe override int GetChars (byte* bytes, int byteCount, char* chars, int charCount)
- {
- DecoderFallbackBuffer buf = null;
- uint leftOverBits = 0;
- uint leftOverCount = 0;
- return InternalGetChars (bytes, byteCount, chars,
- charCount, ref leftOverBits, ref leftOverCount, DecoderFallback, ref buf, true);
- }
-
- // Get the maximum number of bytes needed to encode a
- // specified number of characters.
- public override int GetMaxByteCount (int charCount)
- {
- if (charCount < 0) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
- }
-
- // Add 1 to charCount since there may be a lead surrogate left from the previous call to GetBytes/Encoder.Convert
- charCount = charCount + 1;
- if (EncoderFallback.MaxCharCount > 1) {
- charCount = charCount * EncoderFallback.MaxCharCount;
- }
-
- return charCount * 3;
- }
-
- // Get the maximum number of characters needed to decode a
- // specified number of bytes.
- public override int GetMaxCharCount (int byteCount)
- {
- if (byteCount < 0) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
- }
-
- // Add 1 to byteCount since there may be the bytes from part of a surrogate pair left from the previous call to GetChars/Decoder.Convert
- int maxCharCount = byteCount + 1;
- if (DecoderFallback.MaxCharCount > 1) {
- maxCharCount = maxCharCount * DecoderFallback.MaxCharCount;
- }
-
- return maxCharCount;
- }
-
- // Get a UTF8-specific decoder that is attached to this instance.
- public override Decoder GetDecoder ()
- {
- return new UTF8Decoder (this);
- }
-
- // Get a UTF8-specific encoder that is attached to this instance.
- public override Encoder GetEncoder ()
- {
- return new UTF8Encoder (this);
- }
-
- // Get the UTF8 preamble.
- public override byte[] GetPreamble ()
- {
- if (emitIdentifier)
- return new byte [] { 0xEF, 0xBB, 0xBF };
-
- return EmptyArray<byte>.Value;
- }
-
- // Determine if this object is equal to another.
- public override bool Equals (Object value)
- {
- UTF8Encoding enc = (value as UTF8Encoding);
- if (enc != null) {
- return (codePage == enc.codePage &&
- emitIdentifier == enc.emitIdentifier &&
- DecoderFallback.Equals (enc.DecoderFallback) &&
- EncoderFallback.Equals (enc.EncoderFallback));
- } else {
- return false;
- }
- }
-
- // Get the hash code for this object.
- public override int GetHashCode ()
- {
- return base.GetHashCode ();
- }
-
- 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);
- }
-
- // UTF-8 decoder implementation.
- [Serializable]
- private class UTF8Decoder : EncodingDecoder
- {
- private uint leftOverBits;
- private uint leftOverCount;
-
- // Constructor.
- public UTF8Decoder (Encoding encoding)
- : base (encoding)
- {
- leftOverBits = 0;
- leftOverCount = 0;
- }
-
- // Override inherited methods.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- DecoderFallbackBuffer buf = null;
- return InternalGetCharCount (bytes, index, count,
- leftOverBits, leftOverCount, this, ref buf, false);
- }
- public override int GetChars (byte[] bytes, int byteIndex,
- int byteCount, char[] chars, int charIndex)
- {
- DecoderFallbackBuffer buf = null;
- return InternalGetChars (bytes, byteIndex, byteCount,
- chars, charIndex, ref leftOverBits, ref leftOverCount, this, ref buf, false);
- }
-
- } // class UTF8Decoder
-
- // UTF-8 encoder implementation.
- [Serializable]
- private class UTF8Encoder : EncodingEncoder
- {
-// private bool emitIdentifier;
- private char leftOverForCount;
- private char leftOverForConv;
-
- // Constructor.
- public UTF8Encoder (UTF8Encoding encoding)
- : base (encoding)
- {
- leftOverForCount = '\0';
- leftOverForConv = '\0';
- }
-
- // Override inherited methods.
- public override int GetByteCount (char[] chars, int index,
- int count, bool flush)
- {
- return InternalGetByteCount (chars, index, count, Fallback, ref leftOverForCount, flush);
- }
- public override int GetBytes (char[] chars, int charIndex,
- int charCount, byte[] bytes, int byteIndex, bool flush)
- {
- int result;
- EncoderFallbackBuffer buffer = null;
- result = InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, Fallback, ref buffer, ref leftOverForConv, flush);
-// emitIdentifier = false;
- return result;
- }
-
- public unsafe override int GetByteCount (char* chars, int count, bool flush)
- {
- return InternalGetByteCount (chars, count, Fallback, ref leftOverForCount, flush);
- }
-
- public unsafe override int GetBytes (char* chars, int charCount,
- byte* bytes, int byteCount, bool flush)
- {
- int result;
- EncoderFallbackBuffer buffer = null;
- result = InternalGetBytes (chars, charCount, bytes, byteCount, Fallback, ref buffer, ref leftOverForConv, flush);
-// emitIdentifier = false;
- return result;
- }
- } // class UTF8Encoder
-
-}; // class UTF8Encoding
-
-}; // namespace System.Text
+++ /dev/null
-/*
- * UnicodeEncoding.cs - Implementation of the
- * "System.Text.UnicodeEncoding" class.
- *
- * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
- * Copyright (C) 2003, 2004 Novell, Inc.
- * Copyright (C) 2006 Kornél Pál <http://www.kornelpal.hu/>
- *
- * Permission is hereby granted, free of charge, to any person obtaining
- * a copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included
- * in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
- * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
- * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
- * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
- * OTHER DEALINGS IN THE SOFTWARE.
- */
-
-namespace System.Text
-{
-
-using System;
-using System.Runtime.InteropServices;
-
-[Serializable]
-[ComVisible (true)]
-[MonoLimitation ("Serialization format not compatible with .NET")]
-public class UnicodeEncoding : Encoding
-{
- // Magic numbers used by Windows for Unicode.
- internal const int UNICODE_CODE_PAGE = 1200;
- internal const int BIG_UNICODE_CODE_PAGE = 1201;
-
-#if !ECMA_COMPAT
- // Size of characters in this encoding.
- public const int CharSize = 2;
-#endif
-
- // Internal state.
- private bool bigEndian;
- private bool byteOrderMark;
-
- // Constructors.
- public UnicodeEncoding () : this (false, true)
- {
- bigEndian = false;
- byteOrderMark = true;
- }
- public UnicodeEncoding (bool bigEndian, bool byteOrderMark)
- : this (bigEndian, byteOrderMark, false)
- {
- }
-
- public UnicodeEncoding (bool bigEndian, bool byteOrderMark, bool throwOnInvalidBytes)
- : base ((bigEndian ? BIG_UNICODE_CODE_PAGE : UNICODE_CODE_PAGE))
- {
- if (throwOnInvalidBytes)
- SetFallbackInternal (null, new DecoderExceptionFallback ());
- else
- SetFallbackInternal (null, new DecoderReplacementFallback ("\uFFFD"));
-
- this.bigEndian = bigEndian;
- this.byteOrderMark = byteOrderMark;
-
- if (bigEndian){
- body_name = "unicodeFFFE";
- encoding_name = "Unicode (Big-Endian)";
- header_name = "unicodeFFFE";
- is_browser_save = false;
- web_name = "unicodeFFFE";
- } else {
- body_name = "utf-16";
- encoding_name = "Unicode";
- header_name = "utf-16";
- is_browser_save = true;
- web_name = "utf-16";
- }
-
- // Windows reports the same code page number for
- // both the little-endian and big-endian forms.
- windows_code_page = UNICODE_CODE_PAGE;
- }
-
- // Get the number of bytes needed to encode a character buffer.
- public override int GetByteCount (char[] chars, int index, int count)
- {
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (index < 0 || index > chars.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (chars.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- return count * 2;
- }
-
- public override int GetByteCount (String s)
- {
- if (s == null) {
- throw new ArgumentNullException ("s");
- }
- return s.Length * 2;
- }
-
- [CLSCompliantAttribute (false)]
- [ComVisible (false)]
- public unsafe override int GetByteCount (char* chars, int count)
- {
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
-
- return count * 2;
- }
-
- // Get the bytes that result from encoding a character buffer.
- public unsafe override int GetBytes (char [] chars, int charIndex, int charCount,
- byte [] bytes, int byteIndex)
- {
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
- if (charCount < 0 || charCount > (chars.Length - charIndex)) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
-
- if (charCount == 0)
- return 0;
-
- int byteCount = bytes.Length - byteIndex;
- if (bytes.Length == 0)
- bytes = new byte [1];
-
- fixed (char* charPtr = chars)
- fixed (byte* bytePtr = bytes)
- return GetBytesInternal (charPtr + charIndex, charCount, bytePtr + byteIndex, byteCount);
- }
-
- public unsafe override int GetBytes (String s, int charIndex, int charCount,
- byte [] bytes, int byteIndex)
- {
- if (s == null) {
- throw new ArgumentNullException ("s");
- }
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (charIndex < 0 || charIndex > s.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
- }
- if (charCount < 0 || charCount > (s.Length - charIndex)) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
-
- // For consistency
- if (charCount == 0)
- return 0;
-
- int byteCount = bytes.Length - byteIndex;
- if (bytes.Length == 0)
- bytes = new byte [1];
-
- fixed (char* charPtr = s)
- fixed (byte* bytePtr = bytes)
- return GetBytesInternal (charPtr + charIndex, charCount, bytePtr + byteIndex, byteCount);
- }
-
- [CLSCompliantAttribute (false)]
- [ComVisible (false)]
- public unsafe override int GetBytes (char* chars, int charCount,
- byte* bytes, int byteCount)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- return GetBytesInternal (chars, charCount, bytes, byteCount);
- }
-
- private unsafe int GetBytesInternal (char* chars, int charCount,
- byte* bytes, int byteCount)
- {
- int count = charCount * 2;
-
- if (byteCount < count)
- throw new ArgumentException (_("Arg_InsufficientSpace"));
-
- CopyChars ((byte*) chars, bytes, count, bigEndian);
- return count;
- }
-
- // Get the number of characters needed to decode a byte buffer.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (bytes.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- return count / 2;
- }
-
- [CLSCompliantAttribute (false)]
- [ComVisible (false)]
- public unsafe override int GetCharCount (byte* bytes, int count)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (count < 0)
- throw new ArgumentOutOfRangeException ("count");
-
- return count / 2;
- }
-
- // Get the characters that result from decoding a byte buffer.
- public unsafe override int GetChars (byte [] bytes, int byteIndex, int byteCount,
- char [] chars, int charIndex)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
-
- if (byteCount == 0)
- return 0;
-
- int charCount = chars.Length - charIndex;
- if (chars.Length == 0)
- chars = new char [1];
-
- fixed (byte* bytePtr = bytes)
- fixed (char* charPtr = chars)
- return GetCharsInternal (bytePtr + byteIndex, byteCount, charPtr + charIndex, charCount);
-}
-
- [CLSCompliantAttribute (false)]
- [ComVisible (false)]
- public unsafe override int GetChars (byte* bytes, int byteCount,
- char* chars, int charCount)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (chars == null)
- throw new ArgumentNullException ("chars");
- if (charCount < 0)
- throw new ArgumentOutOfRangeException ("charCount");
- if (byteCount < 0)
- throw new ArgumentOutOfRangeException ("byteCount");
-
- return GetCharsInternal (bytes, byteCount, chars, charCount);
- }
-
- // Decode a buffer of bytes into a string.
- [ComVisible (false)]
- public unsafe override String GetString (byte [] bytes, int index, int count)
- {
- if (bytes == null)
- throw new ArgumentNullException ("bytes");
- if (index < 0 || index > bytes.Length)
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- if (count < 0 || count > (bytes.Length - index))
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
-
- if (count == 0)
- return string.Empty;
-
- // GetCharCountInternal
- int charCount = count / 2;
- string s = string.InternalAllocateStr (charCount);
-
- fixed (byte* bytePtr = bytes)
- fixed (char* charPtr = s)
- GetCharsInternal (bytePtr + index, count, charPtr, charCount);
-
- return s;
- }
-
- private unsafe int GetCharsInternal (byte* bytes, int byteCount,
- char* chars, int charCount)
- {
- int count = byteCount / 2;
-
- // Validate that we have sufficient space in "chars".
- if (charCount < count)
- throw new ArgumentException (_("Arg_InsufficientSpace"));
-
- CopyChars (bytes, (byte*) chars, byteCount, bigEndian);
- return count;
- }
-
- [ComVisible (false)]
- public override Encoder GetEncoder ()
- {
- return(base.GetEncoder ());
- }
-
- // Get the maximum number of bytes needed to encode a
- // specified number of characters.
- public override int GetMaxByteCount (int charCount)
- {
- if (charCount < 0) {
- throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
- }
- return charCount * 2 + 2;
- }
-
- // Get the maximum number of characters needed to decode a
- // specified number of bytes.
- public override int GetMaxCharCount (int byteCount)
- {
- if (byteCount < 0) {
- throw new ArgumentOutOfRangeException
- ("byteCount", _("ArgRange_NonNegative"));
- }
- return (byteCount + 1) / 2 + 1;
- }
-
- // Get a Unicode-specific decoder that is attached to this instance.
- public override Decoder GetDecoder ()
- {
- return new UnicodeDecoder (bigEndian, this);
- }
-
- // Get the Unicode preamble.
- public override byte[] GetPreamble ()
- {
- if (byteOrderMark) {
- byte[] preamble = new byte[2];
- if (bigEndian) {
- preamble[0] = (byte)0xFE;
- preamble[1] = (byte)0xFF;
- } else {
- preamble[0] = (byte)0xFF;
- preamble[1] = (byte)0xFE;
- }
- return preamble;
- }
-
- return EmptyArray<byte>.Value;
- }
-
- // Determine if this object is equal to another.
- public override bool Equals (Object value)
- {
- UnicodeEncoding enc = (value as UnicodeEncoding);
- if (enc != null) {
- return (codePage == enc.codePage &&
- bigEndian == enc.bigEndian &&
- byteOrderMark == enc.byteOrderMark);
- } else {
- return false;
- }
- }
-
- // Get the hash code for this object.
- public override int GetHashCode ()
- {
- return base.GetHashCode ();
- }
-
- private unsafe static void CopyChars (byte* src, byte* dest, int count, bool bigEndian)
- {
- if (BitConverter.IsLittleEndian != bigEndian) {
- string.memcpy (dest, src, count & unchecked ((int) 0xFFFFFFFE));
- return;
- }
-
- switch (count) {
- case 0:
- return;
- case 1:
- return;
- case 2:
- goto Count2;
- case 3:
- goto Count2;
- case 4:
- goto Count4;
- case 5:
- goto Count4;
- case 6:
- goto Count4;
- case 7:
- goto Count4;
- case 8:
- goto Count8;
- case 9:
- goto Count8;
- case 10:
- goto Count8;
- case 11:
- goto Count8;
- case 12:
- goto Count8;
- case 13:
- goto Count8;
- case 14:
- goto Count8;
- case 15:
- goto Count8;
- }
-
- do {
- dest [0] = src [1];
- dest [1] = src [0];
- dest [2] = src [3];
- dest [3] = src [2];
- dest [4] = src [5];
- dest [5] = src [4];
- dest [6] = src [7];
- dest [7] = src [6];
- dest [8] = src [9];
- dest [9] = src [8];
- dest [10] = src [11];
- dest [11] = src [10];
- dest [12] = src [13];
- dest [13] = src [12];
- dest [14] = src [15];
- dest [15] = src [14];
- dest += 16;
- src += 16;
- count -= 16;
- } while ((count & unchecked ((int) 0xFFFFFFF0)) != 0);
-
- switch (count) {
- case 0:
- return;
- case 1:
- return;
- case 2:
- goto Count2;
- case 3:
- goto Count2;
- case 4:
- goto Count4;
- case 5:
- goto Count4;
- case 6:
- goto Count4;
- case 7:
- goto Count4;
- }
-
- Count8:;
- dest [0] = src [1];
- dest [1] = src [0];
- dest [2] = src [3];
- dest [3] = src [2];
- dest [4] = src [5];
- dest [5] = src [4];
- dest [6] = src [7];
- dest [7] = src [6];
- dest += 8;
- src += 8;
-
- if ((count & 4) == 0)
- goto TestCount2;
- Count4:;
- dest [0] = src [1];
- dest [1] = src [0];
- dest [2] = src [3];
- dest [3] = src [2];
- dest += 4;
- src += 4;
-
- TestCount2:;
- if ((count & 2) == 0)
- return;
- Count2:;
- dest [0] = src [1];
- dest [1] = src [0];
- }
-
- // Unicode decoder implementation.
- private sealed class UnicodeDecoder : EncodingDecoder
- {
- private bool bigEndian;
- private int leftOverByte;
-
- // Constructor.
- public UnicodeDecoder (bool bigEndian, UnicodeEncoding encoding)
- : base (encoding)
- {
- this.bigEndian = bigEndian;
- leftOverByte = -1;
- }
-
- // Override inherited methods.
- public override int GetCharCount (byte[] bytes, int index, int count)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (index < 0 || index > bytes.Length) {
- throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
- }
- if (count < 0 || count > (bytes.Length - index)) {
- throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
- }
- if (leftOverByte != -1) {
- return (count + 1) / 2;
- } else {
- return count / 2;
- }
- }
-
- public unsafe override int GetChars (byte [] bytes, int byteIndex,
- int byteCount, char [] chars,
- int charIndex)
- {
- if (bytes == null) {
- throw new ArgumentNullException ("bytes");
- }
- if (chars == null) {
- throw new ArgumentNullException ("chars");
- }
- if (byteIndex < 0 || byteIndex > bytes.Length) {
- throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
- }
- if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
- throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
- }
- if (charIndex < 0 || charIndex > chars.Length) {
- throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
- }
-
- if (byteCount == 0)
- return 0;
-
- int leftOver = leftOverByte;
- int count;
-
- if (leftOver != -1)
- count = (byteCount + 1) / 2;
- else
- count = byteCount / 2;
-
- if (chars.Length - charIndex < count)
- throw new ArgumentException (_("Arg_InsufficientSpace"));
-
- if (leftOver != -1) {
- if (bigEndian)
- chars [charIndex] = unchecked ((char) ((leftOver << 8) | (int) bytes [byteIndex]));
- else
- chars [charIndex] = unchecked ((char) (((int) bytes [byteIndex] << 8) | leftOver));
- charIndex++;
- byteIndex++;
- byteCount--;
- }
-
- if ((byteCount & unchecked ((int) 0xFFFFFFFE)) != 0)
- fixed (byte* bytePtr = bytes)
- fixed (char* charPtr = chars)
- CopyChars (bytePtr + byteIndex, (byte*) (charPtr + charIndex), byteCount, bigEndian);
-
- if ((byteCount & 1) == 0)
- leftOverByte = -1;
- else
- leftOverByte = bytes [byteCount + byteIndex - 1];
-
- return count;
- }
-
- } // class UnicodeDecoder
-
-}; // class UnicodeEncoding
-
-}; // namespace System.Text
System.Text/NormalizationForm.cs
System.Text/StringBuilder.cs
System.Text/Latin1Encoding.cs
-../../../external/referencesource/mscorlib/system/text/asciiencoding.cs
-../../../external/referencesource/mscorlib/system/text/codepageencoding.cs
-../../../external/referencesource/mscorlib/system/text/decoderbestfitfallback.cs
-../../../external/referencesource/mscorlib/system/text/decoder.cs
-../../../external/referencesource/mscorlib/system/text/decodernls.cs
-../../../external/referencesource/mscorlib/system/text/decoderexceptionfallback.cs
-../../../external/referencesource/mscorlib/system/text/decoderfallback.cs
-../../../external/referencesource/mscorlib/system/text/decoderreplacementfallback.cs
-../../../external/referencesource/mscorlib/system/text/encoderbestfitfallback.cs
-../../../external/referencesource/mscorlib/system/text/encoder.cs
-../../../external/referencesource/mscorlib/system/text/encodernls.cs
-../../../external/referencesource/mscorlib/system/text/encoderexceptionfallback.cs
-../../../external/referencesource/mscorlib/system/text/encoderfallback.cs
-../../../external/referencesource/mscorlib/system/text/encoderreplacementfallback.cs
-../../../external/referencesource/mscorlib/system/text/encoding.cs
-../../../external/referencesource/mscorlib/system/text/encodinginfo.cs
-../../../external/referencesource/mscorlib/system/text/mlangcodepageencoding.cs
-../../../external/referencesource/mscorlib/system/text/surrogateencoder.cs
-../../../external/referencesource/mscorlib/system/text/unicodeencoding.cs
-../../../external/referencesource/mscorlib/system/text/utf32encoding.cs
-../../../external/referencesource/mscorlib/system/text/utf7encoding.cs
-../../../external/referencesource/mscorlib/system/text/utf8encoding.cs
System.Threading/AsyncFlowControl.cs
System.Threading/CompressedStack.cs
System.Threading/ContextCallback.cs
../../../external/referencesource/mscorlib/system/runtime/compilerservices/unsafevaluetypeattribute.cs
../../../external/referencesource/mscorlib/system/runtime/compilerservices/YieldAwaitable.cs
+../../../external/referencesource/mscorlib/system/text/asciiencoding.cs
+../../../external/referencesource/mscorlib/system/text/codepageencoding.cs
+../../../external/referencesource/mscorlib/system/text/decoderbestfitfallback.cs
+../../../external/referencesource/mscorlib/system/text/decoder.cs
+../../../external/referencesource/mscorlib/system/text/decodernls.cs
+../../../external/referencesource/mscorlib/system/text/decoderexceptionfallback.cs
+../../../external/referencesource/mscorlib/system/text/decoderfallback.cs
+../../../external/referencesource/mscorlib/system/text/decoderreplacementfallback.cs
+../../../external/referencesource/mscorlib/system/text/encoderbestfitfallback.cs
+../../../external/referencesource/mscorlib/system/text/encoder.cs
+../../../external/referencesource/mscorlib/system/text/encodernls.cs
+../../../external/referencesource/mscorlib/system/text/encoderexceptionfallback.cs
+../../../external/referencesource/mscorlib/system/text/encoderfallback.cs
+../../../external/referencesource/mscorlib/system/text/encoderreplacementfallback.cs
+../../../external/referencesource/mscorlib/system/text/encoding.cs
+../../../external/referencesource/mscorlib/system/text/encodinginfo.cs
+../../../external/referencesource/mscorlib/system/text/mlangcodepageencoding.cs
+../../../external/referencesource/mscorlib/system/text/surrogateencoder.cs
+../../../external/referencesource/mscorlib/system/text/unicodeencoding.cs
+../../../external/referencesource/mscorlib/system/text/utf32encoding.cs
+../../../external/referencesource/mscorlib/system/text/utf7encoding.cs
+../../../external/referencesource/mscorlib/system/text/utf8encoding.cs
+
../../../external/referencesource/mscorlib/system/text/stringbuildercache.cs
../../../external/referencesource/mscorlib/system/threading/abandonedmutexexception.cs