Пример #1
0
        // returns false if it failed to add required ESC.
        private unsafe void SwitchMode(byte *bytes, ref int byteIndex,
                                       ref int byteCount, ref ISO2022JPMode cur, ISO2022JPMode next)
        {
            if (cur == next)
            {
                return;
            }

            if (byteCount <= 3)
            {
                throw new ArgumentOutOfRangeException("Insufficient byte buffer.");
            }
            bytes [byteIndex++] = 0x1B;
            switch (next)
            {
            case ISO2022JPMode.JISX0201:
                bytes [byteIndex++] = 0x28;
                bytes [byteIndex++] = 0x49;
                break;

            case ISO2022JPMode.JISX0208:
                bytes [byteIndex++] = 0x24;
                bytes [byteIndex++] = 0x42;
                break;

            default:
                bytes [byteIndex++] = 0x28;
                bytes [byteIndex++] = 0x42;
                break;
            }
            cur = next;
        }
Пример #2
0
        private void SwitchMode(byte[] bytes, ref int byteIndex,
                                ref int byteCount, ref ISO2022JPMode cur, ISO2022JPMode next)
        {
            if (cur == next)
            {
                return;
            }

            // If bytes == null we are just counting chars..
            if (bytes == null)
            {
                byteIndex += 3;
                cur        = next;
                return;
            }

            if (byteCount <= 3)
            {
                throw new ArgumentOutOfRangeException("Insufficient byte buffer.");
            }

            bytes[byteIndex++] = 0x1B;
            switch (next)
            {
            case ISO2022JPMode.JISX0201:
                bytes[byteIndex++] = 0x28;
                bytes[byteIndex++] = 0x49;
                break;

            case ISO2022JPMode.JISX0208:
                bytes[byteIndex++] = 0x24;
                bytes[byteIndex++] = 0x42;
                break;

            default:
                bytes[byteIndex++] = 0x28;
                bytes[byteIndex++] = 0x42;
                break;
            }

            cur = next;
        }
Пример #3
0
 public override void Reset()
 {
     m = ISO2022JPMode.ASCII;
     shifted_in_count = shifted_in_conv = false;
 }
