From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id 18065385084B; Fri, 24 Mar 2023 11:52:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 18065385084B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1679658742; bh=fUWmNNBaGN/ODK99e3IHfgZD9XeoE7Mpg5ulhmekKgY=; h=From:To:Subject:Date:From; b=VC1k2Lbu504kbbQom6bjCmVUR+GK181jaunFyO2wbUwDy4BCa8ZQECkWlIjWzmE32 ast3Hwk5YAGuAGakK4nIIWXNhgYOBXCbUemU6CLY2+TO8yjO2hweslu6gu6I0FrfwF zah3aoaU9x+tlNie/qDOyk3a1x6Q8YtJSos1PYZU= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: locales: implement own method to check locale validity X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 15898b95881b1fa504e671e0bcfd653b242effa8 X-Git-Newrev: b5b67a65f87c518b97dbc74e3d20f4654dfa3f10 Message-Id: <20230324115222.18065385084B@sourceware.org> Date: Fri, 24 Mar 2023 11:52:22 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3Db5b67a65f87= c518b97dbc74e3d20f4654dfa3f10 commit b5b67a65f87c518b97dbc74e3d20f4654dfa3f10 Author: Corinna Vinschen AuthorDate: Fri Mar 24 12:43:47 2023 +0100 Commit: Corinna Vinschen CommitDate: Fri Mar 24 12:50:59 2023 +0100 Cygwin: locales: implement own method to check locale validity =20 The Windows function ResolveLocaleName is next to useless to convert a partial locale identifier into a full, supported locale identifier. It converts anything which vaguely resembles a locale into some other locale it supports. =20 Bad examples are: "en-XY" gets converted to "en-US", and worse, "ff-BF" gets converted to "ff-Latn-SN", even though "ff-Adlm-BF" exists! =20 To check if a locale is supported, we have to enumerate all valid Windows locales, and return the match, even if the locale in Windows requires a script. Implement resolve_locale_name() as replacement function for ResolveLocaleName. =20 Fixes: e95a7a795522 ("Cygwin: convert Windows locale handling from LCID= to ISO5646 strings") Signed-off-by: Corinna Vinschen Diff: --- winsup/cygwin/nlsfuncs.cc | 62 +++++++++++++++++++++++++++++++++++++++++++= +++- 1 file changed, 61 insertions(+), 1 deletion(-) diff --git a/winsup/cygwin/nlsfuncs.cc b/winsup/cygwin/nlsfuncs.cc index 34f25034af2b..6e2681c86150 100644 --- a/winsup/cygwin/nlsfuncs.cc +++ b/winsup/cygwin/nlsfuncs.cc @@ -39,6 +39,64 @@ details. */ =20 #define has_modifier(x) ((x)[0] && !strcmp (modifier, (x))) =20 +/* ResolveLocaleName does not what we want. It converts anything which + vaguely resembles a locale into some other locale it supports. Bad + examples are: "en-XY" gets converted to "en-US", and worse, "ff-BF" gets + converted to "ff-Latn-SN", even though "ff-Adlm-BF" exists! Useless. + To check if a locale is supported, we have to enumerate all valid + Windows locales, and return the match, even if the locale in Windows + requires a script. */ +struct res_loc_t { + const wchar_t *search_iso639; + const wchar_t *search_iso3166; + wchar_t *resolved_locale; + int res_len; +}; + +static BOOL +resolve_locale_proc (LPWSTR win_locale, DWORD info, LPARAM param) +{ + res_loc_t *loc =3D (res_loc_t *) param; + wchar_t *iso639, *iso639_end; + wchar_t *iso3166; + + iso639 =3D win_locale; + iso639_end =3D wcschr (iso639, L'-'); + if (!iso639_end) + return TRUE; + if (wcsncmp (loc->search_iso639, iso639, iso639_end - iso639) !=3D 0) + return TRUE; + iso3166 =3D ++iso639_end; + /* Territory is all upper case */ + while (!iswupper (iso3166[0]) || !iswupper (iso3166[1])) + { + iso3166 =3D wcschr (iso3166, L'-'); + if (!iso3166) + return TRUE; + ++iso3166; + } + if (wcsncmp (loc->search_iso3166, iso3166, wcslen (loc->search_iso3166))) + return TRUE; + wcsncat (loc->resolved_locale, win_locale, loc->res_len - 1); + return FALSE; +} + +static int +resolve_locale_name (const wchar_t *search, wchar_t *result, int rlen) +{ + res_loc_t loc; + + loc.search_iso639 =3D search; + loc.search_iso3166 =3D wcschr (search, L'-') + 1; + loc.resolved_locale =3D result; + loc.res_len =3D rlen; + result[0] =3D L'\0'; + EnumSystemLocalesEx (resolve_locale_proc, + LOCALE_WINDOWS | LOCALE_SUPPLEMENTAL, + (LPARAM) &loc, NULL); + return wcslen (result); +} + /* Fetch Windows RFC 5646 locale from POSIX locale specifier. Return values: =20 @@ -106,8 +164,10 @@ __get_rfc5646_from_locale (const char *name, wchar_t *= win_locale) break; } } + /* If resolve_locale_name returns with error, or if it returns a + locale other than the input locale, we don't support this locale. */ if (!wlocale[0] - && ResolveLocaleName (locale, wlocale, ENCODING_LEN + 1) <=3D 1) + && !resolve_locale_name (locale, wlocale, ENCODING_LEN + 1)) { set_errno (ENOENT); return -1;