winegstreamer: Move the "flip" field to struct wg_parser_stream.
[wine/zf.git] / dlls / kernelbase / locale.c
blobc60b796aa486b824734c716fd6ffe252facc9c16
1 /*
2 * Locale support
4 * Copyright 1995 Martin von Loewis
5 * Copyright 1998 David Lee Lambert
6 * Copyright 2000 Julio César Gázquez
7 * Copyright 2003 Jon Griffiths
8 * Copyright 2005 Dmitry Timoshkov
9 * Copyright 2002, 2019 Alexandre Julliard
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include <stdarg.h>
27 #include <stdlib.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #define WINNORMALIZEAPI
32 #include "windef.h"
33 #include "winbase.h"
34 #include "winreg.h"
35 #include "winnls.h"
36 #include "winuser.h"
37 #include "winternl.h"
38 #include "kernelbase.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(nls);
43 #define CALINFO_MAX_YEAR 2029
45 extern UINT CDECL __wine_get_unix_codepage(void);
47 extern const unsigned int collation_table[] DECLSPEC_HIDDEN;
49 static HANDLE kernel32_handle;
51 static const struct registry_value
53 DWORD lctype;
54 const WCHAR *name;
55 } registry_values[] =
57 { LOCALE_ICALENDARTYPE, L"iCalendarType" },
58 { LOCALE_ICURRDIGITS, L"iCurrDigits" },
59 { LOCALE_ICURRENCY, L"iCurrency" },
60 { LOCALE_IDIGITS, L"iDigits" },
61 { LOCALE_IFIRSTDAYOFWEEK, L"iFirstDayOfWeek" },
62 { LOCALE_IFIRSTWEEKOFYEAR, L"iFirstWeekOfYear" },
63 { LOCALE_ILZERO, L"iLZero" },
64 { LOCALE_IMEASURE, L"iMeasure" },
65 { LOCALE_INEGCURR, L"iNegCurr" },
66 { LOCALE_INEGNUMBER, L"iNegNumber" },
67 { LOCALE_IPAPERSIZE, L"iPaperSize" },
68 { LOCALE_ITIME, L"iTime" },
69 { LOCALE_S1159, L"s1159" },
70 { LOCALE_S2359, L"s2359" },
71 { LOCALE_SCURRENCY, L"sCurrency" },
72 { LOCALE_SDATE, L"sDate" },
73 { LOCALE_SDECIMAL, L"sDecimal" },
74 { LOCALE_SGROUPING, L"sGrouping" },
75 { LOCALE_SLIST, L"sList" },
76 { LOCALE_SLONGDATE, L"sLongDate" },
77 { LOCALE_SMONDECIMALSEP, L"sMonDecimalSep" },
78 { LOCALE_SMONGROUPING, L"sMonGrouping" },
79 { LOCALE_SMONTHOUSANDSEP, L"sMonThousandSep" },
80 { LOCALE_SNEGATIVESIGN, L"sNegativeSign" },
81 { LOCALE_SPOSITIVESIGN, L"sPositiveSign" },
82 { LOCALE_SSHORTDATE, L"sShortDate" },
83 { LOCALE_STHOUSAND, L"sThousand" },
84 { LOCALE_STIME, L"sTime" },
85 { LOCALE_STIMEFORMAT, L"sTimeFormat" },
86 { LOCALE_SYEARMONTH, L"sYearMonth" },
87 /* The following are not listed under MSDN as supported,
88 * but seem to be used and also stored in the registry.
90 { LOCALE_SNAME, L"LocaleName" },
91 { LOCALE_ICOUNTRY, L"iCountry" },
92 { LOCALE_IDATE, L"iDate" },
93 { LOCALE_ILDATE, L"iLDate" },
94 { LOCALE_ITLZERO, L"iTLZero" },
95 { LOCALE_SCOUNTRY, L"sCountry" },
96 { LOCALE_SABBREVLANGNAME, L"sLanguage" },
97 { LOCALE_IDIGITSUBSTITUTION, L"Numshape" },
98 { LOCALE_SNATIVEDIGITS, L"sNativeDigits" },
99 { LOCALE_ITIMEMARKPOSN, L"iTimePrefix" },
102 static WCHAR *registry_cache[ARRAY_SIZE(registry_values)];
104 static const struct { UINT cp; const WCHAR *name; } codepage_names[] =
106 { 37, L"IBM EBCDIC US Canada" },
107 { 424, L"IBM EBCDIC Hebrew" },
108 { 437, L"OEM United States" },
109 { 500, L"IBM EBCDIC International" },
110 { 708, L"Arabic ASMO" },
111 { 737, L"OEM Greek 437G" },
112 { 775, L"OEM Baltic" },
113 { 850, L"OEM Multilingual Latin 1" },
114 { 852, L"OEM Slovak Latin 2" },
115 { 855, L"OEM Cyrillic" },
116 { 856, L"Hebrew PC" },
117 { 857, L"OEM Turkish" },
118 { 860, L"OEM Portuguese" },
119 { 861, L"OEM Icelandic" },
120 { 862, L"OEM Hebrew" },
121 { 863, L"OEM Canadian French" },
122 { 864, L"OEM Arabic" },
123 { 865, L"OEM Nordic" },
124 { 866, L"OEM Russian" },
125 { 869, L"OEM Greek" },
126 { 874, L"ANSI/OEM Thai" },
127 { 875, L"IBM EBCDIC Greek" },
128 { 878, L"Russian KOI8" },
129 { 932, L"ANSI/OEM Japanese Shift-JIS" },
130 { 936, L"ANSI/OEM Simplified Chinese GBK" },
131 { 949, L"ANSI/OEM Korean Unified Hangul" },
132 { 950, L"ANSI/OEM Traditional Chinese Big5" },
133 { 1006, L"IBM Arabic" },
134 { 1026, L"IBM EBCDIC Latin 5 Turkish" },
135 { 1250, L"ANSI Eastern Europe" },
136 { 1251, L"ANSI Cyrillic" },
137 { 1252, L"ANSI Latin 1" },
138 { 1253, L"ANSI Greek" },
139 { 1254, L"ANSI Turkish" },
140 { 1255, L"ANSI Hebrew" },
141 { 1256, L"ANSI Arabic" },
142 { 1257, L"ANSI Baltic" },
143 { 1258, L"ANSI/OEM Viet Nam" },
144 { 1361, L"Korean Johab" },
145 { 10000, L"Mac Roman" },
146 { 10001, L"Mac Japanese" },
147 { 10002, L"Mac Traditional Chinese" },
148 { 10003, L"Mac Korean" },
149 { 10004, L"Mac Arabic" },
150 { 10005, L"Mac Hebrew" },
151 { 10006, L"Mac Greek" },
152 { 10007, L"Mac Cyrillic" },
153 { 10008, L"Mac Simplified Chinese" },
154 { 10010, L"Mac Romanian" },
155 { 10017, L"Mac Ukrainian" },
156 { 10021, L"Mac Thai" },
157 { 10029, L"Mac Latin 2" },
158 { 10079, L"Mac Icelandic" },
159 { 10081, L"Mac Turkish" },
160 { 10082, L"Mac Croatian" },
161 { 20127, L"US-ASCII (7bit)" },
162 { 20866, L"Russian KOI8" },
163 { 20932, L"EUC-JP" },
164 { 21866, L"Ukrainian KOI8" },
165 { 28591, L"ISO 8859-1 Latin 1" },
166 { 28592, L"ISO 8859-2 Latin 2 (East European)" },
167 { 28593, L"ISO 8859-3 Latin 3 (South European)" },
168 { 28594, L"ISO 8859-4 Latin 4 (Baltic old)" },
169 { 28595, L"ISO 8859-5 Cyrillic" },
170 { 28596, L"ISO 8859-6 Arabic" },
171 { 28597, L"ISO 8859-7 Greek" },
172 { 28598, L"ISO 8859-8 Hebrew" },
173 { 28599, L"ISO 8859-9 Latin 5 (Turkish)" },
174 { 28600, L"ISO 8859-10 Latin 6 (Nordic)" },
175 { 28601, L"ISO 8859-11 Latin (Thai)" },
176 { 28603, L"ISO 8859-13 Latin 7 (Baltic)" },
177 { 28604, L"ISO 8859-14 Latin 8 (Celtic)" },
178 { 28605, L"ISO 8859-15 Latin 9 (Euro)" },
179 { 28606, L"ISO 8859-16 Latin 10 (Balkan)" },
180 { 65000, L"Unicode (UTF-7)" },
181 { 65001, L"Unicode (UTF-8)" }
184 /* Unicode expanded ligatures */
185 static const WCHAR ligatures[][5] =
187 { 0x00c6, 'A','E',0 },
188 { 0x00de, 'T','H',0 },
189 { 0x00df, 's','s',0 },
190 { 0x00e6, 'a','e',0 },
191 { 0x00fe, 't','h',0 },
192 { 0x0132, 'I','J',0 },
193 { 0x0133, 'i','j',0 },
194 { 0x0152, 'O','E',0 },
195 { 0x0153, 'o','e',0 },
196 { 0x01c4, 'D',0x017d,0 },
197 { 0x01c5, 'D',0x017e,0 },
198 { 0x01c6, 'd',0x017e,0 },
199 { 0x01c7, 'L','J',0 },
200 { 0x01c8, 'L','j',0 },
201 { 0x01c9, 'l','j',0 },
202 { 0x01ca, 'N','J',0 },
203 { 0x01cb, 'N','j',0 },
204 { 0x01cc, 'n','j',0 },
205 { 0x01e2, 0x0100,0x0112,0 },
206 { 0x01e3, 0x0101,0x0113,0 },
207 { 0x01f1, 'D','Z',0 },
208 { 0x01f2, 'D','z',0 },
209 { 0x01f3, 'd','z',0 },
210 { 0x01fc, 0x00c1,0x00c9,0 },
211 { 0x01fd, 0x00e1,0x00e9,0 },
212 { 0x05f0, 0x05d5,0x05d5,0 },
213 { 0x05f1, 0x05d5,0x05d9,0 },
214 { 0x05f2, 0x05d9,0x05d9,0 },
215 { 0xfb00, 'f','f',0 },
216 { 0xfb01, 'f','i',0 },
217 { 0xfb02, 'f','l',0 },
218 { 0xfb03, 'f','f','i',0 },
219 { 0xfb04, 'f','f','l',0 },
220 { 0xfb05, 0x017f,'t',0 },
221 { 0xfb06, 's','t',0 },
224 enum locationkind { LOCATION_NATION = 0, LOCATION_REGION, LOCATION_BOTH };
226 struct geoinfo
228 GEOID id;
229 WCHAR iso2W[3];
230 WCHAR iso3W[4];
231 GEOID parent;
232 int uncode;
233 enum locationkind kind;
236 static const struct geoinfo geoinfodata[] =
238 { 2, L"AG", L"ATG", 10039880, 28 }, /* Antigua and Barbuda */
239 { 3, L"AF", L"AFG", 47614, 4 }, /* Afghanistan */
240 { 4, L"DZ", L"DZA", 42487, 12 }, /* Algeria */
241 { 5, L"AZ", L"AZE", 47611, 31 }, /* Azerbaijan */
242 { 6, L"AL", L"ALB", 47610, 8 }, /* Albania */
243 { 7, L"AM", L"ARM", 47611, 51 }, /* Armenia */
244 { 8, L"AD", L"AND", 47610, 20 }, /* Andorra */
245 { 9, L"AO", L"AGO", 42484, 24 }, /* Angola */
246 { 10, L"AS", L"ASM", 26286, 16 }, /* American Samoa */
247 { 11, L"AR", L"ARG", 31396, 32 }, /* Argentina */
248 { 12, L"AU", L"AUS", 10210825, 36 }, /* Australia */
249 { 14, L"AT", L"AUT", 10210824, 40 }, /* Austria */
250 { 17, L"BH", L"BHR", 47611, 48 }, /* Bahrain */
251 { 18, L"BB", L"BRB", 10039880, 52 }, /* Barbados */
252 { 19, L"BW", L"BWA", 10039883, 72 }, /* Botswana */
253 { 20, L"BM", L"BMU", 23581, 60 }, /* Bermuda */
254 { 21, L"BE", L"BEL", 10210824, 56 }, /* Belgium */
255 { 22, L"BS", L"BHS", 10039880, 44 }, /* Bahamas, The */
256 { 23, L"BD", L"BGD", 47614, 50 }, /* Bangladesh */
257 { 24, L"BZ", L"BLZ", 27082, 84 }, /* Belize */
258 { 25, L"BA", L"BIH", 47610, 70 }, /* Bosnia and Herzegovina */
259 { 26, L"BO", L"BOL", 31396, 68 }, /* Bolivia */
260 { 27, L"MM", L"MMR", 47599, 104 }, /* Myanmar */
261 { 28, L"BJ", L"BEN", 42483, 204 }, /* Benin */
262 { 29, L"BY", L"BLR", 47609, 112 }, /* Belarus */
263 { 30, L"SB", L"SLB", 20900, 90 }, /* Solomon Islands */
264 { 32, L"BR", L"BRA", 31396, 76 }, /* Brazil */
265 { 34, L"BT", L"BTN", 47614, 64 }, /* Bhutan */
266 { 35, L"BG", L"BGR", 47609, 100 }, /* Bulgaria */
267 { 37, L"BN", L"BRN", 47599, 96 }, /* Brunei */
268 { 38, L"BI", L"BDI", 47603, 108 }, /* Burundi */
269 { 39, L"CA", L"CAN", 23581, 124 }, /* Canada */
270 { 40, L"KH", L"KHM", 47599, 116 }, /* Cambodia */
271 { 41, L"TD", L"TCD", 42484, 148 }, /* Chad */
272 { 42, L"LK", L"LKA", 47614, 144 }, /* Sri Lanka */
273 { 43, L"CG", L"COG", 42484, 178 }, /* Congo */
274 { 44, L"CD", L"COD", 42484, 180 }, /* Congo (DRC) */
275 { 45, L"CN", L"CHN", 47600, 156 }, /* China */
276 { 46, L"CL", L"CHL", 31396, 152 }, /* Chile */
277 { 49, L"CM", L"CMR", 42484, 120 }, /* Cameroon */
278 { 50, L"KM", L"COM", 47603, 174 }, /* Comoros */
279 { 51, L"CO", L"COL", 31396, 170 }, /* Colombia */
280 { 54, L"CR", L"CRI", 27082, 188 }, /* Costa Rica */
281 { 55, L"CF", L"CAF", 42484, 140 }, /* Central African Republic */
282 { 56, L"CU", L"CUB", 10039880, 192 }, /* Cuba */
283 { 57, L"CV", L"CPV", 42483, 132 }, /* Cape Verde */
284 { 59, L"CY", L"CYP", 47611, 196 }, /* Cyprus */
285 { 61, L"DK", L"DNK", 10039882, 208 }, /* Denmark */
286 { 62, L"DJ", L"DJI", 47603, 262 }, /* Djibouti */
287 { 63, L"DM", L"DMA", 10039880, 212 }, /* Dominica */
288 { 65, L"DO", L"DOM", 10039880, 214 }, /* Dominican Republic */
289 { 66, L"EC", L"ECU", 31396, 218 }, /* Ecuador */
290 { 67, L"EG", L"EGY", 42487, 818 }, /* Egypt */
291 { 68, L"IE", L"IRL", 10039882, 372 }, /* Ireland */
292 { 69, L"GQ", L"GNQ", 42484, 226 }, /* Equatorial Guinea */
293 { 70, L"EE", L"EST", 10039882, 233 }, /* Estonia */
294 { 71, L"ER", L"ERI", 47603, 232 }, /* Eritrea */
295 { 72, L"SV", L"SLV", 27082, 222 }, /* El Salvador */
296 { 73, L"ET", L"ETH", 47603, 231 }, /* Ethiopia */
297 { 75, L"CZ", L"CZE", 47609, 203 }, /* Czech Republic */
298 { 77, L"FI", L"FIN", 10039882, 246 }, /* Finland */
299 { 78, L"FJ", L"FJI", 20900, 242 }, /* Fiji Islands */
300 { 80, L"FM", L"FSM", 21206, 583 }, /* Micronesia */
301 { 81, L"FO", L"FRO", 10039882, 234 }, /* Faroe Islands */
302 { 84, L"FR", L"FRA", 10210824, 250 }, /* France */
303 { 86, L"GM", L"GMB", 42483, 270 }, /* Gambia, The */
304 { 87, L"GA", L"GAB", 42484, 266 }, /* Gabon */
305 { 88, L"GE", L"GEO", 47611, 268 }, /* Georgia */
306 { 89, L"GH", L"GHA", 42483, 288 }, /* Ghana */
307 { 90, L"GI", L"GIB", 47610, 292 }, /* Gibraltar */
308 { 91, L"GD", L"GRD", 10039880, 308 }, /* Grenada */
309 { 93, L"GL", L"GRL", 23581, 304 }, /* Greenland */
310 { 94, L"DE", L"DEU", 10210824, 276 }, /* Germany */
311 { 98, L"GR", L"GRC", 47610, 300 }, /* Greece */
312 { 99, L"GT", L"GTM", 27082, 320 }, /* Guatemala */
313 { 100, L"GN", L"GIN", 42483, 324 }, /* Guinea */
314 { 101, L"GY", L"GUY", 31396, 328 }, /* Guyana */
315 { 103, L"HT", L"HTI", 10039880, 332 }, /* Haiti */
316 { 104, L"HK", L"HKG", 47600, 344 }, /* Hong Kong S.A.R. */
317 { 106, L"HN", L"HND", 27082, 340 }, /* Honduras */
318 { 108, L"HR", L"HRV", 47610, 191 }, /* Croatia */
319 { 109, L"HU", L"HUN", 47609, 348 }, /* Hungary */
320 { 110, L"IS", L"ISL", 10039882, 352 }, /* Iceland */
321 { 111, L"ID", L"IDN", 47599, 360 }, /* Indonesia */
322 { 113, L"IN", L"IND", 47614, 356 }, /* India */
323 { 114, L"IO", L"IOT", 39070, 86 }, /* British Indian Ocean Territory */
324 { 116, L"IR", L"IRN", 47614, 364 }, /* Iran */
325 { 117, L"IL", L"ISR", 47611, 376 }, /* Israel */
326 { 118, L"IT", L"ITA", 47610, 380 }, /* Italy */
327 { 119, L"CI", L"CIV", 42483, 384 }, /* Côte d'Ivoire */
328 { 121, L"IQ", L"IRQ", 47611, 368 }, /* Iraq */
329 { 122, L"JP", L"JPN", 47600, 392 }, /* Japan */
330 { 124, L"JM", L"JAM", 10039880, 388 }, /* Jamaica */
331 { 125, L"SJ", L"SJM", 10039882, 744 }, /* Jan Mayen */
332 { 126, L"JO", L"JOR", 47611, 400 }, /* Jordan */
333 { 127, L"XX", L"XX", 161832256 }, /* Johnston Atoll */
334 { 129, L"KE", L"KEN", 47603, 404 }, /* Kenya */
335 { 130, L"KG", L"KGZ", 47590, 417 }, /* Kyrgyzstan */
336 { 131, L"KP", L"PRK", 47600, 408 }, /* North Korea */
337 { 133, L"KI", L"KIR", 21206, 296 }, /* Kiribati */
338 { 134, L"KR", L"KOR", 47600, 410 }, /* Korea */
339 { 136, L"KW", L"KWT", 47611, 414 }, /* Kuwait */
340 { 137, L"KZ", L"KAZ", 47590, 398 }, /* Kazakhstan */
341 { 138, L"LA", L"LAO", 47599, 418 }, /* Laos */
342 { 139, L"LB", L"LBN", 47611, 422 }, /* Lebanon */
343 { 140, L"LV", L"LVA", 10039882, 428 }, /* Latvia */
344 { 141, L"LT", L"LTU", 10039882, 440 }, /* Lithuania */
345 { 142, L"LR", L"LBR", 42483, 430 }, /* Liberia */
346 { 143, L"SK", L"SVK", 47609, 703 }, /* Slovakia */
347 { 145, L"LI", L"LIE", 10210824, 438 }, /* Liechtenstein */
348 { 146, L"LS", L"LSO", 10039883, 426 }, /* Lesotho */
349 { 147, L"LU", L"LUX", 10210824, 442 }, /* Luxembourg */
350 { 148, L"LY", L"LBY", 42487, 434 }, /* Libya */
351 { 149, L"MG", L"MDG", 47603, 450 }, /* Madagascar */
352 { 151, L"MO", L"MAC", 47600, 446 }, /* Macao S.A.R. */
353 { 152, L"MD", L"MDA", 47609, 498 }, /* Moldova */
354 { 154, L"MN", L"MNG", 47600, 496 }, /* Mongolia */
355 { 156, L"MW", L"MWI", 47603, 454 }, /* Malawi */
356 { 157, L"ML", L"MLI", 42483, 466 }, /* Mali */
357 { 158, L"MC", L"MCO", 10210824, 492 }, /* Monaco */
358 { 159, L"MA", L"MAR", 42487, 504 }, /* Morocco */
359 { 160, L"MU", L"MUS", 47603, 480 }, /* Mauritius */
360 { 162, L"MR", L"MRT", 42483, 478 }, /* Mauritania */
361 { 163, L"MT", L"MLT", 47610, 470 }, /* Malta */
362 { 164, L"OM", L"OMN", 47611, 512 }, /* Oman */
363 { 165, L"MV", L"MDV", 47614, 462 }, /* Maldives */
364 { 166, L"MX", L"MEX", 27082, 484 }, /* Mexico */
365 { 167, L"MY", L"MYS", 47599, 458 }, /* Malaysia */
366 { 168, L"MZ", L"MOZ", 47603, 508 }, /* Mozambique */
367 { 173, L"NE", L"NER", 42483, 562 }, /* Niger */
368 { 174, L"VU", L"VUT", 20900, 548 }, /* Vanuatu */
369 { 175, L"NG", L"NGA", 42483, 566 }, /* Nigeria */
370 { 176, L"NL", L"NLD", 10210824, 528 }, /* Netherlands */
371 { 177, L"NO", L"NOR", 10039882, 578 }, /* Norway */
372 { 178, L"NP", L"NPL", 47614, 524 }, /* Nepal */
373 { 180, L"NR", L"NRU", 21206, 520 }, /* Nauru */
374 { 181, L"SR", L"SUR", 31396, 740 }, /* Suriname */
375 { 182, L"NI", L"NIC", 27082, 558 }, /* Nicaragua */
376 { 183, L"NZ", L"NZL", 10210825, 554 }, /* New Zealand */
377 { 184, L"PS", L"PSE", 47611, 275 }, /* Palestinian Authority */
378 { 185, L"PY", L"PRY", 31396, 600 }, /* Paraguay */
379 { 187, L"PE", L"PER", 31396, 604 }, /* Peru */
380 { 190, L"PK", L"PAK", 47614, 586 }, /* Pakistan */
381 { 191, L"PL", L"POL", 47609, 616 }, /* Poland */
382 { 192, L"PA", L"PAN", 27082, 591 }, /* Panama */
383 { 193, L"PT", L"PRT", 47610, 620 }, /* Portugal */
384 { 194, L"PG", L"PNG", 20900, 598 }, /* Papua New Guinea */
385 { 195, L"PW", L"PLW", 21206, 585 }, /* Palau */
386 { 196, L"GW", L"GNB", 42483, 624 }, /* Guinea-Bissau */
387 { 197, L"QA", L"QAT", 47611, 634 }, /* Qatar */
388 { 198, L"RE", L"REU", 47603, 638 }, /* Reunion */
389 { 199, L"MH", L"MHL", 21206, 584 }, /* Marshall Islands */
390 { 200, L"RO", L"ROU", 47609, 642 }, /* Romania */
391 { 201, L"PH", L"PHL", 47599, 608 }, /* Philippines */
392 { 202, L"PR", L"PRI", 10039880, 630 }, /* Puerto Rico */
393 { 203, L"RU", L"RUS", 47609, 643 }, /* Russia */
394 { 204, L"RW", L"RWA", 47603, 646 }, /* Rwanda */
395 { 205, L"SA", L"SAU", 47611, 682 }, /* Saudi Arabia */
396 { 206, L"PM", L"SPM", 23581, 666 }, /* St. Pierre and Miquelon */
397 { 207, L"KN", L"KNA", 10039880, 659 }, /* St. Kitts and Nevis */
398 { 208, L"SC", L"SYC", 47603, 690 }, /* Seychelles */
399 { 209, L"ZA", L"ZAF", 10039883, 710 }, /* South Africa */
400 { 210, L"SN", L"SEN", 42483, 686 }, /* Senegal */
401 { 212, L"SI", L"SVN", 47610, 705 }, /* Slovenia */
402 { 213, L"SL", L"SLE", 42483, 694 }, /* Sierra Leone */
403 { 214, L"SM", L"SMR", 47610, 674 }, /* San Marino */
404 { 215, L"SG", L"SGP", 47599, 702 }, /* Singapore */
405 { 216, L"SO", L"SOM", 47603, 706 }, /* Somalia */
406 { 217, L"ES", L"ESP", 47610, 724 }, /* Spain */
407 { 218, L"LC", L"LCA", 10039880, 662 }, /* St. Lucia */
408 { 219, L"SD", L"SDN", 42487, 736 }, /* Sudan */
409 { 220, L"SJ", L"SJM", 10039882, 744 }, /* Svalbard */
410 { 221, L"SE", L"SWE", 10039882, 752 }, /* Sweden */
411 { 222, L"SY", L"SYR", 47611, 760 }, /* Syria */
412 { 223, L"CH", L"CHE", 10210824, 756 }, /* Switzerland */
413 { 224, L"AE", L"ARE", 47611, 784 }, /* United Arab Emirates */
414 { 225, L"TT", L"TTO", 10039880, 780 }, /* Trinidad and Tobago */
415 { 227, L"TH", L"THA", 47599, 764 }, /* Thailand */
416 { 228, L"TJ", L"TJK", 47590, 762 }, /* Tajikistan */
417 { 231, L"TO", L"TON", 26286, 776 }, /* Tonga */
418 { 232, L"TG", L"TGO", 42483, 768 }, /* Togo */
419 { 233, L"ST", L"STP", 42484, 678 }, /* São Tomé and Príncipe */
420 { 234, L"TN", L"TUN", 42487, 788 }, /* Tunisia */
421 { 235, L"TR", L"TUR", 47611, 792 }, /* Turkey */
422 { 236, L"TV", L"TUV", 26286, 798 }, /* Tuvalu */
423 { 237, L"TW", L"TWN", 47600, 158 }, /* Taiwan */
424 { 238, L"TM", L"TKM", 47590, 795 }, /* Turkmenistan */
425 { 239, L"TZ", L"TZA", 47603, 834 }, /* Tanzania */
426 { 240, L"UG", L"UGA", 47603, 800 }, /* Uganda */
427 { 241, L"UA", L"UKR", 47609, 804 }, /* Ukraine */
428 { 242, L"GB", L"GBR", 10039882, 826 }, /* United Kingdom */
429 { 244, L"US", L"USA", 23581, 840 }, /* United States */
430 { 245, L"BF", L"BFA", 42483, 854 }, /* Burkina Faso */
431 { 246, L"UY", L"URY", 31396, 858 }, /* Uruguay */
432 { 247, L"UZ", L"UZB", 47590, 860 }, /* Uzbekistan */
433 { 248, L"VC", L"VCT", 10039880, 670 }, /* St. Vincent and the Grenadines */
434 { 249, L"VE", L"VEN", 31396, 862 }, /* Bolivarian Republic of Venezuela */
435 { 251, L"VN", L"VNM", 47599, 704 }, /* Vietnam */
436 { 252, L"VI", L"VIR", 10039880, 850 }, /* Virgin Islands */
437 { 253, L"VA", L"VAT", 47610, 336 }, /* Vatican City */
438 { 254, L"NA", L"NAM", 10039883, 516 }, /* Namibia */
439 { 257, L"EH", L"ESH", 42487, 732 }, /* Western Sahara (disputed) */
440 { 258, L"XX", L"XX", 161832256 }, /* Wake Island */
441 { 259, L"WS", L"WSM", 26286, 882 }, /* Samoa */
442 { 260, L"SZ", L"SWZ", 10039883, 748 }, /* Swaziland */
443 { 261, L"YE", L"YEM", 47611, 887 }, /* Yemen */
444 { 263, L"ZM", L"ZMB", 47603, 894 }, /* Zambia */
445 { 264, L"ZW", L"ZWE", 47603, 716 }, /* Zimbabwe */
446 { 269, L"CS", L"SCG", 47610, 891 }, /* Serbia and Montenegro (Former) */
447 { 270, L"ME", L"MNE", 47610, 499 }, /* Montenegro */
448 { 271, L"RS", L"SRB", 47610, 688 }, /* Serbia */
449 { 273, L"CW", L"CUW", 10039880, 531 }, /* Curaçao */
450 { 276, L"SS", L"SSD", 42487, 728 }, /* South Sudan */
451 { 300, L"AI", L"AIA", 10039880, 660 }, /* Anguilla */
452 { 301, L"AQ", L"ATA", 39070, 10 }, /* Antarctica */
453 { 302, L"AW", L"ABW", 10039880, 533 }, /* Aruba */
454 { 303, L"XX", L"XX", 343 }, /* Ascension Island */
455 { 304, L"XX", L"XX", 10210825 }, /* Ashmore and Cartier Islands */
456 { 305, L"XX", L"XX", 161832256 }, /* Baker Island */
457 { 306, L"BV", L"BVT", 39070, 74 }, /* Bouvet Island */
458 { 307, L"KY", L"CYM", 10039880, 136 }, /* Cayman Islands */
459 { 308, L"XX", L"XX", 10210824, 830, LOCATION_BOTH }, /* Channel Islands */
460 { 309, L"CX", L"CXR", 12, 162 }, /* Christmas Island */
461 { 310, L"XX", L"XX", 27114 }, /* Clipperton Island */
462 { 311, L"CC", L"CCK", 10210825, 166 }, /* Cocos (Keeling) Islands */
463 { 312, L"CK", L"COK", 26286, 184 }, /* Cook Islands */
464 { 313, L"XX", L"XX", 10210825 }, /* Coral Sea Islands */
465 { 314, L"XX", L"XX", 114 }, /* Diego Garcia */
466 { 315, L"FK", L"FLK", 31396, 238 }, /* Falkland Islands (Islas Malvinas) */
467 { 317, L"GF", L"GUF", 31396, 254 }, /* French Guiana */
468 { 318, L"PF", L"PYF", 26286, 258 }, /* French Polynesia */
469 { 319, L"TF", L"ATF", 39070, 260 }, /* French Southern and Antarctic Lands */
470 { 321, L"GP", L"GLP", 10039880, 312 }, /* Guadeloupe */
471 { 322, L"GU", L"GUM", 21206, 316 }, /* Guam */
472 { 323, L"XX", L"XX", 39070 }, /* Guantanamo Bay */
473 { 324, L"GG", L"GGY", 308, 831 }, /* Guernsey */
474 { 325, L"HM", L"HMD", 39070, 334 }, /* Heard Island and McDonald Islands */
475 { 326, L"XX", L"XX", 161832256 }, /* Howland Island */
476 { 327, L"XX", L"XX", 161832256 }, /* Jarvis Island */
477 { 328, L"JE", L"JEY", 308, 832 }, /* Jersey */
478 { 329, L"XX", L"XX", 161832256 }, /* Kingman Reef */
479 { 330, L"MQ", L"MTQ", 10039880, 474 }, /* Martinique */
480 { 331, L"YT", L"MYT", 47603, 175 }, /* Mayotte */
481 { 332, L"MS", L"MSR", 10039880, 500 }, /* Montserrat */
482 { 333, L"AN", L"ANT", 10039880, 530, LOCATION_BOTH }, /* Netherlands Antilles (Former) */
483 { 334, L"NC", L"NCL", 20900, 540 }, /* New Caledonia */
484 { 335, L"NU", L"NIU", 26286, 570 }, /* Niue */
485 { 336, L"NF", L"NFK", 10210825, 574 }, /* Norfolk Island */
486 { 337, L"MP", L"MNP", 21206, 580 }, /* Northern Mariana Islands */
487 { 338, L"XX", L"XX", 161832256 }, /* Palmyra Atoll */
488 { 339, L"PN", L"PCN", 26286, 612 }, /* Pitcairn Islands */
489 { 340, L"XX", L"XX", 337 }, /* Rota Island */
490 { 341, L"XX", L"XX", 337 }, /* Saipan */
491 { 342, L"GS", L"SGS", 39070, 239 }, /* South Georgia and the South Sandwich Islands */
492 { 343, L"SH", L"SHN", 42483, 654 }, /* St. Helena */
493 { 346, L"XX", L"XX", 337 }, /* Tinian Island */
494 { 347, L"TK", L"TKL", 26286, 772 }, /* Tokelau */
495 { 348, L"XX", L"XX", 343 }, /* Tristan da Cunha */
496 { 349, L"TC", L"TCA", 10039880, 796 }, /* Turks and Caicos Islands */
497 { 351, L"VG", L"VGB", 10039880, 92 }, /* Virgin Islands, British */
498 { 352, L"WF", L"WLF", 26286, 876 }, /* Wallis and Futuna */
499 { 742, L"XX", L"XX", 39070, 2, LOCATION_REGION }, /* Africa */
500 { 2129, L"XX", L"XX", 39070, 142, LOCATION_REGION }, /* Asia */
501 { 10541, L"XX", L"XX", 39070, 150, LOCATION_REGION }, /* Europe */
502 { 15126, L"IM", L"IMN", 10039882, 833 }, /* Man, Isle of */
503 { 19618, L"MK", L"MKD", 47610, 807 }, /* Macedonia, Former Yugoslav Republic of */
504 { 20900, L"XX", L"XX", 27114, 54, LOCATION_REGION }, /* Melanesia */
505 { 21206, L"XX", L"XX", 27114, 57, LOCATION_REGION }, /* Micronesia */
506 { 21242, L"XX", L"XX", 161832256 }, /* Midway Islands */
507 { 23581, L"XX", L"XX", 10026358, 21, LOCATION_REGION }, /* Northern America */
508 { 26286, L"XX", L"XX", 27114, 61, LOCATION_REGION }, /* Polynesia */
509 { 27082, L"XX", L"XX", 161832257, 13, LOCATION_REGION }, /* Central America */
510 { 27114, L"XX", L"XX", 39070, 9, LOCATION_REGION }, /* Oceania */
511 { 30967, L"SX", L"SXM", 10039880, 534 }, /* Sint Maarten (Dutch part) */
512 { 31396, L"XX", L"XX", 161832257, 5, LOCATION_REGION }, /* South America */
513 { 31706, L"MF", L"MAF", 10039880, 663 }, /* Saint Martin (French part) */
514 { 39070, L"XX", L"XX", 39070, 1, LOCATION_REGION }, /* World */
515 { 42483, L"XX", L"XX", 742, 11, LOCATION_REGION }, /* Western Africa */
516 { 42484, L"XX", L"XX", 742, 17, LOCATION_REGION }, /* Middle Africa */
517 { 42487, L"XX", L"XX", 742, 15, LOCATION_REGION }, /* Northern Africa */
518 { 47590, L"XX", L"XX", 2129, 143, LOCATION_REGION }, /* Central Asia */
519 { 47599, L"XX", L"XX", 2129, 35, LOCATION_REGION }, /* South-Eastern Asia */
520 { 47600, L"XX", L"XX", 2129, 30, LOCATION_REGION }, /* Eastern Asia */
521 { 47603, L"XX", L"XX", 742, 14, LOCATION_REGION }, /* Eastern Africa */
522 { 47609, L"XX", L"XX", 10541, 151, LOCATION_REGION }, /* Eastern Europe */
523 { 47610, L"XX", L"XX", 10541, 39, LOCATION_REGION }, /* Southern Europe */
524 { 47611, L"XX", L"XX", 2129, 145, LOCATION_REGION }, /* Middle East */
525 { 47614, L"XX", L"XX", 2129, 34, LOCATION_REGION }, /* Southern Asia */
526 { 7299303, L"TL", L"TLS", 47599, 626 }, /* Democratic Republic of Timor-Leste */
527 { 9914689, L"XK", L"XKS", 47610, 906 }, /* Kosovo */
528 { 10026358, L"XX", L"XX", 39070, 19, LOCATION_REGION }, /* Americas */
529 { 10028789, L"AX", L"ALA", 10039882, 248 }, /* Åland Islands */
530 { 10039880, L"XX", L"XX", 161832257, 29, LOCATION_REGION }, /* Caribbean */
531 { 10039882, L"XX", L"XX", 10541, 154, LOCATION_REGION }, /* Northern Europe */
532 { 10039883, L"XX", L"XX", 742, 18, LOCATION_REGION }, /* Southern Africa */
533 { 10210824, L"XX", L"XX", 10541, 155, LOCATION_REGION }, /* Western Europe */
534 { 10210825, L"XX", L"XX", 27114, 53, LOCATION_REGION }, /* Australia and New Zealand */
535 { 161832015, L"BL", L"BLM", 10039880, 652 }, /* Saint Barthélemy */
536 { 161832256, L"UM", L"UMI", 27114, 581 }, /* U.S. Minor Outlying Islands */
537 { 161832257, L"XX", L"XX", 10026358, 419, LOCATION_REGION }, /* Latin America and the Caribbean */
540 /* NLS normalization file */
541 struct norm_table
543 WCHAR name[13]; /* 00 file name */
544 USHORT checksum[3]; /* 1a checksum? */
545 USHORT version[4]; /* 20 Unicode version */
546 USHORT form; /* 28 normalization form */
547 USHORT len_factor; /* 2a factor for length estimates */
548 USHORT unknown1; /* 2c */
549 USHORT decomp_size; /* 2e decomposition hash size */
550 USHORT comp_size; /* 30 composition hash size */
551 USHORT unknown2; /* 32 */
552 USHORT classes; /* 34 combining classes table offset */
553 USHORT props_level1; /* 36 char properties table level 1 offset */
554 USHORT props_level2; /* 38 char properties table level 2 offset */
555 USHORT decomp_hash; /* 3a decomposition hash table offset */
556 USHORT decomp_map; /* 3c decomposition character map table offset */
557 USHORT decomp_seq; /* 3e decomposition character sequences offset */
558 USHORT comp_hash; /* 40 composition hash table offset */
559 USHORT comp_seq; /* 42 composition character sequences offset */
560 /* BYTE[] combining class values */
561 /* BYTE[0x2200] char properties index level 1 */
562 /* BYTE[] char properties index level 2 */
563 /* WORD[] decomposition hash table */
564 /* WORD[] decomposition character map */
565 /* WORD[] decomposition character sequences */
566 /* WORD[] composition hash table */
567 /* WORD[] composition character sequences */
570 static NLSTABLEINFO nls_info;
571 static UINT mac_cp = 10000;
572 static HKEY intl_key;
573 static HKEY nls_key;
574 static HKEY tz_key;
576 static CPTABLEINFO codepages[128];
577 static unsigned int nb_codepages;
579 static struct norm_table *norm_info;
581 struct sortguid
583 GUID id; /* sort GUID */
584 DWORD flags; /* flags */
585 DWORD compr; /* offset to compression table */
586 DWORD except; /* exception table offset in sortkey table */
587 DWORD ling_except; /* exception table offset for linguistic casing */
588 DWORD casemap; /* linguistic casemap table offset */
591 #define FLAG_HAS_3_BYTE_WEIGHTS 0x01
592 #define FLAG_REVERSEDIACRITICS 0x10
593 #define FLAG_DOUBLECOMPRESSION 0x20
594 #define FLAG_INVERSECASING 0x40
596 static const struct sortguid *current_locale_sort;
598 static const GUID default_sort_guid = { 0x00000001, 0x57ee, 0x1e5c, { 0x00, 0xb4, 0xd0, 0x00, 0x0b, 0xb1, 0xe1, 0x1e }};
600 static struct
602 DWORD *keys; /* sortkey table, indexed by char */
603 USHORT *casemap; /* casemap table, in l_intl.nls format */
604 WORD *ctypes; /* CT_CTYPE1,2,3 values */
605 BYTE *ctype_idx; /* index to map char to ctypes array entry */
606 DWORD version; /* NLS version */
607 DWORD guid_count; /* number of sort GUIDs */
608 struct sortguid *guids; /* table of sort GUIDs */
609 } sort;
611 static CRITICAL_SECTION locale_section;
612 static CRITICAL_SECTION_DEBUG critsect_debug =
614 0, 0, &locale_section,
615 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
616 0, 0, { (DWORD_PTR)(__FILE__ ": locale_section") }
618 static CRITICAL_SECTION locale_section = { &critsect_debug, -1, 0, 0, 0, 0 };
621 static void init_sortkeys( DWORD *ptr )
623 WORD *ctype;
624 DWORD *table;
626 sort.keys = (DWORD *)((char *)ptr + ptr[0]);
627 sort.casemap = (USHORT *)((char *)ptr + ptr[1]);
629 ctype = (WORD *)((char *)ptr + ptr[2]);
630 sort.ctypes = ctype + 2;
631 sort.ctype_idx = (BYTE *)ctype + ctype[1] + 2;
633 table = (DWORD *)((char *)ptr + ptr[3]);
634 sort.version = table[0];
635 sort.guid_count = table[1];
636 sort.guids = (struct sortguid *)(table + 2);
640 static const struct sortguid *find_sortguid( const GUID *guid )
642 int pos, ret, min = 0, max = sort.guid_count - 1;
644 while (min <= max)
646 pos = (min + max) / 2;
647 ret = memcmp( guid, &sort.guids[pos].id, sizeof(*guid) );
648 if (!ret) return &sort.guids[pos];
649 if (ret > 0) min = pos + 1;
650 else max = pos - 1;
652 ERR( "no sort found for %s\n", debugstr_guid( guid ));
653 return NULL;
657 static const struct sortguid *get_language_sort( const WCHAR *locale )
659 WCHAR *p, *end, buffer[LOCALE_NAME_MAX_LENGTH], guidstr[39];
660 const struct sortguid *ret;
661 UNICODE_STRING str;
662 GUID guid;
663 HKEY key = 0;
664 DWORD size, type;
666 if (locale == LOCALE_NAME_USER_DEFAULT)
668 if (current_locale_sort) return current_locale_sort;
669 GetUserDefaultLocaleName( buffer, ARRAY_SIZE( buffer ));
671 else lstrcpynW( buffer, locale, LOCALE_NAME_MAX_LENGTH );
673 if (buffer[0] && !RegOpenKeyExW( nls_key, L"Sorting\\Ids", 0, KEY_READ, &key ))
675 for (;;)
677 size = sizeof(guidstr);
678 if (!RegQueryValueExW( key, buffer, NULL, &type, (BYTE *)guidstr, &size ) && type == REG_SZ)
680 RtlInitUnicodeString( &str, guidstr );
681 if (!RtlGUIDFromString( &str, &guid ))
683 ret = find_sortguid( &guid );
684 goto done;
686 break;
688 for (p = end = buffer; *p; p++) if (*p == '-' || *p == '_') end = p;
689 if (end == buffer) break;
690 *end = 0;
693 ret = find_sortguid( &default_sort_guid );
694 done:
695 RegCloseKey( key );
696 return ret;
700 /***********************************************************************
701 * init_locale
703 void init_locale(void)
705 UINT ansi_cp = 0, oem_cp = 0;
706 USHORT *ansi_ptr, *oem_ptr;
707 void *sort_ptr;
708 LCID lcid = GetUserDefaultLCID();
709 WCHAR bufferW[80];
710 DYNAMIC_TIME_ZONE_INFORMATION timezone;
711 GEOID geoid = GEOID_NOT_AVAILABLE;
712 DWORD count, dispos, i;
713 SIZE_T size;
714 HKEY hkey;
716 kernel32_handle = GetModuleHandleW( L"kernel32.dll" );
718 GetLocaleInfoW( LOCALE_SYSTEM_DEFAULT, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
719 (WCHAR *)&ansi_cp, sizeof(ansi_cp)/sizeof(WCHAR) );
720 GetLocaleInfoW( LOCALE_SYSTEM_DEFAULT, LOCALE_IDEFAULTMACCODEPAGE | LOCALE_RETURN_NUMBER,
721 (WCHAR *)&mac_cp, sizeof(mac_cp)/sizeof(WCHAR) );
722 GetLocaleInfoW( LOCALE_SYSTEM_DEFAULT, LOCALE_IDEFAULTCODEPAGE | LOCALE_RETURN_NUMBER,
723 (WCHAR *)&oem_cp, sizeof(oem_cp)/sizeof(WCHAR) );
725 NtGetNlsSectionPtr( 9, 0, NULL, &sort_ptr, &size );
726 NtGetNlsSectionPtr( 12, NormalizationC, NULL, (void **)&norm_info, &size );
727 init_sortkeys( sort_ptr );
729 if (!ansi_cp || NtGetNlsSectionPtr( 11, ansi_cp, NULL, (void **)&ansi_ptr, &size ))
730 NtGetNlsSectionPtr( 11, 1252, NULL, (void **)&ansi_ptr, &size );
731 if (!oem_cp || NtGetNlsSectionPtr( 11, oem_cp, 0, (void **)&oem_ptr, &size ))
732 NtGetNlsSectionPtr( 11, 437, NULL, (void **)&oem_ptr, &size );
733 NtCurrentTeb()->Peb->AnsiCodePageData = ansi_ptr;
734 NtCurrentTeb()->Peb->OemCodePageData = oem_ptr;
735 NtCurrentTeb()->Peb->UnicodeCaseTableData = sort.casemap;
736 RtlInitNlsTables( ansi_ptr, oem_ptr, sort.casemap, &nls_info );
737 RtlResetRtlTranslations( &nls_info );
739 RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\Nls",
740 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &nls_key, NULL );
741 RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"Software\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones",
742 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &tz_key, NULL );
743 RegCreateKeyExW( HKEY_CURRENT_USER, L"Control Panel\\International",
744 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &intl_key, NULL );
746 current_locale_sort = get_language_sort( LOCALE_NAME_USER_DEFAULT );
748 if (GetDynamicTimeZoneInformation( &timezone ) != TIME_ZONE_ID_INVALID &&
749 !RegCreateKeyExW( HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\TimeZoneInformation",
750 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
752 RegSetValueExW( hkey, L"StandardName", 0, REG_SZ, (BYTE *)timezone.StandardName,
753 (lstrlenW(timezone.StandardName) + 1) * sizeof(WCHAR) );
754 RegSetValueExW( hkey, L"TimeZoneKeyName", 0, REG_SZ, (BYTE *)timezone.TimeZoneKeyName,
755 (lstrlenW(timezone.TimeZoneKeyName) + 1) * sizeof(WCHAR) );
756 RegCloseKey( hkey );
759 if (!RegCreateKeyExW( intl_key, L"Geo", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, &dispos ))
761 if (dispos == REG_CREATED_NEW_KEY)
763 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IGEOID | LOCALE_RETURN_NUMBER,
764 (WCHAR *)&geoid, sizeof(geoid) / sizeof(WCHAR) );
765 SetUserGeoID( geoid );
767 RegCloseKey( hkey );
770 /* Update registry contents if the user locale has changed.
771 * This simulates the action of the Windows control panel. */
773 count = sizeof(bufferW);
774 if (!RegQueryValueExW( intl_key, L"Locale", NULL, NULL, (BYTE *)bufferW, &count ))
776 if (wcstoul( bufferW, NULL, 16 ) == lcid) return; /* already set correctly */
777 TRACE( "updating registry, locale changed %s -> %08x\n", debugstr_w(bufferW), lcid );
779 else TRACE( "updating registry, locale changed none -> %08x\n", lcid );
780 swprintf( bufferW, ARRAY_SIZE(bufferW), L"%08x", lcid );
781 RegSetValueExW( intl_key, L"Locale", 0, REG_SZ,
782 (BYTE *)bufferW, (lstrlenW(bufferW) + 1) * sizeof(WCHAR) );
784 for (i = 0; i < ARRAY_SIZE(registry_values); i++)
786 GetLocaleInfoW( LOCALE_USER_DEFAULT, registry_values[i].lctype | LOCALE_NOUSEROVERRIDE,
787 bufferW, ARRAY_SIZE( bufferW ));
788 RegSetValueExW( intl_key, registry_values[i].name, 0, REG_SZ,
789 (BYTE *)bufferW, (lstrlenW(bufferW) + 1) * sizeof(WCHAR) );
792 if (geoid == GEOID_NOT_AVAILABLE)
794 GetLocaleInfoW( LOCALE_USER_DEFAULT, LOCALE_IGEOID | LOCALE_RETURN_NUMBER,
795 (WCHAR *)&geoid, sizeof(geoid) / sizeof(WCHAR) );
796 SetUserGeoID( geoid );
799 if (!RegCreateKeyExW( nls_key, L"Codepage",
800 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
802 count = swprintf( bufferW, ARRAY_SIZE(bufferW), L"%03d", ansi_cp );
803 RegSetValueExW( hkey, L"ACP", 0, REG_SZ, (BYTE *)bufferW, (count + 1) * sizeof(WCHAR) );
804 count = swprintf( bufferW, ARRAY_SIZE(bufferW), L"%03d", oem_cp );
805 RegSetValueExW( hkey, L"OEMCP", 0, REG_SZ, (BYTE *)bufferW, (count + 1) * sizeof(WCHAR) );
806 count = swprintf( bufferW, ARRAY_SIZE(bufferW), L"%03d", mac_cp );
807 RegSetValueExW( hkey, L"MACCP", 0, REG_SZ, (BYTE *)bufferW, (count + 1) * sizeof(WCHAR) );
808 RegCloseKey( hkey );
813 static inline USHORT get_table_entry( const USHORT *table, WCHAR ch )
815 return table[table[table[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0xf)];
819 static inline WCHAR casemap( const USHORT *table, WCHAR ch )
821 return ch + table[table[table[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0x0f)];
825 static inline WORD get_char_type( DWORD type, WCHAR ch )
827 const BYTE *ptr = sort.ctype_idx + ((const WORD *)sort.ctype_idx)[ch >> 8];
828 ptr = sort.ctype_idx + ((const WORD *)ptr)[(ch >> 4) & 0x0f] + (ch & 0x0f);
829 return sort.ctypes[*ptr * 3 + type / 2];
833 static BYTE rol( BYTE val, BYTE count )
835 return (val << count) | (val >> (8 - count));
839 static BYTE get_char_props( const struct norm_table *info, unsigned int ch )
841 const BYTE *level1 = (const BYTE *)((const USHORT *)info + info->props_level1);
842 const BYTE *level2 = (const BYTE *)((const USHORT *)info + info->props_level2);
843 BYTE off = level1[ch / 128];
845 if (!off || off >= 0xfb) return rol( off, 5 );
846 return level2[(off - 1) * 128 + ch % 128];
850 static const WCHAR *get_decomposition( WCHAR ch, unsigned int *ret_len )
852 const struct pair { WCHAR src; USHORT dst; } *pairs;
853 const USHORT *hash_table = (const USHORT *)norm_info + norm_info->decomp_hash;
854 const WCHAR *ret;
855 unsigned int i, pos, end, len, hash;
857 *ret_len = 1;
858 hash = ch % norm_info->decomp_size;
859 pos = hash_table[hash];
860 if (pos >> 13)
862 if (get_char_props( norm_info, ch ) != 0xbf) return NULL;
863 ret = (const USHORT *)norm_info + norm_info->decomp_seq + (pos & 0x1fff);
864 len = pos >> 13;
866 else
868 pairs = (const struct pair *)((const USHORT *)norm_info + norm_info->decomp_map);
870 /* find the end of the hash bucket */
871 for (i = hash + 1; i < norm_info->decomp_size; i++) if (!(hash_table[i] >> 13)) break;
872 if (i < norm_info->decomp_size) end = hash_table[i];
873 else for (end = pos; pairs[end].src; end++) ;
875 for ( ; pos < end; pos++)
877 if (pairs[pos].src != (WCHAR)ch) continue;
878 ret = (const USHORT *)norm_info + norm_info->decomp_seq + (pairs[pos].dst & 0x1fff);
879 len = pairs[pos].dst >> 13;
880 break;
882 if (pos >= end) return NULL;
885 if (len == 7) while (ret[len]) len++;
886 if (!ret[0]) len = 0; /* ignored char */
887 *ret_len = len;
888 return ret;
892 static WCHAR compose_chars( WCHAR ch1, WCHAR ch2 )
894 const USHORT *table = (const USHORT *)norm_info + norm_info->comp_hash;
895 const WCHAR *chars = (const USHORT *)norm_info + norm_info->comp_seq;
896 unsigned int hash, start, end, i;
897 WCHAR ch[3];
899 hash = (ch1 + 95 * ch2) % norm_info->comp_size;
900 start = table[hash];
901 end = table[hash + 1];
902 while (start < end)
904 for (i = 0; i < 3; i++, start++)
906 ch[i] = chars[start];
907 if (IS_HIGH_SURROGATE( ch[i] )) start++;
909 if (ch[0] == ch1 && ch[1] == ch2) return ch[2];
911 return 0;
915 static UINT get_lcid_codepage( LCID lcid, ULONG flags )
917 UINT ret = GetACP();
919 if (!(flags & LOCALE_USE_CP_ACP) && lcid != GetSystemDefaultLCID())
920 GetLocaleInfoW( lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
921 (WCHAR *)&ret, sizeof(ret)/sizeof(WCHAR) );
922 return ret;
926 static BOOL is_genitive_name_supported( LCTYPE lctype )
928 switch (LOWORD(lctype))
930 case LOCALE_SMONTHNAME1:
931 case LOCALE_SMONTHNAME2:
932 case LOCALE_SMONTHNAME3:
933 case LOCALE_SMONTHNAME4:
934 case LOCALE_SMONTHNAME5:
935 case LOCALE_SMONTHNAME6:
936 case LOCALE_SMONTHNAME7:
937 case LOCALE_SMONTHNAME8:
938 case LOCALE_SMONTHNAME9:
939 case LOCALE_SMONTHNAME10:
940 case LOCALE_SMONTHNAME11:
941 case LOCALE_SMONTHNAME12:
942 case LOCALE_SMONTHNAME13:
943 return TRUE;
944 default:
945 return FALSE;
950 static int get_value_base_by_lctype( LCTYPE lctype )
952 return lctype == LOCALE_ILANGUAGE || lctype == LOCALE_IDEFAULTLANGUAGE ? 16 : 10;
956 static const struct registry_value *get_locale_registry_value( DWORD lctype )
958 unsigned int i;
960 for (i = 0; i < ARRAY_SIZE( registry_values ); i++)
961 if (registry_values[i].lctype == lctype) return &registry_values[i];
962 return NULL;
966 static INT get_registry_locale_info( const struct registry_value *registry_value, LPWSTR buffer, INT len )
968 DWORD size, index = registry_value - registry_values;
969 INT ret;
971 RtlEnterCriticalSection( &locale_section );
973 if (!registry_cache[index])
975 size = len * sizeof(WCHAR);
976 ret = RegQueryValueExW( intl_key, registry_value->name, NULL, NULL, (BYTE *)buffer, &size );
977 if (!ret)
979 if (buffer && (registry_cache[index] = HeapAlloc( GetProcessHeap(), 0, size + sizeof(WCHAR) )))
981 memcpy( registry_cache[index], buffer, size );
982 registry_cache[index][size / sizeof(WCHAR)] = 0;
984 RtlLeaveCriticalSection( &locale_section );
985 return size / sizeof(WCHAR);
987 else
989 RtlLeaveCriticalSection( &locale_section );
990 if (ret == ERROR_FILE_NOT_FOUND) return -1;
991 if (ret == ERROR_MORE_DATA) SetLastError( ERROR_INSUFFICIENT_BUFFER );
992 else SetLastError( ret );
993 return 0;
997 ret = lstrlenW( registry_cache[index] ) + 1;
998 if (buffer)
1000 if (ret > len)
1002 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1003 ret = 0;
1005 else lstrcpyW( buffer, registry_cache[index] );
1007 RtlLeaveCriticalSection( &locale_section );
1008 return ret;
1012 static const CPTABLEINFO *get_codepage_table( UINT codepage )
1014 unsigned int i;
1015 USHORT *ptr;
1016 SIZE_T size;
1018 switch (codepage)
1020 case CP_ACP:
1021 return &nls_info.AnsiTableInfo;
1022 case CP_OEMCP:
1023 return &nls_info.OemTableInfo;
1024 case CP_MACCP:
1025 codepage = mac_cp;
1026 break;
1027 case CP_THREAD_ACP:
1028 if (NtCurrentTeb()->CurrentLocale == GetUserDefaultLCID()) return &nls_info.AnsiTableInfo;
1029 codepage = get_lcid_codepage( NtCurrentTeb()->CurrentLocale, 0 );
1030 if (!codepage) return &nls_info.AnsiTableInfo;
1031 break;
1032 default:
1033 if (codepage == nls_info.AnsiTableInfo.CodePage) return &nls_info.AnsiTableInfo;
1034 if (codepage == nls_info.OemTableInfo.CodePage) return &nls_info.OemTableInfo;
1035 break;
1038 RtlEnterCriticalSection( &locale_section );
1040 for (i = 0; i < nb_codepages; i++) if (codepages[i].CodePage == codepage) goto done;
1042 if (i == ARRAY_SIZE( codepages ))
1044 RtlLeaveCriticalSection( &locale_section );
1045 ERR( "too many codepages\n" );
1046 return NULL;
1048 if (NtGetNlsSectionPtr( 11, codepage, NULL, (void **)&ptr, &size ))
1050 RtlLeaveCriticalSection( &locale_section );
1051 SetLastError( ERROR_INVALID_PARAMETER );
1052 return NULL;
1054 RtlInitCodePageTable( ptr, &codepages[i] );
1055 nb_codepages++;
1056 done:
1057 RtlLeaveCriticalSection( &locale_section );
1058 return &codepages[i];
1062 static const WCHAR *get_ligature( WCHAR wc )
1064 int low = 0, high = ARRAY_SIZE( ligatures ) -1;
1065 while (low <= high)
1067 int pos = (low + high) / 2;
1068 if (ligatures[pos][0] < wc) low = pos + 1;
1069 else if (ligatures[pos][0] > wc) high = pos - 1;
1070 else return ligatures[pos] + 1;
1072 return NULL;
1076 static NTSTATUS expand_ligatures( const WCHAR *src, int srclen, WCHAR *dst, int *dstlen )
1078 int i, len, pos = 0;
1079 NTSTATUS ret = STATUS_SUCCESS;
1080 const WCHAR *expand;
1082 for (i = 0; i < srclen; i++)
1084 if (!(expand = get_ligature( src[i] )))
1086 expand = src + i;
1087 len = 1;
1089 else len = lstrlenW( expand );
1091 if (*dstlen && ret == STATUS_SUCCESS)
1093 if (pos + len <= *dstlen) memcpy( dst + pos, expand, len * sizeof(WCHAR) );
1094 else ret = STATUS_BUFFER_TOO_SMALL;
1096 pos += len;
1098 *dstlen = pos;
1099 return ret;
1103 static NTSTATUS fold_digits( const WCHAR *src, int srclen, WCHAR *dst, int *dstlen )
1105 extern const WCHAR wine_digitmap[] DECLSPEC_HIDDEN;
1106 int i, len = *dstlen;
1108 *dstlen = srclen;
1109 if (!len) return STATUS_SUCCESS;
1110 if (srclen > len) return STATUS_BUFFER_TOO_SMALL;
1111 for (i = 0; i < srclen; i++)
1113 WCHAR digit = get_table_entry( wine_digitmap, src[i] );
1114 dst[i] = digit ? digit : src[i];
1116 return STATUS_SUCCESS;
1120 static NTSTATUS fold_string( DWORD flags, const WCHAR *src, int srclen, WCHAR *dst, int *dstlen )
1122 NTSTATUS ret;
1123 WCHAR *tmp;
1125 switch (flags)
1127 case MAP_PRECOMPOSED:
1128 return RtlNormalizeString( NormalizationC, src, srclen, dst, dstlen );
1129 case MAP_FOLDCZONE:
1130 case MAP_PRECOMPOSED | MAP_FOLDCZONE:
1131 return RtlNormalizeString( NormalizationKC, src, srclen, dst, dstlen );
1132 case MAP_COMPOSITE:
1133 return RtlNormalizeString( NormalizationD, src, srclen, dst, dstlen );
1134 case MAP_COMPOSITE | MAP_FOLDCZONE:
1135 return RtlNormalizeString( NormalizationKD, src, srclen, dst, dstlen );
1136 case MAP_FOLDDIGITS:
1137 return fold_digits( src, srclen, dst, dstlen );
1138 case MAP_EXPAND_LIGATURES:
1139 case MAP_EXPAND_LIGATURES | MAP_FOLDCZONE:
1140 return expand_ligatures( src, srclen, dst, dstlen );
1141 case MAP_FOLDDIGITS | MAP_PRECOMPOSED:
1142 if (!(tmp = RtlAllocateHeap( GetProcessHeap(), 0, srclen * sizeof(WCHAR) )))
1143 return STATUS_NO_MEMORY;
1144 fold_digits( src, srclen, tmp, &srclen );
1145 ret = RtlNormalizeString( NormalizationC, tmp, srclen, dst, dstlen );
1146 break;
1147 case MAP_FOLDDIGITS | MAP_FOLDCZONE:
1148 case MAP_FOLDDIGITS | MAP_PRECOMPOSED | MAP_FOLDCZONE:
1149 if (!(tmp = RtlAllocateHeap( GetProcessHeap(), 0, srclen * sizeof(WCHAR) )))
1150 return STATUS_NO_MEMORY;
1151 fold_digits( src, srclen, tmp, &srclen );
1152 ret = RtlNormalizeString( NormalizationKC, tmp, srclen, dst, dstlen );
1153 break;
1154 case MAP_FOLDDIGITS | MAP_COMPOSITE:
1155 if (!(tmp = RtlAllocateHeap( GetProcessHeap(), 0, srclen * sizeof(WCHAR) )))
1156 return STATUS_NO_MEMORY;
1157 fold_digits( src, srclen, tmp, &srclen );
1158 ret = RtlNormalizeString( NormalizationD, tmp, srclen, dst, dstlen );
1159 break;
1160 case MAP_FOLDDIGITS | MAP_COMPOSITE | MAP_FOLDCZONE:
1161 if (!(tmp = RtlAllocateHeap( GetProcessHeap(), 0, srclen * sizeof(WCHAR) )))
1162 return STATUS_NO_MEMORY;
1163 fold_digits( src, srclen, tmp, &srclen );
1164 ret = RtlNormalizeString( NormalizationKD, tmp, srclen, dst, dstlen );
1165 break;
1166 case MAP_EXPAND_LIGATURES | MAP_FOLDDIGITS:
1167 case MAP_EXPAND_LIGATURES | MAP_FOLDDIGITS | MAP_FOLDCZONE:
1168 if (!(tmp = RtlAllocateHeap( GetProcessHeap(), 0, srclen * sizeof(WCHAR) )))
1169 return STATUS_NO_MEMORY;
1170 fold_digits( src, srclen, tmp, &srclen );
1171 ret = expand_ligatures( tmp, srclen, dst, dstlen );
1172 break;
1173 default:
1174 return STATUS_INVALID_PARAMETER_1;
1176 RtlFreeHeap( GetProcessHeap(), 0, tmp );
1177 return ret;
1181 static int mbstowcs_cpsymbol( DWORD flags, const char *src, int srclen, WCHAR *dst, int dstlen )
1183 int len, i;
1185 if (flags)
1187 SetLastError( ERROR_INVALID_FLAGS );
1188 return 0;
1190 if (!dstlen) return srclen;
1191 len = min( srclen, dstlen );
1192 for (i = 0; i < len; i++)
1194 unsigned char c = src[i];
1195 dst[i] = (c < 0x20) ? c : c + 0xf000;
1197 if (len < srclen)
1199 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1200 return 0;
1202 return len;
1206 static int mbstowcs_utf7( DWORD flags, const char *src, int srclen, WCHAR *dst, int dstlen )
1208 static const signed char base64_decoding_table[] =
1210 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x00-0x0F */
1211 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* 0x10-0x1F */
1212 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20-0x2F */
1213 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30-0x3F */
1214 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40-0x4F */
1215 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50-0x5F */
1216 -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60-0x6F */
1217 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1 /* 0x70-0x7F */
1220 const char *source_end = src + srclen;
1221 int offset = 0, pos = 0;
1222 DWORD byte_pair = 0;
1224 if (flags)
1226 SetLastError( ERROR_INVALID_FLAGS );
1227 return 0;
1229 #define OUTPUT(ch) \
1230 do { \
1231 if (dstlen > 0) \
1233 if (pos >= dstlen) goto overflow; \
1234 dst[pos] = (ch); \
1236 pos++; \
1237 } while(0)
1239 while (src < source_end)
1241 if (*src == '+')
1243 src++;
1244 if (src >= source_end) break;
1245 if (*src == '-')
1247 /* just a plus sign escaped as +- */
1248 OUTPUT( '+' );
1249 src++;
1250 continue;
1255 signed char sextet = *src;
1256 if (sextet == '-')
1258 /* skip over the dash and end base64 decoding
1259 * the current, unfinished byte pair is discarded */
1260 src++;
1261 offset = 0;
1262 break;
1264 if (sextet < 0)
1266 /* the next character of src is < 0 and therefore not part of a base64 sequence
1267 * the current, unfinished byte pair is NOT discarded in this case
1268 * this is probably a bug in Windows */
1269 break;
1271 sextet = base64_decoding_table[sextet];
1272 if (sextet == -1)
1274 /* -1 means that the next character of src is not part of a base64 sequence
1275 * in other words, all sextets in this base64 sequence have been processed
1276 * the current, unfinished byte pair is discarded */
1277 offset = 0;
1278 break;
1281 byte_pair = (byte_pair << 6) | sextet;
1282 offset += 6;
1283 if (offset >= 16)
1285 /* this byte pair is done */
1286 OUTPUT( byte_pair >> (offset - 16) );
1287 offset -= 16;
1289 src++;
1291 while (src < source_end);
1293 else
1295 OUTPUT( (unsigned char)*src );
1296 src++;
1299 return pos;
1301 overflow:
1302 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1303 return 0;
1304 #undef OUTPUT
1308 static int mbstowcs_utf8( DWORD flags, const char *src, int srclen, WCHAR *dst, int dstlen )
1310 DWORD reslen;
1311 NTSTATUS status;
1313 if (flags & ~(MB_PRECOMPOSED | MB_COMPOSITE | MB_USEGLYPHCHARS | MB_ERR_INVALID_CHARS))
1315 SetLastError( ERROR_INVALID_FLAGS );
1316 return 0;
1318 if (!dstlen) dst = NULL;
1319 status = RtlUTF8ToUnicodeN( dst, dstlen * sizeof(WCHAR), &reslen, src, srclen );
1320 if (status == STATUS_SOME_NOT_MAPPED)
1322 if (flags & MB_ERR_INVALID_CHARS)
1324 SetLastError( ERROR_NO_UNICODE_TRANSLATION );
1325 return 0;
1328 else if (!set_ntstatus( status )) reslen = 0;
1330 return reslen / sizeof(WCHAR);
1334 static inline int is_private_use_area_char( WCHAR code )
1336 return (code >= 0xe000 && code <= 0xf8ff);
1340 static int check_invalid_chars( const CPTABLEINFO *info, const unsigned char *src, int srclen )
1342 if (info->DBCSOffsets)
1344 for ( ; srclen; src++, srclen-- )
1346 USHORT off = info->DBCSOffsets[*src];
1347 if (off)
1349 if (srclen == 1) break; /* partial char, error */
1350 if (info->DBCSOffsets[off + src[1]] == info->UniDefaultChar &&
1351 ((src[0] << 8) | src[1]) != info->TransUniDefaultChar) break;
1352 src++;
1353 srclen--;
1354 continue;
1356 if (info->MultiByteTable[*src] == info->UniDefaultChar && *src != info->TransUniDefaultChar)
1357 break;
1358 if (is_private_use_area_char( info->MultiByteTable[*src] )) break;
1361 else
1363 for ( ; srclen; src++, srclen-- )
1365 if (info->MultiByteTable[*src] == info->UniDefaultChar && *src != info->TransUniDefaultChar)
1366 break;
1367 if (is_private_use_area_char( info->MultiByteTable[*src] )) break;
1370 return !!srclen;
1375 static int mbstowcs_decompose( const CPTABLEINFO *info, const unsigned char *src, int srclen,
1376 WCHAR *dst, int dstlen )
1378 WCHAR ch;
1379 USHORT off;
1380 int len;
1381 const WCHAR *decomp;
1382 unsigned int decomp_len;
1384 if (info->DBCSOffsets)
1386 if (!dstlen) /* compute length */
1388 for (len = 0; srclen; srclen--, src++, len += decomp_len)
1390 if ((off = info->DBCSOffsets[*src]))
1392 if (srclen > 1 && src[1])
1394 src++;
1395 srclen--;
1396 ch = info->DBCSOffsets[off + *src];
1398 else ch = info->UniDefaultChar;
1400 else ch = info->MultiByteTable[*src];
1401 get_decomposition( ch, &decomp_len );
1403 return len;
1406 for (len = dstlen; srclen && len; srclen--, src++, dst += decomp_len, len -= decomp_len)
1408 if ((off = info->DBCSOffsets[*src]))
1410 if (srclen > 1 && src[1])
1412 src++;
1413 srclen--;
1414 ch = info->DBCSOffsets[off + *src];
1416 else ch = info->UniDefaultChar;
1418 else ch = info->MultiByteTable[*src];
1420 if ((decomp = get_decomposition( ch, &decomp_len )))
1422 if (len < decomp_len) break;
1423 memcpy( dst, decomp, decomp_len * sizeof(WCHAR) );
1425 else *dst = ch;
1428 else
1430 if (!dstlen) /* compute length */
1432 for (len = 0; srclen; srclen--, src++, len += decomp_len)
1433 get_decomposition( info->MultiByteTable[*src], &decomp_len );
1434 return len;
1437 for (len = dstlen; srclen && len; srclen--, src++, dst += decomp_len, len -= decomp_len)
1439 ch = info->MultiByteTable[*src];
1440 if ((decomp = get_decomposition( ch, &decomp_len )))
1442 if (len < decomp_len) break;
1443 memcpy( dst, decomp, decomp_len * sizeof(WCHAR) );
1445 else *dst = ch;
1449 if (srclen)
1451 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1452 return 0;
1454 return dstlen - len;
1458 static int mbstowcs_sbcs( const CPTABLEINFO *info, const unsigned char *src, int srclen,
1459 WCHAR *dst, int dstlen )
1461 const USHORT *table = info->MultiByteTable;
1462 int ret = srclen;
1464 if (!dstlen) return srclen;
1466 if (dstlen < srclen) /* buffer too small: fill it up to dstlen and return error */
1468 srclen = dstlen;
1469 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1470 ret = 0;
1473 while (srclen >= 16)
1475 dst[0] = table[src[0]];
1476 dst[1] = table[src[1]];
1477 dst[2] = table[src[2]];
1478 dst[3] = table[src[3]];
1479 dst[4] = table[src[4]];
1480 dst[5] = table[src[5]];
1481 dst[6] = table[src[6]];
1482 dst[7] = table[src[7]];
1483 dst[8] = table[src[8]];
1484 dst[9] = table[src[9]];
1485 dst[10] = table[src[10]];
1486 dst[11] = table[src[11]];
1487 dst[12] = table[src[12]];
1488 dst[13] = table[src[13]];
1489 dst[14] = table[src[14]];
1490 dst[15] = table[src[15]];
1491 src += 16;
1492 dst += 16;
1493 srclen -= 16;
1496 /* now handle the remaining characters */
1497 src += srclen;
1498 dst += srclen;
1499 switch (srclen)
1501 case 15: dst[-15] = table[src[-15]];
1502 case 14: dst[-14] = table[src[-14]];
1503 case 13: dst[-13] = table[src[-13]];
1504 case 12: dst[-12] = table[src[-12]];
1505 case 11: dst[-11] = table[src[-11]];
1506 case 10: dst[-10] = table[src[-10]];
1507 case 9: dst[-9] = table[src[-9]];
1508 case 8: dst[-8] = table[src[-8]];
1509 case 7: dst[-7] = table[src[-7]];
1510 case 6: dst[-6] = table[src[-6]];
1511 case 5: dst[-5] = table[src[-5]];
1512 case 4: dst[-4] = table[src[-4]];
1513 case 3: dst[-3] = table[src[-3]];
1514 case 2: dst[-2] = table[src[-2]];
1515 case 1: dst[-1] = table[src[-1]];
1516 case 0: break;
1518 return ret;
1522 static int mbstowcs_dbcs( const CPTABLEINFO *info, const unsigned char *src, int srclen,
1523 WCHAR *dst, int dstlen )
1525 USHORT off;
1526 int i;
1528 if (!dstlen)
1530 for (i = 0; srclen; i++, src++, srclen--)
1531 if (info->DBCSOffsets[*src] && srclen > 1 && src[1]) { src++; srclen--; }
1532 return i;
1535 for (i = dstlen; srclen && i; i--, srclen--, src++, dst++)
1537 if ((off = info->DBCSOffsets[*src]))
1539 if (srclen > 1 && src[1])
1541 src++;
1542 srclen--;
1543 *dst = info->DBCSOffsets[off + *src];
1545 else *dst = info->UniDefaultChar;
1547 else *dst = info->MultiByteTable[*src];
1549 if (srclen)
1551 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1552 return 0;
1554 return dstlen - i;
1558 static int mbstowcs_codepage( UINT codepage, DWORD flags, const char *src, int srclen,
1559 WCHAR *dst, int dstlen )
1561 CPTABLEINFO local_info;
1562 const CPTABLEINFO *info = get_codepage_table( codepage );
1563 const unsigned char *str = (const unsigned char *)src;
1565 if (!info)
1567 SetLastError( ERROR_INVALID_PARAMETER );
1568 return 0;
1570 if (flags & ~(MB_PRECOMPOSED | MB_COMPOSITE | MB_USEGLYPHCHARS | MB_ERR_INVALID_CHARS))
1572 SetLastError( ERROR_INVALID_FLAGS );
1573 return 0;
1576 if ((flags & MB_USEGLYPHCHARS) && info->MultiByteTable[256] == 256)
1578 local_info = *info;
1579 local_info.MultiByteTable += 257;
1580 info = &local_info;
1582 if ((flags & MB_ERR_INVALID_CHARS) && check_invalid_chars( info, str, srclen ))
1584 SetLastError( ERROR_NO_UNICODE_TRANSLATION );
1585 return 0;
1588 if (flags & MB_COMPOSITE) return mbstowcs_decompose( info, str, srclen, dst, dstlen );
1590 if (info->DBCSOffsets)
1591 return mbstowcs_dbcs( info, str, srclen, dst, dstlen );
1592 else
1593 return mbstowcs_sbcs( info, str, srclen, dst, dstlen );
1597 static int wcstombs_cpsymbol( DWORD flags, const WCHAR *src, int srclen, char *dst, int dstlen,
1598 const char *defchar, BOOL *used )
1600 int len, i;
1602 if (flags)
1604 SetLastError( ERROR_INVALID_FLAGS );
1605 return 0;
1607 if (defchar || used)
1609 SetLastError( ERROR_INVALID_PARAMETER );
1610 return 0;
1612 if (!dstlen) return srclen;
1613 len = min( srclen, dstlen );
1614 for (i = 0; i < len; i++)
1616 if (src[i] < 0x20) dst[i] = src[i];
1617 else if (src[i] >= 0xf020 && src[i] < 0xf100) dst[i] = src[i] - 0xf000;
1618 else
1620 SetLastError( ERROR_NO_UNICODE_TRANSLATION );
1621 return 0;
1624 if (srclen > len)
1626 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1627 return 0;
1629 return len;
1633 static int wcstombs_utf7( DWORD flags, const WCHAR *src, int srclen, char *dst, int dstlen,
1634 const char *defchar, BOOL *used )
1636 static const char directly_encodable[] =
1638 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, /* 0x00 - 0x0f */
1639 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x10 - 0x1f */
1640 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, /* 0x20 - 0x2f */
1641 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x30 - 0x3f */
1642 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4f */
1643 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, /* 0x50 - 0x5f */
1644 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6f */
1645 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 /* 0x70 - 0x7a */
1647 #define ENCODABLE(ch) ((ch) <= 0x7a && directly_encodable[(ch)])
1649 static const char base64_encoding_table[] =
1650 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1652 const WCHAR *source_end = src + srclen;
1653 int pos = 0;
1655 if (defchar || used)
1657 SetLastError( ERROR_INVALID_PARAMETER );
1658 return 0;
1660 if (flags)
1662 SetLastError( ERROR_INVALID_FLAGS );
1663 return 0;
1666 #define OUTPUT(ch) \
1667 do { \
1668 if (dstlen > 0) \
1670 if (pos >= dstlen) goto overflow; \
1671 dst[pos] = (ch); \
1673 pos++; \
1674 } while (0)
1676 while (src < source_end)
1678 if (*src == '+')
1680 OUTPUT( '+' );
1681 OUTPUT( '-' );
1682 src++;
1684 else if (ENCODABLE(*src))
1686 OUTPUT( *src );
1687 src++;
1689 else
1691 unsigned int offset = 0, byte_pair = 0;
1693 OUTPUT( '+' );
1694 while (src < source_end && !ENCODABLE(*src))
1696 byte_pair = (byte_pair << 16) | *src;
1697 offset += 16;
1698 while (offset >= 6)
1700 offset -= 6;
1701 OUTPUT( base64_encoding_table[(byte_pair >> offset) & 0x3f] );
1703 src++;
1705 if (offset)
1707 /* Windows won't create a padded base64 character if there's no room for the - sign
1708 * as well ; this is probably a bug in Windows */
1709 if (dstlen > 0 && pos + 1 >= dstlen) goto overflow;
1710 byte_pair <<= (6 - offset);
1711 OUTPUT( base64_encoding_table[byte_pair & 0x3f] );
1713 /* Windows always explicitly terminates the base64 sequence
1714 even though RFC 2152 (page 3, rule 2) does not require this */
1715 OUTPUT( '-' );
1718 return pos;
1720 overflow:
1721 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1722 return 0;
1723 #undef OUTPUT
1724 #undef ENCODABLE
1728 static int wcstombs_utf8( DWORD flags, const WCHAR *src, int srclen, char *dst, int dstlen,
1729 const char *defchar, BOOL *used )
1731 DWORD reslen;
1732 NTSTATUS status;
1734 if (defchar || used)
1736 SetLastError( ERROR_INVALID_PARAMETER );
1737 return 0;
1739 if (flags & ~(WC_DISCARDNS | WC_SEPCHARS | WC_DEFAULTCHAR | WC_ERR_INVALID_CHARS |
1740 WC_COMPOSITECHECK | WC_NO_BEST_FIT_CHARS))
1742 SetLastError( ERROR_INVALID_FLAGS );
1743 return 0;
1745 if (!dstlen) dst = NULL;
1746 status = RtlUnicodeToUTF8N( dst, dstlen, &reslen, src, srclen * sizeof(WCHAR) );
1747 if (status == STATUS_SOME_NOT_MAPPED)
1749 if (flags & WC_ERR_INVALID_CHARS)
1751 SetLastError( ERROR_NO_UNICODE_TRANSLATION );
1752 return 0;
1755 else if (!set_ntstatus( status )) reslen = 0;
1756 return reslen;
1760 static int wcstombs_sbcs( const CPTABLEINFO *info, const WCHAR *src, unsigned int srclen,
1761 char *dst, unsigned int dstlen )
1763 const char *table = info->WideCharTable;
1764 int ret = srclen;
1766 if (!dstlen) return srclen;
1768 if (dstlen < srclen)
1770 /* buffer too small: fill it up to dstlen and return error */
1771 srclen = dstlen;
1772 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1773 ret = 0;
1776 while (srclen >= 16)
1778 dst[0] = table[src[0]];
1779 dst[1] = table[src[1]];
1780 dst[2] = table[src[2]];
1781 dst[3] = table[src[3]];
1782 dst[4] = table[src[4]];
1783 dst[5] = table[src[5]];
1784 dst[6] = table[src[6]];
1785 dst[7] = table[src[7]];
1786 dst[8] = table[src[8]];
1787 dst[9] = table[src[9]];
1788 dst[10] = table[src[10]];
1789 dst[11] = table[src[11]];
1790 dst[12] = table[src[12]];
1791 dst[13] = table[src[13]];
1792 dst[14] = table[src[14]];
1793 dst[15] = table[src[15]];
1794 src += 16;
1795 dst += 16;
1796 srclen -= 16;
1799 /* now handle remaining characters */
1800 src += srclen;
1801 dst += srclen;
1802 switch(srclen)
1804 case 15: dst[-15] = table[src[-15]];
1805 case 14: dst[-14] = table[src[-14]];
1806 case 13: dst[-13] = table[src[-13]];
1807 case 12: dst[-12] = table[src[-12]];
1808 case 11: dst[-11] = table[src[-11]];
1809 case 10: dst[-10] = table[src[-10]];
1810 case 9: dst[-9] = table[src[-9]];
1811 case 8: dst[-8] = table[src[-8]];
1812 case 7: dst[-7] = table[src[-7]];
1813 case 6: dst[-6] = table[src[-6]];
1814 case 5: dst[-5] = table[src[-5]];
1815 case 4: dst[-4] = table[src[-4]];
1816 case 3: dst[-3] = table[src[-3]];
1817 case 2: dst[-2] = table[src[-2]];
1818 case 1: dst[-1] = table[src[-1]];
1819 case 0: break;
1821 return ret;
1825 static int wcstombs_dbcs( const CPTABLEINFO *info, const WCHAR *src, unsigned int srclen,
1826 char *dst, unsigned int dstlen )
1828 const USHORT *table = info->WideCharTable;
1829 int i;
1831 if (!dstlen)
1833 for (i = 0; srclen; src++, srclen--, i++) if (table[*src] & 0xff00) i++;
1834 return i;
1837 for (i = dstlen; srclen && i; i--, srclen--, src++)
1839 if (table[*src] & 0xff00)
1841 if (i == 1) break; /* do not output a partial char */
1842 i--;
1843 *dst++ = table[*src] >> 8;
1845 *dst++ = (char)table[*src];
1847 if (srclen)
1849 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1850 return 0;
1852 return dstlen - i;
1856 static inline int is_valid_sbcs_mapping( const CPTABLEINFO *info, DWORD flags, unsigned int wch )
1858 const unsigned char *table = info->WideCharTable;
1860 if (wch >= 0x10000) return 0;
1861 if ((flags & WC_NO_BEST_FIT_CHARS) || table[wch] == info->DefaultChar)
1862 return (info->MultiByteTable[table[wch]] == wch);
1863 return 1;
1867 static inline int is_valid_dbcs_mapping( const CPTABLEINFO *info, DWORD flags, unsigned int wch )
1869 const unsigned short *table = info->WideCharTable;
1870 unsigned short ch;
1872 if (wch >= 0x10000) return 0;
1873 ch = table[wch];
1874 if ((flags & WC_NO_BEST_FIT_CHARS) || ch == info->DefaultChar)
1876 if (ch >> 8) return info->DBCSOffsets[info->DBCSOffsets[ch >> 8] + (ch & 0xff)] == wch;
1877 return info->MultiByteTable[ch] == wch;
1879 return 1;
1883 static int wcstombs_sbcs_slow( const CPTABLEINFO *info, DWORD flags, const WCHAR *src, unsigned int srclen,
1884 char *dst, unsigned int dstlen, const char *defchar, BOOL *used )
1886 const char *table = info->WideCharTable;
1887 const char def = defchar ? *defchar : (char)info->DefaultChar;
1888 int i;
1889 BOOL tmp;
1890 WCHAR wch;
1891 unsigned int composed;
1893 if (!used) used = &tmp; /* avoid checking on every char */
1894 *used = FALSE;
1896 if (!dstlen)
1898 for (i = 0; srclen; i++, src++, srclen--)
1900 wch = *src;
1901 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose_chars( src[0], src[1] )))
1903 /* now check if we can use the composed char */
1904 if (is_valid_sbcs_mapping( info, flags, composed ))
1906 /* we have a good mapping, use it */
1907 src++;
1908 srclen--;
1909 continue;
1911 /* no mapping for the composed char, check the other flags */
1912 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
1914 *used = TRUE;
1915 src++; /* skip the non-spacing char */
1916 srclen--;
1917 continue;
1919 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
1921 src++;
1922 srclen--;
1924 /* WC_SEPCHARS is the default */
1926 if (!*used) *used = !is_valid_sbcs_mapping( info, flags, wch );
1928 return i;
1931 for (i = dstlen; srclen && i; dst++, i--, src++, srclen--)
1933 wch = *src;
1934 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose_chars( src[0], src[1] )))
1936 /* now check if we can use the composed char */
1937 if (is_valid_sbcs_mapping( info, flags, composed ))
1939 /* we have a good mapping, use it */
1940 *dst = table[composed];
1941 src++;
1942 srclen--;
1943 continue;
1945 /* no mapping for the composed char, check the other flags */
1946 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
1948 *dst = def;
1949 *used = TRUE;
1950 src++; /* skip the non-spacing char */
1951 srclen--;
1952 continue;
1954 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
1956 src++;
1957 srclen--;
1959 /* WC_SEPCHARS is the default */
1962 *dst = table[wch];
1963 if (!is_valid_sbcs_mapping( info, flags, wch ))
1965 *dst = def;
1966 *used = TRUE;
1969 if (srclen)
1971 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1972 return 0;
1974 return dstlen - i;
1978 static int wcstombs_dbcs_slow( const CPTABLEINFO *info, DWORD flags, const WCHAR *src, unsigned int srclen,
1979 char *dst, unsigned int dstlen, const char *defchar, BOOL *used )
1981 const USHORT *table = info->WideCharTable;
1982 WCHAR wch, defchar_value;
1983 unsigned int composed;
1984 unsigned short res;
1985 BOOL tmp;
1986 int i;
1988 if (!defchar[1]) defchar_value = (unsigned char)defchar[0];
1989 else defchar_value = ((unsigned char)defchar[0] << 8) | (unsigned char)defchar[1];
1991 if (!used) used = &tmp; /* avoid checking on every char */
1992 *used = FALSE;
1994 if (!dstlen)
1996 if (!defchar && !used && !(flags & WC_COMPOSITECHECK))
1998 for (i = 0; srclen; srclen--, src++, i++) if (table[*src] & 0xff00) i++;
1999 return i;
2001 for (i = 0; srclen; srclen--, src++, i++)
2003 wch = *src;
2004 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose_chars( src[0], src[1] )))
2006 /* now check if we can use the composed char */
2007 if (is_valid_dbcs_mapping( info, flags, composed ))
2009 /* we have a good mapping for the composed char, use it */
2010 res = table[composed];
2011 if (res & 0xff00) i++;
2012 src++;
2013 srclen--;
2014 continue;
2016 /* no mapping for the composed char, check the other flags */
2017 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
2019 if (defchar_value & 0xff00) i++;
2020 *used = TRUE;
2021 src++; /* skip the non-spacing char */
2022 srclen--;
2023 continue;
2025 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
2027 src++;
2028 srclen--;
2030 /* WC_SEPCHARS is the default */
2033 res = table[wch];
2034 if (!is_valid_dbcs_mapping( info, flags, wch ))
2036 res = defchar_value;
2037 *used = TRUE;
2039 if (res & 0xff00) i++;
2041 return i;
2045 for (i = dstlen; srclen && i; i--, srclen--, src++)
2047 wch = *src;
2048 if ((flags & WC_COMPOSITECHECK) && (srclen > 1) && (composed = compose_chars( src[0], src[1] )))
2050 /* now check if we can use the composed char */
2051 if (is_valid_dbcs_mapping( info, flags, composed ))
2053 /* we have a good mapping for the composed char, use it */
2054 res = table[composed];
2055 src++;
2056 srclen--;
2057 goto output_char;
2059 /* no mapping for the composed char, check the other flags */
2060 if (flags & WC_DEFAULTCHAR) /* use the default char instead */
2062 res = defchar_value;
2063 *used = TRUE;
2064 src++; /* skip the non-spacing char */
2065 srclen--;
2066 goto output_char;
2068 if (flags & WC_DISCARDNS) /* skip the second char of the composition */
2070 src++;
2071 srclen--;
2073 /* WC_SEPCHARS is the default */
2076 res = table[wch];
2077 if (!is_valid_dbcs_mapping( info, flags, wch ))
2079 res = defchar_value;
2080 *used = TRUE;
2083 output_char:
2084 if (res & 0xff00)
2086 if (i == 1) break; /* do not output a partial char */
2087 i--;
2088 *dst++ = res >> 8;
2090 *dst++ = (char)res;
2092 if (srclen)
2094 SetLastError( ERROR_INSUFFICIENT_BUFFER );
2095 return 0;
2097 return dstlen - i;
2101 static int wcstombs_codepage( UINT codepage, DWORD flags, const WCHAR *src, int srclen,
2102 char *dst, int dstlen, const char *defchar, BOOL *used )
2104 const CPTABLEINFO *info = get_codepage_table( codepage );
2106 if (!info)
2108 SetLastError( ERROR_INVALID_PARAMETER );
2109 return 0;
2111 if (flags & ~(WC_DISCARDNS | WC_SEPCHARS | WC_DEFAULTCHAR | WC_ERR_INVALID_CHARS |
2112 WC_COMPOSITECHECK | WC_NO_BEST_FIT_CHARS))
2114 SetLastError( ERROR_INVALID_FLAGS );
2115 return 0;
2117 if (flags || defchar || used)
2119 if (!defchar) defchar = (const char *)&info->DefaultChar;
2120 if (info->DBCSOffsets)
2121 return wcstombs_dbcs_slow( info, flags, src, srclen, dst, dstlen, defchar, used );
2122 else
2123 return wcstombs_sbcs_slow( info, flags, src, srclen, dst, dstlen, defchar, used );
2125 if (info->DBCSOffsets)
2126 return wcstombs_dbcs( info, src, srclen, dst, dstlen );
2127 else
2128 return wcstombs_sbcs( info, src, srclen, dst, dstlen );
2132 static int get_sortkey( DWORD flags, const WCHAR *src, int srclen, char *dst, int dstlen )
2134 WCHAR dummy[4]; /* no decomposition is larger than 4 chars */
2135 int key_len[4];
2136 char *key_ptr[4];
2137 const WCHAR *src_save = src;
2138 int srclen_save = srclen;
2140 key_len[0] = key_len[1] = key_len[2] = key_len[3] = 0;
2141 for (; srclen; srclen--, src++)
2143 unsigned int i, decomposed_len = 1;/*wine_decompose(*src, dummy, 4);*/
2144 dummy[0] = *src;
2145 if (decomposed_len)
2147 for (i = 0; i < decomposed_len; i++)
2149 WCHAR wch = dummy[i];
2150 unsigned int ce;
2152 if ((flags & NORM_IGNORESYMBOLS) &&
2153 (get_char_type( CT_CTYPE1, wch ) & (C1_PUNCT | C1_SPACE)))
2154 continue;
2156 if (flags & NORM_IGNORECASE) wch = casemap( nls_info.LowerCaseTable, wch );
2158 ce = collation_table[collation_table[collation_table[wch >> 8] + ((wch >> 4) & 0x0f)] + (wch & 0xf)];
2159 if (ce != (unsigned int)-1)
2161 if (ce >> 16) key_len[0] += 2;
2162 if ((ce >> 8) & 0xff) key_len[1]++;
2163 if ((ce >> 4) & 0x0f) key_len[2]++;
2164 if (ce & 1)
2166 if (wch >> 8) key_len[3]++;
2167 key_len[3]++;
2170 else
2172 key_len[0] += 2;
2173 if (wch >> 8) key_len[0]++;
2174 if (wch & 0xff) key_len[0]++;
2180 if (!dstlen) /* compute length */
2181 /* 4 * '\1' + key length */
2182 return key_len[0] + key_len[1] + key_len[2] + key_len[3] + 4;
2184 if (dstlen < key_len[0] + key_len[1] + key_len[2] + key_len[3] + 4 + 1)
2185 return 0; /* overflow */
2187 src = src_save;
2188 srclen = srclen_save;
2190 key_ptr[0] = dst;
2191 key_ptr[1] = key_ptr[0] + key_len[0] + 1;
2192 key_ptr[2] = key_ptr[1] + key_len[1] + 1;
2193 key_ptr[3] = key_ptr[2] + key_len[2] + 1;
2195 for (; srclen; srclen--, src++)
2197 unsigned int i, decomposed_len = 1;/*wine_decompose(*src, dummy, 4);*/
2198 dummy[0] = *src;
2199 if (decomposed_len)
2201 for (i = 0; i < decomposed_len; i++)
2203 WCHAR wch = dummy[i];
2204 unsigned int ce;
2206 if ((flags & NORM_IGNORESYMBOLS) &&
2207 (get_char_type( CT_CTYPE1, wch ) & (C1_PUNCT | C1_SPACE)))
2208 continue;
2210 if (flags & NORM_IGNORECASE) wch = casemap( nls_info.LowerCaseTable, wch );
2212 ce = collation_table[collation_table[collation_table[wch >> 8] + ((wch >> 4) & 0x0f)] + (wch & 0xf)];
2213 if (ce != (unsigned int)-1)
2215 WCHAR key;
2216 if ((key = ce >> 16))
2218 *key_ptr[0]++ = key >> 8;
2219 *key_ptr[0]++ = key & 0xff;
2221 /* make key 1 start from 2 */
2222 if ((key = (ce >> 8) & 0xff)) *key_ptr[1]++ = key + 1;
2223 /* make key 2 start from 2 */
2224 if ((key = (ce >> 4) & 0x0f)) *key_ptr[2]++ = key + 1;
2225 /* key 3 is always a character code */
2226 if (ce & 1)
2228 if (wch >> 8) *key_ptr[3]++ = wch >> 8;
2229 if (wch & 0xff) *key_ptr[3]++ = wch & 0xff;
2232 else
2234 *key_ptr[0]++ = 0xff;
2235 *key_ptr[0]++ = 0xfe;
2236 if (wch >> 8) *key_ptr[0]++ = wch >> 8;
2237 if (wch & 0xff) *key_ptr[0]++ = wch & 0xff;
2243 *key_ptr[0] = 1;
2244 *key_ptr[1] = 1;
2245 *key_ptr[2] = 1;
2246 *key_ptr[3]++ = 1;
2247 *key_ptr[3] = 0;
2248 return key_ptr[3] - dst;
2252 /* compose a full-width katakana. return consumed source characters. */
2253 static int compose_katakana( const WCHAR *src, int srclen, WCHAR *dst )
2255 static const BYTE katakana_map[] =
2257 /* */ 0x02, 0x0c, 0x0d, 0x01, 0xfb, 0xf2, 0xa1, /* U+FF61- */
2258 0xa3, 0xa5, 0xa7, 0xa9, 0xe3, 0xe5, 0xe7, 0xc3, /* U+FF68- */
2259 0xfc, 0xa2, 0xa4, 0xa6, 0xa8, 0xaa, 0xab, 0xad, /* U+FF70- */
2260 0xaf, 0xb1, 0xb3, 0xb5, 0xb7, 0xb9, 0xbb, 0xbd, /* U+FF78- */
2261 0xbf, 0xc1, 0xc4, 0xc6, 0xc8, 0xca, 0xcb, 0xcc, /* U+FF80- */
2262 0xcd, 0xce, 0xcf, 0xd2, 0xd5, 0xd8, 0xdb, 0xde, /* U+FF88- */
2263 0xdf, 0xe0, 0xe1, 0xe2, 0xe4, 0xe6, 0xe8, 0xe9, /* U+FF90- */
2264 0xea, 0xeb, 0xec, 0xed, 0xef, 0xf3, 0x99, 0x9a, /* U+FF98- */
2266 WCHAR dummy;
2267 int shift;
2269 if (!dst) dst = &dummy;
2271 switch (*src)
2273 case 0x309b:
2274 case 0x309c:
2275 *dst = *src - 2;
2276 return 1;
2277 case 0x30f0:
2278 case 0x30f1:
2279 case 0x30fd:
2280 *dst = *src;
2281 break;
2282 default:
2283 shift = *src - 0xff61;
2284 if (shift < 0 || shift >= ARRAY_SIZE( katakana_map )) return 0;
2285 *dst = katakana_map[shift] | 0x3000;
2286 break;
2289 if (srclen <= 1) return 1;
2291 switch (src[1])
2293 case 0xff9e: /* datakuten (voiced sound) */
2294 if ((*src >= 0xff76 && *src <= 0xff84) || (*src >= 0xff8a && *src <= 0xff8e) || *src == 0x30fd)
2295 *dst += 1;
2296 else if (*src == 0xff73)
2297 *dst = 0x30f4; /* KATAKANA LETTER VU */
2298 else if (*src == 0xff9c)
2299 *dst = 0x30f7; /* KATAKANA LETTER VA */
2300 else if (*src == 0x30f0)
2301 *dst = 0x30f8; /* KATAKANA LETTER VI */
2302 else if (*src == 0x30f1)
2303 *dst = 0x30f9; /* KATAKANA LETTER VE */
2304 else if (*src == 0xff66)
2305 *dst = 0x30fa; /* KATAKANA LETTER VO */
2306 else
2307 return 1;
2308 break;
2309 case 0xff9f: /* handakuten (semi-voiced sound) */
2310 if (*src >= 0xff8a && *src <= 0xff8e)
2311 *dst += 2;
2312 else
2313 return 1;
2314 break;
2315 default:
2316 return 1;
2318 return 2;
2321 /* map one or two half-width characters to one full-width character */
2322 static int map_to_fullwidth( const WCHAR *src, int srclen, WCHAR *dst )
2324 INT n;
2326 if (*src <= '~' && *src > ' ' && *src != '\\')
2327 *dst = *src - 0x20 + 0xff00;
2328 else if (*src == ' ')
2329 *dst = 0x3000;
2330 else if (*src <= 0x00af && *src >= 0x00a2)
2332 static const BYTE misc_symbols_table[] =
2334 0xe0, 0xe1, 0x00, 0xe5, 0xe4, 0x00, 0x00, /* U+00A2- */
2335 0x00, 0x00, 0x00, 0xe2, 0x00, 0x00, 0xe3 /* U+00A9- */
2337 if (misc_symbols_table[*src - 0x00a2])
2338 *dst = misc_symbols_table[*src - 0x00a2] | 0xff00;
2339 else
2340 *dst = *src;
2342 else if (*src == 0x20a9) /* WON SIGN */
2343 *dst = 0xffe6;
2344 else if ((n = compose_katakana(src, srclen, dst)) > 0)
2345 return n;
2346 else if (*src >= 0xffa0 && *src <= 0xffdc)
2348 static const BYTE hangul_mapping_table[] =
2350 0x64, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, /* U+FFA0- */
2351 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f, /* U+FFA8- */
2352 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, /* U+FFB0- */
2353 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x00, /* U+FFB8- */
2354 0x00, 0x00, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, /* U+FFC0- */
2355 0x00, 0x00, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, /* U+FFC8- */
2356 0x00, 0x00, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, /* U+FFD0- */
2357 0x00, 0x00, 0x61, 0x62, 0x63 /* U+FFD8- */
2360 if (hangul_mapping_table[*src - 0xffa0])
2361 *dst = hangul_mapping_table[*src - 0xffa0] | 0x3100;
2362 else
2363 *dst = *src;
2365 else
2366 *dst = *src;
2368 return 1;
2371 /* decompose a full-width katakana character into one or two half-width characters. */
2372 static int decompose_katakana( WCHAR c, WCHAR *dst, int dstlen )
2374 static const BYTE katakana_map[] =
2376 /* */ 0x9e, 0x9f, 0x9e, 0x9f, 0x00, 0x00, 0x00, /* U+3099- */
2377 0x00, 0x67, 0x71, 0x68, 0x72, 0x69, 0x73, 0x6a, /* U+30a1- */
2378 0x74, 0x6b, 0x75, 0x76, 0x01, 0x77, 0x01, 0x78, /* U+30a8- */
2379 0x01, 0x79, 0x01, 0x7a, 0x01, 0x7b, 0x01, 0x7c, /* U+30b0- */
2380 0x01, 0x7d, 0x01, 0x7e, 0x01, 0x7f, 0x01, 0x80, /* U+30b8- */
2381 0x01, 0x81, 0x01, 0x6f, 0x82, 0x01, 0x83, 0x01, /* U+30c0- */
2382 0x84, 0x01, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, /* U+30c8- */
2383 0x01, 0x02, 0x8b, 0x01, 0x02, 0x8c, 0x01, 0x02, /* U+30d0- */
2384 0x8d, 0x01, 0x02, 0x8e, 0x01, 0x02, 0x8f, 0x90, /* U+30d8- */
2385 0x91, 0x92, 0x93, 0x6c, 0x94, 0x6d, 0x95, 0x6e, /* U+30e0- */
2386 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x00, 0x9c, /* U+30e8- */
2387 0x00, 0x00, 0x66, 0x9d, 0x4e, 0x00, 0x00, 0x08, /* U+30f0- */
2388 0x58, 0x58, 0x08, 0x65, 0x70, 0x00, 0x51 /* U+30f8- */
2390 int len = 0, shift = c - 0x3099;
2391 BYTE k;
2393 if (shift < 0 || shift >= ARRAY_SIZE( katakana_map )) return 0;
2395 k = katakana_map[shift];
2396 if (!k)
2398 if (dstlen > 0) *dst = c;
2399 len++;
2401 else if (k > 0x60)
2403 if (dstlen > 0) *dst = k | 0xff00;
2404 len++;
2406 else
2408 if (dstlen >= 2)
2410 dst[0] = (k > 0x50) ? (c - (k & 0xf)) : (katakana_map[shift - k] | 0xff00);
2411 dst[1] = (k == 2) ? 0xff9f : 0xff9e;
2413 len += 2;
2415 return len;
2418 /* map single full-width character to single or double half-width characters. */
2419 static int map_to_halfwidth( WCHAR c, WCHAR *dst, int dstlen )
2421 int n = decompose_katakana( c, dst, dstlen );
2422 if (n > 0) return n;
2424 if (c == 0x3000)
2425 *dst = ' ';
2426 else if (c == 0x3001)
2427 *dst = 0xff64;
2428 else if (c == 0x3002)
2429 *dst = 0xff61;
2430 else if (c == 0x300c || c == 0x300d)
2431 *dst = (c - 0x300c) + 0xff62;
2432 else if (c >= 0x3131 && c <= 0x3163)
2434 *dst = c - 0x3131 + 0xffa1;
2435 if (*dst >= 0xffbf) *dst += 3;
2436 if (*dst >= 0xffc8) *dst += 2;
2437 if (*dst >= 0xffd0) *dst += 2;
2438 if (*dst >= 0xffd8) *dst += 2;
2440 else if (c == 0x3164)
2441 *dst = 0xffa0;
2442 else if (c == 0x2019)
2443 *dst = '\'';
2444 else if (c == 0x201d)
2445 *dst = '"';
2446 else if (c > 0xff00 && c < 0xff5f && c != 0xff3c)
2447 *dst = c - 0xff00 + 0x20;
2448 else if (c >= 0xffe0 && c <= 0xffe6)
2450 static const WCHAR misc_symbol_map[] = { 0x00a2, 0x00a3, 0x00ac, 0x00af, 0x00a6, 0x00a5, 0x20a9 };
2451 *dst = misc_symbol_map[c - 0xffe0];
2453 else
2454 *dst = c;
2456 return 1;
2460 /* 32-bit collation element table format:
2461 * unicode weight - high 16 bit, diacritic weight - high 8 bit of low 16 bit,
2462 * case weight - high 4 bit of low 8 bit.
2465 enum weight { UNICODE_WEIGHT, DIACRITIC_WEIGHT, CASE_WEIGHT };
2467 static unsigned int get_weight( WCHAR ch, enum weight type )
2469 unsigned int ret;
2471 ret = collation_table[collation_table[collation_table[ch >> 8] + ((ch >> 4) & 0x0f)] + (ch & 0xf)];
2472 if (ret == ~0u) return ch;
2474 switch (type)
2476 case UNICODE_WEIGHT: return ret >> 16;
2477 case DIACRITIC_WEIGHT: return (ret >> 8) & 0xff;
2478 case CASE_WEIGHT: return (ret >> 4) & 0x0f;
2479 default: return 0;
2484 static void inc_str_pos( const WCHAR **str, int *len, unsigned int *dpos, unsigned int *dlen )
2486 (*dpos)++;
2487 if (*dpos == *dlen)
2489 *dpos = *dlen = 0;
2490 (*str)++;
2491 (*len)--;
2496 static int compare_weights(int flags, const WCHAR *str1, int len1,
2497 const WCHAR *str2, int len2, enum weight type )
2499 unsigned int ce1, ce2, dpos1 = 0, dpos2 = 0, dlen1 = 0, dlen2 = 0;
2500 const WCHAR *dstr1 = NULL, *dstr2 = NULL;
2502 while (len1 > 0 && len2 > 0)
2504 if (!dlen1 && !(dstr1 = get_decomposition( *str1, &dlen1 ))) dstr1 = str1;
2505 if (!dlen2 && !(dstr2 = get_decomposition( *str2, &dlen2 ))) dstr2 = str2;
2507 if (flags & NORM_IGNORESYMBOLS)
2509 int skip = 0;
2510 /* FIXME: not tested */
2511 if (get_char_type( CT_CTYPE1, dstr1[dpos1] ) & (C1_PUNCT | C1_SPACE))
2513 inc_str_pos( &str1, &len1, &dpos1, &dlen1 );
2514 skip = 1;
2516 if (get_char_type( CT_CTYPE1, dstr2[dpos2] ) & (C1_PUNCT | C1_SPACE))
2518 inc_str_pos( &str2, &len2, &dpos2, &dlen2 );
2519 skip = 1;
2521 if (skip) continue;
2524 /* hyphen and apostrophe are treated differently depending on
2525 * whether SORT_STRINGSORT specified or not
2527 if (type == UNICODE_WEIGHT && !(flags & SORT_STRINGSORT))
2529 if (dstr1[dpos1] == '-' || dstr1[dpos1] == '\'')
2531 if (dstr2[dpos2] != '-' && dstr2[dpos2] != '\'')
2533 inc_str_pos( &str1, &len1, &dpos1, &dlen1 );
2534 continue;
2537 else if (dstr2[dpos2] == '-' || dstr2[dpos2] == '\'')
2539 inc_str_pos( &str2, &len2, &dpos2, &dlen2 );
2540 continue;
2544 ce1 = get_weight( dstr1[dpos1], type );
2545 if (!ce1)
2547 inc_str_pos( &str1, &len1, &dpos1, &dlen1 );
2548 continue;
2550 ce2 = get_weight( dstr2[dpos2], type );
2551 if (!ce2)
2553 inc_str_pos( &str2, &len2, &dpos2, &dlen2 );
2554 continue;
2557 if (ce1 - ce2) return ce1 - ce2;
2559 inc_str_pos( &str1, &len1, &dpos1, &dlen1 );
2560 inc_str_pos( &str2, &len2, &dpos2, &dlen2 );
2562 while (len1)
2564 if (!dlen1 && !(dstr1 = get_decomposition( *str1, &dlen1 ))) dstr1 = str1;
2565 ce1 = get_weight( dstr1[dpos1], type );
2566 if (ce1) break;
2567 inc_str_pos( &str1, &len1, &dpos1, &dlen1 );
2569 while (len2)
2571 if (!dlen2 && !(dstr2 = get_decomposition( *str2, &dlen2 ))) dstr2 = str2;
2572 ce2 = get_weight( dstr2[dpos2], type );
2573 if (ce2) break;
2574 inc_str_pos( &str2, &len2, &dpos2, &dlen2 );
2576 return len1 - len2;
2580 static const struct geoinfo *get_geoinfo_ptr( GEOID geoid )
2582 int min = 0, max = ARRAY_SIZE( geoinfodata )-1;
2584 while (min <= max)
2586 int n = (min + max)/2;
2587 const struct geoinfo *ptr = &geoinfodata[n];
2588 if (geoid == ptr->id) /* we don't need empty entries */
2589 return *ptr->iso2W ? ptr : NULL;
2590 if (ptr->id > geoid) max = n-1;
2591 else min = n+1;
2593 return NULL;
2597 static int compare_tzdate( const TIME_FIELDS *tf, const SYSTEMTIME *compare )
2599 static const int month_lengths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
2600 int first, last, limit, dayinsecs;
2602 if (tf->Month < compare->wMonth) return -1; /* We are in a month before the date limit. */
2603 if (tf->Month > compare->wMonth) return 1; /* We are in a month after the date limit. */
2605 /* if year is 0 then date is in day-of-week format, otherwise
2606 * it's absolute date.
2608 if (!compare->wYear)
2610 /* wDay is interpreted as number of the week in the month
2611 * 5 means: the last week in the month */
2612 /* calculate the day of the first DayOfWeek in the month */
2613 first = (6 + compare->wDayOfWeek - tf->Weekday + tf->Day) % 7 + 1;
2614 /* check needed for the 5th weekday of the month */
2615 last = month_lengths[tf->Month - 1] +
2616 (tf->Month == 2 && (!(tf->Year % 4) && (tf->Year % 100 || !(tf->Year % 400))));
2617 limit = first + 7 * (compare->wDay - 1);
2618 if (limit > last) limit -= 7;
2620 else limit = compare->wDay;
2622 limit = ((limit * 24 + compare->wHour) * 60 + compare->wMinute) * 60;
2623 dayinsecs = ((tf->Day * 24 + tf->Hour) * 60 + tf->Minute) * 60 + tf->Second;
2624 return dayinsecs - limit;
2628 static DWORD get_timezone_id( const TIME_ZONE_INFORMATION *info, LARGE_INTEGER time, BOOL is_local )
2630 int year;
2631 BOOL before_standard_date, after_daylight_date;
2632 LARGE_INTEGER t2;
2633 TIME_FIELDS tf;
2635 if (!info->DaylightDate.wMonth) return TIME_ZONE_ID_UNKNOWN;
2637 /* if year is 0 then date is in day-of-week format, otherwise it's absolute date */
2638 if (info->StandardDate.wMonth == 0 ||
2639 (info->StandardDate.wYear == 0 &&
2640 (info->StandardDate.wDay < 1 || info->StandardDate.wDay > 5 ||
2641 info->DaylightDate.wDay < 1 || info->DaylightDate.wDay > 5)))
2643 SetLastError( ERROR_INVALID_PARAMETER );
2644 return TIME_ZONE_ID_INVALID;
2647 if (!is_local) time.QuadPart -= info->Bias * (LONGLONG)600000000;
2648 RtlTimeToTimeFields( &time, &tf );
2649 year = tf.Year;
2650 if (!is_local)
2652 t2.QuadPart = time.QuadPart - info->DaylightBias * (LONGLONG)600000000;
2653 RtlTimeToTimeFields( &t2, &tf );
2655 if (tf.Year == year)
2656 before_standard_date = compare_tzdate( &tf, &info->StandardDate ) < 0;
2657 else
2658 before_standard_date = tf.Year < year;
2660 if (!is_local)
2662 t2.QuadPart = time.QuadPart - info->StandardBias * (LONGLONG)600000000;
2663 RtlTimeToTimeFields( &t2, &tf );
2665 if (tf.Year == year)
2666 after_daylight_date = compare_tzdate( &tf, &info->DaylightDate ) >= 0;
2667 else
2668 after_daylight_date = tf.Year > year;
2670 if (info->DaylightDate.wMonth < info->StandardDate.wMonth) /* Northern hemisphere */
2672 if (before_standard_date && after_daylight_date) return TIME_ZONE_ID_DAYLIGHT;
2674 else /* Down south */
2676 if (before_standard_date || after_daylight_date) return TIME_ZONE_ID_DAYLIGHT;
2678 return TIME_ZONE_ID_STANDARD;
2682 /* Note: the Internal_ functions are not documented. The number of parameters
2683 * should be correct, but their exact meaning may not.
2686 /******************************************************************************
2687 * Internal_EnumCalendarInfo (kernelbase.@)
2689 BOOL WINAPI DECLSPEC_HOTPATCH Internal_EnumCalendarInfo( CALINFO_ENUMPROCW proc, LCID lcid, CALID id,
2690 CALTYPE type, BOOL unicode, BOOL ex,
2691 BOOL exex, LPARAM lparam )
2693 WCHAR buffer[256];
2694 DWORD optional = 0;
2695 INT ret;
2697 if (!proc)
2699 SetLastError( ERROR_INVALID_PARAMETER );
2700 return FALSE;
2703 if (id == ENUM_ALL_CALENDARS)
2705 if (!GetLocaleInfoW( lcid, LOCALE_ICALENDARTYPE | LOCALE_RETURN_NUMBER,
2706 (WCHAR *)&id, sizeof(id) / sizeof(WCHAR) )) return FALSE;
2707 if (!GetLocaleInfoW( lcid, LOCALE_IOPTIONALCALENDAR | LOCALE_RETURN_NUMBER,
2708 (WCHAR *)&optional, sizeof(optional) / sizeof(WCHAR) )) optional = 0;
2711 for (;;)
2713 if (type & CAL_RETURN_NUMBER)
2714 ret = GetCalendarInfoW( lcid, id, type, NULL, 0, (LPDWORD)buffer );
2715 else if (unicode)
2716 ret = GetCalendarInfoW( lcid, id, type, buffer, ARRAY_SIZE(buffer), NULL );
2717 else
2719 WCHAR bufW[256];
2720 ret = GetCalendarInfoW( lcid, id, type, bufW, ARRAY_SIZE(bufW), NULL );
2721 if (ret) WideCharToMultiByte( CP_ACP, 0, bufW, -1, (char *)buffer, sizeof(buffer), NULL, NULL );
2724 if (ret)
2726 if (exex) ret = ((CALINFO_ENUMPROCEXEX)proc)( buffer, id, NULL, lparam );
2727 else if (ex) ret = ((CALINFO_ENUMPROCEXW)proc)( buffer, id );
2728 else ret = proc( buffer );
2730 if (!ret) break;
2731 if (!optional) break;
2732 id = optional;
2734 return TRUE;
2738 /**************************************************************************
2739 * Internal_EnumDateFormats (kernelbase.@)
2741 BOOL WINAPI DECLSPEC_HOTPATCH Internal_EnumDateFormats( DATEFMT_ENUMPROCW proc, LCID lcid, DWORD flags,
2742 BOOL unicode, BOOL ex, BOOL exex, LPARAM lparam )
2744 WCHAR buffer[256];
2745 LCTYPE lctype;
2746 CALID cal_id;
2747 INT ret;
2749 if (!proc)
2751 SetLastError( ERROR_INVALID_PARAMETER );
2752 return FALSE;
2754 if (!GetLocaleInfoW( lcid, LOCALE_ICALENDARTYPE|LOCALE_RETURN_NUMBER,
2755 (LPWSTR)&cal_id, sizeof(cal_id)/sizeof(WCHAR) ))
2756 return FALSE;
2758 switch (flags & ~LOCALE_USE_CP_ACP)
2760 case 0:
2761 case DATE_SHORTDATE:
2762 lctype = LOCALE_SSHORTDATE;
2763 break;
2764 case DATE_LONGDATE:
2765 lctype = LOCALE_SLONGDATE;
2766 break;
2767 case DATE_YEARMONTH:
2768 lctype = LOCALE_SYEARMONTH;
2769 break;
2770 default:
2771 FIXME( "unknown date format 0x%08x\n", flags );
2772 SetLastError( ERROR_INVALID_PARAMETER );
2773 return FALSE;
2776 lctype |= flags & LOCALE_USE_CP_ACP;
2777 if (unicode)
2778 ret = GetLocaleInfoW( lcid, lctype, buffer, ARRAY_SIZE(buffer) );
2779 else
2780 ret = GetLocaleInfoA( lcid, lctype, (char *)buffer, sizeof(buffer) );
2782 if (ret)
2784 if (exex) ((DATEFMT_ENUMPROCEXEX)proc)( buffer, cal_id, lparam );
2785 else if (ex) ((DATEFMT_ENUMPROCEXW)proc)( buffer, cal_id );
2786 else proc( buffer );
2788 return TRUE;
2792 /******************************************************************************
2793 * Internal_EnumLanguageGroupLocales (kernelbase.@)
2795 BOOL WINAPI DECLSPEC_HOTPATCH Internal_EnumLanguageGroupLocales( LANGGROUPLOCALE_ENUMPROCW proc, LGRPID id,
2796 DWORD flags, LONG_PTR param, BOOL unicode )
2798 WCHAR name[10], value[10];
2799 DWORD name_len, value_len, type, index = 0, alt = 0;
2800 HKEY key, altkey;
2801 LCID lcid;
2803 if (!proc || id < LGRPID_WESTERN_EUROPE || id > LGRPID_ARMENIAN)
2805 SetLastError( ERROR_INVALID_PARAMETER );
2806 return FALSE;
2809 if (RegOpenKeyExW( nls_key, L"Locale", 0, KEY_READ, &key )) return FALSE;
2810 if (RegOpenKeyExW( key, L"Alternate Sorts", 0, KEY_READ, &altkey )) altkey = 0;
2812 for (;;)
2814 name_len = ARRAY_SIZE(name);
2815 value_len = sizeof(value);
2816 if (RegEnumValueW( alt ? altkey : key, index++, name, &name_len, NULL,
2817 &type, (BYTE *)value, &value_len ))
2819 if (alt++) break;
2820 index = 0;
2821 continue;
2823 if (type != REG_SZ) continue;
2824 if (id != wcstoul( value, NULL, 16 )) continue;
2825 lcid = wcstoul( name, NULL, 16 );
2826 if (!unicode)
2828 char nameA[10];
2829 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL );
2830 if (!((LANGGROUPLOCALE_ENUMPROCA)proc)( id, lcid, nameA, param )) break;
2832 else if (!proc( id, lcid, name, param )) break;
2834 RegCloseKey( altkey );
2835 RegCloseKey( key );
2836 return TRUE;
2840 /***********************************************************************
2841 * Internal_EnumSystemCodePages (kernelbase.@)
2843 BOOL WINAPI DECLSPEC_HOTPATCH Internal_EnumSystemCodePages( CODEPAGE_ENUMPROCW proc, DWORD flags,
2844 BOOL unicode )
2846 WCHAR name[10];
2847 DWORD name_len, type, index = 0;
2848 HKEY key;
2850 if (RegOpenKeyExW( nls_key, L"Codepage", 0, KEY_READ, &key )) return FALSE;
2852 for (;;)
2854 name_len = ARRAY_SIZE(name);
2855 if (RegEnumValueW( key, index++, name, &name_len, NULL, &type, NULL, NULL )) break;
2856 if (type != REG_SZ) continue;
2857 if (!wcstoul( name, NULL, 10 )) continue;
2858 if (!unicode)
2860 char nameA[10];
2861 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL );
2862 if (!((CODEPAGE_ENUMPROCA)proc)( nameA )) break;
2864 else if (!proc( name )) break;
2866 RegCloseKey( key );
2867 return TRUE;
2871 /******************************************************************************
2872 * Internal_EnumSystemLanguageGroups (kernelbase.@)
2874 BOOL WINAPI DECLSPEC_HOTPATCH Internal_EnumSystemLanguageGroups( LANGUAGEGROUP_ENUMPROCW proc,
2875 DWORD flags, LONG_PTR param, BOOL unicode )
2877 WCHAR name[10], value[10], descr[80];
2878 DWORD name_len, value_len, type, index = 0;
2879 HKEY key;
2880 LGRPID id;
2882 if (!proc)
2884 SetLastError( ERROR_INVALID_PARAMETER );
2885 return FALSE;
2888 switch (flags)
2890 case 0:
2891 flags = LGRPID_INSTALLED;
2892 break;
2893 case LGRPID_INSTALLED:
2894 case LGRPID_SUPPORTED:
2895 break;
2896 default:
2897 SetLastError( ERROR_INVALID_FLAGS );
2898 return FALSE;
2901 if (RegOpenKeyExW( nls_key, L"Language Groups", 0, KEY_READ, &key )) return FALSE;
2903 for (;;)
2905 name_len = ARRAY_SIZE(name);
2906 value_len = sizeof(value);
2907 if (RegEnumValueW( key, index++, name, &name_len, NULL, &type, (BYTE *)value, &value_len )) break;
2908 if (type != REG_SZ) continue;
2910 id = wcstoul( name, NULL, 16 );
2912 if (!(flags & LGRPID_SUPPORTED) && !wcstoul( value, NULL, 10 )) continue;
2913 if (!LoadStringW( kernel32_handle, 0x2000 + id, descr, ARRAY_SIZE(descr) )) descr[0] = 0;
2914 TRACE( "%p: %u %s %s %x %lx\n", proc, id, debugstr_w(name), debugstr_w(descr), flags, param );
2915 if (!unicode)
2917 char nameA[10], descrA[80];
2918 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL );
2919 WideCharToMultiByte( CP_ACP, 0, descr, -1, descrA, sizeof(descrA), NULL, NULL );
2920 if (!((LANGUAGEGROUP_ENUMPROCA)proc)( id, nameA, descrA, flags, param )) break;
2922 else if (!proc( id, name, descr, flags, param )) break;
2924 RegCloseKey( key );
2925 return TRUE;
2929 /**************************************************************************
2930 * Internal_EnumTimeFormats (kernelbase.@)
2932 BOOL WINAPI DECLSPEC_HOTPATCH Internal_EnumTimeFormats( TIMEFMT_ENUMPROCW proc, LCID lcid, DWORD flags,
2933 BOOL unicode, BOOL ex, LPARAM lparam )
2935 WCHAR buffer[256];
2936 LCTYPE lctype;
2937 INT ret;
2939 if (!proc)
2941 SetLastError( ERROR_INVALID_PARAMETER );
2942 return FALSE;
2944 switch (flags & ~LOCALE_USE_CP_ACP)
2946 case 0:
2947 lctype = LOCALE_STIMEFORMAT;
2948 break;
2949 case TIME_NOSECONDS:
2950 lctype = LOCALE_SSHORTTIME;
2951 break;
2952 default:
2953 FIXME( "Unknown time format %x\n", flags );
2954 SetLastError( ERROR_INVALID_PARAMETER );
2955 return FALSE;
2958 lctype |= flags & LOCALE_USE_CP_ACP;
2959 if (unicode)
2960 ret = GetLocaleInfoW( lcid, lctype, buffer, ARRAY_SIZE(buffer) );
2961 else
2962 ret = GetLocaleInfoA( lcid, lctype, (char *)buffer, sizeof(buffer) );
2964 if (ret)
2966 if (ex) ((TIMEFMT_ENUMPROCEX)proc)( buffer, lparam );
2967 else proc( buffer );
2969 return TRUE;
2973 /******************************************************************************
2974 * Internal_EnumUILanguages (kernelbase.@)
2976 BOOL WINAPI DECLSPEC_HOTPATCH Internal_EnumUILanguages( UILANGUAGE_ENUMPROCW proc, DWORD flags,
2977 LONG_PTR param, BOOL unicode )
2979 WCHAR name[10];
2980 DWORD name_len, type, index = 0;
2981 HKEY key;
2983 if (!proc)
2985 SetLastError( ERROR_INVALID_PARAMETER );
2986 return FALSE;
2988 if (flags & ~MUI_LANGUAGE_ID)
2990 SetLastError( ERROR_INVALID_FLAGS );
2991 return FALSE;
2994 if (RegOpenKeyExW( nls_key, L"Locale", 0, KEY_READ, &key )) return FALSE;
2996 for (;;)
2998 name_len = ARRAY_SIZE(name);
2999 if (RegEnumValueW( key, index++, name, &name_len, NULL, &type, NULL, NULL )) break;
3000 if (type != REG_SZ) continue;
3001 if (!wcstoul( name, NULL, 16 )) continue;
3002 if (!unicode)
3004 char nameA[10];
3005 WideCharToMultiByte( CP_ACP, 0, name, -1, nameA, sizeof(nameA), NULL, NULL );
3006 if (!((UILANGUAGE_ENUMPROCA)proc)( nameA, param )) break;
3008 else if (!proc( name, param )) break;
3010 RegCloseKey( key );
3011 return TRUE;
3015 /******************************************************************************
3016 * CompareStringEx (kernelbase.@)
3018 INT WINAPI CompareStringEx( const WCHAR *locale, DWORD flags, const WCHAR *str1, int len1,
3019 const WCHAR *str2, int len2, NLSVERSIONINFO *version,
3020 void *reserved, LPARAM handle )
3022 DWORD supported_flags = NORM_IGNORECASE | NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS | SORT_STRINGSORT |
3023 NORM_IGNOREKANATYPE | NORM_IGNOREWIDTH | LOCALE_USE_CP_ACP;
3024 DWORD semistub_flags = NORM_LINGUISTIC_CASING | LINGUISTIC_IGNORECASE | 0x10000000;
3025 /* 0x10000000 is related to diacritics in Arabic, Japanese, and Hebrew */
3026 INT ret;
3027 static int once;
3029 if (version) FIXME( "unexpected version parameter\n" );
3030 if (reserved) FIXME( "unexpected reserved value\n" );
3031 if (handle) FIXME( "unexpected handle\n" );
3033 if (!str1 || !str2)
3035 SetLastError( ERROR_INVALID_PARAMETER );
3036 return 0;
3039 if (flags & ~(supported_flags | semistub_flags))
3041 SetLastError( ERROR_INVALID_FLAGS );
3042 return 0;
3045 if (flags & semistub_flags)
3047 if (!once++) FIXME( "semi-stub behavior for flag(s) 0x%x\n", flags & semistub_flags );
3050 if (len1 < 0) len1 = lstrlenW(str1);
3051 if (len2 < 0) len2 = lstrlenW(str2);
3053 ret = compare_weights( flags, str1, len1, str2, len2, UNICODE_WEIGHT );
3054 if (!ret)
3056 if (!(flags & NORM_IGNORENONSPACE))
3057 ret = compare_weights( flags, str1, len1, str2, len2, DIACRITIC_WEIGHT );
3058 if (!ret && !(flags & NORM_IGNORECASE))
3059 ret = compare_weights( flags, str1, len1, str2, len2, CASE_WEIGHT );
3061 if (!ret) return CSTR_EQUAL;
3062 return (ret < 0) ? CSTR_LESS_THAN : CSTR_GREATER_THAN;
3066 /******************************************************************************
3067 * CompareStringA (kernelbase.@)
3069 INT WINAPI DECLSPEC_HOTPATCH CompareStringA( LCID lcid, DWORD flags, const char *str1, int len1,
3070 const char *str2, int len2 )
3072 WCHAR *buf1W = NtCurrentTeb()->StaticUnicodeBuffer;
3073 WCHAR *buf2W = buf1W + 130;
3074 LPWSTR str1W, str2W;
3075 INT len1W = 0, len2W = 0, ret;
3076 UINT locale_cp = CP_ACP;
3078 if (!str1 || !str2)
3080 SetLastError( ERROR_INVALID_PARAMETER );
3081 return 0;
3083 if (len1 < 0) len1 = strlen(str1);
3084 if (len2 < 0) len2 = strlen(str2);
3086 locale_cp = get_lcid_codepage( lcid, flags );
3087 if (len1)
3089 if (len1 <= 130) len1W = MultiByteToWideChar( locale_cp, 0, str1, len1, buf1W, 130 );
3090 if (len1W) str1W = buf1W;
3091 else
3093 len1W = MultiByteToWideChar( locale_cp, 0, str1, len1, NULL, 0 );
3094 str1W = HeapAlloc( GetProcessHeap(), 0, len1W * sizeof(WCHAR) );
3095 if (!str1W)
3097 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3098 return 0;
3100 MultiByteToWideChar( locale_cp, 0, str1, len1, str1W, len1W );
3103 else
3105 len1W = 0;
3106 str1W = buf1W;
3109 if (len2)
3111 if (len2 <= 130) len2W = MultiByteToWideChar( locale_cp, 0, str2, len2, buf2W, 130 );
3112 if (len2W) str2W = buf2W;
3113 else
3115 len2W = MultiByteToWideChar( locale_cp, 0, str2, len2, NULL, 0 );
3116 str2W = HeapAlloc( GetProcessHeap(), 0, len2W * sizeof(WCHAR) );
3117 if (!str2W)
3119 if (str1W != buf1W) HeapFree( GetProcessHeap(), 0, str1W );
3120 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3121 return 0;
3123 MultiByteToWideChar( locale_cp, 0, str2, len2, str2W, len2W );
3126 else
3128 len2W = 0;
3129 str2W = buf2W;
3132 ret = CompareStringW( lcid, flags, str1W, len1W, str2W, len2W );
3134 if (str1W != buf1W) HeapFree( GetProcessHeap(), 0, str1W );
3135 if (str2W != buf2W) HeapFree( GetProcessHeap(), 0, str2W );
3136 return ret;
3140 /******************************************************************************
3141 * CompareStringW (kernelbase.@)
3143 INT WINAPI DECLSPEC_HOTPATCH CompareStringW( LCID lcid, DWORD flags, const WCHAR *str1, int len1,
3144 const WCHAR *str2, int len2 )
3146 return CompareStringEx( NULL, flags, str1, len1, str2, len2, NULL, NULL, 0 );
3150 /******************************************************************************
3151 * CompareStringOrdinal (kernelbase.@)
3153 INT WINAPI DECLSPEC_HOTPATCH CompareStringOrdinal( const WCHAR *str1, INT len1,
3154 const WCHAR *str2, INT len2, BOOL ignore_case )
3156 int ret;
3158 if (!str1 || !str2)
3160 SetLastError( ERROR_INVALID_PARAMETER );
3161 return 0;
3163 if (len1 < 0) len1 = lstrlenW( str1 );
3164 if (len2 < 0) len2 = lstrlenW( str2 );
3166 ret = RtlCompareUnicodeStrings( str1, len1, str2, len2, ignore_case );
3167 if (ret < 0) return CSTR_LESS_THAN;
3168 if (ret > 0) return CSTR_GREATER_THAN;
3169 return CSTR_EQUAL;
3173 /******************************************************************************
3174 * ConvertDefaultLocale (kernelbase.@)
3176 LCID WINAPI DECLSPEC_HOTPATCH ConvertDefaultLocale( LCID lcid )
3178 switch (lcid)
3180 case LOCALE_INVARIANT:
3181 return lcid; /* keep as-is */
3182 case LOCALE_SYSTEM_DEFAULT:
3183 return GetSystemDefaultLCID();
3184 case LOCALE_USER_DEFAULT:
3185 case LOCALE_NEUTRAL:
3186 return GetUserDefaultLCID();
3187 case MAKELANGID( LANG_CHINESE, SUBLANG_NEUTRAL ):
3188 case MAKELANGID( LANG_CHINESE, 0x1e ):
3189 return MAKELANGID( LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED );
3190 case MAKELANGID( LANG_CHINESE, 0x1f ):
3191 return MAKELANGID( LANG_CHINESE, SUBLANG_CHINESE_HONGKONG );
3192 case MAKELANGID( LANG_SPANISH, SUBLANG_NEUTRAL ):
3193 return MAKELANGID( LANG_SPANISH, SUBLANG_SPANISH_MODERN );
3194 case MAKELANGID( LANG_IRISH, SUBLANG_NEUTRAL ):
3195 return MAKELANGID( LANG_IRISH, SUBLANG_IRISH_IRELAND );
3196 case MAKELANGID( LANG_BENGALI, SUBLANG_NEUTRAL ):
3197 return MAKELANGID( LANG_BENGALI, SUBLANG_BENGALI_BANGLADESH );
3198 case MAKELANGID( LANG_SINDHI, SUBLANG_NEUTRAL ):
3199 return MAKELANGID( LANG_SINDHI, SUBLANG_SINDHI_AFGHANISTAN );
3200 case MAKELANGID( LANG_INUKTITUT, SUBLANG_NEUTRAL ):
3201 return MAKELANGID( LANG_INUKTITUT, SUBLANG_INUKTITUT_CANADA_LATIN );
3202 case MAKELANGID( LANG_TAMAZIGHT, SUBLANG_NEUTRAL ):
3203 return MAKELANGID( LANG_TAMAZIGHT, SUBLANG_TAMAZIGHT_ALGERIA_LATIN );
3204 case MAKELANGID( LANG_FULAH, SUBLANG_NEUTRAL ):
3205 return MAKELANGID( LANG_FULAH, SUBLANG_FULAH_SENEGAL );
3206 case MAKELANGID( LANG_TIGRINYA, SUBLANG_NEUTRAL ):
3207 return MAKELANGID( LANG_TIGRINYA, SUBLANG_TIGRINYA_ERITREA );
3208 default:
3209 /* Replace SUBLANG_NEUTRAL with SUBLANG_DEFAULT */
3210 if (SUBLANGID(lcid) == SUBLANG_NEUTRAL && SORTIDFROMLCID(lcid) == SORT_DEFAULT)
3211 lcid = MAKELANGID( PRIMARYLANGID(lcid), SUBLANG_DEFAULT );
3212 break;
3214 return lcid;
3218 /******************************************************************************
3219 * EnumCalendarInfoW (kernelbase.@)
3221 BOOL WINAPI DECLSPEC_HOTPATCH EnumCalendarInfoW( CALINFO_ENUMPROCW proc, LCID lcid,
3222 CALID id, CALTYPE type )
3224 return Internal_EnumCalendarInfo( proc, lcid, id, type, TRUE, FALSE, FALSE, 0 );
3228 /******************************************************************************
3229 * EnumCalendarInfoExW (kernelbase.@)
3231 BOOL WINAPI DECLSPEC_HOTPATCH EnumCalendarInfoExW( CALINFO_ENUMPROCEXW proc, LCID lcid,
3232 CALID id, CALTYPE type )
3234 return Internal_EnumCalendarInfo( (CALINFO_ENUMPROCW)proc, lcid, id, type, TRUE, TRUE, FALSE, 0 );
3237 /******************************************************************************
3238 * EnumCalendarInfoExEx (kernelbase.@)
3240 BOOL WINAPI DECLSPEC_HOTPATCH EnumCalendarInfoExEx( CALINFO_ENUMPROCEXEX proc, LPCWSTR locale, CALID id,
3241 LPCWSTR reserved, CALTYPE type, LPARAM lparam )
3243 LCID lcid = LocaleNameToLCID( locale, 0 );
3244 return Internal_EnumCalendarInfo( (CALINFO_ENUMPROCW)proc, lcid, id, type, TRUE, TRUE, TRUE, lparam );
3248 /**************************************************************************
3249 * EnumDateFormatsW (kernelbase.@)
3251 BOOL WINAPI DECLSPEC_HOTPATCH EnumDateFormatsW( DATEFMT_ENUMPROCW proc, LCID lcid, DWORD flags )
3253 return Internal_EnumDateFormats( proc, lcid, flags, TRUE, FALSE, FALSE, 0 );
3257 /**************************************************************************
3258 * EnumDateFormatsExW (kernelbase.@)
3260 BOOL WINAPI DECLSPEC_HOTPATCH EnumDateFormatsExW( DATEFMT_ENUMPROCEXW proc, LCID lcid, DWORD flags )
3262 return Internal_EnumDateFormats( (DATEFMT_ENUMPROCW)proc, lcid, flags, TRUE, TRUE, FALSE, 0 );
3266 /**************************************************************************
3267 * EnumDateFormatsExEx (kernelbase.@)
3269 BOOL WINAPI DECLSPEC_HOTPATCH EnumDateFormatsExEx( DATEFMT_ENUMPROCEXEX proc, const WCHAR *locale,
3270 DWORD flags, LPARAM lparam )
3272 LCID lcid = LocaleNameToLCID( locale, 0 );
3273 return Internal_EnumDateFormats( (DATEFMT_ENUMPROCW)proc, lcid, flags, TRUE, TRUE, TRUE, lparam );
3278 /******************************************************************************
3279 * EnumDynamicTimeZoneInformation (kernelbase.@)
3281 DWORD WINAPI DECLSPEC_HOTPATCH EnumDynamicTimeZoneInformation( DWORD index,
3282 DYNAMIC_TIME_ZONE_INFORMATION *info )
3284 DYNAMIC_TIME_ZONE_INFORMATION tz;
3285 LSTATUS ret;
3286 DWORD size;
3288 if (!info) return ERROR_INVALID_PARAMETER;
3290 size = ARRAY_SIZE(tz.TimeZoneKeyName);
3291 ret = RegEnumKeyExW( tz_key, index, tz.TimeZoneKeyName, &size, NULL, NULL, NULL, NULL );
3292 if (ret) return ret;
3294 tz.DynamicDaylightTimeDisabled = TRUE;
3295 if (!GetTimeZoneInformationForYear( 0, &tz, (TIME_ZONE_INFORMATION *)info )) return GetLastError();
3297 lstrcpyW( info->TimeZoneKeyName, tz.TimeZoneKeyName );
3298 info->DynamicDaylightTimeDisabled = FALSE;
3299 return 0;
3303 /******************************************************************************
3304 * EnumLanguageGroupLocalesW (kernelbase.@)
3306 BOOL WINAPI DECLSPEC_HOTPATCH EnumLanguageGroupLocalesW( LANGGROUPLOCALE_ENUMPROCW proc, LGRPID id,
3307 DWORD flags, LONG_PTR param )
3309 return Internal_EnumLanguageGroupLocales( proc, id, flags, param, TRUE );
3313 /******************************************************************************
3314 * EnumUILanguagesW (kernelbase.@)
3316 BOOL WINAPI DECLSPEC_HOTPATCH EnumUILanguagesW( UILANGUAGE_ENUMPROCW proc, DWORD flags, LONG_PTR param )
3318 return Internal_EnumUILanguages( proc, flags, param, TRUE );
3322 /***********************************************************************
3323 * EnumSystemCodePagesW (kernelbase.@)
3325 BOOL WINAPI DECLSPEC_HOTPATCH EnumSystemCodePagesW( CODEPAGE_ENUMPROCW proc, DWORD flags )
3327 return Internal_EnumSystemCodePages( proc, flags, TRUE );
3331 /******************************************************************************
3332 * EnumSystemGeoID (kernelbase.@)
3334 BOOL WINAPI DECLSPEC_HOTPATCH EnumSystemGeoID( GEOCLASS class, GEOID parent, GEO_ENUMPROC proc )
3336 INT i;
3338 TRACE( "(%d, %d, %p)\n", class, parent, proc );
3340 if (!proc)
3342 SetLastError( ERROR_INVALID_PARAMETER );
3343 return FALSE;
3345 if (class != GEOCLASS_NATION && class != GEOCLASS_REGION && class != GEOCLASS_ALL)
3347 SetLastError( ERROR_INVALID_FLAGS );
3348 return FALSE;
3351 for (i = 0; i < ARRAY_SIZE(geoinfodata); i++)
3353 const struct geoinfo *ptr = &geoinfodata[i];
3355 if (class == GEOCLASS_NATION && (ptr->kind != LOCATION_NATION)) continue;
3356 /* LOCATION_BOTH counts as region */
3357 if (class == GEOCLASS_REGION && (ptr->kind == LOCATION_NATION)) continue;
3358 if (parent && ptr->parent != parent) continue;
3359 if (!proc( ptr->id )) break;
3361 return TRUE;
3365 /******************************************************************************
3366 * EnumSystemLanguageGroupsW (kernelbase.@)
3368 BOOL WINAPI DECLSPEC_HOTPATCH EnumSystemLanguageGroupsW( LANGUAGEGROUP_ENUMPROCW proc,
3369 DWORD flags, LONG_PTR param )
3371 return Internal_EnumSystemLanguageGroups( proc, flags, param, TRUE );
3375 /******************************************************************************
3376 * EnumSystemLocalesA (kernelbase.@)
3378 BOOL WINAPI DECLSPEC_HOTPATCH EnumSystemLocalesA( LOCALE_ENUMPROCA proc, DWORD flags )
3380 char name[10];
3381 DWORD name_len, type, index = 0;
3382 HKEY key;
3384 if (RegOpenKeyExW( nls_key, L"Locale", 0, KEY_READ, &key )) return FALSE;
3386 for (;;)
3388 name_len = ARRAY_SIZE(name);
3389 if (RegEnumValueA( key, index++, name, &name_len, NULL, &type, NULL, NULL )) break;
3390 if (type != REG_SZ) continue;
3391 if (!strtoul( name, NULL, 16 )) continue;
3392 if (!proc( name )) break;
3394 RegCloseKey( key );
3395 return TRUE;
3399 /******************************************************************************
3400 * EnumSystemLocalesW (kernelbase.@)
3402 BOOL WINAPI DECLSPEC_HOTPATCH EnumSystemLocalesW( LOCALE_ENUMPROCW proc, DWORD flags )
3404 WCHAR name[10];
3405 DWORD name_len, type, index = 0;
3406 HKEY key;
3408 if (RegOpenKeyExW( nls_key, L"Locale", 0, KEY_READ, &key )) return FALSE;
3410 for (;;)
3412 name_len = ARRAY_SIZE(name);
3413 if (RegEnumValueW( key, index++, name, &name_len, NULL, &type, NULL, NULL )) break;
3414 if (type != REG_SZ) continue;
3415 if (!wcstoul( name, NULL, 16 )) continue;
3416 if (!proc( name )) break;
3418 RegCloseKey( key );
3419 return TRUE;
3423 /******************************************************************************
3424 * EnumSystemLocalesEx (kernelbase.@)
3426 BOOL WINAPI DECLSPEC_HOTPATCH EnumSystemLocalesEx( LOCALE_ENUMPROCEX proc, DWORD wanted_flags,
3427 LPARAM param, void *reserved )
3429 WCHAR buffer[256], name[10];
3430 DWORD name_len, type, neutral, flags, index = 0, alt = 0;
3431 HKEY key, altkey;
3432 LCID lcid;
3434 if (reserved)
3436 SetLastError( ERROR_INVALID_PARAMETER );
3437 return FALSE;
3440 if (RegOpenKeyExW( nls_key, L"Locale", 0, KEY_READ, &key )) return FALSE;
3441 if (RegOpenKeyExW( key, L"Alternate Sorts", 0, KEY_READ, &altkey )) altkey = 0;
3443 for (;;)
3445 name_len = ARRAY_SIZE(name);
3446 if (RegEnumValueW( alt ? altkey : key, index++, name, &name_len, NULL, &type, NULL, NULL ))
3448 if (alt++) break;
3449 index = 0;
3450 continue;
3452 if (type != REG_SZ) continue;
3453 if (!(lcid = wcstoul( name, NULL, 16 ))) continue;
3455 GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE, buffer, ARRAY_SIZE( buffer ));
3456 if (!GetLocaleInfoW( lcid, LOCALE_INEUTRAL | LOCALE_NOUSEROVERRIDE | LOCALE_RETURN_NUMBER,
3457 (LPWSTR)&neutral, sizeof(neutral) / sizeof(WCHAR) ))
3458 neutral = 0;
3460 if (alt)
3461 flags = LOCALE_ALTERNATE_SORTS;
3462 else
3463 flags = LOCALE_WINDOWS | (neutral ? LOCALE_NEUTRALDATA : LOCALE_SPECIFICDATA);
3465 if (wanted_flags && !(flags & wanted_flags)) continue;
3466 if (!proc( buffer, flags, param )) break;
3468 RegCloseKey( altkey );
3469 RegCloseKey( key );
3470 return TRUE;
3474 /**************************************************************************
3475 * EnumTimeFormatsW (kernelbase.@)
3477 BOOL WINAPI DECLSPEC_HOTPATCH EnumTimeFormatsW( TIMEFMT_ENUMPROCW proc, LCID lcid, DWORD flags )
3479 return Internal_EnumTimeFormats( proc, lcid, flags, TRUE, FALSE, 0 );
3483 /**************************************************************************
3484 * EnumTimeFormatsEx (kernelbase.@)
3486 BOOL WINAPI DECLSPEC_HOTPATCH EnumTimeFormatsEx( TIMEFMT_ENUMPROCEX proc, const WCHAR *locale,
3487 DWORD flags, LPARAM lparam )
3489 LCID lcid = LocaleNameToLCID( locale, 0 );
3490 return Internal_EnumTimeFormats( (TIMEFMT_ENUMPROCW)proc, lcid, flags, TRUE, TRUE, lparam );
3494 /**************************************************************************
3495 * FindNLSString (kernelbase.@)
3497 INT WINAPI DECLSPEC_HOTPATCH FindNLSString( LCID lcid, DWORD flags, const WCHAR *src,
3498 int srclen, const WCHAR *value, int valuelen, int *found )
3500 WCHAR locale[LOCALE_NAME_MAX_LENGTH];
3502 LCIDToLocaleName( lcid, locale, ARRAY_SIZE(locale), 0 );
3503 return FindNLSStringEx( locale, flags, src, srclen, value, valuelen, found, NULL, NULL, 0 );
3507 /**************************************************************************
3508 * FindNLSStringEx (kernelbase.@)
3510 INT WINAPI DECLSPEC_HOTPATCH FindNLSStringEx( const WCHAR *locale, DWORD flags, const WCHAR *src,
3511 int srclen, const WCHAR *value, int valuelen, int *found,
3512 NLSVERSIONINFO *version, void *reserved, LPARAM handle )
3514 /* FIXME: this function should normalize strings before calling CompareStringEx() */
3515 DWORD mask = flags;
3516 int offset, inc, count;
3518 TRACE( "%s %x %s %d %s %d %p %p %p %ld\n", wine_dbgstr_w(locale), flags,
3519 wine_dbgstr_w(src), srclen, wine_dbgstr_w(value), valuelen, found,
3520 version, reserved, handle );
3522 if (version || reserved || handle || !IsValidLocaleName(locale) ||
3523 !src || !srclen || srclen < -1 || !value || !valuelen || valuelen < -1)
3525 SetLastError( ERROR_INVALID_PARAMETER );
3526 return -1;
3528 if (srclen == -1) srclen = lstrlenW(src);
3529 if (valuelen == -1) valuelen = lstrlenW(value);
3531 srclen -= valuelen;
3532 if (srclen < 0) return -1;
3534 mask = flags & ~(FIND_FROMSTART | FIND_FROMEND | FIND_STARTSWITH | FIND_ENDSWITH);
3535 count = flags & (FIND_FROMSTART | FIND_FROMEND) ? srclen + 1 : 1;
3536 offset = flags & (FIND_FROMSTART | FIND_STARTSWITH) ? 0 : srclen;
3537 inc = flags & (FIND_FROMSTART | FIND_STARTSWITH) ? 1 : -1;
3538 while (count--)
3540 if (CompareStringEx( locale, mask, src + offset, valuelen,
3541 value, valuelen, NULL, NULL, 0 ) == CSTR_EQUAL)
3543 if (found) *found = valuelen;
3544 return offset;
3546 offset += inc;
3548 return -1;
3552 /******************************************************************************
3553 * FindStringOrdinal (kernelbase.@)
3555 INT WINAPI DECLSPEC_HOTPATCH FindStringOrdinal( DWORD flag, const WCHAR *src, INT src_size,
3556 const WCHAR *val, INT val_size, BOOL ignore_case )
3558 INT offset, inc, count;
3560 TRACE( "%#x %s %d %s %d %d\n", flag, wine_dbgstr_w(src), src_size,
3561 wine_dbgstr_w(val), val_size, ignore_case );
3563 if (!src || !val)
3565 SetLastError( ERROR_INVALID_PARAMETER );
3566 return -1;
3569 if (flag != FIND_FROMSTART && flag != FIND_FROMEND && flag != FIND_STARTSWITH && flag != FIND_ENDSWITH)
3571 SetLastError( ERROR_INVALID_FLAGS );
3572 return -1;
3575 if (src_size == -1) src_size = lstrlenW( src );
3576 if (val_size == -1) val_size = lstrlenW( val );
3578 SetLastError( ERROR_SUCCESS );
3579 src_size -= val_size;
3580 if (src_size < 0) return -1;
3582 count = flag & (FIND_FROMSTART | FIND_FROMEND) ? src_size + 1 : 1;
3583 offset = flag & (FIND_FROMSTART | FIND_STARTSWITH) ? 0 : src_size;
3584 inc = flag & (FIND_FROMSTART | FIND_STARTSWITH) ? 1 : -1;
3585 while (count--)
3587 if (CompareStringOrdinal( src + offset, val_size, val, val_size, ignore_case ) == CSTR_EQUAL)
3588 return offset;
3589 offset += inc;
3591 return -1;
3595 /******************************************************************************
3596 * FoldStringW (kernelbase.@)
3598 INT WINAPI DECLSPEC_HOTPATCH FoldStringW( DWORD flags, LPCWSTR src, INT srclen, LPWSTR dst, INT dstlen )
3600 NTSTATUS status;
3601 WCHAR *buf = dst;
3602 int len = dstlen;
3604 if (!src || !srclen || dstlen < 0 || (dstlen && !dst) || src == dst)
3606 SetLastError( ERROR_INVALID_PARAMETER );
3607 return 0;
3609 if (srclen == -1) srclen = lstrlenW(src) + 1;
3611 if (!dstlen && (flags & (MAP_PRECOMPOSED | MAP_FOLDCZONE | MAP_COMPOSITE)))
3613 len = srclen * 4;
3614 if (!(buf = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
3616 SetLastError( ERROR_OUTOFMEMORY );
3617 return 0;
3621 for (;;)
3623 status = fold_string( flags, src, srclen, buf, &len );
3624 if (buf != dst) RtlFreeHeap( GetProcessHeap(), 0, buf );
3625 if (status != STATUS_BUFFER_TOO_SMALL) break;
3626 if (!(buf = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
3628 SetLastError( ERROR_OUTOFMEMORY );
3629 return 0;
3632 if (status == STATUS_INVALID_PARAMETER_1)
3634 SetLastError( ERROR_INVALID_FLAGS );
3635 return 0;
3637 if (!set_ntstatus( status )) return 0;
3639 if (dstlen && dstlen < len) SetLastError( ERROR_INSUFFICIENT_BUFFER );
3640 return len;
3644 static const WCHAR *get_message( DWORD flags, const void *src, UINT id, UINT lang,
3645 BOOL ansi, WCHAR **buffer )
3647 DWORD len;
3649 if (!(flags & FORMAT_MESSAGE_FROM_STRING))
3651 const MESSAGE_RESOURCE_ENTRY *entry;
3652 NTSTATUS status = STATUS_INVALID_PARAMETER;
3654 if (flags & FORMAT_MESSAGE_FROM_HMODULE)
3656 HMODULE module = (HMODULE)src;
3657 if (!module) module = GetModuleHandleW( 0 );
3658 status = RtlFindMessage( module, RT_MESSAGETABLE, lang, id, &entry );
3660 if (status && (flags & FORMAT_MESSAGE_FROM_SYSTEM))
3662 /* Fold win32 hresult to its embedded error code. */
3663 if (HRESULT_SEVERITY(id) == SEVERITY_ERROR && HRESULT_FACILITY(id) == FACILITY_WIN32)
3664 id = HRESULT_CODE( id );
3665 status = RtlFindMessage( kernel32_handle, RT_MESSAGETABLE, lang, id, &entry );
3667 if (!set_ntstatus( status )) return NULL;
3669 src = entry->Text;
3670 ansi = !(entry->Flags & MESSAGE_RESOURCE_UNICODE);
3673 if (!ansi) return src;
3674 len = MultiByteToWideChar( CP_ACP, 0, src, -1, NULL, 0 );
3675 if (!(*buffer = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return NULL;
3676 MultiByteToWideChar( CP_ACP, 0, src, -1, *buffer, len );
3677 return *buffer;
3681 /***********************************************************************
3682 * FormatMessageA (kernelbase.@)
3684 DWORD WINAPI DECLSPEC_HOTPATCH FormatMessageA( DWORD flags, const void *source, DWORD msgid, DWORD langid,
3685 char *buffer, DWORD size, __ms_va_list *args )
3687 DWORD ret = 0;
3688 ULONG len, retsize = 0;
3689 ULONG width = (flags & FORMAT_MESSAGE_MAX_WIDTH_MASK);
3690 const WCHAR *src;
3691 WCHAR *result, *message = NULL;
3692 NTSTATUS status;
3694 TRACE( "(0x%x,%p,%#x,0x%x,%p,%u,%p)\n", flags, source, msgid, langid, buffer, size, args );
3696 if (flags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
3698 if (!buffer)
3700 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3701 return 0;
3703 *(char **)buffer = NULL;
3705 if (size >= 32768)
3707 SetLastError( ERROR_INVALID_PARAMETER );
3708 return 0;
3711 if (width == 0xff) width = ~0u;
3713 if (!(src = get_message( flags, source, msgid, langid, TRUE, &message ))) return 0;
3715 if (!(result = HeapAlloc( GetProcessHeap(), 0, 65536 )))
3716 status = STATUS_NO_MEMORY;
3717 else
3718 status = RtlFormatMessage( src, width, !!(flags & FORMAT_MESSAGE_IGNORE_INSERTS),
3719 TRUE, !!(flags & FORMAT_MESSAGE_ARGUMENT_ARRAY), args,
3720 result, 65536, &retsize );
3722 HeapFree( GetProcessHeap(), 0, message );
3724 if (status == STATUS_BUFFER_OVERFLOW)
3726 SetLastError( ERROR_INSUFFICIENT_BUFFER );
3727 goto done;
3729 if (!set_ntstatus( status )) goto done;
3731 len = WideCharToMultiByte( CP_ACP, 0, result, retsize / sizeof(WCHAR), NULL, 0, NULL, NULL );
3732 if (len <= 1)
3734 SetLastError( ERROR_NO_WORK_DONE );
3735 goto done;
3738 if (flags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
3740 char *buf = LocalAlloc( LMEM_ZEROINIT, max( size, len ));
3741 if (!buf)
3743 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
3744 goto done;
3746 *(char **)buffer = buf;
3747 WideCharToMultiByte( CP_ACP, 0, result, retsize / sizeof(WCHAR), buf, max( size, len ), NULL, NULL );
3749 else if (len > size)
3751 SetLastError( ERROR_INSUFFICIENT_BUFFER );
3752 goto done;
3754 else WideCharToMultiByte( CP_ACP, 0, result, retsize / sizeof(WCHAR), buffer, size, NULL, NULL );
3756 ret = len - 1;
3758 done:
3759 HeapFree( GetProcessHeap(), 0, result );
3760 return ret;
3764 /***********************************************************************
3765 * FormatMessageW (kernelbase.@)
3767 DWORD WINAPI DECLSPEC_HOTPATCH FormatMessageW( DWORD flags, const void *source, DWORD msgid, DWORD langid,
3768 WCHAR *buffer, DWORD size, __ms_va_list *args )
3770 ULONG retsize = 0;
3771 ULONG width = (flags & FORMAT_MESSAGE_MAX_WIDTH_MASK);
3772 const WCHAR *src;
3773 WCHAR *message = NULL;
3774 NTSTATUS status;
3776 TRACE( "(0x%x,%p,%#x,0x%x,%p,%u,%p)\n", flags, source, msgid, langid, buffer, size, args );
3778 if (!buffer)
3780 SetLastError( ERROR_INVALID_PARAMETER );
3781 return 0;
3784 if (width == 0xff) width = ~0u;
3786 if (flags & FORMAT_MESSAGE_ALLOCATE_BUFFER) *(LPWSTR *)buffer = NULL;
3788 if (!(src = get_message( flags, source, msgid, langid, FALSE, &message ))) return 0;
3790 if (flags & FORMAT_MESSAGE_ALLOCATE_BUFFER)
3792 WCHAR *result;
3793 ULONG alloc = max( size * sizeof(WCHAR), 65536 );
3795 for (;;)
3797 if (!(result = HeapAlloc( GetProcessHeap(), 0, alloc )))
3799 status = STATUS_NO_MEMORY;
3800 break;
3802 status = RtlFormatMessage( src, width, !!(flags & FORMAT_MESSAGE_IGNORE_INSERTS),
3803 FALSE, !!(flags & FORMAT_MESSAGE_ARGUMENT_ARRAY), args,
3804 result, alloc, &retsize );
3805 if (!status)
3807 if (retsize <= sizeof(WCHAR)) HeapFree( GetProcessHeap(), 0, result );
3808 else *(WCHAR **)buffer = HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY,
3809 result, max( retsize, size * sizeof(WCHAR) ));
3810 break;
3812 HeapFree( GetProcessHeap(), 0, result );
3813 if (status != STATUS_BUFFER_OVERFLOW) break;
3814 alloc *= 2;
3817 else status = RtlFormatMessage( src, width, !!(flags & FORMAT_MESSAGE_IGNORE_INSERTS),
3818 FALSE, !!(flags & FORMAT_MESSAGE_ARGUMENT_ARRAY), args,
3819 buffer, size * sizeof(WCHAR), &retsize );
3821 HeapFree( GetProcessHeap(), 0, message );
3823 if (status == STATUS_BUFFER_OVERFLOW)
3825 if (size) buffer[size - 1] = 0;
3826 SetLastError( ERROR_INSUFFICIENT_BUFFER );
3827 return 0;
3829 if (!set_ntstatus( status )) return 0;
3830 if (retsize <= sizeof(WCHAR)) SetLastError( ERROR_NO_WORK_DONE );
3831 return retsize / sizeof(WCHAR) - 1;
3835 /******************************************************************************
3836 * GetACP (kernelbase.@)
3838 UINT WINAPI GetACP(void)
3840 return nls_info.AnsiTableInfo.CodePage;
3844 /***********************************************************************
3845 * GetCPInfo (kernelbase.@)
3847 BOOL WINAPI DECLSPEC_HOTPATCH GetCPInfo( UINT codepage, CPINFO *cpinfo )
3849 const CPTABLEINFO *table;
3851 if (!cpinfo)
3853 SetLastError( ERROR_INVALID_PARAMETER );
3854 return FALSE;
3856 switch (codepage)
3858 case CP_UTF7:
3859 case CP_UTF8:
3860 cpinfo->DefaultChar[0] = 0x3f;
3861 cpinfo->DefaultChar[1] = 0;
3862 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
3863 cpinfo->MaxCharSize = (codepage == CP_UTF7) ? 5 : 4;
3864 break;
3865 default:
3866 if (!(table = get_codepage_table( codepage ))) return FALSE;
3867 cpinfo->MaxCharSize = table->MaximumCharacterSize;
3868 memcpy( cpinfo->DefaultChar, &table->DefaultChar, sizeof(cpinfo->DefaultChar) );
3869 memcpy( cpinfo->LeadByte, table->LeadByte, sizeof(cpinfo->LeadByte) );
3870 break;
3872 return TRUE;
3876 /***********************************************************************
3877 * GetCPInfoExW (kernelbase.@)
3879 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD flags, CPINFOEXW *cpinfo )
3881 const CPTABLEINFO *table;
3882 int min, max, pos;
3884 if (!cpinfo)
3886 SetLastError( ERROR_INVALID_PARAMETER );
3887 return FALSE;
3889 switch (codepage)
3891 case CP_UTF7:
3892 cpinfo->DefaultChar[0] = 0x3f;
3893 cpinfo->DefaultChar[1] = 0;
3894 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
3895 cpinfo->MaxCharSize = 5;
3896 cpinfo->CodePage = CP_UTF7;
3897 cpinfo->UnicodeDefaultChar = 0x3f;
3898 break;
3899 case CP_UTF8:
3900 cpinfo->DefaultChar[0] = 0x3f;
3901 cpinfo->DefaultChar[1] = 0;
3902 cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
3903 cpinfo->MaxCharSize = 4;
3904 cpinfo->CodePage = CP_UTF8;
3905 cpinfo->UnicodeDefaultChar = 0x3f;
3906 break;
3907 default:
3908 if (!(table = get_codepage_table( codepage ))) return FALSE;
3909 cpinfo->MaxCharSize = table->MaximumCharacterSize;
3910 memcpy( cpinfo->DefaultChar, &table->DefaultChar, sizeof(cpinfo->DefaultChar) );
3911 memcpy( cpinfo->LeadByte, table->LeadByte, sizeof(cpinfo->LeadByte) );
3912 cpinfo->CodePage = table->CodePage;
3913 cpinfo->UnicodeDefaultChar = table->UniDefaultChar;
3914 break;
3917 min = 0;
3918 max = ARRAY_SIZE(codepage_names) - 1;
3919 cpinfo->CodePageName[0] = 0;
3920 while (min <= max)
3922 pos = (min + max) / 2;
3923 if (codepage_names[pos].cp < cpinfo->CodePage) min = pos + 1;
3924 else if (codepage_names[pos].cp > cpinfo->CodePage) max = pos - 1;
3925 else
3927 wcscpy( cpinfo->CodePageName, codepage_names[pos].name );
3928 break;
3931 return TRUE;
3935 /***********************************************************************
3936 * GetCalendarInfoW (kernelbase.@)
3938 INT WINAPI DECLSPEC_HOTPATCH GetCalendarInfoW( LCID lcid, CALID calendar, CALTYPE type,
3939 WCHAR *data, INT count, DWORD *value )
3941 static const LCTYPE lctype_map[] =
3943 0, /* not used */
3944 0, /* CAL_ICALINTVALUE */
3945 0, /* CAL_SCALNAME */
3946 0, /* CAL_IYEAROFFSETRANGE */
3947 0, /* CAL_SERASTRING */
3948 LOCALE_SSHORTDATE,
3949 LOCALE_SLONGDATE,
3950 LOCALE_SDAYNAME1,
3951 LOCALE_SDAYNAME2,
3952 LOCALE_SDAYNAME3,
3953 LOCALE_SDAYNAME4,
3954 LOCALE_SDAYNAME5,
3955 LOCALE_SDAYNAME6,
3956 LOCALE_SDAYNAME7,
3957 LOCALE_SABBREVDAYNAME1,
3958 LOCALE_SABBREVDAYNAME2,
3959 LOCALE_SABBREVDAYNAME3,
3960 LOCALE_SABBREVDAYNAME4,
3961 LOCALE_SABBREVDAYNAME5,
3962 LOCALE_SABBREVDAYNAME6,
3963 LOCALE_SABBREVDAYNAME7,
3964 LOCALE_SMONTHNAME1,
3965 LOCALE_SMONTHNAME2,
3966 LOCALE_SMONTHNAME3,
3967 LOCALE_SMONTHNAME4,
3968 LOCALE_SMONTHNAME5,
3969 LOCALE_SMONTHNAME6,
3970 LOCALE_SMONTHNAME7,
3971 LOCALE_SMONTHNAME8,
3972 LOCALE_SMONTHNAME9,
3973 LOCALE_SMONTHNAME10,
3974 LOCALE_SMONTHNAME11,
3975 LOCALE_SMONTHNAME12,
3976 LOCALE_SMONTHNAME13,
3977 LOCALE_SABBREVMONTHNAME1,
3978 LOCALE_SABBREVMONTHNAME2,
3979 LOCALE_SABBREVMONTHNAME3,
3980 LOCALE_SABBREVMONTHNAME4,
3981 LOCALE_SABBREVMONTHNAME5,
3982 LOCALE_SABBREVMONTHNAME6,
3983 LOCALE_SABBREVMONTHNAME7,
3984 LOCALE_SABBREVMONTHNAME8,
3985 LOCALE_SABBREVMONTHNAME9,
3986 LOCALE_SABBREVMONTHNAME10,
3987 LOCALE_SABBREVMONTHNAME11,
3988 LOCALE_SABBREVMONTHNAME12,
3989 LOCALE_SABBREVMONTHNAME13,
3990 LOCALE_SYEARMONTH,
3991 0, /* CAL_ITWODIGITYEARMAX */
3992 LOCALE_SSHORTESTDAYNAME1,
3993 LOCALE_SSHORTESTDAYNAME2,
3994 LOCALE_SSHORTESTDAYNAME3,
3995 LOCALE_SSHORTESTDAYNAME4,
3996 LOCALE_SSHORTESTDAYNAME5,
3997 LOCALE_SSHORTESTDAYNAME6,
3998 LOCALE_SSHORTESTDAYNAME7,
3999 LOCALE_SMONTHDAY,
4000 0, /* CAL_SABBREVERASTRING */
4002 DWORD flags = 0;
4003 CALTYPE calinfo = type & 0xffff;
4005 if (type & CAL_NOUSEROVERRIDE) FIXME("flag CAL_NOUSEROVERRIDE used, not fully implemented\n");
4006 if (type & CAL_USE_CP_ACP) FIXME("flag CAL_USE_CP_ACP used, not fully implemented\n");
4008 if ((type & CAL_RETURN_NUMBER) && !value)
4010 SetLastError( ERROR_INVALID_PARAMETER );
4011 return 0;
4014 if (type & CAL_RETURN_GENITIVE_NAMES) flags |= LOCALE_RETURN_GENITIVE_NAMES;
4016 switch (calinfo)
4018 case CAL_ICALINTVALUE:
4019 if (type & CAL_RETURN_NUMBER)
4020 return GetLocaleInfoW( lcid, LOCALE_RETURN_NUMBER | LOCALE_ICALENDARTYPE,
4021 (WCHAR *)value, sizeof(*value) / sizeof(WCHAR) );
4022 return GetLocaleInfoW( lcid, LOCALE_ICALENDARTYPE, data, count );
4024 case CAL_SCALNAME:
4025 FIXME( "Unimplemented caltype %d\n", calinfo );
4026 if (data) *data = 0;
4027 return 1;
4029 case CAL_IYEAROFFSETRANGE:
4030 case CAL_SERASTRING:
4031 case CAL_SABBREVERASTRING:
4032 FIXME( "Unimplemented caltype %d\n", calinfo );
4033 return 0;
4035 case CAL_SSHORTDATE:
4036 case CAL_SLONGDATE:
4037 case CAL_SDAYNAME1:
4038 case CAL_SDAYNAME2:
4039 case CAL_SDAYNAME3:
4040 case CAL_SDAYNAME4:
4041 case CAL_SDAYNAME5:
4042 case CAL_SDAYNAME6:
4043 case CAL_SDAYNAME7:
4044 case CAL_SABBREVDAYNAME1:
4045 case CAL_SABBREVDAYNAME2:
4046 case CAL_SABBREVDAYNAME3:
4047 case CAL_SABBREVDAYNAME4:
4048 case CAL_SABBREVDAYNAME5:
4049 case CAL_SABBREVDAYNAME6:
4050 case CAL_SABBREVDAYNAME7:
4051 case CAL_SMONTHNAME1:
4052 case CAL_SMONTHNAME2:
4053 case CAL_SMONTHNAME3:
4054 case CAL_SMONTHNAME4:
4055 case CAL_SMONTHNAME5:
4056 case CAL_SMONTHNAME6:
4057 case CAL_SMONTHNAME7:
4058 case CAL_SMONTHNAME8:
4059 case CAL_SMONTHNAME9:
4060 case CAL_SMONTHNAME10:
4061 case CAL_SMONTHNAME11:
4062 case CAL_SMONTHNAME12:
4063 case CAL_SMONTHNAME13:
4064 case CAL_SABBREVMONTHNAME1:
4065 case CAL_SABBREVMONTHNAME2:
4066 case CAL_SABBREVMONTHNAME3:
4067 case CAL_SABBREVMONTHNAME4:
4068 case CAL_SABBREVMONTHNAME5:
4069 case CAL_SABBREVMONTHNAME6:
4070 case CAL_SABBREVMONTHNAME7:
4071 case CAL_SABBREVMONTHNAME8:
4072 case CAL_SABBREVMONTHNAME9:
4073 case CAL_SABBREVMONTHNAME10:
4074 case CAL_SABBREVMONTHNAME11:
4075 case CAL_SABBREVMONTHNAME12:
4076 case CAL_SABBREVMONTHNAME13:
4077 case CAL_SMONTHDAY:
4078 case CAL_SYEARMONTH:
4079 case CAL_SSHORTESTDAYNAME1:
4080 case CAL_SSHORTESTDAYNAME2:
4081 case CAL_SSHORTESTDAYNAME3:
4082 case CAL_SSHORTESTDAYNAME4:
4083 case CAL_SSHORTESTDAYNAME5:
4084 case CAL_SSHORTESTDAYNAME6:
4085 case CAL_SSHORTESTDAYNAME7:
4086 return GetLocaleInfoW( lcid, lctype_map[calinfo] | flags, data, count );
4088 case CAL_ITWODIGITYEARMAX:
4089 if (type & CAL_RETURN_NUMBER)
4091 *value = CALINFO_MAX_YEAR;
4092 return sizeof(DWORD) / sizeof(WCHAR);
4094 else
4096 WCHAR buffer[10];
4097 int ret = swprintf( buffer, ARRAY_SIZE(buffer), L"%u", CALINFO_MAX_YEAR ) + 1;
4098 if (!data) return ret;
4099 if (ret <= count)
4101 lstrcpyW( data, buffer );
4102 return ret;
4104 SetLastError( ERROR_INSUFFICIENT_BUFFER );
4105 return 0;
4107 break;
4108 default:
4109 FIXME( "Unknown caltype %d\n", calinfo );
4110 SetLastError( ERROR_INVALID_FLAGS );
4111 return 0;
4113 return 0;
4117 /***********************************************************************
4118 * GetCalendarInfoEx (kernelbase.@)
4120 INT WINAPI DECLSPEC_HOTPATCH GetCalendarInfoEx( const WCHAR *locale, CALID calendar, const WCHAR *reserved,
4121 CALTYPE type, WCHAR *data, INT count, DWORD *value )
4123 LCID lcid = LocaleNameToLCID( locale, 0 );
4124 return GetCalendarInfoW( lcid, calendar, type, data, count, value );
4127 static CRITICAL_SECTION tzname_section;
4128 static CRITICAL_SECTION_DEBUG tzname_section_debug =
4130 0, 0, &tzname_section,
4131 { &tzname_section_debug.ProcessLocksList, &tzname_section_debug.ProcessLocksList },
4132 0, 0, { (DWORD_PTR)(__FILE__ ": tzname_section") }
4134 static CRITICAL_SECTION tzname_section = { &tzname_section_debug, -1, 0, 0, 0, 0 };
4135 static struct {
4136 LCID lcid;
4137 WCHAR key_name[128];
4138 WCHAR standard_name[32];
4139 WCHAR daylight_name[32];
4140 } cached_tzname;
4142 /***********************************************************************
4143 * GetDynamicTimeZoneInformation (kernelbase.@)
4145 DWORD WINAPI DECLSPEC_HOTPATCH GetDynamicTimeZoneInformation( DYNAMIC_TIME_ZONE_INFORMATION *info )
4147 HKEY key;
4148 LARGE_INTEGER now;
4150 if (!set_ntstatus( RtlQueryDynamicTimeZoneInformation( (RTL_DYNAMIC_TIME_ZONE_INFORMATION *)info )))
4151 return TIME_ZONE_ID_INVALID;
4153 RtlEnterCriticalSection( &tzname_section );
4154 if (cached_tzname.lcid == GetThreadLocale() &&
4155 !wcscmp( info->TimeZoneKeyName, cached_tzname.key_name ))
4157 wcscpy( info->StandardName, cached_tzname.standard_name );
4158 wcscpy( info->DaylightName, cached_tzname.daylight_name );
4159 RtlLeaveCriticalSection( &tzname_section );
4161 else
4163 RtlLeaveCriticalSection( &tzname_section );
4164 if (!RegOpenKeyExW( tz_key, info->TimeZoneKeyName, 0, KEY_ALL_ACCESS, &key ))
4166 RegLoadMUIStringW( key, L"MUI_Std", info->StandardName,
4167 sizeof(info->StandardName), NULL, 0, system_dir );
4168 RegLoadMUIStringW( key, L"MUI_Dlt", info->DaylightName,
4169 sizeof(info->DaylightName), NULL, 0, system_dir );
4170 RegCloseKey( key );
4172 else return TIME_ZONE_ID_INVALID;
4174 RtlEnterCriticalSection( &tzname_section );
4175 cached_tzname.lcid = GetThreadLocale();
4176 wcscpy( cached_tzname.key_name, info->TimeZoneKeyName );
4177 wcscpy( cached_tzname.standard_name, info->StandardName );
4178 wcscpy( cached_tzname.daylight_name, info->DaylightName );
4179 RtlLeaveCriticalSection( &tzname_section );
4182 NtQuerySystemTime( &now );
4183 return get_timezone_id( (TIME_ZONE_INFORMATION *)info, now, FALSE );
4187 /******************************************************************************
4188 * GetDynamicTimeZoneInformationEffectiveYears (kernelbase.@)
4190 DWORD WINAPI DECLSPEC_HOTPATCH GetDynamicTimeZoneInformationEffectiveYears( const DYNAMIC_TIME_ZONE_INFORMATION *info,
4191 DWORD *first, DWORD *last )
4193 HKEY key, dst_key = 0;
4194 DWORD type, count, ret = ERROR_FILE_NOT_FOUND;
4196 if (RegOpenKeyExW( tz_key, info->TimeZoneKeyName, 0, KEY_ALL_ACCESS, &key )) return ret;
4198 if (RegOpenKeyExW( key, L"Dynamic DST", 0, KEY_ALL_ACCESS, &dst_key )) goto done;
4199 count = sizeof(DWORD);
4200 if (RegQueryValueExW( dst_key, L"FirstEntry", NULL, &type, (BYTE *)first, &count )) goto done;
4201 if (type != REG_DWORD) goto done;
4202 count = sizeof(DWORD);
4203 if (RegQueryValueExW( dst_key, L"LastEntry", NULL, &type, (BYTE *)last, &count )) goto done;
4204 if (type != REG_DWORD) goto done;
4205 ret = 0;
4207 done:
4208 RegCloseKey( dst_key );
4209 RegCloseKey( key );
4210 return ret;
4214 /******************************************************************************
4215 * GetFileMUIInfo (kernelbase.@)
4217 BOOL WINAPI /* DECLSPEC_HOTPATCH */ GetFileMUIInfo( DWORD flags, const WCHAR *path,
4218 FILEMUIINFO *info, DWORD *size )
4220 FIXME( "stub: %u, %s, %p, %p\n", flags, debugstr_w(path), info, size );
4221 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
4222 return FALSE;
4226 /******************************************************************************
4227 * GetFileMUIPath (kernelbase.@)
4229 BOOL WINAPI /* DECLSPEC_HOTPATCH */ GetFileMUIPath( DWORD flags, const WCHAR *filepath,
4230 WCHAR *language, ULONG *languagelen,
4231 WCHAR *muipath, ULONG *muipathlen,
4232 ULONGLONG *enumerator )
4234 FIXME( "stub: 0x%x, %s, %s, %p, %p, %p, %p\n", flags, debugstr_w(filepath),
4235 debugstr_w(language), languagelen, muipath, muipathlen, enumerator );
4236 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
4237 return FALSE;
4241 /******************************************************************************
4242 * GetGeoInfoW (kernelbase.@)
4244 INT WINAPI DECLSPEC_HOTPATCH GetGeoInfoW( GEOID id, GEOTYPE type, WCHAR *data, int count, LANGID lang )
4246 const struct geoinfo *ptr = get_geoinfo_ptr( id );
4247 WCHAR bufferW[12];
4248 const WCHAR *str = bufferW;
4249 int len;
4251 TRACE( "%d %d %p %d %d\n", id, type, data, count, lang );
4253 if (!ptr)
4255 SetLastError( ERROR_INVALID_PARAMETER );
4256 return 0;
4258 switch (type)
4260 case GEO_NATION:
4261 if (ptr->kind != LOCATION_NATION) return 0;
4262 /* fall through */
4263 case GEO_ID:
4264 swprintf( bufferW, ARRAY_SIZE(bufferW), L"%u", ptr->id );
4265 break;
4266 case GEO_ISO_UN_NUMBER:
4267 swprintf( bufferW, ARRAY_SIZE(bufferW), L"%03u", ptr->uncode );
4268 break;
4269 case GEO_PARENT:
4270 swprintf( bufferW, ARRAY_SIZE(bufferW), L"%u", ptr->parent );
4271 break;
4272 case GEO_ISO2:
4273 str = ptr->iso2W;
4274 break;
4275 case GEO_ISO3:
4276 str = ptr->iso3W;
4277 break;
4278 case GEO_RFC1766:
4279 case GEO_LCID:
4280 case GEO_FRIENDLYNAME:
4281 case GEO_OFFICIALNAME:
4282 case GEO_TIMEZONES:
4283 case GEO_OFFICIALLANGUAGES:
4284 case GEO_LATITUDE:
4285 case GEO_LONGITUDE:
4286 case GEO_DIALINGCODE:
4287 case GEO_CURRENCYCODE:
4288 case GEO_CURRENCYSYMBOL:
4289 case GEO_NAME:
4290 FIXME( "type %d is not supported\n", type );
4291 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
4292 return 0;
4293 default:
4294 WARN( "unrecognized type %d\n", type );
4295 SetLastError( ERROR_INVALID_FLAGS );
4296 return 0;
4299 len = lstrlenW(str) + 1;
4300 if (!data || !count) return len;
4302 memcpy( data, str, min(len, count) * sizeof(WCHAR) );
4303 if (count < len) SetLastError( ERROR_INSUFFICIENT_BUFFER );
4304 return count < len ? 0 : len;
4308 /******************************************************************************
4309 * GetLocaleInfoA (kernelbase.@)
4311 INT WINAPI DECLSPEC_HOTPATCH GetLocaleInfoA( LCID lcid, LCTYPE lctype, char *buffer, INT len )
4313 WCHAR *bufferW;
4314 INT lenW, ret;
4316 TRACE( "lcid=0x%x lctype=0x%x %p %d\n", lcid, lctype, buffer, len );
4318 if (len < 0 || (len && !buffer))
4320 SetLastError( ERROR_INVALID_PARAMETER );
4321 return 0;
4323 if (LOWORD(lctype) == LOCALE_SSHORTTIME || (lctype & LOCALE_RETURN_GENITIVE_NAMES))
4325 SetLastError( ERROR_INVALID_FLAGS );
4326 return 0;
4329 if (LOWORD(lctype) == LOCALE_FONTSIGNATURE || (lctype & LOCALE_RETURN_NUMBER))
4330 return GetLocaleInfoW( lcid, lctype, (WCHAR *)buffer, len / sizeof(WCHAR) ) * sizeof(WCHAR);
4332 if (!(lenW = GetLocaleInfoW( lcid, lctype, NULL, 0 ))) return 0;
4334 if (!(bufferW = RtlAllocateHeap( GetProcessHeap(), 0, lenW * sizeof(WCHAR) )))
4336 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
4337 return 0;
4339 ret = GetLocaleInfoW( lcid, lctype, bufferW, lenW );
4340 if (ret) ret = WideCharToMultiByte( get_lcid_codepage( lcid, lctype ), 0,
4341 bufferW, ret, buffer, len, NULL, NULL );
4342 RtlFreeHeap( GetProcessHeap(), 0, bufferW );
4343 return ret;
4347 /******************************************************************************
4348 * GetLocaleInfoW (kernelbase.@)
4350 INT WINAPI DECLSPEC_HOTPATCH GetLocaleInfoW( LCID lcid, LCTYPE lctype, WCHAR *buffer, INT len )
4352 HRSRC hrsrc;
4353 HGLOBAL hmem;
4354 INT ret;
4355 UINT lcflags = lctype;
4356 const WCHAR *p;
4357 unsigned int i;
4359 if (len < 0 || (len && !buffer))
4361 SetLastError( ERROR_INVALID_PARAMETER );
4362 return 0;
4364 if (lctype & LOCALE_RETURN_GENITIVE_NAMES && !is_genitive_name_supported( lctype ))
4366 SetLastError( ERROR_INVALID_FLAGS );
4367 return 0;
4370 if (!len) buffer = NULL;
4372 lcid = ConvertDefaultLocale( lcid );
4373 lctype = LOWORD(lctype);
4375 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d)\n", lcid, lctype, buffer, len );
4377 /* first check for overrides in the registry */
4379 if (!(lcflags & LOCALE_NOUSEROVERRIDE) && lcid == ConvertDefaultLocale( LOCALE_USER_DEFAULT ))
4381 const struct registry_value *value = get_locale_registry_value( lctype );
4383 if (value)
4385 if (lcflags & LOCALE_RETURN_NUMBER)
4387 WCHAR tmp[16];
4388 ret = get_registry_locale_info( value, tmp, ARRAY_SIZE( tmp ));
4389 if (ret > 0)
4391 WCHAR *end;
4392 UINT number = wcstol( tmp, &end, get_value_base_by_lctype( lctype ) );
4393 if (*end) /* invalid number */
4395 SetLastError( ERROR_INVALID_FLAGS );
4396 return 0;
4398 ret = sizeof(UINT) / sizeof(WCHAR);
4399 if (!len) return ret;
4400 if (ret > len)
4402 SetLastError( ERROR_INSUFFICIENT_BUFFER );
4403 return 0;
4405 memcpy( buffer, &number, sizeof(number) );
4408 else ret = get_registry_locale_info( value, buffer, len );
4410 if (ret != -1) return ret;
4414 /* now load it from kernel resources */
4416 if (!(hrsrc = FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
4417 ULongToPtr((lctype >> 4) + 1), lcid )))
4419 SetLastError( ERROR_INVALID_FLAGS ); /* no such lctype */
4420 return 0;
4422 if (!(hmem = LoadResource( kernel32_handle, hrsrc ))) return 0;
4424 p = LockResource( hmem );
4425 for (i = 0; i < (lctype & 0x0f); i++) p += *p + 1;
4427 if (lcflags & LOCALE_RETURN_NUMBER) ret = sizeof(UINT) / sizeof(WCHAR);
4428 else if (is_genitive_name_supported( lctype ) && *p)
4430 /* genitive form is stored after a null separator from a nominative */
4431 for (i = 1; i <= *p; i++) if (!p[i]) break;
4433 if (i <= *p && (lcflags & LOCALE_RETURN_GENITIVE_NAMES))
4435 ret = *p - i + 1;
4436 p += i;
4438 else ret = i;
4440 else
4441 ret = (lctype == LOCALE_FONTSIGNATURE) ? *p : *p + 1;
4443 if (!len) return ret;
4445 if (ret > len)
4447 SetLastError( ERROR_INSUFFICIENT_BUFFER );
4448 return 0;
4451 if (lcflags & LOCALE_RETURN_NUMBER)
4453 UINT number;
4454 WCHAR *end, *tmp = HeapAlloc( GetProcessHeap(), 0, (*p + 1) * sizeof(WCHAR) );
4455 if (!tmp) return 0;
4456 memcpy( tmp, p + 1, *p * sizeof(WCHAR) );
4457 tmp[*p] = 0;
4458 number = wcstol( tmp, &end, get_value_base_by_lctype( lctype ) );
4459 if (!*end)
4460 memcpy( buffer, &number, sizeof(number) );
4461 else /* invalid number */
4463 SetLastError( ERROR_INVALID_FLAGS );
4464 ret = 0;
4466 HeapFree( GetProcessHeap(), 0, tmp );
4468 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning number %d\n",
4469 lcid, lctype, buffer, len, number );
4471 else
4473 memcpy( buffer, p + 1, ret * sizeof(WCHAR) );
4474 if (lctype != LOCALE_FONTSIGNATURE) buffer[ret-1] = 0;
4476 TRACE( "(lcid=0x%x,lctype=0x%x,%p,%d) returning %d %s\n",
4477 lcid, lctype, buffer, len, ret, debugstr_w(buffer) );
4479 return ret;
4483 /******************************************************************************
4484 * GetLocaleInfoEx (kernelbase.@)
4486 INT WINAPI DECLSPEC_HOTPATCH GetLocaleInfoEx( const WCHAR *locale, LCTYPE info, WCHAR *buffer, INT len )
4488 LCID lcid = LocaleNameToLCID( locale, 0 );
4490 TRACE( "%s lcid=0x%x 0x%x\n", debugstr_w(locale), lcid, info );
4492 if (!lcid) return 0;
4494 /* special handling for neutral locale names */
4495 if (locale && lstrlenW( locale ) == 2)
4497 switch (LOWORD( info ))
4499 case LOCALE_SNAME:
4500 if (len && len < 3)
4502 SetLastError( ERROR_INSUFFICIENT_BUFFER );
4503 return 0;
4505 if (len) lstrcpyW( buffer, locale );
4506 return 3;
4507 case LOCALE_SPARENT:
4508 if (len) buffer[0] = 0;
4509 return 1;
4512 return GetLocaleInfoW( lcid, info, buffer, len );
4516 /******************************************************************************
4517 * GetNLSVersion (kernelbase.@)
4519 BOOL WINAPI DECLSPEC_HOTPATCH GetNLSVersion( NLS_FUNCTION func, LCID lcid, NLSVERSIONINFO *info )
4521 WCHAR locale[LOCALE_NAME_MAX_LENGTH];
4523 if (info->dwNLSVersionInfoSize < offsetof( NLSVERSIONINFO, dwEffectiveId ))
4525 SetLastError( ERROR_INSUFFICIENT_BUFFER );
4526 return FALSE;
4528 if (!LCIDToLocaleName( lcid, locale, LOCALE_NAME_MAX_LENGTH, LOCALE_ALLOW_NEUTRAL_NAMES ))
4530 SetLastError( ERROR_INVALID_PARAMETER );
4531 return FALSE;
4533 return GetNLSVersionEx( func, locale, (NLSVERSIONINFOEX *)info );
4537 /******************************************************************************
4538 * GetNLSVersionEx (kernelbase.@)
4540 BOOL WINAPI DECLSPEC_HOTPATCH GetNLSVersionEx( NLS_FUNCTION func, const WCHAR *locale,
4541 NLSVERSIONINFOEX *info )
4543 LCID lcid = 0;
4545 if (func != COMPARE_STRING)
4547 SetLastError( ERROR_INVALID_FLAGS );
4548 return FALSE;
4550 if (info->dwNLSVersionInfoSize < sizeof(*info) &&
4551 (info->dwNLSVersionInfoSize != offsetof( NLSVERSIONINFO, dwEffectiveId )))
4553 SetLastError( ERROR_INSUFFICIENT_BUFFER );
4554 return FALSE;
4557 if (!(lcid = LocaleNameToLCID( locale, 0 ))) return FALSE;
4559 info->dwNLSVersion = info->dwDefinedVersion = sort.version;
4560 if (info->dwNLSVersionInfoSize >= sizeof(*info))
4562 const struct sortguid *sortid = get_language_sort( locale );
4563 info->dwEffectiveId = lcid;
4564 info->guidCustomVersion = sortid ? sortid->id : default_sort_guid;
4566 return TRUE;
4570 /******************************************************************************
4571 * GetOEMCP (kernelbase.@)
4573 UINT WINAPI GetOEMCP(void)
4575 return nls_info.OemTableInfo.CodePage;
4579 /***********************************************************************
4580 * GetProcessPreferredUILanguages (kernelbase.@)
4582 BOOL WINAPI DECLSPEC_HOTPATCH GetProcessPreferredUILanguages( DWORD flags, ULONG *count,
4583 WCHAR *buffer, ULONG *size )
4585 return set_ntstatus( RtlGetProcessPreferredUILanguages( flags, count, buffer, size ));
4589 /***********************************************************************
4590 * GetStringTypeA (kernelbase.@)
4592 BOOL WINAPI DECLSPEC_HOTPATCH GetStringTypeA( LCID locale, DWORD type, const char *src, int count,
4593 WORD *chartype )
4595 UINT cp;
4596 INT countW;
4597 LPWSTR srcW;
4598 BOOL ret = FALSE;
4600 if (count == -1) count = strlen(src) + 1;
4602 cp = get_lcid_codepage( locale, 0 );
4603 countW = MultiByteToWideChar(cp, 0, src, count, NULL, 0);
4604 if((srcW = HeapAlloc(GetProcessHeap(), 0, countW * sizeof(WCHAR))))
4606 MultiByteToWideChar(cp, 0, src, count, srcW, countW);
4608 * NOTE: the target buffer has 1 word for each CHARACTER in the source
4609 * string, with multibyte characters there maybe be more bytes in count
4610 * than character space in the buffer!
4612 ret = GetStringTypeW(type, srcW, countW, chartype);
4613 HeapFree(GetProcessHeap(), 0, srcW);
4615 return ret;
4619 /***********************************************************************
4620 * GetStringTypeW (kernelbase.@)
4622 BOOL WINAPI DECLSPEC_HOTPATCH GetStringTypeW( DWORD type, const WCHAR *src, INT count, WORD *chartype )
4624 if (!src)
4626 SetLastError( ERROR_INVALID_PARAMETER );
4627 return FALSE;
4629 if (type != CT_CTYPE1 && type != CT_CTYPE2 && type != CT_CTYPE3)
4631 SetLastError( ERROR_INVALID_PARAMETER );
4632 return FALSE;
4635 if (count == -1) count = lstrlenW(src) + 1;
4637 while (count--) *chartype++ = get_char_type( type, *src++ );
4639 return TRUE;
4643 /***********************************************************************
4644 * GetStringTypeExW (kernelbase.@)
4646 BOOL WINAPI DECLSPEC_HOTPATCH GetStringTypeExW( LCID locale, DWORD type, const WCHAR *src, int count,
4647 WORD *chartype )
4649 /* locale is ignored for Unicode */
4650 return GetStringTypeW( type, src, count, chartype );
4654 /***********************************************************************
4655 * GetSystemDefaultLCID (kernelbase.@)
4657 LCID WINAPI DECLSPEC_HOTPATCH GetSystemDefaultLCID(void)
4659 LCID lcid;
4660 NtQueryDefaultLocale( FALSE, &lcid );
4661 return lcid;
4665 /***********************************************************************
4666 * GetSystemDefaultLangID (kernelbase.@)
4668 LANGID WINAPI DECLSPEC_HOTPATCH GetSystemDefaultLangID(void)
4670 return LANGIDFROMLCID( GetSystemDefaultLCID() );
4674 /***********************************************************************
4675 * GetSystemDefaultLocaleName (kernelbase.@)
4677 INT WINAPI DECLSPEC_HOTPATCH GetSystemDefaultLocaleName( LPWSTR name, INT len )
4679 return LCIDToLocaleName( GetSystemDefaultLCID(), name, len, 0 );
4683 /***********************************************************************
4684 * GetSystemDefaultUILanguage (kernelbase.@)
4686 LANGID WINAPI DECLSPEC_HOTPATCH GetSystemDefaultUILanguage(void)
4688 LANGID lang;
4689 NtQueryInstallUILanguage( &lang );
4690 return lang;
4694 /***********************************************************************
4695 * GetSystemPreferredUILanguages (kernelbase.@)
4697 BOOL WINAPI DECLSPEC_HOTPATCH GetSystemPreferredUILanguages( DWORD flags, ULONG *count,
4698 WCHAR *buffer, ULONG *size )
4700 return set_ntstatus( RtlGetSystemPreferredUILanguages( flags, 0, count, buffer, size ));
4704 /***********************************************************************
4705 * GetThreadPreferredUILanguages (kernelbase.@)
4707 BOOL WINAPI DECLSPEC_HOTPATCH GetThreadPreferredUILanguages( DWORD flags, ULONG *count,
4708 WCHAR *buffer, ULONG *size )
4710 return set_ntstatus( RtlGetThreadPreferredUILanguages( flags, count, buffer, size ));
4714 /***********************************************************************
4715 * GetTimeZoneInformation (kernelbase.@)
4717 DWORD WINAPI DECLSPEC_HOTPATCH GetTimeZoneInformation( TIME_ZONE_INFORMATION *info )
4719 DYNAMIC_TIME_ZONE_INFORMATION tzinfo;
4720 DWORD ret = GetDynamicTimeZoneInformation( &tzinfo );
4722 memcpy( info, &tzinfo, sizeof(*info) );
4723 return ret;
4727 /***********************************************************************
4728 * GetTimeZoneInformationForYear (kernelbase.@)
4730 BOOL WINAPI DECLSPEC_HOTPATCH GetTimeZoneInformationForYear( USHORT year,
4731 DYNAMIC_TIME_ZONE_INFORMATION *dynamic,
4732 TIME_ZONE_INFORMATION *info )
4734 DYNAMIC_TIME_ZONE_INFORMATION local_info;
4735 HKEY key = 0, dst_key;
4736 DWORD count;
4737 LRESULT ret;
4738 struct
4740 LONG bias;
4741 LONG std_bias;
4742 LONG dlt_bias;
4743 SYSTEMTIME std_date;
4744 SYSTEMTIME dlt_date;
4745 } data;
4747 TRACE( "(%u,%p)\n", year, info );
4749 if (!dynamic)
4751 if (GetDynamicTimeZoneInformation( &local_info ) == TIME_ZONE_ID_INVALID) return FALSE;
4752 dynamic = &local_info;
4755 if ((ret = RegOpenKeyExW( tz_key, dynamic->TimeZoneKeyName, 0, KEY_ALL_ACCESS, &key ))) goto done;
4756 if (RegLoadMUIStringW( key, L"MUI_Std", info->StandardName,
4757 sizeof(info->StandardName), NULL, 0, system_dir ))
4759 count = sizeof(info->StandardName);
4760 if ((ret = RegQueryValueExW( key, L"Std", NULL, NULL, (BYTE *)info->StandardName, &count )))
4761 goto done;
4763 if (RegLoadMUIStringW( key, L"MUI_Dlt", info->DaylightName,
4764 sizeof(info->DaylightName), NULL, 0, system_dir ))
4766 count = sizeof(info->DaylightName);
4767 if ((ret = RegQueryValueExW( key, L"Dlt", NULL, NULL, (BYTE *)info->DaylightName, &count )))
4768 goto done;
4771 ret = ERROR_FILE_NOT_FOUND;
4772 if (!dynamic->DynamicDaylightTimeDisabled &&
4773 !RegOpenKeyExW( key, L"Dynamic DST", 0, KEY_ALL_ACCESS, &dst_key ))
4775 WCHAR yearW[16];
4776 swprintf( yearW, ARRAY_SIZE(yearW), L"%u", year );
4777 count = sizeof(data);
4778 ret = RegQueryValueExW( dst_key, yearW, NULL, NULL, (BYTE *)&data, &count );
4779 RegCloseKey( dst_key );
4781 if (ret)
4783 count = sizeof(data);
4784 ret = RegQueryValueExW( key, L"TZI", NULL, NULL, (BYTE *)&data, &count );
4787 if (!ret)
4789 info->Bias = data.bias;
4790 info->StandardBias = data.std_bias;
4791 info->DaylightBias = data.dlt_bias;
4792 info->StandardDate = data.std_date;
4793 info->DaylightDate = data.dlt_date;
4796 done:
4797 RegCloseKey( key );
4798 if (ret) SetLastError( ret );
4799 return !ret;
4803 /***********************************************************************
4804 * GetUserDefaultLCID (kernelbase.@)
4806 LCID WINAPI DECLSPEC_HOTPATCH GetUserDefaultLCID(void)
4808 LCID lcid;
4809 NtQueryDefaultLocale( TRUE, &lcid );
4810 return lcid;
4814 /***********************************************************************
4815 * GetUserDefaultLangID (kernelbase.@)
4817 LANGID WINAPI DECLSPEC_HOTPATCH GetUserDefaultLangID(void)
4819 return LANGIDFROMLCID( GetUserDefaultLCID() );
4823 /***********************************************************************
4824 * GetUserDefaultLocaleName (kernelbase.@)
4826 INT WINAPI DECLSPEC_HOTPATCH GetUserDefaultLocaleName( LPWSTR name, INT len )
4828 return LCIDToLocaleName( GetUserDefaultLCID(), name, len, 0 );
4832 /***********************************************************************
4833 * GetUserDefaultUILanguage (kernelbase.@)
4835 LANGID WINAPI DECLSPEC_HOTPATCH GetUserDefaultUILanguage(void)
4837 LANGID lang;
4838 NtQueryDefaultUILanguage( &lang );
4839 return lang;
4843 /******************************************************************************
4844 * GetUserGeoID (kernelbase.@)
4846 GEOID WINAPI DECLSPEC_HOTPATCH GetUserGeoID( GEOCLASS geoclass )
4848 GEOID ret = 39070;
4849 const WCHAR *name;
4850 WCHAR bufferW[40];
4851 HKEY hkey;
4853 switch (geoclass)
4855 case GEOCLASS_NATION:
4856 name = L"Nation";
4857 break;
4858 case GEOCLASS_REGION:
4859 name = L"Region";
4860 break;
4861 default:
4862 WARN("Unknown geoclass %d\n", geoclass);
4863 return GEOID_NOT_AVAILABLE;
4865 if (!RegOpenKeyExW( intl_key, L"Geo", 0, KEY_ALL_ACCESS, &hkey ))
4867 DWORD count = sizeof(bufferW);
4868 if (!RegQueryValueExW( hkey, name, NULL, NULL, (BYTE *)bufferW, &count ))
4869 ret = wcstol( bufferW, NULL, 10 );
4870 RegCloseKey( hkey );
4872 return ret;
4876 /******************************************************************************
4877 * GetUserPreferredUILanguages (kernelbase.@)
4879 BOOL WINAPI DECLSPEC_HOTPATCH GetUserPreferredUILanguages( DWORD flags, ULONG *count,
4880 WCHAR *buffer, ULONG *size )
4882 return set_ntstatus( RtlGetUserPreferredUILanguages( flags, 0, count, buffer, size ));
4886 /******************************************************************************
4887 * IdnToAscii (kernelbase.@)
4889 INT WINAPI DECLSPEC_HOTPATCH IdnToAscii( DWORD flags, const WCHAR *src, INT srclen,
4890 WCHAR *dst, INT dstlen )
4892 NTSTATUS status = RtlIdnToAscii( flags, src, srclen, dst, &dstlen );
4893 if (!set_ntstatus( status )) return 0;
4894 return dstlen;
4898 /******************************************************************************
4899 * IdnToNameprepUnicode (kernelbase.@)
4901 INT WINAPI DECLSPEC_HOTPATCH IdnToNameprepUnicode( DWORD flags, const WCHAR *src, INT srclen,
4902 WCHAR *dst, INT dstlen )
4904 NTSTATUS status = RtlIdnToNameprepUnicode( flags, src, srclen, dst, &dstlen );
4905 if (!set_ntstatus( status )) return 0;
4906 return dstlen;
4910 /******************************************************************************
4911 * IdnToUnicode (kernelbase.@)
4913 INT WINAPI DECLSPEC_HOTPATCH IdnToUnicode( DWORD flags, const WCHAR *src, INT srclen,
4914 WCHAR *dst, INT dstlen )
4916 NTSTATUS status = RtlIdnToUnicode( flags, src, srclen, dst, &dstlen );
4917 if (!set_ntstatus( status )) return 0;
4918 return dstlen;
4922 /******************************************************************************
4923 * IsCharAlphaA (kernelbase.@)
4925 BOOL WINAPI DECLSPEC_HOTPATCH IsCharAlphaA( CHAR c )
4927 WCHAR wc = nls_info.AnsiTableInfo.MultiByteTable[(unsigned char)c];
4928 return !!(get_char_type( CT_CTYPE1, wc ) & C1_ALPHA);
4932 /******************************************************************************
4933 * IsCharAlphaW (kernelbase.@)
4935 BOOL WINAPI DECLSPEC_HOTPATCH IsCharAlphaW( WCHAR wc )
4937 return !!(get_char_type( CT_CTYPE1, wc ) & C1_ALPHA);
4941 /******************************************************************************
4942 * IsCharAlphaNumericA (kernelbase.@)
4944 BOOL WINAPI DECLSPEC_HOTPATCH IsCharAlphaNumericA( CHAR c )
4946 WCHAR wc = nls_info.AnsiTableInfo.MultiByteTable[(unsigned char)c];
4947 return !!(get_char_type( CT_CTYPE1, wc ) & (C1_ALPHA | C1_DIGIT));
4951 /******************************************************************************
4952 * IsCharAlphaNumericW (kernelbase.@)
4954 BOOL WINAPI DECLSPEC_HOTPATCH IsCharAlphaNumericW( WCHAR wc )
4956 return !!(get_char_type( CT_CTYPE1, wc ) & (C1_ALPHA | C1_DIGIT));
4960 /******************************************************************************
4961 * IsCharBlankW (kernelbase.@)
4963 BOOL WINAPI DECLSPEC_HOTPATCH IsCharBlankW( WCHAR wc )
4965 return !!(get_char_type( CT_CTYPE1, wc ) & C1_BLANK);
4969 /******************************************************************************
4970 * IsCharCntrlW (kernelbase.@)
4972 BOOL WINAPI DECLSPEC_HOTPATCH IsCharCntrlW( WCHAR wc )
4974 return !!(get_char_type( CT_CTYPE1, wc ) & C1_CNTRL);
4978 /******************************************************************************
4979 * IsCharDigitW (kernelbase.@)
4981 BOOL WINAPI DECLSPEC_HOTPATCH IsCharDigitW( WCHAR wc )
4983 return !!(get_char_type( CT_CTYPE1, wc ) & C1_DIGIT);
4987 /******************************************************************************
4988 * IsCharLowerA (kernelbase.@)
4990 BOOL WINAPI DECLSPEC_HOTPATCH IsCharLowerA( CHAR c )
4992 WCHAR wc = nls_info.AnsiTableInfo.MultiByteTable[(unsigned char)c];
4993 return !!(get_char_type( CT_CTYPE1, wc ) & C1_LOWER);
4997 /******************************************************************************
4998 * IsCharLowerW (kernelbase.@)
5000 BOOL WINAPI DECLSPEC_HOTPATCH IsCharLowerW( WCHAR wc )
5002 return !!(get_char_type( CT_CTYPE1, wc ) & C1_LOWER);
5006 /******************************************************************************
5007 * IsCharPunctW (kernelbase.@)
5009 BOOL WINAPI DECLSPEC_HOTPATCH IsCharPunctW( WCHAR wc )
5011 return !!(get_char_type( CT_CTYPE1, wc ) & C1_PUNCT);
5015 /******************************************************************************
5016 * IsCharSpaceA (kernelbase.@)
5018 BOOL WINAPI DECLSPEC_HOTPATCH IsCharSpaceA( CHAR c )
5020 WCHAR wc = nls_info.AnsiTableInfo.MultiByteTable[(unsigned char)c];
5021 return !!(get_char_type( CT_CTYPE1, wc ) & C1_SPACE);
5025 /******************************************************************************
5026 * IsCharSpaceW (kernelbase.@)
5028 BOOL WINAPI DECLSPEC_HOTPATCH IsCharSpaceW( WCHAR wc )
5030 return !!(get_char_type( CT_CTYPE1, wc ) & C1_SPACE);
5034 /******************************************************************************
5035 * IsCharUpperA (kernelbase.@)
5037 BOOL WINAPI DECLSPEC_HOTPATCH IsCharUpperA( CHAR c )
5039 WCHAR wc = nls_info.AnsiTableInfo.MultiByteTable[(unsigned char)c];
5040 return !!(get_char_type( CT_CTYPE1, wc ) & C1_UPPER);
5044 /******************************************************************************
5045 * IsCharUpperW (kernelbase.@)
5047 BOOL WINAPI DECLSPEC_HOTPATCH IsCharUpperW( WCHAR wc )
5049 return !!(get_char_type( CT_CTYPE1, wc ) & C1_UPPER);
5053 /******************************************************************************
5054 * IsCharXDigitW (kernelbase.@)
5056 BOOL WINAPI DECLSPEC_HOTPATCH IsCharXDigitW( WCHAR wc )
5058 return !!(get_char_type( CT_CTYPE1, wc ) & C1_XDIGIT);
5062 /******************************************************************************
5063 * IsDBCSLeadByte (kernelbase.@)
5065 BOOL WINAPI DECLSPEC_HOTPATCH IsDBCSLeadByte( BYTE testchar )
5067 return nls_info.AnsiTableInfo.DBCSCodePage && nls_info.AnsiTableInfo.DBCSOffsets[testchar];
5071 /******************************************************************************
5072 * IsDBCSLeadByteEx (kernelbase.@)
5074 BOOL WINAPI DECLSPEC_HOTPATCH IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
5076 const CPTABLEINFO *table = get_codepage_table( codepage );
5077 return table && table->DBCSCodePage && table->DBCSOffsets[testchar];
5081 /******************************************************************************
5082 * IsNormalizedString (kernelbase.@)
5084 BOOL WINAPI DECLSPEC_HOTPATCH IsNormalizedString( NORM_FORM form, const WCHAR *str, INT len )
5086 BOOLEAN res;
5087 if (!set_ntstatus( RtlIsNormalizedString( form, str, len, &res ))) res = FALSE;
5088 return res;
5092 /******************************************************************************
5093 * IsValidCodePage (kernelbase.@)
5095 BOOL WINAPI DECLSPEC_HOTPATCH IsValidCodePage( UINT codepage )
5097 switch (codepage)
5099 case CP_ACP:
5100 case CP_OEMCP:
5101 case CP_MACCP:
5102 case CP_THREAD_ACP:
5103 return FALSE;
5104 case CP_UTF7:
5105 case CP_UTF8:
5106 return TRUE;
5107 default:
5108 return get_codepage_table( codepage ) != NULL;
5113 /******************************************************************************
5114 * IsValidLanguageGroup (kernelbase.@)
5116 BOOL WINAPI DECLSPEC_HOTPATCH IsValidLanguageGroup( LGRPID id, DWORD flags )
5118 WCHAR name[10], value[10];
5119 DWORD type, value_len = sizeof(value);
5120 BOOL ret = FALSE;
5121 HKEY key;
5123 if (RegOpenKeyExW( nls_key, L"Language Groups", 0, KEY_READ, &key )) return FALSE;
5125 swprintf( name, ARRAY_SIZE(name), L"%x", id );
5126 if (!RegQueryValueExW( key, name, NULL, &type, (BYTE *)value, &value_len ) && type == REG_SZ)
5127 ret = (flags & LGRPID_SUPPORTED) || wcstoul( value, NULL, 10 );
5129 RegCloseKey( key );
5130 return ret;
5134 /******************************************************************************
5135 * IsValidLocale (kernelbase.@)
5137 BOOL WINAPI DECLSPEC_HOTPATCH IsValidLocale( LCID lcid, DWORD flags )
5139 /* check if language is registered in the kernel32 resources */
5140 return FindResourceExW( kernel32_handle, (LPWSTR)RT_STRING,
5141 ULongToPtr( (LOCALE_ILANGUAGE >> 4) + 1 ), LANGIDFROMLCID(lcid)) != 0;
5145 /******************************************************************************
5146 * IsValidLocaleName (kernelbase.@)
5148 BOOL WINAPI DECLSPEC_HOTPATCH IsValidLocaleName( const WCHAR *locale )
5150 LCID lcid;
5152 return !RtlLocaleNameToLcid( locale, &lcid, 2 );
5156 /******************************************************************************
5157 * IsValidNLSVersion (kernelbase.@)
5159 DWORD WINAPI DECLSPEC_HOTPATCH IsValidNLSVersion( NLS_FUNCTION func, const WCHAR *locale,
5160 NLSVERSIONINFOEX *info )
5162 static const GUID GUID_NULL;
5163 NLSVERSIONINFOEX infoex;
5164 DWORD ret;
5166 if (func != COMPARE_STRING)
5168 SetLastError( ERROR_INVALID_PARAMETER );
5169 return FALSE;
5171 if (info->dwNLSVersionInfoSize < sizeof(*info) &&
5172 (info->dwNLSVersionInfoSize != offsetof( NLSVERSIONINFO, dwEffectiveId )))
5174 SetLastError( ERROR_INVALID_PARAMETER );
5175 return FALSE;
5177 infoex.dwNLSVersionInfoSize = sizeof(infoex);
5178 if (!GetNLSVersionEx( func, locale, &infoex )) return FALSE;
5180 ret = (infoex.dwNLSVersion & ~0xff) == (info->dwNLSVersion & ~0xff);
5181 if (ret && !IsEqualGUID( &info->guidCustomVersion, &GUID_NULL ))
5182 ret = find_sortguid( &info->guidCustomVersion ) != NULL;
5184 if (!ret) SetLastError( ERROR_SUCCESS );
5185 return ret;
5189 /***********************************************************************
5190 * LCIDToLocaleName (kernelbase.@)
5192 INT WINAPI DECLSPEC_HOTPATCH LCIDToLocaleName( LCID lcid, WCHAR *name, INT count, DWORD flags )
5194 static int once;
5195 if (flags && !once++) FIXME( "unsupported flags %x\n", flags );
5197 return GetLocaleInfoW( lcid, LOCALE_SNAME | LOCALE_NOUSEROVERRIDE, name, count );
5201 /***********************************************************************
5202 * LCMapStringEx (kernelbase.@)
5204 INT WINAPI DECLSPEC_HOTPATCH LCMapStringEx( const WCHAR *locale, DWORD flags, const WCHAR *src, int srclen,
5205 WCHAR *dst, int dstlen, NLSVERSIONINFO *version,
5206 void *reserved, LPARAM handle )
5208 const struct sortguid *sortid = NULL;
5209 LPWSTR dst_ptr;
5210 INT len;
5212 if (version) FIXME( "unsupported version structure %p\n", version );
5213 if (reserved) FIXME( "unsupported reserved pointer %p\n", reserved );
5214 if (handle)
5216 static int once;
5217 if (!once++) FIXME( "unsupported lparam %lx\n", handle );
5220 if (!src || !srclen || dstlen < 0)
5222 SetLastError( ERROR_INVALID_PARAMETER );
5223 return 0;
5226 /* mutually exclusive flags */
5227 if ((flags & (LCMAP_LOWERCASE | LCMAP_UPPERCASE)) == (LCMAP_LOWERCASE | LCMAP_UPPERCASE) ||
5228 (flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA)) == (LCMAP_HIRAGANA | LCMAP_KATAKANA) ||
5229 (flags & (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) == (LCMAP_HALFWIDTH | LCMAP_FULLWIDTH) ||
5230 (flags & (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE)) == (LCMAP_TRADITIONAL_CHINESE | LCMAP_SIMPLIFIED_CHINESE) ||
5231 !flags)
5233 SetLastError( ERROR_INVALID_FLAGS );
5234 return 0;
5237 if (!dstlen) dst = NULL;
5239 if (flags & LCMAP_LINGUISTIC_CASING && !(sortid = get_language_sort( locale )))
5241 FIXME( "unknown locale %s\n", debugstr_w(locale) );
5242 SetLastError( ERROR_INVALID_PARAMETER );
5243 return 0;
5246 if (flags & LCMAP_SORTKEY)
5248 INT ret;
5250 if (src == dst)
5252 SetLastError( ERROR_INVALID_FLAGS );
5253 return 0;
5255 if (srclen < 0) srclen = lstrlenW(src);
5257 TRACE( "(%s,0x%08x,%s,%d,%p,%d)\n",
5258 debugstr_w(locale), flags, debugstr_wn(src, srclen), srclen, dst, dstlen );
5260 if ((ret = get_sortkey( flags, src, srclen, (char *)dst, dstlen ))) ret++;
5261 else SetLastError( ERROR_INSUFFICIENT_BUFFER );
5262 return ret;
5265 /* SORT_STRINGSORT must be used exclusively with LCMAP_SORTKEY */
5266 if (flags & SORT_STRINGSORT)
5268 SetLastError( ERROR_INVALID_FLAGS );
5269 return 0;
5271 if (((flags & (NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS)) &&
5272 (flags & ~(NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS))) ||
5273 ((flags & (LCMAP_HIRAGANA | LCMAP_KATAKANA | LCMAP_HALFWIDTH | LCMAP_FULLWIDTH)) &&
5274 (flags & (LCMAP_SIMPLIFIED_CHINESE | LCMAP_TRADITIONAL_CHINESE))))
5276 SetLastError( ERROR_INVALID_FLAGS );
5277 return 0;
5280 if (srclen < 0) srclen = lstrlenW(src) + 1;
5282 TRACE( "(%s,0x%08x,%s,%d,%p,%d)\n",
5283 debugstr_w(locale), flags, debugstr_wn(src, srclen), srclen, dst, dstlen );
5285 if (!dst) /* return required string length */
5287 if (flags & NORM_IGNORESYMBOLS)
5289 for (len = 0; srclen; src++, srclen--)
5290 if (!(get_char_type( CT_CTYPE1, *src ) & (C1_PUNCT | C1_SPACE))) len++;
5292 else if (flags & LCMAP_FULLWIDTH)
5294 for (len = 0; srclen; src++, srclen--, len++)
5296 if (compose_katakana( src, srclen, NULL ) == 2)
5298 src++;
5299 srclen--;
5303 else if (flags & LCMAP_HALFWIDTH)
5305 for (len = 0; srclen; src++, srclen--, len++)
5307 WCHAR wch = *src;
5308 /* map Hiragana to Katakana before decomposition if needed */
5309 if ((flags & LCMAP_KATAKANA) &&
5310 ((wch >= 0x3041 && wch <= 0x3096) || wch == 0x309D || wch == 0x309E))
5311 wch += 0x60;
5313 if (decompose_katakana( wch, NULL, 0 ) == 2) len++;
5316 else len = srclen;
5317 return len;
5320 if (src == dst && (flags & ~(LCMAP_LOWERCASE | LCMAP_UPPERCASE)))
5322 SetLastError( ERROR_INVALID_FLAGS );
5323 return 0;
5326 if (flags & (NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS))
5328 for (len = dstlen, dst_ptr = dst; srclen && len; src++, srclen--)
5330 if ((flags & NORM_IGNORESYMBOLS) && (get_char_type( CT_CTYPE1, *src ) & (C1_PUNCT | C1_SPACE)))
5331 continue;
5332 *dst_ptr++ = *src;
5333 len--;
5335 goto done;
5338 if (flags & (LCMAP_FULLWIDTH | LCMAP_HALFWIDTH | LCMAP_HIRAGANA | LCMAP_KATAKANA))
5340 for (len = dstlen, dst_ptr = dst; len && srclen; src++, srclen--, len--, dst_ptr++)
5342 WCHAR wch;
5343 if (flags & LCMAP_FULLWIDTH)
5345 /* map half-width character to full-width one,
5346 e.g. U+FF71 -> U+30A2, U+FF8C U+FF9F -> U+30D7. */
5347 if (map_to_fullwidth( src, srclen, &wch ) == 2)
5349 src++;
5350 srclen--;
5353 else wch = *src;
5355 if (flags & LCMAP_KATAKANA)
5357 /* map hiragana to katakana, e.g. U+3041 -> U+30A1.
5358 we can't use C3_HIRAGANA as some characters can't map to katakana */
5359 if ((wch >= 0x3041 && wch <= 0x3096) || wch == 0x309D || wch == 0x309E) wch += 0x60;
5361 else if (flags & LCMAP_HIRAGANA)
5363 /* map katakana to hiragana, e.g. U+30A1 -> U+3041.
5364 we can't use C3_KATAKANA as some characters can't map to hiragana */
5365 if ((wch >= 0x30A1 && wch <= 0x30F6) || wch == 0x30FD || wch == 0x30FE) wch -= 0x60;
5368 if (flags & LCMAP_HALFWIDTH)
5370 /* map full-width character to half-width one,
5371 e.g. U+30A2 -> U+FF71, U+30D7 -> U+FF8C U+FF9F. */
5372 if (map_to_halfwidth(wch, dst_ptr, len) == 2)
5374 len--;
5375 dst_ptr++;
5376 if (!len) break;
5379 else *dst_ptr = wch;
5381 if (!(flags & (LCMAP_UPPERCASE | LCMAP_LOWERCASE)) || srclen) goto done;
5383 srclen = dst_ptr - dst;
5384 src = dst;
5387 if (flags & (LCMAP_UPPERCASE | LCMAP_LOWERCASE))
5389 const USHORT *table = sort.casemap + (flags & LCMAP_LINGUISTIC_CASING ? sortid->casemap : 0);
5390 table = table + 2 + (flags & LCMAP_LOWERCASE ? table[1] : 0);
5391 for (len = dstlen, dst_ptr = dst; srclen && len; src++, srclen--, len--)
5392 *dst_ptr++ = casemap( table, *src );
5394 else
5396 len = min( srclen, dstlen );
5397 memcpy( dst, src, len * sizeof(WCHAR) );
5398 dst_ptr = dst + len;
5399 srclen -= len;
5402 done:
5403 if (srclen)
5405 SetLastError( ERROR_INSUFFICIENT_BUFFER );
5406 return 0;
5409 return dst_ptr - dst;
5413 /***********************************************************************
5414 * LCMapStringA (kernelbase.@)
5416 INT WINAPI DECLSPEC_HOTPATCH LCMapStringA( LCID lcid, DWORD flags, const char *src, int srclen,
5417 char *dst, int dstlen )
5419 WCHAR *bufW = NtCurrentTeb()->StaticUnicodeBuffer;
5420 LPWSTR srcW, dstW;
5421 INT ret = 0, srclenW, dstlenW;
5422 UINT locale_cp = CP_ACP;
5424 if (!src || !srclen || dstlen < 0)
5426 SetLastError( ERROR_INVALID_PARAMETER );
5427 return 0;
5430 locale_cp = get_lcid_codepage( lcid, flags );
5432 srclenW = MultiByteToWideChar( locale_cp, 0, src, srclen, bufW, 260 );
5433 if (srclenW) srcW = bufW;
5434 else
5436 srclenW = MultiByteToWideChar( locale_cp, 0, src, srclen, NULL, 0 );
5437 srcW = HeapAlloc( GetProcessHeap(), 0, srclenW * sizeof(WCHAR) );
5438 if (!srcW)
5440 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
5441 return 0;
5443 MultiByteToWideChar( locale_cp, 0, src, srclen, srcW, srclenW );
5446 if (flags & LCMAP_SORTKEY)
5448 if (src == dst)
5450 SetLastError( ERROR_INVALID_FLAGS );
5451 goto done;
5453 ret = LCMapStringEx( NULL, flags, srcW, srclenW, (WCHAR *)dst, dstlen, NULL, NULL, 0 );
5454 goto done;
5457 if (flags & SORT_STRINGSORT)
5459 SetLastError( ERROR_INVALID_FLAGS );
5460 goto done;
5463 dstlenW = LCMapStringEx( NULL, flags, srcW, srclenW, NULL, 0, NULL, NULL, 0 );
5464 if (!dstlenW) goto done;
5466 dstW = HeapAlloc( GetProcessHeap(), 0, dstlenW * sizeof(WCHAR) );
5467 if (!dstW)
5469 SetLastError( ERROR_NOT_ENOUGH_MEMORY );
5470 goto done;
5472 LCMapStringEx( NULL, flags, srcW, srclenW, dstW, dstlenW, NULL, NULL, 0 );
5473 ret = WideCharToMultiByte( locale_cp, 0, dstW, dstlenW, dst, dstlen, NULL, NULL );
5474 HeapFree( GetProcessHeap(), 0, dstW );
5476 done:
5477 if (srcW != bufW) HeapFree( GetProcessHeap(), 0, srcW );
5478 return ret;
5482 /***********************************************************************
5483 * LCMapStringW (kernelbase.@)
5485 INT WINAPI DECLSPEC_HOTPATCH LCMapStringW( LCID lcid, DWORD flags, const WCHAR *src, int srclen,
5486 WCHAR *dst, int dstlen )
5488 return LCMapStringEx( NULL, flags, src, srclen, dst, dstlen, NULL, NULL, 0 );
5492 /***********************************************************************
5493 * LocaleNameToLCID (kernelbase.@)
5495 LCID WINAPI DECLSPEC_HOTPATCH LocaleNameToLCID( const WCHAR *name, DWORD flags )
5497 LCID lcid;
5499 if (!name) return GetUserDefaultLCID();
5500 if (!set_ntstatus( RtlLocaleNameToLcid( name, &lcid, 2 ))) return 0;
5501 if (!(flags & LOCALE_ALLOW_NEUTRAL_NAMES)) lcid = ConvertDefaultLocale( lcid );
5502 return lcid;
5506 /******************************************************************************
5507 * MultiByteToWideChar (kernelbase.@)
5509 INT WINAPI DECLSPEC_HOTPATCH MultiByteToWideChar( UINT codepage, DWORD flags, const char *src, INT srclen,
5510 WCHAR *dst, INT dstlen )
5512 int ret;
5514 if (!src || !srclen || (!dst && dstlen) || dstlen < 0)
5516 SetLastError( ERROR_INVALID_PARAMETER );
5517 return 0;
5519 if (srclen < 0) srclen = strlen(src) + 1;
5521 switch (codepage)
5523 case CP_SYMBOL:
5524 ret = mbstowcs_cpsymbol( flags, src, srclen, dst, dstlen );
5525 break;
5526 case CP_UTF7:
5527 ret = mbstowcs_utf7( flags, src, srclen, dst, dstlen );
5528 break;
5529 case CP_UTF8:
5530 ret = mbstowcs_utf8( flags, src, srclen, dst, dstlen );
5531 break;
5532 case CP_UNIXCP:
5533 codepage = __wine_get_unix_codepage();
5534 if (codepage == CP_UTF8)
5536 ret = mbstowcs_utf8( flags, src, srclen, dst, dstlen );
5537 #ifdef __APPLE__ /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
5538 if (ret && dstlen) RtlNormalizeString( NormalizationC, dst, ret, dst, &ret );
5539 #endif
5540 break;
5542 /* fall through */
5543 default:
5544 ret = mbstowcs_codepage( codepage, flags, src, srclen, dst, dstlen );
5545 break;
5547 TRACE( "cp %d %s -> %s, ret = %d\n", codepage, debugstr_an(src, srclen), debugstr_wn(dst, ret), ret );
5548 return ret;
5552 /******************************************************************************
5553 * NormalizeString (kernelbase.@)
5555 INT WINAPI DECLSPEC_HOTPATCH NormalizeString(NORM_FORM form, const WCHAR *src, INT src_len,
5556 WCHAR *dst, INT dst_len)
5558 NTSTATUS status = RtlNormalizeString( form, src, src_len, dst, &dst_len );
5560 switch (status)
5562 case STATUS_OBJECT_NAME_NOT_FOUND:
5563 status = STATUS_INVALID_PARAMETER;
5564 break;
5565 case STATUS_BUFFER_TOO_SMALL:
5566 case STATUS_NO_UNICODE_TRANSLATION:
5567 dst_len = -dst_len;
5568 break;
5570 SetLastError( RtlNtStatusToDosError( status ));
5571 return dst_len;
5575 /******************************************************************************
5576 * ResolveLocaleName (kernelbase.@)
5578 INT WINAPI DECLSPEC_HOTPATCH ResolveLocaleName( LPCWSTR name, LPWSTR buffer, INT len )
5580 FIXME( "stub: %s, %p, %d\n", wine_dbgstr_w(name), buffer, len );
5582 SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
5583 return 0;
5587 /******************************************************************************
5588 * SetLocaleInfoW (kernelbase.@)
5590 BOOL WINAPI DECLSPEC_HOTPATCH SetLocaleInfoW( LCID lcid, LCTYPE lctype, const WCHAR *data )
5592 const struct registry_value *value;
5593 DWORD index;
5594 LSTATUS status;
5596 lctype = LOWORD(lctype);
5597 value = get_locale_registry_value( lctype );
5599 if (!data || !value)
5601 SetLastError( ERROR_INVALID_PARAMETER );
5602 return FALSE;
5605 if (lctype == LOCALE_IDATE || lctype == LOCALE_ILDATE)
5607 SetLastError( ERROR_INVALID_FLAGS );
5608 return FALSE;
5611 TRACE( "setting %x (%s) to %s\n", lctype, debugstr_w(value->name), debugstr_w(data) );
5613 /* FIXME: should check that data to set is sane */
5615 status = RegSetValueExW( intl_key, value->name, 0, REG_SZ, (BYTE *)data, (lstrlenW(data)+1)*sizeof(WCHAR) );
5616 index = value - registry_values;
5618 RtlEnterCriticalSection( &locale_section );
5619 HeapFree( GetProcessHeap(), 0, registry_cache[index] );
5620 registry_cache[index] = NULL;
5621 RtlLeaveCriticalSection( &locale_section );
5623 if (lctype == LOCALE_SSHORTDATE || lctype == LOCALE_SLONGDATE)
5625 /* Set I-value from S value */
5626 WCHAR *pD, *pM, *pY, buf[2];
5628 pD = wcschr( data, 'd' );
5629 pM = wcschr( data, 'M' );
5630 pY = wcschr( data, 'y' );
5632 if (pD <= pM) buf[0] = '1'; /* D-M-Y */
5633 else if (pY <= pM) buf[0] = '2'; /* Y-M-D */
5634 else buf[0] = '0'; /* M-D-Y */
5635 buf[1] = 0;
5637 lctype = (lctype == LOCALE_SSHORTDATE) ? LOCALE_IDATE : LOCALE_ILDATE;
5638 value = get_locale_registry_value( lctype );
5639 index = value - registry_values;
5641 RegSetValueExW( intl_key, value->name, 0, REG_SZ, (BYTE *)buf, sizeof(buf) );
5643 RtlEnterCriticalSection( &locale_section );
5644 HeapFree( GetProcessHeap(), 0, registry_cache[index] );
5645 registry_cache[index] = NULL;
5646 RtlLeaveCriticalSection( &locale_section );
5648 return set_ntstatus( status );
5652 /***********************************************************************
5653 * SetCalendarInfoW (kernelbase.@)
5655 INT WINAPI /* DECLSPEC_HOTPATCH */ SetCalendarInfoW( LCID lcid, CALID calendar, CALTYPE type, const WCHAR *data )
5657 FIXME( "(%08x,%08x,%08x,%s): stub\n", lcid, calendar, type, debugstr_w(data) );
5658 return 0;
5662 /***********************************************************************
5663 * SetProcessPreferredUILanguages (kernelbase.@)
5665 BOOL WINAPI DECLSPEC_HOTPATCH SetProcessPreferredUILanguages( DWORD flags, PCZZWSTR buffer, ULONG *count )
5667 return set_ntstatus( RtlSetProcessPreferredUILanguages( flags, buffer, count ));
5671 /***********************************************************************
5672 * SetThreadPreferredUILanguages (kernelbase.@)
5674 BOOL WINAPI DECLSPEC_HOTPATCH SetThreadPreferredUILanguages( DWORD flags, PCZZWSTR buffer, ULONG *count )
5676 return set_ntstatus( RtlSetThreadPreferredUILanguages( flags, buffer, count ));
5680 /***********************************************************************
5681 * SetTimeZoneInformation (kernelbase.@)
5683 BOOL WINAPI DECLSPEC_HOTPATCH SetTimeZoneInformation( const TIME_ZONE_INFORMATION *info )
5685 return set_ntstatus( RtlSetTimeZoneInformation( (const RTL_TIME_ZONE_INFORMATION *)info ));
5689 /******************************************************************************
5690 * SetUserGeoID (kernelbase.@)
5692 BOOL WINAPI DECLSPEC_HOTPATCH SetUserGeoID( GEOID id )
5694 const struct geoinfo *geoinfo = get_geoinfo_ptr( id );
5695 WCHAR bufferW[10];
5696 HKEY hkey;
5698 if (!geoinfo)
5700 SetLastError( ERROR_INVALID_PARAMETER );
5701 return FALSE;
5703 if (!RegCreateKeyExW( intl_key, L"Geo", 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
5705 const WCHAR *name = geoinfo->kind == LOCATION_NATION ? L"Nation" : L"Region";
5706 swprintf( bufferW, ARRAY_SIZE(bufferW), L"%u", geoinfo->id );
5707 RegSetValueExW( hkey, name, 0, REG_SZ, (BYTE *)bufferW, (lstrlenW(bufferW) + 1) * sizeof(WCHAR) );
5708 RegCloseKey( hkey );
5710 return TRUE;
5714 /***********************************************************************
5715 * SystemTimeToTzSpecificLocalTime (kernelbase.@)
5717 BOOL WINAPI DECLSPEC_HOTPATCH SystemTimeToTzSpecificLocalTime( const TIME_ZONE_INFORMATION *info,
5718 const SYSTEMTIME *system,
5719 SYSTEMTIME *local )
5721 TIME_ZONE_INFORMATION tzinfo;
5722 LARGE_INTEGER ft;
5724 if (!info)
5726 RtlQueryTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION *)&tzinfo );
5727 info = &tzinfo;
5730 if (!SystemTimeToFileTime( system, (FILETIME *)&ft )) return FALSE;
5731 switch (get_timezone_id( info, ft, FALSE ))
5733 case TIME_ZONE_ID_UNKNOWN:
5734 ft.QuadPart -= info->Bias * (LONGLONG)600000000;
5735 break;
5736 case TIME_ZONE_ID_STANDARD:
5737 ft.QuadPart -= (info->Bias + info->StandardBias) * (LONGLONG)600000000;
5738 break;
5739 case TIME_ZONE_ID_DAYLIGHT:
5740 ft.QuadPart -= (info->Bias + info->DaylightBias) * (LONGLONG)600000000;
5741 break;
5742 default:
5743 return FALSE;
5745 return FileTimeToSystemTime( (FILETIME *)&ft, local );
5749 /***********************************************************************
5750 * TzSpecificLocalTimeToSystemTime (kernelbase.@)
5752 BOOL WINAPI DECLSPEC_HOTPATCH TzSpecificLocalTimeToSystemTime( const TIME_ZONE_INFORMATION *info,
5753 const SYSTEMTIME *local,
5754 SYSTEMTIME *system )
5756 TIME_ZONE_INFORMATION tzinfo;
5757 LARGE_INTEGER ft;
5759 if (!info)
5761 RtlQueryTimeZoneInformation( (RTL_TIME_ZONE_INFORMATION *)&tzinfo );
5762 info = &tzinfo;
5765 if (!SystemTimeToFileTime( local, (FILETIME *)&ft )) return FALSE;
5766 switch (get_timezone_id( info, ft, TRUE ))
5768 case TIME_ZONE_ID_UNKNOWN:
5769 ft.QuadPart += info->Bias * (LONGLONG)600000000;
5770 break;
5771 case TIME_ZONE_ID_STANDARD:
5772 ft.QuadPart += (info->Bias + info->StandardBias) * (LONGLONG)600000000;
5773 break;
5774 case TIME_ZONE_ID_DAYLIGHT:
5775 ft.QuadPart += (info->Bias + info->DaylightBias) * (LONGLONG)600000000;
5776 break;
5777 default:
5778 return FALSE;
5780 return FileTimeToSystemTime( (FILETIME *)&ft, system );
5784 /***********************************************************************
5785 * VerLanguageNameA (kernelbase.@)
5787 DWORD WINAPI DECLSPEC_HOTPATCH VerLanguageNameA( DWORD lang, LPSTR buffer, DWORD size )
5789 return GetLocaleInfoA( MAKELCID( lang, SORT_DEFAULT ), LOCALE_SENGLANGUAGE, buffer, size );
5793 /***********************************************************************
5794 * VerLanguageNameW (kernelbase.@)
5796 DWORD WINAPI DECLSPEC_HOTPATCH VerLanguageNameW( DWORD lang, LPWSTR buffer, DWORD size )
5798 return GetLocaleInfoW( MAKELCID( lang, SORT_DEFAULT ), LOCALE_SENGLANGUAGE, buffer, size );
5802 /***********************************************************************
5803 * WideCharToMultiByte (kernelbase.@)
5805 INT WINAPI DECLSPEC_HOTPATCH WideCharToMultiByte( UINT codepage, DWORD flags, LPCWSTR src, INT srclen,
5806 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
5808 int ret;
5810 if (!src || !srclen || (!dst && dstlen) || dstlen < 0)
5812 SetLastError( ERROR_INVALID_PARAMETER );
5813 return 0;
5816 if (srclen < 0) srclen = lstrlenW(src) + 1;
5818 switch (codepage)
5820 case CP_SYMBOL:
5821 ret = wcstombs_cpsymbol( flags, src, srclen, dst, dstlen, defchar, used );
5822 break;
5823 case CP_UTF7:
5824 ret = wcstombs_utf7( flags, src, srclen, dst, dstlen, defchar, used );
5825 break;
5826 case CP_UTF8:
5827 ret = wcstombs_utf8( flags, src, srclen, dst, dstlen, defchar, used );
5828 break;
5829 case CP_UNIXCP:
5830 codepage = __wine_get_unix_codepage();
5831 if (codepage == CP_UTF8)
5833 if (used) *used = FALSE;
5834 ret = wcstombs_utf8( flags, src, srclen, dst, dstlen, NULL, NULL );
5835 break;
5837 /* fall through */
5838 default:
5839 ret = wcstombs_codepage( codepage, flags, src, srclen, dst, dstlen, defchar, used );
5840 break;
5842 TRACE( "cp %d %s -> %s, ret = %d\n", codepage, debugstr_wn(src, srclen), debugstr_an(dst, ret), ret );
5843 return ret;