1 | /* $Id: codepage.cpp,v 1.17 2001-09-05 12:57:58 bird Exp $
|
---|
2 | *
|
---|
3 | * Code page functions
|
---|
4 | *
|
---|
5 | * Based on Wine code (memory\codepage.c)
|
---|
6 | *
|
---|
7 | * Copyright 2000 Alexandre Julliard
|
---|
8 | *
|
---|
9 | * Project Odin Software License can be found in LICENSE.TXT
|
---|
10 | *
|
---|
11 | */
|
---|
12 |
|
---|
13 | #include <assert.h>
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <stdlib.h>
|
---|
16 | #include <string.h>
|
---|
17 |
|
---|
18 | #include "winbase.h"
|
---|
19 | #include "winerror.h"
|
---|
20 | #include "winnls.h"
|
---|
21 | #include "wine/unicode.h"
|
---|
22 | #include "debugtools.h"
|
---|
23 |
|
---|
24 | #ifdef __WIN32OS2__
|
---|
25 | #include <options.h>
|
---|
26 | #include "codepage.h"
|
---|
27 |
|
---|
28 | #define DBG_LOCALLOG DBG_codepage
|
---|
29 | #include "dbglocal.h"
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | DEFAULT_DEBUG_CHANNEL(string);
|
---|
33 |
|
---|
34 | /* current code pages */
|
---|
35 | static const union cptable *ansi_cptable;
|
---|
36 | static const union cptable *oem_cptable;
|
---|
37 | static const union cptable *mac_cptable;
|
---|
38 |
|
---|
39 | /* retrieve a code page table from the locale info */
|
---|
40 | static const union cptable *get_locale_cp( LCID lcid, LCTYPE type )
|
---|
41 | {
|
---|
42 | const union cptable *table = NULL;
|
---|
43 | char buf[32];
|
---|
44 |
|
---|
45 | if (GetLocaleInfoA( lcid, type, buf, sizeof(buf) )) table = cp_get_table( atoi(buf) );
|
---|
46 | return table;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /* setup default codepage info before we can get at the locale stuff */
|
---|
50 | static void init_codepages(void)
|
---|
51 | {
|
---|
52 | ansi_cptable = cp_get_table( 1252 );
|
---|
53 | oem_cptable = cp_get_table( 437 );
|
---|
54 | mac_cptable = cp_get_table( 10000 );
|
---|
55 | assert( ansi_cptable );
|
---|
56 | assert( oem_cptable );
|
---|
57 | assert( mac_cptable );
|
---|
58 | }
|
---|
59 |
|
---|
60 | /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
|
---|
61 | static const union cptable *get_codepage_table( unsigned int codepage )
|
---|
62 | {
|
---|
63 | const union cptable *ret = NULL;
|
---|
64 |
|
---|
65 | if (!ansi_cptable) init_codepages();
|
---|
66 |
|
---|
67 | switch(codepage)
|
---|
68 | {
|
---|
69 | case CP_ACP: return ansi_cptable;
|
---|
70 | case CP_OEMCP: return oem_cptable;
|
---|
71 | case CP_MACCP: return mac_cptable;
|
---|
72 | case CP_THREAD_ACP: return get_locale_cp( GetThreadLocale(), LOCALE_IDEFAULTANSICODEPAGE );
|
---|
73 | case CP_UTF7:
|
---|
74 | case CP_UTF8:
|
---|
75 | break;
|
---|
76 | default:
|
---|
77 | if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
|
---|
78 | if (codepage == oem_cptable->info.codepage) return oem_cptable;
|
---|
79 | if (codepage == mac_cptable->info.codepage) return mac_cptable;
|
---|
80 | ret = cp_get_table( codepage );
|
---|
81 | break;
|
---|
82 | }
|
---|
83 | return ret;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* initialize default code pages from locale info */
|
---|
87 | /* FIXME: should be done in init_codepages, but it can't right now */
|
---|
88 | /* since it needs KERNEL32 to be loaded for the locale info. */
|
---|
89 | void CODEPAGE_Init(void)
|
---|
90 | {
|
---|
91 | const union cptable *table;
|
---|
92 | LCID lcid = GetUserDefaultLCID();
|
---|
93 |
|
---|
94 | if (!ansi_cptable) init_codepages(); /* just in case */
|
---|
95 |
|
---|
96 | if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTANSICODEPAGE ))) ansi_cptable = table;
|
---|
97 | if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTMACCODEPAGE ))) mac_cptable = table;
|
---|
98 | if ((table = get_locale_cp( lcid, LOCALE_IDEFAULTCODEPAGE ))) oem_cptable = table;
|
---|
99 |
|
---|
100 | TRACE( "ansi=%03d oem=%03d mac=%03d\n", ansi_cptable->info.codepage,
|
---|
101 | oem_cptable->info.codepage, mac_cptable->info.codepage );
|
---|
102 | #ifdef __WIN32OS2__
|
---|
103 | dprintf(("Language %s", getenv("LANG")));
|
---|
104 | #endif
|
---|
105 | }
|
---|
106 |
|
---|
107 | #ifdef __WIN32OS2__
|
---|
108 | ULONG GetDisplayCodepage()
|
---|
109 | {
|
---|
110 | if (!ansi_cptable) CODEPAGE_Init();
|
---|
111 |
|
---|
112 | return ansi_cptable->info.codepage;
|
---|
113 | }
|
---|
114 |
|
---|
115 | ULONG GetWindowsCodepage()
|
---|
116 | {
|
---|
117 | if (!ansi_cptable) CODEPAGE_Init();
|
---|
118 |
|
---|
119 | return ansi_cptable->info.codepage;
|
---|
120 | }
|
---|
121 |
|
---|
122 | #endif
|
---|
123 |
|
---|
124 | /******************************************************************************
|
---|
125 | * GetACP (KERNEL32)
|
---|
126 | *
|
---|
127 | * RETURNS
|
---|
128 | * Current ANSI code-page identifier, default if no current defined
|
---|
129 | */
|
---|
130 | UINT WINAPI GetACP(void)
|
---|
131 | {
|
---|
132 | if (!ansi_cptable) init_codepages();
|
---|
133 | #ifdef __WIN32OS2__
|
---|
134 | dprintf(("GetACP %d", ansi_cptable->info.codepage));
|
---|
135 | #endif
|
---|
136 | return ansi_cptable->info.codepage;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | /***********************************************************************
|
---|
141 | * GetOEMCP (KERNEL32)
|
---|
142 | */
|
---|
143 | UINT WINAPI GetOEMCP(void)
|
---|
144 | {
|
---|
145 | if (!oem_cptable) init_codepages();
|
---|
146 | #ifdef __WIN32OS2__
|
---|
147 | dprintf(("GetOEMCP %d", oem_cptable->info.codepage));
|
---|
148 | #endif
|
---|
149 | return oem_cptable->info.codepage;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /***********************************************************************
|
---|
154 | * IsValidCodePage (KERNEL32)
|
---|
155 | */
|
---|
156 | BOOL WINAPI IsValidCodePage( UINT codepage )
|
---|
157 | {
|
---|
158 | #ifdef __WIN32OS2__
|
---|
159 | dprintf(("IsValidCodePage %d", codepage));
|
---|
160 | #endif
|
---|
161 | return cp_get_table( codepage ) != NULL;
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /***********************************************************************
|
---|
166 | * IsDBCSLeadByteEx (KERNEL32)
|
---|
167 | */
|
---|
168 | BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
|
---|
169 | {
|
---|
170 | #ifdef __WIN32OS2__
|
---|
171 | dprintf2(("IsDBCSLeadByteEx %d %x", codepage, testchar));
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | const union cptable *table = get_codepage_table( codepage );
|
---|
175 | return table && is_dbcs_leadbyte( table, testchar );
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | /***********************************************************************
|
---|
180 | * IsDBCSLeadByte (KERNEL32)
|
---|
181 | */
|
---|
182 | BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
|
---|
183 | {
|
---|
184 | if (!ansi_cptable) init_codepages();
|
---|
185 | return is_dbcs_leadbyte( ansi_cptable, testchar );
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /***********************************************************************
|
---|
190 | * GetCPInfo (KERNEL32)
|
---|
191 | */
|
---|
192 | BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
|
---|
193 | {
|
---|
194 | const union cptable *table = get_codepage_table( codepage );
|
---|
195 |
|
---|
196 | #ifdef __WIN32OS2__
|
---|
197 | dprintf(("GetCPInfo %d %x", codepage, cpinfo));
|
---|
198 | #endif
|
---|
199 |
|
---|
200 | if (!table)
|
---|
201 | {
|
---|
202 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
203 | return FALSE;
|
---|
204 | }
|
---|
205 | if (table->info.def_char & 0xff00)
|
---|
206 | {
|
---|
207 | cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
|
---|
208 | cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
|
---|
213 | cpinfo->DefaultChar[1] = 0;
|
---|
214 | }
|
---|
215 | if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
|
---|
216 | memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
|
---|
217 | else
|
---|
218 | cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
|
---|
219 |
|
---|
220 | return TRUE;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /***********************************************************************
|
---|
225 | * EnumSystemCodePagesA (KERNEL32)
|
---|
226 | */
|
---|
227 | BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
|
---|
228 | {
|
---|
229 | const union cptable *table;
|
---|
230 | char buffer[10];
|
---|
231 | int index = 0;
|
---|
232 |
|
---|
233 | #ifdef __WIN32OS2__
|
---|
234 | dprintf(("EnumSystemCodePagesA %x %x", lpfnCodePageEnum, flags));
|
---|
235 | #endif
|
---|
236 |
|
---|
237 | for (;;)
|
---|
238 | {
|
---|
239 | if (!(table = cp_enum_table( index++ ))) break;
|
---|
240 | sprintf( buffer, "%d", table->info.codepage );
|
---|
241 | if (!lpfnCodePageEnum( buffer )) break;
|
---|
242 | }
|
---|
243 | return TRUE;
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | /***********************************************************************
|
---|
248 | * EnumSystemCodePagesW (KERNEL32)
|
---|
249 | */
|
---|
250 | BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
|
---|
251 | {
|
---|
252 | const union cptable *table;
|
---|
253 | WCHAR buffer[10], *p;
|
---|
254 | int page, index = 0;
|
---|
255 |
|
---|
256 | #ifdef __WIN32OS2__
|
---|
257 | dprintf(("EnumSystemCodePagesW %x %x", lpfnCodePageEnum, flags));
|
---|
258 | #endif
|
---|
259 |
|
---|
260 | for (;;)
|
---|
261 | {
|
---|
262 | if (!(table = cp_enum_table( index++ ))) break;
|
---|
263 | p = buffer + sizeof(buffer)/sizeof(WCHAR);
|
---|
264 | *--p = 0;
|
---|
265 | page = table->info.codepage;
|
---|
266 | do
|
---|
267 | {
|
---|
268 | *--p = '0' + (page % 10);
|
---|
269 | page /= 10;
|
---|
270 | } while( page );
|
---|
271 | if (!lpfnCodePageEnum( p )) break;
|
---|
272 | }
|
---|
273 | return TRUE;
|
---|
274 | }
|
---|
275 |
|
---|
276 |
|
---|
277 | /***********************************************************************
|
---|
278 | * MultiByteToWideChar (KERNEL32)
|
---|
279 | *
|
---|
280 | * PARAMS
|
---|
281 | * page [in] Codepage character set to convert from
|
---|
282 | * flags [in] Character mapping flags
|
---|
283 | * src [in] Source string buffer
|
---|
284 | * srclen [in] Length of source string buffer
|
---|
285 | * dst [in] Destination buffer
|
---|
286 | * dstlen [in] Length of destination buffer
|
---|
287 | *
|
---|
288 | * NOTES
|
---|
289 | * The returned length includes the null terminator character.
|
---|
290 | *
|
---|
291 | * RETURNS
|
---|
292 | * Success: If dstlen > 0, number of characters written to destination
|
---|
293 | * buffer. If dstlen == 0, number of characters needed to do
|
---|
294 | * conversion.
|
---|
295 | * Failure: 0. Occurs if not enough space is available.
|
---|
296 | *
|
---|
297 | * ERRORS
|
---|
298 | * ERROR_INSUFFICIENT_BUFFER
|
---|
299 | * ERROR_INVALID_PARAMETER
|
---|
300 | * ERROR_NO_UNICODE_TRANSLATION
|
---|
301 | *
|
---|
302 | */
|
---|
303 | INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
|
---|
304 | LPWSTR dst, INT dstlen )
|
---|
305 | {
|
---|
306 | const union cptable *table;
|
---|
307 | int ret;
|
---|
308 |
|
---|
309 | #ifdef __WIN32OS2__
|
---|
310 | dprintf2(("MultiByteToWideChar %d %x %x %d %x %d", page, flags, src, srclen, dst, dstlen));
|
---|
311 | #endif
|
---|
312 |
|
---|
313 | //Docs say source ptr can't be the same as destination (Windows ME, NT4-SP6)
|
---|
314 | if (!src || (!dst && dstlen) || ((void *)src == (void *)dst))
|
---|
315 | {
|
---|
316 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
317 | return 0;
|
---|
318 | }
|
---|
319 |
|
---|
320 | //Even though the docs claim this only works for -1, testing shows it
|
---|
321 | //is done for any negative value (Windows ME, NT4-SP6)
|
---|
322 | if (srclen <= -1) srclen = strlen(src) + 1;
|
---|
323 |
|
---|
324 | if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
|
---|
325 |
|
---|
326 | switch(page)
|
---|
327 | {
|
---|
328 | case CP_UTF7:
|
---|
329 | FIXME("UTF not supported\n");
|
---|
330 | SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
|
---|
331 | return 0;
|
---|
332 | case CP_UTF8:
|
---|
333 | ret = utf8_mbstowcs( flags, src, srclen, dst, dstlen );
|
---|
334 | break;
|
---|
335 | default:
|
---|
336 | if (!(table = get_codepage_table( page )))
|
---|
337 | {
|
---|
338 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
339 | return 0;
|
---|
340 | }
|
---|
341 | ret = cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
|
---|
342 | break;
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (ret < 0)
|
---|
346 | {
|
---|
347 | switch(ret)
|
---|
348 | {
|
---|
349 | case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
|
---|
350 | case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
|
---|
351 | }
|
---|
352 | ret = 0;
|
---|
353 | }
|
---|
354 | return ret;
|
---|
355 | }
|
---|
356 |
|
---|
357 |
|
---|
358 | /***********************************************************************
|
---|
359 | * WideCharToMultiByte (KERNEL32)
|
---|
360 | *
|
---|
361 | * PARAMS
|
---|
362 | * page [in] Codepage character set to convert to
|
---|
363 | * flags [in] Character mapping flags
|
---|
364 | * src [in] Source string buffer
|
---|
365 | * srclen [in] Length of source string buffer
|
---|
366 | * dst [in] Destination buffer
|
---|
367 | * dstlen [in] Length of destination buffer
|
---|
368 | * defchar [in] Default character to use for conversion if no exact
|
---|
369 | * conversion can be made
|
---|
370 | * used [out] Set if default character was used in the conversion
|
---|
371 | *
|
---|
372 | * NOTES
|
---|
373 | * The returned length includes the null terminator character.
|
---|
374 | *
|
---|
375 | * RETURNS
|
---|
376 | * Success: If dstlen > 0, number of characters written to destination
|
---|
377 | * buffer. If dstlen == 0, number of characters needed to do
|
---|
378 | * conversion.
|
---|
379 | * Failure: 0. Occurs if not enough space is available.
|
---|
380 | *
|
---|
381 | * ERRORS
|
---|
382 | * ERROR_INSUFFICIENT_BUFFER
|
---|
383 | * ERROR_INVALID_PARAMETER
|
---|
384 | */
|
---|
385 | INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
|
---|
386 | LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
|
---|
387 | {
|
---|
388 | const union cptable *table;
|
---|
389 | int ret, used_tmp;
|
---|
390 |
|
---|
391 | #ifdef __WIN32OS2__
|
---|
392 | dprintf2(("WideCharToMultiByte %d %x %x %d %x %d", page, flags, src, srclen, dst, dstlen));
|
---|
393 | #endif
|
---|
394 |
|
---|
395 | //Docs say source ptr can't be the same as destination (Windows ME, NT4-SP6)
|
---|
396 | if (!src || (!dst && dstlen) || ((void *)src == (void *)dst))
|
---|
397 | {
|
---|
398 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
399 | return 0;
|
---|
400 | }
|
---|
401 |
|
---|
402 | //Even though the docs claim this only works for -1, testing shows it
|
---|
403 | //is done for any negative value (Windows ME, NT4 - SP6)
|
---|
404 | if (srclen <= -1) srclen = strlenW(src) + 1;
|
---|
405 |
|
---|
406 | switch(page)
|
---|
407 | {
|
---|
408 | case CP_UTF7:
|
---|
409 | FIXME("UTF-7 not supported\n");
|
---|
410 | SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
|
---|
411 | return 0;
|
---|
412 | case CP_UTF8:
|
---|
413 | ret = utf8_wcstombs( src, srclen, dst, dstlen );
|
---|
414 | break;
|
---|
415 | default:
|
---|
416 | if (!(table = get_codepage_table( page )))
|
---|
417 | {
|
---|
418 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
419 | return 0;
|
---|
420 | }
|
---|
421 | ret = cp_wcstombs( table, flags, src, srclen, dst, dstlen,
|
---|
422 | defchar, used ? &used_tmp : NULL );
|
---|
423 | if (used) *used = used_tmp;
|
---|
424 | break;
|
---|
425 | }
|
---|
426 |
|
---|
427 | if (ret == -1)
|
---|
428 | {
|
---|
429 | SetLastError( ERROR_INSUFFICIENT_BUFFER );
|
---|
430 | ret = 0;
|
---|
431 | }
|
---|
432 | return ret;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | /******************************************************************************
|
---|
437 | * GetStringTypeW (KERNEL32)
|
---|
438 | *
|
---|
439 | */
|
---|
440 | BOOL WINAPI GetStringTypeW( DWORD type, LPCWSTR src, INT count, LPWORD chartype )
|
---|
441 | {
|
---|
442 | #ifdef __WIN32OS2__
|
---|
443 | dprintf(("GetStringTypeW %x %x %d %x", type, src, count, chartype));
|
---|
444 | #endif
|
---|
445 |
|
---|
446 | if (count == -1) count = strlenW(src) + 1;
|
---|
447 | switch(type)
|
---|
448 | {
|
---|
449 | case CT_CTYPE1:
|
---|
450 | while (count--) *chartype++ = get_char_typeW( *src++ ) & 0xfff;
|
---|
451 | break;
|
---|
452 | case CT_CTYPE2:
|
---|
453 | while (count--) *chartype++ = get_char_typeW( *src++ ) >> 12;
|
---|
454 | break;
|
---|
455 | case CT_CTYPE3:
|
---|
456 | FIXME("CT_CTYPE3 not supported.\n");
|
---|
457 | default:
|
---|
458 | SetLastError( ERROR_INVALID_PARAMETER );
|
---|
459 | return FALSE;
|
---|
460 | }
|
---|
461 | return TRUE;
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | /******************************************************************************
|
---|
466 | * GetStringTypeExW (KERNEL32)
|
---|
467 | */
|
---|
468 | BOOL WINAPI GetStringTypeExW( LCID locale, DWORD type, LPCWSTR src, INT count, LPWORD chartype )
|
---|
469 | {
|
---|
470 | /* locale is ignored for Unicode */
|
---|
471 | return GetStringTypeW( type, src, count, chartype );
|
---|
472 | }
|
---|
473 |
|
---|
474 | WCHAR WIN32API tolowerW( WCHAR ch )
|
---|
475 | {
|
---|
476 | extern const WCHAR casemap_lower[];
|
---|
477 | return ch + casemap_lower[casemap_lower[ch >> 8] + (ch & 0xff)];
|
---|
478 | }
|
---|
479 |
|
---|
480 | WCHAR WIN32API toupperW( WCHAR ch )
|
---|
481 | {
|
---|
482 | extern const WCHAR casemap_upper[];
|
---|
483 | return ch + casemap_upper[casemap_upper[ch >> 8] + (ch & 0xff)];
|
---|
484 | }
|
---|
485 |
|
---|
486 | /* the character type contains the C1_* flags in the low 12 bits */
|
---|
487 | /* and the C2_* type in the high 4 bits */
|
---|
488 | unsigned short get_char_typeW( WCHAR ch )
|
---|
489 | {
|
---|
490 | extern const unsigned short wctype_table[];
|
---|
491 | return wctype_table[wctype_table[ch >> 8] + (ch & 0xff)];
|
---|
492 | }
|
---|