Пример #4
0
        public override int GetChars(byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
        {
            int start = charIndex;
            int end   = byteIndex + byteCount;

            for (int i = byteIndex; i < end && charIndex < chars.Length; i++)
            {
                if (allow_shift_io)
                {
                    switch (bytes [i])
                    {
                    case 0x0F:
                        shifted_in_conv = false;
                        continue;

                    case 0x0E:
                        shifted_in_conv = true;
                        continue;
                    }
                }

                if (bytes [i] != 0x1B)
                {
                    if (shifted_in_conv || m == ISO2022JPMode.JISX0201)
                    {
                        // half-kana
                        if (bytes [i] < 0x60)
                        {
                            chars [charIndex++] = (char)(bytes [i] + 0xFF40);
                        }
                        else
                        {
                            // invalid
                            chars [charIndex++] = '?';
                        }
                    }
                    else if (m == ISO2022JPMode.JISX0208)
                    {
                        if (i + 1 == end)
                        {
                            break;                             // incomplete head of wide char
                        }
                        // am so lazy, so reusing jis2sjis
                        int s1 = ((bytes [i] - 1) >> 1) + ((bytes [i] <= 0x5e) ? 0x71 : 0xb1);
                        int s2 = bytes [i + 1] + (((bytes [i] & 1) != 0) ? 0x20 : 0x7e);
                        int v  = (s1 - 0x81) * 0xBC;
                        v += s2 - 0x41;

                        int ch = ToChar(v);
                        if (ch < 0)
                        {
                            chars [charIndex++] = '?';
                        }
                        else
                        {
                            chars [charIndex++] = (char)ch;
                        }
                        i++;
                    }
                    // LAMESPEC: actually this should not
                    // be allowed when 1byte-kana is not
                    // allowed, but MS.NET seems to allow
                    // it in any mode.
                    else if (bytes [i] > 0xA0 && bytes [i] < 0xE0)                     // half-width Katakana
                    {
                        chars [charIndex++] = (char)(bytes [i] - 0xA0 + 0xFF60);
                    }
                    else
                    {
                        chars [charIndex++] = (char)bytes [i];
                    }
                    continue;
                }
                else
                {
                    if (i + 2 >= end)
                    {
                        break;                         // incomplete escape sequence
                    }
                    i++;
                    bool wide = false;
                    if (bytes [i] == 0x24)
                    {
                        wide = true;
                    }
                    else if (bytes [i] == 0x28)
                    {
                        wide = false;
                    }
                    else
                    {
                        chars [charIndex++] = '\x1B';
                        chars [charIndex++] = (char)bytes [i];
                        continue;
                    }
                    i++;
                    if (bytes [i] == 0x42)
                    {
                        m = wide ? ISO2022JPMode.JISX0208 : ISO2022JPMode.ASCII;
                    }
                    else if (bytes [i] == 0x4A)                     // obsoleted
                    {
                        m = ISO2022JPMode.ASCII;
                    }
                    else if (bytes [i] == 0x49)
                    {
                        m = ISO2022JPMode.JISX0201;
                    }
                    else
                    {
                        chars [charIndex++] = '\x1B';
                        chars [charIndex++] = (char)bytes [i - 1];
                        chars [charIndex++] = (char)bytes [i];
                    }
                }
            }

            return(charIndex - start);
        }
Пример #5
0
        // GetCharCount
        public override int GetCharCount(byte [] bytes, int index, int count)
        {
            int ret = 0;

            int end = index + count;

            for (int i = index; i < end; i++)
            {
                if (allow_shift_io)
                {
                    switch (bytes [i])
                    {
                    case 0x0F:
                        shifted_in_count = false;
                        continue;

                    case 0x0E:
                        shifted_in_count = true;
                        continue;
                    }
                }
                if (bytes [i] != 0x1B)
                {
                    if (!shifted_in_count && m == ISO2022JPMode.JISX0208)
                    {
                        if (i + 1 == end)
                        {
                            break;                             // incomplete head of wide char
                        }
                        else
                        {
                            ret++;
                        }
                        i++;                         // 2 byte char
                    }
                    else
                    {
                        ret++;                         // half-kana or ASCII
                    }
                }
                else
                {
                    if (i + 2 >= end)
                    {
                        break;                         // incomplete escape sequence
                    }
                    i++;
                    bool wide = false;
                    if (bytes [i] == 0x24)
                    {
                        wide = true;
                    }
                    else if (bytes [i] == 0x28)
                    {
                        wide = false;
                    }
                    else
                    {
                        ret += 2;
                        continue;
                    }
                    i++;
                    if (bytes [i] == 0x42)
                    {
                        m = wide ? ISO2022JPMode.JISX0208 : ISO2022JPMode.ASCII;
                    }
                    else if (bytes [i] == 0x4A)                     // obsoleted
                    {
                        m = ISO2022JPMode.ASCII;
                    }
                    else if (bytes [i] == 0x49)
                    {
                        m = ISO2022JPMode.JISX0201;
                    }
                    else
                    {
                        ret += 3;
                    }
                }
            }
            return(ret);
        }
Пример #6
0
		public override void Reset ()
		{
			m = ISO2022JPMode.ASCII;
			shifted_in_count = shifted_in_conv = false;
		}
Пример #7
0
		public override int GetChars (byte [] bytes, int byteIndex, int byteCount, char [] chars, int charIndex)
		{
			int start = charIndex;
			int end = byteIndex + byteCount;
			for (int i = byteIndex; i < end && charIndex < chars.Length; i++) {
				if (allow_shift_io) {
					switch (bytes [i]) {
					case 0x0F:
						shifted_in_conv = false;
						continue;
					case 0x0E:
						shifted_in_conv = true;
						continue;
					}
				}

				if (bytes [i] != 0x1B) {
					if (shifted_in_conv || m == ISO2022JPMode.JISX0201) {
						// half-kana
						if (bytes [i] < 0x60)
							chars [charIndex++] = (char) (bytes [i] + 0xFF40);
						else
							// invalid
							chars [charIndex++] = '?';
					}
					else if (m == ISO2022JPMode.JISX0208) {
						if (i + 1 == end)
							break; // incomplete head of wide char

						// am so lazy, so reusing jis2sjis
						int s1 = ((bytes [i] - 1) >> 1) + ((bytes [i] <= 0x5e) ? 0x71 : 0xb1);
						int s2 = bytes [i + 1] + (((bytes [i] & 1) != 0) ? 0x20 : 0x7e);
						int v = (s1 - 0x81) * 0xBC;
						v += s2 - 0x41;

						int ch = ToChar (v);
						if (ch < 0)
							chars [charIndex++] = '?';
						else
							chars [charIndex++] = (char) ch;
						i++;
					}
					// LAMESPEC: actually this should not
					// be allowed when 1byte-kana is not
					// allowed, but MS.NET seems to allow
					// it in any mode.
					else if (bytes [i] > 0xA0 && bytes [i] < 0xE0) // half-width Katakana
						chars [charIndex++] = (char) (bytes [i] - 0xA0 + 0xFF60);
					else
						chars [charIndex++] = (char) bytes [i];
					continue;
				} else {
					if (i + 2 >= end)
						break; // incomplete escape sequence
					i++;
					bool wide = false;
					if (bytes [i] == 0x24)
						wide = true;
					else if (bytes [i] == 0x28)
						wide = false;
					else {
						chars [charIndex++] = '\x1B';
						chars [charIndex++] = (char) bytes [i];
						continue;
					}
					i++;
					if (bytes [i] == 0x42)
						m = wide ? ISO2022JPMode.JISX0208 : ISO2022JPMode.ASCII;
					else if (bytes [i] == 0x4A) // obsoleted
						m = ISO2022JPMode.ASCII;
					else if (bytes [i] == 0x49)
						m = ISO2022JPMode.JISX0201;
					else {
						chars [charIndex++] = '\x1B';
						chars [charIndex++] = (char) bytes [i - 1];
						chars [charIndex++] = (char) bytes [i];
					}
				}
			}

			return charIndex - start;
		}
Пример #8
0
		// GetCharCount
		public override int GetCharCount (byte [] bytes, int index, int count)
		{
			int ret = 0;

			int end = index + count;
			for (int i = index; i < end; i++) {
				if (allow_shift_io) {
					switch (bytes [i]) {
					case 0x0F:
						shifted_in_count = false;
						continue;
					case 0x0E:
						shifted_in_count = true;
						continue;
					}
				}
				if (bytes [i] != 0x1B) {
					if (!shifted_in_count && m == ISO2022JPMode.JISX0208) {
						if (i + 1 == end)
							break; // incomplete head of wide char
						else
							ret++;
						i++; // 2 byte char
					}
					else
						ret++; // half-kana or ASCII
				} else {
					if (i + 2 >= end)
						break; // incomplete escape sequence
					i++;
					bool wide = false;
					if (bytes [i] == 0x24)
						wide = true;
					else if (bytes [i] == 0x28)
						wide = false;
					else {
						ret += 2;
						continue;
					}
					i++;
					if (bytes [i] == 0x42)
						m = wide ? ISO2022JPMode.JISX0208 : ISO2022JPMode.ASCII;
					else if (bytes [i] == 0x4A) // obsoleted
						m = ISO2022JPMode.ASCII;
					else if (bytes [i] == 0x49)
						m = ISO2022JPMode.JISX0201;
					else
						ret += 3;
				}
			}
			return ret;
		}
Пример #9
0
		private void SwitchMode(byte[] bytes, ref int byteIndex,
			ref int byteCount, ref ISO2022JPMode cur, ISO2022JPMode next)
		{
			if (cur == next)
				return;

			// If bytes == null we are just counting chars..
			if (bytes == null)
			{
				byteIndex += 3;
				cur = next;
				return;
			}

			if (byteCount <= 3)
				throw new ArgumentOutOfRangeException("Insufficient byte buffer.");

			bytes[byteIndex++] = 0x1B;
			switch (next)
			{
				case ISO2022JPMode.JISX0201:
					bytes[byteIndex++] = 0x28;
					bytes[byteIndex++] = 0x49;
					break;
				case ISO2022JPMode.JISX0208:
					bytes[byteIndex++] = 0x24;
					bytes[byteIndex++] = 0x42;
					break;
				default:
					bytes[byteIndex++] = 0x28;
					bytes[byteIndex++] = 0x42;
					break;
			}

			cur = next;
		}
Пример #10
0
		// returns false if it failed to add required ESC.
		private unsafe void SwitchMode (byte* bytes, ref int byteIndex,
			ref int byteCount, ref ISO2022JPMode cur, ISO2022JPMode next)
		{
			if (cur == next)
				return;

			if (byteCount <= 3)
				throw new ArgumentOutOfRangeException ("Insufficient byte buffer.");
			bytes [byteIndex++] = 0x1B;
			switch (next) {
			case ISO2022JPMode.JISX0201:
				bytes [byteIndex++] = 0x28;
				bytes [byteIndex++] = 0x49;
				break;
			case ISO2022JPMode.JISX0208:
				bytes [byteIndex++] = 0x24;
				bytes [byteIndex++] = 0x42;
				break;
			default:
				bytes [byteIndex++] = 0x28;
				bytes [byteIndex++] = 0x42;
				break;
			}
			cur = next;
		}
Пример #11
0
		public unsafe override int GetByteCountImpl (char* chars, int charCount, bool flush)
		{
			int charIndex = 0;
			int end = charCount;
			int value;
			int byteCount = 0;

			for (int i = charIndex; i < end; i++) {
				char ch = chars [i];
				// When half-kana is not allowed and it is
				// actually in the input, convert to full width
				// kana.
				if (!allow_1byte_kana &&
					ch >= 0xFF60 && ch <= 0xFFA0)
					ch = full_width_map [ch - 0xFF60];

				if (ch >= 0x2010 && ch <= 0x9FA5)
				{
					if (shifted_in_count) {
						shifted_in_count = false;
						byteCount++; // shift_out
					}
					if (m != ISO2022JPMode.JISX0208)
						byteCount += 3;
					m = ISO2022JPMode.JISX0208;
					// This range contains the bulk of the CJK set.
					value = (ch - 0x2010) * 2;
					value = ((int)(convert.cjkToJis[value])) |
							(((int)(convert.cjkToJis[value + 1])) << 8);
				} else if (ch >= 0xFF01 && ch <= 0xFF60) {
					if (shifted_in_count) {
						shifted_in_count = false;
						byteCount++;
					}
					if (m != ISO2022JPMode.JISX0208)
						byteCount += 3;
					m = ISO2022JPMode.JISX0208;

					// This range contains extra characters,
					value = (ch - 0xFF01) * 2;
					value = ((int)(convert.extraToJis[value])) |
							(((int)(convert.extraToJis[value + 1])) << 8);
				} else if(ch >= 0xFF60 && ch <= 0xFFA0) {
					if (allow_shift_io) {
						if (!shifted_in_count) {
							byteCount++;
							shifted_in_count = true;
						}
					}
					else if (m != ISO2022JPMode.JISX0201) {
						byteCount += 3;
						m = ISO2022JPMode.JISX0201;
					}
					value = ch - 0xFF60 + 0xA0;
				} else if (ch < 128) {
					if (shifted_in_count) {
						shifted_in_count = false;
						byteCount++;
					}
					if (m != ISO2022JPMode.ASCII)
						byteCount += 3;
					m = ISO2022JPMode.ASCII;
					value = (int) ch;
				} else
					// skip non-convertible character
					continue;

				if (value > 0x100)
					byteCount += 2;
				else
					byteCount++;
			}
			// must end in ASCII mode
			if (flush) {
				if (shifted_in_count) {
					shifted_in_count = false;
					byteCount++;
				}
				if (m != ISO2022JPMode.ASCII)
					byteCount += 3;
				m = ISO2022JPMode.ASCII;
			}
			return byteCount;
		}
Пример #12
0
        public unsafe override int GetByteCountImpl(char *chars, int charCount, bool flush)
        {
            int charIndex = 0;
            int end       = charCount;
            int value;
            int byteCount = 0;

            for (int i = charIndex; i < end; i++)
            {
                char ch = chars [i];
                // When half-kana is not allowed and it is
                // actually in the input, convert to full width
                // kana.
                if (!allow_1byte_kana &&
                    ch >= 0xFF60 && ch <= 0xFFA0)
                {
                    ch = full_width_map [ch - 0xFF60];
                }

                if (ch >= 0x2010 && ch <= 0x9FA5)
                {
                    if (shifted_in_count)
                    {
                        shifted_in_count = false;
                        byteCount++;                         // shift_out
                    }
                    if (m != ISO2022JPMode.JISX0208)
                    {
                        byteCount += 3;
                    }
                    m = ISO2022JPMode.JISX0208;
                    // This range contains the bulk of the CJK set.
                    value = (ch - 0x2010) * 2;
                    value = ((int)(convert.cjkToJis[value])) |
                            (((int)(convert.cjkToJis[value + 1])) << 8);
                }
                else if (ch >= 0xFF01 && ch <= 0xFF60)
                {
                    if (shifted_in_count)
                    {
                        shifted_in_count = false;
                        byteCount++;
                    }
                    if (m != ISO2022JPMode.JISX0208)
                    {
                        byteCount += 3;
                    }
                    m = ISO2022JPMode.JISX0208;

                    // This range contains extra characters,
                    value = (ch - 0xFF01) * 2;
                    value = ((int)(convert.extraToJis[value])) |
                            (((int)(convert.extraToJis[value + 1])) << 8);
                }
                else if (ch >= 0xFF60 && ch <= 0xFFA0)
                {
                    if (allow_shift_io)
                    {
                        if (!shifted_in_count)
                        {
                            byteCount++;
                            shifted_in_count = true;
                        }
                    }
                    else if (m != ISO2022JPMode.JISX0201)
                    {
                        byteCount += 3;
                        m          = ISO2022JPMode.JISX0201;
                    }
                    value = ch - 0xFF60 + 0xA0;
                }
                else if (ch < 128)
                {
                    if (shifted_in_count)
                    {
                        shifted_in_count = false;
                        byteCount++;
                    }
                    if (m != ISO2022JPMode.ASCII)
                    {
                        byteCount += 3;
                    }
                    m     = ISO2022JPMode.ASCII;
                    value = (int)ch;
                }
                else
                {
                    // skip non-convertible character
                    continue;
                }

                if (value > 0x100)
                {
                    byteCount += 2;
                }
                else
                {
                    byteCount++;
                }
            }
            // must end in ASCII mode
            if (flush)
            {
                if (shifted_in_count)
                {
                    shifted_in_count = false;
                    byteCount++;
                }
                if (m != ISO2022JPMode.ASCII)
                {
                    byteCount += 3;
                }
                m = ISO2022JPMode.ASCII;
            }
            return(byteCount);
        }