Fallback() public abstract method

public abstract Fallback ( char charUnknownHigh, char charUnknownLow, int index ) : bool
charUnknownHigh char
charUnknownLow char
index int
return bool
Ejemplo n.º 1
0
		public void HandleFallback(ref EncoderFallbackBuffer buffer,
			char[] chars, ref int charIndex, ref int charCount,
			byte[] bytes, ref int byteIndex, ref int byteCount, object state)
		{
			if (buffer == null)
				buffer = EncoderFallback.CreateFallbackBuffer();

			// THIS IS WERE THE BUG IS!! (pruiz)
			if (charCount > 1 && (Char.IsSurrogate(chars[charIndex]) && Char.IsSurrogate(chars[charIndex + 1])))
			{
				buffer.Fallback (chars[charIndex], chars[charIndex + 1], charIndex);
				charIndex++;
				charCount--;
			}
			else
				buffer.Fallback (chars[charIndex], charIndex);

			char[] tmp = new char[buffer.Remaining];
			int idx = 0;
			while (buffer.Remaining > 0)
				tmp[idx++] = buffer.GetNextChar();

			var len = state == null ?
				GetBytes(tmp, 0, tmp.Length, bytes, byteIndex)
				: GetBytesInternal(tmp, 0, tmp.Length, bytes, byteIndex, true, state);
			byteIndex += len;
			byteCount -= len;
		}
Ejemplo n.º 2
0
		public unsafe void HandleFallback (ref EncoderFallbackBuffer buffer,
			char* chars, ref int charIndex, ref int charCount,
			byte* bytes, ref int byteIndex, ref int byteCount, object state)
		{
			if (buffer == null)
				buffer = EncoderFallback.CreateFallbackBuffer ();

			if (charCount > 1 && (Char.IsSurrogate (chars [charIndex]) && Char.IsSurrogate (chars [charIndex + 1]))) {
				buffer.Fallback (chars [charIndex], chars [charIndex + 1], charIndex);
				charIndex++;
				charCount--;
			}
			else
				buffer.Fallback (chars [charIndex], charIndex);
			char [] tmp = new char [buffer.Remaining];
			int idx = 0;
			while (buffer.Remaining > 0)
				tmp [idx++] = buffer.GetNextChar ();

			fixed (char* tmparr = tmp) {
				var outbytes = bytes == null ? null : bytes + byteIndex;
				var len = state == null ?
					GetBytes(tmparr, tmp.Length, outbytes, byteCount)
					: GetBytesInternal(tmparr, tmp.Length, outbytes, byteCount, true, state);

				byteIndex += len;
				byteCount -= len;
			}
		}
Ejemplo n.º 3
0
		public unsafe void HandleFallback (ref EncoderFallbackBuffer buffer,
			char* chars, ref int charIndex, ref int charCount,
			byte* bytes, ref int byteIndex, ref int byteCount)
		{
			if (buffer == null)
				buffer = EncoderFallback.CreateFallbackBuffer ();
			if (Char.IsSurrogate (chars [charIndex]) && charCount > 0 &&
				Char.IsSurrogate (chars [charIndex + 1])) {
				buffer.Fallback (chars [charIndex], chars [charIndex + 1], charIndex);
				charIndex++;
				charCount--;
			}
			else
				buffer.Fallback (chars [charIndex], charIndex);
			char [] tmp = new char [buffer.Remaining];
			int idx = 0;
			while (buffer.Remaining > 0)
				tmp [idx++] = buffer.GetNextChar ();
			fixed (char* tmparr = tmp) {
				byteIndex += GetBytes (tmparr, tmp.Length, bytes + byteIndex, byteCount);
			}
		}
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
	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;
	}
	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;
	}
Ejemplo n.º 8
0
        int GetBytes(char[] chars, int charIndex, int charCount,
                     byte[] bytes, int byteIndex,
                     ref EncoderFallbackBuffer buffer,
                     ref char [] fallback_chars)
        {
#endif
            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)
            {
                throw new ArgumentException(_("Arg_InsufficientSpace"));
            }
            int  count = charCount;
            char ch;
            while (count-- > 0)
            {
                ch = chars [charIndex++];
                if (ch < (char)0x0100)
                {
                    bytes [byteIndex++] = (byte)ch;
                }
                else if (ch >= '\uFF01' && ch <= '\uFF5E')
                {
                    bytes [byteIndex++] = (byte)(ch - 0xFEE0);
                }
                else
                {
#if NET_2_0
                    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);
#else
                    bytes [byteIndex++] = (byte)'?';
#endif
                }
            }
            return(charCount);
        }
