X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/putty/blobdiff_plain/f7f273099c8798f70611550bfc06b417e5c28ac7..60371ab36cd27a18236d969f0ebdbd2aa73b1705:/unicode.c diff --git a/unicode.c b/unicode.c index e8542211..f59a7007 100644 --- a/unicode.c +++ b/unicode.c @@ -5,9 +5,11 @@ #include #include #include - #include +#include + #include "putty.h" +#include "terminal.h" #include "misc.h" /* Character conversion arrays; they are usually taken from windows, @@ -16,7 +18,7 @@ */ static char **uni_tbl; -static WCHAR unitab_xterm_std[32] = { +static const WCHAR unitab_xterm_std[32] = { 0x2666, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1, 0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba, 0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c, @@ -37,7 +39,7 @@ static WCHAR unitab_xterm_std[32] = { /* XXX: This could be done algorithmically, but I'm not sure it's * worth the hassle -- jtn */ /* ISO/IEC 8859-1:1998 (Latin-1, "Western", "West European") */ -static wchar_t iso_8859_1[] = { +static const wchar_t iso_8859_1[] = { 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, @@ -53,7 +55,7 @@ static wchar_t iso_8859_1[] = { }; /* ISO 8859-2:1999 (Latin-2, "Central European", "East European") */ -static wchar_t iso_8859_2[] = { +static const wchar_t iso_8859_2[] = { 0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7, 0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B, 0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7, @@ -69,7 +71,7 @@ static wchar_t iso_8859_2[] = { }; /* ISO/IEC 8859-3:1999 (Latin-3, "South European", "Maltese & Esperanto") */ -static wchar_t iso_8859_3[] = { +static const wchar_t iso_8859_3[] = { 0x00A0, 0x0126, 0x02D8, 0x00A3, 0x00A4, 0xFFFD, 0x0124, 0x00A7, 0x00A8, 0x0130, 0x015E, 0x011E, 0x0134, 0x00AD, 0xFFFD, 0x017B, 0x00B0, 0x0127, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x0125, 0x00B7, @@ -85,7 +87,7 @@ static wchar_t iso_8859_3[] = { }; /* ISO/IEC 8859-4:1998 (Latin-4, "North European") */ -static wchar_t iso_8859_4[] = { +static const wchar_t iso_8859_4[] = { 0x00A0, 0x0104, 0x0138, 0x0156, 0x00A4, 0x0128, 0x013B, 0x00A7, 0x00A8, 0x0160, 0x0112, 0x0122, 0x0166, 0x00AD, 0x017D, 0x00AF, 0x00B0, 0x0105, 0x02DB, 0x0157, 0x00B4, 0x0129, 0x013C, 0x02C7, @@ -101,7 +103,7 @@ static wchar_t iso_8859_4[] = { }; /* ISO 8859-5:1999 (Latin/Cyrillic) */ -static wchar_t iso_8859_5[] = { +static const wchar_t iso_8859_5[] = { 0x00A0, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0406, 0x0407, 0x0408, 0x0409, 0x040A, 0x040B, 0x040C, 0x00AD, 0x040E, 0x040F, 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416, 0x0417, @@ -117,7 +119,7 @@ static wchar_t iso_8859_5[] = { }; /* ISO 8859-6:1999 (Latin/Arabic) */ -static wchar_t iso_8859_6[] = { +static const wchar_t iso_8859_6[] = { 0x00A0, 0xFFFD, 0xFFFD, 0xFFFD, 0x00A4, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x060C, 0x00AD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, @@ -133,7 +135,7 @@ static wchar_t iso_8859_6[] = { }; /* ISO 8859-7:1987 (Latin/Greek) */ -static wchar_t iso_8859_7[] = { +static const wchar_t iso_8859_7[] = { 0x00A0, 0x2018, 0x2019, 0x00A3, 0xFFFD, 0xFFFD, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0xFFFD, 0x00AB, 0x00AC, 0x00AD, 0xFFFD, 0x2015, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x0384, 0x0385, 0x0386, 0x00B7, @@ -149,7 +151,7 @@ static wchar_t iso_8859_7[] = { }; /* ISO/IEC 8859-8:1999 (Latin/Hebrew) */ -static wchar_t iso_8859_8[] = { +static const wchar_t iso_8859_8[] = { 0x00A0, 0xFFFD, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00D7, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, @@ -165,7 +167,7 @@ static wchar_t iso_8859_8[] = { }; /* ISO/IEC 8859-9:1999 (Latin-5, "Turkish") */ -static wchar_t iso_8859_9[] = { +static const wchar_t iso_8859_9[] = { 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x00A4, 0x00A5, 0x00A6, 0x00A7, 0x00A8, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x00B4, 0x00B5, 0x00B6, 0x00B7, @@ -184,7 +186,7 @@ static wchar_t iso_8859_9[] = { /* Translation table from RDB. unicode.org (ISO/IEC 8859-10:1998) has * U+2015 (HORIZONTAL BAR) at codepoint 0xBD instead * (U+2014 is EM DASH). -- jtn */ -static wchar_t iso_8859_10[] = { +static const wchar_t iso_8859_10[] = { 0x00A0, 0x0104, 0x0112, 0x0122, 0x012A, 0x0128, 0x0136, 0x00A7, 0x013B, 0x0110, 0x0160, 0x0166, 0x017D, 0x00AD, 0x016A, 0x014A, 0x00B0, 0x0105, 0x0113, 0x0123, 0x012B, 0x0129, 0x0137, 0x00B7, @@ -202,7 +204,7 @@ static wchar_t iso_8859_10[] = { /* "ISO 8859-11:1997" ("Thai", "TIS620") */ /* From RDB -- ISO and unicode.org disclaim all knowledge of this one. * Maybe still in draft. --jtn */ -static wchar_t iso_8859_11[] = { +static const wchar_t iso_8859_11[] = { 0x00A0, 0x0E01, 0x0E02, 0x0E03, 0x0E04, 0x0E05, 0x0E06, 0x0E07, 0x0E08, 0x0E09, 0x0E0A, 0x0E0B, 0x0E0C, 0x0E0D, 0x0E0E, 0x0E0F, 0x0E10, 0x0E11, 0x0E12, 0x0E13, 0x0E14, 0x0E15, 0x0E16, 0x0E17, @@ -218,7 +220,7 @@ static wchar_t iso_8859_11[] = { }; /* ISO/IEC 8859-13:1998 (Latin-7, "Baltic Rim") */ -static wchar_t iso_8859_13[] = { +static const wchar_t iso_8859_13[] = { 0x00A0, 0x201D, 0x00A2, 0x00A3, 0x00A4, 0x201E, 0x00A6, 0x00A7, 0x00D8, 0x00A9, 0x0156, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00C6, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x201C, 0x00B5, 0x00B6, 0x00B7, @@ -234,7 +236,7 @@ static wchar_t iso_8859_13[] = { }; /* ISO/IEC 8859-14:1998 (Latin-8, "Celtic", "Gaelic/Welsh") */ -static wchar_t iso_8859_14[] = { +static const wchar_t iso_8859_14[] = { 0x00A0, 0x1E02, 0x1E03, 0x00A3, 0x010A, 0x010B, 0x1E0A, 0x00A7, 0x1E80, 0x00A9, 0x1E82, 0x1E0B, 0x1EF2, 0x00AD, 0x00AE, 0x0178, 0x1E1E, 0x1E1F, 0x0120, 0x0121, 0x1E40, 0x1E41, 0x00B6, 0x1E56, @@ -250,7 +252,7 @@ static wchar_t iso_8859_14[] = { }; /* ISO/IEC 8859-15:1999 (Latin-9 aka -0, "euro") */ -static wchar_t iso_8859_15[] = { +static const wchar_t iso_8859_15[] = { 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0x20AC, 0x00A5, 0x0160, 0x00A7, 0x0161, 0x00A9, 0x00AA, 0x00AB, 0x00AC, 0x00AD, 0x00AE, 0x00AF, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0x017D, 0x00B5, 0x00B6, 0x00B7, @@ -266,7 +268,7 @@ static wchar_t iso_8859_15[] = { }; /* ISO/IEC 8859-16:2001 (Latin-10, "Balkan") */ -static wchar_t iso_8859_16[] = { +static const wchar_t iso_8859_16[] = { 0x00A0, 0x0104, 0x0105, 0x0141, 0x20AC, 0x201E, 0x0160, 0x00A7, 0x0161, 0x00A9, 0x0218, 0x00AB, 0x0179, 0x00AD, 0x017A, 0x017B, 0x00B0, 0x00B1, 0x010C, 0x0142, 0x017D, 0x201D, 0x00B6, 0x00B7, @@ -281,7 +283,7 @@ static wchar_t iso_8859_16[] = { 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF }; -static wchar_t roman8[] = { +static const wchar_t roman8[] = { 0x00A0, 0x00C0, 0x00C2, 0x00C8, 0x00CA, 0x00CB, 0x00CE, 0x00CF, 0x00B4, 0x02CB, 0x02C6, 0x00A8, 0x02DC, 0x00D9, 0x00DB, 0x20A4, 0x00AF, 0x00DD, 0x00FD, 0x00B0, 0x00C7, 0x00E7, 0x00D1, 0x00F1, @@ -296,7 +298,7 @@ static wchar_t roman8[] = { 0x00BD, 0x00AA, 0x00BA, 0x00AB, 0x25A0, 0x00BB, 0x00B1, 0xFFFD }; -static wchar_t koi8_u[] = { +static const wchar_t koi8_u[] = { 0x2500, 0x2502, 0x250C, 0x2510, 0x2514, 0x2518, 0x251C, 0x2524, 0x252C, 0x2534, 0x253C, 0x2580, 0x2584, 0x2588, 0x258C, 0x2590, 0x2591, 0x2592, 0x2593, 0x2320, 0x25A0, 0x2022, 0x221A, 0x2248, @@ -315,7 +317,7 @@ static wchar_t koi8_u[] = { 0x042C, 0x042B, 0x0417, 0x0428, 0x042D, 0x0429, 0x0427, 0x042A }; -static wchar_t vscii[] = { +static const wchar_t vscii[] = { 0x0000, 0x0001, 0x1EB2, 0x0003, 0x0004, 0x1EB4, 0x1EAA, 0x0007, 0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012, 0x0013, 0x1EF6, 0x0015, 0x0016, 0x0017, @@ -350,7 +352,7 @@ static wchar_t vscii[] = { 0x1EE5, 0x00F9, 0x00FA, 0x0169, 0x1EE7, 0x00FD, 0x1EE3, 0x1EEE }; -static wchar_t dec_mcs[] = { +static const wchar_t dec_mcs[] = { 0x00A0, 0x00A1, 0x00A2, 0x00A3, 0xFFFD, 0x00A5, 0xFFFD, 0x00A7, 0x00A4, 0x00A9, 0x00AA, 0x00AB, 0xFFFD, 0xFFFD, 0xFFFD, 0xFFFD, 0x00B0, 0x00B1, 0x00B2, 0x00B3, 0xFFFD, 0x00B5, 0x00B6, 0x00B7, @@ -369,10 +371,10 @@ struct cp_list_item { char *name; int codepage; int cp_size; - wchar_t *cp_table; + const wchar_t *cp_table; }; -static struct cp_list_item cp_list[] = { +static const struct cp_list_item cp_list[] = { {"ISO-8859-1:1998 (Latin-1, West Europe)", 0, 96, iso_8859_1}, {"ISO-8859-2:1999 (Latin-2, East Europe)", 0, 96, iso_8859_2}, {"ISO-8859-3:1999 (Latin-3, South Europe)", 0, 96, iso_8859_3}, @@ -418,7 +420,7 @@ static struct cp_list_item cp_list[] = { static void link_font(WCHAR * line_tbl, WCHAR * font_tbl, WCHAR attr); -void init_ucs_tables(void) +void init_ucs(void) { int i, j; int used_dtf = 0; @@ -597,89 +599,6 @@ static void link_font(WCHAR * line_tbl, WCHAR * font_tbl, WCHAR attr) } } -void lpage_send(int codepage, char *buf, int len, int interactive) -{ - static wchar_t *widebuffer = 0; - static int widesize = 0; - int wclen; - - if (codepage < 0) { - ldisc_send(buf, len, interactive); - return; - } - - if (len > widesize) { - sfree(widebuffer); - widebuffer = smalloc(len * 2 * sizeof(wchar_t)); - widesize = len * 2; - } - - wclen = - MultiByteToWideChar(codepage, 0, buf, len, widebuffer, widesize); - luni_send(widebuffer, wclen, interactive); -} - -void luni_send(wchar_t * widebuf, int len, int interactive) -{ - static char *linebuffer = 0; - static int linesize = 0; - int ratio = (in_utf)?3:1; - int i; - char *p; - - if (len * ratio > linesize) { - sfree(linebuffer); - linebuffer = smalloc(len * ratio * 2 * sizeof(wchar_t)); - linesize = len * ratio * 2; - } - - if (in_utf) { - /* UTF is a simple algorithm */ - for (p = linebuffer, i = 0; i < len; i++) { - wchar_t ch = widebuf[i]; - /* Windows wchar_t is UTF-16 */ - if ((ch&0xF800) == 0xD800) ch = '.'; - - if (ch < 0x80) { - *p++ = (char) (ch); - } else if (ch < 0x800) { - *p++ = (0xC0 | (ch >> 6)); - *p++ = (0x80 | (ch & 0x3F)); - } else { - *p++ = (0xE0 | (ch >> 12)); - *p++ = (0x80 | ((ch >> 6) & 0x3F)); - *p++ = (0x80 | (ch & 0x3F)); - } - } - } else if (!uni_tbl) { - int rv; - rv = WideCharToMultiByte(line_codepage, 0, widebuf, len, - linebuffer, linesize, NULL, NULL); - if (rv >= 0) - p = linebuffer + rv; - else - p = linebuffer; - } else { - /* Others are a lookup in an array */ - for (p = linebuffer, i = 0; i < len; i++) { - wchar_t ch = widebuf[i]; - int by; - char *p1; - if (uni_tbl && (p1 = uni_tbl[(ch >> 8) & 0xFF]) - && (by = p1[ch & 0xFF])) - *p++ = by; - else if (ch < 0x80) - *p++ = (char) ch; -#if 1 - else - *p++ = '.'; -#endif - } - } - if (p > linebuffer) - ldisc_send(linebuffer, p - linebuffer, interactive); -} - wchar_t xlat_uskbd2cyrllic(int ch) { static wchar_t cyrtab[] = { @@ -1063,7 +982,7 @@ int check_compose(int first, int second) int decode_codepage(char *cp_name) { char *s, *d; - struct cp_list_item *cpi; + const struct cp_list_item *cpi; int codepage = -1; CPINFO cpinfo; @@ -1173,7 +1092,7 @@ int decode_codepage(char *cp_name) char *cp_name(int codepage) { - struct cp_list_item *cpi, *cpno; + const struct cp_list_item *cpi, *cpno; static char buf[32]; if (codepage == -1) { @@ -1243,7 +1162,7 @@ void get_unitab(int codepage, wchar_t * unitab, int ftype) for (i = 0; i < max; i++) { tbuf[0] = i; - if (MultiByteToWideChar(codepage, flg, tbuf, 1, unitab + i, 1) + if (mb_to_wc(codepage, flg, tbuf, 1, unitab + i, 1) != 1) unitab[i] = 0xFFFD; } @@ -1255,3 +1174,51 @@ void get_unitab(int codepage, wchar_t * unitab, int ftype) unitab[i] = cp_list[codepage & 0xFFFF].cp_table[i - j]; } } + +int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen, + char *mbstr, int mblen, char *defchr, int *defused) +{ + char *p; + int i; + if (codepage == line_codepage && uni_tbl) { + /* Do this by array lookup if we can. */ + if (wclen < 0) { + for (wclen = 0; wcstr[wclen++] ;); /* will include the NUL */ + } + for (p = mbstr, i = 0; i < wclen; i++) { + wchar_t ch = wcstr[i]; + int by; + char *p1; + if (uni_tbl && (p1 = uni_tbl[(ch >> 8) & 0xFF]) + && (by = p1[ch & 0xFF])) + *p++ = by; + else if (ch < 0x80) + *p++ = (char) ch; + else if (defchr) { + int j; + for (j = 0; defchr[j]; j++) + *p++ = defchr[j]; + if (defused) *defused = 1; + } +#if 1 + else + *p++ = '.'; +#endif + assert(p - mbstr < mblen); + } + return p - mbstr; + } else + return WideCharToMultiByte(codepage, flags, wcstr, wclen, + mbstr, mblen, defchr, defused); +} + +int mb_to_wc(int codepage, int flags, char *mbstr, int mblen, + wchar_t *wcstr, int wclen) +{ + return MultiByteToWideChar(codepage, flags, mbstr, mblen, wcstr, wclen); +} + +int is_dbcs_leadbyte(int codepage, char byte) +{ + return IsDBCSLeadByteEx(codepage, byte); +}