Update copyright for 2022
[pgsql.git] / src / port / chklocale.c
blob3e777253524d063b3d409be4bc515c7450f112a0
1 /*-------------------------------------------------------------------------
3 * chklocale.c
4 * Functions for handling locale-related info
7 * Copyright (c) 1996-2022, PostgreSQL Global Development Group
10 * IDENTIFICATION
11 * src/port/chklocale.c
13 *-------------------------------------------------------------------------
16 #ifndef FRONTEND
17 #include "postgres.h"
18 #else
19 #include "postgres_fe.h"
20 #endif
22 #ifdef HAVE_LANGINFO_H
23 #include <langinfo.h>
24 #endif
26 #include "mb/pg_wchar.h"
30 * This table needs to recognize all the CODESET spellings for supported
31 * backend encodings, as well as frontend-only encodings where possible
32 * (the latter case is currently only needed for initdb to recognize
33 * error situations). On Windows, we rely on entries for codepage
34 * numbers (CPnnn).
36 * Note that we search the table with pg_strcasecmp(), so variant
37 * capitalizations don't need their own entries.
39 struct encoding_match
41 enum pg_enc pg_enc_code;
42 const char *system_enc_name;
45 static const struct encoding_match encoding_match_list[] = {
46 {PG_EUC_JP, "EUC-JP"},
47 {PG_EUC_JP, "eucJP"},
48 {PG_EUC_JP, "IBM-eucJP"},
49 {PG_EUC_JP, "sdeckanji"},
50 {PG_EUC_JP, "CP20932"},
52 {PG_EUC_CN, "EUC-CN"},
53 {PG_EUC_CN, "eucCN"},
54 {PG_EUC_CN, "IBM-eucCN"},
55 {PG_EUC_CN, "GB2312"},
56 {PG_EUC_CN, "dechanzi"},
57 {PG_EUC_CN, "CP20936"},
59 {PG_EUC_KR, "EUC-KR"},
60 {PG_EUC_KR, "eucKR"},
61 {PG_EUC_KR, "IBM-eucKR"},
62 {PG_EUC_KR, "deckorean"},
63 {PG_EUC_KR, "5601"},
64 {PG_EUC_KR, "CP51949"},
66 {PG_EUC_TW, "EUC-TW"},
67 {PG_EUC_TW, "eucTW"},
68 {PG_EUC_TW, "IBM-eucTW"},
69 {PG_EUC_TW, "cns11643"},
70 /* No codepage for EUC-TW ? */
72 {PG_UTF8, "UTF-8"},
73 {PG_UTF8, "utf8"},
74 {PG_UTF8, "CP65001"},
76 {PG_LATIN1, "ISO-8859-1"},
77 {PG_LATIN1, "ISO8859-1"},
78 {PG_LATIN1, "iso88591"},
79 {PG_LATIN1, "CP28591"},
81 {PG_LATIN2, "ISO-8859-2"},
82 {PG_LATIN2, "ISO8859-2"},
83 {PG_LATIN2, "iso88592"},
84 {PG_LATIN2, "CP28592"},
86 {PG_LATIN3, "ISO-8859-3"},
87 {PG_LATIN3, "ISO8859-3"},
88 {PG_LATIN3, "iso88593"},
89 {PG_LATIN3, "CP28593"},
91 {PG_LATIN4, "ISO-8859-4"},
92 {PG_LATIN4, "ISO8859-4"},
93 {PG_LATIN4, "iso88594"},
94 {PG_LATIN4, "CP28594"},
96 {PG_LATIN5, "ISO-8859-9"},
97 {PG_LATIN5, "ISO8859-9"},
98 {PG_LATIN5, "iso88599"},
99 {PG_LATIN5, "CP28599"},
101 {PG_LATIN6, "ISO-8859-10"},
102 {PG_LATIN6, "ISO8859-10"},
103 {PG_LATIN6, "iso885910"},
105 {PG_LATIN7, "ISO-8859-13"},
106 {PG_LATIN7, "ISO8859-13"},
107 {PG_LATIN7, "iso885913"},
109 {PG_LATIN8, "ISO-8859-14"},
110 {PG_LATIN8, "ISO8859-14"},
111 {PG_LATIN8, "iso885914"},
113 {PG_LATIN9, "ISO-8859-15"},
114 {PG_LATIN9, "ISO8859-15"},
115 {PG_LATIN9, "iso885915"},
116 {PG_LATIN9, "CP28605"},
118 {PG_LATIN10, "ISO-8859-16"},
119 {PG_LATIN10, "ISO8859-16"},
120 {PG_LATIN10, "iso885916"},
122 {PG_KOI8R, "KOI8-R"},
123 {PG_KOI8R, "CP20866"},
125 {PG_KOI8U, "KOI8-U"},
126 {PG_KOI8U, "CP21866"},
128 {PG_WIN866, "CP866"},
129 {PG_WIN874, "CP874"},
130 {PG_WIN1250, "CP1250"},
131 {PG_WIN1251, "CP1251"},
132 {PG_WIN1251, "ansi-1251"},
133 {PG_WIN1252, "CP1252"},
134 {PG_WIN1253, "CP1253"},
135 {PG_WIN1254, "CP1254"},
136 {PG_WIN1255, "CP1255"},
137 {PG_WIN1256, "CP1256"},
138 {PG_WIN1257, "CP1257"},
139 {PG_WIN1258, "CP1258"},
141 {PG_ISO_8859_5, "ISO-8859-5"},
142 {PG_ISO_8859_5, "ISO8859-5"},
143 {PG_ISO_8859_5, "iso88595"},
144 {PG_ISO_8859_5, "CP28595"},
146 {PG_ISO_8859_6, "ISO-8859-6"},
147 {PG_ISO_8859_6, "ISO8859-6"},
148 {PG_ISO_8859_6, "iso88596"},
149 {PG_ISO_8859_6, "CP28596"},
151 {PG_ISO_8859_7, "ISO-8859-7"},
152 {PG_ISO_8859_7, "ISO8859-7"},
153 {PG_ISO_8859_7, "iso88597"},
154 {PG_ISO_8859_7, "CP28597"},
156 {PG_ISO_8859_8, "ISO-8859-8"},
157 {PG_ISO_8859_8, "ISO8859-8"},
158 {PG_ISO_8859_8, "iso88598"},
159 {PG_ISO_8859_8, "CP28598"},
161 {PG_SJIS, "SJIS"},
162 {PG_SJIS, "PCK"},
163 {PG_SJIS, "CP932"},
164 {PG_SJIS, "SHIFT_JIS"},
166 {PG_BIG5, "BIG5"},
167 {PG_BIG5, "BIG5HKSCS"},
168 {PG_BIG5, "Big5-HKSCS"},
169 {PG_BIG5, "CP950"},
171 {PG_GBK, "GBK"},
172 {PG_GBK, "CP936"},
174 {PG_UHC, "UHC"},
175 {PG_UHC, "CP949"},
177 {PG_JOHAB, "JOHAB"},
178 {PG_JOHAB, "CP1361"},
180 {PG_GB18030, "GB18030"},
181 {PG_GB18030, "CP54936"},
183 {PG_SHIFT_JIS_2004, "SJIS_2004"},
185 {PG_SQL_ASCII, "US-ASCII"},
187 {PG_SQL_ASCII, NULL} /* end marker */
190 #ifdef WIN32
192 * On Windows, use CP<code page number> instead of the nl_langinfo() result
194 * Visual Studio 2012 expanded the set of valid LC_CTYPE values, so have its
195 * locale machinery determine the code page. See comments at IsoLocaleName().
196 * For other compilers, follow the locale's predictable format.
198 * Visual Studio 2015 should still be able to do the same, but the declaration
199 * of lc_codepage is missing in _locale_t, causing this code compilation to
200 * fail, hence this falls back instead on GetLocaleInfoEx. VS 2015 may be an
201 * exception and post-VS2015 versions should be able to handle properly the
202 * codepage number using _create_locale(). So, instead of the same logic as
203 * VS 2012 and VS 2013, this routine uses GetLocaleInfoEx to parse short
204 * locale names like "de-DE", "fr-FR", etc. If those cannot be parsed correctly
205 * process falls back to the pre-VS-2010 manual parsing done with
206 * using <Language>_<Country>.<CodePage> as a base.
208 * Returns a malloc()'d string for the caller to free.
210 static char *
211 win32_langinfo(const char *ctype)
213 char *r = NULL;
215 #if defined(_MSC_VER) && (_MSC_VER < 1900)
216 _locale_t loct = NULL;
218 loct = _create_locale(LC_CTYPE, ctype);
219 if (loct != NULL)
221 r = malloc(16); /* excess */
222 if (r != NULL)
223 sprintf(r, "CP%u", loct->locinfo->lc_codepage);
224 _free_locale(loct);
226 #else
227 char *codepage;
229 #if defined(_MSC_VER) && (_MSC_VER >= 1900)
230 uint32 cp;
231 WCHAR wctype[LOCALE_NAME_MAX_LENGTH];
233 memset(wctype, 0, sizeof(wctype));
234 MultiByteToWideChar(CP_ACP, 0, ctype, -1, wctype, LOCALE_NAME_MAX_LENGTH);
236 if (GetLocaleInfoEx(wctype,
237 LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
238 (LPWSTR) &cp, sizeof(cp) / sizeof(WCHAR)) > 0)
240 r = malloc(16); /* excess */
241 if (r != NULL)
244 * If the return value is CP_ACP that means no ANSI code page is
245 * available, so only Unicode can be used for the locale.
247 if (cp == CP_ACP)
248 strcpy(r, "utf8");
249 else
250 sprintf(r, "CP%u", cp);
253 else
254 #endif
257 * Locale format on Win32 is <Language>_<Country>.<CodePage>. For
258 * example, English_United States.1252. If we see digits after the
259 * last dot, assume it's a codepage number. Otherwise, we might be
260 * dealing with a Unix-style locale string; Windows' setlocale() will
261 * take those even though GetLocaleInfoEx() won't, so we end up here.
262 * In that case, just return what's after the last dot and hope we can
263 * find it in our table.
265 codepage = strrchr(ctype, '.');
266 if (codepage != NULL)
268 size_t ln;
270 codepage++;
271 ln = strlen(codepage);
272 r = malloc(ln + 3);
273 if (r != NULL)
275 if (strspn(codepage, "0123456789") == ln)
276 sprintf(r, "CP%s", codepage);
277 else
278 strcpy(r, codepage);
283 #endif
285 return r;
288 #ifndef FRONTEND
290 * Given a Windows code page identifier, find the corresponding PostgreSQL
291 * encoding. Issue a warning and return -1 if none found.
294 pg_codepage_to_encoding(UINT cp)
296 char sys[16];
297 int i;
299 sprintf(sys, "CP%u", cp);
301 /* Check the table */
302 for (i = 0; encoding_match_list[i].system_enc_name; i++)
303 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
304 return encoding_match_list[i].pg_enc_code;
306 ereport(WARNING,
307 (errmsg("could not determine encoding for codeset \"%s\"", sys)));
309 return -1;
311 #endif
312 #endif /* WIN32 */
314 #if (defined(HAVE_LANGINFO_H) && defined(CODESET)) || defined(WIN32)
317 * Given a setting for LC_CTYPE, return the Postgres ID of the associated
318 * encoding, if we can determine it. Return -1 if we can't determine it.
320 * Pass in NULL to get the encoding for the current locale setting.
321 * Pass "" to get the encoding selected by the server's environment.
323 * If the result is PG_SQL_ASCII, callers should treat it as being compatible
324 * with any desired encoding.
326 * If running in the backend and write_message is false, this function must
327 * cope with the possibility that elog() and palloc() are not yet usable.
330 pg_get_encoding_from_locale(const char *ctype, bool write_message)
332 char *sys;
333 int i;
335 /* Get the CODESET property, and also LC_CTYPE if not passed in */
336 if (ctype)
338 char *save;
339 char *name;
341 /* If locale is C or POSIX, we can allow all encodings */
342 if (pg_strcasecmp(ctype, "C") == 0 ||
343 pg_strcasecmp(ctype, "POSIX") == 0)
344 return PG_SQL_ASCII;
346 save = setlocale(LC_CTYPE, NULL);
347 if (!save)
348 return -1; /* setlocale() broken? */
349 /* must copy result, or it might change after setlocale */
350 save = strdup(save);
351 if (!save)
352 return -1; /* out of memory; unlikely */
354 name = setlocale(LC_CTYPE, ctype);
355 if (!name)
357 free(save);
358 return -1; /* bogus ctype passed in? */
361 #ifndef WIN32
362 sys = nl_langinfo(CODESET);
363 if (sys)
364 sys = strdup(sys);
365 #else
366 sys = win32_langinfo(name);
367 #endif
369 setlocale(LC_CTYPE, save);
370 free(save);
372 else
374 /* much easier... */
375 ctype = setlocale(LC_CTYPE, NULL);
376 if (!ctype)
377 return -1; /* setlocale() broken? */
379 /* If locale is C or POSIX, we can allow all encodings */
380 if (pg_strcasecmp(ctype, "C") == 0 ||
381 pg_strcasecmp(ctype, "POSIX") == 0)
382 return PG_SQL_ASCII;
384 #ifndef WIN32
385 sys = nl_langinfo(CODESET);
386 if (sys)
387 sys = strdup(sys);
388 #else
389 sys = win32_langinfo(ctype);
390 #endif
393 if (!sys)
394 return -1; /* out of memory; unlikely */
396 /* Check the table */
397 for (i = 0; encoding_match_list[i].system_enc_name; i++)
399 if (pg_strcasecmp(sys, encoding_match_list[i].system_enc_name) == 0)
401 free(sys);
402 return encoding_match_list[i].pg_enc_code;
406 /* Special-case kluges for particular platforms go here */
408 #ifdef __darwin__
411 * Current macOS has many locales that report an empty string for CODESET,
412 * but they all seem to actually use UTF-8.
414 if (strlen(sys) == 0)
416 free(sys);
417 return PG_UTF8;
419 #endif
422 * We print a warning if we got a CODESET string but couldn't recognize
423 * it. This means we need another entry in the table.
425 if (write_message)
427 #ifdef FRONTEND
428 fprintf(stderr, _("could not determine encoding for locale \"%s\": codeset is \"%s\""),
429 ctype, sys);
430 /* keep newline separate so there's only one translatable string */
431 fputc('\n', stderr);
432 #else
433 ereport(WARNING,
434 (errmsg("could not determine encoding for locale \"%s\": codeset is \"%s\"",
435 ctype, sys)));
436 #endif
439 free(sys);
440 return -1;
442 #else /* (HAVE_LANGINFO_H && CODESET) || WIN32 */
445 * stub if no multi-language platform support
447 * Note: we could return -1 here, but that would have the effect of
448 * forcing users to specify an encoding to initdb on such platforms.
449 * It seems better to silently default to SQL_ASCII.
452 pg_get_encoding_from_locale(const char *ctype, bool write_message)
454 return PG_SQL_ASCII;
457 #endif /* (HAVE_LANGINFO_H && CODESET) || WIN32 */