Implemented GetCPInfoExA/W.
[wine] / memory / codepage.c
1 /*
2  * Code page functions
3  *
4  * Copyright 2000 Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <assert.h>
22 #include <stdarg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winnls.h"
31 #include "wine/unicode.h"
32 #include "thread.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(string);
36
37 /* current code pages */
38 static const union cptable *ansi_cptable;
39 static const union cptable *oem_cptable;
40 static const union cptable *mac_cptable;
41 static const union cptable *unix_cptable;  /* NULL if UTF8 */
42 static LCID default_lcid = MAKELCID( MAKELANGID(LANG_ENGLISH,SUBLANG_DEFAULT), SORT_DEFAULT );
43
44 /* setup default codepage info before we can get at the locale stuff */
45 static void init_codepages(void)
46 {
47     ansi_cptable = wine_cp_get_table( 1252 );
48     oem_cptable  = wine_cp_get_table( 437 );
49     mac_cptable  = wine_cp_get_table( 10000 );
50     unix_cptable  = wine_cp_get_table( 28591 );
51     assert( ansi_cptable );
52     assert( oem_cptable );
53     assert( mac_cptable );
54     assert( unix_cptable );
55 }
56
57 /* find the table for a given codepage, handling CP_ACP etc. pseudo-codepages */
58 static const union cptable *get_codepage_table( unsigned int codepage )
59 {
60     const union cptable *ret = NULL;
61
62     if (!ansi_cptable) init_codepages();
63
64     switch(codepage)
65     {
66     case CP_ACP:
67         return ansi_cptable;
68     case CP_OEMCP:
69         return oem_cptable;
70     case CP_MACCP:
71         return mac_cptable;
72     case CP_UTF7:
73     case CP_UTF8:
74         break;
75     case CP_THREAD_ACP:
76         if (!(codepage = NtCurrentTeb()->code_page)) return ansi_cptable;
77         /* fall through */
78     default:
79         if (codepage == ansi_cptable->info.codepage) return ansi_cptable;
80         if (codepage == oem_cptable->info.codepage) return oem_cptable;
81         if (codepage == mac_cptable->info.codepage) return mac_cptable;
82         ret = wine_cp_get_table( codepage );
83         break;
84     }
85     return ret;
86 }
87
88 /* initialize default code pages from locale info */
89 /* FIXME: should be done in init_codepages, but it can't right now */
90 /* since it needs KERNEL32 to be loaded for the locale info. */
91 void CODEPAGE_Init( UINT ansi_cp, UINT oem_cp, UINT mac_cp, UINT unix_cp, LCID lcid )
92 {
93     extern void __wine_init_codepages( const union cptable *ansi_cp, const union cptable *oem_cp );
94     const union cptable *table;
95
96     default_lcid = lcid;
97     if (!ansi_cptable) init_codepages();  /* just in case */
98
99     if ((table = wine_cp_get_table( ansi_cp ))) ansi_cptable = table;
100     if ((table = wine_cp_get_table( oem_cp ))) oem_cptable = table;
101     if ((table = wine_cp_get_table( mac_cp ))) mac_cptable = table;
102     if (unix_cp == CP_UTF8)
103         unix_cptable = NULL;
104     else if ((table = wine_cp_get_table( unix_cp )))
105         unix_cptable = table;
106
107     __wine_init_codepages( ansi_cptable, oem_cptable );
108
109     TRACE( "ansi=%03d oem=%03d mac=%03d unix=%03d\n",
110            ansi_cptable->info.codepage, oem_cptable->info.codepage,
111            mac_cptable->info.codepage, unix_cp );
112 }
113
114 /******************************************************************************
115  *              GetACP   (KERNEL32.@)
116  *
117  * RETURNS
118  *    Current ANSI code-page identifier, default if no current defined
119  */
120 UINT WINAPI GetACP(void)
121 {
122     if (!ansi_cptable) return 1252;
123     return ansi_cptable->info.codepage;
124 }
125
126
127 /***********************************************************************
128  *              GetOEMCP   (KERNEL32.@)
129  */
130 UINT WINAPI GetOEMCP(void)
131 {
132     if (!oem_cptable) return 437;
133     return oem_cptable->info.codepage;
134 }
135
136
137 /***********************************************************************
138  *           IsValidCodePage   (KERNEL32.@)
139  */
140 BOOL WINAPI IsValidCodePage( UINT codepage )
141 {
142     switch(codepage) {
143     case CP_SYMBOL:
144         return FALSE;
145     case CP_UTF7:
146     case CP_UTF8:
147         return TRUE;
148     default:
149         return wine_cp_get_table( codepage ) != NULL;
150     }
151 }
152
153
154 /***********************************************************************
155  *              GetUserDefaultLangID (KERNEL32.@)
156  */
157 LANGID WINAPI GetUserDefaultLangID(void)
158 {
159     return LANGIDFROMLCID(default_lcid);
160 }
161
162
163 /***********************************************************************
164  *              GetSystemDefaultLangID (KERNEL32.@)
165  */
166 LANGID WINAPI GetSystemDefaultLangID(void)
167 {
168     return GetUserDefaultLangID();
169 }
170
171
172 /***********************************************************************
173  *              GetUserDefaultLCID (KERNEL32.@)
174  */
175 LCID WINAPI GetUserDefaultLCID(void)
176 {
177     return default_lcid;
178 }
179
180
181 /***********************************************************************
182  *              GetSystemDefaultLCID (KERNEL32.@)
183  */
184 LCID WINAPI GetSystemDefaultLCID(void)
185 {
186     return GetUserDefaultLCID();
187 }
188
189
190 /***********************************************************************
191  *              GetUserDefaultUILanguage (KERNEL32.@)
192  */
193 LANGID WINAPI GetUserDefaultUILanguage(void)
194 {
195     return GetUserDefaultLangID();
196 }
197
198
199 /***********************************************************************
200  *              GetSystemDefaultUILanguage (KERNEL32.@)
201  */
202 LANGID WINAPI GetSystemDefaultUILanguage(void)
203 {
204     return GetSystemDefaultLangID();
205 }
206
207
208 /***********************************************************************
209  *           IsDBCSLeadByteEx   (KERNEL32.@)
210  *
211  * Determine if a character is a lead byte in a given code page.
212  *
213  * PARAMS
214  *  codepage [I] Code page for the test.
215  *  testchar [I] Character to test
216  *
217  * RETURNS
218  *  TRUE, if testchar is a lead byte in codepage,
219  *  FALSE otherwise.
220  */
221 BOOL WINAPI IsDBCSLeadByteEx( UINT codepage, BYTE testchar )
222 {
223     const union cptable *table = get_codepage_table( codepage );
224     return table && is_dbcs_leadbyte( table, testchar );
225 }
226
227
228 /***********************************************************************
229  *           IsDBCSLeadByte   (KERNEL32.@)
230  *           IsDBCSLeadByte   (KERNEL.207)
231  *
232  * Determine if a character is a lead byte.
233  *
234  * PARAMS
235  *  testchar [I] Character to test
236  *
237  * RETURNS
238  *  TRUE, if testchar is a lead byte in the Ansii code page,
239  *  FALSE otherwise.
240  */
241 BOOL WINAPI IsDBCSLeadByte( BYTE testchar )
242 {
243     if (!ansi_cptable) return FALSE;
244     return is_dbcs_leadbyte( ansi_cptable, testchar );
245 }
246
247
248 /***********************************************************************
249  *           GetCPInfo   (KERNEL32.@)
250  *
251  * Get information about a code page.
252  *
253  * PARAMS
254  *  codepage [I] Code page number
255  *  cpinfo   [O] Destination for code page information
256  *
257  * RETURNS
258  *  Success: TRUE. cpinfo is updated with the information about codepage.
259  *  Failure: FALSE, if codepage is invalid or cpinfo is NULL.
260  */
261 BOOL WINAPI GetCPInfo( UINT codepage, LPCPINFO cpinfo )
262 {
263     const union cptable *table = get_codepage_table( codepage );
264
265     if (!table)
266     {
267         SetLastError( ERROR_INVALID_PARAMETER );
268         return FALSE;
269     }
270     if (table->info.def_char & 0xff00)
271     {
272         cpinfo->DefaultChar[0] = table->info.def_char & 0xff00;
273         cpinfo->DefaultChar[1] = table->info.def_char & 0x00ff;
274     }
275     else
276     {
277         cpinfo->DefaultChar[0] = table->info.def_char & 0xff;
278         cpinfo->DefaultChar[1] = 0;
279     }
280     if ((cpinfo->MaxCharSize = table->info.char_size) == 2)
281         memcpy( cpinfo->LeadByte, table->dbcs.lead_bytes, sizeof(cpinfo->LeadByte) );
282     else
283         cpinfo->LeadByte[0] = cpinfo->LeadByte[1] = 0;
284
285     return TRUE;
286 }
287
288 /***********************************************************************
289  *           GetCPInfoExA   (KERNEL32.@)
290  *
291  * Get extended information about a code page.
292  *
293  * PARAMS
294  *  codepage [I] Code page number
295  *  dwFlags  [I] Reserved, must to 0.
296  *  cpinfo   [O] Destination for code page information
297  *
298  * RETURNS
299  *  Success: TRUE. cpinfo is updated with the information about codepage.
300  *  Failure: FALSE, if codepage is invalid or cpinfo is NULL.
301  */
302 BOOL WINAPI GetCPInfoExA( UINT codepage, DWORD dwFlags, LPCPINFOEXA cpinfo )
303 {
304     const union cptable *table = get_codepage_table( codepage );
305
306     if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
307       return FALSE;
308
309     cpinfo->CodePage = codepage;
310     cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
311     strcpy(cpinfo->CodePageName, table->info.name);
312     return TRUE;
313 }
314
315 /***********************************************************************
316  *           GetCPInfoExW   (KERNEL32.@)
317  *
318  * Unicode version of GetCPInfoExA.
319  */
320 BOOL WINAPI GetCPInfoExW( UINT codepage, DWORD dwFlags, LPCPINFOEXW cpinfo )
321 {
322     const union cptable *table = get_codepage_table( codepage );
323
324     if (!GetCPInfo( codepage, (LPCPINFO)cpinfo ))
325       return FALSE;
326
327     cpinfo->CodePage = codepage;
328     cpinfo->UnicodeDefaultChar = table->info.def_unicode_char;
329     MultiByteToWideChar( CP_ACP, 0, table->info.name, -1, cpinfo->CodePageName,
330                          sizeof(cpinfo->CodePageName)/sizeof(WCHAR));
331     return TRUE;
332 }
333
334 /***********************************************************************
335  *              EnumSystemCodePagesA   (KERNEL32.@)
336  */
337 BOOL WINAPI EnumSystemCodePagesA( CODEPAGE_ENUMPROCA lpfnCodePageEnum, DWORD flags )
338 {
339     const union cptable *table;
340     char buffer[10];
341     int index = 0;
342
343     for (;;)
344     {
345         if (!(table = wine_cp_enum_table( index++ ))) break;
346         sprintf( buffer, "%d", table->info.codepage );
347         if (!lpfnCodePageEnum( buffer )) break;
348     }
349     return TRUE;
350 }
351
352
353 /***********************************************************************
354  *              EnumSystemCodePagesW   (KERNEL32.@)
355  */
356 BOOL WINAPI EnumSystemCodePagesW( CODEPAGE_ENUMPROCW lpfnCodePageEnum, DWORD flags )
357 {
358     const union cptable *table;
359     WCHAR buffer[10], *p;
360     int page, index = 0;
361
362     for (;;)
363     {
364         if (!(table = wine_cp_enum_table( index++ ))) break;
365         p = buffer + sizeof(buffer)/sizeof(WCHAR);
366         *--p = 0;
367         page = table->info.codepage;
368         do
369         {
370             *--p = '0' + (page % 10);
371             page /= 10;
372         } while( page );
373         if (!lpfnCodePageEnum( p )) break;
374     }
375     return TRUE;
376 }
377
378
379 /***********************************************************************
380  *              MultiByteToWideChar   (KERNEL32.@)
381  *
382  * Convert a multibyte character string into a Unicode string.
383  *
384  * PARAMS
385  *   page   [I] Codepage character set to convert from
386  *   flags  [I] Character mapping flags
387  *   src    [I] Source string buffer
388  *   srclen [I] Length of src, or -1 if src is NUL terminated
389  *   dst    [O] Destination buffer
390  *   dstlen [I] Length of dst, or 0 to compute the required length
391  *
392  * RETURNS
393  *   Success: If dstlen > 0, the number of characters written to dst.
394  *            If dstlen == 0, the number of characters needed to perform the
395  *            conversion. In both cases the count includes the terminating NUL.
396  *   Failure: 0. Use GetLastError() to determine the cause. Possible errors are
397  *            ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
398  *            and dstlen != 0; ERROR_INVALID_PARAMETER,  if an invalid parameter
399  *            is passed, and ERROR_NO_UNICODE_TRANSLATION if no translation is
400  *            possible for src.
401  */
402 INT WINAPI MultiByteToWideChar( UINT page, DWORD flags, LPCSTR src, INT srclen,
403                                 LPWSTR dst, INT dstlen )
404 {
405     const union cptable *table;
406     int ret;
407
408     if (!src || (!dst && dstlen))
409     {
410         SetLastError( ERROR_INVALID_PARAMETER );
411         return 0;
412     }
413
414     if (srclen < 0) srclen = strlen(src) + 1;
415
416     if (flags & MB_USEGLYPHCHARS) FIXME("MB_USEGLYPHCHARS not supported\n");
417
418     switch(page)
419     {
420     case CP_UTF7:
421         FIXME("UTF-7 not supported\n");
422         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
423         return 0;
424     case CP_UNIXCP:
425         if (unix_cptable)
426         {
427             ret = wine_cp_mbstowcs( unix_cptable, flags, src, srclen, dst, dstlen );
428             break;
429         }
430         /* fall through */
431     case CP_UTF8:
432         ret = wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
433         break;
434     default:
435         if (!(table = get_codepage_table( page )))
436         {
437             SetLastError( ERROR_INVALID_PARAMETER );
438             return 0;
439         }
440         ret = wine_cp_mbstowcs( table, flags, src, srclen, dst, dstlen );
441         break;
442     }
443
444     if (ret < 0)
445     {
446         switch(ret)
447         {
448         case -1: SetLastError( ERROR_INSUFFICIENT_BUFFER ); break;
449         case -2: SetLastError( ERROR_NO_UNICODE_TRANSLATION ); break;
450         }
451         ret = 0;
452     }
453     return ret;
454 }
455
456
457 /***********************************************************************
458  *              WideCharToMultiByte   (KERNEL32.@)
459  *
460  * Convert a Unicode character string into a multibyte string.
461  *
462  * PARAMS
463  *   page    [I] Code page character set to convert to
464  *   flags   [I] Mapping Flags (MB_ constants from "winnls.h").
465  *   src     [I] Source string buffer
466  *   srclen  [I] Length of src, or -1 if src is NUL terminated
467  *   dst     [O] Destination buffer
468  *   dstlen  [I] Length of dst, or 0 to compute the required length
469  *   defchar [I] Default character to use for conversion if no exact
470  *                  conversion can be made
471  *   used    [O] Set if default character was used in the conversion
472  *
473  * RETURNS
474  *   Success: If dstlen > 0, the number of characters written to dst.
475  *            If dstlen == 0, number of characters needed to perform the
476  *            conversion. In both cases the count includes the terminating NUL.
477  *   Failure: 0. Use GetLastError() to determine the cause. Possible errors are
478  *            ERROR_INSUFFICIENT_BUFFER, if not enough space is available in dst
479  *            and dstlen != 0, and ERROR_INVALID_PARAMETER, if an invalid
480  *            parameter was given.
481  */
482 INT WINAPI WideCharToMultiByte( UINT page, DWORD flags, LPCWSTR src, INT srclen,
483                                 LPSTR dst, INT dstlen, LPCSTR defchar, BOOL *used )
484 {
485     const union cptable *table;
486     int ret, used_tmp;
487
488     if (!src || (!dst && dstlen))
489     {
490         SetLastError( ERROR_INVALID_PARAMETER );
491         return 0;
492     }
493
494     if (srclen < 0) srclen = strlenW(src) + 1;
495
496     switch(page)
497     {
498     case CP_UTF7:
499         FIXME("UTF-7 not supported\n");
500         SetLastError( ERROR_CALL_NOT_IMPLEMENTED );
501         return 0;
502     case CP_UNIXCP:
503         if (unix_cptable)
504         {
505             ret = wine_cp_wcstombs( unix_cptable, flags, src, srclen, dst, dstlen,
506                                     defchar, used ? &used_tmp : NULL );
507             break;
508         }
509         /* fall through */
510     case CP_UTF8:
511         ret = wine_utf8_wcstombs( src, srclen, dst, dstlen );
512         break;
513     default:
514         if (!(table = get_codepage_table( page )))
515         {
516             SetLastError( ERROR_INVALID_PARAMETER );
517             return 0;
518         }
519         ret = wine_cp_wcstombs( table, flags, src, srclen, dst, dstlen,
520                                 defchar, used ? &used_tmp : NULL );
521         if (used) *used = used_tmp;
522         break;
523     }
524
525     if (ret == -1)
526     {
527         SetLastError( ERROR_INSUFFICIENT_BUFFER );
528         ret = 0;
529     }
530     return ret;
531 }