Fix leak of 'fname' introduced by the rewrite of write_random_seed in
[sgt/putty] / windows / winucs.c
index 2412e40..cd50983 100644 (file)
@@ -390,6 +390,8 @@ struct cp_list_item {
 };
 
 static const struct cp_list_item cp_list[] = {
+    {"UTF-8", CP_UTF8},
+
     {"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},
@@ -406,8 +408,6 @@ static const struct cp_list_item cp_list[] = {
     {"ISO-8859-15:1999 (Latin-9, \"euro\")", 0, 96, iso_8859_15},
     {"ISO-8859-16:2001 (Latin-10, Balkan)", 0, 96, iso_8859_16},
 
-    {"UTF-8", CP_UTF8},
-
     {"KOI8-U", 0, 128, koi8_u},
     {"KOI8-R", 20866},
     {"HP-ROMAN8", 0, 96, roman8},
@@ -427,6 +427,7 @@ static const struct cp_list_item cp_list[] = {
     {"CP437", 437},
     {"CP620 (Mazovia)", 0, 128, mazovia},
     {"CP819", 28591},
+    {"CP852", 852},
     {"CP878", 20866},
 
     {"Use font encoding", -1},
@@ -436,24 +437,27 @@ static const struct cp_list_item cp_list[] = {
 
 static void link_font(WCHAR * line_tbl, WCHAR * font_tbl, WCHAR attr);
 
-void init_ucs(Config *cfg, struct unicode_data *ucsdata)
+void init_ucs(Conf *conf, struct unicode_data *ucsdata)
 {
     int i, j;
     int used_dtf = 0;
     char tbuf[256];
+    int vtmode;
 
     for (i = 0; i < 256; i++)
        tbuf[i] = i;
 
     /* Decide on the Line and Font codepages */
-    ucsdata->line_codepage = decode_codepage(cfg->line_codepage);
+    ucsdata->line_codepage = decode_codepage(conf_get_str(conf,
+                                                         CONF_line_codepage));
 
     if (ucsdata->font_codepage <= 0) { 
        ucsdata->font_codepage=0; 
        ucsdata->dbcs_screenfont=0; 
     }
 
-    if (cfg->vtmode == VT_OEMONLY) {
+    vtmode = conf_get_int(conf, CONF_vtmode);
+    if (vtmode == VT_OEMONLY) {
        ucsdata->font_codepage = 437;
        ucsdata->dbcs_screenfont = 0;
        if (ucsdata->line_codepage <= 0)
@@ -473,7 +477,7 @@ void init_ucs(Config *cfg, struct unicode_data *ucsdata)
        if (ucsdata->font_codepage == 437)
            ucsdata->unitab_font[0] = ucsdata->unitab_font[255] = 0xFFFF;
     }
-    if (cfg->vtmode == VT_XWINDOWS)
+    if (vtmode == VT_XWINDOWS)
        memcpy(ucsdata->unitab_font + 1, unitab_xterm_std,
               sizeof(unitab_xterm_std));
 
@@ -481,7 +485,7 @@ void init_ucs(Config *cfg, struct unicode_data *ucsdata)
     get_unitab(CP_OEMCP, ucsdata->unitab_oemcp, 1);
 
     /* Collect CP437 ucs table for SCO acs */
-    if (cfg->vtmode == VT_OEMANSI || cfg->vtmode == VT_XWINDOWS)
+    if (vtmode == VT_OEMANSI || vtmode == VT_XWINDOWS)
        memcpy(ucsdata->unitab_scoacs, ucsdata->unitab_oemcp,
               sizeof(ucsdata->unitab_scoacs));
     else
@@ -490,7 +494,7 @@ void init_ucs(Config *cfg, struct unicode_data *ucsdata)
     /* Collect line set ucs table */
     if (ucsdata->line_codepage == ucsdata->font_codepage &&
        (ucsdata->dbcs_screenfont ||
-        cfg->vtmode == VT_POORMAN || ucsdata->font_codepage==0)) {
+        vtmode == VT_POORMAN || ucsdata->font_codepage==0)) {
 
        /* For DBCS and POOR fonts force direct to font */
        used_dtf = 1;
@@ -560,14 +564,14 @@ void init_ucs(Config *cfg, struct unicode_data *ucsdata)
            ucsdata->unitab_ctrl[i] = 0xFF;
 
     /* Generate line->screen direct conversion links. */
-    if (cfg->vtmode == VT_OEMANSI || cfg->vtmode == VT_XWINDOWS)
+    if (vtmode == VT_OEMANSI || vtmode == VT_XWINDOWS)
        link_font(ucsdata->unitab_scoacs, ucsdata->unitab_oemcp, CSET_OEMCP);
 
     link_font(ucsdata->unitab_line, ucsdata->unitab_font, CSET_ACP);
     link_font(ucsdata->unitab_scoacs, ucsdata->unitab_font, CSET_ACP);
     link_font(ucsdata->unitab_xterm, ucsdata->unitab_font, CSET_ACP);
 
-    if (cfg->vtmode == VT_OEMANSI || cfg->vtmode == VT_XWINDOWS) {
+    if (vtmode == VT_OEMANSI || vtmode == VT_XWINDOWS) {
        link_font(ucsdata->unitab_line, ucsdata->unitab_oemcp, CSET_OEMCP);
        link_font(ucsdata->unitab_xterm, ucsdata->unitab_oemcp, CSET_OEMCP);
     }
@@ -581,7 +585,7 @@ void init_ucs(Config *cfg, struct unicode_data *ucsdata)
     }
 
     /* Last chance, if !unicode then try poorman links. */
-    if (cfg->vtmode != VT_UNICODE) {
+    if (vtmode != VT_UNICODE) {
        static const char poorman_scoacs[] = 
            "CueaaaaceeeiiiAAE**ooouuyOUc$YPsaiounNao?++**!<>###||||++||++++++--|-+||++--|-+----++++++++##||#aBTPEsyt******EN=+><++-=... n2* ";
        static const char poorman_latin1[] =
@@ -1012,48 +1016,8 @@ int decode_codepage(char *cp_name)
     int codepage = -1;
     CPINFO cpinfo;
 
-    if (!*cp_name) {
-       /*
-        * Here we select a plausible default code page based on
-        * the locale the user is in. We wish to select an ISO code
-        * page or appropriate local default _rather_ than go with
-        * the Win125* series, because it's more important to have
-        * CSI and friends enabled by default than the ghastly
-        * Windows extra quote characters, and because it's more
-        * likely the user is connecting to a remote server that
-        * does something Unixy or VMSy and hence standards-
-        * compliant than that they're connecting back to a Windows
-        * box using horrible nonstandard charsets.
-        * 
-        * Accordingly, Robert de Bath suggests a method for
-        * picking a default character set that runs as follows:
-        * first call GetACP to get the system's ANSI code page
-        * identifier, and translate as follows:
-        * 
-        * 1250 -> ISO 8859-2
-        * 1251 -> KOI8-U
-        * 1252 -> ISO 8859-1
-        * 1253 -> ISO 8859-7
-        * 1254 -> ISO 8859-9
-        * 1255 -> ISO 8859-8
-        * 1256 -> ISO 8859-6
-        * 1257 -> ISO 8859-13 (changed from 8859-4 on advice of a Lithuanian)
-        * 
-        * and for anything else, choose direct-to-font.
-        */
-       int cp = GetACP();
-       switch (cp) {
-         case 1250: cp_name = "ISO-8859-2"; break;
-         case 1251: cp_name = "KOI8-U"; break;
-         case 1252: cp_name = "ISO-8859-1"; break;
-         case 1253: cp_name = "ISO-8859-7"; break;
-         case 1254: cp_name = "ISO-8859-9"; break;
-         case 1255: cp_name = "ISO-8859-8"; break;
-         case 1256: cp_name = "ISO-8859-6"; break;
-         case 1257: cp_name = "ISO-8859-13"; break;
-           /* default: leave it blank, which will select -1, direct->font */
-       }
-    }
+    if (!*cp_name)
+        return CP_UTF8;                /* default */
 
     if (cp_name && *cp_name)
        for (cpi = cp_list; cpi->name; cpi++) {
@@ -1088,7 +1052,7 @@ int decode_codepage(char *cp_name)
        if (tolower(d[0]) == 'c' && tolower(d[1]) == 'p')
            d += 2;
        if (tolower(d[0]) == 'i' && tolower(d[1]) == 'b'
-           && tolower(d[1]) == 'm')
+           && tolower(d[2]) == 'm')
            d += 3;
        for (s = d; *s >= '0' && *s <= '9'; s++);
        if (*s == 0 && s != d)
@@ -1201,7 +1165,7 @@ void get_unitab(int codepage, wchar_t * unitab, int ftype)
     }
 }
 
-int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
+int wc_to_mb(int codepage, int flags, const wchar_t *wcstr, int wclen,
             char *mbstr, int mblen, char *defchr, int *defused,
             struct unicode_data *ucsdata)
 {
@@ -1239,7 +1203,7 @@ int wc_to_mb(int codepage, int flags, wchar_t *wcstr, int wclen,
                                   mbstr, mblen, defchr, defused);
 }
 
-int mb_to_wc(int codepage, int flags, char *mbstr, int mblen,
+int mb_to_wc(int codepage, int flags, const char *mbstr, int mblen,
             wchar_t *wcstr, int wclen)
 {
     return MultiByteToWideChar(codepage, flags, mbstr, mblen, wcstr, wclen);