Ejemplo n.º 9
0
	int GetBytes (char[] chars, int charIndex, int charCount,
		      byte[] bytes, int byteIndex,
		      ref EncoderFallbackBuffer buffer,
		      ref char [] fallback_chars)
	{
#endif
		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) {
			throw new ArgumentException (_("Arg_InsufficientSpace"));
		}
		int count = charCount;
		char ch;
		while (count-- > 0) {
			ch = chars [charIndex++];
			if (ch < (char)0x0100) {
				bytes [byteIndex++] = (byte)ch;
			} else if (ch >= '\uFF01' && ch <= '\uFF5E') {
				bytes [byteIndex++] = (byte)(ch - 0xFEE0);
			} else {
#if NET_2_0
				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);
#else
				bytes [byteIndex++] = (byte)'?';
#endif
			}
		}
		return charCount;
	}
Ejemplo n.º 10
0
        private 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");
            }
            if (bytes == null)
            {
                throw new ArgumentNullException("bytes");
            }
            if (charIndex < 0 || charIndex > chars.Length)
            {
                throw new ArgumentOutOfRangeException("charIndex", Encoding._("ArgRange_StringIndex"));
            }
            if (charCount < 0 || charCount > chars.Length - charIndex)
            {
                throw new ArgumentOutOfRangeException("charCount", Encoding._("ArgRange_StringRange"));
            }
            if (byteIndex < 0 || byteIndex > bytes.Length)
            {
                throw new ArgumentOutOfRangeException("byteIndex", Encoding._("ArgRange_Array"));
            }
            if (bytes.Length - byteIndex < charCount)
            {
                throw new ArgumentException(Encoding._("Arg_InsufficientSpace"));
            }
            int num = charCount;

            while (num-- > 0)
            {
                char c = chars[charIndex++];
                if (c < '\u0080')
                {
                    bytes[byteIndex++] = (byte)c;
                }
                else
                {
                    if (buffer == null)
                    {
                        buffer = base.EncoderFallback.CreateFallbackBuffer();
                    }
                    if (char.IsSurrogate(c) && num > 1 && char.IsSurrogate(chars[charIndex]))
                    {
                        buffer.Fallback(c, chars[charIndex], charIndex++ - 1);
                    }
                    else
                    {
                        buffer.Fallback(c, 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 += this.GetBytes(fallback_chars, 0, fallback_chars.Length, bytes, byteIndex, ref buffer, ref fallback_chars);
                }
            }
            return(charCount);
        }
Ejemplo n.º 11
0
	// This function is called when we want to flush the decoder state
	// (i.e. in case of invalid UTF-16 characters or dangling surrogates)
	internal unsafe static EncoderStatus InternalGetBytesFlush (
		byte* bytes, int byteCount,
		EncoderFallbackBuffer fallbackBuffer,
		int charsProcessed, ref int bytesProcessed,
		ref uint leftChar)
	{
		int t_charsProcessed, t_bytesProcessed;

		// in normal circumstances fallbackBuffer never is null, except
		// when we have called InternalGetBytes from this function
		// (for avoiding infinite recursive calls)
		if (fallbackBuffer == null)
			return EncoderStatus.Ok;

		// if there is nothing to flush, then return silently
		if(leftChar == 0)
			return EncoderStatus.Ok;

		// invalid UTF-16 or invalid surrogate
		fallbackBuffer.Fallback ((char) leftChar, charsProcessed - 1);
		// if we've arrived here we are working in replacement mode:
		// build a replacement fallback_chars buffer
		char[] fallback_chars = new char [fallbackBuffer.Remaining];
		for (int i = 0; i < fallback_chars.Length; i++)
			fallback_chars [i] = fallbackBuffer.GetNextChar ();
		fallbackBuffer.Reset ();
		// and encode it into UTF8 bytes...
		fixed (char *fb_chars = fallback_chars) {
			leftChar = 0;
			switch (bytes != null
				? InternalGetBytes (
						fb_chars, fallback_chars.Length,
						bytes + bytesProcessed, byteCount - bytesProcessed,
						null, out t_charsProcessed, out t_bytesProcessed,
						ref leftChar,
						true)
				: InternalGetBytes (
						fb_chars, fallback_chars.Length,
						null, byteCount,
						null, out t_charsProcessed, out t_bytesProcessed,
						ref leftChar,
						true)) {
			case EncoderStatus.Ok:
				// everything OK :D
				bytesProcessed += t_bytesProcessed;
				break;
			case EncoderStatus.InsufficientSpace:
				return EncoderStatus.InsufficientSpace;
			case EncoderStatus.InputRunOut:
			case EncoderStatus.InvalidChar:
			case EncoderStatus.InvalidSurrogate:
				throw new ArgumentException ("Fallback chars are pure evil.", "fallback buffer bytes");
			}
		}
		// flush encoder state
		leftChar = 0;
		return EncoderStatus.Ok;
	}